Views
No views yet
latest_net_G.pth: Generator model weightslatest_net_D.pth: Discriminator model weights1import argparse
2import os
3import shutil
4
5import matplotlib.pyplot as plt
6import torch
7import torch.nn.functional as F
8import torchvision.transforms as transforms
9from huggingface_hub import hf_hub_download
10from torchvision.datasets import MNIST
11
12from models.pix2pix_model import Pix2PixModel
13from options.test_options import TestOptions
14
15hf_model_path = hf_hub_download(repo_id="egpivo/mnist-pix2pix", filename="latest_net_G.pth")
16
17expected_checkpoints_dir = os.path.join(os.path.dirname(hf_model_path), "mnist_pix2pix")
18expected_model_path = os.path.join(expected_checkpoints_dir, "latest_net_G.pth")
19
20os.makedirs(expected_checkpoints_dir, exist_ok=True)
21shutil.copy(hf_model_path, expected_model_path)
22print(f"Model copied to: {expected_model_path}")
23
24opt = argparse.Namespace(
25 dataroot="./dummy_data",
26 isTrain=False,
27 name="mnist_pix2pix",
28 gpu_ids=[],
29 checkpoints_dir=os.path.dirname(hf_model_path),
30 model="pix2pix",
31 input_nc=3,
32 output_nc=3,
33 ngf=64,
34 ndf=64,
35 netD="basic",
36 netG="unet_256",
37 n_layers_D=3,
38 norm="batch",
39 init_type="normal",
40 init_gain=0.02,
41 no_dropout=True,
42 dataset_mode="aligned",
43 direction="AtoB",
44 serial_batches=True,
45 num_threads=0,
46 batch_size=1,
47 load_size=256,
48 crop_size=256,
49 max_dataset_size=float("inf"),
50 preprocess="resize_and_crop",
51 no_flip=True,
52 display_winsize=256,
53 epoch="latest",
54 load_iter=0,
55 verbose=False,
56 suffix="",
57 use_wandb=False,
58 wandb_project_name="",
59 results_dir="./results",
60 aspect_ratio=1.0,
61 phase="test",
62 eval=True,
63 num_test=50,
64)
65
66model = Pix2PixModel(opt)
67model.setup(opt)
68
69model.netG.load_state_dict(torch.load(expected_model_path, map_location="cpu"))
70model.netG.eval()1mnist_dataset = MNIST(root="./data", train=False, download=True)
2mnist_image, _ = mnist_dataset[0] # Get first test digit
3
4distorted_transform = transforms.Compose([
5 transforms.Resize((64, 64)),
6 transforms.RandomRotation(30),
7 transforms.GaussianBlur(3),
8 transforms.ToTensor()
9])
10
11distorted_image = distorted_transform(mnist_image)
12
13# Convert grayscale to RGB (since Pix2Pix expects 3 channels)
14input_tensor = distorted_image.unsqueeze(0).repeat(1, 3, 1, 1) # Shape: [1, 3, 64, 64]
15
16# Resize input to match model input size
17input_tensor = F.interpolate(input_tensor, size=(128, 128), mode="bilinear", align_corners=False)
18
19# Normalize to [-1, 1] as required by Pix2Pix
20input_tensor = (input_tensor - 0.5) * 2
21
22output = model.netG(input_tensor)
23
24output_image = output.squeeze(0).permute(1, 2, 0).detach().cpu().numpy()
25output_image = (output_image + 1) / 2 # Rescale to [0,1] range for display
26
27plt.figure(figsize=(8, 4))
28
29plt.subplot(1, 2, 1)
30plt.imshow(distorted_image.squeeze(), cmap="gray")
31plt.axis("off")
32plt.title("Distorted Input")
33
34plt.subplot(1, 2, 2)
35plt.imshow(output_image)
36plt.axis("off")
37plt.title("Recovered Output")
38
39plt.show()
40
41print("Testing Completed")