{"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":"2021-09-26T16:00:25.623915Z","iopub.execute_input":"2021-09-26T16:00:25.62464Z","iopub.status.idle":"2021-09-26T16:00:25.719196Z","shell.execute_reply.started":"2021-09-26T16:00:25.624548Z","shell.execute_reply":"2021-09-26T16:00:25.718337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This practice notebook is based on [this notebook](https://www.kaggle.com/uysimty/keras-cnn-dog-or-cat-classification)","metadata":{}},{"cell_type":"code","source":"import zipfile\n\nwith zipfile.ZipFile('/kaggle/input/dogs-vs-cats/train.zip', 'r') as z1:\n    z1.extractall()\n\nwith zipfile.ZipFile('/kaggle/input/dogs-vs-cats/test1.zip', 'r') as z2:\n    z2.extractall()","metadata":{"execution":{"iopub.status.busy":"2021-09-26T16:00:25.721211Z","iopub.execute_input":"2021-09-26T16:00:25.721489Z","iopub.status.idle":"2021-09-26T16:00:42.233808Z","shell.execute_reply.started":"2021-09-26T16:00:25.721454Z","shell.execute_reply":"2021-09-26T16:00:42.233051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport os\n\n\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2021-09-26T16:00:42.235236Z","iopub.execute_input":"2021-09-26T16:00:42.235535Z","iopub.status.idle":"2021-09-26T16:00:47.427361Z","shell.execute_reply.started":"2021-09-26T16:00:42.2355Z","shell.execute_reply":"2021-09-26T16:00:47.426623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(os.listdir('./'))","metadata":{"execution":{"iopub.status.busy":"2021-09-26T16:00:47.428637Z","iopub.execute_input":"2021-09-26T16:00:47.428988Z","iopub.status.idle":"2021-09-26T16:00:47.434309Z","shell.execute_reply.started":"2021-09-26T16:00:47.428951Z","shell.execute_reply":"2021-09-26T16:00:47.433405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# prepare training data\nfilenames = os.listdir('./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({'filename': filenames,\n                  'category': categories})","metadata":{"execution":{"iopub.status.busy":"2021-09-26T16:00:47.437147Z","iopub.execute_input":"2021-09-26T16:00:47.437816Z","iopub.status.idle":"2021-09-26T16:00:47.486921Z","shell.execute_reply.started":"2021-09-26T16:00:47.437776Z","shell.execute_reply":"2021-09-26T16:00:47.486273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-26T16:00:47.489409Z","iopub.execute_input":"2021-09-26T16:00:47.489599Z","iopub.status.idle":"2021-09-26T16:00:47.513292Z","shell.execute_reply.started":"2021-09-26T16:00:47.489578Z","shell.execute_reply":"2021-09-26T16:00:47.512655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x='category', data=df)","metadata":{"execution":{"iopub.status.busy":"2021-09-26T16:00:47.514465Z","iopub.execute_input":"2021-09-26T16:00:47.514731Z","iopub.status.idle":"2021-09-26T16:00:47.71312Z","shell.execute_reply.started":"2021-09-26T16:00:47.514698Z","shell.execute_reply":"2021-09-26T16:00:47.71243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# look at image\nplt.figure(figsize=(12,12))\nfor i in range(0, 10):\n    plt.subplot(5, 3, i+1)\n    sample = df['filename'][i]\n    img = load_img('./train/'+sample)\n    plt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2021-09-26T16:00:47.714557Z","iopub.execute_input":"2021-09-26T16:00:47.715046Z","iopub.status.idle":"2021-09-26T16:00:48.994558Z","shell.execute_reply.started":"2021-09-26T16:00:47.715003Z","shell.execute_reply":"2021-09-26T16:00:48.993847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# build model\nimg_width = 128\nimg_height = 128\nimg_channel = 3\n\nmodel = keras.models.Sequential([\n    # conv 1\n    keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(img_width, img_height, img_channel)),\n    keras.layers.BatchNormalization(),\n    keras.layers.MaxPooling2D(pool_size=(2,2)),\n    keras.layers.Dropout(0.25),\n    # conv 2\n    keras.layers.Conv2D(64, (3,3), activation='relu'),\n    keras.layers.BatchNormalization(),\n    keras.layers.MaxPooling2D(pool_size=(2,2)),\n    keras.layers.Dropout(0.25),\n    # conv 3\n    keras.layers.Conv2D(128, (3,3), activation='relu'),\n    keras.layers.BatchNormalization(),\n    keras.layers.MaxPooling2D(pool_size=(2,2)),\n    keras.layers.Dropout(0.25),\n    # flatten\n    keras.layers.Flatten(),\n    # full conenct\n    keras.layers.Dense(512, activation='relu'),\n    keras.layers.BatchNormalization(),\n    keras.layers.Dropout(0.5),\n    # output\n    keras.layers.Dense(2, activation='softmax')\n])\n\nmodel.compile(loss='categorical_crossentropy',\n             optimizer='rmsprop',\n             metrics=['accuracy'])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-09-26T16:00:48.99548Z","iopub.execute_input":"2021-09-26T16:00:48.995696Z","iopub.status.idle":"2021-09-26T16:00:51.211752Z","shell.execute_reply.started":"2021-09-26T16:00:48.995669Z","shell.execute_reply":"2021-09-26T16:00:51.211059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# callback\n\n# prevent over fitting, it stops afther 10 epochs and val_loss value not decreased\nearlystop = keras.callbacks.EarlyStopping(patience=10)\n\n# it reduces learning rate when accuracy not increase for 2 steps\nlearning_rate_reduction = keras.callbacks.ReduceLROnPlateau(monitor='val_acc',\n                                                           patience=2,\n                                                           verbose=1,\n                                                           factor=0.5,\n                                                           min_lr=0.00001)\n\ncallbacks = [earlystop, learning_rate_reduction]","metadata":{"execution":{"iopub.status.busy":"2021-09-26T16:00:51.213662Z","iopub.execute_input":"2021-09-26T16:00:51.214075Z","iopub.status.idle":"2021-09-26T16:00:51.220996Z","shell.execute_reply.started":"2021-09-26T16:00:51.214029Z","shell.execute_reply":"2021-09-26T16:00:51.220268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# prepare training data\n\n# train data with image generator, which requries stirng value, then it will convert it ont-hot encoding\ndf['category'] = df['category'].replace({0: 'cat', 1: 'dog'})\n\ndf_train, df_validate = train_test_split(df, test_size=0.2, random_state=42)\ndf_train = df_train.reset_index(drop=True)\ndf_validate = df_validate.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2021-09-26T16:00:51.222383Z","iopub.execute_input":"2021-09-26T16:00:51.222811Z","iopub.status.idle":"2021-09-26T16:00:51.237284Z","shell.execute_reply.started":"2021-09-26T16:00:51.222777Z","shell.execute_reply":"2021-09-26T16:00:51.23641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(data=df_train, x='category')","metadata":{"execution":{"iopub.status.busy":"2021-09-26T16:00:51.238602Z","iopub.execute_input":"2021-09-26T16:00:51.238936Z","iopub.status.idle":"2021-09-26T16:00:51.444875Z","shell.execute_reply.started":"2021-09-26T16:00:51.238903Z","shell.execute_reply":"2021-09-26T16:00:51.443212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(data=df_validate, x='category')","metadata":{"execution":{"iopub.status.busy":"2021-09-26T16:00:51.447154Z","iopub.execute_input":"2021-09-26T16:00:51.447421Z","iopub.status.idle":"2021-09-26T16:00:51.641696Z","shell.execute_reply.started":"2021-09-26T16:00:51.447389Z","shell.execute_reply":"2021-09-26T16:00:51.640947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total_train = df_train.shape[0]\ntotal_validate = df_validate.shape[0]\nbatch_size = 15\nimg_size = (img_width, img_height)\n\n# the generators\ntrain_gen = ImageDataGenerator(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_gen.flow_from_dataframe(df_train,\n                                               './train/',\n                                               x_col='filename',\n                                               y_col='category',\n                                               target_size=img_size,\n                                               class_mode='categorical',\n                                               batch_size=batch_size)","metadata":{"execution":{"iopub.status.busy":"2021-09-26T16:00:51.649069Z","iopub.execute_input":"2021-09-26T16:00:51.651734Z","iopub.status.idle":"2021-09-26T16:00:52.060523Z","shell.execute_reply.started":"2021-09-26T16:00:51.651698Z","shell.execute_reply":"2021-09-26T16:00:52.059773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validate_gen = ImageDataGenerator(rescale=1./255)\nvalidate_generator = train_gen.flow_from_dataframe(df_validate,\n                                               './train/',\n                                               x_col='filename',\n                                               y_col='category',\n                                               target_size=img_size,\n                                               class_mode='categorical',\n                                               batch_size=batch_size)","metadata":{"execution":{"iopub.status.busy":"2021-09-26T16:00:52.064788Z","iopub.execute_input":"2021-09-26T16:00:52.067033Z","iopub.status.idle":"2021-09-26T16:00:52.166193Z","shell.execute_reply.started":"2021-09-26T16:00:52.066989Z","shell.execute_reply":"2021-09-26T16:00:52.162559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# show how generator work\nexample_df = df_train.sample(n=1).reset_index(drop=True)\nexample_generator = train_gen.flow_from_dataframe(\n    example_df, \n    \"./train/\", \n    x_col='filename',\n    y_col='category',\n    target_size=img_size,\n    class_mode='categorical'\n)","metadata":{"execution":{"iopub.status.busy":"2021-09-26T16:00:52.181701Z","iopub.execute_input":"2021-09-26T16:00:52.188558Z","iopub.status.idle":"2021-09-26T16:00:52.263007Z","shell.execute_reply.started":"2021-09-26T16:00:52.188505Z","shell.execute_reply":"2021-09-26T16:00:52.256325Z"},"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        img = X_batch[0]\n        plt.imshow(img)\n        break\n        \nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-26T16:00:52.264026Z","iopub.execute_input":"2021-09-26T16:00:52.264293Z","iopub.status.idle":"2021-09-26T16:00:54.473037Z","shell.execute_reply.started":"2021-09-26T16:00:52.264259Z","shell.execute_reply":"2021-09-26T16:00:54.47223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# fit model\nfast_run = False\nepochs = 5 if fast_run else 30\nhistory = model.fit_generator(train_generator,\n                             epochs=epochs,\n                             validation_data=validate_generator,\n                             validation_steps=total_validate//batch_size,\n                             steps_per_epoch=total_train//batch_size,\n                             callbacks=callbacks)","metadata":{"execution":{"iopub.status.busy":"2021-09-26T16:00:54.474502Z","iopub.execute_input":"2021-09-26T16:00:54.474758Z","iopub.status.idle":"2021-09-26T16:28:47.869214Z","shell.execute_reply.started":"2021-09-26T16:00:54.474727Z","shell.execute_reply":"2021-09-26T16:28:47.866066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# save model\nmodel.save_weights(\"model.h5\")","metadata":{"execution":{"iopub.status.busy":"2021-09-26T16:28:52.829618Z","iopub.execute_input":"2021-09-26T16:28:52.830177Z","iopub.status.idle":"2021-09-26T16:28:52.956633Z","shell.execute_reply.started":"2021-09-26T16:28:52.83014Z","shell.execute_reply":"2021-09-26T16:28:52.955847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# virtulize training\nfig, (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\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":"2021-09-26T16:28:55.130392Z","iopub.execute_input":"2021-09-26T16:28:55.130923Z","iopub.status.idle":"2021-09-26T16:28:55.456193Z","shell.execute_reply.started":"2021-09-26T16:28:55.130887Z","shell.execute_reply":"2021-09-26T16:28:55.454484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# testing data\ntest_filenames = os.listdir('./test1')\ndf_test = pd.DataFrame({'filename': test_filenames})\n\ntotal_test = df_test.shape[0]\n\ntest_gen = ImageDataGenerator(rescale=1./255)\ntest_generator = test_gen.flow_from_dataframe(\n    df_test,\n    './test1/', \n    x_col='filename',\n    y_col=None,\n    class_mode=None,\n    target_size=img_size,\n    batch_size=batch_size,\n    shuffle=False\n)","metadata":{"execution":{"iopub.status.busy":"2021-09-26T16:28:47.881585Z","iopub.status.idle":"2021-09-26T16:28:47.882428Z","shell.execute_reply.started":"2021-09-26T16:28:47.882201Z","shell.execute_reply":"2021-09-26T16:28:47.882225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predict\npredict = model.predict(test_generator,steps=np.ceil(total_test/batch_size))","metadata":{"execution":{"iopub.status.busy":"2021-09-26T16:28:47.883867Z","iopub.status.idle":"2021-09-26T16:28:47.884385Z","shell.execute_reply.started":"2021-09-26T16:28:47.884114Z","shell.execute_reply":"2021-09-26T16:28:47.884137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test['category'] = np.argmax(predict, axis=-1)","metadata":{"execution":{"iopub.status.busy":"2021-09-26T16:28:47.886042Z","iopub.status.idle":"2021-09-26T16:28:47.886387Z","shell.execute_reply.started":"2021-09-26T16:28:47.886201Z","shell.execute_reply":"2021-09-26T16:28:47.886221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-26T16:28:47.892081Z","iopub.status.idle":"2021-09-26T16:28:47.892433Z","shell.execute_reply.started":"2021-09-26T16:28:47.892233Z","shell.execute_reply":"2021-09-26T16:28:47.892255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# label_map = dict((v,k) for k,v in train_generator.class_indices.items())\n# df_test['category'] = df_test['category'].replace(label_map)","metadata":{"execution":{"iopub.status.busy":"2021-09-26T16:28:47.893832Z","iopub.status.idle":"2021-09-26T16:28:47.894184Z","shell.execute_reply.started":"2021-09-26T16:28:47.893992Z","shell.execute_reply":"2021-09-26T16:28:47.894014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df_test.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-26T16:28:47.895337Z","iopub.status.idle":"2021-09-26T16:28:47.895679Z","shell.execute_reply.started":"2021-09-26T16:28:47.895509Z","shell.execute_reply":"2021-09-26T16:28:47.895531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df_test['category'] = df_test['category'].replace({'dog': 1, 'cat': 0})\n# df_test.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-26T16:28:47.89693Z","iopub.status.idle":"2021-09-26T16:28:47.897265Z","shell.execute_reply.started":"2021-09-26T16:28:47.897097Z","shell.execute_reply":"2021-09-26T16:28:47.897118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(data=df_test, x='category')","metadata":{"execution":{"iopub.status.busy":"2021-09-26T16:28:47.902801Z","iopub.status.idle":"2021-09-26T16:28:47.903229Z","shell.execute_reply.started":"2021-09-26T16:28:47.902953Z","shell.execute_reply":"2021-09-26T16:28:47.902978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# show predict\n\nsample_test = df_test.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(\"./test1/\"+filename, target_size=img_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":"2021-09-26T16:28:47.905613Z","iopub.status.idle":"2021-09-26T16:28:47.906169Z","shell.execute_reply.started":"2021-09-26T16:28:47.905884Z","shell.execute_reply":"2021-09-26T16:28:47.905909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}