Skip to content

Commit ab7c050

Browse files
authored
Simplify the notebook (WIP)
1 parent 735c1de commit ab7c050

File tree

1 file changed

+38
-157
lines changed

1 file changed

+38
-157
lines changed

playing_with_pytorch.ipynb

Lines changed: 38 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,23 @@
3737
"id": "oWi4McPs_NuY"
3838
},
3939
"source": [
40-
"!pip install -qq SimpleITK"
40+
"!pip install -qq SimpleITK nnunetv2"
4141
],
4242
"execution_count": null,
4343
"outputs": []
4444
},
45+
{
46+
"cell_type": "code",
47+
"source": [
48+
"%cd /content\n",
49+
"!git clone https://github.com/woctezuma/playing-with-simpleitk.git"
50+
],
51+
"metadata": {
52+
"id": "vL5WJI3NVxIe"
53+
},
54+
"execution_count": null,
55+
"outputs": []
56+
},
4557
{
4658
"cell_type": "markdown",
4759
"metadata": {
@@ -163,18 +175,6 @@
163175
"execution_count": null,
164176
"outputs": []
165177
},
166-
{
167-
"cell_type": "code",
168-
"source": [
169-
"%cd /content\n",
170-
"!git clone https://github.com/woctezuma/playing-with-simpleitk.git\n"
171-
],
172-
"metadata": {
173-
"id": "h2Jbh5kO7Z9v"
174-
},
175-
"execution_count": null,
176-
"outputs": []
177-
},
178178
{
179179
"cell_type": "code",
180180
"source": [
@@ -204,8 +204,8 @@
204204
"id": "HbPo9F8kB31W"
205205
},
206206
"source": [
207-
"%mkdir -p /content/data\n",
208-
"%mv /content/playing-with-simpleitk/data/output/* /content/data/"
207+
"%mkdir -p /content/nnUNet_base/Dataset000_MY_DATASET\n",
208+
"%mv /content/playing-with-simpleitk/data/output/* /content/nnUNet_base/Dataset000_MY_DATASET/"
209209
],
210210
"execution_count": null,
211211
"outputs": []
@@ -545,8 +545,7 @@
545545
"\n",
546546
" output_image_name = input_image_name\n",
547547
" sitk.WriteImage(output_image, output_image_name)\n",
548-
" print('Image size: {}'.format(output_image.GetSize()))\n",
549-
""
548+
" print('Image size: {}'.format(output_image.GetSize()))\n"
550549
],
551550
"execution_count": null,
552551
"outputs": []
@@ -572,65 +571,27 @@
572571
{
573572
"cell_type": "code",
574573
"source": [
575-
"%cd /content\n",
576-
"!git clone https://github.com/MIC-DKFZ/nnUNet.git\n",
577-
"%cd nnUNet\n",
578-
"%pip install -e ."
574+
"%env nnUNet_raw=/content/nnUNet_base/\n",
575+
"%env nnUNet_preprocessed=/content/nnUNet_preprocessed/\n",
576+
"%env nnUNet_results=/content/RESULTS_FOLDER/"
579577
],
580578
"metadata": {
581579
"id": "l3OXi7a40T5b"
582580
},
583581
"execution_count": null,
584582
"outputs": []
585583
},
586-
{
587-
"cell_type": "markdown",
588-
"metadata": {
589-
"id": "lOP7FXOdLP4G"
590-
},
591-
"source": [
592-
"### Copy data to the right folder"
593-
]
594-
},
595-
{
596-
"cell_type": "code",
597-
"metadata": {
598-
"id": "8EHGKR_uKTNA"
599-
},
600-
"source": [
601-
"%mkdir -p /content/nnUNet_base/nnUNet_raw_splitted/\n",
602-
"%mv /content/data /content/nnUNet_base/nnUNet_raw_splitted/Dataset000_MY_DATASET"
603-
],
604-
"execution_count": null,
605-
"outputs": []
606-
},
607584
{
608585
"cell_type": "markdown",
609586
"metadata": {
610587
"id": "9jdfq0ZASGDX"
611588
},
612589
"source": [
613-
"## Edit dataset.json at the root of Task00_MY_DATASET/\n",
590+
"## Edit dataset.json at the root of Dataset000_MY_DATASET/\n",
614591
"\n",
615-
"Reference: https://github.com/MIC-DKFZ/nnUNet/blob/master/nnunet/dataset_conversion/LiverTumorSegmentationChallenge.py"
592+
"Reference: https://github.com/MIC-DKFZ/nnUNet/tree/master/nnunetv2/dataset_conversion"
616593
]
617594
},
618-
{
619-
"cell_type": "code",
620-
"metadata": {
621-
"id": "yrd4WM8CsFLB"
622-
},
623-
"source": [
624-
"output_folder = \"/content/nnUNet_base/nnUNet_raw_splitted/Dataset000_MY_DATASET/\"\n",
625-
"\n",
626-
"train_ids = [\n",
627-
" 'patientID{}'.format(patient_no)\n",
628-
" for patient_no in get_patient_indices()\n",
629-
"]"
630-
],
631-
"execution_count": null,
632-
"outputs": []
633-
},
634595
{
635596
"cell_type": "code",
636597
"metadata": {
@@ -641,6 +602,8 @@
641602
"import json\n",
642603
"from collections import OrderedDict\n",
643604
"\n",
605+
"output_folder = \"/content/nnUNet_base/Dataset000_MY_DATASET/\"\n",
606+
"\n",
644607
"json_dict = OrderedDict()\n",
645608
"\n",
646609
"json_dict['channel_names'] = {\n",
@@ -652,40 +615,16 @@
652615
" \"liver\": 1,\n",
653616
"}\n",
654617
"\n",
655-
"json_dict['numTraining'] = len(train_ids)\n",
618+
"num_patients = len(get_patient_indices())\n",
619+
"json_dict['numTraining'] = num_patients\n",
656620
"json_dict['file_ending'] = \".nii.gz\"\n",
657621
"\n",
658-
"# json_dict['training'] = [{'image': \"./imagesTr/%s.nii.gz\" % i, \"label\": \"./labelsTr/%s.nii.gz\" % i} for i in train_ids]\n",
659-
"# json_dict['test'] = [\"./imagesTs/%s.nii.gz\" % i for i in test_ids]\n",
660-
"\n",
661622
"with open(os.path.join(output_folder, \"dataset.json\"), 'w') as f:\n",
662623
" json.dump(json_dict, f, indent=4, sort_keys=True)"
663624
],
664625
"execution_count": null,
665626
"outputs": []
666627
},
667-
{
668-
"cell_type": "markdown",
669-
"metadata": {
670-
"id": "IhKCU_jl0gIU"
671-
},
672-
"source": [
673-
"## How many processes (pl, pf) on Google Colab?\n",
674-
"\n",
675-
"Apparently, 2.\n"
676-
]
677-
},
678-
{
679-
"cell_type": "code",
680-
"metadata": {
681-
"id": "Tj-lOLy26lJt"
682-
},
683-
"source": [
684-
"!cat /proc/cpuinfo"
685-
],
686-
"execution_count": null,
687-
"outputs": []
688-
},
689628
{
690629
"cell_type": "markdown",
691630
"metadata": {
@@ -698,31 +637,7 @@
698637
{
699638
"cell_type": "code",
700639
"source": [
701-
"%cd /content/nnUNet/nnunetv2"
702-
],
703-
"metadata": {
704-
"id": "FO53PI9-23v1"
705-
},
706-
"execution_count": null,
707-
"outputs": []
708-
},
709-
{
710-
"cell_type": "code",
711-
"source": [
712-
"%env nnUNet_raw=/content/nnUNet_base/nnUNet_raw_splitted/\n",
713-
"%env nnUNet_preprocessed=/content/nnUNet_preprocessed/\n",
714-
"%env nnUNet_results=/content/RESULTS_FOLDER/"
715-
],
716-
"metadata": {
717-
"id": "0v4Ty7Vi9Uzh"
718-
},
719-
"execution_count": null,
720-
"outputs": []
721-
},
722-
{
723-
"cell_type": "code",
724-
"source": [
725-
"!nnUNetv2_plan_and_preprocess -d 0 --verify_dataset_integrity"
640+
"!nnUNetv2_plan_and_preprocess -d 000 --verify_dataset_integrity"
726641
],
727642
"metadata": {
728643
"id": "JrqUAqQH3GQ8"
@@ -750,24 +665,16 @@
750665
"## Training"
751666
]
752667
},
753-
{
754-
"cell_type": "code",
755-
"metadata": {
756-
"id": "Ns26aJUi2O1V"
757-
},
758-
"source": [
759-
"!python run/run_training.py -h"
760-
],
761-
"execution_count": null,
762-
"outputs": []
763-
},
764668
{
765669
"cell_type": "code",
766670
"metadata": {
767671
"id": "WC_sIpdZ2E8I"
768672
},
769673
"source": [
770-
"!OMP_NUM_THREADS=1 python run/run_training.py Dataset000_MY_DATASET 3d_fullres all"
674+
"!nnUNetv2_train Dataset000_MY_DATASET 3d_fullres all\n",
675+
"\n",
676+
"# To continue training, add --c as follows:\n",
677+
"# !nnUNetv2_train Dataset000_MY_DATASET 3d_fullres all --c"
771678
],
772679
"execution_count": null,
773680
"outputs": []
@@ -781,24 +688,13 @@
781688
"## Inference"
782689
]
783690
},
784-
{
785-
"cell_type": "code",
786-
"metadata": {
787-
"id": "cNDhwjmSsrCe"
788-
},
789-
"source": [
790-
"!OMP_NUM_THREADS=1 python inference/predict_from_raw_data.py -h"
791-
],
792-
"execution_count": null,
793-
"outputs": []
794-
},
795691
{
796692
"cell_type": "code",
797693
"metadata": {
798694
"id": "NujgNKHarzLw"
799695
},
800696
"source": [
801-
"INPUT_FOLDER='/content/nnUNet_base/nnUNet_raw_splitted/Dataset000_MY_DATASET/imagesTr/'"
697+
"INPUT_FOLDER='/content/nnUNet_base/Dataset000_MY_DATASET/imagesTr/'"
802698
],
803699
"execution_count": null,
804700
"outputs": []
@@ -837,24 +733,18 @@
837733
"execution_count": null,
838734
"outputs": []
839735
},
840-
{
841-
"cell_type": "code",
842-
"source": [
843-
"%pip install nnunetv2"
844-
],
845-
"metadata": {
846-
"id": "HcvYQtXfDssP"
847-
},
848-
"execution_count": null,
849-
"outputs": []
850-
},
851736
{
852737
"cell_type": "code",
853738
"metadata": {
854739
"id": "zf9hZKqCrNph"
855740
},
856741
"source": [
857-
"!nnUNetv2_predict -i $INPUT_FOLDER -o $OUTPUT_FOLDER -d Dataset000_MY_DATASET -f all -c 3d_fullres"
742+
"for fname in [\"2d\", \"3d_fullres\"]\n",
743+
" %cd /content/RESULTS_FOLDER/Dataset000_MY_DATASET/nnUNetTrainer__nnUNetPlans__{fname}/fold_all\n",
744+
" %cp checkpoint_best.pth checkpoint_final.pth\n",
745+
"\n",
746+
"# !nnUNetv2_predict -i $INPUT_FOLDER -o $OUTPUT_FOLDER -d Dataset000_MY_DATASET -f all -c 3d_fullres\n",
747+
"!nnUNetv2_predict -i $INPUT_FOLDER -o $OUTPUT_FOLDER -d Dataset000_MY_DATASET -f all -c 2d"
858748
],
859749
"execution_count": null,
860750
"outputs": []
@@ -878,7 +768,7 @@
878768
"import SimpleITK as sitk\n",
879769
"\n",
880770
"dice_scores = []\n",
881-
"original_data_folder = '/content/nnUNet_base/nnUNet_raw_splitted/Dataset000_MY_DATASET/'\n",
771+
"original_data_folder = '/content/nnUNet_base/Dataset000_MY_DATASET/'\n",
882772
"downsampled_data_folder = '/content/data/'\n",
883773
"\n",
884774
"for patient_no in get_patient_indices():\n",
@@ -913,15 +803,6 @@
913803
],
914804
"execution_count": null,
915805
"outputs": []
916-
},
917-
{
918-
"cell_type": "markdown",
919-
"metadata": {
920-
"id": "mNWntxcMtOpx"
921-
},
922-
"source": [
923-
"[DONE] Download data and display segmentation overlayed on CT images with ITK-SNAP."
924-
]
925806
}
926807
]
927808
}

0 commit comments

Comments
 (0)