コード例 #1
0
 def test_correct_results(self, _, mean, std):
     seed = 0
     gaussian_fn = RandGaussianNoise(prob=1.0, mean=mean, std=std)
     gaussian_fn.set_random_state(seed)
     noised = gaussian_fn(self.imt)
     np.random.seed(seed)
     np.random.random()
     expected = self.imt + np.random.normal(mean, np.random.uniform(0, std), size=self.imt.shape)
     np.testing.assert_allclose(expected, noised)
コード例 #2
0
 def test_correct_results(self, _, im_type, mean, std):
     seed = 0
     gaussian_fn = RandGaussianNoise(prob=1.0, mean=mean, std=std)
     gaussian_fn.set_random_state(seed)
     im = im_type(self.imt)
     noised = gaussian_fn(im)
     np.random.seed(seed)
     np.random.random()
     expected = self.imt + np.random.normal(mean, np.random.uniform(0, std), size=self.imt.shape)
     if isinstance(noised, torch.Tensor):
         noised = noised.cpu()
     np.testing.assert_allclose(expected, noised, atol=1e-5)
コード例 #3
0
import shutil
import tempfile
import unittest

import nibabel as nib
import numpy as np
from parameterized import parameterized

from monai.data import ArrayDataset
from monai.transforms import AddChannel, Compose, LoadNifti, RandAdjustContrast, Spacing, RandGaussianNoise

TEST_CASE_1 = [
    Compose([
        LoadNifti(image_only=True),
        AddChannel(),
        RandGaussianNoise(prob=1.0)
    ]),
    Compose([
        LoadNifti(image_only=True),
        AddChannel(),
        RandGaussianNoise(prob=1.0)
    ]),
    (0, 1),
    (1, 128, 128, 128),
]

TEST_CASE_2 = [
    Compose([
        LoadNifti(image_only=True),
        AddChannel(),
        RandAdjustContrast(prob=1.0)
コード例 #4
0
import os
import shutil
import tempfile
import unittest

import nibabel as nib
import numpy as np
from parameterized import parameterized
from torch.utils.data import DataLoader

from monai.data import ArrayDataset
from monai.transforms import AddChannel, Compose, LoadNifti, RandAdjustContrast, RandGaussianNoise, Spacing

TEST_CASE_1 = [
    Compose([LoadNifti(image_only=True), AddChannel(), RandGaussianNoise(prob=1.0)]),
    Compose([LoadNifti(image_only=True), AddChannel(), RandGaussianNoise(prob=1.0)]),
    (0, 1),
    (1, 128, 128, 128),
]

TEST_CASE_2 = [
    Compose([LoadNifti(image_only=True), AddChannel(), RandAdjustContrast(prob=1.0)]),
    Compose([LoadNifti(image_only=True), AddChannel(), RandAdjustContrast(prob=1.0)]),
    (0, 1),
    (1, 128, 128, 128),
]


class TestCompose(Compose):
    def __call__(self, input_):
コード例 #5
0
import os
import sys
import tempfile
import unittest

import nibabel as nib
import numpy as np
from parameterized import parameterized
from torch.utils.data import DataLoader

from monai.data import ArrayDataset
from monai.transforms import AddChannel, Compose, LoadImage, RandAdjustContrast, RandGaussianNoise, Spacing

TEST_CASE_1 = [
    Compose([LoadImage(image_only=True), AddChannel(), RandGaussianNoise(prob=1.0)]),
    Compose([LoadImage(image_only=True), AddChannel(), RandGaussianNoise(prob=1.0)]),
    (0, 1),
    (1, 128, 128, 128),
]

TEST_CASE_2 = [
    Compose([LoadImage(image_only=True), AddChannel(), RandAdjustContrast(prob=1.0)]),
    Compose([LoadImage(image_only=True), AddChannel(), RandAdjustContrast(prob=1.0)]),
    (0, 1),
    (1, 128, 128, 128),
]


class TestCompose(Compose):
    def __call__(self, input_):
コード例 #6
0
ファイル: resnet_2d_segmenter.py プロジェクト: ciskoh/qu
    def _define_transforms(self):
        """Define and initialize all data transforms.

          * training set images transform
          * training set masks transform
          * validation set images transform
          * validation set masks transform
          * validation set images post-transform
          * test set images transform
          * test set masks transform
          * test set images post-transform
          * prediction set images transform
          * prediction set images post-transform

        @return True if data transforms could be instantiated, False otherwise.
        """

        if self._mask_type == MaskType.UNKNOWN:
            raise Exception("The mask type is unknown. Cannot continue!")

        # Depending on the mask type, we will need to adapt the Mask Loader
        # and Transform. We start by initializing the most common types.
        MaskLoader = LoadMask(self._mask_type)
        MaskTransform = Identity

        # Adapt the transform for the LABEL types
        if self._mask_type == MaskType.TIFF_LABELS or self._mask_type == MaskType.NUMPY_LABELS:
            MaskTransform = ToOneHot(num_classes=self._out_channels)

        # The H5_ONE_HOT type requires a different loader
        if self._mask_type == MaskType.H5_ONE_HOT:
            # MaskLoader: still missing
            raise Exception("HDF5 one-hot masks are not supported yet!")

        # Define transforms for training
        self._train_image_transforms = Compose([
            LoadImage(image_only=True),
            ScaleIntensity(),
            AddChannel(),
            RandSpatialCrop(self._roi_size, random_size=False),
            RandRotate90(prob=0.5, spatial_axes=(0, 1)),
            RandGaussianNoise(prob=0.5, mean=0., std=0.2),
            ToTensor()
        ])
        self._train_mask_transforms = Compose([
            MaskLoader, MaskTransform,
            RandSpatialCrop(self._roi_size, random_size=False),
            RandRotate90(prob=0.5, spatial_axes=(0, 1)),
            RandGaussianNoise(prob=0.5, mean=0., std=0.2),
            ToTensor()
        ])

        # Define transforms for validation
        self._validation_image_transforms = Compose([
            LoadImage(image_only=True),
            ScaleIntensity(),
            AddChannel(),
            ToTensor()
        ])
        self._validation_mask_transforms = Compose(
            [MaskLoader, MaskTransform, ToTensor()])

        # Define transforms for testing
        self._test_image_transforms = Compose([
            LoadImage(image_only=True),
            ScaleIntensity(),
            AddChannel(),
            ToTensor()
        ])
        self._test_mask_transforms = Compose(
            [MaskLoader, MaskTransform, ToTensor()])

        # Define transforms for prediction
        self._prediction_image_transforms = Compose([
            LoadImage(image_only=True),
            ScaleIntensity(),
            AddChannel(),
            ToTensor()
        ])

        # Post transforms
        self._validation_post_transforms = Compose(
            [Activations(softmax=True),
             AsDiscrete(threshold_values=True)])

        self._test_post_transforms = Compose(
            [Activations(softmax=True),
             AsDiscrete(threshold_values=True)])

        self._prediction_post_transforms = Compose(
            [Activations(softmax=True),
             AsDiscrete(threshold_values=True)])
コード例 #7
0
ファイル: train.py プロジェクト: lisun97/Context_Aware_SSL
def main_worker(gpu, ngpus_per_node, args):
    args.gpu = gpu

    # suppress printing if not master
    if args.multiprocessing_distributed and args.gpu != 0:

        def print_pass(*args):
            pass

        builtins.print = print_pass

    if args.gpu is not None:
        print("Use GPU: {} for training".format(args.gpu))

    if args.distributed:
        if args.dist_url == "env://" and args.rank == -1:
            args.rank = int(os.environ["RANK"])
        if args.multiprocessing_distributed:
            # For multiprocessing distributed training, rank needs to be the
            # global rank among all the processes
            args.rank = args.rank * ngpus_per_node + gpu
        dist.init_process_group(backend=args.dist_backend,
                                init_method=args.dist_url,
                                world_size=args.world_size,
                                rank=args.rank)
    if args.rank == 0:
        configure(os.path.join('./exp', args.exp_name))

    # create model
    model_patch = moco.builder_v3.MoCo(Encoder, args.num_patch, args.moco_dim,
                                       args.moco_k_patch, args.moco_m,
                                       args.moco_t, args.mlp)

    model_graph = moco.builder_graph.MoCo(GraphNet, args.gpu, args.moco_dim,
                                          args.moco_k_graph, args.moco_m,
                                          args.moco_t, args.mlp)

    if args.distributed:
        # For multiprocessing distributed, DistributedDataParallel constructor
        # should always set the single device scope, otherwise,
        # DistributedDataParallel will use all available devices.
        if args.gpu is not None:
            torch.cuda.set_device(args.gpu)
            model_patch.cuda(args.gpu)
            model_graph.cuda(args.gpu)
            # When using a single GPU per process and per
            # DistributedDataParallel, we need to divide the batch size
            # ourselves based on the total number of GPUs we have
            args.batch_size_patch = int(args.batch_size_patch / ngpus_per_node)
            args.batch_size_graph = int(args.batch_size_graph / ngpus_per_node)
            args.workers_patch = int(
                (args.workers_patch + ngpus_per_node - 1) / ngpus_per_node)
            args.workers_graph = int(
                (args.workers_graph + ngpus_per_node - 1) / ngpus_per_node)
            model_patch = torch.nn.parallel.DistributedDataParallel(
                model_patch, device_ids=[args.gpu])
            model_graph = torch.nn.parallel.DistributedDataParallel(
                model_graph,
                device_ids=[args.gpu],
                find_unused_parameters=True)
        else:
            model.cuda()
            # DistributedDataParallel will divide and allocate batch_size to all
            # available GPUs if device_ids are not set
            model = torch.nn.parallel.DistributedDataParallel(model)
    elif args.gpu is not None:
        torch.cuda.set_device(args.gpu)
        model = model.cuda(args.gpu)
        # comment out the following line for debugging
        raise NotImplementedError("Only DistributedDataParallel is supported.")
    else:
        # AllGather implementation (batch shuffle, queue update, etc.) in
        # this code only supports DistributedDataParallel.
        raise NotImplementedError("Only DistributedDataParallel is supported.")

    # define loss function (criterion) and optimizer
    criterion = nn.CrossEntropyLoss().cuda(args.gpu)

    optimizer_patch = torch.optim.SGD(model_patch.parameters(),
                                      args.lr,
                                      momentum=args.momentum,
                                      weight_decay=args.weight_decay)
    optimizer_graph = torch.optim.SGD(model_graph.parameters(),
                                      args.lr,
                                      momentum=args.momentum,
                                      weight_decay=args.weight_decay)
    # save the initial model
    if not args.resume:
        save_checkpoint(
            {
                'epoch': 0,
                'arch': args.arch,
                'state_dict': model_patch.state_dict(),
                'optimizer': optimizer_patch.state_dict(),
            },
            is_best=False,
            filename=os.path.join(os.path.join('./exp', args.exp_name),
                                  'checkpoint_patch_init.pth.tar'))
        save_checkpoint(
            {
                'epoch': 0,
                'arch': args.arch,
                'state_dict': model_graph.state_dict(),
                'optimizer': optimizer_graph.state_dict(),
            },
            is_best=False,
            filename=os.path.join(os.path.join('./exp', args.exp_name),
                                  'checkpoint_graph_init.pth.tar'))

    # optionally resume from a checkpoint
    if args.resume:
        if os.path.isfile(args.resume):
            print("=> loading checkpoint '{}'".format(args.resume))
            if args.gpu is None:
                checkpoint_patch = torch.load(args.resume)
                checkpoint_graph = torch.load(args.resume_graph)
            else:
                # Map model to be loaded to specified single gpu.
                loc = 'cuda:{}'.format(args.gpu)
                checkpoint_patch = torch.load(args.resume, map_location=loc)
                checkpoint_graph = torch.load(args.resume_graph,
                                              map_location=loc)
            args.start_epoch = checkpoint_patch['epoch']
            model_patch.load_state_dict(checkpoint_patch['state_dict'])
            model_graph.load_state_dict(checkpoint_graph['state_dict'])
            optimizer_patch.load_state_dict(checkpoint_patch['optimizer'])
            optimizer_graph.load_state_dict(checkpoint_graph['optimizer'])
            print("=> loaded checkpoint '{}' (epoch {})".format(
                args.resume, checkpoint_patch['epoch']))
        else:
            print("=> no checkpoint found at '{}'".format(args.resume))
            exit()

    transform_re = Rand3DElastic(
        mode='bilinear',
        prob=1.0,
        sigma_range=(8, 12),
        magnitude_range=(0, 1024 + 240),  #[-1024, 240] -> [0, 1024+240]
        spatial_size=(32, 32, 32),
        translate_range=(12, 12, 12),
        rotate_range=(np.pi / 18, np.pi / 18, np.pi / 18),
        scale_range=(0.1, 0.1, 0.1),
        padding_mode='border',
        device=torch.device('cuda:' + str(args.gpu)))
    transform_rgn = RandGaussianNoise(prob=0.25, mean=0.0, std=50)
    transform_rac = RandAdjustContrast(prob=0.25)

    train_tratransforms = Compose([transform_rac, transform_rgn, transform_re])

    train_dataset_patch = COPD_dataset_patch(
        "train", args, moco.loader.TwoCropsTransform(train_tratransforms))
    train_dataset_graph = COPD_dataset_graph(
        "train", args, moco.loader.TwoCropsTransform(train_tratransforms))

    if args.distributed:
        train_sampler_patch = torch.utils.data.distributed.DistributedSampler(
            train_dataset_patch)
        train_sampler_graph = torch.utils.data.distributed.DistributedSampler(
            train_dataset_graph)
    else:
        train_sampler = None

    train_loader_patch = torch.utils.data.DataLoader(
        train_dataset_patch,
        batch_size=args.batch_size_patch,
        shuffle=(train_sampler_patch is None),
        num_workers=args.workers_patch,
        pin_memory=True,
        sampler=train_sampler_patch,
        drop_last=True)
    train_loader_graph = torch.utils.data.DataLoader(
        train_dataset_graph,
        batch_size=args.batch_size_graph,
        shuffle=(train_sampler_graph is None),
        num_workers=args.workers_graph,
        pin_memory=True,
        sampler=train_sampler_graph,
        drop_last=True)

    for epoch in range(args.start_epoch, args.epochs):
        if args.distributed:
            train_sampler_patch.set_epoch(epoch)
        adjust_learning_rate(optimizer_patch, epoch, args)
        # train for one epoch
        train_patch(train_loader_patch, model_patch, criterion,
                    optimizer_patch, epoch, args)
        # save model for every epoch
        if not args.multiprocessing_distributed or (
                args.multiprocessing_distributed
                and args.rank % ngpus_per_node == 0):
            save_checkpoint(
                {
                    'epoch': epoch + 1,
                    'arch': args.arch,
                    'state_dict': model_patch.state_dict(),
                    'optimizer': optimizer_patch.state_dict(),
                },
                is_best=False,
                filename=os.path.join(
                    os.path.join('./exp', args.exp_name),
                    'checkpoint_patch_{:04d}.pth.tar'.format(epoch)))

        for sub_epoch in range(args.num_sub_epoch):
            if args.distributed:
                train_sampler_graph.set_epoch(args.num_sub_epoch * epoch +
                                              sub_epoch)
            adjust_learning_rate(optimizer_graph,
                                 args.num_sub_epoch * epoch + sub_epoch, args)
            train_graph(train_loader_graph, model_graph, model_patch,
                        criterion, optimizer_graph,
                        args.num_sub_epoch * epoch + sub_epoch, args)
            if not args.multiprocessing_distributed or (
                    args.multiprocessing_distributed
                    and args.rank % ngpus_per_node == 0):
                save_checkpoint(
                    {
                        'epoch': args.num_sub_epoch * epoch + sub_epoch + 1,
                        'arch': args.arch,
                        'state_dict': model_graph.state_dict(),
                        'optimizer': optimizer_graph.state_dict(),
                    },
                    is_best=False,
                    filename=os.path.join(
                        os.path.join('./exp', args.exp_name),
                        'checkpoint_graph_{:04d}.pth.tar'.format(
                            args.num_sub_epoch * epoch + sub_epoch)))
コード例 #8
0
ファイル: utils.py プロジェクト: ckbr0/RIS
def transform_and_copy(data, cahce_dir):
    copy_dir = os.path.join(cahce_dir, 'copied_images')
    if not os.path.exists(copy_dir):
        os.mkdir(copy_dir)
    copy_list_path = os.path.join(copy_dir, 'copied_images.npy')
    if not os.path.exists(copy_list_path):
        print("transforming and copying images...")
        imageLoader = LoadImage()
        to_copy_list = [x for x in data if int(x['_label']) == 1]
        mul = 1  #int(len(data)/len(to_copy_list) - 1)

        rand_x_flip = RandFlip(spatial_axis=0, prob=0.50)
        rand_y_flip = RandFlip(spatial_axis=1, prob=0.50)
        rand_z_flip = RandFlip(spatial_axis=2, prob=0.50)
        rand_affine = RandAffine(prob=1.0,
                                 rotate_range=(0, 0, np.pi / 10),
                                 shear_range=(0.12, 0.12, 0.0),
                                 translate_range=(0, 0, 0),
                                 scale_range=(0.12, 0.12, 0.0),
                                 padding_mode="zeros")
        rand_gaussian_noise = RandGaussianNoise(prob=0.5, mean=0.0, std=0.05)
        transform = Compose([
            AddChannel(),
            rand_x_flip,
            rand_y_flip,
            rand_z_flip,
            rand_affine,
            SqueezeDim(),
        ])
        copy_list = []
        n = len(to_copy_list)
        for i in range(len(to_copy_list)):
            print(f'Copying image {i+1}/{n}', end="\r")
            to_copy = to_copy_list[i]
            image_file = to_copy['image']
            _image_file = replace_suffix(image_file, '.nii.gz', '')
            label = to_copy['label']
            _label = to_copy['_label']
            image_data, _ = imageLoader(image_file)
            seg_file = to_copy['seg']
            seg_data, _ = nrrd.read(seg_file)

            for i in range(mul):
                rand_seed = np.random.randint(1e8)
                transform.set_random_state(seed=rand_seed)
                new_image_data = rand_gaussian_noise(
                    np.array(transform(image_data)))
                transform.set_random_state(seed=rand_seed)
                new_seg_data = np.array(transform(seg_data))
                #multi_slice_viewer(image_data, image_file)
                #multi_slice_viewer(seg_data, seg_file)
                #seg_image = MaskIntensity(seg_data)(image_data)
                #multi_slice_viewer(seg_image, seg_file)
                image_basename = os.path.basename(_image_file)
                seg_basename = image_basename + f'_seg_{i}.nrrd'
                image_basename = image_basename + f'_{i}.nii.gz'

                new_image_file = os.path.join(copy_dir, image_basename)
                write_nifti(new_image_data, new_image_file, resample=False)
                new_seg_file = os.path.join(copy_dir, seg_basename)
                nrrd.write(new_seg_file, new_seg_data)
                copy_list.append({
                    'image': new_image_file,
                    'seg': new_seg_file,
                    'label': label,
                    '_label': _label
                })

        np.save(copy_list_path, copy_list)
        print("done transforming and copying!")

    copy_list = np.load(copy_list_path, allow_pickle=True)
    return copy_list