{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## Imports","metadata":{}},{"cell_type":"code","source":"import sys\nsys.path.append('../input/efficientnet-weights/brain_automl/')\nsys.path.append('../input/efficientnet-weights/brain_automl/efficientnetv2/')\n\nimport effnetv2_model","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-04-12T00:10:00.511809Z","iopub.execute_input":"2022-04-12T00:10:00.512572Z","iopub.status.idle":"2022-04-12T00:10:00.517408Z","shell.execute_reply.started":"2022-04-12T00:10:00.512533Z","shell.execute_reply":"2022-04-12T00:10:00.516732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nfrom multiprocessing import cpu_count\nwarnings.simplefilter('ignore')","metadata":{"execution":{"iopub.status.busy":"2022-04-12T00:10:00.579379Z","iopub.execute_input":"2022-04-12T00:10:00.579662Z","iopub.status.idle":"2022-04-12T00:10:00.583623Z","shell.execute_reply.started":"2022-04-12T00:10:00.579633Z","shell.execute_reply":"2022-04-12T00:10:00.582838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf, re, math\nimport tensorflow_addons as tfa\n\nfrom tensorflow.keras import layers, optimizers, losses, metrics, callbacks, initializers\nfrom tensorflow.keras import Sequential, Model, Input\n\nimport os\nimport sys\nimport glob\nimport json\nimport gc\n\nfrom functools import partial\n\nimport random\nimport numpy as np\nimport pandas as pd \n\nfrom sklearn.model_selection import train_test_split, StratifiedKFold\nfrom sklearn.preprocessing import LabelEncoder\n\nfrom tqdm.notebook import tqdm\n\nimport matplotlib.pyplot as plt\nplt.style.use(\"ggplot\")\n%matplotlib inline\n\nimport seaborn as sns\n\nimport albumentations as A\nimport imgaug.augmenters as iaa\n\nimport cv2\nfrom PIL import Image\n\nprint(f'tensorflow version: {tf.__version__}')\nprint(f'python version: P{sys.version}')\n\ndef seed_everything(seed):\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    random.seed(seed)\n    np.random.seed(seed)\n    tf.random.set_seed(seed)    \n    \nseed_everything(42)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T00:10:00.64634Z","iopub.execute_input":"2022-04-12T00:10:00.646647Z","iopub.status.idle":"2022-04-12T00:10:00.756139Z","shell.execute_reply.started":"2022-04-12T00:10:00.646618Z","shell.execute_reply":"2022-04-12T00:10:00.75535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## TPU&GPU Check","metadata":{}},{"cell_type":"code","source":"try:\n    TPU = tf.distribute.cluster_resolver.TPUClusterResolver()  # TPU detection. No parameters necessary if TPU_NAME environment variable is set. On Kaggle this is always the case.\n    print('Running on TPU ', TPU.master())\nexcept ValueError:\n    print('Running on GPU')\n    TPU = None\n\nif TPU:\n    tf.config.experimental_connect_to_cluster(TPU)\n    tf.tpu.experimental.initialize_tpu_system(TPU)\n    strategy = tf.distribute.experimental.TPUStrategy(TPU)\nelse:\n    strategy = tf.distribute.get_strategy() # default distribution strategy in Tensorflow. Works on CPU and single GPU.\n\nREPLICAS = strategy.num_replicas_in_sync\nprint(f'REPLICAS: {REPLICAS}')\n\nAUTO = tf.data.experimental.AUTOTUNE","metadata":{"execution":{"iopub.status.busy":"2022-04-12T00:10:00.758611Z","iopub.execute_input":"2022-04-12T00:10:00.759057Z","iopub.status.idle":"2022-04-12T00:10:00.767536Z","shell.execute_reply.started":"2022-04-12T00:10:00.759023Z","shell.execute_reply":"2022-04-12T00:10:00.766796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/sorghum-id-fgvc-9/train_cultivar_mapping.csv')\ntrain = train.dropna().reset_index(drop=True)\ndisplay(train.head())","metadata":{"execution":{"iopub.status.busy":"2022-04-12T00:10:00.963875Z","iopub.execute_input":"2022-04-12T00:10:00.964361Z","iopub.status.idle":"2022-04-12T00:10:01.006151Z","shell.execute_reply.started":"2022-04-12T00:10:00.964328Z","shell.execute_reply":"2022-04-12T00:10:01.005442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Parameters","metadata":{}},{"cell_type":"code","source":"# Input Image Shape\nIMG_SIZE = 224\nN_CHANNELS = 3\nINPUT_SHAPE = (IMG_SIZE, IMG_SIZE, N_CHANNELS)\nN_SAMPLES = len(train)\n\nN_EPOCHS = 15\n\nBATCH_SIZE_BASE = 16\nBATCH_SIZE = BATCH_SIZE_BASE * REPLICAS\n\n# ImageNet Normalization\nIMAGENET_MEAN = tf.constant([0.485, 0.456, 0.406], dtype=tf.float32)\nIMAGENET_STD = tf.constant([0.229, 0.224, 0.225], dtype=tf.float32)\n\nAUTO = tf.data.experimental.AUTOTUNE\nEPS = tf.keras.backend.epsilon()\n\nval_fold = 0\nk_fold = 5\nN_SPLITS=10\n\nprint(f'N_SAMPLES: {N_SAMPLES}, BATCH_SIZE: {BATCH_SIZE}')","metadata":{"execution":{"iopub.status.busy":"2022-04-12T00:10:00.819244Z","iopub.execute_input":"2022-04-12T00:10:00.81954Z","iopub.status.idle":"2022-04-12T00:10:00.827537Z","shell.execute_reply.started":"2022-04-12T00:10:00.81951Z","shell.execute_reply":"2022-04-12T00:10:00.826781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Train CSV","metadata":{}},{"cell_type":"code","source":"def get_image_path(id, dir):\n    return f\"{dir + id}\"\n\ntrain[\"image_path\"] = train[\"image\"].apply(get_image_path, dir='../input/sorghum-id-fgvc-9/train_images/')\n\nencoder = LabelEncoder()\nids2label = {l: i for (i, l) in enumerate(encoder.fit(train[\"cultivar\"]).classes_)}\nlabel2ids = {x[1]: x[0] for x in ids2label.items()}\n\ntrain[\"cultivar_id\"] = encoder.fit_transform(train[\"cultivar\"])\n\nskf = StratifiedKFold(n_splits=N_SPLITS)\nfor fold, (_, val_) in enumerate(skf.split(X=train, y=train.cultivar)):\n    train.loc[val_, \"kfold\"] = fold\n    \ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-12T00:10:01.106402Z","iopub.execute_input":"2022-04-12T00:10:01.106607Z","iopub.status.idle":"2022-04-12T00:10:01.134289Z","shell.execute_reply.started":"2022-04-12T00:10:01.106581Z","shell.execute_reply":"2022-04-12T00:10:01.13363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Number of Labels","metadata":{}},{"cell_type":"code","source":"N_CULTIVAR = train['cultivar'].nunique()\nprint(f'N_INDIVIDUAL_IDS: {N_CULTIVAR}')","metadata":{"execution":{"iopub.status.busy":"2022-04-12T00:10:01.172979Z","iopub.execute_input":"2022-04-12T00:10:01.173265Z","iopub.status.idle":"2022-04-12T00:10:01.179212Z","shell.execute_reply.started":"2022-04-12T00:10:01.173236Z","shell.execute_reply":"2022-04-12T00:10:01.178368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Augmentation","metadata":{}},{"cell_type":"code","source":"transforms_train = A.Compose([\n        A.HorizontalFlip(p=0.5),\n        A.ImageCompression(quality_lower=99, quality_upper=100),\n        A.ShiftScaleRotate(shift_limit=0.2, scale_limit=0.2, rotate_limit=10, border_mode=0, p=0.7),\n        A.Resize(IMG_SIZE, IMG_SIZE),\n        A.Cutout(max_h_size=int(IMG_SIZE * 0.4), max_w_size=int(IMG_SIZE * 0.4), num_holes=1, p=0.5)\n    ])","metadata":{"execution":{"iopub.status.busy":"2022-04-12T00:10:01.237939Z","iopub.execute_input":"2022-04-12T00:10:01.238304Z","iopub.status.idle":"2022-04-12T00:10:01.244331Z","shell.execute_reply.started":"2022-04-12T00:10:01.238271Z","shell.execute_reply":"2022-04-12T00:10:01.243511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Dataset Utils","metadata":{}},{"cell_type":"code","source":"def decode_image(filename, label=None, image_size=IMG_SIZE):\n    bits = tf.io.read_file(filename)\n    image = tf.image.decode_jpeg(bits, channels=3)\n    image = tf.image.resize(image, [image_size, image_size])\n    image = tf.cast(image, tf.float32) / 255.0\n    #image = (image - IMAGENET_MEAN) / IMAGENET_STD\n    return image, label\n    \ndef aug_fn(image, img_size=IMG_SIZE):\n    data = {\"image\":image}\n    aug_data = transforms_train(**data)\n    aug_img = aug_data[\"image\"]\n    aug_img = tf.image.resize(aug_img, size=[img_size, img_size])\n    return aug_img\n\ndef process_data(image, label, img_size=IMG_SIZE):\n    aug_img = tf.numpy_function(func=aug_fn, inp=[image, img_size], Tout=tf.float32)\n    return aug_img, label\n\ndef set_shapes(image, label, img_shape=INPUT_SHAPE):\n    image.set_shape(img_shape)\n    #label.set_shape([])\n    return image, label\n\ndef one_hot(image, label, CLASSES=N_CULTIVAR):\n    return image, tf.one_hot(indices=label, \n                             depth=CLASSES,\n                             dtype=tf.float32)\n\ndef view_image(ds):\n    image, label = next(iter(ds)) # extract 1 batch from the dataset\n    image = image.numpy()\n    label = label.numpy()\n    \n    fig = plt.figure(figsize=(22, 22))\n    for i in range(20):\n        ax = fig.add_subplot(4, 5, i+1, xticks=[], yticks=[])\n        ax.imshow(image[i])\n        ax.set_title(f\"Label: {label[i]}\")","metadata":{"execution":{"iopub.status.busy":"2022-04-12T00:10:01.303961Z","iopub.execute_input":"2022-04-12T00:10:01.304159Z","iopub.status.idle":"2022-04-12T00:10:01.315436Z","shell.execute_reply.started":"2022-04-12T00:10:01.304134Z","shell.execute_reply":"2022-04-12T00:10:01.314471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Train and Valid Dataset","metadata":{}},{"cell_type":"code","source":"train_df = train[train.kfold%k_fold != val_fold].reset_index(drop=True)\n\nval_df = train[train.kfold%k_fold == val_fold].reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T00:10:01.370251Z","iopub.execute_input":"2022-04-12T00:10:01.370542Z","iopub.status.idle":"2022-04-12T00:10:01.382896Z","shell.execute_reply.started":"2022-04-12T00:10:01.370513Z","shell.execute_reply":"2022-04-12T00:10:01.382017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = (\n    tf.data.Dataset\n    .from_tensor_slices((train_df.image_path, train_df.cultivar_id))\n    .map(decode_image, num_parallel_calls=AUTO)\n    .map(partial(process_data, img_size=IMG_SIZE), num_parallel_calls=AUTO)\n    .map(set_shapes, num_parallel_calls=AUTO)\n    .map(one_hot, num_parallel_calls=AUTO)\n    .repeat()\n    .shuffle(1024)\n    .batch(BATCH_SIZE)\n    .prefetch(AUTO)\n)\n\nvalid_dataset = (\n    tf.data.Dataset\n    .from_tensor_slices((val_df.image_path, val_df.cultivar_id))\n    .map(decode_image, num_parallel_calls=AUTO)\n    .map(one_hot, num_parallel_calls=AUTO)                        \n    .batch(BATCH_SIZE)\n    .prefetch(AUTO)\n)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T00:10:01.435503Z","iopub.execute_input":"2022-04-12T00:10:01.435703Z","iopub.status.idle":"2022-04-12T00:10:01.642115Z","shell.execute_reply.started":"2022-04-12T00:10:01.435675Z","shell.execute_reply":"2022-04-12T00:10:01.641383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model","metadata":{}},{"cell_type":"code","source":"def get_model():\n    tf.keras.backend.clear_session()\n    \n    tf.config.optimizer.set_jit(True)\n\n    with strategy.scope():\n        image = Input(INPUT_SHAPE, name='image', dtype=tf.float32)\n        \n        cnn = effnetv2_model.get_model('efficientnet-b5', include_top=False, weights=None)\n        WEIGHT_PATH = '../input/efficientnet-weights/noisy_student_efficientnet-b5'\n        ckpt = tf.train.latest_checkpoint(WEIGHT_PATH)\n        cnn.load_weights(ckpt)\n        \n        embedding = cnn(image)\n        \n        outputs = layers.Dropout(0.25)(embedding)\n        \n        outputs = layers.Dense(N_CULTIVAR, activation=\"softmax\", name='predictions')(outputs)\n        \n        model = Model(inputs=image, outputs=outputs)\n        \n        # OPTIMIZER\n        #optimizer = optimizers.Adam()\n        optimizer = tfa.optimizers.AdamW(weight_decay=1e-8)\n        \n        # LOSS\n        loss = {\n            'predictions': losses.CategoricalCrossentropy(label_smoothing=0.1),\n        }\n \n        # METRICS\n        metricsx =[\n            tfa.metrics.F1Score(num_classes=N_CULTIVAR, name='f1_score'),\n            metrics.TopKCategoricalAccuracy(k=1, name='top1acc'),\n        ]\n\n        # Compile Model\n        model.compile(optimizer=optimizer, loss=loss, metrics=metricsx)\n\n        return model","metadata":{"execution":{"iopub.status.busy":"2022-04-12T00:10:01.645355Z","iopub.execute_input":"2022-04-12T00:10:01.645563Z","iopub.status.idle":"2022-04-12T00:10:01.654381Z","shell.execute_reply.started":"2022-04-12T00:10:01.645537Z","shell.execute_reply":"2022-04-12T00:10:01.653679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = get_model()","metadata":{"execution":{"iopub.status.busy":"2022-04-12T00:10:01.655668Z","iopub.execute_input":"2022-04-12T00:10:01.657338Z","iopub.status.idle":"2022-04-12T00:10:18.661869Z","shell.execute_reply.started":"2022-04-12T00:10:01.657295Z","shell.execute_reply":"2022-04-12T00:10:18.661162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-04-12T00:10:18.664716Z","iopub.execute_input":"2022-04-12T00:10:18.665498Z","iopub.status.idle":"2022-04-12T00:10:18.709241Z","shell.execute_reply.started":"2022-04-12T00:10:18.665449Z","shell.execute_reply":"2022-04-12T00:10:18.708554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Learning Rate Scheduler","metadata":{}},{"cell_type":"code","source":"class CosineScheduler():\n    def __init__(self, max_update, base_lr=0.01, final_lr=0,\n               warmup_steps=0, warmup_begin_lr=0):\n        self.base_lr_orig = base_lr\n        self.max_update = max_update\n        self.final_lr = final_lr\n        self.warmup_steps = warmup_steps\n        self.warmup_begin_lr = warmup_begin_lr\n        self.max_steps = self.max_update - self.warmup_steps\n\n    def get_warmup_lr(self, epoch):\n        increase = (self.base_lr_orig - self.warmup_begin_lr) \\\n                       * float(epoch) / float(self.warmup_steps)\n        return self.warmup_begin_lr + increase\n\n    def __call__(self, epoch):\n        if epoch < self.warmup_steps:\n            return self.get_warmup_lr(epoch)\n        if epoch <= self.max_update:\n            self.base_lr = self.final_lr + (\n                self.base_lr_orig - self.final_lr) * (1 + math.cos(\n                math.pi * (epoch - self.warmup_steps) / self.max_steps)) / 2\n        return self.base_lr\n\nscheduler = CosineScheduler(max_update=N_EPOCHS, base_lr=1e-3, final_lr=1e-4, warmup_steps=3, warmup_begin_lr=1e-4)\nplt.plot([scheduler(t) for t in range(N_EPOCHS)])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-12T00:10:18.710625Z","iopub.execute_input":"2022-04-12T00:10:18.710905Z","iopub.status.idle":"2022-04-12T00:10:18.888521Z","shell.execute_reply.started":"2022-04-12T00:10:18.710854Z","shell.execute_reply":"2022-04-12T00:10:18.887893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Callbacks","metadata":{}},{"cell_type":"code","source":"model_checkpoint_callback = callbacks.ModelCheckpoint(\n    'model_best.h5', \n    monitor='loss', \n    verbose=1, \n    save_best_only=True, \n    save_weights_only=True\n)\n\nmodel_checkpoint_callback.set_model(model)\n\nlearning_rate_callback = callbacks.LearningRateScheduler(scheduler, verbose=1)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T00:10:18.889755Z","iopub.execute_input":"2022-04-12T00:10:18.890016Z","iopub.status.idle":"2022-04-12T00:10:18.89479Z","shell.execute_reply.started":"2022-04-12T00:10:18.889981Z","shell.execute_reply":"2022-04-12T00:10:18.893837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Train","metadata":{}},{"cell_type":"code","source":"STEPS_PER_EPOCH = len(train_df) // BATCH_SIZE\n\nSTEPS_PER_EPOCH","metadata":{"execution":{"iopub.status.busy":"2022-04-12T00:10:18.896409Z","iopub.execute_input":"2022-04-12T00:10:18.896683Z","iopub.status.idle":"2022-04-12T00:10:18.905909Z","shell.execute_reply.started":"2022-04-12T00:10:18.896646Z","shell.execute_reply":"2022-04-12T00:10:18.905081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_dataset,\n                    validation_data=valid_dataset,\n                    epochs=N_EPOCHS,\n                    callbacks=[learning_rate_callback,\n                              model_checkpoint_callback,],\n                    steps_per_epoch=STEPS_PER_EPOCH\n                   )","metadata":{"execution":{"iopub.status.busy":"2022-04-12T00:10:18.907334Z","iopub.execute_input":"2022-04-12T00:10:18.907615Z","iopub.status.idle":"2022-04-12T00:12:14.05318Z","shell.execute_reply.started":"2022-04-12T00:10:18.907581Z","shell.execute_reply":"2022-04-12T00:12:14.051682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Training History\n","metadata":{}},{"cell_type":"code","source":"def plot_history_metric(metric, f_best=np.argmax, yscale='linear'):\n    x = np.arange(1, len(history.history[metric]) + 1)\n    y_train = history.history[metric]\n    plt.figure(figsize=(20, 8))\n    # TRAIN\n    plt.plot(x, y_train, color='tab:blue', lw=3, label='train')\n    plt.title(f'Training {metric}', fontsize=24, pad=10)\n    plt.ylabel(metric, fontsize=20, labelpad=10)\n    plt.xlabel('epoch', fontsize=20, labelpad=10)\n    plt.xticks([1] + np.arange(5, N_EPOCHS + 1, 5).tolist(), fontsize=16) # set tick step to 1 and let x axis start at 1\n    plt.yticks(fontsize=16)\n    plt.yscale(yscale)\n    \n    # Train Best Marker\n    x_best = f_best(y_train)\n    y_best = y_train[x_best]\n    plt.scatter(x_best + 1, y_best, color='purple', s=100, marker='o', label=f'train best: {y_best:.4f}')\n \n    if f'val_{metric}' in history.history:\n        y_val = history.history[f'val_{metric}']\n       # Validation Best Marker\n        plt.plot(x, y_val, color='tab:orange', lw=3, label='validation')\n        # VALIDATION\n        x_best = f_best(y_val)\n        y_best = y_val[x_best]\n        plt.scatter(x_best + 1, y_best, color='red', s=100, marker='o', label=f'validation best: {y_best:.4f}')\n    \n    plt.grid()\n    plt.legend(prop={'size': 18})\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-12T00:12:14.054172Z","iopub.status.idle":"2022-04-12T00:12:14.054761Z","shell.execute_reply.started":"2022-04-12T00:12:14.054589Z","shell.execute_reply":"2022-04-12T00:12:14.054613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_history_metric('loss', f_best=np.argmin)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T00:12:14.055809Z","iopub.status.idle":"2022-04-12T00:12:14.056398Z","shell.execute_reply.started":"2022-04-12T00:12:14.056162Z","shell.execute_reply":"2022-04-12T00:12:14.056187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_history_metric('top1acc', f_best=np.argmax)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T00:12:14.057377Z","iopub.status.idle":"2022-04-12T00:12:14.057723Z","shell.execute_reply.started":"2022-04-12T00:12:14.057535Z","shell.execute_reply":"2022-04-12T00:12:14.057555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Prediction","metadata":{}},{"cell_type":"code","source":"model.load_weights('./model_best.h5')\n\n@tf.function(experimental_compile=True)\ndef predict_on_batch(images):\n    return model(images, training=False)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T00:12:14.058613Z","iopub.status.idle":"2022-04-12T00:12:14.058988Z","shell.execute_reply.started":"2022-04-12T00:12:14.058758Z","shell.execute_reply":"2022-04-12T00:12:14.058776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv('../input/sorghum-id-fgvc-9/sample_submission.csv')\n\ntest_df[\"image_path\"] = test_df[\"filename\"].apply(get_image_path, dir='../input/sorghum-id-fgvc-9/test/')\n\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-12T00:12:45.726629Z","iopub.execute_input":"2022-04-12T00:12:45.726918Z","iopub.status.idle":"2022-04-12T00:12:45.779247Z","shell.execute_reply.started":"2022-04-12T00:12:45.726858Z","shell.execute_reply":"2022-04-12T00:12:45.778559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset = (\n    tf.data.Dataset\n    .from_tensor_slices((test_df.image_path, test_df.filename))\n    .map(decode_image, num_parallel_calls=AUTO)                 \n    .batch(BATCH_SIZE)\n    .prefetch(AUTO)\n)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T00:12:14.063543Z","iopub.status.idle":"2022-04-12T00:12:14.064273Z","shell.execute_reply.started":"2022-04-12T00:12:14.064035Z","shell.execute_reply":"2022-04-12T00:12:14.06406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"N_TEST_SAMPLES = len(glob.glob('../input/sorghum-id-fgvc-9/test/*png'))\nTEST_EMBEDDINGS = np.zeros(shape=[N_TEST_SAMPLES, N_CULTIVAR], dtype=np.float32)\nTEST_EMBEDDINGS_IDS = np.empty(shape=[N_TEST_SAMPLES], dtype=object)\n\ntotal = math.ceil(N_TEST_SAMPLES / BATCH_SIZE)\noffset = 0\n\nwith strategy.scope():\n    for idx, (images, image_ids) in enumerate(tqdm(test_dataset, total=total)):\n        start = offset\n        end = offset + len(images)\n        TEST_EMBEDDINGS[start:end] = predict_on_batch(images)\n        TEST_EMBEDDINGS_IDS[start:end] = image_ids.numpy().astype(str)\n        offset += len(images)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T00:12:14.065388Z","iopub.status.idle":"2022-04-12T00:12:14.066217Z","shell.execute_reply.started":"2022-04-12T00:12:14.065981Z","shell.execute_reply":"2022-04-12T00:12:14.066006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'TEST_EMBEDDINGS shape: {TEST_EMBEDDINGS.shape}, dtype: {TEST_EMBEDDINGS.dtype}')\nprint(f'TEST_EMBEDDINGS_IDS shape: {TEST_EMBEDDINGS_IDS.shape}, dtype: {TEST_EMBEDDINGS_IDS.dtype}')","metadata":{"execution":{"iopub.status.busy":"2022-04-12T00:12:14.067205Z","iopub.status.idle":"2022-04-12T00:12:14.067989Z","shell.execute_reply.started":"2022-04-12T00:12:14.067742Z","shell.execute_reply":"2022-04-12T00:12:14.067766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TEST_EMBEDDINGS_TF = tf.constant(TEST_EMBEDDINGS)\nTEST_EMBEDDINGS_IDS_TF = tf.constant(TEST_EMBEDDINGS_IDS)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T00:12:14.069038Z","iopub.status.idle":"2022-04-12T00:12:14.069828Z","shell.execute_reply.started":"2022-04-12T00:12:14.069605Z","shell.execute_reply":"2022-04-12T00:12:14.069629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del TEST_EMBEDDINGS, TEST_EMBEDDINGS_IDS\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-04-12T00:12:14.070871Z","iopub.status.idle":"2022-04-12T00:12:14.071695Z","shell.execute_reply.started":"2022-04-12T00:12:14.071456Z","shell.execute_reply":"2022-04-12T00:12:14.071481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Submission","metadata":{}},{"cell_type":"code","source":"pred = tf.argmax(TEST_EMBEDDINGS_TF ,axis=1)\npredicts = [label2ids[x] for x in pred.numpy()]\nfiles = [i.decode() for i in TEST_EMBEDDINGS_IDS_TF.numpy()]\n\nsub = pd.DataFrame(zip(files, predicts), columns=['filename', 'cultivar'])\n\nsub.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T00:12:14.072757Z","iopub.status.idle":"2022-04-12T00:12:14.073551Z","shell.execute_reply.started":"2022-04-12T00:12:14.073318Z","shell.execute_reply":"2022-04-12T00:12:14.073341Z"},"trusted":true},"execution_count":null,"outputs":[]}]}