Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix flake8 error #1618

Merged
merged 11 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
16 changes: 9 additions & 7 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",
KumoLiu marked this conversation as resolved.
Show resolved Hide resolved
"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,7 +370,7 @@
" num_workers: 4\n",
"\n",
"trainer:\n",
" _target_: SupervisedTrainer\n",
" _target_: SupervisedTrainer\n",
" device: '@device'\n",
" max_epochs: '@max_epochs'\n",
" train_data_loader: '@train_dl'\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
23 changes: 6 additions & 17 deletions deployment/bentoml/mednist_classifier_bentoml.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@
}
KumoLiu marked this conversation as resolved.
Show resolved Hide resolved
],
"source": [
"from bentoml.utils import cached_property # noqa: F401\n",
KumoLiu marked this conversation as resolved.
Show resolved Hide resolved
"from bentoml.adapters import FileInput, JsonOutput # noqa: F401\n",
"from bentoml.frameworks.pytorch import PytorchModelArtifact # noqa: F401\n",
"import bentoml # noqa: F401\n",
"from PIL import Image # noqa: F401\n",
"from typing import BinaryIO, List # noqa: F401\n",
"import os\n",
"import shutil\n",
"import tempfile\n",
Expand Down Expand Up @@ -373,23 +379,6 @@
"source": [
"%%writefile mednist_classifier_bentoml.py\n",
"\n",
"from typing import BinaryIO, List\n",
"import numpy as np\n",
"from PIL import Image\n",
"import torch\n",
"\n",
"from monai.transforms import (\n",
" EnsureChannelFirst,\n",
" Compose,\n",
" Transform,\n",
" ScaleIntensity,\n",
" EnsureType,\n",
")\n",
"\n",
"import bentoml\n",
"from bentoml.frameworks.pytorch import PytorchModelArtifact\n",
"from bentoml.adapters import FileInput, JsonOutput\n",
"from bentoml.utils import cached_property\n",
"\n",
"MEDNIST_CLASSES = [\"AbdomenCT\", \"BreastMRI\", \"CXR\", \"ChestCT\", \"Hand\", \"HeadCT\"]\n",
"\n",
Expand Down
16 changes: 1 addition & 15 deletions deployment/ray/mednist_classifier_ray.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -348,26 +348,12 @@
"source": [
KumoLiu marked this conversation as resolved.
Show resolved Hide resolved
"%%writefile mednist_classifier_start.py\n",
"\n",
"import io\n",
KumoLiu marked this conversation as resolved.
Show resolved Hide resolved
"from PIL import Image\n",
"import torch\n",
"import numpy as np\n",
"\n",
"import ray\n",
"from ray import serve\n",
"\n",
"from monai.config import print_config\n",
"from monai.transforms import (\n",
" EnsureChannelFirst,\n",
" Compose,\n",
" ScaleIntensity,\n",
" EnsureType,\n",
")\n",
"\n",
"MEDNIST_CLASSES = [\"AbdomenCT\", \"BreastMRI\", \"CXR\", \"ChestCT\", \"Hand\", \"HeadCT\"]\n",
"\n",
"ray.init(address=\"auto\", namespace=\"serve\")\n",
"\n",
"\n",
"@serve.deployment\n",
"class MedNISTClassifier:\n",
" def __init__(self):\n",
Expand Down
Loading
Loading