junyanz / pytorch-CycleGAN-and-pix2pix

Image-to-Image Translation in PyTorch
Other
23.09k stars 6.32k forks source link

Perform interpolation between Input and Output #1648

Open obeidahmad opened 6 months ago

obeidahmad commented 6 months ago

My idea is to take a certain input generate its latent space, same for the output. Then perform latent space interpolation from the input to output.

However, I can't seem to find a way to get the latent space of a certain image.

I did the following:

gen = define_G(
    input_nc=3,
    output_nc=3,
    ngf=64,
    netG="unet_256",
    norm="batch",
    use_dropout=True,
    init_type="normal",
    init_gain=0.02,
    gpu_ids=[0, 1],
)
device = torch.device("cuda:1")

# Load model gen
model_path = "facades_label2photo.pth"
state_dict = torch.load(model_path, map_location=str(device))
if hasattr(state_dict, "_metadata"):
    del state_dict._metadata
state_dict = OrderedDict([(f"module.{k}", v) for k, v in state_dict.items()])
gen.load_state_dict(state_dict)
gen.eval()

# get input image
input_image_path = "datasets/facades/test/1.jpg"
img = Image.open(input_image_path).convert('RGB')
width, height = img.size
start_col = width // 2
end_col = width
start_row = 0
end_row = height
img = img.crop((start_col, start_row, end_col, end_row))

# Encode the input image to obtain the latent vector
with torch.no_grad():
    latent_vector = gen.encoder(img)

However, it seems the model does not have an encoder function.