{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":30635,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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":"2024-03-11T08:20:45.365007Z","iopub.execute_input":"2024-03-11T08:20:45.365383Z","iopub.status.idle":"2024-03-11T08:20:45.373739Z","shell.execute_reply.started":"2024-03-11T08:20:45.365353Z","shell.execute_reply":"2024-03-11T08:20:45.372711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Import Libraries","metadata":{}},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix, accuracy_score\nfrom sklearn.model_selection import train_test_split\nimport zipfile\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential, Model\nfrom tensorflow.keras.layers import Activation, Dense, Conv2D, Flatten, Dropout, MaxPooling2D,BatchNormalization\nimport cv2\nimport matplotlib.pyplot as plt\nfrom keras.preprocessing.image import ImageDataGenerator, load_img","metadata":{"execution":{"iopub.status.busy":"2024-03-11T08:38:10.275805Z","iopub.execute_input":"2024-03-11T08:38:10.276523Z","iopub.status.idle":"2024-03-11T08:38:10.282793Z","shell.execute_reply.started":"2024-03-11T08:38:10.276486Z","shell.execute_reply":"2024-03-11T08:38:10.281557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Extract Files","metadata":{}},{"cell_type":"code","source":"with zipfile.ZipFile(\"/kaggle/input/dogs-vs-cats/train.zip\",\"r\") as z:\n    z.extractall(\".\")\nwith zipfile.ZipFile(\"/kaggle/input/dogs-vs-cats/test1.zip\",\"r\") as z:\n    z.extractall(\".\")","metadata":{"execution":{"iopub.status.busy":"2024-03-11T07:28:09.542821Z","iopub.execute_input":"2024-03-11T07:28:09.543402Z","iopub.status.idle":"2024-03-11T07:28:20.581657Z","shell.execute_reply.started":"2024-03-11T07:28:09.543369Z","shell.execute_reply":"2024-03-11T07:28:20.580805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"main_dir = \"/kaggle/working/\"\ntrain_dir = \"train\"\npath = os.path.join(main_dir,train_dir)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T07:28:20.582816Z","iopub.execute_input":"2024-03-11T07:28:20.583123Z","iopub.status.idle":"2024-03-11T07:28:20.587665Z","shell.execute_reply.started":"2024-03-11T07:28:20.583098Z","shell.execute_reply":"2024-03-11T07:28:20.586707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"width=128\nheight=128\nsize=(width,height)\nchannels=3\nbatch_size=15","metadata":{"execution":{"iopub.status.busy":"2024-03-11T07:28:20.589820Z","iopub.execute_input":"2024-03-11T07:28:20.590118Z","iopub.status.idle":"2024-03-11T07:28:21.268856Z","shell.execute_reply.started":"2024-03-11T07:28:20.590094Z","shell.execute_reply":"2024-03-11T07:28:21.267829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filename=os.listdir(path)\ncategories=[]\nfor f in filename:\n    category=f.split('.')[0]\n    if category=='dog':\n        categories.append(1)\n    else:\n        categories.append(0)\ndataframe=pd.DataFrame({'filename':filename,'category':categories})","metadata":{"execution":{"iopub.status.busy":"2024-03-11T07:28:21.270132Z","iopub.execute_input":"2024-03-11T07:28:21.270504Z","iopub.status.idle":"2024-03-11T07:28:21.319175Z","shell.execute_reply.started":"2024-03-11T07:28:21.270471Z","shell.execute_reply":"2024-03-11T07:28:21.318400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataframe.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-11T07:28:21.320249Z","iopub.execute_input":"2024-03-11T07:28:21.320545Z","iopub.status.idle":"2024-03-11T07:28:21.329309Z","shell.execute_reply.started":"2024-03-11T07:28:21.320519Z","shell.execute_reply":"2024-03-11T07:28:21.328267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataframe.tail()\n","metadata":{"execution":{"iopub.status.busy":"2024-03-11T07:28:21.330484Z","iopub.execute_input":"2024-03-11T07:28:21.330805Z","iopub.status.idle":"2024-03-11T07:28:21.341056Z","shell.execute_reply.started":"2024-03-11T07:28:21.330743Z","shell.execute_reply":"2024-03-11T07:28:21.340183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\nsample=random.choice(filename)\nimg=load_img(path+'/'+sample)\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T07:28:21.342061Z","iopub.execute_input":"2024-03-11T07:28:21.342342Z","iopub.status.idle":"2024-03-11T07:28:21.732164Z","shell.execute_reply.started":"2024-03-11T07:28:21.342309Z","shell.execute_reply":"2024-03-11T07:28:21.731207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## CNN Model","metadata":{}},{"cell_type":"code","source":"model=Sequential()\n\nmodel.add(Conv2D(32,(3,3),activation='relu',input_shape=(width,height,channels)))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(64, (3, 3), activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(128, (3, 3), activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Flatten())\n\nmodel.add(Dense(512, activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.5))\nmodel.add(Dense(2, activation='softmax'))\n\nmodel.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-03-11T07:28:21.733436Z","iopub.execute_input":"2024-03-11T07:28:21.733760Z","iopub.status.idle":"2024-03-11T07:28:21.936231Z","shell.execute_reply.started":"2024-03-11T07:28:21.733722Z","shell.execute_reply":"2024-03-11T07:28:21.935276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model Architecture","metadata":{}},{"cell_type":"code","source":"import keras\nkeras.utils.plot_model(model,'model.png')","metadata":{"execution":{"iopub.status.busy":"2024-03-11T07:28:21.937515Z","iopub.execute_input":"2024-03-11T07:28:21.937870Z","iopub.status.idle":"2024-03-11T07:28:22.054174Z","shell.execute_reply.started":"2024-03-11T07:28:21.937835Z","shell.execute_reply":"2024-03-11T07:28:22.053244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split of Dataset ","metadata":{"execution":{"iopub.status.busy":"2024-03-11T07:28:22.055456Z","iopub.execute_input":"2024-03-11T07:28:22.055792Z","iopub.status.idle":"2024-03-11T07:28:22.060331Z","shell.execute_reply.started":"2024-03-11T07:28:22.055760Z","shell.execute_reply":"2024-03-11T07:28:22.059332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Split The Dataset","metadata":{}},{"cell_type":"code","source":"trainData, tempData= train_test_split(dataframe,test_size=0.3, random_state=42)\ntest_Data, validData= train_test_split(tempData,test_size=0.5, random_state=42)\ntrainData=trainData.reset_index(drop=True)\nvalidData=validData.reset_index(drop=True)\ntest_Data=test_Data.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T07:28:22.066159Z","iopub.execute_input":"2024-03-11T07:28:22.066446Z","iopub.status.idle":"2024-03-11T07:28:22.079785Z","shell.execute_reply.started":"2024-03-11T07:28:22.066401Z","shell.execute_reply":"2024-03-11T07:28:22.079025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainData.shape","metadata":{"execution":{"iopub.status.busy":"2024-03-11T07:28:22.080997Z","iopub.execute_input":"2024-03-11T07:28:22.081303Z","iopub.status.idle":"2024-03-11T07:28:22.087926Z","shell.execute_reply.started":"2024-03-11T07:28:22.081259Z","shell.execute_reply":"2024-03-11T07:28:22.086906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validData.shape","metadata":{"execution":{"iopub.status.busy":"2024-03-11T07:28:22.089159Z","iopub.execute_input":"2024-03-11T07:28:22.089465Z","iopub.status.idle":"2024-03-11T07:28:22.097285Z","shell.execute_reply.started":"2024-03-11T07:28:22.089430Z","shell.execute_reply":"2024-03-11T07:28:22.096455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_Data.shape","metadata":{"execution":{"iopub.status.busy":"2024-03-11T07:28:22.098353Z","iopub.execute_input":"2024-03-11T07:28:22.098649Z","iopub.status.idle":"2024-03-11T07:28:22.107404Z","shell.execute_reply.started":"2024-03-11T07:28:22.098614Z","shell.execute_reply":"2024-03-11T07:28:22.106623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tot_trainData=trainData.shape[0]\ntot_validData=validData.shape[0]\ntot_test_Data=test_Data.shape[0]","metadata":{"execution":{"iopub.status.busy":"2024-03-11T07:28:22.108540Z","iopub.execute_input":"2024-03-11T07:28:22.109109Z","iopub.status.idle":"2024-03-11T07:28:22.115202Z","shell.execute_reply.started":"2024-03-11T07:28:22.109077Z","shell.execute_reply":"2024-03-11T07:28:22.114247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Generating Training Data","metadata":{}},{"cell_type":"code","source":"train_data_gen = ImageDataGenerator(\n    rotation_range=15,\n    rescale=1./255,\n    shear_range=0.1,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    width_shift_range=0.1,\n    height_shift_range=0.1\n)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T07:28:22.125392Z","iopub.execute_input":"2024-03-11T07:28:22.125969Z","iopub.status.idle":"2024-03-11T07:28:22.133643Z","shell.execute_reply.started":"2024-03-11T07:28:22.125937Z","shell.execute_reply":"2024-03-11T07:28:22.132892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainData.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-11T07:28:22.134533Z","iopub.execute_input":"2024-03-11T07:28:22.134817Z","iopub.status.idle":"2024-03-11T07:28:22.147539Z","shell.execute_reply.started":"2024-03-11T07:28:22.134793Z","shell.execute_reply":"2024-03-11T07:28:22.146638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainData.dtypes","metadata":{"execution":{"iopub.status.busy":"2024-03-11T07:28:22.148887Z","iopub.execute_input":"2024-03-11T07:28:22.149227Z","iopub.status.idle":"2024-03-11T07:28:22.159084Z","shell.execute_reply.started":"2024-03-11T07:28:22.149201Z","shell.execute_reply":"2024-03-11T07:28:22.158199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainData['category']=trainData['category'].astype(str)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T07:28:22.160142Z","iopub.execute_input":"2024-03-11T07:28:22.160403Z","iopub.status.idle":"2024-03-11T07:28:22.177065Z","shell.execute_reply.started":"2024-03-11T07:28:22.160380Z","shell.execute_reply":"2024-03-11T07:28:22.176169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = train_data_gen.flow_from_dataframe(\n    trainData, \n    path+'/', \n    x_col='filename',\n    y_col='category',\n    target_size=(128,128),\n    class_mode='categorical',\n    batch_size=15\n)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T07:28:22.178362Z","iopub.execute_input":"2024-03-11T07:28:22.178865Z","iopub.status.idle":"2024-03-11T07:28:22.397051Z","shell.execute_reply.started":"2024-03-11T07:28:22.178832Z","shell.execute_reply":"2024-03-11T07:28:22.396087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainData.dtypes","metadata":{"execution":{"iopub.status.busy":"2024-03-11T07:28:22.398322Z","iopub.execute_input":"2024-03-11T07:28:22.398698Z","iopub.status.idle":"2024-03-11T07:28:22.406090Z","shell.execute_reply.started":"2024-03-11T07:28:22.398664Z","shell.execute_reply":"2024-03-11T07:28:22.405133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Genarating validation data\n","metadata":{}},{"cell_type":"code","source":"validData['category']=validData['category'].astype(str)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T07:28:22.407222Z","iopub.execute_input":"2024-03-11T07:28:22.407535Z","iopub.status.idle":"2024-03-11T07:28:22.417338Z","shell.execute_reply.started":"2024-03-11T07:28:22.407510Z","shell.execute_reply":"2024-03-11T07:28:22.416447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_data_gen = ImageDataGenerator(\n    rescale=1./255\n)\nvalid_generator = valid_data_gen.flow_from_dataframe(\n    validData, \n    path+'/', \n    x_col='filename',\n    y_col='category',\n    target_size=(128,128),\n    class_mode='categorical',\n    batch_size=15\n)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T07:28:22.418386Z","iopub.execute_input":"2024-03-11T07:28:22.418694Z","iopub.status.idle":"2024-03-11T07:28:22.474130Z","shell.execute_reply.started":"2024-03-11T07:28:22.418670Z","shell.execute_reply":"2024-03-11T07:28:22.473226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Early Stopping\nfrom keras.callbacks import EarlyStopping,ReduceLROnPlateau","metadata":{"execution":{"iopub.status.busy":"2024-03-11T07:28:22.475112Z","iopub.execute_input":"2024-03-11T07:28:22.475359Z","iopub.status.idle":"2024-03-11T07:28:22.479782Z","shell.execute_reply.started":"2024-03-11T07:28:22.475337Z","shell.execute_reply":"2024-03-11T07:28:22.478921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"earlystop= EarlyStopping(patience=10)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T07:28:22.480936Z","iopub.execute_input":"2024-03-11T07:28:22.481257Z","iopub.status.idle":"2024-03-11T07:28:22.488014Z","shell.execute_reply.started":"2024-03-11T07:28:22.481227Z","shell.execute_reply":"2024-03-11T07:28:22.487223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learning_rate_reduction=ReduceLROnPlateau(\n    monitor='val_acc',\n    patience=2,\n    verbose=1,\n    factor=0.5,\n    min_lr=0.00001\n)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T07:28:22.489033Z","iopub.execute_input":"2024-03-11T07:28:22.489292Z","iopub.status.idle":"2024-03-11T07:28:22.496751Z","shell.execute_reply.started":"2024-03-11T07:28:22.489269Z","shell.execute_reply":"2024-03-11T07:28:22.495852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callbacks=[earlystop,learning_rate_reduction]","metadata":{"execution":{"iopub.status.busy":"2024-03-11T07:28:22.497767Z","iopub.execute_input":"2024-03-11T07:28:22.498060Z","iopub.status.idle":"2024-03-11T07:28:22.505270Z","shell.execute_reply.started":"2024-03-11T07:28:22.498035Z","shell.execute_reply":"2024-03-11T07:28:22.504352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-03-11T07:28:22.506404Z","iopub.execute_input":"2024-03-11T07:28:22.506672Z","iopub.status.idle":"2024-03-11T07:28:22.525048Z","shell.execute_reply.started":"2024-03-11T07:28:22.506649Z","shell.execute_reply":"2024-03-11T07:28:22.524176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history=model.fit(train_generator,epochs=100,verbose=1, validation_data=valid_generator,validation_steps=tot_validData//batch_size,steps_per_epoch=tot_trainData//batch_size,callbacks=callbacks)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T07:28:22.526170Z","iopub.execute_input":"2024-03-11T07:28:22.526428Z","iopub.status.idle":"2024-03-11T07:33:08.166401Z","shell.execute_reply.started":"2024-03-11T07:28:22.526396Z","shell.execute_reply":"2024-03-11T07:33:08.165466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('cat_vs_dog.h5')","metadata":{"execution":{"iopub.status.busy":"2024-03-11T08:09:06.307272Z","iopub.execute_input":"2024-03-11T08:09:06.308188Z","iopub.status.idle":"2024-03-11T08:09:06.682658Z","shell.execute_reply.started":"2024-03-11T08:09:06.308153Z","shell.execute_reply":"2024-03-11T08:09:06.681650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history.history.keys()","metadata":{"execution":{"iopub.status.busy":"2024-03-11T08:09:23.085215Z","iopub.execute_input":"2024-03-11T08:09:23.086027Z","iopub.status.idle":"2024-03-11T08:09:23.092196Z","shell.execute_reply.started":"2024-03-11T08:09:23.085987Z","shell.execute_reply":"2024-03-11T08:09:23.091174Z"},"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.xlabel('epochs')\nplt.legend(['train','validation'],loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-11T08:09:26.025227Z","iopub.execute_input":"2024-03-11T08:09:26.025823Z","iopub.status.idle":"2024-03-11T08:09:26.359533Z","shell.execute_reply.started":"2024-03-11T08:09:26.025791Z","shell.execute_reply":"2024-03-11T08:09:26.358619Z"},"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.xlabel('epochs')\nplt.legend(['train','validation'],loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-11T08:09:31.294569Z","iopub.execute_input":"2024-03-11T08:09:31.295433Z","iopub.status.idle":"2024-03-11T08:09:31.562327Z","shell.execute_reply.started":"2024-03-11T08:09:31.295400Z","shell.execute_reply":"2024-03-11T08:09:31.561330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dir='test1'\npath2 = os.path.join(main_dir,test_dir)\ntest_filename=os.listdir(path2)\n#test_filename\n\ntestData=pd.DataFrame({'filename':test_filename})","metadata":{"execution":{"iopub.status.busy":"2024-03-11T08:09:40.346379Z","iopub.execute_input":"2024-03-11T08:09:40.346739Z","iopub.status.idle":"2024-03-11T08:09:40.360818Z","shell.execute_reply.started":"2024-03-11T08:09:40.346709Z","shell.execute_reply":"2024-03-11T08:09:40.360048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testData.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-11T08:09:43.284549Z","iopub.execute_input":"2024-03-11T08:09:43.285476Z","iopub.status.idle":"2024-03-11T08:09:43.294298Z","shell.execute_reply.started":"2024-03-11T08:09:43.285443Z","shell.execute_reply":"2024-03-11T08:09:43.293367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_count=testData.shape[0]\nsample_count","metadata":{"execution":{"iopub.status.busy":"2024-03-11T08:09:48.885320Z","iopub.execute_input":"2024-03-11T08:09:48.886431Z","iopub.status.idle":"2024-03-11T08:09:48.892602Z","shell.execute_reply.started":"2024-03-11T08:09:48.886393Z","shell.execute_reply":"2024-03-11T08:09:48.891665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data_gen=ImageDataGenerator(rescale=1./255)\ntest_generator = test_data_gen.flow_from_dataframe(\n    testData, \n    path2+'/', \n    x_col='filename',\n    y_col=None,\n    target_size=(128,128),\n    class_mode=None,\n    batch_size=15,\n    shuffle=False\n)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T08:09:55.509354Z","iopub.execute_input":"2024-03-11T08:09:55.510082Z","iopub.status.idle":"2024-03-11T08:09:55.641427Z","shell.execute_reply.started":"2024-03-11T08:09:55.510048Z","shell.execute_reply":"2024-03-11T08:09:55.640657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_Data_gen=ImageDataGenerator(rescale=1./255)\ntest_generator2 = test_Data_gen.flow_from_dataframe(\n    test_Data, \n    path+'/', \n    x_col='filename',\n    y_col=None,\n    target_size=(128,128),\n    class_mode=None,\n    batch_size=15,\n    shuffle=False\n)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T08:10:19.605540Z","iopub.execute_input":"2024-03-11T08:10:19.605933Z","iopub.status.idle":"2024-03-11T08:10:19.653481Z","shell.execute_reply.started":"2024-03-11T08:10:19.605895Z","shell.execute_reply":"2024-03-11T08:10:19.652471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Prediction\ntest_Data.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-11T08:10:37.985537Z","iopub.execute_input":"2024-03-11T08:10:37.985933Z","iopub.status.idle":"2024-03-11T08:10:37.995721Z","shell.execute_reply.started":"2024-03-11T08:10:37.985900Z","shell.execute_reply":"2024-03-11T08:10:37.994685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict_generator(test_generator, steps=np.ceil(sample_count/15))","metadata":{"execution":{"iopub.status.busy":"2024-03-11T08:10:55.825740Z","iopub.execute_input":"2024-03-11T08:10:55.826113Z","iopub.status.idle":"2024-03-11T08:11:14.131813Z","shell.execute_reply.started":"2024-03-11T08:10:55.826084Z","shell.execute_reply":"2024-03-11T08:11:14.130945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred","metadata":{"execution":{"iopub.status.busy":"2024-03-11T08:11:26.045013Z","iopub.execute_input":"2024-03-11T08:11:26.045395Z","iopub.status.idle":"2024-03-11T08:11:26.052869Z","shell.execute_reply.started":"2024-03-11T08:11:26.045365Z","shell.execute_reply":"2024-03-11T08:11:26.051753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testData['category'] = np.argmax(pred, axis=-1)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T08:11:34.944682Z","iopub.execute_input":"2024-03-11T08:11:34.945431Z","iopub.status.idle":"2024-03-11T08:11:34.950190Z","shell.execute_reply.started":"2024-03-11T08:11:34.945401Z","shell.execute_reply":"2024-03-11T08:11:34.949167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testData.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-11T08:11:37.065554Z","iopub.execute_input":"2024-03-11T08:11:37.066400Z","iopub.status.idle":"2024-03-11T08:11:37.076191Z","shell.execute_reply.started":"2024-03-11T08:11:37.066365Z","shell.execute_reply":"2024-03-11T08:11:37.075186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred2 = model.predict_generator(test_generator2, steps=np.ceil(sample_count/15))\npred2","metadata":{"execution":{"iopub.status.busy":"2024-03-11T08:12:03.264668Z","iopub.execute_input":"2024-03-11T08:12:03.265644Z","iopub.status.idle":"2024-03-11T08:12:09.033755Z","shell.execute_reply.started":"2024-03-11T08:12:03.265611Z","shell.execute_reply":"2024-03-11T08:12:09.032917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#label_map = dict((v,k) for k,v in train_generator.class_indices.items())","metadata":{"execution":{"iopub.status.busy":"2024-03-11T07:27:57.902241Z","iopub.status.idle":"2024-03-11T07:27:57.902618Z","shell.execute_reply.started":"2024-03-11T07:27:57.902432Z","shell.execute_reply":"2024-03-11T07:27:57.902449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testData['category'] = testData['category'].replace({ 'dog': 1, 'cat': 0 })","metadata":{"execution":{"iopub.status.busy":"2024-03-11T08:12:25.225318Z","iopub.execute_input":"2024-03-11T08:12:25.226198Z","iopub.status.idle":"2024-03-11T08:12:25.231455Z","shell.execute_reply.started":"2024-03-11T08:12:25.226162Z","shell.execute_reply":"2024-03-11T08:12:25.230393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testData.shape","metadata":{"execution":{"iopub.status.busy":"2024-03-11T08:12:29.164611Z","iopub.execute_input":"2024-03-11T08:12:29.165335Z","iopub.status.idle":"2024-03-11T08:12:29.171192Z","shell.execute_reply.started":"2024-03-11T08:12:29.165305Z","shell.execute_reply":"2024-03-11T08:12:29.170289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission=testData.copy()","metadata":{"execution":{"iopub.status.busy":"2024-03-11T07:27:57.906908Z","iopub.status.idle":"2024-03-11T07:27:57.907254Z","shell.execute_reply.started":"2024-03-11T07:27:57.907090Z","shell.execute_reply":"2024-03-11T07:27:57.907106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['id']=submission['filename'].str.split('.').str[0]","metadata":{"execution":{"iopub.status.busy":"2024-03-11T07:27:57.909640Z","iopub.status.idle":"2024-03-11T07:27:57.910109Z","shell.execute_reply.started":"2024-03-11T07:27:57.909851Z","shell.execute_reply":"2024-03-11T07:27:57.909889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['label']=submission['category']\nsubmission.drop(['filename','category'],axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T07:27:57.911954Z","iopub.status.idle":"2024-03-11T07:27:57.912424Z","shell.execute_reply.started":"2024-03-11T07:27:57.912181Z","shell.execute_reply":"2024-03-11T07:27:57.912203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2024-03-11T07:27:57.913514Z","iopub.status.idle":"2024-03-11T07:27:57.913992Z","shell.execute_reply.started":"2024-03-11T07:27:57.913728Z","shell.execute_reply":"2024-03-11T07:27:57.913751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T07:27:57.915628Z","iopub.status.idle":"2024-03-11T07:27:57.916021Z","shell.execute_reply.started":"2024-03-11T07:27:57.915809Z","shell.execute_reply":"2024-03-11T07:27:57.915827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_Data.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-11T08:13:27.626531Z","iopub.execute_input":"2024-03-11T08:13:27.626950Z","iopub.status.idle":"2024-03-11T08:13:27.637302Z","shell.execute_reply.started":"2024-03-11T08:13:27.626911Z","shell.execute_reply":"2024-03-11T08:13:27.636315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_Data['category'] = np.argmax(pred2, axis=-1)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T08:13:45.525012Z","iopub.execute_input":"2024-03-11T08:13:45.526203Z","iopub.status.idle":"2024-03-11T08:13:45.531061Z","shell.execute_reply.started":"2024-03-11T08:13:45.526167Z","shell.execute_reply":"2024-03-11T08:13:45.530058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_Data.head(15)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T08:14:13.206141Z","iopub.execute_input":"2024-03-11T08:14:13.206837Z","iopub.status.idle":"2024-03-11T08:14:13.216347Z","shell.execute_reply.started":"2024-03-11T08:14:13.206808Z","shell.execute_reply":"2024-03-11T08:14:13.215343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testData.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-11T08:19:40.204541Z","iopub.execute_input":"2024-03-11T08:19:40.205112Z","iopub.status.idle":"2024-03-11T08:19:40.214255Z","shell.execute_reply.started":"2024-03-11T08:19:40.205083Z","shell.execute_reply":"2024-03-11T08:19:40.213302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_Data['category']=test_Data['category'].astype(str)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T08:24:14.245816Z","iopub.execute_input":"2024-03-11T08:24:14.246616Z","iopub.status.idle":"2024-03-11T08:24:14.254336Z","shell.execute_reply.started":"2024-03-11T08:24:14.246580Z","shell.execute_reply":"2024-03-11T08:24:14.253231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_Data.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-11T08:24:23.364698Z","iopub.execute_input":"2024-03-11T08:24:23.365086Z","iopub.status.idle":"2024-03-11T08:24:23.374929Z","shell.execute_reply.started":"2024-03-11T08:24:23.365056Z","shell.execute_reply":"2024-03-11T08:24:23.373918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_Data['filename']=test_Data['filename'].str.split('.').str[0]","metadata":{"execution":{"iopub.status.busy":"2024-03-11T08:30:33.267092Z","iopub.execute_input":"2024-03-11T08:30:33.267403Z","iopub.status.idle":"2024-03-11T08:30:33.278994Z","shell.execute_reply.started":"2024-03-11T08:30:33.267377Z","shell.execute_reply":"2024-03-11T08:30:33.277889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_Data.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-11T08:30:45.772163Z","iopub.execute_input":"2024-03-11T08:30:45.772800Z","iopub.status.idle":"2024-03-11T08:30:45.783050Z","shell.execute_reply.started":"2024-03-11T08:30:45.772768Z","shell.execute_reply":"2024-03-11T08:30:45.781871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_Data['filename'] = test_Data['filename'].replace({ 'dog':1 , 'cat':0  })","metadata":{"execution":{"iopub.status.busy":"2024-03-11T08:33:34.925556Z","iopub.execute_input":"2024-03-11T08:33:34.926287Z","iopub.status.idle":"2024-03-11T08:33:34.935058Z","shell.execute_reply.started":"2024-03-11T08:33:34.926254Z","shell.execute_reply":"2024-03-11T08:33:34.934176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_Data","metadata":{"execution":{"iopub.status.busy":"2024-03-11T08:33:37.304498Z","iopub.execute_input":"2024-03-11T08:33:37.305375Z","iopub.status.idle":"2024-03-11T08:33:37.316582Z","shell.execute_reply.started":"2024-03-11T08:33:37.305345Z","shell.execute_reply":"2024-03-11T08:33:37.315617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_Data.dtypes","metadata":{"execution":{"iopub.status.busy":"2024-03-11T08:39:59.244791Z","iopub.execute_input":"2024-03-11T08:39:59.245499Z","iopub.status.idle":"2024-03-11T08:39:59.252385Z","shell.execute_reply.started":"2024-03-11T08:39:59.245468Z","shell.execute_reply":"2024-03-11T08:39:59.251392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_Data=test_Data.astype(str)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T08:41:17.624770Z","iopub.execute_input":"2024-03-11T08:41:17.625129Z","iopub.status.idle":"2024-03-11T08:41:17.632398Z","shell.execute_reply.started":"2024-03-11T08:41:17.625102Z","shell.execute_reply":"2024-03-11T08:41:17.631402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cm=confusion_matrix(test_Data['filename'],test_Data['category'])","metadata":{"execution":{"iopub.status.busy":"2024-03-11T08:41:20.685527Z","iopub.execute_input":"2024-03-11T08:41:20.686137Z","iopub.status.idle":"2024-03-11T08:41:20.724706Z","shell.execute_reply.started":"2024-03-11T08:41:20.686105Z","shell.execute_reply":"2024-03-11T08:41:20.723820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Confuision Matrix","metadata":{}},{"cell_type":"code","source":"cm_df = pd.DataFrame(cm, index=['Actual 0', 'Actual 1'], columns=['Predicted 0', 'Predicted 1'])\nprint(\"Confusion Matrix:\")\nprint(cm_df)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T08:43:36.886703Z","iopub.execute_input":"2024-03-11T08:43:36.887424Z","iopub.status.idle":"2024-03-11T08:43:36.894765Z","shell.execute_reply.started":"2024-03-11T08:43:36.887380Z","shell.execute_reply":"2024-03-11T08:43:36.893671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Accuracy Score","metadata":{"execution":{"iopub.status.busy":"2024-03-11T08:42:04.764743Z","iopub.execute_input":"2024-03-11T08:42:04.765698Z","iopub.status.idle":"2024-03-11T08:42:04.771981Z","shell.execute_reply.started":"2024-03-11T08:42:04.765660Z","shell.execute_reply":"2024-03-11T08:42:04.770927Z"}}},{"cell_type":"code","source":"accuracy = accuracy_score(test_Data['filename'], test_Data['category'])","metadata":{"execution":{"iopub.status.busy":"2024-03-11T08:42:51.604654Z","iopub.execute_input":"2024-03-11T08:42:51.605378Z","iopub.status.idle":"2024-03-11T08:42:51.626580Z","shell.execute_reply.started":"2024-03-11T08:42:51.605349Z","shell.execute_reply":"2024-03-11T08:42:51.625553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy","metadata":{"execution":{"iopub.status.busy":"2024-03-11T08:42:59.585017Z","iopub.execute_input":"2024-03-11T08:42:59.585756Z","iopub.status.idle":"2024-03-11T08:42:59.592282Z","shell.execute_reply.started":"2024-03-11T08:42:59.585726Z","shell.execute_reply":"2024-03-11T08:42:59.590743Z"},"trusted":true},"execution_count":null,"outputs":[]}]}