deeplearningwithpython豆瓣_python_deeplearning05_趣味盎然上

20180423 qzd

ch05 - 趣味盎然上

自己的手写数字

(创建较小的PNG图片,调整到28*28像素来匹配之前用过的MNIST数据集中的图片。)

#从常见的图像文件格式中(包括PNG格式)读取和解码数据

# helper to load data from PNG image files

import scipy.misc

import matplotlib.pyplot

%matplotlib inline

image_file_name = 'my_own_images/2828_my_own_2.png'

#scipy.misc.imread()函数帮助我们从图像文件,如PNG或JPG文件中,读取数据

#参数flatten=True 将图像变成简单的浮点数数组,如果图像是彩色的,那么颜色值将被转换为所需要的灰度 dim--> 28*28

img_array = scipy.misc.imread(image_file_name, flatten = True)

#重塑数组,减去255的原因是:常规而言,0指的是黑色,255指的是白色,但是MNIST数据集使用相反的方式表示,因此不得不将值逆转过来以匹配MNIST数据。 dim--> 784

img_data = 255.0 - img_array.reshape(784)

#scale

img_data = (img_data / 255.0 *0.99) + 0.01

matplotlib.pyplot.imshow(img_data.reshape(28,28), cmap='Greys', interpolation='None')

result:

2a31dc35a9b1

load own images

# python notebook for Make Your Own Neural Network

# code for a 3-layer neural network, and code for learning the MNIST dataset

# helper to load data from PNG image files

import scipy.misc

# glob helps select multiple files using patterns

import glob

import numpy

# library for plotting arrays

import matplotlib.pyplot

# ensure the plots are inside this notebook, not an external window

%matplotlib inline

# our own image test data set

our_own_dataset = []

for image_file_name in glob.glob('my_own_images/2828_my_own_?.png'):

print ("loading ... ", image_file_name)

# use the filename to set the correct label

label = int(image_file_name[-5:-4])

# load image data from png files into an array

img_array = scipy.misc.imread(image_file_name, flatten=True)

# reshape from 28x28 to list of 784 values, invert values

img_data = 255.0 - img_array.reshape(784)

# then scale data to range from 0.01 to 1.0

img_data = (img_data / 255.0 * 0.99) + 0.01

print(numpy.min(img_data))

print(numpy.max(img_data))

# append label and image data to test data set

record = numpy.append(label,img_data)

print(record)

our_own_dataset.append(record)

pass

result:

loading ... my_own_images\2828_my_own_2.png

0.01

1.0

[ 2. 0.01 0.01 0.01 0.01 0.01 0.01

0.01 0.01 0.01 0.01 0.01 0.01 0.01

0.01 0.01 0.01 …… 0.01 ]

loading ... my_own_images\2828_my_own_3.png

0.01

1.0

[ 3. 0.01 0.01 0.01 0.01 0.01 0.01

0.01 0.01 0.01 0.01 0.01 0.01 0.01

0.01 0.01 0.01 …… 0.01 ]

……

matplotlib.pyplot.imshow(our_own_dataset[3][1:].reshape(28,28), cmap='Greys', interpolation='None')

result:

2a31dc35a9b1

1524492152(1).png

print(our_own_dataset[0])

result:

[ 2. 0.01 0.01 0.01 0.01 0.01 0.01

……

0.52247059 0.71658826 0.61952943 0.29729411 0.01 0.01 0.01

……

0.01 0.01 0.01 0.01 0.01 0.01 0.01 ]

使用自己创建的图像数据对网络进行测试

# python notebook for Make Your Own Neural Network

# code for a 3-layer neural network, and code for learning the MNIST dataset

# this version trains using the MNIST dataset, then tests on our own images

import numpy

# scipy.special for the sigmoid function expit()

import scipy.special

# library for plotting arrays

import matplotlib.pyplot

# ensure the plots are inside this notebook, not an external window

%matplotlib inline

# helper to load data from PNG image files

import scipy.misc

# glob helps select multiple files using patterns

import glob

# neural network class definition

class neuralNetwork:

# initialise the neural network

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 = numpy.random.normal(0.0, pow(self.inodes, -0.5), (self.hnodes, self.inodes))

self.who = numpy.random.normal(0.0, pow(self.hnodes, -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)

pass

# train the neural network

def train(self, inputs_list, targets_list):

# convert inputs list to 2d array

inputs = numpy.array(inputs_list, ndmin=2).T

targets = numpy.array(targets_list, ndmin=2).T

# calculate signals into hidden layer

hidden_inputs = numpy.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 = numpy.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 = numpy.dot(self.who.T, output_errors)

# update the weights for the links between the hidden and output layers

self.who += self.lr * numpy.dot((output_errors * final_outputs * (1.0 - final_outputs)), numpy.transpose(hidden_outputs))

# update the weights for the links between the input and hidden layers

self.wih += self.lr * numpy.dot((hidden_errors * hidden_outputs * (1.0 - hidden_outputs)), numpy.transpose(inputs))

pass

# query the neural network

def query(self, inputs_list):

# convert inputs list to 2d array

inputs = numpy.array(inputs_list, ndmin=2).T

# calculate signals into hidden layer

hidden_inputs = numpy.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 = numpy.dot(self.who, hidden_outputs)

# calculate the signals emerging from final output layer

final_outputs = self.activation_function(final_inputs)

return final_outputs

# number of input, hidden and output nodes

input_nodes = 784

hidden_nodes = 200

output_nodes = 10

# learning rate

learning_rate = 0.1

# create instance of neural network

n = neuralNetwork(input_nodes,hidden_nodes,output_nodes, learning_rate)

# load the mnist training data CSV file into a list

training_data_file = open("mnist_dataset/mnist_train.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 = 1

for e in range(epochs):

# go through all records in the training data set

for record in training_data_list:

# split the record by the ',' commas

all_values = record.split(',')

# scale and shift the inputs

inputs = (numpy.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01

# create the target output values (all 0.01, except the desired label which is 0.99)

targets = numpy.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)

pass

pass

our own image test data set

# our own image test data set

our_own_dataset = []

# load the png image data as test data set

for image_file_name in glob.glob('my_own_images/2828_my_own_?.png'):

# use the filename to set the correct label

label = int(image_file_name[-5:-4])

# load image data from png files into an array

print ("loading ... ", image_file_name)

img_array = scipy.misc.imread(image_file_name, flatten=True)

# reshape from 28x28 to list of 784 values, invert values

img_data = 255.0 - img_array.reshape(784)

# then scale data to range from 0.01 to 1.0

img_data = (img_data / 255.0 * 0.99) + 0.01

print(numpy.min(img_data))

print(numpy.max(img_data))

# append label and image data to test data set

record = numpy.append(label,img_data)

our_own_dataset.append(record)

pass

result:

loading ... my_own_images\2828_my_own_2.png

0.01

1.0

loading ... my_own_images\2828_my_own_3.png

0.01

1.0

loading ... my_own_images\2828_my_own_4.png

0.01

0.930118

loading ... my_own_images\2828_my_own_5.png

0.01

0.868

loading ... my_own_images\2828_my_own_6.png

0.01

1.0

test the neural network with our own images

# test the neural network with our own images

# record to test

item = 2

# plot image

matplotlib.pyplot.imshow(our_own_dataset[item][1:].reshape(28,28), cmap='Greys', interpolation='None')

# correct answer is first value

correct_label = our_own_dataset[item][0]

# data is remaining values

inputs = our_own_dataset[item][1:]

# query the network

outputs = n.query(inputs)

print (outputs)

# the index of the highest value corresponds to the label

label = numpy.argmax(outputs)

print("network says ", label)

# append correct or incorrect to list

if (label == correct_label):

print ("match!")

else:

print ("no match!")

pass

result:

[[ 9.28222354e-04]

[ 3.27077392e-03]

[ 7.16421986e-03]

[ 6.50147746e-03]

[ 9.20999296e-01]

[ 1.42068282e-02]

[ 2.33703529e-02]

[ 1.24264294e-02]

[ 2.83751170e-05]

[ 2.31418176e-02]]

network says 4

match!

2a31dc35a9b1

1524493765(1).png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值