{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"_kg_hide-output":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\n\nimport os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n        \nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\n\nfrom keras.layers import GlobalAveragePooling2D, Flatten, Dense, Dropout, BatchNormalization\nfrom keras.callbacks import EarlyStopping, ModelCheckpoint, LearningRateScheduler\nfrom tensorflow.keras.applications import EfficientNetB3\n# from efficientnet.keras import EfficientNetB3\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras import models, optimizers\n\nfrom PIL import Image \n\nimport tensorflow as tf\nimport tensorflow_addons as tfa\nnp.random.seed(42)\ntf.random.set_seed(42)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dirr = '../input/cassava-leaf-disease-classification/'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.read_csv(dirr + 'train.csv')\ndf","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df['label'].value_counts().sort_index()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df['label'].value_counts().sort_index() / df.shape[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"d = pd.read_json(dirr + 'label_num_to_disease_map.json', typ='series').to_dict()\nd","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df['label'] = df['label'].map(d)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train, X_valid = train_test_split(df, test_size=0.2, random_state=42, stratify=df['label'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train['label'].value_counts().sort_index() / X_train.shape[0], X_valid['label'].value_counts().sort_index() / X_valid.shape[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"datagen = ImageDataGenerator(\n    \n    preprocessing_function = tf.keras.applications.efficientnet.preprocess_input,\n    rotation_range=90,\n    width_shift_range=0.3,\n    height_shift_range=0.3,\n    shear_range=0.3,\n    zoom_range=0.3,\n    horizontal_flip=True,\n    vertical_flip=True,\n    fill_mode='nearest'\n)\n\n\n# image_size = 600\nimage_size = 512\nbatch_size = 12\n\nX_train2 = datagen.flow_from_dataframe(\n    X_train,\n    directory=dirr + 'train_images',\n    seed=42,\n    x_col='image_id',\n    y_col='label',\n    target_size=(image_size, image_size),\n    class_mode='categorical',\n    interpolation='nearest',\n    shuffle=True,\n    batch_size=batch_size\n)\n\nX_valid2 = datagen.flow_from_dataframe(\n    X_valid,\n    directory=dirr + 'train_images',\n    seed=42,\n    x_col='image_id',\n    y_col='label',\n    target_size=(image_size, image_size),\n    class_mode='categorical',\n    interpolation='nearest',\n    shuffle=True,\n    batch_size=batch_size\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"imgs, labels = X_train2.next()\nrows = 3\nimgs = np.array(imgs).astype(np.uint8)\n\nfig = plt.figure(figsize=(15, 10))\ncols = len(imgs) // rows if len(imgs) % 2 == 0 else len(imgs) // rows + 1\nfor i in range(len(imgs)):\n    fig.add_subplot(rows, cols, i + 1)\n    plt.axis('off')\n    plt.title(d[np.argmax(labels[i])], fontsize=12)\n    plt.imshow(imgs[i])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = models.Sequential()\nmodel.add(\n    EfficientNetB3(\n        input_shape=(image_size, image_size, 3), \n        include_top=False,\n        weights='imagenet',\n        drop_connect_rate=0.6)\n)\n\nmodel.add(GlobalAveragePooling2D())\nmodel.add(Flatten())\nmodel.add(Dense(72, activation='relu'))\nmodel.add(Dropout(0.4))\nmodel.add(Dense(len(d), activation='softmax'))\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(optimizer=optimizers.Adam(lr=2e-3),\n              loss='categorical_crossentropy', metrics=['categorical_accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"checkpoint_path = f'bestmodel.hdf5'\ncheckpoint = ModelCheckpoint(checkpoint_path, monitor='val_categorical_accuracy', \n                             verbose=1, save_best_only=True, mode='max')\n\nscheduler = LearningRateScheduler(lambda epoch, lr: lr * 0.5, verbose=1)\n\nearly_stop = EarlyStopping(monitor='val_loss', min_delta=0, patience=1, mode='min', verbose=0)\n\ntqdm_callback = tfa.callbacks.TQDMProgressBar(\n    leave_epoch_progress=False, \n    leave_overall_progress=True, \n    show_epoch_progress=False,\n    show_overall_progress=True\n)\n\ncallbacks_list = [\n    checkpoint, \n    scheduler, \n    tqdm_callback, \n    early_stop\n]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train2.n // batch_size","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history = model.fit_generator(generator=X_train2, validation_data=X_valid2, epochs=6,\n                              steps_per_epoch=X_train2.n // batch_size,\n                              callbacks=callbacks_list, verbose=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def graph_plot(history, typ=False):\n    if typ:\n        for i in history.history.keys():\n            print(f'{i} = [{min(history.history[i])}; {max(history.history[i])}]\\n')\n    \n    epoch = len(history.history['loss'])\n    # на каждую: (train, val) + lr\n    size = len(history.history.keys()) // 2 + 1\n    \n    fig = plt.figure(figsize=(30, 5))\n    i = 1\n    for k in list(history.history.keys()):\n        if 'val' not in k:\n            fig.add_subplot(1, size, i)\n            plt.plot(history.history[k], marker='o', markersize=5)\n            if k != 'lr':\n                plt.plot(history.history['val_' + k], marker='o', markersize=5)\n            plt.title(k, fontsize=10)\n\n            plt.ylabel(k)\n            plt.xlabel('epoch')\n            plt.grid()\n\n            plt.yticks(fontsize=10, rotation=30)\n            plt.xticks(fontsize=10, rotation=30)\n            plt.legend(['train', 'valid'], loc='upper left', fontsize=10, title_fontsize=15)\n            i += 1\n#         plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"graph_plot(history)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cassava_model = models.load_model(checkpoint_path)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cassava_model.evaluate(X_train2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cassava_model.evaluate(X_valid2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dirr2 = '../input/cassava-leaf-disease-classification/test_images/'\ntest = os.listdir(dirr2)\npreds = []\n\nfor link in test:\n    img = Image.open(dirr2 + link).resize((image_size, image_size))\n    img = np.expand_dims(img, axis=0)\n    preds.append(cassava_model.predict(img).argmax(axis=1)[0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_test = pd.DataFrame({'image_id': test, 'label': preds})\ndf_test","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_test.to_csv('submission.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}