Skip to content
Navigation Menu
{{ message }}
forked from tensorlayer/TensorLayer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtutorial_vgg19.py
More file actions
executable file
·256 lines (235 loc) · 8.62 KB
/
Copy pathtutorial_vgg19.py
File metadata and controls
executable file
·256 lines (235 loc) · 8.62 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#! /usr/bin/python
# -*- coding: utf8 -*-
import tensorflow as tf
import tensorlayer as tl
from data.imagenet_classes import *
import os
import numpy as np
import time
import inspect
import skimage
import skimage.io
import skimage.transform
"""
VGG-19 for ImageNet
--------------------
Pre-trained model in this example - VGG19 NPZ and
trainable examples of VGG16/19 in TensorFlow can be found here:
https://github.com/machrisaa/tensorflow-vgg
For simplified CNN layer see "Convolutional layer (Simplified)"
in read the docs website.
"""
VGG_MEAN = [103.939, 116.779, 123.68]
def load_image(path):
# load image
img = skimage.io.imread(path)
img = img / 255.0
assert (0 <= img).all() and (img <= 1.0).all()
# print "Original Image Shape: ", img.shape
# we crop image from center
short_edge = min(img.shape[:2])
yy = int((img.shape[0] - short_edge) / 2)
xx = int((img.shape[1] - short_edge) / 2)
crop_img = img[yy: yy + short_edge, xx: xx + short_edge]
# resize to 224, 224
resized_img = skimage.transform.resize(crop_img, (224, 224))
return resized_img
def print_prob(prob):
synset = class_names
# print prob
pred = np.argsort(prob)[::-1]
# Get top1 label
top1 = synset[pred[0]]
print("Top1: ", top1, prob[pred[0]])
# Get top5 label
top5 = [(synset[pred[i]], prob[pred[i]]) for i in range(5)]
print("Top5: ", top5)
return top1
def Vgg19(rgb):
"""
Build the VGG 19 Model
Parameters
-----------
rgb : rgb image placeholder [batch, height, width, 3] values scaled [0, 1]
"""
start_time = time.time()
print("build model started")
rgb_scaled = rgb * 255.0
# Convert RGB to BGR
red, green, blue = tf.split(3, 3, rgb_scaled)
assert red.get_shape().as_list()[1:] == [224, 224, 1]
assert green.get_shape().as_list()[1:] == [224, 224, 1]
assert blue.get_shape().as_list()[1:] == [224, 224, 1]
bgr = tf.concat(3, [
blue - VGG_MEAN[0],
green - VGG_MEAN[1],
red - VGG_MEAN[2],
])
assert bgr.get_shape().as_list()[1:] == [224, 224, 3]
""" input layer """
net_in = tl.layers.InputLayer(bgr, name='input_layer')
""" conv1 """
network = tl.layers.Conv2dLayer(net_in,
act = tf.nn.relu,
shape = [3, 3, 3, 64],
strides = [1, 1, 1, 1],
padding='SAME',
name ='conv1_1')
network = tl.layers.Conv2dLayer(network,
act = tf.nn.relu,
shape = [3, 3, 64, 64],
strides = [1, 1, 1, 1],
padding='SAME',
name ='conv1_2')
network = tl.layers.PoolLayer(network,
ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1],
padding='SAME',
pool = tf.nn.max_pool,
name ='pool1')
""" conv2 """
network = tl.layers.Conv2dLayer(network,
act = tf.nn.relu,
shape = [3, 3, 64, 128],
strides = [1, 1, 1, 1],
padding='SAME',
name ='conv2_1')
network = tl.layers.Conv2dLayer(network,
act = tf.nn.relu,
shape = [3, 3, 128, 128],
strides = [1, 1, 1, 1],
padding='SAME',
name ='conv2_2')
network = tl.layers.PoolLayer(network,
ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1],
padding='SAME',
pool = tf.nn.max_pool,
name ='pool2')
""" conv3 """
network = tl.layers.Conv2dLayer(network,
act = tf.nn.relu,
shape = [3, 3, 128, 256],
strides = [1, 1, 1, 1],
padding='SAME',
name ='conv3_1')
network = tl.layers.Conv2dLayer(network,
act = tf.nn.relu,
shape = [3, 3, 256, 256],
strides = [1, 1, 1, 1],
padding='SAME',
name ='conv3_2')
network = tl.layers.Conv2dLayer(network,
act = tf.nn.relu,
shape = [3, 3, 256, 256],
strides = [1, 1, 1, 1],
padding='SAME',
name ='conv3_3')
network = tl.layers.Conv2dLayer(network,
act = tf.nn.relu,
shape = [3, 3, 256, 256],
strides = [1, 1, 1, 1],
padding='SAME',
name ='conv3_4')
network = tl.layers.PoolLayer(network,
ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1],
padding='SAME',
pool = tf.nn.max_pool,
name ='pool3')
""" conv4 """
network = tl.layers.Conv2dLayer(network,
act = tf.nn.relu,
shape = [3, 3, 256, 512],
strides = [1, 1, 1, 1],
padding='SAME',
name ='conv4_1')
network = tl.layers.Conv2dLayer(network,
act = tf.nn.relu,
shape = [3, 3, 512, 512],
strides = [1, 1, 1, 1],
padding='SAME',
name ='conv4_2')
network = tl.layers.Conv2dLayer(network,
act = tf.nn.relu,
shape = [3, 3, 512, 512],
strides = [1, 1, 1, 1],
padding='SAME',
name ='conv4_3')
network = tl.layers.Conv2dLayer(network,
act = tf.nn.relu,
shape = [3, 3, 512, 512],
strides = [1, 1, 1, 1],
padding='SAME',
name ='conv4_4')
network = tl.layers.PoolLayer(network,
ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1],
padding='SAME',
pool = tf.nn.max_pool,
name ='pool4')
""" conv5 """
network = tl.layers.Conv2dLayer(network,
act = tf.nn.relu,
shape = [3, 3, 512, 512],
strides = [1, 1, 1, 1],
padding='SAME',
name ='conv5_1')
network = tl.layers.Conv2dLayer(network,
act = tf.nn.relu,
shape = [3, 3, 512, 512],
strides = [1, 1, 1, 1],
padding='SAME',
name ='conv5_2')
network = tl.layers.Conv2dLayer(network,
act = tf.nn.relu,
shape = [3, 3, 512, 512],
strides = [1, 1, 1, 1],
padding='SAME',
name ='conv5_3')
network = tl.layers.Conv2dLayer(network,
act = tf.nn.relu,
shape = [3, 3, 512, 512],
strides = [1, 1, 1, 1],
padding='SAME',
name ='conv5_4')
network = tl.layers.PoolLayer(network,
ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1],
padding='SAME',
pool = tf.nn.max_pool,
name ='pool5')
""" fc 6~8 """
network = tl.layers.FlattenLayer(network, name='flatten')
network = tl.layers.DenseLayer(network, n_units=4096,
act = tf.nn.relu, name = 'fc6')
network = tl.layers.DenseLayer(network, n_units=4096,
act = tf.nn.relu, name = 'fc7')
network = tl.layers.DenseLayer(network, n_units=1000,
act = tf.identity, name = 'fc8')
print("build model finished: %fs" % (time.time() - start_time))
return network
sess = tf.InteractiveSession()
x = tf.placeholder("float", [None, 224, 224, 3])
network = Vgg19(x)
y = network.outputs
probs = tf.nn.softmax(y, name="prob")
sess.run(tf.initialize_all_variables())
# You need to download the pre-trained model - VGG19 NPZ
# in https://github.com/machrisaa/tensorflow-vgg
vgg19_npy_path = "vgg19.npy"
npz = np.load(vgg19_npy_path, encoding='latin1').item()
params = []
for val in sorted( npz.items() ):
W = np.asarray(val[1][0])
b = np.asarray(val[1][1])
print(" Loading %s: %s, %s" % (val[0], W.shape, b.shape))
params.extend([W, b])
print("Restoring model from npz file")
tl.files.assign_params(sess, params, network)
img1 = load_image("data/tiger.jpeg")
img1 = img1.reshape((1, 224, 224, 3))
start_time = time.time()
prob = sess.run(probs, feed_dict= {x : img1})
print("End time : %.5ss" % (time.time() - start_time))
print_prob(prob[0])
You can’t perform that action at this time.
