'数组形状不正确。'从npy转换为binaryproto时出错

时间:2017-02-17 09:27:34

标签: python numpy

我有brainwash_mean.npy个文件,它是一个没有错误的正确文件。

我正在尝试转换npy file to binaryproto并且我有'Incorrect array shape.'错误。

我的代码是

def convert_numpy_binaryproto(filename):
    print filename;
    avg_img = np.load(filename);
    #avg_img is your numpy array with the average data 
    blob = caffe.io.array_to_blobproto( avg_img);
    with open( mean.binaryproto, 'wb' ) as f :
        f.write( blob.SerializeToString())


def main(argv):
    convert_numpy_binaryproto(sys.argv[1]);


if __name__ == "__main__":
   main(sys.argv[1:])

可能出现什么问题?

1 个答案:

答案 0 :(得分:0)

以下代码对我有用。

def convert_numpy_binaryproto(path):
    print path;
    avg_img = np.load(path);
    #avg_img is your numpy array with the average data 
    blob = caffe.proto.caffe_pb2.BlobProto();
    blob.channels, blob.height, blob.width = avg_img.shape;
    blob.data.extend(avg_img.astype(float).flat);
    binaryproto_file = open('mean.binaryproto', 'wb' );
    binaryproto_file.write(blob.SerializeToString());
    binaryproto_file.close();