summaryrefslogtreecommitdiff
path: root/app/client/modules/pix2pixhd/views/pix2pixhd.train.js
blob: 691aeddade706aa92c3d20c677def540cd53ea64 (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
276
277
278
279
280
281
282
import { h, Component } from 'preact'
import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'
import util from '../../../util'

import * as pix2pixhdActions from '../pix2pixhd.actions'
import * as pix2pixhdTasks from '../pix2pixhd.tasks'

import {
  Loading,
  FileList, FileRow,
  Select, SelectGroup, Group, Param, Button,
  TextInput, NumberInput,
  CurrentTask, TaskList,
  AugmentationGrid,
} from '../../../common'
import DatasetForm from '../../../dataset/dataset.form'
import NewDatasetForm from '../../../dataset/dataset.new'
import UploadStatus from '../../../dataset/upload.status'

import DatasetComponent from '../../../dataset/dataset.component'
import SequenceEditor from './sequence.editor'

import pix2pixhdModule from '../pix2pixhd.module'

class Pix2PixHDTrain extends Component {
  state = {
    dataset: 'PLACEHOLDER',
    epoch: 'latest',
    augment_name: '',
    augment_take: 100,
    augment_make: 20,
    processed: false,
  }
  constructor(props){
    super(props)
    this.handleChange = this.handleChange.bind(this)
  }
  componentDidMount(){
    const id = this.props.match.params.id || localStorage.getItem('pix2pixhd.last_id')
    const dataset = localStorage.getItem('pix2pixhd.last_dataset')
    console.log('load dataset:', id, dataset)
    const { match, pix2pixhd, actions } = this.props
    if (id === 'new') return
    if (id) {
      if (parseInt(id)) localStorage.setItem('pix2pixhd.last_id', id)
      if (! pix2pixhd.folder || pix2pixhd.folder.id !== id) {
        actions.load_directories(id)
      }
    } else {
      this.props.history.push('/pix2pixhd/new/')
    }
    if (dataset) {
      this.setState({ dataset })
    }
  }
  componentDidUpdate(prevProps, prevState){
    if ((!prevProps.pix2pixhd.data && this.props.pix2pixhd.data)
      || (prevProps.pix2pixhd.data && prevState.dataset !== this.state.dataset)) {
      const dataset = this.props.pix2pixhd.data.datasetLookup[this.state.dataset]
      if (dataset) {
        const processed = dataset.input
          .map(f => this.props.pix2pixhd.data.fileLookup[f])
          .reduce((a, b) => {
            console.log(b)
            return b.processed || a
          }, false)
        console.log(dataset.input, processed)
        dataset.processed = processed
        this.setState({ processed })
      }
    }
    if (prevState.dataset !== this.state.dataset) {
      localStorage.setItem('pix2pixhd.last_dataset', this.state.dataset)
      this.setState({
        epoch: 'latest',
      })
      this.props.actions.list_epochs(this.state.dataset)
      this.props.actions.count_dataset(this.state.dataset)
    }
  }
  handleChange(name, value){
    console.log('name', name, 'value', value)
    this.setState({ [name]: value })
  }
  interrupt(){
    this.props.actions.queue.stop_task('gpu')
  }
  render(){
    if (this.props.pix2pixhd.loading) {
      return <Loading progress={this.props.pix2pixhd.progress} />
    }
    const { pix2pixhd, match, history } = this.props
    const { folderLookup, datasetLookup } = (pix2pixhd.data || {})
    const folder = (folderLookup || {})[pix2pixhd.folder_id] || {}
    const { checkpoint } = pix2pixhd
    // console.log(pix2pixhd)

    const sequenceGroups = Object.keys(folderLookup).map(id => {
      const folder = this.props.pix2pixhd.data.folderLookup[id]
      if (folder.name === 'results') return
      const datasets = folder.datasets.map(name => {
        const dataset = datasetLookup[name]
        // console.log(folder)
        if (dataset.isBuilt) { // && dataset.checkpoints.length
          return name
        }
        return null
      }).filter(n => !!n)
      return {
        name: folder.name,
        options: datasets.sort(),
      }
    }).filter(n => !!n && !!n.options.length).sort((a,b) => a.name.localeCompare(b.name))

    let epochs = this.props.pix2pixhd.epochs
    if (!epochs || !epochs.length) epochs = ["latest"]
    // console.log('state', this.props.pix2pixhd.data.epochs)
    // console.log(this.state.dataset, this.state.epoch)
    // console.log(queue)
    return (
      <div className='app pix2pixhd'>
        <div className='heading'>
          <h1>pix2pixhd training</h1>
        </div>
        <div className='columns'>
          <div className='column'>
            <Group title='Sequence'>
              <SelectGroup
                name='dataset'
                title='Sequence name'
                options={sequenceGroups}
                onChange={this.handleChange}
                placeholder='Pick a dataset'
                value={this.state.dataset}
              />
              <Select
                title="Epoch"
                name="epoch"
                options={epochs}
                onChange={this.handleChange}
                value={this.state.epoch}
              />
            </Group>
          </div>
        </div>
        {checkpoint && checkpoint.sequence && checkpoint.sequence.length
          ? this.renderEditor()
          : checkpoint && <div>Sequence empty, augmentation impossible</div>}
      </div>
    )
  }

  renderEditor(){
    const { pix2pixhd, queue, remote } = this.props
    const { dataset, processed } = this.state
    let { checkpoint, folder_id } = pix2pixhd

    const currentDataset = this.props.pix2pixhd.data.datasetLookup[this.state.dataset]
    if (currentDataset && currentDataset.folder_id) {
      folder_id = currentDataset.folder_id || folder_id
      console.log("dataset folder id", folder_id)
      // const processed = currentDataset.input
      //   .map(f => this.props.pix2pixhd.data.fileLookup[f])
      //   .reduce((a, b) => {
      //     console.log(b)
      //     return b.processed || a
      //   }, false)
      // console.log("is processed?", processed)
    }
    console.log(currentDataset)
    console.log("so folder id is..", folder_id)

    return (
      <div>
        <Group title='Sequence Editor'>
          <SequenceEditor
            folder_id={folder_id}
            module={pix2pixhdModule}
            checkpoint={checkpoint}
            processed={processed}
          />
        </Group>
        <div className='columns'>
          <div className='column'>
            <Group title='Augmentation Grid'>
              <AugmentationGrid
                checkpoint={checkpoint}
                take={[1,2,3,4,5,10,15,20,25,50,75,100,200,300,400,500,1000]}
                make={[1,2,3,4,5,10,15,20,25,50,75,100,200,]}
                onAugment={(augment_take, augment_make) => {
                  remote.augment_task(dataset, {
                    ...this.state,
                    augment_take,
                    augment_make,
                  })
                }}
                onTrain={() => {
                  remote.train_task(dataset, folder_id, 1)
                  setTimeout(() => { // auto-generate epoch demo
                    remote.augment_task(dataset, {
                      ...this.state,
                      augment_take: 10,
                      augment_make: 149,
                      no_symlinks: true,
                      mov: true,
                      folder_id: pix2pixhd.data.resultsFolder.id
                    })
                  }, 250)
                }}
              />
            </Group>
            <Group title='Augment'>
              <NumberInput
                name="augment_take"
                title="Pick N random frames"
                value={this.state.augment_take}
                onChange={this.handleChange}
                type="int"
                min="1"
                max="1000"
              />
              <NumberInput
                name="augment_make"
                title="Generate N recursively"
                value={this.state.augment_make}
                onChange={this.handleChange}
                type="int"
                min="1"
                max="1000"
              />
              <Button
                title="Augment dataset"
                value="Augment"
                onClick={() => this.props.remote.augment_task(dataset, this.state)}
              />
              <Button
                title="Make a movie without augmenting"
                value="Generate"
                onClick={() => {
                  this.props.remote.augment_task(dataset, {
                    ...this.state,
                    no_symlinks: true,
                    mov: true,
                    folder_id: pix2pixhd.data.resultsFolder.id
                  })
                }}
              />
            </Group>

            <Group title='Status'>
              <Button
                title="GPU"
                value={this.props.runner.gpu.status === 'IDLE' ? "Idle" : "Interrupt"}
                onClick={() => this.interrupt()}
              />
              <CurrentTask />
            </Group>
          </div>
          <div className='column'>
            <Group title='Upcoming Tasks'>
              <TaskList tasks={queue.queue.map(id => queue.tasks[id])} sort="date asc" />
            </Group>
          </div>
        </div>
      </div>
    )
  }
}

const mapStateToProps = state => ({
  pix2pixhd: state.module.pix2pixhd,
  runner: state.system.runner,
  queue: state.queue,
})

const mapDispatchToProps = (dispatch, ownProps) => ({
  actions: bindActionCreators(pix2pixhdActions, dispatch),
  remote: bindActionCreators(pix2pixhdTasks, dispatch),
})

export default connect(mapStateToProps, mapDispatchToProps)(Pix2PixHDTrain)