{"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 the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout\nfrom keras.optimizers import Adam\nfrom keras.preprocessing.image import ImageDataGenerator","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8ca09d82d38cb06aeac61ca45d98154dd4bb36ed"},"cell_type":"code","source":"# Image dimensions\nimg_width, img_height = 150, 150 ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7d2d09e5810b25475eb1bc43c2ee567f7b485e3a"},"cell_type":"code","source":"def create_model(p, input_shape=(32, 32, 3)):\n    # Initialising the CNN\n    model = Sequential()\n    # Convolution + Pooling Layer \n    model.add(Conv2D(32, (3, 3), padding='same', input_shape=input_shape, activation='relu'))\n    model.add(MaxPooling2D(pool_size=(2, 2)))\n    # Convolution + Pooling Layer \n    model.add(Conv2D(32, (3, 3), padding='same', activation='relu'))\n    model.add(MaxPooling2D(pool_size=(2, 2)))\n    # Convolution + Pooling Layer \n    model.add(Conv2D(64, (3, 3), padding='same', activation='relu'))\n    model.add(MaxPooling2D(pool_size=(2, 2)))\n    # Convolution + Pooling Layer \n    model.add(Conv2D(64, (3, 3), padding='same', activation='relu'))\n    model.add(MaxPooling2D(pool_size=(2, 2)))\n    \n    # Flattening\n    model.add(Flatten())\n    # Fully connection\n    model.add(Dense(64, activation='relu'))\n    model.add(Dropout(p))\n    model.add(Dense(64, activation='relu'))\n    model.add(Dense(64, activation='relu'))\n    model.add(Dropout(p/2))\n    model.add(Dense(1, activation='sigmoid'))\n    \n    # Compiling the CNN\n    optimizer = Adam(lr=1e-3)\n    metrics=['accuracy']\n    model.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=metrics)\n    return model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e9d4539878d3426de30c394a1571405d1a979c15"},"cell_type":"code","source":"def run_training(bs=32, epochs=10):\n    \n    train_datagen = ImageDataGenerator(rescale = 1./255, \n                                       shear_range = 0.2, \n                                       zoom_range = 0.2, \n                                       horizontal_flip = True)\n    test_datagen = ImageDataGenerator(rescale = 1./255)\n \n    training_set = train_datagen.flow_from_directory('../input/dataset/dataset/training_set',\n                                                 target_size = (img_width, img_height),\n                                                 batch_size = bs,\n                                                 class_mode = 'binary')\n                                                 \n    test_set = test_datagen.flow_from_directory('../input/dataset/dataset/test_set',\n                                            target_size = (img_width, img_height),\n                                            batch_size = bs,\n                                            class_mode = 'binary')\n                                            \n    model = create_model(p=0.6, input_shape=(img_width, img_height, 3))                                  \n    model.fit_generator(training_set,\n                         steps_per_epoch=8000/bs,\n                         epochs = epochs,\n                         validation_data = test_set,\n                         validation_steps = 2000/bs)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3d831cafee3f8143d456ebb6f1a73cf7c15839f0"},"cell_type":"code","source":"def main():\n    run_training(bs=32, epochs=100)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e2da6976916d4a01f39c9f168d653639145db400"},"cell_type":"code","source":"if __name__ == \"__main__\":\n    main()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"884f60db8e4785bdd9d9bb2a433886382a08280f"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}