1. 程式人生 > >deeplearning.ai 第四課第二週 resnet 50層神經網路實現

deeplearning.ai 第四課第二週 resnet 50層神經網路實現

1、匯入函式庫:

import numpy as np
from keras import layers
from keras.layers import Input, Add, Dense, Activation, ZeroPadding2D, BatchNormalization, Flatten, Conv2D, AveragePooling2D, MaxPooling2D, GlobalMaxPooling2D
from keras.models import Model, load_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 resnets_utils import * from keras.initializers import
glorot_uniform import scipy.misc from matplotlib.pyplot import imshow %matplotlib inline import keras.backend as K K.set_image_data_format('channels_last') K.set_learning_phase(1)

2、定義一個identity_block,該block的特質是其在一個block內,四層的矩陣維度都沒有變化
block如下圖所示:
identity_block

# GRADED FUNCTION: identity_block

def identity_block
(X, f, filters, stage, block):
""" Implementation of the identity block as defined in Figure 3 Arguments: X -- input tensor of shape (m, n_H_prev, n_W_prev, n_C_prev) f -- integer, specifying the shape of the middle CONV's window for the main path filters -- python list of integers, defining the number of filters in the CONV layers of the main path stage -- integer, used to name the layers, depending on their position in the network block -- string/character, used to name the layers, depending on their position in the network Returns: X -- output of the identity block, tensor of shape (n_H, n_W, n_C) """ # defining name basis conv_name_base = 'res' + str(stage) + block + '_branch' bn_name_base = 'bn' + str(stage) + block + '_branch' # Retrieve Filters F1, F2, F3 = filters # Save the input value. You'll need this later to add back to the main path. X_shortcut = X # First component of main path X = Conv2D(filters = F1, kernel_size = (1, 1), strides = (1,1), padding = 'valid', name = conv_name_base + '2a', kernel_initializer = glorot_uniform(seed=0))(X) X = BatchNormalization(axis = 3, name = bn_name_base + '2a')(X) X = Activation('relu')(X) ### START CODE HERE ### # Second component of main path (≈3 lines) X = Conv2D(filters=F2,kernel_size=(f,f),strides=(1,1),padding='same',name=conv_name_base+'2b',kernel_initializer=glorot_uniform(seed=0))(X) X = BatchNormalization(axis = 3, name = bn_name_base + '2b')(X) X = Activation('relu')(X) # Third component of main path (≈2 lines) X = Conv2D(filters=F3,kernel_size=(1,1),strides=(1,1),padding='valid',name=conv_name_base+'2c',kernel_initializer=glorot_uniform(seed=0))(X) X = BatchNormalization(axis=3,name=bn_name_base+'2c')(X) # Final step: Add shortcut value to main path, and pass it through a RELU activation (≈2 lines) X = Add()([X,X_shortcut]) X = Activation('relu')(X) ### END CODE HERE ### return X

3、定義convolution_block 該定義是,該block跳躍兩層,總共有四層,且中間傳輸資料維度有變化。圖片如下
convolution_block

def convolutional_block(X, f, filters, stage, block, s = 2):
    """
    Implementation of the convolutional block as defined in Figure 4

    Arguments:
    X -- input tensor of shape (m, n_H_prev, n_W_prev, n_C_prev)
    f -- integer, specifying the shape of the middle CONV's window for the main path
    filters -- python list of integers, defining the number of filters in the CONV layers of the main path
    stage -- integer, used to name the layers, depending on their position in the network
    block -- string/character, used to name the layers, depending on their position in the network
    s -- Integer, specifying the stride to be used

    Returns:
    X -- output of the convolutional block, tensor of shape (n_H, n_W, n_C)
    """

    # defining name basis
    conv_name_base = 'res' + str(stage) + block + '_branch'
    bn_name_base = 'bn' + str(stage) + block + '_branch'

    # Retrieve Filters
    F1, F2, F3 = filters

    # Save the input value
    X_shortcut = X


    ##### MAIN PATH #####
    # First component of main path 
    X = Conv2D(F1, (1, 1), strides = (s,s), name = conv_name_base + '2a', kernel_initializer = glorot_uniform(seed=0))(X)
    X = BatchNormalization(axis = 3, name = bn_name_base + '2a')(X)
    X = Activation('relu')(X)

    ### START CODE HERE ###

    # Second component of main path (≈3 lines)
    X = Conv2D(F2,(f,f),strides=(1,1),name=conv_name_base+'2b',padding='same',kernel_initializer=glorot_uniform(seed=0))(X)
    X = BatchNormalization(axis=3,name=bn_name_base+'2b')(X)
    X = Activation('relu')(X)

    # Third component of main path (≈2 lines)
    X = Conv2D(F3,(1,1),strides=(1,1),name=conv_name_base+'2c',padding='valid',kernel_initializer=glorot_uniform(seed=0))(X)
    X = BatchNormalization(axis=3,name=bn_name_base+'2c')(X)

    ##### SHORTCUT PATH #### (≈2 lines)
    X_shortcut = Conv2D(F3,(1,1),strides=(s,s),name=conv_name_base+'1',padding='valid',kernel_initializer=glorot_uniform(seed=0))(X_shortcut)
    X_shortcut = BatchNormalization(axis=3,name=bn_name_base+'1')(X_shortcut)

    # Final step: Add shortcut value to main path, and pass it through a RELU activation (≈2 lines)
    X = Add()([X,X_shortcut])
    X = Activation('relu')(X)

    ### END CODE HERE ###

    return X

4、建立50層的resnet,相應構圖及要求如下:
resnet構圖

resnet 詳細要求

# GRADED FUNCTION: ResNet50

def ResNet50(input_shape = (64, 64, 3), classes = 6):
    """
    Implementation of the popular ResNet50 the following architecture:
    CONV2D -> BATCHNORM -> RELU -> MAXPOOL -> CONVBLOCK -> IDBLOCK*2 -> CONVBLOCK -> IDBLOCK*3
    -> CONVBLOCK -> IDBLOCK*5 -> CONVBLOCK -> IDBLOCK*2 -> AVGPOOL -> TOPLAYER

    Arguments:
    input_shape -- shape of the images of the dataset
    classes -- integer, number of classes

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

    # Define the input as a tensor with shape input_shape
    X_input = Input(input_shape)


    # Zero-Padding
    X = ZeroPadding2D((3, 3))(X_input)

    # Stage 1
    X = Conv2D(64, (7, 7), strides = (2, 2), name = 'conv1', kernel_initializer = glorot_uniform(seed=0))(X)
    X = BatchNormalization(axis = 3, name = 'bn_conv1')(X)
    X = Activation('relu')(X)
    X = MaxPooling2D((3, 3), strides=(2, 2))(X)

    # Stage 2
    X = convolutional_block(X, f = 3, filters = [64, 64, 256], stage = 2, block='a', s = 1)
    X = identity_block(X, 3, [64, 64, 256], stage=2, block='b')
    X = identity_block(X, 3, [64, 64, 256], stage=2, block='c')

    ### START CODE HERE ###

    # Stage 3 (≈4 lines)
    X = convolutional_block(X,f=3,filters=[128,128,512],stage=3,block='a',s=2)
    X = identity_block(X,f=3,filters=[128,128,512],stage=3,block='b')
    X = identity_block(X,f=3,filters=[128,128,512],stage=3,block='c')
    X = identity_block(X,f=3,filters=[128,128,512],stage=3,block='d')

    # Stage 4 (≈6 lines)
    X = convolutional_block(X,f = 3, filters=[256,256,1024],stage=4,block='a',s=2)
    X = identity_block(X,f=3,filters=[256,256,1024],stage=4,block='b')
    X = identity_block(X,f=3,filters=[256,256,1024],stage=4,block='c')
    X = identity_block(X,f=3,filters=[256,256,1024],stage=4,block='d')
    X = identity_block(X,f=3,filters=[256,256,1024],stage=4,block='e')
    X = identity_block(X,f=3,filters=[256,256,1024],stage=4,block='f')

    # Stage 5 (≈3 lines)
    X = convolutional_block(X,f=3,filters=[512,512,2048],stage=5,block='a',s=2)
    X = identity_block(X,f=3,filters=[512,512,2048],stage=5,block='b')
    X = identity_block(X,f=3,filters=[512,512,2048],stage=5,block='c')

    # AVGPOOL (≈1 line). Use "X = AveragePooling2D(...)(X)"
    X = AveragePooling2D(pool_size=(2,2),strides=(1,1),padding='valid')(X)

    ### END CODE HERE ###

    # output layer
    X = Flatten()(X)
    X = Dense(classes, activation='softmax', name='fc' + str(classes), kernel_initializer = glorot_uniform(seed=0))(X)


    # Create model
    model = Model(inputs = X_input, outputs = X, name='ResNet50')

    return model

5、模型實體化:

model = ResNet50(input_shape = (64, 64, 3), classes = 6)

6、定義模型訓練過程及相應超參 (compile)

model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

7、訓練資料載入:

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.

# Convert training and test labels to one hot matrices
Y_train = convert_to_one_hot(Y_train_orig, 6).T
Y_test = convert_to_one_hot(Y_test_orig, 6).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))

8、模型訓練:

model.fit(X_train, Y_train, epochs = 100, batch_size = 32)

輸出如下:
訓練輸出過程(中間間斷過重新訓練會承接上一次訓練引數開始)

9、模型評估:

preds = model.evaluate(X_test, Y_test)
print ("Loss = " + str(preds[0]))
print ("Test Accuracy = " + str(preds[1]))

模型測試結果

相關推薦

deeplearning.ai 第二 resnet 50神經網路實現

1、匯入函式庫: import numpy as np from keras import layers from keras.layers import Input, Add, Dense, Activation, ZeroPadding2D, Batc

deeplearning.ai 第一, 卷積神經網路的tensorflow實現

1、載入需要模組和函式: import math import numpy as np import h5py import matplotlib.pyplot as plt import scipy from PIL import Image from

deeplearning.ai第一:卷積神經網路

1 計算機視覺 計算機視覺包含的典型任務有: 影象分類; 目標檢測; 影象風格轉換。 計算機視覺任務的特點是輸入資料量較大,對於一副1000 * 1000 * 3的影象而言,需要的輸入神經元數量為3百萬。假如第一隱藏層有1000個神經元,那麼

deeplearning.ai 第二,keras導航

1、函式庫匯入:(案例是一個happyhouse案例) import numpy as np from keras import layers from keras.layers import Input, Dense, Activation, ZeroP

吳恩達deeplearning.ai學習心得:卷積神經網路與計算機視覺

不久前,Coursera 上放出了吳恩達 deeplearning.ai 的第四門課程《卷積神經網路》。本文是加拿大國家銀行首席分析師 Ryan Shrott 在完成該課程後所寫的學習心得,有助於大家直觀地瞭解、學習計算機視覺。 我最近在 Coursera 上完成了吳恩達教授的計算機視覺課程。吳恩達

deeplearning.ai 第一,step by step 卷積神經網路的python實現

1、填充零的padding函式實現 # GRADED FUNCTION: zero_pad def zero_pad(X, pad): """ Pad with zeros all images of the dataset X. The

deep learning 吳恩達 第二程式設計 Keras - Tutorial - Happy House v2

Keras tutorial - the Happy House Welcome to the first assignment of week 2. In this assignment, you will: Learn to use Keras, a high-level neur

deep learning 吳恩達 第二 Residual Networks - v2

Residual Networks Welcome to the second assignment of this week! You will learn how to build very deep convolutional networks, using Residual Networ

Ng深度學習課程-第二筆記摘要

深度卷積網路:例項探究  為什麼要進行例項探究? 經典網路 殘差網路(ResNets)(Residual Networks (ResNets))

《深度學習——Andrew Ng》第二程式設計作業

深度學習第四課是 卷積神經網路 ,共四周內容: 第一週 卷積神經網路(卷積的含義,各個層的功能,如何計算資料在不同層的大小(shape)) 第二週 深度卷積網路:例項探究(LeNet5、ResNet50等經典神經網路,遷移學習,資料擴充) 第三週

吳恩達Coursera深度學習課程 DeepLearning.ai 提煉筆記(1-3)-- 淺神經網路

以下為在Coursera上吳恩達老師的DeepLearning.ai課程專案中,第一部分《神經網路和深度學習》第三週課程“淺層神經網路”部分關鍵點的筆記。筆記並不包含全部小視訊課程的記錄,如需學習筆記中捨棄的內容請至Coursera 或者 網易雲課堂

深度學習DeepLearning.ai系列課程學習總結:8. 多神經網路程式碼實戰

轉載過程中,圖片丟失,程式碼顯示錯亂。 為了更好的學習內容,請訪問原創版本: http://www.missshi.cn/api/view/blog/59ac0136e519f50d040001a7 Ps:初次訪問由於js檔案較大,請耐心等候(8s左

Coursera 吳恩達DeepLearning.AI sequence model 序列模型 第二 Emofify

這個Emojify裡最坑的一個就是,avg初始化的時候一定要是 (50,) ,如果你用(word_to_vec_map["a"]).shape 就死活過不了。Emojify!Welcome to the second assignment of Week 2. You are going to use wor

吳恩達Deeplearning.ai Sequence Model 第一------Deep RNNs

這一節主要講解了深度RNN網路的結構。 左邊是在一般的神經網路中DNN的結構,由輸入經過多層網路最終得到輸出 與此類似,Deep RNN也有類似的結構,之前的RNN網路都只是一層,如圖畫出了三層。 用a[l]<t>來表示第l層激勵的第t個t

吳恩達Deeplearning.ai Sequence Model 第一------Sampling novel sequence

這一節主要講了如何從一個訓練好的RNN中進行取樣得到序列 從訓練好的RNN中取樣出一個序列 之前在序列產生中講到,首先RNN輸入a<0>(0向量)和x<1>(0向量),通過一個RNN cell產生一個輸出y。 y = softm

吳恩達Deeplearning.ai Sequence Model 第一------Recurrent Neural Network Model

這一節內容比較多,主要講述瞭如何搭建一個RNN標準單元 使用標準神經網路的不足: 1.不同樣本的輸入輸出長度不等(雖然都可以padding成最大長度的樣本) 2.(更主要的原因)text不同的位置之間不共享學習到的引數 RNN模型,可以用左邊也可

吳恩達Deeplearning.ai Sequence Model 第一------Long Short Term Memory(LSTM)

這一節主要講解了LSTM單元 LSTM和GRU略有區別,可以說是一種更加通用的GRU模型 在LSTM中,c<t>不再等於a<t>,因此原來公式中的c<t-1>要改成a<t-1>,同時在LSTM中,也沒有了Γ

Coursera 吳恩達DeepLearning.AI sequence model 序列模型 第一 Improvise a Jazz Solo with an LSTM Network

We have taken care of the preprocessing of the musical data to render it in terms of musical "values." You can informally think of each "value" as a note,

吳恩達Deeplearning.ai Sequence Model 第一------Backpropagation through time

這一節主要講了RNN的反向傳播過程 BP即從輸出到輸入,因此和FP是相反的一些箭頭 計算a的引數Wa和ba,計算y的引數Wy和by,綠色的箭頭表示在這些不同的a和y的計算中都要用到這些引數(這裡展示的是一層RNN,後面會講到多層,在這一層中,不同的c

吳恩達 第一 程式設計 Convolution model - Step by Step - v2

  Convolutional Neural Networks: Step by Step Welcome to Course 4's first assignment! In this assignment, you will implement convolutional (C