{"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":"import os\n\nbase_dir = \"../input/dogs-vs-cats\"\n\nimport os,shutil","metadata":{"execution":{"iopub.status.busy":"2022-08-20T07:12:02.197707Z","iopub.execute_input":"2022-08-20T07:12:02.199175Z","iopub.status.idle":"2022-08-20T07:12:02.239181Z","shell.execute_reply.started":"2022-08-20T07:12:02.198969Z","shell.execute_reply":"2022-08-20T07:12:02.237825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntrain_dir = os.path.join(base_dir, \"train.zip\")\ntest_dir = os.path.join(base_dir, \"test1.zip\")\n\nimport zipfile\n\nwith zipfile.ZipFile(train_dir,\"r\") as z:\n    \n    z.extractall()\n\nwith zipfile.ZipFile(test_dir,\"r\") as z:\n    \n    z.extractall()","metadata":{"execution":{"iopub.status.busy":"2022-08-20T07:12:20.974509Z","iopub.execute_input":"2022-08-20T07:12:20.975147Z","iopub.status.idle":"2022-08-20T07:12:41.274903Z","shell.execute_reply.started":"2022-08-20T07:12:20.975096Z","shell.execute_reply":"2022-08-20T07:12:41.273202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\n\npics = os.listdir(\"./train\")\n\ndata = pd.DataFrame(pics)\n\ndata = data.rename(columns={0:'image'})\ndata['image'] = data['image'].apply(lambda x: './train/' + x)\ndata['label'] = data['image'].apply(lambda x: 'cat' if 'cat' in x else 'dog')\n\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-20T07:13:45.374968Z","iopub.execute_input":"2022-08-20T07:13:45.375460Z","iopub.status.idle":"2022-08-20T07:13:45.439379Z","shell.execute_reply.started":"2022-08-20T07:13:45.375425Z","shell.execute_reply":"2022-08-20T07:13:45.438274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(data)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T09:38:47.297552Z","iopub.execute_input":"2022-08-12T09:38:47.29804Z","iopub.status.idle":"2022-08-12T09:38:47.305824Z","shell.execute_reply.started":"2022-08-12T09:38:47.298003Z","shell.execute_reply":"2022-08-12T09:38:47.304655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = data.iloc[0:18750,:]\n\ntrain_data.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-12T09:47:51.346583Z","iopub.execute_input":"2022-08-12T09:47:51.34732Z","iopub.status.idle":"2022-08-12T09:47:51.355558Z","shell.execute_reply.started":"2022-08-12T09:47:51.347256Z","shell.execute_reply":"2022-08-12T09:47:51.354135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation_data = data.iloc[18750:,:]\n\nvalidation_data.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-12T09:47:54.979595Z","iopub.execute_input":"2022-08-12T09:47:54.980033Z","iopub.status.idle":"2022-08-12T09:47:54.987948Z","shell.execute_reply.started":"2022-08-12T09:47:54.979995Z","shell.execute_reply":"2022-08-12T09:47:54.987128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2022-08-12T09:48:00.545777Z","iopub.execute_input":"2022-08-12T09:48:00.546205Z","iopub.status.idle":"2022-08-12T09:48:00.551458Z","shell.execute_reply.started":"2022-08-12T09:48:00.546169Z","shell.execute_reply":"2022-08-12T09:48:00.550253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen =  ImageDataGenerator(rescale=1/255.)\nvalidation_datagen =  ImageDataGenerator(rescale=1/255.)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T09:48:03.588356Z","iopub.execute_input":"2022-08-12T09:48:03.588779Z","iopub.status.idle":"2022-08-12T09:48:03.594338Z","shell.execute_reply.started":"2022-08-12T09:48:03.588744Z","shell.execute_reply":"2022-08-12T09:48:03.593356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_genarator = train_datagen.flow_from_dataframe(train_data,target_size=(150,150),\n                                                     x_col='image',y_col='label',class_mode='binary',batch_size=20)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T09:49:21.604152Z","iopub.execute_input":"2022-08-12T09:49:21.60456Z","iopub.status.idle":"2022-08-12T09:49:21.841605Z","shell.execute_reply.started":"2022-08-12T09:49:21.604528Z","shell.execute_reply":"2022-08-12T09:49:21.840329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation_genarator = train_datagen.flow_from_dataframe(validation_data,target_size=(150,150),\n                                                         x_col='image',y_col='label',class_mode='binary',batch_size=20)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T09:49:45.456824Z","iopub.execute_input":"2022-08-12T09:49:45.457308Z","iopub.status.idle":"2022-08-12T09:49:45.533205Z","shell.execute_reply.started":"2022-08-12T09:49:45.457256Z","shell.execute_reply":"2022-08-12T09:49:45.532282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf \nfrom tensorflow import keras\n\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten\nfrom keras.layers import Conv2D, MaxPooling2D\nfrom keras.preprocessing import image\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom keras import optimizers\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.utils import to_categorical\nfrom tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2022-08-12T09:51:28.128799Z","iopub.execute_input":"2022-08-12T09:51:28.129494Z","iopub.status.idle":"2022-08-12T09:51:28.792862Z","shell.execute_reply.started":"2022-08-12T09:51:28.129452Z","shell.execute_reply":"2022-08-12T09:51:28.791664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(32, kernel_size=(3,3), input_shape=(150, 150, 3), activation='relu'))\nmodel.add(MaxPooling2D(pool_size=2))\nmodel.add(Conv2D(64, kernel_size=(3,3), activation = 'relu'))\nmodel.add(MaxPooling2D(pool_size=2))\nmodel.add(Conv2D(128, kernel_size=(3,3), activation = 'relu'))\nmodel.add(MaxPooling2D(pool_size=2))\nmodel.add(Conv2D(128, kernel_size=(3,3), activation = 'relu'))\nmodel.add(MaxPooling2D(pool_size=2))\n\nmodel.add(Flatten())\nmodel.add(Dense(512, activation='relu'))\n\nmodel.add(Dense(1, activation='sigmoid'))","metadata":{"execution":{"iopub.status.busy":"2022-08-12T09:51:32.023575Z","iopub.execute_input":"2022-08-12T09:51:32.023978Z","iopub.status.idle":"2022-08-12T09:51:32.268341Z","shell.execute_reply.started":"2022-08-12T09:51:32.023944Z","shell.execute_reply":"2022-08-12T09:51:32.267121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T09:51:36.302558Z","iopub.execute_input":"2022-08-12T09:51:36.303136Z","iopub.status.idle":"2022-08-12T09:51:36.31141Z","shell.execute_reply.started":"2022-08-12T09:51:36.30309Z","shell.execute_reply":"2022-08-12T09:51:36.309944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='binary_crossentropy',\n              optimizer='adam',\n              metrics=['accuracy'])\n             ","metadata":{"execution":{"iopub.status.busy":"2022-08-12T09:51:44.258208Z","iopub.execute_input":"2022-08-12T09:51:44.258611Z","iopub.status.idle":"2022-08-12T09:51:44.275153Z","shell.execute_reply.started":"2022-08-12T09:51:44.258581Z","shell.execute_reply":"2022-08-12T09:51:44.274308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_genarator,epochs=25,validation_data= validation_genarator,workers=4,)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T09:52:42.901545Z","iopub.execute_input":"2022-08-12T09:52:42.902021Z","iopub.status.idle":"2022-08-12T12:39:02.077582Z","shell.execute_reply.started":"2022-08-12T09:52:42.90198Z","shell.execute_reply":"2022-08-12T12:39:02.076286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import load_model\n\nmodel.save('model_cat_dog.h5') ","metadata":{"execution":{"iopub.status.busy":"2022-08-12T12:39:19.463263Z","iopub.execute_input":"2022-08-12T12:39:19.464165Z","iopub.status.idle":"2022-08-12T12:39:19.586738Z","shell.execute_reply.started":"2022-08-12T12:39:19.46409Z","shell.execute_reply":"2022-08-12T12:39:19.585431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_df = pd.DataFrame(history.history)\nhistory_df.loc[:, ['loss', 'val_loss']].plot();\nprint(\"Minimum validation loss: {}\".format(history_df['val_loss'].min()))","metadata":{"execution":{"iopub.status.busy":"2022-08-12T12:39:22.927567Z","iopub.execute_input":"2022-08-12T12:39:22.928339Z","iopub.status.idle":"2022-08-12T12:39:23.154963Z","shell.execute_reply.started":"2022-08-12T12:39:22.928299Z","shell.execute_reply":"2022-08-12T12:39:23.154121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_df.loc[:, ['accuracy', 'val_accuracy']].plot();\nprint(\"Maximum accuracy : {}\".format(history_df['val_accuracy'].max()))","metadata":{"execution":{"iopub.status.busy":"2022-08-12T12:39:29.383484Z","iopub.execute_input":"2022-08-12T12:39:29.383959Z","iopub.status.idle":"2022-08-12T12:39:29.557507Z","shell.execute_reply.started":"2022-08-12T12:39:29.383918Z","shell.execute_reply":"2022-08-12T12:39:29.556448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Data_Augmentation","metadata":{}},{"cell_type":"code","source":"train_datagen_aug = ImageDataGenerator(\n    featurewise_center=False, samplewise_center=False,\n    featurewise_std_normalization=False, samplewise_std_normalization=False,\n    zca_whitening=False, zca_epsilon=1e-6, rotation_range=40,\n    width_shift_range=0.2, height_shift_range=0.2, brightness_range=None, \n    shear_range=0.2, zoom_range=0.2, channel_shift_range=0., fill_mode='nearest',\n    cval=0., horizontal_flip=True, vertical_flip=False, rescale=1/255., \n    preprocessing_function=None, data_format=None, validation_split=0.0, dtype=None)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-12T12:39:44.755431Z","iopub.execute_input":"2022-08-12T12:39:44.756618Z","iopub.status.idle":"2022-08-12T12:39:44.763599Z","shell.execute_reply.started":"2022-08-12T12:39:44.756564Z","shell.execute_reply":"2022-08-12T12:39:44.762671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_genarator = train_datagen_aug.flow_from_dataframe(train_data,target_size=(150,150),\n                                                     x_col='image',y_col='label',class_mode='binary',batch_size=20,)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T12:39:48.134167Z","iopub.execute_input":"2022-08-12T12:39:48.134603Z","iopub.status.idle":"2022-08-12T12:39:48.32001Z","shell.execute_reply.started":"2022-08-12T12:39:48.134565Z","shell.execute_reply":"2022-08-12T12:39:48.31879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(32, kernel_size=(3,3), input_shape=(150, 150, 3), activation='relu'))\nmodel.add(MaxPooling2D(pool_size=2))\nmodel.add(Conv2D(64, kernel_size=(3,3), activation = 'relu'))\nmodel.add(MaxPooling2D(pool_size=2))\nmodel.add(Conv2D(128, kernel_size=(3,3), activation = 'relu'))\nmodel.add(MaxPooling2D(pool_size=2))\nmodel.add(Conv2D(128, kernel_size=(3,3), activation = 'relu'))\nmodel.add(MaxPooling2D(pool_size=2))\n\nmodel.add(Flatten())\nmodel.add(Dropout(0.50))\nmodel.add(Dense(512, activation='relu'))\n\nmodel.add(Dense(1, activation='sigmoid'))","metadata":{"execution":{"iopub.status.busy":"2022-08-12T12:39:53.079795Z","iopub.execute_input":"2022-08-12T12:39:53.08017Z","iopub.status.idle":"2022-08-12T12:39:53.183819Z","shell.execute_reply.started":"2022-08-12T12:39:53.080137Z","shell.execute_reply":"2022-08-12T12:39:53.182641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='binary_crossentropy',\n              optimizer='adam',\n              metrics=['accuracy'])\n             ","metadata":{"execution":{"iopub.status.busy":"2022-08-12T12:39:56.445435Z","iopub.execute_input":"2022-08-12T12:39:56.445882Z","iopub.status.idle":"2022-08-12T12:39:56.457763Z","shell.execute_reply.started":"2022-08-12T12:39:56.445843Z","shell.execute_reply":"2022-08-12T12:39:56.456431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_genarator,epochs=25,validation_data= validation_genarator,workers=4,)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T12:40:00.228609Z","iopub.execute_input":"2022-08-12T12:40:00.229049Z","iopub.status.idle":"2022-08-12T15:38:48.751914Z","shell.execute_reply.started":"2022-08-12T12:40:00.229014Z","shell.execute_reply":"2022-08-12T15:38:48.749763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import load_model\n\nmodel.save('model_cat_dog_aug.h5') ","metadata":{"execution":{"iopub.status.busy":"2022-08-12T15:54:05.014949Z","iopub.execute_input":"2022-08-12T15:54:05.015466Z","iopub.status.idle":"2022-08-12T15:54:05.157891Z","shell.execute_reply.started":"2022-08-12T15:54:05.015427Z","shell.execute_reply":"2022-08-12T15:54:05.156796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_df = pd.DataFrame(history.history)\nhistory_df.loc[:, ['loss', 'val_loss']].plot();\nprint(\"Minimum validation loss: {}\".format(history_df['val_loss'].min()))","metadata":{"execution":{"iopub.status.busy":"2022-08-12T15:58:18.898798Z","iopub.execute_input":"2022-08-12T15:58:18.899226Z","iopub.status.idle":"2022-08-12T15:58:19.129655Z","shell.execute_reply.started":"2022-08-12T15:58:18.89919Z","shell.execute_reply":"2022-08-12T15:58:19.128347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pics_test = os.listdir(\"./test1\")\n\ndata_test = pd.DataFrame(pics)\n\ndata_test = data_test.rename(columns={0:'image'})\ndata_test['image'] = data_test['image'].apply(lambda x: './test1/' + x)\ndata_test['label'] = data_test['image'].apply(lambda x: 'cat' if 'cat' in x else 'dog').astype('str')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_df.loc[:, ['accuracy', 'val_accuracy']].plot();\nprint(\"Maximum accuracy : {}\".format(history_df['val_accuracy'].max()))","metadata":{"execution":{"iopub.status.busy":"2022-08-12T15:58:42.388338Z","iopub.execute_input":"2022-08-12T15:58:42.38879Z","iopub.status.idle":"2022-08-12T15:58:42.619428Z","shell.execute_reply.started":"2022-08-12T15:58:42.388754Z","shell.execute_reply":"2022-08-12T15:58:42.618308Z"},"trusted":true},"execution_count":null,"outputs":[]}]}