Closed jamessmith90 closed 4 years ago
Thanks for your interest in our work.
Please refer the following pseudo-code:
from PIL import Image
import torchvision.transforms as transforms
transformer = transforms.Compose([
transforms.Resize((16, 16)) #optional
transforms.ToTensor(),
transforms.Normalize(0.5, 0.5, 0.5), (0.5, 0.5, 0.5)
])
single_image = Image.open('image_path')
single_image = transformer(single_image).unsqueeze(0).to(device)
generated_image = generator(single_image)
Hi Kim,
I have writen a simple code for single image super resolution, but the result is strange. Could you please tell me how to normalize the generated image (b) from your model to [0,255] range for saving?
import torch
import numpy as np
from model import Generator
from PIL import Image
import torchvision.transforms as transforms
totensor = transforms.Compose([
transforms.Resize((16, 16)),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
])
single_image = Image.open('./dataset/0_0.png')
'''
single_image = single_image.resize((16, 16))
single_image = np.array(single_image)
single_image = (single_image - 128) / 128
single_image = torch.from_numpy(single_image).float().permute(2, 0, 1)
'''
single_image = totensor(single_image)
single_image = single_image.unsqueeze(0).cuda()
print(single_image.shape)
cnn = Generator().cuda()
cnn_dict = torch.load('./checkpoints/generator_checkpoint_singleGPU.ckpt')
cnn.load_state_dict(cnn_dict['model_state_dict'], strict=False)
b = cnn(single_image) # *2 larger
print(b.shape)
b = b.squeeze(0).detach().permute(1, 2, 0).cpu().numpy()
# there may be some normalization operation here???
b = b.astype(np.uint8)
b = Image.fromarray(b)
b.save('1.png')
Regards, Yuzhi
You should set parameter of Generator.
b=cnn(single_image, step=3, alpha=1)
and denormalize as
b =0.5*b+0.5
Thanks.
@zhaoyuzhi Can you post the final code ?
@zhaoyuzhi Merged code ?
You may try this code. However, I do not test on CelebA dataset and the results are strange.
import torch
import numpy as np
from model import Generator
from PIL import Image
import torchvision.transforms as transforms
totensor = transforms.Compose([
transforms.Resize((16, 16)),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
])
single_image = Image.open('./dataset/0_0.png')
'''
single_image = single_image.resize((16, 16))
single_image = np.array(single_image)
single_image = (single_image - 128) / 128
single_image = torch.from_numpy(single_image).float().permute(2, 0, 1)
'''
single_image = totensor(single_image)
single_image = single_image.unsqueeze(0).cuda()
print(single_image.shape)
cnn = Generator().cuda()
cnn_dict = torch.load('./checkpoints/generator_checkpoint_singleGPU.ckpt')
cnn.load_state_dict(cnn_dict['model_state_dict'], strict=False)
b = cnn(single_image, step=3, alpha=1) # *2 larger
print(b.shape)
b = b.squeeze(0).detach().permute(1, 2, 0).cpu().numpy()
b =0.5*b+0.5
b = b.astype(np.uint8)
b = Image.fromarray(b)
b.save('1.png')
You may try this code. However, I do not test on CelebA dataset and the results are strange. import torch import numpy as np from model import Generator from PIL import Image import torchvision.transforms as transforms
totensor = transforms.Compose([ transforms.Resize((16, 16)), transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)), ])
single_image = Image.open('./dataset/0_0.png') ''' single_image = single_image.resize((16, 16)) single_image = np.array(single_image) single_image = (single_image - 128) / 128 single_image = torch.from_numpy(single_image).float().permute(2, 0, 1) ''' single_image = totensor(single_image) single_image = single_image.unsqueeze(0).cuda() print(single_image.shape)
cnn = Generator().cuda() cnn_dict = torch.load('./checkpoints/generator_checkpoint_singleGPU.ckpt') cnn.load_state_dict(cnn_dict['model_state_dict'], strict=False)
b = cnn(single_image, step=3, alpha=1) # 2 larger print(b.shape) b = b.squeeze(0).detach().permute(1, 2, 0).cpu().numpy() b =0.5b+0.5 b = b.astype(np.uint8) b = Image.fromarray(b) b.save('1.png')
Hello,thamk you for your work.But the result picture is black?Can you tell me why?
You should set parameter of Generator. b=cnn(single_image, step=3, alpha=1) and denormalize as b =0.5*b+0.5
Hello,thank you for work.And i have a question.I tried demo from @zhaoyuzhi ,but the result is a black picture,can you tell me why?Thank you very much.
Help make one picture
Dear all,
I will upload the demo code as soon as possible.
Thank you guys for your interest!
Thanks Deokyun! Looking forward to it as soon as this is one of the best implementations of super-resolutions I've seen, but had quite a lot of issues setting it up yesterday
Can you please help and share in how i can run this model on a single image ?