{"metadata":{"colab":{"provenance":[],"toc_visible":true},"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":5441,"databundleVersionId":38425,"sourceType":"competition"}],"dockerImageVersionId":30761,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"","metadata":{"id":"mGzS-KPiV_KQ"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from google.colab import drive\n# drive.mount('/content/drive')","metadata":{"id":"XfRy6mq8WDSC","outputId":"064bf996-f8e4-4498-a7a0-363e0b4ed4d3","execution":{"iopub.status.busy":"2024-08-28T09:02:05.045942Z","iopub.execute_input":"2024-08-28T09:02:05.046388Z","iopub.status.idle":"2024-08-28T09:02:05.052135Z","shell.execute_reply.started":"2024-08-28T09:02:05.046345Z","shell.execute_reply":"2024-08-28T09:02:05.050813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\nfrom PIL import Image","metadata":{"id":"2lKzNGqxX0-X","execution":{"iopub.status.busy":"2024-08-28T09:02:08.660122Z","iopub.execute_input":"2024-08-28T09:02:08.660605Z","iopub.status.idle":"2024-08-28T09:02:08.665906Z","shell.execute_reply.started":"2024-08-28T09:02:08.660563Z","shell.execute_reply":"2024-08-28T09:02:08.664678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os","metadata":{"id":"wfnGKT1iX0q5","execution":{"iopub.status.busy":"2024-08-28T09:02:09.601048Z","iopub.execute_input":"2024-08-28T09:02:09.601457Z","iopub.status.idle":"2024-08-28T09:02:09.607022Z","shell.execute_reply.started":"2024-08-28T09:02:09.601417Z","shell.execute_reply":"2024-08-28T09:02:09.605372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Путь к zip архиву на яндекс диске\narchive_path = '/kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip'\n","metadata":{"id":"Lo78LpBbXDqp","execution":{"iopub.status.busy":"2024-08-28T09:02:10.196105Z","iopub.execute_input":"2024-08-28T09:02:10.196592Z","iopub.status.idle":"2024-08-28T09:02:10.202264Z","shell.execute_reply.started":"2024-08-28T09:02:10.196507Z","shell.execute_reply":"2024-08-28T09:02:10.200845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"archive_path_1 = '/kaggle/input/dogs-vs-cats-redux-kernels-edition/test.zip'","metadata":{"id":"WWvcsp0jYHjV","execution":{"iopub.status.busy":"2024-08-28T09:02:10.440968Z","iopub.execute_input":"2024-08-28T09:02:10.441404Z","iopub.status.idle":"2024-08-28T09:02:10.446957Z","shell.execute_reply.started":"2024-08-28T09:02:10.441361Z","shell.execute_reply":"2024-08-28T09:02:10.445582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Распаковываем zip архив\nwith zipfile.ZipFile(archive_path, 'r') as zip_ref:\n    zip_ref.extractall()","metadata":{"id":"wV1qGXvmXKbK","execution":{"iopub.status.busy":"2024-08-28T09:02:11.072644Z","iopub.execute_input":"2024-08-28T09:02:11.073733Z","iopub.status.idle":"2024-08-28T09:02:23.037220Z","shell.execute_reply.started":"2024-08-28T09:02:11.073679Z","shell.execute_reply":"2024-08-28T09:02:23.036086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Распаковываем zip архив\nwith zipfile.ZipFile(archive_path_1, 'r') as zip_ref_1:\n    zip_ref_1.extractall()","metadata":{"id":"MANvSsayYQnL","execution":{"iopub.status.busy":"2024-08-28T09:02:23.039276Z","iopub.execute_input":"2024-08-28T09:02:23.039758Z","iopub.status.idle":"2024-08-28T09:02:28.739153Z","shell.execute_reply.started":"2024-08-28T09:02:23.039712Z","shell.execute_reply":"2024-08-28T09:02:28.738122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(os.listdir())","metadata":{"id":"RtRXz7RsXoQu","execution":{"iopub.status.busy":"2024-08-28T09:02:28.740551Z","iopub.execute_input":"2024-08-28T09:02:28.741163Z","iopub.status.idle":"2024-08-28T09:02:28.747538Z","shell.execute_reply.started":"2024-08-28T09:02:28.741108Z","shell.execute_reply":"2024-08-28T09:02:28.746436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Загрузка необходимых библиотек и модулей\n\nimport zipfile # Библиотека для работы с zip архивами\nimport os      # Библиотека для работы с фаловой системой\nimport time    # Библиотека для работы со временем\n\n# from google.colab import drive # Модуль для работы с Google Disk\nfrom PIL import Image          # Модуль Image для работы с изображениями\nimport pandas as pd","metadata":{"id":"SoJ-I5VcYvpA","execution":{"iopub.status.busy":"2024-08-28T09:02:28.750140Z","iopub.execute_input":"2024-08-28T09:02:28.750829Z","iopub.status.idle":"2024-08-28T09:02:29.253753Z","shell.execute_reply.started":"2024-08-28T09:02:28.750771Z","shell.execute_reply":"2024-08-28T09:02:29.252523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport numpy as np\nimport tensorflow as tf\nfrom tensorflow.keras.applications import vgg16\nimport matplotlib.pyplot as plt\n\nprint(tf.__version__)\nprint(tf.executing_eagerly())","metadata":{"id":"V5BrT9lHYvl_","outputId":"dde42685-b7d2-4791-e535-b862c63c571e","execution":{"iopub.status.busy":"2024-08-28T09:02:29.255487Z","iopub.execute_input":"2024-08-28T09:02:29.256208Z","iopub.status.idle":"2024-08-28T09:02:45.901485Z","shell.execute_reply.started":"2024-08-28T09:02:29.256152Z","shell.execute_reply":"2024-08-28T09:02:45.900288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfrom random import shuffle\nfrom glob import glob\n\nIMG_SIZE = (224, 224)  # размер входного изображения сети\n\n#train_files = glob('data/train_sample/*.jpg')\n#test_files = glob('data/test_sample/*.jpg')\ntrain_files = glob('/kaggle/working/train/*.jpg')\ntest_files = glob('/kaggle/working/test/*.jpg')\n\n\n\n# загружаем входное изображение и предобрабатываем\ndef load_image(path, target_size=IMG_SIZE): # это основная картинка которая загружает картинку\n    img = cv2.imread(path)[...,::-1]\n    img = cv2.resize(img, target_size) # изменяет ширину и высоту до указ.значения, в данном случае до квадратного вида 224х224\n    return vgg16.preprocess_input(img)  # предобработка для VGG16, чтобы картинку можно было запустить в предобуч.нейрон.сеть\n\n\n# функция-генератор загрузки обучающих данных с диска\ndef fit_generator(files, batch_size=32):\n    batch_size = min(batch_size, len(files))\n    while True:\n        shuffle(files)\n        for k in range(len(files) // batch_size):\n            i = k * batch_size\n            j = i + batch_size\n            if j > len(files):\n                j = - j % len(files)\n            x = np.array([load_image(path) for path in files[i:j]])\n            y = np.array([1. if os.path.basename(path).startswith('dog') else 0.\n                          for path in files[i:j]])\n            yield (x, y)\n\n# функция-генератор загрузки тестовых изображений с диска\ndef predict_generator(files):\n    while True:\n        for path in files:\n            yield np.array([load_image(path)])","metadata":{"id":"DHw35kJnYvjH","execution":{"iopub.status.busy":"2024-08-28T09:12:48.664789Z","iopub.execute_input":"2024-08-28T09:12:48.665662Z","iopub.status.idle":"2024-08-28T09:12:48.836133Z","shell.execute_reply.started":"2024-08-28T09:12:48.665610Z","shell.execute_reply":"2024-08-28T09:12:48.835000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"id":"kpgp3oA84NVb"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%matplotlib inline\n\nfrom matplotlib import pyplot as plt\nfig = plt.figure(figsize=(16, 8))\n\nfor i, path in enumerate(train_files[:10], 1):\n    subplot = fig.add_subplot(2, 5, i)\n    subplot.set_title('%s' % path.split('/')[-1])\n    img = cv2.imread(path)[...,::-1]\n    img = cv2.resize(img, IMG_SIZE)\n    plt.imshow(img)","metadata":{"id":"LlZmp7-dYvf-","outputId":"b9b532d4-38f8-4b87-a1fb-ee95ca15f34e","execution":{"iopub.status.busy":"2024-08-28T09:12:52.253200Z","iopub.execute_input":"2024-08-28T09:12:52.253646Z","iopub.status.idle":"2024-08-28T09:12:54.899354Z","shell.execute_reply.started":"2024-08-28T09:12:52.253604Z","shell.execute_reply":"2024-08-28T09:12:54.898054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# base_model - объект класса keras.models.Model (Functional Model)\nbase_model = vgg16.VGG16(weights='imagenet',\n                         include_top=False,\n                         input_shape=(IMG_SIZE[0], IMG_SIZE[1], 3))","metadata":{"id":"y6Ydk_CuZiL7","outputId":"042576d2-5777-4282-af9c-f9ef8b8f66e6","execution":{"iopub.status.busy":"2024-08-28T09:12:54.902210Z","iopub.execute_input":"2024-08-28T09:12:54.902624Z","iopub.status.idle":"2024-08-28T09:12:55.783909Z","shell.execute_reply.started":"2024-08-28T09:12:54.902580Z","shell.execute_reply":"2024-08-28T09:12:55.782866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model.summary()","metadata":{"id":"E2tv5Z6SZiCa","outputId":"8a2aa47f-3e3b-4c96-b7ac-d47d193c450b","execution":{"iopub.status.busy":"2024-08-28T09:12:55.785202Z","iopub.execute_input":"2024-08-28T09:12:55.785544Z","iopub.status.idle":"2024-08-28T09:12:55.830171Z","shell.execute_reply.started":"2024-08-28T09:12:55.785500Z","shell.execute_reply":"2024-08-28T09:12:55.828777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# фиксируем все веса предобученной сети\nfor layer in base_model.layers:\n    layer.trainable = False # мы скажем что эти слои не будут учиться\n\nx = base_model.layers[-5].output # возьмем 5 с конца слой и модифицируем нейронную сеть\n# в итоге мы будем учить не все слои, а только до 5 с конца, а вместо 5 поставим полносвязный слой, который и\n# будет выполнять задачу классификации\nx = tf.keras.layers.Flatten()(x)\nx = tf.keras.layers.Dense(1,  # один выход (бинарная классификация)\n                          activation='sigmoid',  # функция активации\n                          kernel_regularizer=tf.keras.regularizers.l1(1e-4))(x)\n\nmodel = tf.keras.Model(inputs=base_model.input, outputs=x, name='dogs_vs_cats')","metadata":{"id":"gjAJw_afZh2d","execution":{"iopub.status.busy":"2024-08-28T09:12:55.924491Z","iopub.execute_input":"2024-08-28T09:12:55.924962Z","iopub.status.idle":"2024-08-28T09:12:55.952440Z","shell.execute_reply.started":"2024-08-28T09:12:55.924917Z","shell.execute_reply":"2024-08-28T09:12:55.951350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"id":"m3itBbjNZqUH","outputId":"3af8536e-d6ac-4a7d-f2e4-5cb80b2de433","execution":{"iopub.status.busy":"2024-08-28T09:12:56.139731Z","iopub.execute_input":"2024-08-28T09:12:56.140778Z","iopub.status.idle":"2024-08-28T09:12:56.179468Z","shell.execute_reply.started":"2024-08-28T09:12:56.140730Z","shell.execute_reply":"2024-08-28T09:12:56.177983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',\n              loss='binary_crossentropy',  # функция потерь binary_crossentropy (log loss\n              metrics=['accuracy'])","metadata":{"id":"AJxgrnsPZqQ_","execution":{"iopub.status.busy":"2024-08-28T09:12:59.355162Z","iopub.execute_input":"2024-08-28T09:12:59.355606Z","iopub.status.idle":"2024-08-28T09:12:59.372072Z","shell.execute_reply.started":"2024-08-28T09:12:59.355565Z","shell.execute_reply":"2024-08-28T09:12:59.370985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_samples = 5  # число изображений в валидационной выборке\n\nshuffle(train_files)  # перемешиваем обучающую выборку\nvalidation_data = next(fit_generator(train_files[:val_samples], val_samples))\ntrain_data = fit_generator(train_files[val_samples:])  # данные читаем функцией-генератором\n\n# запускаем процесс обучения\nmodel.fit(train_data,\n          steps_per_epoch=10,  # число вызовов генератора за эпоху\n          epochs=10,  # число эпох обучения\n          validation_data=validation_data)","metadata":{"id":"V5s6_baDZqNu","outputId":"1ad87b22-4955-4ac1-a2e8-d72b75293d86","execution":{"iopub.status.busy":"2024-08-28T09:13:01.693316Z","iopub.execute_input":"2024-08-28T09:13:01.694228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('cats-dogs-vgg16.hdf5') # сохраним веса модели методом save","metadata":{"id":"pLeN4nNSEEvt","outputId":"aa2dd3be-f977-4c99-af0d-6b96c9524a18"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_pred = model.predict(\n    predict_generator(test_files), steps=len(test_files))","metadata":{"id":"9BprHnVFEhgC","outputId":"491c971b-1b42-493c-8ebf-a46f05ca80a8"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(16, 8))\nfor i, (path, score) in enumerate(zip(test_files[:10], test_pred[:10]), 1):\n    subplot = fig.add_subplot(2, 5, i)\n    subplot.set_title('%.2f %s' % (score, os.path.basename(path)))\n    img = cv2.imread(path)[...,::-1]\n    img = cv2.resize(img, IMG_SIZE)\n    subplot.imshow(img)","metadata":{"id":"LACXONnMEhcL","outputId":"a14ca23b-cf6c-4666-8703-a411a83b036c"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import re\n\nwith open('submit.txt', 'w') as dst:\n    dst.write('id,label\\n')\n    for path, score in zip(test_files, test_pred):\n        dst.write('%s,%f\\n' % (re.search('(\\d+).jpg$', path).group(1), score))","metadata":{"id":"_40wR8Lyly8T","outputId":"54e00cfe-791b-4eef-ff7e-cd6cca273afc"},"execution_count":null,"outputs":[]}]}