{"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":"markdown","source":"# Import Libraries","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd \nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.utils import load_img\nfrom keras.utils import to_categorical\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt\nimport random\nimport os\nimport zipfile\n","metadata":{"execution":{"iopub.status.busy":"2023-05-27T09:20:53.156967Z","iopub.execute_input":"2023-05-27T09:20:53.157663Z","iopub.status.idle":"2023-05-27T09:21:00.449005Z","shell.execute_reply.started":"2023-05-27T09:20:53.157634Z","shell.execute_reply":"2023-05-27T09:21:00.448032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Unzip train and test directory","metadata":{}},{"cell_type":"code","source":"os.listdir('/kaggle/input/dogs-vs-cats/')","metadata":{"execution":{"iopub.status.busy":"2023-05-27T09:21:00.451133Z","iopub.execute_input":"2023-05-27T09:21:00.451846Z","iopub.status.idle":"2023-05-27T09:21:00.462889Z","shell.execute_reply.started":"2023-05-27T09:21:00.451809Z","shell.execute_reply":"2023-05-27T09:21:00.461884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir train","metadata":{"execution":{"iopub.status.busy":"2023-05-27T09:21:03.833636Z","iopub.execute_input":"2023-05-27T09:21:03.836761Z","iopub.status.idle":"2023-05-27T09:21:04.826096Z","shell.execute_reply.started":"2023-05-27T09:21:03.836720Z","shell.execute_reply":"2023-05-27T09:21:04.824786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip -q /kaggle/input/dogs-vs-cats/train.zip -d train/","metadata":{"execution":{"iopub.status.busy":"2023-05-27T09:21:04.828887Z","iopub.execute_input":"2023-05-27T09:21:04.829292Z","iopub.status.idle":"2023-05-27T09:21:15.718463Z","shell.execute_reply.started":"2023-05-27T09:21:04.829251Z","shell.execute_reply":"2023-05-27T09:21:15.717152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('/kaggle/working/train/')","metadata":{"execution":{"iopub.status.busy":"2023-05-27T09:21:15.720651Z","iopub.execute_input":"2023-05-27T09:21:15.721030Z","iopub.status.idle":"2023-05-27T09:21:15.732667Z","shell.execute_reply.started":"2023-05-27T09:21:15.720990Z","shell.execute_reply":"2023-05-27T09:21:15.731505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir test","metadata":{"execution":{"iopub.status.busy":"2023-05-27T09:21:22.672853Z","iopub.execute_input":"2023-05-27T09:21:22.673244Z","iopub.status.idle":"2023-05-27T09:21:23.646331Z","shell.execute_reply.started":"2023-05-27T09:21:22.673206Z","shell.execute_reply":"2023-05-27T09:21:23.644979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip -q /kaggle/input/dogs-vs-cats/test1.zip -d test/","metadata":{"execution":{"iopub.status.busy":"2023-05-27T09:21:23.648816Z","iopub.execute_input":"2023-05-27T09:21:23.649241Z","iopub.status.idle":"2023-05-27T09:21:29.224531Z","shell.execute_reply.started":"2023-05-27T09:21:23.649200Z","shell.execute_reply":"2023-05-27T09:21:29.223130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('/kaggle/working/test/')","metadata":{"execution":{"iopub.status.busy":"2023-05-27T09:21:29.226663Z","iopub.execute_input":"2023-05-27T09:21:29.227447Z","iopub.status.idle":"2023-05-27T09:21:29.235401Z","shell.execute_reply.started":"2023-05-27T09:21:29.227402Z","shell.execute_reply":"2023-05-27T09:21:29.234204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**if want to remove train directory**\n* rm -r /kaggle/working/train/","metadata":{}},{"cell_type":"markdown","source":"**If we want to read images without create directory by the following way**","metadata":{}},{"cell_type":"code","source":"#------------------for train.zip directory---------------------- \n# import zipfile\n# from PIL import Image\n# import io\n# import numpy as np\n\n# # Load the train.zip file\n# with zipfile.ZipFile('/kaggle/input/dogs-vs-cats/train.zip', 'r') as zip_ref:\n#     # Get a list of all the file names in the zip file\n#     file_names = zip_ref.namelist()\n#     # Initialize an empty list to store the image data\n#     train_imgs = []\n#     # Loop over each file in the zip file\n#     for file_name in file_names:\n#         # Check if the file is an image file\n#         if file_name.endswith('.jpg'):\n#             # Open the image file inside the zip\n#             with zip_ref.open(file_name) as file:\n#                 # Read the contents of the file into memory\n#                 img_data = io.BytesIO(file.read())\n#                 # Load the image data into a PIL Image object\n#                 img = Image.open(img_data)\n#                 # Append the image data to the list\n#                 train_imgs.append(np.array(img))\n# len(train_imgs)","metadata":{"execution":{"iopub.status.busy":"2023-05-27T09:21:31.858241Z","iopub.execute_input":"2023-05-27T09:21:31.859269Z","iopub.status.idle":"2023-05-27T09:21:31.865146Z","shell.execute_reply.started":"2023-05-27T09:21:31.859234Z","shell.execute_reply":"2023-05-27T09:21:31.864223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#------------------for test.zip directory---------------------- \n# with zipfile.ZipFile('/kaggle/input/dogs-vs-cats/test1.zip', 'r') as zip_ref:\n#     test_imgs = []\n#     for file_name in zip_ref.namelist():\n#         if file_name.endswith('.jpg'):\n#             with zip_ref.open(file_name) as file:\n#                 img_data = io.BytesIO(file.read())\n#                 img = Image.open(img_data)\n#                 test_imgs.append(np.array(img))\n#len(train_imgs)","metadata":{"execution":{"iopub.status.busy":"2023-05-27T09:21:32.142326Z","iopub.execute_input":"2023-05-27T09:21:32.143412Z","iopub.status.idle":"2023-05-27T09:21:32.149861Z","shell.execute_reply.started":"2023-05-27T09:21:32.143378Z","shell.execute_reply":"2023-05-27T09:21:32.148525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Overview on the data","metadata":{}},{"cell_type":"markdown","source":"**we have 2500 train image**","metadata":{}},{"cell_type":"code","source":"filenames = os.listdir(\"../working/train/train\")\nlen(filenames)","metadata":{"execution":{"iopub.status.busy":"2023-05-27T09:21:33.329857Z","iopub.execute_input":"2023-05-27T09:21:33.330505Z","iopub.status.idle":"2023-05-27T09:21:33.354732Z","shell.execute_reply.started":"2023-05-27T09:21:33.330471Z","shell.execute_reply":"2023-05-27T09:21:33.353728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**we have 12500 test image**","metadata":{}},{"cell_type":"code","source":"testfilenames = os.listdir(\"../working/test/test1\")\nlen(testfilenames)","metadata":{"execution":{"iopub.status.busy":"2023-05-27T09:21:34.336381Z","iopub.execute_input":"2023-05-27T09:21:34.336757Z","iopub.status.idle":"2023-05-27T09:21:34.353932Z","shell.execute_reply.started":"2023-05-27T09:21:34.336729Z","shell.execute_reply":"2023-05-27T09:21:34.353101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames[:10]","metadata":{"execution":{"iopub.status.busy":"2023-05-27T09:21:34.680714Z","iopub.execute_input":"2023-05-27T09:21:34.682063Z","iopub.status.idle":"2023-05-27T09:21:35.258600Z","shell.execute_reply.started":"2023-05-27T09:21:34.682012Z","shell.execute_reply":"2023-05-27T09:21:35.257473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"categories=[]\nfor img in filenames:\n    category = img.split('.')[0]\n    if category == 'cat':\n        categories.append(0)\n    elif category == 'dog':\n        categories.append(1)","metadata":{"execution":{"iopub.status.busy":"2023-05-27T09:21:35.260916Z","iopub.execute_input":"2023-05-27T09:21:35.261695Z","iopub.status.idle":"2023-05-27T09:21:35.283308Z","shell.execute_reply.started":"2023-05-27T09:21:35.261661Z","shell.execute_reply":"2023-05-27T09:21:35.282394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame({'filename':filenames,'category':categories})","metadata":{"execution":{"iopub.status.busy":"2023-05-27T09:21:35.534778Z","iopub.execute_input":"2023-05-27T09:21:35.535127Z","iopub.status.idle":"2023-05-27T09:21:35.560163Z","shell.execute_reply.started":"2023-05-27T09:21:35.535094Z","shell.execute_reply":"2023-05-27T09:21:35.559351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-27T09:21:36.003841Z","iopub.execute_input":"2023-05-27T09:21:36.004797Z","iopub.status.idle":"2023-05-27T09:21:36.025042Z","shell.execute_reply.started":"2023-05-27T09:21:36.004756Z","shell.execute_reply":"2023-05-27T09:21:36.024123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.tail()","metadata":{"execution":{"iopub.status.busy":"2023-05-27T09:21:36.422617Z","iopub.execute_input":"2023-05-27T09:21:36.423145Z","iopub.status.idle":"2023-05-27T09:21:36.432251Z","shell.execute_reply.started":"2023-05-27T09:21:36.423114Z","shell.execute_reply":"2023-05-27T09:21:36.430948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['category'].value_counts().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2023-05-27T09:21:38.403802Z","iopub.execute_input":"2023-05-27T09:21:38.404174Z","iopub.status.idle":"2023-05-27T09:21:38.649828Z","shell.execute_reply.started":"2023-05-27T09:21:38.404145Z","shell.execute_reply":"2023-05-27T09:21:38.648927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = random.choice(filenames)\nimage = load_img(\"/kaggle/working/train/train/\"+sample)\nplt.imshow(image)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-27T09:21:40.540863Z","iopub.execute_input":"2023-05-27T09:21:40.541820Z","iopub.status.idle":"2023-05-27T09:21:40.917417Z","shell.execute_reply.started":"2023-05-27T09:21:40.541784Z","shell.execute_reply":"2023-05-27T09:21:40.916430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"size=[]\nfor file in filenames:\n    img = load_img(\"/kaggle/working/train/train/\"+file)\n    np_img = np.array(img)\n    size.append(np_img.shape)\npd.Series(size).value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-05-27T09:21:40.918828Z","iopub.execute_input":"2023-05-27T09:21:40.919451Z","iopub.status.idle":"2023-05-27T09:22:13.404987Z","shell.execute_reply.started":"2023-05-27T09:21:40.919419Z","shell.execute_reply":"2023-05-27T09:22:13.403950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = np.array(image)\nimg.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-27T09:22:13.410543Z","iopub.execute_input":"2023-05-27T09:22:13.414232Z","iopub.status.idle":"2023-05-27T09:22:13.428057Z","shell.execute_reply.started":"2023-05-27T09:22:13.414194Z","shell.execute_reply":"2023-05-27T09:22:13.427101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### set shape of images","metadata":{}},{"cell_type":"code","source":"IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_CHANNELS = 128,128,3\nIMAGE_SIZE = (IMAGE_WIDTH, IMAGE_HEIGHT)\nFAST_RUN = False","metadata":{"execution":{"iopub.status.busy":"2023-05-27T09:22:13.430618Z","iopub.execute_input":"2023-05-27T09:22:13.437766Z","iopub.status.idle":"2023-05-27T09:22:13.443109Z","shell.execute_reply.started":"2023-05-27T09:22:13.437726Z","shell.execute_reply":"2023-05-27T09:22:13.442156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Build Model","metadata":{}},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense, Activation, BatchNormalization\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))\nmodel.add(Dense(2, activation='softmax')) # 2 because we have cat and dog classes\n\nmodel.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-05-27T09:25:11.701824Z","iopub.execute_input":"2023-05-27T09:25:11.702285Z","iopub.status.idle":"2023-05-27T09:25:15.365817Z","shell.execute_reply.started":"2023-05-27T09:25:11.702251Z","shell.execute_reply":"2023-05-27T09:25:15.365107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Callbacks","metadata":{}},{"cell_type":"code","source":"from keras.callbacks import EarlyStopping, ReduceLROnPlateau","metadata":{"execution":{"iopub.status.busy":"2023-05-27T09:25:15.367358Z","iopub.execute_input":"2023-05-27T09:25:15.367918Z","iopub.status.idle":"2023-05-27T09:25:15.374973Z","shell.execute_reply.started":"2023-05-27T09:25:15.367883Z","shell.execute_reply":"2023-05-27T09:25:15.374298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Early Stop\nTo prevent over fitting we will stop the learning after 10 epochs and val_loss value not decreased","metadata":{}},{"cell_type":"code","source":"earlystop = EarlyStopping(patience=10)","metadata":{"execution":{"iopub.status.busy":"2023-05-27T09:25:15.375981Z","iopub.execute_input":"2023-05-27T09:25:15.376336Z","iopub.status.idle":"2023-05-27T09:25:15.383212Z","shell.execute_reply.started":"2023-05-27T09:25:15.376306Z","shell.execute_reply":"2023-05-27T09:25:15.382380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Learning Rate Reduction\nWe will reduce the learning rate when then accuracy not increase for 2 steps","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)","metadata":{"execution":{"iopub.status.busy":"2023-05-27T09:25:15.385014Z","iopub.execute_input":"2023-05-27T09:25:15.385448Z","iopub.status.idle":"2023-05-27T09:25:15.396159Z","shell.execute_reply.started":"2023-05-27T09:25:15.385417Z","shell.execute_reply":"2023-05-27T09:25:15.395348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callbacks = [earlystop, learning_rate_reduction]","metadata":{"execution":{"iopub.status.busy":"2023-05-27T09:25:18.568630Z","iopub.execute_input":"2023-05-27T09:25:18.568979Z","iopub.status.idle":"2023-05-27T09:25:18.576067Z","shell.execute_reply.started":"2023-05-27T09:25:18.568952Z","shell.execute_reply":"2023-05-27T09:25:18.575070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prepare Data (Preprocessing)","metadata":{}},{"cell_type":"markdown","source":"Because we will use image genaretor `with class_mode=\"categorical\"`. We need to convert column category into string. Then imagenerator will convert it one-hot encoding which is good for our classification.\n\nSo we will convert 1 to dog and 0 to cat","metadata":{}},{"cell_type":"code","source":"df[\"category\"] = df[\"category\"].replace({0: 'cat', 1: 'dog'}) ","metadata":{"execution":{"iopub.status.busy":"2023-05-27T09:25:19.846820Z","iopub.execute_input":"2023-05-27T09:25:19.847242Z","iopub.status.idle":"2023-05-27T09:25:19.862590Z","shell.execute_reply.started":"2023-05-27T09:25:19.847211Z","shell.execute_reply":"2023-05-27T09:25:19.859098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df, validate_df = train_test_split(df, test_size=0.20, random_state=42)\ntrain_df = train_df.reset_index(drop=True)\nvalidate_df = validate_df.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2023-05-27T09:25:20.271472Z","iopub.execute_input":"2023-05-27T09:25:20.272464Z","iopub.status.idle":"2023-05-27T09:25:20.287460Z","shell.execute_reply.started":"2023-05-27T09:25:20.272424Z","shell.execute_reply":"2023-05-27T09:25:20.286515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.index","metadata":{"execution":{"iopub.status.busy":"2023-05-27T09:25:20.679600Z","iopub.execute_input":"2023-05-27T09:25:20.679984Z","iopub.status.idle":"2023-05-27T09:25:20.687771Z","shell.execute_reply.started":"2023-05-27T09:25:20.679954Z","shell.execute_reply":"2023-05-27T09:25:20.686617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['category'].value_counts().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2023-05-27T09:25:22.270630Z","iopub.execute_input":"2023-05-27T09:25:22.270972Z","iopub.status.idle":"2023-05-27T09:25:22.476110Z","shell.execute_reply.started":"2023-05-27T09:25:22.270945Z","shell.execute_reply":"2023-05-27T09:25:22.475094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validate_df['category'].value_counts().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2023-05-27T09:25:23.048441Z","iopub.execute_input":"2023-05-27T09:25:23.048807Z","iopub.status.idle":"2023-05-27T09:25:23.258936Z","shell.execute_reply.started":"2023-05-27T09:25:23.048778Z","shell.execute_reply":"2023-05-27T09:25:23.257952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(validate_df)","metadata":{"execution":{"iopub.status.busy":"2023-05-27T09:25:23.345159Z","iopub.execute_input":"2023-05-27T09:25:23.345448Z","iopub.status.idle":"2023-05-27T09:25:23.352919Z","shell.execute_reply.started":"2023-05-27T09:25:23.345424Z","shell.execute_reply":"2023-05-27T09:25:23.351765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_df)","metadata":{"execution":{"iopub.status.busy":"2023-05-27T09:25:27.563403Z","iopub.execute_input":"2023-05-27T09:25:27.563978Z","iopub.status.idle":"2023-05-27T09:25:27.570411Z","shell.execute_reply.started":"2023-05-27T09:25:27.563945Z","shell.execute_reply":"2023-05-27T09:25:27.569400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total_train = train_df.shape[0]\ntotal_validate = validate_df.shape[0]\nbatch_size=15","metadata":{"execution":{"iopub.status.busy":"2023-05-27T09:25:27.572678Z","iopub.execute_input":"2023-05-27T09:25:27.573435Z","iopub.status.idle":"2023-05-27T09:25:27.579355Z","shell.execute_reply.started":"2023-05-27T09:25:27.573382Z","shell.execute_reply":"2023-05-27T09:25:27.578418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Augmentation","metadata":{}},{"cell_type":"markdown","source":"### Traning Generator","metadata":{}},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n    rotation_range=15,\n    rescale=1./255,\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)\ntrain_generator = train_datagen.flow_from_dataframe(\ntrain_df,\n\"/kaggle/working/train/train\",\nx_col=\"filename\",\ny_col=\"category\",\ntarget_size=IMAGE_SIZE,\nclass_mode=\"categorical\",\nbatch_size=batch_size)","metadata":{"execution":{"iopub.status.busy":"2023-05-27T09:25:27.580705Z","iopub.execute_input":"2023-05-27T09:25:27.581780Z","iopub.status.idle":"2023-05-27T09:25:27.836937Z","shell.execute_reply.started":"2023-05-27T09:25:27.581748Z","shell.execute_reply":"2023-05-27T09:25:27.836011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Validation Generator","metadata":{}},{"cell_type":"code","source":"validation_datagen = ImageDataGenerator(rescale=1./255)\nvalidation_generator = validation_datagen.flow_from_dataframe(\n    validate_df, \n    \"/kaggle/working/train/train\", \n    x_col='filename',\n    y_col='category',\n    target_size=IMAGE_SIZE,\n    class_mode='categorical',\n    batch_size=batch_size\n)","metadata":{"execution":{"iopub.status.busy":"2023-05-27T09:25:27.839221Z","iopub.execute_input":"2023-05-27T09:25:27.839577Z","iopub.status.idle":"2023-05-27T09:25:27.910865Z","shell.execute_reply.started":"2023-05-27T09:25:27.839544Z","shell.execute_reply":"2023-05-27T09:25:27.910066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**See how our generator work**","metadata":{}},{"cell_type":"code","source":"example_df = train_df.sample(n=1).reset_index(drop=True)\nexample_generator = train_datagen.flow_from_dataframe(\n    example_df, \n    \"/kaggle/working/train/train\", \n    x_col='filename',\n    y_col='category',\n    target_size=IMAGE_SIZE,\n    class_mode='categorical'\n)","metadata":{"execution":{"iopub.status.busy":"2023-05-27T09:25:29.106410Z","iopub.execute_input":"2023-05-27T09:25:29.106764Z","iopub.status.idle":"2023-05-27T09:25:29.118777Z","shell.execute_reply.started":"2023-05-27T09:25:29.106735Z","shell.execute_reply":"2023-05-27T09:25:29.117835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"example_generator","metadata":{"execution":{"iopub.status.busy":"2023-05-27T09:25:29.120508Z","iopub.execute_input":"2023-05-27T09:25:29.121673Z","iopub.status.idle":"2023-05-27T09:25:29.134953Z","shell.execute_reply.started":"2023-05-27T09:25:29.121639Z","shell.execute_reply":"2023-05-27T09:25:29.133722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Print the batch size of the generator\nprint('Batch size:', example_generator.batch_size)","metadata":{"execution":{"iopub.status.busy":"2023-05-27T09:25:31.921143Z","iopub.execute_input":"2023-05-27T09:25:31.921494Z","iopub.status.idle":"2023-05-27T09:25:31.927190Z","shell.execute_reply.started":"2023-05-27T09:25:31.921467Z","shell.execute_reply":"2023-05-27T09:25:31.926103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Retrieve the next batch of samples and labels from the generator\nx_example, y_example = next(example_generator)\n\n# Print the shape of the input samples and labels\nprint('Input samples shape:', x_example.shape)\nprint('Input labels shape:', y_example.shape) ","metadata":{"execution":{"iopub.status.busy":"2023-05-27T09:25:31.969670Z","iopub.execute_input":"2023-05-27T09:25:31.970202Z","iopub.status.idle":"2023-05-27T09:25:31.984721Z","shell.execute_reply.started":"2023-05-27T09:25:31.970177Z","shell.execute_reply":"2023-05-27T09:25:31.983750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12, 12))\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":"2023-05-27T09:25:32.601016Z","iopub.execute_input":"2023-05-27T09:25:32.601654Z","iopub.status.idle":"2023-05-27T09:25:34.910375Z","shell.execute_reply.started":"2023-05-27T09:25:32.601613Z","shell.execute_reply":"2023-05-27T09:25:34.909475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Fit Model","metadata":{}},{"cell_type":"code","source":"epochs=5 if FAST_RUN else 50\nhistory = model.fit_generator(\n    train_generator, \n    epochs=epochs,\n    validation_data=validation_generator,\n    validation_steps=total_validate//batch_size,\n    steps_per_epoch=total_train//batch_size,\n    callbacks=callbacks\n)","metadata":{"execution":{"iopub.status.busy":"2023-05-27T09:25:34.911862Z","iopub.execute_input":"2023-05-27T09:25:34.912916Z","iopub.status.idle":"2023-05-27T11:14:48.420353Z","shell.execute_reply.started":"2023-05-27T09:25:34.912883Z","shell.execute_reply":"2023-05-27T11:14:48.419340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Save Model\n","metadata":{}},{"cell_type":"code","source":"model.save_weights(\"model.h5\")","metadata":{"execution":{"iopub.status.busy":"2023-05-27T11:14:50.887433Z","iopub.execute_input":"2023-05-27T11:14:50.888025Z","iopub.status.idle":"2023-05-27T11:14:51.075983Z","shell.execute_reply.started":"2023-05-27T11:14:50.887991Z","shell.execute_reply":"2023-05-27T11:14:51.074935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Virtualize Mode Performance on Training ","metadata":{}},{"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_xlabel(\"Epochs\")\nax1.set_yticks(np.arange(0, 1, 0.1))\nax1.set_ylabel(\"Loss\")\nax1.set_title(\"Model Loss\")\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))\nax2.set_xlabel(\"Epochs\")\nax2.set_ylabel(\"Accuracy\")\nax2.set_title(\"Model Accuracy\")\n\nlegend = plt.legend(loc='best', shadow=True)\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-27T11:22:35.364782Z","iopub.execute_input":"2023-05-27T11:22:35.365199Z","iopub.status.idle":"2023-05-27T11:22:36.329994Z","shell.execute_reply.started":"2023-05-27T11:22:35.365165Z","shell.execute_reply":"2023-05-27T11:22:36.329000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prepare Testing Data\n","metadata":{}},{"cell_type":"code","source":"test_df = pd.DataFrame({\n    'filename': testfilenames\n})\nnb_samples = test_df.shape[0]","metadata":{"execution":{"iopub.status.busy":"2023-05-27T11:24:16.945355Z","iopub.execute_input":"2023-05-27T11:24:16.945765Z","iopub.status.idle":"2023-05-27T11:24:16.955047Z","shell.execute_reply.started":"2023-05-27T11:24:16.945735Z","shell.execute_reply":"2023-05-27T11:24:16.953413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-27T11:24:37.907917Z","iopub.execute_input":"2023-05-27T11:24:37.909009Z","iopub.status.idle":"2023-05-27T11:24:37.923853Z","shell.execute_reply.started":"2023-05-27T11:24:37.908967Z","shell.execute_reply":"2023-05-27T11:24:37.922063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Testing Generator","metadata":{}},{"cell_type":"code","source":"test_gen = ImageDataGenerator(rescale=1./255)\ntest_generator = test_gen.flow_from_dataframe(\n    test_df, \n    \"../working/test/test1\", \n    x_col='filename',\n    y_col=None,\n    class_mode=None,\n    target_size=IMAGE_SIZE,\n    batch_size=batch_size,\n    shuffle=False\n)","metadata":{"execution":{"iopub.status.busy":"2023-05-27T11:26:23.039729Z","iopub.execute_input":"2023-05-27T11:26:23.040135Z","iopub.status.idle":"2023-05-27T11:26:23.182254Z","shell.execute_reply.started":"2023-05-27T11:26:23.040101Z","shell.execute_reply":"2023-05-27T11:26:23.181222Z"},"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":"2023-05-27T11:26:33.250670Z","iopub.execute_input":"2023-05-27T11:26:33.251064Z","iopub.status.idle":"2023-05-27T11:26:53.843091Z","shell.execute_reply.started":"2023-05-27T11:26:33.251033Z","shell.execute_reply":"2023-05-27T11:26:53.842122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"For categoral classication the prediction will come with probability of each category. So we will pick the category that have the highest probability with numpy average max","metadata":{}},{"cell_type":"code","source":"predict","metadata":{"execution":{"iopub.status.busy":"2023-05-27T11:27:12.806548Z","iopub.execute_input":"2023-05-27T11:27:12.806935Z","iopub.status.idle":"2023-05-27T11:27:12.814973Z","shell.execute_reply.started":"2023-05-27T11:27:12.806905Z","shell.execute_reply":"2023-05-27T11:27:12.813820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['category'] = np.argmax(predict, axis=-1)","metadata":{"execution":{"iopub.status.busy":"2023-05-27T11:27:35.182877Z","iopub.execute_input":"2023-05-27T11:27:35.183263Z","iopub.status.idle":"2023-05-27T11:27:35.192272Z","shell.execute_reply.started":"2023-05-27T11:27:35.183232Z","shell.execute_reply":"2023-05-27T11:27:35.191217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-27T11:27:58.666626Z","iopub.execute_input":"2023-05-27T11:27:58.666999Z","iopub.status.idle":"2023-05-27T11:27:58.677985Z","shell.execute_reply.started":"2023-05-27T11:27:58.666970Z","shell.execute_reply":"2023-05-27T11:27:58.676742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We will convert the predict category back into our generator classes by using `train_generator.class_indices`. It is the classes that image generator map while converting data into computer vision","metadata":{}},{"cell_type":"code","source":"label_map = dict((v,k) for k,v in train_generator.class_indices.items())\nlabel_map","metadata":{"execution":{"iopub.status.busy":"2023-05-27T12:02:33.871283Z","iopub.execute_input":"2023-05-27T12:02:33.872248Z","iopub.status.idle":"2023-05-27T12:02:33.882316Z","shell.execute_reply.started":"2023-05-27T12:02:33.872179Z","shell.execute_reply":"2023-05-27T12:02:33.881111Z"},"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['category'] = test_df['category'].replace(label_map)","metadata":{"execution":{"iopub.status.busy":"2023-05-27T12:03:27.603455Z","iopub.execute_input":"2023-05-27T12:03:27.604176Z","iopub.status.idle":"2023-05-27T12:03:27.613273Z","shell.execute_reply.started":"2023-05-27T12:03:27.604140Z","shell.execute_reply":"2023-05-27T12:03:27.612170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-27T12:04:03.613259Z","iopub.execute_input":"2023-05-27T12:04:03.613628Z","iopub.status.idle":"2023-05-27T12:04:03.625960Z","shell.execute_reply.started":"2023-05-27T12:04:03.613600Z","shell.execute_reply":"2023-05-27T12:04:03.624679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"From our prepare data part. We map data with `{0: 'cat', 1: 'dog'}`. Now we will map the result back to dog is 1 and cat is 0\n\n","metadata":{}},{"cell_type":"code","source":"test_df['category'] = test_df['category'].replace({ 'cat': 0, 'dog': 1 })","metadata":{"execution":{"iopub.status.busy":"2023-05-27T12:05:51.698687Z","iopub.execute_input":"2023-05-27T12:05:51.699213Z","iopub.status.idle":"2023-05-27T12:05:51.719367Z","shell.execute_reply.started":"2023-05-27T12:05:51.699171Z","shell.execute_reply":"2023-05-27T12:05:51.718440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Virtaulize Result","metadata":{}},{"cell_type":"code","source":"test_df['category'].value_counts().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2023-05-27T12:05:55.622904Z","iopub.execute_input":"2023-05-27T12:05:55.623271Z","iopub.status.idle":"2023-05-27T12:05:55.833965Z","shell.execute_reply.started":"2023-05-27T12:05:55.623242Z","shell.execute_reply":"2023-05-27T12:05:55.832972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# See predicted result with images","metadata":{}},{"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['filename']\n    category = row['category']\n    img = load_img(\"../working/test/test1/\"+filename, target_size=IMAGE_SIZE)\n    plt.subplot(6, 3, index+1)\n    plt.imshow(img)\n    plt.xlabel(filename +' --> predicted as ' +'(' + \"{}\".format(category)+': '+label_map[category]+')')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-27T12:11:25.141217Z","iopub.execute_input":"2023-05-27T12:11:25.141829Z","iopub.status.idle":"2023-05-27T12:11:29.797578Z","shell.execute_reply.started":"2023-05-27T12:11:25.141792Z","shell.execute_reply":"2023-05-27T12:11:29.795163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission","metadata":{}},{"cell_type":"code","source":"submission_df = test_df.copy()\nsubmission_df['id'] = submission_df['filename'].str.split('.').str[0]\nsubmission_df['label'] = submission_df['category']\nsubmission_df.drop(['filename', 'category'], axis=1, inplace=True)\nsubmission_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-05-27T12:12:21.028312Z","iopub.execute_input":"2023-05-27T12:12:21.028683Z","iopub.status.idle":"2023-05-27T12:12:21.087451Z","shell.execute_reply.started":"2023-05-27T12:12:21.028654Z","shell.execute_reply":"2023-05-27T12:12:21.086472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Thanks","metadata":{}}]}