Skip to content

fix pep 8 for all previously-excluded notebooks #1137

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

Merged
merged 6 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
88 changes: 52 additions & 36 deletions 3d_segmentation/spleen_segmentation_3d_visualization_basic.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -535,15 +535,15 @@
"# standard PyTorch program style: create UNet, DiceLoss and Adam optimizer\n",
"device = torch.device(\"cuda:0\")\n",
"\n",
"UNet_meatdata = dict(\n",
" spatial_dims=3,\n",
" in_channels=1,\n",
" out_channels=2,\n",
" channels=(16, 32, 64, 128, 256),\n",
" strides=(2, 2, 2, 2),\n",
" num_res_units=2,\n",
" norm=Norm.BATCH\n",
")\n",
"UNet_meatdata = {\n",
" \"spatial_dims\": 3,\n",
" \"in_channels\": 1,\n",
" \"out_channels\": 2,\n",
" \"channels\": (16, 32, 64, 128, 256),\n",
" \"strides\": (2, 2, 2, 2),\n",
" \"num_res_units\": 2,\n",
" \"norm\": Norm.BATCH,\n",
"}\n",
"\n",
"model = UNet(**UNet_meatdata).to(device)\n",
"loss_function = DiceLoss(to_onehot_y=True, softmax=True)\n",
Expand All @@ -554,7 +554,9 @@
"Optimizer_metadata = {}\n",
"for ind, param_group in enumerate(optimizer.param_groups):\n",
" optim_meta_keys = list(param_group.keys())\n",
" Optimizer_metadata[f'param_group_{ind}'] = {key: value for (key, value) in param_group.items() if 'params' not in key}"
" Optimizer_metadata[f'param_group_{ind}'] = {\n",
" key: value for (key, value) in param_group.items() if 'params' not in key\n",
" }"
]
},
{
Expand Down Expand Up @@ -612,26 +614,28 @@
" loss.backward()\n",
" optimizer.step()\n",
" epoch_loss += loss.item()\n",
" print(f\"{step}/{len(train_ds) // train_loader.batch_size}, \"\n",
" f\"train_loss: {loss.item():.4f}\")\n",
" print(\n",
" f\"{step}/{len(train_ds) // train_loader.batch_size}, \"\n",
" f\"train_loss: {loss.item():.4f}\"\n",
" )\n",
" # track batch loss metric\n",
" aim_run.track(loss.item(), name=\"batch_loss\", context={'type':loss_type})\n",
" \n",
" aim_run.track(loss.item(), name=\"batch_loss\", context={'type': loss_type})\n",
"\n",
" epoch_loss /= step\n",
" epoch_loss_values.append(epoch_loss)\n",
" \n",
"\n",
" # track epoch loss metric\n",
" aim_run.track(epoch_loss, name=\"epoch_loss\", context={'type':loss_type})\n",
" aim_run.track(epoch_loss, name=\"epoch_loss\", context={'type': loss_type})\n",
"\n",
" print(f\"epoch {epoch + 1} average loss: {epoch_loss:.4f}\")\n",
"\n",
" if (epoch + 1) % val_interval == 0:\n",
" if (epoch + 1) % val_interval * 2 == 0:\n",
" # track model params and gradients\n",
" track_params_dists(model,aim_run)\n",
" track_params_dists(model, aim_run)\n",
" # THIS SEGMENT TAKES RELATIVELY LONG (Advise Against it)\n",
" track_gradients_dists(model, aim_run)\n",
" \n",
"\n",
" model.eval()\n",
" with torch.no_grad():\n",
" for index, val_data in enumerate(val_loader):\n",
Expand All @@ -643,28 +647,35 @@
" sw_batch_size = 4\n",
" val_outputs = sliding_window_inference(\n",
" val_inputs, roi_size, sw_batch_size, model)\n",
" \n",
"\n",
" # tracking input, label and output images with Aim\n",
" output = torch.argmax(val_outputs, dim=1)[0, :, :, slice_to_track].float()\n",
" \n",
" aim_run.track(aim.Image(val_inputs[0, 0, :, :, slice_to_track], \\\n",
" caption=f'Input Image: {index}'), \\\n",
" name='validation', context={'type':'input'})\n",
" aim_run.track(aim.Image(val_labels[0, 0, :, :, slice_to_track], \\\n",
" caption=f'Label Image: {index}'), \\\n",
" name='validation', context={'type':'label'})\n",
" aim_run.track(aim.Image(output, caption=f'Predicted Label: {index}'), \\\n",
" name = 'predictions', context={'type':'labels'}) \n",
"\n",
" aim_run.track(\n",
" aim.Image(val_inputs[0, 0, :, :, slice_to_track], caption=f'Input Image: {index}'),\n",
" name='validation',\n",
" context={'type': 'input'},\n",
" )\n",
" aim_run.track(\n",
" aim.Image(val_labels[0, 0, :, :, slice_to_track], caption=f'Label Image: {index}'),\n",
" name='validation',\n",
" context={'type': 'label'},\n",
" )\n",
" aim_run.track(\n",
" aim.Image(output, caption=f'Predicted Label: {index}'),\n",
" name='predictions',\n",
" context={'type': 'labels'},\n",
" )\n",
"\n",
" val_outputs = [post_pred(i) for i in decollate_batch(val_outputs)]\n",
" val_labels = [post_label(i) for i in decollate_batch(val_labels)]\n",
" # compute metric for current iteration\n",
" dice_metric(y_pred=val_outputs, y=val_labels)\n",
" \n",
"\n",
" # aggregate the final mean dice result\n",
" metric = dice_metric.aggregate().item()\n",
" # track val metric\n",
" aim_run.track(metric, name=\"val_metric\", context={'type':loss_type})\n",
" aim_run.track(metric, name=\"val_metric\", context={'type': loss_type})\n",
"\n",
" # reset the status for next validation round\n",
" dice_metric.reset()\n",
Expand All @@ -675,16 +686,16 @@
" best_metric_epoch = epoch + 1\n",
" torch.save(model.state_dict(), os.path.join(\n",
" root_dir, \"best_metric_model.pth\"))\n",
" \n",
"\n",
" best_model_log_message = f\"saved new best metric model at the {epoch+1}th epoch\"\n",
" aim_run.track(aim.Text(best_model_log_message), name='best_model_log_message', epoch=epoch+1)\n",
" print(best_model_log_message)\n",
" \n",
"\n",
" message1 = f\"current epoch: {epoch + 1} current mean dice: {metric:.4f}\"\n",
" message2 = f\"\\nbest mean dice: {best_metric:.4f} \"\n",
" message3 = f\"at epoch: {best_metric_epoch}\"\n",
" \n",
" aim_run.track(aim.Text(message1 +\"\\n\" + message2 + message3), name='epoch_summary', epoch=epoch+1)\n",
"\n",
" aim_run.track(aim.Text(message1 + \"\\n\" + message2 + message3), name='epoch_summary', epoch=epoch+1)\n",
" print(message1, message2, message3)\n"
]
},
Expand Down Expand Up @@ -903,7 +914,7 @@
"provenance": []
},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -917,7 +928,12 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.13"
"version": "3.8.13 | packaged by conda-forge | (default, Mar 25 2022, 06:04:10) \n[GCC 10.3.0]"
},
"vscode": {
"interpreter": {
"hash": "d4d1e4263499bec80672ea0156c357c1ee493ec2b1c70f0acce89fc37c4a6abe"
}
}
},
"nbformat": 4,
Expand Down
80 changes: 40 additions & 40 deletions 3d_segmentation/swin_unetr_brats21_segmentation_3d.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@
" json_data = json_data[key]\n",
"\n",
" for d in json_data:\n",
" for k, v in d.items():\n",
" for k in d:\n",
" if isinstance(d[k], list):\n",
" d[k] = [os.path.join(basedir, iv) for iv in d[k]]\n",
" elif isinstance(d[k], str):\n",
Expand Down Expand Up @@ -559,17 +559,17 @@
" acc_func(y_pred=val_output_convert, y=val_labels_list)\n",
" acc, not_nans = acc_func.aggregate()\n",
" run_acc.update(acc.cpu().numpy(), n=not_nans.cpu().numpy())\n",
" Dice_TC = run_acc.avg[0]\n",
" Dice_WT = run_acc.avg[1]\n",
" Dice_ET = run_acc.avg[2]\n",
" dice_tc = run_acc.avg[0]\n",
" dice_wt = run_acc.avg[1]\n",
" dice_et = run_acc.avg[2]\n",
" print(\n",
" \"Val {}/{} {}/{}\".format(epoch, max_epochs, idx, len(loader)),\n",
" \", Dice_TC:\",\n",
" Dice_TC,\n",
" \", Dice_WT:\",\n",
" Dice_WT,\n",
" \", Dice_ET:\",\n",
" Dice_ET,\n",
" \", dice_tc:\",\n",
" dice_tc,\n",
" \", dice_wt:\",\n",
" dice_wt,\n",
" \", dice_et:\",\n",
" dice_et,\n",
" \", time {:.2f}s\".format(time.time() - start_time),\n",
" )\n",
" start_time = time.time()\n",
Expand Down Expand Up @@ -605,10 +605,10 @@
"):\n",
"\n",
" val_acc_max = 0.0\n",
" Dices_TC = []\n",
" Dices_WT = []\n",
" Dices_ET = []\n",
" Dices_avg = []\n",
" dices_tc = []\n",
" dices_wt = []\n",
" dices_et = []\n",
" dices_avg = []\n",
" loss_epochs = []\n",
" trains_epoch = []\n",
" for epoch in range(start_epoch, max_epochs):\n",
Expand Down Expand Up @@ -640,26 +640,26 @@
" post_sigmoid=post_sigmoid,\n",
" post_pred=post_pred,\n",
" )\n",
" Dice_TC = val_acc[0]\n",
" Dice_WT = val_acc[1]\n",
" Dice_ET = val_acc[2]\n",
" dice_tc = val_acc[0]\n",
" dice_wt = val_acc[1]\n",
" dice_et = val_acc[2]\n",
" val_avg_acc = np.mean(val_acc)\n",
" print(\n",
" \"Final validation stats {}/{}\".format(epoch, max_epochs - 1),\n",
" \", Dice_TC:\",\n",
" Dice_TC,\n",
" \", Dice_WT:\",\n",
" Dice_WT,\n",
" \", Dice_ET:\",\n",
" Dice_ET,\n",
" \", dice_tc:\",\n",
" dice_tc,\n",
" \", dice_wt:\",\n",
" dice_wt,\n",
" \", dice_et:\",\n",
" dice_et,\n",
" \", Dice_Avg:\",\n",
" val_avg_acc,\n",
" \", time {:.2f}s\".format(time.time() - epoch_time),\n",
" )\n",
" Dices_TC.append(Dice_TC)\n",
" Dices_WT.append(Dice_WT)\n",
" Dices_ET.append(Dice_ET)\n",
" Dices_avg.append(val_avg_acc)\n",
" dices_tc.append(dice_tc)\n",
" dices_wt.append(dice_wt)\n",
" dices_et.append(dice_et)\n",
" dices_avg.append(val_avg_acc)\n",
" if val_avg_acc > val_acc_max:\n",
" print(\"new best ({:.6f} --> {:.6f}). \".format(val_acc_max, val_avg_acc))\n",
" val_acc_max = val_avg_acc\n",
Expand All @@ -672,10 +672,10 @@
" print(\"Training Finished !, Best Accuracy: \", val_acc_max)\n",
" return (\n",
" val_acc_max,\n",
" Dices_TC,\n",
" Dices_WT,\n",
" Dices_ET,\n",
" Dices_avg,\n",
" dices_tc,\n",
" dices_wt,\n",
" dices_et,\n",
" dices_avg,\n",
" loss_epochs,\n",
" trains_epoch,\n",
" )"
Expand All @@ -700,10 +700,10 @@
"\n",
"(\n",
" val_acc_max,\n",
" Dices_TC,\n",
" Dices_WT,\n",
" Dices_ET,\n",
" Dices_avg,\n",
" dices_tc,\n",
" dices_wt,\n",
" dices_et,\n",
" dices_avg,\n",
" loss_epochs,\n",
" trains_epoch,\n",
") = trainer(\n",
Expand Down Expand Up @@ -784,21 +784,21 @@
"plt.subplot(1, 2, 2)\n",
"plt.title(\"Val Mean Dice\")\n",
"plt.xlabel(\"epoch\")\n",
"plt.plot(trains_epoch, Dices_avg, color=\"green\")\n",
"plt.plot(trains_epoch, dices_avg, color=\"green\")\n",
"plt.show()\n",
"plt.figure(\"train\", (18, 6))\n",
"plt.subplot(1, 3, 1)\n",
"plt.title(\"Val Mean Dice TC\")\n",
"plt.xlabel(\"epoch\")\n",
"plt.plot(trains_epoch, Dices_TC, color=\"blue\")\n",
"plt.plot(trains_epoch, dices_tc, color=\"blue\")\n",
"plt.subplot(1, 3, 2)\n",
"plt.title(\"Val Mean Dice WT\")\n",
"plt.xlabel(\"epoch\")\n",
"plt.plot(trains_epoch, Dices_WT, color=\"brown\")\n",
"plt.plot(trains_epoch, dices_wt, color=\"brown\")\n",
"plt.subplot(1, 3, 3)\n",
"plt.title(\"Val Mean Dice ET\")\n",
"plt.xlabel(\"epoch\")\n",
"plt.plot(trains_epoch, Dices_ET, color=\"purple\")\n",
"plt.plot(trains_epoch, dices_et, color=\"purple\")\n",
"plt.show()"
]
},
Expand Down Expand Up @@ -912,7 +912,7 @@
"\n",
"\n",
"with torch.no_grad():\n",
" for idx, batch_data in enumerate(test_loader):\n",
" for batch_data in test_loader:\n",
" image = batch_data[\"image\"].cuda()\n",
" prob = torch.sigmoid(model_inferer_test(image))\n",
" seg = prob[0].detach().cpu().numpy()\n",
Expand Down
20 changes: 11 additions & 9 deletions 3d_segmentation/swin_unetr_btcv_segmentation_3d.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,9 @@
"outputs": [],
"source": [
"data_dir = \"data/\"\n",
"split_JSON = \"dataset_0.json\"\n",
"split_json = \"dataset_0.json\"\n",
"\n",
"datasets = data_dir + split_JSON\n",
"datasets = data_dir + split_json\n",
"datalist = load_decathlon_datalist(datasets, True, \"training\")\n",
"val_files = load_decathlon_datalist(datasets, True, \"validation\")\n",
"train_ds = CacheDataset(\n",
Expand Down Expand Up @@ -496,7 +496,7 @@
"def validation(epoch_iterator_val):\n",
" model.eval()\n",
" with torch.no_grad():\n",
" for step, batch in enumerate(epoch_iterator_val):\n",
" for batch in epoch_iterator_val:\n",
" val_inputs, val_labels = (batch[\"image\"].cuda(), batch[\"label\"].cuda())\n",
" with torch.cuda.amp.autocast():\n",
" val_outputs = sliding_window_inference(val_inputs, (96, 96, 96), 4, model)\n",
Expand Down Expand Up @@ -536,10 +536,7 @@
" scaler.step(optimizer)\n",
" scaler.update()\n",
" optimizer.zero_grad()\n",
" epoch_iterator.set_description(\n",
" \"Training (%d / %d Steps) (loss=%2.5f)\"\n",
" % (global_step, max_iterations, loss)\n",
" )\n",
" epoch_iterator.set_description(f\"Training ({global_step} / {max_iterations} Steps) (loss={loss:2.5f})\")\n",
" if (\n",
" global_step % eval_num == 0 and global_step != 0\n",
" ) or global_step == max_iterations:\n",
Expand Down Expand Up @@ -731,7 +728,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -745,7 +742,12 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.13"
"version": "3.8.13 | packaged by conda-forge | (default, Mar 25 2022, 06:04:10) \n[GCC 10.3.0]"
},
"vscode": {
"interpreter": {
"hash": "d4d1e4263499bec80672ea0156c357c1ee493ec2b1c70f0acce89fc37c4a6abe"
}
}
},
"nbformat": 4,
Expand Down
Loading