Neural Networks and Deep Learning:https://www.coursera.org/learn/neural-networks-deep-learning/home/week/2
這段時間會一直更深度學習的內容。
先明確說明代碼不是我寫的,我只是整理,來源於Cousera的代碼,將代碼大幅度縮短,補充說明。
-1.效果
h5文件,pdf和ipynb也有整理好的現成的,裏面相對更詳細點。
https://download.csdn.net/download/qq_42731466/10929001
0. 如何識別貓
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
得到的是一致的,注意返回的類型是<class 'numpy.ndarray'>
另外:
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取而代之,但是目前低版本仍然是可以用的。