caffe-tensorflow icon indicating copy to clipboard operation
caffe-tensorflow copied to clipboard

Padding issue in GoogleNet

Open d4nst opened this issue 8 years ago • 2 comments

There is an issue when converting GoogleNet. Although the predicted labels seems to always be the same, the outputs when using Caffe and TensorFlow are different (in some cases the predicted labels are different). I tried removing all the layers except the first convolutional one and the outputs were still different. Only when I changed the padding from 3 to 0 I got the same outputs, so the problem seems to be related to the padding.

Below is the code to reproduce this:

import cv2
import numpy as np
import caffe
import tensorflow as tf
from googlenet import GoogleNet

filename = 'test.png'
mean = np.array([104, 117, 123])

# load tensorflow data
img = cv2.imread(filename)
img = img.astype('float32') - mean
tf_data = np.asarray([img])

# load caffe data
transformer = caffe.io.Transformer({'data': (1, 3, 224, 224)})
transformer.set_mean('data', mean)
transformer.set_raw_scale('data', 255)
transformer.set_transpose('data', (2, 0, 1))
transformer.set_channel_swap('data', (2, 1, 0)) 
caffe_data = transformer.preprocess('data', caffe.io.load_image(filename, color=True))[np.newaxis, :, :, :]

# check that both images are the same
print(np.max(abs(tf_data - caffe_data.transpose(0, 2, 3, 1))))

# tensorflow output
images_placeholder = tf.placeholder(tf.float32, [1, 224, 224, 3])
net = GoogleNet({'data': images_placeholder})
with tf.Session() as sess:
    net.load('googlenet.npy', sess)
    feed = {images_placeholder: tf_data}
    tensorflow_feat = sess.run(sess.graph.get_tensor_by_name('prob:0'), feed_dict=feed)

# caffe output
network = 'googlenet.prototxt'
weights = 'googlenet.caffemodel'
net = caffe.Net(network, weights, caffe.TEST)
out = net.forward()
net.blobs['data'].data[0] = caffe_data
net.forward()
caffe_feat = net.blobs['prob'].data[...]

# maximum difference between caffe and tensorflow features
print(np.max(tensorflow_feat - caffe_feat))

Edit: I've also realized that the converted model weights 28 MB instead of the 53.5 MB of the Caffe model. Something else might be wrong?

d4nst avatar Feb 20 '17 18:02 d4nst

Edit: I've also realized that the converted model weights 28 MB instead of the 53.5 MB of the Caffe model. Something else might be wrong?

I was also confused by the same thing but convinced myself that the 53.5 MB caffemodel includes parameters from early loss layers in train_val.prototxt (13.37M total params) that do not exist in deploy.prototxt (6.99M total params). Recall that each param is 4 bytes.

ghost avatar May 26 '17 22:05 ghost

Maybe related to the issue, where did you get the file "googlenet.prototxt"

XudongLinthu avatar Oct 16 '17 08:10 XudongLinthu