{"cells":[{"cell_type":"code","execution_count":1,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":21173,"status":"ok","timestamp":1647216458566,"user":{"displayName":"Guray Erus","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiGnBNnSM10DAl86Xzx2wV9MnzBNDvcAv6FeVBn=s64","userId":"15374812584437350386"},"user_tz":240},"id":"XqJrPBVVg4PP","outputId":"9b06767b-2640-4139-9b04-3bbf8ea749ae"},"outputs":[{"name":"stdout","output_type":"stream","text":["Mounted at /content/drive\n"]}],"source":["from google.colab import drive\n","drive.mount('/content/drive')"]},{"cell_type":"code","execution_count":2,"metadata":{"executionInfo":{"elapsed":3646,"status":"ok","timestamp":1647216462205,"user":{"displayName":"Guray Erus","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiGnBNnSM10DAl86Xzx2wV9MnzBNDvcAv6FeVBn=s64","userId":"15374812584437350386"},"user_tz":240},"id":"Ei3nq5dc3cql"},"outputs":[],"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"]},{"cell_type":"code","execution_count":3,"metadata":{"executionInfo":{"elapsed":7,"status":"ok","timestamp":1647216462206,"user":{"displayName":"Guray Erus","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiGnBNnSM10DAl86Xzx2wV9MnzBNDvcAv6FeVBn=s64","userId":"15374812584437350386"},"user_tz":240},"id":"khcKQd0x3LsV"},"outputs":[],"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"]},{"cell_type":"code","execution_count":4,"metadata":{"executionInfo":{"elapsed":7,"status":"ok","timestamp":1647216462207,"user":{"displayName":"Guray Erus","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiGnBNnSM10DAl86Xzx2wV9MnzBNDvcAv6FeVBn=s64","userId":"15374812584437350386"},"user_tz":240},"id":"NLQ_xtL63mJ6"},"outputs":[],"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"]},{"cell_type":"code","execution_count":5,"metadata":{"executionInfo":{"elapsed":467,"status":"ok","timestamp":1647216462668,"user":{"displayName":"Guray Erus","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiGnBNnSM10DAl86Xzx2wV9MnzBNDvcAv6FeVBn=s64","userId":"15374812584437350386"},"user_tz":240},"id":"_5wlxJ9M3pSZ"},"outputs":[],"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"]},{"cell_type":"code","execution_count":6,"metadata":{"executionInfo":{"elapsed":12,"status":"ok","timestamp":1647216462668,"user":{"displayName":"Guray Erus","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiGnBNnSM10DAl86Xzx2wV9MnzBNDvcAv6FeVBn=s64","userId":"15374812584437350386"},"user_tz":240},"id":"H-X8aj0Y3sXR"},"outputs":[],"source":["model.compile(loss='binary_crossentropy',\n"," optimizer='rmsprop',\n"," metrics=['accuracy'])"]},{"cell_type":"code","execution_count":7,"metadata":{"executionInfo":{"elapsed":11,"status":"ok","timestamp":1647216462669,"user":{"displayName":"Guray Erus","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiGnBNnSM10DAl86Xzx2wV9MnzBNDvcAv6FeVBn=s64","userId":"15374812584437350386"},"user_tz":240},"id":"_V0uIPq63uST"},"outputs":[],"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"]},{"cell_type":"code","execution_count":8,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":2258,"status":"ok","timestamp":1647216464916,"user":{"displayName":"Guray Erus","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiGnBNnSM10DAl86Xzx2wV9MnzBNDvcAv6FeVBn=s64","userId":"15374812584437350386"},"user_tz":240},"id":"uQBrRbeS3HCP","outputId":"7f71c63b-d831-46ac-ea14-fb8c97ed821c"},"outputs":[{"name":"stdout","output_type":"stream","text":["Found 600 images belonging to 2 classes.\n"]}],"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')"]},{"cell_type":"code","execution_count":9,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":1046,"status":"ok","timestamp":1647216465956,"user":{"displayName":"Guray Erus","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiGnBNnSM10DAl86Xzx2wV9MnzBNDvcAv6FeVBn=s64","userId":"15374812584437350386"},"user_tz":240},"id":"RJMl4FwG4W2g","outputId":"ecbfa224-cdc9-4524-cef4-95e4ebac007c"},"outputs":[{"name":"stdout","output_type":"stream","text":["Found 400 images belonging to 2 classes.\n"]}],"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"]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"oMLs8aLh30xL","outputId":"88df0782-432f-4642-bd2a-dbd3f3bc15d2"},"outputs":[{"name":"stderr","output_type":"stream","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"]},{"name":"stdout","output_type":"stream","text":["Epoch 1/50\n"," 2/25 [=>............................] - ETA: 20s - loss: 1.5367 - accuracy: 0.5938 "]}],"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"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"JKs0hK-M4um2"},"outputs":[],"source":["model.save_weights('first_try.h5')"]}],"metadata":{"colab":{"authorship_tag":"ABX9TyNNxNwF05yYWdjOoaZZu1x9","collapsed_sections":[],"name":"DLBasics_KerasDataAugmentation_Application.ipynb","provenance":[]},"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.11.0"},"vscode":{"interpreter":{"hash":"c261aea317cc0286b3b3261fbba9abdec21eaa57589985bb7a274bf54d6cc0a7"}}},"nbformat":4,"nbformat_minor":0}