{"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":"2022-08-29T05:39:05.733558Z","iopub.execute_input":"2022-08-29T05:39:05.734011Z","iopub.status.idle":"2022-08-29T05:39:05.745075Z","shell.execute_reply.started":"2022-08-29T05:39:05.733967Z","shell.execute_reply":"2022-08-29T05:39:05.743619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Import libraries\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport matplotlib.image as img\nimport random\nimport os\nimport zipfile\nfrom tensorflow.keras.utils import to_categorical\nfrom sklearn.model_selection import train_test_split\nfrom keras.preprocessing.image import ImageDataGenerator,load_img\nfrom keras.models import Sequential\nfrom keras.layers import Conv2D,MaxPooling2D,Dropout,Flatten,Dense,Activation,BatchNormalization","metadata":{"execution":{"iopub.status.busy":"2022-08-29T05:39:05.751673Z","iopub.execute_input":"2022-08-29T05:39:05.752342Z","iopub.status.idle":"2022-08-29T05:39:05.760967Z","shell.execute_reply.started":"2022-08-29T05:39:05.752303Z","shell.execute_reply":"2022-08-29T05:39:05.759613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir(\"../input/dogs-vs-cats\")","metadata":{"execution":{"iopub.status.busy":"2022-08-29T05:39:05.763544Z","iopub.execute_input":"2022-08-29T05:39:05.764344Z","iopub.status.idle":"2022-08-29T05:39:05.777159Z","shell.execute_reply.started":"2022-08-29T05:39:05.764288Z","shell.execute_reply":"2022-08-29T05:39:05.775668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# Unzipp the data to new path\ntrain_zip_path = \"../input/dogs-vs-cats/train.zip\"\ntest_zip_path = \"../input/dogs-vs-cats/test1.zip\"\n\nfiles_path = \"/kaggle/files/unzipped/\"\n\nwith zipfile.ZipFile(train_zip_path, 'r') as zipp:\n    zipp.extractall(files_path)\n    \nwith zipfile.ZipFile(test_zip_path, 'r') as zipp:\n    zipp.extractall(files_path)\n\n\ntrain_path = \"/kaggle/files/unzipped/train\"\ntest_path = \"/kaggle/files/unzipped/test1\"","metadata":{"execution":{"iopub.status.busy":"2022-08-29T05:39:05.778706Z","iopub.execute_input":"2022-08-29T05:39:05.780041Z","iopub.status.idle":"2022-08-29T05:39:29.063768Z","shell.execute_reply.started":"2022-08-29T05:39:05.779998Z","shell.execute_reply":"2022-08-29T05:39:29.061335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir(\"/kaggle/files/unzipped\")","metadata":{"execution":{"iopub.status.busy":"2022-08-29T05:39:29.068839Z","iopub.execute_input":"2022-08-29T05:39:29.069489Z","iopub.status.idle":"2022-08-29T05:39:29.094031Z","shell.execute_reply.started":"2022-08-29T05:39:29.069426Z","shell.execute_reply":"2022-08-29T05:39:29.092622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create new test dataframe\ntrain_df = pd.DataFrame({\"file\":os.listdir(train_path)})\ntrain_df[\"label\"] = train_df[\"file\"].apply(lambda x:x.split(\".\")[0])\ntrain_df.head()\n","metadata":{"execution":{"iopub.status.busy":"2022-08-29T05:39:29.095447Z","iopub.execute_input":"2022-08-29T05:39:29.095899Z","iopub.status.idle":"2022-08-29T05:39:29.359027Z","shell.execute_reply.started":"2022-08-29T05:39:29.095861Z","shell.execute_reply":"2022-08-29T05:39:29.357462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.groupby(\"label\").count()","metadata":{"execution":{"iopub.status.busy":"2022-08-29T05:39:29.360795Z","iopub.execute_input":"2022-08-29T05:39:29.361238Z","iopub.status.idle":"2022-08-29T05:39:29.384345Z","shell.execute_reply.started":"2022-08-29T05:39:29.361198Z","shell.execute_reply":"2022-08-29T05:39:29.383111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create new train dataframe\ntest_df = pd.DataFrame({\"file\":os.listdir(test_path)})\ntest_df.head()\nnb_samples = test_df.shape[0]","metadata":{"execution":{"iopub.status.busy":"2022-08-29T05:39:29.385811Z","iopub.execute_input":"2022-08-29T05:39:29.386171Z","iopub.status.idle":"2022-08-29T05:39:29.407084Z","shell.execute_reply.started":"2022-08-29T05:39:29.386138Z","shell.execute_reply":"2022-08-29T05:39:29.405870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define Image Properties\nFast_run = False\nImage_width = 128\nImage_height = 128\nImage_size = (Image_width, Image_height)\nImage_channels = 3","metadata":{"execution":{"iopub.status.busy":"2022-08-29T05:39:29.410431Z","iopub.execute_input":"2022-08-29T05:39:29.411017Z","iopub.status.idle":"2022-08-29T05:39:29.418050Z","shell.execute_reply.started":"2022-08-29T05:39:29.410960Z","shell.execute_reply":"2022-08-29T05:39:29.416571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# EDA\nimport plotly.express as px\ncounts = train_df[\"label\"].value_counts()  # give us the cats and dogs numbers how many\nfig=px.pie(counts.head(10),values= 'label', names=train_df[\"label\"].unique(),hole=0.425)\nfig.update_layout(title='Data Distribution of Data',font_size=15,title_x=0.45,annotations=[dict(text='Cat vs Dog',font_size=18, showarrow=False,height=800,width=700)])\nfig.update_traces(textfont_size=15,textinfo='percent')\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-29T05:39:29.423970Z","iopub.execute_input":"2022-08-29T05:39:29.425369Z","iopub.status.idle":"2022-08-29T05:39:29.508216Z","shell.execute_reply.started":"2022-08-29T05:39:29.425324Z","shell.execute_reply":"2022-08-29T05:39:29.507217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Sample Images Visulazation\nfig = plt.figure(1, figsize=(9,9))\nfig.suptitle(\"Sample  images\")\n\nfor i in range(16):\n    plt.subplot(4,4,i+1)\n    #image = img.imread(train_path + \"/\"+ train_df[\"file\"][i])  # image definations are used for same job\n    image = load_img(train_path + \"/\"+ train_df[\"file\"][i])\n    plt.imshow(image)\n\nfig.tight_layout()\nplt.show()\n\n","metadata":{"execution":{"iopub.status.busy":"2022-08-29T05:39:29.509578Z","iopub.execute_input":"2022-08-29T05:39:29.510898Z","iopub.status.idle":"2022-08-29T05:39:31.740246Z","shell.execute_reply.started":"2022-08-29T05:39:29.510848Z","shell.execute_reply":"2022-08-29T05:39:31.738653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Dogs Images Visulaziation\n\nfig=plt.figure(figsize=(10,10))\nfig.suptitle(\"Dog  images\")\nk = 0\n\nfor i in range(2500):\n    #plt.subplot(4,4,k+1)\n    if k <= 15:\n        if os.listdir(train_path)[i].split('.')[0]=='dog':\n            plt.subplot(4,4,k+1)\n            image = img.imread(train_path + \"/\"+ train_df[\"file\"][i])\n            image = load_img(train_path + \"/\"+ train_df[\"file\"][i])\n            plt.imshow(image)\n            k += 1\n        else:\n            continue\n    else:\n        break\n\nfig.tight_layout()\nplt.show()\n\n\n\n","metadata":{"execution":{"iopub.status.busy":"2022-08-29T05:39:31.741771Z","iopub.execute_input":"2022-08-29T05:39:31.742174Z","iopub.status.idle":"2022-08-29T05:39:34.681300Z","shell.execute_reply.started":"2022-08-29T05:39:31.742137Z","shell.execute_reply":"2022-08-29T05:39:34.679946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Cats Images Visulaziation\n\nfig=plt.figure(figsize=(10,10))\nfig.suptitle(\"Cat images\")\nk = 0\n\nfor i in range(2500):\n    #plt.subplot(4,4,k+1)\n    if k <= 15:\n        if os.listdir(train_path)[i].split('.')[0]=='cat':\n            plt.subplot(4,4,k+1)\n            image = img.imread(train_path + \"/\"+ train_df[\"file\"][i])\n            image = load_img(train_path + \"/\"+ train_df[\"file\"][i])\n            plt.imshow(image)\n            k += 1\n        else:\n            continue\n    else:\n        break\n\nfig.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-29T05:39:34.683149Z","iopub.execute_input":"2022-08-29T05:39:34.683756Z","iopub.status.idle":"2022-08-29T05:39:37.443163Z","shell.execute_reply.started":"2022-08-29T05:39:34.683685Z","shell.execute_reply":"2022-08-29T05:39:37.441570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Create Convolutional Neural Network\n","metadata":{}},{"cell_type":"markdown","source":"Convolutional neural network has three layers.\n* Convolutional layer \n \n Convolutional layer behave as filter.\n\n* Pooling layer \n \n Pooling layer reduces the dimension of feature map. \n\n* Fully Connected Layer \n \n The final layer is the Fully Connected Layer and it's input is the rich features that have been extracted using convolutional filters.\n","metadata":{}},{"cell_type":"code","source":"# Create Neural Network\n\nmodel=Sequential()\n\nmodel.add(Conv2D(32,(3,3),activation='relu',input_shape=(Image_width,Image_height,Image_channels)))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(64,(3,3),activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(128,(3,3),activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Flatten())\nmodel.add(Dense(512,activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.5))\n\nmodel.add(Dense(2,activation='softmax'))   # 2 because we have cat and dog classes\nmodel.compile(loss='categorical_crossentropy', optimizer='rmsprop',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-08-29T05:39:37.445005Z","iopub.execute_input":"2022-08-29T05:39:37.445444Z","iopub.status.idle":"2022-08-29T05:39:37.670712Z","shell.execute_reply.started":"2022-08-29T05:39:37.445403Z","shell.execute_reply":"2022-08-29T05:39:37.669502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()\n","metadata":{"execution":{"iopub.status.busy":"2022-08-29T05:39:37.672420Z","iopub.execute_input":"2022-08-29T05:39:37.672984Z","iopub.status.idle":"2022-08-29T05:39:37.684835Z","shell.execute_reply.started":"2022-08-29T05:39:37.672928Z","shell.execute_reply":"2022-08-29T05:39:37.682948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import plot_model\nfrom IPython.display import Image\nplot_model(model, to_file='convnet.png', show_shapes=True,show_layer_names=True)\nImage(filename='convnet.png') \n","metadata":{"execution":{"iopub.status.busy":"2022-08-29T05:39:37.687448Z","iopub.execute_input":"2022-08-29T05:39:37.688355Z","iopub.status.idle":"2022-08-29T05:39:38.142143Z","shell.execute_reply.started":"2022-08-29T05:39:37.688296Z","shell.execute_reply":"2022-08-29T05:39:38.140281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install visualkeras\nimport visualkeras\nvisualkeras.layered_view(model, legend=True) ","metadata":{"execution":{"iopub.status.busy":"2022-08-29T05:39:38.144949Z","iopub.execute_input":"2022-08-29T05:39:38.145478Z","iopub.status.idle":"2022-08-29T05:39:51.856756Z","shell.execute_reply.started":"2022-08-29T05:39:38.145428Z","shell.execute_reply":"2022-08-29T05:39:51.855094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Callbacks\n","metadata":{}},{"cell_type":"markdown","source":"Earlystopping prevents the overfitting","metadata":{}},{"cell_type":"code","source":"from keras.callbacks import EarlyStopping, ReduceLROnPlateau","metadata":{"execution":{"iopub.status.busy":"2022-08-29T05:39:51.859892Z","iopub.execute_input":"2022-08-29T05:39:51.860525Z","iopub.status.idle":"2022-08-29T05:39:51.867235Z","shell.execute_reply.started":"2022-08-29T05:39:51.860453Z","shell.execute_reply":"2022-08-29T05:39:51.865438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"earlystop = EarlyStopping(patience=10)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-29T05:39:51.869838Z","iopub.execute_input":"2022-08-29T05:39:51.870305Z","iopub.status.idle":"2022-08-29T05:39:51.881087Z","shell.execute_reply.started":"2022-08-29T05:39:51.870264Z","shell.execute_reply":"2022-08-29T05:39:51.879628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Learning rate reduction reduce the learning rate","metadata":{}},{"cell_type":"code","source":"learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc', \n                                            patience=2, \n                                            verbose=1, \n                                            factor=0.5, \n                                            min_lr=0.00001)\n### ???? ?????? *??????? ????","metadata":{"execution":{"iopub.status.busy":"2022-08-29T05:39:51.883854Z","iopub.execute_input":"2022-08-29T05:39:51.885034Z","iopub.status.idle":"2022-08-29T05:39:51.894615Z","shell.execute_reply.started":"2022-08-29T05:39:51.884974Z","shell.execute_reply":"2022-08-29T05:39:51.892990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callbacks = [earlystop, learning_rate_reduction]","metadata":{"execution":{"iopub.status.busy":"2022-08-29T05:39:51.897399Z","iopub.execute_input":"2022-08-29T05:39:51.898642Z","iopub.status.idle":"2022-08-29T05:39:51.913667Z","shell.execute_reply.started":"2022-08-29T05:39:51.898577Z","shell.execute_reply":"2022-08-29T05:39:51.912064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### split data in train and validation","metadata":{}},{"cell_type":"code","source":"train_data, val_data = train_test_split(train_df, test_size=0.20, random_state=42)\n\ntrain_data = train_data.reset_index(drop=True)\nval_data = val_data.reset_index(drop=True)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-29T05:39:51.917344Z","iopub.execute_input":"2022-08-29T05:39:51.918786Z","iopub.status.idle":"2022-08-29T05:39:51.943147Z","shell.execute_reply.started":"2022-08-29T05:39:51.918713Z","shell.execute_reply":"2022-08-29T05:39:51.942031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data['label'].value_counts().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2022-08-29T05:39:51.944940Z","iopub.execute_input":"2022-08-29T05:39:51.945671Z","iopub.status.idle":"2022-08-29T05:39:52.159114Z","shell.execute_reply.started":"2022-08-29T05:39:51.945626Z","shell.execute_reply":"2022-08-29T05:39:52.157748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_data['label'].value_counts().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2022-08-29T05:39:52.161379Z","iopub.execute_input":"2022-08-29T05:39:52.161971Z","iopub.status.idle":"2022-08-29T05:39:52.357283Z","shell.execute_reply.started":"2022-08-29T05:39:52.161918Z","shell.execute_reply":"2022-08-29T05:39:52.355794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total_train = train_data.shape[0]\nprint(total_train)\ntotal_val = val_data.shape[0]\nprint(total_val)\nbatch_size=15   #  ??? ??? ","metadata":{"execution":{"iopub.status.busy":"2022-08-29T05:39:52.364051Z","iopub.execute_input":"2022-08-29T05:39:52.364531Z","iopub.status.idle":"2022-08-29T05:39:52.371375Z","shell.execute_reply.started":"2022-08-29T05:39:52.364481Z","shell.execute_reply":"2022-08-29T05:39:52.369966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Training and Validation Data Genetrator\n\nIf we have little data to train, we have to use ImageDataGenerator in order to increase the number of the data.\nData augmentation helps reduce the overfitting when training machine learning model.\n\nBasic augmentation:\n* Flipping\n* Rotating\n* Zooming and scaling\n* Cropping \n* Translating\n* Moving x and y axis\n* Adding noise ","metadata":{}},{"cell_type":"markdown","source":"1. rescale = 1./255 : change the value between 0 and 1\n2. rotation_range = 15 : Random rotation within 15 degrees\n3. shear_range = 0.1 : shear range 10% \n4. zoom_range = 0.2 : zoom range 20%\n5. horizontal_flip = True : Randomly flip horizontally.\n6. width_shift_range = 0.1 : Randomly move the original image horizontally within 10% of the width\n7. height_shift_range=0.1 : Randomly move the original image vertically within 10% of the width","metadata":{}},{"cell_type":"markdown","source":"#### Train Generator","metadata":{}},{"cell_type":"code","source":"print(os.listdir(\"/kaggle/files/unzipped/\"))","metadata":{"execution":{"iopub.status.busy":"2022-08-29T05:39:52.373241Z","iopub.execute_input":"2022-08-29T05:39:52.374388Z","iopub.status.idle":"2022-08-29T05:39:52.384666Z","shell.execute_reply.started":"2022-08-29T05:39:52.374340Z","shell.execute_reply":"2022-08-29T05:39:52.383481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_generator = ImageDataGenerator(\n    rescale = 1./255,\n    rotation_range = 15, \n    shear_range = 0.1,\n    zoom_range = 0.2,\n    horizontal_flip = True,\n    width_shift_range = 0.1, \n    height_shift_range = 0.1,                                   \n)\n\ntrain_generator = train_data_generator.flow_from_dataframe(\n    train_data, \n    \"/kaggle/files/unzipped/train/\", \n    x_col='file',\n    y_col='label',\n    target_size=Image_size,\n    class_mode='categorical',\n    batch_size=batch_size\n)","metadata":{"execution":{"iopub.status.busy":"2022-08-29T05:39:52.386262Z","iopub.execute_input":"2022-08-29T05:39:52.386844Z","iopub.status.idle":"2022-08-29T05:39:52.665618Z","shell.execute_reply.started":"2022-08-29T05:39:52.386802Z","shell.execute_reply":"2022-08-29T05:39:52.664178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_data)\nlen(train_generator)","metadata":{"execution":{"iopub.status.busy":"2022-08-29T05:39:52.667272Z","iopub.execute_input":"2022-08-29T05:39:52.667721Z","iopub.status.idle":"2022-08-29T05:39:52.676558Z","shell.execute_reply.started":"2022-08-29T05:39:52.667680Z","shell.execute_reply":"2022-08-29T05:39:52.675011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Validation Generator\nNo neeed to apply augmentation in validation data set.","metadata":{}},{"cell_type":"code","source":"validation_data_generator = ImageDataGenerator(rescale = 1./255)\n\nvalidation_generator = validation_data_generator.flow_from_dataframe(\n    val_data,\n    \"/kaggle/files/unzipped/train/\",\n    x_col='file',\n    y_col='label',\n    target_size=Image_size,\n    class_mode='categorical',\n    batch_size=batch_size\n)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-29T05:39:52.678600Z","iopub.execute_input":"2022-08-29T05:39:52.679135Z","iopub.status.idle":"2022-08-29T05:39:52.760390Z","shell.execute_reply.started":"2022-08-29T05:39:52.679084Z","shell.execute_reply":"2022-08-29T05:39:52.759082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Test generator","metadata":{}},{"cell_type":"code","source":"test_data_generator = ImageDataGenerator(\n    rescale = 1./255,\n    rotation_range = 15, \n    shear_range = 0.1,\n    zoom_range = 0.2,\n    horizontal_flip = True,\n    width_shift_range = 0.1, \n    height_shift_range = 0.1\n)\n\ntest_generator = test_data_generator.flow_from_dataframe(\n    test_df, \n    \"/kaggle/files/unzipped/test1/\", \n    x_col='file',\n    y_col=None,\n    target_size=Image_size,\n    class_mode=None,\n    batch_size=batch_size,\n    shuffle=False\n\n)","metadata":{"execution":{"iopub.status.busy":"2022-08-29T05:39:52.762111Z","iopub.execute_input":"2022-08-29T05:39:52.762639Z","iopub.status.idle":"2022-08-29T05:39:52.906938Z","shell.execute_reply.started":"2022-08-29T05:39:52.762595Z","shell.execute_reply":"2022-08-29T05:39:52.905309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Generator work ? ","metadata":{}},{"cell_type":"code","source":"example_df = train_data.sample(n=1).reset_index(drop=True)\nexample_generator = train_data_generator.flow_from_dataframe(\n    example_df, \n    \"/kaggle/files/unzipped/train/\", \n    x_col='file',\n    y_col='label',\n    target_size=Image_size,\n    class_mode='categorical'\n)","metadata":{"execution":{"iopub.status.busy":"2022-08-29T05:39:52.908833Z","iopub.execute_input":"2022-08-29T05:39:52.909790Z","iopub.status.idle":"2022-08-29T05:39:52.925184Z","shell.execute_reply.started":"2022-08-29T05:39:52.909720Z","shell.execute_reply":"2022-08-29T05:39:52.923739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"figure = plt.figure(figsize=(12,12))\nfigure.suptitle(\"Random Augmented images\")\nfor i in range(0, 15):\n    plt.subplot(5, 3, i+1)\n    for X_batch, Y_batch in example_generator:\n        image = X_batch[0]\n        plt.imshow(image)\n        break\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-29T05:39:52.926923Z","iopub.execute_input":"2022-08-29T05:39:52.927325Z","iopub.status.idle":"2022-08-29T05:39:55.814903Z","shell.execute_reply.started":"2022-08-29T05:39:52.927287Z","shell.execute_reply":"2022-08-29T05:39:55.813493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_generator)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-29T05:39:55.817052Z","iopub.execute_input":"2022-08-29T05:39:55.817474Z","iopub.status.idle":"2022-08-29T05:39:55.826066Z","shell.execute_reply.started":"2022-08-29T05:39:55.817433Z","shell.execute_reply":"2022-08-29T05:39:55.824722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Fit Model\n","metadata":{}},{"cell_type":"code","source":"#epochs = 3 if Fast_run else 50\nepoches = 12\n\nhistory = model.fit_generator(\n    train_generator, \n    epochs=epochs,\n    validation_data=validation_generator,\n    validation_steps=total_val//batch_size,\n    steps_per_epoch=total_train//batch_size,\n    callbacks=callbacks\n)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-29T05:39:55.828098Z","iopub.execute_input":"2022-08-29T05:39:55.829622Z","iopub.status.idle":"2022-08-29T11:19:54.826295Z","shell.execute_reply.started":"2022-08-29T05:39:55.829570Z","shell.execute_reply":"2022-08-29T11:19:54.823573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save_weights(\"./cnn_model.h5\")\n","metadata":{"execution":{"iopub.status.busy":"2022-08-29T11:19:54.833057Z","iopub.execute_input":"2022-08-29T11:19:54.833763Z","iopub.status.idle":"2022-08-29T11:19:55.059447Z","shell.execute_reply.started":"2022-08-29T11:19:54.833702Z","shell.execute_reply":"2022-08-29T11:19:55.058433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Plotting Loss and Accuracy","metadata":{}},{"cell_type":"code","source":"acc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepoches = range(len(acc))\n\nplt.plot(epoches, acc, 'bo', label='Training accuracy')\nplt.plot(epoches, val_acc, 'b', label='Validation accuracy')\nplt.title('Training and validation accuracy')\nplt.legend()\n\nplt.figure()\n\nplt.plot(epoches, loss, 'go', label='Training Loss')\nplt.plot(epoches, val_loss, 'g', label='Validation Loss')\nplt.title('Training and validation loss')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-29T11:19:55.061216Z","iopub.execute_input":"2022-08-29T11:19:55.061866Z","iopub.status.idle":"2022-08-29T11:19:55.587253Z","shell.execute_reply.started":"2022-08-29T11:19:55.061826Z","shell.execute_reply":"2022-08-29T11:19:55.585947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(12, 12))\nax1.plot(history.history['loss'], color='b', label=\"Training loss\")\nax1.plot(history.history['val_loss'], color='r', label=\"validation loss\")\nax1.set_xticks(np.arange(1, epochs, 1))\nax1.set_yticks(np.arange(0, 1, 0.1))\nlegend = plt.legend()\n\nax2.plot(history.history['accuracy'], color='b', label=\"Training accuracy\")\nax2.plot(history.history['val_accuracy'], color='r',label=\"Validation accuracy\")\nax2.set_xticks(np.arange(1, epochs, 1))\n\nlegend = plt.legend(loc='best', shadow=True)\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-29T11:19:55.589552Z","iopub.execute_input":"2022-08-29T11:19:55.590279Z","iopub.status.idle":"2022-08-29T11:19:56.593250Z","shell.execute_reply.started":"2022-08-29T11:19:55.590236Z","shell.execute_reply":"2022-08-29T11:19:56.591988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Predict","metadata":{}},{"cell_type":"code","source":"predict = model.predict_generator(test_generator, steps=np.ceil(nb_samples/batch_size))","metadata":{"execution":{"iopub.status.busy":"2022-08-29T11:19:56.595449Z","iopub.execute_input":"2022-08-29T11:19:56.596231Z","iopub.status.idle":"2022-08-29T11:22:05.538959Z","shell.execute_reply.started":"2022-08-29T11:19:56.596180Z","shell.execute_reply":"2022-08-29T11:22:05.537347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df[\"label\"] =  np.argmax(predict, axis=-1)","metadata":{"execution":{"iopub.status.busy":"2022-08-29T11:22:05.541502Z","iopub.execute_input":"2022-08-29T11:22:05.542781Z","iopub.status.idle":"2022-08-29T11:22:05.558116Z","shell.execute_reply.started":"2022-08-29T11:22:05.542734Z","shell.execute_reply":"2022-08-29T11:22:05.556811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_map = dict((v,k) for k,v in train_generator.class_indices.items())\ntest_df['label'] = test_df['label'].replace(label_map)","metadata":{"execution":{"iopub.status.busy":"2022-08-29T11:22:05.561931Z","iopub.execute_input":"2022-08-29T11:22:05.563281Z","iopub.status.idle":"2022-08-29T11:22:05.586812Z","shell.execute_reply.started":"2022-08-29T11:22:05.563230Z","shell.execute_reply":"2022-08-29T11:22:05.585486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['label'] = test_df['label'].replace({ 'dog': 1, 'cat': 0 })","metadata":{"execution":{"iopub.status.busy":"2022-08-29T11:33:54.147504Z","iopub.execute_input":"2022-08-29T11:33:54.148212Z","iopub.status.idle":"2022-08-29T11:33:54.169351Z","shell.execute_reply.started":"2022-08-29T11:33:54.148170Z","shell.execute_reply":"2022-08-29T11:33:54.167895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['label'].value_counts().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2022-08-29T11:33:57.740060Z","iopub.execute_input":"2022-08-29T11:33:57.740647Z","iopub.status.idle":"2022-08-29T11:33:57.917431Z","shell.execute_reply.started":"2022-08-29T11:33:57.740603Z","shell.execute_reply":"2022-08-29T11:33:57.915872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['label'] = test_df['label'].replace({ 1: 'dog', 0: \"cat\" })","metadata":{"execution":{"iopub.status.busy":"2022-08-29T11:35:08.799855Z","iopub.execute_input":"2022-08-29T11:35:08.800416Z","iopub.status.idle":"2022-08-29T11:35:08.811084Z","shell.execute_reply.started":"2022-08-29T11:35:08.800374Z","shell.execute_reply":"2022-08-29T11:35:08.809318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_test = test_df.head(18)\nsample_test.head()\nplt.figure(figsize=(12, 24))\nfor index, row in sample_test.iterrows():\n    filename = row['file']\n    category = row['label']\n    img = load_img(\"/kaggle/files/unzipped/test1/\"+filename, target_size=Image_size)\n    plt.subplot(6, 3, index+1)\n    plt.imshow(img)\n    plt.xlabel(filename + '(' + \"{}\".format(category) + ')' )\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-29T11:35:11.642659Z","iopub.execute_input":"2022-08-29T11:35:11.643861Z","iopub.status.idle":"2022-08-29T11:35:14.923723Z","shell.execute_reply.started":"2022-08-29T11:35:11.643800Z","shell.execute_reply":"2022-08-29T11:35:14.921255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}