{"cells":[{"metadata":{"trusted":true,"collapsed":true,"_uuid":"21a62f277febc0c42b20ae87d5ac4657b09f8f2f"},"cell_type":"markdown","source":"***This is my code to train a deep learning model to differentiate picutres of Cats and Dogs***"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"#import needed libraries\nimport numpy as np\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.python import keras\nfrom tensorflow.python.keras.models import Sequential\nfrom tensorflow.python.keras.layers import Dense, Flatten, Conv2D, Dropout\nfrom tensorflow.python.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.python.keras.preprocessing.image import load_img, img_to_array\nfrom tensorflow.python.keras.applications.resnet50 import preprocess_input","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"#PREPARE DATA (Pictures)\n#we will have two different ways to preprocess\n#with ImageGenerator function and without, then we will compare the results\n# NUMBER 1: my own function to preprocess data\n\nimg_size=224\ndef read_and_prep_images(img_paths, img_height=img_size, img_width=img_size):\n    imgs= [load_img(img_path, target_size=(img_height, img_width)) for img_path in img_paths]\n    img_array=np.array([img_to_array(img) for img in imgs])\n    output=preprocess_input(img_array)\n    return(output)\n\nimgs_paths_train1='../input/train'\nimgs_paths_test1='../input/test'\n\nimage_data_train1=read_and_prep_images(imgs_paths_train1)\nimage_data_test1=read_and_prep_images(imgs_paths_test1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e345e3471ef998e07f1ea80e4b4c0391d8d799d6"},"cell_type":"code","source":"#Number 2: Using ImageGenerator\n#also allows us to use data_augmentation\n\nimage_size=224\nimgs_paths_train2='../input/train'\nimgs_paths_test2='../input/test'\n\ndata_generator_with_aug=ImageDataGenerator(preprocessing_function=preprocess_input,\n                                           horizontal_flip=True,\n                                           width_shift_range = 0.2,\n                                           height_shift_range = 0.2)\n\ndata_generator_no_aug=ImageDataGenerator(preprocessing_function=preprocess_input)\n\ntrain_generator_aug=data_generator_with_aug.flow_from_directory(\n        directory='../input/train/',\n        target_size=(image_size, image_size),\n        batch_size=100,\n        class_mode='categorical')\n\ntrain_generator_no_aug=data_generator_no_aug.flow_from_directory(\n        directory='../input/train/',\n        target_size=(image_size, image_size),\n        batch_size=100,\n        class_mode='categorical')\n\ntest_generator=data_generator_no_aug.flow_from_directory(\n        directory='../input/test/',\n        target_size=(image_size, image_size),\n        class_mode='categorical')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"f4464316d79d01715f18a220d359f64f8ed67822"},"cell_type":"code","source":"#CONSTRUCT A MODEL\n#now image data is prepared with two different technics, lets see which one will work\n\nnum_classes=2\nmodel=Sequential()\nmodel.add(Conv2D(24, kernel_size=(3,3), strides=2,\n                activation='relu',\n                input_shape=(image_size, image_size,1)))\nmodel.add(Dropout(0.5))\nmodel.add(Conv2D(24, kernel_size=(3, 3), strides=2,\n                activation='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Conv2D(24, kernel_size=(3,3), strides=2,\n                activation='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Flatten())\nmodel.add(Dense(128, activation='relu'))\nmodel.add(Dense(num_classes, activation='softmax'))\n\n#compile the model\nmodel.compile(loss=keras.losses.categorical_crossentropy,\n             optimizer='adam',\n             metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"35816092d6044b64fe7698ffb0c83e620b9ec75a","collapsed":true},"cell_type":"code","source":"#FINALLY RUN THE MODEL\n#many different cases\n#1. train with augmentation\nmodel.fit(train_generator_aug,\n                   epochs=3)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}