epoch_id: 0, batch_id: 0, loss is: [27.441448]
epoch_id: 0, batch_id: 1000, loss is: [2.281343]
epoch_id: 0, batch_id: 2000, loss is: [4.0873246]
epoch_id: 0, batch_id: 3000, loss is: [4.101654]
epoch_id: 1, batch_id: 0, loss is: [4.409295]
epoch_id: 1, batch_id: 1000, loss is: [2.039107]
epoch_id: 1, batch_id: 2000, loss is: [2.5832603]
epoch_id: 1, batch_id: 3000, loss is: [2.6795988]
epoch_id: 2, batch_id: 0, loss is: [3.9104843]
epoch_id: 2, batch_id: 1000, loss is: [3.9539223]
epoch_id: 2, batch_id: 2000, loss is: [3.0901222]
epoch_id: 2, batch_id: 3000, loss is: [2.88086]
epoch_id: 3, batch_id: 0, loss is: [2.8132215]
epoch_id: 3, batch_id: 1000, loss is: [3.9469924]
epoch_id: 3, batch_id: 2000, loss is: [2.484096]
epoch_id: 3, batch_id: 3000, loss is: [2.9386535]
epoch_id: 4, batch_id: 0, loss is: [4.952403]
epoch_id: 4, batch_id: 1000, loss is: [4.189546]
epoch_id: 4, batch_id: 2000, loss is: [4.81828]
epoch_id: 4, batch_id: 3000, loss is: [2.296103]
epoch_id: 5, batch_id: 0, loss is: [5.300122]
epoch_id: 5, batch_id: 1000, loss is: [3.3335328]
epoch_id: 5, batch_id: 2000, loss is: [1.3920281]
epoch_id: 5, batch_id: 3000, loss is: [4.3053403]
epoch_id: 6, batch_id: 0, loss is: [3.0284095]
epoch_id: 6, batch_id: 1000, loss is: [1.5199479]
epoch_id: 6, batch_id: 2000, loss is: [3.2134027]
epoch_id: 6, batch_id: 3000, loss is: [2.2408743]
epoch_id: 7, batch_id: 0, loss is: [2.8081536]
epoch_id: 7, batch_id: 1000, loss is: [1.917313]
epoch_id: 7, batch_id: 2000, loss is: [5.512356]
epoch_id: 7, batch_id: 3000, loss is: [3.0259757]
epoch_id: 8, batch_id: 0, loss is: [2.2609704]
epoch_id: 8, batch_id: 1000, loss is: [2.1595194]
epoch_id: 8, batch_id: 2000, loss is: [3.7097778]
epoch_id: 8, batch_id: 3000, loss is: [4.8904915]
epoch_id: 9, batch_id: 0, loss is: [2.3321204]
epoch_id: 9, batch_id: 1000, loss is: [5.85237]
epoch_id: 9, batch_id: 2000, loss is: [4.1020594]
epoch_id: 9, batch_id: 3000, loss is: [1.5817034]
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
img_path ='./work/example_0.jpg'
im = Image.open('./work/example_0.jpg')
plt.imshow(im)
plt.show()
im = im.convert('L')print('原始图像shape: ', np.array(im).shape)
im = im.resize((28,28), Image.ANTIALIAS)
plt.imshow(im)
plt.show()print("采样后图片shape: ", np.array(im).shape)
原始图像shape: (252, 255)
采样后图片shape: (28, 28)
defload_image(img_path):
im = Image.open(img_path).convert('L')
im = im.resize((28,28), Image.ANTIALIAS)
im = np.array(im).reshape(1,-1).astype(np.float32)
im =1- im /255return im
model = MNIST()
params_file_path ='mnist.pdparams'
img_path ='./work/example_0.jpg'
param_dict = paddle.load(params_file_path)
model.load_dict(param_dict)
model.eval()
tensor_img = load_image(img_path)
result = model(paddle.to_tensor(tensor_img))print('result',result)print("本次预测的数字是", result.numpy().astype('int32'))
result Tensor(shape=[1, 1], dtype=float32, place=CPUPlace, stop_gradient=False,
[[3.76953363]])
本次预测的数字是 [[3]]