Keras is a high-level neural networks API (programming framework), written in Python and capable of running on top of several lower-level frameworks including TensorFlow and CNTK.
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 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)) print ("number of test examples = " + str(X_test.shape)) 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)) 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. 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 #happyModel = HappyModel(X_train.shape[1:]) happyModel = HappyModel((64,64,3)) happyModel.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy']) happyModel.fit(x=X_train,y=Y_train,epochs=50,batch_size=50) preds = happyModel.evaluate(x=X_test,y=Y_test)
We import Conv2D BatchNormalization Activation ZeroPadding2D MaxPooling2D Flatten Dense from keras to help us build the model.
And then we go through four steps to train the model:
- Create the model by calling the function above
- Compile the model by calling model.compile(optimizer = "...", loss = "...", metrics = ["accuracy"])
- Train the model on train data by calling model.fit(x = ..., y = ..., epochs = ..., batch_size = ...)
- Test the model on test data by calling model.evaluate(x = ..., y = ...)
we find that the optimizer adam effective, and the loss function is determined by the type of the function
For detailed API docs ,look up them in the https://keras.io/