简体   繁体   English

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

[英]'Incorrect array shape.' error in converting from npy to binaryproto

I have brainwash_mean.npy file and it is a correct file with no error. 我有brainwash_mean.npy文件,它是没有错误的正确文件。

I am trying to convert the npy file to binaryproto and I have 'Incorrect array shape.' 我正在尝试将npy file to binaryproto转换为npy file to binaryproto并且出现'Incorrect array shape.' error. 错误。

My code is 我的代码是

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:])

What could be wrong? 有什么事吗

The following code worked for me. 以下代码对我有用。

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM