{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# -*- coding: utf-8 -*-\n\"\"\"\nCreated on Sat Feb 19 10:41:53 2022\n\n@author: mahesh\n\"\"\"\n\nimport tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import Input, Flatten, Dense, Dropout,GlobalAveragePooling2D\nfrom tensorflow.keras.applications.mobilenet import MobileNet, preprocess_input\nimport math\nfrom tensorflow.keras.preprocessing import image\nimport numpy as np\n\nTRAIN_DATA_DIR = 'D:/Study/Kaggle/dogs-vs-cats-redux-kernels-edition/train/train/'\nVALIDATION_DATA_DIR = 'D:/Study/Kaggle/dogs-vs-cats-redux-kernels-edition/train/validation/'\nTRAIN_SAMPLES = 500\nVALIDATION_SAMPLES = 500\nNUM_CLASSES = 2\nIMG_WIDTH, IMG_HEIGHT = 224, 224\nBATCH_SIZE = 64\n\ntrain_datagen = ImageDataGenerator(preprocessing_function=preprocess_input,\n                                   rotation_range=20,\n                                   width_shift_range=0.2,\n                                   height_shift_range=0.2,\n                                   zoom_range=0.2)\n\nval_datagen = ImageDataGenerator(preprocessing_function=preprocess_input)\n\ntrain_generator = train_datagen.flow_from_directory(TRAIN_DATA_DIR,\n                                                    target_size=(IMG_WIDTH, IMG_HEIGHT),\n                                                    batch_size=BATCH_SIZE,\n                                                    shuffle=True,\n                                                    seed=12345,\n                                                    class_mode='categorical')\n\nvalidation_generator = val_datagen.flow_from_directory(VALIDATION_DATA_DIR,\n                                                       target_size=(IMG_WIDTH, IMG_HEIGHT),\n                                                       batch_size=BATCH_SIZE,\n                                                       shuffle=False,\n                                                       class_mode='categorical')\n\n\ndef model_maker():\n    base_model = MobileNet(include_top=False, input_shape = (IMG_WIDTH,IMG_HEIGHT,3))\n    for layer in base_model.layers[:]:\n        layer.trainable = False # Freeze the layers\n    input = Input(shape=(IMG_WIDTH, IMG_HEIGHT, 3))\n    custom_model = base_model(input)\n    custom_model = GlobalAveragePooling2D()(custom_model)\n    custom_model = Dense(64, activation='relu')(custom_model)\n    custom_model = Dropout(0.5)(custom_model)\n    predictions = Dense(NUM_CLASSES, activation='softmax')(custom_model)\n    return Model(inputs=input, outputs=predictions)\n\nmodel = model_maker()\nmodel.compile(loss='categorical_crossentropy',optimizer= tf.optimizers.Adam(lr=0.001),metrics=['acc'])\nnum_steps = math.ceil(float(TRAIN_SAMPLES)/BATCH_SIZE)\nmodel.fit_generator(train_generator,steps_per_epoch = num_steps,epochs=10,\n                    validation_data = validation_generator,\n                    validation_steps = num_steps)\n\nmodel.save('D:/Study/Kaggle/dogs-vs-cats-redux-kernels-edition/model.h5')\n\nfrom tensorflow.keras.models import load_model\nmodel = load_model('D:/Study/Kaggle/dogs-vs-cats-redux-kernels-edition/model.h5')\n\nimg_path = r'D:\\Study\\Kaggle\\dogs-vs-cats-redux-kernels-edition\\train\\validation\\dog/dog.12423.jpg'\nimg = image.load_img(img_path, target_size=(224,224))\nimg_array = image.img_to_array(img)\nexpanded_img_array = np.expand_dims(img_array, axis=0)\npreprocessed_img = preprocess_input(expanded_img_array) # Preprocess the image\nprediction = model.predict(preprocessed_img)\nprint (\"[[CAT, DOG]]\")\nprint(prediction)\n\nimport tensorflow as tf\nimport numpy as np\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.applications.mobilenet import MobileNet, preprocess_input\nimport matplotlib\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\n\nIMG_WIDTH, IMG_HEIGHT = 224, 224\nVALIDATION_DATA_DIR = 'D:/Study/Kaggle/dogs-vs-cats-redux-kernels-edition/train/validation/'\nVALIDATION_BATCH_SIZE = 64\n\nvalidation_datagen = ImageDataGenerator(preprocessing_function=preprocess_input)\n\nvalidation_generator = validation_datagen.flow_from_directory(\n    VALIDATION_DATA_DIR,\n    target_size=(IMG_WIDTH, IMG_HEIGHT),\n    batch_size=VALIDATION_BATCH_SIZE,\n    shuffle=False,\n    class_mode='categorical')\n\nfrom tensorflow.keras.models import load_model\nfrom tensorflow.keras.utils import CustomObjectScope\nfrom tensorflow.keras.initializers import glorot_uniform\n\nwith CustomObjectScope(\n    {'GlorotUniform': glorot_uniform()}):\n    model = load_model('D:/Study/Kaggle/dogs-vs-cats-redux-kernels-edition/model.h5')\n\n# Let's view the names of the files.\nfilenames = validation_generator.filenames\nprint(len(filenames))\nprint(filenames[:10])\n\nground_truth = validation_generator.classes\nprint(ground_truth[:10])\nprint(len(ground_truth))\n\nlabel_to_index = validation_generator.class_indices\nprint(label_to_index)\n\nindex_to_label = dict((v, k) for k, v in label_to_index.items())\nprint(index_to_label)\n\npredictions = model.predict_generator(validation_generator, steps=None)\n\nprint(predictions[:10])\n\nprediction_index = []\nfor prediction in predictions:\n    prediction_index.append(np.argmax(prediction))\n\ndef accuracy(predictions, ground_truth):\n    total = 0\n    for i, j in zip(predictions, ground_truth):\n        if i == j:\n            total += 1\n    return total * 1.0 / len(predictions)\n\nprint(accuracy(prediction_index, ground_truth))\n\n\nprediction_table = {}\nfor index, val in enumerate(predictions):\n    index_of_highest_probability = np.argmax(val)\n    value_of_highest_probability = val[index_of_highest_probability]\n    prediction_table[index] = [\n        value_of_highest_probability, index_of_highest_probability,\n        ground_truth[index]\n    ]\nassert len(predictions) == len(ground_truth) == len(prediction_table)\n\n\ndef get_images_with_sorted_probabilities(prediction_table,\n                                         get_highest_probability,\n                                         label,\n                                         number_of_items,\n                                         only_false_predictions=False):\n    sorted_prediction_table = [(k, prediction_table[k])\n                               for k in sorted(prediction_table,\n                                               key=prediction_table.get,\n                                               reverse=get_highest_probability)\n                               ]\n    result = []\n    for index, key in enumerate(sorted_prediction_table):\n        image_index, [probability, predicted_index, gt] = key\n        if predicted_index == label:\n            if only_false_predictions == True:\n                if predicted_index != gt:\n                    result.append(\n                        [image_index, [probability, predicted_index, gt]])\n            else:\n                result.append(\n                    [image_index, [probability, predicted_index, gt]])\n    return result[:number_of_items]\n\n\ndef plot_images(filenames, distances, message):\n    images = []\n    for filename in filenames:\n        images.append(mpimg.imread(filename))\n    plt.figure(figsize=(20, 15))\n    columns = 5\n    for i, image in enumerate(images):\n        ax = plt.subplot(len(images) / columns + 1, columns, i + 1)\n        ax.set_title(\"\\n\\n\" + filenames[i].split(\"/\")[-1] + \"\\n\" +\n                     \"\\nProbability: \" +\n                     str(float(\"{0:.2f}\".format(distances[i]))))\n        plt.suptitle(message, fontsize=20, fontweight='bold')\n        plt.axis('off')\n        plt.imshow(image)\n        \ndef display(sorted_indices, message):\n    similar_image_paths = []\n    distances = []\n    for name, value in sorted_indices:\n        [probability, predicted_index, gt] = value\n        similar_image_paths.append(VALIDATION_DATA_DIR + filenames[name])\n        distances.append(probability)\n    plot_images(similar_image_paths, distances, message)\n\nmost_confident_dog_images = get_images_with_sorted_probabilities(prediction_table, True, 1, 10,\n                                               False)\nmessage = 'Images with highest probability of containing dogs'\ndisplay(most_confident_dog_images, message)\n\nleast_confident_dog_images = get_images_with_sorted_probabilities(prediction_table, False, 1, 10,\n                                               False)\nmessage = 'Images with lowest probability of containing dogs'\ndisplay(least_confident_dog_images, message)\n\nincorrect_dog_images = get_images_with_sorted_probabilities(prediction_table, True, 1, 10,\n                                               True)\nmessage = 'Images of cats with highest probability of containing dogs'\ndisplay(incorrect_dog_images, message)\n\nmost_confident_cat_images = get_images_with_sorted_probabilities(prediction_table, True, 0, 10,\n                                               False)\nmessage = 'Images with highest probability of containing cats'\ndisplay(most_confident_cat_images, message)\n\n\nleast_confident_cat_images = get_images_with_sorted_probabilities(prediction_table, False, 0, 10,\n                                               False)\nmessage = 'Images with lowest probability of containing cats'\ndisplay(least_confident_cat_images, message)\n\nincorrect_cat_images = get_images_with_sorted_probabilities(prediction_table, True, 0, 10,\n                                               True)\nmessage = 'Images of dogs with highest probability of containing cats'\ndisplay(incorrect_cat_images, message)\n\n\n","metadata":{},"execution_count":null,"outputs":[]}]}