-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrain.py
More file actions
165 lines (130 loc) · 4.96 KB
/
train.py
File metadata and controls
165 lines (130 loc) · 4.96 KB
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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import sys
import pickle
import csv
import numpy as np
from model import image_caption_model
from joblib import Parallel, delayed
import time
from keras.utils.layer_utils import print_summary
def gene_word_caption(captions):
word2idx = dict()
idx2word = dict()
word_count = 0
ret = []
for caption in captions:
caption = caption.replace(',', ' ,')
caption = caption.replace(';', ' ;')
caption = caption.replace(':', ' :')
caption = caption.replace('.', ' .')
tokens = caption.split(' ')
res = []
for word in tokens:
if word == '':
continue
if word not in word2idx:
word2idx[word] = word_count
idx2word[word_count] = word
word_count += 1
res.append(word2idx[word])
ret.append(res)
# add start sign
word2idx['+'] = word_count
idx2word[word_count] = '+'
word_count += 1
return ret, word2idx, idx2word, word_count
def gen_batch_in_thread(img_map, df_cap, vocab_size, n_jobs=4,
size_per_thread=32):
imgs, curs, nxts, seqs, vhists = [], [], [], [], []
returns = Parallel(n_jobs=4, backend='threading')(
delayed(generate_batch)
(img_train, df_cap, vocab_size, size=size_per_thread)
for i in range(0, n_jobs))
for triple in returns:
imgs.extend(triple[0])
curs.extend(triple[1])
nxts.extend(triple[2])
seqs.extend(triple[3])
vhists.extend(triple[4])
return np.array(imgs), np.array(curs).reshape((-1, 1)), np.array(nxts), \
np.array(seqs), np.array(vhists)
def generate_batch(img_map, df_cap, vocab_size, size=32, max_caplen=28):
imgs, curs, nxts, seqs, vhists = [], [], [], [], []
for idx in np.random.randint(len(df_cap), size=size):
row = df_cap[idx]
cap = row['caption']
img = img_map[row['img_id']]
vhist = np.zeros((len(cap)-1, vocab_size))
for i in range(1, len(cap)):
seq = np.zeros((max_caplen))
nxt = np.zeros((vocab_size))
nxt[cap[i]] = 1
curs.append(cap[i-1])
seq[i-1] = 1
if i < len(cap)-1:
vhist[i, :] = np.logical_or(vhist[i, :], vhist[i-1, :])
vhist[i, cap[i-1]] = 1
nxts.append(nxt)
imgs.append(img)
seqs.append(seq)
vhists.extend(vhist)
return imgs, curs, nxts, seqs, vhists
if __name__ == '__main__':
# initialization
n_jobs = 64
size_per_thread = 64
mdl_path = 'weights/'
pkl_count = 0
img_train = {}
while True:
pkl_count += 1
try:
with open('train/train_img2048_%d.pkl' % pkl_count, 'rb') as f:
img_train.update(pickle.load(f))
except FileNotFoundError:
break
print('got %d image context vectors' % len(img_train))
anns = csv.reader(open("train/anns.csv"))
anns = [row for row in anns if row[1] != 'caption']
print('got %d training captions' % len(anns))
captions = ['+ ' + row[1] for row in anns]
captions, word2idx, idx2word, vocab_size = gene_word_caption(captions)
print('got %d words, saving word translate dict...' % vocab_size)
with open('./train/word2idx.pkl', 'wb') as f:
pickle.dump(word2idx, f)
with open('./train/idx2word.pkl', 'wb') as f:
pickle.dump(idx2word, f)
img_ids = [int(row[3]) for row in anns]
df_cap = []
for i in range(0, len(img_ids)):
df_cap.append(
dict(img_id=img_ids[i], caption=captions[i])
)
model = image_caption_model(vocab_size=vocab_size)
if len(sys.argv) >= 2:
print('load weights from : {}'.format(sys.argv[1]))
model.load_weights(sys.argv[1])
# insert ur version name here
version = 'v1.0.0'
batch_num = 70
hist_loss = []
for i in range(0, 100):
for j in range(0, batch_num):
s = time.time()
img1, cur1, nxt1, seq1, vhists1 = gen_batch_in_thread(
img_train, df_cap, vocab_size, n_jobs=n_jobs,
size_per_thread=size_per_thread)
hist = model.fit([img1, cur1, seq1, vhists1], nxt1,
batch_size=n_jobs * size_per_thread,
nb_epoch=1, verbose=0, shuffle=True)
print("epoch {0}, batch {1} - training loss : {2}".format(
i, j, hist.history['loss'][-1]))
# record the training history
hist_loss.extend(hist.history['loss'])
if j % int(batch_num / 2) == 0:
print('check point arrived, saving...')
m_name = "{0}{1}_{2}_{3}_{4}_{5}.h5".format(
mdl_path, version, i, j, time.time(), str(vocab_size))
model.save_weights(m_name)