今天进阶到手写识别神经网络的搭建。
源代码:https://download.csdn.net/download/rance_king/11010836
import numpy as np
import matplotlib.pyplot as plt
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import Adam
from keras.utils.np_utils import to_categorical
import random
np.random.seed(0)
(X_train, y_train), (X_test, y_test) = mnist.load_data()
assert(X_train.shape[0] == y_train.shape[0]), "The number of images is not equal to the number of labels."
assert(X_test.shape[0] == y_test.shape[0]), "The number of images is not equal to the number of labels."
assert(X_train.shape[1:] == (28,28)), "The dimensions of the images are not 28x28"
assert(X_test.shape[1:] == (28,28)), "The dimensions of the images are not 28x28"
#我想查看一下这个数据集里面每个标签下有多少个样本,用空数组接收后面被放入的每个标签下样本数量
num_of_samples = []
cols = 5
num_classes = 10
#plt.subplots是创建子图,子图是把很多图画到一张图里,nrows是多少行,ncols是多少列,figsize是整体图的宽高。
fig, axs = plt.subplots(nrows=num_classes, ncols = cols, figsize=(5, 8))
#采用了满的布局,fig是整张图的情况,axs是控制单张图的情况
fig.tight_layout()
#用图片填充建好的子图框架
for i in range(cols):
for j in range(num_classes):
#再次出现过滤技巧,因为分类为10,每一个分类的j就是分类标签,用y_train训练集标签来判断是否
#其等于j,借此返回布尔数组,并获取相关联的X_train图片
x_selected = X_train[y_train == j]
#在被选中的X数据集中随机选择图片放进框架里面,因为X的shape是(60000,28,28)所以第一个标号是选择图片
#把图像调成灰度模式cmap(colormap)
axs[j][i].imshow(x_selected[random.randint(0, len(x_selected - 1)), :, :], cmap=plt.get_cmap("gray"))
#不显示坐标轴
axs[j][i].axis("off")
#对每个第二列的位置放置一个标题
if i == 2:
axs[j][i].set_title(str(j))
num_of_samples.append(len(x_selected))
print(num_of_samples)
plt.figure(figsize=(12, 4))
plt.bar(range(0, num_classes), num_of_samples)
plt.title("Distribution of the training dataset")
plt.xlabel("Class number")
plt.ylabel("Number of images")
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)
X_train = X_train/255
X_test = X_test/255
X_train = X_train.reshape(X_train.shape[0], num_pixels)
X_test = X_test.reshape(X_test.shape[0], num_pixels)
num_pixels = 784
def create_model():
model = Sequential()
model.add(Dense(10, input_dim=num_pixels, activation='relu'))
model.add(Dense(30, activation='relu'))
model.add(Dense(10, activation='relu'))
#输出层一共有10个对应0-9十个数字,输出的激活函数是softmax
model.add(Dense(num_classes, activation='softmax'))
model.compile(Adam(lr=0.01), loss='categorical_crossentropy', metrics=['accuracy'])
return model
model = create_model()
print(model.summary())
history = model.fit(X_train, y_train, validation_split=0.1, epochs = 10, batch_size = 200, verbose = 1, shuffle = 1)
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.legend(['loss', 'val_loss'])
plt.title('Loss')
plt.xlabel('epoch')
score = model.evaluate(X_test, y_test, verbose=0)
print(type(score))
print('Test score:', score[0])
print('Test accuracy:', score[1])
import requests
from PIL import Image
#url地址
url = 'https://www.researchgate.net/profile/Jose_Sempere/publication/221258631/figure/fig1/AS:305526891139075@1449854695342/Handwritten-digit-2.png'
#进行request请求发送图片
response = requests.get(url, stream=True)
#打开图片
img = Image.open(response.raw)
#以灰度模式展示图片
plt.imshow(img, cmap=plt.get_cmap('gray'))
#导入opencv2库
import cv2
#将img转换为numpy.array,并且缩放为28*28的灰度图片
img = np.asarray(img)
img = cv2.resize(img, (28, 28))
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#对二进制图像进行逻辑‘非’的操作,实际上是反转写了字的区域,将其变为白色
img = cv2.bitwise_not(img)
plt.imshow(img, cmap=plt.get_cmap('gray'))
img = img/255
img = img.reshape(1, 784)
prediction = model.predict_classes(img)
print("predicted digit:", str(prediction))