{"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":"markdown","source":"# Computer Vision for Image Analytics\n# Image Classification using Pre-Trained models","metadata":{"papermill":{"duration":0.02438,"end_time":"2022-06-29T16:43:55.544144","exception":false,"start_time":"2022-06-29T16:43:55.519764","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"# Check the tensorflow GPU and torch GPU","metadata":{"papermill":{"duration":0.010127,"end_time":"2022-06-29T16:43:55.604003","exception":false,"start_time":"2022-06-29T16:43:55.593876","status":"completed"},"pycharm":{"name":"#%% md\n"},"tags":[]}},{"cell_type":"code","source":"import pandas as pd\nimport tensorflow as tf\nprint(len(tf.config.list_physical_devices('GPU')))\nimport torch\nprint(torch.cuda.is_available())","metadata":{"execution":{"iopub.execute_input":"2023-08-19T15:51:24.378953Z","iopub.status.busy":"2023-08-19T15:51:24.378559Z","iopub.status.idle":"2023-08-19T15:51:24.385019Z","shell.execute_reply":"2023-08-19T15:51:24.383752Z","shell.execute_reply.started":"2023-08-19T15:51:24.378921Z"},"jupyter":{"outputs_hidden":false},"papermill":{"duration":6.563542,"end_time":"2022-06-29T16:44:02.179042","exception":false,"start_time":"2022-06-29T16:43:55.615500","status":"completed"},"pycharm":{"name":"#%%\n"},"tags":[],"collapsed":false},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Import important libraries","metadata":{"papermill":{"duration":0.010527,"end_time":"2022-06-29T16:44:02.200457","exception":false,"start_time":"2022-06-29T16:44:02.189930","status":"completed"},"pycharm":{"name":"#%% md\n"},"tags":[]}},{"cell_type":"code","source":"\nimport tensorflow as tf\nfrom tensorflow.keras.layers import Dense, Conv2D, MaxPooling2D, Flatten, Activation, Dropout, BatchNormalization\nfrom tensorflow.keras.optimizers import RMSprop, Adam\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom PIL import Image\nfrom skimage import transform","metadata":{"execution":{"iopub.execute_input":"2023-08-19T15:51:25.305053Z","iopub.status.busy":"2023-08-19T15:51:25.304399Z","iopub.status.idle":"2023-08-19T15:51:25.310715Z","shell.execute_reply":"2023-08-19T15:51:25.309548Z","shell.execute_reply.started":"2023-08-19T15:51:25.305019Z"},"jupyter":{"outputs_hidden":false},"papermill":{"duration":1.628471,"end_time":"2022-06-29T16:44:03.839255","exception":false,"start_time":"2022-06-29T16:44:02.210784","status":"completed"},"pycharm":{"name":"#%%\n"},"tags":[],"collapsed":false},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"photo_size=200","metadata":{"execution":{"iopub.execute_input":"2023-08-19T15:51:26.062400Z","iopub.status.busy":"2023-08-19T15:51:26.062009Z","iopub.status.idle":"2023-08-19T15:51:26.067457Z","shell.execute_reply":"2023-08-19T15:51:26.066482Z","shell.execute_reply.started":"2023-08-19T15:51:26.062368Z"},"jupyter":{"outputs_hidden":false},"papermill":{"duration":0.018753,"end_time":"2022-06-29T16:44:03.868502","exception":false,"start_time":"2022-06-29T16:44:03.849749","status":"completed"},"pycharm":{"name":"#%%\n"},"tags":[],"collapsed":false},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prepare dataset and add some augmentation","metadata":{"papermill":{"duration":0.010235,"end_time":"2022-06-29T16:44:03.889482","exception":false,"start_time":"2022-06-29T16:44:03.879247","status":"completed"},"pycharm":{"name":"#%% md\n"},"tags":[]}},{"cell_type":"code","source":"def prepare_dataset(data_dir):\n    datagen = ImageDataGenerator(\n        rescale=1 / 255,\n        rotation_range=40,\n        width_shift_range=.2,\n        height_shift_range=.2,\n        shear_range=.1,\n        horizontal_flip=True,\n        fill_mode='nearest',\n        zoom_range=.2,\n    )\n    generator = datagen.flow_from_directory(\n        data_dir,\n        target_size=(photo_size,photo_size),\n        class_mode='binary',\n        batch_size=128,\n        # classes=[str(i) for i in range(2)]\n    )\n    return generator","metadata":{"execution":{"iopub.execute_input":"2023-08-19T15:51:27.819464Z","iopub.status.busy":"2023-08-19T15:51:27.818405Z","iopub.status.idle":"2023-08-19T15:51:27.827870Z","shell.execute_reply":"2023-08-19T15:51:27.826809Z","shell.execute_reply.started":"2023-08-19T15:51:27.819428Z"},"jupyter":{"outputs_hidden":false},"papermill":{"duration":0.01942,"end_time":"2022-06-29T16:44:03.919370","exception":false,"start_time":"2022-06-29T16:44:03.899950","status":"completed"},"pycharm":{"name":"#%%\n"},"tags":[],"collapsed":false},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\nzip_df = zipfile.ZipFile(\"/kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip\", 'r')\nzip_df.extractall(\"/kaggle/working/\")\nzip_df.close()","metadata":{"execution":{"iopub.execute_input":"2023-08-19T15:51:29.390349Z","iopub.status.busy":"2023-08-19T15:51:29.389671Z","iopub.status.idle":"2023-08-19T15:51:43.830806Z","shell.execute_reply":"2023-08-19T15:51:43.829748Z","shell.execute_reply.started":"2023-08-19T15:51:29.390313Z"},"papermill":{"duration":11.578917,"end_time":"2022-06-29T16:44:15.508517","exception":false,"start_time":"2022-06-29T16:44:03.929600","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Spliting Dogs and Cats in the training file to determine the classes","metadata":{"papermill":{"duration":0.017243,"end_time":"2022-06-29T16:44:15.544209","exception":false,"start_time":"2022-06-29T16:44:15.526966","status":"completed"},"pycharm":{"name":"#%% md\n"},"tags":[]}},{"cell_type":"code","source":"import os\nfrom shutil import copyfile,move\nfile_path = \"./train/\"\nos.mkdir(os.path.join(file_path, 'Cats'))\nos.mkdir(os.path.join(file_path, 'Dogs'))\nfor i,image_name in enumerate(os.listdir(file_path)):\n    if 'cat'in image_name :\n        move(os.path.join(file_path, image_name),os.path.join(os.path.join(file_path, 'Cats'),image_name))\n\nfor i,image_name in enumerate(os.listdir(file_path)):\n    if 'dog'in image_name :\n        move(os.path.join(file_path, image_name),os.path.join(os.path.join(file_path, 'Dogs'),image_name))\n","metadata":{"execution":{"iopub.execute_input":"2023-08-19T15:51:43.834751Z","iopub.status.busy":"2023-08-19T15:51:43.832653Z","iopub.status.idle":"2023-08-19T15:51:44.710125Z","shell.execute_reply":"2023-08-19T15:51:44.707321Z","shell.execute_reply.started":"2023-08-19T15:51:43.834723Z"},"jupyter":{"outputs_hidden":false},"papermill":{"duration":0.910503,"end_time":"2022-06-29T16:44:16.471080","exception":false,"start_time":"2022-06-29T16:44:15.560577","status":"completed"},"pycharm":{"name":"#%%\n"},"tags":[],"collapsed":false},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create validation directory","metadata":{"papermill":{"duration":0.010566,"end_time":"2022-06-29T16:44:16.492258","exception":false,"start_time":"2022-06-29T16:44:16.481692","status":"completed"},"tags":[]}},{"cell_type":"code","source":"os.mkdir(os.path.join('./', 'validation'))","metadata":{"execution":{"iopub.execute_input":"2023-08-19T15:51:44.713895Z","iopub.status.busy":"2023-08-19T15:51:44.713593Z","iopub.status.idle":"2023-08-19T15:51:44.721006Z","shell.execute_reply":"2023-08-19T15:51:44.720160Z","shell.execute_reply.started":"2023-08-19T15:51:44.713864Z"},"papermill":{"duration":0.017821,"end_time":"2022-06-29T16:44:16.521348","exception":false,"start_time":"2022-06-29T16:44:16.503527","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n# Split training ,validation and  testing Dataset","metadata":{"papermill":{"duration":0.010525,"end_time":"2022-06-29T16:44:16.542254","exception":false,"start_time":"2022-06-29T16:44:16.531729","status":"completed"},"pycharm":{"name":"#%% md\n"},"tags":[]}},{"cell_type":"code","source":"import shutil\nimport os\nimport numpy as np\nimport argparse\n\ndef get_files_from_folder(path):\n    files = os.listdir(path)\n    return np.asarray(files)\n\ndef image_train_test_split(path_to_data, path_to_test_data, train_ratio):\n    # get dirs\n    _, dirs, _ = next(os.walk(path_to_data))\n\n    # calculates how many train data per class\n    data_counter_per_class = np.zeros((len(dirs)))\n    for i in range(len(dirs)):\n        path = os.path.join(path_to_data, dirs[i])\n        files = get_files_from_folder(path)\n        data_counter_per_class[i] = len(files)\n    test_counter = np.round(data_counter_per_class * (1 - train_ratio))\n\n    # transfers files\n    for i in range(len(dirs)):\n        path_to_original = os.path.join(path_to_data, dirs[i])\n        path_to_save = os.path.join(path_to_test_data, dirs[i])\n\n        #creates dir\n        if not os.path.exists(path_to_save):\n            os.makedirs(path_to_save)\n        files = get_files_from_folder(path_to_original)\n        # moves data\n        for j in range(int(test_counter[i])):\n            dst = os.path.join(path_to_save, files[j])\n            src = os.path.join(path_to_original, files[j])\n            shutil.move(src, dst)\n\n","metadata":{"execution":{"iopub.execute_input":"2023-08-19T15:51:44.723948Z","iopub.status.busy":"2023-08-19T15:51:44.723586Z","iopub.status.idle":"2023-08-19T15:51:44.745350Z","shell.execute_reply":"2023-08-19T15:51:44.744367Z","shell.execute_reply.started":"2023-08-19T15:51:44.723913Z"},"jupyter":{"outputs_hidden":false},"papermill":{"duration":0.024925,"end_time":"2022-06-29T16:44:16.577735","exception":false,"start_time":"2022-06-29T16:44:16.552810","status":"completed"},"pycharm":{"name":"#%%\n"},"tags":[],"collapsed":false},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_train_test_split('./train/', './validation/', float(0.9))","metadata":{"execution":{"iopub.execute_input":"2023-08-19T15:51:44.747915Z","iopub.status.busy":"2023-08-19T15:51:44.747012Z","iopub.status.idle":"2023-08-19T15:51:44.884719Z","shell.execute_reply":"2023-08-19T15:51:44.883707Z","shell.execute_reply.started":"2023-08-19T15:51:44.747881Z"},"jupyter":{"outputs_hidden":false},"papermill":{"duration":0.122939,"end_time":"2022-06-29T16:44:16.711098","exception":false,"start_time":"2022-06-29T16:44:16.588159","status":"completed"},"pycharm":{"name":"#%%\n"},"tags":[],"collapsed":false},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Dataset from directory","metadata":{"papermill":{"duration":0.01033,"end_time":"2022-06-29T16:44:16.732076","exception":false,"start_time":"2022-06-29T16:44:16.721746","status":"completed"},"pycharm":{"name":"#%% md\n"},"tags":[]}},{"cell_type":"code","source":"train_data=prepare_dataset('./train/')\nvalidation_data = prepare_dataset('./validation/')\n\n","metadata":{"execution":{"iopub.execute_input":"2023-08-19T15:51:44.886820Z","iopub.status.busy":"2023-08-19T15:51:44.886449Z","iopub.status.idle":"2023-08-19T15:51:46.059560Z","shell.execute_reply":"2023-08-19T15:51:46.058621Z","shell.execute_reply.started":"2023-08-19T15:51:44.886787Z"},"jupyter":{"outputs_hidden":false},"papermill":{"duration":1.221435,"end_time":"2022-06-29T16:44:17.963957","exception":false,"start_time":"2022-06-29T16:44:16.742522","status":"completed"},"pycharm":{"name":"#%%\n"},"tags":[],"collapsed":false},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Unzip test data","metadata":{"papermill":{"duration":0.899539,"end_time":"2022-06-29T16:44:18.876629","exception":false,"start_time":"2022-06-29T16:44:17.977090","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import zipfile\nzip_df = zipfile.ZipFile(\"/kaggle/input/dogs-vs-cats-redux-kernels-edition/test.zip\", 'r')\nzip_df.extractall(\"/kaggle/working/\")\nzip_df.close()","metadata":{"execution":{"iopub.execute_input":"2023-08-19T15:51:46.061084Z","iopub.status.busy":"2023-08-19T15:51:46.060754Z","iopub.status.idle":"2023-08-19T15:51:55.842370Z","shell.execute_reply":"2023-08-19T15:51:55.841347Z","shell.execute_reply.started":"2023-08-19T15:51:46.061051Z"},"papermill":{"duration":5.957561,"end_time":"2022-06-29T16:44:24.846566","exception":false,"start_time":"2022-06-29T16:44:18.889005","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation_data.class_indices","metadata":{"execution":{"iopub.execute_input":"2023-08-19T15:51:55.844077Z","iopub.status.busy":"2023-08-19T15:51:55.843720Z","iopub.status.idle":"2023-08-19T15:51:55.851895Z","shell.execute_reply":"2023-08-19T15:51:55.850945Z","shell.execute_reply.started":"2023-08-19T15:51:55.844045Z"},"jupyter":{"outputs_hidden":false},"papermill":{"duration":0.022431,"end_time":"2022-06-29T16:44:24.879724","exception":false,"start_time":"2022-06-29T16:44:24.857293","status":"completed"},"pycharm":{"name":"#%%\n"},"tags":[],"collapsed":false},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load pre-trained models","metadata":{"papermill":{"duration":0.010322,"end_time":"2022-06-29T16:44:24.901117","exception":false,"start_time":"2022-06-29T16:44:24.890795","status":"completed"},"pycharm":{"name":"#%% md\n"},"tags":[]}},{"cell_type":"markdown","source":"## Basic Vgg model","metadata":{"papermill":{"duration":0.010976,"end_time":"2022-06-29T16:44:24.922564","exception":false,"start_time":"2022-06-29T16:44:24.911588","status":"completed"},"pycharm":{"name":"#%% md\n"},"tags":[]}},{"cell_type":"code","source":"# example of tending the vgg16 model\ndef create_vgg_model():\n    from keras.applications.vgg16 import VGG16\n    from keras.models import Model\n    from keras.layers import Dense\n    from keras.layers import Flatten\n    # load model without classifier layers\n    model = VGG16(include_top=False, input_shape=(photo_size, photo_size, 3))\n    for layer_idx in range(len(model.layers)):\n        if layer_idx not in [1,2,3,15,16,17,18]:\n            model.layers[layer_idx].trainable = False\n    # add new classifier layers\n    flat1 = Flatten()(model.layers[-1].output)\n    class1 = Dense(128, activation='relu')(flat1)\n    # class1 = Dense(256, activation='relu')(class1)\n    output = Dense(1, activation='sigmoid')(class1)\n    # define new model\n    model = Model(inputs=model.inputs, outputs=output)\n    return model","metadata":{"execution":{"iopub.execute_input":"2023-08-19T04:17:37.373895Z","iopub.status.busy":"2023-08-19T04:17:37.373168Z","iopub.status.idle":"2023-08-19T04:17:37.381488Z","shell.execute_reply":"2023-08-19T04:17:37.380289Z","shell.execute_reply.started":"2023-08-19T04:17:37.373860Z"},"jupyter":{"outputs_hidden":false},"papermill":{"duration":0.021471,"end_time":"2022-06-29T16:44:24.954512","exception":false,"start_time":"2022-06-29T16:44:24.933041","status":"completed"},"pycharm":{"name":"#%%\n"},"tags":[],"collapsed":false},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vgg_model=create_vgg_model()\nvgg_model.summary()","metadata":{"execution":{"iopub.execute_input":"2023-08-19T04:17:39.413648Z","iopub.status.busy":"2023-08-19T04:17:39.413276Z","iopub.status.idle":"2023-08-19T04:17:45.823322Z","shell.execute_reply":"2023-08-19T04:17:45.822526Z","shell.execute_reply.started":"2023-08-19T04:17:39.413616Z"},"jupyter":{"outputs_hidden":false},"papermill":{"duration":6.119256,"end_time":"2022-06-29T16:44:31.084412","exception":false,"start_time":"2022-06-29T16:44:24.965156","status":"completed"},"pycharm":{"name":"#%%\n"},"scrolled":true,"tags":[],"collapsed":false},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vgg_model.compile(optimizer=Adam(learning_rate=0.001), loss='binary_crossentropy', metrics=['accuracy'])\nvgg_model.fit(\n    train_data,\n    epochs=5,\n    validation_data=validation_data\n)\nvgg_model.save(\"Dogs_vs_cats_vgg_model30.h5\")","metadata":{"execution":{"iopub.execute_input":"2023-08-19T04:18:11.195031Z","iopub.status.busy":"2023-08-19T04:18:11.194645Z","iopub.status.idle":"2023-08-19T04:44:16.198030Z","shell.execute_reply":"2023-08-19T04:44:16.196978Z","shell.execute_reply.started":"2023-08-19T04:18:11.195001Z"},"jupyter":{"outputs_hidden":false},"papermill":{"duration":385.20379,"end_time":"2022-06-29T16:50:56.301020","exception":false,"start_time":"2022-06-29T16:44:31.097230","status":"completed"},"pycharm":{"name":"#%%\n"},"tags":[],"collapsed":false},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# VGG After Fine Tuning","metadata":{}},{"cell_type":"code","source":"from keras.applications.vgg16 import VGG16\nfrom keras.models import Model\nfrom keras.layers import Dense, Flatten, Dropout\nfrom keras.optimizers import Adam\nfrom keras.preprocessing.image import ImageDataGenerator\n\ndef create_fine_tuned_vgg_model(photo_size, learning_rate):\n    base_model = VGG16(include_top=False, input_shape=(photo_size, photo_size, 3))\n    for layer in base_model.layers:\n        layer.trainable = False\n\n    x = Flatten()(base_model.output)\n    x = Dense(128, activation='relu')(x)\n    x = Dropout(0.5)(x)  # Adding dropout for regularization\n    output = Dense(1, activation='sigmoid')(x)\n\n    model = Model(inputs=base_model.input, outputs=output)\n\n    model.compile(optimizer=Adam(learning_rate),\n                  loss='binary_crossentropy',\n                  metrics=['accuracy'])\n\n    return model\n\n# Data augmentation parameters\ntrain_datagen = ImageDataGenerator(\n    rescale=1.0 / 255,\n    rotation_range=20,\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    fill_mode='nearest'\n)\n\nvalidation_datagen = ImageDataGenerator(rescale=1.0 / 255)\n\n# Set up data generators\ntrain_generator = train_datagen.flow_from_directory(\n    './train/',  # Specify your training data directory\n    target_size=(224, 224),\n    batch_size=32,\n    class_mode='binary'\n)\n\nvalidation_generator = validation_datagen.flow_from_directory(\n    './validation/',  # Specify your validation data directory\n    target_size=(224, 224),\n    batch_size=32,\n    class_mode='binary'\n)\n\nmodel = create_fine_tuned_vgg_model(photo_size=224, learning_rate=0.001)\n\nmodel.fit(train_generator, epochs=5, validation_data=validation_generator)\nmodel.save(\"Dogs_vs_cats_VGG_mode.h5\")","metadata":{"execution":{"iopub.status.idle":"2023-08-19T09:03:31.065984Z","shell.execute_reply":"2023-08-19T09:03:31.064852Z","shell.execute_reply.started":"2023-08-19T08:34:05.592049Z"}},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Basic ResNet50","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.applications import ResNet50\n#from keras.applications.resnet50 import ResNet50\nfrom keras.models import Model\nfrom keras.layers import Dense, Flatten\n\n# Create a ResNet50 model with modified classifier layers\ndef create_resnet_model():\n    # Load ResNet50 model without classifier layers\n    model = ResNet50(weights='imagenet', include_top=False, input_shape=(photo_size, photo_size, 3))\n\n    \n    # Freeze layers except for specific ones\n    for layer_idx in range(len(model.layers)):\n        if layer_idx not in [81, 82, 83, 91, 92, 93, 94]:  # Choose the appropriate layers\n            model.layers[layer_idx].trainable = False\n    \n    # Add new classifier layers\n    flat1 = Flatten()(model.layers[-1].output)\n    class1 = Dense(128, activation='relu')(flat1)\n    # class1 = Dense(256, activation='relu')(class1)\n    output = Dense(1, activation='sigmoid')(class1)\n    \n    # Define the new model\n    model = Model(inputs=model.inputs, outputs=output)\n    return model","metadata":{"execution":{"iopub.execute_input":"2023-08-19T09:05:57.710413Z","iopub.status.busy":"2023-08-19T09:05:57.710000Z","iopub.status.idle":"2023-08-19T09:05:57.720727Z","shell.execute_reply":"2023-08-19T09:05:57.719499Z","shell.execute_reply.started":"2023-08-19T09:05:57.710381Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resnet_model = create_resnet_model()\nresnet_model.summary()","metadata":{"execution":{"iopub.execute_input":"2023-08-19T09:05:58.460329Z","iopub.status.busy":"2023-08-19T09:05:58.459521Z","iopub.status.idle":"2023-08-19T09:06:01.133330Z","shell.execute_reply":"2023-08-19T09:06:01.132492Z","shell.execute_reply.started":"2023-08-19T09:05:58.460258Z"},"scrolled":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resnet_model.compile(optimizer=Adam(learning_rate=0.001), loss='binary_crossentropy', metrics=['accuracy'])\nresnet_model.fit(\n    train_data,\n    epochs=5,\n    validation_data=validation_data\n)\nresnet_model.save(\"Dogs_vs_cats_resnet_model31.h5\")","metadata":{"execution":{"iopub.execute_input":"2023-08-19T09:06:01.135438Z","iopub.status.busy":"2023-08-19T09:06:01.134871Z","iopub.status.idle":"2023-08-19T09:32:22.468728Z","shell.execute_reply":"2023-08-19T09:32:22.467723Z","shell.execute_reply.started":"2023-08-19T09:06:01.135402Z"}},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ResNet50 After Fine Tuning","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.applications import ResNet50\n#from keras.applications.resnet50 import ResNet50\nfrom keras.models import Model\nfrom keras.layers import Dense, GlobalAveragePooling2D\nfrom keras.optimizers import Adam\nfrom keras.preprocessing.image import ImageDataGenerator\n\ndef create_fine_tuned_resnet_model(photo_size, learning_rate):\n    base_model = ResNet50(include_top=False, input_shape=(photo_size, photo_size, 3))\n    for layer in base_model.layers:\n        layer.trainable = False\n\n    x = GlobalAveragePooling2D()(base_model.output)\n    x = Dense(128, activation='relu')(x)\n    x = Dropout(0.5)(x)  # Adding dropout for regularization\n    output = Dense(1, activation='sigmoid')(x)\n\n    model = Model(inputs=base_model.input, outputs=output)\n\n    model.compile(optimizer=Adam(learning_rate),\n                  loss='binary_crossentropy',\n                  metrics=['accuracy'])\n\n    return model\n\n# Data augmentation parameters\ntrain_datagen = ImageDataGenerator(\n    rescale=1.0 / 255,\n    rotation_range=20,\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    fill_mode='nearest'\n)\n\nvalidation_datagen = ImageDataGenerator(rescale=1.0 / 255)\n\n# Set up data generators\ntrain_generator = train_datagen.flow_from_directory(\n    './train/',  # Specify your training data directory\n    target_size=(224, 224),\n    batch_size=32,\n    class_mode='binary'\n)\n\nvalidation_generator = validation_datagen.flow_from_directory(\n    './validation/',  # Specify your validation data directory\n    target_size=(224, 224),\n    batch_size=32,\n    class_mode='binary'\n)\n\nmodel = create_fine_tuned_resnet_model(photo_size=224, learning_rate=0.001)\n\nmodel.fit(train_generator, epochs=5, validation_data=validation_generator)\nmodel.save(\"Dogs_vs_cats_renet_mode.h5\")","metadata":{"execution":{"iopub.execute_input":"2023-08-19T10:07:07.468442Z","iopub.status.busy":"2023-08-19T10:07:07.467716Z","iopub.status.idle":"2023-08-19T10:36:27.783487Z","shell.execute_reply":"2023-08-19T10:36:27.782427Z","shell.execute_reply.started":"2023-08-19T10:07:07.468404Z"}},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Basic AlexNet","metadata":{}},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Conv2D, MaxPooling2D, Flatten, Dense\n\nnum_classes = 2  # Number of classes for binary classification\n\ndef create_alexnet_model(include_top=False, input_shape=(photo_size, photo_size, 3)):\n    model = Sequential([\n        Conv2D(96, (11, 11), strides=(4, 4), activation='relu', input_shape=input_shape),\n        MaxPooling2D((3, 3), strides=(2, 2)),\n        Conv2D(256, (5, 5), activation='relu'),\n        MaxPooling2D((3, 3), strides=(2, 2)),\n        Conv2D(384, (3, 3), activation='relu'),\n        Conv2D(384, (3, 3), activation='relu'),\n        Conv2D(256, (3, 3), activation='relu'),\n        MaxPooling2D((3, 3), strides=(2, 2)),\n        Flatten(),\n        Dense(4096, activation='relu'),\n        Dense(4096, activation='relu'),\n        Dense(num_classes, activation='softmax')\n    ])\n    return model","metadata":{"execution":{"iopub.execute_input":"2023-08-19T10:36:27.785958Z","iopub.status.busy":"2023-08-19T10:36:27.785003Z","iopub.status.idle":"2023-08-19T10:36:27.796193Z","shell.execute_reply":"2023-08-19T10:36:27.795071Z","shell.execute_reply.started":"2023-08-19T10:36:27.785921Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"alexnet_model = create_alexnet_model()\nalexnet_model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.execute_input":"2023-08-19T10:36:27.799337Z","iopub.status.busy":"2023-08-19T10:36:27.798962Z","iopub.status.idle":"2023-08-19T10:36:27.944792Z","shell.execute_reply":"2023-08-19T10:36:27.943790Z","shell.execute_reply.started":"2023-08-19T10:36:27.799303Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"alexnet_model.fit(\n    train_data,  # Your training data here\n    epochs=5,\n    validation_data=validation_data,\n    steps_per_epoch=5\n)","metadata":{"execution":{"iopub.execute_input":"2023-08-19T10:36:27.946426Z","iopub.status.busy":"2023-08-19T10:36:27.946056Z","iopub.status.idle":"2023-08-19T10:39:51.475205Z","shell.execute_reply":"2023-08-19T10:39:51.474221Z","shell.execute_reply.started":"2023-08-19T10:36:27.946393Z"}},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# AlexNet after fine tuning","metadata":{}},{"cell_type":"markdown","source":"**Please note that Keras doesn't provide a pre-built AlexNet model like it does for VGG16 or ResNet. You'll need to implement AlexNet architecture manually using Keras layers. Here's an example code using the AlexNet architecture and data augmentation:**","metadata":{}},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout\nfrom keras.optimizers import Adam\nfrom keras.preprocessing.image import ImageDataGenerator\n\ndef create_alexnet_model(photo_size, learning_rate):\n    model = Sequential()\n\n    # Convolutional and Pooling Layers\n    model.add(Conv2D(96, (11, 11), strides=(4, 4), activation='relu', input_shape=(photo_size, photo_size, 3)))\n    model.add(MaxPooling2D((3, 3), strides=(2, 2)))\n    model.add(Conv2D(256, (5, 5), padding='same', activation='relu'))\n    model.add(MaxPooling2D((3, 3), strides=(2, 2)))\n    model.add(Conv2D(384, (3, 3), padding='same', activation='relu'))\n    model.add(Conv2D(384, (3, 3), padding='same', activation='relu'))\n    model.add(Conv2D(256, (3, 3), padding='same', activation='relu'))\n    model.add(MaxPooling2D((3, 3), strides=(2, 2)))\n\n    # Fully Connected Layers\n    model.add(Flatten())\n    model.add(Dense(4096, activation='relu'))\n    model.add(Dropout(0.5))\n    model.add(Dense(4096, activation='relu'))\n    model.add(Dropout(0.5))\n    model.add(Dense(1, activation='sigmoid'))\n\n    model.compile(optimizer=Adam(learning_rate),\n                  loss='binary_crossentropy',\n                  metrics=['accuracy'])\n\n    return model\n\n# Data augmentation parameters\ntrain_datagen = ImageDataGenerator(\n    rescale=1.0 / 255,\n    rotation_range=20,\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    fill_mode='nearest'\n)\n\nvalidation_datagen = ImageDataGenerator(rescale=1.0 / 255)\n\n# Set up data generators\ntrain_generator = train_datagen.flow_from_directory(\n    './train/',  # Specify your training data directory\n    target_size=(227, 227),\n    batch_size=32,\n    class_mode='binary'\n)\n\nvalidation_generator = validation_datagen.flow_from_directory(\n    './validation/',  # Specify your validation data directory\n    target_size=(227, 227),\n    batch_size=32,\n    class_mode='binary'\n)\n\nmodel = create_alexnet_model(photo_size=227, learning_rate=0.001)\n\nmodel.fit(train_generator, epochs=5, validation_data=validation_generator)\nmodel.save(\"Dogs_vs_cats_Alexnet_mode.h5\")","metadata":{"execution":{"iopub.execute_input":"2023-08-19T10:39:51.477271Z","iopub.status.busy":"2023-08-19T10:39:51.476885Z","iopub.status.idle":"2023-08-19T11:09:51.226758Z","shell.execute_reply":"2023-08-19T11:09:51.225392Z","shell.execute_reply.started":"2023-08-19T10:39:51.477237Z"}},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Basic Inception model","metadata":{"papermill":{"duration":0.021386,"end_time":"2022-06-29T16:50:56.344820","exception":false,"start_time":"2022-06-29T16:50:56.323434","status":"completed"},"pycharm":{"name":"#%% md\n"},"tags":[]}},{"cell_type":"code","source":"def create_inception_model():\n    from tensorflow.keras.applications.inception_v3 import InceptionV3\n    base_model = InceptionV3(input_shape = (photo_size, photo_size, 3), include_top = False, weights = 'imagenet')\n    for layer in base_model.layers:\n        layer.trainable = False\n    # for layer_idx in range(len(pretrained_model.layers)):\n    #     if layer_idx not in [1,2,3,305,306,307,308,309,310]:\n    #         pretrained_model.layers[layer_idx].trainable = False\n    from tensorflow.keras.optimizers import RMSprop\n    from tensorflow.keras import layers\n    x = layers.Flatten()(base_model.output)\n    x = layers.Dense(512, activation='relu')(x)\n    x = layers.Dropout(0.2)(x)\n    x = layers.Dense(256, activation='relu')(x)\n    # Add a final sigmoid layer with 1 node for classification output\n    x = layers.Dense(1, activation='sigmoid')(x)\n    model = tf.keras.models.Model(base_model.input, x)\n    model.compile(optimizer = RMSprop(learning_rate=0.0001), loss = 'binary_crossentropy', metrics = ['accuracy'])\n    return model","metadata":{"_kg_hide-input":true,"execution":{"iopub.execute_input":"2023-08-19T07:26:42.047879Z","iopub.status.busy":"2023-08-19T07:26:42.046876Z","iopub.status.idle":"2023-08-19T07:26:42.056608Z","shell.execute_reply":"2023-08-19T07:26:42.055490Z","shell.execute_reply.started":"2023-08-19T07:26:42.047843Z"},"jupyter":{"outputs_hidden":false},"papermill":{"duration":0.032865,"end_time":"2022-06-29T16:50:56.399142","exception":false,"start_time":"2022-06-29T16:50:56.366277","status":"completed"},"pycharm":{"name":"#%%\n"},"tags":[],"collapsed":false},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inception_model=create_inception_model()\ninception_model.summary()","metadata":{"execution":{"iopub.execute_input":"2023-08-19T07:26:42.457445Z","iopub.status.busy":"2023-08-19T07:26:42.457070Z","iopub.status.idle":"2023-08-19T07:26:51.803836Z","shell.execute_reply":"2023-08-19T07:26:51.796599Z","shell.execute_reply.started":"2023-08-19T07:26:42.457416Z"},"jupyter":{"outputs_hidden":false},"papermill":{"duration":2.344158,"end_time":"2022-06-29T16:50:58.765588","exception":false,"start_time":"2022-06-29T16:50:56.421430","status":"completed"},"pycharm":{"name":"#%%\n"},"scrolled":true,"tags":[],"collapsed":false},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inception_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\ninception_model.fit(\n    train_data,\n    epochs=5,\n    validation_data=validation_data,steps_per_epoch=5\n)\ninception_model.save(\"Dogs_vs_cats_inception_model_1_5step.h5\")","metadata":{"execution":{"iopub.execute_input":"2023-08-19T07:26:51.806390Z","iopub.status.busy":"2023-08-19T07:26:51.806011Z","iopub.status.idle":"2023-08-19T07:30:59.493633Z","shell.execute_reply":"2023-08-19T07:30:59.492577Z","shell.execute_reply.started":"2023-08-19T07:26:51.806354Z"},"jupyter":{"outputs_hidden":false},"papermill":{"duration":46.655609,"end_time":"2022-06-29T16:51:45.449785","exception":false,"start_time":"2022-06-29T16:50:58.794176","status":"completed"},"pycharm":{"name":"#%%\n"},"tags":[],"collapsed":false},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Inception_V3 After Fine tuning","metadata":{}},{"cell_type":"code","source":"from keras.applications.inception_v3 import InceptionV3\nfrom keras.models import Model\nfrom keras.layers import Dense, GlobalAveragePooling2D\nfrom keras.optimizers import Adam\nfrom keras.preprocessing.image import ImageDataGenerator\n\ndef create_fine_tuned_inception_model(photo_size, learning_rate):\n    base_model = InceptionV3(include_top=False, input_shape=(photo_size, photo_size, 3))\n    for layer in base_model.layers:\n        layer.trainable = False\n\n    x = GlobalAveragePooling2D()(base_model.output)\n    x = Dense(128, activation='relu')(x)\n    x = Dropout(0.5)(x)  # Adding dropout for regularization\n    output = Dense(1, activation='sigmoid')(x)\n\n    model = Model(inputs=base_model.input, outputs=output)\n\n    model.compile(optimizer=Adam(learning_rate),\n                  loss='binary_crossentropy',\n                  metrics=['accuracy'])\n\n    return model\n\n# Data augmentation parameters\ntrain_datagen = ImageDataGenerator(\n    rescale=1.0 / 255,\n    rotation_range=20,\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    fill_mode='nearest'\n)\n\nvalidation_datagen = ImageDataGenerator(rescale=1.0 / 255)\n\n# Set up data generators\ntrain_generator = train_datagen.flow_from_directory(\n    './train/',  # Specify your training data directory\n    target_size=(299, 299),\n    batch_size=32,\n    class_mode='binary'\n)\n\nvalidation_generator = validation_datagen.flow_from_directory(\n    './validation/',  # Specify your validation data directory\n    target_size=(299, 299),\n    batch_size=32,\n    class_mode='binary'\n)\n\nmodel = create_fine_tuned_inception_model(photo_size=299, learning_rate=0.001)\n\nmodel.fit(train_generator, epochs=5, validation_data=validation_generator)\nmodel.save(\"Dogs_vs_cats_inception_mode.h5\")","metadata":{"execution":{"iopub.execute_input":"2023-08-19T07:30:59.495941Z","iopub.status.busy":"2023-08-19T07:30:59.495231Z","iopub.status.idle":"2023-08-19T08:19:59.018901Z","shell.execute_reply":"2023-08-19T08:19:59.017914Z","shell.execute_reply.started":"2023-08-19T07:30:59.495905Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install efficientnet","metadata":{"execution":{"iopub.execute_input":"2023-08-19T11:13:38.878173Z","iopub.status.busy":"2023-08-19T11:13:38.877649Z","iopub.status.idle":"2023-08-19T11:13:51.947122Z","shell.execute_reply":"2023-08-19T11:13:51.945881Z","shell.execute_reply.started":"2023-08-19T11:13:38.878126Z"},"papermill":{"duration":11.991414,"end_time":"2022-06-29T16:51:57.466551","exception":false,"start_time":"2022-06-29T16:51:45.475137","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Basic Efficient net","metadata":{"papermill":{"duration":0.024671,"end_time":"2022-06-29T16:51:57.516463","exception":false,"start_time":"2022-06-29T16:51:57.491792","status":"completed"},"pycharm":{"name":"#%% md\n"},"tags":[]}},{"cell_type":"code","source":"def use_efficient_net(model_type='B0'):\n    from tensorflow.keras.optimizers import RMSprop\n    from tensorflow.keras.models import Model\n    from efficientnet.tfkeras import EfficientNetB0,EfficientNetB7\n    if model_type=='B0':\n        efn_model = EfficientNetB0(input_shape = (photo_size, photo_size, 3), include_top = False, weights = 'imagenet')\n    else:\n        efn_model = EfficientNetB7(input_shape = (photo_size, photo_size, 3), include_top = False, weights = 'imagenet')\n    for layer in efn_model.layers:\n        layer.trainable = False\n    #\n    x = efn_model.output\n    x = Flatten()(x)\n    x = Dense(256, activation=\"relu\")(x)\n    x = Dropout(0.5)(x)\n    x = Dense(256, activation=\"relu\")(x)\n    # Add a final sigmoid layer with 1 node for classification output\n    predictions = Dense(1, activation=\"sigmoid\")(x)\n    efficient_net = Model(efn_model.input,predictions)\n\n    efficient_net.compile(RMSprop(learning_rate=0.0001, rho=0.9),loss='binary_crossentropy',metrics=['accuracy'])\n    return efficient_net\nefficient_net=use_efficient_net('B0')\nefficient_net.summary()","metadata":{"execution":{"iopub.execute_input":"2023-08-19T11:13:51.951162Z","iopub.status.busy":"2023-08-19T11:13:51.950818Z","iopub.status.idle":"2023-08-19T11:13:55.326220Z","shell.execute_reply":"2023-08-19T11:13:55.325325Z","shell.execute_reply.started":"2023-08-19T11:13:51.951133Z"},"papermill":{"duration":2.671452,"end_time":"2022-06-29T16:52:00.212815","exception":false,"start_time":"2022-06-29T16:51:57.541363","status":"completed"},"pycharm":{"name":"#%%\n"},"scrolled":true,"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"efficient_net.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\neff_history = efficient_net.fit(train_data, validation_data = validation_data, epochs = 5,steps_per_epoch=3)\nefficient_net.save(\"Dog_vs_cats_efficient_netB0_1_3step.h5\")","metadata":{"execution":{"iopub.execute_input":"2023-08-19T11:13:55.327870Z","iopub.status.busy":"2023-08-19T11:13:55.327484Z","iopub.status.idle":"2023-08-19T11:18:04.158392Z","shell.execute_reply":"2023-08-19T11:18:04.157356Z","shell.execute_reply.started":"2023-08-19T11:13:55.327832Z"},"jupyter":{"outputs_hidden":false},"papermill":{"duration":43.17914,"end_time":"2022-06-29T16:52:43.423211","exception":false,"start_time":"2022-06-29T16:52:00.244071","status":"completed"},"pycharm":{"name":"#%%\n"},"tags":[],"collapsed":false},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Efficient Net after Fine tuning","metadata":{}},{"cell_type":"code","source":"from keras.applications.efficientnet import EfficientNetB0\nfrom keras.models import Model\nfrom keras.layers import Dense, GlobalAveragePooling2D\nfrom keras.optimizers import Adam\nfrom keras.preprocessing.image import ImageDataGenerator\n\ndef create_fine_tuned_efficientnet_model(photo_size, learning_rate):\n    base_model = EfficientNetB0(include_top=False, input_shape=(photo_size, photo_size, 3))\n    for layer in base_model.layers:\n        layer.trainable = False\n\n    x = GlobalAveragePooling2D()(base_model.output)\n    x = Dense(128, activation='relu')(x)\n    x = Dropout(0.5)(x)  # Adding dropout for regularization\n    output = Dense(1, activation='sigmoid')(x)\n\n    model = Model(inputs=base_model.input, outputs=output)\n\n    model.compile(optimizer=Adam(learning_rate),\n                  loss='binary_crossentropy',\n                  metrics=['accuracy'])\n\n    return model\n\n# Data augmentation parameters\ntrain_datagen = ImageDataGenerator(\n    rescale=1.0 / 255,\n    rotation_range=20,\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    fill_mode='nearest'\n)\n\nvalidation_datagen = ImageDataGenerator(rescale=1.0 / 255)\n\n# Set up data generators\ntrain_generator = train_datagen.flow_from_directory(\n    './train/',  # Specify your training data directory\n    target_size=(224, 224),\n    batch_size=32,\n    class_mode='binary'\n)\n\nvalidation_generator = validation_datagen.flow_from_directory(\n    './validation/',  # Speci./validation/fy your validation data directory\n    target_size=(224, 224),\n    batch_size=32,\n    class_mode='binary'\n)\n\nmodel = create_fine_tuned_efficientnet_model(photo_size=224, learning_rate=0.001)\n\nmodel.fit(train_generator, epochs=5, validation_data=validation_generator)","metadata":{"execution":{"iopub.execute_input":"2023-08-19T12:19:03.923815Z","iopub.status.busy":"2023-08-19T12:19:03.922903Z","iopub.status.idle":"2023-08-19T12:46:14.794458Z","shell.execute_reply":"2023-08-19T12:46:14.793369Z","shell.execute_reply.started":"2023-08-19T12:19:03.923777Z"}},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# GoogleNet After Fine Tuning","metadata":{}},{"cell_type":"code","source":"# Tried with 1 epoc for time being also checked reasults with 5 epocs\nfrom keras.applications.inception_v3 import InceptionV3\nfrom keras.models import Model\nfrom keras.layers import Dense, GlobalAveragePooling2D\nfrom keras.optimizers import Adam\nfrom keras.preprocessing.image import ImageDataGenerator\n\ndef create_fine_tuned_googlenet_model(photo_size, learning_rate):\n    base_model = InceptionV3(include_top=False, input_shape=(photo_size, photo_size, 3))\n    for layer in base_model.layers:\n        layer.trainable = False\n\n    x = GlobalAveragePooling2D()(base_model.output)\n    x = Dense(128, activation='relu')(x)\n    x = Dropout(0.5)(x)  # Adding dropout for regularization\n    output = Dense(1, activation='sigmoid')(x)\n\n    model = Model(inputs=base_model.input, outputs=output)\n\n    model.compile(optimizer=Adam(learning_rate),\n                  loss='binary_crossentropy',\n                  metrics=['accuracy'])\n\n    return model\n\n# Data augmentation parameters\ntrain_datagen = ImageDataGenerator(\n    rescale=1.0 / 255,\n    rotation_range=20,\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    fill_mode='nearest'\n)\n\nvalidation_datagen = ImageDataGenerator(rescale=1.0 / 255)\n\n# Set up data generators\ntrain_generator = train_datagen.flow_from_directory(\n    './train/',  # Specify your training data directory\n    target_size=(299, 299),\n    batch_size=32,\n    class_mode='binary'\n)\n\nvalidation_generator = validation_datagen.flow_from_directory(\n    './validation/',  # Specify your validation data directory\n    target_size=(299, 299),\n    batch_size=32,\n    class_mode='binary'\n)\n\nmodel = create_fine_tuned_googlenet_model(photo_size=299, learning_rate=0.001)\n\nmodel.fit(train_generator, epochs=1, validation_data=validation_generator)\nmodel.save(\"Dogs_vs_cats_googlenet_mode.h5\")","metadata":{"execution":{"iopub.execute_input":"2023-08-19T15:58:04.858464Z","iopub.status.busy":"2023-08-19T15:58:04.858062Z","iopub.status.idle":"2023-08-19T16:06:35.914832Z","shell.execute_reply":"2023-08-19T16:06:35.913739Z","shell.execute_reply.started":"2023-08-19T15:58:04.858432Z"}},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{"papermill":{"duration":0.028545,"end_time":"2022-06-29T17:15:46.906744","exception":false,"start_time":"2022-06-29T17:15:46.878199","status":"completed"},"pycharm":{"name":"#%% md\n"},"tags":[]}},{"cell_type":"markdown","source":"# Lets Try to Pridict using Inception model","metadata":{}},{"cell_type":"code","source":"from keras.models import load_model\nfrom keras.applications.efficientnet import preprocess_input\nimport numpy as np\nfrom PIL import Image\nimport os\nfrom sklearn.metrics import confusion_matrix\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom concurrent.futures import ThreadPoolExecutor\n\n# Load the trained Inception model\nmodel = load_model('/kaggle/working/Dogs_vs_cats_inception_mode.h5')  # Replace with the actual path to your model file\n\n# Path to the folder containing test images\ntest_folder = '/kaggle/working/test/'  # Replace with the path to your test image folder\n\ntrue_labels = []  # Store true labels (0 or 1)\npredicted_labels = []  # Store predicted labels (0 or 1)\n\n# Batch processing configuration\nbatch_size = 16  # Adjust according to your system's capacity\n\n# Function for image processing and prediction\ndef process_image(img_path):\n    img = Image.open(img_path)\n    img = img.resize((299, 299))  # Resize to match the model's input size\n    img_array = np.array(img)\n    img_array = preprocess_input(img_array)  # Preprocess input image\n\n    # Expand dimensions to create a batch-like structure\n    img_array = np.expand_dims(img_array, axis=0)\n\n    # Make predictions\n    predictions = model.predict(img_array)\n\n    true_label = int(os.path.basename(img_path).startswith('dog'))  # Assuming file names start with 'dog' or 'cat'\n    predicted_label = int(predictions[0][0] > 0.5)\n\n    return true_label, predicted_label\n\n# Get image paths\nimage_paths = [os.path.join(test_folder, filename) for filename in os.listdir(test_folder)\n               if filename.lower().endswith(('.jpg', '.png', '.jpeg'))]\n\n# Process images in parallel using ThreadPoolExecutor\nwith ThreadPoolExecutor(max_workers=os.cpu_count()) as executor:\n    results = list(executor.map(process_image, image_paths))\n\nfor true_label, predicted_label in results:\n    true_labels.append(true_label)\n    predicted_labels.append(predicted_label)\n\n    print(f\"True label: {true_label}, Predicted class: {predicted_label}\")","metadata":{"execution":{"iopub.execute_input":"2023-08-19T08:19:59.026880Z","iopub.status.busy":"2023-08-19T08:19:59.024632Z"},"scrolled":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a confusion matrix heatmap using seaborn\nplt.figure(figsize=(8, 6))\nclass_names = ['Cat', 'Dog']  # Assuming 0 is for cat and 1 is for dog\nsns.heatmap(cm, annot=True, fmt=\"d\", cmap=\"Blues\", xticklabels=class_names, yticklabels=class_names)\nplt.xlabel('Predicted')\nplt.ylabel('True')\nplt.title('Confusion Matrix')\nplt.show()","metadata":{"execution":{"iopub.execute_input":"2023-08-19T12:52:48.795427Z","iopub.status.busy":"2023-08-19T12:52:48.794979Z","iopub.status.idle":"2023-08-19T12:52:49.076356Z","shell.execute_reply":"2023-08-19T12:52:49.075269Z","shell.execute_reply.started":"2023-08-19T12:52:48.795391Z"}},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Lets Try to Pridict using GoogleNet model","metadata":{}},{"cell_type":"code","source":"from keras.models import load_model\nfrom keras.applications.efficientnet import preprocess_input\nimport numpy as np\nfrom PIL import Image\nimport os\nfrom sklearn.metrics import confusion_matrix\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom concurrent.futures import ThreadPoolExecutor\n\n# Load the trained Inception model\nmodel = load_model('/kaggle/working/Dogs_vs_cats_googlenet_mode.h5')  # Replace with the actual path to your model file\n\n# Path to the folder containing test images\ntest_folder = '/kaggle/working/test/'  # Replace with the path to your test image folder\n\ntrue_labels = []  # Store true labels (0 or 1)\npredicted_labels = []  # Store predicted labels (0 or 1)\n\n# Batch processing configuration\nbatch_size = 16  # Adjust according to your system's capacity\n\n# Function for image processing and prediction\ndef process_image(img_path):\n    img = Image.open(img_path)\n    img = img.resize((299, 299))  # Resize to match the model's input size\n    img_array = np.array(img)\n    img_array = preprocess_input(img_array)  # Preprocess input image\n\n    # Expand dimensions to create a batch-like structure\n    img_array = np.expand_dims(img_array, axis=0)\n\n    # Make predictions\n    predictions = model.predict(img_array)\n\n    true_label = int(os.path.basename(img_path).startswith('dog'))  # Assuming file names start with 'dog' or 'cat'\n    predicted_label = int(predictions[0][0] > 0.5)\n\n    return true_label, predicted_label\n\n# Get image paths\nimage_paths = [os.path.join(test_folder, filename) for filename in os.listdir(test_folder)\n               if filename.lower().endswith(('.jpg', '.png', '.jpeg'))]\n\n# Process images in parallel using ThreadPoolExecutor\nwith ThreadPoolExecutor(max_workers=os.cpu_count()) as executor:\n    results = list(executor.map(process_image, image_paths))\n\nfor true_label, predicted_label in results:\n    true_labels.append(true_label)\n    predicted_labels.append(predicted_label)\n\n    print(f\"True label: {true_label}, Predicted class: {predicted_label}\")","metadata":{"execution":{"iopub.execute_input":"2023-08-19T16:12:20.940735Z","iopub.status.busy":"2023-08-19T16:12:20.940348Z"},"scrolled":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import load_model\nfrom keras.applications.inception_v3 import preprocess_input\nfrom PIL import Image\nimport os\nimport numpy as np\nfrom sklearn.metrics import confusion_matrix\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\n# Load the fine-tuned GoogLeNet model\nfine_tuned_googlenet_model = load_model('/kaggle/working/Dogs_vs_cats_googlenet_mode.h5')\n\ndef load_image_from_path(image_path):\n    img = Image.open(image_path)\n    img = img.resize((299, 299))  # Resize to match the model's input size\n    img_array = np.array(img)\n    img_array = preprocess_input(img_array)\n    img_array = np.expand_dims(img_array, axis=0)\n    return img_array\n\nmTestPath = \"test/\"\nsubmission = {'id': [], 'label': []}\ntrue_labels = []\n\nfor i, test in enumerate(os.listdir(mTestPath)):\n    img = load_image_from_path(os.path.join(mTestPath, test))\n    submission['id'].append(i + 1)\n    \n    # Assuming file names start with 'dog' or 'cat'\n    true_label = int(test.startswith('dog'))\n    true_labels.append(true_label)\n    \n    # Make predictions using the fine-tuned GoogLeNet model\n    res = fine_tuned_googlenet_model.predict(img)[0][0]\n    submission['label'].append(res)\n    print(test, \"\\t\", res)\n\nimport pandas as pd\nsubmission_df = pd.DataFrame(submission)\nsubmission_df.to_csv('Dogs_vs_cats_GoogleNet_model_1_5step.csv', index=False)\n\n# Calculate the confusion matrix\npredicted_labels = [1 if label > 0.5 else 0 for label in submission_df['label']]\ncm = confusion_matrix(true_labels, predicted_labels)\n\n# Create a confusion matrix heatmap using seaborn\nplt.figure(figsize=(8, 6))\nclass_names = ['Cat', 'Dog']\nsns.heatmap(cm, annot=True, fmt=\"d\", cmap=\"Blues\", xticklabels=class_names, yticklabels=class_names)\nplt.xlabel('Predicted')\nplt.ylabel('True')\nplt.title('Confusion Matrix')\nplt.show()","metadata":{"execution":{"iopub.execute_input":"2023-08-19T16:42:50.288617Z","iopub.status.busy":"2023-08-19T16:42:50.287705Z"},"scrolled":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a confusion matrix heatmap using seaborn\nplt.figure(figsize=(8, 6))\nclass_names = ['Cat', 'Dog']\nsns.heatmap(cm, annot=True, fmt=\"d\", cmap=\"Blues\", xticklabels=class_names, yticklabels=class_names)\nplt.xlabel('Predicted')\nplt.ylabel('True')\nplt.title('Confusion Matrix')\nplt.show()","metadata":{"execution":{"iopub.execute_input":"2023-08-19T17:06:09.040638Z","iopub.status.busy":"2023-08-19T17:06:09.039981Z","iopub.status.idle":"2023-08-19T17:06:09.418260Z","shell.execute_reply":"2023-08-19T17:06:09.417129Z","shell.execute_reply.started":"2023-08-19T17:06:09.040595Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# I have executed all above models with basic method and then tried again by fine tuning it.\n# As per my observation Inception, EfficentNet and GoogleNet perform very well interms of accuracy and loss fucntion.\n# In this example i have used Inception and GoogleNet's Fine Tuned models and got better results in each epoc.\n# I have taken max 5 epoc for each pre-trained model (As GPU limit is exhusted) and Confusion matrix is not satisfied to me.\n# I belive that if i try for some more epoc i get some better results.\n# With this Assignment i understood how to play with pre-trained models and different model architechures.","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}