I trained a Pix2Pix GAN model (converting satellite images to maps) and wanted to test out the performance of the model. However, I can only see blank/White images as output. I am using the pretrained model (pix2pix_15000.pth) that I saved towards the end of the training (i.e. with desired generator and discriminator loss). During the training however, I could see the generated images to resemble the maps(True labels) as expected.
Below is the code that I am using to test the model and check the generated images:
input_dim = 3 # Number of input channels
real_dim = 3 # Number of output channels
target_shape = 256 # Target shape for the image
device = 'cpu' # Use 'cuda' if CUDA is available
gen = UNet(input_dim, real_dim).to(device)
loaded_state = torch.load("pix2pix_15000.pth", map_location=torch.device('cpu'))
gen.load_state_dict(loaded_state["gen"]) # Adjust the path to your saved model
gen.eval() # Set the model to evaluation mode
# Define the transformation to preprocess the input image
transform = transforms.Compose([
transforms.Resize((target_shape, target_shape)), # Resize to match the target shape
transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,)) # Convert the image to a tensor
])
# Load and preprocess the image
image_path = "test/842-sat.jpg" # Path to your satellite image
image = Image.open(image_path).convert('RGB')
condition = transform(image).unsqueeze(0) # Add batch dimension
condition_final = condition.to(device)
# Generate the output
with torch.no_grad():
generated_image = gen(condition_final)
print(generated_image)
# Convert the generated image tensor to a PIL image
to_pil = transforms.ToPILImage()
generated_image = generated_image.squeeze(0).cpu() # Remove batch dimension and move to CPU
generated_image_pil = to_pil(generated_image)
# Display the image
plt.imshow(generated_image_pil)
plt.axis('off') # Hide the axes
plt.show()
I expected the output to be a Map, corresponding to the satellite image. Kindly provide your valuable suggestions to as where I might be wrong. Thanks
Jatinpreet Singh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.