Skip to content

Commit

Permalink
Fix flake8 error (Project-MONAI#1618)
Browse files Browse the repository at this point in the history
Fixes Project-MONAI#1617


### Checks
<!--- Put an `x` in all the boxes that apply, and remove the not
applicable items -->
- [ ] Avoid including large-size files in the PR.
- [ ] Clean up long text outputs from code cells in the notebook.
- [ ] For security purposes, please check the contents and remove any
sensitive info such as user names and private key.
- [ ] Ensure (1) hyperlinks and markdown anchors are working (2) use
relative paths for tutorial repo files (3) put figure and graphs in the
`./figure` folder
- [ ] Notebook runs automatically `./runner.sh -t <path to .ipynb file>`

---------

Signed-off-by: YunLiu <[email protected]>
  • Loading branch information
KumoLiu authored Jan 19, 2024
1 parent e3fd896 commit 2b41e8c
Show file tree
Hide file tree
Showing 19 changed files with 126 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,6 @@
"pygments_lexer": "ipython3",
"version": "3.8.10 (default, Nov 14 2022, 12:59:47) \n[GCC 9.4.0]"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "916dbcbb3f70747c44a77c7bcd40155683ae19c65e1c03b4aa3499c5328201f1"
Expand Down
3 changes: 1 addition & 2 deletions 2d_classification/monai_101.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,7 @@
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
},
"orig_nbformat": 4
}
},
"nbformat": 4,
"nbformat_minor": 2
Expand Down
6 changes: 4 additions & 2 deletions 3d_segmentation/swin_unetr_btcv_segmentation_3d.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@
" val_outputs_list = decollate_batch(val_outputs)\n",
" val_output_convert = [post_pred(val_pred_tensor) for val_pred_tensor in val_outputs_list]\n",
" dice_metric(y_pred=val_output_convert, y=val_labels_convert)\n",
" epoch_iterator_val.set_description(\"Validate (%d / %d Steps)\" % (global_step, 10.0))\n",
" epoch_iterator_val.set_description(\"Validate (%d / %d Steps)\" % (global_step, 10.0)) # noqa: B038\n",
" mean_dice_val = dice_metric.aggregate().item()\n",
" dice_metric.reset()\n",
" return mean_dice_val\n",
Expand All @@ -546,7 +546,9 @@
" scaler.step(optimizer)\n",
" scaler.update()\n",
" optimizer.zero_grad()\n",
" epoch_iterator.set_description(f\"Training ({global_step} / {max_iterations} Steps) (loss={loss:2.5f})\")\n",
" epoch_iterator.set_description( # noqa: B038\n",
" f\"Training ({global_step} / {max_iterations} Steps) (loss={loss:2.5f})\"\n",
" )\n",
" if (global_step % eval_num == 0 and global_step != 0) or global_step == max_iterations:\n",
" epoch_iterator_val = tqdm(val_loader, desc=\"Validate (X / X Steps) (dice=X.X)\", dynamic_ncols=True)\n",
" dice_val = validation(epoch_iterator_val)\n",
Expand Down
6 changes: 4 additions & 2 deletions 3d_segmentation/unetr_btcv_segmentation_3d.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@
" val_outputs_list = decollate_batch(val_outputs)\n",
" val_output_convert = [post_pred(val_pred_tensor) for val_pred_tensor in val_outputs_list]\n",
" dice_metric(y_pred=val_output_convert, y=val_labels_convert)\n",
" epoch_iterator_val.set_description(\"Validate (%d / %d Steps)\" % (global_step, 10.0))\n",
" epoch_iterator_val.set_description(\"Validate (%d / %d Steps)\" % (global_step, 10.0)) # noqa: B038\n",
" mean_dice_val = dice_metric.aggregate().item()\n",
" dice_metric.reset()\n",
" return mean_dice_val\n",
Expand All @@ -641,7 +641,9 @@
" epoch_loss += loss.item()\n",
" optimizer.step()\n",
" optimizer.zero_grad()\n",
" epoch_iterator.set_description(\"Training (%d / %d Steps) (loss=%2.5f)\" % (global_step, max_iterations, loss))\n",
" epoch_iterator.set_description( # noqa: B038\n",
" \"Training (%d / %d Steps) (loss=%2.5f)\" % (global_step, max_iterations, loss)\n",
" )\n",
" if (global_step % eval_num == 0 and global_step != 0) or global_step == max_iterations:\n",
" epoch_iterator_val = tqdm(val_loader, desc=\"Validate (X / X Steps) (dice=X.X)\", dynamic_ncols=True)\n",
" dice_val = validation(epoch_iterator_val)\n",
Expand Down
40 changes: 21 additions & 19 deletions bundle/02_mednist_classification.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,9 @@
"cell_type": "code",
"execution_count": 3,
"id": "d11681af-3210-4b2b-b7bd-8ad8dedfe230",
"metadata": {},
"metadata": {
"lines_to_next_cell": 2
},
"outputs": [
{
"name": "stdout",
Expand Down Expand Up @@ -310,8 +312,7 @@
" - _target_: EnsureChannelFirstd\n",
" keys: 'image'\n",
" - _target_: ScaleIntensityd\n",
" keys: 'image'\n",
" "
" keys: 'image'"
]
},
{
Expand Down Expand Up @@ -369,19 +370,19 @@
" num_workers: 4\n",
"\n",
"trainer:\n",
" _target_: SupervisedTrainer\n",
" device: '@device'\n",
" max_epochs: '@max_epochs'\n",
" train_data_loader: '@train_dl'\n",
" network: '@net'\n",
" optimizer: \n",
" _target_: torch.optim.Adam\n",
" params: '[email protected]()'\n",
" lr: 0.00001 # learning rate set slow so that you can see network improvement over epochs\n",
" loss_function: \n",
" _target_: torch.nn.CrossEntropyLoss\n",
" inferer: \n",
" _target_: SimpleInferer\n",
" _target_: SupervisedTrainer\n",
" device: '@device'\n",
" max_epochs: '@max_epochs'\n",
" train_data_loader: '@train_dl'\n",
" network: '@net'\n",
" optimizer: \n",
" _target_: torch.optim.Adam\n",
" params: '[email protected]()'\n",
" lr: 0.00001 # learning rate set slow so that you can see network improvement over epochs\n",
" loss_function: \n",
" _target_: torch.nn.CrossEntropyLoss\n",
" inferer: \n",
" _target_: SimpleInferer\n",
"\n",
"train:\n",
"- '[email protected]()'\n",
Expand Down Expand Up @@ -527,8 +528,7 @@
" prob = result.detach().to(\"cpu\")[0]\n",
" pred = class_names[prob.argmax()]\n",
" gt = item[\"class_name\"][0]\n",
" print(f\"Prediction: {pred}. Ground-truth: {gt}\")\n",
" "
" print(f\"Prediction: {pred}. Ground-truth: {gt}\")\n"
]
},
{
Expand All @@ -543,7 +543,9 @@
"cell_type": "code",
"execution_count": 9,
"id": "b4e1f99a-a68b-4aeb-bcf2-842f26609b52",
"metadata": {},
"metadata": {
"lines_to_next_cell": 2
},
"outputs": [
{
"name": "stdout",
Expand Down
19 changes: 12 additions & 7 deletions bundle/03_mednist_classification_v2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,9 @@
"cell_type": "code",
"execution_count": 4,
"id": "0cb1b023-d192-4ad7-b2eb-c4a2c6b42b84",
"metadata": {},
"metadata": {
"lines_to_next_cell": 2
},
"outputs": [
{
"name": "stdout",
Expand Down Expand Up @@ -233,7 +235,9 @@
"cell_type": "code",
"execution_count": 5,
"id": "d11681af-3210-4b2b-b7bd-8ad8dedfe230",
"metadata": {},
"metadata": {
"lines_to_next_cell": 2
},
"outputs": [
{
"name": "stdout",
Expand Down Expand Up @@ -287,8 +291,7 @@
"- _target_: EnsureChannelFirstd\n",
" keys: '@image'\n",
"- _target_: ScaleIntensityd\n",
" keys: '@image'\n",
" "
" keys: '@image'\n"
]
},
{
Expand All @@ -306,7 +309,9 @@
"cell_type": "code",
"execution_count": 6,
"id": "4dfd052e-abe7-473a-bbf4-25674a3b20ea",
"metadata": {},
"metadata": {
"lines_to_next_cell": 2
},
"outputs": [
{
"name": "stdout",
Expand All @@ -322,7 +327,7 @@
"max_epochs: 25\n",
"learning_rate: 0.00001 # learning rate, again artificially slow\n",
"val_interval: 1 # run validation every n'th epoch\n",
"save_interval: 1 # save the model weights every n'th epoch\n",
"save_interval: 1 # save the model weights every n'th epoch\n",
"\n",
"# choose a unique output subdirectory every time training is started, \n",
"output_dir: '$datetime.datetime.now().strftime(@root_dir+''/output/output_%y%m%d_%H%M%S'')'\n",
Expand Down Expand Up @@ -429,7 +434,7 @@
" output_transform: '$lambda x: None'\n",
"- _target_: LogfileHandler\n",
" output_dir: '@output_dir'\n",
" \n",
"\n",
"# Metrics to assess validation results, you can have more than one here but may \n",
"# need to adapt the format of pred and label.\n",
"metrics:\n",
Expand Down
33 changes: 18 additions & 15 deletions bundle/04_integrating_code.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@
"execution_count": 3,
"id": "dcdbe1ae-ea13-49cb-b5a3-3c2c78f91f2b",
"metadata": {
"lines_to_next_cell": 2,
"tags": []
},
"outputs": [
Expand All @@ -224,7 +225,6 @@
"import torch.nn as nn\n",
"import torch.nn.functional as F\n",
"\n",
"\n",
"class Net(nn.Module):\n",
" def __init__(self):\n",
" super().__init__()\n",
Expand Down Expand Up @@ -257,7 +257,9 @@
"cell_type": "code",
"execution_count": 4,
"id": "189d71c5-6556-4891-a382-0adbc8f80d30",
"metadata": {},
"metadata": {
"lines_to_next_cell": 2
},
"outputs": [
{
"name": "stdout",
Expand All @@ -274,14 +276,15 @@
"\n",
"transform = transforms.Compose(\n",
" [transforms.ToTensor(),\n",
" transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])\n"
" transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "3d8f233e-495c-450c-a445-46d295ba7461",
"metadata": {
"lines_to_next_cell": 2,
"tags": []
},
"outputs": [
Expand All @@ -301,17 +304,18 @@
"\n",
"batch_size = 4\n",
"\n",
"\n",
"def get_dataloader(is_training, transform):\n",
" \n",
"\n",
" if is_training:\n",
" trainset = torchvision.datasets.CIFAR10(root='./data', train=True,\n",
" download=True, transform=transform)\n",
" download=True, transform=transform)\n",
" trainloader = torch.utils.data.DataLoader(trainset, batch_size=batch_size,\n",
" shuffle=True, num_workers=2)\n",
" return trainloader\n",
" else:\n",
" testset = torchvision.datasets.CIFAR10(root='./data', train=False,\n",
" download=True, transform=transform)\n",
" download=True, transform=transform)\n",
" testloader = torch.utils.data.DataLoader(testset, batch_size=batch_size,\n",
" shuffle=False, num_workers=2)\n",
" return testloader "
Expand Down Expand Up @@ -347,8 +351,7 @@
"import torch.nn as nn\n",
"import torch.optim as optim\n",
"\n",
"\n",
"def train(net,trainloader):\n",
"def train(net, trainloader):\n",
" criterion = nn.CrossEntropyLoss()\n",
" optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)\n",
"\n",
Expand Down Expand Up @@ -390,6 +393,7 @@
"execution_count": 7,
"id": "0b9764a8-674c-42ae-ad4b-f2dea027bdbf",
"metadata": {
"lines_to_next_cell": 2,
"tags": []
},
"outputs": [
Expand Down Expand Up @@ -542,11 +546,10 @@
"\n",
"import torch\n",
"\n",
"\n",
"def test(net, testloader):\n",
" correct = 0\n",
" total = 0\n",
" \n",
"\n",
" with torch.no_grad():\n",
" for data in testloader:\n",
" images, labels = data\n",
Expand All @@ -555,15 +558,16 @@
" total += labels.size(0)\n",
" correct += (predicted == labels).sum().item()\n",
"\n",
" print(f'Accuracy of the network on the 10000 test images: {100 * correct // total} %')\n",
" "
" print(f'Accuracy of the network on the 10000 test images: {100 * correct // total} %')\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "fb49aef2-9fb5-4e74-83d2-9da935e07648",
"metadata": {},
"metadata": {
"lines_to_next_cell": 2
},
"outputs": [
{
"name": "stdout",
Expand Down Expand Up @@ -678,13 +682,12 @@
"import torch\n",
"from PIL import Image\n",
"\n",
"\n",
"def inference(net, transforms, filenames):\n",
" for fn in filenames:\n",
" with Image.open(fn) as im:\n",
" tim=transforms(im)\n",
" outputs=net(tim[None])\n",
" _, predictions = torch.max(outputs, 1)\n",
" _, predictions=torch.max(outputs, 1)\n",
" print(fn, predictions[0].item())"
]
},
Expand Down
2 changes: 1 addition & 1 deletion bundle/05_spleen_segmentation_lightning.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1187,7 +1187,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.18"
"version": "3.10.12"
}
},
"nbformat": 4,
Expand Down
2 changes: 1 addition & 1 deletion computer_assisted_intervention/video_seg.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@
" iou_list.append(epoch_iou)\n",
" epoch_loss /= len(dl)\n",
" losses.append(epoch_loss)\n",
" tr.set_description(f\"Loss: {epoch_loss:.4f}\")\n",
" tr.set_description(f\"Loss: {epoch_loss:.4f}\") # noqa: B038\n",
"\n",
"fig, ax = plt.subplots(1, 1, figsize=(6, 6), facecolor=\"white\")\n",
"ax.set_xlabel(\"Epoch\")\n",
Expand Down
1 change: 1 addition & 0 deletions deployment/ray/mednist_classifier_ray.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@
"\n",
"ray.init(address=\"auto\", namespace=\"serve\")\n",
"\n",
"\n",
"@serve.deployment\n",
"class MedNISTClassifier:\n",
" def __init__(self):\n",
Expand Down
Loading

0 comments on commit 2b41e8c

Please sign in to comment.