Neural Networks and Deep Learning:https://www.coursera.org/learn/neural-networks-deep-learning/home/week/2
这段时间会一直更深度学习的内容。
先明确说明代码不是我写的,我只是整理,来源于Cousera的代码,将代码大幅度缩短,补充说明。
h5文件,pdf和ipynb也有整理好的现成的,里面相对更详细点。
https://download.csdn.net/download/qq_42731466/10929001
import numpy as np
import matplotlib.pyplot as plt
import h5py
import scipy
from PIL import Image
from scipy import ndimage
# 0.数据读取
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
# 1.数据整理(列表重装,装置,标准化)
# 这一部分是对数据进行整理,将每一个图像调整为一个列向量
train_set_x_orig, train_set_y, test_set_x_orig, test_set_y, classes = load_dataset()
m_train = train_set_x_orig.shape[0] # 209 个训练集
m_test = test_set_x_orig.shape[0] # 50 个测试集
num_px = train_set_x_orig.shape[1] # 64 像素/边长
# 1.0 列表重装、转置
train_set_x_flatten = train_set_x_orig.reshape(train_set_x_orig.shape[0],-1).T # (12288, 209)
test_set_x_flatten = test_set_x_orig.reshape(test_set_x_orig.shape[0],-1).T # (12288, 50)
# 1.1 标准化
train_set_x = train_set_x_flatten/255.
test_set_x = test_set_x_flatten/255.
# 2.深度学习 (模块准备,参数初始化)(浅度学习,误)
# 2.0模块准备,(sigmoid,predict)
sigmoid = lambda z:1/(1+np.exp(-z))
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] = int(A[0,i]+0.5)
return Y_prediction
# 2.1 参数初始化(参数初始化,超参设定)
# 2.1.0初始化 w,b
dim = train_set_x.shape[0] # (12288, 209)
w = np.zeros((dim,1)) # (12288, 1)
b = 0 # Real
# 2.1.1设定超参,迭代次数和学习速率
num_iterations = 2000 #iter迭代次数,lr学习率
learning_rate = 0.005
m = train_set_x.shape[1] #样本数 # m
X,Y= train_set_x,train_set_y # x (12288, 209)
# y (1, 209)
costs = [] # 列表组装costs的变化
# 2.2 梯度下降(正向传播,反向传播,参数更新)
for i in range(num_iterations):
# 2.2.0正向传播
z = np.dot(w.T,X)+b # (1,12288)X(12288,209) (1,209)
A = sigmoid(z) # (1,209)
cost = -1/m*np.sum((Y*np.log(A)+(1-Y)*np.log(1-A))) # (1,209)(1,209)+(1,209)(1,209) (1,209)
# sum(1,209) Real
# 2.2.1反向传播
dz = A-Y # (1,209)
dw = 1/m*(np.dot(X,dz.T)) # (12288, 209)X(209,1) (12288,1)
db = 1/m*np.sum(A-Y) # sum(1,209) Real
# cost = np.squeeze(cost) # 去除冗余维度
# 2.2.2参数更新
w = w-learning_rate*dw
b = b-learning_rate*db
if i % 100 == 0:
costs.append(cost)
# print ("Cost after iteration %i: %f" %(i, cost)) # 不输出
# 3预测
Y_prediction_train = predict(w, b, train_set_x)
Y_prediction_test = predict(w, b, test_set_x)
print("训练集精度: {} %".format(100 - np.mean(np.abs(Y_prediction_train - train_set_y)) * 100))
print("测试集精度: {} %".format(100 - np.mean(np.abs(Y_prediction_test - test_set_y)) * 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}
# 图像被错误分类的例子
index = 1
plt.imshow(test_set_x[:,index].reshape((num_px, num_px, 3)))
res = test_set_y[0,index]
print ("y = " + str(res) + ", you predicted that it is a \"" + classes[res].decode("utf-8") + "\" picture.")
my_image = 'Sample4.jpg' # 这个位置放自己的图来进行测试
fname = my_image
image = np.array(ndimage.imread(fname, flatten=False))
my_image = scipy.misc.imresize(image, size=(num_px,num_px)).reshape((1, num_px*num_px*3)).T
my_predicted_image = predict(d["w"], d["b"], my_image)
plt.imshow(image)
print("y = " + str(np.squeeze(my_predicted_image)) + ", your algorithm predicts a \"" + classes[int(np.squeeze(my_predicted_image)),].decode("utf-8") + "\" picture.")
1.数据整理
1.0 列表重装
1.1 转置
1.2 标准化2.深度学习
2.0模块准备
2.0.1 sigmoid
2.0.2 predict2.1参数初始化
2.1.0 参数初始化
2.1.1 超参设定2.2 梯度下降
2.2.0 正向传播
2.2.1 反向传播
2.2.2 参数更新
- 预测
1.h5文件的读取写入
文档:http://docs.h5py.org/en/latest/index.html
处理的图像全部集中在.h5内,
比如:
将一个数组保存起来
data = array([2.2 , 2.30853994, 2.18838609, 2.32246582, 2.17417731,
2.33980757, 2.15683671, 2.36143782, 2.13574103, 2.38846572,
2.11018235, 2.42230381, 2.07938188, 2.46475111, 2.04252434,
2.51809121, 1.99882507, 2.58519528, 1.94764491, 2.66960134])
处理如下:
with h5py.File('result.h5','w') as f:
f['alg0'] = data
读取:
f = h5py.File('result.h5','r')
#s = list(f.keys()) #可以查看键
dataRecover = f['alg0'].value
2.scipy对于图像的处理
https://www.yiibai.com/scipy/scipy_ndimage.html?app=post
https://blog.csdn.net/lanchunhui/article/details/56292337
ndimage.imread
是来读图像,jpg会得到3个通道,有些图像会得到4个,比如png,不同在于RGB还是RGBA
代码最后用的scipy
下的ndimage.imread
和scipy.misc.imresize
需要注意的是:
D:\ipykernel_launcher.py:2: DeprecationWarning: `imread` is deprecated!
`imread` is deprecated in SciPy 1.0.0.
Use ``matplotlib.pyplot.imread`` instead.
scipy
中的ndimage.imread
被将被弃用了,matplotlib.pyplot.imread
来代替。
这个好办,替换成以下语句:
image = np.array(matplotlib.pyplot.imread(fname))
然后再检查一下范围的类型和维度,和ndimage.imread
得到的是一致的,注意返回的类型是
另外:
scipy.misc.imresize(img, size=(px1,px2))
用来改变原图像大小,img接受numpy ndarray,size可以是int,float,tuple,前两者表示缩放率,最后一个表示尺寸大小。
原代码中的处理是:
my_image = scipy.misc.imresize(image, size=(num_px,num_px)).reshape((1, num_px*num_px*3)).T image = np.array(ndimage.imread(fname, flatten=False))
拆成两段来看,先转化成了(num_px,num_px)的大小,然后reshape成一个行向量,最后转置成列向量。
需要提一下的:
D:\ipykernel_launcher.py:4: DeprecationWarning: imresize is deprecated! imresize is deprecated in SciPy 1.0.0, and will be removed in 1.2.0. Use ``skimage.transform.resize`` instead. after removing the cwd from sys.path.
这个 scipy.misc.imresize
同样将被弃用了,skimage取而代之,但是目前低版本仍然是可以用的。