吴恩达Coursera深度学习课程 deeplearning.ai (4-2) 深度卷积网络--编程作业

这篇博客介绍了如何使用Keras建立深度学习模型,包括解决Happy House问题的简单模型和解决梯度消失问题的残差网络(ResNets)。在Happy House问题中,通过Keras快速建立模型,训练和测试,实现99%的训练准确率和95%的测试准确率。然后,深入探讨了ResNets的结构和解决深层网络训练问题的方法,包括一致块和卷积块,并给出了ResNet-50的实现细节。
摘要由CSDN通过智能技术生成

Part 1:Keras tutorial - the Happy House

第二周的第一个作业

  • 学习使用 Keras
    • Keras是一个高层神经网络API,Keras由纯Python编写而成并基Tensorflow、Theano以及CNTK后端。
  • 尝试如何在数小时内搭建一个深度学习算法

为什么要使用Keras ?

  • 可以让深度学习工程师快速搭建和试验不同的模型。比TensorFlow更加抽象,以最短时间将想法变为模型。
  • 但是,由于高度抽象,所以不够灵活,很多复杂的模型仍然需要用TensorFlow模型实现。
  • Keras 对于大部分通用模型都可以快速搭建,不过运行速度较慢一些,对于快速搭建和试验也可以了。

在下面练习中,我们将解决 “Happy House” 问题。

导包
import numpy as np
from keras import layers
from keras.layers import Input, Dense, Activation, ZeroPadding2D, BatchNormalization, Flatten, Conv2D
from keras.layers import AveragePooling2D, MaxPooling2D, Dropout, GlobalMaxPooling2D, GlobalAveragePooling2D
from keras.models import Model
from keras.preprocessing import image
from keras.utils import layer_utils
from keras.utils.data_utils import get_file
from keras.applications.imagenet_utils import preprocess_input
import pydot
from IPython.display import SVG
from keras.utils.vis_utils import model_to_dot
from keras.utils import plot_model
from kt_utils import *

import keras.backend as K
K.set_image_data_format('channels_last')
import matplotlib.pyplot as plt
from matplotlib.pyplot import imshow

%matplotlib inline

上面我们导入了很多keras的包,都可以直接调用, 比如:

X = Input(...)
X = ZeroPadding2D(...)

1 The Happy House

下个假期,你打算和五个朋友在校外过一周。下图是一个不错的房子可以做很多事情,最重要的是每个人都想在房子里的时候是快乐的,所以任何想住进这个房子的人都必须证明他们现在的快乐状态。

image

作为一个深度学习专家,为了保证”happy”规则的严格执行,你打算建立一个算法,利用门口摄像头采集的照片判断该人是否”happy”, 只有”happy”的时候们才打开。

你已经收集并标注了一些摄像头采集的你的和你朋友的照片。

image

数据标准化

X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes = load_dataset()

# Normalize image vectors
X_train = X_train_orig/255.
X_test = X_test_orig/255.

# Reshape
Y_train = Y_train_orig.T
Y_test = Y_test_orig.T

print ("number of training examples = " + str(X_train.shape[0]))
print ("number of test examples = " + str(X_test.shape[0]))
print ("X_train shape: " + str(X_train.shape))
print ("Y_train shape: " + str(Y_train.shape))
print ("X_test shape: " + str(X_test.shape))
print ("Y_test shape: " + str(Y_test.shape))

# number of training examples = 600
# number of test examples = 150
# X_train shape: (600, 64, 64, 3)
# Y_train shape: (600, 1)
# X_test shape: (150, 64, 64, 3)
# Y_test shape: (150, 1)

数据集细节

  • 照片大小(64x64x3)
  • 训练集:600张
  • 测试集:150张

是时候大展身手了,加油!

2 利用Keras建立模型

Keras很擅长快速原型建模,你可以在短时间内得到显著的效果。

举例:

def model(input_shape):
    # Define the input placeholder as a tensor with shape input_shape. Think of this as your input image!
    X_input = Input(input_shape)

    # Zero-Padding: pads the border of X_input with zeroes
    X = ZeroPadding2D((3, 3))(X_input)

    # CONV -> BN -> RELU Block applied to X
    X = Conv2D(32, (7, 7), strides = (1, 1), name = 'conv0')(X)
    X = BatchNormalization(axis = 3, name = 'bn0')(X)
    X = Activation('relu')(X)

    # MAXPOOL
    X = MaxPooling2D((2, 2), name='max_pool')(X)

    # FLATTEN X (means convert it to a vector) + FULLYCONNECTED
    X = Flatten()(X)
    X = Dense(1, activation='sigmoid', name='fc')(X)

    # Create model. This creates your Keras model instance, you'll use this instance to train/test the model.
    model = Model(inputs = X_input, outputs = X, name='HappyModel')

    return model

注意到keras并没有像numpy/TensorFlow那样定义X, Z, A1, Z1, A2,… 而是采用了不同的变量表示方式,输入层X_input, 其他各层都用X迭代。最后利用X_input 和最后的X 来建立模型。

练习:实现 HappyModel()
# GRADED FUNCTION: HappyModel

def HappyModel(input_shape):
    """
    Implementation of the HappyModel.

    Arguments:
    input_shape -- shape of the images of the dataset

    Returns:
    model -- a Model() instance in Keras
    """

    ### START CODE HERE ###
    # Feel free to use the suggested outline in the text above to get started, and run through the whole
    # exercise (including the later portions of this notebook) once. The come back also try out other
    # network architectures as well. 

    # Define the input placeholder as a tensor with shape input_shape. Think of this as your input image!
    X_input = Input(input_shape)

    # Zero-Padding: pads the border of X_input with zeroes
    X = ZeroPadding2D((3, 3))(X_input)

    # CONV -> BN -> RELU Block applied to X
    X = Conv2D(32, (7, 7), strides = (1, 1), name = 'conv0')(X)
    X = BatchNormalization(axis = 3, name = 'bn0')(X)
    X = Activation('relu')(X)

    # MAXPOOL
    X = MaxPooling2D((2, 2), name='max_pool')(X)

    # FLATTEN X (means convert it to a vector) + FULLYCONNECTED
    X = Flatten()(X)
    X = Dense(1, activation='sigmoid', name='fc')(X)

    # Create model. This creates your Keras model instance, you'll use this instance to train/test the model.
    model = Model(inputs = X_input, outputs = X, name='HappyModel')

    ### END CODE HERE ###

    return model

现在你已经建立了一个方法来描述你的模型,keras利用下面四步来训练和测试模型

  • 建立模型
    • model = HappyModel(X_train.shape[1:])
  • 编译模型
    • model.compile(optimizer = “…”, loss = “…”, metrics = [“accuracy”])
  • 训练模型
    • model.fit(x = …, y = …, epochs = …, batch_size = …)
  • 测试模型
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值