Commit acb63c26 authored by cc215's avatar cc215 💬
Browse files

add scrpredict script

parent 45edab2c
...@@ -83,7 +83,7 @@ Results will be saved under `test_results` by default ...@@ -83,7 +83,7 @@ Results will be saved under `test_results` by default
## Finetuning ## Finetuning
- open config file (`configs/basic_opt.json`), change model resume path and adjust learning rate to be 0.0001 or 0.00001: - open config file (`configs/basic_opt.json`), change model resume path and adjust learning rate to be 0.0001 or 0.00001:
- "resume_path":"./checkpoints/Unet_LVSA_best.pkl" - "resume_path":"./checkpoints/Unet_LVSA_trained_from_UKBB.pkl" ## model trained on UKBB dataset
- "lr": 0.0001 - "lr": 0.0001
## Output ## Output
......
...@@ -29,11 +29,11 @@ def rescale_intensity(data,new_min=0,new_max=1): ...@@ -29,11 +29,11 @@ def rescale_intensity(data,new_min=0,new_max=1):
:return: data with intensity ranging from 0 to 1 :return: data with intensity ranging from 0 to 1
''' '''
bs, c , h, w = data.size(0),data.size(1),data.size(2), data.size(3) bs, c , h, w = data.size(0),data.size(1),data.size(2), data.size(3)
data = data.reshape(bs, -1) data = data.view(bs, -1)
old_max = torch.max(data, dim=1, keepdim=True).values old_max = torch.max(data, dim=1, keepdim=True).values
old_min = torch.min(data, dim=1, keepdim=True).values old_min = torch.min(data, dim=1, keepdim=True).values
new_data = (data - old_min) / (old_max - old_min + 1e-6)*(new_max-new_min)+new_min new_data = (data - old_min) / (old_max - old_min + 1e-6)*(new_max-new_min)+new_min
new_data = new_data.reshape(bs, c, h, w) new_data = new_data.view(bs, c, h, w)
return new_data return new_data
......
...@@ -13,7 +13,7 @@ def cross_entropy_2D(input, target, weight=None, size_average=True): ...@@ -13,7 +13,7 @@ def cross_entropy_2D(input, target, weight=None, size_average=True):
target = target.view(target.numel()) target = target.view(target.numel())
loss = F.nll_loss(log_p, target, weight=weight, size_average=False) loss = F.nll_loss(log_p, target, weight=weight, size_average=False)
if size_average: if size_average:
loss /= float(target.numel()+1e-10) loss /= float(target.numel())
return loss return loss
......
...@@ -406,7 +406,7 @@ def hd_2D_stack(result,reference,pixelspacing=None, connectivity=1): ...@@ -406,7 +406,7 @@ def hd_2D_stack(result,reference,pixelspacing=None, connectivity=1):
else: else:
mean =mean +1000000000 mean =mean +1000000000
c=c+1 c=c+1
print (i) # print (i)
if c==0: if c==0:
return -1 return -1
return mean / c return mean / c
......
{ "name": "Composite",
"data": {
"dataset_name":"UKBB" ,
"readable_frames": ["ED", "ES"],
"train_dir": "/vol/medic02/users/wbai/data/cardiac_atlas/UKBB_2964/sa/train",
"validate_dir": "/vol/medic02/users/wbai/data/cardiac_atlas/UKBB_2964/sa/validation",
"image_format_name": "sa_{frame}.nii.gz",
"label_format_name": "label_sa_{frame}.nii.gz",
"data_aug_policy" :"UKBB_advancedv2",
"if_resample": true,
"new_spacing": [1.25, 1.25, 10],
"keep_z_spacing": true,
"image_size":[224,224,1],
"label_size":[224,224],
"pad_size": [192,192,1],
"crop_size" :[192,192,1],
"num_classes": 4,
"use_cache": false,
"myocardium_only": false,
"ignore_black_slices": true
},
"segmentation_model": {
"network_type": "UNet_64",
"num_classes": 4,
"resume_path":"./checkpoints/Unet_LVSA_trained_from_UKBB.pkl",
"lr": 0.00001,
"optimizer_name": "adam",
"n_epochs": 1000,
"max_iteration": 500000,
"batch_size": 20,
"use_gpu": true,
"decoder_dropout": 0.2
},
"adversarial_augmentation":
{
"transformation_type":"composite"
}
,
"output":
{
"save_epoch_every_num_epochs":1,
"save_dir":"./result/Composite"
}
}
\ No newline at end of file
{ "name": "Composite",
"data": {
"dataset_name":"UKBB" ,
"readable_frames": ["ED", "ES"],
"train_dir": "/vol/medic02/users/wbai/data/cardiac_atlas/UKBB_2964/sa/train",
"validate_dir": "/vol/medic02/users/wbai/data/cardiac_atlas/UKBB_2964/sa/validation",
"image_format_name": "sa_{frame}.nii.gz",
"label_format_name": "label_sa_{frame}.nii.gz",
"data_aug_policy" :"UKBB_advanced_z_score",
"if_resample": true,
"new_spacing": [1.25, 1.25, 10],
"keep_z_spacing": true,
"image_size":[256,256,1],
"label_size":[256,256],
"pad_size": [256,256,1],
"crop_size" :[192,192,1],
"num_classes": 4,
"use_cache": false,
"myocardium_only": false,
"ignore_black_slices": true
},
"segmentation_model": {
"network_type": "UNet_64",
"num_classes": 4,
"resume_path":"./checkpoints/Unet_LVSA_trained_from_UKBB.pkl",
"lr": 0.00001,
"optimizer_name": "SGD",
"n_epochs": 1000,
"max_iteration": 500000,
"batch_size": 20,
"use_gpu": true,
"decoder_dropout": 0.1
},
"adversarial_augmentation":
{
"transformation_type":"composite"
}
,
"output":
{
"save_epoch_every_num_epochs":1,
"save_dir":"./result/Composite"
}
}
\ No newline at end of file
{ "name": "Composite",
"data": {
"dataset_name":"UKBB" ,
"readable_frames": ["ED", "ES"],
"train_dir": "/vol/medic02/users/wbai/data/cardiac_atlas/UKBB_2964/sa/train",
"validate_dir": "/vol/medic02/users/wbai/data/cardiac_atlas/UKBB_2964/sa/validation",
"image_format_name": "sa_{frame}.nii.gz",
"label_format_name": "label_sa_{frame}.nii.gz",
"data_aug_policy" :"UKBB_advancedv3",
"if_resample": true,
"new_spacing": [1.25, 1.25, 10],
"keep_z_spacing": true,
"image_size":[256,256,1],
"label_size":[256,256],
"pad_size": [256,256,1],
"crop_size" :[192,192,1],
"num_classes": 4,
"use_cache": false,
"myocardium_only": false,
"ignore_black_slices": true
},
"segmentation_model": {
"network_type": "UNet_64",
"num_classes": 4,
"resume_path":"./checkpoints/Unet_LVSA_trained_from_UKBB.pkl",
"lr": 0.00001,
"optimizer_name": "SGD",
"n_epochs": 1000,
"max_iteration": 500000,
"batch_size": 20,
"use_gpu": true,
"decoder_dropout": 0.1
},
"adversarial_augmentation":
{
"transformation_type":"composite"
}
,
"output":
{
"save_epoch_every_num_epochs":1,
"save_dir":"./result/Composite"
}
}
\ No newline at end of file
{ "name": "Composite",
"data": {
"dataset_name":"UKBB" ,
"readable_frames": ["ED", "ES"],
"train_dir": "/vol/medic02/users/wbai/data/cardiac_atlas/UKBB_2964/sa/train",
"validate_dir": "/vol/medic02/users/wbai/data/cardiac_atlas/UKBB_2964/sa/validation",
"image_format_name": "sa_{frame}.nii.gz",
"label_format_name": "label_sa_{frame}.nii.gz",
"data_aug_policy" :"UKBB_advanced_z_score",
"if_resample": true,
"new_spacing": [1.25, 1.25, 10],
"keep_z_spacing": true,
"image_size":[256,256,1],
"label_size":[256,256],
"pad_size": [256,256,1],
"crop_size" :[192,192,1],
"num_classes": 4,
"use_cache": false,
"myocardium_only": false,
"ignore_black_slices": true
},
"segmentation_model": {
"network_type": "UNet_64",
"num_classes": 4,
"resume_path":"./checkpoints/Unet_LVSA_trained_from_UKBB.pkl",
"lr": 0.00001,
"optimizer_name": "adam",
"n_epochs": 1000,
"max_iteration": 500000,
"batch_size": 20,
"use_gpu": true,
"decoder_dropout": 0.1
},
"adversarial_augmentation":
{
"transformation_type":"composite"
}
,
"output":
{
"save_epoch_every_num_epochs":1,
"save_dir":"./result/Composite"
}
}
\ No newline at end of file
{ "name": "Composite",
"data": {
"dataset_name":"UKBB" ,
"readable_frames": ["ED", "ES"],
"train_dir": "/vol/medic02/users/wbai/data/cardiac_atlas/UKBB_2964/sa/train",
"validate_dir": "/vol/medic02/users/wbai/data/cardiac_atlas/UKBB_2964/sa/validation",
"image_format_name": "sa_{frame}.nii.gz",
"label_format_name": "label_sa_{frame}.nii.gz",
"data_aug_policy" :"UKBB_advancedv3",
"if_resample": true,
"new_spacing": [1.25, 1.25, 10],
"keep_z_spacing": true,
"image_size":[256,256,1],
"label_size":[256,256],
"pad_size": [256,256,1],
"crop_size" :[192,192,1],
"num_classes": 4,
"use_cache": false,
"myocardium_only": false,
"ignore_black_slices": true
},
"segmentation_model": {
"network_type": "UNet_64",
"num_classes": 4,
"resume_path":"./checkpoints/Unet_LVSA_trained_from_UKBB.pkl",
"lr": 0.00001,
"optimizer_name": "SGD",
"n_epochs": 1000,
"max_iteration": 500000,
"batch_size": 20,
"use_gpu": true,
"decoder_dropout": 0.1
},
"adversarial_augmentation":
{
"transformation_type":"bias"
}
,
"output":
{
"save_epoch_every_num_epochs":1,
"save_dir":"./result/Composite"
}
}
\ No newline at end of file
{ "name": "Composite",
"data": {
"dataset_name":"UKBB" ,
"readable_frames": ["ED", "ES"],
"train_dir": "/vol/medic02/users/wbai/data/cardiac_atlas/UKBB_2964/sa/train",
"validate_dir": "/vol/medic02/users/wbai/data/cardiac_atlas/UKBB_2964/sa/validation",
"image_format_name": "sa_{frame}.nii.gz",
"label_format_name": "label_sa_{frame}.nii.gz",
"data_aug_policy" :"UKBB_advancedv3",
"if_resample": true,
"new_spacing": [1.25, 1.25, 10],
"keep_z_spacing": true,
"image_size":[256,256,1],
"label_size":[256,256],
"pad_size": [256,256,1],
"crop_size" :[192,192,1],
"num_classes": 4,
"use_cache": false,
"myocardium_only": false,
"ignore_black_slices": true
},
"segmentation_model": {
"network_type": "UNet_64",
"num_classes": 4,
"resume_path":"./checkpoints/Unet_LVSA_trained_from_UKBB.pkl",
"lr": 0.00001,
"optimizer_name": "SGD",
"n_epochs": 1000,
"max_iteration": 500000,
"batch_size": 20,
"use_gpu": true,
"decoder_dropout": 0.1
},
"adversarial_augmentation":
{
"transformation_type":"composite",
"optimization_mode":"independent"
}
,
"output":
{
"save_epoch_every_num_epochs":1,
"save_dir":"./result/Composite"
}
}
\ No newline at end of file
{ "name": "Composite",
"data": {
"dataset_name":"UKBB" ,
"readable_frames": ["ED", "ES"],
"train_dir": "/vol/medic02/users/wbai/data/cardiac_atlas/UKBB_2964/sa/train",
"validate_dir": "/vol/medic02/users/wbai/data/cardiac_atlas/UKBB_2964/sa/validation",
"image_format_name": "sa_{frame}.nii.gz",
"label_format_name": "label_sa_{frame}.nii.gz",
"data_aug_policy" :"UKBB_advancedv2",
"if_resample": true,
"new_spacing": [1.25, 1.25, 10],
"keep_z_spacing": true,
"image_size":[224,224,1],
"label_size":[224,224],
"pad_size": [192,192,1],
"crop_size" :[192,192,1],
"num_classes": 4,
"use_cache": false,
"myocardium_only": false,
"ignore_black_slices": true
},
"segmentation_model": {
"network_type": "UNet_64",
"num_classes": 4,
"resume_path":"./checkpoints/Unet_LVSA_trained_from_UKBB.pkl",
"lr": 0.00001,
"optimizer_name": "adam",
"n_epochs": 1000,
"max_iteration": 500000,
"batch_size": 20,
"use_gpu": true,
"decoder_dropout": 0.2
},
"adversarial_augmentation":
{
"transformation_type":"composite"
}
,
"output":
{
"save_epoch_every_num_epochs":1,
"save_dir":"./result/Composite"
}
}
\ No newline at end of file
{ "name": "Composite",
"data": {
"dataset_name":"UKBB" ,
"readable_frames": ["ED", "ES"],
"train_dir": "/vol/medic02/users/wbai/data/cardiac_atlas/UKBB_2964/sa/train",
"validate_dir": "/vol/medic02/users/wbai/data/cardiac_atlas/UKBB_2964/sa/validation",
"image_format_name": "sa_{frame}.nii.gz",
"label_format_name": "label_sa_{frame}.nii.gz",
"data_aug_policy" :"UKBB_advancedv3",
"if_resample": true,
"new_spacing": [1.25, 1.25, 10],
"keep_z_spacing": true,
"image_size":[256,256,1],
"label_size":[256,256],
"pad_size": [256,256,1],
"crop_size" :[192,192,1],
"num_classes": 4,
"use_cache": false,
"myocardium_only": false,
"ignore_black_slices": true
},
"segmentation_model": {
"network_type": "UNet_64",
"num_classes": 4,
"resume_path":"./checkpoints/Unet_LVSA_trained_from_UKBB.pkl",
"lr": 0.00001,
"optimizer_name": "SGD",
"n_epochs": 1000,
"max_iteration": 500000,
"batch_size": 20,
"use_gpu": true,
"decoder_dropout": 0.1
},
"adversarial_augmentation":
{
"transformation_type":"composite"
}
,
"output":
{
"save_epoch_every_num_epochs":1,
"save_dir":"./result/Composite"
}
}
\ No newline at end of file
...@@ -6,13 +6,13 @@ ...@@ -6,13 +6,13 @@
"validate_dir": "/vol/medic02/users/wbai/data/cardiac_atlas/UKBB_2964/sa/validation", "validate_dir": "/vol/medic02/users/wbai/data/cardiac_atlas/UKBB_2964/sa/validation",
"image_format_name": "sa_{frame}.nii.gz", "image_format_name": "sa_{frame}.nii.gz",
"label_format_name": "label_sa_{frame}.nii.gz", "label_format_name": "label_sa_{frame}.nii.gz",
"data_aug_policy" :"UKBB_advancedv2", "data_aug_policy" :"UKBB_advancedv3",
"if_resample": true, "if_resample": true,
"new_spacing": [1.25, 1.25, 10], "new_spacing": [1.25, 1.25, 10],
"keep_z_spacing": true, "keep_z_spacing": true,
"image_size":[224,224,1], "image_size":[256,256,1],
"label_size":[224,224], "label_size":[256,256],
"pad_size": [192,192,1], "pad_size": [256,256,1],
"crop_size" :[192,192,1], "crop_size" :[192,192,1],
"num_classes": 4, "num_classes": 4,
"use_cache": false, "use_cache": false,
......
...@@ -9,6 +9,7 @@ import SimpleITK as sitk ...@@ -9,6 +9,7 @@ import SimpleITK as sitk
import numpy as np import numpy as np
from scipy.ndimage.interpolation import map_coordinates from scipy.ndimage.interpolation import map_coordinates
from scipy.ndimage.filters import gaussian_filter from scipy.ndimage.filters import gaussian_filter
from skimage import transform
import torch import torch
...@@ -57,8 +58,7 @@ class MyElasticTransform(object): ...@@ -57,8 +58,7 @@ class MyElasticTransform(object):
return indices return indices
def __call__(self,*inputs): def __call__(self,*inputs):
if abs(self.p_thresh-0)<1e-3 or (not np.random.rand()<self.p_thresh):
if self.p_thresh-0<1e-3 or not np.random.random()<self.p_thresh :
return inputs return inputs
else: else:
#print ('perform elastic transformations') #print ('perform elastic transformations')
...@@ -102,67 +102,52 @@ class MyElasticTransform(object): ...@@ -102,67 +102,52 @@ class MyElasticTransform(object):
class MyElasticTorchTransform(object): class MyElasticTransformCoarseGrid(object):
def __init__(self, alpha=None,sigma=None,random_state=None,is_labelmap=[False, True], p_thresh =0.5): def __init__(self, mu=0,sigma=10,random_state=None,is_labelmap=[False, True], p_thresh =0.5):
""" """
Perform elastic transform Perform elastic transform using 3x3 coarse grid
Elastic deformation of images as described in [Simard2003]_ (with modifications). reference: "Semi-Supervised and Task-Driven Data Augmentation"
.. [Simard2003] Simard, Steinkraus and Platt, "Best Practices for
Convolutional Neural Networks applied to Visual Document Analysis", in
Proc. of the International Conference on Document Analysis and
Recognition, 2003.
Based on https://gist.github.com/erniejunior/601cdf56d2b424757de5
Arguments Arguments
--------- ---------
""" """
self.random_state= random_state self.random_state= random_state
self.sigma = sigma #small values = local deformations, large values = large deformations. self.sigma = sigma
self.alpha =alpha self.mu =mu
self.is_label_map=is_labelmap self.is_label_map=is_labelmap
self.p_thresh =p_thresh ## if prob>p_thresh, them perform elastic transformation self.p_thresh =p_thresh ## if prob>p_thresh, them perform elastic transformation
def gen_deformation_field(self,shape,alpha, sigma): def gen_deformation_field(self,shape, mu=0, sigma=10,order=3):
if self.random_state is None: if self.random_state is None:
random_state = np.random.RandomState(None) random_state = np.random.RandomState(None)
else: else:
random_state =self.random_state random_state =self.random_state
sigma = sigma
alpha= alpha
# Make random fields # Make random fields
dx = random_state.uniform(-1, 1, shape) dx = random_state.normal(mu, sigma, 9)
dy = random_state.uniform(-1, 1, shape) dx_mat = np.reshape(dx,(3,3))
dx = gaussian_filter(dx, sigma=sigma, mode='constant', cval=0) * alpha dy = np.random.normal(mu, sigma, 9)
dy = gaussian_filter(dy, sigma=sigma, mode='constant', cval=0) * alpha dy_mat = np.reshape(dy,(3,3))
dx_img = transform.resize(dx_mat, output_shape=(shape[0],shape[1]), order=order,mode='reflect')
dy_img = transform.resize(dy_mat, output_shape=(shape[0],shape[1]), order=order,mode='reflect')
## deformation field ## deformation field
x, y = np.meshgrid(np.arange(shape[1]), np.arange(shape[0])) x, y = np.meshgrid(np.arange(shape[1]), np.arange(shape[0]))
indices = np.reshape(y + dy, (-1, 1)), np.reshape(x + dx, (-1, 1)) indices = np.reshape(y + dy_img, (-1, 1)), np.reshape(x + dx_img, (-1, 1))
return indices return indices
def __call__(self,*inputs):
if not np.random.random()>self.p_thresh: def __call__(self,*inputs):
if np.random.rand()>self.p_thresh:
return inputs return inputs
else: else:
#print ('perform elastic transformations') # print ('perform elastic transformations')
outputs=[]