{"cells":[{"metadata":{"_cell_guid":"b070a289-f505-4247-897a-6f2013a69849","_uuid":"d45fdc11ae5e48e49a606cded4182b7c4da4e3f1"},"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":{"_kg_hide-input":true,"_cell_guid":"164a704f-7ac6-44ca-8b4d-9614e8a0516c","_uuid":"66dc3b545857fbab1b09cd36916ea22826af9015","trusted":true},"cell_type":"code","source":"from IPython.display import YouTubeVideo\nYouTubeVideo('Epn3ryqr-F8', width=800, height=450)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"c2bfc7c7-6520-4ec9-ba19-d31d219dced1","_uuid":"cb9e861c5f2a765c1521aab7b3b551ac066f9ec7"},"cell_type":"markdown","source":"# Sample Code\n\n### Elija las imagenes para trabajar con ellas:"},{"metadata":{"_cell_guid":"775beea9-6b34-4aa0-ac22-493e5d2abd9b","collapsed":true,"_uuid":"5ae20575e2d003ab6ce09ff19d8cdd92204cf703","trusted":true},"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":{"_cell_guid":"511df4a9-fa6c-49dd-8fbd-c0df7d5c0d36","_uuid":"1e38d823eec3719864c0db6d6600d32c373efc5a"},"cell_type":"markdown","source":"### Función para leer y preparar imágenes para modelar"},{"metadata":{"_cell_guid":"3700e2d2-b1e2-4b7d-9b0e-5c70d67d97fa","_uuid":"1a5e87fe71815f96305c304f0e60d69ea6dfcce9","trusted":true,"collapsed":true},"cell_type":"code","source":"#Importamos la libreria numpy\nimport 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#Definimos el tamaño de la imagen\nimage_size = 224\n#Leemos la imagen\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":{"_cell_guid":"32aa7325-ca97-4a54-b810-34cf11c4e8ee","_uuid":"53aacd83b9296d7fd45051b90884e08077dd6477"},"cell_type":"markdown","source":"### Creaun modelo con un archivo de pesos entrenado previamente. Para hacer predicciones"},{"metadata":{"_cell_guid":"2a891e71-a6fe-4241-9eb9-38d076a85aa8","_uuid":"db5c4349ffc90e6aa4fb10655fb558da6808cc70","trusted":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)\npreds = my_model.predict(test_data)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"5d2f7427-d502-4fa5-a10c-ea3f6ee71d5b","_uuid":"66738b98d9c027eae8cc97c5586a80091fe312b5"},"cell_type":"markdown","source":"### Visualizacion de predicciones"},{"metadata":{"_cell_guid":"9f8d9df8-f369-4087-b3ea-ae2bcc071fa9","_uuid":"c97287027fd009c506a7d2b861ee39271505bc40","trusted":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":{"_cell_guid":"55d48e9d-2a53-4aba-a687-ccfd481ce3d7","_uuid":"69137be1b8a1822e104c19429ae3d4a6f52bc071"},"cell_type":"markdown","source":"# Ejercicio\n\nAhora estás listo para [\nusa un poderoso modelo TensorFlow](https://www.kaggle.com/dansbecker/my-first-exercise-with-tensorflow-and-keras/) tu mismo.\n\n# Continue\nDespués del ejercicio, continúe aprendiendo sobre [\nTransferir aprendizaje](https://www.kaggle.com/dansbecker/transfer-learning/).  \nEl aprendizaje de transferencia le permitirá aprovechar los modelos pre-entrenados para propósitos más allá de aquellos para los que fueron originalmente diseñados. La mayoría de las personas se sorprenden cuando experimentan por primera vez el poder de la transferencia de aprendizaje.\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":{"language_info":{"name":"python","version":"3.6.6","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}