Skip to content

Commit 0a9d8cf

Browse files
authored
add scripts
1 parent 864e39d commit 0a9d8cf

File tree

7 files changed

+822
-0
lines changed

7 files changed

+822
-0
lines changed

04 VGG Tensorflow/VGG.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
2+
import tensorflow as tf
3+
import tools
4+
5+
#%%
6+
def VGG16(x, n_classes, is_pretrain=True):
7+
8+
x = tools.conv('conv1_1', x, 64, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
9+
x = tools.conv('conv1_2', x, 64, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
10+
x = tools.pool('pool1', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True)
11+
12+
x = tools.conv('conv2_1', x, 128, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
13+
x = tools.conv('conv2_2', x, 128, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
14+
x = tools.pool('pool2', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True)
15+
16+
x = tools.conv('conv3_1', x, 256, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
17+
x = tools.conv('conv3_2', x, 256, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
18+
x = tools.conv('conv3_3', x, 256, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
19+
x = tools.pool('pool3', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True)
20+
21+
x = tools.conv('conv4_1', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
22+
x = tools.conv('conv4_2', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
23+
x = tools.conv('conv4_3', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
24+
x = tools.pool('pool3', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True)
25+
26+
x = tools.conv('conv5_1', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
27+
x = tools.conv('conv5_2', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
28+
x = tools.conv('conv5_3', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
29+
x = tools.pool('pool3', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True)
30+
31+
x = tools.FC_layer('fc6', x, out_nodes=4096)
32+
x = tools.batch_norm(x)
33+
x = tools.FC_layer('fc7', x, out_nodes=4096)
34+
x = tools.batch_norm(x)
35+
x = tools.FC_layer('fc8', x, out_nodes=n_classes)
36+
37+
return x
38+
39+
40+
41+
42+
43+
#%% TO get better tensorboard figures!
44+
45+
def VGG16N(x, n_classes, is_pretrain=True):
46+
47+
with tf.name_scope('VGG16'):
48+
49+
x = tools.conv('conv1_1', x, 64, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
50+
x = tools.conv('conv1_2', x, 64, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
51+
with tf.name_scope('pool1'):
52+
x = tools.pool('pool1', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True)
53+
54+
x = tools.conv('conv2_1', x, 128, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
55+
x = tools.conv('conv2_2', x, 128, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
56+
with tf.name_scope('pool2'):
57+
x = tools.pool('pool2', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True)
58+
59+
60+
61+
x = tools.conv('conv3_1', x, 256, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
62+
x = tools.conv('conv3_2', x, 256, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
63+
x = tools.conv('conv3_3', x, 256, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
64+
with tf.name_scope('pool3'):
65+
x = tools.pool('pool3', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True)
66+
67+
68+
x = tools.conv('conv4_1', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
69+
x = tools.conv('conv4_2', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
70+
x = tools.conv('conv4_3', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
71+
with tf.name_scope('pool4'):
72+
x = tools.pool('pool4', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True)
73+
74+
75+
x = tools.conv('conv5_1', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
76+
x = tools.conv('conv5_2', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
77+
x = tools.conv('conv5_3', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
78+
with tf.name_scope('pool5'):
79+
x = tools.pool('pool5', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True)
80+
81+
82+
x = tools.FC_layer('fc6', x, out_nodes=4096)
83+
with tf.name_scope('batch_norm1'):
84+
x = tools.batch_norm(x)
85+
x = tools.FC_layer('fc7', x, out_nodes=4096)
86+
with tf.name_scope('batch_norm2'):
87+
x = tools.batch_norm(x)
88+
x = tools.FC_layer('fc8', x, out_nodes=n_classes)
89+
90+
return x
91+
92+
93+
94+
#%%
95+
96+
97+
98+
99+
100+
101+
102+

04 VGG Tensorflow/input_data.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
2+
import tensorflow as tf
3+
import numpy as np
4+
import os
5+
6+
#%% Reading data
7+
8+
def read_cifar10(data_dir, is_train, batch_size, shuffle):
9+
"""Read CIFAR10
10+
11+
Args:
12+
data_dir: the directory of CIFAR10
13+
is_train: boolen
14+
batch_size:
15+
shuffle:
16+
Returns:
17+
label: 1D tensor, tf.int32
18+
image: 4D tensor, [batch_size, height, width, 3], tf.float32
19+
20+
"""
21+
img_width = 32
22+
img_height = 32
23+
img_depth = 3
24+
label_bytes = 1
25+
image_bytes = img_width*img_height*img_depth
26+
27+
28+
with tf.name_scope('input'):
29+
30+
if is_train:
31+
filenames = [os.path.join(data_dir, 'data_batch_%d.bin' %ii)
32+
for ii in np.arange(1, 6)]
33+
else:
34+
filenames = [os.path.join(data_dir, 'test_batch.bin')]
35+
36+
filename_queue = tf.train.string_input_producer(filenames)
37+
38+
reader = tf.FixedLengthRecordReader(label_bytes + image_bytes)
39+
40+
key, value = reader.read(filename_queue)
41+
42+
record_bytes = tf.decode_raw(value, tf.uint8)
43+
44+
label = tf.slice(record_bytes, [0], [label_bytes])
45+
label = tf.cast(label, tf.int32)
46+
47+
image_raw = tf.slice(record_bytes, [label_bytes], [image_bytes])
48+
image_raw = tf.reshape(image_raw, [img_depth, img_height, img_width])
49+
image = tf.transpose(image_raw, (1,2,0)) # convert from D/H/W to H/W/D
50+
image = tf.cast(image, tf.float32)
51+
52+
53+
# # data argumentation
54+
55+
# image = tf.random_crop(image, [24, 24, 3])# randomly crop the image size to 24 x 24
56+
# image = tf.image.random_flip_left_right(image)
57+
# image = tf.image.random_brightness(image, max_delta=63)
58+
# image = tf.image.random_contrast(image,lower=0.2,upper=1.8)
59+
60+
61+
62+
image = tf.image.per_image_standardization(image) #substract off the mean and divide by the variance
63+
64+
65+
if shuffle:
66+
images, label_batch = tf.train.shuffle_batch(
67+
[image, label],
68+
batch_size = batch_size,
69+
num_threads= 64,
70+
capacity = 20000,
71+
min_after_dequeue = 3000)
72+
else:
73+
images, label_batch = tf.train.batch(
74+
[image, label],
75+
batch_size = batch_size,
76+
num_threads = 64,
77+
capacity= 2000)
78+
## ONE-HOT
79+
n_classes = 10
80+
label_batch = tf.one_hot(label_batch, depth= n_classes)
81+
label_batch = tf.cast(label_batch, dtype=tf.int32)
82+
label_batch = tf.reshape(label_batch, [batch_size, n_classes])
83+
84+
return images, label_batch
85+
#%%
86+
87+
88+
89+
90+
91+
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
2+
import tensorflow as tf
3+
import numpy as np
4+
import matplotlib.pyplot as plt
5+
from PIL import Image
6+
import tools
7+
8+
9+
#%%
10+
def show_feature_map():
11+
cat = plt.imread('cat.jpg') #unit8
12+
plt.imshow(cat)
13+
cat = tf.cast(cat, tf.float32) #[360, 300, 3]
14+
x = tf.reshape(cat, [1, 360, 300,3]) #[1, 360, 300, 3]
15+
16+
out = 25
17+
18+
with tf.variable_scope('conv1'):
19+
w = tools.weight([3,3,3,out], is_uniform=True)
20+
x_w = tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME')
21+
b = tools.bias([out])
22+
x_b = tf.nn.bias_add(x_w, b)
23+
x_relu = tf.nn.relu(x_b)
24+
25+
n_feature = int(x_w.get_shape()[-1])
26+
sess = tf.Session()
27+
sess.run(tf.global_variables_initializer())
28+
feature_map = tf.reshape(x_w, [360,300,out])
29+
images = tf.image.convert_image_dtype (feature_map, dtype=tf.uint8)
30+
images = sess.run(images)
31+
32+
plt.figure(figsize=(10, 10))
33+
for i in np.arange(0, n_feature):
34+
plt.subplot(5, 5, i + 1)
35+
plt.axis('off')
36+
plt.imshow(images[:,:,i])
37+
plt.show()
38+
39+
40+
show_feature_map()
41+
42+
43+
44+
#%%
45+
def show_rich_feature():
46+
cat = plt.imread('cat.jpg') #unit8
47+
plt.imshow(cat)
48+
cat = tf.cast(cat, tf.float32) #[360, 300, 3]
49+
x = tf.reshape(cat, [1, 360, 300,3]) #[1, 360, 300, 3]
50+
51+
with tf.variable_scope('conv1_1', reuse=True):
52+
w1 = tf.get_variable('weights', (3,3,3,64))
53+
b1 = tf.get_variable('biases', (64))
54+
55+
x_w = tf.nn.conv2d(x, w1, strides=[1, 1, 1, 1], padding='SAME')
56+
x_b = tf.nn.bias_add(x_w, b1)
57+
x_relu = tf.nn.relu(x_b)
58+
59+
out = 64
60+
61+
n_feature = int(x_w.get_shape()[-1])
62+
sess = tf.Session()
63+
sess.run(tf.global_variables_initializer())
64+
feature_map = tf.reshape(x_relu, [360,300,out])
65+
images = tf.image.convert_image_dtype (feature_map, dtype=tf.uint8)
66+
images = sess.run(images)
67+
68+
plt.figure(figsize=(10, 10))
69+
for i in np.arange(0, 25):
70+
plt.subplot(5, 5, i + 1)
71+
plt.axis('off')
72+
plt.imshow(images[:,:,i])
73+
plt.show()
74+
#%%
75+
76+
77+
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
2+
import tensorflow as tf
3+
import matplotlib.pyplot as plt
4+
import tools
5+
6+
#%%
7+
cat = plt.imread('cat.jpg') #unit8
8+
plt.imshow(cat)
9+
cat = tf.cast(cat, tf.float32) #[360, 300, 3]
10+
x = tf.reshape(cat, [1, 360, 300, 3]) #[1, 360, 300, 3]
11+
12+
#%%
13+
14+
# First conv
15+
with tf.variable_scope('conv1'):
16+
w = tools.weight([3,3,3,16], is_uniform=True)
17+
x_w = tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME')
18+
19+
b = tools.bias([16])
20+
x_b = tf.nn.bias_add(x_w, b)
21+
22+
x_relu = tf.nn.relu(x_b)
23+
24+
x_pool = tools.pool('test1', x_relu, kernel=[1,2,2,1], stride=[1,2,2,1],is_max_pool=True)
25+
26+
# Second conv
27+
with tf.variable_scope('conv2'):
28+
w2 = tools.weight([3,3,16,32], is_uniform=True)
29+
x_w2 = tf.nn.conv2d(x_pool, w2, strides=[1, 1, 1, 1], padding='SAME')
30+
31+
b2 = tools.bias([32])
32+
x_b2 = tf.nn.bias_add(x_w2, b2)
33+
34+
x_relu2 = tf.nn.relu(x_b2)
35+
36+
x_pool2 = tools.pool('test2',x_relu2, kernel=[1,2,2,1],stride=[1,2,2,1], is_max_pool=False)
37+
38+
x_BN = tools.batch_norm(x_pool2)
39+
40+
#%%
41+
def shape(x):
42+
return str(x.get_shape())
43+
44+
## First conv
45+
print('\n')
46+
print('** First conv: **\n')
47+
print('input size: ', shape(x))
48+
print('w size:', shape(w))
49+
print('x_w size: ', shape(x_w))
50+
print('b size: ', shape(b))
51+
print('x_b size: ', shape(x_b))
52+
print('x_relu size: ', shape(x_relu))
53+
print('x_pool size: ', shape(x_pool))
54+
print('\n')
55+
56+
## Second conv
57+
print('** Second conv: **\n')
58+
print('input size: ', shape(x_pool))
59+
print('w2 size:', shape(w2))
60+
print('x_w2 size: ', shape(x_w2))
61+
print('b2 size: ', shape(b2))
62+
print('x_b2 size: ', shape(x_b2))
63+
print('x_relu2 size: ', shape(x_relu2))
64+
print('x_pool2 size: ', shape(x_pool2))
65+
print('x_BN size: ', shape(x_BN))
66+
print('\n')
67+
68+
69+
70+
71+
72+
73+
74+
75+
76+
77+
78+
79+
80+
81+
82+
83+
84+
85+
86+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import tensorflow as tf
2+
3+
#%%
4+
5+
# First conv
6+
with tf.variable_scope('conv1'):
7+
w = tf.get_variable(name='weights',
8+
shape=[3,3,3,16],
9+
initializer=tf.contrib.layers.xavier_initializer(), trainable=False)
10+
b = tf.get_variable(name='biases',
11+
shape=[16],
12+
initializer=tf.constant_initializer(0.0), trainable=False)
13+
14+
print('w name: ', w.name)
15+
print('b name:', b.name)
16+
17+
with tf.name_scope('conv1'):
18+
w = tf.get_variable(name='weights',
19+
shape=[3,3,3,16],
20+
initializer=tf.contrib.layers.xavier_initializer())
21+
b = tf.get_variable(name='biases',
22+
shape=[16],
23+
initializer=tf.constant_initializer(0.0))
24+
25+
print('w name: ', w.name)
26+
print('b name:', b.name)
27+
28+
29+
30+
31+

0 commit comments

Comments
 (0)