-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
histoGAN.py
305 lines (286 loc) · 11.7 KB
/
histoGAN.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
"""
If you find this code useful, please cite our paper:
Mahmoud Afifi, Marcus A. Brubaker, and Michael S. Brown. "HistoGAN:
Controlling Colors of GAN-Generated and Real Images via Color Histograms."
In CVPR, 2021.
@inproceedings{afifi2021histogan,
title={Histo{GAN}: Controlling Colors of {GAN}-Generated and Real Images via
Color Histograms},
author={Afifi, Mahmoud and Brubaker, Marcus A. and Brown, Michael S.},
booktitle={CVPR},
year={2021}
}
"""
from tqdm import tqdm
from histoGAN import Trainer, NanException
from histogram_classes.RGBuvHistBlock import RGBuvHistBlock
from datetime import datetime
import torch
import argparse
from retry.api import retry_call
import os
from PIL import Image
from torchvision import transforms
import numpy as np
SCALE = 1 / np.sqrt(2.0)
def train_from_folder(
data='./dataset/',
results_dir='./results',
models_dir='./models',
name='test',
new=False,
load_from=-1,
image_size=128,
network_capacity=16,
transparent=False,
batch_size=2,
gradient_accumulate_every=8,
num_train_steps=150000,
learning_rate=2e-4,
num_workers=None,
save_every=1000,
generate=False,
save_noise_latent=False,
target_noise_file=None,
target_latent_file=None,
num_image_tiles=8,
trunc_psi=0.75,
fp16=False,
fq_layers=[],
fq_dict_size=256,
attn_layers=[],
hist_method='inverse-quadratic',
hist_resizing='sampling',
hist_sigma=0.02,
hist_bin=64,
hist_insz=150,
alpha=2,
target_hist=None,
aug_prob=0.0,
dataset_aug_prob=0.0,
aug_types=None):
model = Trainer(
name,
results_dir,
models_dir,
batch_size=batch_size,
gradient_accumulate_every=gradient_accumulate_every,
image_size=image_size,
network_capacity=network_capacity,
transparent=transparent,
lr=learning_rate,
num_workers=num_workers,
save_every=save_every,
trunc_psi=trunc_psi,
fp16=fp16,
fq_layers=fq_layers,
fq_dict_size=fq_dict_size,
attn_layers=attn_layers,
hist_insz=hist_insz,
hist_bin=hist_bin,
hist_sigma=hist_sigma,
hist_resizing=hist_resizing,
hist_method=hist_method,
aug_prob=aug_prob,
dataset_aug_prob=dataset_aug_prob,
aug_types=aug_types
)
if not new:
model.load(load_from)
else:
model.clear()
if generate:
now = datetime.now()
timestamp = now.strftime("%m-%d-%Y_%H-%M-%S")
if save_noise_latent and not os.path.exists('temp'):
os.mkdir('./temp')
if save_noise_latent and not os.path.exists(f'./temp/{name}'):
os.mkdir(f'./temp/{name}')
if target_hist is None:
raise Exception('No target histogram or image is given')
extension = os.path.splitext(target_hist)[1]
if extension == '.npy':
hist = np.load(target_hist)
h = torch.from_numpy(hist).to(device=torch.cuda.current_device())
if num_image_tiles > 1:
num_image_tiles = num_image_tiles - num_image_tiles % 2
for i in range(int(np.log2(num_image_tiles))):
h = torch.cat((h, h), dim=0)
samples_name = ('generated-' +
f'{os.path.basename(os.path.splitext(target_hist)[0])}'
f'-{timestamp}')
model.evaluate(samples_name, hist_batch=h,
num_image_tiles=num_image_tiles,
save_noise_latent=save_noise_latent,
load_noise_file=target_noise_file,
load_latent_file=target_latent_file)
print(f'sample images generated at {results_dir}/{name}/{samples_name}')
elif str.lower(extension) == '.jpg' or str.lower(extension) == '.png':
histblock = RGBuvHistBlock(insz=hist_insz, h=hist_bin,
resizing=hist_resizing, method=hist_method,
sigma=hist_sigma,
device=torch.cuda.current_device())
transform = transforms.Compose([transforms.ToTensor()])
img = Image.open(target_hist)
img = torch.unsqueeze(transform(img), dim=0).to(
device=torch.cuda.current_device())
h = histblock(img)
if num_image_tiles > 1:
num_image_tiles = num_image_tiles - num_image_tiles % 2
for i in range(int(np.log2(num_image_tiles))):
h = torch.cat((h, h), dim=0)
samples_name = ('generated-' +
f'{os.path.basename(os.path.splitext(target_hist)[0])}'
f'-{timestamp}')
model.evaluate(samples_name, hist_batch=h,
num_image_tiles=num_image_tiles,
save_noise_latent=save_noise_latent,
load_noise_file=target_noise_file,
load_latent_file=target_latent_file)
print(f'sample images generated at {results_dir}/{name}/{samples_name}')
elif extension == '':
files = [os.path.join(target_hist, f) for f in os.listdir(target_hist) if
os.path.isfile(os.path.join(target_hist, f))]
histblock = RGBuvHistBlock(insz=hist_insz, h=hist_bin,
resizing=hist_resizing, method=hist_method,
sigma=hist_sigma,
device=torch.cuda.current_device())
transform = transforms.Compose([transforms.ToTensor()])
for f in files:
extension = os.path.splitext(f)[1]
if extension == '.npy':
hist = np.load(f)
h = torch.from_numpy(hist).to(device=torch.cuda.current_device())
elif (extension == str.lower(extension) == '.jpg' or str.lower(
extension) == '.png'):
img = Image.open(f)
img = torch.unsqueeze(transform(img), dim=0).to(
device=torch.cuda.current_device())
h = histblock(img)
else:
print(f'Warning: File extension of {f} is not supported.')
continue
if num_image_tiles > 1:
num_image_tiles = num_image_tiles - num_image_tiles % 2
for i in range(int(np.log2(num_image_tiles))):
h = torch.cat((h, h), dim=0)
samples_name = ('generated-' +
f'{os.path.basename(os.path.splitext(f)[0])}'
f'-{timestamp}')
model.evaluate(samples_name, hist_batch=h,
num_image_tiles=num_image_tiles,
save_noise_latent=save_noise_latent,
load_noise_file=target_noise_file,
load_latent_file=target_latent_file)
print(f'sample images generated at {results_dir}/{name}/'
f'{samples_name}')
else:
print('The file extension of target image is not supported.')
raise NotImplementedError
return
print('\nStart training....\n')
print(f'Alpha = {alpha}')
model.set_data_src(data)
for _ in tqdm(range(num_train_steps - model.steps), mininterval=10.,
desc=f'{name}<{data}>'):
retry_call(model.train, fargs=[alpha], tries=3, exceptions=NanException)
if _ % 50 == 0:
model.print_log()
def get_args():
parser = argparse.ArgumentParser(description='Train/Test HistoGAN.')
parser.add_argument('--data', dest='data', default='./dataset/')
parser.add_argument('--results_dir', dest='results_dir',
default='./results_HistoGAN')
parser.add_argument('--models_dir', dest='models_dir', default='./models')
parser.add_argument('--target_hist', dest='target_hist', default=None)
parser.add_argument('--name', dest='name', default='histoGAN_model')
parser.add_argument('--new', dest='new', default=False)
parser.add_argument('--load_from', dest='load_from', default=-1)
parser.add_argument('--image_size', dest='image_size', default=256, type=int)
parser.add_argument('--network_capacity', dest='network_capacity', default=16,
type=int)
parser.add_argument('--transparent', dest='transparent', default=False)
parser.add_argument('--batch_size', dest='batch_size', default=2, type=int)
parser.add_argument('--gradient_accumulate_every',
dest='gradient_accumulate_every', default=8, type=int)
parser.add_argument('--num_train_steps', dest='num_train_steps',
default=1500000, type=int)
parser.add_argument('--learning_rate', dest='learning_rate', default=2e-4,
type=float)
parser.add_argument('--num_workers', dest='num_workers', default=None)
parser.add_argument('--save_every', dest='save_every', default=5000,
type=int)
parser.add_argument('--generate', dest='generate', default=False)
parser.add_argument('--save_noise_latent', dest='save_n_l', default=False)
parser.add_argument('--target_noise_file', dest='target_n', default=None)
parser.add_argument('--target_latent_file', dest='target_l', default=None)
parser.add_argument('--num_image_tiles', dest='num_image_tiles',
default=16, type=int)
parser.add_argument('--trunc_psi', dest='trunc_psi', default=0.75,
type=float)
parser.add_argument('--fp 16', dest='fp16', default=False)
parser.add_argument('--fq_layers', dest='fq_layers', default=[])
parser.add_argument('--fq_dict_size', dest='fq_dict_size', default=256,
type=int)
parser.add_argument('--attn_layers', dest='attn_layers', default=[])
parser.add_argument('--gpu', dest='gpu', default=0, type=int)
parser.add_argument('--hist_bin', dest='hist_bin', default=64, type=int)
parser.add_argument('--hist_insz', dest='hist_insz', default=150, type=int)
parser.add_argument('--hist_method', dest='hist_method',
default='inverse-quadratic')
parser.add_argument('--hist_resizing', dest='hist_resizing',
default='interpolation')
parser.add_argument('--hist_sigma', dest='hist_sigma', default=0.02,
type=float)
parser.add_argument('--alpha', dest='alpha', default=2, type=float)
parser.add_argument('--aug_prob', dest='aug_prob', default=0.0, type=float,
help='Probability of discriminator augmentation. It '
'applies operations specified in --aug_types.')
parser.add_argument('--dataset_aug_prob', dest='dataset_aug_prob',
default=0.0, type=float,
help='Probability of dataset augmentation. It applies '
'random cropping')
parser.add_argument('--aug_types', dest='aug_types',
default=['translation', 'cutout'], nargs='+',
help='Options include: translation, cutout, and color')
return parser.parse_args()
if __name__ == "__main__":
args = get_args()
torch.cuda.set_device(args.gpu)
train_from_folder(
data=args.data,
results_dir=args.results_dir,
models_dir=args.models_dir,
name=args.name,
new=args.new,
load_from=args.load_from,
image_size=args.image_size,
network_capacity=args.network_capacity,
transparent=args.transparent,
batch_size=args.batch_size,
gradient_accumulate_every=args.gradient_accumulate_every,
num_train_steps=args.num_train_steps,
learning_rate=args.learning_rate,
num_workers=args.num_workers,
save_every=args.save_every,
generate=args.generate,
save_noise_latent=args.save_n_l,
target_noise_file=args.target_n,
target_latent_file=args.target_l,
num_image_tiles=args.num_image_tiles,
trunc_psi=args.trunc_psi,
fp16=args.fp16,
fq_layers=args.fq_layers,
fq_dict_size=args.fq_dict_size,
attn_layers=args.attn_layers,
hist_method=args.hist_method,
hist_resizing=args.hist_resizing,
hist_sigma=args.hist_sigma,
hist_bin=args.hist_bin,
hist_insz=args.hist_insz,
target_hist=args.target_hist,
alpha=args.alpha,
aug_prob=args.aug_prob,
dataset_aug_prob=args.dataset_aug_prob,
aug_types=args.aug_types
)