{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"from os.path import join\n\nimage_dir = '../input/user-dogs/' #filepath ../input/dog-breed-identification/train/\nimg_paths = [join(image_dir, filename) for filename in \n                           ['stormTEST.jpg']] #joined with image director to make path to image file that is being predicted","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":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\nimage_size = 224 #size of image that model is being trained on\n\ndef read_and_prep_images(img_paths, img_height=image_size, img_width=image_size): #normalize data\n    #target_size specifies the size of the image that we want when we model with them\n    imgs = [load_img(img_path, target_size=(img_height, img_width)) for img_path in img_paths] #loads images using load_img since it can classify multiple images, using list comprehension\n    img_array = np.array([img_to_array(img) for img in imgs]) #convert each image into an array. Storing images in 3D tensor, which are stacked in an array, making them 4D\n    output = preprocess_input(img_array) #does epic math on pixels - making sure pixel values are between -1 and 1 - done for consistency\n    return(output)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"08b892c01d5c550b64cccb0d9f10bbb00c866604","scrolled":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') #specifying the model in this case, ResNet50. The file path points to where the values of the convolutional filters are\ntest_data = read_and_prep_images(img_paths) #read and pre-process model\npreds = my_model.predict(test_data) #get predictions by calling predict() on the model defined above","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fa1b3a0e157292cd90545ddd5a2bd6c9d77e4487","scrolled":false},"cell_type":"code","source":"from learntools.deep_learning.decode_predictions import decode_predictions\nfrom IPython.display import Image, display\n\n#passing in our predictions from above\nmost_likely_labels = decode_predictions(preds, top=5, class_list_path='../input/resnet50/imagenet_class_index.json') #extracts the top X highest probabilities for each photo\n\n#following prints and displays predictions and images\nfor i, img_path in enumerate(img_paths):\n    display(Image(img_path))\n    #print(\"Breed:\", most_likely_labels[i])\n    #[('id', 'breedname', 'predicted percentage'), ...]\n    #to reference highest percentage breed name: most_likely_labels[i][0][1]\n    predictions_dict = {}\n    for unformatted_tuple in most_likely_labels[i]:\n        predictions_dict[unformatted_tuple[1]] = 100 * float(unformatted_tuple[2]) #percentage in terms of 100 i.e 0.50 = 50%\n    \n    print(\"Results for \" + img_path + \": \")\n    print(\"---------- (Breed , Confidence) ----------\")\n    breeds = predictions_dict.keys()\n    for breed in breeds:\n        print(breed, \",\", str(predictions_dict[breed]) + \"%\")\n    ","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"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"}},"nbformat":4,"nbformat_minor":1}