-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptions.py
285 lines (268 loc) · 9.52 KB
/
options.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
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
283
284
285
import time
import kornia
from tensorboardX import SummaryWriter
from datetime import datetime
import argparse
from models.PRoGAN import PRoGAN
import os
from tensorboardX import SummaryWriter
import matplotlib.pyplot as plt
import importlib
import torch.utils.data
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
from torch.optim.lr_scheduler import StepLR
import numpy as np
from util.util import *
from util.visualizer import Visualizer
def options():
"""
Options for training and validating
"""
desc = "Pytorch implementation of PRoGAN"
parser = argparse.ArgumentParser(description=desc)
parser.add_argument('--display_id',
type=int,
default=10,
help='window id of the web display')
parser.add_argument('--display_server',
type=str,
default="http://localhost",
help='visdom server of the web display')
parser.add_argument(
'--display_env',
type=str,
default='main',
help='visdom display environment name (default is "main")')
parser.add_argument('--display_port',
type=int,
default=8097,
help='visdom port of the web display')
parser.add_argument(
'--update_html_freq',
type=int,
default=500,
help='frequency of saving training results to html')
parser.add_argument(
'--print_freq',
type=int,
default=20,
help='frequency of showing training results on console')
parser.add_argument(
'--no_html',
action='store_true',
help='do not save intermediate training results to [opt.checkpoints_dir]/[opt.name]/web/'
)
parser.add_argument('--save_latest_freq',
type=int,
default=500,
help='frequency of saving the latest results')
parser.add_argument(
'--save_epoch_freq',
type=int,
default=1,
help='frequency of saving checkpoints at the end of epochs')
parser.add_argument(
'--name',
type=str,
default='experiment_name',
help='name of the experiment'
)
parser.add_argument('--gpu_ids',
type=str,
default='0',
help='The ids of gpu to use, gpu ids: e.g. 0 0,1,2, 0,2. use -1 for CPU')
parser.add_argument('--checkpoints_dir',
type=str,
default='./checkpoints',
help='The place to save models')
parser.add_argument(
'--input_nc',
type=int,
default=3,
help='The channel of input image')
parser.add_argument(
'--output_nc',
type=int,
default=3,
help='The channel of output image')
parser.add_argument('--ngf',
type=int,
default=64,
help='# of gen filters in the last conv layer')
parser.add_argument(
'--ndf',
type=int,
default=64,
help='# of discrim filters in the first conv layer')
parser.add_argument(
'--netG',
type=str,
default='resnet_9blocks',
help='specify generator architecture [resnet_9blocks | resnet_6blocks | unet_256 | unet_128]'
)
parser.add_argument('--n_layers_D',
type=int,
default=3,
help='only used if netD==n_layers')
parser.add_argument('--no_dropout',
action='store_true',
help='no dropout for the generator')
parser.add_argument('--num_threads',
default=4,
type=int,
help='# threads for loading data')
parser.add_argument('--batch_size',
type=int,
default=1,
help='The batch size of input')
parser.add_argument('--load_size',
type=int,
default=256,
help='The size of input image for network (128 / 256)')
parser.add_argument(
'--display_winsize',
type=int,
default=256,
help='display window size for both visdom and HTML')
parser.add_argument(
'--epoch',
type=str,
default='latest',
help='The start epoch for continuing to train'
)
parser.add_argument('--save_by_iter',
action='store_true',
help='whether saves model by iteration')
parser.add_argument('--continue_train',
action='store_true',
help='Continue to train')
parser.add_argument(
'--epoch_count',
type=int,
default=1,
help='the starting epoch count, we save the model by <epoch_count>, <epoch_count>+<save_latest_freq>, ...'
)
parser.add_argument('--phase',
type=str,
default='train',
help='Choose to train or validate (train / val)')
parser.add_argument(
'--n_epochs',
type=int,
default=100,
help='number of epochs with the initial learning rate')
parser.add_argument(
'--n_epochs_decay',
type=int,
default=100,
help='number of epochs to linearly decay learning rate to zero')
parser.add_argument('--beta1',
type=float,
default=0.5,
help='momentum term of adam')
parser.add_argument('--lr',
type=float,
default=0.00015,
help='initial learning rate for adam')
parser.add_argument(
'--pool_size',
type=int,
default=0,
help='the size of image buffer that stores previously generated images')
parser.add_argument(
'--lr_decay_iters',
type=int,
default=50,
help='multiply by a gamma every lr_decay_iters iterations')
parser.add_argument('--train_writer_path',
type=str,
default="./checkpoints/log/",
help='Where to write the Log of training')
parser.add_argument('--val_writer_path',
type=str,
default="./outputs/",
help='Where to save the images of validating')
parser.add_argument('--dataset',
type=str,
default="night",
help='dataset')
parser.add_argument('--lambda_L1',
type=float,
default=100.0,
help='weight for L1 loss')
parser.add_argument(
'--display_freq',
type=int,
default=20,
help='frequency of showing training results on screen')
parser.add_argument('--use_scale',
action='store_true',
help='Use scale for transformation')
parser.add_argument('--aeroground',
action='store_true',
help='Use dataset: AeroGround')
parser.add_argument('--carla',
action='store_true',
help='Use dataset: carla')
return parser
def config(opt):
"""The configuration of options"""
str_ids = opt.gpu_ids.split(',')
opt.gpu_ids = []
for str_id in str_ids:
id = int(str_id)
if id >= 0:
opt.gpu_ids.append(id)
if len(opt.gpu_ids) > 0:
torch.cuda.set_device(opt.gpu_ids[0])
if opt.aeroground:
opt.dataset = 'stereo'
opt.netG = 'resnet_6blocks'
opt.load_size = 128
opt.input_nc = 1
opt.output_nc = 1
opt.name = 'aeroground_train'
if opt.carla:
opt.dataset = 'night'
opt.netG = 'unet_256'
opt.load_size = 256
opt.input_nc = 3
opt.output_nc = 3
opt.name = 'carla_train'
if opt.continue_train:
opt.epoch_count = int(opt.epoch) + 1
return opt
def print_options(opt, parser):
"""Print the options for training and validating"""
message = ''
message += '----------------- PRoGAN Options ---------------\n'
for k, v in sorted(vars(opt).items()):
comment = ''
default = parser.get_default(k)
if v != default:
comment = '\t[default: %s]' % str(default)
message += '{:>25}: {:<30}{}\n'.format(str(k), str(v), comment)
message += '----------------- End -------------------'
print(message)
# save to the disk
expr_dir = os.path.join(opt.checkpoints_dir, opt.name)
mkdirs(expr_dir)
file_name = os.path.join(expr_dir, '{}_opt.txt'.format(opt.phase))
with open(file_name, 'wt') as opt_file:
opt_file.write(message)
opt_file.write('\n')
def print_current_losses(args, epoch, iters, losses, t_comp, t_data):
log_name = os.path.join(args.checkpoints_dir, args.name,
'loss_log.txt')
message = '(epoch: %d, iters: %d, time: %.3f, data: %.3f) ' % (
epoch, iters, t_comp, t_data)
for k, v in losses.items():
message += '%s: %.3f ' % (k, v)
print(message) # print the message
print('\n')
with open(log_name, "a") as log_file:
log_file.write('%s\n' % message) # save the message