模型加载出错
Created by: reid3290
我按照官方的图像分类教程学习使用paddlepaddle,模型训练没有问题,但是应用模型出现了如下问题: ValueError: cannot reshape array of size 294912 into shape ()
代码如下:
from PIL import Image
import paddle.v2 as paddle
import numpy as np
import os
import gzip
def load_image(file):
im = Image.open(file)
im = im.resize((32, 32), Image.ANTIALIAS)
im = np.array(im).astype(np.float32)
im = im.transpose((2, 0, 1)) # CHW
im = im[(2, 1, 0),:,:] # BGR
im = im.flatten()
im = im / 255.0
return im
test_data = []
cur_dir = os.path.dirname(os.path.realpath(__file__))
test_data.append(load_image(cur_dir + '/image/dog.png'))
with gzip.open('params_pass_50.tar.gz', 'r') as f:
parameters = paddle.parameters.Parameters.from_tar(f)
probs = paddle.infer(output_layer=out, parameters=parameters, input=test_data)
lab = np.argsort(-probs) # probs and lab are the results of one batch data
print "Label of image/dog.png is: %d" % lab[0][0]