{"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 5GB 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 numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport os\nfrom os import getcwd\nimport shutil\nimport zipfile\nfrom shutil import copyfile\nimport random","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":"cat_dog_path = \"../input/dogs-vs-cats\"\nos.listdir(cat_dog_path)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"*Extracting the Train Images*"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_zip = \"../input/dogs-vs-cats/train.zip\"\nzip_file = zipfile.ZipFile(train_zip,'r')\nzip_file.extractall('.')\nzip_file.close()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"*Extracting the test images*"},{"metadata":{"trusted":true},"cell_type":"code","source":"test_zip = \"../input/dogs-vs-cats/test1.zip\"\nzip_file = zipfile.ZipFile(train_zip,'r')\nzip_file.extractall('.')\nzip_file.close()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"try:\n    main_dir = \"/kaggle/working/\"\n    train_dir = \"train\"\n    val_dir = \"val\"\n    train_dir = os.path.join(main_dir,train_dir)\n    \n    # Directory with our training cat/dog pictures\n    train_cats_dir = os.path.join(train_dir, 'cats')\n    train_dogs_dir = os.path.join(train_dir, 'dogs')\n    os.mkdir(train_cats_dir)\n    os.mkdir(train_dogs_dir)\n    \n    # Directory with our validation cat/dog pictures\n    val_dir = os.path.join(main_dir,\"val\")\n    os.mkdir(val_dir)\n    val_cats_dir = os.path.join(val_dir, 'cats')\n    val_dogs_dir = os.path.join(val_dir, 'dogs')\n    os.mkdir(val_cats_dir)\n    os.mkdir(val_dogs_dir)\n\nexcept OSError:\n    pass","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"main_dir = \"/kaggle/working/\"\ntrain_dir = \"train\"\ntrain_path = os.path.join(main_dir,train_dir)\n\nprefixed_dogs = [filename for filename in os.listdir(train_path) if filename.startswith(\"dog.\")]\nprint(len(prefixed_dogs))\nprefixed_cats = [filename for filename in os.listdir(train_path) if filename.startswith(\"cat.\")]\nprint(len(prefixed_cats))\n\ndef move_files(src_file):\n    \n    for filename in prefixed_dogs:\n        shutil.move(src_file+filename, src_file+'dogs/'+filename)\n        \n    for filename in prefixed_cats:\n        shutil.move(src_file+filename, src_file+'cats/'+filename)\n    \n\nmove_files(\"/kaggle/working/train/\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(len(os.listdir('/kaggle/working/train/dogs')))\nprint(len(os.listdir('/kaggle/working/train/cats')))\nprint(len(os.listdir('/kaggle/working/train')))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def split_data(SOURCE, VALID, SPLIT_SIZE):\n# This funtion takes as argument:\n###SOURCE : the directory's path of images that will be splitted\n###VALID : the directory's path of the validation receiving the dogs or the cats images\n###SPLIT_SIZE: the size of the split. 0.9 means 90% of cats images will remain in train/cats and 10% will be moved to the validation directory's cats \n###and the same will be done to the dogs images\n    SRC_files = [f for f in os.listdir(SOURCE) if os.path.isfile(os.path.join(SOURCE, f))]\n    SRC_Size = len(SRC_files)\n    #print(SRC_Size)\n    if SRC_Size != 0:\n        # we shuffle the images before the split\n        shuffled_files = random.sample(SRC_files, len(SRC_files))\n        #print(\"shuffled\")\n        TRN_size = int(SRC_Size * SPLIT_SIZE)\n        VAL_SIZE = int(SRC_Size - TRN_size)\n        print(TRN_size)\n        train_set = shuffled_files[0:TRN_size]\n        val_set = shuffled_files[-VAL_SIZE:SRC_Size]\n        for filename in val_set:\n            if os.path.getsize(SOURCE+filename)!=0:\n                shutil.move(SOURCE+filename, VALID+filename)\n            else:\n                print(filename + ' is zero length. So ignoring!')\n                pass\n\n\n                    \nCAT_SOURCE_DIR = \"/kaggle/working/train/cats/\"\nTESTING_CATS_DIR = \"/kaggle/working/val/cats/\"\n\nDOG_SOURCE_DIR = \"/kaggle/working/train/dogs/\"\nTESTING_DOGS_DIR = \"/kaggle/working/val/dogs/\"\n\nsplit_size = .9\nsplit_data(CAT_SOURCE_DIR, TESTING_CATS_DIR, split_size)\nsplit_data(DOG_SOURCE_DIR, TESTING_DOGS_DIR, split_size)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(len(os.listdir('/kaggle/working/train/dogs')))\nprint(len(os.listdir('/kaggle/working/train/cats')))\nprint(len(os.listdir('/kaggle/working/train')))\nprint(len(os.listdir('/kaggle/working/val/dogs')))\nprint(len(os.listdir('/kaggle/working/val/cats')))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from matplotlib.image import imread","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_dog_image = '/kaggle/working/train/dogs/dog.7024.jpg'\nplt.imshow(imread(sample_dog_image))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_path = \"/kaggle/working/train/\"\ntest_path=\"/kaggle/working/test/\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"image_width = 128\nimage_height = 128\nimage_color = 3\nimage_shape = (image_width,image_height,image_color)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.preprocessing.image import load_img,img_to_array\nfrom numpy import expand_dims","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_img_gen = ImageDataGenerator(rotation_range=20,\n                                    height_shift_range=0.06,\n                                    width_shift_range=0.06,\n                                    shear_range=0.1,\n                                    horizontal_flip=True,\n                                    zoom_range=[0.5,0.8])\n\nsample_image = load_img(sample_dog_image)\nsample_image_array = img_to_array(sample_image)\ntest_image = expand_dims(sample_image_array,0)\nimage_aug = sample_img_gen.flow(test_image,batch_size=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for i in range(9):\n    plt.subplot(330+1+i)\n    batch = image_aug.next()\n    image = batch[0].astype('uint8')\n    plt.imshow(image)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for i in range(9):\n    plt.subplot(330+1+i)\n    batch = image_aug.next()\n    image = batch[0].astype('uint8')\n    plt.imshow(image)\nplt.show()","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}