densenet121实现图片大小为48*48四分类代码
时间: 2023-11-23 14:07:40 浏览: 69
以下是使用PyTorch实现DenseNet121进行48x48四分类的样例代码:
```python
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader
import torchvision.transforms as transforms
import numpy as np
from PIL import Image
# 定义数据集类
class MyDataset(Dataset):
def __init__(self, data_path, transform=None):
self.transform = transform
self.data = np.load(data_path)
self.imgs = self.data[:, :-1].reshape(-1, 48, 48).astype(np.uint8)[:, :, :, None]
self.labels = self.data[:, -1].astype(np.long)
def __getitem__(self, index):
img = Image.fromarray(self.imgs[index])
if self.transform is not None:
img = self.transform(img)
label = self.labels[index]
return img, label
def __len__(self):
return len(self.data)
# 定义模型类
class DenseNet(nn.Module):
def __init__(self):
super(DenseNet, self).__init__()
self.densenet = torch.hub.load('pytorch/vision:v0.6.0', 'densenet121', pretrained=True)
num_ftrs = self.densenet.classifier.in_features
self.densenet.classifier = nn.Linear(num_ftrs, 4)
def forward(self, x):
x = self.densenet(x)
return x
# 定义训练函数
def train(model, device, train_loader, optimizer, criterion):
model.train()
for batch_idx, (data, target) in enumerate(train_loader):
data, target = data.to(device), target.to(device)
optimizer.zero_grad()
output = model(data)
loss = criterion(output, target)
loss.backward()
optimizer.step()
# 定义测试函数
def test(model, device, test_loader, criterion):
model.eval()
test_loss = 0
correct = 0
with torch.no_grad():
for data, target in test_loader:
data, target = data.to(device), target.to(device)
output = model(data)
test_loss += criterion(output, target).item()
pred = output.argmax(dim=1, keepdim=True)
correct += pred.eq(target.view_as(pred)).sum().item()
test_loss /= len(test_loader.dataset)
accuracy = correct / len(test_loader.dataset) * 100.
print('Test set: Average loss: {:.4f}, Accuracy: {}/{} ({:.2f}%)'.format(
test_loss, correct, len(test_loader.dataset), accuracy))
if __name__ == '__main__':
# 加载数据和数据增强
transform_train = transforms.Compose([
transforms.RandomCrop(48, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
])
train_dataset = MyDataset('train_data.npy', transform_train)
test_dataset = MyDataset('test_data.npy', transform_test)
train_loader = DataLoader(train_dataset, batch_size=64, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=64, shuffle=False)
# 定义模型和优化器
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = DenseNet().to(device)
optimizer = optim.Adam(model.parameters(), lr=1e-3)
criterion = nn.CrossEntropyLoss()
# 训练和测试
for epoch in range(1, 21):
train(model, device, train_loader, optimizer, criterion)
test(model, device, test_loader, criterion)
```
其中,`MyDataset`类用于定义数据集,`DenseNet`类用于定义模型,`train`函数用于训练模型,`test`函数用于测试模型。在`if __name__ == '__main__':`中,加载数据、定义模型、定义优化器、定义损失函数、训练和测试模型。需要注意的是,在`MyDataset`中,需要将图片数据reshape成(48, 48, 1)的形状,且标签数据需要转换为`long`类型。
阅读全文