Learn practical skills, build real-world projects, and advance your career

Keras tutorial - Emotion Detection in Images of Faces

Welcome to the first assignment of week 2. In this assignment, you will:

  1. Learn to use Keras, 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.
  2. See how you can in a couple of hours build a deep learning algorithm.

Why are we using Keras?

  • Keras was developed to enable deep learning engineers to build and experiment with different models very quickly.
  • Just as TensorFlow is a higher-level framework than Python, Keras is an even higher-level framework and provides additional abstractions.
  • Being able to go from idea to result with the least possible delay is key to finding good models.
  • However, Keras is more restrictive than the lower-level frameworks, so there are some very complex models that you would still implement in TensorFlow rather than in Keras.
  • That being said, Keras will work fine for many common models.

Updates

If you were working on the notebook before this update...
  • The current notebook is version "v2a".
  • You can find your original work saved in the notebook with the previous version name ("v2").
  • To view the file directory, go to the menu "File->Open", and this will open a new tab that shows the file directory.
List of updates
  • Changed back-story of model to "emotion detection" from "happy house."
  • Cleaned/organized wording of instructions and commentary.
  • Added instructions on how to set input_shape
  • Added explanation of "objects as functions" syntax.
  • Clarified explanation of variable naming convention.
  • Added hints for steps 1,2,3,4

Load packages

  • In this exercise, you'll work on the "Emotion detection" model, which we'll explain below.
  • Let's load the required packages.
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
Using TensorFlow backend.

Note: As you can see, we've imported a lot of functions from Keras. You can use them by calling them directly in your code. Ex: X = Input(...) or X = ZeroPadding2D(...).

In other words, unlike TensorFlow, you don't have to create the graph and then make a separate sess.run() call to evaluate those variables.