Views
No views yet
pip install torch torchvision pillow1import torch
2from PIL import Image
3from torchvision import transforms
4
5# 加载模型(需要先定义 Generator 类,见 inference.py)
6from inference import Generator
7
8device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
9model = Generator(n_residual=9).to(device)
10model.load_state_dict(torch.load("G_AB_final.pth", map_location=device))
11model.eval()
12
13# 图像预处理
14transform = transforms.Compose([
15 transforms.Resize(256),
16 transforms.ToTensor(),
17 transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
18])
19
20# 加载并转换图像
21img = Image.open("input.jpg").convert("RGB")
22input_tensor = transform(img).unsqueeze(0).to(device)
23
24# 推理
25with torch.no_grad():
26 output_tensor = model(input_tensor)
27
28# 保存结果
29output_img = output_tensor[0] * 0.5 + 0.5
30transforms.ToPILImage()(output_img).save("output_ir.jpg")1from inference import batch_convert
2
3batch_convert(
4 input_dir="path/to/rgb/images",
5 output_dir="path/to/output",
6 model_path="G_AB_final.pth"
7)| 参数 | 值 |
|---|---|
| 训练轮数 | 100 epochs |
| 图像尺寸 | 256x256 |
| 批大小 | 2 |
| 学习率 | 0.0002 |
| 损失函数 | LSGAN + L1 Cycle Loss |
| 输入(RGB) | 输出(伪红外) |
|---|---|
| RGB图像 | 红外风格图像 |
1@misc{rgb2infrared-cyclegan,
2 title={RGB to Infrared CycleGAN},
3 author={Your Name},
4 year={2026},
5 publisher={Hugging Face},
6 howpublished={\url{https://huggingface.co/chunxue-dev2026/rgb2infrared}}
7}