summaryrefslogtreecommitdiff
path: root/app/relay/modules/pix2pixhd.js
blob: 5b9fd5c0d21afe879f0ac7bbc5fc9d5bd0cc2cec (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
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 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 datasets_path = path.join(cwd, 'datasets', task.dataset)
    const checkpoints_path = path.join(cwd, 'checkpoints', task.dataset)
    if (fs.existsSync(checkpoints_path)) {
      try {
        const checkpoints = fs.readdirSync(checkpoints_path)
        checkpoints.forEach(c => {
          epoch = Math.max(parseInt(c.name) || 0, epoch)
        })
        console.log(task.module, task.dataset, epoch, task.epochs)
      } catch (e) { }
    }
    let args = [
      '--dataroot', datasets_path,
      '--module_name', task.module,
      '--name', task.dataset,
      '--model', 'pix2pixHD',
      '--label_nc', 0, '--no_instance',
      '--niter', task.epochs,
      '--niter_decay', 0,
    ]
    if (epoch) {
      args = args.concat([
        '--epoch_count', task.epoch + task.epochs + 1,
        '--which_epoch', 'latest',
        '--continue_train',
      ])
    }
    return args
  },
}
const generate = {
  type: 'pytorch',
  script: 'test.py',
  params: (task) => {
    return [
      '--dataroot', '/sequences/' + task.dataset,
      '--module_name', task.module,
      '--name', task.dataset,
      '--start_img', '/sequences/' + task.dataset + '/frame_00001.png',
      '--how_many', 1000,
      '--model', 'test',
      '--aspect_ratio', 1.777777,
      '--which_model_netG', 'unet_256',
      '--which_direction', 'AtoB',
      '--dataset_mode', 'test',
      '--loadSize', 256,
      '--fineSize', 256,
      '--norm', 'batch'
    ]
  },
}
const live = {
  type: 'pytorch',
  script: 'live.py',
  params: (task) => {
    console.log(task)
    const opt = task.opt || {}
    return [
      'LD_LIBRARY_PATH=' + process.env.HOME + '/Downloads/TensorRT-3.0.4/lib',
      '--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,
      '--recursive', '--recursive-frac', 0.1,
      '--sequence', '--sequence-frac', 0.3,
      '--process-frac', 0.5,
      '--nThreads', 0,
      '--transition-min', 0.05,
      '--how_many', 1000000, '--transition-period', 1000,
      '--loadSize', 256, '--fineSize', 256,
      '--just-copy', '--poll_delay', opt.poll_delay || 0.09,
      '--model', 'test',
      '--which_model_netG', 'unet_256',
      '--which_direction', 'AtoB',
      '--dataset_mode', 'recursive',
      '--which_epoch', 'latest',
      '--norm', 'batch',
    ]
  },
}

export default {
  name, cwd,
  activities: {
    fetch,
    build,
    train,
    generate,
    live,
  }
}