{"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":"After training **EfficientNetB0** models in this [notebook](https://www.kaggle.com/code/umongsain/efficientnetb0-tpu-pytorch), we load the models and predict on the test data.","metadata":{}},{"cell_type":"code","source":"! pip install -q timm","metadata":{"execution":{"iopub.status.busy":"2023-03-19T17:03:00.571227Z","iopub.execute_input":"2023-03-19T17:03:00.571579Z","iopub.status.idle":"2023-03-19T17:03:13.192503Z","shell.execute_reply.started":"2023-03-19T17:03:00.571539Z","shell.execute_reply":"2023-03-19T17:03:13.191227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob\nimport io\n\nimport numpy as np\nimport pandas as pd\n\nimport tensorflow as tf\nimport timm\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport torch.optim as optim\nimport torchvision.transforms as T\n\nfrom PIL import Image\nfrom torch.utils.data import Dataset, DataLoader\nfrom tqdm.auto import tqdm","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-03-19T17:03:13.195565Z","iopub.execute_input":"2023-03-19T17:03:13.195977Z","iopub.status.idle":"2023-03-19T17:03:23.480430Z","shell.execute_reply.started":"2023-03-19T17:03:13.195927Z","shell.execute_reply":"2023-03-19T17:03:23.479192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def tfrecords_to_dataframe(fp, test=False):\n\n    def parse(pb, test=False):\n        d = {\n            \"id\": tf.io.FixedLenFeature([], tf.string),\n            \"image\": tf.io.FixedLenFeature([], tf.string),\n        }\n        if not test:\n            d[\"class\"] = tf.io.FixedLenFeature([], tf.int64)\n        return tf.io.parse_single_example(pb, d)\n\n    df = {\"id\": [], \"img\": []}\n    if not test:\n        df[\"lab\"] = []\n    for sample in tf.data.TFRecordDataset(glob.glob(fp)).map(\n        lambda pb: parse(pb, test)\n    ):\n        df[\"id\"].append(sample[\"id\"].numpy().decode(\"utf-8\"))\n        df[\"img\"].append(sample[\"image\"].numpy())\n        if not test:\n            df[\"lab\"].append(sample[\"class\"].numpy())\n    return pd.DataFrame(df)","metadata":{"execution":{"iopub.status.busy":"2023-03-19T17:03:23.482366Z","iopub.execute_input":"2023-03-19T17:03:23.482774Z","iopub.status.idle":"2023-03-19T17:03:23.493933Z","shell.execute_reply.started":"2023-03-19T17:03:23.482728Z","shell.execute_reply":"2023-03-19T17:03:23.489994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" test_df = tfrecords_to_dataframe(\n     \"../input/tpu-getting-started/tfrecords-jpeg-224x224/test/*.tfrec\",\n     test=True\n )","metadata":{"execution":{"iopub.status.busy":"2023-03-19T17:03:23.497179Z","iopub.execute_input":"2023-03-19T17:03:23.498113Z","iopub.status.idle":"2023-03-19T17:03:34.112260Z","shell.execute_reply.started":"2023-03-19T17:03:23.498083Z","shell.execute_reply":"2023-03-19T17:03:34.111197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class PetalDataset(Dataset):\n    \n    def __init__(self, df, test=False):\n        self.df = df\n        self.test = test\n        self.transform = T.ToTensor()\n        \n    def __len__(self):\n        return len(self.df)\n    \n    def __getitem__(self, idx):\n        item = self.df.iloc[idx]\n        img = Image.open(io.BytesIO(item.img))\n        img = self.transform(img)\n        \n        if self.test:\n            return img\n        \n        label = item.lab\n        return img, label","metadata":{"execution":{"iopub.status.busy":"2023-03-19T17:03:34.113758Z","iopub.execute_input":"2023-03-19T17:03:34.114133Z","iopub.status.idle":"2023-03-19T17:03:34.124666Z","shell.execute_reply.started":"2023-03-19T17:03:34.114096Z","shell.execute_reply":"2023-03-19T17:03:34.123698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ds = PetalDataset(test_df, test=True)\ntest_loader = DataLoader(test_ds, batch_size=8)","metadata":{"execution":{"iopub.status.busy":"2023-03-19T17:03:34.126193Z","iopub.execute_input":"2023-03-19T17:03:34.126584Z","iopub.status.idle":"2023-03-19T17:03:34.141273Z","shell.execute_reply.started":"2023-03-19T17:03:34.126539Z","shell.execute_reply":"2023-03-19T17:03:34.140320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_paths = glob.glob('/kaggle/input/efficientnetb0-tpu-pytorch/*.pt')\nmodel = timm.create_model('efficientnet_b0', pretrained=False, num_classes=104)","metadata":{"execution":{"iopub.status.busy":"2023-03-19T17:04:11.676550Z","iopub.execute_input":"2023-03-19T17:04:11.677243Z","iopub.status.idle":"2023-03-19T17:04:11.772837Z","shell.execute_reply.started":"2023-03-19T17:04:11.677206Z","shell.execute_reply":"2023-03-19T17:04:11.771807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"@torch.no_grad()\ndef infer(model, test_loader, device):\n    preds = []\n    for data in  tqdm(test_loader):\n        data = data.to(device)\n        logits = model(data)\n        proba = logits.softmax(axis=-1)\n        preds.append(proba.cpu().numpy())\n    return np.concatenate(preds, axis=0)","metadata":{"execution":{"iopub.status.busy":"2023-03-19T17:04:11.904251Z","iopub.execute_input":"2023-03-19T17:04:11.904965Z","iopub.status.idle":"2023-03-19T17:04:11.910950Z","shell.execute_reply.started":"2023-03-19T17:04:11.904928Z","shell.execute_reply":"2023-03-19T17:04:11.909755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\nmodel.to(device)\nmodel.eval()\n\nall_preds = []\nfor model_fp in model_paths:\n    model.load_state_dict(torch.load(model_fp))\n    all_preds.append(infer(model, test_loader, device))","metadata":{"execution":{"iopub.status.busy":"2023-03-19T17:04:12.132590Z","iopub.execute_input":"2023-03-19T17:04:12.133079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit_df = pd.DataFrame(\n    dict(\n        id = test_df['id'],\n        label = np.mean(all_preds, axis=0).argmax(axis=-1),\n    )\n)\n","metadata":{"execution":{"iopub.status.busy":"2023-03-19T17:03:34.345152Z","iopub.execute_input":"2023-03-19T17:03:34.346937Z","iopub.status.idle":"2023-03-19T17:03:34.361887Z","shell.execute_reply.started":"2023-03-19T17:03:34.346889Z","shell.execute_reply":"2023-03-19T17:03:34.360704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-03-19T17:03:34.363488Z","iopub.execute_input":"2023-03-19T17:03:34.364250Z","iopub.status.idle":"2023-03-19T17:03:34.380211Z","shell.execute_reply.started":"2023-03-19T17:03:34.364211Z","shell.execute_reply":"2023-03-19T17:03:34.379300Z"},"trusted":true},"execution_count":null,"outputs":[]}]}