HiLab-git / PyMIC_examples

examples of using PyMIC for medical image computing with deep learning
32 stars 4 forks source link

Why 'post_process = KeepLargestComponent' didn't work in testing process. #18

Closed Lucarqi closed 4 months ago

Lucarqi commented 4 months ago

I use Unet2d to segment Myo in CMR. I want to keep the largest region. But it did't work in testing process. Here is my train_val.cfg and test.cfg files.

[dataset]
# tensor type (float or double)
tensor_type = float
task_type = seg
supervise_type = fully_sup
root_dir  = MyoPS_data_dir/data_preprocessed
train_csv = config/data/foldi_train.csv
valid_csv = config/data/foldi_valid.csv
test_csv  = config/data/foldi_valid.csv
modal_num = 1
# batch size
train_batch_size = 18
num_workder = 8

# data transforms
train_transform = [Pad, RandomRotate, RandomCrop, RandomFlip, NormalizeWithMeanStd, GammaCorrection, GaussianNoise, LabelConvert, LabelToProbability]
valid_transform = [NormalizeWithMeanStd, Pad, LabelConvert, LabelToProbability]
test_transform  = [NormalizeWithMeanStd, Pad]

Pad_output_size = [8, 256, 256]
Pad_ceil_mode   = False

RandomRotate_angle_range_d = [-30, 30]
RandomRotate_angle_range_h = None
RandomRotate_angle_range_w = None

RandomCrop_output_size = [6, 224, 224]
RandomCrop_foreground_focus = False
RandomCrop_foreground_ratio = None
Randomcrop_mask_label       = None

RandomFlip_flip_depth  = False
RandomFlip_flip_height = True
RandomFlip_flip_width  = True

NormalizeWithMeanStd_channels = [0]

GammaCorrection_channels  = [0]
GammaCorrection_gamma_min = 0.7
GammaCorrection_gamma_max = 1.5

GaussianNoise_channels = [0]
GaussianNoise_mean     = 0
GaussianNoise_std      = 0.05
GaussianNoise_probability = 0.5

LabelConvert_source_list = [0,1,2]
LabelConvert_target_list = [0,1,1]

LabelToProbability_class_num = 2

[network]
# this section gives parameters for network
# the keys may be different for different networks

# type of network
net_type = UNet2D

# number of class, required for segmentation task
class_num     = 2
in_chns       = 1
feature_chns  = [16, 32, 64, 128, 256]
dropout       = [0, 0, 0.5, 0.5, 0.5]
bilinear      = True
multiscale_pred = False

[training]
# list of gpus
gpus        = [0]

loss_type   = [CrossEntropyLoss, DiceLoss]
loss_weight = [1.0, 1.0]

# for optimizers
optimizer     = Adam
learning_rate = 1e-3
momentum      = 0.9
weight_decay  = 1e-5

# for lr schedular (MultiStepLR)
lr_scheduler  = MultiStepLR
lr_gamma      = 0.5
lr_milestones = [3000, 6000, 9000, 12000]

ckpt_save_dir = model/unet2d/fold_i
ckpt_save_prefix = model

# start iter
iter_start = 0
iter_max   = 16000
iter_valid = 100
iter_save  = [4000, 8000, 12000, 15000, 15200, 15400, 15600, 15800, 16000]

[testing]
# gpu
gpus = [0]
# checkpoint mode can be [0-latest, 1-best, 2-specified]
ckpt_mode         = 1
output_dir        = result/unet2d
post_process      = KeepLargestComponent

sliding_window_enable = True
sliding_window_size   = [6, 224, 224]
sliding_window_stride = [6, 224, 224]
[dataset]
# tensor type (float or double)
tensor_type = float

task_type = seg
root_dir  = projects/RenJi811/data_raw
test_csv  = config/data/data_test.csv

# modality number
modal_num = 1

# data transforms
test_transform  = [NormalizeWithMeanStd, Pad]

NormalizeWithMeanStd_channels = [0]
Pad_output_size = [6,244,244]

[network]
# this section gives parameters for network
# the keys may be different for different networks

# type of network
net_type = UNet2D

# number of class, required for segmentation task
class_num     = 2
in_chns       = 1
feature_chns  = [16, 32, 64, 128, 256]
dropout       = [0, 0, 0.5, 0.5, 0.5]
bilinear      = True
multiscale_pred = False

[training]
# nothing here

[testing]
# list of gpus
gpus   = [0]

# checkpoint mode can be [0-latest, 1-best, 2-specified, 3-multiple ckpt ensemble]
ckpt_mode         = 3
ckpt_name         = [model/unet2d/fold_1/fold_1_15000.pt, model/unet2d/fold_2/fold_2_13300.pt, model/unet2d/fold_3/fold_3_13500.pt, model/unet2d/fold_4/fold_4_12900.pt, model/unet2d/fold_5/fold_5_12500.pt]
output_dir        = result/unet2d_test/

post_process = KeepLargestComponent
KeepLargestComponent_mode = 2

label_source  = [0,1]
label_target  = [0,3]

sliding_window_enable = True
sliding_window_size   = [6, 224, 224]
sliding_window_stride = [6, 224, 224]
Lucarqi commented 4 months ago

Oh, i have figured it out. I use the code from MyoPS2020-UESTC repo. The KeepLargestComponent operation is achieved by run the script python postprocess ....