Skip to content

Commit b1d5345

Browse files
committed
update cifar10 examples
1 parent 3b3b116 commit b1d5345

File tree

2 files changed

+97
-26
lines changed

2 files changed

+97
-26
lines changed

example/tutorial_cifar10.py

100644100755
Lines changed: 75 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414

1515
X_train, y_train, X_test, y_test = tl.files.load_cifar10_dataset(
1616
shape=(-1, 32, 32, 3), plotable=False)
17-
scale = X_train.max()
18-
X_train /= scale
19-
X_test /= scale
17+
# scale = X_train.max()
18+
# X_train /= scale
19+
# X_test /= scale
2020

2121
def model(x, y_, is_train, reuse):
2222
W_init = tf.truncated_normal_initializer(stddev=5e-2)
@@ -63,6 +63,59 @@ def model(x, y_, is_train, reuse):
6363

6464
return network, cost, acc
6565

66+
67+
def model2(x, y_, is_train, reuse):
68+
W_init = tf.truncated_normal_initializer(stddev=5e-2)
69+
b_init = tf.constant_initializer(value=0.0)
70+
W_init2 = tf.truncated_normal_initializer(stddev=0.04)
71+
b_init2 = tf.constant_initializer(value=0.1)
72+
with tf.variable_scope("model", reuse=reuse):
73+
tl.layers.set_name_reuse(reuse)
74+
network = tl.layers.InputLayer(x, name='input')
75+
network = tl.layers.Conv2dLayer(network, act=tf.nn.relu,
76+
shape=[5, 5, 3, 64], strides=[1, 1, 1, 1], padding='SAME', # 64 features for each 5x5x3 patch
77+
W_init=W_init, b_init=b_init, name ='cnn1') # output: (batch_size, 24, 24, 64)
78+
network = tl.layers.PoolLayer(network, ksize=[1, 3, 3, 1],
79+
strides=[1, 2, 2, 1], padding='SAME',
80+
pool = tf.nn.max_pool, name ='pool1',) # output: (batch_size, 12, 12, 64)
81+
# network.outputs = tf.nn.lrn(network.outputs, 4, bias=1.0, alpha=0.001 / 9.0,
82+
# beta=0.75, name='norm1')
83+
network = tl.layers.LocalResponseNormLayer(network, depth_radius=4, bias=1.0,
84+
alpha=0.001 / 9.0, beta=0.75, name='norm1')
85+
network = tl.layers.Conv2dLayer(network, act=tf.nn.relu,
86+
shape=[5, 5, 64, 64], strides=[1, 1, 1, 1], padding='SAME',# 64 features for each 5x5 patch
87+
W_init=W_init, b_init=b_init, name ='cnn2') # output: (batch_size, 12, 12, 64)
88+
# network.outputs = tf.nn.lrn(network.outputs, 4, bias=1.0, alpha=0.001 / 9.0,
89+
# beta=0.75, name='norm2')
90+
network = tl.layers.LocalResponseNormLayer(network, depth_radius=4, bias=1.0,
91+
alpha=0.001 / 9.0, beta=0.75, name='norm2')
92+
network = tl.layers.PoolLayer(network, ksize=[1, 3, 3, 1],
93+
strides=[1, 2, 2, 1], padding='SAME',
94+
pool = tf.nn.max_pool, name ='pool2') # output: (batch_size, 6, 6, 64)
95+
network = tl.layers.FlattenLayer(network, name='flatten') # output: (batch_size, 2304)
96+
network = tl.layers.DenseLayer(network, n_units=384, act=tf.nn.relu,
97+
W_init=W_init2, b_init=b_init2, name='relu1') # output: (batch_size, 384)
98+
network = tl.layers.DenseLayer(network, n_units=192, act=tf.nn.relu,
99+
W_init=W_init2, b_init=b_init2, name='relu2') # output: (batch_size, 192)
100+
network = tl.layers.DenseLayer(network, n_units=10, act=tf.identity,
101+
W_init=tf.truncated_normal_initializer(stddev=1/192.0),
102+
b_init = tf.constant_initializer(value=0.0),
103+
name='output') # output: (batch_size, 10)
104+
y = network.outputs
105+
106+
ce = tl.cost.cross_entropy(y, y_, name='cost')
107+
# L2 for the MLP, without this, the accuracy will be reduced by 15%.
108+
L2 = tf.contrib.layers.l2_regularizer(0.004)(network.all_params[4]) + \
109+
tf.contrib.layers.l2_regularizer(0.004)(network.all_params[6])
110+
cost = ce + L2
111+
112+
# correct_prediction = tf.equal(tf.argmax(tf.nn.softmax(y), 1), y_)
113+
correct_prediction = tf.equal(tf.argmax(y, 1), y_)
114+
acc = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
115+
116+
return network, cost, acc
117+
118+
66119
def distort_fn(x, is_train=False):
67120
"""
68121
Description
@@ -76,18 +129,33 @@ def distort_fn(x, is_train=False):
76129
.. Randomly distort the image brightness.
77130
.. Randomly zoom in.
78131
"""
132+
# print('begin',x.shape, np.min(x), np.max(x))
79133
x = tl.prepro.crop(x, 24, 24, is_random=is_train)
134+
# print('after crop',x.shape, np.min(x), np.max(x))
80135
if is_train:
81-
x = tl.prepro.zoom(x, zoom_range=(0.9, 1.0), is_random=True)
136+
# x = tl.prepro.zoom(x, zoom_range=(0.9, 1.0), is_random=True)
137+
# print('after zoom', x.shape, np.min(x), np.max(x))
82138
x = tl.prepro.flip_axis(x, axis=1, is_random=True)
83-
x = tl.prepro.brightness(x, gamma=0.2, gain=1, is_random=True)
139+
# print('after flip',x.shape, np.min(x), np.max(x))
140+
x = tl.prepro.brightness(x, gamma=0.1, gain=1, is_random=True)
141+
# print('after brightness',x.shape, np.min(x), np.max(x))
142+
# tmp = np.max(x)
143+
# x += np.random.uniform(-0.1, 0.1)
144+
# x /= tmp
145+
# normalize the image
146+
x = (x - np.mean(x)) / max(np.std(x), 1.0/24.0) # avoid values divided by 0
147+
# print('after norm', x.shape, np.min(x), np.max(x), np.mean(x))
84148
return x
85149

150+
# x = X_train[0]
151+
# x = distort_fn(x, True)
152+
# exit()
153+
86154
x = tf.placeholder(tf.float32, shape=[None, 24, 24, 3], name='x')
87155
y_ = tf.placeholder(tf.int64, shape=[None, ], name='y_')
88156

89-
network, cost, _ = model(x, y_, True, False)
90-
_, cost_test, acc = model(x, y_, False, True)
157+
network, cost, _ = model2(x, y_, True, False)
158+
_, cost_test, acc = model2(x, y_, False, True)
91159

92160
## train
93161
n_epoch = 50000

example/tutorial_cifar10_tfrecord.py

100644100755
Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -113,18 +113,18 @@ def read_and_decode(filename, is_train=None):
113113
# 4. Randomly change contrast.
114114
img = tf.image.random_contrast(img, lower=0.2, upper=1.8)
115115
# 5. Subtract off the mean and divide by the variance of the pixels.
116-
try: # TF12
116+
try: # TF 0.12+
117117
img = tf.image.per_image_standardization(img)
118-
except: #earlier TF versions
118+
except: # earlier TF versions
119119
img = tf.image.per_image_whitening(img)
120120

121121
elif is_train == False:
122122
# 1. Crop the central [height, width] of the image.
123123
img = tf.image.resize_image_with_crop_or_pad(img, 24, 24)
124124
# 2. Subtract off the mean and divide by the variance of the pixels.
125-
try: # TF12
125+
try: # TF 0.12+
126126
img = tf.image.per_image_standardization(img)
127-
except: #earlier TF versions
127+
except: # earlier TF versions
128128
img = tf.image.per_image_whitening(img)
129129
elif is_train == None:
130130
img = img
@@ -201,19 +201,22 @@ def inference(x_crop, y_, reuse):
201201
network = tl.layers.InputLayer(x_crop, name='input')
202202
network = tl.layers.Conv2dLayer(network, act=tf.nn.relu,
203203
shape=[5, 5, 3, 64], strides=[1, 1, 1, 1], padding='SAME', # 64 features for each 5x5x3 patch
204-
W_init=W_init, b_init=b_init, name ='cnn1') # output: (batch_size, 24, 24, 64)
204+
W_init=W_init, b_init=b_init, name ='cnn_layer1') # output: (batch_size, 24, 24, 64)
205205
network = tl.layers.PoolLayer(network, ksize=[1, 3, 3, 1],
206206
strides=[1, 2, 2, 1], padding='SAME',
207207
pool = tf.nn.max_pool, name ='pool1',) # output: (batch_size, 12, 12, 64)
208-
# you can also use tl.layers.LocalResponseNormLayer
209-
network.outputs = tf.nn.lrn(network.outputs, 4, bias=1.0, alpha=0.001 / 9.0,
210-
beta=0.75, name='norm1')
208+
# network.outputs = tf.nn.lrn(network.outputs, 4, bias=1.0, alpha=0.001 / 9.0,
209+
# beta=0.75, name='norm1')
210+
network = tl.layers.LocalResponseNormLayer(network, depth_radius=4, bias=1.0,
211+
alpha=0.001 / 9.0, beta=0.75, name='norm1')
211212

212213
network = tl.layers.Conv2dLayer(network, act=tf.nn.relu,
213214
shape=[5, 5, 64, 64], strides=[1, 1, 1, 1], padding='SAME',# 64 features for each 5x5 patch
214215
W_init=W_init, b_init=b_init, name ='cnn2') # output: (batch_size, 12, 12, 64)
215-
network.outputs = tf.nn.lrn(network.outputs, 4, bias=1.0, alpha=0.001 / 9.0,
216-
beta=0.75, name='norm2')
216+
# network.outputs = tf.nn.lrn(network.outputs, 4, bias=1.0, alpha=0.001 / 9.0,
217+
# beta=0.75, name='norm2')
218+
network = tl.layers.LocalResponseNormLayer(network, depth_radius=4, bias=1.0,
219+
alpha=0.001 / 9.0, beta=0.75, name='norm2')
217220
network = tl.layers.PoolLayer(network, ksize=[1, 3, 3, 1],
218221
strides=[1, 2, 2, 1], padding='SAME',
219222
pool = tf.nn.max_pool, name ='pool2') # output: (batch_size, 6, 6, 64)
@@ -252,35 +255,35 @@ def inference_batch_norm(x_crop, y_, reuse, is_train):
252255
b_init2 = tf.constant_initializer(value=0.1)
253256
with tf.variable_scope("model", reuse=reuse):
254257
tl.layers.set_name_reuse(reuse)
255-
network = tl.layers.InputLayer(x_crop, name='input_layer')
258+
network = tl.layers.InputLayer(x_crop, name='input')
256259

257260
network = tl.layers.Conv2dLayer(network, act=tf.identity,
258261
shape=[5, 5, 3, 64], strides=[1, 1, 1, 1], padding='SAME', # 64 features for each 5x5x3 patch
259-
W_init=W_init, b_init=None, name='cnn_layer1') # output: (batch_size, 24, 24, 64)
262+
W_init=W_init, b_init=None, name='cnn1') # output: (batch_size, 24, 24, 64)
260263
network = tl.layers.BatchNormLayer(network, is_train=is_train,
261-
act=tf.nn.relu, name='batch_norm1')
264+
act=tf.nn.relu, name='batch1')
262265
network = tl.layers.PoolLayer(network, ksize=[1, 3, 3, 1],
263266
strides=[1, 2, 2, 1], padding='SAME',
264-
pool=tf.nn.max_pool, name='pool_layer1',) # output: (batch_size, 12, 12, 64)
267+
pool=tf.nn.max_pool, name='pool1',) # output: (batch_size, 12, 12, 64)
265268

266269
network = tl.layers.Conv2dLayer(network, act=tf.identity,
267270
shape=[5, 5, 64, 64], strides=[1, 1, 1, 1], padding='SAME',# 64 features for each 5x5 patch
268-
W_init=W_init, b_init=None, name ='cnn_layer2') # output: (batch_size, 12, 12, 64)
271+
W_init=W_init, b_init=None, name ='cnn2') # output: (batch_size, 12, 12, 64)
269272
network = tl.layers.BatchNormLayer(network, is_train=is_train,
270-
act=tf.nn.relu, name='batch_norm2')
273+
act=tf.nn.relu, name='batch2')
271274
network = tl.layers.PoolLayer(network, ksize=[1, 3, 3, 1],
272275
strides=[1, 2, 2, 1], padding='SAME',
273-
pool = tf.nn.max_pool, name ='pool_layer2') # output: (batch_size, 6, 6, 64)
276+
pool = tf.nn.max_pool, name ='pool2') # output: (batch_size, 6, 6, 64)
274277

275-
network = tl.layers.FlattenLayer(network, name='flatten_layer') # output: (batch_size, 2304)
278+
network = tl.layers.FlattenLayer(network, name='flatten') # output: (batch_size, 2304)
276279
network = tl.layers.DenseLayer(network, n_units=384, act=tf.nn.relu,
277280
W_init=W_init2, b_init=b_init2, name='relu1') # output: (batch_size, 384)
278281
network = tl.layers.DenseLayer(network, n_units=192, act = tf.nn.relu,
279282
W_init=W_init2, b_init=b_init2, name='relu2') # output: (batch_size, 192)
280283
network = tl.layers.DenseLayer(network, n_units=10, act = tf.identity,
281284
W_init=tf.truncated_normal_initializer(stddev=1/192.0),
282285
b_init = tf.constant_initializer(value=0.0),
283-
name='output_layer') # output: (batch_size, 10)
286+
name='output') # output: (batch_size, 10)
284287
y = network.outputs
285288

286289
ce = tl.cost.cross_entropy(y, y_, name='cost')

0 commit comments

Comments
 (0)