{"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","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\nbase_dir = \"../input/dogs-vs-cats\"\n\nimport os,shutil","metadata":{"execution":{"iopub.status.busy":"2022-08-20T09:21:50.864693Z","iopub.execute_input":"2022-08-20T09:21:50.865639Z","iopub.status.idle":"2022-08-20T09:21:50.872219Z","shell.execute_reply.started":"2022-08-20T09:21:50.865598Z","shell.execute_reply":"2022-08-20T09:21:50.869202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_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-20T09:21:55.944003Z","iopub.execute_input":"2022-08-20T09:21:55.944923Z","iopub.status.idle":"2022-08-20T09:22:12.765749Z","shell.execute_reply.started":"2022-08-20T09:21:55.944862Z","shell.execute_reply":"2022-08-20T09:22:12.764770Z"},"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-20T09:22:12.767519Z","iopub.execute_input":"2022-08-20T09:22:12.767853Z","iopub.status.idle":"2022-08-20T09:22:12.820592Z","shell.execute_reply.started":"2022-08-20T09:22:12.767825Z","shell.execute_reply":"2022-08-20T09:22:12.819607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = data.iloc[0:2000,:]\n\ntrain_data.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-20T09:22:50.525478Z","iopub.execute_input":"2022-08-20T09:22:50.525850Z","iopub.status.idle":"2022-08-20T09:22:50.533466Z","shell.execute_reply.started":"2022-08-20T09:22:50.525818Z","shell.execute_reply":"2022-08-20T09:22:50.532422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation_data = data.iloc[2000:2500,:]\n\nvalidation_data.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-20T09:22:54.003768Z","iopub.execute_input":"2022-08-20T09:22:54.004832Z","iopub.status.idle":"2022-08-20T09:22:54.013259Z","shell.execute_reply.started":"2022-08-20T09:22:54.004787Z","shell.execute_reply":"2022-08-20T09:22:54.011942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2022-08-20T09:22:56.985693Z","iopub.execute_input":"2022-08-20T09:22:56.986377Z","iopub.status.idle":"2022-08-20T09:23:02.552728Z","shell.execute_reply.started":"2022-08-20T09:22:56.986339Z","shell.execute_reply":"2022-08-20T09:23:02.551739Z"},"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-20T09:23:02.554528Z","iopub.execute_input":"2022-08-20T09:23:02.555227Z","iopub.status.idle":"2022-08-20T09:23:02.564250Z","shell.execute_reply.started":"2022-08-20T09:23:02.555180Z","shell.execute_reply":"2022-08-20T09:23:02.562675Z"},"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-20T09:23:05.498846Z","iopub.execute_input":"2022-08-20T09:23:05.499993Z","iopub.status.idle":"2022-08-20T09:23:05.533122Z","shell.execute_reply.started":"2022-08-20T09:23:05.499947Z","shell.execute_reply":"2022-08-20T09:23:05.532076Z"},"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-20T09:23:09.113987Z","iopub.execute_input":"2022-08-20T09:23:09.114379Z","iopub.status.idle":"2022-08-20T09:23:09.131189Z","shell.execute_reply.started":"2022-08-20T09:23:09.114346Z","shell.execute_reply":"2022-08-20T09:23:09.130176Z"},"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-20T09:23:12.439420Z","iopub.execute_input":"2022-08-20T09:23:12.440166Z","iopub.status.idle":"2022-08-20T09:23:13.053625Z","shell.execute_reply.started":"2022-08-20T09:23:12.440131Z","shell.execute_reply":"2022-08-20T09:23:13.052676Z"},"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-20T09:23:16.968755Z","iopub.execute_input":"2022-08-20T09:23:16.969316Z","iopub.status.idle":"2022-08-20T09:23:19.968536Z","shell.execute_reply.started":"2022-08-20T09:23:16.969278Z","shell.execute_reply":"2022-08-20T09:23:19.967534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-08-20T09:23:22.544279Z","iopub.execute_input":"2022-08-20T09:23:22.545358Z","iopub.status.idle":"2022-08-20T09:23:22.555021Z","shell.execute_reply.started":"2022-08-20T09:23:22.545312Z","shell.execute_reply":"2022-08-20T09:23:22.553933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='binary_crossentropy',\n              optimizer='adam',\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-08-20T09:23:25.673996Z","iopub.execute_input":"2022-08-20T09:23:25.675026Z","iopub.status.idle":"2022-08-20T09:23:25.690567Z","shell.execute_reply.started":"2022-08-20T09:23:25.674981Z","shell.execute_reply":"2022-08-20T09:23:25.689553Z"},"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-20T09:23:28.713862Z","iopub.execute_input":"2022-08-20T09:23:28.714229Z","iopub.status.idle":"2022-08-20T09:26:22.961363Z","shell.execute_reply.started":"2022-08-20T09:23:28.714198Z","shell.execute_reply":"2022-08-20T09:26:22.960357Z"},"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-20T09:26:22.963404Z","iopub.execute_input":"2022-08-20T09:26:22.963787Z","iopub.status.idle":"2022-08-20T09:26:23.061052Z","shell.execute_reply.started":"2022-08-20T09:26:22.963738Z","shell.execute_reply":"2022-08-20T09:26:23.060073Z"},"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-20T09:26:32.469824Z","iopub.execute_input":"2022-08-20T09:26:32.470850Z","iopub.status.idle":"2022-08-20T09:26:32.705596Z","shell.execute_reply.started":"2022-08-20T09:26:32.470803Z","shell.execute_reply":"2022-08-20T09:26:32.704704Z"},"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-20T09:26:35.813987Z","iopub.execute_input":"2022-08-20T09:26:35.814674Z","iopub.status.idle":"2022-08-20T09:26:36.183328Z","shell.execute_reply.started":"2022-08-20T09:26:35.814637Z","shell.execute_reply":"2022-08-20T09:26:36.182270Z"},"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)","metadata":{"execution":{"iopub.status.busy":"2022-08-20T09:35:57.549974Z","iopub.execute_input":"2022-08-20T09:35:57.550708Z","iopub.status.idle":"2022-08-20T09:35:57.557157Z","shell.execute_reply.started":"2022-08-20T09:35:57.550669Z","shell.execute_reply":"2022-08-20T09:35:57.556149Z"},"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-20T09:36:00.833852Z","iopub.execute_input":"2022-08-20T09:36:00.834525Z","iopub.status.idle":"2022-08-20T09:36:00.864289Z","shell.execute_reply.started":"2022-08-20T09:36:00.834490Z","shell.execute_reply":"2022-08-20T09:36:00.863208Z"},"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-20T09:36:04.333866Z","iopub.execute_input":"2022-08-20T09:36:04.334547Z","iopub.status.idle":"2022-08-20T09:36:04.406278Z","shell.execute_reply.started":"2022-08-20T09:36:04.334509Z","shell.execute_reply":"2022-08-20T09:36:04.405385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='binary_crossentropy',\n              optimizer='adam',\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-08-20T09:36:08.689005Z","iopub.execute_input":"2022-08-20T09:36:08.689605Z","iopub.status.idle":"2022-08-20T09:36:08.699960Z","shell.execute_reply.started":"2022-08-20T09:36:08.689566Z","shell.execute_reply":"2022-08-20T09:36:08.698954Z"},"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-20T09:36:11.113787Z","iopub.execute_input":"2022-08-20T09:36:11.114477Z","iopub.status.idle":"2022-08-20T09:43:46.416925Z","shell.execute_reply.started":"2022-08-20T09:36:11.114437Z","shell.execute_reply":"2022-08-20T09:43:46.416032Z"},"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-20T09:44:54.410833Z","iopub.execute_input":"2022-08-20T09:44:54.411462Z","iopub.status.idle":"2022-08-20T09:44:54.519868Z","shell.execute_reply.started":"2022-08-20T09:44:54.411425Z","shell.execute_reply":"2022-08-20T09:44:54.518917Z"},"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-20T09:44:57.303671Z","iopub.execute_input":"2022-08-20T09:44:57.304047Z","iopub.status.idle":"2022-08-20T09:44:57.501889Z","shell.execute_reply.started":"2022-08-20T09:44:57.304015Z","shell.execute_reply":"2022-08-20T09:44:57.500960Z"},"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-08-20T09:45:02.773886Z","iopub.execute_input":"2022-08-20T09:45:02.774619Z","iopub.status.idle":"2022-08-20T09:45:02.802664Z","shell.execute_reply.started":"2022-08-20T09:45:02.774582Z","shell.execute_reply":"2022-08-20T09:45:02.801815Z"},"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-20T09:45:05.364157Z","iopub.execute_input":"2022-08-20T09:45:05.364761Z","iopub.status.idle":"2022-08-20T09:45:05.567066Z","shell.execute_reply.started":"2022-08-20T09:45:05.364724Z","shell.execute_reply":"2022-08-20T09:45:05.566126Z"},"trusted":true},"execution_count":null,"outputs":[]}]}