本文转自:http://blog.csdn.net/eagelangel/article/details/51009682?ref=myread
python调用caffe接口进行classify时提示Mean shape incompatible with input shape错误的解决方法:
第一种:将 mean = np.load(args.mean_file),改成 mean = np.load(args.mean_file).mean(1).mean(1)
第二种:修改caffe中python接口的源代码caffe/python/caffe/io.py,将
<code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: inherit;"><span style="font-size:18px;"><span class="kwd" style="margin: 0px; padding: 0px; border: 0px; color: rgb(0, 0, 139);">if</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: black;"> ms </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">!=</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: black;"> self</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">.</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: black;">inputs</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">[</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: black;">in_</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">][</span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; color: maroon;">1</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">:]:</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: black;"> </span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px; color: rgb(0, 0, 139);">raise</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: black;"> </span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; color: rgb(43, 145, 175);">ValueError</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">(</span><span class="str" style="margin: 0px; padding: 0px; border: 0px; color: maroon;">'Mean shape incompatible with input shape.'</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">)</span></span></code>修改为
<code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: inherit;"><span style="font-size:18px;"><span class="kwd" style="margin: 0px; padding: 0px; border: 0px; color: rgb(0, 0, 139);">if</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: black;"> ms </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">!=</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: black;"> self</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">.</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: black;">inputs</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">[</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: black;">in_</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">][</span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; color: maroon;">1</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">:]:</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: black;"> </span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px; color: rgb(0, 0, 139);">print</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">(</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: black;">self</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">.</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: black;">inputs</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">[</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: black;">in_</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">])</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: black;"> in_shape </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">=</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: black;"> self</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">.</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: black;">inputs</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">[</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: black;">in_</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">][</span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; color: maroon;">1</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">:]</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: black;"> m_min</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">,</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: black;"> m_max </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">=</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: black;"> mean</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">.</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: black;">min</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">(),</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: black;"> mean</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">.</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: black;">max</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">()</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: black;"> normal_mean </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">=</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: black;"> </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">(</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: black;">mean </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">-</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: black;"> m_min</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">)</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: black;"> </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">/</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: black;"> </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">(</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: black;">m_max </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">-</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: black;"> m_min</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">)</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: black;"> mean </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">=</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: black;"> resize_image</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">(</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: black;">normal_mean</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">.</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: black;">transpose</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">((</span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; color: maroon;">1</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">,</span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; color: maroon;">2</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">,</span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; color: maroon;">0</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">)),</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: black;">in_shape</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">[</span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; color: maroon;">1</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">:]).</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: black;">transpose</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">((</span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; color: maroon;">2</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">,</span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; color: maroon;">0</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">,</span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; color: maroon;">1</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">))</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: black;"> </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">*</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: black;"> </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">(</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: black;">m_max </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">-</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: black;"> m_min</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">)</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: black;"> </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: black;">+</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: black;"> m_min </span><span class="com" style="margin: 0px; padding: 0px; border: 0px; color: gray;">#raise ValueError('Mean shape incompatible with input shape.')</span></span></code>并重新编译caffe-python接口