You have now built a function to describe your model. To train and test this model, there are four steps in Keras:
Create the model using functional API (example model below)
from tf.keras import Model #To instantiate a Keras model
from tf.keras import Input #To get input
def ExampleModel(input_shape):
"""
Implementation of the ExampleModel.
Arguments:
input_shape -- shape of the images of the dataset
(height, width, channels) as a tuple.
Note that this does not include the 'batch' as a dimension.
If you have a batch like 'X_train',
then you can provide the input_shape using
X_train.shape[1:]
Returns:
model -- a Model() instance in Keras
"""
X_input = Input(input_shape)
X = ZeroPadding2D(padding=3)(X_input)
X = Conv2D(32, (3, 3), strides=1, activation='relu')(X)
X = MaxPooling2D((2, 2), strides=2)(X)
X = Conv2D(64, (2, 2), strides=2, activation='relu')(X)
X = MaxPooling2D((2, 2), strides=1)(X)
X = Flatten()(X)
X = Dense(64, activation='relu')(X)
X = Dense(1, activation='sigmoid')(X)
model = Model(inputs = X_input, outputs = X, name = "HappyModel")
return model
model = ExampleModel(X_train.shape)
Compile the model by calling model.compile()
model.compile(optimizer = "...", loss = "...", metrics = ["accuracy"])
Train the model on train data by calling model.fit()
model.fit(x = ..., y = ..., epochs = ..., batch_size = ...)
Test the model on test data by calling model.evaluate()
eval_results = model.evaluate(x = ..., y = ...)
>>>> print(model.metrics_names) #prints a list of all metrics, including loss
['loss', 'accuracy', ...]
>>>> print(eval_results) #prints a list of all results from model.evaluate()
#in the order of model.metrics_names
[0.20, .97192, ...]
Returns the loss value & metrics values for the model in test mode (i.e. on test set).
We can also test a separate image (out of the test set). For that, image should first be loaded (tf.keras.preprocessing.image
has various methods to load an image and work with it) on to a variable, and then preprocessing (according to what our model takes as input) is required before it can be fed to the trained model for predictions using model.predict(x=input)
Save the model to a TensorFlow SavedModel file or a HDF5 file (optional step)
The savefile includes:
Saved models can be reinstantiated via tf.keras.models.load_model()
. The model returned by load_model is a compiled model ready to be used (unless the saved model was never compiled in the first place).
<aside> 💡 Models built with the Sequential and Functional API can be saved to both the HDF5 and SavedModel formats. Subclassed models can only be saved with the SavedModel format.
</aside>
model.save(filepath="/filepath", overwrite=True, include_optimizer=True,
save_format=None)
To load a saved model, use tf.keras.models.load_model()
new_model = tf.keras.models.load_model(filepath = "/filepath/modelfilename",
compile = True)
Following are some important layers used for Convolutional Neural Networks and some important functions required to define a model: