Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inference of a single image (help) #1664

Open
themoshe12 opened this issue Jun 25, 2024 · 1 comment
Open

inference of a single image (help) #1664

themoshe12 opened this issue Jun 25, 2024 · 1 comment

Comments

@themoshe12
Copy link

themoshe12 commented Jun 25, 2024

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants