{"cells":[{"metadata":{"_uuid":"81d65271-b7f7-481e-8137-0d0019d4cd14","_cell_guid":"f2f1b09c-9d47-4f46-bca6-c8d0f48a86a3","trusted":true},"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)\nnp.set_printoptions(suppress=False)\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","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!unzip '../input/dogs-vs-cats-redux-kernels-edition/test.zip'\n!unzip '../input/dogs-vs-cats-redux-kernels-edition/train.zip'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"n_train = len(os.listdir('./train'))\nn_test = len(os.listdir('./test'))\nprint(f'Train images: {n_train}')\nprint(f' Test images: {n_test}')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import glob\nimport cv2\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow import keras\nfrom keras.layers import *\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import ReduceLROnPlateau, EarlyStopping\nfrom keras.models import Model\nfrom keras.applications.mobilenet_v2 import MobileNetV2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_filename = glob.glob('./train/*.jpg')\ntest_filename = glob.glob('./test/*.jpg')\ntrain_labels = []\nfor i in train_filename:\n    train_clsname = i.split('/')[-1].split('.')[0]\n    train_labels.append(train_clsname)\n\ndf = pd.DataFrame({'filename': train_filename, 'label': train_labels})\ntrain_df, test_df = train_test_split(df, test_size=0.2, shuffle=True, random_state=32)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n    rescale=1./255,\n    rotation_range=45,\n    shear_range=10,\n    zoom_range=[1,1.3],\n    validation_split=0.2\n)\n\ntest_datagen = ImageDataGenerator(\n    rescale=1./255,\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_images = train_datagen.flow_from_dataframe(\n    train_df,\n    target_size=(224,224),\n    x_col='filename',\n    y_col='label',\n    subset='training',\n    color_mode='rgb',\n    class_mode='categorical'\n)\n\nval_images = train_datagen.flow_from_dataframe(\n    train_df,\n    subset='validation',\n    target_size=(224,224),\n    class_mode='categorical',\n    color_mode='rgb',\n    x_col='filename',\n    y_col='label'\n)\n\ntest_images = test_datagen.flow_from_dataframe(\n    test_df,\n    target_size=(224,224),\n    class_mode='categorical',\n    color_mode='rgb',\n    x_col='filename',\n    y_col='label'\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_images.class_indices","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mn_model = MobileNetV2(weights='imagenet', include_top=False, input_shape=(224,224,3))\nmn_model.trainable = False\nx = mn_model.output\nx = GlobalAveragePooling2D()(x)\nx = Dense(1024, activation='relu')(x)\nx = Dense(512, activation = 'relu')(x)\npred = Dense(2, activation='softmax')(x)\n\nmodel = Model(inputs=mn_model.input, outputs=pred)\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\nlr_reduction = ReduceLROnPlateau(patience=1, verbose=1, factor=0.25, min_lr=0.000003)\nes = EarlyStopping(monitor='val_loss', patience=5, verbose=5, restore_best_weights=True)\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"hist = model.fit(train_images, validation_data=val_images, epochs=20, callbacks=[lr_reduction,es])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\nprint(hist.history.keys())\nplt.figure(figsize=(15,15))\n\nplt.subplot(1,2,1)\nplt.plot(hist.history['accuracy'])\nplt.plot(hist.history['val_accuracy'])\nplt.title('model accuracy')\nplt.xlabel('epochs')\nplt.ylabel('accuracy')\nplt.legend(['train','test'], loc='upper left')\n\nplt.subplot(1,2,2)\nplt.plot(hist.history['loss'])\nplt.plot(hist.history['val_loss'])\nplt.title('model loss')\nplt.xlabel('epochs')\nplt.ylabel('loss')\nplt.legend(['train','test'], loc='upper left')\n\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"res = model.evaluate(test_images)\nprint(f'    Loss: {res[0]}')\nprint(f'Accuracy: {res[1]}')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pred_images = glob.glob('./test/*.jpg')\nprob = []\nfor image in pred_images:\n    img = cv2.imread(image)/255.0\n    img = cv2.resize(img, (224,224))\n    img = img.reshape(1,224,224,3)\n    prob.append(model.predict(img)[0][1])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ids = []\nfor i in test_filename:\n    name = i.split('/')[-1].split('.')[0]\n    ids.append(name)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"output = pd.DataFrame({'id': ids, 'label': prob})\noutput.to_csv('./my_submission.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"output","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"output.describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}