{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":53666,"databundleVersionId":6589269,"sourceType":"competition"},{"sourceId":6632733,"sourceType":"datasetVersion","datasetId":3785250,"isSourceIdPinned":true},{"sourceId":7094186,"sourceType":"datasetVersion","datasetId":4088466},{"sourceId":7094455,"sourceType":"datasetVersion","datasetId":4088632},{"sourceId":144358840,"sourceType":"kernelVersion"},{"sourceId":149254949,"sourceType":"kernelVersion"}],"dockerImageVersionId":30559,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## Code modified from below orginal notebook and codes\n## https://www.kaggle.com/code/tubotubo/cmi-submit\n## https://github.com/tubo213/kaggle-child-mind-institute-detect-sleep-states/tree/main","metadata":{"execution":{"iopub.status.busy":"2023-12-04T09:16:15.466315Z","iopub.execute_input":"2023-12-04T09:16:15.467357Z","iopub.status.idle":"2023-12-04T09:16:36.632404Z","shell.execute_reply.started":"2023-12-04T09:16:15.467308Z","shell.execute_reply":"2023-12-04T09:16:36.631353Z"}}},{"cell_type":"code","source":"!pip install hydra-core segmentation_models_pytorch==0.3.3 --no-index --find-links=/kaggle/input/ex-library -q","metadata":{"execution":{"iopub.status.busy":"2023-12-04T09:40:21.737409Z","iopub.execute_input":"2023-12-04T09:40:21.737793Z","iopub.status.idle":"2023-12-04T09:40:42.266763Z","shell.execute_reply.started":"2023-12-04T09:40:21.737752Z","shell.execute_reply":"2023-12-04T09:40:42.265645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp -r /kaggle/input/cmi-code ./","metadata":{"execution":{"iopub.status.busy":"2023-12-04T09:40:42.269166Z","iopub.execute_input":"2023-12-04T09:40:42.269546Z","iopub.status.idle":"2023-12-04T09:40:43.532827Z","shell.execute_reply.started":"2023-12-04T09:40:42.26951Z","shell.execute_reply":"2023-12-04T09:40:43.531822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp -r /kaggle/input/cmi-model ./\n!cp -r /kaggle/input/cmidssv2/exp003 ./cmi-model/exp0001\n!cp -r /kaggle/input/cmidssv1/dummy ./cmi-model/exp0002\n!cp -r /kaggle/input/cmidssv1/exp001 ./cmi-model/exp0003\n!cp -r /kaggle/input/cmidssv1/exp002 ./cmi-model/exp0004","metadata":{"execution":{"iopub.status.busy":"2023-12-04T09:40:43.534171Z","iopub.execute_input":"2023-12-04T09:40:43.534449Z","iopub.status.idle":"2023-12-04T09:41:45.121781Z","shell.execute_reply.started":"2023-12-04T09:40:43.534424Z","shell.execute_reply":"2023-12-04T09:41:45.120664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/working/cmi-code/kaggle-child-mind-institute-detect-sleep-states/","metadata":{"execution":{"iopub.status.busy":"2023-12-04T09:41:45.124912Z","iopub.execute_input":"2023-12-04T09:41:45.125653Z","iopub.status.idle":"2023-12-04T09:41:45.132488Z","shell.execute_reply.started":"2023-12-04T09:41:45.125595Z","shell.execute_reply":"2023-12-04T09:41:45.131662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nDURATION = 5760\nDOWNSAMPLE_RATE = 2\nPHASE = 'test'\nEXP_NAME = 'exp9999'","metadata":{"execution":{"iopub.status.busy":"2023-12-04T09:41:45.13378Z","iopub.execute_input":"2023-12-04T09:41:45.134101Z","iopub.status.idle":"2023-12-04T09:41:46.566379Z","shell.execute_reply.started":"2023-12-04T09:41:45.134071Z","shell.execute_reply":"2023-12-04T09:41:46.565339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile /kaggle/working/cmi-code/kaggle-child-mind-institute-detect-sleep-states/run/conf/dir/kaggle.yaml\ndata_dir: /kaggle/input/child-mind-institute-detect-sleep-states\nprocessed_dir: /kaggle/working/processed_data\noutput_dir: /kaggle/working/output\nmodel_dir: /kaggle/working/cmi-model\nsub_dir: /kaggle/working","metadata":{"execution":{"iopub.status.busy":"2023-12-04T09:41:46.567913Z","iopub.execute_input":"2023-12-04T09:41:46.568595Z","iopub.status.idle":"2023-12-04T09:41:47.079595Z","shell.execute_reply.started":"2023-12-04T09:41:46.568552Z","shell.execute_reply":"2023-12-04T09:41:47.078463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python -m run.prepare_data dir=kaggle phase=$PHASE","metadata":{"execution":{"iopub.status.busy":"2023-12-04T09:41:47.080932Z","iopub.execute_input":"2023-12-04T09:41:47.081324Z","iopub.status.idle":"2023-12-04T09:41:49.520977Z","shell.execute_reply.started":"2023-12-04T09:41:47.08126Z","shell.execute_reply":"2023-12-04T09:41:49.519781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DURATION = 5760\nDOWNSAMPLE_RATE = 2\nPHASE = 'test'\nEXP_NAME = 'exp999999'","metadata":{"execution":{"iopub.status.busy":"2023-12-04T09:41:54.008722Z","iopub.execute_input":"2023-12-04T09:41:54.009507Z","iopub.status.idle":"2023-12-04T09:41:54.013905Z","shell.execute_reply.started":"2023-12-04T09:41:54.009473Z","shell.execute_reply":"2023-12-04T09:41:54.012938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile /kaggle/working/cmi-code/kaggle-child-mind-institute-detect-sleep-states/run/inference.py\nfrom pathlib import Path\n\nimport hydra\nimport numpy as np\nimport polars as pl\nimport torch\nimport torch.nn as nn\nfrom omegaconf import DictConfig\nfrom pytorch_lightning import seed_everything\nfrom torch.utils.data import DataLoader\nfrom torchvision.transforms.functional import resize\nfrom tqdm import tqdm\n\nfrom src.datamodule.seg import TestDataset, load_chunk_features, nearest_valid_size\nfrom src.models.common import get_model\nfrom src.utils.common import trace\nfrom src.utils.post_process import post_process_for_seg\n\n\ndef load_model(cfg: DictConfig) -> nn.Module:\n    num_timesteps = nearest_valid_size(int(cfg.duration * cfg.upsample_rate), cfg.downsample_rate)\n    model = get_model(\n        cfg,\n        feature_dim=len(cfg.features),\n        n_classes=len(cfg.labels),\n        num_timesteps=num_timesteps // cfg.downsample_rate,\n    )\n\n    # load weights\n    if cfg.weight is not None:\n        weight_path = (\n            Path(cfg.dir.model_dir)\n            / cfg.weight[\"exp_name\"]\n            / cfg.weight[\"run_name\"]\n            / \"best_model.pth\"\n        )\n        model.load_state_dict(torch.load(weight_path))\n        print('load weight from \"{}\"'.format(weight_path))\n    return model\n\n\ndef get_test_dataloader(cfg: DictConfig) -> DataLoader:\n    \"\"\"get test dataloader\n\n    Args:\n        cfg (DictConfig): config\n\n    Returns:\n        DataLoader: test dataloader\n    \"\"\"\n    feature_dir = Path(cfg.dir.processed_dir) / cfg.phase\n    series_ids = [x.name for x in feature_dir.glob(\"*\")]\n    chunk_features = load_chunk_features(\n        duration=cfg.duration,\n        feature_names=cfg.features,\n        series_ids=series_ids,\n        processed_dir=Path(cfg.dir.processed_dir),\n        phase=cfg.phase,\n    )\n    test_dataset = TestDataset(cfg, chunk_features=chunk_features)\n    test_dataloader = DataLoader(\n        test_dataset,\n        batch_size=cfg.batch_size,\n        shuffle=False,\n        num_workers=cfg.num_workers,\n        pin_memory=True,\n        drop_last=False,\n    )\n    return test_dataloader\n\n\ndef inference(\n    duration: int, loader: DataLoader, models, device: torch.device, use_amp\n) -> tuple[list[str], np.ndarray]:\n\n\n    preds = []\n\n    keys = []\n    for batch in tqdm(loader, desc=\"inference\"):\n\n        with torch.no_grad():\n            with torch.cuda.amp.autocast(enabled=use_amp):\n                x = batch[\"feature\"].to(device)\n                predz_ = []\n                for model in models:\n                    model = model.to(device)\n                    model.eval()\n                    pred = model(x)[\"logits\"]\n                    predz_.append(pred)\n                predz_ = torch.stack(predz_)\n                pred = torch.sigmoid(torch.mean(predz_, dim=0))\n\n                pred = resize(\n                    pred.detach().cpu(),\n                    size=[duration, pred.shape[2]],\n                    antialias=False,\n                )\n            key = batch[\"key\"]\n            preds.append(pred.detach().cpu().numpy())\n            keys.extend(key)\n\n    preds = np.concatenate(preds)\n\n    return keys, preds  # type: ignore\n\n\ndef make_submission(\n    keys: list[str], preds: np.ndarray, downsample_rate, score_th, distance\n) -> pl.DataFrame:\n    sub_df = post_process_for_seg(\n        keys,\n        preds[:, :, [1, 2]],  # type: ignore\n        score_th=score_th,\n        distance=distance,  # type: ignore\n    )\n\n    return sub_df\n\n\n@hydra.main(config_path=\"conf\", config_name=\"inference\", version_base=\"1.2\")\ndef main(cfg: DictConfig):\n    models = []\n    models2 = []\n    duration2 = []\n    seed_everything(cfg.seed)\n\n    with trace(\"load test dataloader\"):\n        test_dataloader = get_test_dataloader(cfg)\n    with trace(\"load model\"):\n        \n        cfg.model.encoder_name = 'resnet34'\n        cfg.exp_name = 'exp018'\n        print('loading ',cfg.exp_name)\n        model = load_model(cfg)\n        models.append(model)\n        \n        cfg.model.encoder_name = 'resnet34'\n        cfg.exp_name = 'exp021'\n        print('loading ',cfg.exp_name)\n        model = load_model(cfg)\n        models.append(model)\n        \n        cfg.model.encoder_name = 'tu-resnest269e'\n        cfg.exp_name = 'exp017'\n        print('loading ',cfg.exp_name)\n        model = load_model(cfg)\n        models.append(model)\n        \n        cfg.model.encoder_name = 'resnet34'\n        cfg.exp_name = 'exp019'\n        print('loading ',cfg.exp_name)\n        model = load_model(cfg)\n        models.append(model)\n        \n        cfg.model.encoder_name = 'resnet34'\n        cfg.exp_name = 'exp020'\n        print('loading ',cfg.exp_name)\n        model = load_model(cfg)\n        models.append(model)\n        \n        cfg.model.name = 'Spec2DCNN'\n        cfg.model.encoder_name = 'resnext101_32x4d'\n        cfg.decoder.name = 'UNet1DDecoder'\n        cfg.exp_name = 'exp0001'\n        print('loading ',cfg.exp_name)\n        model = load_model(cfg)\n        models.append(model)\n        \n        cfg.model.name = 'Spec2DCNN'\n        cfg.model.encoder_name = 'resnext101_32x4d'\n        cfg.decoder.name = 'UNet1DDecoder'\n        cfg.exp_name = 'exp0004'\n        print('loading ',cfg.exp_name)\n        model = load_model(cfg)\n        models.append(model)\n        \n        cfg.model.name = 'Spec2DCNN'\n        cfg.model.encoder_name = 'efficientnet-b0'\n        cfg.decoder.name = 'UNet1DDecoder'\n        cfg.exp_name = 'exp0003'\n        print('loading ',cfg.exp_name)\n        model = load_model(cfg)\n        models.append(model)\n        \n        cfg.model.name = 'Spec2DCNN'\n        cfg.model.encoder_name = 'resnet18'\n        cfg.decoder.name = 'UNet1DDecoder'\n        cfg.exp_name = 'exp0002'\n        print('loading ',cfg.exp_name)\n        model = load_model(cfg)\n        models.append(model)\n        \n    device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n\n    with trace(\"inference\"):\n        keys, preds = inference(cfg.duration, test_dataloader, models, device, use_amp=cfg.use_amp)\n\n    with trace(\"make submission\"):\n        sub_df = make_submission(\n            keys,\n            preds,\n            downsample_rate=cfg.downsample_rate,\n            score_th=cfg.post_process.score_th,\n            distance=cfg.post_process.distance,\n        )\n    sub_df.write_csv(Path(cfg.dir.sub_dir) / \"submission.csv\")\n\n\nif __name__ == \"__main__\":\n    main()","metadata":{"execution":{"iopub.status.busy":"2023-12-04T09:59:47.801675Z","iopub.execute_input":"2023-12-04T09:59:47.80204Z","iopub.status.idle":"2023-12-04T09:59:47.81177Z","shell.execute_reply.started":"2023-12-04T09:59:47.802014Z","shell.execute_reply":"2023-12-04T09:59:47.810863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python -m run.inference\\\n    dir=kaggle\\\n    model.encoder_name=resnet34\\\n    model.encoder_weights=null\\\n    num_workers=4\\\n    exp_name=$EXP_NAME\\\n    weight.run_name=single\\\n    batch_size=2\\\n    duration=$DURATION\\\n    downsample_rate=$DOWNSAMPLE_RATE\\\n    post_process.score_th=0.004\\\n    post_process.distance=40\\\n    phase=$PHASE","metadata":{"execution":{"iopub.status.busy":"2023-12-04T09:59:51.138536Z","iopub.execute_input":"2023-12-04T09:59:51.138908Z","iopub.status.idle":"2023-12-04T10:00:12.683934Z","shell.execute_reply.started":"2023-12-04T09:59:51.138877Z","shell.execute_reply":"2023-12-04T10:00:12.683009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-12-04T09:45:20.731918Z","iopub.execute_input":"2023-12-04T09:45:20.732675Z","iopub.status.idle":"2023-12-04T09:45:21.698775Z","shell.execute_reply.started":"2023-12-04T09:45:20.732635Z","shell.execute_reply":"2023-12-04T09:45:21.697627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n!rm -rf /kaggle/working/processed_data\n!rm -rf /kaggle/working/output\n!rm -rf /kaggle/working/cmi-code\n!rm -rf /kaggle/working/cmi-model","metadata":{"execution":{"iopub.status.busy":"2023-12-04T08:55:28.03012Z","iopub.execute_input":"2023-12-04T08:55:28.030543Z","iopub.status.idle":"2023-12-04T08:55:30.09Z","shell.execute_reply.started":"2023-12-04T08:55:28.030505Z","shell.execute_reply":"2023-12-04T08:55:30.088569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}