第四周编程作业(二)-Deep Neural Network for Image Classification: Application(三)

简介: 第四周编程作业(二)-Deep Neural Network for Image Classification: Application(三)

5 - L-layer Neural Network


Question: Use the helper functions you have implemented previously to build an $L$-layer neural network with the following structure: [LINEAR -> RELU]$\times$(L-1) -> LINEAR -> SIGMOID. The functions you may need and their inputs are:

def initialize_parameters_deep(layer_dims):
    ...
    return parameters 
def L_model_forward(X, parameters):
    ...
    return AL, caches
def compute_cost(AL, Y):
    ...
    return cost
def L_model_backward(AL, Y, caches):
    ...
    return grads
def update_parameters(parameters, grads, learning_rate):
    ...
    return parameters

### CONSTANTS ###
layers_dims = [12288, 20, 7, 5, 1] #  5-layer model

# GRADED FUNCTION: L_layer_model
def L_layer_model(X, Y, layers_dims, learning_rate = 0.0075, num_iterations = 3000, print_cost=False):#lr was 0.009
    """
    Implements a L-layer neural network: [LINEAR->RELU]*(L-1)->LINEAR->SIGMOID.
    Arguments:
    X -- data, numpy array of shape (number of examples, num_px * num_px * 3)
    Y -- true "label" vector (containing 0 if cat, 1 if non-cat), of shape (1, number of examples)
    layers_dims -- list containing the input size and each layer size, of length (number of layers + 1).
    learning_rate -- learning rate of the gradient descent update rule
    num_iterations -- number of iterations of the optimization loop
    print_cost -- if True, it prints the cost every 100 steps
    Returns:
    parameters -- parameters learnt by the model. They can then be used to predict.
    """
    np.random.seed(1)
    costs = []                         # keep track of cost
    # Parameters initialization.
    ### START CODE HERE ###
    parameters = initialize_parameters_deep(layers_dims)
    ### END CODE HERE ###
    # Loop (gradient descent)
    for i in range(0, num_iterations):
        # Forward propagation: [LINEAR -> RELU]*(L-1) -> LINEAR -> SIGMOID.
        ### START CODE HERE ### (≈ 1 line of code)
        AL, caches = L_model_forward(X,parameters)
        ### END CODE HERE ###
        # Compute cost.
        ### START CODE HERE ### (≈ 1 line of code)
        cost = compute_cost(AL,Y)
        ### END CODE HERE ###
        # Backward propagation.
        ### START CODE HERE ### (≈ 1 line of code)
        grads = L_model_backward(AL,Y,caches)
        ### END CODE HERE ###
        # Update parameters.
        ### START CODE HERE ### (≈ 1 line of code)
        parameters = update_parameters(parameters,grads,learning_rate)
        ### END CODE HERE ###
        # Print the cost every 100 training example
        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)
    # plot the 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


You will now train the model as a 5-layer neural network.

Run the cell below to train your model. The cost should decrease on every iteration. It may take up to 5 minutes to run 2500 iterations. Check if the "Cost after iteration 0" matches the expected output below, if not click on the square (⬛) on the upper bar of the notebook to stop the cell and try to find your error.

parameters = L_layer_model(train_x, train_y, layers_dims, num_iterations = 2500, print_cost = True)

Cost after iteration 0: 0.771749
Cost after iteration 100: 0.672053
Cost after iteration 200: 0.648263
Cost after iteration 300: 0.611507
Cost after iteration 400: 0.567047
Cost after iteration 500: 0.540138
Cost after iteration 600: 0.527930
Cost after iteration 700: 0.465477
Cost after iteration 800: 0.369126
Cost after iteration 900: 0.391747
Cost after iteration 1000: 0.315187
Cost after iteration 1100: 0.272700
Cost after iteration 1200: 0.237419
Cost after iteration 1300: 0.199601
Cost after iteration 1400: 0.189263
Cost after iteration 1500: 0.161189
Cost after iteration 1600: 0.148214
Cost after iteration 1700: 0.137775
Cost after iteration 1800: 0.129740
Cost after iteration 1900: 0.121225
Cost after iteration 2000: 0.113821
Cost after iteration 2100: 0.107839
Cost after iteration 2200: 0.102855
Cost after iteration 2300: 0.100897
Cost after iteration 2400: 0.092878


18.png

output_30_1.png


Expected Output:

<table>

<tr>

<td> Cost after iteration 0</td>

<td> 0.771749 </td>

</tr>

<tr>

<td> Cost after iteration 100</td>

<td> 0.672053 </td>

</tr>

<tr>

<td> ...</td>

<td> ... </td>

</tr>

<tr>

<td> Cost after iteration 2400</td>

<td> 0.092878 </td>

</tr>

</table>

pred_train = predict(train_x, train_y, parameters)

Accuracy: 0.985645933014

pred_test = predict(test_x, test_y, parameters)

Accuracy: 0.8


Expected Output:

<table>

<tr>

<td> Test Accuracy</td>

<td> 0.8 </td>

</tr>

</table>

Congrats! It seems that your 5-layer neural network has better performance (80%) than your 2-layer neural network (72%) on the same test set.

This is good performance for this task. Nice job!

Though in the next course on "Improving deep neural networks" you will learn how to obtain even higher accuracy by systematically searching for better hyperparameters (learning_rate, layers_dims, num_iterations, and others you'll also learn in the next course).


6) Results Analysis


First, let's take a look at some images the L-layer model labeled incorrectly. This will show a few mislabeled images.

print_mislabeled_images(classes, test_x, test_y, pred_test)


17.png

output_37_0.png


A few type of images the model tends to do poorly on include:

  • Cat body in an unusual position
  • Cat appears against a background of a similar color
  • Unusual cat color and species
  • Camera Angle
  • Brightness of the picture
  • Scale variation (cat is very large or small in image)


7) Test with your own image (optional/ungraded exercise)


Congratulations on finishing this assignment. You can use your own image and see the output of your model. To do that:

1. Click on "File" in the upper bar of this notebook, then click "Open" to go on your Coursera Hub.

2. Add your image to this Jupyter Notebook's directory, in the "images" folder

3. Change your image's name in the following code

4. Run the code and check if the algorithm is right (1 = cat, 0 = non-cat)!

## START CODE HERE ##
my_image = "my_image.jpg" # change this to the name of your image file 
my_label_y = [1] # the true class of your image (1 -> cat, 0 -> non-cat)
## END CODE HERE ##
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.")

Accuracy: 1.0
y = 1.0, your L-layer model predicts a "cat" picture.


16.png

output_40_1.png

References:

相关文章
|
人工智能 算法 定位技术
AR 导航导览技术如何实现的?提供全场景解决方案深度解析
传统导航在复杂场景中存在定位断层、空间认知成本高及实景导航实施难等问题。AR导航导览通过“实景+虚拟融合”,结合高精度定位引擎、实时三维渲染引擎和智能路径规划引擎,实现室内外无缝衔接的沉浸式体验。如需AR导航导览解决方案欢迎私信获取,如有项目合作及技术交流欢迎私信作者哦~
1538 1
|
算法 Java 程序员
Java中高级面试题总览(二)(1)
Java中高级面试题总览(二)
291 0
|
存储 数据库 文件存储
YashanDB目录划分
本文介绍了YashanDB的安装目录、HOME与DATA目录规划、运行日志目录配置以及共享集群部署时的LUN绑定操作。主要内容包括:安装目录设置在/home/yashan,HOME和DATA目录规划在/data/yashan;运行日志目录建议配置在独立物理盘以优化性能;共享集群部署需绑定系统盘和数据盘至指定路径(如/dev/yfs),并通过脚本生成配置文件完成LUN绑定,确保多路径WWID一致性。适用于集群多服务器并行安装场景。
|
算法 Java Linux
【数据结构】红黑树
【数据结构】红黑树
|
Python
Python二叉树的简单定义及使用
生活不会永远顺着我们,每个人都要通过自己的努力去决定生活的样子。
|
算法 Python
Python 手工计算x的算术平方根,一个中国古代的数学成就
Python 手工计算x的算术平方根,一个中国古代的数学成就
677 0
|
存储 SQL 弹性计算
如何轻松搞定SAP HANA数据库备份?
阿里云混合云备份(HBR)推出了SAP HANA BACKINT集成方案,成为亚洲首家通过SAP HANA BACKINT认证的云厂商,此外,HBR还可以为阿里云ECS,NAS,OSS以及自建机房内的文件,VMware,SQL Server等提供安全,高效的备份和容灾保护。
537 0
如何轻松搞定SAP HANA数据库备份?
|
Web App开发 JavaScript 前端开发
客户端与服务器持续同步解析(轮询,comet,WebSocket)
在B/S模型的Web应用中,客户端常常需要保持和服务器的持续更新。这种对及时性要求比较高的应用比如:股票价格的查询,实时的商品价格,自动更新的twitter timeline以及基于浏览器的聊天系统(如GTalk)等等。
2612 0
|
物联网 SDN 数据中心
|
2天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8077 2