keras—神经网络CNN—CIFAR_10图像识别

 1 from keras.datasets import cifar10
from keras.utils import np_utils
import matplotlib.pyplot as plt
from keras.models import load_model
import numpy as np
np.random.seed()
(x_img_train,y_label_train),(x_img_test,y_label_test)=cifar10.load_data()
print('train data=',len(x_img_train))
print('test data=',len(x_img_test))
print('x_train_image:',x_img_train.shape)
print('x_test_image:',x_img_test.shape)
x_img_train_4D=x_img_train.reshape(x_img_train.shape[],,,).astype('float32')
x_img_test_4D=x_img_test.reshape(x_img_test.shape[],,,).astype('float32')
x_img_train_normalize=x_img_train_4D/255.0
x_img_test_normalize=x_img_test_4D/255.0
print(x_img_train_normalize[][][])
y_label_train_OneHot=np_utils.to_categorical(y_label_train)
y_label_test_OneHot=np_utils.to_categorical(y_label_test)
print(y_label_train_OneHot[:])
from keras.models import Sequential
from keras.layers import Dense,Dropout,Flatten,Conv2D,MaxPooling2D,ZeroPadding2D,Activation
model=Sequential()
model.add(Conv2D(filters=,
kernel_size=(,),
padding='same',
input_shape=(,,),
activation='relu'))
model.add(Dropout(0.25))
model.add(MaxPooling2D(pool_size=(,)))
model.add(Conv2D(filters=,
kernel_size=(,),
padding='same',
activation='relu'))
model.add(Dropout(0.25))
model.add(MaxPooling2D(pool_size=(,)))
model.add(Flatten())
model.add(Dropout(0.25))
model.add(Dense(,activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(,activation='softmax'))
print(model.summary())
model.compile(loss='categorical_crossentropy',
optimizer='adam',metrics=['accuracy'])
try:
model=load_model("CnnModel.h5")
print("Load model successfully!Continuous training model!......")
except :
print("Failure of loading model!Start training a new model......") train_history=model.fit(x=x_img_train_normalize,
y=y_label_train_OneHot,validation_split=0.2,
epochs=,batch_size=,verbose=)
model.save("CnnModel.h5")
print("Saved model to disk")
def show_train_history(train_history,train,validation):
plt.plot(train_history.history[train])
plt.plot(train_history.history[validation])
plt.title('Train History')
plt.ylabel(train)
plt.xlabel('Epoch')
plt.legend(['train','validation'],loc='upper left') #显示左上角标签
plt.show()
show_train_history(train_history,'acc','val_acc')
show_train_history(train_history,'loss','val_loss')
scores=model.evaluate(x_img_test_normalize,y_label_test_OneHot)
print()
print('accuracy',scores[])
prediction=model.predict_classes(x_img_test_normalize)
print("prediction[:10]",prediction[:])
import matplotlib.pyplot as plt
label_dict={:"airplane",:"automobile",:"bird",:"cat",:"deer",:"dog",:"frog",:"horse",:"ship",:"truck"}
def plot_image_labels_prediction_1(image,labels,prediction,idx,num=):
fig=plt.gcf()
fig.set_size_inches(,)
if num>:num=
for i in range(,num):
ax=plt.subplot(,,i+)
ax.imshow(image[idx],cmap='binary')
title=str(i)+','+label_dict[labels[i][]]
if len(prediction)>:
title+="=>"+label_dict[prediction[i]]
ax.set_title(title,fontsize=)
ax.set_xticks([]);ax.set_yticks([])
idx+=
plt.show()
plot_image_labels_prediction_1(x_img_test,y_label_test,prediction,,)
Predicted_Probability=model.predict(x_img_test_normalize)
def show_Predicted_Probability(y,prediction,x_img_test,Predicted_Probability,i):
print('label:',label_dict[y[i][]],'predict:',label_dict[prediction[i]])
plt.figure(figsize=(,))
plt.imshow(np.reshape(x_img_test[i],(,,)))
plt.show()
for j in range():
print(label_dict[j]+
'Probability:%1.9f'%(Predicted_Probability[i][j]))
show_Predicted_Probability(y_label_test,prediction,x_img_test,Predicted_Probability,)
show_Predicted_Probability(y_label_test,prediction,x_img_test,Predicted_Probability,)
################################

keras—神经网络CNN—CIFAR_10图像识别

keras—神经网络CNN—CIFAR_10图像识别

keras—神经网络CNN—CIFAR_10图像识别

keras—神经网络CNN—CIFAR_10图像识别

keras—神经网络CNN—CIFAR_10图像识别

keras—神经网络CNN—CIFAR_10图像识别

keras—神经网络CNN—CIFAR_10图像识别

keras—神经网络CNN—CIFAR_10图像识别

keras—神经网络CNN—CIFAR_10图像识别

keras—神经网络CNN—CIFAR_10图像识别

keras—神经网络CNN—CIFAR_10图像识别

上一篇:(一)Android开发之安卓系统的前世今生


下一篇:[转]Android 导入v7包常见错误,以及项目引用v7包错误解决