{"cells": [{"metadata": {"_uuid": "d45fdc11ae5e48e49a606cded4182b7c4da4e3f1", "_cell_guid": "b070a289-f505-4247-897a-6f2013a69849"}, "cell_type": "markdown", "source": ["# Intro\n", "\n", "**This is Lesson 3 in the [Deep Learning](https://www.kaggle.com/learn/machine-learning) track**  \n", "\n", "At 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"]}, {"outputs": [], "metadata": {"_kg_hide-input": true, "_uuid": "66dc3b545857fbab1b09cd36916ea22826af9015", "collapsed": true, "_cell_guid": "164a704f-7ac6-44ca-8b4d-9614e8a0516c"}, "cell_type": "code", "source": ["from IPython.display import YouTubeVideo\n", "YouTubeVideo('sDG5tPtsbSA', width=800, height=450)"], "execution_count": null}, {"metadata": {"_uuid": "cb9e861c5f2a765c1521aab7b3b551ac066f9ec7", "_cell_guid": "c2bfc7c7-6520-4ec9-ba19-d31d219dced1"}, "cell_type": "markdown", "source": ["# Sample Code\n", "\n", "### Choose Images to Work With"]}, {"outputs": [], "metadata": {"_uuid": "5ae20575e2d003ab6ce09ff19d8cdd92204cf703", "collapsed": true, "_cell_guid": "775beea9-6b34-4aa0-ac22-493e5d2abd9b"}, "cell_type": "code", "source": ["from os.path import join\n", "\n", "image_dir = '../input/dog-breed-identification/train/'\n", "img_paths = [join(image_dir, filename) for filename in \n", "                           ['0246f44bb123ce3f91c939861eb97fb7.jpg',\n", "                            '84728e78632c0910a69d33f82e62638c.jpg',\n", "                            '8825e914555803f4c67b26593c9d5aff.jpg',\n", "                            '91a5e8db15bccfb6cfa2df5e8b95ec03.jpg']]"], "execution_count": 1}, {"metadata": {"_uuid": "1e38d823eec3719864c0db6d6600d32c373efc5a", "_cell_guid": "511df4a9-fa6c-49dd-8fbd-c0df7d5c0d36"}, "cell_type": "markdown", "source": ["### Function to Read and Prep Images for Modeling"]}, {"outputs": [], "metadata": {"_uuid": "1a5e87fe71815f96305c304f0e60d69ea6dfcce9", "collapsed": true, "_cell_guid": "3700e2d2-b1e2-4b7d-9b0e-5c70d67d97fa"}, "cell_type": "code", "source": ["import numpy as np\n", "from tensorflow.python.keras.applications.resnet50 import preprocess_input\n", "from tensorflow.python.keras.preprocessing.image import load_img, img_to_array\n", "\n", "image_size = 224\n", "\n", "def 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": 2}, {"metadata": {"_uuid": "53aacd83b9296d7fd45051b90884e08077dd6477", "_cell_guid": "32aa7325-ca97-4a54-b810-34cf11c4e8ee"}, "cell_type": "markdown", "source": ["### Create Model with Pre-Trained Weights File. Make Predictions"]}, {"outputs": [], "metadata": {"_uuid": "db5c4349ffc90e6aa4fb10655fb558da6808cc70", "collapsed": true, "_cell_guid": "2a891e71-a6fe-4241-9eb9-38d076a85aa8"}, "cell_type": "code", "source": ["from tensorflow.python.keras.applications import ResNet50\n", "\n", "my_model = ResNet50(weights='../input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels.h5')\n", "test_data = read_and_prep_images(img_paths)\n", "preds = my_model.predict(test_data)"], "execution_count": 3}, {"metadata": {"_uuid": "66738b98d9c027eae8cc97c5586a80091fe312b5", "_cell_guid": "5d2f7427-d502-4fa5-a10c-ea3f6ee71d5b"}, "cell_type": "markdown", "source": ["### Visualize Predictions"]}, {"outputs": [], "metadata": {"_uuid": "c97287027fd009c506a7d2b861ee39271505bc40", "_cell_guid": "9f8d9df8-f369-4087-b3ea-ae2bcc071fa9"}, "cell_type": "code", "source": ["import sys\n", "# Add directory holding utility functions to path to allow importing\n", "sys.path.append('/kaggle/input/python-utility-code-for-deep-learning-exercises/utils')\n", "from decode_predictions import decode_predictions\n", "\n", "from IPython.display import Image, display\n", "\n", "most_likely_labels = decode_predictions(preds, top=3, class_list_path='../input/resnet50/imagenet_class_index.json')\n", "\n", "for i, img_path in enumerate(img_paths):\n", "    display(Image(img_path))\n", "    print(most_likely_labels[i])"], "execution_count": 4}, {"metadata": {"_uuid": "69137be1b8a1822e104c19429ae3d4a6f52bc071", "_cell_guid": "55d48e9d-2a53-4aba-a687-ccfd481ce3d7"}, "cell_type": "markdown", "source": ["# Exercise\n", "Now 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\n", "After 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", "---\n", "**[Deep Learning Track Home](https://www.kaggle.com/learn/deep-learning)**"]}, {"outputs": [], "metadata": {"collapsed": true}, "cell_type": "code", "source": [], "execution_count": null}], "metadata": {"kernelspec": {"display_name": "Python 3", "language": "python", "name": "python3"}, "language_info": {"nbconvert_exporter": "python", "version": "3.6.4", "codemirror_mode": {"name": "ipython", "version": 3}, "pygments_lexer": "ipython3", "name": "python", "file_extension": ".py", "mimetype": "text/x-python"}}, "nbformat": 4, "nbformat_minor": 1}