python - 将 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 维时出现错误。
解决方案
转置时是 (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 )
试试这个
推荐阅读
- java - 如何使用 Spring Data JPA 正确外部化本机 SQL 查询
- java - 更改列表中的单元格的功能
- c++ - myComboBox->Dismiss(); 在 wxEVT_TEXT 和 wxEVT_COMBOBOX_DROPDOWN 下不工作
- php - Laravel 8 用户表列名
- r - 用于 R 中 misc3d 的数据帧到数组
- sml - 错误 SML:错误:未绑定的变量或构造函数:valof
- android - :app:generateCrashlyticsSymbolFileRelease 错误:generateCrashlyticsSymbolFileRelease 所需的未剥离本机库路径不存在:
- kotlin - 检查其中一个值是否包含换行符
- javascript - 从选项选择列表 Html-javascript 中删除一些“选项”
- node.js - 我如何将 MongoDB 聚合匹配用于数组以使用分页