{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Intro\n\n**This is Lesson 3 in the [Deep Learning](https://www.kaggle.com/learn/deep-learning) track**  \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":{"_uuid":"d45fdc11ae5e48e49a606cded4182b7c4da4e3f1","_cell_guid":"b070a289-f505-4247-897a-6f2013a69849"}},{"cell_type":"code","source":"from IPython.display import YouTubeVideo\nYouTubeVideo('Epn3ryqr-F8', width=800, height=450)","metadata":{"_kg_hide-input":true,"_uuid":"66dc3b545857fbab1b09cd36916ea22826af9015","_cell_guid":"164a704f-7ac6-44ca-8b4d-9614e8a0516c","collapsed":true,"jupyter":{"outputs_hidden":true}},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Sample Code\n\n### Choose Images to Work With","metadata":{"_uuid":"cb9e861c5f2a765c1521aab7b3b551ac066f9ec7","_cell_guid":"c2bfc7c7-6520-4ec9-ba19-d31d219dced1"}},{"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                           ['0246f44bb123ce3f91c939861eb97fb7.jpg',\n                            '84728e78632c0910a69d33f82e62638c.jpg',\n                            '8825e914555803f4c67b26593c9d5aff.jpg',\n                            '91a5e8db15bccfb6cfa2df5e8b95ec03.jpg']]","metadata":{"_uuid":"5ae20575e2d003ab6ce09ff19d8cdd92204cf703","_cell_guid":"775beea9-6b34-4aa0-ac22-493e5d2abd9b","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Function to Read and Prep Images for Modeling","metadata":{"_uuid":"1e38d823eec3719864c0db6d6600d32c373efc5a","_cell_guid":"511df4a9-fa6c-49dd-8fbd-c0df7d5c0d36"}},{"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    return preprocess_input(img_array)","metadata":{"_uuid":"1a5e87fe71815f96305c304f0e60d69ea6dfcce9","_cell_guid":"3700e2d2-b1e2-4b7d-9b0e-5c70d67d97fa","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Create Model with Pre-Trained Weights File. Make Predictions","metadata":{"_uuid":"53aacd83b9296d7fd45051b90884e08077dd6477","_cell_guid":"32aa7325-ca97-4a54-b810-34cf11c4e8ee"}},{"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)","metadata":{"_uuid":"db5c4349ffc90e6aa4fb10655fb558da6808cc70","_cell_guid":"2a891e71-a6fe-4241-9eb9-38d076a85aa8","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Visualize Predictions","metadata":{"_uuid":"66738b98d9c027eae8cc97c5586a80091fe312b5","_cell_guid":"5d2f7427-d502-4fa5-a10c-ea3f6ee71d5b"}},{"cell_type":"code","source":"import sys\n# Add directory holding utility functions to path to allow importing\nsys.path.append('/kaggle/input/python-utility-code-for-deep-learning-exercises/utils')\nfrom decode_predictions import decode_predictions\n\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])","metadata":{"_uuid":"c97287027fd009c506a7d2b861ee39271505bc40","_cell_guid":"9f8d9df8-f369-4087-b3ea-ae2bcc071fa9","collapsed":true,"jupyter":{"outputs_hidden":true}},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Exercise\nNow you are ready to [use a powerful TensorFlow model](https://www.kaggle.com/dansbecker/my-first-exercise-with-tensorflow-and-keras/) yourself.\n\n# Continue\nAfter the exercise, continue to learn about [Transfer Learning](https://www.kaggle.com/dansbecker/transfer-learning/).  Transfer learning will let you leverage pre-trained models for purposes far beyond what they were originally built for. Most people are amazed when they first experience the power of transfer learning.\n\n---\nHave questions, comments or feedback?  Bring them to [the Learn forum](https://www.kaggle.com/learn-forum)\n\n**[Deep Learning Track Home](https://www.kaggle.com/learn/deep-learning)**","metadata":{"_uuid":"69137be1b8a1822e104c19429ae3d4a6f52bc071","_cell_guid":"55d48e9d-2a53-4aba-a687-ccfd481ce3d7"}}]}