{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nimport tensorflow as tf\nimport keras\nimport matplotlib.pyplot as plt\nfrom keras import optimizers\nfrom keras import models\nfrom keras import layers\n# from keras.applications import Xception,VGG16\n\n# import tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nimport functools\nfrom tqdm import tqdm","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# coding=utf1-8\n# Copyright 2019 The Google Research Authors.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\n\"\"\"Robust Bi-Tempered Logistic Loss Based on Bregman Divergences.\n\nSource: https://bit.ly/3jSol8T\n\"\"\"\n\nimport functools\nimport tensorflow.compat.v1 as tf1\n\n\ndef for_loop(num_iters, body, initial_args):\n  \"\"\"Runs a simple for-loop with given body and initial_args.\n\n  Args:\n    num_iters: Maximum number of iterations.\n    body: Body of the for-loop.\n    initial_args: Args to the body for the first iteration.\n\n  Returns:\n    Output of the final iteration.\n  \"\"\"\n  for i in range(num_iters):\n    if i == 0:\n      outputs = body(*initial_args)\n    else:\n      outputs = body(*outputs)\n  return outputs\n\n\ndef log_t(u, t):\n  \"\"\"Compute log_t for `u`.\"\"\"\n\n  def _internal_log_t(u, t):\n    return (u**(1.0 - t) - 1.0) / (1.0 - t)\n\n  return tf1.cond(\n      tf1.equal(t, 1.0), lambda: tf1.log(u),\n      functools.partial(_internal_log_t, u, t))\n\n\ndef exp_t(u, t):\n  \"\"\"Compute exp_t for `u`.\"\"\"\n\n  def _internal_exp_t(u, t):\n    return tf1.nn.relu(1.0 + (1.0 - t) * u)**(1.0 / (1.0 - t))\n\n  return tf1.cond(\n      tf1.equal(t, 1.0), lambda: tf1.exp(u),\n      functools.partial(_internal_exp_t, u, t))\n\n\ndef compute_normalization_fixed_point(activations, t, num_iters=5):\n  \"\"\"Returns the normalization value for each example (t > 1.0).\n\n  Args:\n    activations: A multi-dimensional tensor with last dimension `num_classes`.\n    t: Temperature 2 (> 1.0 for tail heaviness).\n    num_iters: Number of iterations to run the method.\n  Return: A tensor of same rank as activation with the last dimension being 1.\n  \"\"\"\n\n  mu = tf1.reduce_max(activations, -1, keep_dims=True)\n  normalized_activations_step_0 = activations - mu\n  shape_normalized_activations = tf1.shape(normalized_activations_step_0)\n\n  def iter_body(i, normalized_activations):\n    logt_partition = tf1.reduce_sum(\n        exp_t(normalized_activations, t), -1, keep_dims=True)\n    normalized_activations_t = tf1.reshape(\n        normalized_activations_step_0 * tf1.pow(logt_partition, 1.0 - t),\n        shape_normalized_activations)\n    return [i + 1, normalized_activations_t]\n\n  _, normalized_activations_t = for_loop(num_iters, iter_body,\n                                         [0, normalized_activations_step_0])\n  logt_partition = tf1.reduce_sum(\n      exp_t(normalized_activations_t, t), -1, keep_dims=True)\n  return -log_t(1.0 / logt_partition, t) + mu\n\n\ndef compute_normalization_binary_search(activations, t, num_iters=10):\n  \"\"\"Returns the normalization value for each example (t < 1.0).\n\n  Args:\n    activations: A multi-dimensional tensor with last dimension `num_classes`.\n    t: Temperature 2 (< 1.0 for finite support).\n    num_iters: Number of iterations to run the method.\n  Return: A tensor of same rank as activation with the last dimension being 1.\n  \"\"\"\n  mu = tf1.reduce_max(activations, -1, keep_dims=True)\n  normalized_activations = activations - mu\n  shape_activations = tf1.shape(activations)\n  effective_dim = tf1.cast(\n      tf1.reduce_sum(\n          tf1.cast(\n              tf1.greater(normalized_activations, -1.0 / (1.0 - t)), tf1.int32),\n          -1,\n          keep_dims=True), tf1.float32)\n  shape_partition = tf1.concat([shape_activations[:-1], [1]], 0)\n  lower = tf1.zeros(shape_partition)\n  upper = -log_t(1.0 / effective_dim, t) * tf1.ones(shape_partition)\n\n  def iter_body(i, lower, upper):\n    logt_partition = (upper + lower)/2.0\n    sum_probs = tf1.reduce_sum(exp_t(\n        normalized_activations - logt_partition, t), -1, keep_dims=True)\n    update = tf1.cast(tf1.less(sum_probs, 1.0), tf1.float32)\n    lower = tf1.reshape(lower * update + (1.0 - update) * logt_partition,\n                       shape_partition)\n    upper = tf1.reshape(upper * (1.0 - update) + update * logt_partition,\n                       shape_partition)\n    return [i + 1, lower, upper]\n\n  _, lower, upper = for_loop(num_iters, iter_body, [0, lower, upper])\n  logt_partition = (upper + lower)/2.0\n  return logt_partition + mu\n\n\ndef compute_normalization(activations, t, num_iters=5):\n  \"\"\"Returns the normalization value for each example.\n\n  Args:\n    activations: A multi-dimensional tensor with last dimension `num_classes`.\n    t: Temperature 2 (< 1.0 for finite support, > 1.0 for tail heaviness).\n    num_iters: Number of iterations to run the method.\n  Return: A tensor of same rank as activation with the last dimension being 1.\n  \"\"\"\n  return tf1.cond(\n      tf1.less(t, 1.0),\n      functools.partial(compute_normalization_binary_search, activations, t,\n                        num_iters),\n      functools.partial(compute_normalization_fixed_point, activations, t,\n                        num_iters))\n\n\ndef _internal_bi_tempered_logistic_loss(activations, labels, t1, t2):\n  \"\"\"Computes the Bi-Tempered logistic loss.\n\n  Args:\n    activations: A multi-dimensional tensor with last dimension `num_classes`.\n    labels: batch_size\n    t1: Temperature 1 (< 1.0 for boundedness).\n    t2: Temperature 2 (> 1.0 for tail heaviness).\n\n  Returns:\n    A loss tensor for robust loss.\n  \"\"\"\n  if t2 == 1.0:\n    normalization_constants = tf1.log(\n        tf1.reduce_sum(tf1.exp(activations), -1, keep_dims=True))\n    if t1 == 1.0:\n      return normalization_constants + tf1.reduce_sum(\n          tf1.multiply(labels, tf1.log(labels + 1e-10) - activations), -1)\n    else:\n      shifted_activations = tf1.exp(activations - normalization_constants)\n      one_minus_t1 = (1.0 - t1)\n      one_minus_t2 = 1.0\n  else:\n    one_minus_t1 = (1.0 - t1)\n    one_minus_t2 = (1.0 - t2)\n    normalization_constants = compute_normalization(\n        activations, t2, num_iters=5)\n    shifted_activations = tf1.nn.relu(1.0 + one_minus_t2 *\n                                     (activations - normalization_constants))\n\n  if t1 == 1.0:\n    return tf1.reduce_sum(\n        tf1.multiply(\n            tf1.log(labels + 1e-10) -\n            tf1.log(tf1.pow(shifted_activations, 1.0 / one_minus_t2)), labels),\n        -1)\n  else:\n    beta = 1.0 + one_minus_t1\n    logt_probs = (tf1.pow(shifted_activations, one_minus_t1 / one_minus_t2) -\n                  1.0) / one_minus_t1\n    return tf1.reduce_sum(\n        tf1.multiply(log_t(labels, t1) - logt_probs, labels) - 1.0 / beta *\n        (tf1.pow(labels, beta) -\n         tf1.pow(shifted_activations, beta / one_minus_t2)), -1)\n\n\ndef tempered_sigmoid(activations, t, num_iters=5):\n  \"\"\"Tempered sigmoid function.\n\n  Args:\n    activations: Activations for the positive class for binary classification.\n    t: Temperature tensor > 0.0.\n    num_iters: Number of iterations to run the method.\n\n  Returns:\n    A probabilities tensor.\n  \"\"\"\n  t = tf1.convert_to_tensor(t)\n  input_shape = tf1.shape(activations)\n  activations_2d = tf1.reshape(activations, [-1, 1])\n  internal_activations = tf1.concat(\n      [tf1.zeros_like(activations_2d), activations_2d], 1)\n  normalization_constants = tf1.cond(\n      # pylint: disable=g-long-lambda\n      tf1.equal(t, 1.0),\n      lambda: tf1.log(\n          tf1.reduce_sum(tf1.exp(internal_activations), -1, keep_dims=True)),\n      functools.partial(compute_normalization, internal_activations, t,\n                        num_iters))\n  internal_probabilities = exp_t(internal_activations - normalization_constants,\n                                 t)\n  one_class_probabilities = tf1.split(internal_probabilities, 2, axis=1)[1]\n  return tf1.reshape(one_class_probabilities, input_shape)\n\n\ndef tempered_softmax(activations, t, num_iters=5):\n  \"\"\"Tempered softmax function.\n\n  Args:\n    activations: A multi-dimensional tensor with last dimension `num_classes`.\n    t: Temperature tensor > 0.0.\n    num_iters: Number of iterations to run the method.\n\n  Returns:\n    A probabilities tensor.\n  \"\"\"\n  t = tf1.convert_to_tensor(t)\n  normalization_constants = tf1.cond(\n      tf1.equal(t, 1.0),\n      lambda: tf1.log(tf1.reduce_sum(tf1.exp(activations), -1, keep_dims=True)),\n      functools.partial(compute_normalization, activations, t, num_iters))\n  return exp_t(activations - normalization_constants, t)\n\n\ndef bi_tempered_binary_logistic_loss(activations,\n                                     labels,\n                                     t1,\n                                     t2,\n                                     label_smoothing=0.0,\n                                     num_iters=5):\n  \"\"\"Bi-Tempered binary logistic loss.\n\n  Args:\n    activations: A tensor containing activations for class 1.\n    labels: A tensor with shape and dtype as activations.\n    t1: Temperature 1 (< 1.0 for boundedness).\n    t2: Temperature 2 (> 1.0 for tail heaviness, < 1.0 for finite support).\n    label_smoothing: Label smoothing\n    num_iters: Number of iterations to run the method.\n\n  Returns:\n    A loss tensor.\n  \"\"\"\n  with tf1.name_scope('binary_bitempered_logistic'):\n    t1 = tf1.convert_to_tensor(t1)\n    t2 = tf1.convert_to_tensor(t2)\n    out_shape = tf1.shape(labels)\n    labels_2d = tf1.reshape(labels, [-1, 1])\n    activations_2d = tf1.reshape(activations, [-1, 1])\n    internal_labels = tf1.concat([1.0 - labels_2d, labels_2d], 1)\n    internal_logits = tf1.concat([tf1.zeros_like(activations_2d), activations_2d],\n                                1)\n    losses = bi_tempered_logistic_loss(internal_logits, internal_labels, t1, t2,\n                                       label_smoothing, num_iters)\n    return tf1.reshape(losses, out_shape)\n\n\ndef bi_tempered_logistic_loss(labels,\n                              activations,\n                              t1=0.2,\n                              t2=1.0,\n                              label_smoothing=0.1,\n                              num_iters=10):\n  \"\"\"Bi-Tempered Logistic Loss with custom gradient.\n\n  Args:\n    activations: A multi-dimensional tensor with last dimension `num_classes`.\n    labels: A tensor with shape and dtype as activations.\n    t1: Temperature 1 (< 1.0 for boundedness).\n    t2: Temperature 2 (> 1.0 for tail heaviness, < 1.0 for finite support).\n    label_smoothing: Label smoothing parameter between [0, 1).\n    num_iters: Number of iterations to run the method.\n\n  Returns:\n    A loss tensor.\n  \"\"\"\n  with tf1.name_scope('bitempered_logistic'):\n    t1 = tf1.convert_to_tensor(t1)\n    t2 = tf1.convert_to_tensor(t2)\n    if label_smoothing > 0.0:\n      num_classes = tf1.cast(tf1.shape(labels)[-1], tf1.float32)\n      labels = (\n          1 - num_classes /\n          (num_classes - 1) * label_smoothing) * labels + label_smoothing / (\n              num_classes - 1)\n\n    @tf1.custom_gradient\n    def _custom_gradient_bi_tempered_logistic_loss(activations):\n      \"\"\"Bi-Tempered Logistic Loss with custom gradient.\n\n      Args:\n        activations: A multi-dimensional tensor with last dim `num_classes`.\n\n      Returns:\n        A loss tensor, grad.\n      \"\"\"\n      with tf1.name_scope('gradient_bitempered_logistic'):\n        probabilities = tempered_softmax(activations, t2, num_iters)\n        loss_values = tf1.multiply(\n            labels,\n            log_t(labels + 1e-10, t1) -\n            log_t(probabilities, t1)) - 1.0 / (2.0 - t1) * (\n                tf1.pow(labels, 2.0 - t1) - tf1.pow(probabilities, 2.0 - t1))\n\n        def grad(d_loss):\n          \"\"\"Explicit gradient calculation.\n\n          Args:\n            d_loss: Infinitesimal change in the loss value.\n          Returns:\n            Loss gradient.\n          \"\"\"\n          delta_probs = probabilities - labels\n          forget_factor = tf1.pow(probabilities, t2 - t1)\n          delta_probs_times_forget_factor = tf1.multiply(delta_probs,\n                                                        forget_factor)\n          delta_forget_sum = tf1.reduce_sum(\n              delta_probs_times_forget_factor, -1, keep_dims=True)\n          escorts = tf1.pow(probabilities, t2)\n          escorts = escorts / tf1.reduce_sum(escorts, -1, keep_dims=True)\n          derivative = delta_probs_times_forget_factor - tf1.multiply(\n              escorts, delta_forget_sum)\n          return tf1.multiply(d_loss, derivative)\n\n        return loss_values, grad\n\n    loss_values = tf1.cond(tf1.logical_and(tf1.equal(t1, 1.0), tf1.equal(t2, 1.0)),\n                          functools.partial(\n                              tf1.nn.softmax_cross_entropy_with_logits,\n                              labels=labels,\n                              logits=activations),\n                          functools.partial(\n                              _custom_gradient_bi_tempered_logistic_loss,\n                              activations))\n    reduce_sum_last = lambda x: tf1.reduce_sum(x, -1)\n    loss_values = tf1.cond(tf1.logical_and(tf1.equal(t1, 1.0), tf1.equal(t2, 1.0)),\n                          functools.partial(tf1.identity, loss_values),\n                          functools.partial(reduce_sum_last, loss_values))\n#     _,temp = functools.partial(_custom_gradient_bi_tempered_logistic_loss,activations)\n    return loss_values\n\n\ndef sparse_bi_tempered_logistic_loss(activations, labels, t1, t2, num_iters=5):\n  \"\"\"Sparse Bi-Tempered Logistic Loss with custom gradient.\n\n  Args:\n    activations: A multi-dimensional tensor with last dimension `num_classes`.\n    labels: A tensor with dtype of int32.\n    t1: Temperature 1 (< 1.0 for boundedness).\n    t2: Temperature 2 (> 1.0 for tail heaviness, < 1.0 for finite support).\n    num_iters: Number of iterations to run the method.\n\n  Returns:\n    A loss tensor.\n  \"\"\"\n  with tf1.name_scope('sparse_bitempered_logistic'):\n    t1 = tf1.convert_to_tensor(t1)\n    t2 = tf1.convert_to_tensor(t2)\n    num_classes = tf1.shape(activations)[-1]\n\n    @tf1.custom_gradient\n    def _custom_gradient_sparse_bi_tempered_logistic_loss(activations):\n      \"\"\"Sparse Bi-Tempered Logistic Loss with custom gradient.\n\n      Args:\n        activations: A multi-dimensional tensor with last dim `num_classes`.\n\n      Returns:\n        A loss tensor, grad.\n      \"\"\"\n      with tf1.name_scope('gradient_sparse_bitempered_logistic'):\n        probabilities = tempered_softmax(activations, t2, num_iters)\n        # TODO(eamid): Replace one hot with gather.\n        loss_values = -log_t(\n            tf1.reshape(\n                tf1.gather_nd(probabilities,\n                             tf1.where(tf1.one_hot(labels, num_classes))),\n                tf1.shape(activations)[:-1]), t1) - 1.0 / (2.0 - t1) * (\n                    1.0 - tf1.reduce_sum(tf1.pow(probabilities, 2.0 - t1), -1))\n\n        def grad(d_loss):\n          \"\"\"Explicit gradient calculation.\n\n          Args:\n            d_loss: Infinitesimal change in the loss value.\n          Returns:\n            Loss gradient.\n          \"\"\"\n          delta_probs = probabilities - tf1.one_hot(labels, num_classes)\n          forget_factor = tf1.pow(probabilities, t2 - t1)\n          delta_probs_times_forget_factor = tf1.multiply(delta_probs,\n                                                        forget_factor)\n          delta_forget_sum = tf1.reduce_sum(\n              delta_probs_times_forget_factor, -1, keep_dims=True)\n          escorts = tf1.pow(probabilities, t2)\n          escorts = escorts / tf1.reduce_sum(escorts, -1, keep_dims=True)\n          derivative = delta_probs_times_forget_factor - tf1.multiply(\n              escorts, delta_forget_sum)\n          return tf1.multiply(d_loss, derivative)\n\n        return loss_values, grad\n\n    loss_values = tf1.cond(\n        tf1.logical_and(tf1.equal(t1, 1.0), tf1.equal(t2, 1.0)),\n        functools.partial(tf1.nn.sparse_softmax_cross_entropy_with_logits,\n                          labels=labels, logits=activations),\n        functools.partial(_custom_gradient_sparse_bi_tempered_logistic_loss,\n                          activations))\n    return loss_values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def acc_gambler(y_true,y_pred):\n    y_temp = y_pred[:,1:]\n    count = tf.constant((0,))\n    for i in range(len(y_true)):\n        tf.autograph.experimental.set_loop_options(\n        shape_invariants=[(count, tf.TensorShape([None]))])\n        if tf.math.argmax(y_temp[i]) == tf.math.argmax(y_true[i]) :\n            count = tf.math.add(count,1)\n    return float(count)/float(len(y_true))\n\nimport keras.backend as K\n\n# @tf.autograph.experimental.do_not_convert\ndef loss_gambler(label_smoothing=0.0):\n    def loss_gamb(y_true,y_pred):\n        y_true = tf.math.add(y_true,tf.math.add(tf.math.multiply(label_smoothing/2.0,tf.math.add(1.0,-1*y_true)),tf.math.multiply(-1*label_smoothing/2.0,y_true)))\n        y_temp = y_pred[:,1:]\n        f0 = y_pred[:,0]\n        lamb = tf.math.divide(tf.math.multiply(K.sum(y_temp),K.sum(y_temp)),K.sum(tf.math.multiply(y_temp,y_temp)))\n        loss = tf.constant((0.0,))\n        for i in range(len(y_true[0])):\n    #         f0 = y_pred[i,0]\n    #         lamb = tf.math.divide(tf.math.multiply(K.sum(y_temp[i]),K.sum(y_temp[i])),K.sum(tf.math.multiply(y_temp[i],y_temp[i])))\n            tf.autograph.experimental.set_loop_options(\n            shape_invariants=[(loss, tf.TensorShape([None]))])\n            temp = tf.constant((0.0,))\n    #         for j in range(5):\n    #             temp = tf.math.add(temp,(-1.0*(1/float(BATCH_SIZE))*tf.math.multiply(y_true[i,j],K.log(y_temp[i,j]+f0/lamb))))\n            loss = tf.math.add(loss,tf.math.add(temp,(-1.0*(1/float(len(y_true)))*tf.math.multiply(y_true[:,i],K.log(y_temp[:,i]+f0/lamb)))))\n        return tf.math.reduce_sum(loss)\n    return loss_gamb","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model_v1 = tf.keras.models.load_model('../input/only-xception-with-cropping/saved-model-11-0.879')\nmodel_v2 = tf.keras.models.load_model('../input/gambler-s-loss-cassava/saved-model-10-0.843',custom_objects={'acc_gambler' : acc_gambler,'loss_gamb' : loss_gambler(0.1)})\nmodel_v3 = tf.keras.models.load_model('../input/bitempered-loss-only-xception-with-cropping/saved-model-12-0.849',custom_objects={'bi_tempered_logistic_loss' : bi_tempered_logistic_loss})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def random_crop(img, random_crop_size):\n    # Note: image_data_format is 'channel_last'\n    assert img.shape[2] == 3\n    height, width = img.shape[0], img.shape[1]\n    dy, dx = random_crop_size\n    x = np.random.randint(0, width - dx + 1)\n    y = np.random.randint(0, height - dy + 1)\n    return img[y:(y+dy), x:(x+dx), :]\n\n\ndef crop_generator(batches, crop_length):\n    \"\"\"Take as input a Keras ImageGen (Iterator) and generate random\n    crops from the image batches generated by the original iterator.\n    \"\"\"\n    while True:\n        batch_x = next(batches)\n        batch_crops = np.zeros((batch_x.shape[0], crop_length, crop_length, 3))\n        for i in range(batch_x.shape[0]):\n            batch_crops[i] = random_crop(batch_x[i], (crop_length, crop_length))\n        yield batch_crops","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Model 1"},{"metadata":{"trusted":true},"cell_type":"code","source":"labels = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4}\n\ntest_datagen_v1 = ImageDataGenerator(\n                    zoom_range=0.4,\n                    horizontal_flip=True\n                    )\n\n\ntest_dir_v1 = '../input/cassava-leaf-disease-classification/test_images/'\n\ntest_v1=pd.DataFrame()\ntest_v1['image_id']=os.listdir(test_dir_v1)\n\n# test_dir = '../input/cassava-leaf-disease-classification/train_images/'\n\n# test=pd.DataFrame()\n# test['image_id']=os.listdir('../input/cassava-leaf-disease-classification/train_images/')\n\n\ntest_generator_v1 = test_datagen_v1.flow_from_dataframe(\n        test_v1,\n        directory = test_dir_v1,\n        x_col = 'image_id',\n        target_size = (448, 448),\n        batch_size = 1,\n        class_mode = None,\n        shuffle=False)\n# test_generator_v1 = crop_generator(test_generator_v1,448)\n\ntest_generator_v1.reset()\n\npred_v1 = model_v1.predict_generator(test_generator_v1,verbose=1,steps = len(test_v1))\n\nfor i in range(9):\n    pred_v1 = pred_v1+model_v1.predict_generator(test_generator_v1,verbose=1,steps= len(test_v1))\n\npred_v1 = pred_v1/10.0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# predicted_class_indices_v1 = np.argmax(pred_v1,axis=1)\n\n# # labels = (train_generator.class_indices)\n# labels = dict((v,k) for k,v in labels.items())\n# predictions_v1 = [labels[k] for k in predicted_class_indices_v1]\n\n# filenames=test_generator_v1.filenames\n# results_v1=pd.DataFrame({\"image_id\":filenames,\n#                       \"label\":predictions_v1})\n\n# results_v1.to_csv('/kaggle/working/submission.csv',index=False)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Model 2"},{"metadata":{"trusted":true},"cell_type":"code","source":"labels = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4}\n\ntest_datagen_v2 = ImageDataGenerator(\n                    zoom_range=0.4,\n                    horizontal_flip=True\n                    )\n\ntest_dir_v2 = '../input/cassava-leaf-disease-classification/test_images/'\n\ntest_v2=pd.DataFrame()\ntest_v2['image_id']=os.listdir('../input/cassava-leaf-disease-classification/test_images/')\n\ntest_generator_v2 = test_datagen_v2.flow_from_dataframe(\n        test_v2,\n        directory = test_dir_v2,\n        x_col = 'image_id',\n        target_size = (448, 448),\n        batch_size = 1,\n        class_mode = None,\n        shuffle=False)\n# test_generator_v22 = crop_generator(test_generator_v2,448)\ntest_generator_v2.reset()\n\n\ntemp = model_v2.predict_generator(test_generator_v2,verbose=1,steps = len(test_v2))\n# print(temp)\npred_v2 = temp[:,1:]\n\nfor i in range(9):\n    temp = model_v2.predict_generator(test_generator_v2,verbose=1,steps = len(test_v2))\n#     print(temp)\n    pred_v2 = pred_v2+temp[:,1:]\n    \npred_v2 = pred_v2/10.0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# predicted_class_indices_v2 = np.argmax(pred_v2,axis=1)\n\n# # labels = (train_generator_v2.class_indices)\n# labels = dict((v,k) for k,v in labels.items())\n# predictions_v2 = [labels[k] for k in predicted_class_indices_v2]\n\n# filenames=test_generator_v2.filenames\n# results_v2=pd.DataFrame({\"image_id\":filenames,\n#                       \"label\":predictions_v2})\n\n# results_v2.to_csv('/kaggle/working/submission.csv',index=False)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Model 3"},{"metadata":{"trusted":true},"cell_type":"code","source":"labels = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4}\n\ntest_datagen_v3 = ImageDataGenerator(\n                    zoom_range=0.4,\n                    horizontal_flip=True\n                    )\n\n\ntest_dir_v3 = '../input/cassava-leaf-disease-classification/test_images/'\n\ntest_v3=pd.DataFrame()\ntest_v3['image_id']=os.listdir(test_dir_v3)\n\n# test_dir = '../input/cassava-leaf-disease-classification/train_images/'\n\n# test=pd.DataFrame()\n# test['image_id']=os.listdir('../input/cassava-leaf-disease-classification/train_images/')\n\n\ntest_generator_v3 = test_datagen_v3.flow_from_dataframe(\n        test_v3,\n        directory = test_dir_v3,\n        x_col = 'image_id',\n        target_size = (448, 448),\n        batch_size = 1,\n        class_mode = None,\n        shuffle=False)\n# test_generator_v3 = crop_generator(test_generator_v3,448)\n\ntest_generator_v3.reset()\n\npred_v3 = model_v3.predict_generator(test_generator_v3,verbose=1,steps = len(test_v3))\n\nfor i in range(9) :\n    pred_v3 = pred_v3+model_v3.predict_generator(test_generator_v3,verbose=1,steps = len(test_v3))\npred_v3 = pred_v3/10.0","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Ensemble"},{"metadata":{"trusted":true},"cell_type":"code","source":"labels = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4}\n# pred_new = 0.40*pred_v1+0.30*pred_v2+0.30*pred_v3\n# pred_new = 0.5*pred_v1+0.5*pred_v2\npred_new = pred_v1+pred_v2+pred_v3\n\npredicted_class_indices_new = np.argmax(pred_new,axis=1)\n\n# labels = (train_generator_v2.class_indices)\nlabels = dict((v,k) for k,v in labels.items())\npredictions_new = [labels[k] for k in predicted_class_indices_new]\n\nfilenames=test_generator_v2.filenames\nresults_new=pd.DataFrame({\"image_id\":filenames,\n                      \"label\":predictions_new})\n\nresults_new.to_csv('/kaggle/working/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}