{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"DLBasics_KerasDataAugmentation_Application.ipynb","provenance":[],"collapsed_sections":[],"authorship_tag":"ABX9TyNNxNwF05yYWdjOoaZZu1x9"},"kernelspec":{"name":"python3","display_name":"Python 3"}},"cells":[{"cell_type":"code","metadata":{"id":"XqJrPBVVg4PP","outputId":"9b06767b-2640-4139-9b04-3bbf8ea749ae","executionInfo":{"status":"ok","timestamp":1647216458566,"user_tz":240,"elapsed":21173,"user":{"displayName":"Guray Erus","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiGnBNnSM10DAl86Xzx2wV9MnzBNDvcAv6FeVBn=s64","userId":"15374812584437350386"}},"colab":{"base_uri":"https://localhost:8080/"}},"source":["from google.colab import drive\n","drive.mount('/content/drive')"],"execution_count":1,"outputs":[{"output_type":"stream","name":"stdout","text":["Mounted at /content/drive\n"]}]},{"cell_type":"code","metadata":{"id":"Ei3nq5dc3cql","executionInfo":{"status":"ok","timestamp":1647216462205,"user_tz":240,"elapsed":3646,"user":{"displayName":"Guray Erus","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiGnBNnSM10DAl86Xzx2wV9MnzBNDvcAv6FeVBn=s64","userId":"15374812584437350386"}}},"source":["### From https://www.kaggle.com/c/dogs-vs-cats/data\n","\n","from keras.preprocessing.image import ImageDataGenerator\n","from keras.models import Sequential\n","from keras.layers import Conv2D, MaxPooling2D\n","from keras.layers import Activation, Dropout, Flatten, Dense\n","from keras import backend as K"],"execution_count":2,"outputs":[]},{"cell_type":"code","metadata":{"id":"khcKQd0x3LsV","executionInfo":{"status":"ok","timestamp":1647216462206,"user_tz":240,"elapsed":7,"user":{"displayName":"Guray Erus","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiGnBNnSM10DAl86Xzx2wV9MnzBNDvcAv6FeVBn=s64","userId":"15374812584437350386"}}},"source":["# variables\n","img_width, img_height = 150, 150\n","\n","BDIR = '/content/drive/MyDrive/CommonFiles/MUSA650-Data/CatsDogs/'\n","\n","train_data_dir = BDIR + 'dataSmall/train'\n","validation_data_dir = BDIR + 'dataSmall/validation'\n","nb_train_samples = 400\n","nb_validation_samples = 200\n","epochs = 50\n","batch_size = 16\n"],"execution_count":3,"outputs":[]},{"cell_type":"code","metadata":{"id":"NLQ_xtL63mJ6","executionInfo":{"status":"ok","timestamp":1647216462207,"user_tz":240,"elapsed":7,"user":{"displayName":"Guray Erus","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiGnBNnSM10DAl86Xzx2wV9MnzBNDvcAv6FeVBn=s64","userId":"15374812584437350386"}}},"source":["if K.image_data_format() == 'channels_first':\n"," input_shape = (3, img_width, img_height)\n","else:\n"," input_shape = (img_width, img_height, 3)\n"],"execution_count":4,"outputs":[]},{"cell_type":"code","metadata":{"id":"_5wlxJ9M3pSZ","executionInfo":{"status":"ok","timestamp":1647216462668,"user_tz":240,"elapsed":467,"user":{"displayName":"Guray Erus","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiGnBNnSM10DAl86Xzx2wV9MnzBNDvcAv6FeVBn=s64","userId":"15374812584437350386"}}},"source":["model = Sequential()\n","model.add(Conv2D(32, (3, 3), input_shape=input_shape))\n","model.add(Activation('relu'))\n","model.add(MaxPooling2D(pool_size=(2, 2)))\n","\n","model.add(Conv2D(32, (3, 3)))\n","model.add(Activation('relu'))\n","model.add(MaxPooling2D(pool_size=(2, 2)))\n","\n","model.add(Conv2D(64, (3, 3)))\n","model.add(Activation('relu'))\n","model.add(MaxPooling2D(pool_size=(2, 2)))\n","\n","model.add(Flatten())\n","model.add(Dense(64))\n","model.add(Activation('relu'))\n","model.add(Dropout(0.5))\n","model.add(Dense(1))\n","model.add(Activation('sigmoid'))\n"],"execution_count":5,"outputs":[]},{"cell_type":"code","metadata":{"id":"H-X8aj0Y3sXR","executionInfo":{"status":"ok","timestamp":1647216462668,"user_tz":240,"elapsed":12,"user":{"displayName":"Guray Erus","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiGnBNnSM10DAl86Xzx2wV9MnzBNDvcAv6FeVBn=s64","userId":"15374812584437350386"}}},"source":["model.compile(loss='binary_crossentropy',\n"," optimizer='rmsprop',\n"," metrics=['accuracy'])"],"execution_count":6,"outputs":[]},{"cell_type":"code","metadata":{"id":"_V0uIPq63uST","executionInfo":{"status":"ok","timestamp":1647216462669,"user_tz":240,"elapsed":11,"user":{"displayName":"Guray Erus","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiGnBNnSM10DAl86Xzx2wV9MnzBNDvcAv6FeVBn=s64","userId":"15374812584437350386"}}},"source":["# this is the augmentation configuration we will use for training\n","train_datagen = ImageDataGenerator(\n"," rescale=1. / 255,\n"," shear_range=0.2,\n"," zoom_range=0.2,\n"," horizontal_flip=True)\n","\n","# this is the augmentation configuration we will use for testing:\n","# only rescaling\n","test_datagen = ImageDataGenerator(rescale=1. / 255)\n"],"execution_count":7,"outputs":[]},{"cell_type":"code","metadata":{"id":"uQBrRbeS3HCP","colab":{"base_uri":"https://localhost:8080/"},"outputId":"7f71c63b-d831-46ac-ea14-fb8c97ed821c","executionInfo":{"status":"ok","timestamp":1647216464916,"user_tz":240,"elapsed":2258,"user":{"displayName":"Guray Erus","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiGnBNnSM10DAl86Xzx2wV9MnzBNDvcAv6FeVBn=s64","userId":"15374812584437350386"}}},"source":["train_generator = train_datagen.flow_from_directory(\n"," train_data_dir,\n"," target_size=(img_width, img_height),\n"," batch_size=batch_size,\n"," class_mode='binary')"],"execution_count":8,"outputs":[{"output_type":"stream","name":"stdout","text":["Found 600 images belonging to 2 classes.\n"]}]},{"cell_type":"code","metadata":{"id":"RJMl4FwG4W2g","colab":{"base_uri":"https://localhost:8080/"},"outputId":"ecbfa224-cdc9-4524-cef4-95e4ebac007c","executionInfo":{"status":"ok","timestamp":1647216465956,"user_tz":240,"elapsed":1046,"user":{"displayName":"Guray Erus","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiGnBNnSM10DAl86Xzx2wV9MnzBNDvcAv6FeVBn=s64","userId":"15374812584437350386"}}},"source":["validation_generator = test_datagen.flow_from_directory(\n"," validation_data_dir,\n"," target_size=(img_width, img_height),\n"," batch_size=batch_size,\n"," class_mode='binary')\n"],"execution_count":9,"outputs":[{"output_type":"stream","name":"stdout","text":["Found 400 images belonging to 2 classes.\n"]}]},{"cell_type":"code","metadata":{"id":"oMLs8aLh30xL","colab":{"base_uri":"https://localhost:8080/"},"outputId":"88df0782-432f-4642-bd2a-dbd3f3bc15d2"},"source":["model.fit_generator(\n"," train_generator,\n"," steps_per_epoch=nb_train_samples // batch_size,\n"," epochs=epochs,\n"," validation_data=validation_generator,\n"," validation_steps=nb_validation_samples // batch_size)\n"],"execution_count":null,"outputs":[{"output_type":"stream","name":"stderr","text":["/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:6: UserWarning: `Model.fit_generator` is deprecated and will be removed in a future version. Please use `Model.fit`, which supports generators.\n"," \n"]},{"output_type":"stream","name":"stdout","text":["Epoch 1/50\n"," 2/25 [=>............................] - ETA: 20s - loss: 1.5367 - accuracy: 0.5938 "]}]},{"cell_type":"code","metadata":{"id":"JKs0hK-M4um2"},"source":["model.save_weights('first_try.h5')"],"execution_count":null,"outputs":[]}]}