junyanz / pytorch-CycleGAN-and-pix2pix

Image-to-Image Translation in PyTorch
Other
22.92k stars 6.31k forks source link

inference of a single image (help) #1664

Open themoshe12 opened 3 months ago

themoshe12 commented 3 months ago

I have trained a cycleGan model using !python train.py --dataroot /content/drive/MyDrive/project/dataset --name F2F --model cycle_gan --display_id -1 To infer set of images from a folder i used

opt = TestOptions()
  #defined options occurs here
dataset = create_dataset(opt)  
Initialize the model
model = create_model(opt)
model.setup(opt)
model.eval()
data_iter = iter(dataset.dataloader)
data_dict = next(data_iter)
input_image_tensor = data_dict['A']
data = {'A': input_image_tensor,'A_paths': ''}
model.set_input(data)
model.test()
visuals = model.get_current_visuals()
output_image = visuals['fake']
output_image_np = output_image.squeeze().cpu().numpy().transpose(1, 2, 0)
output_image_np = ((output_image_np - output_image_np.min()) / (output_image_np.max() - output_image_np.min()) * 255).astype(np.uint8)
output_image_np = cv2.cvtColor(output_image_np, cv2.COLOR_BGR2RGB)
cv2_imshow(output_image_np)

The above snippet worked as expected and generated good results.
I would like to infer a single image without going through the loader. I tried to imitate the transforms inside the create_dataset(opt) function using this ...

def preprocess(image):
        if image.ndim == 2 or image.shape[2] == 1:
        image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
    elif image.shape[2] == 4:
        image = cv2.cvtColor(image, cv2.COLOR_BGRA2BGR)
    elif image.shape[2] == 3:
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
       image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    pil_image = transforms.ToPILImage()(image)
    transform_pipeline = transforms.Compose([
        transforms.Resize(286),
        transforms.CenterCrop(256),
        transforms.ToTensor(),
        transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
    ])
       image_tensor = transform_pipeline(pil_image)
       image_tensor = image_tensor.unsqueeze(0)

    return image_tensor
input_image_tensor = preprocess(input_image)
data = {'A': input_image_tensor,'A_paths': ''}
model.set_input(data)
model.test()
visuals = model.get_current_visuals()
output_image = visuals['fake']
output_image_np = output_image.squeeze().cpu().numpy().transpose(1, 2, 0)
output_image_np = ((output_image_np - output_image_np.min()) / (output_image_np.max() - output_image_np.min()) * 255).astype(np.uint8)
output_image_np = cv2.cvtColor(output_image_np, cv2.COLOR_BGR2RGB)
cv2_imshow(output_image_np)

But the results are very blurry. Any help of how can achieve this would be much appreciated !

Yatzkan2 commented 3 months ago

I have encountered a similar problem!! couldn't find a solution yet :(