参考文章链接:https://github.com/Kulbear/deep-learning-coursera/blob/master/Neural%20Networks%20and%20Deep%20Learning/Logistic%20Regression%20with%20a%20Neural%20Network%20mindset.ipynb
作业要求:使用神经网络的思维构建一个逻辑回归分类器识别出图片是否是猫,并使用自己的图片进行检测
1.导入各种需要使用的模块
原代码:
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
修改后的:
import numpy as np
import matplotlib.pyplot as plt
import h5py
from PIL import Image
import imageio
from lr_utils import load_dataset
%matplotlib inline
因为scipy版本问题。scipy>=1.0.0不再包含函数imread,imresize,官网给出的解释及解决办法如下:
`imread` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0. Use ``imageio.imread`` instead.
imresize is deprecated! imresize is deprecated in SciPy 1.0.0, and will be removed in 1.3.0. Use Pillow instead: numpy.array(Image.fromarray(arr).resize()).
可以在命令行窗口cmd通过pip list查看自己安装的各模块版本,因为本人是直接通过pip install XXX命令安装,所以安装的都是最新版
(因为当时出现问题在网上查有博主说是scipy版本过高,所以又把scipy版本变低了)
根据官网说明故将scipy去掉,换成了imageio,后面会看到如何使用
2.解决RuntimeWarning:divide by zero encountered in log;RuntimeWarning: invalid value encountered in multiply等等,以及结果出现nan值
这些bug都出在log函数的使用上,在训练过程中可能出现log函数的参数为0的情况,这个时候会导致计算结果趋于无穷即cost溢出的情况,打印出来的结果就是nan值,而警告是log参数过大或过小,所以只要优化一下log函数,这些问题就都解决了。
解决方法:
#正向传播和反向传播
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+1e-5)+(1-Y)*(np.log(1-A)))
cost = np.squeeze(cost)
assert(cost.shape == ())
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)
grads = {"dw":dw,"db":db}
return grads,cost
在np.log函数括号里添加一个很小的值,这里加的是。
3.imread,imresize的替代方法
原代码:
my_image = "my_image.jpg"
fname = "images/" + my_image
image = np.array(ndimage.imread(fname, flatten=False))
修改后:
image = np.array(imageio.imread(r"cat.jpg"))
原代码:
my_image = scipy.misc.imresize(image, size=(num_px, num_px)).reshape((1, num_px * num_px * 3)).T
修改后:
my_image = np.array(Image.fromarray(image).resize((num_px, num_px),Image.ANTIALIAS)) #ANTIALIAS表示保留图片所有像素,不丢失原有像素
my_image = my_image.reshape((1, num_px * num_px * 3)).T
附上测试自己下载图片的代码:
d = model(train_set_x, train_set_y, test_set_x, test_set_y, num_iterations = 2000, learning_rate = 0.5, print_cost = True)
image = np.array(imageio.imread(r"cat.jpg"))
num_px=64
my_image = np.array(Image.fromarray(image).resize((num_px, num_px),Image.ANTIALIAS))
my_image = my_image.reshape((1, num_px * num_px * 3)).T
#print(image)
#plt.imshow(my_image)
my_predicted_image = predict(d["w"], d["b"], my_image)
print("y = " + str(np.squeeze(my_predicted_image)) + ", your algorithm predicts a \"" + classes[int(np.squeeze(my_predicted_image)),].decode("utf-8") + "\" picture.")
最后结果: