{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"from numba import cuda\nimport tensorflow as tf\nimport tensorflow.keras as keras\nimport numpy as np\nimport pandas as pd\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, Flatten, Dense, MaxPool2D, AveragePooling2D, GlobalAveragePooling2D\nfrom tensorflow.keras.applications import Xception, ResNet101\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras import Input\nimport os\nfrom tensorflow.keras.callbacks import ModelCheckpoint\nfrom tensorflow.keras.losses import CategoricalCrossentropy\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nimport random\nfrom keras.layers import Activation\n# from bi_tempered_loss import bi_tempered_logistic_loss\nfrom tensorflow.keras.utils import to_categorical\nfrom sklearn.preprocessing import LabelBinarizer\nfrom PIL import Image\nimport glob\nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"T_1 = 0.2\nT_2 = 1.0\nSMOOTH_FRACTION = 0.3\nTARGET_SIZE = 512\nN_CLASSES = 5\nN_ITER = 5\nVALIDATION_SIZE = 0.2\nbatch_size = 128","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_model = Xception(include_top=False,\n    weights=None,\n    input_tensor=Input(shape=(TARGET_SIZE,TARGET_SIZE,3)),\n    input_shape=(TARGET_SIZE,TARGET_SIZE,3))\navg_layer = GlobalAveragePooling2D()(x_model.output)\nfinal_layer = Dense(5, activation='softmax')(avg_layer)\nfinal_model = Model(inputs=x_model.input, outputs=final_layer)\n\nres_model = ResNet101(include_top=False,\n    weights=None,\n    input_tensor=Input(shape=(TARGET_SIZE,TARGET_SIZE,3)),\n    input_shape=(TARGET_SIZE,TARGET_SIZE,3))\nres_avg_layer = GlobalAveragePooling2D()(res_model.output)\nres_final_layer = Dense(5, activation='softmax')(res_avg_layer)\nres_final_model = Model(inputs=res_model.input, outputs=res_final_layer)\n# res_final_model.load_weights('../input/labelsmoothing/weights_resnet101_pre_GlobalAvgLast_FreezeBN_LabelSmoothing_20201228.hdf5')\n# weight = '../input/fivefolds-triplexception/FiveFolds_Xception_LabelSmoothing_BiTempedLogLoss_fold_1.h5'\n# final_model.load_weights(weight)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from albumentations import (\n    HorizontalFlip, VerticalFlip, IAAPerspective, ShiftScaleRotate, CLAHE, RandomRotate90,\n    Transpose, ShiftScaleRotate, Blur, OpticalDistortion, GridDistortion, HueSaturationValue,\n    IAAAdditiveGaussianNoise, GaussNoise, MotionBlur, MedianBlur, IAAPiecewiseAffine, RandomResizedCrop,\n    IAASharpen, IAAEmboss, RandomBrightnessContrast, Flip, OneOf, Compose, Normalize, Cutout, CoarseDropout, ShiftScaleRotate, CenterCrop, Resize\n)\nimport albumentations as A\nfrom albumentations.pytorch import ToTensorV2\nfrom tensorflow.keras.utils import Sequence\n\nclass AugmentDataGenerator(Sequence):\n    def __init__(self, datagen, augment=None):\n        self.datagen = datagen\n        if augment is None:\n            self.augment = A.Compose([])\n        else:\n            self.augment = augment\n\n    def __len__(self):\n        return len(self.datagen)\n\n    def __getitem__(self, x):\n        images, *rest = self.datagen[x]\n        augmented = []\n        for image in images:\n            image = self.augment(image=image)['image']\n            augmented.append(image)\n        return (np.array(augmented), *rest)\n    \naug_transform = A.Compose([RandomResizedCrop(TARGET_SIZE, TARGET_SIZE),\n        Transpose(p=0.5),\n        IAASharpen(p=0.5),\n        Blur(p=0.5),\n        HorizontalFlip(p=0.5),\n        VerticalFlip(p=0.5),\n        ShiftScaleRotate(rotate_limit = 180 ,p=0.5),\n        HueSaturationValue(hue_shift_limit=0.2, sat_shift_limit=0.2, val_shift_limit=0.2, p=0.5),\n        RandomBrightnessContrast(brightness_limit=(-0.1,0.1), contrast_limit=(-0.1, 0.1), p=0.5),\n        Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], max_pixel_value=255.0, p=1.0),\n        CoarseDropout(p=0.5),\n        Cutout(p=0.5)\n#         ,ToTensorV2(p=1.0)\n        ], p = 1.0)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"test_images = glob.glob('../input/cassava-leaf-disease-classification/test_images/*.jpg')\n\ndf_test = pd.DataFrame(test_images, columns = ['path'])\n\n\nORIGIN_WIDTH = 600\nORIGIN_HEIGHT = 800\ndef make_test_gen( batch_size=64):\n    my_test_idg = ImageDataGenerator(\n#         rescale = 1/255.0,\n#                                  preprocessing_function = None,\n#                                  rotation_range = 30,\n#                                  horizontal_flip = True,\n#                                  vertical_flip = True,\n#                                  fill_mode = 'nearest',\n#                                  shear_range = 0.1,\n#                                  height_shift_range = 0.1,\n#                                  width_shift_range = 0.1\n                                    )\n        \n    test_gen= my_test_idg.flow_from_dataframe(dataframe=df_test,\n                                                x_col=\"path\",\n                                                y_col=None,\n                                                batch_size=batch_size,\n                                                seed=42,\n                                                shuffle=False,\n                                                class_mode=None,\n                                                target_size=(ORIGIN_WIDTH, ORIGIN_HEIGHT)) ## (height, width)\n    test_gen_aug = AugmentDataGenerator(test_gen, aug_transform)\n    return test_gen","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_gen = make_test_gen(batch_size = 128)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"weights_dic = {\n#     'res1': '../input/labelsmoothing/weights_resnet101_pre_GlobalAvgLast_FreezeBN_LabelSmoothing_20201228.hdf5',\n#     'xcep1': '../input/fivefolds-triplexception/FiveFolds_Xception_LabelSmoothing_BiTempedLogLoss_fold_1.h5',\n#     'xcep2': '../input/fivefolds-triplexception/FiveFolds_Xception_LabelSmoothing_BiTempedLogLoss_fold_2.h5',\n#     'xcep3': '../input/fivefolds-triplexception/FiveFolds_Xception_LabelSmoothing_BiTempedLogLoss_fold_3.h5',\n#     'xcep4': '../input/fivefolds-triplexception/4_xception_base_LabelSmoothing_BiTempedLogLoss_Full_fold_24-0.90-0.09.hdf5',\n#     'xcep5': '../input/fivefolds-triplexception/5_xception_base_LabelSmoothing_BiTempedLogLoss_Full_fold_25-0.88-0.09.hdf5'\n    'xcep_aug_1': '../input/xception-newaug/1_xception_base_Aug_LabelSmoothing_BiTempedLogLoss_Full_fold_17-0.87-0.09.hdf5',\n    'xcep_aug_2': '../input/xception-newaug/2_xception_base_Aug_LabelSmoothing_BiTempedLogLoss_Full_fold_18-0.87-0.09.hdf5'\n}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pred = []\nfor f_key, f_value in weights_dic.items():\n    p_model = final_model\n#     p_model = res_final_model\n    if 'xcep' in f_key:\n        print('Load model')\n        p_model = final_model\n    p_model.load_weights(f_value)\n    preds = []\n    for i in range(4):\n        #test_gen.reset()\n        local_pred = p_model.predict(test_gen,  verbose = True)\n        print(local_pred)\n        preds.append(local_pred)\n    pred.append(np.mean(preds, axis=0))\n    tf.keras.backend.clear_session()\n# pred","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pred","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# weights = ['../input/fivefolds-triplexception/FiveFolds_Xception_LabelSmoothing_BiTempedLogLoss_fold_1.h5',\n#           '../input/fivefolds-triplexception/FiveFolds_Xception_LabelSmoothing_BiTempedLogLoss_fold_2.h5',\n#           '../input/fivefolds-triplexception/FiveFolds_Xception_LabelSmoothing_BiTempedLogLoss_fold_3.h5']\n# pred = []\n# for item in weights:\n#     final_model.load_weights(item)\n#     preds = []\n#     for i in range(4):\n#         #test_gen.reset()\n#         local_pred = final_model.predict(test_gen,  verbose = True)\n#         preds.append(local_pred)\n#     pred.append(np.mean(preds, axis=0))\n#     tf.keras.backend.clear_session()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# pred_test = np.mean(preds, axis=0)\nresult = np.mean(pred, axis=0)\nresult","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pred_test_labels = np.argmax(result, axis = -1)\npred_test_labels","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"final_submission = df_test\nfinal_submission['image_id'] = final_submission.path.str.split('/').str[-1]\nfinal_submission['label'] = pred_test_labels\n\nfinal_csv = final_submission[['image_id', 'label']]\nfinal_csv.to_csv('submission.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# weights_path_1 = '../input/fivefolds-triplexception/FiveFolds_Xception_LabelSmoothing_BiTempedLogLoss_fold_1.h5'\n# weights_path_2 = '../input/fivefolds-triplexception/FiveFolds_Xception_LabelSmoothing_BiTempedLogLoss_fold_2.h5'\n# weights_path_3 = '../input/fivefolds-triplexception/FiveFolds_Xception_LabelSmoothing_BiTempedLogLoss_fold_3.h5'\n\n\n# WORK_DIR = '../input/cassava-leaf-disease-classification'\n# ss = pd.read_csv(os.path.join(WORK_DIR, \"sample_submission.csv\"))\n# ss\n# preds = []\n# for image_id in ss.image_id:\n#     image = Image.open(os.path.join(WORK_DIR,  \"test_images\", image_id))\n#     image = image.resize((TARGET_SIZE, TARGET_SIZE))\n#     image = np.expand_dims(image, axis = 0)\n#     image = image/255.0\n#     image = np.float32(image)\n#     final_model.load_weights(weights_path_1)\n#     pred_1 = final_model.predict(image)\n\n#     final_model.load_weights(weights_path_2)\n#     pred_2 = final_model.predict(image)\n\n#     final_model.load_weights(weights_path_3)\n#     pred_3 = final_model.predict(image)\n#     result = (pred_1 + pred_2 + pred_3)/3\n#     print(result)\n#     preds.append(np.argmax(result))\n    \n# ss['label'] = preds\n# ss\n# ss.to_csv('submission.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}