{"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","execution":{"iopub.status.busy":"2023-06-26T11:49:49.287171Z","iopub.execute_input":"2023-06-26T11:49:49.287834Z","iopub.status.idle":"2023-06-26T11:49:49.303007Z","shell.execute_reply.started":"2023-06-26T11:49:49.287801Z","shell.execute_reply":"2023-06-26T11:49:49.302023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport random\nimport os\nimport zipfile\nimport seaborn as sns\n\n\nfrom tensorflow.keras.utils import load_img\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras import Sequential, Model\nfrom tensorflow.keras.layers import (\n    Conv2D, MaxPooling2D, AveragePooling2D, \n    BatchNormalization, Dropout,Flatten, \n    Dense, Input)","metadata":{"execution":{"iopub.status.busy":"2023-06-26T11:49:51.905978Z","iopub.execute_input":"2023-06-26T11:49:51.906968Z","iopub.status.idle":"2023-06-26T11:50:02.461288Z","shell.execute_reply.started":"2023-06-26T11:49:51.906925Z","shell.execute_reply":"2023-06-26T11:50:02.459938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMAGE_H = 150\nIMAGE_W = 150\nIMAGE_C = 3","metadata":{"execution":{"iopub.status.busy":"2023-06-26T11:50:02.463540Z","iopub.execute_input":"2023-06-26T11:50:02.464372Z","iopub.status.idle":"2023-06-26T11:50:02.470266Z","shell.execute_reply.started":"2023-06-26T11:50:02.464326Z","shell.execute_reply":"2023-06-26T11:50:02.469234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Unzipping the train file","metadata":{}},{"cell_type":"code","source":"ziplocation = '/kaggle/input/dogs-vs-cats/train.zip'\ndestination = '/kaggle/working/train_images/'\n\nos.makedirs(destination,exist_ok=True)\n\nwith zipfile.ZipFile(ziplocation,'r') as zip_ref:\n    zip_ref.extractall(destination)","metadata":{"execution":{"iopub.status.busy":"2023-06-26T11:50:21.121363Z","iopub.execute_input":"2023-06-26T11:50:21.121754Z","iopub.status.idle":"2023-06-26T11:50:30.467171Z","shell.execute_reply.started":"2023-06-26T11:50:21.121727Z","shell.execute_reply":"2023-06-26T11:50:30.466071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create a dataframe with id and type","metadata":{}},{"cell_type":"code","source":"images = os.listdir('/kaggle/working/train_images/train/')\ncate = [] # category\n\nfor image in images:\n    temp = image.split('.')[0]\n    if temp.lower() == 'cat':\n        cate.append('cat')\n    else:\n        cate.append('dog')\n        \ndf = pd.DataFrame({\n    'image': images,\n    'type': cate\n})\n\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2023-06-26T11:50:35.782660Z","iopub.execute_input":"2023-06-26T11:50:35.783020Z","iopub.status.idle":"2023-06-26T11:50:35.846513Z","shell.execute_reply.started":"2023-06-26T11:50:35.782992Z","shell.execute_reply":"2023-06-26T11:50:35.845479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2023-06-26T11:50:40.685187Z","iopub.execute_input":"2023-06-26T11:50:40.685568Z","iopub.status.idle":"2023-06-26T11:50:40.692491Z","shell.execute_reply.started":"2023-06-26T11:50:40.685538Z","shell.execute_reply":"2023-06-26T11:50:40.691326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(data=df,x='type', label=\"count\")","metadata":{"execution":{"iopub.status.busy":"2023-06-26T11:50:43.705095Z","iopub.execute_input":"2023-06-26T11:50:43.705497Z","iopub.status.idle":"2023-06-26T11:50:44.047852Z","shell.execute_reply.started":"2023-06-26T11:50:43.705462Z","shell.execute_reply":"2023-06-26T11:50:44.046790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Viewing an image","metadata":{}},{"cell_type":"code","source":"image = load_img('/kaggle/working/train_images/train/cat.9862.jpg')\nplt.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2023-06-26T11:50:56.691568Z","iopub.execute_input":"2023-06-26T11:50:56.691934Z","iopub.status.idle":"2023-06-26T11:50:57.033791Z","shell.execute_reply.started":"2023-06-26T11:50:56.691906Z","shell.execute_reply":"2023-06-26T11:50:57.032901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"All the images are not of same size so it needs to be resized.","metadata":{}},{"cell_type":"markdown","source":"### Data augmentation","metadata":{}},{"cell_type":"code","source":"dataAug = ImageDataGenerator(\n    rotation_range=10,\n    zoom_range=0.1,\n    horizontal_flip=True,\n    vertical_flip=True,\n    rescale=1./255\n)","metadata":{"execution":{"iopub.status.busy":"2023-06-26T11:51:44.393036Z","iopub.execute_input":"2023-06-26T11:51:44.393402Z","iopub.status.idle":"2023-06-26T11:51:44.397989Z","shell.execute_reply.started":"2023-06-26T11:51:44.393373Z","shell.execute_reply":"2023-06-26T11:51:44.397066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Data splitting","metadata":{}},{"cell_type":"code","source":"train,val = train_test_split(df, test_size=0.25, random_state=42)\nprint(train.shape)\nprint(val.shape)","metadata":{"execution":{"iopub.status.busy":"2023-06-26T11:52:07.469887Z","iopub.execute_input":"2023-06-26T11:52:07.470589Z","iopub.status.idle":"2023-06-26T11:52:07.482032Z","shell.execute_reply.started":"2023-06-26T11:52:07.470555Z","shell.execute_reply":"2023-06-26T11:52:07.480875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainData = dataAug.flow_from_dataframe(\n    train,\n    \"/kaggle/working/train_images/train/\",\n    x_col='image',\n    y_col='type',\n    target_size=(150,150),\n    class_mode='categorical',\n    batch_size=15\n)\n\nvalData = dataAug.flow_from_dataframe(\n    val,\n    \"/kaggle/working/train_images/train/\",\n    x_col='image',\n    y_col='type',\n    target_size=(150,150),\n    class_mode='categorical',\n    batch_size=15\n)","metadata":{"execution":{"iopub.status.busy":"2023-06-26T11:52:26.774284Z","iopub.execute_input":"2023-06-26T11:52:26.774977Z","iopub.status.idle":"2023-06-26T11:52:27.085882Z","shell.execute_reply.started":"2023-06-26T11:52:26.774943Z","shell.execute_reply":"2023-06-26T11:52:27.084993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Model building","metadata":{}},{"cell_type":"code","source":"input_layer = Input(shape=(150,150,3))\nx = Conv2D(32,(3,3),activation='relu',padding='same')(input_layer)\nx = BatchNormalization()(x)\nx = MaxPooling2D()(x)\nx = Dropout(0.3)(x)\n\nx = Conv2D(64,(3,3),activation='relu')(x)\nx = BatchNormalization()(x)\nx = MaxPooling2D()(x)\nx = Dropout(0.3)(x)\n\nx = Conv2D(64,(3,3),activation='relu')(x)\nx = BatchNormalization()(x)\nx = MaxPooling2D()(x)\nx = Dropout(0.3)(x)\n\nx = Conv2D(128,(3,3),activation='relu')(x)\nx = BatchNormalization()(x)\nx = MaxPooling2D()(x)\nx = Dropout(0.3)(x)\n\nx = Flatten()(x)\nx = Dense(64,activation='relu')(x)\nx = Dense(2,activation='softmax')(x)\n\nmodel = Model(inputs=input_layer,outputs=x)\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-06-26T11:53:21.465277Z","iopub.execute_input":"2023-06-26T11:53:21.465686Z","iopub.status.idle":"2023-06-26T11:53:25.932473Z","shell.execute_reply.started":"2023-06-26T11:53:21.465654Z","shell.execute_reply":"2023-06-26T11:53:25.931629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])\n\n%timeit\nhistory = model.fit(trainData,validation_data=valData,epochs=10)","metadata":{"execution":{"iopub.status.busy":"2023-06-26T11:54:35.697359Z","iopub.execute_input":"2023-06-26T11:54:35.698605Z","iopub.status.idle":"2023-06-26T12:25:06.428044Z","shell.execute_reply.started":"2023-06-26T11:54:35.698561Z","shell.execute_reply":"2023-06-26T12:25:06.426992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(history.params)\n\n# check the keys of history object\nprint(history.history.keys())","metadata":{"execution":{"iopub.status.busy":"2023-06-26T12:36:23.261657Z","iopub.execute_input":"2023-06-26T12:36:23.262045Z","iopub.status.idle":"2023-06-26T12:36:23.268224Z","shell.execute_reply.started":"2023-06-26T12:36:23.262014Z","shell.execute_reply":"2023-06-26T12:36:23.266982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot the loss and accuracy\nplt.plot(history.history['loss'], label='Loss')\nplt.plot(history.history['val_loss'], label='Val Loss')\nplt.title('Loss and val_loss')\nplt.xlabel('Epoch')\nplt.ylabel('Value')\nplt.legend()\nplt.show()   # Thinking of reducing LR to smooth the val_loss","metadata":{"execution":{"iopub.status.busy":"2023-06-26T12:43:26.835013Z","iopub.execute_input":"2023-06-26T12:43:26.835416Z","iopub.status.idle":"2023-06-26T12:43:27.145067Z","shell.execute_reply.started":"2023-06-26T12:43:26.835383Z","shell.execute_reply":"2023-06-26T12:43:27.144079Z"},"trusted":true},"execution_count":null,"outputs":[]}]}