{"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":"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\n# import os\n# for 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":"2022-04-12T11:21:13.271664Z","iopub.status.idle":"2022-04-12T11:21:13.272356Z"},"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\n\nprint(tf.__version__)\nprint(tf.executing_eagerly())\n\nimport os\nfrom random import shuffle\nfrom glob import glob\n\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2022-04-12T11:21:13.273785Z","iopub.status.idle":"2022-04-12T11:21:13.274540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip -q ../input/dogs-vs-cats-redux-kernels-edition/test.zip\n!unzip -q ../input/dogs-vs-cats-redux-kernels-edition/train.zip","metadata":{"execution":{"iopub.status.busy":"2022-04-12T11:21:13.275993Z","iopub.status.idle":"2022-04-12T11:21:13.276665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMG_SIZE = (224, 224)  # размер входного изображения сети\n\ntrain_files = glob('./train/*.jpg')\ntest_files = glob('./test/*.jpg')\n# train_files_full = glob('data/train/*.jpg')\n# test_files_full = glob('data/test/*.jpg')\n\nprint(f'Количество тренировочных файлов', len(train_files))\nprint(f'Количество тестовых файлов',len(test_files))\n\n","metadata":{"execution":{"iopub.status.busy":"2022-04-12T11:21:13.278086Z","iopub.status.idle":"2022-04-12T11:21:13.278767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# загружаем входное изображение и предобрабатываем\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":{"execution":{"iopub.status.busy":"2022-04-12T11:21:13.280147Z","iopub.status.idle":"2022-04-12T11:21:13.280833Z"},"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)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T11:21:13.282230Z","iopub.status.idle":"2022-04-12T11:21:13.282971Z"},"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":{"execution":{"iopub.status.busy":"2022-04-12T11:21:13.284357Z","iopub.status.idle":"2022-04-12T11:21:13.285034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-04-12T11:21:13.286444Z","iopub.status.idle":"2022-04-12T11:21:13.287188Z"},"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[-1].output\nx = tf.keras.layers.Dropout(rate=0.2, noise_shape=None, seed=None)(x)\nx = tf.keras.layers.BatchNormalization()(x)\nx = tf.keras.layers.Flatten()(x)\n# x = tf.keras.layers.BatchNormalization()(x)\n# x = tf.keras.layers.Dense(4,  # один выход (бинарная классификация)\n#                           activation='sigmoid',  # функция активации  \n#                           kernel_regularizer=tf.keras.regularizers.l1(1e-4))(x)\n# x = tf.keras.layers.BatchNormalization()(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":{"execution":{"iopub.status.busy":"2022-04-12T11:21:13.289378Z","iopub.status.idle":"2022-04-12T11:21:13.290133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-04-12T11:21:13.291196Z","iopub.status.idle":"2022-04-12T11:21:13.291933Z"},"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":{"execution":{"iopub.status.busy":"2022-04-12T11:21:13.293006Z","iopub.status.idle":"2022-04-12T11:21:13.293764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import EarlyStopping, ModelCheckpoint\n\nearly_stop = EarlyStopping(monitor='val_loss', min_delta=0.001,\n                           patience=10, verbose=1, mode='auto')\nchkpt = ModelCheckpoint('cats-dogs-vgg16.hdf5', \n                        monitor='loss', \n                        verbose=1, \n                        save_best_only=True, \n                        mode='auto')\ncallbacks = [early_stop, chkpt]","metadata":{"execution":{"iopub.status.busy":"2022-04-12T11:21:13.294839Z","iopub.status.idle":"2022-04-12T11:21:13.295540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_samples = 5  # число изображений в валидационной выборке\nepochs=100\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=epochs,  # число эпох обучения\n          validation_data=validation_data,\n          callbacks=callbacks)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T11:21:13.296641Z","iopub.status.idle":"2022-04-12T11:21:13.297362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('cats-dogs-vgg16.hdf5')","metadata":{"execution":{"iopub.status.busy":"2022-04-12T11:21:13.298435Z","iopub.status.idle":"2022-04-12T11:21:13.299185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model.load_weights('cats-dogs-vgg16.hdf5')","metadata":{"execution":{"iopub.status.busy":"2022-04-12T11:21:13.300227Z","iopub.status.idle":"2022-04-12T11:21:13.300951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_pred = model.predict(\n    predict_generator(test_files), steps=len(test_files), verbose=1)","metadata":{"_kg_hide-output":false,"execution":{"iopub.status.busy":"2022-04-12T11:21:13.302002Z","iopub.status.idle":"2022-04-12T11:21:13.302716Z"},"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":{"execution":{"iopub.status.busy":"2022-04-12T11:21:13.303812Z","iopub.status.idle":"2022-04-12T11:21:13.304507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score = model.evaluate(fit_generator(train_files[:1000]), verbose=1, steps=len(train_files[:1000]))\nprint('Train Validation loss:', score[0])\nprint('Train Validation accuracy:', score[1])","metadata":{"execution":{"iopub.status.busy":"2022-04-12T11:04:05.931761Z","iopub.execute_input":"2022-04-12T11:04:05.93208Z","iopub.status.idle":"2022-04-12T11:09:05.133729Z","shell.execute_reply.started":"2022-04-12T11:04:05.932047Z","shell.execute_reply":"2022-04-12T11:09:05.132838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ids = []\nprob = []\nfor path, score in zip(test_files, test_pred):\n#     print(path, score[0])\n#     name = path.split('/')[-1].split('.')[0]\n\n    ids.append((os.path.basename(path)).split('.')[-2])\n    prob.append(score[0])","metadata":{"execution":{"iopub.status.busy":"2022-04-12T11:09:05.135209Z","iopub.execute_input":"2022-04-12T11:09:05.135492Z","iopub.status.idle":"2022-04-12T11:09:05.169909Z","shell.execute_reply.started":"2022-04-12T11:09:05.135465Z","shell.execute_reply":"2022-04-12T11:09:05.169092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = pd.DataFrame({'id': ids, 'label': prob})\noutput.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T11:09:05.171582Z","iopub.execute_input":"2022-04-12T11:09:05.171999Z","iopub.status.idle":"2022-04-12T11:09:05.376833Z","shell.execute_reply.started":"2022-04-12T11:09:05.171956Z","shell.execute_reply":"2022-04-12T11:09:05.375931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output","metadata":{"execution":{"iopub.status.busy":"2022-04-12T11:09:05.378342Z","iopub.execute_input":"2022-04-12T11:09:05.378926Z","iopub.status.idle":"2022-04-12T11:09:05.40057Z","shell.execute_reply.started":"2022-04-12T11:09:05.378887Z","shell.execute_reply":"2022-04-12T11:09:05.399393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}