-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
199 lines (172 loc) · 6.87 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
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
from __future__ import print_function
from miscc.config import cfg, cfg_from_file
from datasets import TextDataset
from dataset_fashiongen2 import TextDataset as TextFashionGenDataset
from trainer import condGANTrainer as trainer
import os
import sys
import time
import random
import pprint
import datetime
import dateutil.tz
import argparse
import numpy as np
import torch
import torchvision.transforms as transforms
dir_path = (os.path.abspath(os.path.join(os.path.realpath(__file__), './.')))
sys.path.append(dir_path)
def parse_args():
parser = argparse.ArgumentParser(description='Train a AttnGAN network')
parser.add_argument('--cfg', dest='cfg_file',
help='optional config file',
default='cfg/fashiongen2_attn2.yml', type=str)
parser.add_argument('--gpu', dest='gpu_id', type=int, default=-1)
parser.add_argument('--data_dir', dest='data_dir', type=str, default='')
parser.add_argument('--manualSeed', type=int, help='manual seed')
args = parser.parse_args()
return args
def gen_example(wordtoix, algo):
'''generate images from example sentences'''
from nltk.tokenize import RegexpTokenizer
filepath = '%s/example_filenames.txt' % (cfg.DATA_DIR)
data_dic = {}
with open(filepath, "r") as f:
filenames = f.read().decode('utf8').split('\n')
for name in filenames:
if len(name) == 0:
continue
filepath = '%s/%s.txt' % (cfg.DATA_DIR, name)
with open(filepath, "r") as f:
print('Load from:', name)
sentences = f.read().decode('utf8').split('\n')
# a list of indices for a sentence
captions = []
cap_lens = []
for sent in sentences:
if len(sent) == 0:
continue
sent = sent.replace("\ufffd\ufffd", " ")
tokenizer = RegexpTokenizer(r'\w+')
tokens = tokenizer.tokenize(sent.lower())
if len(tokens) == 0:
print('sent', sent)
continue
rev = []
for t in tokens:
t = t.encode('ascii', 'ignore').decode('ascii')
if len(t) > 0 and t in wordtoix:
rev.append(wordtoix[t])
captions.append(rev)
cap_lens.append(len(rev))
max_len = np.max(cap_lens)
sorted_indices = np.argsort(cap_lens)[::-1]
cap_lens = np.asarray(cap_lens)
cap_lens = cap_lens[sorted_indices]
cap_array = np.zeros((len(captions), max_len), dtype='int64')
for i in range(len(captions)):
idx = sorted_indices[i]
cap = captions[idx]
c_len = len(cap)
cap_array[i, :c_len] = cap
key = name[(name.rfind('/') + 1):]
data_dic[key] = [cap_array, cap_lens, sorted_indices]
algo.gen_example(data_dic)
if __name__ == "__main__":
args = parse_args()
if args.cfg_file is not None:
cfg_from_file(args.cfg_file)
if args.gpu_id != -1:
cfg.GPU_ID = args.gpu_id
else:
cfg.CUDA = False
if args.data_dir != '':
cfg.DATA_DIR = args.data_dir
print('Using config:')
pprint.pprint(cfg)
cfg.DATASET_NAME = 'fashiongen2'
cfg.DATA_DIR = '../data/fashiongen'
cfg.CONFG_NAME = 'glu-gan2'
cfg.TREE.BRANCH_NUM = 3
cfg.TRAIN.BATCH_SIZE = 4
#cfg.TRAIN.NET_E = '../DAMSMencoders/fashiongen2/text_encoder80.pth'
#cfg.TRAIN.NET_G = ''
#cfg.TRAIN.FLAG = True ## TRAINING
#cfg.TRAIN.NET_G = '../output/fashiongen2__2019_12_01_09_07_18/Model/netG_epoch_8.pth'
cfg.TRAIN.NET_G = '../output/fashiongen2__2019_12_06_00_49_28/Model/netG_epoch_8.pth'
cfg.TRAIN.FLAG = False ##SAMPLE NEW IMAGES
cfg.TRAIN.MAX_EPOCH = 8
cfg.TRAIN.SNAPSHOT_INTERVAL = 2
cfg.TRAIN.B_NET_D = True
cfg.TRAIN.GENERATOR_LR = 0.0002
cfg.TRAIN.DISCRIMINATOR_LR = 0.0002
cfg.TRAIN.NET_E = '../DAMSMencoders/fashiongen2/text_encoder95.pth'
cfg.WORKERS = 1
cfg.TRAIN.SMOOTH.GAMMA1 = 4.0
cfg.TRAIN.SMOOTH.GAMMA2 = 5.0
cfg.TRAIN.SMOOTH.GAMMA3 = 10.0
#cfg.TRAIN.SMOOTH.GAMMA3 = 10.0
#cfg.TRAIN.SMOOTH.LAMDA = 50.0
cfg.TRAIN.SMOOTH.LAMDA = 1.0
cfg.TREE.BASE_SIZE = 64
cfg.TRAIN.RNN_GRAD_CLIP = 0.25
cfg.TEXT.CAPTIONS_PER_IMAGE = 1
cfg.TEXT.WORDS_NUM = 10
cfg.TEXT.EMBEDDING_DIM = 256
cfg.GAN.DF_DIM = 96
cfg.GAN.GF_DIM = 48
cfg.GAN.Z_DIM = 100
cfg.GAN.R_NUM = 3
#cfg.GAN.B_ATTENTION = True
cfg.B_VALIDATION = False #True
cfg.GAN.B_DCGAN = False
cfg.GAN.CONDITION_DIM = 100
cfg.RNN_TYPE = 'LSTM'
if not cfg.TRAIN.FLAG:
args.manualSeed = 100
elif args.manualSeed is None:
args.manualSeed = random.randint(1, 10000)
random.seed(args.manualSeed)
np.random.seed(args.manualSeed)
torch.manual_seed(args.manualSeed)
if cfg.CUDA:
torch.cuda.manual_seed_all(args.manualSeed)
now = datetime.datetime.now(dateutil.tz.tzlocal())
timestamp = now.strftime('%Y_%m_%d_%H_%M_%S')
output_dir = '../output/%s_%s_%s' % (cfg.DATASET_NAME, cfg.CONFIG_NAME, timestamp)
split_dir, bshuffle = 'train', True
#split_dir, bshuffle = 'validation', True
if not cfg.TRAIN.FLAG:
# bshuffle = False
split_dir = 'validation'
# Get data loader
imsize = cfg.TREE.BASE_SIZE * (2 ** (cfg.TREE.BRANCH_NUM - 1))
print(imsize)
if cfg.DATASET_NAME == 'fashiongen2':
print('correct data')
image_transform = transforms.Compose([
transforms.Resize(imsize),
transforms.RandomHorizontalFlip()])
dataset = TextFashionGenDataset(cfg.DATA_DIR, split_dir, base_size=cfg.TREE.BASE_SIZE, transform=image_transform)
else:
image_transform = transforms.Compose([
transforms.Scale(int(imsize * 76 / 64)),
transforms.RandomCrop(imsize),
transforms.RandomHorizontalFlip()])
dataset = TextDataset(cfg.DATA_DIR, split_dir, base_size=cfg.TREE.BASE_SIZE, transform=image_transform)
assert dataset
dataloader = torch.utils.data.DataLoader(
dataset, batch_size=cfg.TRAIN.BATCH_SIZE, drop_last=True, shuffle=bshuffle, num_workers=int(cfg.WORKERS))
# Define models and go to train/evaluate
algo = trainer(output_dir, dataloader, dataset.n_words, dataset.ixtoword)
start_t = time.time()
if cfg.TRAIN.FLAG:
algo.train()
else:
'''generate images from pre-extracted embeddings'''
if cfg.B_VALIDATION:
algo.sampling(split_dir) # generate images for the whole valid dataset
else:
gen_example(dataset.wordtoix, algo) # generate images for customized captions
end_t = time.time()
print('Total time for training:', end_t - start_t)