{"metadata":{"colab":{"provenance":[],"toc_visible":true},"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":5441,"databundleVersionId":38425,"sourceType":"competition"}],"dockerImageVersionId":30733,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import cv2\nimport numpy as np\nimport tensorflow as tf\nfrom tensorflow.keras.applications import vgg16\n\nprint(tf.__version__)\nprint(tf.executing_eagerly())","metadata":{"id":"3EUhtz2UHHAk","outputId":"441e29e3-c44a-4d90-8727-de537521ce99","execution":{"iopub.status.busy":"2024-06-25T06:06:44.667215Z","iopub.execute_input":"2024-06-25T06:06:44.667995Z","iopub.status.idle":"2024-06-25T06:06:44.673652Z","shell.execute_reply.started":"2024-06-25T06:06:44.667966Z","shell.execute_reply":"2024-06-25T06:06:44.672635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip /kaggle/input/dogs-vs-cats-redux-kernels-edition/test.zip\n!unzip /kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip","metadata":{"execution":{"iopub.status.busy":"2024-06-25T05:57:12.945011Z","iopub.execute_input":"2024-06-25T05:57:12.945682Z","iopub.status.idle":"2024-06-25T05:57:35.986681Z","shell.execute_reply.started":"2024-06-25T05:57:12.945650Z","shell.execute_reply":"2024-06-25T05:57:35.985720Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import zipfile\n# from google.colab import drive\n# from PIL import Image","metadata":{"id":"remW_IjZcUkM","execution":{"iopub.status.busy":"2024-06-25T05:57:35.988534Z","iopub.execute_input":"2024-06-25T05:57:35.988869Z","iopub.status.idle":"2024-06-25T05:57:35.993049Z","shell.execute_reply.started":"2024-06-25T05:57:35.988840Z","shell.execute_reply":"2024-06-25T05:57:35.992129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# drive.mount(\"/content/drive\", force_remount=False)","metadata":{"id":"q8Zfnp6Jcot2","outputId":"75d001ba-c2ed-4d6f-8fa8-2f3584b5a562","execution":{"iopub.status.busy":"2024-06-25T05:57:35.994279Z","iopub.execute_input":"2024-06-25T05:57:35.994875Z","iopub.status.idle":"2024-06-25T05:57:36.005116Z","shell.execute_reply.started":"2024-06-25T05:57:35.994845Z","shell.execute_reply":"2024-06-25T05:57:36.004418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"id":"PZXGVfS5c-ix"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfrom random import shuffle\nfrom glob import glob\n\nIMG_SIZE = (224, 224)  # размер входного изображения сети\n\ntrain_files = glob('train/*.jpg')\ntest_files = glob('test/*.jpg')\n\n# загружаем входное изображение и предобрабатываем\ndef load_image(path, target_size=IMG_SIZE):\n    img = cv2.imread(path)[...,::-1]\n    img = cv2.resize(img, target_size)\n    return vgg16.preprocess_input(img)  # предобработка для VGG16\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":"oQDQu6zrHHfz","execution":{"iopub.status.busy":"2024-06-25T06:08:25.234257Z","iopub.execute_input":"2024-06-25T06:08:25.234612Z","iopub.status.idle":"2024-06-25T06:08:25.373626Z","shell.execute_reply.started":"2024-06-25T06:08:25.234584Z","shell.execute_reply":"2024-06-25T06:08:25.372782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_files[:10]","metadata":{"execution":{"iopub.status.busy":"2024-06-25T06:07:21.350905Z","iopub.execute_input":"2024-06-25T06:07:21.351298Z","iopub.status.idle":"2024-06-25T06:07:21.357453Z","shell.execute_reply.started":"2024-06-25T06:07:21.351267Z","shell.execute_reply":"2024-06-25T06:07:21.356604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%matplotlib inline\nfrom matplotlib import pyplot as plt\nfig = plt.figure(figsize=(16, 8))\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)\nplt.show()","metadata":{"id":"uJ0-V9rMHO25","outputId":"22aa158a-d36b-48d3-eb95-5b685b11d2de","execution":{"iopub.status.busy":"2024-06-25T06:07:22.947354Z","iopub.execute_input":"2024-06-25T06:07:22.948073Z","iopub.status.idle":"2024-06-25T06:07:24.700670Z","shell.execute_reply.started":"2024-06-25T06:07:22.948042Z","shell.execute_reply":"2024-06-25T06:07:24.699613Z"},"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":"AOLpcWIRHQ5F","outputId":"ce91253e-c9f2-4432-a21c-6df7d4f53789","execution":{"iopub.status.busy":"2024-06-25T06:07:51.102788Z","iopub.execute_input":"2024-06-25T06:07:51.103493Z","iopub.status.idle":"2024-06-25T06:07:51.372871Z","shell.execute_reply.started":"2024-06-25T06:07:51.103463Z","shell.execute_reply":"2024-06-25T06:07:51.372064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model.summary()\n","metadata":{"id":"mjo6g9Tl18R0","outputId":"1b352861-a04f-4d00-e646-f3020c82db9f","execution":{"iopub.status.busy":"2024-06-25T06:07:51.601890Z","iopub.execute_input":"2024-06-25T06:07:51.602689Z","iopub.status.idle":"2024-06-25T06:07:51.634785Z","shell.execute_reply.started":"2024-06-25T06:07:51.602660Z","shell.execute_reply":"2024-06-25T06:07:51.633918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# фиксируем все веса предобученной сети\nfor layer in base_model.layers:\n    layer.trainable = False\n\nmodel = tf.keras.Sequential([\n    base_model,\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(256, activation='relu'),\n    tf.keras.layers.Dropout(0.5),\n    tf.keras.layers.Dense(1, activation='sigmoid')\n])","metadata":{"id":"MA01KS3-HbRj","execution":{"iopub.status.busy":"2024-06-25T06:07:52.198376Z","iopub.execute_input":"2024-06-25T06:07:52.198727Z","iopub.status.idle":"2024-06-25T06:07:52.212174Z","shell.execute_reply.started":"2024-06-25T06:07:52.198700Z","shell.execute_reply":"2024-06-25T06:07:52.211215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])","metadata":{"id":"e41MuY5ALF6K","execution":{"iopub.status.busy":"2024-06-25T06:07:52.963162Z","iopub.execute_input":"2024-06-25T06:07:52.963944Z","iopub.status.idle":"2024-06-25T06:07:52.972149Z","shell.execute_reply.started":"2024-06-25T06:07:52.963912Z","shell.execute_reply":"2024-06-25T06:07:52.971387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('cats_vs_dogs_vgg16.h5')","metadata":{"id":"xBBviHDx5icb","outputId":"25883a50-a32c-436a-a0a0-a3705156a49c","execution":{"iopub.status.busy":"2024-06-25T06:07:53.501768Z","iopub.execute_input":"2024-06-25T06:07:53.502131Z","iopub.status.idle":"2024-06-25T06:07:53.657411Z","shell.execute_reply.started":"2024-06-25T06:07:53.502104Z","shell.execute_reply":"2024-06-25T06:07:53.656418Z"},"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":"35O1ht5hLHK0","outputId":"85457fc5-65b9-49f1-e075-7e4765fcf844","execution":{"iopub.status.busy":"2024-06-25T06:07:56.604376Z","iopub.execute_input":"2024-06-25T06:07:56.604706Z","iopub.status.idle":"2024-06-25T06:08:12.148684Z","shell.execute_reply.started":"2024-06-25T06:07:56.604682Z","shell.execute_reply":"2024-06-25T06:08:12.147708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_pred = model.predict(\n    predict_generator(test_files), steps=len(test_files))","metadata":{"id":"04hp5VgQLMsm","execution":{"iopub.status.busy":"2024-06-25T06:08:53.033509Z","iopub.execute_input":"2024-06-25T06:08:53.034001Z","iopub.status.idle":"2024-06-25T06:09:54.332349Z","shell.execute_reply.started":"2024-06-25T06:08:53.033970Z","shell.execute_reply":"2024-06-25T06:09:54.331449Z"},"trusted":true},"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":"IwWJMVN2LRU4","execution":{"iopub.status.busy":"2024-06-25T06:09:59.938820Z","iopub.execute_input":"2024-06-25T06:09:59.939220Z","iopub.status.idle":"2024-06-25T06:10:02.138674Z","shell.execute_reply.started":"2024-06-25T06:09:59.939191Z","shell.execute_reply":"2024-06-25T06:10:02.137792Z"},"trusted":true},"execution_count":null,"outputs":[]}]}