Skip to content

Commit 1b6a798

Browse files
committed
update cifar10 examples
1 parent 473889e commit 1b6a798

File tree

2 files changed

+178
-191
lines changed

2 files changed

+178
-191
lines changed

example/tutorial_cifar10.py

Lines changed: 89 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import tensorflow as tf
77
import tensorlayer as tl
8-
from tensorlayer.layers import set_keep
8+
from tensorlayer.layers import *
99
import numpy as np
1010
import time, os, io
1111
from PIL import Image
@@ -14,107 +14,110 @@
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
2017

21-
def model(x, y_, is_train, reuse):
18+
def model(x, y_, reuse):
2219
W_init = tf.truncated_normal_initializer(stddev=5e-2)
2320
W_init2 = tf.truncated_normal_initializer(stddev=0.04)
2421
b_init2 = tf.constant_initializer(value=0.1)
2522
with tf.variable_scope("model", reuse=reuse):
2623
tl.layers.set_name_reuse(reuse)
27-
network = tl.layers.InputLayer(x, name='input')
28-
29-
network = tl.layers.Conv2dLayer(network, act=tf.identity,
30-
shape=[5, 5, 3, 64], strides=[1, 1, 1, 1], padding='SAME', # 64 features for each 5x5x3 patch
31-
W_init=W_init, b_init=None, name='cnn1') # output: (batch_size, 24, 24, 64)
32-
network = tl.layers.BatchNormLayer(network, is_train=is_train,
33-
act=tf.nn.relu, name='batch1')
34-
network = tl.layers.PoolLayer(network, ksize=[1, 3, 3, 1],
35-
strides=[1, 2, 2, 1], padding='SAME',
36-
pool=tf.nn.max_pool, name='pool1',) # output: (batch_size, 12, 12, 64)
37-
38-
network = tl.layers.Conv2dLayer(network, act=tf.identity,
39-
shape=[5, 5, 64, 64], strides=[1, 1, 1, 1], padding='SAME',# 64 features for each 5x5 patch
40-
W_init=W_init, b_init=None, name ='cnn2') # output: (batch_size, 12, 12, 64)
41-
network = tl.layers.BatchNormLayer(network, is_train=is_train,
42-
act=tf.nn.relu, name='batch2')
43-
network = tl.layers.PoolLayer(network, ksize=[1, 3, 3, 1],
44-
strides=[1, 2, 2, 1], padding='SAME',
45-
pool = tf.nn.max_pool, name ='pool2') # output: (batch_size, 6, 6, 64)
46-
47-
network = tl.layers.FlattenLayer(network, name='flatten') # output: (batch_size, 2304)
48-
network = tl.layers.DenseLayer(network, n_units=384, act=tf.nn.relu,
24+
net = InputLayer(x, name='input')
25+
net = Conv2d(net, 64, (5, 5), (1, 1), padding='SAME',
26+
W_init=W_init, name='cnn1')
27+
# net = Conv2dLayer(net, act=tf.nn.relu, shape=[5, 5, 3, 64],
28+
# strides=[1, 1, 1, 1], padding='SAME', # 64 features for each 5x5x3 patch
29+
# W_init=W_init, name ='cnn1') # output: (batch_size, 24, 24, 64)
30+
net = MaxPool2d(net, (3, 3), (2, 2), padding='SAME',name='pool1')
31+
# net = PoolLayer(net, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1],
32+
# padding='SAME', pool = tf.nn.max_pool, name ='pool1',)# output: (batch_size, 12, 12, 64)
33+
net = LocalResponseNormLayer(net, depth_radius=4, bias=1.0,
34+
alpha=0.001 / 9.0, beta=0.75, name='norm1')
35+
# net.outputs = tf.nn.lrn(net.outputs, 4, bias=1.0, alpha=0.001 / 9.0,
36+
# beta=0.75, name='norm1')
37+
38+
net = Conv2d(net, 64, (5, 5), (1, 1), padding='SAME',
39+
W_init=W_init, name='cnn2')
40+
# net = Conv2dLayer(net, act=tf.nn.relu, shape=[5, 5, 64, 64],
41+
# strides=[1, 1, 1, 1], padding='SAME', # 64 features for each 5x5 patch
42+
# W_init=W_init, name ='cnn2') # output: (batch_size, 12, 12, 64)
43+
net = LocalResponseNormLayer(net, depth_radius=4, bias=1.0,
44+
alpha=0.001 / 9.0, beta=0.75, name='norm2')
45+
# net.outputs = tf.nn.lrn(net.outputs, 4, bias=1.0, alpha=0.001 / 9.0,
46+
# beta=0.75, name='norm2')
47+
net = MaxPool2d(net, (3, 3), (2, 2), padding='SAME',name='pool2')
48+
# net = PoolLayer(net, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1],
49+
# padding='SAME', pool = tf.nn.max_pool, name ='pool2') # output: (batch_size, 6, 6, 64)
50+
net = FlattenLayer(net, name='flatten') # output: (batch_size, 2304)
51+
net = DenseLayer(net, n_units=384, act=tf.nn.relu,
4952
W_init=W_init2, b_init=b_init2, name='relu1') # output: (batch_size, 384)
50-
network = tl.layers.DenseLayer(network, n_units=192, act = tf.nn.relu,
53+
net = DenseLayer(net, n_units=192, act=tf.nn.relu,
5154
W_init=W_init2, b_init=b_init2, name='relu2') # output: (batch_size, 192)
52-
network = tl.layers.DenseLayer(network, n_units=10, act = tf.identity,
55+
net = DenseLayer(net, n_units=10, act=tf.identity,
5356
W_init=tf.truncated_normal_initializer(stddev=1/192.0),
54-
b_init = tf.constant_initializer(value=0.0),
55-
name='output') # output: (batch_size, 10)
56-
y = network.outputs
57-
cost = tl.cost.cross_entropy(y, y_, name='cost')
58-
L2 = tf.contrib.layers.l2_regularizer(0.004)(network.all_params[4]) + \
59-
tf.contrib.layers.l2_regularizer(0.004)(network.all_params[6])
60-
cost = cost + L2
57+
name='output') # output: (batch_size, 10)
58+
y = net.outputs
59+
60+
ce = tl.cost.cross_entropy(y, y_, name='cost')
61+
# L2 for the MLP, without this, the accuracy will be reduced by 15%.
62+
L2 = tf.contrib.layers.l2_regularizer(0.004)(net.all_params[4]) + \
63+
tf.contrib.layers.l2_regularizer(0.004)(net.all_params[6])
64+
cost = ce + L2
65+
6166
correct_prediction = tf.equal(tf.argmax(y, 1), y_)
6267
acc = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
6368

64-
return network, cost, acc
69+
return net, cost, acc
6570

6671

67-
def model2(x, y_, is_train, reuse):
72+
def model_batch_norm(x, y_, reuse, is_train):
73+
""" Batch normalization should be placed before rectifier. """
6874
W_init = tf.truncated_normal_initializer(stddev=5e-2)
69-
b_init = tf.constant_initializer(value=0.0)
7075
W_init2 = tf.truncated_normal_initializer(stddev=0.04)
7176
b_init2 = tf.constant_initializer(value=0.1)
7277
with tf.variable_scope("model", reuse=reuse):
7378
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,
79+
net = InputLayer(x, name='input')
80+
81+
net = Conv2d(net, 64, (5, 5), (1, 1), padding='SAME',
82+
W_init=W_init, b_init=None, name='cnn1')
83+
# net = Conv2dLayer(net, act=tf.identity, shape=[5, 5, 3, 64],
84+
# strides=[1, 1, 1, 1], padding='SAME', # 64 features for each 5x5x3 patch
85+
# W_init=W_init, b_init=None, name='cnn1') # output: (batch_size, 24, 24, 64)
86+
net = BatchNormLayer(net, is_train, act=tf.nn.relu, name='batch1')
87+
net = MaxPool2d(net, (3, 3), (2, 2), padding='SAME',name='pool1')
88+
# net = PoolLayer(net, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1],
89+
# padding='SAME', pool=tf.nn.max_pool, name='pool1',) # output: (batch_size, 12, 12, 64)
90+
91+
net = Conv2d(net, 64, (5, 5), (1, 1), padding='SAME',
92+
W_init=W_init, b_init=None, name='cnn2')
93+
# net = Conv2dLayer(net, act=tf.identity, shape=[5, 5, 64, 64],
94+
# strides=[1, 1, 1, 1], padding='SAME', # 64 features for each 5x5 patch
95+
# W_init=W_init, b_init=None, name ='cnn2') # output: (batch_size, 12, 12, 64)
96+
net = BatchNormLayer(net, is_train, act=tf.nn.relu, name='batch2')
97+
net = MaxPool2d(net, (3, 3), (2, 2), padding='SAME',name='pool2')
98+
# net = PoolLayer(net, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1],
99+
# padding='SAME', pool = tf.nn.max_pool, name ='pool2') # output: (batch_size, 6, 6, 64)
100+
101+
net = FlattenLayer(net, name='flatten') # output: (batch_size, 2304)
102+
net = DenseLayer(net, n_units=384, act=tf.nn.relu,
97103
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,
104+
net = DenseLayer(net, n_units=192, act = tf.nn.relu,
99105
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,
106+
net = DenseLayer(net, n_units=10, act = tf.identity,
101107
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
108+
name='output') # output: (batch_size, 10)
109+
y = net.outputs
105110

106111
ce = tl.cost.cross_entropy(y, y_, name='cost')
107112
# 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])
113+
L2 = tf.contrib.layers.l2_regularizer(0.004)(net.all_params[4]) + \
114+
tf.contrib.layers.l2_regularizer(0.004)(net.all_params[6])
110115
cost = ce + L2
111116

112-
# correct_prediction = tf.equal(tf.argmax(tf.nn.softmax(y), 1), y_)
113117
correct_prediction = tf.equal(tf.argmax(y, 1), y_)
114118
acc = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
115119

116-
return network, cost, acc
117-
120+
return net, cost, acc
118121

119122
def distort_fn(x, is_train=False):
120123
"""
@@ -127,7 +130,6 @@ def distort_fn(x, is_train=False):
127130
artificially increase the data set size:
128131
.. Randomly flip the image from left to right.
129132
.. Randomly distort the image brightness.
130-
.. Randomly zoom in.
131133
"""
132134
# print('begin',x.shape, np.min(x), np.max(x))
133135
x = tl.prepro.crop(x, 24, 24, is_random=is_train)
@@ -140,22 +142,20 @@ def distort_fn(x, is_train=False):
140142
x = tl.prepro.brightness(x, gamma=0.1, gain=1, is_random=True)
141143
# print('after brightness',x.shape, np.min(x), np.max(x))
142144
# tmp = np.max(x)
143-
# x += np.random.uniform(-0.1, 0.1)
145+
# x += np.random.uniform(-20, 20)
144146
# x /= tmp
145147
# normalize the image
146-
x = (x - np.mean(x)) / max(np.std(x), 1.0/24.0) # avoid values divided by 0
148+
x = (x - np.mean(x)) / max(np.std(x), 1e-5) # avoid values divided by 0
147149
# print('after norm', x.shape, np.min(x), np.max(x), np.mean(x))
148150
return x
149151

150-
# x = X_train[0]
151-
# x = distort_fn(x, True)
152-
# exit()
153-
154152
x = tf.placeholder(tf.float32, shape=[None, 24, 24, 3], name='x')
155153
y_ = tf.placeholder(tf.int64, shape=[None, ], name='y_')
156154

157-
network, cost, _ = model2(x, y_, True, False)
158-
_, cost_test, acc = model2(x, y_, False, True)
155+
# network, cost, _ = model(x, y_, False)
156+
# _, cost_test, acc = model(x, y_, True)
157+
network, cost, _ = model_batch_norm(x, y_, False, is_train=True)
158+
_, cost_test, acc = model_batch_norm(x, y_, True, is_train=False)
159159

160160
## train
161161
n_epoch = 50000
@@ -184,14 +184,14 @@ def distort_fn(x, is_train=False):
184184

185185
if epoch + 1 == 1 or (epoch + 1) % print_freq == 0:
186186
print("Epoch %d of %d took %fs" % (epoch + 1, n_epoch, time.time() - start_time))
187-
train_loss, train_acc, n_batch = 0, 0, 0
188-
for X_train_a, y_train_a in tl.iterate.minibatches(
189-
X_train, y_train, batch_size, shuffle=True):
190-
X_train_a = tl.prepro.threading_data(X_train_a, fn=distort_fn, is_train=False) # central crop
191-
err, ac = sess.run([cost_test, acc], feed_dict={x: X_train_a, y_: y_train_a})
192-
train_loss += err; train_acc += ac; n_batch += 1
193-
print(" train loss: %f" % (train_loss/ n_batch))
194-
print(" train acc: %f" % (train_acc/ n_batch))
187+
# train_loss, train_acc, n_batch = 0, 0, 0
188+
# for X_train_a, y_train_a in tl.iterate.minibatches(
189+
# X_train, y_train, batch_size, shuffle=True):
190+
# X_train_a = tl.prepro.threading_data(X_train_a, fn=distort_fn, is_train=False) # central crop
191+
# err, ac = sess.run([cost_test, acc], feed_dict={x: X_train_a, y_: y_train_a})
192+
# train_loss += err; train_acc += ac; n_batch += 1
193+
# print(" train loss: %f" % (train_loss/ n_batch))
194+
# print(" train acc: %f" % (train_acc/ n_batch))
195195
test_loss, test_acc, n_batch = 0, 0, 0
196196
for X_test_a, y_test_a in tl.iterate.minibatches(
197197
X_test, y_test, batch_size, shuffle=True):

0 commit comments

Comments
 (0)