Introduction to Deep Learning with Keras
Miguel Esteban
Data Scientist & Founder




# Import Conv2D layer and Flatten from tensorflow keras layers from tensorflow.keras.layers import Dense, Conv2D, Flatten# Instantiate your model as usual model = Sequential() # Add a convolutional layer with 32 filters of size 3x3 model.add(Conv2D(filters=32, kernel_size=3, input_shape=(28, 28, 1), activation='relu'))# Add another convolutional layer model.add(Conv2D(8, kernel_size=3, activation='relu')) # Flatten the output of the previous layer model.add(Flatten())# End this multiclass model with 3 outputs and softmax model.add(Dense(3, activation='softmax'))

# Import image from keras preprocessing from tensorflow.keras.preprocessing import image# Import preprocess_input from tensorflow keras applications resnet50 from tensorflow.keras.applications.resnet50 import preprocess_input# Load the image with the right target size for your model img = image.load_img(img_path, target_size=(224, 224))# Turn it into an array img = image.img_to_array(img)# Expand the dimensions so that it's understood by our network: # img.shape turns from (224, 224, 3) into (1, 224, 224, 3) img = np.expand_dims(img, axis=0)# Pre-process the img in the same way training images were img = preprocess_input(img)
# Import ResNet50 and decode_predictions from tensorflow.keras.applications.resnet50 from tensorflow.keras.applications.resnet50 import ResNet50, decode_predictions# Instantiate a ResNet50 model with imagenet weights model = ResNet50(weights='imagenet')# Predict with ResNet50 on our img preds = model.predict(img)# Decode predictions and print it print('Predicted:', decode_predictions(preds, top=1)[0])
Predicted: [('n07697313', 'cheeseburger', 0.9868016)]

Introduction to Deep Learning with Keras