-
Notifications
You must be signed in to change notification settings - Fork 87
/
train.caltech.py
183 lines (137 loc) · 6.75 KB
/
train.caltech.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
import tensorflow as tf
import numpy as np
import pandas as pd
from detector import Detector
from util import load_image
import os
import ipdb
weight_path = '../data/caffe_layers_value.pickle'
model_path = '../models/caltech256/'
pretrained_model_path = None #'../models/caltech256/model-0'
n_epochs = 10000
init_learning_rate = 0.01
weight_decay_rate = 0.0005
momentum = 0.9
batch_size = 60
dataset_path = '/media/storage3/Study/data/256_ObjectCategories'
caltech_path = '../data/caltech'
trainset_path = '../data/caltech/train.pickle'
testset_path = '../data/caltech/test.pickle'
label_dict_path = '../data/caltech/label_dict.pickle'
if not os.path.exists( trainset_path ):
if not os.path.exists( caltech_path ):
os.makedirs( caltech_path )
image_dir_list = os.listdir( dataset_path )
label_pairs = map(lambda x: x.split('.'), image_dir_list)
labels, label_names = zip(*label_pairs)
labels = map(lambda x: int(x), labels)
label_dict = pd.Series( labels, index=label_names )
label_dict -= 1
n_labels = len( label_dict )
image_paths_per_label = map(lambda one_dir: map(lambda one_file: os.path.join( dataset_path, one_dir, one_file ), os.listdir( os.path.join( dataset_path, one_dir))), image_dir_list)
image_paths_train = np.hstack(map(lambda one_class: one_class[:-10], image_paths_per_label))
image_paths_test = np.hstack(map(lambda one_class: one_class[-10:], image_paths_per_label))
trainset = pd.DataFrame({'image_path': image_paths_train})
testset = pd.DataFrame({'image_path': image_paths_test })
trainset = trainset[ trainset['image_path'].map( lambda x: x.endswith('.jpg'))]
trainset['label'] = trainset['image_path'].map(lambda x: int(x.split('/')[-2].split('.')[0]) - 1)
trainset['label_name'] = trainset['image_path'].map(lambda x: x.split('/')[-2].split('.')[1])
testset = testset[ testset['image_path'].map( lambda x: x.endswith('.jpg'))]
testset['label'] = testset['image_path'].map(lambda x: int(x.split('/')[-2].split('.')[0]) - 1)
testset['label_name'] = testset['image_path'].map(lambda x: x.split('/')[-2].split('.')[1])
label_dict.to_pickle(label_dict_path)
trainset.to_pickle(trainset_path)
testset.to_pickle(testset_path)
else:
trainset = pd.read_pickle( trainset_path )
testset = pd.read_pickle( testset_path )
label_dict = pd.read_pickle( label_dict_path )
n_labels = len(label_dict)
learning_rate = tf.placeholder( tf.float32, [])
images_tf = tf.placeholder( tf.float32, [None, 224, 224, 3], name="images")
labels_tf = tf.placeholder( tf.int64, [None], name='labels')
detector = Detector(weight_path, n_labels)
p1,p2,p3,p4,conv5, conv6, gap, output = detector.inference(images_tf)
loss_tf = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits( output, labels_tf ))
weights_only = filter( lambda x: x.name.endswith('W:0'), tf.trainable_variables() )
weight_decay = tf.reduce_sum(tf.pack([tf.nn.l2_loss(x) for x in weights_only])) * weight_decay_rate
loss_tf += weight_decay
sess = tf.InteractiveSession()
saver = tf.train.Saver( max_to_keep=50 )
optimizer = tf.train.MomentumOptimizer( learning_rate, momentum )
grads_and_vars = optimizer.compute_gradients( loss_tf )
grads_and_vars = map(lambda gv: (gv[0], gv[1]) if ('conv6' in gv[1].name or 'GAP' in gv[1].name) else (gv[0]*0.1, gv[1]), grads_and_vars)
#grads_and_vars = [(tf.clip_by_value(gv[0], -5., 5.), gv[1]) for gv in grads_and_vars]
train_op = optimizer.apply_gradients( grads_and_vars )
tf.initialize_all_variables().run()
if pretrained_model_path:
print "Pretrained"
saver.restore(sess, pretrained_model_path)
testset.index = range( len(testset) )
#testset = testset.ix[np.random.permutation( len(testset) )]#[:1000]
#trainset2 = testset[1000:]
#testset = testset[:1000]
#trainset = pd.concat( [trainset, trainset2] )
# We lack the number of training set. Let's use some of the test images
f_log = open('../results/log.caltech256.txt', 'w')
iterations = 0
loss_list = []
for epoch in range(n_epochs):
trainset.index = range( len(trainset) )
trainset = trainset.ix[ np.random.permutation( len(trainset) )]
for start, end in zip(
range( 0, len(trainset)+batch_size, batch_size),
range(batch_size, len(trainset)+batch_size, batch_size)):
current_data = trainset[start:end]
current_image_paths = current_data['image_path'].values
current_images = np.array(map(lambda x: load_image(x), current_image_paths))
good_index = np.array(map(lambda x: x is not None, current_images))
current_data = current_data[good_index]
current_images = np.stack(current_images[good_index])
current_labels = current_data['label'].values
_, loss_val, output_val = sess.run(
[train_op, loss_tf, output],
feed_dict={
learning_rate: init_learning_rate,
images_tf: current_images,
labels_tf: current_labels
})
loss_list.append( loss_val )
iterations += 1
if iterations % 5 == 0:
print "======================================"
print "Epoch", epoch, "Iteration", iterations
print "Processed", start, '/', len(trainset)
label_predictions = output_val.argmax(axis=1)
acc = (label_predictions == current_labels).sum()
print "Accuracy:", acc, '/', len(current_labels)
print "Training Loss:", np.mean(loss_list)
print "\n"
loss_list = []
n_correct = 0
n_data = 0
for start, end in zip(
range(0, len(testset)+batch_size, batch_size),
range(batch_size, len(testset)+batch_size, batch_size)
):
current_data = testset[start:end]
current_image_paths = current_data['image_path'].values
current_images = np.array(map(lambda x: load_image(x), current_image_paths))
good_index = np.array(map(lambda x: x is not None, current_images))
current_data = current_data[good_index]
current_images = np.stack(current_images[good_index])
current_labels = current_data['label'].values
output_vals = sess.run(
output,
feed_dict={images_tf:current_images})
label_predictions = output_vals.argmax(axis=1)
acc = (label_predictions == current_labels).sum()
n_correct += acc
n_data += len(current_data)
acc_all = n_correct / float(n_data)
f_log.write('epoch:'+str(epoch)+'\tacc:'+str(acc_all) + '\n')
print "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"
print 'epoch:'+str(epoch)+'\tacc:'+str(acc_all) + '\n'
print "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"
saver.save( sess, os.path.join( model_path, 'model'), global_step=epoch)
init_learning_rate *= 0.99