欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Python神经网络实现手写数字识别

程序员文章站 2022-05-21 23:52:43
...

利用Python制作简易的神经网络

* 实现手写数字的识别
* github项目地址:https://github.com/coding-cheng/mnist 
* 完整的数据集:
	训练集 :http://www.pjreddie.com/midea/files/mnist_train.csv
	测试集 :http://www.pjreddie.com/midea/files/mnist_test.csv
* 参考书籍 : 《Python神经网络编程》
* github项目分为以下几个部分:
		1. neural_network.py : 神经网络
		2. train.py : 训练模型,并保存模型
		3. test.py : 测试模型,并且保存模型的准确率
		4. useyournum.py : 可使用自己写的数字作为测试对象
		5. mnist_dataset : 部分训练测试数据
		6. save_model : 保存训练后的模型数据
		7. yournum : 用于保存你所写的数字
		8. accuracy : 保存测试准确率
数据集网盘下载地址:
链接:https://pan.baidu.com/s/1d_JpNKqIoK86a7pcGoB_Hw 
提取码:qbn9 
  • 项目结构
    Python神经网络实现手写数字识别
  1. 简易神经网络的搭建
import numpy as np
import scipy.special

class neuralNetwork():
    # initialise the neuralNetwork
    def __init__(self, inputnodes, hiddennodes, outputnodes, learningrate):
        # set number of nodes in each input, hidden, output layer
        self.inodes = inputnodes
        self.hnodes = hiddennodes
        self.onodes = outputnodes

        # link weight matrices, wih and who
        # weights inside the arrays are w_i_j, where link is from node i to node j in the next layer
        # w11 w21
        # w12 w22 etc
        self.wih = np.random.normal(0.0, pow(self.hnodes, -0.5), (self.hnodes, self.inodes))
        self.who = np.random.normal(0.0, pow(self.onodes, -0.5), (self.onodes, self.hnodes))

        # learning rate
        self.lr = learningrate

        # activation function is the sigmoid function
        self.activation_function = lambda x: scipy.special.expit(x)

    # train the neuralNetwork
    def train(self, inputs_list, targets_list):
        # convert inputs list to 2d array
        inputs = np.array(inputs_list, ndmin=2).T
        targets = np.array(targets_list, ndmin=2).T

        # calculate signals into hidden layer
        hidden_inputs = np.dot(self.wih, inputs)
        # calculate the signals emerging from hidden layer
        hidden_outputs = self.activation_function(hidden_inputs)

        # calculate signals from final output layer
        final_inputs = np.dot(self.who, hidden_outputs)
        # calculate the signals emerging from final output layer
        final_outputs = self.activation_function(final_inputs)
        # output layer error is the (target - actual)
        output_errors = targets - final_outputs
        # hidden layer error is the output_errors, split by weights, recombined at hidden nodes
        hidden_errors = np.dot(self.who.T, output_errors)

        # update the weights for the links between the hidden and output layer
        self.who += self.lr * np.dot((output_errors * final_outputs * (1.0 - final_outputs)),
                                     np.transpose(hidden_outputs))

        # update the weights for the links between the input and hidden layer
        self.wih += self.lr * np.dot((hidden_errors * hidden_outputs * (1.0 - hidden_outputs)), np.transpose(inputs))

    # query the neural network
    def query(self, inputs_list):
        # convert inputs list to 2d array
        inputs = np.array(inputs_list, ndmin=2).T

        # calculate signals into hidden layer
        hidden_inputs = np.dot(self.wih, inputs)
        # calculate the signals emerging from hidden layer
        hidden_outputs = self.activation_function(hidden_inputs)

        # calculate signals into final output layer
        final_inputs = np.dot(self.who, hidden_outputs)
        # calculate the signals emerging from final output layer
        fianl_outputs = self.activation_function(final_inputs)

        return fianl_outputs
  1. 训练模块
from neural_network import neuralNetwork
import numpy as np


# number of input, hidden and output nodes
input_nodes = 784  # 28*28
hidden_nodes = 500
output_nodes = 10

# learning rate is 0.3
learning_rate = 0.1

# create instance of neural network
n = neuralNetwork(input_nodes, hidden_nodes, output_nodes, learning_rate)

training_data_file = open("mnist_dataset/mnist_train_100.csv", "r")
training_data_list = training_data_file.readlines()
training_data_file.close()

# train the neural network

# epochs is the number of times the training data set is used for training
epochs = 5

for e in range(epochs):
    # go through all records in the training data set
    for record in training_data_list:
        # split the record by ',' commas
        all_values = record.split(',')
        # scale and shift the inputs
        inputs = (np.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01
        # create the target output values (all 0.01, expert the desired label which is 0.99)
        targets = np.zeros(output_nodes) + 0.01
        # all_values[0] is the target label for this record
        targets[int(all_values[0])] = 0.99
        n.train(inputs, targets)

save_wih_file = "save_model/wih.npy"
np.save(save_wih_file, n.wih)
save_who_file = "save_model/who.npy"
np.save(save_who_file, n.who)

  1. 测试模块
from neural_network import neuralNetwork
import numpy as np

# number of input, hidden and output nodes
input_nodes = 784  # 28*28
hidden_nodes = 100
output_nodes = 10

# learning rate is 0.3
learning_rate = 0.3

# create instance of neural network
n = neuralNetwork(input_nodes, hidden_nodes, output_nodes, learning_rate)

save_wih_file = "save_model/wih.npy"
save_who_file = "save_model/who.npy"
n.wih = np.load(save_wih_file)
n.who = np.load(save_who_file)

# load the mnist test data csv file into a list
test_data_file = open("mnist_dataset/mnist_test_10.csv", "r")
test_data_list = test_data_file.readlines()
test_data_file.close()

# test the neural network
# scorecard for how well the network performs, initially empty
scorecard = []
# go through all records in the test data set
for record in test_data_list:
    # split the record by ',' commas
    all_values = record.split(',')
    # correct answer is first value
    correct_label = int(all_values[0])
    # print(correct_label, "correct_label")
    # scale and shift the inputs
    inputs = (np.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01
    # query the network
    outputs = n.query(inputs)
    # the index of the highest value corresponds to the label
    label = np.argmax(outputs)
    # print(label, "network's answer")
    # append correct or incorrect to list
    if label == correct_label:
        # network's answer matches correct answer, add 1 to scorecard
        scorecard.append(1)
    else:
        # network's answer doesn't match correct answer, add 1 to scorecard
        scorecard.append(0)

# calculate the performance score, the fraction of correct answers
scorecard_array = np.asarray(scorecard)
performance = scorecard_array.sum() / scorecard_array.size
print("performance = ", performance)

save_accuracy = "accuracy.txt"
with open(save_accuracy, 'a') as f:
    f.write("Learning rate is : " + str(n.lr))
    f.write("       Accuracy is : " + str(performance) + '\n')
  1. 使用你自己的手写数字
from PIL import Image
import numpy as np
from neural_network import neuralNetwork
import matplotlib.pyplot as plt
import imageio

# number of input, hidden and output nodes
input_nodes = 784  # 28*28
hidden_nodes = 100
output_nodes = 10

# learning rate is 0.3
learning_rate = 0.1

# create instance of neural network
n = neuralNetwork(input_nodes, hidden_nodes, output_nodes, learning_rate)

# change pixel value
file_in = 'yournum/yournum.png'
width = 28
height = 28
file_out = 'yournum/yournum_c.png'
image = Image.open(file_in)
resized_image = image.resize((width, height), Image.ANTIALIAS)
resized_image.save(file_out)

save_wih_file = "save_model/wih.npy"
save_who_file = "save_model/who.npy"
n.wih = np.load(save_wih_file)
n.who = np.load(save_who_file)

# test the neural network

image_file_name = "yournum/yournum_c.png"
img_array = imageio.imread(image_file_name, as_gray=True)

img_data = 255.0 - img_array.reshape(784)
img_data = (img_data / 255.0 * 0.99) + 0.01
# query the network
outputs = n.query(img_data)
for i in range(10):
    print(i, " ", outputs[i])
result = np.argmax(outputs)
print("Neural network predicts ", result)
image_array = np.asfarray(img_data).reshape((28, 28))
plt.imshow(image_array, cmap='Greys', interpolation='None')
plt.show()
相关标签: Python

上一篇: Redis集群

下一篇: redis集群