{"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 in \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 \"../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'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Importing all the required libraries"},{"metadata":{"trusted":true},"cell_type":"code","source":"from zipfile import ZipFile\nfrom tensorflow.keras.applications import ResNet50\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Flatten, GlobalAveragePooling2D","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import os\nimport shutil\n\ndirpath='../output/kaggle/working/train/train'\nif os.path.exists(dirpath):\n    shutil.rmtree(dirpath)\n    print('path deleted')\nelse:\n    print('path doesnot exists')\n    \n    \ndirpath='../output/kaggle/working/train/dog/train/'\nif os.path.exists(dirpath):\n    shutil.rmtree(dirpath)\n    print('path deleted')\nelse:\n    print('path doesnot exists')\n    \ndirpath='../output/kaggle/working/test/test'\nif os.path.exists(dirpath):\n    shutil.rmtree(dirpath)\n    print('path deleted')\nelse:\n    print('path doesnot exists')\n    \ndirpath='../output/kaggle/working/train'\nif os.path.exists(dirpath):\n    shutil.rmtree(dirpath)\n    print('path deleted')\nelse:\n    print('path doesnot exists')\n    \n    \ndirpath='../output/kaggle/working/test'\nif os.path.exists(dirpath):\n    shutil.rmtree(dirpath)\n    print('path deleted')\nelse:\n    print('path doesnot exists')\n    \ndirpath='../kaggle/working/validation'\nif os.path.exists(dirpath):\n    shutil.rmtree(dirpath)\n    print('path deleted')\nelse:\n    print('path doesnot exists')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Extracting the test zip  "},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"from zipfile import ZipFile\n\n# specifying the zip file name \ntest_file_name = \"../input/dogs-vs-cats-redux-kernels-edition/test.zip\"\n  \n# opening the zip file in READ mode \nwith ZipFile(test_file_name, 'r') as zip: \n    # printing all the contents of the zip file \n    #zip.printdir() \n  \n    # extracting all the files \n    print('Extracting all the files now...') \n    zip.extractall('../output/kaggle/working/') \n    print('Done!') \n    \n    \n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Checking the count of the test files"},{"metadata":{"trusted":true},"cell_type":"code","source":"#import os\nf=[]\nfor root, dirs, files in os.walk(\"../output/kaggle/working/test/\"):\n    for filename in files:\n        f.append(filename)\n\nprint(len(f))\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#import os\n\n#for root, dirs, files in os.walk(\"../output/kaggle/working/\"):\n #   for filename in files:\n  #      print(filename)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Extracting the all the files from the train zip"},{"metadata":{"trusted":true},"cell_type":"code","source":"# specifying the zip file name \ntrain_file_name = \"../input/dogs-vs-cats-redux-kernels-edition/train.zip\"\n  \n# opening the zip file in READ mode \nwith ZipFile(train_file_name, 'r') as zip: \n    # printing all the contents of the zip file \n   # zip.printdir() \n  \n    # extracting all the files \n    print('Extracting all the files now...') \n    zip.extractall('../output/kaggle/working/') \n    print('Done!') \n    ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Checking the count of dog and cat files from the train folder"},{"metadata":{"trusted":true},"cell_type":"code","source":"import shutil\n\ndogslist=[]\ncatslist=[]\nfor root, dirs, files in os.walk(\"../output/kaggle/working/train/\"):\n    for filename in files:\n        if filename.startswith('dog'):\n            dogslist.append(filename)\n        else:\n            catslist.append(filename)\n            \nprint(len(dogslist))\n             \nprint(len(catslist))\n\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Copying all the dog files into train/dog folder"},{"metadata":{"trusted":true},"cell_type":"code","source":"\nif not os.path.exists('../output/kaggle/working/train/dog'):\n    os.makedirs('../output/kaggle/working/train/dog')\n\nfor e in [os.path.join('../output/kaggle/working/train/',name) for name in dogslist]:\n    shutil.copy(e,'../output/kaggle/working/train/dog/')\n   \n    \n    \n\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Copying all the cat files into train/cat folder"},{"metadata":{"trusted":true},"cell_type":"code","source":"if not os.path.exists('../output/kaggle/working/train/cat'):\n    os.makedirs('../output/kaggle/working/train/cat')\n\nfor e in [os.path.join('../output/kaggle/working/train/',name) for name in catslist]:\n    shutil.copy(e,'../output/kaggle/working/train/cat/')\n    ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Validating the dog file count after copy"},{"metadata":{"trusted":true},"cell_type":"code","source":"import shutil\n\ndogslist=[]\ncatslist=[]\nfor root, dirs, files in os.walk(\"../output/kaggle/working/train/dog\"):\n    for filename in files:\n        if filename.startswith('dog'):\n            dogslist.append(filename)\n        else:\n            catslist.append(filename)\n            \nprint(len(dogslist))\n             \nprint(len(catslist))\n\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Validating the cat file count after copy"},{"metadata":{"trusted":true},"cell_type":"code","source":"import shutil\n\ndogslist=[]\ncatslist=[]\nfor root, dirs, files in os.walk(\"../output/kaggle/working/train/cat\"):\n    for filename in files:\n        if filename.startswith('cat'):\n            catslist.append(filename)\n        else:\n            dogslist.append(filename)\n            \nprint(len(catslist))\nprint(len(dogslist))\n             ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Defining the number of classes to predict\n# \n# Using the Transfer Model techniqure RESNet50 using predefined weights"},{"metadata":{"trusted":true},"cell_type":"code","source":"num_classes=2\n\n\nresnet_weights_path = '../input/weight/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'\n\n\n\nmy_model=Sequential()\n\nmy_model.add(ResNet50(include_top=False,pooling='avg',weights=resnet_weights_path))\n\nmy_model.add(Dense(num_classes,activation='softmax'))\n\nmy_model.layers[0].trainable=False","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Compiling the model using the SGD as optimzer and loss function as Categorical_Crossentropy"},{"metadata":{"trusted":true},"cell_type":"code","source":"my_model.compile(optimizer='sgd', \n                     loss='categorical_crossentropy', \n                     metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"if not os.path.exists('../output/kaggle/working/validation/dog'):\n    os.makedirs('../output/kaggle/working/validation/dog')\n    \nif not os.path.exists('../output/kaggle/working/validation/cat'):\n    os.makedirs('../output/kaggle/working/validation/cat')\n    \n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pip install split_folders","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import split_folders\n\nsplit_folders.ratio('../output/kaggle/working/train', output=\"../output/kaggle/working/Val\", seed=1337, ratio=(.8,.2))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import shutil\n\ndogslist=[]\ncatslist=[]\nfor root, dirs, files in os.walk(\"../working/validation/train/cat/\"):\n    for filename in files:\n        if filename.startswith('cat'):\n            catslist.append(filename)\n        else:\n            dogslist.append(filename)\n            \nprint(len(catslist))\nprint(len(dogslist))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import shutil\n\ndogslist=[]\ncatslist=[]\nfor root, dirs, files in os.walk(\"../working/validation/train/dog/\"):\n    for filename in files:\n        if filename.startswith('dog'):\n            dogslist.append(filename)\n        else:\n            catslist.append(filename)\n            \n\nprint(len(dogslist))\nprint(len(catslist))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\n\nimport shutil\n\ndogslist=[]\ncatslist=[]\nfor root, dirs, files in os.walk(\"../working/validation/val/dog/\"):\n    for filename in files:\n        if filename.startswith('dog'):\n            dogslist.append(filename)\n        else:\n            catslist.append(filename)\n            \n\nprint(len(dogslist))\nprint(len(catslist))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\nimport shutil\n\ndogslist=[]\ncatslist=[]\nfor root, dirs, files in os.walk(\"../working/validation/val/cat/\"):\n    for filename in files:\n        if filename.startswith('cat'):\n            catslist.append(filename)\n        else:\n            dogslist.append(filename)\n            \n\n\nprint(len(catslist))\nprint(len(dogslist))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from tensorflow.keras.applications.resnet50 import preprocess_input\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\nimage_size=224\n\ndata_generator = ImageDataGenerator(preprocess_input)\n\n\ntrain_data_generator= data_generator.flow_from_directory(\n        '/kaggle/output/kaggle/working/Val/train/',\n        target_size=(image_size, image_size),\n        batch_size=24,\n        class_mode='categorical')\n\n\nvalidation_generator = data_generator.flow_from_directory(\n        '/kaggle/output/kaggle/working/Val/val/',\n        target_size=(image_size, image_size),\n        class_mode='categorical')\n\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"f=[]\n\nfor root, dirs, files in os.walk(\"../output/kaggle/working/test/\"):\n    for filename in files:\n        f.append(filename)\n    \n\nif not os.path.exists('/kaggle/output/kaggle/working/Val/test/'):\n    os.makedirs('/kaggle/output/kaggle/working/Val/test/')\nelse:\n    print('folder exists')\n    \n\n    \nif not os.path.exists('/kaggle/output/kaggle/working/Val/test/data/'):\n    os.makedirs('/kaggle/output/kaggle/working/Val/test/data')\nelse:\n    print('folder exists')\n\nfor e in [os.path.join('../output/kaggle/working/test/',name) for name in f]:\n    shutil.copy(e,'/kaggle/output/kaggle/working/Val/test/data/')\n\n    \nd=[]\n\nfor root, dirs, files in os.walk(\"/kaggle/output/kaggle/working/Val/test/\"):\n    for filename in files:\n        d.append(filename)\n        \nprint(len(d))\n\ntest_generator = data_generator.flow_from_directory(\n     '/kaggle/output/kaggle/working/Val/test',\n    target_size = (224, 224),\n    batch_size = 24,\n    class_mode = None,\n    shuffle = False,\n    seed = 123\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"my_model.fit_generator(\n        train_data_generator,\n        steps_per_epoch=10,\n        validation_data=validation_generator,\n        validation_steps=1)\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_generator.reset()\n\npred = my_model.predict_generator(test_generator, steps = len(test_generator), verbose = 1)\n\npredicted_class_indices = np.argmax(pred, axis = 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import cv2\n\n\nfrom matplotlib import pyplot as plt\n\nTEST_DIR = '/kaggle/output/kaggle/working/Val/test/'\n\nf, ax = plt.subplots(10, 5, figsize = (15, 15))\n\nfor i in range(0,50):\n    print(TEST_DIR+test_generator.filenames[i])\n    imgBGR = cv2.imread(TEST_DIR + test_generator.filenames[i])\n    imgRGB = cv2.cvtColor(imgBGR, cv2.COLOR_BGR2RGB)\n    \n    # a if condition else b\n    predicted_class = \"Dog\" if predicted_class_indices[i] else \"Cat\"\n\n    ax[i//5, i%5].imshow(imgRGB)\n    ax[i//5, i%5].axis('off')\n    ax[i//5, i%5].set_title(\"Predicted:{}\".format(predicted_class))    \n\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"results_df = pd.DataFrame(\n    {\n        'id': pd.Series(test_generator.filenames), \n        'label': pd.Series(predicted_class_indices)\n    })\nresults_df['id'] = results_df.id.str.extract('(\\d+)')\nresults_df['id'] = pd.to_numeric(results_df['id'], errors = 'coerce')\nresults_df.sort_values(by='id', inplace = True)\n\nresults_df.to_csv('submission.csv', index=False)\nresults_df.head()","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}