<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Exercise
Step1:  2) Set Up Preprocessing
Scan and prepare the images
Step2:  3) Modeling
Create a Resnet50 model and save it as my_model.
Apply the read_and_prep_images() function to image_paths and save the result as image_data.
Use my_model to predict the contents of image_data and store the results inmy_preds`.
Apply the decode_predictions() function to my_preds.
You can focus on just the top prediction for each image with the argument top=1.
Step3:  4) Visualize Your Results
This tutorial will use a version of decode_predictions() that is optimized for Kaggle kernels rather than the TensorFlow version.
Step4:  Now you are ready to move on to transfer learning, which allows you to build fairly robust models with relatively little data.
Transfer Learning
At the end of this lesson, you will be able to use transfer learning to build highly accurate computer vision models for your custom purposes, even when you have relatively little data.
Specify the Model
Step5:  Compile the Model
Step6:  Fit the Model
<ASSISTANT_TASK:>
Python Code:
from os.path import join
hot_dog_image_dir = 'inputs/seefood/train/hot_dog'
hot_dog_paths = [join(hot_dog_image_dir, filename) for filename in
                ['1000288.jpg',
                 '127117.jpg']]
not_hot_dog_image_dir = 'inputs/seefood/train/not_hot_dog'
not_hot_dog_paths = [join(not_hot_dog_image_dir, filename) for filename in
                    ['823536.jpg',
                     '99890.jpg']]
img_paths = hot_dog_paths + not_hot_dog_paths
img_paths
Explanation: Exercise: Coding in TensorFlow and Keras
The TV show Silicon Valley starred an app called "See Food" that promised to identify food in pictures.
Yep, we're gonna build that.
1) Create Image Paths
We have supplied image files to examine.
Run the cell below to store the filepaths with the name image_paths:
End of explanation
import numpy as np
import tensorflow as tf
from tensorflow.python.keras.applications.resnet50 import preprocess_input
from tensorflow.python.keras.applications import ResNet50
from tensorflow.python.keras.preprocessing.image import load_img, img_to_array
image_size = 224
def read_and_prep_images(img_paths, img_height=image_size, img_width=image_size):
    imgs = [load_img(img_path, target_size=(img_height, img_width)) for img_path in img_paths]
    img_array = np.array([img_to_array(img) for img in imgs])
    return preprocess_input(img_array)
Explanation: 2) Set Up Preprocessing
Scan and prepare the images:
End of explanation
my_model = ResNet50(weights='inputs/resnet50_weights_tf_dim_ordering_tf_kernels.h5')
test_data = read_and_prep_images(img_paths)
preds = my_model.predict(test_data)
import sys
sys.path.append('inputs/utils')
from decode_predictions import decode_predictions
from IPython.display import Image, display
most_likely_labels = decode_predictions(preds, top=1, class_list_path='inputs/imagenet_class_index.json')
for i, img_path in enumerate(img_paths):
    display(Image(img_path))
    print(most_likely_labels[i])
Explanation: 3) Modeling
Create a Resnet50 model and save it as my_model.
Apply the read_and_prep_images() function to image_paths and save the result as image_data.
Use my_model to predict the contents of image_data and store the results inmy_preds`.
Apply the decode_predictions() function to my_preds.
You can focus on just the top prediction for each image with the argument top=1.
End of explanation
import sys
most_likely_labels = decode_predictions(preds, top=3, class_list_path='inputs/imagenet_class_index.json')
for i, img_path in enumerate(img_paths):
    display(Image(img_path))
    print(most_likely_labels[i])
Explanation: 4) Visualize Your Results
This tutorial will use a version of decode_predictions() that is optimized for Kaggle kernels rather than the TensorFlow version.
End of explanation
from tensorflow.python.keras.applications import ResNet50
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense, Flatten, GlobalAveragePooling2D
num_classes = 2
resnet_weights_path = 'inputs/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'
my_new_model = Sequential()
my_new_model.add(ResNet50(include_top=False, pooling='avg', weights=resnet_weights_path))
my_new_model.add(Dense(num_classes, activation='softmax'))
# No need to train the first layer of the ResNet model:
my_new_model.layers[0].trainable = False
Explanation: Now you are ready to move on to transfer learning, which allows you to build fairly robust models with relatively little data.
Transfer Learning
At the end of this lesson, you will be able to use transfer learning to build highly accurate computer vision models for your custom purposes, even when you have relatively little data.
Specify the Model
End of explanation
my_new_model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'])
Explanation: Compile the Model
End of explanation
from tensorflow.python.keras.applications.resnet50 import preprocess_input
from tensorflow.python.keras.preprocessing.image import ImageDataGenerator
image_size = 224
data_generator = ImageDataGenerator()
train_generator = data_generator.flow_from_directory(
        'inputs/rural_and_urban_photos/train',
        target_size=(image_size,image_size),
        batch_size=24,
        class_mode='categorical')
validation_generator = data_generator.flow_from_directory(
        'inputs/rural_and_urban_photos/val',
        target_size=(image_size,image_size),
        class_mode='categorical')
my_new_model.fit_generator(
        train_generator,
        steps_per_epoch=3,
        validation_data=validation_generator,
        validation_steps=1)
Explanation: Fit the Model
End of explanation
<END_TASK>