summaryrefslogtreecommitdiff
path: root/app/relay/modules/pix2pixhd.js
blob: e09b67f69242bea439329348b24caee510a84f7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import path from 'path'
import fs from 'fs'

const name = 'pix2pixhd'
const cwd = process.env.PIX2PIXHD_CWD || path.join(process.env.HOME, 'code/' + name + '/')
const env = {
  LD_LIBRARY_PATH: '/usr/local/cuda/lib64:' + process.env.HOME + '/Downloads/TensorRT-4.0.0.3/lib',
}

const fetch = {
  type: 'perl',
  script: 'get.pl',
  params: (task) => {
    console.log(task)
    return [ task.opt.url ]
  },
  listen: (task, res, i) => {
    // relay the new dataset name from youtube-dl or w/e
    const lines = res.split('\n')
    for (let line of lines) {
      // console.log(line)
      if ( line.match(/^created dataset: /) ) {
        let tag = line.split(': ')[1].trim()
        task.dataset = tag
        // task.opt.filename = filename
        console.log(">>>>>> created dataset", tag)
        return { type: 'progress', action: 'resolve_dataset', task }
      }
    }
    return null
  },
  after: 'build',
}
const build = {
  type: 'perl',
  script: 'build_dataset.pl',
  params: (task) => {
    return [
      task.dataset,
    ]
  }
}
const train = {
  type: 'pytorch',
  script: 'train.py',
  params: (task) => {
    let epoch = 0
    const dataset = task.dataset.toLowerCase()
    const datasets_path = path.join(cwd, 'datasets', dataset)
    const checkpoints_path = path.join(cwd, 'checkpoints', dataset)
    const iter_txt = path.join(checkpoints_path, 'iter.txt')
    const checkpoint_path = path.join(checkpoints_path, 'latest_net_G.pth')
    console.log(dataset, iter_txt)
    if (fs.existsSync(iter_txt) && fs.existsSync(checkpoint_path)) {
      const iter = fs.readFileSync(iter_txt).toString().split('\n');
      console.log(iter)
      epoch = iter[0] || 0
      console.log(task.module, dataset, '=>', epoch, task.epochs)
    } else {
      console.log(task.module, dataset, '=>', 'starting new training')
    }
    let args = [
      '--dataroot', datasets_path,
      '--module_name', task.module,
      '--name', dataset,
      '--model', 'pix2pixHD',
      '--label_nc', 0, '--no_instance',
      '--niter', task.epochs || 1,
      '--niter_decay', 0,
      '--save_epoch_freq', 1,
    ]
    if (epoch) {
      args = args.concat([
        '--which_epoch', 'latest',
        '--continue_train',
      ])
    }
    return args
  },
  listen: (task, res, i) => {
    console.log(res)
    return null
  },
}
const generate = {
  type: 'pytorch',
  script: 'test.py',
  params: (task) => {
    let epoch = 0
    const dataset = task.dataset.toLowerCase()
    const datasets_path = path.join(cwd, 'datasets', dataset)
    const checkpoints_path = path.join(cwd, 'checkpoints', dataset)
    const iter_txt = path.join(checkpoints_path, 'iter.txt')
    console.log(dataset, iter_txt)
    if (fs.existsSync(iter_txt)) {
      const iter = fs.readFileSync(iter_txt).toString().split('\n');
      console.log(iter)
      epoch = iter[0] || 0
      console.log(task.module, dataset, '=>', epoch, task.epochs)
    } else {
      console.log(task.module, dataset, '=>', 'starting new training')
    }
    return [
      '--dataroot', datasets_path,
      '--module_name', task.module,
      '--name', dataset,
      '--model', 'pix2pixHD',
      '--label_nc', 0, '--no_instance',
      '--niter', task.epochs,
      '--niter_decay', 0,
      '--save_epoch_freq', 1,
    ]
  },
  after: 'render',
}
const augment = {
  type: 'pytorch',
  script: 'augment.py',
  params: (task) => {
    let epoch = 0
    const dataset = task.dataset.toLowerCase()
    const datasets_path = path.join(cwd, 'datasets', dataset)
    const checkpoints_path = path.join(cwd, 'checkpoints', dataset)
    // supply render_dir
    let args = [
      '--dataroot', datasets_path,
      '--results_dir', './recursive',
      '--module_name', task.module,
      '--name', dataset,
      '--sequence-name', dataset,
      '--model', 'pix2pixHD',
      '--label_nc', 0, '--no_instance',
      '--augment-take', task.opt.augment_take,
      '--augment-make', task.opt.augment_make,
      '--which_epoch', task.opt.epoch || "latest",
    ]
    if (!!task.opt.augment_name) {
      args.push('--augment-name', task.opt.augment_name)
    }
    if (!!task.opt.no_symlinks) {
      args.push('--no-symlinks')
    }
    return args
  },
  listen: (task, res, i) => {
    // extract the new path name
    const lines = res.split('\n')
    for (let line of lines) {
      console.log(line)
      if ( line.match(/^render_dir: /) ) {
        let tag = line.split(': ')[1].trim()
        task.opt.render_dir = tag
        // task.opt.filename = filename
        console.log(">>>>>> created dataset", tag)
        return { type: 'progress', action: 'resolve_dataset', task }
      }
    }
    return null
  },
  after: 'render_recursive',
}
const live = {
  type: 'pytorch',
  script: 'live.py',
  params: (task) => {
    console.log(task)
    const opt = task.opt || {}
    return [
      '--phase', 'recursive',
      '--dataroot', path.join(cwd, 'sequences', task.dataset),
      '--start_img', path.join(cwd, 'sequences', task.dataset, 'frame_00001.png'),
      '--checkpoint-name', task.checkpoint,
      '--experiment', task.checkpoint,
      '--name', task.checkpoint,
      '--module_name', 'pix2pixHD',
      '--sequence-name', task.dataset,
      '--sequence', '--sequence-frac', 0.3,
      '--process-frac', 0.5,
      '--label_nc', '0', '--no_instance',
      '--how_many', 10000, '--transition-period', 1000,
      '--just-copy', '--poll_delay', opt.poll_delay || 0.09,
      '--which_epoch', 'latest',
      '--norm', 'batch',
      '--store_b', // uncomment this line to store all live output
    ]
  },
  listen: (task, res, i) => {
    // relay the new dataset name from youtube-dl or w/e
    const lines = res.split('\n')
    for (let line of lines) {
      // console.log(line)
      if ( line.match(/^final result: /) ) {
        let tag = line.split(': ')[1].trim()
        task.dataset = tag
        console.log(">>>>>> recording live to", tag)
        return { type: 'progress', action: 'resolve_dataset', task }
      }
    }
    return null
  },
  after: 'render',
}
const render = {
  type: 'perl',
  script: 'dir-to-movie.pl',
  params: (task) => {
    return [
      '--path', 'results',
      '--tag', task.dataset,
      '--module', task.module,
      '--endpoint', process.env.API_REMOTE + '/api/folder/' + task.opt.folder_id + '/upload/',
    ]
  }
}
const render_recursive = {
  type: 'perl',
  script: 'dir-to-movie.pl',
  params: (task) => {
    if (!task.opt.mov) {
      console.log('will not render mov')
      return 'CANCEL'
    }
    if (!task.opt.render_dir) {
      return 'CANCEL';
    }
    const render_dir = task.opt.render_dir.replace('./recursive/', '')
    console.log('rendering recursve path:', render_dir)
    return [
      '--path', 'recursive',
      '--tag', render_dir,
      '--module', task.module,
      '--endpoint', process.env.API_REMOTE + '/api/folder/' + task.opt.folder_id + '/upload/',
      '--prefix', 'recur',
    ]
  }
}
const splice = {
  type: 'perl',
  script: 'splice.pl',
  params: (task) => {
    console.log(task.opt.selection)
    return [
      '--dataset', task.dataset,
      '--sequence', task.opt.sequence,
      '--start_frame', task.opt.selection.start.i,
      '--end_frame', task.opt.selection.end.i,
      '--module', task.module,
      '--folder_id', task.opt.folder_id,
      '--endpoint', process.env.API_REMOTE + '/api/file/',
    ]
  },
  after: 'train'
}

function pad(num, size) {
  if (isNaN(parseInt(num))) return num;
  var s = num + "";
  while (s.length < size) s = "0" + s;
  return s;
}

export default {
  name, cwd, env,
  activities: {
    fetch,
    build,
    train,
    generate,
    augment,
    live,
    render,
    render_recursive,
    splice,
  }
}