{"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\n\nbase_dir = \"../input/dogs-vs-cats\"\n\nimport os,shutil","metadata":{"execution":{"iopub.status.busy":"2022-09-14T05:10:21.610001Z","iopub.execute_input":"2022-09-14T05:10:21.610442Z","iopub.status.idle":"2022-09-14T05:10:21.635489Z","shell.execute_reply.started":"2022-09-14T05:10:21.610358Z","shell.execute_reply":"2022-09-14T05:10:21.634639Z"},"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-09-14T05:10:21.647144Z","iopub.execute_input":"2022-09-14T05:10:21.647404Z","iopub.status.idle":"2022-09-14T05:10:37.564266Z","shell.execute_reply.started":"2022-09-14T05:10:21.647380Z","shell.execute_reply":"2022-09-14T05:10:37.563257Z"},"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-09-14T05:10:37.566168Z","iopub.execute_input":"2022-09-14T05:10:37.566566Z","iopub.status.idle":"2022-09-14T05:10:37.615612Z","shell.execute_reply.started":"2022-09-14T05:10:37.566529Z","shell.execute_reply":"2022-09-14T05:10:37.614621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(data)","metadata":{"execution":{"iopub.status.busy":"2022-09-14T05:10:37.617049Z","iopub.execute_input":"2022-09-14T05:10:37.617400Z","iopub.status.idle":"2022-09-14T05:10:37.623955Z","shell.execute_reply.started":"2022-09-14T05:10:37.617366Z","shell.execute_reply":"2022-09-14T05:10:37.622853Z"},"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-09-14T05:10:37.626759Z","iopub.execute_input":"2022-09-14T05:10:37.627114Z","iopub.status.idle":"2022-09-14T05:10:37.637830Z","shell.execute_reply.started":"2022-09-14T05:10:37.627080Z","shell.execute_reply":"2022-09-14T05:10:37.636762Z"},"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-09-14T05:10:37.639467Z","iopub.execute_input":"2022-09-14T05:10:37.640056Z","iopub.status.idle":"2022-09-14T05:10:37.649262Z","shell.execute_reply.started":"2022-09-14T05:10:37.639992Z","shell.execute_reply":"2022-09-14T05:10:37.648131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n","metadata":{"execution":{"iopub.status.busy":"2022-09-14T05:10:37.651044Z","iopub.execute_input":"2022-09-14T05:10:37.651374Z","iopub.status.idle":"2022-09-14T05:10:43.620281Z","shell.execute_reply.started":"2022-09-14T05:10:37.651343Z","shell.execute_reply":"2022-09-14T05:10:43.619242Z"},"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-09-14T05:10:43.622078Z","iopub.execute_input":"2022-09-14T05:10:43.622752Z","iopub.status.idle":"2022-09-14T05:10:43.632936Z","shell.execute_reply.started":"2022-09-14T05:10:43.622713Z","shell.execute_reply":"2022-09-14T05:10:43.630835Z"},"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=32)","metadata":{"execution":{"iopub.status.busy":"2022-09-14T05:10:43.634400Z","iopub.execute_input":"2022-09-14T05:10:43.634762Z","iopub.status.idle":"2022-09-14T05:10:44.948026Z","shell.execute_reply.started":"2022-09-14T05:10:43.634728Z","shell.execute_reply":"2022-09-14T05:10:44.946987Z"},"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=32,)","metadata":{"execution":{"iopub.status.busy":"2022-09-14T05:10:44.949354Z","iopub.execute_input":"2022-09-14T05:10:44.950031Z","iopub.status.idle":"2022-09-14T05:10:45.040588Z","shell.execute_reply.started":"2022-09-14T05:10:44.949995Z","shell.execute_reply":"2022-09-14T05:10:45.039532Z"},"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\nfrom keras.models import load_model","metadata":{"execution":{"iopub.status.busy":"2022-09-14T05:10:45.047190Z","iopub.execute_input":"2022-09-14T05:10:45.047611Z","iopub.status.idle":"2022-09-14T05:10:45.640996Z","shell.execute_reply.started":"2022-09-14T05:10:45.047576Z","shell.execute_reply":"2022-09-14T05:10:45.640000Z"},"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-09-14T05:10:45.643504Z","iopub.execute_input":"2022-09-14T05:10:45.644197Z","iopub.status.idle":"2022-09-14T05:10:48.359187Z","shell.execute_reply.started":"2022-09-14T05:10:45.644159Z","shell.execute_reply":"2022-09-14T05:10:48.357932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-09-14T05:10:48.360717Z","iopub.execute_input":"2022-09-14T05:10:48.361533Z","iopub.status.idle":"2022-09-14T05:10:48.370017Z","shell.execute_reply.started":"2022-09-14T05:10:48.361475Z","shell.execute_reply":"2022-09-14T05:10:48.368771Z"},"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-09-14T05:10:48.371709Z","iopub.execute_input":"2022-09-14T05:10:48.372222Z","iopub.status.idle":"2022-09-14T05:10:48.386561Z","shell.execute_reply.started":"2022-09-14T05:10:48.372184Z","shell.execute_reply":"2022-09-14T05:10:48.385696Z"},"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-09-14T05:10:48.389464Z","iopub.execute_input":"2022-09-14T05:10:48.389743Z","iopub.status.idle":"2022-09-14T05:36:32.934221Z","shell.execute_reply.started":"2022-09-14T05:10:48.389718Z","shell.execute_reply":"2022-09-14T05:36:32.933246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.makedirs(\"./Saved_Models\")","metadata":{"execution":{"iopub.status.busy":"2022-09-14T05:36:32.935860Z","iopub.execute_input":"2022-09-14T05:36:32.936223Z","iopub.status.idle":"2022-09-14T05:36:32.941442Z","shell.execute_reply.started":"2022-09-14T05:36:32.936187Z","shell.execute_reply":"2022-09-14T05:36:32.940447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import load_model\n\nmodel.save('./Saved_Models/model_cat_dog.h5') ","metadata":{"execution":{"iopub.status.busy":"2022-09-14T05:36:32.943282Z","iopub.execute_input":"2022-09-14T05:36:32.943972Z","iopub.status.idle":"2022-09-14T05:36:33.056139Z","shell.execute_reply.started":"2022-09-14T05:36:32.943935Z","shell.execute_reply":"2022-09-14T05:36:33.055031Z"},"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-09-14T05:36:33.057497Z","iopub.execute_input":"2022-09-14T05:36:33.057860Z","iopub.status.idle":"2022-09-14T05:36:33.294648Z","shell.execute_reply.started":"2022-09-14T05:36:33.057827Z","shell.execute_reply":"2022-09-14T05:36:33.293629Z"},"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-09-14T05:36:33.296067Z","iopub.execute_input":"2022-09-14T05:36:33.296693Z","iopub.status.idle":"2022-09-14T05:36:33.674637Z","shell.execute_reply.started":"2022-09-14T05:36:33.296655Z","shell.execute_reply":"2022-09-14T05:36:33.673636Z"},"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-09-14T05:36:33.676051Z","iopub.execute_input":"2022-09-14T05:36:33.676688Z","iopub.status.idle":"2022-09-14T05:36:33.683446Z","shell.execute_reply.started":"2022-09-14T05:36:33.676649Z","shell.execute_reply":"2022-09-14T05:36:33.682177Z"},"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=32,)","metadata":{"execution":{"iopub.status.busy":"2022-09-14T05:59:15.406930Z","iopub.execute_input":"2022-09-14T05:59:15.407293Z","iopub.status.idle":"2022-09-14T05:59:15.566877Z","shell.execute_reply.started":"2022-09-14T05:59:15.407263Z","shell.execute_reply":"2022-09-14T05:59:15.565807Z"},"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-09-14T05:59:18.365509Z","iopub.execute_input":"2022-09-14T05:59:18.366172Z","iopub.status.idle":"2022-09-14T05:59:18.436494Z","shell.execute_reply.started":"2022-09-14T05:59:18.366136Z","shell.execute_reply":"2022-09-14T05:59:18.435580Z"},"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-09-14T05:59:21.728558Z","iopub.execute_input":"2022-09-14T05:59:21.729013Z","iopub.status.idle":"2022-09-14T05:59:21.739524Z","shell.execute_reply.started":"2022-09-14T05:59:21.728974Z","shell.execute_reply":"2022-09-14T05:59:21.738536Z"},"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-09-14T05:59:25.111765Z","iopub.execute_input":"2022-09-14T05:59:25.112125Z","iopub.status.idle":"2022-09-14T06:53:07.923690Z","shell.execute_reply.started":"2022-09-14T05:59:25.112094Z","shell.execute_reply":"2022-09-14T06:53:07.922706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import load_model\n\nmodel.save('./Saved_Models/model_cat_dog_aug.h5') ","metadata":{"execution":{"iopub.status.busy":"2022-09-14T06:53:17.661921Z","iopub.execute_input":"2022-09-14T06:53:17.662284Z","iopub.status.idle":"2022-09-14T06:53:17.756301Z","shell.execute_reply.started":"2022-09-14T06:53:17.662254Z","shell.execute_reply":"2022-09-14T06:53:17.755207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!zip -r saved_models.zip \"./Saved_Models\"","metadata":{"execution":{"iopub.status.busy":"2022-09-14T06:53:55.989438Z","iopub.execute_input":"2022-09-14T06:53:55.990242Z","iopub.status.idle":"2022-09-14T06:54:00.804647Z","shell.execute_reply.started":"2022-09-14T06:53:55.990203Z","shell.execute_reply":"2022-09-14T06:54:00.803425Z"},"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-09-14T06:54:16.095452Z","iopub.execute_input":"2022-09-14T06:54:16.096600Z","iopub.status.idle":"2022-09-14T06:54:16.296404Z","shell.execute_reply.started":"2022-09-14T06:54:16.096546Z","shell.execute_reply":"2022-09-14T06:54:16.295443Z"},"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":{"execution":{"iopub.status.busy":"2022-09-14T06:54:21.292375Z","iopub.execute_input":"2022-09-14T06:54:21.292742Z","iopub.status.idle":"2022-09-14T06:54:21.322158Z","shell.execute_reply.started":"2022-09-14T06:54:21.292711Z","shell.execute_reply":"2022-09-14T06:54:21.321248Z"},"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-09-14T06:54:25.120099Z","iopub.execute_input":"2022-09-14T06:54:25.120793Z","iopub.status.idle":"2022-09-14T06:54:25.325838Z","shell.execute_reply.started":"2022-09-14T06:54:25.120754Z","shell.execute_reply":"2022-09-14T06:54:25.324846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}