def __getitem__(self, index):
        """Return a data point and its metadata information.

        Parameters:
            index (int)      -- a random integer for data indexing

        Returns a dictionary that contains A, B, A_paths and B_paths
            A (tensor)       -- an image in the input domain
            B (tensor)       -- its corresponding image in the target domain
            A_paths (str)    -- image paths
            B_paths (str)    -- image paths
        """
        A_path = self.A_paths[index % self.A_size]  # make sure index is within then range
        if self.opt.serial_batches:   # make sure index is within then range
            index_B = index % self.B_size
        else:   # randomize the index for domain B to avoid fixed pairs.
            index_B = random.randint(0, self.B_size - 1)
        B_path = self.B_paths[index_B]
        A_img = Image.open(A_path).convert('RGB')
        B_img = Image.open(B_path).convert('RGB')

        # Apply image transformation
        # For FastCUT mode, if in finetuning phase (learning rate is decaying),
        # do not perform resize-crop data augmentation of CycleGAN.
        # print('current_epoch', self.current_epoch)
        is_finetuning = self.opt.isTrain and self.current_epoch > self.opt.n_epochs
        modified_opt = util.copyconf(self.opt, load_size=self.opt.crop_size if is_finetuning else self.opt.load_size)
        transform = get_transform(modified_opt)
        A = transform(A_img)
        B = transform(B_img)

        return {'A': A, 'B': B, 'A_paths': A_path, 'B_paths': B_path}
    def __getitem__(self, index, with_annotations=False):
        """Return a data point and its metadata information.

        Parameters:
            index (int)      -- a random integer for data indexing

        Returns a dictionary that contains A, B, A_paths and B_paths
            A (tensor)       -- an image in the input domain
            B (tensor)       -- its corresponding image in the target domain
            A_paths (str)    -- image paths
            B_paths (str)    -- image paths
        """

        A_path = self.ants['images'][index % self.A_size][
            'file_name']  # make sure index is within then range
        A_path = os.path.join(self.dir_A, os.path.basename(A_path))
        ants = self.ants['annotations'][index % self.A_size]

        #A_path = self.A_paths[index % self.A_size]  # make sure index is within then range
        if self.opt.serial_batches:  # make sure index is within then range
            index_B = index % self.B_size
        else:  # randomize the index for domain B to avoid fixed pairs.
            index_B = random.randint(0, self.B_size - 1)
        B_path = self.B_paths[index_B]
        A_img = Image.open(A_path).convert('RGB')
        B_img = Image.open(B_path).convert('RGB')

        # Apply image transformation
        # For FastCUT mode, if in finetuning phase (learning rate is decaying),
        # do not perform resize-crop data augmentation of CycleGAN.
        #        print('current_epoch', self.current_epoch)
        is_finetuning = self.opt.isTrain and self.current_epoch > self.opt.n_epochs
        modified_opt = util.copyconf(self.opt,
                                     load_size=self.opt.crop_size
                                     if is_finetuning else self.opt.load_size)
        transform = get_transform(modified_opt)
        A = transform(A_img)
        B = transform(B_img)

        self.ants['images'].index(A_path)

        from PIL import ImageDraw
        draw = ImageDraw.Draw(A_img)
        draw.rectangle([
            ants['bbox'][0], ants['bbox'][1], ants['bbox'][2] +
            ants['bbox'][0], ants['bbox'][3] + ants['bbox'][1]
        ],
                       fill=128)
        A_img.show()
        del draw

        if with_annotations:
            return {'A': A, 'B': B, 'A_paths': A_path, 'B_paths': B_path}
        else:
            return {'A': A, 'B': B, 'A_paths': A_path, 'B_paths': B_path}
from models import create_model
from util.visualizer import Visualizer
from util import html
import util.util as util


if __name__ == '__main__':
    opt = TestOptions().parse()  # get test options
    # hard-code some parameters for test
    opt.num_threads = 0   # test code only supports num_threads = 1
    opt.batch_size = 1    # test code only supports batch_size = 1
    opt.serial_batches = True  # disable data shuffling; comment this line if results on randomly chosen images are needed.
    opt.no_flip = True    # no flip; comment this line if results on flipped images are needed.
    opt.display_id = -1   # no visdom display; the test code saves the results to a HTML file.
    dataset = create_dataset(opt)  # create a dataset given opt.dataset_mode and other options
    train_dataset = create_dataset(util.copyconf(opt, phase="train"))
    model = create_model(opt)      # create a model given opt.model and other options
    # create a webpage for viewing the results
    web_dir = os.path.join(opt.results_dir, opt.name, '{}_{}'.format(opt.phase, opt.epoch))  # define the website directory
    print('creating web directory', web_dir)
    webpage = html.HTML(web_dir, 'Experiment = %s, Phase = %s, Epoch = %s' % (opt.name, opt.phase, opt.epoch))

    for i, data in enumerate(dataset):
        if i == 0:
            model.data_dependent_initialize(data)
            model.setup(opt)               # regular setup: load and print networks; create schedulers
            model.parallelize()
            if opt.eval:
                model.eval()
        if i >= opt.num_test:  # only apply our model to opt.num_test images.
            break
Example #4
0
    def __getitem__(self, index):
        """Return a data point and its metadata information.

        Parameters:
            index (int)      -- a random integer for data indexing

        Returns a dictionary that contains A, B, A_paths and B_paths
            A (tensor)       -- an image in the input domain
            B (tensor)       -- its corresponding image in the target domain
            A_paths (str)    -- image paths
            B_paths (str)    -- image paths
        """

        start_time = time.time()
        A_path = self.A_paths[
            index % self.A_size]  # make sure index is within then range
        # print("A path {}".format(A_path))
        pat, mr_series, slicenii = os.path.basename(A_path).split("-")
        print(slicenii)

        if len(slicenii) == 5:
            slice_number = int(slicenii[:1])
        elif len(slicenii) == 6:
            slice_number = int(slicenii[:2])
        elif len(slicenii) == 7:
            slice_number = int(slicenii[:3])
        B_path = '/path/to/B'
        # print(slice_number)

        slice_number_orig = slice_number
        r = 5
        if slice_number <= 10:
            slice_number += random.randint(0, 5)
        elif slice_number >= 290:
            slice_number += random.randint(-5, 0)
        else:
            slice_number += random.randint(-5, 5)
        for i in range(150):
            if not os.path.exists(B_path):
                # print(slice_number)
                slicenii2 = str(slice_number) + ".nii"
                pat_ct_slice = pat + "-CTSim-" + slicenii2
                # print(pat_ct_slice)
                B_paths = [
                    path_ct for path_ct in self.B_paths
                    if pat_ct_slice in path_ct
                ]
            if B_paths == []:
                # print("empty")
                slice_number = int(slice_number)
                # print(slice_number)
                if slice_number < 61:
                    slice_number += 1
                elif slice_number > 260:
                    slice_number += -1
            else:
                B_path = B_paths[0]
                try:
                    B_img_nifti = nib.load(B_path)
                    B_img_numpy = B_img_nifti.get_fdata(caching="unchanged")
                    if not np.any(B_img_numpy):
                        print("Not any {}".format(B_path))
                        B_path = '/path/to/B'
                        slice_number = int(slice_number)
                        # print(slice_number)
                        if slice_number < 61:
                            slice_number += 1
                        elif slice_number > 260:
                            slice_number += -1
                    else:
                        break
                    # slice_number = slice_number_orig
                except:
                    slice_number = int(slice_number)
                    # print(slice_number)
                    if slice_number < 61:
                        slice_number += 1
                    elif slice_number > 260:
                        slice_number += -1
                    pass

            # r += 1

        # if self.opt.serial_batches:   # make sure index is within then range
        #     index_B = index % self.B_size
        # else:   # randomize the index for domain B to avoid fixed pairs.
        #     index_B = random.randint(0, self.B_size - 1)

        # B_path = self.B_paths[index_B]
        #A_img = Image.open(A_path).convert('RGB')

        # print("B_path {}".format(B_path))
        # print("Time {}".format(time.time() - start_time))
        A_img_nifti = nib.load(A_path)
        A_img_numpy = A_img_nifti.get_fdata(caching="unchanged")
        A_img_numpy = np.squeeze(A_img_numpy)
        # A_img_numpy = np.rot90(A_img_numpy)

        A_img_numpy = (A_img_numpy - np.amin(A_img_numpy)) / (
            np.amax(A_img_numpy) - np.amin(A_img_numpy)
        )  #Normalize MR to be in range [0, 255]
        if np.amax(A_img_numpy) == 0:
            print("MR empty in {}".format(A_path))
        A_img_numpy[A_img_numpy > 1.] = 1.
        A_img_numpy[A_img_numpy < 0.] = 0.
        A_img_numpy = 255 * A_img_numpy
        A_nonzero = np.count_nonzero(A_img_numpy)
        A_img_numpy = A_img_numpy.astype(np.uint8)
        A_img = Image.fromarray(A_img_numpy)

        if self.opt.isTrain:

            if self.opt.crop_size == 256:
                # Random crop
                i, j, h, w = transforms.RandomCrop.get_params(
                    A_img,
                    output_size=(self.opt.crop_size, self.opt.crop_size))
                A_crop = TF.crop(A_img, i, j, h, w)
                for i in range(50):
                    if not A_crop.getbbox():
                        i, j, h, w = transforms.RandomCrop.get_params(
                            A_img,
                            output_size=(self.opt.crop_size,
                                         self.opt.crop_size))
                        A_crop = TF.crop(A_img, i, j, h, w)
                    else:
                        break
                np_im = np.array(A_crop)
                for i in range(100):
                    if np.count_nonzero(np_im) < round(A_nonzero // 3):
                        i, j, h, w = transforms.RandomCrop.get_params(
                            A_img,
                            output_size=(self.opt.crop_size,
                                         self.opt.crop_size))
                        A_crop = TF.crop(A_img, i, j, h, w)
                        np_im = np.array(A_crop)
            else:
                A_crop = A_img
        #B_img = Image.open(B_path).convert('RGB')
        # B_img_nifti = nib.load(B_path)
        # B_img_numpy = B_img_nifti.get_fdata(caching = "unchanged")
        try:
            B_img_numpy = np.squeeze(B_img_numpy)
        except:
            print("Error {}".format(A_path))
            print("Error {}".format(B_path))
        # B_img_numpy = np.rot90(B_img_numpy)
        B_img_numpy = (B_img_numpy +
                       1024.) / 4095.  #Normalize CT to be in range [0, 255]
        B_img_numpy[B_img_numpy > 1.] = 1.
        B_img_numpy[B_img_numpy < 0.] = 0.
        if np.amax(B_img_numpy) == 0:
            print(B_path)
        B_img_numpy = 255 * B_img_numpy
        B_img_numpy = B_img_numpy.astype(np.uint8)
        B_img = Image.fromarray(B_img_numpy)
        if self.opt.isTrain:
            if self.opt.crop_size == 256:
                B_crop = TF.crop(B_img, i, j, h, w)
            else:
                B_crop = B_img
        # Apply image transformation
        # For FastCUT mode, if in finetuning phase (learning rate is decaying),
        # do not perform resize-crop data augmentation of CycleGAN.
#        print('current_epoch', self.current_epoch)
            is_finetuning = self.opt.isTrain and self.current_epoch > self.opt.n_epochs
            modified_opt = util.copyconf(self.opt,
                                         load_size=self.opt.crop_size if
                                         is_finetuning else self.opt.load_size)
            transform = get_transform(modified_opt)
            A = transform(A_crop)
            np_a = np.array(A)
            # print("A: max {}, min {}".format(np.amax(np_a),np.amin(np_a)))
            B = transform(B_crop)
            np_b = np.array(B)
            # print("B: max {}, min {}".format(np.amax(np_b),np.amin(np_b)))
            if np.amax(np_b) < 0:
                print(B_path)

        else:
            # transform = transforms.ToTensor()
            is_finetuning = self.opt.isTrain and self.current_epoch > self.opt.n_epochs
            modified_opt = util.copyconf(self.opt,
                                         load_size=self.opt.crop_size if
                                         is_finetuning else self.opt.load_size)
            transform = get_transform(modified_opt)
            A = transform(A_img)
            B = transform(B_img)
        # A = torch.unsqueeze(A, dim = 3)
        # B = torch.unsqueeze(B, dim = 3)
        # el_def = RandomElasticDeformation(num_control_points=6, locked_borders=2)
        # A = el_def(A)
        # B = el_def(B)
        # A = torch.squeeze(A, dim = 3)
        # B = torch.squeeze(B, dim = 3)
        return {'A': A, 'B': B, 'A_paths': A_path, 'B_paths': B_path}
Example #5
0

if __name__ == '__main__':
    opt = InferenceOptions().parse()  # get test options
    # hard-code some parameters for test
    opt.num_threads = 0  # test code only supports num_threads = 1
    opt.batch_size = 1  # test code only supports batch_size = 1
    opt.serial_batches = True  # disable data shuffling; comment this line if results on randomly chosen images are needed.
    opt.no_flip = True  # no flip; comment this line if results on flipped images are needed.
    opt.display_id = -1  # no visdom display; the test code saves the results to a HTML file.
    opt.dataset_mode = "single"
    opt.num_test = 1
    dataset = SingleImageDataset(
        opt
    )  #create_dataset(opt)  # create a dataset given opt.dataset_mode and other options
    train_dataset = SingleImageDataset(util.copyconf(
        opt, phase="test"))  #create_dataset(util.copyconf(opt, phase="test"))
    if dataset is None:
        print("Path to file is incorrect, exiting")
        exit(1)
    model = create_model(
        opt)  # create a model given opt.model and other options
    # create a webpage for viewing the results
    output_path = self.output  #os.path.join(opt.results_dir, opt.name, '{}_{}'.format(opt.phase, opt.epoch))  # define the website directory
    #print('creating web directory', web_dir)
    #webpage = html.HTML(web_dir, 'Experiment = %s, Phase = %s, Epoch = %s' % (opt.name, opt.phase, opt.epoch))
    assert len(dataset) == 1
    # for now we only support single image as an input

    for i, data in enumerate(dataset):
        if i == 0:
            model.data_dependent_initialize(data)