{"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-19T09:16:51.745195Z","iopub.execute_input":"2021-08-19T09:16:51.745574Z","iopub.status.idle":"2021-08-19T09:16:51.758468Z","shell.execute_reply.started":"2021-08-19T09:16:51.745488Z","shell.execute_reply":"2021-08-19T09:16:51.757593Z"},"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-19T09:16:51.759831Z","iopub.execute_input":"2021-08-19T09:16:51.760341Z","iopub.status.idle":"2021-08-19T09:17:07.208579Z","shell.execute_reply.started":"2021-08-19T09:16:51.760305Z","shell.execute_reply":"2021-08-19T09:17:07.207420Z"},"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-19T09:17:07.210509Z","iopub.execute_input":"2021-08-19T09:17:07.210862Z","iopub.status.idle":"2021-08-19T09:17:11.058523Z","shell.execute_reply.started":"2021-08-19T09:17:07.210821Z","shell.execute_reply":"2021-08-19T09:17:11.057315Z"},"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-19T09:17:11.062323Z","iopub.execute_input":"2021-08-19T09:17:11.062601Z","iopub.status.idle":"2021-08-19T09:17:16.252134Z","shell.execute_reply.started":"2021-08-19T09:17:11.062570Z","shell.execute_reply":"2021-08-19T09:17:16.251265Z"},"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-19T09:17:16.253409Z","iopub.execute_input":"2021-08-19T09:17:16.253751Z","iopub.status.idle":"2021-08-19T09:17:16.294652Z","shell.execute_reply.started":"2021-08-19T09:17:16.253715Z","shell.execute_reply":"2021-08-19T09:17:16.293816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.path.isdir('train/')","metadata":{"execution":{"iopub.status.busy":"2021-08-19T09:17:16.295906Z","iopub.execute_input":"2021-08-19T09:17:16.296265Z","iopub.status.idle":"2021-08-19T09:17:16.361124Z","shell.execute_reply.started":"2021-08-19T09:17:16.296228Z","shell.execute_reply":"2021-08-19T09:17:16.360236Z"},"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-19T09:17:16.363537Z","iopub.execute_input":"2021-08-19T09:17:16.363818Z","iopub.status.idle":"2021-08-19T09:17:16.370343Z","shell.execute_reply.started":"2021-08-19T09:17:16.363790Z","shell.execute_reply":"2021-08-19T09:17:16.369581Z"},"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-19T09:17:16.372560Z","iopub.execute_input":"2021-08-19T09:17:16.373019Z","iopub.status.idle":"2021-08-19T09:17:16.379848Z","shell.execute_reply.started":"2021-08-19T09:17:16.372980Z","shell.execute_reply":"2021-08-19T09:17:16.379034Z"},"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-19T09:17:16.381294Z","iopub.execute_input":"2021-08-19T09:17:16.381888Z","iopub.status.idle":"2021-08-19T09:17:18.873882Z","shell.execute_reply.started":"2021-08-19T09:17:16.381831Z","shell.execute_reply":"2021-08-19T09:17:18.872886Z"},"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-19T09:17:18.875300Z","iopub.execute_input":"2021-08-19T09:17:18.875651Z","iopub.status.idle":"2021-08-19T09:17:19.956058Z","shell.execute_reply.started":"2021-08-19T09:17:18.875613Z","shell.execute_reply":"2021-08-19T09:17:19.955169Z"},"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-19T09:17:19.957313Z","iopub.execute_input":"2021-08-19T09:17:19.957813Z","iopub.status.idle":"2021-08-19T09:17:19.963711Z","shell.execute_reply.started":"2021-08-19T09:17:19.957773Z","shell.execute_reply":"2021-08-19T09:17:19.962790Z"},"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-19T09:17:19.965020Z","iopub.execute_input":"2021-08-19T09:17:19.965582Z","iopub.status.idle":"2021-08-19T09:17:20.244298Z","shell.execute_reply.started":"2021-08-19T09:17:19.965543Z","shell.execute_reply":"2021-08-19T09:17:20.243271Z"},"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-19T09:17:20.245580Z","iopub.execute_input":"2021-08-19T09:17:20.246083Z","iopub.status.idle":"2021-08-19T09:17:23.081708Z","shell.execute_reply.started":"2021-08-19T09:17:20.246040Z","shell.execute_reply":"2021-08-19T09:17:23.080864Z"},"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-19T09:17:23.083838Z","iopub.execute_input":"2021-08-19T09:17:23.084609Z","iopub.status.idle":"2021-08-19T09:17:23.098477Z","shell.execute_reply.started":"2021-08-19T09:17:23.084569Z","shell.execute_reply":"2021-08-19T09:17:23.097418Z"},"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-19T09:17:23.099831Z","iopub.execute_input":"2021-08-19T09:17:23.100548Z","iopub.status.idle":"2021-08-19T09:20:10.184066Z","shell.execute_reply.started":"2021-08-19T09:17:23.100506Z","shell.execute_reply":"2021-08-19T09:20:10.182969Z"},"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-19T09:20:10.186011Z","iopub.execute_input":"2021-08-19T09:20:10.186378Z","iopub.status.idle":"2021-08-19T09:20:10.192862Z","shell.execute_reply.started":"2021-08-19T09:20:10.186336Z","shell.execute_reply":"2021-08-19T09:20:10.191952Z"},"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-19T09:20:10.194359Z","iopub.execute_input":"2021-08-19T09:20:10.194704Z","iopub.status.idle":"2021-08-19T09:20:10.204800Z","shell.execute_reply.started":"2021-08-19T09:20:10.194659Z","shell.execute_reply":"2021-08-19T09:20:10.203724Z"},"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-19T09:20:10.206301Z","iopub.execute_input":"2021-08-19T09:20:10.206735Z","iopub.status.idle":"2021-08-19T09:20:10.214397Z","shell.execute_reply.started":"2021-08-19T09:20:10.206689Z","shell.execute_reply":"2021-08-19T09:20:10.213257Z"},"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-19T09:20:10.219558Z","iopub.execute_input":"2021-08-19T09:20:10.219933Z","iopub.status.idle":"2021-08-19T09:20:10.283604Z","shell.execute_reply.started":"2021-08-19T09:20:10.219896Z","shell.execute_reply":"2021-08-19T09:20:10.282560Z"},"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-19T09:20:10.303676Z","iopub.execute_input":"2021-08-19T09:20:10.304044Z","iopub.status.idle":"2021-08-19T09:20:59.933351Z","shell.execute_reply.started":"2021-08-19T09:20:10.304006Z","shell.execute_reply":"2021-08-19T09:20:59.932266Z"},"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-19T09:20:59.934950Z","iopub.execute_input":"2021-08-19T09:20:59.935298Z","iopub.status.idle":"2021-08-19T09:20:59.954945Z","shell.execute_reply.started":"2021-08-19T09:20:59.935259Z","shell.execute_reply":"2021-08-19T09:20:59.954001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-19T09:20:59.956187Z","iopub.execute_input":"2021-08-19T09:20:59.956686Z","iopub.status.idle":"2021-08-19T09:21:00.248208Z","shell.execute_reply.started":"2021-08-19T09:20:59.956644Z","shell.execute_reply":"2021-08-19T09:21:00.247242Z"},"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-19T09:21:00.249531Z","iopub.execute_input":"2021-08-19T09:21:00.250043Z","iopub.status.idle":"2021-08-19T09:21:00.360165Z","shell.execute_reply.started":"2021-08-19T09:21:00.250002Z","shell.execute_reply":"2021-08-19T09:21:00.359101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(int(12500 / 32) * 32)","metadata":{"execution":{"iopub.status.busy":"2021-08-19T09:21:00.361539Z","iopub.execute_input":"2021-08-19T09:21:00.361915Z","iopub.status.idle":"2021-08-19T09:21:00.367081Z","shell.execute_reply.started":"2021-08-19T09:21:00.361874Z","shell.execute_reply":"2021-08-19T09:21:00.366119Z"},"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-19T09:21:00.368308Z","iopub.execute_input":"2021-08-19T09:21:00.368777Z","iopub.status.idle":"2021-08-19T09:21:00.381361Z","shell.execute_reply.started":"2021-08-19T09:21:00.368738Z","shell.execute_reply":"2021-08-19T09:21:00.380360Z"},"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-19T09:21:00.382834Z","iopub.execute_input":"2021-08-19T09:21:00.383343Z","iopub.status.idle":"2021-08-19T09:22:19.828714Z","shell.execute_reply.started":"2021-08-19T09:21:00.383305Z","shell.execute_reply":"2021-08-19T09:22:19.827845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test.shape","metadata":{"execution":{"iopub.status.busy":"2021-08-19T09:22:19.830309Z","iopub.execute_input":"2021-08-19T09:22:19.830990Z","iopub.status.idle":"2021-08-19T09:22:19.837210Z","shell.execute_reply.started":"2021-08-19T09:22:19.830948Z","shell.execute_reply":"2021-08-19T09:22:19.836200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = clf_model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2021-08-19T09:22:19.838674Z","iopub.execute_input":"2021-08-19T09:22:19.839343Z","iopub.status.idle":"2021-08-19T09:22:20.148948Z","shell.execute_reply.started":"2021-08-19T09:22:19.839305Z","shell.execute_reply":"2021-08-19T09:22:20.148073Z"},"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-19T09:22:20.150420Z","iopub.execute_input":"2021-08-19T09:22:20.150754Z","iopub.status.idle":"2021-08-19T09:22:20.195410Z","shell.execute_reply.started":"2021-08-19T09:22:20.150716Z","shell.execute_reply":"2021-08-19T09:22:20.194659Z"},"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-19T09:22:20.196783Z","iopub.execute_input":"2021-08-19T09:22:20.197154Z","iopub.status.idle":"2021-08-19T09:22:20.243408Z","shell.execute_reply.started":"2021-08-19T09:22:20.197117Z","shell.execute_reply":"2021-08-19T09:22:20.242422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions['target'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-08-19T09:22:20.244841Z","iopub.execute_input":"2021-08-19T09:22:20.245220Z","iopub.status.idle":"2021-08-19T09:22:20.255517Z","shell.execute_reply.started":"2021-08-19T09:22:20.245180Z","shell.execute_reply":"2021-08-19T09:22:20.254512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}