junyanz / pytorch-CycleGAN-and-pix2pix

Image-to-Image Translation in PyTorch
Other
22.4k stars 6.22k forks source link

pix2pix using grayscale image with 16-bit signed integer pixel values #1209

Open YBurakD opened 3 years ago

YBurakD commented 3 years ago

I am trying to use single layer images that have intensity range from -1024 to 3072 (12 bit, signed). I have changed the aligned_dataset.py so that I can read my images into numpy arrays and then convert them into PIL Images. The change I made in the code is as follows: if ".hdf5" in AB_path: AB = sitk.ReadImage(AB_path) AB = Image.fromarray(sitk.GetArrayFromImage(AB) else: AB = Image.open(AB_path).convert('RGB') The rest is the same and all images are 256x256. When I try to run the following command:

$python train.py --dataroot ./datasets/data_path --model pix2pix --gpu_ids 0 --direction AtoB --input_nc 1 --output_nc 1 --n_epochs 100

I get following response:

Setting up a new session... create web directory ./checkpoints/experiment_name/web... /home/ubuntu/anaconda3/lib/python3.7/site-packages/torch/optim/lr_scheduler.py:123: UserWarning: Detected call of lr_scheduler.step() before optimizer.step(). In PyTorch 1.1.0 and later, you should call them in the opposite order: optimizer.step() before lr_scheduler.step(). Failure to do this will result in PyTorch skipping the first value of the learning rate schedule. See more details at https://pytorch.org/docs/stable/optim.html#how-to-adjust-learning-rate "https://pytorch.org/docs/stable/optim.html#how-to-adjust-learning-rate", UserWarning) learning rate 0.0002000 -> 0.0002000 Traceback (most recent call last): File "train.py", line 46, in for i, data in enumerate(dataset): # inner loop within one epoch File "/home/ubuntu/PIX2PIX/pytorch-CycleGAN-and-pix2pix/data/init.py", line 90, in iter for i, data in enumerate(self.dataloader): File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 345, in next data = self._next_data() File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 856, in _next_data return self._process_data(data) File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 881, in _process_data data.reraise() File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/torch/_utils.py", line 395, in reraise raise self.exc_type(msg) RuntimeError: Caught RuntimeError in DataLoader worker process 0. Original Traceback (most recent call last): File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/torch/utils/data/_utils/worker.py", line 178, in _worker_loop data = fetcher.fetch(index) File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py", line 44, in fetch data = [self.dataset[idx] for idx in possibly_batched_index] File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py", line 44, in data = [self.dataset[idx] for idx in possibly_batched_index] File "/home/ubuntu/PIX2PIX/pytorch-CycleGAN-and-pix2pix/data/aligned_dataset.py", line 61, in getitem A = Atransform(A) File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/torchvision/transforms/transforms.py", line 61, in call img = t(img) File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/torchvision/transforms/transforms.py", line 166, in call return F.normalize(tensor, self.mean, self.std, self.inplace) File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/torchvision/transforms/functional.py", line 208, in normalize tensor.sub(mean).div_(std) RuntimeError: output with shape [1, 256, 256] doesn't match the broadcast shape [3, 256, 256]

What is wrong?

tongfh1 commented 9 months ago

Hello, I have also encountered this problem. May I ask how you solved this problem?