以吴恩达DL第一课第二周为基础的猫识别算法
本文记录了在学习BP的过程中,借由吴恩达Deep Learning第一课第二周为模板写的猫识别算法。(其实相当于是课后作业hh)
1.加载库,用matplot作图,里面的lr_utils是吴恩达打包好的一个用来加载数据的包,如果是直接运行的话可能会报错,最后面给出一个别的大佬写的代码也可以用。
import numpy as np import matplotlib.pyplot as plt import h5py import scipy from PIL import Image from scipy import ndimage from lr_utils import load_dataset %matplotlib inline
2.加载训练集和测试集
train_set_x_orig, train_set_y, test_set_x_orig, test_set_y, classes = load_dataset()
3.记录样本数m和图像的长宽(长宽相等,只用了一个num_px),打印一下看看长什么样
m_train=train_set_x_orig.shape[0]
m_test=test_set_x_orig.shape[0]
num_px=train_set_x_orig.shape[1]
print ("Number of training examples: m_train = " + str(m_train))
print ("Number of testing examples: m_test = " + str(m_test))
print ("Height/Width of each image: num_px = " + str(num_px))
print ("Each image is of size: (" + str(num_px) + ", " + str(num_px) + ", 3)")
print ("train_set_x shape: " + str(train_set_x_orig.shape))
print ("train_set_y shape: " + str(train_set_y.shape))
print ("test_set_x shape: " + str(test_set_x_orig.shape))
print ("test_set_y shape: " + str(test_set_y.shape))
4.把获得的数据转换成一个二维数组的形式,打印看看转换之后的样子
train_set_x_flatten=train_set_x_orig.reshape(train_set_x_orig.shape[0],-1).T
test_set_x_flatten=test_set_x_orig.reshape(test_set_x_orig.shape[0],-1).T
print ("train_set_x_flatten shape: " + str(train_set_x_flatten.shape))
print ("train_set_y shape: " + str(train_set_y.shape))
print ("test_set_x_flatten shape: " + str(test_set_x_flatten.shape))
print ("test_set_y shape: " + str(test_set_y.shape))
print ("sanity check after reshaping: " + str(train_set_x_flatten[0:5,0]))
5.彩色图像像素值实际上是一个由三个数字组成的向量,范围从0到255。这里给他标准化一下,不是图像需要从每个示例中减去整个数组的平均值,然后将每个样本除以整个数组的标准偏差,图像的话直接除以255就完事儿了
train_set_x = train_set_x_flatten/255. test_set_x = test_set_x_flatten/255.
6.使用np.exp写激活函数sigmoid
def sigmoid(z):
s=1/(1+np.exp(-z))
return s
7.由于是简单的单层神经网络,直接初始化W,B为0就可以了,assert负责检查一下w、b的格式(形状),看看和预期的一不一致,这里最好注意一下,不然深层的网络查错会很麻烦
def initialize_with_zeros(dim):
###
w -- initialized vector of shape (dim, 1)
b -- initialized scalar (corresponds to the bias)
###
w=np.zeros(shape=(dim,1))
b=0
assert(w.shape == (dim, 1))
assert(isinstance(b, float) or isinstance(b, int))
return w, b
8.前向传播的模块 np.squeeze()负责删掉维度为1的,防止后面cost出现一些奇奇怪怪的形状
def propagate(w, b, X, Y):
m = X.shape[1]
A=sigmoid(np.dot(w.T,X)+b)
cost = -1/m * np.sum(Y*np.log(A)+(1-Y)*np.log(1-A))
dw = 1/m *np.dot(X ,(A-Y).T)
db=1/m*np.sum(A-Y)
assert(dw.shape == w.shape)
assert(db.dtype == float)
cost = np.squeeze(cost)
assert(cost.shape == ())
grads = {"dw": dw,"db": db}
return grads, cost
9.优化optimize的模块,每迭代100次记录一下他的代价,并保存到costs里面,方便后面作图
def optimize(w, b, X, Y, num_iterations, learning_rate, print_cost = False):
costs = []
for i in range(num_iterations):
grads, cost = propagate(w,b,X,Y)
dw = grads["dw"]
db = grads["db"]
w=w-learning_rate*dw
b=b-learning_rate*db
if i % 100 == 0:
costs.append(cost)
if print_cost and i % 100 == 0:
print ("Cost after iteration %i: %f" %(i, cost))
params = {"w": w,
"b": b}
grads = {"dw": dw,
"db": db}
return params, grads, costs
10.对图片进行预测的模块
def predict(w, b, X):
m = X.shape[1]
Y_prediction = np.zeros((1,m))
w = w.reshape(X.shape[0], 1)
A = sigmoid(np.dot(w.T,X)+b)
for i in range(A.shape[1]):
Y_prediction[0,i] = 1 if A[0,i] > 0.5 else 0
###
if A[0,i]>0.5:
Y_prediction[0,i]=1
else:
Y_prediction[0,i]=0
###
assert(Y_prediction.shape == (1, m))
return Y_prediction
11.模型整理(其实就是把之前做的模块整合到一起),打印一下此时的cost
def model(X_train, Y_train, X_test, Y_test, num_iterations = 2000, learning_rate = 0.5, print_cost = False):
w,b=initialize_with_zeros(X_train.shape[0])
parameters, grads, costs = optimize(w,b,X_train,Y_train,num_iterations,learning_rate,print_cost)
w = parameters["w"]
b = parameters["b"]
Y_prediction_test = predict(w,b,X_test)
Y_prediction_train = predict(w,b,X_train)
print("train accuracy: {} %".format(100 - np.mean(np.abs(Y_prediction_train - Y_train)) * 100))
print("test accuracy: {} %".format(100 - np.mean(np.abs(Y_prediction_test - Y_test)) * 100))
d = {"costs": costs,
"Y_prediction_test": Y_prediction_test,
"Y_prediction_train" : Y_prediction_train,
"w" : w,
"b" : b,
"learning_rate" : learning_rate,
"num_iterations": num_iterations}
return d
12.跑模型~
d = model(train_set_x, train_set_y, test_set_x, test_set_y, num_iterations = 2000, learning_rate = 0.005, print_cost = True)
13.收获的时候到了,看看你的。这个index可以随便换,只要是测试集里面的图片标签都可以(大概是),验收一下成果
index = 1
plt.imshow(test_set_x[:,index].reshape((num_px, num_px, 3)))
print ("y = " + str(test_set_y[0,index]) + ", you predicted that it is a \"" + classes[int(d["Y_prediction_test"][0,index])].decode("utf-8") + "\" picture.")
14.做个图,看看loss(也就是cost)
costs = np.squeeze(d['costs'])
plt.plot(costs)
plt.ylabel('cost')
plt.xlabel('iterations (per hundreds)')
plt.title("Learning rate =" + str(d["learning_rate"]))
plt.show()
完结撒花!对了还有数据集我不知道怎么弄到博客园上,先试试,不行的话我再开一个专门上传这个数据集
15.补一个大佬给的加载文件的方法。
import numpy as np
import h5py
def load_dataset():
train_dataset = h5py.File('datasets/train_catvnoncat.h5', "r")
train_set_x_orig = np.array(train_dataset["train_set_x"][:]) # your train set features
train_set_y_orig = np.array(train_dataset["train_set_y"][:]) # your train set labels
test_dataset = h5py.File('datasets/test_catvnoncat.h5', "r")
test_set_x_orig = np.array(test_dataset["test_set_x"][:]) # your test set features
test_set_y_orig = np.array(test_dataset["test_set_y"][:]) # your test set labels
classes = np.array(test_dataset["list_classes"][:]) # the list of classes
train_set_y_orig = train_set_y_orig.reshape((1, train_set_y_orig.shape[0]))
test_set_y_orig = test_set_y_orig.reshape((1, test_set_y_orig.shape[0]))
return train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classes
#来自一位csdn大佬,有机会的话希望能给他点一点小红心
浙公网安备 33010602011771号