Skip to content

Commit 3a133ae

Browse files
committed
Initial Commit
0 parents  commit 3a133ae

7 files changed

+755
-0
lines changed

create_caffemodel.py

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
from __future__ import print_function, division
2+
caffe_root = '/TUB/robo/caffe-master/'
3+
4+
import sys
5+
sys.path.insert(0, caffe_root+'python')
6+
import caffe
7+
import numpy as np
8+
9+
#load the data file
10+
data_file = np.load('vgg_net_19_112.npy')
11+
12+
#get the weights and biases out of the array
13+
#the weights have to be transposed because of differences between Caffe and Tensorflow
14+
#format filter weights:
15+
#Tensorflow: [height (0), width (1), depth (2), number of filters (3)]
16+
#Caffe: [number of filters (3), depth (2), height (0), width (1)]
17+
weights1 = data_file[0][0].transpose((3,2,0,1))
18+
bias1 = data_file[0][1]
19+
20+
weights2 = data_file[1][0].transpose((3,2,0,1))
21+
bias2 = data_file[1][1]
22+
23+
weights3 = data_file[2][0].transpose((3,2,0,1))
24+
bias3 = data_file[2][1]
25+
26+
weights4 = data_file[3][0].transpose((3,2,0,1))
27+
bias4 = data_file[3][1]
28+
29+
weights5 = data_file[4][0].transpose((3,2,0,1))
30+
bias5 = data_file[4][1]
31+
32+
weights6 = data_file[5][0].transpose((3,2,0,1))
33+
bias6 = data_file[5][1]
34+
35+
weights7 = data_file[6][0].transpose((3,2,0,1))
36+
bias7 = data_file[6][1]
37+
38+
weights8 = data_file[7][0].transpose((3,2,0,1))
39+
bias8 = data_file[7][1]
40+
41+
weights9 = data_file[8][0].transpose((3,2,0,1))
42+
bias9 = data_file[8][1]
43+
44+
weights10 = data_file[9][0].transpose((3,2,0,1))
45+
bias10 = data_file[9][1]
46+
47+
weights11 = data_file[10][0].transpose((3,2,0,1))
48+
bias11 = data_file[10][1]
49+
50+
weights12 = data_file[11][0].transpose((3,2,0,1))
51+
bias12 = data_file[11][1]
52+
53+
weights13 = data_file[12][0].transpose((3,2,0,1))
54+
bias13 = data_file[12][1]
55+
56+
weights14 = data_file[13][0].transpose((3,2,0,1))
57+
bias14 = data_file[13][1]
58+
59+
weights15 = data_file[14][0].transpose((3,2,0,1))
60+
bias15 = data_file[14][1]
61+
62+
weights16 = data_file[15][0].transpose((3,2,0,1))
63+
bias16 = data_file[15][1]
64+
65+
#connecting the tensor after last pooling layer with the first fully-connected layer
66+
#for an explanation watch the video (youtube link with time stamp)
67+
fc1_w = data_file[16][0].reshape((4,4,512,4096))
68+
fc1_w = fc1_w.transpose((3,2,0,1))
69+
fc1_w = fc1_w.reshape((4096,8192))
70+
fc1_b = data_file[16][1]
71+
72+
#fully connected layer format:
73+
#Tensorflow: [number of inputs (0), number of outputs (1)]
74+
#Caffe: [number of outputs (1), number of inputs (0)]
75+
fc2_w = data_file[17][0].transpose((1,0))
76+
fc2_b = data_file[17][1]
77+
78+
fc3_w = data_file[18][0].transpose((1,0))
79+
fc3_b = data_file[18][1]
80+
81+
#define architecture
82+
net = caffe.Net('vgg_net_19.prototxt', caffe.TEST)
83+
84+
#load parameters
85+
net.params['conv1'][0].data[...] = weights1
86+
net.params['conv1'][1].data[...] = bias1
87+
88+
net.params['conv2'][0].data[...] = weights2
89+
net.params['conv2'][1].data[...] = bias2
90+
91+
net.params['conv3'][0].data[...] = weights3
92+
net.params['conv3'][1].data[...] = bias3
93+
94+
net.params['conv4'][0].data[...] = weights4
95+
net.params['conv4'][1].data[...] = bias4
96+
97+
net.params['conv5'][0].data[...] = weights5
98+
net.params['conv5'][1].data[...] = bias5
99+
100+
net.params['conv6'][0].data[...] = weights6
101+
net.params['conv6'][1].data[...] = bias6
102+
103+
net.params['conv7'][0].data[...] = weights7
104+
net.params['conv7'][1].data[...] = bias7
105+
106+
net.params['conv8'][0].data[...] = weights8
107+
net.params['conv8'][1].data[...] = bias8
108+
109+
net.params['conv9'][0].data[...] = weights9
110+
net.params['conv9'][1].data[...] = bias9
111+
112+
net.params['conv10'][0].data[...] = weights10
113+
net.params['conv10'][1].data[...] = bias10
114+
115+
net.params['conv11'][0].data[...] = weights11
116+
net.params['conv11'][1].data[...] = bias11
117+
118+
net.params['conv12'][0].data[...] = weights12
119+
net.params['conv12'][1].data[...] = bias12
120+
121+
net.params['conv13'][0].data[...] = weights13
122+
net.params['conv13'][1].data[...] = bias13
123+
124+
net.params['conv14'][0].data[...] = weights14
125+
net.params['conv14'][1].data[...] = bias14
126+
127+
net.params['conv15'][0].data[...] = weights15
128+
net.params['conv15'][1].data[...] = bias15
129+
130+
net.params['conv16'][0].data[...] = weights16
131+
net.params['conv16'][1].data[...] = bias16
132+
133+
net.params['fc1'][0].data[...] = fc1_w
134+
net.params['fc1'][1].data[...] = fc1_b
135+
136+
net.params['fc2'][0].data[...] = fc2_w
137+
net.params['fc2'][1].data[...] = fc2_b
138+
139+
net.params['fc3'][0].data[...] = fc3_w
140+
net.params['fc3'][1].data[...] = fc3_b
141+
142+
#save caffemodel
143+
net.save('vgg_net_19.caffemodel')

export_parameters.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import tflearn
2+
import numpy as np
3+
4+
from model import vgg_net_19
5+
6+
MODEL_NAME = 'vgg_net_19.model'
7+
8+
#load network architecture and parameters
9+
MODEL = vgg_net_19(112, 112)
10+
MODEL.load(MODEL_NAME)
11+
12+
#the names of the layers we want to extract
13+
LAYER_ARRAY = ['Conv2D', 'Conv2D_1', 'Conv2D_2', 'Conv2D_3', 'Conv2D_4', 'Conv2D_5', 'Conv2D_6', 'Conv2D_7', 'Conv2D_8', 'Conv2D_9',
14+
'Conv2D_10', 'Conv2D_11', 'Conv2D_12', 'Conv2D_13', 'Conv2D_14', 'Conv2D_15', 'FullyConnected', 'FullyConnected_1', 'FullyConnected_2']
15+
16+
def get_tf_weights(model, layer_array):
17+
data_file = [] #contains weights and biases
18+
19+
for layer in layer_array:
20+
#get parameters of a certain layer
21+
conv2d_vars = tflearn.variables.get_layer_variables_by_name(layer)
22+
#get weights out of the parameters
23+
weights = model.get_weights(conv2d_vars[0])
24+
#get biases out of the parameters
25+
biases = model.get_weights(conv2d_vars[1])
26+
#combine layer parameters in an array
27+
layer_pair = [weights, biases]
28+
#append array to data file
29+
data_file.append(layer_pair)
30+
31+
#save the data file
32+
np.save('vgg_net_19_112.npy', data_file)
33+
34+
get_tf_weights(MODEL, LAYER_ARRAY)

model.py

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
from tflearn import input_data, conv_2d, max_pool_2d, fully_connected, dropout, Momentum, regression, DNN
2+
3+
#model of vgg-19
4+
def vgg_net_19(width, height):
5+
network = input_data(shape=[None, height, width, 3], name='input')
6+
network = conv_2d(network, 64, 3, activation = 'relu', regularizer='L2', weight_decay=5e-4)
7+
network = conv_2d(network, 64, 3, activation = 'relu', regularizer='L2', weight_decay=5e-4)
8+
network = max_pool_2d(network, 2, strides=2)
9+
network = conv_2d(network, 128, 3, activation = 'relu', regularizer='L2', weight_decay=5e-4)
10+
network = conv_2d(network, 128, 3, activation = 'relu', regularizer='L2', weight_decay=5e-4)
11+
network = max_pool_2d(network, 2, strides=2)
12+
network = conv_2d(network, 256, 3, activation = 'relu', regularizer='L2', weight_decay=5e-4)
13+
network = conv_2d(network, 256, 3, activation = 'relu', regularizer='L2', weight_decay=5e-4)
14+
network = conv_2d(network, 256, 3, activation = 'relu', regularizer='L2', weight_decay=5e-4)
15+
network = conv_2d(network, 256, 3, activation = 'relu', regularizer='L2', weight_decay=5e-4)
16+
network = max_pool_2d(network, 2, strides=2)
17+
network = conv_2d(network, 512, 3, activation = 'relu', regularizer='L2', weight_decay=5e-4)
18+
network = conv_2d(network, 512, 3, activation = 'relu', regularizer='L2', weight_decay=5e-4)
19+
network = conv_2d(network, 512, 3, activation = 'relu', regularizer='L2', weight_decay=5e-4)
20+
network = conv_2d(network, 512, 3, activation = 'relu', regularizer='L2', weight_decay=5e-4)
21+
network = max_pool_2d(network, 2, strides=2)
22+
network = conv_2d(network, 512, 3, activation = 'relu', regularizer='L2', weight_decay=5e-4)
23+
network = conv_2d(network, 512, 3, activation = 'relu', regularizer='L2', weight_decay=5e-4)
24+
network = conv_2d(network, 512, 3, activation = 'relu', regularizer='L2', weight_decay=5e-4)
25+
network = conv_2d(network, 512, 3, activation = 'relu', regularizer='L2', weight_decay=5e-4)
26+
network = max_pool_2d(network, 2, strides=2)
27+
network = fully_connected(network, 4096, activation='relu', weight_decay=5e-4)
28+
network = dropout(network, keep_prob=0.5)
29+
network = fully_connected(network, 4096, activation='relu', weight_decay=5e-4)
30+
network = dropout(network, keep_prob=0.5)
31+
network = fully_connected(network, 1000, activation='softmax', weight_decay=5e-4)
32+
33+
opt = Momentum(learning_rate=0, momentum = 0.9)
34+
network = regression(network, optimizer=opt, loss='categorical_crossentropy', name='targets')
35+
36+
model = DNN(network, checkpoint_path='', max_checkpoints=1, tensorboard_verbose=2, tensorboard_dir='')
37+
38+
return model
39+
40+
#model of vgg-19 for testing of the activations
41+
#rename the output you want to test, connect it to the next layer and change the output layer at the bottom (model = DNN(...))
42+
#make sure to use the correct test function (depending if your output is a tensor or a vector)
43+
def vgg_net_19_activations(width, height):
44+
network = input_data(shape=[None, height, width, 3], name='input')
45+
network1 = conv_2d(network, 64, 3, activation = 'relu', regularizer='L2', weight_decay=5e-4)
46+
network2 = conv_2d(network1, 64, 3, activation = 'relu', regularizer='L2', weight_decay=5e-4)
47+
network = max_pool_2d(network2, 2, strides=2)
48+
network = conv_2d(network, 128, 3, activation = 'relu', regularizer='L2', weight_decay=5e-4)
49+
network = conv_2d(network, 128, 3, activation = 'relu', regularizer='L2', weight_decay=5e-4)
50+
network = max_pool_2d(network, 2, strides=2)
51+
network = conv_2d(network, 256, 3, activation = 'relu', regularizer='L2', weight_decay=5e-4)
52+
network = conv_2d(network, 256, 3, activation = 'relu', regularizer='L2', weight_decay=5e-4)
53+
network = conv_2d(network, 256, 3, activation = 'relu', regularizer='L2', weight_decay=5e-4)
54+
network = conv_2d(network, 256, 3, activation = 'relu', regularizer='L2', weight_decay=5e-4)
55+
network = max_pool_2d(network, 2, strides=2)
56+
network = conv_2d(network, 512, 3, activation = 'relu', regularizer='L2', weight_decay=5e-4)
57+
network = conv_2d(network, 512, 3, activation = 'relu', regularizer='L2', weight_decay=5e-4)
58+
network = conv_2d(network, 512, 3, activation = 'relu', regularizer='L2', weight_decay=5e-4)
59+
network = conv_2d(network, 512, 3, activation = 'relu', regularizer='L2', weight_decay=5e-4)
60+
network = max_pool_2d(network, 2, strides=2)
61+
network = conv_2d(network, 512, 3, activation = 'relu', regularizer='L2', weight_decay=5e-4)
62+
network = conv_2d(network, 512, 3, activation = 'relu', regularizer='L2', weight_decay=5e-4)
63+
network = conv_2d(network, 512, 3, activation = 'relu', regularizer='L2', weight_decay=5e-4)
64+
network = conv_2d(network, 512, 3, activation = 'relu', regularizer='L2', weight_decay=5e-4)
65+
network = max_pool_2d(network, 2, strides=2)
66+
network = fully_connected(network, 4096, activation='relu', weight_decay=5e-4)
67+
network = dropout(network, keep_prob=0.5)
68+
network = fully_connected(network, 4096, activation='relu', weight_decay=5e-4)
69+
network = dropout(network, keep_prob=0.5)
70+
network = fully_connected(network, 1000, activation='softmax', weight_decay=5e-4)
71+
72+
opt = Momentum(learning_rate=0, momentum = 0.9)
73+
network = regression(network, optimizer=opt, loss='categorical_crossentropy', name='targets')
74+
75+
model = DNN(network1, checkpoint_path='', max_checkpoints=1, tensorboard_verbose=2, tensorboard_dir='')
76+
77+
return model

save_model.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from model import vgg_net_19
2+
3+
#create an initialized model (random weights, bias = 0) and save it
4+
model = vgg_net_19(112,112)
5+
model.save('vgg_net_19.model')

test_network_caffe.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#if you use python2 for caffe and python3 for tensorflow you have to import division,
2+
#otherwise the results will not be equal
3+
from __future__ import division
4+
5+
caffe_root = '/TUB/robo/caffe-master/'
6+
7+
import sys
8+
sys.path.insert(0, caffe_root + 'python')
9+
import caffe
10+
import numpy as np
11+
import cv2
12+
13+
WIDTH = 112
14+
HEIGHT = 112
15+
16+
#load architecture and parameters
17+
net = caffe.Net('vgg_net_19.prototxt', 'vgg_net_19.caffemodel', caffe.TEST)
18+
19+
# load input and configure preprocessing
20+
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
21+
22+
#transposes image coming from opencv
23+
#opencv: [height (0), width (1), channels (2)]
24+
#caffe: [batch size (x), channels (2), height (0), width (1)]
25+
transformer.set_transpose('data', (2,0,1))
26+
27+
#add batch size (1, since we test just one image)
28+
net.blobs['data'].reshape(1,3,HEIGHT,WIDTH)
29+
30+
#load image
31+
img_name = 'test_img.png'
32+
img = cv2.imread(img_name)
33+
34+
#load the image in the data layer
35+
net.blobs['data'].data[...] = transformer.preprocess('data', img)
36+
37+
#compute forward pass
38+
out = net.forward()
39+
40+
output = out['prob1']
41+
print 'output'
42+
print output
43+
44+
print ''
45+
print 'activations first convolutional layer'
46+
#get the activations of the layer with the name 'conv1' (defined in prototxt)
47+
conv1_activations = net.blobs['conv1'].data
48+
print conv1_activations

test_network_tensorflow.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import numpy as np
2+
import cv2
3+
4+
from model import vgg_net_19, vgg_net_19_activations
5+
6+
MODEL_SAVE_PATH = 'vgg_net_19.model'
7+
8+
IMG_NAME = 'test_img.png'
9+
10+
WIDTH = 112
11+
HEIGHT = 112
12+
13+
#for testing a vector output
14+
def test_model_vector_output():
15+
#load architecture and parameters
16+
model = vgg_net_19(WIDTH, HEIGHT)
17+
model.load(MODEL_SAVE_PATH)
18+
19+
#load image, add batch size and predict
20+
image = cv2.imread(IMG_NAME)
21+
image = image.reshape(1, HEIGHT, WIDTH, 3)
22+
output = model.predict(image)
23+
24+
print(output)
25+
26+
#for testing a tensor output (will output caffe format)
27+
def test_model_tensor_output():
28+
#load architecture and parameters
29+
model = vgg_net_19_activations(WIDTH, HEIGHT)
30+
model.load(MODEL_SAVE_PATH)
31+
32+
#load image, add batch size and predict
33+
image = cv2.imread(IMG_NAME)
34+
image = image.reshape(1, HEIGHT, WIDTH, 3)
35+
output = model.predict(image)
36+
37+
#conversion to caffe format
38+
#output format tensor:
39+
#Tensorflow: [batch size (0), height (1), width (2), depth (3)]
40+
#Caffe: [batch size (0), depth (3), height (1), width (2)]
41+
output = output.transpose((0,3,1,2))
42+
43+
print(output)
44+
45+
#test_model_vector_output()
46+
test_model_tensor_output()

0 commit comments

Comments
 (0)