-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.py
156 lines (123 loc) · 6.02 KB
/
main.py
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
import numpy as np
import torch
from torch.autograd import Variable
import torchvision.transforms as transforms
from glob import glob
import scipy.io
import os
import time
from data.database import *
from utils.my_utils import *
from options.options import *
from models.model import KeyPatchGanModel
###############################################################
# Get Options
###############################################################
opts = Options().parse()
###############################################################
# Initialize Database
###############################################################
dataset = Dataset()
dataset.initialize(opts)
# Split train/test data
np.random.seed(opts.random_seed)
all_idx = np.random.permutation(len(dataset))
test_idx = all_idx[-opts.num_tests:]
sample_idx = all_idx[:opts.num_samples]
train_idx = all_idx[:-opts.num_tests]
num_train_imgs = len(train_idx)
###############################################################
# Initialize Model
###############################################################
model = KeyPatchGanModel()
model.initialize(opts)
###############################################################
# Start Training
###############################################################
''' Preparing Test Data '''
# set test images
test_img_paths, test_bbs = dataset[test_idx]
is_flip = False
test_images, test_part1_images, test_part2_images, test_part3_images, test_gt_masks, test_z = \
prepare_data(test_img_paths, test_bbs, is_flip, opts)
''' Preparing Sample Data '''
# set sample images
sample_img_paths, sample_bbs = dataset[sample_idx]
is_flip = False
sample_images, sample_part1_images, sample_part2_images, sample_part3_images, sample_gt_masks, sample_z = \
prepare_data(sample_img_paths, sample_bbs, is_flip, opts)
''' Main Training Loop Here '''
# compcar (4, 2) try !
m_weight_mask = np.logspace(-2, -4, num=opts.epoch)
m_weight_appr = np.logspace(-2, -4, num=opts.epoch)
# m_weight_mask = np.logspace(0, 0, num=opts.epoch)
# m_weight_appr = np.logspace(0, 0, num=opts.epoch)
start_time = time.time()
for epoch in range(opts.epoch):
# shuffle data
curr_epoch_idx = np.random.permutation(num_train_imgs)
curr_train_idx = train_idx[curr_epoch_idx]
num_batches = num_train_imgs // opts.batch_size
for i in range(num_batches):
batch_idx_offset = i * opts.batch_size
batch_train_idx = curr_train_idx[batch_idx_offset:batch_idx_offset+opts.batch_size]
batch_train_other_idx = curr_train_idx[np.setdiff1d(np.arange(len(curr_train_idx)),
np.arange(batch_idx_offset, batch_idx_offset+opts.batch_size))]
batch_shuff_idx = np.random.choice(batch_train_other_idx, size=opts.batch_size)
train_image_paths, train_bbs = dataset[batch_train_idx]
shuff_image_paths, _ = dataset[batch_shuff_idx]
if np.random.rand() > 0.5:
is_flip = True
train_bbs[:, :, 0] = opts.output_size - (train_bbs[:, :, 0] + train_bbs[:, :, 2])
else:
is_flip = False
# load images
train_images, train_part1_images, train_part2_images, train_part3_images, train_gt_masks, train_z = \
prepare_data(train_image_paths, train_bbs, is_flip, opts)
train_shuff_images = [get_image(shuff_image_paths[j], opts.image_size, opts.output_size, opts.is_crop, is_flip) for
j in range(opts.batch_size)]
# Set input images
model.set_inputs_for_train(train_images, train_shuff_images,
train_part1_images, train_part2_images, train_part3_images,
train_z, train_gt_masks, m_weight_mask[epoch],m_weight_appr[epoch])
model.loss = {}
# Train D
model.forward()
model.optimize_parameters_D()
# Train G
if i % 2 == 1:
model.forward()
model.optimize_parameters_G()
if (i % 10 == 1):
print('epoch: %02d/%02d, iter: %04d/%04d, d_loss: %f. g_loss_gan: %f, g_loss_appr: %f, g_loss_mask: %f, %f sec'
% (epoch+1, opts.epoch, i, num_batches, model.d_loss.cpu().data.numpy(),
model.g_loss_gan.cpu().data.numpy(),
model.g_loss_l1_appr.cpu().data.numpy(),
model.g_loss_l1_mask.cpu().data.numpy(),
time.time()-start_time))
if opts.use_tensorboard:
for tag, value in model.loss.items():
model.logger.scalar_summary(tag, value, epoch * num_batches + i)
if (i % 200 == 1):
if opts.use_visdom:
model.set_inputs_for_train(sample_images, sample_images,
sample_part1_images, sample_part2_images, sample_part3_images,
sample_z, sample_gt_masks, m_weight_mask[epoch],m_weight_appr[epoch])
model.forward()
model.visualize(win_offset=0)
model.set_inputs_for_train(test_images, test_images,
test_part1_images, test_part2_images, test_part3_images,
test_z, test_gt_masks, m_weight_mask[epoch],m_weight_appr[epoch])
model.forward()
model.visualize(win_offset=100)
model.set_inputs_for_train(sample_images, sample_images,
sample_part1_images, sample_part2_images, sample_part3_images,
sample_z, sample_gt_masks, m_weight_mask[epoch],m_weight_appr[epoch])
model.forward()
model.save_images(epoch, i, is_test=False)
model.set_inputs_for_train(test_images, test_images,
test_part1_images, test_part2_images, test_part3_images,
test_z, test_gt_masks, m_weight_mask[epoch],m_weight_appr[epoch])
model.forward()
model.save_images(epoch, i, is_test=True)
model.save(epoch)