{"cells":[{"metadata":{"_uuid":"d45fdc11ae5e48e49a606cded4182b7c4da4e3f1","_cell_guid":"b070a289-f505-4247-897a-6f2013a69849"},"cell_type":"markdown","source":"# Pre-trained Convnet"},{"metadata":{"_uuid":"1a5e87fe71815f96305c304f0e60d69ea6dfcce9","_cell_guid":"3700e2d2-b1e2-4b7d-9b0e-5c70d67d97fa","trusted":true,"collapsed":true},"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\nfrom os.path import join\n\nimage_dir = '../input/images/'\nimg_paths = [join(image_dir, filename) for filename in \n                           ['img1.jpg',\n                            'img2.jpg']]\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)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"53aacd83b9296d7fd45051b90884e08077dd6477","_cell_guid":"32aa7325-ca97-4a54-b810-34cf11c4e8ee"},"cell_type":"markdown","source":"### Create Model with Pre-Trained Weights File. Make Predictions"},{"metadata":{"_uuid":"db5c4349ffc90e6aa4fb10655fb558da6808cc70","_cell_guid":"2a891e71-a6fe-4241-9eb9-38d076a85aa8","trusted":true,"collapsed":true},"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)\nprint (test_data.shape)\npreds = my_model.predict(test_data)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"66738b98d9c027eae8cc97c5586a80091fe312b5","_cell_guid":"5d2f7427-d502-4fa5-a10c-ea3f6ee71d5b"},"cell_type":"markdown","source":"### Visualize Predictions"},{"metadata":{"_uuid":"c97287027fd009c506a7d2b861ee39271505bc40","_cell_guid":"9f8d9df8-f369-4087-b3ea-ae2bcc071fa9","trusted":true,"collapsed":true},"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])","execution_count":null,"outputs":[]}],"metadata":{"language_info":{"name":"python","version":"3.6.5","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"}},"nbformat":4,"nbformat_minor":1}