【边喝caffee边Caffe 】(二)python版本的报错:Mean shape incompatible with input shape

本文介绍了一种解决Caffe Python示例中出现的“Mean shape incompatible with input shape”错误的方法。该问题源于提供的平均值(mean)与输入尺寸不匹配,通过调整io.py文件中的代码实现自动调整mean尺寸,使其实现与输入尺寸一致。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在运行Ubuntu上的python版本,希望对比一下C++的结果是否正确时,发现一直报错Mean shape incompatible with input shape

后来遍查网络,发现了一个解决方法,见

http://blog.youkuaiyun.com/gzljss/article/details/45849013

直接贴过来

------------------------------------------分割线-----------------------------------------

解决方法参考http://stackoverflow.com/questions/29842935/mean-shape-incompatible-with-input-shape-caffe-classification-error-in-io-py

在运行caffe的python例子时,命令行输入

[html]  view plain  copy
  1. python classify.py cat.jpg  foo  

报错

ValueError: Mean shape incompatible with input shape.


原因是‘mean’与‘input’的尺寸不同造成的。

参考前面的文章,修改了 ./python/caffe/io.py 文件

将:

[html]  view plain  copy
  1. if ms != self.inputs[in_][1:]:  
  2. raise ValueError('Mean shape incompatible with input shape.')  
 

修改为:

[python]  view plain  copy
  1. if ms != self.inputs[in_][1:]:  
  2. print(self.inputs[in_])  
  3. in_shape = self.inputs[in_][1:]  
  4. m_min, m_max = mean.min(), mean.max()  
  5. normal_mean = (mean - m_min) / (m_max - m_min)  
  6. mean = resize_image(normal_mean.transpose((1,2,0)),  
  7.     in_shape[1:]).transpose((2,0,1)) * \  
  8.    (m_max - m_min) + m_min  
  9. #raise ValueError('Mean shape incompatible with input shape.')  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值