{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Introduction"},{"metadata":{},"cell_type":"markdown","source":"Using the Keras library to detect image similarties, I will find out what the real-world canine comparisons of canine-like Pokemon are.\n\nI'm using a pre-trained model <code>Resnet50</code> to process images of dogs and convert them into their vector form. These vectors are compared to the vector of canine-like Pokemon species and euclidean distances are computed between them. "},{"metadata":{},"cell_type":"markdown","source":"# Solution"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"from keras.applications.resnet50 import ResNet50\nfrom keras.preprocessing import image\nfrom keras.preprocessing.image import load_img\nfrom keras.applications.resnet50 import preprocess_input, decode_predictions\nfrom keras.models import Model\nimport numpy as np\nfrom os import listdir, walk\nfrom os.path import isfile, join\nimport itertools\nimport sys,requests\nfrom matplotlib import pyplot as plt\nimport pandas as pd","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Helper Functions\nHere are some helper functions that I will use in this project."},{"metadata":{"trusted":true},"cell_type":"code","source":"def getAllFilesInDirectory(directoryPath: str):\n    ''' Helper function to read all the files in the directory by Kaggle '''\n    return [(directoryPath + \"/\" + f) for f in listdir(directoryPath) if isfile(join(directoryPath, f))]\n\ndef load_image(img_path, show=False):\n    ''' Helper function to load and show images using Keras'''\n    img = image.load_img(img_path, target_size=(224, 224))\n    img_tensor = image.img_to_array(img)\n    img_tensor = np.expand_dims(img_tensor, axis=0)\n    if show:\n        plt.imshow(img_tensor[0]/255)                           \n        plt.axis('off')\n        plt.show()\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Data Processing"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Missing yamper & boltund\n\ncanid_pokemon_imgs = ['vulpix.png','ninetales.jpg','growlithe.png','arcanine.png',\n                        'eevee.png','vaporeon.png','jolteon.png','flareon.png',\n                        'espeon.png','umbreon.png','snubbull.png','granbull.png',\n                        'houndour.png','houndoom.png','raikou.png','entei.png',\n                        'suicune.png','poochyena.png','mightyena.png','electrike.png',\n                        'manectric.png','absol.png','riolu.png','lucario.png',\n                        'leafeon.png','glaceon.png','lillipup.png','herdier.png',\n                        'stoutland.png','zorua.png','zoroark.png','fennekin.png',\n                        'braixen.png','delphox.png','furfrou.png','sylveon.png',\n                        'rockruff.jpg','lycanroc-midday.jpg','zeraora.jpg']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Pull all the canine-like images from the Pokemon dataset\npokemon_dir = '../input/pokemon-images-and-types/images/images'\nall_pokemon_files = getAllFilesInDirectory(pokemon_dir)\n\ncanine_pokemon_files = []\nfor canine_file in canid_pokemon_imgs:\n    if join(pokemon_dir, canine_file) in all_pokemon_files:\n        canine_pokemon_files.append(join(pokemon_dir, canine_file))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Check files to see if it looks about right\nfor each in canine_pokemon_files:\n    print(each)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# What do these Pokemon look like?\nfor path in canine_pokemon_files:\n    load_image(path, show = True)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Output"},{"metadata":{"trusted":true},"cell_type":"code","source":"def calcSimilarity(self_vect, feature_vectors):\n    ''' Calculate Euclidian distances'''\n    similar: dict = {}\n    keys = [k for k,v in feature_vectors.items()]\n    min_dist = 10000000\n    for k,v in feature_vectors.items():\n       dist=np.linalg.norm(self_vect-v)\n       if(dist < min_dist):\n           min_dist = dist\n           similar = k\n    return similar ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def predict(img_path : str, model: Model):\n    img = image.load_img(img_path, target_size=(224, 224))\n    x = image.img_to_array(img)\n    x = np.expand_dims(x, axis=0)\n    x = preprocess_input(x)\n    return model.predict(x)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# def driver(poke_img):\n    \n#     # Predict with model\n#     feature_vectors: dict = {}\n#     model = ResNet50(weights='imagenet')\n#     for img_path in getAllFilesInDirectory(\"../input/dog-breed-identification/train\"):\n#         feature_vectors[img_path] = predict(img_path,model)[0]\n#     poke_vect = predict(poke_img,model)[0]\n#     # print (\"Computing image similarity\")\n#     result=calcSimilarity(poke_vect, feature_vectors)\n#     print (\"Your picture is most similar to : \",result)\n# #     print(self_vect)\n#     return result\n\n\ndef driver(self_img):\n    feature_vectors: dict = {}\n    model = ResNet50(weights='imagenet')\n    print (\"Reading images\")\n    for img_path in getAllFilesInDirectory(\"../input/dog-breed-identification/train\"):\n        feature_vectors[img_path] = predict(img_path,model)[0]\n    self_vect = predict(self_img,model)[0]\n    print (\"Computing image similarity\")\n    result=calcSimilarity(self_vect, feature_vectors)\n    print (\"Your picture is most similar to : \",result)\n#     print(self_vect)\n    return result","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"feature_vectors: dict = {}\nmodel = ResNet50(weights='imagenet')\nfor img_path in getAllFilesInDirectory(\"../input/dog-breed-identification/train\"):\n    feature_vectors[img_path] = predict(img_path,model)[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pokemon_vect = predict(canine_pokemon_files[0],model)[0]\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"result = driver(canine_pokemon_files[0])\nload_image(canine_pokemon_files[0], show = True)\nload_image(result, show = True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# for each_poke in canine_pokemon_files:\n#     print(each_poke)\n#     result = driver(each_poke)\n#     load_image(each_poke, show = True)\n#     load_image(result, show = True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}