首页 > 解决方案 > 将 caffe 上的 net forward 从 MATLAB 转换为 Python 的问题

问题描述

我正在尝试将一些 MATLAB 代码转换为 Python,因为我在 MATLAB 上运行代码失败(每次运行它都会崩溃)。到目前为止,我已经完成了代码的转换并运行它,但结果非常错误。所以我尝试调试它,但部分代码仍然给我不确定性,因为我无法在 MATLAB 上检查结果

这是我要转换的 MATLAB 代码

%Prepare images
im=reshape(im,[size(im)]); im=single(im)/255;
im_data = im(:, :, [3, 2, 1]);  % permute channels from RGB to BGR
im_data = permute(im_data, [2, 1, 3]); 

%pass images  
out_im = net.forward({im_data});
n_out=out_im{2}; al_out=out_im{1}; light_out=out_im{3};

这是我创建的 Python 代码

#Prepare images (rotate,flip,change color,reshape)
im=np.reshape(im,(im.shape))
im=np.float32(im)/255
#already convert to BGR on top
#im_data = cv2.cvtColor(im, cv2.COLOR_RGB2BGR)
#Dibalik lalu di rotate 90 ke kiri
im_data = np.transpose(im, (1, 0, 2)) 
im_data=np.moveaxis(im_data, -1, 0) 
im_input = im_data[np.newaxis, : , :, :]
net.blobs['data'].reshape(*im_input.shape)
net.blobs['data'].data[...] = im_input
#pass images  
out_im = net.forward()
n_out=out_im['Nconv0'][0] 
al_out=out_im['Aconv0'][0] 
light_out=out_im['fc_light'][0]

我尝试按照 caffe 用户的教程进行操作,输入应该是 (N,channel,width,height) 所以我将输入转换为 (1,3,128,128) 但在 MATLAB 代码上我相信形状是 (width,height,渠道)。上次我尝试仅输入 3 维时出现错误。

标签: pythonmatlabcaffe

解决方案


转置时是 (2,0,1),而不是 (1,0,2)。

inputs = cv.resize(inputs,(128,128))
inputs = inputs.transpose((2,0,1))
inputs = inputs[None,:]
net.forward_all( data = inputs )

试试这个


推荐阅读