DeokyunKim / Progressive-Face-Super-Resolution

Official Pytorch Implementation of Progressive Face Super-Resolution (BMVC 2019 Accepted)
260 stars 60 forks source link

How to run this pretrained model on single image ? #13

Closed jamessmith90 closed 4 years ago

jamessmith90 commented 4 years ago

Can you please help and share in how i can run this model on a single image ?

DeokyunKim commented 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)
zhaoyuzhi commented 4 years ago

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

DeokyunKim commented 4 years ago

You should set parameter of Generator.

b=cnn(single_image, step=3, alpha=1)

and denormalize as

b =0.5*b+0.5

zhaoyuzhi commented 4 years ago

Thanks.

deimsdeutsch commented 4 years ago

@zhaoyuzhi Can you post the final code ?

jamessmith90 commented 4 years ago

@zhaoyuzhi Merged code ?

zhaoyuzhi commented 4 years ago

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')
yangyingni commented 4 years ago

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?

yangyingni commented 4 years ago

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.

slava-fmsu-org commented 4 years ago

Help make one picture photo_2019-12-29_19-27-02

DeokyunKim commented 4 years ago

Dear all,

I will upload the demo code as soon as possible.

Thank you guys for your interest!

ding-si-han commented 4 years ago

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