{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","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)\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","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import os\nimport glob\nimport random\nfrom PIL import Image\nimport cv2\nimport shutil\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom keras.models import Sequential\nfrom keras.applications.resnet50 import ResNet50\nfrom keras.layers import Conv2D,MaxPooling2D,GlobalAveragePooling2D,Dense,Flatten,Dropout,AvgPool2D\nfrom keras.layers.normalization import BatchNormalization\nfrom keras.applications import inception_v3\nfrom keras.applications.vgg16 import preprocess_input, decode_predictions\nfrom sklearn.linear_model import LogisticRegression\nfrom keras.applications import MobileNetV2\nfrom keras.preprocessing.image import ImageDataGenerator, load_img","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(os.listdir(\"../input\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def make_directory(dir_path):\n    if os.path.exists(dir_path):\n        shutil.rmtree(dir_path)\n    os.makedirs(dir_path)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import zipfile\n\n\nDATASET_PATH = './dogs-vs-cats'\n\nmake_directory(DATASET_PATH)\n\n# extract train data\nwith zipfile.ZipFile('../input/dogs-vs-cats/train.zip', 'r') as zip_ref:\n    zip_ref.extractall(DATASET_PATH)\n\n# extract test data\nwith zipfile.ZipFile('../input/dogs-vs-cats/test1.zip', 'r') as zip_ref:\n    zip_ref.extractall(DATASET_PATH)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"TRAIN_DATA_PATH = os.path.sep.join([DATASET_PATH, 'train'])\nTEST_DATA_PATH = os.path.sep.join([DATASET_PATH, 'test1'])\ntrain_files = os.listdir(TRAIN_DATA_PATH)\ntest_files = os.listdir(TEST_DATA_PATH)\n\nprint(len(train_files), len(test_files))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Image.open(os.path.sep.join([TRAIN_DATA_PATH, train_files[0]])) \n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(os.path.sep.join([TRAIN_DATA_PATH, train_files[0]])) ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_files","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"make_directory('./dogs-vs-cats/new_train')\nfor i in train_files:\n    dir_path = i.split('/')[-1]\n    if dir_path.split(\".\")[0] not in os.listdir('./dogs-vs-cats/new_train/'):\n        make_directory('./dogs-vs-cats/new_train/' + dir_path.split(\".\")[0])\n    shutil.copy('./dogs-vs-cats/train/' + i  ,'./dogs-vs-cats/new_train/' + dir_path.split(\".\")[0] + '/' + i)    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(os.listdir('./dogs-vs-cats/new_train/dog/'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from tensorflow.python.keras.preprocessing.image import ImageDataGenerator \n\nimage_size = 224\nimage_path = './dogs-vs-cats/new_train'\ntrain_datagen=ImageDataGenerator(\n                        rescale=1./255,\n                        validation_split=0.1,\n                        horizontal_flip=False,\n                        width_shift_range=0.2,\n                        height_shift_range=0.2,\n                        shear_range=0.2,\n                        rotation_range=40,\n                        fill_mode='nearest'\n                        )\n\ntrain_generator=train_datagen.flow_from_directory(\n                        image_path, \n                        target_size=(image_size,image_size),\n                        subset='training',\n                        shuffle=True,\n                        batch_size=128,\n                        class_mode='categorical'\n                        )\n\nvalid_datagen=ImageDataGenerator(\n                        validation_split=0.1,\n                        rescale=1./255\n                        )\n\nvalid_generator=valid_datagen.flow_from_directory(\n                        image_path, \n                        target_size=(image_size,image_size),\n                        subset='validation',\n                        shuffle=False,\n                        batch_size=128,\n                        class_mode='categorical'\n                        )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x,y = train_generator.next()\nlabels = (train_generator.class_indices)\nlabels = dict((v,k) for k,v in labels.items())\nlabels = (train_generator.class_indices)\nlabels = dict((v,k) for k,v in labels.items())\nfor i in range(0,10):\n    image = x[i]\n    plt.imshow(image)\n    c=0\n    for i in y[i]:\n        if i==0:\n            c+=1\n        else:break\n    label=labels[c]\n    label=label.replace('_',' ')\n    label=label.lower()\n    plt.title(label)\n    plt.axis('off')\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from tensorflow.keras.applications.inception_v3 import InceptionV3\n\nInception = InceptionV3(input_shape = (224,224,3), \n                                 include_top = False, \n                                 weights = 'imagenet')\n\n\nmodel=Sequential()\nmodel.add(Inception)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.add(GlobalAveragePooling2D())\nmodel.add(Dense(512 , activation = 'relu'))\nmodel.add(Dropout(0.4))\nmodel.add(Dense(512 , activation = 'relu'))\nmodel.add(Dropout(0.4))\nmodel.add(Dense(2 , activation = 'softmax'))\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(optimizer = 'adam' ,\n    loss = 'categorical_crossentropy' ,\n              metrics = ['accuracy']\n             )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"callback=tf.keras.callbacks.EarlyStopping(monitor='loss',patience=5,min_delta=0,mode='auto',restore_best_weights=False,baseline=None)\n\nhistory=model.fit(train_generator,\n                   steps_per_epoch=None,\n                   epochs=4,\n                   validation_data=valid_generator,\n                   validation_steps=4,\n                   callbacks=[callback])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"os.mkdir('./model/')\nmodel.save('./model/dog_cat.h5')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def plotModel(history):\n    fig,(ax1 , ax2) = plt.subplots(1 , 2 , figsize = (12 , 4))\n    fig.suptitle('Train vs Val')\n    ax1.plot(history.history['accuracy'])\n    ax1.plot(history.history['val_accuracy'])\n    ax1.title.set_text('Accuracy')\n    ax1.set_xlabel('Epochs')\n    ax1.set_ylabel('Accuracy')\n    ax1.legend(['train' , 'val'] , loc = 4)\n    \n    ax2.plot(history.history['loss'])\n    ax2.plot(history.history['val_loss'])\n    ax2.title.set_text('Loss')\n    ax2.set_xlabel('Epochs')\n    ax2.set_ylabel('Loss')\n    ax2.legend(['Train' , 'Val'] , loc = 1 )\n    \n    fig.show()\n    \nplotModel(history)    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_filenames = os.listdir(\"./dogs-vs-cats/test1\")\ntest_df = pd.DataFrame({\n    'filename': test_filenames\n})\nnb_samples = test_df.shape[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_gen = ImageDataGenerator(rescale=1./255)\ntest_generator = test_gen.flow_from_dataframe(\n    test_df, \n    \"./dogs-vs-cats/test1/\", \n    x_col='filename',\n    y_col=None,\n    class_mode=None,\n    batch_size=32,\n    target_size=(224, 224),\n    shuffle=False\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predict = model.predict_generator(test_generator, steps=np.ceil(nb_samples/32))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df['category'] = np.argmax(predict, axis=-1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"label_map = dict((v,k) for k,v in train_generator.class_indices.items())\ntest_df['category'] = test_df['category'].replace(label_map)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df['category'] = test_df['category'].replace({ 'dog': 1, 'cat': 0 })","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_test = test_df.head(18)\nsample_test.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(12, 24))\nfor index, row in sample_test.iterrows():\n    filenam = row['filename']\n    category = row['category']\n    img = load_img(\"./dogs-vs-cats/test1/\"+str(filenam))\n    plt.subplot(6, 3, index+1)\n    plt.imshow(img)\n    plt.xlabel(filename + '(' + \"{}\".format(category) + ')' )\nplt.tight_layout()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission_df = test_df.copy()\nsubmission_df['id'] = submission_df['filename'].str.split('.').str[0]\nsubmission_df['label'] = submission_df['category']\nsubmission_df.drop(['filename', 'category'], axis=1, inplace=True)\nsubmission_df.to_csv('submission.csv', index=False)","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}