{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Setup"},{"metadata":{"trusted":true},"cell_type":"code","source":"# !/opt/conda/bin/python3.7 -m pip install --upgrade pi\n# !pip install --upgrade tensorflow - q\n!pip install keras_efficientnets -q\n!pip install keras_applications -q","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import json\nimport math, re, os\n\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport albumentations as A\n\nfrom functools import partial\nfrom kaggle_datasets import KaggleDatasets\n\nimport tensorflow as tf\nfrom tensorflow.keras.applications import ResNet50\nfrom tensorflow.keras.layers import Dense, BatchNormalization, GlobalAveragePooling2D, Flatten, Input, Activation, Conv2D, Add, Dropout\nfrom tensorflow.keras.layers.experimental import preprocessing\nfrom tensorflow.keras.initializers import TruncatedNormal\nfrom tensorflow.keras.regularizers import l1, l2, l1_l2\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.callbacks import EarlyStopping, LearningRateScheduler, ReduceLROnPlateau\n\nfrom keras_efficientnets import EfficientNetB0\nfrom sklearn.model_selection import KFold\n\nBASE_DIR = \"/kaggle/input/cassava-leaf-disease-classification/\"\nTRAIN_DIR = \"/kaggle/input/cassava-leaf-disease-classification/train_images/\"\nTEST_DIR = \"/kaggle/input/cassava-leaf-disease-classification/test_images/\"\n\nsub = pd.read_csv(f'{BASE_DIR}sample_submission.csv')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Setup\nSetting up the TPU/GPU configuration"},{"metadata":{"trusted":true},"cell_type":"code","source":"try:\n    tpu = tf.distribute.cluster_resolver.TPUClusterResolver()\n    print('Device:', tpu.master())\n    tf.config.experimental_connect_to_cluster(tpu)\n    tf.tpu.experimental.initialize_tpu_system(tpu)\n    strategy = tf.distribute.experimental.TPUStrategy(tpu)\nexcept:\n    strategy = tf.distribute.get_strategy()\nprint('Number of replicas:', strategy.num_replicas_in_sync)\n\nAUTOTUNE = tf.data.experimental.AUTOTUNE\nGCS_PATH = KaggleDatasets().get_gcs_path('cassava-leaf-disease-classification')\nGCS_PATH_AUG = KaggleDatasets().get_gcs_path('cassavaaug')\nBATCH_SIZE = 16 * strategy.num_replicas_in_sync\nIMAGE_SIZE = [512, 512]\nCLASSES = ['0', '1', '2', '3', '4']\nEPOCHS = 25\n\nTRAINING_FILENAMES = np.array(tf.io.gfile.glob(GCS_PATH + '/train_tfrecords/ld_train*.tfrec'))\nTEST_FILENAMES = np.array(tf.io.gfile.glob(GCS_PATH + '/test_tfrecords/ld_test*.tfrec'))\nAUG_FILENAME = np.array(tf.io.gfile.glob(GCS_PATH_AUG + '/cassava_aug_*.tfrec'))\ndef count_data_items(filenames):\n    n = [int(re.compile(r\"-([0-9]*)\\.\").search(filename).group(1)) for filename in filenames]\n    return np.sum(n)\nNUM_TRAINING_IMAGES = count_data_items(TRAINING_FILENAMES)\nNUM_TEST_IMAGES = count_data_items(TEST_FILENAMES)\n\n# TRAINING_FILENAMES = np.concatenate([AUG_FILENAME, TRAINING_FILENAMES])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# EDA"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"with open(os.path.join(BASE_DIR, \"label_num_to_disease_map.json\")) as file:\n    map_classes = json.loads(file.read())\n    map_classes = {int(k) : v for k, v in map_classes.items()}\n    \nprint(json.dumps(map_classes, indent=4))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.read_csv(os.path.join(BASE_DIR, \"train.csv\"))\ntrain[\"class_name\"] = train[\"label\"].map(map_classes)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(8, 4))\nsns.countplot(y=\"class_name\", data=train);","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Loading data\n-  Load the same number of data for each of the classes"},{"metadata":{"trusted":true},"cell_type":"code","source":"def decode_image(image):\n    image = tf.image.decode_jpeg(image, channels=3)\n    image = tf.cast(image, tf.float32) / 255.0\n    image = tf.reshape(image, [*IMAGE_SIZE, 3])\n    return image\n\ndef read_tfrecord(example, labeled):\n    tfrecord_format = {\n        \"image\": tf.io.FixedLenFeature([], tf.string),\n        \"target\": tf.io.FixedLenFeature([], tf.int64)\n    } if labeled else {\n        \"image\": tf.io.FixedLenFeature([], tf.string),\n        \"image_name\": tf.io.FixedLenFeature([], tf.string)\n    }\n    example = tf.io.parse_single_example(example, tfrecord_format)\n    image = decode_image(example['image'])\n    if labeled:\n        label = tf.cast(example['target'], tf.int32)\n        return image, label\n    idnum = example['image_name']\n    return image, idnum\n\n\ndef load_dataset(filenames, labeled=True, ordered=False):\n    ignore_order = tf.data.Options()\n    if not ordered:\n        ignore_order.experimental_deterministic = False # disable order, increase speed\n    dataset = tf.data.TFRecordDataset(filenames, num_parallel_reads=AUTOTUNE) # automatically interleaves reads from multiple files\n    dataset = dataset.with_options(ignore_order) # uses data as soon as it streams in, rather than in its original order\n    dataset = dataset.map(partial(read_tfrecord, labeled=labeled), num_parallel_calls=AUTOTUNE)\n    return dataset\n\ndef data_augment(image, label):\n    # Thanks to the dataset.prefetch(AUTO) statement in the following function this happens essentially for free on TPU. \n#     Data pipeline code is executed on the \"CPU\" part of the TPU while the TPU itself is computing gradients.\n#     image = transform1(image=image)\n    image = tf.image.random_flip_left_right(image)\n    image = tf.image.random_brightness(image, 0.2)\n    image = tf.image.adjust_contrast(image, 1.5)\n    return image, label\n\ndef get_training_dataset(file_names, augmenatate=False):\n    dataset = load_dataset(TRAINING_FILENAMES, labeled=True)  \n    if augmenatate:\n        dataset = dataset.map(data_augment, num_parallel_calls=AUTOTUNE)  \n        dataset = dataset.repeat()\n#     dataset = dataset.shuffle(2048)\n    dataset = dataset.batch(BATCH_SIZE)\n    dataset = dataset.prefetch(AUTOTUNE)\n    return dataset\n\ndef get_test_dataset(ordered=False):\n    dataset = load_dataset(TEST_FILENAMES, labeled=False, ordered=ordered)\n    dataset = dataset.batch(BATCH_SIZE)\n    dataset = dataset.prefetch(AUTOTUNE)\n    return dataset","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"count_data_items(TRAINING_FILENAMES[0:1])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import collections","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"file_name = {}\nfor i in range(len(TRAINING_FILENAMES)):\n    name = {\"0\": 0, \"1\": 0, \"2\": 0, \"3\": 0, \"4\": 0}\n    num = count_data_items(TRAINING_FILENAMES[i:i+1]) / BATCH_SIZE\n    for _, label in get_training_dataset(TRAINING_FILENAMES[i:i+1]).take(num):\n        reps = collections.Counter(label.numpy())\n#         print(reps.most_common())\n        for key, rep in collections.Counter(label.numpy()).most_common():\n            name[str(key)] += rep\n            \n    file_name[TRAINING_FILENAMES[i]] = name","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"file_name","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Modeling"},{"metadata":{"trusted":true},"cell_type":"code","source":"def efn():\n    inputs = Input(shape=(*IMAGE_SIZE, 3))\n    model = EfficientNetB0(include_top=False, input_tensor=inputs, weights=\"imagenet\")\n\n    # Freeze the pretrained weights\n    model.trainable = False\n    \n    for layer in model.layers[-12:]:\n        if not isinstance(layer, BatchNormalization):\n            layer.trainable = True\n\n    # Rebuild top\n    x = GlobalAveragePooling2D(name=\"avg_pool\")(model.output)\n    x = BatchNormalization()(x)\n\n    top_dropout_rate = 0.2\n    x = Dropout(top_dropout_rate, name=\"top_dropout\")(x)\n    outputs = Dense(5, activation=\"softmax\", name=\"prediction\")(x)\n\n    # Compile\n    \n    model = tf.keras.Model(inputs, outputs, name=\"EfficientNet\")\n    optimizer = Adam(learning_rate=1.5e-3)\n    \n    \n    model_metrics = [tf.keras.metrics.SparseCategoricalAccuracy(name=\"spa\")]\n    model_loss = tf.keras.losses.SparseCategoricalCrossentropy(name='scc')\n    model.compile(\n        optimizer=optimizer, loss=model_loss, metrics=model_metrics\n    )\n    return model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"with strategy.scope(): \n    \n    model = efn()\n    \n#     model = tf.keras.models.load_model('/kaggle/input/nncassava/M01')\n#     tf.keras.backend.set_value(model.optimizer.learning_rate, 0.002)   \n    \n    \n\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tf.keras.utils.plot_model(model, show_shapes=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# hist = model.fit(get_training_dataset(AUG_FILENAME, False),\n#                 epochs=100,\n#                  verbose=1,\n                 \n#         )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def scheduler(epoch, lr): return max(1e-25, lr * 0.96 ** (epoch // 20))\n\ndef plot(hist):\n    fig, axes = plt.subplots(len(hist), 2, figsize=(10,10))\n    for ax_index, history in enumerate(hist.values()):\n        history_frame = pd.DataFrame(history.history)\n        history_frame.loc[:, ['loss', 'val_loss']].plot(ax=axes[ax_index][0], title=\"Losses\")\n        history_frame.loc[:, ['spa', 'val_spa']].plot(ax=axes[ax_index][1], title=\"Accuracies\")\n\ndef run_kfold(model, seed=2983, epochs=50, n_fold=10):\n    BATCH_SIZE = 16 * strategy.num_replicas_in_sync\n    EPOCHS = epochs\n    N_FOLDS = n_fold\n    kf = KFold(n_splits=N_FOLDS, random_state=seed, shuffle=True)\n    hist = {}\n\n    for fold_index, (train_indices, val_indices) in enumerate(kf.split(TRAINING_FILENAMES)):\n        print(f'Fold {fold_index + 1} -----------------------------------------------------------')\n\n        train, val = get_training_dataset(TRAINING_FILENAMES[train_indices], augmenatate=True),get_training_dataset(TRAINING_FILENAMES[val_indices])\n\n        STEPS_PER_EPOCH = count_data_items(TRAINING_FILENAMES[train_indices]) // BATCH_SIZE\n        VALID_STEPS = count_data_items(TRAINING_FILENAMES[val_indices]) // BATCH_SIZE\n\n        hist[fold_index] = model.fit(train,\n                         steps_per_epoch=STEPS_PER_EPOCH, \n                         epochs=EPOCHS, \n                         verbose=1,\n                         validation_data=val,\n                         validation_steps=VALID_STEPS,\n                         callbacks = [\n#                              LearningRateScheduler(scheduler),\n                             EarlyStopping(monitor='val_loss', \n                                           patience=30, \n                                           restore_best_weights=True, \n                                           min_delta=0, \n                                           verbose=True),\n                             ReduceLROnPlateau(monitor='val_spa', \n                                               factor=0.9, \n                                               patience=10, \n                                               verbose=0,\n                                               mode='max', \n                                               min_delta=0.001, \n                                               cooldown=0, \n                                               min_lr=0)\n                         ]\n        )\n        \n    return hist","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"hist = run_kfold(model, epochs=150, n_fold=6)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Plotting and Sanity Check"},{"metadata":{"trusted":true},"cell_type":"code","source":"plot(hist)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"imgs = np.empty((5,600,800,3))\nc0_name=train[train['label'] == 0].iloc[1000]['image_id']\nimgs[0] = plt.imread(f'{TRAIN_DIR}{c0_name}') / 255\n\nc1_name=train[train['label'] == 1].iloc[100]['image_id']\nimgs[1] = plt.imread(f'{TRAIN_DIR}{c1_name}') / 255\n\nc1_name=train[train['label'] == 2].iloc[230]['image_id']\nimgs[2] = plt.imread(f'{TRAIN_DIR}{c1_name}') / 255\n\nc1_name=train[train['label'] == 3].iloc[340]['image_id']\nimgs[3] = plt.imread(f'{TRAIN_DIR}{c1_name}') / 255\n\nc1_name=train[train['label'] == 4].iloc[230]['image_id']\nimgs[4] = plt.imread(f'{TRAIN_DIR}{c1_name}') / 255","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.imshow(imgs[3])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.predict(imgs)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.save(f'/kaggle/working/efc01', save_format='h5')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"testing_dataset = get_test_dataset()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def to_float32(image, label):\n    return tf.cast(image, tf.float32), label\ntest_ds = get_test_dataset(ordered=True) \ntest_ds = test_ds.map(to_float32)\n\nprint('Computing predictions...')\ntest_images_ds = testing_dataset\ntest_images_ds = test_ds.map(lambda image, idnum: image)\nprobabilities = model.predict(test_images_ds)\npredictions = np.argmax(probabilities, axis=-1)\nprint(predictions)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"NUM_TEST_IMAGES = len(TEST_FILENAMES)\nprint('Generating submission.csv file...')\ntest_ids_ds = test_ds.map(lambda image, idnum: idnum).unbatch()\ntest_ids = next(iter(test_ids_ds.batch(NUM_TEST_IMAGES))).numpy().astype('U') # all in one batch\nnp.savetxt('submission.csv', np.rec.fromarrays([test_ids, predictions]), fmt=['%s', '%d'], delimiter=',', header='id,label', comments='')\n!head submission.csv","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Refrences\n- Data Loading and prepration: https://www.kaggle.com/jessemostipak/getting-started-tpus-cassava-leaf-disease/data#Load-the-data\n- Data Augmentation and visualization: https://www.kaggle.com/ihelon/cassava-leaf-disease-exploratory-data-analysis"}],"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}