pytorch-CycleGAN-and-pix2pix icon indicating copy to clipboard operation
pytorch-CycleGAN-and-pix2pix copied to clipboard

About image normalization

Open kazuma0606 opened this issue 3 years ago • 4 comments

Hi, junyanz I wrote the follows code to unpack the image with reference to test.py and get the output of the middle layer of the model.

`if name == 'main': img = [] opt = TestOptions().parse() # get test options # hard-code some parameters for test opt.num_threads = 0 # test code only supports num_threads = 0 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 model = create_model(opt) # create a model given opt.model and other options model.setup(opt) # regular setup: load and print networks; create schedulers layers = list(model.netG_A.children())[:-1] feature_extractor = nn.Sequential(*layers) print(list(model.netG_A.children())) #get the middral layer

if opt.eval:
    model.eval()
for i, data in enumerate(dataset):
  if i >= opt.num_test:
    break
  AtoB = opt.direction == 'AtoB'
  real_A = data['A' if AtoB else 'B'].to(device=torch.device('cuda'))
  real_B = data['B' if AtoB else 'A'].to(device=torch.device('cuda'))
  image_paths = data['A_paths' if AtoB else 'B_paths']
  #print(feature_extractor(real_B.to('cpu').detach().numpy().copy()[0][0]))
  print(real_B[0][0])`

Looking at the output of the image, pixel values that were 0 in the original image are now -1. Is there some kind of normalization running? I couldn't figure it out by looking at the original code. Please tell me.

Kind regards, hisanori.

kazuma0606 avatar Jul 04 '22 11:07 kazuma0606

We normalized the input from [0, 1] to [-1, 1]. To undo it, you can call tensor2im. https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/util/util.py#L9

junyanz avatar Jul 04 '22 16:07 junyanz

Hi, junyanz. Thank you for reply. I understood your comments. In Addition, I have a question. I think the output image (*.png) had a pixel value of [0 to 256]. Are these images normalized before they are entered into the network? Or is it done when the output image is saved?

Kind regards, hisanori.

kazuma0606 avatar Jul 08 '22 00:07 kazuma0606

We normalized the input from [0, 1] to [-1, 1]. To undo it, you can call tensor2im. https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/util/util.py#L9

Where do you do the conversion to [-1, 1], in get_transform?

sebasmos avatar Mar 30 '23 13:03 sebasmos

People find that normalizing data into zero-mean data helps model training. See this [post] for more details. Also, our generator's final layer has a TanH layer, which will only produce data at [-1, 1].

junyanz avatar May 02 '23 20:05 junyanz