{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-12T18:37:47.839880Z","iopub.execute_input":"2023-05-12T18:37:47.840852Z","iopub.status.idle":"2023-05-12T18:38:12.221045Z","shell.execute_reply.started":"2023-05-12T18:37:47.840744Z","shell.execute_reply":"2023-05-12T18:38:12.219476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings('ignore')\n\nimport zipfile\nimport glob\nimport os\nimport random\nimport pandas as pd\nimport numpy as np\nfrom PIL import Image\n\nimport matplotlib\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport seaborn as sns\nfrom sklearn.utils import shuffle\nfrom keras.preprocessing.image import ImageDataGenerator, img_to_array, array_to_img, load_img\nfrom sklearn.model_selection import train_test_split\n\n#importing various metrics\nfrom sklearn.metrics import confusion_matrix, classification_report, accuracy_score, f1_score, roc_auc_score\n\n# importing model relevant dependencies\nimport tensorflow as tf\nfrom keras.models import Model, Sequential\nfrom keras.layers import Conv2D, MaxPooling2D, Dropout, Dense, Flatten\nfrom keras import optimizers, regularizers\nfrom tensorflow.keras.applications.mobilenet_v2 import MobileNetV2\nfrom tensorflow.keras.applications.mobilenet_v2 import preprocess_input\nfrom keras.callbacks import EarlyStopping, LearningRateScheduler\n\nfrom keras import backend as K\nK.clear_session()\n\n#","metadata":{"execution":{"iopub.status.busy":"2023-05-12T18:38:12.222599Z","iopub.execute_input":"2023-05-12T18:38:12.223194Z","iopub.status.idle":"2023-05-12T18:38:19.184414Z","shell.execute_reply.started":"2023-05-12T18:38:12.223156Z","shell.execute_reply":"2023-05-12T18:38:19.183428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# extracting train and test data from zipfiles to data folder\nzip_files = glob.glob('/kaggle/input/dogs-vs-cats/*.zip')\nprint(\"{} files found in the input directory\".format(len(zip_files)))","metadata":{"execution":{"iopub.status.busy":"2023-05-12T18:38:19.186095Z","iopub.execute_input":"2023-05-12T18:38:19.187076Z","iopub.status.idle":"2023-05-12T18:38:19.197873Z","shell.execute_reply.started":"2023-05-12T18:38:19.187034Z","shell.execute_reply":"2023-05-12T18:38:19.196871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This code snippet is used for extracting the contents of a zipped file in the \"data\" directory.\n\n","metadata":{}},{"cell_type":"code","source":"for file in zip_files:\n    with zipfile.ZipFile(file,'r') as Z:\n        Z.extractall('data')\n    print('{} extraction complete'.format(file.split('/')[-1]))","metadata":{"execution":{"iopub.status.busy":"2023-05-12T18:38:19.201601Z","iopub.execute_input":"2023-05-12T18:38:19.201972Z","iopub.status.idle":"2023-05-12T18:38:35.709251Z","shell.execute_reply.started":"2023-05-12T18:38:19.201934Z","shell.execute_reply":"2023-05-12T18:38:35.707564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# EDA with images in the directory\ntrain_dir = '/kaggle/working/data/train'\ntest_dir = '/kaggle/working/data/test1'\n\nprint(\"{} is the count of images in train_dir\".format(len(os.listdir(train_dir))))\nprint(\"{} is the count of images in test_dir\".format(len(os.listdir(test_dir))))","metadata":{"execution":{"iopub.status.busy":"2023-05-12T18:38:35.710898Z","iopub.execute_input":"2023-05-12T18:38:35.711566Z","iopub.status.idle":"2023-05-12T18:38:35.767749Z","shell.execute_reply.started":"2023-05-12T18:38:35.711522Z","shell.execute_reply":"2023-05-12T18:38:35.761604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# let's explore a sample file and how to extract label and file name from it.\n\nfor _ in range(5):\n    print(random.choice(os.listdir(train_dir)))","metadata":{"execution":{"iopub.status.busy":"2023-05-12T18:38:35.769146Z","iopub.execute_input":"2023-05-12T18:38:35.769553Z","iopub.status.idle":"2023-05-12T18:38:35.860920Z","shell.execute_reply.started":"2023-05-12T18:38:35.769495Z","shell.execute_reply":"2023-05-12T18:38:35.859874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# saving extracted data into dataframe for further processing\ndef label(path):\n    return [file.split('.')[0] for file in os.listdir(path)]\n\ndef filename(path):\n    return [file for file in os.listdir(path)]\n\nx_train_images_name = filename(train_dir)\nx_test_images_name = filename(test_dir)\ny_train_label_name = label(train_dir)\n\n#creating df with name and label\n\ndf_train_image = pd.DataFrame({'filename':x_train_images_name, 'label':y_train_label_name})\ndf_submission = pd.DataFrame({'filename':x_test_images_name})","metadata":{"execution":{"iopub.status.busy":"2023-05-12T18:38:35.862300Z","iopub.execute_input":"2023-05-12T18:38:35.865803Z","iopub.status.idle":"2023-05-12T18:38:35.930372Z","shell.execute_reply.started":"2023-05-12T18:38:35.865763Z","shell.execute_reply":"2023-05-12T18:38:35.929478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_image.head()\n","metadata":{"execution":{"iopub.status.busy":"2023-05-12T18:38:35.931863Z","iopub.execute_input":"2023-05-12T18:38:35.932205Z","iopub.status.idle":"2023-05-12T18:38:35.952377Z","shell.execute_reply.started":"2023-05-12T18:38:35.932169Z","shell.execute_reply":"2023-05-12T18:38:35.951452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_submission.head()\n","metadata":{"execution":{"iopub.status.busy":"2023-05-12T18:38:35.953797Z","iopub.execute_input":"2023-05-12T18:38:35.954139Z","iopub.status.idle":"2023-05-12T18:38:35.964072Z","shell.execute_reply.started":"2023-05-12T18:38:35.954110Z","shell.execute_reply":"2023-05-12T18:38:35.962973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# training directory\nfor _ in range(5):\n    file_name = train_dir + \"/\"+ random.choice(os.listdir(train_dir))\n    #read image\n    image = load_img(file_name, target_size=(224,224))\n    plt.imshow(image)\n    plt.title(\"{}: displayed\".format(file_name))\n    plt.show()\n    ","metadata":{"execution":{"iopub.status.busy":"2023-05-12T18:38:35.970486Z","iopub.execute_input":"2023-05-12T18:38:35.970786Z","iopub.status.idle":"2023-05-12T18:38:37.323092Z","shell.execute_reply.started":"2023-05-12T18:38:35.970759Z","shell.execute_reply":"2023-05-12T18:38:37.322081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The code iterates over the loop five times, each time selecting a random image from the test directory.","metadata":{}},{"cell_type":"code","source":"# test directory\nfor _ in range(5):\n    file_name = test_dir + \"/\"+ random.choice(os.listdir(test_dir))\n    #read image\n    image = load_img(file_name, target_size=(224,224))\n    plt.imshow(image)\n    plt.title(\"{}: displayed\".format(file_name))\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-12T18:38:37.327843Z","iopub.execute_input":"2023-05-12T18:38:37.330461Z","iopub.status.idle":"2023-05-12T18:38:38.770391Z","shell.execute_reply.started":"2023-05-12T18:38:37.330418Z","shell.execute_reply":"2023-05-12T18:38:38.769433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This code snippet is used to split the dataset into train, validation, and test sets.\n\n","metadata":{}},{"cell_type":"code","source":"# splitting the data into train test\ntrain_val_df, test_df = train_test_split(df_train_image, test_size=0.02)\n# splitting the train val further into train, val\ntrain_df, val_df = train_test_split(train_val_df, test_size=0.2)\n\nprint(\"Total number of images in the train is {}\".format(train_df.shape[0]))\nprint(\"Total number of images in the internal test is {}\".format(test_df.shape[0]))\nprint(\"Total number of images in the val is {}\".format(val_df.shape[0]))\nprint(\"Total number of images in the submission is {}\".format(df_submission.shape[0]))","metadata":{"execution":{"iopub.status.busy":"2023-05-12T18:38:38.772054Z","iopub.execute_input":"2023-05-12T18:38:38.772862Z","iopub.status.idle":"2023-05-12T18:38:38.790433Z","shell.execute_reply.started":"2023-05-12T18:38:38.772815Z","shell.execute_reply":"2023-05-12T18:38:38.789567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This code snippet uses the seaborn library to create a countplot graph that shows the distribution of data within each of the three sets - train, validation, and internal testing data - in the Kaggle Dogs-vs-Cats challenge.\n","metadata":{}},{"cell_type":"code","source":"\n# checking the data distribution within each train,val and internal testing data.\nsns.countplot(x ='label', data = train_df)","metadata":{"execution":{"iopub.status.busy":"2023-05-12T18:38:38.792216Z","iopub.execute_input":"2023-05-12T18:38:38.792907Z","iopub.status.idle":"2023-05-12T18:38:39.005795Z","shell.execute_reply.started":"2023-05-12T18:38:38.792869Z","shell.execute_reply":"2023-05-12T18:38:39.004785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x ='label', data = train_df)","metadata":{"execution":{"iopub.status.busy":"2023-05-12T18:38:39.007381Z","iopub.execute_input":"2023-05-12T18:38:39.007780Z","iopub.status.idle":"2023-05-12T18:38:39.207700Z","shell.execute_reply.started":"2023-05-12T18:38:39.007740Z","shell.execute_reply":"2023-05-12T18:38:39.206778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x ='label', data = test_df)","metadata":{"execution":{"iopub.status.busy":"2023-05-12T18:38:39.209333Z","iopub.execute_input":"2023-05-12T18:38:39.209714Z","iopub.status.idle":"2023-05-12T18:38:39.395545Z","shell.execute_reply.started":"2023-05-12T18:38:39.209676Z","shell.execute_reply":"2023-05-12T18:38:39.394450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Classification with MobileNetV2","metadata":{}},{"cell_type":"code","source":"# Image augmentation data preparation with Image Data Generator\nimg_size = 224\nbatch_size = 1024\ntrain_aug_map = ImageDataGenerator(\n                rotation_range=10,\n                horizontal_flip = True,\n                fill_mode = 'nearest',\n                preprocessing_function = preprocess_input)\n\ntrain_data = train_aug_map.flow_from_dataframe(\n                            train_df, train_dir,\n                            x_col='filename',\n                            y_col = 'label',\n                            target_size = (img_size, img_size),\n                            batch_size= batch_size,\n                            class_mode='categorical')\n\n\nval_aug_mag = ImageDataGenerator(\n                preprocessing_function = preprocess_input)\n\nval_data = val_aug_mag.flow_from_dataframe(\n                            val_df, train_dir,\n                            x_col='filename',\n                            y_col = 'label',\n                            target_size = (img_size, img_size),\n                            batch_size= batch_size,\n                            class_mode='categorical')\n\ntest_aug_map = ImageDataGenerator(\n                preprocessing_function = preprocess_input)\n\ntest_data = val_aug_mag.flow_from_dataframe(\n                            test_df, train_dir,\n                            x_col='filename',\n                            y_col = None,\n                            class_mode = None,\n                            target_size = (img_size, img_size),\n                            shuffle = False)","metadata":{"execution":{"iopub.status.busy":"2023-05-12T18:38:39.397162Z","iopub.execute_input":"2023-05-12T18:38:39.397557Z","iopub.status.idle":"2023-05-12T18:38:39.929802Z","shell.execute_reply.started":"2023-05-12T18:38:39.397502Z","shell.execute_reply":"2023-05-12T18:38:39.928632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The code snippet is building Mobilenetv2 ","metadata":{}},{"cell_type":"code","source":"# building mobile net model\nmobilenet = MobileNetV2(weights='imagenet', include_top=False, input_shape=(224,224,3))\nmobilenet.trainable = False\nmobilenet_model = Sequential([mobilenet,Flatten(),Dense(1024, activation='relu'),\n                             Dropout(0.4), Dense(2,activation='softmax')])\n\noptimizer = tf.keras.optimizers.Adam(1e-6)\n\nmobilenet_model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-05-12T18:38:39.931602Z","iopub.execute_input":"2023-05-12T18:38:39.932000Z","iopub.status.idle":"2023-05-12T18:38:45.540987Z","shell.execute_reply.started":"2023-05-12T18:38:39.931960Z","shell.execute_reply":"2023-05-12T18:38:45.539878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# early stopping criterion\n\nepoch = 50\nlearning_rate = 3e-5 \nlr_start = 0.00000001\nlr_min = 0.000001\nlr_max = 3e-5 \nlr_rampup_epochs = 1\nlr_sustain_epochs = 1\nlr_exp_decay = .8\n\ndef adjustable_lr(epoch):\n    if epoch < lr_rampup_epochs:\n        lr = (lr_max - lr_start) / lr_rampup_epochs * epoch + lr_start\n    elif epoch < lr_rampup_epochs + lr_sustain_epochs:\n        lr = lr_max\n    else:\n        lr = (lr_max - lr_min) * lr_exp_decay**(epoch - lr_rampup_epochs - lr_sustain_epochs) + lr_min\n    return lr\n    \n    \nearly_stop = EarlyStopping(patience=5)\nlr_callback = LearningRateScheduler(adjustable_lr, verbose=True)\ncallbacks = [early_stop, lr_callback]","metadata":{"execution":{"iopub.status.busy":"2023-05-12T18:38:45.542717Z","iopub.execute_input":"2023-05-12T18:38:45.543392Z","iopub.status.idle":"2023-05-12T18:38:45.551959Z","shell.execute_reply.started":"2023-05-12T18:38:45.543348Z","shell.execute_reply":"2023-05-12T18:38:45.550835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mobilenet_model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy'])\ntrain_images = train_df.shape[0]\nval_images = val_df.shape[0]\nhistory = mobilenet_model.fit_generator(train_data, epochs=20, validation_data=val_data, validation_steps=val_images//batch_size, steps_per_epoch=train_images//batch_size, callbacks=callbacks)","metadata":{"execution":{"iopub.status.busy":"2023-05-12T18:38:45.553660Z","iopub.execute_input":"2023-05-12T18:38:45.554088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])\nplt.title('model accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train', 'val'], loc='upper left')\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('model loss')\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.legend(['train', 'val'], loc='upper left')\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mobilenet_v2_pred = mobilenet_model.predict_generator(test_data)\ntest_df['prediction'] = np.argmax(mobilenet_v2_pred, axis=-1)\nlabels = dict((v,k) for k,v in train_data.class_indices.items())\ntest_df['prediction'] = test_df['prediction'].map(labels)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mobilenet_confusion_matrix = confusion_matrix(test_df['label'], test_df['prediction'])\nprint(mobilenet_confusion_matrix)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predictions on submission dataframe\nmobilenet_submission_augmap = ImageDataGenerator(preprocessing_function = preprocess_input)\nmobilenet_submission_data = mobilenet_submission_augmap.flow_from_dataframe(\n             df_submission, test_dir,\n             x_col = 'filename',\n             y_col = None,\n             class_mode = None,\n             target_size = (img_size, img_size),\n             shuffle = False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# writing the predictions\nmobilenet_submission_pred = mobilenet_model.predict_generator(mobilenet_submission_data)\ndf_submission['prediction'] = np.argmax(mobilenet_submission_pred, axis = -1)\nlabels = dict((v,k) for k,v in train_data.class_indices.items())\n#df_submission['prediction'] = df_submission['prediction'].map(labels)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# finally printing\n\ndf_submission.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}