{"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":"**2022-04-21**\n\nUpdated to include dorsal crops aka \"backfin\".\n\n---\n\nThis is the TFRecords creation pipeline for [this](https://www.kaggle.com/code/lextoumbourou/happywhale-arcface-baseline-from-0-470-to-0-804?scriptVersionId=93216274) solution.","metadata":{}},{"cell_type":"code","source":"!pip install imgsize","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:36:28.952666Z","iopub.execute_input":"2022-04-22T00:36:28.952952Z","iopub.status.idle":"2022-04-22T00:36:40.13933Z","shell.execute_reply.started":"2022-04-22T00:36:28.952923Z","shell.execute_reply":"2022-04-22T00:36:40.138422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from multiprocessing import Pool\n\nimport os, json, random, cv2\nimport numpy as np, pandas as pd\nimport matplotlib.pyplot as plt\nfrom matplotlib import patches\nimport tensorflow as tf, re, math\nfrom tqdm import tqdm\nfrom functools import partial\n\nfrom imgsize import get_size\nfrom PIL import Image\nimport PIL","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-04-22T00:36:40.141683Z","iopub.execute_input":"2022-04-22T00:36:40.142049Z","iopub.status.idle":"2022-04-22T00:36:45.391909Z","shell.execute_reply.started":"2022-04-22T00:36:40.142Z","shell.execute_reply":"2022-04-22T00:36:45.390942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def convert_to_rect(img, label, color='b', linewidth=1):\n    if isinstance(img, PIL.Image.Image):\n        width, height = img.size[0], img.size[1]\n    else:\n        height, width = img.size[0], img.size[1]\n\n    xmin, ymin, xmax, ymax  = label[0], label[1], label[2], label[3]\n    rect = patches.Rectangle((\n         xmin * width,\n         ymin * height\n    ),\n        (xmax - xmin) * width,\n        (ymax - ymin) * height,\n        linewidth=linewidth, edgecolor=color, facecolor='none'\n    )\n    return rect\n\ndef show_img_grid(df, dataset):\n    row = 3; col = 3;\n\n    plt.figure(figsize=(25,int(25*row/col)))\n    for j in range(row*col):\n        df_row = df.iloc[j]\n        img = Image.open(f'../input/happy-whale-and-dolphin/{dataset}_images/{df_row.image}')\n        plt.subplot(row,col,j+1)\n        plt.axis('off')\n        plt.imshow(img)\n        ax = plt.gca()\n        if 'xmin' in df:\n            ax.add_patch(convert_to_rect(img, [df_row.xmin, df_row.ymin, df_row.xmax, df_row.ymax]))\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:36:45.394547Z","iopub.execute_input":"2022-04-22T00:36:45.394801Z","iopub.status.idle":"2022-04-22T00:36:45.406565Z","shell.execute_reply.started":"2022-04-22T00:36:45.394773Z","shell.execute_reply":"2022-04-22T00:36:45.405904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Goal of this notebook is to generate TFRecords but with each image's largest side no larger than 512.\n\nI will use this for fast prototyping, particularly on GPUs.\n\nThe goal is to get the dataset size < 20GB.","metadata":{}},{"cell_type":"markdown","source":"# Config","metadata":{}},{"cell_type":"code","source":"MAX_IMAGE_SIDE = None","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:36:45.407995Z","iopub.execute_input":"2022-04-22T00:36:45.408674Z","iopub.status.idle":"2022-04-22T00:36:45.422931Z","shell.execute_reply.started":"2022-04-22T00:36:45.408638Z","shell.execute_reply":"2022-04-22T00:36:45.422299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Data and Clean Species","metadata":{}},{"cell_type":"code","source":"def clean_species(species):\n    return species.replace({\n        \"globis\": \"short_finned_pilot_whale\",\n        \"pilot_whale\": \"short_finned_pilot_whale\",\n        \"kiler_whale\": \"killer_whale\",\n        \"bottlenose_dolpin\": \"bottlenose_dolphin\",\n        \"beluga\": \"beluga_whale\"\n    })","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:36:45.42385Z","iopub.execute_input":"2022-04-22T00:36:45.424293Z","iopub.status.idle":"2022-04-22T00:36:45.433745Z","shell.execute_reply.started":"2022-04-22T00:36:45.42426Z","shell.execute_reply":"2022-04-22T00:36:45.433028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('../input/happy-whale-and-dolphin/train.csv')\ntrain_df.species = clean_species(train_df.species)\n\ntest_df = pd.read_csv('../input/happy-whale-and-dolphin/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:36:45.436219Z","iopub.execute_input":"2022-04-22T00:36:45.437071Z","iopub.status.idle":"2022-04-22T00:36:45.629782Z","shell.execute_reply.started":"2022-04-22T00:36:45.437021Z","shell.execute_reply":"2022-04-22T00:36:45.629157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Generate 5 Folds","metadata":{}},{"cell_type":"code","source":"train_df.image","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:36:45.630844Z","iopub.execute_input":"2022-04-22T00:36:45.631174Z","iopub.status.idle":"2022-04-22T00:36:45.641347Z","shell.execute_reply.started":"2022-04-22T00:36:45.631145Z","shell.execute_reply":"2022-04-22T00:36:45.640464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import StratifiedKFold\n\ntrain_df['fold'] = -1\nskf = StratifiedKFold(n_splits=10)\nfor i, (train_index, val_index) in enumerate(skf.split(train_df.index, train_df.individual_id)):\n    train_df.loc[val_index, 'fold'] = i","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:36:45.642769Z","iopub.execute_input":"2022-04-22T00:36:45.643104Z","iopub.status.idle":"2022-04-22T00:36:47.169172Z","shell.execute_reply.started":"2022-04-22T00:36:45.643059Z","shell.execute_reply":"2022-04-22T00:36:47.168409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.fold.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:36:47.170343Z","iopub.execute_input":"2022-04-22T00:36:47.17061Z","iopub.status.idle":"2022-04-22T00:36:47.181425Z","shell.execute_reply.started":"2022-04-22T00:36:47.170579Z","shell.execute_reply":"2022-04-22T00:36:47.18065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['split'] = test_df.index%10\n\ntrain_df_dupes = train_df[['individual_id', 'species']].drop_duplicates()\nindid_2_species = {}\nfor idx, row in train_df_dupes.iterrows():\n    indid_2_species[row.individual_id] = row.species\n\ntrain_df['species'] = train_df.species.astype(\"category\")\nid_2_species = train_df.species.cat.categories\nspecies_map = {cat: i for i, cat in enumerate(train_df.species.cat.categories)}","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:36:47.185155Z","iopub.execute_input":"2022-04-22T00:36:47.18571Z","iopub.status.idle":"2022-04-22T00:36:48.227379Z","shell.execute_reply.started":"2022-04-22T00:36:47.185665Z","shell.execute_reply":"2022-04-22T00:36:48.226407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id_2_species","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:36:48.228673Z","iopub.execute_input":"2022-04-22T00:36:48.228909Z","iopub.status.idle":"2022-04-22T00:36:48.235397Z","shell.execute_reply.started":"2022-04-22T00:36:48.228881Z","shell.execute_reply":"2022-04-22T00:36:48.234489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"species_map","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:36:48.237157Z","iopub.execute_input":"2022-04-22T00:36:48.237486Z","iopub.status.idle":"2022-04-22T00:36:48.247972Z","shell.execute_reply.started":"2022-04-22T00:36:48.237432Z","shell.execute_reply":"2022-04-22T00:36:48.247205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open('species.json', 'w') as fh:\n    json.dump(species_map, fh)","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:36:48.249578Z","iopub.execute_input":"2022-04-22T00:36:48.249819Z","iopub.status.idle":"2022-04-22T00:36:48.259128Z","shell.execute_reply.started":"2022-04-22T00:36:48.24979Z","shell.execute_reply":"2022-04-22T00:36:48.258543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['species'] = train_df.species.cat.codes","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:36:48.260292Z","iopub.execute_input":"2022-04-22T00:36:48.261067Z","iopub.status.idle":"2022-04-22T00:36:48.26958Z","shell.execute_reply.started":"2022-04-22T00:36:48.261021Z","shell.execute_reply":"2022-04-22T00:36:48.26892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:36:48.270634Z","iopub.execute_input":"2022-04-22T00:36:48.271018Z","iopub.status.idle":"2022-04-22T00:36:48.289843Z","shell.execute_reply.started":"2022-04-22T00:36:48.27088Z","shell.execute_reply":"2022-04-22T00:36:48.289224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"copy = train_df.copy()\ncopy['species'] = copy.species.apply(lambda specie: id_2_species[specie] )\ncopy.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:36:48.290853Z","iopub.execute_input":"2022-04-22T00:36:48.291167Z","iopub.status.idle":"2022-04-22T00:36:48.364102Z","shell.execute_reply.started":"2022-04-22T00:36:48.291138Z","shell.execute_reply":"2022-04-22T00:36:48.363489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.individual_id = train_df.individual_id.astype('category')\nid_2_individual = train_df.individual_id.cat.categories","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:36:48.365371Z","iopub.execute_input":"2022-04-22T00:36:48.366148Z","iopub.status.idle":"2022-04-22T00:36:48.405296Z","shell.execute_reply.started":"2022-04-22T00:36:48.3661Z","shell.execute_reply":"2022-04-22T00:36:48.40443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id_2_individual","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:36:48.406569Z","iopub.execute_input":"2022-04-22T00:36:48.406909Z","iopub.status.idle":"2022-04-22T00:36:48.414328Z","shell.execute_reply.started":"2022-04-22T00:36:48.406867Z","shell.execute_reply":"2022-04-22T00:36:48.41374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"individual_ids_map = {cat: i for i, cat in enumerate(train_df.individual_id.cat.categories)}","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:36:48.415258Z","iopub.execute_input":"2022-04-22T00:36:48.416072Z","iopub.status.idle":"2022-04-22T00:36:48.432375Z","shell.execute_reply.started":"2022-04-22T00:36:48.416036Z","shell.execute_reply":"2022-04-22T00:36:48.431589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open('individual_ids.json', 'w') as fh:\n    json.dump(individual_ids_map, fh)","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:36:48.434219Z","iopub.execute_input":"2022-04-22T00:36:48.434497Z","iopub.status.idle":"2022-04-22T00:36:48.477531Z","shell.execute_reply.started":"2022-04-22T00:36:48.434467Z","shell.execute_reply":"2022-04-22T00:36:48.476775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.individual_id = train_df.individual_id.cat.codes","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:36:48.47855Z","iopub.execute_input":"2022-04-22T00:36:48.479269Z","iopub.status.idle":"2022-04-22T00:36:48.484405Z","shell.execute_reply.started":"2022-04-22T00:36:48.479222Z","shell.execute_reply":"2022-04-22T00:36:48.483468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:36:48.485642Z","iopub.execute_input":"2022-04-22T00:36:48.486089Z","iopub.status.idle":"2022-04-22T00:36:48.501465Z","shell.execute_reply.started":"2022-04-22T00:36:48.486042Z","shell.execute_reply":"2022-04-22T00:36:48.500621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Get Image Stats","metadata":{}},{"cell_type":"markdown","source":"In order to be able to easily convert from one bounding box format to another, I'm caching the width and height of each of the images.","metadata":{}},{"cell_type":"code","source":"def _get_image_size(row, dataset):\n    idx, row = row\n    return get_size(open(f'../input/happy-whale-and-dolphin/{dataset}_images/{row.image}', 'rb'))\n\n\ndef get_img_sizes(df, dataset):\n    with Pool(8) as p:\n        return list(tqdm(p.imap(partial(_get_image_size, dataset=dataset), df.iterrows()), total=len(df)))","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:36:48.503165Z","iopub.execute_input":"2022-04-22T00:36:48.503668Z","iopub.status.idle":"2022-04-22T00:36:48.512621Z","shell.execute_reply.started":"2022-04-22T00:36:48.503624Z","shell.execute_reply":"2022-04-22T00:36:48.511784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sizes = get_img_sizes(train_df, 'train')\ntrain_df['width'] = [s[0] for s in sizes]\ntrain_df['height'] = [s[1] for s in sizes]","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:36:48.513926Z","iopub.execute_input":"2022-04-22T00:36:48.514156Z","iopub.status.idle":"2022-04-22T00:38:33.700849Z","shell.execute_reply.started":"2022-04-22T00:36:48.514119Z","shell.execute_reply":"2022-04-22T00:38:33.699805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sizes = get_img_sizes(test_df, 'test')\ntest_df['width'] = [s[0] for s in sizes]\ntest_df['height'] = [s[1] for s in sizes]","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:38:33.702882Z","iopub.execute_input":"2022-04-22T00:38:33.703319Z","iopub.status.idle":"2022-04-22T00:39:26.777507Z","shell.execute_reply.started":"2022-04-22T00:38:33.703278Z","shell.execute_reply":"2022-04-22T00:39:26.776278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head(), test_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:39:26.779532Z","iopub.execute_input":"2022-04-22T00:39:26.779786Z","iopub.status.idle":"2022-04-22T00:39:26.800036Z","shell.execute_reply.started":"2022-04-22T00:39:26.779754Z","shell.execute_reply":"2022-04-22T00:39:26.799016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# TokenCut","metadata":{}},{"cell_type":"markdown","source":"These bounding box predictions where trained using TokenCut using ViTBase.\n\nhttps://www.kaggle.com/lextoumbourou/happywhale-tokencut-generate-vit-base","metadata":{}},{"cell_type":"code","source":"train_vitbase_df = pd.read_csv('../input/happywhale-tokencut-all-bbs/train_vit_base.csv').drop('Unnamed: 0', axis=1)\ntest_vitbase_df = pd.read_csv('../input/happywhale-tokencut-all-bbs/test_vit_base.csv').drop('Unnamed: 0', axis=1)\n\ntrain_vit_small_df = pd.read_csv('../input/happywhale-tokencut-all-bbs/train_vit_small.csv').drop('Unnamed: 0', axis=1)\ntest_vit_small_df = pd.read_csv('../input/happywhale-tokencut-all-bbs/test_vit_small.csv').drop('Unnamed: 0', axis=1)\n\ntrain_moco_vit_base = pd.read_csv('../input/happywhale-tokencut-all-bbs/train_moco_vit_base.csv').drop('Unnamed: 0', axis=1)\ntest_moco_vit_base = pd.read_csv('../input/happywhale-tokencut-all-bbs/test_moco_vit_base.csv').drop('Unnamed: 0', axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:39:26.80209Z","iopub.execute_input":"2022-04-22T00:39:26.803053Z","iopub.status.idle":"2022-04-22T00:39:27.396023Z","shell.execute_reply.started":"2022-04-22T00:39:26.803015Z","shell.execute_reply":"2022-04-22T00:39:27.395138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_img_grid(train_vitbase_df, 'train')","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:39:27.403978Z","iopub.execute_input":"2022-04-22T00:39:27.404244Z","iopub.status.idle":"2022-04-22T00:39:34.028126Z","shell.execute_reply.started":"2022-04-22T00:39:27.404211Z","shell.execute_reply":"2022-04-22T00:39:34.027412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_img_grid(test_vitbase_df, 'test')","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:39:34.029352Z","iopub.execute_input":"2022-04-22T00:39:34.029826Z","iopub.status.idle":"2022-04-22T00:39:41.609408Z","shell.execute_reply.started":"2022-04-22T00:39:34.029775Z","shell.execute_reply":"2022-04-22T00:39:41.608468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import math","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:39:41.610684Z","iopub.execute_input":"2022-04-22T00:39:41.611039Z","iopub.status.idle":"2022-04-22T00:39:41.61437Z","shell.execute_reply.started":"2022-04-22T00:39:41.611007Z","shell.execute_reply":"2022-04-22T00:39:41.613834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def columns_to_string(df):\n    return df.apply(lambda row: '' if math.isnan(row.xmin) else (f'{row.xmin} {row.ymin} {row.xmax} {row.ymax}'), axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:39:41.615246Z","iopub.execute_input":"2022-04-22T00:39:41.615758Z","iopub.status.idle":"2022-04-22T00:39:41.626879Z","shell.execute_reply.started":"2022-04-22T00:39:41.615723Z","shell.execute_reply":"2022-04-22T00:39:41.626173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_vitbase_df['tc_vitbase'] = columns_to_string(train_vitbase_df)\ntest_vitbase_df['tc_vitbase'] = columns_to_string(test_vitbase_df)\n\ntrain_vit_small_df['tc_vitsmall'] = columns_to_string(train_vit_small_df)\ntest_vit_small_df['tc_vitsmall'] = columns_to_string(test_vit_small_df)\n\ntrain_moco_vit_base['tc_mocovit'] = columns_to_string(train_moco_vit_base)\ntest_moco_vit_base['tc_mocovit'] = columns_to_string(test_moco_vit_base)","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:39:41.62788Z","iopub.execute_input":"2022-04-22T00:39:41.628596Z","iopub.status.idle":"2022-04-22T00:39:57.053549Z","shell.execute_reply.started":"2022-04-22T00:39:41.628559Z","shell.execute_reply":"2022-04-22T00:39:57.052591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = train_df.merge(train_vitbase_df[['image', 'tc_vitbase']], how='left', on='image')\ntest_df = test_df.merge(test_vitbase_df[['image', 'tc_vitbase']], how='left', on='image')\n\ntrain_df = train_df.merge(train_vit_small_df[['image', 'tc_vitsmall']], how='left', on='image')\ntest_df = test_df.merge(test_vit_small_df[['image', 'tc_vitsmall']], how='left', on='image')\n\ntrain_df = train_df.merge(train_moco_vit_base[['image', 'tc_mocovit']], how='left', on='image')\ntest_df = test_df.merge(test_moco_vit_base[['image', 'tc_mocovit']], how='left', on='image')","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:39:57.054954Z","iopub.execute_input":"2022-04-22T00:39:57.055377Z","iopub.status.idle":"2022-04-22T00:39:57.389793Z","shell.execute_reply.started":"2022-04-22T00:39:57.055314Z","shell.execute_reply":"2022-04-22T00:39:57.388808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:39:57.391075Z","iopub.execute_input":"2022-04-22T00:39:57.391325Z","iopub.status.idle":"2022-04-22T00:39:57.410622Z","shell.execute_reply.started":"2022-04-22T00:39:57.391293Z","shell.execute_reply":"2022-04-22T00:39:57.409534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:39:57.412049Z","iopub.execute_input":"2022-04-22T00:39:57.412361Z","iopub.status.idle":"2022-04-22T00:39:57.439582Z","shell.execute_reply.started":"2022-04-22T00:39:57.412316Z","shell.execute_reply":"2022-04-22T00:39:57.437161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# My Annotations\n\nI did 490 crops by hand. I found all the images which didn't have Dietic or Yolo + some where they disagreed.\n\nThis now ensures that the train set now has 100% bounding box coverage.","metadata":{}},{"cell_type":"code","source":"extra_anno = pd.read_csv('../input/happywhaleextraannotations/extra_annotations.csv')\nextra_anno = extra_anno.rename(columns={'filename': 'image'})\nextra_anno = extra_anno.merge(train_df, on='image')\n\n# Denorm to make life easier when resizing\nextra_anno.xmin = extra_anno.xmin / extra_anno.width\nextra_anno.ymin = extra_anno.ymin / extra_anno.height\nextra_anno.xmax = extra_anno.xmax / extra_anno.width\nextra_anno.ymax = extra_anno.ymax / extra_anno.height","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:39:57.441314Z","iopub.execute_input":"2022-04-22T00:39:57.441957Z","iopub.status.idle":"2022-04-22T00:39:57.516159Z","shell.execute_reply.started":"2022-04-22T00:39:57.441898Z","shell.execute_reply":"2022-04-22T00:39:57.515406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_img_grid(extra_anno, 'train')","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:39:57.517636Z","iopub.execute_input":"2022-04-22T00:39:57.517987Z","iopub.status.idle":"2022-04-22T00:40:05.938583Z","shell.execute_reply.started":"2022-04-22T00:39:57.517943Z","shell.execute_reply":"2022-04-22T00:40:05.937509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"str_rows = extra_anno.apply(lambda row: (f'{row.xmin} {row.ymin} {row.xmax} {row.ymax}'), axis=1)\nextra_anno['my_box'] = str_rows\ntrain_df = train_df.merge(extra_anno[['image', 'my_box']], how='left', on='image')\ntrain_df.loc[train_df.my_box.isna(), ['my_box']] = ''","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:40:05.939861Z","iopub.execute_input":"2022-04-22T00:40:05.940111Z","iopub.status.idle":"2022-04-22T00:40:06.018446Z","shell.execute_reply.started":"2022-04-22T00:40:05.940081Z","shell.execute_reply":"2022-04-22T00:40:06.01749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Yolov5\n\nLoad the [Yolov5 bounding box predictions](https://www.kaggle.com/awsaf49/happywhale-cropped-dataset-yolov5) created by [awsaf49](https://www.kaggle.com/awsaf49).","metadata":{}},{"cell_type":"code","source":"yolo5_train_df = pd.read_csv('../input/happywhale-cropped-dataset-yolov5/train.csv')\nyolo5_test_df = pd.read_csv('../input/happywhale-cropped-dataset-yolov5/test.csv')\nyolo5_train_df.bbox = yolo5_train_df.bbox.str[2:-2].str.replace(',', '')\nyolo5_test_df.bbox = yolo5_test_df.bbox.str[2:-2].str.replace(',', '')","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:40:06.01986Z","iopub.execute_input":"2022-04-22T00:40:06.020106Z","iopub.status.idle":"2022-04-22T00:40:06.947813Z","shell.execute_reply.started":"2022-04-22T00:40:06.020076Z","shell.execute_reply":"2022-04-22T00:40:06.94677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tqdm.pandas()","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:40:06.949534Z","iopub.execute_input":"2022-04-22T00:40:06.949873Z","iopub.status.idle":"2022-04-22T00:40:06.955563Z","shell.execute_reply.started":"2022-04-22T00:40:06.949828Z","shell.execute_reply":"2022-04-22T00:40:06.954411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def _norm_bb(row):\n    if not row.bbox:\n        return pd.Series(dict(\n        image=row.image))\n\n    xmin, ymin, xmax, ymax = row.bbox.split()\n    return pd.Series(dict(\n        image=row.image,\n        xmin=float(xmin)/row.width,\n        ymin=float(ymin)/row.height,\n        xmax=float(xmax)/row.width,\n        ymax=float(ymax)/row.height))\n\nyolo_train_norm_df = yolo5_train_df.progress_apply(_norm_bb, axis=1)\nyolo_test_norm_df = yolo5_test_df.progress_apply(_norm_bb, axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:40:06.957039Z","iopub.execute_input":"2022-04-22T00:40:06.957344Z","iopub.status.idle":"2022-04-22T00:41:02.265573Z","shell.execute_reply.started":"2022-04-22T00:40:06.957301Z","shell.execute_reply":"2022-04-22T00:41:02.264607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_img_grid(yolo_train_norm_df, 'train')","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:41:02.266769Z","iopub.execute_input":"2022-04-22T00:41:02.267Z","iopub.status.idle":"2022-04-22T00:41:09.939452Z","shell.execute_reply.started":"2022-04-22T00:41:02.266973Z","shell.execute_reply":"2022-04-22T00:41:09.938371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_img_grid(yolo_test_norm_df, 'test')","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:41:09.940782Z","iopub.execute_input":"2022-04-22T00:41:09.941039Z","iopub.status.idle":"2022-04-22T00:41:15.409973Z","shell.execute_reply.started":"2022-04-22T00:41:09.941008Z","shell.execute_reply":"2022-04-22T00:41:15.408719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"yolo_train_norm_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:41:15.411553Z","iopub.execute_input":"2022-04-22T00:41:15.411906Z","iopub.status.idle":"2022-04-22T00:41:15.42439Z","shell.execute_reply.started":"2022-04-22T00:41:15.411872Z","shell.execute_reply":"2022-04-22T00:41:15.423628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"yolo_train_norm_df['yolov5'] = columns_to_string(yolo_train_norm_df)\nyolo_test_norm_df['yolov5'] = columns_to_string(yolo_test_norm_df)","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:41:15.425481Z","iopub.execute_input":"2022-04-22T00:41:15.425741Z","iopub.status.idle":"2022-04-22T00:41:20.399453Z","shell.execute_reply.started":"2022-04-22T00:41:15.425709Z","shell.execute_reply":"2022-04-22T00:41:20.398653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = train_df.merge(yolo_train_norm_df[['image', 'yolov5']], how='left', on='image')\ntest_df = test_df.merge(yolo_test_norm_df[['image', 'yolov5']], how='left', on='image')","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:41:20.401262Z","iopub.execute_input":"2022-04-22T00:41:20.401595Z","iopub.status.idle":"2022-04-22T00:41:20.510535Z","shell.execute_reply.started":"2022-04-22T00:41:20.40155Z","shell.execute_reply":"2022-04-22T00:41:20.509848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head(2)","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:41:20.511818Z","iopub.execute_input":"2022-04-22T00:41:20.512592Z","iopub.status.idle":"2022-04-22T00:41:20.52632Z","shell.execute_reply.started":"2022-04-22T00:41:20.51255Z","shell.execute_reply":"2022-04-22T00:41:20.525375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head(2)","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:41:20.527515Z","iopub.execute_input":"2022-04-22T00:41:20.527828Z","iopub.status.idle":"2022-04-22T00:41:20.551616Z","shell.execute_reply.started":"2022-04-22T00:41:20.527786Z","shell.execute_reply":"2022-04-22T00:41:20.550933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Detic\n\nLoad the [Detic bounding box predictions] created by [phalanx](https://www.kaggle.com/phalanx).","metadata":{}},{"cell_type":"code","source":"detic_train_df = pd.read_csv('../input/whale2-cropped-dataset/train2.csv')\ndetic_test_df = pd.read_csv('../input/whale2-cropped-dataset/test2.csv')\n\ndetic_train_df = detic_train_df.merge(train_df[['image', 'width', 'height']], how='left')\ndetic_test_df = detic_test_df.merge(test_df[['image', 'width', 'height']], how='left')\n\ndetic_train_df.loc[detic_train_df.box.isna(), 'box'] = ''\ndetic_test_df.loc[detic_test_df.box.isna(), 'box'] = ''\n\ndef _norm_bb(row):\n    if not row.box:\n        return pd.Series(dict(\n        image=row.image))\n\n    xmin, ymin, xmax, ymax = row.box.split()\n    return pd.Series(dict(\n        image=row.image,\n        xmin=float(xmin)/row.width,\n        ymin=float(ymin)/row.height,\n        xmax=float(xmax)/row.width,\n        ymax=float(ymax)/row.height))\n\ndetic_train_norm_df = detic_train_df.progress_apply(_norm_bb, axis=1)\ndetic_test_norm_df = detic_test_df.progress_apply(_norm_bb, axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:41:20.553138Z","iopub.execute_input":"2022-04-22T00:41:20.554199Z","iopub.status.idle":"2022-04-22T00:42:04.99633Z","shell.execute_reply.started":"2022-04-22T00:41:20.554156Z","shell.execute_reply":"2022-04-22T00:42:04.995374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_img_grid(detic_train_norm_df, 'train')","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:42:04.998123Z","iopub.execute_input":"2022-04-22T00:42:04.998484Z","iopub.status.idle":"2022-04-22T00:42:12.30588Z","shell.execute_reply.started":"2022-04-22T00:42:04.998415Z","shell.execute_reply":"2022-04-22T00:42:12.30491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_img_grid(detic_test_norm_df, 'test')","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:42:12.307176Z","iopub.execute_input":"2022-04-22T00:42:12.30743Z","iopub.status.idle":"2022-04-22T00:42:17.586254Z","shell.execute_reply.started":"2022-04-22T00:42:12.307398Z","shell.execute_reply":"2022-04-22T00:42:17.581198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['detic'] = columns_to_string(detic_train_norm_df)\ntest_df['detic'] = columns_to_string(detic_test_norm_df)","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:42:17.587618Z","iopub.execute_input":"2022-04-22T00:42:17.587866Z","iopub.status.idle":"2022-04-22T00:42:21.587272Z","shell.execute_reply.started":"2022-04-22T00:42:17.587837Z","shell.execute_reply":"2022-04-22T00:42:21.586527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Full Body","metadata":{}},{"cell_type":"code","source":"fullbody_train = pd.read_csv('../input/fullbodywhaleannotations/fullbody_train.csv')\nfullbody_test = pd.read_csv('../input/fullbodywhaleannotations/fullbody_test.csv')\n\nfullbody_train.bbox = fullbody_train.bbox.str[2:-2]\nfullbody_test.bbox = fullbody_test.bbox.str[2:-2]","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:42:21.588625Z","iopub.execute_input":"2022-04-22T00:42:21.588893Z","iopub.status.idle":"2022-04-22T00:42:22.315396Z","shell.execute_reply.started":"2022-04-22T00:42:21.58886Z","shell.execute_reply":"2022-04-22T00:42:22.314658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(fullbody_train)","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:42:22.316942Z","iopub.execute_input":"2022-04-22T00:42:22.317451Z","iopub.status.idle":"2022-04-22T00:42:22.324239Z","shell.execute_reply.started":"2022-04-22T00:42:22.317387Z","shell.execute_reply":"2022-04-22T00:42:22.32338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def _norm_bb(row):\n    xmin, ymin, xmax, ymax = row.bbox.split()\n    return pd.Series(dict(\n        image=row.image,\n        xmin=float(xmin)/row.width,\n        ymin=float(ymin)/row.height,\n        xmax=float(xmax)/row.width,\n        ymax=float(ymax)/row.height))","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:42:22.325942Z","iopub.execute_input":"2022-04-22T00:42:22.326594Z","iopub.status.idle":"2022-04-22T00:42:22.336229Z","shell.execute_reply.started":"2022-04-22T00:42:22.326546Z","shell.execute_reply":"2022-04-22T00:42:22.335448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fullbody_train_norm = fullbody_train.progress_apply(_norm_bb, axis=1)\nfullbody_test_norm = fullbody_test.progress_apply(_norm_bb, axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:42:22.337978Z","iopub.execute_input":"2022-04-22T00:42:22.338321Z","iopub.status.idle":"2022-04-22T00:42:56.76177Z","shell.execute_reply.started":"2022-04-22T00:42:22.338275Z","shell.execute_reply":"2022-04-22T00:42:56.760666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_img_grid(fullbody_train_norm, 'train')","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:42:56.763322Z","iopub.execute_input":"2022-04-22T00:42:56.763611Z","iopub.status.idle":"2022-04-22T00:43:04.083594Z","shell.execute_reply.started":"2022-04-22T00:42:56.763578Z","shell.execute_reply":"2022-04-22T00:43:04.082737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_img_grid(fullbody_test_norm, 'test')","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:43:04.084938Z","iopub.execute_input":"2022-04-22T00:43:04.085849Z","iopub.status.idle":"2022-04-22T00:43:09.291749Z","shell.execute_reply.started":"2022-04-22T00:43:04.085797Z","shell.execute_reply":"2022-04-22T00:43:09.290798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['fullbody'] = columns_to_string(fullbody_train_norm)\ntest_df['fullbody'] = columns_to_string(fullbody_test_norm)","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:43:09.293383Z","iopub.execute_input":"2022-04-22T00:43:09.293864Z","iopub.status.idle":"2022-04-22T00:43:14.255899Z","shell.execute_reply.started":"2022-04-22T00:43:09.29382Z","shell.execute_reply":"2022-04-22T00:43:14.255014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:43:14.257137Z","iopub.execute_input":"2022-04-22T00:43:14.257565Z","iopub.status.idle":"2022-04-22T00:43:14.27671Z","shell.execute_reply.started":"2022-04-22T00:43:14.257525Z","shell.execute_reply":"2022-04-22T00:43:14.275706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Dorsal (\"Backfin\") crops","metadata":{}},{"cell_type":"code","source":"dorsal_train = pd.read_csv('../input/backfin-detection-with-yolov5/train.csv')\ndorsal_test = pd.read_csv('../input/backfin-detection-with-yolov5/test.csv')\n\ndorsal_train.bbox = dorsal_train.bbox.str[2:-2]\ndorsal_test.bbox = dorsal_test.bbox.str[2:-2]","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:43:14.27843Z","iopub.execute_input":"2022-04-22T00:43:14.278846Z","iopub.status.idle":"2022-04-22T00:43:15.013325Z","shell.execute_reply.started":"2022-04-22T00:43:14.2788Z","shell.execute_reply":"2022-04-22T00:43:15.012201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(dorsal_train), len(dorsal_test)","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:43:15.01466Z","iopub.execute_input":"2022-04-22T00:43:15.014916Z","iopub.status.idle":"2022-04-22T00:43:15.021346Z","shell.execute_reply.started":"2022-04-22T00:43:15.014884Z","shell.execute_reply":"2022-04-22T00:43:15.020315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def _norm_bb(row):\n    if type(row.bbox) != str:\n        xmin, ymin, xmax, ymax = -1, -1, -1, -1\n    else:\n        xmin, ymin, xmax, ymax = row.bbox.split()\n    return pd.Series(dict(\n        image=row.image,\n        xmin=float(xmin)/row.width,\n        ymin=float(ymin)/row.height,\n        xmax=float(xmax)/row.width,\n        ymax=float(ymax)/row.height))","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:43:15.022652Z","iopub.execute_input":"2022-04-22T00:43:15.02297Z","iopub.status.idle":"2022-04-22T00:43:15.033969Z","shell.execute_reply.started":"2022-04-22T00:43:15.022933Z","shell.execute_reply":"2022-04-22T00:43:15.033221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dorsal_train_norm = dorsal_train.progress_apply(_norm_bb, axis=1)\ndorsal_test_norm = dorsal_test.progress_apply(_norm_bb, axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:43:15.03573Z","iopub.execute_input":"2022-04-22T00:43:15.037644Z","iopub.status.idle":"2022-04-22T00:43:51.020139Z","shell.execute_reply.started":"2022-04-22T00:43:15.03759Z","shell.execute_reply":"2022-04-22T00:43:51.019161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_img_grid(dorsal_train_norm, 'train')","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:43:51.021494Z","iopub.execute_input":"2022-04-22T00:43:51.021934Z","iopub.status.idle":"2022-04-22T00:43:58.507809Z","shell.execute_reply.started":"2022-04-22T00:43:51.021898Z","shell.execute_reply":"2022-04-22T00:43:58.506647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_img_grid(dorsal_test_norm, 'test')","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:43:58.509176Z","iopub.execute_input":"2022-04-22T00:43:58.509487Z","iopub.status.idle":"2022-04-22T00:44:03.770217Z","shell.execute_reply.started":"2022-04-22T00:43:58.509422Z","shell.execute_reply":"2022-04-22T00:44:03.768563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['dorsal'] = columns_to_string(dorsal_train_norm)\ntest_df['dorsal'] = columns_to_string(dorsal_test_norm)","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:44:03.771612Z","iopub.execute_input":"2022-04-22T00:44:03.771864Z","iopub.status.idle":"2022-04-22T00:44:08.752911Z","shell.execute_reply.started":"2022-04-22T00:44:03.771832Z","shell.execute_reply":"2022-04-22T00:44:08.751791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:44:08.754256Z","iopub.execute_input":"2022-04-22T00:44:08.754572Z","iopub.status.idle":"2022-04-22T00:44:08.774235Z","shell.execute_reply.started":"2022-04-22T00:44:08.754537Z","shell.execute_reply":"2022-04-22T00:44:08.773489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Pseudo Labels","metadata":{}},{"cell_type":"code","source":"pseudo_labels_df = pd.read_csv('../input/whale-pseudo-labels/pseudo_labels.csv')","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:44:08.776244Z","iopub.execute_input":"2022-04-22T00:44:08.776549Z","iopub.status.idle":"2022-04-22T00:44:08.805934Z","shell.execute_reply.started":"2022-04-22T00:44:08.776512Z","shell.execute_reply":"2022-04-22T00:44:08.804661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pseudo_labels_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:44:08.807583Z","iopub.execute_input":"2022-04-22T00:44:08.807847Z","iopub.status.idle":"2022-04-22T00:44:08.820224Z","shell.execute_reply.started":"2022-04-22T00:44:08.807818Z","shell.execute_reply":"2022-04-22T00:44:08.819549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pseudo_labels_df = pseudo_labels_df.rename(columns={'target': 'individual_id'})","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:44:08.821343Z","iopub.execute_input":"2022-04-22T00:44:08.822102Z","iopub.status.idle":"2022-04-22T00:44:08.834901Z","shell.execute_reply.started":"2022-04-22T00:44:08.822054Z","shell.execute_reply":"2022-04-22T00:44:08.833939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pseudo_labels_df['species'] = pseudo_labels_df.individual_id.map(lambda val: indid_2_species[val])","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:44:08.836228Z","iopub.execute_input":"2022-04-22T00:44:08.837117Z","iopub.status.idle":"2022-04-22T00:44:08.855774Z","shell.execute_reply.started":"2022-04-22T00:44:08.837068Z","shell.execute_reply":"2022-04-22T00:44:08.854763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pseudo_labels_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:44:08.857046Z","iopub.execute_input":"2022-04-22T00:44:08.857605Z","iopub.status.idle":"2022-04-22T00:44:08.872254Z","shell.execute_reply.started":"2022-04-22T00:44:08.857549Z","shell.execute_reply":"2022-04-22T00:44:08.871641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"orig_train_df = pd.read_csv('../input/happy-whale-and-dolphin/train.csv')\n\nrow = 1\ncol = 4\n\nfor test_img in range(10):\n    plt.figure(figsize=(25,int(25*row/col)))\n    df_row = pseudo_labels_df.iloc[test_img]\n\n    plt.subplot(row, col, 1)\n\n    img = Image.open(f'../input/happy-whale-and-dolphin/test_images/{df_row.image}')\n    plt.imshow(img)\n\n    individual_rows = orig_train_df[orig_train_df.individual_id == df_row.individual_id]\n\n    for i, (idx, _row) in enumerate(individual_rows.iterrows()):\n        plt.subplot(row, col, i + 1)\n\n        img = Image.open(f'../input/happy-whale-and-dolphin/train_images/{_row.image}')\n        plt.imshow(img)\n        if i >= 3:\n            break\n\n    plt.axis('off')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:44:08.873338Z","iopub.execute_input":"2022-04-22T00:44:08.873935Z","iopub.status.idle":"2022-04-22T00:44:34.502198Z","shell.execute_reply.started":"2022-04-22T00:44:08.8739Z","shell.execute_reply":"2022-04-22T00:44:34.501529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pseudo_labels_df.individual_id = pseudo_labels_df.individual_id.apply(lambda _id: individual_ids_map[_id])\npseudo_labels_df.species = pseudo_labels_df.species.apply(lambda spec: species_map[spec])","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:44:34.503301Z","iopub.execute_input":"2022-04-22T00:44:34.504106Z","iopub.status.idle":"2022-04-22T00:44:34.528691Z","shell.execute_reply.started":"2022-04-22T00:44:34.504064Z","shell.execute_reply":"2022-04-22T00:44:34.527529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pseudo_labels_df = pseudo_labels_df.merge(test_df[['image', 'width', 'height', 'tc_vitbase', 'tc_vitsmall', 'tc_mocovit', 'yolov5', 'detic', 'fullbody', 'dorsal']], on='image', how='left')","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:44:34.530146Z","iopub.execute_input":"2022-04-22T00:44:34.530453Z","iopub.status.idle":"2022-04-22T00:44:34.605512Z","shell.execute_reply.started":"2022-04-22T00:44:34.530398Z","shell.execute_reply":"2022-04-22T00:44:34.604402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Replace missing examples","metadata":{}},{"cell_type":"markdown","source":"## Yolov5","metadata":{}},{"cell_type":"markdown","source":"Any Yolo examples that are missing, should be set to my hand annotated dataset.","metadata":{}},{"cell_type":"code","source":"print('Train Yolo missing before:', len(train_df.loc[train_df.yolov5 == '']))\ntrain_df.loc[train_df.yolov5 == '', 'yolov5'] = train_df.my_box\nprint('Yolo missing after:', len(train_df.loc[train_df.yolov5 == '']))","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:44:34.611759Z","iopub.execute_input":"2022-04-22T00:44:34.612305Z","iopub.status.idle":"2022-04-22T00:44:34.677036Z","shell.execute_reply.started":"2022-04-22T00:44:34.61225Z","shell.execute_reply":"2022-04-22T00:44:34.675812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Then TokenCut.","metadata":{}},{"cell_type":"code","source":"print('Yolo missing before:', len(train_df.loc[train_df.yolov5 == '']))\ntrain_df.loc[train_df.yolov5 == '', 'yolov5'] = train_df.tc_vitbase\nprint('Yolo missing after:', len(train_df.loc[train_df.yolov5 == '']))\n\nprint('Test Yolo missing before:', len(test_df.loc[test_df.yolov5 == '']))\ntest_df.loc[test_df.yolov5 == '', 'yolov5'] = test_df.tc_vitbase\nprint('Test Yolo missing after:', len(test_df.loc[test_df.yolov5 == '']))\n\nprint('Pseudo Yolo missing before:', len(pseudo_labels_df.loc[pseudo_labels_df.yolov5 == '']))\npseudo_labels_df.loc[pseudo_labels_df.yolov5 == '', 'yolov5'] = pseudo_labels_df.tc_vitbase\nprint('Pseudo Yolo missing after:', len(pseudo_labels_df.loc[pseudo_labels_df.yolov5 == '']))","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:44:34.678493Z","iopub.execute_input":"2022-04-22T00:44:34.678819Z","iopub.status.idle":"2022-04-22T00:44:34.762937Z","shell.execute_reply.started":"2022-04-22T00:44:34.678774Z","shell.execute_reply":"2022-04-22T00:44:34.762175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Detic","metadata":{}},{"cell_type":"code","source":"print('Detic missing before:', len(train_df.loc[train_df.detic == '']))\ntrain_df.loc[train_df.detic == '', 'detic'] = train_df.my_box\nprint('Detic missing after:', len(train_df.loc[train_df.detic == '']))","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:44:34.764165Z","iopub.execute_input":"2022-04-22T00:44:34.764956Z","iopub.status.idle":"2022-04-22T00:44:34.816791Z","shell.execute_reply.started":"2022-04-22T00:44:34.764895Z","shell.execute_reply":"2022-04-22T00:44:34.816012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Then TokenCut.","metadata":{}},{"cell_type":"code","source":"print('Detic missing before:', len(train_df.loc[train_df.detic == '']))\ntrain_df.loc[train_df.detic == '', 'detic'] = train_df.tc_vitbase\nprint('Detic missing after:', len(train_df.loc[train_df.detic == '']))\n\nprint('Test Detic missing before:', len(test_df.loc[test_df.detic == '']))\ntest_df.loc[test_df.detic == '', 'detic'] = test_df.tc_vitbase\nprint('Test Detic missing after:', len(test_df.loc[test_df.detic == '']))\n\nprint('Pseudo Detic missing before:', len(pseudo_labels_df.loc[pseudo_labels_df.detic == '']))\npseudo_labels_df.loc[pseudo_labels_df.detic == '', 'detic'] = pseudo_labels_df.tc_vitbase\nprint('Pseudo Detic missing after:', len(pseudo_labels_df.loc[pseudo_labels_df.detic == '']))","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:44:34.81809Z","iopub.execute_input":"2022-04-22T00:44:34.818563Z","iopub.status.idle":"2022-04-22T00:44:34.90535Z","shell.execute_reply.started":"2022-04-22T00:44:34.81852Z","shell.execute_reply":"2022-04-22T00:44:34.904378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Setup Dataset","metadata":{}},{"cell_type":"code","source":"%%time\n\n### Create Kaggle Dataset if not exists \nDATASET_NAME = f'happywhale-tfrecords-private2'\n\n!rm -rf /tmp/{DATASET_NAME}\n\nos.makedirs(f'/tmp/{DATASET_NAME}', exist_ok=True)\n\nwith open('../input/kaggleapi/kaggle.json') as f:\n    kaggle_creds = json.load(f)\n    \nos.environ['KAGGLE_USERNAME'] = kaggle_creds['username']\nos.environ['KAGGLE_KEY'] = kaggle_creds['key']\n\n# Created interactively.\n!kaggle datasets init -p /tmp/{DATASET_NAME}\n\nwith open(f'/tmp/{DATASET_NAME}/dataset-metadata.json') as f:\n    dataset_meta = json.load(f)\n\ndataset_meta['id'] = f'lextoumbourou/{DATASET_NAME}'\ndataset_meta['title'] = DATASET_NAME\nwith open(f'/tmp/{DATASET_NAME}/dataset-metadata.json', \"w\") as outfile:\n    json.dump(dataset_meta, outfile)\n\nprint(dataset_meta)\n\n!cp /tmp/{DATASET_NAME}/dataset-metadata.json /tmp/{DATASET_NAME}/meta.json\n!ls /tmp/{DATASET_NAME}\n\n# Created interactively.\n!kaggle datasets create -p /tmp/{DATASET_NAME} \n\n# Create TFRecords","metadata":{"execution":{"iopub.status.busy":"2022-04-22T01:08:07.16166Z","iopub.execute_input":"2022-04-22T01:08:07.163595Z","iopub.status.idle":"2022-04-22T01:08:15.232443Z","shell.execute_reply.started":"2022-04-22T01:08:07.163526Z","shell.execute_reply":"2022-04-22T01:08:15.23118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_fold(fold):\n    val_df = train_df[train_df.fold==fold].reset_index(drop=True)\n    val_df['order'] = val_df.index\n    val_df['order'] = val_df.groupby('individual_id').order.rank()\n    val_total_counts = val_df.individual_id.value_counts().to_dict()\n    val_df['total_counts'] = val_df.individual_id.map(val_total_counts)\n    val_df['order'] = val_df['order']/val_df['total_counts']\n    val_df = val_df.sort_values('order',ascending=False).reset_index(drop=True)\n    val_df = val_df[['image','species','individual_id', 'width', 'height', 'yolov5', 'detic', 'tc_vitbase', 'tc_vitsmall', 'tc_mocovit', 'fullbody', 'dorsal']]\n    return val_df","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:44:34.906863Z","iopub.execute_input":"2022-04-22T00:44:34.90742Z","iopub.status.idle":"2022-04-22T00:44:34.915819Z","shell.execute_reply.started":"2022-04-22T00:44:34.907386Z","shell.execute_reply":"2022-04-22T00:44:34.914739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def _bytes_feature(value):\n  \"\"\"Returns a bytes_list from a string / byte.\"\"\"\n  if isinstance(value, type(tf.constant(0))):\n    value = value.numpy() # BytesList won't unpack a string from an EagerTensor.\n  return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))\n\ndef _float_feature(value):\n  \"\"\"Returns a float_list from a float / double.\"\"\"\n  return tf.train.Feature(float_list=tf.train.FloatList(value=[value]))\n\ndef _int64_feature(value):\n  \"\"\"Returns an int64_list from a bool / enum / int / uint.\"\"\"\n  return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))\n\ndef _bb_feature(bb):\n    \"\"\"Returns an int64_list from a bool / enum / int / uint.\"\"\"\n    return tf.train.Feature(int64_list=tf.train.Int64List(value=bb))\n\ndef serialize_example(image,image_name,target,species,yolov5_bb,detic_bb,tc_vitbase,tc_vitsmall,tc_mocovit,fullbody,dorsal):\n    feature = {\n        'image': _bytes_feature(image),\n        'image_name': _bytes_feature(image_name),\n        'target': _int64_feature(target),\n        'species': _int64_feature(species),\n        'yolov5_box': _bb_feature(yolov5_bb),\n        'detic_box': _bb_feature(detic_bb),\n        'tc_vitbase': _bb_feature(tc_vitbase),\n        'tc_vitsmall': _bb_feature(tc_vitsmall),\n        'tc_mocovit': _bb_feature(tc_mocovit),\n        'fullbody': _bb_feature(fullbody),\n        'dorsal': _bb_feature(dorsal)\n    }\n    example_proto = tf.train.Example(features=tf.train.Features(feature=feature))\n    return example_proto.SerializeToString()","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:44:34.917106Z","iopub.execute_input":"2022-04-22T00:44:34.917394Z","iopub.status.idle":"2022-04-22T00:44:34.939278Z","shell.execute_reply.started":"2022-04-22T00:44:34.91736Z","shell.execute_reply":"2022-04-22T00:44:34.938137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"def read_bbox(bbox, width, height):\n    xmin, ymin, xmax, ymax = [float(i) for i in bbox.split()]\n    return np.array([\n        int(round(xmin * width)),\n        int(round(ymin * height)),\n        int(round(xmax * width)),\n        int(round(ymax * height))\n    ])\n\n\ndef create_tf_records_from_df(df, label, dataset, fold, progress=False):\n    tfr_filename = f'/tmp/{DATASET_NAME}/happywhale-2022-{label}-{fold}-{df.shape[0]}.tfrec'\n    with tf.io.TFRecordWriter(tfr_filename) as writer:\n        it = df.iterrows()\n        if progress:\n            it = tqdm(it, total=len(df))\n        for i,row in it:\n            image_id = row.image\n            target = row.individual_id\n            species = row.species\n            image_path = f\"../input/happy-whale-and-dolphin/{dataset}_images/{image_id}\"\n            image_encoded = tf.io.read_file(image_path)\n            image_name = str.encode(image_id)\n            yolov5_bb = list(read_bbox(row.yolov5, row.width, row.height))\n            detic_bb = list(read_bbox(row.detic, row.width, row.height))\n            tc_vitbase_bb = list(read_bbox(row.tc_vitbase, row.width, row.height))\n            tc_vitsmall_bb = list(read_bbox(row.tc_vitsmall, row.width, row.height))\n            tc_mocovit_bb = list(read_bbox(row.tc_mocovit, row.width, row.height))\n            fullbody = list(read_bbox(row.fullbody, row.width, row.height))\n            dorsal = list(read_bbox(row.dorsal, row.width, row.height))\n            example = serialize_example(image_encoded,image_name,target,species,yolov5_bb,detic_bb,tc_vitbase_bb,tc_vitsmall_bb,tc_mocovit_bb,fullbody,dorsal)\n            writer.write(example)\n            \n\ndef create_tf_records(label, dataset, fold=0, progress=False):\n    df = get_fold(fold)\n    return create_tf_records_from_df(df, label, dataset, fold, progress)","metadata":{"execution":{"iopub.status.busy":"2022-04-22T00:44:34.940713Z","iopub.execute_input":"2022-04-22T00:44:34.940999Z","iopub.status.idle":"2022-04-22T00:44:34.961667Z","shell.execute_reply.started":"2022-04-22T00:44:34.940964Z","shell.execute_reply":"2022-04-22T00:44:34.960567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create_tf_records(label='train', dataset='train', fold=0, progress=True)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T11:43:13.49842Z","iopub.execute_input":"2022-04-21T11:43:13.498752Z","iopub.status.idle":"2022-04-21T11:44:30.685431Z","shell.execute_reply.started":"2022-04-21T11:43:13.498722Z","shell.execute_reply":"2022-04-21T11:44:30.684719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import joblib\n_ = joblib.Parallel(n_jobs=8)(\n        joblib.delayed(create_tf_records)(label='train', dataset='train', fold=fold) for fold in tqdm(range(10), total=10)\n    )","metadata":{"execution":{"iopub.status.busy":"2022-04-21T11:42:08.002801Z","iopub.execute_input":"2022-04-21T11:42:08.003106Z","iopub.status.idle":"2022-04-21T11:43:09.391853Z","shell.execute_reply.started":"2022-04-21T11:42:08.00307Z","shell.execute_reply":"2022-04-21T11:43:09.389382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Pseudo labels","metadata":{}},{"cell_type":"code","source":"create_tf_records_from_df(pseudo_labels_df, label='pseudo', dataset='test', fold='')","metadata":{"execution":{"iopub.status.busy":"2022-04-22T01:08:21.248476Z","iopub.execute_input":"2022-04-22T01:08:21.248846Z","iopub.status.idle":"2022-04-22T01:11:25.510621Z","shell.execute_reply.started":"2022-04-22T01:08:21.24881Z","shell.execute_reply":"2022-04-22T01:11:25.508568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_test_tf_records(fold  = 0, progress=False):\n    df = test_df[test_df.split==fold]\n    tfr_filename = f'/tmp/{DATASET_NAME}/happywhale-2022-test-{fold}-{df.shape[0]}.tfrec'\n    with tf.io.TFRecordWriter(tfr_filename) as writer:\n        it = df.iterrows()\n        if progress:\n            it = tqdm(it, total=len(df))\n        for i,row in it:\n            image_id = row.image\n            target = -1\n            species = -1\n            image_path = f\"../input/happy-whale-and-dolphin/test_images/{image_id}\"\n            image = tf.io.read_file(image_path)\n            image_encoded = tf.io.read_file(image_path)\n            image_name = str.encode(image_id)\n            yolov5_bb = list(read_bbox(row.yolov5, row.width, row.height))\n            detic_bb = list(read_bbox(row.detic, row.width, row.height))\n            tc_vitbase_bb = list(read_bbox(row.tc_vitbase, row.width, row.height))\n            tc_vitsmall_bb = list(read_bbox(row.tc_vitsmall, row.width, row.height))\n            tc_mocovit_bb = list(read_bbox(row.tc_mocovit, row.width, row.height))\n            fullbody = list(read_bbox(row.fullbody, row.width, row.height))\n            dorsal = list(read_bbox(row.dorsal, row.width, row.height))\n            example = serialize_example(image_encoded,image_name,target,species,yolov5_bb,detic_bb,tc_vitbase_bb,tc_vitsmall_bb,tc_mocovit_bb, fullbody, dorsal)\n            writer.write(example)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T11:36:34.053114Z","iopub.execute_input":"2022-04-21T11:36:34.05337Z","iopub.status.idle":"2022-04-21T11:36:34.066186Z","shell.execute_reply.started":"2022-04-21T11:36:34.053339Z","shell.execute_reply":"2022-04-21T11:36:34.065338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import joblib\n_ = joblib.Parallel(n_jobs=8)(\n        joblib.delayed(create_test_tf_records)(fold) for fold in tqdm(range(10))\n    )","metadata":{"execution":{"iopub.status.busy":"2022-04-21T11:36:34.068532Z","iopub.execute_input":"2022-04-21T11:36:34.069134Z","iopub.status.idle":"2022-04-21T11:39:41.602678Z","shell.execute_reply.started":"2022-04-21T11:36:34.069085Z","shell.execute_reply":"2022-04-21T11:39:41.597254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from datetime import datetime\nversion_name = datetime.now().strftime(\"%Y%m%d-%H%M%S\")\nprint(version_name)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T11:39:41.613148Z","iopub.execute_input":"2022-04-21T11:39:41.613469Z","iopub.status.idle":"2022-04-21T11:39:41.629964Z","shell.execute_reply.started":"2022-04-21T11:39:41.613433Z","shell.execute_reply":"2022-04-21T11:39:41.62869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Verify TFRecords","metadata":{}},{"cell_type":"markdown","source":"In this example I load some images from TFRecords and plot with the 2 bounding boxes.","metadata":{}},{"cell_type":"code","source":"from functools import partial","metadata":{"execution":{"iopub.status.busy":"2022-04-21T11:44:30.687556Z","iopub.execute_input":"2022-04-21T11:44:30.688257Z","iopub.status.idle":"2022-04-21T11:44:30.694617Z","shell.execute_reply.started":"2022-04-21T11:44:30.688203Z","shell.execute_reply":"2022-04-21T11:44:30.692682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def decode_image(image_data):\n    image = tf.image.decode_jpeg(image_data, channels = 3)\n    image = tf.cast(image, tf.float32) / 255.0  # convert image to floats in [0, 1] range\n    return image","metadata":{"execution":{"iopub.status.busy":"2022-04-21T11:44:30.696355Z","iopub.execute_input":"2022-04-21T11:44:30.696666Z","iopub.status.idle":"2022-04-21T11:44:31.80105Z","shell.execute_reply.started":"2022-04-21T11:44:30.696616Z","shell.execute_reply":"2022-04-21T11:44:31.800005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def normalise_bounding_box(box, img):\n    shape = tf.shape(img)\n    height, width = shape[0], shape[1]\n    box = tf.cast(box, tf.int32)\n    return tf.convert_to_tensor([\n        box[0] / width,\n        box[1] / height,\n        box[2] / width,\n        box[3] / height\n    ])","metadata":{"execution":{"iopub.status.busy":"2022-04-21T11:44:31.803769Z","iopub.execute_input":"2022-04-21T11:44:31.804109Z","iopub.status.idle":"2022-04-21T11:44:31.815636Z","shell.execute_reply.started":"2022-04-21T11:44:31.804066Z","shell.execute_reply":"2022-04-21T11:44:31.814604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_labeled_tfrecord(example):\n    LABELED_TFREC_FORMAT = {\n        \"image\": tf.io.FixedLenFeature([], tf.string), # tf.string means bytestring\n        \"image_name\": tf.io.FixedLenFeature([], tf.string),  # shape [] means single element\n        'target': tf.io.FixedLenFeature([], tf.int64),\n        'species': tf.io.FixedLenFeature([], tf.int64),\n        'yolov5_box': tf.io.FixedLenFeature([4], tf.int64),\n        'detic_box': tf.io.FixedLenFeature([4], tf.int64),\n        'tc_vitbase': tf.io.FixedLenFeature([4], tf.int64),\n        'tc_vitsmall': tf.io.FixedLenFeature([4], tf.int64),\n        'tc_mocovit': tf.io.FixedLenFeature([4], tf.int64),\n        'fullbody': tf.io.FixedLenFeature([4], tf.int64),\n        'dorsal': tf.io.FixedLenFeature([4], tf.int64)\n    }\n\n    example = tf.io.parse_single_example(example, LABELED_TFREC_FORMAT)\n\n    image = decode_image(example['image'])\n    detic = normalise_bounding_box(example['detic_box'], image)\n    yolov5 = normalise_bounding_box(example['yolov5_box'], image)\n    tc_vitbase = normalise_bounding_box(example['tc_vitbase'], image)\n    tc_vitsmall = normalise_bounding_box(example['tc_vitsmall'], image)\n    tc_mocovit = normalise_bounding_box(example['tc_mocovit'], image)\n    fullbody = normalise_bounding_box(example['fullbody'], image)\n    dorsal = normalise_bounding_box(example['dorsal'], image)\n        \n    image = tf.image.resize(image, [IMAGE_SIZE,IMAGE_SIZE])\n    label = example['target']\n    return (\n        image, label, example['species'],\n        detic, yolov5, tc_vitbase,\n        tc_vitsmall, tc_mocovit, fullbody,\n        dorsal\n    )","metadata":{"execution":{"iopub.status.busy":"2022-04-21T11:44:31.816996Z","iopub.execute_input":"2022-04-21T11:44:31.817507Z","iopub.status.idle":"2022-04-21T11:44:31.831892Z","shell.execute_reply.started":"2022-04-21T11:44:31.817462Z","shell.execute_reply":"2022-04-21T11:44:31.830882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_dataset(filenames, crop_method, labeled=True, ordered=False):\n    # Read from TFRecords. For optimal performance, reading from multiple files at once and\n    # disregarding data order. Order does not matter since we will be shuffling the data anyway.\n\n    ignore_order = tf.data.Options()\n    if not ordered:\n        ignore_order.experimental_deterministic = False # disable order, increase speed\n\n    dataset = tf.data.TFRecordDataset(filenames, num_parallel_reads=AUTO) # 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_labeled_tfrecord, crop_method=crop_method))\n    # returns a dataset of (image, label) pairs if labeled=True or (image, id) pairs if labeled=False\n    return dataset\n\ndef get_training_dataset(crop_method):\n    dataset = load_dataset(TRAINING_FILENAMES, crop_method, labeled=True)\n    dataset = dataset.repeat() # the training dataset must repeat for several epochs\n    dataset = dataset.shuffle(512)\n    dataset = dataset.batch(BATCH_SIZE)\n    dataset = dataset.prefetch(AUTO) # prefetch next batch while training (autotune prefetch buffer size)\n    return dataset\n\ndef count_data_items(filenames):\n    # the number of data items is written in the name of the .tfrec files, i.e. flowers00-230.tfrec = 230 data items\n    n = [int(re.compile(r\"-([0-9]*)\\.\").search(filename).group(1)) for filename in filenames]\n    return np.sum(n)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T11:44:31.833317Z","iopub.execute_input":"2022-04-21T11:44:31.833574Z","iopub.status.idle":"2022-04-21T11:44:31.850397Z","shell.execute_reply.started":"2022-04-21T11:44:31.833543Z","shell.execute_reply":"2022-04-21T11:44:31.849665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_dataset(filenames, labeled=True, ordered=False):\n    # Read from TFRecords. For optimal performance, reading from multiple files at once and\n    # disregarding data order. Order does not matter since we will be shuffling the data anyway.\n\n    ignore_order = tf.data.Options()\n    if not ordered:\n        ignore_order.experimental_deterministic = False # disable order, increase speed\n\n    dataset = tf.data.TFRecordDataset(filenames, num_parallel_reads=AUTO) # 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(read_labeled_tfrecord)\n    # returns a dataset of (image, label) pairs if labeled=True or (image, id) pairs if labeled=False\n    return dataset","metadata":{"execution":{"iopub.status.busy":"2022-04-21T11:44:31.851665Z","iopub.execute_input":"2022-04-21T11:44:31.852344Z","iopub.status.idle":"2022-04-21T11:44:31.867415Z","shell.execute_reply.started":"2022-04-21T11:44:31.852302Z","shell.execute_reply":"2022-04-21T11:44:31.866424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_dataset(dataset):\n    row = 5; col = 4;\n    row = min(row,BATCH_SIZE//col)\n\n    for (image, label, species, detic, yolov5, vitbase, vitsmall, mocovit, fullbody, dorsal) in dataset:\n        img = image.numpy()\n        plt.figure(figsize=(25,int(25*row/col)))\n        for j in range(row*col):\n            plt.subplot(row,col,j+1)\n            # plt.title(f\"{image_id[j,].numpy()}\")\n            plt.axis('off')\n\n            # PIL needs int between 0 and 255.\n            im = Image.fromarray((img[j,]*255).astype(np.uint8))\n            plt.imshow(im)\n            ax = plt.gca()\n\n            rect = convert_to_rect(im, detic[j,].numpy(), color='b', linewidth=1)\n            ax.add_patch(rect)\n\n            rect = convert_to_rect(im, yolov5[j,].numpy(), color='r', linewidth=2)\n            ax.add_patch(rect)\n\n            rect = convert_to_rect(im, vitbase[j,].numpy(), color='y', linewidth=3)\n            ax.add_patch(rect)\n\n            rect = convert_to_rect(im, vitsmall[j,].numpy(), color='m', linewidth=4)\n            ax.add_patch(rect)\n\n            rect = convert_to_rect(im, mocovit[j,].numpy(), color='r', linewidth=5)\n            ax.add_patch(rect)\n            \n            rect = convert_to_rect(im, fullbody[j,].numpy(), color='violet', linewidth=5)\n            ax.add_patch(rect)\n            \n            rect = convert_to_rect(im, dorsal[j,].numpy(), color='mediumblue', linewidth=6)\n            ax.add_patch(rect)\n            \n        plt.show()\n        break","metadata":{"execution":{"iopub.status.busy":"2022-04-21T11:44:31.868983Z","iopub.execute_input":"2022-04-21T11:44:31.869259Z","iopub.status.idle":"2022-04-21T11:44:31.884512Z","shell.execute_reply.started":"2022-04-21T11:44:31.86922Z","shell.execute_reply":"2022-04-21T11:44:31.883509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMAGE_SIZE = 224\nBATCH_SIZE = 32","metadata":{"execution":{"iopub.status.busy":"2022-04-21T11:44:31.886523Z","iopub.execute_input":"2022-04-21T11:44:31.887681Z","iopub.status.idle":"2022-04-21T11:44:31.900259Z","shell.execute_reply.started":"2022-04-21T11:44:31.887631Z","shell.execute_reply":"2022-04-21T11:44:31.899535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMAGESIZE = [IMAGE_SIZE,IMAGE_SIZE]\nAUTO = tf.data.experimental.AUTOTUNE\nTRAINING_FILENAMES = tf.io.gfile.glob(f'/tmp/{DATASET_NAME}/happywhale-2022-pseudo-*.tfrec')\nprint(len(TRAINING_FILENAMES))\ndataset = load_dataset(TRAINING_FILENAMES, labeled=True)\ndataset = dataset.repeat()\ndataset = dataset.shuffle(1024)\ndataset = dataset.batch(BATCH_SIZE)\ndataset = dataset.prefetch(AUTO)\n\nprint(count_data_items(TRAINING_FILENAMES))\nplot_dataset(dataset)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T11:39:55.078284Z","iopub.execute_input":"2022-04-21T11:39:55.078618Z","iopub.status.idle":"2022-04-21T11:40:03.246578Z","shell.execute_reply.started":"2022-04-21T11:39:55.078581Z","shell.execute_reply":"2022-04-21T11:40:03.245323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMAGESIZE = [IMAGE_SIZE,IMAGE_SIZE]\nAUTO = tf.data.experimental.AUTOTUNE\nTEST_FILENAMES = tf.io.gfile.glob(f'/tmp/{DATASET_NAME}/happywhale-2022-train-*.tfrec')\nprint(len(TEST_FILENAMES))\ndataset = load_dataset(TEST_FILENAMES, labeled=True)\ndataset = dataset.repeat()\ndataset = dataset.shuffle(1024)\ndataset = dataset.batch(BATCH_SIZE)\ndataset = dataset.prefetch(AUTO)\n\nprint(count_data_items(TEST_FILENAMES))\nplot_dataset(dataset)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T11:44:31.903114Z","iopub.execute_input":"2022-04-21T11:44:31.904124Z","iopub.status.idle":"2022-04-21T11:45:27.804397Z","shell.execute_reply.started":"2022-04-21T11:44:31.904072Z","shell.execute_reply":"2022-04-21T11:45:27.803226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMAGESIZE = [IMAGE_SIZE,IMAGE_SIZE]\nAUTO = tf.data.experimental.AUTOTUNE\nTEST_FILENAMES = tf.io.gfile.glob(f'/tmp/{DATASET_NAME}/happywhale-2022-test-*.tfrec')\nprint(len(TEST_FILENAMES))\ndataset = load_dataset(TEST_FILENAMES, labeled=True)\ndataset = dataset.repeat()\ndataset = dataset.shuffle(1024)\ndataset = dataset.batch(BATCH_SIZE)\ndataset = dataset.prefetch(AUTO)\n\nprint(count_data_items(TEST_FILENAMES))\nplot_dataset(dataset)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T11:40:03.249209Z","iopub.status.idle":"2022-04-21T11:40:03.249853Z","shell.execute_reply.started":"2022-04-21T11:40:03.249641Z","shell.execute_reply":"2022-04-21T11:40:03.249664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Upload Dataset","metadata":{}},{"cell_type":"markdown","source":"This was uploaded interactively, hence being commented out here.","metadata":{}},{"cell_type":"code","source":"!kaggle datasets version -m {version_name} -p /tmp/{DATASET_NAME} -r zip -q","metadata":{"execution":{"iopub.status.busy":"2022-04-11T03:32:55.063892Z","iopub.execute_input":"2022-04-11T03:32:55.064078Z","iopub.status.idle":"2022-04-11T03:38:20.746981Z","shell.execute_reply.started":"2022-04-11T03:32:55.064055Z","shell.execute_reply":"2022-04-11T03:38:20.745539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}