TransUNet icon indicating copy to clipboard operation
TransUNet copied to clipboard

Question about how to train with image size with different length and width

Open xiaoerlaigeid opened this issue 3 years ago • 2 comments

Very impressive works. I trying to use the transUnet in my dataset. But my dataset shape is (672,448) which length and width not equal. I find this will lead bugs. because the patch needs the same w and h. How to deal with this problem, do you have some advise ? Thanks!

xiaoerlaigeid avatar May 23 '21 04:05 xiaoerlaigeid

Look at https://github.com/Beckschen/TransUNet/issues/35

I think you could crop or rescale your image according to the mentioned factor

andife avatar May 25 '21 07:05 andife

The errors may happen when data argumentation, an easy way to solve this is that zoom first and argumentation later. Change the code like this:

class RandomGenerator(object):
    def __init__(self, output_size):
        self.output_size = output_size

    def __call__(self, sample):
        image, label = sample['image'], sample['label'] 
        x, y = label.shape
        if x != self.output_size[0] or y != self.output_size[1]:
            image = zoom(image, (1, self.output_size[0] / x, self.output_size[1] / y), order=3) 
            label = zoom(label, (self.output_size[0] / x, self.output_size[1] / y), order=0)
        if random.random() > 0.5:
            image, label = random_rot_flip(image, label)
        elif random.random() > 0.5:
            image, label = random_rotate(image, label) 
        image = torch.from_numpy(image.astype(np.float32))
        label = torch.from_numpy(label.astype(np.float32))
        sample = {'image': image, 'label': label.long()}
        return sample

liyiersan avatar Aug 04 '21 04:08 liyiersan