{"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":{"execution":{"iopub.status.busy":"2021-08-22T09:33:58.083800Z","iopub.execute_input":"2021-08-22T09:33:58.084144Z","iopub.status.idle":"2021-08-22T09:33:58.097814Z","shell.execute_reply.started":"2021-08-22T09:33:58.084069Z","shell.execute_reply":"2021-08-22T09:33:58.096742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip -q ../input/dogs-vs-cats/test1.zip\n!unzip -q ../input/dogs-vs-cats/train.zip","metadata":{"execution":{"iopub.status.busy":"2021-08-22T09:34:59.060328Z","iopub.execute_input":"2021-08-22T09:34:59.060656Z","iopub.status.idle":"2021-08-22T09:35:16.332580Z","shell.execute_reply.started":"2021-08-22T09:34:59.060626Z","shell.execute_reply":"2021-08-22T09:35:16.331427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir Train\n!mkdir Validation\n!mkdir Train/cat\n!mkdir Train/dog\n!mkdir Validation/cat\n!mkdir Validation/dog","metadata":{"id":"xq_-9rWzVxug","execution":{"iopub.status.busy":"2021-08-22T09:35:38.893781Z","iopub.execute_input":"2021-08-22T09:35:38.894118Z","iopub.status.idle":"2021-08-22T09:35:42.673832Z","shell.execute_reply.started":"2021-08-22T09:35:38.894084Z","shell.execute_reply":"2021-08-22T09:35:42.672815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport tensorflow\nimport tensorflow.keras\nfrom tensorflow.keras.models import Model, Sequential\nfrom tensorflow.keras.layers import Input, Dense, Dropout, GlobalMaxPooling2D\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.applications.vgg16 import VGG16, preprocess_input\nfrom tqdm import tqdm\nimport os\nimport cv2\nimport shutil\nfrom random import shuffle\nimport matplotlib.pyplot as plt\n# from google.colab.patches import cv2_imshow\n%matplotlib inline","metadata":{"id":"TYopVYtgWnpp","execution":{"iopub.status.busy":"2021-08-22T09:36:25.528476Z","iopub.execute_input":"2021-08-22T09:36:25.528858Z","iopub.status.idle":"2021-08-22T09:36:30.062538Z","shell.execute_reply.started":"2021-08-22T09:36:25.528820Z","shell.execute_reply":"2021-08-22T09:36:30.061616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files = os.listdir('train')\nshuffle(files)","metadata":{"id":"lyyxhf9_W3n-","execution":{"iopub.status.busy":"2021-08-22T09:36:37.249003Z","iopub.execute_input":"2021-08-22T09:36:37.249329Z","iopub.status.idle":"2021-08-22T09:36:37.288995Z","shell.execute_reply.started":"2021-08-22T09:36:37.249298Z","shell.execute_reply":"2021-08-22T09:36:37.288239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.path.isdir('train/')","metadata":{"execution":{"iopub.status.busy":"2021-08-22T09:36:38.839892Z","iopub.execute_input":"2021-08-22T09:36:38.840215Z","iopub.status.idle":"2021-08-22T09:36:38.850832Z","shell.execute_reply.started":"2021-08-22T09:36:38.840184Z","shell.execute_reply":"2021-08-22T09:36:38.849984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# cv2.imshow(cv2.imread('train/' + files[4]))","metadata":{"id":"4ZEy8XNgbldq","outputId":"94e9bb2e-c4db-449f-cbe1-fd4bf4842814","execution":{"iopub.status.busy":"2021-08-22T09:36:41.003705Z","iopub.execute_input":"2021-08-22T09:36:41.004058Z","iopub.status.idle":"2021-08-22T09:36:41.008333Z","shell.execute_reply.started":"2021-08-22T09:36:41.004027Z","shell.execute_reply":"2021-08-22T09:36:41.007324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_path = 'train'\ndest_path1 = 'Train'\ndest_path2 = 'Validation'","metadata":{"id":"GsRVi-Ugb3-X","execution":{"iopub.status.busy":"2021-08-22T09:36:41.932870Z","iopub.execute_input":"2021-08-22T09:36:41.933209Z","iopub.status.idle":"2021-08-22T09:36:41.936998Z","shell.execute_reply.started":"2021-08-22T09:36:41.933178Z","shell.execute_reply":"2021-08-22T09:36:41.935923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_split = int(len(files) * 0.1)\ndog_split = int(len(files) * 0.1)\ncat_counter = 0\ndog_counter = 0\n\nfor i in tqdm(range(len(files))):\n    img = files[i]\n    pet = img.split('.')[0]\n    \n    if pet == 'dog':\n        if dog_counter < dog_split:\n            shutil.copyfile(\n                os.path.join(base_path, img),\n                os.path.join(dest_path2, pet, img))\n            dog_counter += 1\n        else:\n            shutil.copyfile(\n                os.path.join(base_path, img),\n                os.path.join(dest_path1, pet, img))\n    \n    elif pet == 'cat':\n        if cat_counter < cat_split:\n            shutil.copyfile(\n                os.path.join(base_path, img),\n                os.path.join(dest_path2, pet, img))\n            cat_counter += 1\n        else:\n            shutil.copyfile(\n                os.path.join(base_path, img),\n                os.path.join(dest_path1, pet, img))","metadata":{"id":"1UfKivjpYetg","outputId":"53fd7cd0-5604-43b1-c865-5f93991a1856","execution":{"iopub.status.busy":"2021-08-22T09:36:43.628489Z","iopub.execute_input":"2021-08-22T09:36:43.628833Z","iopub.status.idle":"2021-08-22T09:36:45.925273Z","shell.execute_reply.started":"2021-08-22T09:36:43.628803Z","shell.execute_reply":"2021-08-22T09:36:45.924260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = ImageDataGenerator(rescale=1./255 ,preprocessing_function=preprocess_input)\n\ntrain_datagen = datagen.flow_from_directory('Train',\n                                            batch_size=32,\n                                            shuffle=True,\n                                            target_size=(224, 224),\n                                            class_mode='binary')\n\nval_datagen = datagen.flow_from_directory('Validation',\n                                            batch_size=32,\n                                            shuffle=True,\n                                            target_size=(224, 224),\n                                            class_mode='binary')","metadata":{"id":"V3UOFGn8g0YM","outputId":"6b124f55-65b5-4e14-f5e7-81e369fb4e34","execution":{"iopub.status.busy":"2021-08-22T09:38:22.469061Z","iopub.execute_input":"2021-08-22T09:38:22.469397Z","iopub.status.idle":"2021-08-22T09:38:23.695409Z","shell.execute_reply.started":"2021-08-22T09:38:22.469366Z","shell.execute_reply":"2021-08-22T09:38:23.694490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(int(20000 / 32) * 32)\nprint(int(5000 / 32) * 32)","metadata":{"id":"UUfeweL-jTQk","outputId":"e2ea5169-e2d0-40f3-98cc-d5864bc0bbd6","execution":{"iopub.status.busy":"2021-08-22T09:38:32.515602Z","iopub.execute_input":"2021-08-22T09:38:32.515921Z","iopub.status.idle":"2021-08-22T09:38:32.521007Z","shell.execute_reply.started":"2021-08-22T09:38:32.515891Z","shell.execute_reply":"2021-08-22T09:38:32.519965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Cat label = 0.0\n\nDog label = 1.0","metadata":{"id":"yfOqsPdii2uc"}},{"cell_type":"code","source":"data, label = next(train_datagen)\ni = 0\nplt.imshow(data[i])\nprint(label[i])","metadata":{"id":"CpzSD-uuhPhS","outputId":"3f406de9-85ea-477b-c4f2-d31fd15cf7d6","execution":{"iopub.status.busy":"2021-08-22T09:38:36.764580Z","iopub.execute_input":"2021-08-22T09:38:36.764907Z","iopub.status.idle":"2021-08-22T09:38:37.058104Z","shell.execute_reply.started":"2021-08-22T09:38:36.764880Z","shell.execute_reply":"2021-08-22T09:38:37.057167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = VGG16(weights='imagenet', include_top=False)\n\ninp = Input(shape=(224, 224, 3), batch_size=32)\n\nx = model(inp)\nx = GlobalMaxPooling2D()(x)\n\nmodel_feature_extractor = Model(inputs=[inp], outputs=[x])","metadata":{"id":"64bbEWTQhVYW","outputId":"c414508f-1a9a-4485-97d5-62066dfa0697","execution":{"iopub.status.busy":"2021-08-22T09:38:49.555280Z","iopub.execute_input":"2021-08-22T09:38:49.555642Z","iopub.status.idle":"2021-08-22T09:38:54.517223Z","shell.execute_reply.started":"2021-08-22T09:38:49.555609Z","shell.execute_reply":"2021-08-22T09:38:54.516389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = np.zeros((20000, 512), dtype=np.float32)\ny_train = np.zeros((20000))\n\nX_val = np.zeros((4992, 512), dtype=np.float32)\ny_val = np.zeros((4992))","metadata":{"id":"FF9XLFLVjfy0","execution":{"iopub.status.busy":"2021-08-22T09:39:04.186134Z","iopub.execute_input":"2021-08-22T09:39:04.186485Z","iopub.status.idle":"2021-08-22T09:39:04.199152Z","shell.execute_reply.started":"2021-08-22T09:39:04.186452Z","shell.execute_reply":"2021-08-22T09:39:04.198329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in tqdm(range(0, 20000, 32)):\n    batch = train_datagen.next()\n    X_train[i:i + 32] = model_feature_extractor.predict(batch[0])\n    y_train[i:i + 32] = batch[1]\n\nfor i in tqdm(range(0, 4992, 32)):\n    batch = val_datagen.next()\n    X_val[i:i + 32] = model_feature_extractor.predict(batch[0])\n    y_val[i:i + 32] = batch[1]","metadata":{"id":"tZIEiI_1jq-Q","outputId":"2fedff70-23bc-4fb0-ab9d-27c0b40ed143","execution":{"iopub.status.busy":"2021-08-22T09:39:05.722487Z","iopub.execute_input":"2021-08-22T09:39:05.722824Z","iopub.status.idle":"2021-08-22T09:41:49.968960Z","shell.execute_reply.started":"2021-08-22T09:39:05.722796Z","shell.execute_reply":"2021-08-22T09:41:49.967737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train.shape, y_train.shape)\nprint(X_val.shape, y_val.shape)","metadata":{"id":"uUZGPKddkyiB","outputId":"ab4960e1-4a56-4aed-a7ae-8ee2bad416ce","execution":{"iopub.status.busy":"2021-08-22T09:41:54.120480Z","iopub.execute_input":"2021-08-22T09:41:54.120835Z","iopub.status.idle":"2021-08-22T09:41:54.126385Z","shell.execute_reply.started":"2021-08-22T09:41:54.120802Z","shell.execute_reply":"2021-08-22T09:41:54.125384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_history(history):\n    fig, axs = plt.subplots(2)\n    # Plot both Loss & Accuracy in Subplot\n  # creat accuracy plot\n    axs[0].plot(history.history['acc'], label='train accuracy')\n    axs[0].plot(history.history['val_acc'], label='test accuracy')\n    axs[0].set_ylabel('Accuracy')\n    axs[0].legend(loc='best')\n    axs[0].set_title('Accuracy eval')\n\n  # creat loss plot\n    axs[1].plot(history.history['loss'], label='train loss')\n    axs[1].plot(history.history['val_loss'], label='test loss')\n    axs[1].set_ylabel('Loss')\n    axs[1].set_xlabel('Epoch')\n    axs[1].legend(loc='best')\n    axs[1].set_title('Loss eval')\n\n    plt.show()","metadata":{"id":"d7Ej733OkE_T","execution":{"iopub.status.busy":"2021-08-22T09:41:55.869865Z","iopub.execute_input":"2021-08-22T09:41:55.870198Z","iopub.status.idle":"2021-08-22T09:41:55.877904Z","shell.execute_reply.started":"2021-08-22T09:41:55.870168Z","shell.execute_reply":"2021-08-22T09:41:55.876829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.regularizers import l2\nfrom tensorflow.keras.optimizers import Adam\nimport datetime","metadata":{"id":"_7KKhsNWo9wc","execution":{"iopub.status.busy":"2021-08-22T09:42:01.155247Z","iopub.execute_input":"2021-08-22T09:42:01.155626Z","iopub.status.idle":"2021-08-22T09:42:01.162274Z","shell.execute_reply.started":"2021-08-22T09:42:01.155592Z","shell.execute_reply":"2021-08-22T09:42:01.161331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf_model =  Sequential()\n\nclf_model.add(Dense(128, activation='relu', input_dim=512,\n                    kernel_regularizer=l2(0.001)))\nclf_model.add(Dropout(0.3))\n\n# clf_model.add(Dense(64, activation='relu', kernel_regularizer=l2(0.001)))\n# clf_model.add(Dropout(0.3))\n\nclf_model.add(Dense(1, activation='sigmoid'))\n\nclf_model.summary()\n\nclf_model.compile(optimizer=Adam(learning_rate=0.0001), \n                  loss='binary_crossentropy', metrics=['acc'])","metadata":{"id":"NcyPfSYNnVru","outputId":"d7589971-6f61-40f7-92d3-105af1a0c7df","execution":{"iopub.status.busy":"2021-08-22T09:42:02.433529Z","iopub.execute_input":"2021-08-22T09:42:02.433879Z","iopub.status.idle":"2021-08-22T09:42:02.484706Z","shell.execute_reply.started":"2021-08-22T09:42:02.433848Z","shell.execute_reply":"2021-08-22T09:42:02.483892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res = clf_model.fit(X_train, y_train.reshape(-1, 1), epochs=50, batch_size=32,\n              validation_data=(X_val, y_val.reshape(-1, 1)), verbose=2)\n\nplot_history(res)","metadata":{"id":"Lqk3Xmg4k7kn","outputId":"0c0e79f6-7c36-4788-9dea-5c6a8b5629e3","execution":{"iopub.status.busy":"2021-08-22T09:42:04.495045Z","iopub.execute_input":"2021-08-22T09:42:04.495398Z","iopub.status.idle":"2021-08-22T09:42:55.091386Z","shell.execute_reply.started":"2021-08-22T09:42:04.495365Z","shell.execute_reply":"2021-08-22T09:42:55.090594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_filenames = os.listdir(\"/kaggle/working/test1\")\ntest_df = pd.DataFrame({\n    'filename': test_filenames\n})\nnb_samples = test_df.shape[0]","metadata":{"execution":{"iopub.status.busy":"2021-08-22T09:43:04.700854Z","iopub.execute_input":"2021-08-22T09:43:04.701176Z","iopub.status.idle":"2021-08-22T09:43:04.719280Z","shell.execute_reply.started":"2021-08-22T09:43:04.701147Z","shell.execute_reply":"2021-08-22T09:43:04.718464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-22T09:43:05.827005Z","iopub.execute_input":"2021-08-22T09:43:05.827335Z","iopub.status.idle":"2021-08-22T09:43:06.124267Z","shell.execute_reply.started":"2021-08-22T09:43:05.827306Z","shell.execute_reply":"2021-08-22T09:43:06.123259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_datagen = datagen.flow_from_dataframe(\n    test_df, \n    \"/kaggle/working/test1/\",\n    x_col='filename',\n    y_col=None,\n    class_mode=None,\n    target_size=(224, 224),\n    batch_size=32,\n    shuffle=False\n)","metadata":{"execution":{"iopub.status.busy":"2021-08-22T09:43:07.402069Z","iopub.execute_input":"2021-08-22T09:43:07.402400Z","iopub.status.idle":"2021-08-22T09:43:07.512796Z","shell.execute_reply.started":"2021-08-22T09:43:07.402369Z","shell.execute_reply":"2021-08-22T09:43:07.512012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(int(12500 / 32) * 32)","metadata":{"execution":{"iopub.status.busy":"2021-08-22T09:43:09.661168Z","iopub.execute_input":"2021-08-22T09:43:09.661505Z","iopub.status.idle":"2021-08-22T09:43:09.667508Z","shell.execute_reply.started":"2021-08-22T09:43:09.661474Z","shell.execute_reply":"2021-08-22T09:43:09.666501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = np.zeros((12480, 512), dtype=np.float32)","metadata":{"execution":{"iopub.status.busy":"2021-08-22T09:43:11.162481Z","iopub.execute_input":"2021-08-22T09:43:11.162875Z","iopub.status.idle":"2021-08-22T09:43:11.171020Z","shell.execute_reply.started":"2021-08-22T09:43:11.162844Z","shell.execute_reply":"2021-08-22T09:43:11.169842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in tqdm(range(0, 12480, 32)):\n    batch = test_datagen.next()\n    X_test[i:i + 32] = model_feature_extractor.predict(batch)\n#     y_train[i:i + 32] = batch[1]","metadata":{"execution":{"iopub.status.busy":"2021-08-22T09:43:12.376063Z","iopub.execute_input":"2021-08-22T09:43:12.376387Z","iopub.status.idle":"2021-08-22T09:44:30.429753Z","shell.execute_reply.started":"2021-08-22T09:43:12.376356Z","shell.execute_reply":"2021-08-22T09:44:30.428821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test.shape","metadata":{"execution":{"iopub.status.busy":"2021-08-22T09:45:01.348816Z","iopub.execute_input":"2021-08-22T09:45:01.349135Z","iopub.status.idle":"2021-08-22T09:45:01.356229Z","shell.execute_reply.started":"2021-08-22T09:45:01.349105Z","shell.execute_reply":"2021-08-22T09:45:01.355153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = clf_model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2021-08-22T09:45:02.589976Z","iopub.execute_input":"2021-08-22T09:45:02.590295Z","iopub.status.idle":"2021-08-22T09:45:02.882769Z","shell.execute_reply.started":"2021-08-22T09:45:02.590266Z","shell.execute_reply":"2021-08-22T09:45:02.881890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(len(preds)):\n    if preds[i] >= 0.5:\n        preds[i] = 1\n    else:\n        preds[i] = 0","metadata":{"execution":{"iopub.status.busy":"2021-08-22T09:45:03.955184Z","iopub.execute_input":"2021-08-22T09:45:03.955517Z","iopub.status.idle":"2021-08-22T09:45:03.996285Z","shell.execute_reply.started":"2021-08-22T09:45:03.955486Z","shell.execute_reply":"2021-08-22T09:45:03.995488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = pd.DataFrame()\npredictions[\"id\"] = np.arange(1, 12481)\npredictions[\"target\"] = preds\n\npredictions.to_csv('Pejman_submission.csv', index=False, header=predictions.columns)\npredictions.head(10)","metadata":{"execution":{"iopub.status.busy":"2021-08-22T09:45:05.055120Z","iopub.execute_input":"2021-08-22T09:45:05.055475Z","iopub.status.idle":"2021-08-22T09:45:05.101233Z","shell.execute_reply.started":"2021-08-22T09:45:05.055416Z","shell.execute_reply":"2021-08-22T09:45:05.099973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions['target'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-08-22T09:45:06.503091Z","iopub.execute_input":"2021-08-22T09:45:06.503406Z","iopub.status.idle":"2021-08-22T09:45:06.515581Z","shell.execute_reply.started":"2021-08-22T09:45:06.503376Z","shell.execute_reply":"2021-08-22T09:45:06.514809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}