{"cells":[{"cell_type":"markdown","metadata":{},"source":"# Introduction\n\nAt the end of this lesson, you will be able to write TensorFlow and Keras code to use one of the best models in computer vision.\n\n# Lesson\n"},{"cell_type":"code","execution_count":null,"metadata":{"_kg_hide-input":true,"collapsed":true},"outputs":[],"source":"from IPython.display import YouTubeVideo\nYouTubeVideo('sDG5tPtsbSA', width=800, height=450)"},{"cell_type":"markdown","metadata":{},"source":"# Sample Code\n\n### Choose Images to Work With"},{"cell_type":"code","execution_count":null,"metadata":{"collapsed":true},"outputs":[],"source":"from os.path import join\n\nimage_dir = '../input/dog-breed-identification/train/'\nimg_paths = [join(image_dir, filename) for filename in \n                           ['0c8fe33bd89646b678f6b2891df8a1c6.jpg',\n                            '0c3b282ecbed1ca9eb17de4cb1b6e326.jpg',\n                            '04fb4d719e9fe2b6ffe32d9ae7be8a22.jpg',\n                            '0e79be614f12deb4f7cae18614b7391b.jpg']]"},{"cell_type":"markdown","metadata":{},"source":"### Function to Read and Prep Images for Modeling"},{"cell_type":"code","execution_count":null,"metadata":{"collapsed":true},"outputs":[],"source":"import numpy as np\nfrom tensorflow.python.keras.applications.resnet50 import preprocess_input\nfrom tensorflow.python.keras.preprocessing.image import load_img, img_to_array\n\nimage_size = 224\n\ndef read_and_prep_images(img_paths, img_height=image_size, img_width=image_size):\n    imgs = [load_img(img_path, target_size=(img_height, img_width)) for img_path in img_paths]\n    img_array = np.array([img_to_array(img) for img in imgs])\n    output = preprocess_input(img_array)\n    return(output)"},{"cell_type":"markdown","metadata":{},"source":"### Create Model with Pre-Trained Weights File. Make Predictions"},{"cell_type":"code","execution_count":null,"metadata":{"collapsed":true},"outputs":[],"source":"from tensorflow.python.keras.applications import ResNet50\n\nmy_model = ResNet50(weights='../input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels.h5')\ntest_data = read_and_prep_images(img_paths)\npreds = my_model.predict(test_data)"},{"cell_type":"markdown","metadata":{},"source":"### Visualize Predictions"},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":"from learntools.deep_learning.decode_predictions import decode_predictions\nfrom IPython.display import Image, display\n\nmost_likely_labels = decode_predictions(preds, top=3, class_list_path='../input/resnet50/imagenet_class_index.json')\n\nfor i, img_path in enumerate(img_paths):\n    display(Image(img_path))\n    print(most_likely_labels[i])"},{"cell_type":"markdown","metadata":{},"source":"# Exercise\nNow you are ready to **[use a powerful TensorFlow model](https://www.kaggle.com/kernels/fork/3349935)** yourself."}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.6.5"}},"nbformat":4,"nbformat_minor":2}