6

私は分類するためにpythonでcaffeを使用しています。ここからコードを取得します。ここでは、次のような単純なコードを使用します。

plt.rcParams['figure.figsize'] = (10, 10)
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'
mean_filename='./mean.binaryproto'
proto_data = open(mean_filename, "rb").read()
a = caffe.io.caffe_pb2.BlobProto.FromString(proto_data)
mean = caffe.io.blobproto_to_array(a)[0]
age_net_pretrained='./age_net.caffemodel'
age_net_model_file='./deploy_age.prototxt'
age_net = caffe.Classifier(age_net_model_file, age_net_pretrained,
mean=mean,
channel_swap=(2,1,0),
raw_scale=255,
image_dims=(256, 256))

ただし、次のようなエラーが発生しました

Traceback (most recent call last):
File "cnn_age_gender_demo.py", line 25, in 
image_dims=(256, 256))
File "/home/john/Downloads/caffe/python/caffe/classifier.py", line 34, in init
self.transformer.set_mean(in_, mean)
File "/home/john/Downloads/caffe/python/caffe/io.py", line 255, in set_mean
raise ValueError('Mean shape incompatible with input shape.')
ValueError: Mean shape incompatible with input shape.

私がそれを再愛するのを手伝ってもらえますか?ありがとう

4

4 に答える 4

16

caffe/python/caffe/io.py の 253 ~ 254 行に移動します

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

if ms != self.inputs[in_][1:]:
    print(self.inputs[in_])
    in_shape = self.inputs[in_][1:]
    m_min, m_max = mean.min(), mean.max()
    normal_mean = (mean - m_min) / (m_max - m_min)
    mean = resize_image(normal_mean.transpose((1,2,0)),in_shape[1:]).transpose((2,0,1)) * (m_max - m_min) + m_min
    #raise ValueError('Mean shape incompatible with input shape.')

再構築します。それが役立つことを願っています

于 2015-06-12T17:31:47.217 に答える
1

私は同じ問題を抱えていました。imagenet Webデモに基づいて、この方法を使用してスクリプトを変更し、95行目に平均ファイルをロードしました

mean = np.load(args.mean_file).mean(1).mean(1)

于 2016-09-02T18:11:30.457 に答える
0

カフェのインストールは簡単ではなかったので、コードを再構築するのはかなり怖いです。しかし、修正するには、平均をサイズ変更するソリューションには、caffe/classifier.py で内部的に設定されている in_shape (user8264 の応答) が必要です。

とにかく、デバッグして、age_net.caffemodel の in_shape = (3, 227, 227) の値を見つけました

したがって、年齢と性別の予測に使用されるモデルは次のように変更されます。

age_net_pretrained='./age_net.caffemodel'
age_net_model_file='./deploy_age.prototxt'
age_net = caffe.Classifier(age_net_model_file, age_net_pretrained,
                   mean=mean,
                   channel_swap=(2,1,0),
                   raw_scale=255,
                   image_dims=(227, 227))

ただし、最初に mean を変更する必要があります。

m_min, m_max = mean.min(), mean.max()
normal_mean = (mean - m_min) / (m_max - m_min)
in_shape=(227, 227)
mean = caffe.io.resize_image(normal_mean.transpose((1,2,0)),in_shape)
                            .transpose((2,0,1)) * (m_max - m_min) + m_min

これにより、「ValueError: Mean shape incompatible with input shape」が解消されます。しかし、私は精度について確信が持てません。どうやら、私にとって平均パラメータをスキップすると、より良い年齢予測が得られました:)

于 2017-07-22T12:31:23.083 に答える