Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
228 views
in Technique[技术] by (71.8m points)

python - How to get better predictions by data augmentation on CNN model for Image Classification

I’m a beginner. I try Image Classification to my photos by using CNN model for on MNIST. To get better predictions, I did data augmentation to this, but I couldn’t improve it. Something wrong? And I don’t understand why CASE A on the following table had the wrong prediction to the number “4” comparing with CASE B, even though it has almost same accuracy at each time. Please give me some advice.

my 10 photos (.png, 28x28pixel) enter image description here

  1. Change the number of MNIST data to 5 cases
  2. Data augmentation No or Yes to 5 cases
  3. Predict my 10 photos by using learned model

enter image description here

# import library
from tensorflow import keras
from tensorflow.keras import datasets, layers, models
from tensorflow.keras.layers import Dense, Conv2D, Flatten, Dropout, MaxPooling2D
from tensorflow.keras.models import Sequential
from tensorflow.keras.preprocessing.image import ImageDataGenerator

# MNIST
mnist=keras.datasets.mnist
(x_train,y_train),(x_test,y_test)=mnist.load_data()
#(x_train,y_train),(x_test,y_test)=(x_train[:80],y_train[:80]),(x_test[:20], y_test[:20])
#(x_train,y_train),(x_test,y_test)=(x_train[:160],y_train[:160]),(x_test[:40], y_test[:40])
#(x_train,y_train),(x_test,y_test)=(x_train[:800],y_train[:800]),(x_test[:200], y_test[:200])
#(x_train,y_train),(x_test,y_test)=(x_train[:8000],y_train[:8000]),(x_test[:2000], y_test[:2000])
x_train=x_train.reshape(x_train.shape[0],28,28,1)
x_test=x_test.reshape(x_test.shape[0],28,28,1)
x_train=x_train/255
x_test=x_test/255
print("x_train",x_train.shape)
print("x_test",x_test.shape)

# Convolutional Neural Networks
model = Sequential()
model.add(Conv2D(16,(3,3),padding='same',input_shape=(28,28,1),activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(128,(3,3),activation='relu'))
model.add(Conv2D(256,(3,3),activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(128,activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(10,activation='softmax'))
model.summary()

# model compile and learn
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])
model.fit(x_train,y_train,epochs=5)

# evoluate for test data
loss,acc=model.evaluate(x_test,y_test,verbose=2)
print('accuracy:',acc)

# data augmentation
datagen=ImageDataGenerator(rescale=1/255,
                           width_shift_range=0.01,
                           height_shift_range=0.025,                                                    
                           zoom_range=0.05)

# learn
history=model.fit_generator(
datagen.flow(x_train,y_train,batch_size=64),
steps_per_epoch=60,
epochs=40,
validation_data=(x_test,y_test),
validation_steps=5,
verbose=1)

# evoluate for test data
loss,acc=model.evaluate(x_test,y_test,verbose=2)
print('accuracy:',acc)
question from:https://stackoverflow.com/questions/65889069/how-to-get-better-predictions-by-data-augmentation-on-cnn-model-for-image-classi

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...