|
from keras.models import Sequential |
|
from keras.layers.core import Flatten, Dense, Dropout |
|
from keras.layers.convolutional import Convolution2D, MaxPooling2D, ZeroPadding2D |
|
from keras.optimizers import SGD |
|
import cv2, numpy as np |
|
|
|
def VGG_16(weights_path=None): |
|
model = Sequential() |
|
model.add(ZeroPadding2D((1,1),input_shape=(3,224,224))) |
|
model.add(Convolution2D(64, 3, 3, activation='relu')) |
|
model.add(ZeroPadding2D((1,1))) |
|
model.add(Convolution2D(64, 3, 3, activation='relu')) |
|
model.add(MaxPooling2D((2,2), strides=(2,2))) |
|
|
|
model.add(ZeroPadding2D((1,1))) |
|
model.add(Convolution2D(128, 3, 3, activation='relu')) |
|
model.add(ZeroPadding2D((1,1))) |
|
model.add(Convolution2D(128, 3, 3, activation='relu')) |
|
model.add(MaxPooling2D((2,2), strides=(2,2))) |
|
|
|
model.add(ZeroPadding2D((1,1))) |
|
model.add(Convolution2D(256, 3, 3, activation='relu')) |
|
model.add(ZeroPadding2D((1,1))) |
|
model.add(Convolution2D(256, 3, 3, activation='relu')) |
|
model.add(ZeroPadding2D((1,1))) |
|
model.add(Convolution2D(256, 3, 3, activation='relu')) |
|
model.add(MaxPooling2D((2,2), strides=(2,2))) |
|
|
|
model.add(ZeroPadding2D((1,1))) |
|
model.add(Convolution2D(512, 3, 3, activation='relu')) |
|
model.add(ZeroPadding2D((1,1))) |
|
model.add(Convolution2D(512, 3, 3, activation='relu')) |
|
model.add(ZeroPadding2D((1,1))) |
|
model.add(Convolution2D(512, 3, 3, activation='relu')) |
|
model.add(MaxPooling2D((2,2), strides=(2,2))) |
|
|
|
model.add(ZeroPadding2D((1,1))) |
|
model.add(Convolution2D(512, 3, 3, activation='relu')) |
|
model.add(ZeroPadding2D((1,1))) |
|
model.add(Convolution2D(512, 3, 3, activation='relu')) |
|
model.add(ZeroPadding2D((1,1))) |
|
model.add(Convolution2D(512, 3, 3, activation='relu')) |
|
model.add(MaxPooling2D((2,2), strides=(2,2))) |
|
|
|
model.add(Flatten()) |
|
model.add(Dense(4096, activation='relu')) |
|
model.add(Dropout(0.5)) |
|
model.add(Dense(4096, activation='relu')) |
|
model.add(Dropout(0.5)) |
|
model.add(Dense(1000, activation='softmax')) |
|
|
|
if weights_path: |
|
model.load_weights(weights_path) |
|
|
|
return model |
|
|
|
if __name__ == "__main__": |
|
im = cv2.resize(cv2.imread('cat.jpg'), (224, 224)).astype(np.float32) |
|
im[:,:,0] -= 103.939 |
|
im[:,:,1] -= 116.779 |
|
im[:,:,2] -= 123.68 |
|
im = im.transpose((2,0,1)) |
|
im = np.expand_dims(im, axis=0) |
|
|
|
# Test pretrained model |
|
model = VGG_16('vgg16_weights.h5') |
|
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True) |
|
model.compile(optimizer=sgd, loss='categorical_crossentropy') |
|
out = model.predict(im) |
|
print np.argmax(out) |
Hey,
I have my own model and weights in tensorflow. I am trying to assign these weights to caffe model but when I assign weights to the caffe layer in prototxt to the fully connected layer it gives an error.
`weights1 = data_file[0][0].transpose((3,2,0,1))
bias1 = data_file[0][1]
weights2 = data_file[1][0].transpose((3,2,0,1))
bias2 = data_file[1][1]
weights3 = data_file[2][0].transpose((3,2,0,1))
bias3 = data_file[2][1]
#connecting the tensor after last pooling layer with the first fully-connected layer
#here is the link to the video where this part is explained (https://youtu.be/kvXHOIn3-8s?t=3m38s)
print(np.shape(data_file[3][0]))
fc1_w = data_file[3][0].reshape((28,28,64,120))
fc1_w = fc1_w.transpose((3,2,0,1))
fc1_w = fc1_w.reshape((120,50176))
fc1_b = data_file[3][1]
#fully connected layer format:
#Tensorflow: [number of inputs (0), number of outputs (1)]
#Caffe: [number of outputs (1), number of inputs (0)]
fc2_w = data_file[4][0].transpose((1,0))
fc2_b = data_file[4][1]
fc3_w = data_file[5][0].transpose((1,0))
fc3_b = data_file[5][1]
#define architecture
print('Start creating caffe model')
net = caffe.Net('./LeNet.prototxt', caffe.TEST)
#load parameters
net.params['conv2d_1'][0].data[...] = weights1
net.params['conv2d_1'][1].data[...] = bias1
net.params['conv2d_2'][0].data[...] = weights2
net.params['conv2d_2'][1].data[...] = bias2
net.params['conv2d_3'][0].data[...] = weights3
net.params['conv2d_3'][1].data[...] = bias3
net.params['dense_1'][0].data[...]= fc1_w
net.params['dense_1'][1].data[...] = fc1_b
net.params['dense_2'][0].data[...] = fc2_w
net.params['dense_2'][1].data[...] = fc2_b
net.params['dense_3'][0].data[...] = fc3_w
net.params['dense_3'][1].data[...] = fc3_b
print('End')`
Error
`runfile('/home/rehman/Tensorflow2caffe/create_caffemdoel.py', wdir='/home/rehman/Tensorflow2caffe')
Start creating caffe model
Traceback (most recent call last):
File "", line 1, in
runfile('/home/rehman/Tensorflow2caffe/create_caffemdoel.py', wdir='/home/rehman/Tensorflow2caffe')
File "/usr/lib/python3/dist-packages/spyder/utils/site/sitecustomize.py", line 705, in runfile
execfile(filename, namespace)
File "/usr/lib/python3/dist-packages/spyder/utils/site/sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "/home/rehman/Tensorflow2caffe/create_caffemdoel.py", line 64, in
net.params['dense_1'][0].data[...]= fc1_w
ValueError: could not broadcast input array from shape (120,50176) into shape (120,64)`
Here is my prototxt
`name: 'LeNet'
input: 'data'
input_shape{
dim: 1 #no of images
dim: 3 #no of channels
dim :224 #width
dim :224
}
layer {
name: "conv2d_1"
type: "Convolution"
bottom: "data"
top: "conv2d_1"
convolution_param{
num_output: 64
kernel_size: 5
stride: 2
pad: 0
}
}
layer {
name: "activation_1"
type: "ReLU"
bottom: "conv2d_1"
top: "conv2d_1"
}
layer {
name: "max_pooling2d_1"
type: "Pooling"
bottom: "conv2d_1"
top: "max_pooling2d_1"
pooling_param{
pool :MAX
kernel_size: 5
stride: 2
pad: 0
}
}
layer {
name: "conv2d_2"
type: "Convolution"
bottom: "max_pooling2d_1"
top: "conv2d_2"
convolution_param{
num_output: 16
kernel_size: 5
stride: 2
pad: 0
}
}
layer {
name: "activation_2"
type: "ReLU"
bottom: "conv2d_2"
top: "conv2d_2"
}
layer {
name: "max_pooling2d_2"
type: "Pooling"
bottom: "conv2d_2"
top: "max_pooling2d_2"
pooling_param{
pool :MAX
kernel_size: 5
stride: 2
pad: 0
}
}
layer {
name: "conv2d_3"
type: "Convolution"
bottom: "max_pooling2d_2"
top: "conv2d_3"
convolution_param{
num_output: 64
kernel_size: 5
stride: 2
pad: 0
}
}
layer {
name: "activation_3"
type: "ReLU"
bottom: "conv2d_3"
top: "conv2d_3"
}
layer {
name: "max_pooling2d_3"
type: "Pooling"
bottom: "conv2d_3"
top: "max_pooling2d_3"
pooling_param{
pool :MAX
kernel_size: 5
stride: 2
pad: 0
}
}
layer{
name: "flatten_1"
type: "Reshape"
bottom: "max_pooling2d_3"
top: "flatten_1"
reshape_param{shape:{dim:-1 dim:1 dim:1 dim:1}}
}
layer {
name: "dense_1"
type: "InnerProduct"
bottom:"flatten_1"
top:"dense_1"
inner_product_param {
num_output: 120
}
param {
lr_mult: 1
}
param {
lr_mult: 2
}
}
layer {
name: "activation_4"
type: "ReLU"
bottom: "dense_1"
top: "dense_1"
}
layer {
name: "dense_2"
type: "InnerProduct"
bottom:"dense_1"
top:"dense_2"
inner_product_param {
num_output: 64
}
}
layer {
name: "activation_5"
type: "ReLU"
bottom: "dense_2"
top: "dense_2"
}
layer {
name: "dense_3"
type: "InnerProduct"
bottom:"dense_2"
top:"dense_3"
inner_product_param {
num_output: 10
}
}
layer {
name: "activation_6"
type: "Softmax"
bottom: "dense_3"
top: "dense_3"
}
`
and my model description is this:
`Layer (type) Output Shape Param #
conv2d_1 (Conv2D) (None, 224, 224, 64) 4864
activation_1 (Activation) (None, 224, 224, 64) 0
max_pooling2d_1 (MaxPooling2 (None, 112, 112, 64) 0
dropout_1 (Dropout) (None, 112, 112, 64) 0
conv2d_2 (Conv2D) (None, 112, 112, 16) 25616
activation_2 (Activation) (None, 112, 112, 16) 0
max_pooling2d_2 (MaxPooling2 (None, 56, 56, 16) 0
conv2d_3 (Conv2D) (None, 56, 56, 64) 25664
activation_3 (Activation) (None, 56, 56, 64) 0
max_pooling2d_3 (MaxPooling2 (None, 28, 28, 64) 0
flatten_1 (Flatten) (None, 50176) 0
dense_1 (Dense) (None, 120) 6021240
activation_4 (Activation) (None, 120) 0
dropout_2 (Dropout) (None, 120) 0
dense_2 (Dense) (None, 64) 7744
activation_5 (Activation) (None, 64) 0
dense_3 (Dense) (None, 10) 650
activation_6 (Activation) (None, 10) 0
Total params: 6,085,778
Trainable params: 6,085,778
Non-trainable params: 0
_________________________________________________________________`