import paddle
from paddle. nn import Linear
import paddle. nn. functional as F
import os
import numpy as np
import matplotlib. pyplot as plt
train_dataset= paddle. vision. datasets. MNIST( mode= 'train' )
train_data0= np. array( train_dataset[ 0 ] [ 0 ] )
train_label_0= np. array( train_dataset[ 0 ] [ 1 ] )
plt. figure( "Image" )
plt. figure( figsize= ( 2 , 2 ) )
plt. imshow( train_data0, cmap= plt. cm. binary)
plt. axis( 'on' )
plt. title( 'image' )
plt. show( )
print ( "图像数据形状和对应数据为:" , train_data0. shape)
print ( "图像标签形状和对应数据为:" , train_label_0. shape, train_label_0)
print ( "\n打印第一个batch的第一个图象,对应标签数字为{}" . format ( train_label_0) )
class MNIST ( paddle. nn. Layer) :
def __init__ ( self) :
super ( MNIST, self) . __init__( )
self. fc= paddle. nn. Linear( in_features= 784 , out_features= 1 )
def forward ( self, inputs) :
outputs= self. fc( inputs)
return outputs
model= MNIST( )
def train ( modle) :
model. train( )
train_loader= paddle. io. DataLoader( paddle. vision. datasets. MNIST( mode= 'train' ) , batch_size= 16 , shuffle= True )
opt= paddle. optimizeer. SGD( learning_rate= 0.001 , parameters= model. paramenters( ) )
def norm_img ( img) :
assert len ( img. shape) == 3
batch_size, img_h, img_w= img. shape[ 0 ] , img. shape[ 1 ] , img. shape[ 2 ]
img= img/ 255
img= paddle. reshape( img, [ batch_size, img_h* img_w] )
return img
paddle. vision. set_image_backend( 'cv2' )
model= MNIST( )
def train ( model) :
model. train( )
train_loader= paddle. io. DataLoader( paddle. vision. datasets. MNIST( mode= 'train' ) , batch_size= 16 , shuffle= True )
opt= paddle. optimizer. SGD( learning_rate= 0.001 , parameters= model. parameters( ) )
EPOCH_NUM= 10
for epoch in range ( EPOCH_NUM) :
for batch_id, data in enumerate ( train_loader( ) ) :
images= norm_img( data[ 0 ] ) . astype( 'float32' )
labels= data[ 1 ] . astype( 'float32' )
predicts= model( images)
loss= F. square_error_cost( predicts, labels)
avg_loss= paddle. mean( loss)
if batch_id % 1000 == 0 :
print ( "epoch_id:{},batch_id:{},loss is:{}" . format ( epoch, batch_id, avg_loss. numpy( ) ) )
avg_loss. backward( )
opt. step( )
opt. clear_grad( )
train( model)
paddle. save( model. state_dict( ) , './mnist.pdparams' )
from PIL import Image
img_path= './work/00.png'
im= Image. open ( './work/00.png' )
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)
def load_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/ 255
return im
model= MNIST( )
params_file_path= 'mnist.pdparams'
img_path= './work/00.png'
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)
ensor_img= load_image( img_path)
result= model( paddle. to_tensor( tensor_img) )
print ( 'result' , result)
print ( "本次预测的数字是" , result. numpy( ) . astype( 'int32' ) )
图像数据形状和对应数据为: (28, 28)
图像标签形状和对应数据为: (1,) [5]
打印第一个batch的第一个图象,对应标签数字为[5]
epoch_id:0,batch_id:0,loss is:[28.449667]
epoch_id:0,batch_id:1000,loss is:[2.2133603]
epoch_id:0,batch_id:2000,loss is:[2.6192307]
epoch_id:0,batch_id:3000,loss is:[1.4816303]
epoch_id:1,batch_id:0,loss is:[4.873765]
epoch_id:1,batch_id:1000,loss is:[1.1245084]
epoch_id:1,batch_id:2000,loss is:[4.7486277]
epoch_id:1,batch_id:3000,loss is:[8.636472]
epoch_id:2,batch_id:0,loss is:[3.22654]
epoch_id:2,batch_id:1000,loss is:[4.1829042]
epoch_id:2,batch_id:2000,loss is:[3.8323183]
epoch_id:2,batch_id:3000,loss is:[3.3910174]
epoch_id:3,batch_id:0,loss is:[5.5484705]
epoch_id:3,batch_id:1000,loss is:[2.9698162]
epoch_id:3,batch_id:2000,loss is:[2.0920122]
epoch_id:3,batch_id:3000,loss is:[2.552484]
epoch_id:4,batch_id:0,loss is:[4.1760187]
epoch_id:4,batch_id:1000,loss is:[2.0923305]
epoch_id:4,batch_id:2000,loss is:[4.3818398]
epoch_id:4,batch_id:3000,loss is:[1.7267109]
epoch_id:5,batch_id:0,loss is:[2.777513]
epoch_id:5,batch_id:1000,loss is:[4.043735]
epoch_id:5,batch_id:2000,loss is:[4.275708]
epoch_id:5,batch_id:3000,loss is:[3.8894782]
epoch_id:6,batch_id:0,loss is:[3.5191324]
epoch_id:6,batch_id:1000,loss is:[2.433545]
epoch_id:6,batch_id:2000,loss is:[3.819005]
epoch_id:6,batch_id:3000,loss is:[2.326075]
epoch_id:7,batch_id:0,loss is:[2.5925946]
epoch_id:7,batch_id:1000,loss is:[3.17057]
epoch_id:7,batch_id:2000,loss is:[3.6248827]
epoch_id:7,batch_id:3000,loss is:[5.20137]
epoch_id:8,batch_id:0,loss is:[4.69654]
epoch_id:8,batch_id:1000,loss is:[2.5298047]
epoch_id:8,batch_id:2000,loss is:[4.6738186]
epoch_id:8,batch_id:3000,loss is:[3.891779]
epoch_id:9,batch_id:0,loss is:[4.864161]
epoch_id:9,batch_id:1000,loss is:[3.8327315]
epoch_id:9,batch_id:2000,loss is:[3.3783822]
epoch_id:9,batch_id:3000,loss is:[3.8774865]
原始图像shape: (329, 329)
采样后图片shape: (28, 28)
result Tensor(shape=[1, 1], dtype=float32, place=CPUPlace, stop_gradient=False,
[[2.93318319]])
本次预测的数字是 [[2]]