Keras 是一個用 Python 編寫的高級神經(jīng)網(wǎng)絡(luò) API,它能夠以 TensorFlow, CNTK, 或者 Theano 作為后端運行。 Keras 的開發(fā)重點是支持快速的實驗。能夠以最小的時延把你的想法轉(zhuǎn)換為實驗結(jié)果,是做好研究的關(guān)鍵。
Keras 是更高級的框架,對普通模型來說很友好,但是要實現(xiàn)更復(fù)雜的模型需要 TensorFlow 等低級的框架
導(dǎo)入一些包
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
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)
600個訓(xùn)練樣本,150個測試樣本,圖片維度 64*64*3 = 12288
2. 用Keras建模
Keras 可以快速建模,且模型效果不錯
舉個例子:
defmodel(input_shape):# 定義輸入的 placeholder 作為 tensor with shape input_shape. # 想象這是你的圖片輸入X_input = Input(input_shape)# Zero-Padding: pads the border of X_input with zeroesX = ZeroPadding2D((3,3))(X_input)# CONV -> BN -> RELU Block applied to XX = Conv2D(32,(7,7), strides =(1,1), name ='conv0')(X)X = BatchNormalization(axis =3, name ='bn0')(X)X = Activation('relu')(X)# MAXPOOLX = MaxPooling2D((2,2), name='max_pool')(X)# FLATTEN X (means convert it to a vector) + FULLYCONNECTEDX = 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
本次作業(yè)很open,可以自由搭建模型,修改超參數(shù),請注意各層之間的維度匹配
Keras Model 類參考鏈接
定義模型
# GRADED FUNCTION: HappyModeldefHappyModel(input_shape):"""Implementation of the HappyModel.Arguments:input_shape -- shape of the images of the datasetReturns: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. X_input = Input(input_shape)X = ZeroPadding2D((3,3))(X_input)X = Conv2D(32,(7,7), strides =(1,1), name='conv0')(X)X = BatchNormalization(axis =3, name='bn0')(X)X = Activation('relu')(X)X = MaxPooling2D((2,2), name='max_pool')(X)X = Flatten()(X)X = Dense(1, activation='sigmoid', name='fc')(X)model = Model(inputs = X_input, outputs = X, name='HappyModel')### END CODE HERE ###return model
創(chuàng)建模型實例
happyModel = HappyModel(X_train[0].shape)
配置訓(xùn)練模型
import keras
opt = keras.optimizers.Adam(learning_rate=0.01)
happyModel.compile(optimizer=opt, loss=keras.losses.BinaryCrossentropy(),metrics=['acc'])
訓(xùn)練 并 存儲返回的訓(xùn)練過程數(shù)據(jù)用于可視化
history = happyModel.fit(x=X_train, y=Y_train, validation_split=0.25, batch_size=32, epochs=30)
### START CODE HERE ### (1 line)from keras import metrics
preds = happyModel.evaluate(X_test, Y_test, batch_size=32, verbose=1, sample_weight=None)### END CODE HERE ###print(preds)print("Loss = "+str(preds[0]))print("Test Accuracy = "+str(preds[1]))
輸出:
5/5[==============================]- 0s 20ms/step - loss:0.2842- acc:0.9400[0.28415805101394653,0.9399999976158142]
Loss =0.28415805101394653
Test Accuracy =0.9399999976158142
3. 用你的圖片測試
### START CODE HERE ###
img_path ='images/1.jpg'### END CODE HERE ###
img = image.load_img(img_path, target_size=(64,64))
imshow(img)x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)print(happyModel.predict(x))
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 inlineimport keras.backend as K
K.set_image_data_format('channels_last')
K.set_learning_phase(1)
# GRADED FUNCTION: identity_blockdefidentity_block(X, f, filters, stage, block):"""Implementation of the identity block as defined in Figure 3Arguments: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 pathfilters -- python list of integers, defining the number of filters in the CONV layers of the main pathstage -- integer, used to name the layers, depending on their position in the networkblock -- string/character, used to name the layers, depending on their position in the networkReturns:X -- output of the identity block, tensor of shape (n_H, n_W, n_C)"""# defining name basisconv_name_base ='res'+str(stage)+ block +'_branch'bn_name_base ='bn'+str(stage)+ block +'_branch'# Retrieve FiltersF1, 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 pathX = 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_shortcut, X])X = Activation('relu')(X)### END CODE HERE ###return X
# GRADED FUNCTION: convolutional_blockdefconvolutional_block(X, f, filters, stage, block, s =2):"""Implementation of the convolutional block as defined in Figure 4Arguments: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 pathfilters -- python list of integers, defining the number of filters in the CONV layers of the main pathstage -- integer, used to name the layers, depending on their position in the networkblock -- string/character, used to name the layers, depending on their position in the networks -- Integer, specifying the stride to be usedReturns:X -- output of the convolutional block, tensor of shape (n_H, n_W, n_C)"""# defining name basisconv_name_base ='res'+str(stage)+ block +'_branch'bn_name_base ='bn'+str(stage)+ block +'_branch'# Retrieve FiltersF1, F2, F3 = filters# Save the input valueX_shortcut = X##### MAIN PATH ###### First component of main path X = Conv2D(F1,(1,1), strides =(s,s), 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(F2,(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(F3,(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)##### SHORTCUT PATH #### (≈2 lines)X_shortcut = Conv2D(F3,(1,1), strides=(s,s),padding='valid',name=conv_name_base+'1',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
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).Tprint("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 =1080
number of test examples =120
X_train shape:(1080,64,64,3)
Y_train shape:(1080,6)
X_test shape:(120,64,64,3)
Y_test shape:(120,6)