使用python将Tensorflow代码转换为Caffe

时间:2018-09-14 07:36:29

标签: python tensorflow caffe

我正在研究神经风格转换(NST)。我有this github链接,可帮助我了解Real Time NST。

现在,我想将tensorflow代码转换为caffe,在转换代码后,我的输出不匹配。

tubingen.jpg enter image description here

tensorflow代码

import tensorflow as tf
import scipy
import scipy.misc
import utils
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
%matplotlib inline

##### laod and Preprocess Image
image_path  = "tubingen.jpg"
img=mpimg.imread(image_path)
plt.imshow(img)


img = scipy.misc.imread(image_path)
content_image = img.astype("float32")
reshaped_content_height = (content_image.shape[0] - content_image.shape[0] % 4)
reshaped_content_width = (content_image.shape[1] - content_image.shape[1] % 4)
reshaped_content_image = content_image[:reshaped_content_height, :reshaped_content_width, :]
reshaped_content_image = np.ndarray.reshape(reshaped_content_image, (1,) + reshaped_content_image.shape)

##### Run Tensorflow Code
with tf.Session() as sess:
    img_placeholder = tf.placeholder(tf.float32, shape=reshaped_content_image.shape,name='img_placeholder')
    reshaped_content_image = reshaped_content_image / 255.0
    weights_init = tf.Variable(tf.truncated_normal([9, 9, 3, 32], stddev=0.1, seed=1), dtype=tf.float32)
    net = tf.nn.conv2d(reshaped_content_image, weights_init, [1,1,1,1], padding='SAME')
    sess.run(tf.global_variables_initializer())
    prediction = sess.run(net, feed_dict={img_placeholder:reshaped_content_image})

####### Predict
print(prediction[0].shape)
prediction[0].flatten()[0:10]

tensorflow输出

(768, 1024, 32)
array([-0.60970634,  0.12403676, -0.35182738,  0.13260028, -0.30444232,
       -0.03567056,  0.58853114, -0.25797656, -0.15773779,  0.09053704],
      dtype=float32)

Caffe代码

import tensorflow as tf
import transform
import scipy
import scipy.misc
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
import caffe 
from caffe import layers
from caffe import params
caffe.set_random_seed(1)
%matplotlib inline

##### laod and Preprocess Image
image_path  = "tubingen.jpg"
img=mpimg.imread(image_path)
plt.imshow(img)


img = scipy.misc.imread(image_path)
content_image = img.astype("float32")
reshaped_content_height = (content_image.shape[0] - content_image.shape[0] % 4)
reshaped_content_width = (content_image.shape[1] - content_image.shape[1] % 4)
reshaped_content_image = content_image[:reshaped_content_height, :reshaped_content_width, :]
reshaped_content_image = np.ndarray.reshape(reshaped_content_image, (1,) + reshaped_content_image.shape)
#only for caffe
reshaped_content_image = np.transpose(reshaped_content_image, (0, 3,1,2))

####Create same model using caffe
net = caffe.NetSpec()
net.data = layers.Input(input_param={'shape':{'dim':[1,3,768,1024]}})
p = (9 - 1)/ 2
net.conv1 = layers.Convolution(net.data, name="conv1", kernel_size=9,num_output=32, stride=1, pad=p)


with open("prototxt/output_file.prototxt", 'w') as f:
    f.write(str(net.conv1.to_proto()))


net = caffe.Net("prototxt/output_file.prototxt", caffe.TEST)
reshaped_content_image = reshaped_content_image / 255.0
net.blobs['Input1'].data[...] = reshaped_content_image


weights_shape = list(net.params['conv1'][0].data.shape)
with tf.Session() as ses:
    weights_init = tf.Variable(tf.truncated_normal(weights_shape, stddev=0.1, seed=1), dtype=tf.float32)
    ses.run(tf.global_variables_initializer())
    out = ses.run(weights_init)

### assign same filter weights 
net.params['conv1'][0].data[...] = out   
output = net.forward()
print(output['Convolution1'].shape)
output['Convolution1'].flatten()[0:10]

Caffe输出

(1, 32, 768, 1024)
array([-0.07345342,  0.2382412 ,  0.06588577,  0.1593636 ,  0.04628869,
        0.12718415,  0.15796725,  0.07955551,  0.12399725,  0.12704358],
      dtype=float32)

在这里,我们可以看到Tensorflow和Caffe的输出有所不同,因为我使用的是同一个网络。

感谢的帮助

谢谢。

0 个答案:

没有答案