{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Домашняя работа №4\n\n# Выполнил: Лобанов Александр","metadata":{}},{"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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-11-01T13:26:52.062393Z","iopub.execute_input":"2021-11-01T13:26:52.063252Z","iopub.status.idle":"2021-11-01T13:26:52.071641Z","shell.execute_reply.started":"2021-11-01T13:26:52.063204Z","shell.execute_reply":"2021-11-01T13:26:52.071003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport shutil\nimport re\nimport requests\nimport pandas as pd\nfrom tqdm.notebook import tqdm\nfrom tensorflow.keras.preprocessing import image\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img\nfrom tensorflow.keras import layers, models, optimizers\nfrom tensorflow.keras.applications import DenseNet169, VGG16, Xception, ResNet152V2\nimport tensorflow.image\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Flatten, Conv2D, MaxPooling2D, Dense\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.losses import SparseCategoricalCrossentropy","metadata":{"execution":{"iopub.status.busy":"2021-11-01T13:26:52.073455Z","iopub.execute_input":"2021-11-01T13:26:52.073697Z","iopub.status.idle":"2021-11-01T13:26:52.086368Z","shell.execute_reply.started":"2021-11-01T13:26:52.073669Z","shell.execute_reply":"2021-11-01T13:26:52.085616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_history(history):\n    acc = history.history['acc']\n    val_acc = history.history['val_acc']\n    loss = history.history['loss']\n    val_loss = history.history['val_loss']\n\n    epochs = range(len(acc))\n\n    fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(20, 6))\n    \n    ax[0].plot(epochs, acc, 'bo', label='Точность обучения')\n    ax[0].plot(epochs, val_acc, 'b', label='Точность валидации')\n    ax[0].set_title('Точность (accuracy)')\n    ax[0].legend()\n    ax[0].grid()\n\n    ax[1].plot(epochs, loss, 'bo', label='Функция потерь обучения')\n    ax[1].plot(epochs, val_loss, 'b', label='Функция потерь валидации')\n    ax[1].set_title('Функция потерь')\n    ax[1].legend()\n    ax[1].grid()","metadata":{"execution":{"iopub.status.busy":"2021-11-01T13:26:52.087721Z","iopub.execute_input":"2021-11-01T13:26:52.088103Z","iopub.status.idle":"2021-11-01T13:26:52.098935Z","shell.execute_reply.started":"2021-11-01T13:26:52.088061Z","shell.execute_reply":"2021-11-01T13:26:52.098100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\n\nwith zipfile.ZipFile(\"../input/dogs-vs-cats/train.zip\",\"r\") as z:\n    z.extractall(\".\")\n    \nwith zipfile.ZipFile(\"../input/dogs-vs-cats/test1.zip\",\"r\") as z:\n    z.extractall(\".\") ","metadata":{"execution":{"iopub.status.busy":"2021-11-01T13:26:52.100595Z","iopub.execute_input":"2021-11-01T13:26:52.101600Z","iopub.status.idle":"2021-11-01T13:27:06.726332Z","shell.execute_reply.started":"2021-11-01T13:26:52.101556Z","shell.execute_reply":"2021-11-01T13:27:06.725411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Retrieving a list of directories in each folder\n\nDIR_TRAIN = \"/kaggle/working/train/\"\nDIR_TEST = \"/kaggle/working/test1\"\n\ntrain_imgs = os.listdir(DIR_TRAIN)\ntest_imgs = os.listdir(DIR_TEST)","metadata":{"execution":{"iopub.status.busy":"2021-11-01T13:27:06.728913Z","iopub.execute_input":"2021-11-01T13:27:06.729148Z","iopub.status.idle":"2021-11-01T13:27:06.755588Z","shell.execute_reply.started":"2021-11-01T13:27:06.729119Z","shell.execute_reply":"2021-11-01T13:27:06.754673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = load_img(\"/kaggle/working/train/\"+ \"dog.10008.jpg\")\nplt.imshow(image)\nplt.axis(\"off\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-01T13:27:06.756885Z","iopub.execute_input":"2021-11-01T13:27:06.757805Z","iopub.status.idle":"2021-11-01T13:27:06.942719Z","shell.execute_reply.started":"2021-11-01T13:27:06.757729Z","shell.execute_reply":"2021-11-01T13:27:06.941742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Creating a DataFrame for our train set\n\ncategory = [x.split(\".\")[0] for x in train_imgs]\ndf = pd.DataFrame({\"Filename\":train_imgs, \"Category\":category})\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-01T13:27:06.944185Z","iopub.execute_input":"2021-11-01T13:27:06.944528Z","iopub.status.idle":"2021-11-01T13:27:06.970327Z","shell.execute_reply.started":"2021-11-01T13:27:06.944487Z","shell.execute_reply":"2021-11-01T13:27:06.969419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train, df_test = train_test_split(df, test_size = 7500, \n                                     stratify=df['Category'],\n                                     random_state=42)","metadata":{"execution":{"iopub.status.busy":"2021-11-01T13:27:06.971862Z","iopub.execute_input":"2021-11-01T13:27:06.972104Z","iopub.status.idle":"2021-11-01T13:27:07.011622Z","shell.execute_reply.started":"2021-11-01T13:27:06.972074Z","shell.execute_reply":"2021-11-01T13:27:07.010747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['Category'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-11-01T13:27:07.012980Z","iopub.execute_input":"2021-11-01T13:27:07.013223Z","iopub.status.idle":"2021-11-01T13:27:07.023767Z","shell.execute_reply.started":"2021-11-01T13:27:07.013194Z","shell.execute_reply":"2021-11-01T13:27:07.022937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test['Category'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-11-01T13:27:07.025056Z","iopub.execute_input":"2021-11-01T13:27:07.025313Z","iopub.status.idle":"2021-11-01T13:27:07.041387Z","shell.execute_reply.started":"2021-11-01T13:27:07.025282Z","shell.execute_reply":"2021-11-01T13:27:07.040569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(rescale=1./255)\ntest_datagen = ImageDataGenerator(rescale=1./255)\n\ntrain_generator = train_datagen.flow_from_dataframe(\n        df_train,\n        directory = DIR_TRAIN,\n        x_col = 'Filename',\n        y_col = 'Category',\n        target_size = (200, 200),\n        class_mode = 'binary',\n        batch_size = 20)\n\ntest_generator = test_datagen.flow_from_dataframe(\n        df_test,\n        directory = DIR_TRAIN,\n        x_col = 'Filename',\n        y_col = 'Category',\n        target_size = (200, 200),\n        class_mode = 'binary',\n        batch_size = 20)\n\n","metadata":{"execution":{"iopub.status.busy":"2021-11-01T13:27:07.042780Z","iopub.execute_input":"2021-11-01T13:27:07.043684Z","iopub.status.idle":"2021-11-01T13:27:07.301624Z","shell.execute_reply.started":"2021-11-01T13:27:07.043636Z","shell.execute_reply":"2021-11-01T13:27:07.300906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(8, (3, 3), padding='SAME', activation='relu',\n                        input_shape=(200, 200, 3)))\nmodel.add(MaxPooling2D((2, 2)))\nmodel.add(Conv2D(16, (3, 3), padding='SAME', activation='relu'))\nmodel.add(MaxPooling2D((2, 2)))\nmodel.add(Conv2D(32, (3, 3), activation='relu'))\nmodel.add(MaxPooling2D((2, 2)))\nmodel.add(Conv2D(64, (3, 3), activation='relu'))\nmodel.add(MaxPooling2D((2, 2)))\nmodel.add(Conv2D(128, (3, 3), activation='relu'))\nmodel.add(Flatten())\nmodel.add(Dense(256, activation='relu'))\nmodel.add(Dense(1, activation='sigmoid'))","metadata":{"execution":{"iopub.status.busy":"2021-11-01T13:27:07.302731Z","iopub.execute_input":"2021-11-01T13:27:07.303205Z","iopub.status.idle":"2021-11-01T13:27:07.414549Z","shell.execute_reply.started":"2021-11-01T13:27:07.303162Z","shell.execute_reply":"2021-11-01T13:27:07.413319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-11-01T13:27:07.415935Z","iopub.execute_input":"2021-11-01T13:27:07.416206Z","iopub.status.idle":"2021-11-01T13:27:07.426739Z","shell.execute_reply.started":"2021-11-01T13:27:07.416177Z","shell.execute_reply":"2021-11-01T13:27:07.425710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='binary_crossentropy',\n              optimizer=Adam(learning_rate=2e-4),\n              metrics=['acc'])","metadata":{"execution":{"iopub.status.busy":"2021-11-01T13:27:07.429928Z","iopub.execute_input":"2021-11-01T13:27:07.430183Z","iopub.status.idle":"2021-11-01T13:27:07.440280Z","shell.execute_reply.started":"2021-11-01T13:27:07.430154Z","shell.execute_reply":"2021-11-01T13:27:07.439441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_baseline = model.fit(\n      train_generator,\n      steps_per_epoch=100,\n      epochs=30,\n      validation_data=test_generator,\n      validation_steps=50)","metadata":{"execution":{"iopub.status.busy":"2021-11-01T13:27:07.441740Z","iopub.execute_input":"2021-11-01T13:27:07.442670Z","iopub.status.idle":"2021-11-01T13:35:38.071974Z","shell.execute_reply.started":"2021-11-01T13:27:07.442623Z","shell.execute_reply":"2021-11-01T13:35:38.071226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_history(history_baseline)","metadata":{"execution":{"iopub.status.busy":"2021-11-01T13:35:38.073201Z","iopub.execute_input":"2021-11-01T13:35:38.073885Z","iopub.status.idle":"2021-11-01T13:35:38.514547Z","shell.execute_reply.started":"2021-11-01T13:35:38.073849Z","shell.execute_reply":"2021-11-01T13:35:38.513766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Обучение модели с применением аугментации","metadata":{}},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(rescale=1./255,\n    rotation_range=20,\n    width_shift_range=0.2,\n    height_shift_range=0.1,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True,)\n\ntest_datagen = ImageDataGenerator(rescale=1./255)\n\ntrain_generator = train_datagen.flow_from_dataframe(\n        df_train,\n        directory = DIR_TRAIN,\n        x_col = 'Filename',\n        y_col = 'Category',\n        target_size = (200, 200),\n        class_mode = 'binary',\n        batch_size = 20)\n\ntest_generator = test_datagen.flow_from_dataframe(\n        df_test,\n        directory = DIR_TRAIN,\n        x_col = 'Filename',\n        y_col = 'Category',\n        target_size = (200, 200),\n        class_mode = 'binary',\n        batch_size = 20)","metadata":{"execution":{"iopub.status.busy":"2021-11-01T13:35:38.515573Z","iopub.execute_input":"2021-11-01T13:35:38.515786Z","iopub.status.idle":"2021-11-01T13:35:38.768034Z","shell.execute_reply.started":"2021-11-01T13:35:38.515745Z","shell.execute_reply":"2021-11-01T13:35:38.767094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for data_batch, labels_batch in train_generator:\n    print('data batch shape:', data_batch.shape)\n    print('labels batch shape:', labels_batch.shape)\n    break","metadata":{"execution":{"iopub.status.busy":"2021-11-01T13:35:38.769276Z","iopub.execute_input":"2021-11-01T13:35:38.769586Z","iopub.status.idle":"2021-11-01T13:35:38.992051Z","shell.execute_reply.started":"2021-11-01T13:35:38.769549Z","shell.execute_reply":"2021-11-01T13:35:38.991132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = models.Sequential()\nmodel.add(layers.Conv2D(8, (3, 3), padding='SAME', activation='relu',\n                        input_shape=(200, 200, 3)))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Conv2D(16, (3, 3), padding='SAME', activation='relu'))\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.Conv2D(128, (3, 3), activation='relu'))\nmodel.add(layers.Flatten())\nmodel.add(layers.Dense(256, activation='relu'))\nmodel.add(layers.Dense(1, activation='sigmoid'))","metadata":{"execution":{"iopub.status.busy":"2021-11-01T13:35:38.993265Z","iopub.execute_input":"2021-11-01T13:35:38.993511Z","iopub.status.idle":"2021-11-01T13:35:39.092658Z","shell.execute_reply.started":"2021-11-01T13:35:38.993483Z","shell.execute_reply":"2021-11-01T13:35:39.091570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-11-01T13:35:39.094890Z","iopub.execute_input":"2021-11-01T13:35:39.095198Z","iopub.status.idle":"2021-11-01T13:35:39.107620Z","shell.execute_reply.started":"2021-11-01T13:35:39.095158Z","shell.execute_reply":"2021-11-01T13:35:39.106757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='binary_crossentropy',\n              optimizer=optimizers.Adam(learning_rate=2e-4),\n              metrics=['acc'])","metadata":{"execution":{"iopub.status.busy":"2021-11-01T13:35:39.109653Z","iopub.execute_input":"2021-11-01T13:35:39.110131Z","iopub.status.idle":"2021-11-01T13:35:39.119274Z","shell.execute_reply.started":"2021-11-01T13:35:39.110089Z","shell.execute_reply":"2021-11-01T13:35:39.118739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_augmentation = model.fit(\n      train_generator,\n      steps_per_epoch=100,\n      epochs=140,\n      validation_data=test_generator,\n      validation_steps=50)","metadata":{"execution":{"iopub.status.busy":"2021-11-01T13:35:39.120669Z","iopub.execute_input":"2021-11-01T13:35:39.121132Z","iopub.status.idle":"2021-11-01T15:01:28.723046Z","shell.execute_reply.started":"2021-11-01T13:35:39.121093Z","shell.execute_reply":"2021-11-01T15:01:28.721986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_history(history_augmentation)","metadata":{"execution":{"iopub.status.busy":"2021-11-01T15:01:28.725424Z","iopub.execute_input":"2021-11-01T15:01:28.725693Z","iopub.status.idle":"2021-11-01T15:01:29.190484Z","shell.execute_reply.started":"2021-11-01T15:01:28.725661Z","shell.execute_reply":"2021-11-01T15:01:29.189583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Заключение\n\nСравнивая значения accuracy на тестовой выборке базовой модели (history_baseline) и на модифицированной модели (history_augmentation), можно сделать вывод, что оптимизация при помощи приема аугментации проведена успешна. \n\n> Значения accuracy значительно улучшилось: \n* Accuracy на history_baseline: 0.7730\n* Accuracy на history_augmentation: 0.8840","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}