Skip to content

Commit c8a368b

Browse files
committed
adding vgg inspection eaxamples for course 4
1 parent 553092e commit c8a368b

File tree

7 files changed

+222
-0
lines changed

7 files changed

+222
-0
lines changed

.DS_Store

2 KB
Binary file not shown.

course_example_vgg/.DS_Store

6 KB
Binary file not shown.
8.68 KB
Loading
12 KB
Loading

course_example_vgg/utils.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import skimage
2+
import skimage.io
3+
import skimage.transform
4+
import numpy as np
5+
6+
7+
# synset = [l.strip() for l in open('synset.txt').readlines()]
8+
9+
10+
# returns image of shape [224, 224, 3]
11+
# [height, width, depth]
12+
def load_image(path):
13+
# load image
14+
img = skimage.io.imread(path)
15+
img = img / 255.0
16+
assert (0 <= img).all() and (img <= 1.0).all()
17+
# print "Original Image Shape: ", img.shape
18+
# we crop image from center
19+
short_edge = min(img.shape[:2])
20+
yy = int((img.shape[0] - short_edge) / 2)
21+
xx = int((img.shape[1] - short_edge) / 2)
22+
crop_img = img[yy: yy + short_edge, xx: xx + short_edge]
23+
# resize to 224, 224
24+
resized_img = skimage.transform.resize(crop_img, (224, 224))
25+
return resized_img
26+
27+
28+
# returns the top1 string
29+
def print_prob(prob, file_path):
30+
synset = [l.strip() for l in open(file_path).readlines()]
31+
32+
# print prob
33+
pred = np.argsort(prob)[::-1]
34+
35+
# Get top1 label
36+
top1 = synset[pred[0]]
37+
print(("Top1: ", top1, prob[pred[0]]))
38+
# Get top5 label
39+
top5 = [(synset[pred[i]], prob[pred[i]]) for i in range(5)]
40+
print(("Top5: ", top5))
41+
return top1
42+
43+
44+
def load_image2(path, height=None, width=None):
45+
# load image
46+
img = skimage.io.imread(path)
47+
img = img / 255.0
48+
if height is not None and width is not None:
49+
ny = height
50+
nx = width
51+
elif height is not None:
52+
ny = height
53+
nx = img.shape[1] * ny / img.shape[0]
54+
elif width is not None:
55+
nx = width
56+
ny = img.shape[0] * nx / img.shape[1]
57+
else:
58+
ny = img.shape[0]
59+
nx = img.shape[1]
60+
return skimage.transform.resize(img, (ny, nx))
61+
62+
63+
def test():
64+
img = skimage.io.imread("./test_data/starry_night.jpg")
65+
ny = 300
66+
nx = img.shape[1] * ny / img.shape[0]
67+
img = skimage.transform.resize(img, (ny, nx))
68+
skimage.io.imsave("./test_data/test/output.jpg", img)
69+
70+
71+
if __name__ == "__main__":
72+
test()

course_example_vgg/vgg16.py

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import inspect
2+
import os
3+
4+
import numpy as np
5+
import tensorflow as tf
6+
import time
7+
8+
VGG_MEAN = [103.939, 116.779, 123.68]
9+
10+
11+
class Vgg16:
12+
def __init__(self, vgg16_npy_path=None):
13+
if vgg16_npy_path is None:
14+
path = inspect.getfile(Vgg16)
15+
path = os.path.abspath(os.path.join(path, os.pardir))
16+
path = os.path.join(path, "vgg16.npy")
17+
vgg16_npy_path = path
18+
print(path)
19+
20+
self.data_dict = np.load(vgg16_npy_path, encoding='latin1').item()
21+
print("npy file loaded")
22+
23+
def build(self, rgb):
24+
"""
25+
load variable from npy to build the VGG
26+
:param rgb: rgb image [batch, height, width, 3] values scaled [0, 1]
27+
"""
28+
29+
start_time = time.time()
30+
print("build model started")
31+
rgb_scaled = rgb * 255.0
32+
33+
# Convert RGB to BGR
34+
red, green, blue = tf.split(axis=3, num_or_size_splits=3, value=rgb_scaled)
35+
assert red.get_shape().as_list()[1:] == [224, 224, 1]
36+
assert green.get_shape().as_list()[1:] == [224, 224, 1]
37+
assert blue.get_shape().as_list()[1:] == [224, 224, 1]
38+
bgr = tf.concat(axis=3, values=[
39+
blue - VGG_MEAN[0],
40+
green - VGG_MEAN[1],
41+
red - VGG_MEAN[2],
42+
])
43+
assert bgr.get_shape().as_list()[1:] == [224, 224, 3]
44+
45+
self.conv1_1 = self.conv_layer(bgr, "conv1_1")
46+
self.conv1_2 = self.conv_layer(self.conv1_1, "conv1_2")
47+
self.pool1 = self.max_pool(self.conv1_2, 'pool1')
48+
49+
self.conv2_1 = self.conv_layer(self.pool1, "conv2_1")
50+
self.conv2_2 = self.conv_layer(self.conv2_1, "conv2_2")
51+
self.pool2 = self.max_pool(self.conv2_2, 'pool2')
52+
53+
self.conv3_1 = self.conv_layer(self.pool2, "conv3_1")
54+
self.conv3_2 = self.conv_layer(self.conv3_1, "conv3_2")
55+
self.conv3_3 = self.conv_layer(self.conv3_2, "conv3_3")
56+
self.pool3 = self.max_pool(self.conv3_3, 'pool3')
57+
58+
self.conv4_1 = self.conv_layer(self.pool3, "conv4_1")
59+
self.conv4_2 = self.conv_layer(self.conv4_1, "conv4_2")
60+
self.conv4_3 = self.conv_layer(self.conv4_2, "conv4_3")
61+
self.pool4 = self.max_pool(self.conv4_3, 'pool4')
62+
63+
self.conv5_1 = self.conv_layer(self.pool4, "conv5_1")
64+
self.conv5_2 = self.conv_layer(self.conv5_1, "conv5_2")
65+
self.conv5_3 = self.conv_layer(self.conv5_2, "conv5_3")
66+
self.pool5 = self.max_pool(self.conv5_3, 'pool5')
67+
68+
self.fc6 = self.fc_layer(self.pool5, "fc6")
69+
assert self.fc6.get_shape().as_list()[1:] == [4096]
70+
self.relu6 = tf.nn.relu(self.fc6)
71+
72+
self.fc7 = self.fc_layer(self.relu6, "fc7")
73+
self.relu7 = tf.nn.relu(self.fc7)
74+
75+
self.fc8 = self.fc_layer(self.relu7, "fc8")
76+
77+
self.prob = tf.nn.softmax(self.fc8, name="prob")
78+
79+
self.data_dict = None
80+
print(("build model finished: %ds" % (time.time() - start_time)))
81+
82+
def avg_pool(self, bottom, name):
83+
return tf.nn.avg_pool(bottom, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name=name)
84+
85+
def max_pool(self, bottom, name):
86+
return tf.nn.max_pool(bottom, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name=name)
87+
88+
def conv_layer(self, bottom, name):
89+
with tf.variable_scope(name):
90+
filt = self.get_conv_filter(name)
91+
92+
conv = tf.nn.conv2d(bottom, filt, [1, 1, 1, 1], padding='SAME')
93+
94+
conv_biases = self.get_bias(name)
95+
bias = tf.nn.bias_add(conv, conv_biases)
96+
97+
relu = tf.nn.relu(bias)
98+
return relu
99+
100+
def fc_layer(self, bottom, name):
101+
with tf.variable_scope(name):
102+
shape = bottom.get_shape().as_list()
103+
dim = 1
104+
for d in shape[1:]:
105+
dim *= d
106+
x = tf.reshape(bottom, [-1, dim])
107+
108+
weights = self.get_fc_weight(name)
109+
biases = self.get_bias(name)
110+
111+
# Fully connected layer. Note that the '+' operation automatically
112+
# broadcasts the biases.
113+
fc = tf.nn.bias_add(tf.matmul(x, weights), biases)
114+
115+
return fc
116+
117+
def get_conv_filter(self, name):
118+
return tf.constant(self.data_dict[name][0], name="filter")
119+
120+
def get_bias(self, name):
121+
return tf.constant(self.data_dict[name][1], name="biases")
122+
123+
def get_fc_weight(self, name):
124+
return tf.constant(self.data_dict[name][0], name="weights")

course_example_vgg/vgg16_test.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import numpy as np
2+
import tensorflow as tf
3+
4+
import vgg16
5+
import utils
6+
7+
img1 = utils.load_image("./test_data/tiger.jpeg")
8+
img2 = utils.load_image("./test_data/puzzle.jpeg")
9+
10+
batch1 = img1.reshape((1, 224, 224, 3))
11+
batch2 = img2.reshape((1, 224, 224, 3))
12+
13+
batch = np.concatenate((batch1, batch2), 0)
14+
15+
# with tf.Session(config=tf.ConfigProto(gpu_options=(tf.GPUOptions(per_process_gpu_memory_fraction=0.7)))) as sess:
16+
with tf.device('/cpu:0'):
17+
with tf.Session() as sess:
18+
images = tf.placeholder("float", [2, 224, 224, 3])
19+
feed_dict = {images: batch}
20+
21+
vgg = vgg16.Vgg16()
22+
with tf.name_scope("content_vgg"):
23+
vgg.build(images)
24+
25+
prob = sess.run(vgg.prob, feed_dict=feed_dict)
26+
print(prob)

0 commit comments

Comments
 (0)