以下是使用Pytorch实现基于DenseNet的11 * 11一通道图像分类的示例代码:
```python
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader, Dataset
from torch.optim.lr_scheduler import MultiStepLR
# Define hyperparameters
num_epochs = 50
batch_size = 128
learning_rate = 0.01
num_classes = 10
num_layers_per_block = 4
growth_rate = 32
compression_factor = 0.5
# Define custom dataset
class CustomDataset(Dataset):
def __init__(self, data, transform=None):
self.data = data
self.transform = transform
def __len__(self):
return len(self.data)
def __getitem__(self, index):
img = self.data[index][0]
if self.transform:
img = self.transform(img)
return img, self.data[index][1]
# Define DenseNet block
class DenseNetBlock(nn.Module):
def __init__(self, in_channels, growth_rate, num_layers):
super(DenseNetBlock, self).__init__()
self.layers = nn.ModuleList()
for i in range(num_layers):
self.layers.append(nn.Sequential(
nn.BatchNorm2d(in_channels + i * growth_rate),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels + i * growth_rate, growth_rate, kernel_size=1, bias=False),
nn.BatchNorm2d(growth_rate),
nn.ReLU(inplace=True),
nn.Conv2d(growth_rate, growth_rate, kernel_size=3, padding=1, bias=False)
))
def forward(self, x):
for layer in self.layers:
out = layer(x)
x = torch.cat([x, out], 1)
return x
# Define DenseNet model
class DenseNet(nn.Module):
def __init__(self, num_classes, num_layers_per_block, growth_rate, compression_factor):
super(DenseNet, self).__init__()
self.features = nn.Sequential(
nn.Conv2d(1, 2*growth_rate, kernel_size=3, padding=1, bias=False),
nn.BatchNorm2d(2*growth_rate),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2)
)
num_channels = 2 * growth_rate
self.num_blocks = len(num_layers_per_block)
for i, num_layers in enumerate(num_layers_per_block):
block = DenseNetBlock(num_channels, growth_rate, num_layers)
self.features.add_module("denseblock%d" % (i + 1), block)
num_channels += num_layers * growth_rate
if i != self.num_blocks - 1:
num_channels = int(num_channels * compression_factor)
trans = nn.Sequential(
nn.Conv2d(num_channels, num_channels, kernel_size=1, bias=False),
nn.BatchNorm2d(num_channels),
nn.ReLU(inplace=True),
nn.AvgPool2d(kernel_size=2, stride=2)
)
self.features.add_module("transition%d" % (i + 1), trans)
self.classifier = nn.Linear(num_channels, num_classes)
def forward(self, x):
x = self.features(x)
x = nn.functional.avg_pool2d(x, kernel_size=(x.size(2), x.size(3)))
x = x.view(x.size(0), -1)
x = self.classifier(x)
return x
# Initialize model and optimizer
model = DenseNet(num_classes, num_layers_per_block, growth_rate, compression_factor)
optimizer = optim.SGD(model.parameters(), lr=learning_rate, momentum=0.9, weight_decay=1e-4)
scheduler = MultiStepLR(optimizer, milestones=[int(num_epochs*0.5), int(num_epochs*0.75)], gamma=0.1)
# Load data
train_data = torch.load('./train_data.pt')
test_data = torch.load('./test_data.pt')
train_dataset = CustomDataset(train_data)
test_dataset = CustomDataset(test_data)
train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False)
# Train model
model.train()
for epoch in range(num_epochs):
for i, (images, labels) in enumerate(train_loader):
# Forward pass
outputs = model(images)
loss = nn.functional.cross_entropy(outputs, labels)
# Backward and optimize
optimizer.zero_grad()
loss.backward()
optimizer.step()
# Print training progress
print('Epoch [{}/{}], Loss: {:.4f}'.format(epoch+1, num_epochs, loss.item()))
# Update learning rate
scheduler.step()
# Test model
model.eval()
with torch.no_grad():
correct = 0
total = 0
for images, labels in test_loader:
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
# Print test accuracy
print('Test Accuracy of the model on the 10000 test images: {} %'.format(100 * correct / total))
```
这个示例代码基于DenseNet的思想,使用了一个堆叠的DenseNet块来构建网络。每个块由多个DenseNet层组成,每个层都通过连接前面所有层的方式进行连接,以最大化信息流动。这个代码还包括数据加载、训练和测试等常用的功能。由于输入图像是灰度图像,所以在模型定义中将输入通道数设置为1。