{"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 Library","metadata":{}},{"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\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":{"execution":{"iopub.status.busy":"2023-08-14T18:44:40.527793Z","iopub.execute_input":"2023-08-14T18:44:40.528201Z","iopub.status.idle":"2023-08-14T18:44:40.536127Z","shell.execute_reply.started":"2023-08-14T18:44:40.528167Z","shell.execute_reply":"2023-08-14T18:44:40.535150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\n# code from : https://www.kaggle.com/code/nirmit19/cats-vs-dogs-vgg-19\ntrain_path = \"../input/c/dogs-vs-cats/test1.zip\"\ntest_path = \"../input/c/dogs-vs-cats/train.zip\"\n\nfiles = \"/kaggle/files/unzipped/\"\n\nwith zipfile.ZipFile(train_path, 'r') as zipp:\n    zipp.extractall(files)\n    \nwith zipfile.ZipFile(test_path, 'r') as zipp:\n    zipp.extractall(files)\n","metadata":{"execution":{"iopub.status.busy":"2023-08-14T20:00:06.419866Z","iopub.execute_input":"2023-08-14T20:00:06.420265Z","iopub.status.idle":"2023-08-14T20:00:23.221137Z","shell.execute_reply.started":"2023-08-14T20:00:06.420229Z","shell.execute_reply":"2023-08-14T20:00:23.220129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd \nimport tensorflow as tf\nfrom keras.preprocessing.image import ImageDataGenerator, load_img\nfrom tensorflow.keras.utils import to_categorical\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt\nimport random\nimport os\nprint(os.listdir(\"../input\"))","metadata":{"execution":{"iopub.status.busy":"2023-08-14T18:45:00.296474Z","iopub.execute_input":"2023-08-14T18:45:00.297134Z","iopub.status.idle":"2023-08-14T18:45:06.228836Z","shell.execute_reply.started":"2023-08-14T18:45:00.297067Z","shell.execute_reply":"2023-08-14T18:45:06.226484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"FAST_RUN = False\nIMAGE_WIDTH=128\nIMAGE_HEIGHT=128\nIMAGE_SIZE=(IMAGE_WIDTH, IMAGE_HEIGHT)\nIMAGE_CHANNELS=3","metadata":{"execution":{"iopub.status.busy":"2023-08-14T18:45:06.230599Z","iopub.execute_input":"2023-08-14T18:45:06.231322Z","iopub.status.idle":"2023-08-14T18:45:06.238734Z","shell.execute_reply.started":"2023-08-14T18:45:06.231279Z","shell.execute_reply":"2023-08-14T18:45:06.237725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prepare Traning Data","metadata":{}},{"cell_type":"code","source":"filenames = os.listdir(\"/kaggle/files/unzipped/train\")\ncategories = []\nfor filename in filenames:\n    category = filename.split('.')[0]\n    if category == 'dog':\n        categories.append(1)\n    else:\n        categories.append(0)\n\ndf = pd.DataFrame({\n    'filename': filenames,\n    'category': categories\n})","metadata":{"execution":{"iopub.status.busy":"2023-08-14T18:45:06.241790Z","iopub.execute_input":"2023-08-14T18:45:06.242188Z","iopub.status.idle":"2023-08-14T18:45:07.514412Z","shell.execute_reply.started":"2023-08-14T18:45:06.242149Z","shell.execute_reply":"2023-08-14T18:45:07.513370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2023-08-14T18:45:07.515825Z","iopub.execute_input":"2023-08-14T18:45:07.516872Z","iopub.status.idle":"2023-08-14T18:45:07.537033Z","shell.execute_reply.started":"2023-08-14T18:45:07.516829Z","shell.execute_reply":"2023-08-14T18:45:07.536107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['category'].value_counts().plot.bar()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-14T18:45:07.538651Z","iopub.execute_input":"2023-08-14T18:45:07.539029Z","iopub.status.idle":"2023-08-14T18:45:07.754118Z","shell.execute_reply.started":"2023-08-14T18:45:07.538991Z","shell.execute_reply":"2023-08-14T18:45:07.753145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# See sample image","metadata":{}},{"cell_type":"code","source":"# Train data samples\n\nfig, ax = plt.subplots(5, 5, figsize = (17,17))\nax = ax.ravel() # flaten the matrix into array\nfor i in np.arange(0, 5 * 5): \n    label ='NORMAL'\n    class_dir = os.path.join(\"/kaggle/files/unzipped/train\")\n    # Select a random image\n    image = random.choice(os.listdir(class_dir))\n    # read and display an image with the selected index    \n    img = plt.imread(os.path.join(class_dir,image))\n    ax[i].imshow( img )\n    ax[i].set_title(label, fontsize = 8) # the label\n    ax[i].axis('off')","metadata":{"execution":{"iopub.status.busy":"2023-08-14T18:45:07.755638Z","iopub.execute_input":"2023-08-14T18:45:07.755992Z","iopub.status.idle":"2023-08-14T18:45:10.231608Z","shell.execute_reply.started":"2023-08-14T18:45:07.755952Z","shell.execute_reply":"2023-08-14T18:45:10.230287Z"},"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)))\n#model.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='sigmoid')) # 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-08-14T18:45:10.233006Z","iopub.execute_input":"2023-08-14T18:45:10.233384Z","iopub.status.idle":"2023-08-14T18:45:13.168239Z","shell.execute_reply.started":"2023-08-14T18:45:10.233348Z","shell.execute_reply":"2023-08-14T18:45:13.167160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Callbacks & Early Stop","metadata":{}},{"cell_type":"code","source":"from keras.callbacks import EarlyStopping, ReduceLROnPlateau\nearlystop = EarlyStopping(patience=10)","metadata":{"execution":{"iopub.status.busy":"2023-08-14T18:45:13.169514Z","iopub.execute_input":"2023-08-14T18:45:13.170241Z","iopub.status.idle":"2023-08-14T18:45:13.175114Z","shell.execute_reply.started":"2023-08-14T18:45:13.170208Z","shell.execute_reply":"2023-08-14T18:45:13.174153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Learning Rate Reduction\n\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-08-14T18:45:13.180600Z","iopub.execute_input":"2023-08-14T18:45:13.181582Z","iopub.status.idle":"2023-08-14T18:45:13.205929Z","shell.execute_reply.started":"2023-08-14T18:45:13.181542Z","shell.execute_reply":"2023-08-14T18:45:13.204910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callbacks = [earlystop, learning_rate_reduction]","metadata":{"execution":{"iopub.status.busy":"2023-08-14T18:45:13.207648Z","iopub.execute_input":"2023-08-14T18:45:13.208049Z","iopub.status.idle":"2023-08-14T18:45:13.218515Z","shell.execute_reply.started":"2023-08-14T18:45:13.208004Z","shell.execute_reply":"2023-08-14T18:45:13.217424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prepare data","metadata":{}},{"cell_type":"code","source":"df[\"category\"] = df[\"category\"].replace({0: 'cat', 1: 'dog'}) ","metadata":{"execution":{"iopub.status.busy":"2023-08-14T18:45:13.219633Z","iopub.execute_input":"2023-08-14T18:45:13.219928Z","iopub.status.idle":"2023-08-14T18:45:13.233119Z","shell.execute_reply.started":"2023-08-14T18:45:13.219888Z","shell.execute_reply":"2023-08-14T18:45:13.232064Z"},"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-08-14T18:45:13.235187Z","iopub.execute_input":"2023-08-14T18:45:13.235992Z","iopub.status.idle":"2023-08-14T18:45:13.248011Z","shell.execute_reply.started":"2023-08-14T18:45:13.235952Z","shell.execute_reply":"2023-08-14T18:45:13.246927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['category'].value_counts().plot.bar()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-14T18:45:13.249634Z","iopub.execute_input":"2023-08-14T18:45:13.250051Z","iopub.status.idle":"2023-08-14T18:45:13.420038Z","shell.execute_reply.started":"2023-08-14T18:45:13.250012Z","shell.execute_reply":"2023-08-14T18:45:13.419008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validate_df['category'].value_counts().plot.bar()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-14T18:45:13.421586Z","iopub.execute_input":"2023-08-14T18:45:13.421925Z","iopub.status.idle":"2023-08-14T18:45:13.595464Z","shell.execute_reply.started":"2023-08-14T18:45:13.421888Z","shell.execute_reply":"2023-08-14T18:45:13.594432Z"},"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-08-14T18:45:13.597179Z","iopub.execute_input":"2023-08-14T18:45:13.597546Z","iopub.status.idle":"2023-08-14T18:45:13.605340Z","shell.execute_reply.started":"2023-08-14T18:45:13.597507Z","shell.execute_reply":"2023-08-14T18:45:13.604253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Traning Generator","metadata":{}},{"cell_type":"code","source":"\ntrain_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)\n\ntrain_generator = train_datagen.flow_from_dataframe(\n    train_df, \n    \"/kaggle/files/unzipped/train\", \n    x_col='filename',\n    y_col='category',\n    target_size=IMAGE_SIZE,\n    class_mode='categorical',\n    batch_size=batch_size\n)\n# #VGG16 = tf.keras.applications.VGG16(include_top=False, weights='imagenet')\n# bottleneck_features_train = model.predict_generator(train_datagen, 2000)\n# np.save(open('bottleneck_features_train.npy', 'w'), bottleneck_features_train)","metadata":{"execution":{"iopub.status.busy":"2023-08-14T18:45:13.607156Z","iopub.execute_input":"2023-08-14T18:45:13.607579Z","iopub.status.idle":"2023-08-14T18:45:13.884780Z","shell.execute_reply.started":"2023-08-14T18:45:13.607540Z","shell.execute_reply":"2023-08-14T18:45:13.883142Z"},"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/files/unzipped/train\", \n    x_col='filename',\n    y_col='category',\n    target_size=IMAGE_SIZE,\n    class_mode='categorical',\n    batch_size=batch_size\n)\n# bottleneck_features_validation = VGG16.model.predict_generator(validation_generator, 800)\n# np.save(open('bottleneck_features_validation.npy', 'w'), bottleneck_features_validation)","metadata":{"execution":{"iopub.status.busy":"2023-08-14T18:45:13.890444Z","iopub.execute_input":"2023-08-14T18:45:13.891211Z","iopub.status.idle":"2023-08-14T18:45:14.018649Z","shell.execute_reply.started":"2023-08-14T18:45:13.891126Z","shell.execute_reply":"2023-08-14T18:45:14.016487Z"},"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/files/unzipped/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-08-14T18:45:14.020169Z","iopub.execute_input":"2023-08-14T18:45:14.020533Z","iopub.status.idle":"2023-08-14T18:45:14.032705Z","shell.execute_reply.started":"2023-08-14T18:45:14.020503Z","shell.execute_reply":"2023-08-14T18:45:14.031505Z"},"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-08-14T18:45:14.034666Z","iopub.execute_input":"2023-08-14T18:45:14.035440Z","iopub.status.idle":"2023-08-14T18:45:15.850422Z","shell.execute_reply.started":"2023-08-14T18:45:14.035401Z","shell.execute_reply":"2023-08-14T18:45:15.849530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Fit Model","metadata":{}},{"cell_type":"code","source":"epochs=3 if FAST_RUN else 50\nhistory = model.fit(\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)\n# s=150\n# from tensorflow.keras.applications import VGG16\n# conv_base = VGG16(weights='imagenet',\n#                    include_top=False,\n#                    input_shape=(s, s, 3))\n# from tensorflow.keras import models\n# from tensorflow.keras import layers\n\n# model = models.Sequential()\n# model.add(conv_base)\n# model.add(layers.Flatten())\n# model.add(layers.Dense(256, activation='relu'))\n# model.add(layers.Dense(2, activation='sigmoid'))\n# model.summary()\n# model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-08-14T18:45:15.851761Z","iopub.execute_input":"2023-08-14T18:45:15.852917Z","iopub.status.idle":"2023-08-14T19:56:18.949979Z","shell.execute_reply.started":"2023-08-14T18:45:15.852876Z","shell.execute_reply":"2023-08-14T19:56:18.949019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":" # Save Model","metadata":{}},{"cell_type":"code","source":"  model.save_weights(\"model11.h5\")","metadata":{"execution":{"iopub.status.busy":"2023-08-14T19:56:55.723051Z","iopub.execute_input":"2023-08-14T19:56:55.723446Z","iopub.status.idle":"2023-08-14T19:56:55.856198Z","shell.execute_reply.started":"2023-08-14T19:56:55.723412Z","shell.execute_reply":"2023-08-14T19:56:55.855129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Virtualize 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_yticks(np.arange(0, 1, 0.1))\n\n# Use 'accuracy' and 'val_accuracy' instead of 'acc' and 'val_acc'\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()\n","metadata":{"execution":{"iopub.status.busy":"2023-08-14T19:58:40.872035Z","iopub.execute_input":"2023-08-14T19:58:40.872792Z","iopub.status.idle":"2023-08-14T19:58:41.787047Z","shell.execute_reply.started":"2023-08-14T19:58:40.872752Z","shell.execute_reply":"2023-08-14T19:58:41.784147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prepare Testing Data","metadata":{}}]}