{"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":{"trusted":true},"cell_type":"code","source":"import zipfile\nimport random\nimport shutil","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"path_cats_and_dogs = '/kaggle/input/dogs-vs-cats/train.zip'\nshutil.rmtree('/tmp')\n\nlocal_zip = path_cats_and_dogs\nzip_ref = zipfile.ZipFile(local_zip, 'r')\nzip_ref.extractall('/tmp/source')\nzip_ref.close()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"os.mkdir('/tmp/train')\nos.mkdir('/tmp/train/cats')\nos.mkdir('/tmp/train/dogs')\nos.mkdir('/tmp/validation')\nos.mkdir('/tmp/validation/cats')\nos.mkdir('/tmp/validation/dogs')\nos.mkdir('/tmp/test')\nos.mkdir('/tmp/test/cats')\nos.mkdir('/tmp/test/dogs')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import random","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#train contains all cat and dog pictures\n#function to take from source directory and split by cat and dog\ndef split_dir(source,train_cat_dir,train_dog_dir,val_cat_dir,val_dog_dir,size=0.7):\n    files = os.listdir(source) #list of files in source dir\n    split_num = int(size*len(files)) # no. of files to go into train dir\n    train_files = random.sample(files,split_num) # taking random number of files to go in training_dir\n    val_files = list(set(files)-set(train_files)) # the rest go to validation dir\n    \n    for pic in train_files:\n        if pic[:3] == 'cat':\n            shutil.copy(os.path.join(source,pic),train_cat_dir)\n        elif pic[:3] == 'dog':\n            shutil.copy(os.path.join(source,pic),train_dog_dir)\n    \n    for pic in val_files:\n        if pic[:3] == 'cat':\n            shutil.copy(os.path.join(source,pic),val_cat_dir)\n        elif pic[:3] == 'dog':\n            shutil.copy(os.path.join(source,pic),val_dog_dir)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"os.listdir('/tmp/source/train')[0][:3]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"SOURCE = '/tmp/source/train'\ntrain_cat = '/tmp/train/cats'\ntrain_dog = '/tmp/train/dogs'\nval_cat = '/tmp/validation/cats'\nval_dog = '/tmp/validation/dogs'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"split_dir(SOURCE,train_cat,train_dog,val_cat,val_dog)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(len(os.listdir(SOURCE)))\nprint(len(os.listdir(train_cat))) \nprint(len(os.listdir(train_dog)))\nprint(len(os.listdir(val_cat)))\nprint(len(os.listdir(val_dog)))\n#expecting 17,500 total in training and 7,500 in validation","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Visualising the images"},{"metadata":{"trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport matplotlib.image as mpimg","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#showing 8 random cat images\nplt.figure(figsize=(16,16))\n\n#enumerate with first item being a num between 0 and 7 and second being 8 random numbers between 0 and the length of the train_cat directory\nfor i,cat in enumerate(np.random.randint(0,len(os.listdir(train_cat)),8)):\n  fig = plt.subplot(4,4,i+1) #rows,column,index. Index starts at 1 in matplotlib\n  fig.axis('off')\n  img = mpimg.imread(os.path.join(train_cat, os.listdir(train_cat)[cat])) # reads image from file path\n  fig.imshow(img) # show image","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#showing 8 random dog images\nplt.figure(figsize=(16,16))\nfor i,dog in enumerate(np.random.randint(0,len(os.listdir(train_dog)),8)):\n  fig = plt.subplot(4,4,i+1) #rows,column,index. Index starts at 1 in matplotlib\n  fig.axis('off')\n  img = mpimg.imread(os.path.join(train_dog, os.listdir(train_dog)[dog])) # reads image from file path\n  fig.imshow(img) # show image","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Preprocessing images using generators"},{"metadata":{"trusted":true},"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_dir='/tmp/train'\nvalidation_dir = '/tmp/validation'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#will augment the training images by rotating,shifting,skewing and zooming the images\ntrain_datagen = ImageDataGenerator(rescale=1/255,\n                                  rotation_range=45,\n                                  shear_range=0.2,\n                                  fill_mode='nearest') \n\nval_datagen = ImageDataGenerator(rescale=1/255)\n\ntrain_generator = train_datagen.flow_from_directory(train_dir,\n                                                    batch_size=20,\n                                                    target_size=(150,150),#making all the images 150*150 pixels\n                                                    class_mode='binary')\n\nval_generator = val_datagen.flow_from_directory(validation_dir,\n                                                    batch_size=20,\n                                                    target_size=(150,150),#making all the images 150*150 pixels\n                                                    class_mode='binary')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Building model"},{"metadata":{"trusted":true},"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras import layers","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = Sequential()\n\nmodel.add(layers.Conv2D(16,(3,3),activation ='relu',input_shape=(150,150,3)))\nmodel.add(layers.MaxPooling2D((2,2)))\nmodel.add(layers.Conv2D(32,(3,3),activation ='relu'))\nmodel.add(layers.MaxPooling2D((2,2)))\nmodel.add(layers.Conv2D(64,(3,3),activation ='relu'))\nmodel.add(layers.MaxPooling2D((2,2)))\nmodel.add(layers.Flatten())\nmodel.add(layers.Dense(512,activation='relu'))\nmodel.add(layers.Dense(1,activation='sigmoid'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from tensorflow.keras import optimizers","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(loss='binary_crossentropy',\n             optimizer=optimizers.RMSprop(learning_rate=0.001),\n             metrics=['acc'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from tensorflow.keras.callbacks import EarlyStopping","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"early_stop = EarlyStopping(mode='max',monitor='val_acc',patience=10,verbose=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history = model.fit(train_generator,\n                    validation_data=val_generator,\n                    batch_size=512,\n                    epochs=100,\n                   callbacks=[early_stop])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Predicting"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_generator.class_indices","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"path_test = '/kaggle/input/dogs-vs-cats/test1.zip'\nlocal_zip = path_test\nzip_ref = zipfile.ZipFile(local_zip, 'r')\nzip_ref.extractall('/tmp/source')\nzip_ref.close()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from tensorflow.keras.preprocessing import image","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# predicting images\npred = []\nfor pic in os.listdir('/tmp/source/test1'):\n    path = '/tmp/source/test1/'+ pic\n    img=image.load_img(path, target_size=(150, 150))\n\n    x=image.img_to_array(img)\n    x=np.expand_dims(x, axis=0)\n    x=x/255\n\n    pred.append(int(model.predict_classes(x)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"id_ = [int(name[:-4]) for name in os.listdir('/tmp/source/test1')]\nlabel = pred","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission = pd.DataFrame({'id':id_,\n             'label':pred})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission.sort_values('id',).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}