{"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":"# Create TFRecord for TPU Training with Tensorflow","metadata":{}},{"cell_type":"markdown","source":"* Thank Remek for sharing preprocessed dataset","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nfrom tqdm import tqdm\n\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\nimport tensorflow as tf\n\nfrom sklearn.model_selection import StratifiedKFold","metadata":{"execution":{"iopub.status.busy":"2022-03-13T14:03:23.639155Z","iopub.execute_input":"2022-03-13T14:03:23.639592Z","iopub.status.idle":"2022-03-13T14:03:30.954585Z","shell.execute_reply.started":"2022-03-13T14:03:23.639476Z","shell.execute_reply":"2022-03-13T14:03:30.953647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/ultra-mnist/train.csv')\ntrain","metadata":{"execution":{"iopub.status.busy":"2022-03-13T14:03:30.956368Z","iopub.execute_input":"2022-03-13T14:03:30.957051Z","iopub.status.idle":"2022-03-13T14:03:31.023331Z","shell.execute_reply.started":"2022-03-13T14:03:30.957013Z","shell.execute_reply":"2022-03-13T14:03:31.022516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CFG = {\n    'NUM_FOLD' : 3,\n    'SEED' : 42\n}","metadata":{"execution":{"iopub.status.busy":"2022-03-13T14:03:31.024643Z","iopub.execute_input":"2022-03-13T14:03:31.025052Z","iopub.status.idle":"2022-03-13T14:03:31.029304Z","shell.execute_reply.started":"2022-03-13T14:03:31.025017Z","shell.execute_reply":"2022-03-13T14:03:31.028389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Target Distribution","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(12, 12))\n\nsns.displot(train['digit_sum'])","metadata":{"execution":{"iopub.status.busy":"2022-03-13T14:03:31.03121Z","iopub.execute_input":"2022-03-13T14:03:31.031601Z","iopub.status.idle":"2022-03-13T14:03:31.457288Z","shell.execute_reply.started":"2022-03-13T14:03:31.031551Z","shell.execute_reply":"2022-03-13T14:03:31.456201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['digit_sum'].value_counts().sort_index()","metadata":{"execution":{"iopub.status.busy":"2022-03-13T14:03:31.459863Z","iopub.execute_input":"2022-03-13T14:03:31.460184Z","iopub.status.idle":"2022-03-13T14:03:31.474494Z","shell.execute_reply.started":"2022-03-13T14:03:31.460149Z","shell.execute_reply":"2022-03-13T14:03:31.473471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Cross Validation Strategy\n\n* Using StratifiedKFold","metadata":{}},{"cell_type":"code","source":"skf = StratifiedKFold(n_splits=CFG['NUM_FOLD'], random_state=CFG['SEED'], shuffle=True)\nfor fold, (train_idx, val_idx) in enumerate(skf.split(train, train['digit_sum'])):\n    train[f'fold_{fold}'] = 0\n    train.iloc[train_idx, -1] = 'train'\n    train.iloc[val_idx, -1] = 'val'\ntrain","metadata":{"execution":{"iopub.status.busy":"2022-03-13T14:03:31.476567Z","iopub.execute_input":"2022-03-13T14:03:31.477264Z","iopub.status.idle":"2022-03-13T14:03:31.520761Z","shell.execute_reply.started":"2022-03-13T14:03:31.477207Z","shell.execute_reply":"2022-03-13T14:03:31.519657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create TFRecord","metadata":{}},{"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 serialize_example(image, image_name, target):\n    feature = {\n        'image': _bytes_feature(image),\n        'image_name': _bytes_feature(image_name),\n        'target': _int64_feature(target),\n      }\n    example_proto = tf.train.Example(features=tf.train.Features(feature=feature))\n    return example_proto.SerializeToString()","metadata":{"execution":{"iopub.status.busy":"2022-03-13T14:03:31.522662Z","iopub.execute_input":"2022-03-13T14:03:31.523218Z","iopub.status.idle":"2022-03-13T14:03:31.53568Z","shell.execute_reply.started":"2022-03-13T14:03:31.523156Z","shell.execute_reply":"2022-03-13T14:03:31.534524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_tf_records():\n    for fold in range(CFG['NUM_FOLD']):\n        print(f'----- fold {fold} -----')\n        train_filename = f'ultramnist-train-fold-{fold}.tfrec'\n        val_filename = f'ultramnist-val-fold-{fold}.tfrec'\n        train_data = train[train[f'fold_{fold}'] == 'train']\n        valid_data = train[train[f'fold_{fold}'] == 'val']\n        \n        print('Generating Train Dataset....')\n        # Train Dataset\n        with tf.io.TFRecordWriter(train_filename) as writer:\n            for i,row in tqdm(train_data.iterrows(), total=len(train_data)):\n                image_id = row.id\n                target = row.digit_sum\n                image_path = f\"../input/ultramnistblack/train/{image_id}.jpeg\"\n                image_encoded = tf.io.read_file(image_path)\n                image_name = str.encode(image_id)\n                example = serialize_example(image_encoded, image_name, target)\n                writer.write(example)\n        \n        print('Generating Valid Dataset....')\n        # Valid Dataset\n        with tf.io.TFRecordWriter(val_filename) as writer:\n            for i,row in tqdm(valid_data.iterrows(), total=len(valid_data)):\n                image_id = row.id\n                target = row.digit_sum\n                image_path = f\"../input/ultramnistblack/train/{image_id}.jpeg\"\n                image_encoded = tf.io.read_file(image_path)\n                image_name = str.encode(image_id)\n                example = serialize_example(image_encoded, image_name, target)\n                writer.write(example)","metadata":{"execution":{"iopub.status.busy":"2022-03-13T14:03:31.537343Z","iopub.execute_input":"2022-03-13T14:03:31.538152Z","iopub.status.idle":"2022-03-13T14:03:31.554058Z","shell.execute_reply.started":"2022-03-13T14:03:31.538093Z","shell.execute_reply":"2022-03-13T14:03:31.552768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"create_tf_records()","metadata":{"execution":{"iopub.status.busy":"2022-03-13T14:03:31.555559Z","iopub.execute_input":"2022-03-13T14:03:31.555849Z","iopub.status.idle":"2022-03-13T14:13:31.785406Z","shell.execute_reply.started":"2022-03-13T14:03:31.555816Z","shell.execute_reply":"2022-03-13T14:13:31.782615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Done!","metadata":{}}]}