前言:
文章以Andrew Ng 的 deeplearning.ai 视频课程为主线,记录Programming Assignments 的实现过程。相对于斯坦福的CS231n课程,Andrew的视频课程更加简单易懂,适合深度学习的入门者系统学习!
本次作业主要是在Programming Assignment1的基础上,调用之前定义的每个模块,实现训练和预测。涉及到的数据集还是之前的cat vs non-cat数据集,我们具体看一下代码的实现。
1.1 Data Preprocess
train_x_orig, train_y, test_x_orig, test_y, classes = load_data()
index = 10
plt.imshow(train_x_orig[index])
print ("y = " + str(train_y[0,index]) + ". It's a " + classes[train_y[0,index]].decode("utf-8") + " picture.")
m_train = train_x_orig.shape[0]
num_px = train_x_orig.shape[1]
m_test = test_x_orig.shape[0]
print ("Number of training examples: " + str(m_train))
print ("Number of testing examples: " + str(m_test))
print ("Each image is of size: (" + str(num_px) + ", " + str(num_px) + ", 3)")
print ("train_x_orig shape: " + str(train_x_orig.shape))
print ("train_y shape: " + str(train_y.shape))
print ("test_x_orig shape: " + str(test_x_orig.shape))
print ("test_y shape: " + str(test_y.shape))
train_x = train_x_flatten/255.
test_x = test_x_flatten/255.
1.2 Architecture of the model
下面我们看一下多层神经网络的训练过程代码实现:
def L_layer_model(X, Y, layers_dims, learning_rate = 0.0075, num_iterations = 3000, print_cost=False):#lr was 0.009
parameters = initialize_parameters_deep(layers_dims)
for i in range(0, num_iterations):
AL, caches = L_model_forward(X, parameters)
cost = compute_cost(AL, Y)
grads = L_model_backward(AL, Y, caches)
parameters = update_parameters(parameters, grads, learning_rate)
if print_cost and i % 100 == 0:
print ("Cost after iteration %i: %f" %(i, cost))
if print_cost and i % 100 == 0:
costs.append(cost)
plt.plot(np.squeeze(costs))
plt.ylabel('cost')
plt.xlabel('iterations (per tens)')
plt.title("Learning rate =" + str(learning_rate))
plt.show()
return parameters
其中涉及到的函数调用都在Assignment1中实现完成,定义一个5层网络进行训练
layers_dims = [12288, 20, 7, 5, 1]
parameters = L_layer_model(train_x, train_y, layers_dims, num_iterations = 2500, print_cost = True)
cost随迭代变化情况如下:
使用这个5层标准神经网络在test集合的识别准确率达到80%,我们可以对那些错误的分类结果做一个简单的分析:
print_mislabeled_images(classes, test_x, test_y, pred_test)
从错误分类的图片中我们可以发现:
1.Cat body in an unusual position
2.Cat appears against a background of a similar color
3.Unusual cat color and species
4.Camera Angle
5.Brightness of the picture
6.Scale variation (cat is very large or small in image)
存在这样的情况,模型难以正确识别。
1.3 Test with your own image
下面测试一下我们自己的图片,看一下模型的识别结果:
my_image = "my_image.jpg"
my_label_y = [1]
fname = "images/" + my_image
image = np.array(ndimage.imread(fname, flatten=False))
my_image = scipy.misc.imresize(image, size=(num_px,num_px)).reshape((num_px*num_px*3,1))
my_predicted_image = predict(my_image, my_label_y, parameters)
plt.imshow(image)
print ("y = " + str(np.squeeze(my_predicted_image)) + ", your L-layer model predicts a \"" + classes[int(np.squeeze(my_predicted_image)),].decode("utf-8") + "\" picture.")
运行结果为:
最后附上我作业的得分,表示我程序没有问题,如果觉得我的文章对您有用,请随意打赏,我将持续更新Deeplearning.ai的作业!
网友评论