-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathpascal_conv2d.py
150 lines (113 loc) · 4.24 KB
/
pascal_conv2d.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
from __future__ import print_function
from __future__ import division
from six.moves import xrange
import time
import tensorflow as tf
from lib.datasets import PascalVOC as Data
from lib.model import Model as BaseModel
from lib.layer import ImageAugment, Conv2d, Fire, MaxPool, AveragePool
DATA_DIR = 'data/pascal_voc'
LEARNING_RATE = 0.0001
TRAIN_DIR = None
LOG_DIR = 'data/summaries/pascal_conv2d'
SAVE_STEP = 250
DROPOUT = 0.5
BATCH_SIZE = 64
MAX_STEPS = 50000
DISPLAY_STEP = 10
data = Data(DATA_DIR, fixed_size=224)
placeholders = {
'features':
tf.placeholder(tf.float32,
[None, data.width, data.height,
data.num_channels], 'features'),
'labels':
tf.placeholder(tf.uint8, [None, data.num_classes], 'labels'),
'dropout':
tf.placeholder(tf.float32, [], 'dropout'),
}
class Model(BaseModel):
def _build(self):
augment = ImageAugment()
conv_1 = Conv2d(
data.num_channels, 64, size=3, stride=2, logging=self.logging)
max_pool_1 = MaxPool(3, 2)
fire_1_1 = Fire(64, 16, 64, logging=self.logging)
fire_1_2 = Fire(128, 16, 64, logging=self.logging)
max_pool_2 = MaxPool(3, 2)
fire_2_1 = Fire(128, 32, 128, logging=self.logging)
fire_2_2 = Fire(256, 32, 128, logging=self.logging)
max_pool_3 = MaxPool(3, 2)
fire_3_1 = Fire(256, 48, 192, logging=self.logging)
fire_3_2 = Fire(384, 48, 192, logging=self.logging)
fire_3_3 = Fire(384, 64, 256, logging=self.logging)
fire_3_4 = Fire(512, 64, 256, logging=self.logging)
conv_2 = Conv2d(
512,
20,
size=1,
stride=1,
bias=False,
dropout=DROPOUT,
logging=self.logging)
avg = AveragePool()
self.layers = [
augment, conv_1, max_pool_1, fire_1_1, fire_1_2, max_pool_2,
fire_2_1, fire_2_2, max_pool_3, fire_3_1, fire_3_2, fire_3_3,
fire_3_4, conv_2, avg
]
model = Model(
placeholders=placeholders,
learning_rate=LEARNING_RATE,
train_dir=TRAIN_DIR,
log_dir=LOG_DIR)
model.build()
global_step = model.initialize()
def feed_dict_with_batch(images, labels, dropout=0):
return {
placeholders['features']: images,
placeholders['labels']: labels,
placeholders['dropout']: DROPOUT,
}
try:
for step in xrange(global_step, MAX_STEPS):
t_pre = time.process_time()
images, labels = data.train.next_batch(BATCH_SIZE, shuffle=True)
feed_dict = feed_dict_with_batch(images, labels, DROPOUT)
t_pre = time.process_time() - t_pre
t_train = model.train(feed_dict, step)
if step % DISPLAY_STEP == 0:
# Evaluate on training and validation set with zero dropout.
feed_dict.update({model.placeholders['dropout']: 0})
train_info = model.evaluate(feed_dict, step, 'train')
images, labels = data.val.next_batch(BATCH_SIZE, shuffle=True)
feed_dict = feed_dict_with_batch(images, labels)
val_info = model.evaluate(feed_dict, step, 'val')
log = 'step={}, '.format(step)
log += 'time={:.2f}s + {:.2f}s, '.format(t_pre, t_train)
log += 'train_loss={:.5f}, '.format(train_info[0])
log += 'train_acc={:.5f}, '.format(train_info[1])
log += 'val_loss={:.5f}, '.format(val_info[0])
log += 'val_acc={:.5f}'.format(val_info[1])
print(log)
if step % SAVE_STEP == 0:
model.save()
except KeyboardInterrupt:
print()
print('Optimization finished!')
print('Evaluate on test set. This can take a few minutes.')
try:
num_steps = data.test.num_examples // BATCH_SIZE
test_info = [0, 0]
for i in xrange(num_steps):
images, labels = data.test.next_batch(BATCH_SIZE, shuffle=False)
feed_dict = feed_dict_with_batch(images, labels)
batch_info = model.evaluate(feed_dict)
test_info = [a + b for a, b in zip(test_info, batch_info)]
log = 'Test results: '
log += 'loss={:.5f}, '.format(test_info[0] / num_steps)
log += 'acc={:.5f}, '.format(test_info[1] / num_steps)
print(log)
except KeyboardInterrupt:
print()
print('Test evaluation aborted.')