{"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":"<center><img src=\"https://www.theladders.com/wp-content/uploads/dog-cat-190709-1000x563.jpg\" width=\"900\"></img></center>","metadata":{}},{"cell_type":"markdown","source":"# Import Libraries","metadata":{}},{"cell_type":"code","source":"from zipfile import ZipFile\nimport os\nimport matplotlib.pyplot as plt\nimport pandas as pd\nimport numpy as np\nimport cv2\nfrom sklearn.model_selection import train_test_split\nimport tensorflow as tf\nfrom tensorflow import keras\nimport pickle as pk\nfrom sklearn.metrics import confusion_matrix\nimport seaborn as sns\nfrom sklearn.metrics import classification_report","metadata":{"execution":{"iopub.status.busy":"2023-03-25T10:48:54.905466Z","iopub.execute_input":"2023-03-25T10:48:54.905849Z","iopub.status.idle":"2023-03-25T10:49:07.856328Z","shell.execute_reply.started":"2023-03-25T10:48:54.905814Z","shell.execute_reply":"2023-03-25T10:49:07.854937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Extracting The Compressed Dataset","metadata":{}},{"cell_type":"code","source":"train='/kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip'\ntest='/kaggle/input/dogs-vs-cats-redux-kernels-edition/test.zip'\nwith ZipFile(train,'r') as zip:\n    zip.extractall()\n    print('The train dataset is extracted')\nwith ZipFile(test,'r') as zip:\n    zip.extractall()\n    print('The test dataset is extracted')","metadata":{"execution":{"iopub.status.busy":"2023-03-25T10:49:07.859309Z","iopub.execute_input":"2023-03-25T10:49:07.861161Z","iopub.status.idle":"2023-03-25T10:49:32.382985Z","shell.execute_reply.started":"2023-03-25T10:49:07.861073Z","shell.execute_reply":"2023-03-25T10:49:32.380953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Printing The Name Of Images","metadata":{}},{"cell_type":"code","source":"train_names=os.listdir('/kaggle/working/train')\npd.DataFrame(train_names,columns=['Train Names'])","metadata":{"execution":{"iopub.status.busy":"2023-03-25T10:49:32.385863Z","iopub.execute_input":"2023-03-25T10:49:32.386830Z","iopub.status.idle":"2023-03-25T10:49:32.448426Z","shell.execute_reply.started":"2023-03-25T10:49:32.386774Z","shell.execute_reply":"2023-03-25T10:49:32.447539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Count Number Of Dog and Cat","metadata":{}},{"cell_type":"code","source":"dog_count=0\ncat_count=0\nfor name in train_names:\n    if name[0:3]=='dog':\n        dog_count+=1\n    else:\n        cat_count+=1\nprint('Number of dog images =', dog_count)\nprint('Number of cat images =', cat_count)","metadata":{"execution":{"iopub.status.busy":"2023-03-25T10:49:32.451758Z","iopub.execute_input":"2023-03-25T10:49:32.452672Z","iopub.status.idle":"2023-03-25T10:49:32.467494Z","shell.execute_reply.started":"2023-03-25T10:49:32.452618Z","shell.execute_reply":"2023-03-25T10:49:32.465880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Counting The Number Of Files ","metadata":{}},{"cell_type":"code","source":"train_file=next(os.walk('/kaggle/working/train'))\ntest_file=next(os.walk('/kaggle/working/test'))\nprint('Number of picture in train is :',len(train_file[-1]))\nprint('Number of picture in test is :',len(test_file[-1]))","metadata":{"execution":{"iopub.status.busy":"2023-03-25T10:49:32.469204Z","iopub.execute_input":"2023-03-25T10:49:32.469595Z","iopub.status.idle":"2023-03-25T10:49:32.532227Z","shell.execute_reply.started":"2023-03-25T10:49:32.469559Z","shell.execute_reply":"2023-03-25T10:49:32.530362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Size Of Images","metadata":{}},{"cell_type":"code","source":"path='/kaggle/working/train'\nsize=[]\nfor img in os.listdir(path):\n    img_array=cv2.imread(os.path.join(path,img),cv2.IMREAD_COLOR)\n    size.append(img_array.shape)\npd.DataFrame(pd.Series(size).value_counts(),columns=['Number'])      ","metadata":{"execution":{"iopub.status.busy":"2023-03-25T10:49:32.533687Z","iopub.execute_input":"2023-03-25T10:49:32.534188Z","iopub.status.idle":"2023-03-25T10:50:25.588331Z","shell.execute_reply.started":"2023-03-25T10:49:32.534130Z","shell.execute_reply":"2023-03-25T10:50:25.587008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#  Resize Image","metadata":{}},{"cell_type":"code","source":"IMG_SIZE = 224\nnew_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE),cv2.IMREAD_COLOR)\nplt.imshow(new_array)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-25T10:50:25.589888Z","iopub.execute_input":"2023-03-25T10:50:25.590417Z","iopub.status.idle":"2023-03-25T10:50:25.972147Z","shell.execute_reply.started":"2023-03-25T10:50:25.590365Z","shell.execute_reply":"2023-03-25T10:50:25.970818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Show Picture In Train","metadata":{}},{"cell_type":"code","source":"def process_data(path,flage):\n    data=[]\n    for img in os.listdir(path):\n            img_array=cv2.imread(os.path.join(path,img),cv2.IMREAD_COLOR)\n            new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))\n            if (flage):\n                data.append([new_array,img[0:3]])\n            else:\n                data.append(new_array)\n    return data            ","metadata":{"execution":{"iopub.status.busy":"2023-03-25T10:50:25.974443Z","iopub.execute_input":"2023-03-25T10:50:25.975493Z","iopub.status.idle":"2023-03-25T10:50:25.984915Z","shell.execute_reply.started":"2023-03-25T10:50:25.975429Z","shell.execute_reply":"2023-03-25T10:50:25.983325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_images(data,flage):\n    plt.figure(figsize=(15,15))\n    ii=1\n    for i,data in enumerate(data[:25]):\n        label = data[1]\n        img_data = data[0]\n        if(flage):\n            label=\"None\"\n        plt.subplot(5,5,ii)    \n        plt.imshow(img_data)\n        plt.axis('off')\n        plt.title(\"Label: {}\".format(label))\n        ii+=1","metadata":{"execution":{"iopub.status.busy":"2023-03-25T10:50:25.986901Z","iopub.execute_input":"2023-03-25T10:50:25.988732Z","iopub.status.idle":"2023-03-25T10:50:26.002318Z","shell.execute_reply.started":"2023-03-25T10:50:25.988672Z","shell.execute_reply":"2023-03-25T10:50:26.000796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path='/kaggle/working/train'\ntrain=process_data(path,True)\ntrain[0]","metadata":{"execution":{"iopub.status.busy":"2023-03-25T10:50:26.008561Z","iopub.execute_input":"2023-03-25T10:50:26.009614Z","iopub.status.idle":"2023-03-25T10:51:26.802932Z","shell.execute_reply.started":"2023-03-25T10:50:26.009552Z","shell.execute_reply":"2023-03-25T10:51:26.801615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_images(train,False)","metadata":{"execution":{"iopub.status.busy":"2023-03-25T10:51:26.804340Z","iopub.execute_input":"2023-03-25T10:51:26.804808Z","iopub.status.idle":"2023-03-25T10:51:28.728699Z","shell.execute_reply.started":"2023-03-25T10:51:26.804763Z","shell.execute_reply":"2023-03-25T10:51:28.726951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Show Picture In Test","metadata":{}},{"cell_type":"code","source":"path='/kaggle/working/test'\ntest=process_data(path,False)\ntest[0]","metadata":{"execution":{"iopub.status.busy":"2023-03-25T10:51:28.730259Z","iopub.execute_input":"2023-03-25T10:51:28.731238Z","iopub.status.idle":"2023-03-25T10:51:59.166321Z","shell.execute_reply.started":"2023-03-25T10:51:28.731189Z","shell.execute_reply":"2023-03-25T10:51:59.164979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,15))\nii=1\nfor img in test:\n    plt.subplot(5,5,ii)    \n    plt.imshow(img)\n    plt.axis('off')\n    plt.title(\"Label: {}\".format('None'))\n    if ii==25:\n        break\n    ii+=1","metadata":{"execution":{"iopub.status.busy":"2023-03-25T10:51:59.167812Z","iopub.execute_input":"2023-03-25T10:51:59.168174Z","iopub.status.idle":"2023-03-25T10:52:01.330209Z","shell.execute_reply.started":"2023-03-25T10:51:59.168141Z","shell.execute_reply":"2023-03-25T10:52:01.328692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Splitting Data","metadata":{}},{"cell_type":"code","source":"X = np.array([i[0] for i in train]).reshape(-1,IMG_SIZE,IMG_SIZE,3)\ny = np.array([1 if i[1]=='dog' else 0 for i in train])","metadata":{"execution":{"iopub.status.busy":"2023-03-25T10:52:01.332290Z","iopub.execute_input":"2023-03-25T10:52:01.333146Z","iopub.status.idle":"2023-03-25T10:52:02.914769Z","shell.execute_reply.started":"2023-03-25T10:52:01.333102Z","shell.execute_reply":"2023-03-25T10:52:02.913645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Building","metadata":{}},{"cell_type":"code","source":"model =keras.models.Sequential()\nmodel.add(keras.applications.ResNet50(include_top = False, pooling = 'max', weights = 'imagenet'))\nmodel.add(keras.layers.Dense(2, activation = 'softmax'))\nmodel.layers[0].trainable = False ","metadata":{"execution":{"iopub.status.busy":"2023-03-25T10:52:02.916244Z","iopub.execute_input":"2023-03-25T10:52:02.917026Z","iopub.status.idle":"2023-03-25T10:52:11.142250Z","shell.execute_reply.started":"2023-03-25T10:52:02.916985Z","shell.execute_reply":"2023-03-25T10:52:11.141110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-03-25T10:52:11.144920Z","iopub.execute_input":"2023-03-25T10:52:11.145482Z","iopub.status.idle":"2023-03-25T10:52:11.199764Z","shell.execute_reply.started":"2023-03-25T10:52:11.145430Z","shell.execute_reply":"2023-03-25T10:52:11.198218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.utils.plot_model(model, to_file='model.png', show_shapes=True, show_layer_names=True,show_dtype=True,dpi=120)","metadata":{"execution":{"iopub.status.busy":"2023-03-25T10:52:11.201303Z","iopub.execute_input":"2023-03-25T10:52:11.202706Z","iopub.status.idle":"2023-03-25T10:52:11.844629Z","shell.execute_reply.started":"2023-03-25T10:52:11.202659Z","shell.execute_reply":"2023-03-25T10:52:11.842149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer = 'adam', metrics = ['accuracy'], loss = 'sparse_categorical_crossentropy')","metadata":{"execution":{"iopub.status.busy":"2023-03-25T10:52:11.848136Z","iopub.execute_input":"2023-03-25T10:52:11.849774Z","iopub.status.idle":"2023-03-25T10:52:11.897380Z","shell.execute_reply.started":"2023-03-25T10:52:11.849713Z","shell.execute_reply":"2023-03-25T10:52:11.896080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hist=model.fit(X, y, epochs=5,validation_split=0.1)","metadata":{"execution":{"iopub.status.busy":"2023-03-25T10:52:11.899045Z","iopub.execute_input":"2023-03-25T10:52:11.899489Z","iopub.status.idle":"2023-03-25T14:13:19.048598Z","shell.execute_reply.started":"2023-03-25T10:52:11.899444Z","shell.execute_reply":"2023-03-25T14:13:19.046218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hist_=pd.DataFrame(hist.history)\nhist_","metadata":{"execution":{"iopub.status.busy":"2023-03-25T14:13:19.052780Z","iopub.execute_input":"2023-03-25T14:13:19.053291Z","iopub.status.idle":"2023-03-25T14:13:19.091686Z","shell.execute_reply.started":"2023-03-25T14:13:19.053218Z","shell.execute_reply":"2023-03-25T14:13:19.090066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20,5))\nplt.subplot(1,2,1)\nplt.plot(hist_['accuracy'],marker='*',c='r',label='accuracy')\nplt.plot(hist_['val_accuracy'],marker='o',c='g',label='val_accuracy')\nplt.title('Accuracy && Val_accuracy')\nplt.legend()\nplt.subplot(1,2,2)\nplt.plot(hist_['val_loss'],marker='*',c='r',label='val_loss')\nplt.plot(hist_['loss'],marker='o',c='g',label='loss')\nplt.title('Val_loss && LOSS')\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2023-03-25T14:47:32.446381Z","iopub.execute_input":"2023-03-25T14:47:32.446834Z","iopub.status.idle":"2023-03-25T14:47:32.965781Z","shell.execute_reply.started":"2023-03-25T14:47:32.446795Z","shell.execute_reply":"2023-03-25T14:47:32.964939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Save Model","metadata":{}},{"cell_type":"code","source":"model.save('/kaggle/working/cat_dog_model.model')","metadata":{"execution":{"iopub.status.busy":"2023-03-25T14:13:19.683662Z","iopub.execute_input":"2023-03-25T14:13:19.684027Z","iopub.status.idle":"2023-03-25T14:13:49.881883Z","shell.execute_reply.started":"2023-03-25T14:13:19.683991Z","shell.execute_reply":"2023-03-25T14:13:49.880696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prediction","metadata":{}},{"cell_type":"code","source":"new_model = tf.keras.models.load_model('/kaggle/working/cat_dog_model.model')","metadata":{"execution":{"iopub.status.busy":"2023-03-25T14:13:49.888827Z","iopub.execute_input":"2023-03-25T14:13:49.889771Z","iopub.status.idle":"2023-03-25T14:14:01.461153Z","shell.execute_reply.started":"2023-03-25T14:13:49.889717Z","shell.execute_reply":"2023-03-25T14:14:01.459672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test=np.array(test).reshape(-1,IMG_SIZE,IMG_SIZE,3)\ny_test=new_model.predict(test)","metadata":{"execution":{"iopub.status.busy":"2023-03-25T14:14:01.471344Z","iopub.execute_input":"2023-03-25T14:14:01.472566Z","iopub.status.idle":"2023-03-25T14:34:03.103759Z","shell.execute_reply.started":"2023-03-25T14:14:01.472518Z","shell.execute_reply":"2023-03-25T14:34:03.102375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred=[]\npred_name=[]\nfor row in y_test:\n    N=np.argmax(row)\n    pred.append(row[N])\n    if N==0:\n        pred_name.append('Cat')\n    else:\n        pred_name.append('Dog')\npd.DataFrame(pred)","metadata":{"execution":{"iopub.status.busy":"2023-03-25T14:34:03.106908Z","iopub.execute_input":"2023-03-25T14:34:03.107448Z","iopub.status.idle":"2023-03-25T14:34:03.168115Z","shell.execute_reply.started":"2023-03-25T14:34:03.107362Z","shell.execute_reply":"2023-03-25T14:34:03.166867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(pred_name)","metadata":{"execution":{"iopub.status.busy":"2023-03-25T14:34:03.170311Z","iopub.execute_input":"2023-03-25T14:34:03.170819Z","iopub.status.idle":"2023-03-25T14:34:03.185451Z","shell.execute_reply.started":"2023-03-25T14:34:03.170768Z","shell.execute_reply":"2023-03-25T14:34:03.184001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,15))\nii=1\nfor img in test:\n    plt.subplot(5,5,ii)    \n    plt.imshow(img)\n    plt.axis('off')\n    plt.title(\"Label: {}\".format(pred_name[ii-1]))\n    if ii==25:\n        break\n    ii+=1","metadata":{"execution":{"iopub.status.busy":"2023-03-25T14:34:03.187006Z","iopub.execute_input":"2023-03-25T14:34:03.187374Z","iopub.status.idle":"2023-03-25T14:34:05.245956Z","shell.execute_reply.started":"2023-03-25T14:34:03.187339Z","shell.execute_reply":"2023-03-25T14:34:05.244737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id=list(range(1,len(pred)+1))\ny_pred=pd.DataFrame(id,columns=['id'])\ny_pred['label']=pred\ny_pred.to_csv('/kaggle/working/p.csv')\ny_pred=pd.read_csv('/kaggle/working/p.csv',index_col='id')\ny_pred=y_pred.drop('Unnamed: 0',axis=1)\ny_pred.to_csv('/kaggle/working/prediction.csv')\ny_pred","metadata":{"execution":{"iopub.status.busy":"2023-03-25T14:34:05.251325Z","iopub.execute_input":"2023-03-25T14:34:05.251812Z","iopub.status.idle":"2023-03-25T14:34:05.366429Z","shell.execute_reply.started":"2023-03-25T14:34:05.251769Z","shell.execute_reply":"2023-03-25T14:34:05.365111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.get_weights()","metadata":{"execution":{"iopub.status.busy":"2023-03-25T14:34:28.871190Z","iopub.execute_input":"2023-03-25T14:34:28.871662Z","iopub.status.idle":"2023-03-25T14:34:29.614112Z","shell.execute_reply.started":"2023-03-25T14:34:28.871612Z","shell.execute_reply":"2023-03-25T14:34:29.612654Z"},"trusted":true},"execution_count":null,"outputs":[]}]}