{"cells":[{"metadata":{"_uuid":"f686b97023a5dc66bde2988df6ba82bdc8900524"},"cell_type":"markdown","source":"# Intro\n\n**This is Lesson 3 in the [Deep Learning](https://www.kaggle.com/education/machine-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":{"_kg_hide-input":true,"collapsed":true,"trusted":false,"_uuid":"4633962276bf862978ec9cb49c3a6533d7373d06"},"cell_type":"code","source":"from IPython.display import YouTubeVideo\nYouTubeVideo('sDG5tPtsbSA', width=800, height=450)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b8c9420af312890cc4ecbba0881ff01539808310"},"cell_type":"markdown","source":"# Sample Code\n\n### Choose Images to Work With"},{"metadata":{"trusted":true,"_uuid":"b37480362c0f137f74e53d5a5e298f61f1ee81ff"},"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']]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6a2c22a53e6d0da093fc0be57f76270306059c49"},"cell_type":"markdown","source":"### Function to Read and Prep Images for Modeling"},{"metadata":{"trusted":true,"_uuid":"ae06e855867297c818d6b65fa96597a5fb1cac97"},"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":{"_uuid":"9a4ec2d653906678e110bd5446dc4a7d13da702b"},"cell_type":"markdown","source":"### Create Model with Pre-Trained Weights File. Make Predictions"},{"metadata":{"trusted":true,"_uuid":"a5e6e54f17224607b2a8fd29041473235bed284b"},"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":{"_uuid":"f18959976e96bd28f173ae4e338e4914a98d8cae"},"cell_type":"markdown","source":"### Visualize Predictions"},{"metadata":{"trusted":false,"_uuid":"680e3f7fec652428a11c8723f45d8622e1530af7"},"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":{"_uuid":"127b8c1b4c105f99e3089d7086562ceb89e9930d"},"cell_type":"markdown","source":"# Exercise\nNow you are ready to **[use a powerful TensorFlow model](https://www.kaggle.com/kernels/fork/521452)** yourself.\n\n---\n**[Deep Learning Course Home Page](https://www.kaggle.com/learn/deep-learning)**\n"}],"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":1}