-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix up network naming; add example to load network and run inference …
…in notebook
- Loading branch information
Showing
4 changed files
with
195 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from spf.scripts.train_single_point import (\n", | ||
" load_checkpoint,\n", | ||
" load_config_from_fn,\n", | ||
" load_model,\n", | ||
")\n", | ||
"\n", | ||
"\n", | ||
"def load_model_and_config_from_config_fn_and_checkpoint(config_fn, checkpoint_fn):\n", | ||
" config = load_config_from_fn(config_fn)\n", | ||
" config[\"optim\"][\"checkpoint\"] = checkpoint_fn\n", | ||
" m = load_model(config[\"model\"], config[\"global\"]).to(config[\"optim\"][\"device\"])\n", | ||
" m, _, _, _, _ = load_checkpoint(\n", | ||
" checkpoint_fn=config[\"optim\"][\"checkpoint\"],\n", | ||
" config=config,\n", | ||
" model=m,\n", | ||
" optimizer=None,\n", | ||
" scheduler=None,\n", | ||
" force_load=True,\n", | ||
" )\n", | ||
" return m, config\n", | ||
"\n", | ||
"\n", | ||
"def convert_datasets_config_to_inference(datasets_config, ds_fn):\n", | ||
" datasets_config = datasets_config.copy()\n", | ||
" datasets_config.update(\n", | ||
" {\n", | ||
" \"batch_size\": 1,\n", | ||
" \"flip\": False,\n", | ||
" \"double_flip\": False,\n", | ||
" \"precompute_cache\": \"/home/mouse9911/precompute_cache_chunk16_sept\",\n", | ||
" \"shuffle\": False,\n", | ||
" \"skip_qc\": True,\n", | ||
" \"snapshots_adjacent_stride\": 1,\n", | ||
" \"train_snapshots_per_session\": 1,\n", | ||
" \"val_snapshots_per_session\": 1,\n", | ||
" \"random_snapshot_size\": False,\n", | ||
" \"snapshots_stride\": 1,\n", | ||
" \"train_paths\": [ds_fn],\n", | ||
" \"train_on_val\": True,\n", | ||
" \"workers\": 1,\n", | ||
" }\n", | ||
" )\n", | ||
" return datasets_config" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import torch\n", | ||
"from spf.scripts.train_single_point import load_dataloaders\n", | ||
"\n", | ||
"from tqdm import tqdm\n", | ||
"\n", | ||
"config_fn = \"/home/mouse9911/gits/spf/nov2_checkpoints/nov2_small_paired_checkpoints_inputdo0p3/config.yml\"\n", | ||
"checkpoint_fn = \"/home/mouse9911/gits/spf/nov2_checkpoints/nov2_small_paired_checkpoints_inputdo0p3/best.pth\"\n", | ||
"ds_fn = \"/mnt/4tb_ssd/nosig_data/wallarrayv3_2024_08_21_10_30_58_nRX2_bounce_spacing0p05075.zarr\"\n", | ||
"\n", | ||
"# load model\n", | ||
"model, config = load_model_and_config_from_config_fn_and_checkpoint(\n", | ||
" config_fn=config_fn, checkpoint_fn=checkpoint_fn\n", | ||
")\n", | ||
"\n", | ||
"# load datasets config\n", | ||
"datasets_config = convert_datasets_config_to_inference(\n", | ||
" config[\"datasets\"],\n", | ||
" ds_fn=ds_fn,\n", | ||
")\n", | ||
"\n", | ||
"# load dataloader\n", | ||
"optim_config = {\"device\": \"cuda\", \"dtype\": torch.float32}\n", | ||
"global_config = {\"nthetas\": 65, \"n_radios\": 2, \"seed\": 0, \"beamformer_input\": True}\n", | ||
"train_dataloader, val_dataloader = load_dataloaders(\n", | ||
" datasets_config, optim_config, config[\"global\"], step=0, epoch=0\n", | ||
")\n", | ||
"\n", | ||
"# run inference\n", | ||
"model.eval()\n", | ||
"for _, val_batch_data in enumerate(tqdm(val_dataloader, leave=False)):\n", | ||
" val_batch_data = val_batch_data.to(config[\"optim\"][\"device\"])\n", | ||
" output = model(val_batch_data)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "spf", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.12.3" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters