{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Intro\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"},{"metadata":{"_kg_hide-input":true,"collapsed":true,"trusted":false},"cell_type":"code","source":"from IPython.display import YouTubeVideo\nYouTubeVideo('sDG5tPtsbSA', width=800, height=450)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Sample Code\n\n### Choose Images to Work With"},{"metadata":{"collapsed":true,"trusted":false},"cell_type":"code","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']]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Function to Read and Prep Images for Modeling"},{"metadata":{"collapsed":true,"trusted":false},"cell_type":"code","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)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Create Model with Pre-Trained Weights File. Make Predictions"},{"metadata":{"collapsed":true,"trusted":false},"cell_type":"code","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)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Visualize Predictions"},{"metadata":{"trusted":false},"cell_type":"code","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])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Exercise\nNow you are ready to **[use a powerful TensorFlow model](https://www.kaggle.com/kernels/fork/521452)** yourself."},{"metadata":{},"cell_type":"markdown","source":"---\n\n\n\n\n*Have questions or comments? Visit the [Learn Discussion forum](https://www.kaggle.com/learn-forum/161321) to chat with other Learners.*"}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}