junyanz / pytorch-CycleGAN-and-pix2pix

Image-to-Image Translation in PyTorch
Other
22.8k stars 6.29k forks source link

[Reproduction] how do i fix the randomness I tried everything 😢 #1512

Open Nimbus1997 opened 1 year ago

Nimbus1997 commented 1 year ago

Hello, frist I thank your for sharing your wonderful model!

I am trying to use cycleGAN as an basline for my research, but even though I fix the seeds, but I fail to reproduce the same result 😢

Could anybody help me about this reproduction issue? Thank you so much in advance:)

This is how I fix the randomness in the "train.py" , I added following code------------

random_seed = 42
np.random.seed(random_seed) #1.numpy randomness
random.seed(random_seed) #2.python randomness
torch.manual_seed(random_seed) #3.pytorch randomness
torch.cuda.manual_seed(random_seed) # 4. gpu randomness 
torch.cuda.manual_seed_all(random_seed) # 4. gpu randomness - multi gpu
torch.backends.cudnn.deteministic = True #5.cuDNN randomness - might make computaion slow
torch.backends.cudnn.benchmark = False
#6. Data loader randomness in multi process fix -> in /data/__init__.py -> ellen_made
os.environ['PYTHONHASHSEED'] = str(random_seed)  #7.python hash seed

in the "data/init.py/", I added following code------------

def seed_worker(worker_id): #ellen made for randomness fix 6
    worker_seed = torch.initial_seed() % 2**32
    np.random.seed(worker_seed)
    random.seed(worker_seed)

class CustomDatasetDataLoader():
    """Wrapper class of Dataset class that performs multi-threaded data loading"""

    def __init__(self, opt):
        """Initialize this class

        Step 1: create a dataset instance given the name [dataset_mode] 
                opt.dataset_mode: 'chooses how datasets are loaded. [unaligned | aligned | single | colorization]'
        Step 2: create a multi-threaded data loader.
        """
        self.opt = opt
        dataset_class = find_dataset_using_name(opt.dataset_mode)
        self.dataset = dataset_class(opt)
        print("dataset [%s] was created" % type(self.dataset).__name__)

        g =torch.Generator() # ellen made for randomness fix 6
        g.manual_seed(torch.initial_seed())# ellen made for randomness fix 6

        self.dataloader = torch.utils.data.DataLoader(
            self.dataset,
            batch_size=opt.batch_size,
            shuffle=not opt.serial_batches,
            num_workers=int(opt.num_threads),
            worker_init_fn=seed_worker, # ellen
            generator=g #ellen
            )
maryam95hallal commented 1 year ago

try using the flag --no_dropout in your test.