1, Introduction to Data set
Svhn (Street View house number) Dateset comes from the Google street numbers, native data set 1 is the official website Format 1 is some raw unprocessed color pictures, as shown (does not contain a blue border), the downloaded dataset contains PNG Images and Digitstruct.mat files that contain the position information of the border, which has several numbers on each image, and is suitable for OCR-related directions.
In this case, FORMAT2 is used, and the FORMAT2 is clipped to the size of 32x32, and the data is a. mat file.
2, data processing
The dataset contains two variables x for the image, and the shape of training set X is (32,32,3,73257) (width, height, channels, samples), TensorFlow tensor needs (samples, Widt h, height, channels), so need to convert, because directly call CIFAR 10 network model, the data only need to do a normalization, all pixels except 255 OK, and the original data 0 of the label is 10, here to convert to 0, and provide one_hot encoding.
#!/usr/bin/env Python2#-*-coding:utf-8-*-"""Created on Thu Jan 09:55:36 2017@author:cheers"""ImportScipy.io as SioImportMatplotlib.pyplot as PltImportNumPy as Npimage_size= 32Num_labels= 10defdisplay_data ():Print 'loading Matlab data ...'Train= Sio.loadmat ('Train_32x32.mat') Data=train['X'] Label=train['y'] forIinchRange (10): Plt.subplot (2,5,i+1) Plt.title (label[i][0]) plt.imshow (Data[...,i]) Plt.axis ('off') plt.show ()defLoad_data (One_hot =False): Train= Sio.loadmat ('Train_32x32.mat') Test= Sio.loadmat ('Test_32x32.mat') Train_data=train['X'] Train_label=train['y'] Test_data=test['X'] Test_label=test['y'] Train_data= Np.swapaxes (train_data, 0, 3) Train_data= Np.swapaxes (Train_data, 2, 3) Train_data= Np.swapaxes (Train_data, 1, 2) Test_data= Np.swapaxes (test_data, 0, 3) Test_data= Np.swapaxes (Test_data, 2, 3) Test_data= Np.swapaxes (Test_data, 1, 2) Test_data= test_data/255. Train_data=train_data/255. forIinchRange (Train_label.shape[0]):ifTrain_label[i][0] = = 10: train_label[i][0]=0 forIinchRange (Test_label.shape[0]):ifTest_label[i][0] = = 10: test_label[i][0]=0ifOne_hot:train_label= (Np.arange (num_labels) = =train_label[:,]). Astype (np.float32) Test_label= (Np.arange (num_labels) = =test_label[:,]). Astype (Np.float32)returnTrain_data,train_label, Test_data,test_labelif __name__=='__main__': Load_data (one_hot=True) display_data ()
View Code
3,tfearn Training
Note that the imagepreprocessing is 0-valued for the data. Network structure is also relatively simple, directly call Tflearn Cifar10 example.
from __future__ ImportDivision, print_function, Absolute_importImportTflearn fromTflearn.data_utilsImportShuffle, to_categorical fromTflearn.layers.coreImportInput_data, dropout, fully_connected fromTflearn.layers.convImportconv_2d, max_pool_2d fromTflearn.layers.estimatorImportregression fromTflearn.data_preprocessingImportimagepreprocessing fromTflearn.data_augmentationImportimageaugmentation#Data loading and preprocessingImportSvhn_data as Svhnx, Y, X_test, Y_test= Svhn.load_data (One_hot =True) X, Y=Shuffle (X, Y)#Real-time data preprocessingImg_prep =imagepreprocessing () img_prep.add_featurewise_zero_center () Img_prep.add_featurewise_stdnorm ( )#convolutional Network BuildingNetwork = Input_data (Shape=[none, 32, 32, 3], data_preprocessing=img_prep) Network= conv_2d (Network, 3, activation='Relu') Network= max_pool_2d (Network, 2) Network= conv_2d (Network, 3, activation='Relu') Network= conv_2d (Network, 3, activation='Relu') Network= max_pool_2d (Network, 2) Network= fully_connected (Network, activation=,'Relu') Network= Dropout (Network, 0.5) Network= fully_connected (Network, ten, activation='Softmax') Network= Regression (Network, optimizer='Adam', Loss='categorical_crossentropy', Learning_rate=0.001)#Train using classifierModel = Tflearn. DNN (Network, tensorboard_verbose=0) Model.fit (X, Y, N_epoch=15, Shuffle=true, validation_set=(X_test, y_test), Show_metric=true, batch_size=96, run_id='svhn_cnn')
Training results:
Training step:11452 | Total loss:0.68217 | Time:7. 973s| Adam | epoch:015 | loss:0.68217-acc:0.9329--iter:72576/73257Training Step:11453 | Total loss:0.62980 | Time:7. 983s| Adam | epoch:015 | loss:0.62980-acc:0.9354--iter:72672/73257Training Step:11454 | Total loss:0.58649 | Time:7. 994s| Adam | epoch:015 | loss:0.58649-acc:0.9356--iter:72768/73257Training Step:11455 | Total loss:0.53254 | Time:8. 005s| Adam | epoch:015 | loss:0.53254-acc:0.9421--iter:72864/73257Training Step:11456 | Total loss:0.49179 | Time:8. 016s| Adam | epoch:015 | loss:0.49179-acc:0.9416--iter:72960/73257Training Step:11457 | Total loss:0.45679 | Time:8. 027s| Adam | epoch:015 | loss:0.45679-acc:0.9433--iter:73056/73257Training Step:11458 | Total loss:0.42026 | Time:8. 038s| Adam | epoch:015 | loss:0.42026-acc:0.9469--iter:73152/73257Training Step:11459 | Total loss:0.38929 | Time:8. 049s| Adam | epoch:015 | loss:0.38929-acc:0.9491--iter:73248/73257Training Step:11460 | Total loss:0.35542 | Time:9. 928s| Adam | epoch:015 | loss:0.35542-acc:0.9542 | val_loss:0.40315-val_acc:0.9085--iter:73257/73257
tflearn--(2) Svhn