{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"# Пути к файлам\nlabels_csv='../input/dog-breed-identification/labels.csv'\nsample_submission_csv='../input/dog-breed-identification/sample_submission.csv'\n\n# Пути до картинок\njpg_train='../input/dog-breed-identification/train/{}.jpg'  \njpg_test='../input/dog-breed-identification/test/{}.jpg'\n\n# Настройки нейросети\nimg_size = 224 # Размер изображения\nnum_class = 120 # Кол-во классов\nbatch_size = 32\nEpochs = 64\n\n# Настройки нейросети\n#img_size = 224 # Размер изображения","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Загружаем библиотеки"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\n%matplotlib inline\nimport matplotlib.pyplot as plt\n\nimport tensorflow.keras as keras\nfrom keras import regularizers\nfrom keras.models import Model\nfrom keras.models import Sequential\nfrom keras.layers.convolutional import Conv2D, MaxPooling2D, ZeroPadding2D\nfrom keras.layers import Dense, Dropout, Flatten\nfrom tensorflow.keras.metrics import categorical_accuracy, categorical_crossentropy\nimport os\nimport cv2, random, time, shutil, csv\nfrom sklearn import preprocessing\nfrom sklearn.model_selection import train_test_split\nfrom keras.preprocessing.image import load_img\nfrom keras.preprocessing.image import img_to_array\nfrom keras.preprocessing.image import ImageDataGenerator\nimport time\nfrom keras.utils import to_categorical\nimport gc\nimport tqdm","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def gen_graph(history, title):\n    plt.plot(history.history['loss'])\n    plt.plot(history.history['val_loss'])\n    plt.title('crossentropy ' + title)\n    plt.ylabel('crossentropy')\n    plt.xlabel('Epoch')\n    plt.legend(['train', 'validation'], loc='upper left')\n    plt.show()\n    \n    plt.plot(history.history['categorical_accuracy'])\n    plt.plot(history.history['val_categorical_accuracy'])\n    plt.title('categorical_accuracy ' + title)\n    plt.ylabel('categorical_accuracy')\n    plt.xlabel('Epoch')\n    plt.legend(['train', 'validation'], loc='upper left')\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Считываем изображения и переводим в массив"},{"metadata":{"trusted":true},"cell_type":"code","source":"# set image size here\ndata_dir = '../input/dog-breed-identification'\ndata_df = pd.read_csv(os.path.join(data_dir, 'labels.csv'))\nclass_names = sorted(data_df['breed'].unique())\nprint(f\"No. of classes read - {len(class_names)}\")\ntime.sleep(1)\n\nimages_list = sorted(os.listdir(os.path.join(data_dir, 'train')))\nX = []\nY = []\ni = 0\nfor image in tqdm.tqdm(images_list):\n    cls_name = data_df[data_df['id'] == image[:-4]].iloc[0,1]\n    cls_index = int(class_names.index(cls_name)) \n\n    # Reading RGB Images\n    image_path = os.path.join(data_dir, 'train',image)\n    orig_image = cv2.cvtColor(cv2.imread(image_path), cv2.COLOR_BGR2RGB)\n    res_image = cv2.resize(orig_image,(img_size, img_size))\n    X.append(res_image)\n    Y.append(cls_index)\n    i+=1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Converting to arrays\nprint(len(X), len(Y))\nXarr = np.array(X)\nYarr = np.array(Y).reshape(-1,1)\n\n# converting labels to one hot\nYarr_hot = to_categorical(Y)\n\ndel(X)\nprint(Xarr.shape, Yarr.shape, Yarr_hot.shape)\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Формируем датасет для обучения"},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train, X_valid, Y_train, Y_valid = train_test_split(Xarr, Yarr_hot, shuffle=True,  test_size=0.2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"del Xarr, Yarr_hot, Yarr","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(X_train.shape, Y_train.shape)\nprint(X_valid.shape,Y_valid.shape)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Создаем генератор изображений"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_datagen = ImageDataGenerator(rescale=1./255,\n                            rotation_range=35, #поворот\n                            horizontal_flip=True,\n                            vertical_flip=True,\n                            shear_range=15\n                                )\n\ntest_datagen = ImageDataGenerator(rescale=1./255)\n\ntrain_datagen.fit(X_train)\ntest_datagen.fit(X_valid)\n\n# Создаем генераторы \ntrain_generator =train_datagen.flow(X_train, Y_train, \n                               batch_size=batch_size)\n\ntest_generator =test_datagen.flow(X_valid, Y_valid,\n                              batch_size=batch_size)\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x,y = next(train_generator)\n\nprint(type(x))\nprint(train_generator.n)\nprint(x.shape)\nprint(y.shape)\ndel x,y","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Создаем модель нейросети"},{"metadata":{"trusted":true,"_kg_hide-output":false},"cell_type":"code","source":"\n# Создаем последовательную модель\nmodel = Sequential()\n\n# сверточный слой\nmodel.add(Conv2D(64, (3, 3), padding='same',input_shape=(img_size, img_size, 3), activation='relu'))\n# сверточный слой\nmodel.add(Conv2D(64, (3, 3), activation='relu', padding='same'))\n# первый слой подвыборки\nmodel.add(MaxPooling2D(pool_size=(2, 2), strides=(2,2),padding='same'))\n# Слой регуляризации Dropout\nmodel.add(Dropout(0.20))\n\n\n# сверточный слой\nmodel.add(Conv2D(128, (3, 3), padding='same', activation='relu'))\n# сверточный слой\nmodel.add(Conv2D(128, (3, 3), activation='relu', padding='same'))\n# Второй слой подвыборки\nmodel.add(MaxPooling2D(pool_size=(2, 2), strides=(2,2),padding='same'))\n# Слой регуляризации Dropout\nmodel.add(Dropout(0.20))\n\n\n# сверточный слой\nmodel.add(Conv2D(256, (3, 3), padding='same', activation='relu'))\n# сверточный слой\nmodel.add(Conv2D(256, (3, 3), activation='relu', padding='same'))\n# сверточный слой\nmodel.add(Conv2D(256, (3, 3), activation='relu', padding='same'))\n# Третий слой подвыборки\nmodel.add(MaxPooling2D(pool_size=(2, 2), strides=(2,2),padding='same'))\n# Слой регуляризации Dropout\nmodel.add(Dropout(0.20))\n\n# сверточный слой\nmodel.add(Conv2D(512, (3, 3), padding='same', activation='relu'))\n# сверточный слой\nmodel.add(Conv2D(512, (3, 3), activation='relu', padding='same'))\n# сверточный слой\nmodel.add(Conv2D(512, (3, 3), activation='relu', padding='same'))\n# Четвертый слой подвыборки\nmodel.add(MaxPooling2D(pool_size=(2, 2), strides=(2,2),padding='same'))\n# Слой регуляризации Dropout\nmodel.add(Dropout(0.20))\n\n# сверточный слой\nmodel.add(Conv2D(512, (3, 3), padding='same', activation='relu'))\n# сверточный слой\nmodel.add(Conv2D(512, (3, 3), activation='relu', padding='same'))\n# сверточный слой\nmodel.add(Conv2D(512, (3, 3), activation='relu', padding='same'))\n# Четвертый слой подвыборки\nmodel.add(MaxPooling2D(pool_size=(2, 2), strides=(2,2),padding='same'))\n# Слой регуляризации Dropout\nmodel.add(Dropout(0.20))\n\n\n\n\n\n# Слой преобразования данных из 2D представления в плоское\nmodel.add(Flatten())\n# Полносвязный слой для классификации\nmodel.add(Dense(4096, activation='relu'))\nmodel.add(Dropout(0.30))\nmodel.add(Dense(1024, activation='relu'))\n# Слой регуляризации Dropout\nmodel.add(Dropout(0.30))\n\n\n# Выходной полносвязный слой\nmodel.add(Dense(num_class, activation='softmax'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(optimizer='adam',\n          loss='categorical_crossentropy', \n           metrics=['categorical_accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(model.summary())","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Функция для остановки сети и сохранения лучших результатов"},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.callbacks import ModelCheckpoint, EarlyStopping\n\nearlystop=EarlyStopping(monitor='val_loss', min_delta=0, patience=5)\n\ncheckpoint_callback = ModelCheckpoint('model_best.hdf5',\n                                      monitor='val_categorical_accuracy', \n                                      #save_best_only=True\n                                     mode='auto')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Обучаем нейросеть"},{"metadata":{"trusted":true},"cell_type":"code","source":"history = model.fit(\n    train_generator,\n    callbacks=[earlystop, checkpoint_callback],\n    epochs=Epochs,\n    steps_per_epoch=train_generator.n // train_generator.batch_size,\n    validation_data=test_generator,\n    validation_steps=test_generator.n // test_generator.batch_size\n    )","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Построим графики процесса обучения"},{"metadata":{"trusted":true},"cell_type":"code","source":"#График точности на валидационной и обучающей выборке\ngen_graph(history,\n          \"график точности\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Выберем лучшую модель и получим результаты для тестов"},{"metadata":{"trusted":true},"cell_type":"code","source":"del X_train, X_valid, Y_train, Y_valid","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"images_list = sorted(os.listdir(os.path.join(data_dir, 'test')))\nx_test=[]\nfor image in tqdm.tqdm(images_list):    \n    image_path = os.path.join(data_dir, 'test',image)\n    orig_image = cv2.cvtColor(cv2.imread(image_path), cv2.COLOR_BGR2RGB)\n    res_image = cv2.resize(orig_image,(img_size, img_size))\n    x_test.append(res_image)\nx_test=np.array(x_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_test.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from tensorflow.keras.models import load_model\nmodel=load_model(\"model_best.hdf5\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"preds = model.predict(x_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train = pd.read_csv(labels_csv)\ndf_test = pd.read_csv(sample_submission_csv)\nlabels = df_train['breed']\none_hot = pd.get_dummies(labels, sparse = True)\none_hot_labels = np.asarray(one_hot)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub = pd.DataFrame(preds)\ncol_names = one_hot.columns.values\nsub.columns = col_names\nsub.insert(0, 'id', df_test['id'])\nsub.head(5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub.to_csv(\"output_rmsprop_aug.csv\", index=False)","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}