{"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":"code","source":"!pip install -q   timm","metadata":{"id":"Yn1Ybf15VAqW","outputId":"7eaa84b6-263a-4647-8435-871c8d69742a","papermill":{"duration":10.990518,"end_time":"2021-06-12T10:59:03.018632","exception":false,"start_time":"2021-06-12T10:58:52.028114","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-03T00:24:58.295204Z","iopub.execute_input":"2021-07-03T00:24:58.295516Z","iopub.status.idle":"2021-07-03T00:25:03.988869Z","shell.execute_reply.started":"2021-07-03T00:24:58.295484Z","shell.execute_reply":"2021-07-03T00:25:03.987808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pathlib import Path\n\nimport numpy as np\nimport librosa.display as lbd\nimport pandas as pd\n\nimport torch\nfrom  torch.utils.data import Dataset, DataLoader\n\nfrom matplotlib import pyplot as plt\n\nfrom tqdm.notebook import tqdm\n\nimport timm","metadata":{"id":"2dt7oG43VAqc","papermill":{"duration":4.097309,"end_time":"2021-06-12T10:59:07.210078","exception":false,"start_time":"2021-06-12T10:59:03.112769","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-03T00:25:03.992401Z","iopub.execute_input":"2021-07-03T00:25:03.992682Z","iopub.status.idle":"2021-07-03T00:25:03.997401Z","shell.execute_reply.started":"2021-07-03T00:25:03.992653Z","shell.execute_reply":"2021-07-03T00:25:03.996609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"id":"2NfkUn9SCWs6","papermill":{"duration":0.037583,"end_time":"2021-06-12T10:59:09.616544","exception":false,"start_time":"2021-06-12T10:59:09.578961","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TEST_BATCH_SIZE = 768\nTEST_NUM_WORKERS = 2\n\nDEVICE = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n\nprint(\"Device:\", DEVICE)","metadata":{"id":"Iu56f-7VVAqf","outputId":"d03cd648-5d64-4438-fd81-5d09f817493f","papermill":{"duration":0.093926,"end_time":"2021-06-12T10:59:09.742076","exception":false,"start_time":"2021-06-12T10:59:09.64815","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-03T00:25:03.999304Z","iopub.execute_input":"2021-07-03T00:25:03.999872Z","iopub.status.idle":"2021-07-03T00:25:04.015928Z","shell.execute_reply.started":"2021-07-03T00:25:03.999837Z","shell.execute_reply":"2021-07-03T00:25:04.013999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_file_path(file_id):\n    return str(\"../input/g2net-test-mels-2/audio_images/{}/{}/{}/{}.npy\".format(file_id[0], file_id[1], file_id[2], file_id))","metadata":{"papermill":{"duration":0.037753,"end_time":"2021-06-12T10:59:09.812087","exception":false,"start_time":"2021-06-12T10:59:09.774334","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-03T00:25:04.017011Z","iopub.status.idle":"2021-07-03T00:25:04.017380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"../input/g2net-gravitational-wave-detection/sample_submission.csv\")\ndf[\"impath\"] = df[\"id\"].apply(get_file_path)\n\nprint(df.shape)\ndf","metadata":{"id":"SRRhx1EAN75G","outputId":"e4b986e5-5364-4a3c-928c-6556ee45bf00","papermill":{"duration":0.651951,"end_time":"2021-06-12T10:59:10.495224","exception":false,"start_time":"2021-06-12T10:59:09.843273","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-03T00:25:04.018445Z","iopub.status.idle":"2021-07-03T00:25:04.019093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"id":"xh4hfWZhuglm","papermill":{"duration":0.031806,"end_time":"2021-06-12T10:59:10.56001","exception":false,"start_time":"2021-06-12T10:59:10.528204","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data","metadata":{}},{"cell_type":"code","source":"class G2NetDataset(Dataset):\n    def __init__(self, data):\n        \n        self.data = data\n        \n    def __len__(self):\n        return len(self.data)\n    \n    def __getitem__(self, idx):\n        return np.vstack(np.load(self.data.loc[idx, \"impath\"]).astype(\"float32\") / 255.0)[None]","metadata":{"id":"OWSkCXyhCWs-","papermill":{"duration":0.044807,"end_time":"2021-06-12T10:59:10.709779","exception":false,"start_time":"2021-06-12T10:59:10.664972","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-03T00:25:04.020308Z","iopub.status.idle":"2021-07-03T00:25:04.020939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds = G2NetDataset(data=df)\nprint(len(ds))\n\nx = ds[np.random.choice(len(ds))]\nprint(x.shape)","metadata":{"id":"Np-56XrXVAqm","outputId":"6acc5f86-74c8-43eb-a169-69df97a57fdd","papermill":{"duration":0.043515,"end_time":"2021-06-12T10:59:10.858235","exception":false,"start_time":"2021-06-12T10:59:10.81472","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-03T00:25:04.022094Z","iopub.status.idle":"2021-07-03T00:25:04.022725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lbd.specshow(x[0])","metadata":{"id":"bPwrCoRyCWs-","outputId":"1a1efd8d-1952-4841-9a98-54cb9547c386","papermill":{"duration":0.188452,"end_time":"2021-06-12T10:59:11.257426","exception":false,"start_time":"2021-06-12T10:59:11.068974","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-03T00:25:04.023891Z","iopub.status.idle":"2021-07-03T00:25:04.024514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.03522,"end_time":"2021-06-12T10:59:11.416383","exception":false,"start_time":"2021-06-12T10:59:11.381163","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Inference","metadata":{"id":"F56zXq8CVAqn","papermill":{"duration":0.03534,"end_time":"2021-06-12T10:59:11.487147","exception":false,"start_time":"2021-06-12T10:59:11.451807","status":"completed"},"tags":[]}},{"cell_type":"code","source":"def load_net(checkpoint_path):\n    net = timm.create_model(\"tf_efficientnet_b0\", pretrained=False, num_classes=1)\n    net.conv_stem = torch.nn.Conv2d(1, 32, kernel_size=(3, 3), stride=(2, 2), bias=False)\n    net = net.to(DEVICE)\n    net.load_state_dict(torch.load(checkpoint_path, map_location=DEVICE))\n    net = net.eval()\n    return net","metadata":{"papermill":{"duration":0.043552,"end_time":"2021-06-12T10:59:12.534207","exception":false,"start_time":"2021-06-12T10:59:12.490655","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-03T00:25:04.025715Z","iopub.status.idle":"2021-07-03T00:25:04.026329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"@torch.no_grad()\ndef predict(nets, test_data):\n    preds = []\n    for xb in  tqdm(test_data):\n        pred = 0\n        for net in nets:\n            o = torch.sigmoid(net(xb.to(DEVICE)).squeeze(1))\n            pred += o\n            \n        pred /= len(nets)\n            \n        preds.append(pred.cpu().numpy())\n    preds = np.concatenate(preds)\n    return preds","metadata":{"papermill":{"duration":0.048847,"end_time":"2021-06-12T10:59:40.850318","exception":false,"start_time":"2021-06-12T10:59:40.801471","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-03T00:25:04.027529Z","iopub.status.idle":"2021-07-03T00:25:04.028143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"checkpoint_paths = [\n    \"../input/g2net-kkiller-public-models/tf_efficientnet_b0_fold0.pth\",\n    \"../input/g2net-kkiller-public-models/tf_efficientnet_b0_fold1.pth\",\n    \"../input/g2net-kkiller-public-models/tf_efficientnet_b0_fold2.pth\",\n]\n\nnets = [load_net(checkpoint_path) for checkpoint_path in checkpoint_paths ]\n\nprint(\"n_models:\", len(checkpoint_paths))","metadata":{"papermill":{"duration":27.879275,"end_time":"2021-06-12T10:59:40.687273","exception":false,"start_time":"2021-06-12T10:59:12.807998","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-03T00:25:04.029237Z","iopub.status.idle":"2021-07-03T00:25:04.029929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.038269,"end_time":"2021-06-12T10:59:40.926913","exception":false,"start_time":"2021-06-12T10:59:40.888644","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = G2NetDataset(data=df)\ntest_laoder = DataLoader(test_data, batch_size=TEST_BATCH_SIZE, num_workers=TEST_NUM_WORKERS, shuffle=False)\n\nlen(test_data), len(test_laoder)","metadata":{"papermill":{"duration":0.047465,"end_time":"2021-06-12T10:59:41.011261","exception":false,"start_time":"2021-06-12T10:59:40.963796","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-03T00:25:07.648985Z","iopub.execute_input":"2021-07-03T00:25:07.649314Z","iopub.status.idle":"2021-07-03T00:25:07.656813Z","shell.execute_reply.started":"2021-07-03T00:25:07.649281Z","shell.execute_reply":"2021-07-03T00:25:07.655754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.036892,"end_time":"2021-06-12T11:48:48.827076","exception":false,"start_time":"2021-06-12T11:48:48.790184","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = df[[\"id\", \"target\"]].copy()\nsub[\"target\"] = predict(nets, test_laoder)\n\nsub.to_csv(\"submission.csv\", index=False)\n\nprint(sub.shape)\nsub","metadata":{"papermill":{"duration":0.132999,"end_time":"2021-06-12T11:48:48.997428","exception":false,"start_time":"2021-06-12T11:48:48.864429","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-03T00:25:09.054715Z","iopub.execute_input":"2021-07-03T00:25:09.055045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.038324,"end_time":"2021-06-12T11:48:50.544295","exception":false,"start_time":"2021-06-12T11:48:50.505971","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]}]}