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
## Finetuning
- 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
## Output
......
......@@ -29,11 +29,11 @@ def rescale_intensity(data,new_min=0,new_max=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)
data = data.reshape(bs, -1)
data = data.view(bs, -1)
old_max = torch.max(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 = new_data.reshape(bs, c, h, w)
new_data = new_data.view(bs, c, h, w)
return new_data
......
......@@ -13,7 +13,7 @@ def cross_entropy_2D(input, target, weight=None, size_average=True):
target = target.view(target.numel())
loss = F.nll_loss(log_p, target, weight=weight, size_average=False)
if size_average:
loss /= float(target.numel()+1e-10)
loss /= float(target.numel())
return loss
......
......@@ -406,7 +406,7 @@ def hd_2D_stack(result,reference,pixelspacing=None, connectivity=1):
else:
mean =mean +1000000000
c=c+1
print (i)
# print (i)
if c==0:
return -1
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 @@
"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",
"data_aug_policy" :"UKBB_advancedv3",
"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],
"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,
......
......@@ -9,6 +9,7 @@ import SimpleITK as sitk
import numpy as np
from scipy.ndimage.interpolation import map_coordinates
from scipy.ndimage.filters import gaussian_filter
from skimage import transform
import torch
......@@ -57,8 +58,7 @@ class MyElasticTransform(object):
return indices
def __call__(self,*inputs):
if self.p_thresh-0<1e-3 or not np.random.random()<self.p_thresh :
if abs(self.p_thresh-0)<1e-3 or (not np.random.rand()<self.p_thresh):
return inputs
else:
#print ('perform elastic transformations')
......@@ -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
Elastic deformation of images as described in [Simard2003]_ (with modifications).
.. [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
Perform elastic transform using 3x3 coarse grid
reference: "Semi-Supervised and Task-Driven Data Augmentation"
Arguments
---------
"""
self.random_state= random_state
self.sigma = sigma #small values = local deformations, large values = large deformations.
self.alpha =alpha
self.sigma = sigma
self.mu =mu
self.is_label_map=is_labelmap
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:
random_state = np.random.RandomState(None)
else:
random_state =self.random_state
sigma = sigma
alpha= alpha
# Make random fields
dx = random_state.uniform(-1, 1, shape)
dy = random_state.uniform(-1, 1, shape)
dx = gaussian_filter(dx, sigma=sigma, mode='constant', cval=0) * alpha
dy = gaussian_filter(dy, sigma=sigma, mode='constant', cval=0) * alpha
dx = random_state.normal(mu, sigma, 9)
dx_mat = np.reshape(dx,(3,3))
dy = np.random.normal(mu, sigma, 9)
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
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
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
else:
#print ('perform elastic transformations')
# print ('perform elastic transformations')
outputs=[]
assert len(self.is_label_map) ==len(inputs), 'for each input, must clarify whether this is a label map or not.'
shape = inputs[0].size() ## C H W
#print (shape)
alpha = self.alpha
sigma = self.sigma
if self.alpha is None:
alpha = shape[1] * np.random.uniform(low=1.5,high=2) ## draw a value in-between
if self.sigma is None:
sigma = shape[1] *np.random.uniform(low=0.1,high=0.2)*3/4
indices = self.gen_deformation_field(shape[1:],alpha,sigma)
indices = self.gen_deformation_field(shape[1:],mu=self.mu,sigma=self.sigma)
for idx, _input in enumerate(inputs):
_input =_input.numpy()
#print (_input.shape)
......
......@@ -310,7 +310,7 @@ class MyNormalizeMedic(object):
# scale the intensity values to be unit norm
std_val = np.std(_input.numpy().flatten())
if np.std(std_val-0)<1e-3:
if np.abs(std_val)<1e-6:
_input = _input
else:_input = _input.div(float(std_val))
......
......@@ -8,13 +8,11 @@ import torch
from torch.utils.data import Dataset
import numpy as np
from common_utils.basic_operations import switch_kv_in_dict
def switch_kv_in_dict(mydict):
switched_dict = {y: x for x, y in mydict.items()}
return switched_dict
class BaseSegDataset(Dataset):
def __init__(self, dataset_name, transform, image_size, label_size, idx2cls_dict=None, num_classes=2,
def __init__(self, dataset_name, transform, no_aug_transform,image_size, label_size, idx2cls_dict=None, num_classes=2,
use_cache=False, formalized_label_dict=None,keep_orig_image_label_pair=False):
'''
......@@ -34,24 +32,27 @@ class BaseSegDataset(Dataset):
self.image_size = image_size
self.label_size = label_size
self.transform = transform
self.no_aug_transform = no_aug_transform
self.idx2cls_dict = idx2cls_dict