{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport matplotlib.pyplot as plt\nimport cv2\n\nimport tensorflow as tf\nfrom tensorflow import keras","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_batch_size = 8\nimage_size = 512","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"PATH =  '../input/cassava-leaf-disease-classification/'\nsub_df = pd.read_csv(PATH + 'sample_submission.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub_df[\"path\"] = PATH + 'test_images/' + sub_df[\"image_id\"]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_ds = tf.data.Dataset.from_tensor_slices((sub_df.path.values, sub_df.label.values))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"AUTOTUNE = tf.data.experimental.AUTOTUNE","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from albumentations import (Compose, RandomCrop,Transpose, HorizontalFlip, \n                            VerticalFlip, ShiftScaleRotate, HueSaturationValue,   \n                            RandomBrightness,RandomContrast, CenterCrop, ToFloat)\n\ntest_transforms = Compose([\n            CenterCrop(image_size, image_size),\n            HorizontalFlip(p=0.5),\n            VerticalFlip(p=0.5),\n            ShiftScaleRotate(p=0.5),\n            ToFloat(max_value=255)\n        ])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def test_aug(image):\n    data = {\"image\":image}\n    aug_data = test_transforms(**data)\n    aug_img = aug_data[\"image\"]\n    aug_img = tf.cast(aug_img, tf.float32)\n    #aug_img = tf.image.resize(aug_img, size=[512, 512])    \n    return aug_img","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def process_data(image_path, label, func_aug):\n    image = tf.io.read_file(image_path)\n    image = tf.image.decode_jpeg(image)\n    image = tf.image.convert_image_dtype(image, tf.float32)\n    aug_img = tf.numpy_function(func=func_aug, inp=[image], Tout=tf.float32)\n    return aug_img, label","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from functools import partial\ntest_ds_alb = test_ds.map(partial(process_data, func_aug=test_aug), num_parallel_calls=AUTOTUNE).prefetch(AUTOTUNE)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def set_shapes(img, label, img_shape=(512, 512,3)):\n    img.set_shape(img_shape)\n    label.set_shape([])\n    return img, label","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_ds_alb = test_ds_alb.map(set_shapes, num_parallel_calls=AUTOTUNE).batch(8)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import os\nos.system('pip install /kaggle/input/kerasapplications -q')\nos.system('pip install /kaggle/input/efficientnet-keras-source-code/ -q --no-deps')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import efficientnet.tfkeras as efn \nfrom keras.models import Model\nfrom keras.layers import Input, Dense, Dropout, GlobalAveragePooling2D\n\n\nefficientnet = efn.EfficientNetB4(weights=None, include_top=False,\n                                  drop_connect_rate=0.3, input_shape=(image_size, image_size, 3))\n\ninputs = Input(shape=(image_size, image_size, 3))\nefficientnet = efficientnet(inputs)\npooling = GlobalAveragePooling2D()(efficientnet)\ndropout = Dropout(0.3)(pooling)\noutputs = Dense(5, activation=\"softmax\")(dropout)\nmodel = Model(inputs=inputs, outputs=outputs)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"preds = []\nfor fold in [2,3]:\n    model.load_weights('../input/effnetb45128weights/EffNetB4_512_8_weights_fold_{}.h5'.format(fold+1))\n    preds.append(model.predict(test_ds_alb, workers=4, verbose=1))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"preds = []\n\nfor fold in [2,3]:\n    model.load_weights('../input/effnetb45128weights/EffNetB4_512_8_weights_fold_{}.h5'.format(fold+1))\n    fold_preds = []\n    for i in range(5):\n        fold_preds.append(model.predict(test_ds_alb, workers=4, verbose=1))\n    preds.append(np.mean(fold_preds, axis=0))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"preds_avg = np.mean(preds, axis=0) ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_pred = np.argmax(preds_avg, axis=-1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub_df['label'] = y_pred","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub_df.drop(['path'], axis=1, inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub_df.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}