python编程4个函数。dataset函数中写两个加载数据集的类。一个用于加载训练集和验证集,对coco数据集的单张自然图像进行预处理,返回tensor形式的自然图像。另一个用于加载测试集,对TNO数据集的红外与可见光图像进行预处理,返回tensor形式红外与可见光图像。net函数中构建出此融合方法的网络,在初始化函数中定义各种神经网络操作,并编写encoder、fusion和decoder函数,分别表示编码器、融合策略和解码器。train函数需要实例化训练网络、dataset和dataloader,设置优化器与训练轮数等参数。在训练中对训练集进行多轮训练,每轮训练结束后对验证集进行验证,记录每轮的平均训练损失和平均验证损失,并将模型保存到pth文件夹中。test函数需要实例化测试集、加载模型参数,融合测试集中的红外与可见光图像,并保存到result文件夹中。
时间: 2023-07-15 12:14:34 浏览: 93
简单了解Python中的几种函数
很高兴为您提供代码示例:
```python
import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import transforms
from torch.utils.data import Dataset, DataLoader
# 定义加载数据集的类
class COCODataset(Dataset):
def __init__(self, data_dir, transform=None):
self.data_dir = data_dir
self.transform = transform
def __len__(self):
return len(os.listdir(self.data_dir))
def __getitem__(self, idx):
img_path = os.path.join(self.data_dir, f"{idx}.jpg")
img = Image.open(img_path).convert("RGB")
if self.transform:
img = self.transform(img)
return img
class TNODataset(Dataset):
def __init__(self, data_dir, transform=None):
self.data_dir = data_dir
self.transform = transform
def __len__(self):
return len(os.listdir(self.data_dir))
def __getitem__(self, idx):
ir_path = os.path.join(self.data_dir, f"{idx}_ir.jpg")
vis_path = os.path.join(self.data_dir, f"{idx}_vis.jpg")
ir_img = Image.open(ir_path).convert("RGB")
vis_img = Image.open(vis_path).convert("RGB")
if self.transform:
ir_img = self.transform(ir_img)
vis_img = self.transform(vis_img)
return ir_img, vis_img
# 定义网络结构
class FusionNet(nn.Module):
def __init__(self):
super(FusionNet, self).__init__()
self.encoder = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(128),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(256),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(256, 512, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(512),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
)
self.fusion = nn.Sequential(
nn.Conv2d(1024, 512, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(512),
nn.ReLU(inplace=True),
nn.Conv2d(512, 256, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(256),
nn.ReLU(inplace=True),
nn.Conv2d(256, 128, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(128),
nn.ReLU(inplace=True),
)
self.decoder = nn.Sequential(
nn.ConvTranspose2d(128, 64, kernel_size=4, stride=2, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.ConvTranspose2d(64, 32, kernel_size=4, stride=2, padding=1),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True),
nn.ConvTranspose2d(32, 3, kernel_size=4, stride=2, padding=1),
nn.Tanh(),
)
def forward(self, ir, vis):
ir_enc = self.encoder(ir)
vis_enc = self.encoder(vis)
enc = torch.cat((ir_enc, vis_enc), dim=1)
fusion = self.fusion(enc)
dec = self.decoder(fusion)
return dec
# 定义训练函数
def train(model, train_loader, val_loader, criterion, optimizer, num_epochs, device):
best_loss = float('inf')
for epoch in range(num_epochs):
train_loss = 0.0
val_loss = 0.0
model.train() # 将模型设为训练模式
for i, data in enumerate(train_loader):
ir, vis = data
ir = ir.to(device)
vis = vis.to(device)
optimizer.zero_grad()
outputs = model(ir, vis)
loss = criterion(outputs, ir)
loss.backward()
optimizer.step()
train_loss += loss.item()
model.eval() # 将模型设为验证模式
with torch.no_grad():
for i, data in enumerate(val_loader):
ir, vis = data
ir = ir.to(device)
vis = vis.to(device)
outputs = model(ir, vis)
loss = criterion(outputs, ir)
val_loss += loss.item()
train_loss /= len(train_loader)
val_loss /= len(val_loader)
print(f"Epoch {epoch+1} - Train Loss: {train_loss:.4f} - Val Loss: {val_loss:.4f}")
if val_loss < best_loss:
best_loss = val_loss
torch.save(model.state_dict(), "pth/model.pth")
# 定义测试函数
def test(model, test_loader, device):
model.load_state_dict(torch.load("pth/model.pth"))
model.eval() # 将模型设为测试模式
with torch.no_grad():
for i, data in enumerate(test_loader):
ir, vis = data
ir = ir.to(device)
vis = vis.to(device)
outputs = model(ir, vis)
img = torch.cat((ir, vis, outputs), dim=0)
img = transforms.ToPILImage()(img.cpu())
img.save(f"result/{i}.jpg")
# 数据预处理
transform_train = transforms.Compose([
transforms.Resize((256, 256)),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
transform_test = transforms.Compose([
transforms.Resize((256, 256)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
# 实例化数据集和数据加载器
train_val_dataset = COCODataset("data/train_val", transform_train)
test_dataset = TNODataset("data/test", transform_test)
train_dataset, val_dataset = torch.utils.data.random_split(train_val_dataset, [80000, 20000])
train_loader = DataLoader(train_dataset, batch_size=64, shuffle=True)
val_loader = DataLoader(val_dataset, batch_size=64, shuffle=False)
test_loader = DataLoader(test_dataset, batch_size=1, shuffle=False)
# 实例化模型和优化器
model = FusionNet().to(device)
criterion = nn.MSELoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
# 训练模型
train(model, train_loader, val_loader, criterion, optimizer, num_epochs=10, device=device)
# 测试模型
test(model, test_loader, device=device)
```
阅读全文