{"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-04T03:58:02.755289Z","iopub.execute_input":"2023-06-04T03:58:02.755642Z","iopub.status.idle":"2023-06-04T03:58:02.770664Z","shell.execute_reply.started":"2023-06-04T03:58:02.755613Z","shell.execute_reply":"2023-06-04T03:58:02.769627Z"},"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, BatchNormalization, Dropout,Flatten, Dense, Input)","metadata":{"execution":{"iopub.status.busy":"2023-06-04T03:58:02.772924Z","iopub.execute_input":"2023-06-04T03:58:02.773255Z","iopub.status.idle":"2023-06-04T03:58:11.204882Z","shell.execute_reply.started":"2023-06-04T03:58:02.773226Z","shell.execute_reply":"2023-06-04T03:58:11.203922Z"},"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-04T03:58:11.206799Z","iopub.execute_input":"2023-06-04T03:58:11.207584Z","iopub.status.idle":"2023-06-04T03:58:11.213591Z","shell.execute_reply.started":"2023-06-04T03:58:11.207547Z","shell.execute_reply":"2023-06-04T03:58:11.212455Z"},"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-04T03:58:11.214995Z","iopub.execute_input":"2023-06-04T03:58:11.215575Z","iopub.status.idle":"2023-06-04T03:58:21.297074Z","shell.execute_reply.started":"2023-06-04T03:58:11.215544Z","shell.execute_reply":"2023-06-04T03:58:21.296095Z"},"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 = []\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-04T03:58:21.299802Z","iopub.execute_input":"2023-06-04T03:58:21.300136Z","iopub.status.idle":"2023-06-04T03:58:21.354880Z","shell.execute_reply.started":"2023-06-04T03:58:21.300092Z","shell.execute_reply":"2023-06-04T03:58:21.353905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(data=df,x='type')","metadata":{"execution":{"iopub.status.busy":"2023-06-04T03:58:21.356477Z","iopub.execute_input":"2023-06-04T03:58:21.356936Z","iopub.status.idle":"2023-06-04T03:58:21.786768Z","shell.execute_reply.started":"2023-06-04T03:58:21.356905Z","shell.execute_reply":"2023-06-04T03:58:21.783663Z"},"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-04T03:58:21.788238Z","iopub.execute_input":"2023-06-04T03:58:21.788718Z","iopub.status.idle":"2023-06-04T03:58:22.237094Z","shell.execute_reply.started":"2023-06-04T03:58:21.788673Z","shell.execute_reply":"2023-06-04T03:58:22.236169Z"},"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-04T03:58:22.238255Z","iopub.execute_input":"2023-06-04T03:58:22.238612Z","iopub.status.idle":"2023-06-04T03:58:22.245130Z","shell.execute_reply.started":"2023-06-04T03:58:22.238580Z","shell.execute_reply":"2023-06-04T03:58:22.244145Z"},"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-04T03:58:22.247042Z","iopub.execute_input":"2023-06-04T03:58:22.248039Z","iopub.status.idle":"2023-06-04T03:58:22.262208Z","shell.execute_reply.started":"2023-06-04T03:58:22.248006Z","shell.execute_reply":"2023-06-04T03:58:22.261012Z"},"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-04T03:58:22.264119Z","iopub.execute_input":"2023-06-04T03:58:22.264841Z","iopub.status.idle":"2023-06-04T03:58:22.598485Z","shell.execute_reply.started":"2023-06-04T03:58:22.264807Z","shell.execute_reply":"2023-06-04T03:58:22.597285Z"},"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-04T03:58:22.602056Z","iopub.execute_input":"2023-06-04T03:58:22.602717Z","iopub.status.idle":"2023-06-04T03:58:25.614663Z","shell.execute_reply.started":"2023-06-04T03:58:22.602678Z","shell.execute_reply":"2023-06-04T03:58:25.613915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])\n\nhistory = model.fit(trainData,validation_data=valData,epochs=10)","metadata":{"execution":{"iopub.status.busy":"2023-06-04T03:58:25.615709Z","iopub.execute_input":"2023-06-04T03:58:25.616065Z","iopub.status.idle":"2023-06-04T04:25:45.633396Z","shell.execute_reply.started":"2023-06-04T03:58:25.616033Z","shell.execute_reply":"2023-06-04T04:25:45.632383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}