tiny imagenet 训练_Dataloader读取图片集并训练网络python实现
时间: 2024-02-25 10:58:02 浏览: 238
以下是使用PyTorch实现Dataloader读取Tiny ImageNet数据集并训练网络的示例代码:
import torch
import torchvision
import torchvision.transforms as transforms
from torch.utils.data import Dataset, DataLoader
from torch.utils.data.sampler import SubsetRandomSampler
# 定义自定义数据集类
class TinyImageNetDataset(Dataset):
def __init__(self, data_dir, transform=None):
self.data_dir = data_dir
self.transform = transform
self.image_paths = []
self.labels = []
with open(data_dir + '/wnids.txt', 'r') as f:
self.classes = [line.strip() for line in f.readlines()]
for i, cls in enumerate(self.classes):
for img_file in os.listdir(data_dir + '/train/' + cls + '/images/'):
self.image_paths.append(data_dir + '/train/' + cls + '/images/' + img_file)
self.labels.append(i)
def __len__(self):
return len(self.labels)
def __getitem__(self, idx):
image_path = self.image_paths[idx]
image = Image.open(image_path).convert('RGB')
label = self.labels[idx]
if self.transform:
image = self.transform(image)
return image, label
# 定义数据增强和预处理操作
transform_train = transforms.Compose([
transforms.RandomCrop(64, padding=4),
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(64),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
# 创建训练、验证和测试数据集的实例
train_dataset = TinyImageNetDataset(data_dir='/path/to/tiny-imagenet-200', transform=transform_train)
val_dataset = TinyImageNetDataset(data_dir='/path/to/tiny-imagenet-200', transform=transform_test)
test_dataset = TinyImageNetDataset(data_dir='/path/to/tiny-imagenet-200', transform=transform_test)
# 创建随机子采样器
train_sampler = SubsetRandomSampler(range(100000))
val_sampler = SubsetRandomSampler(range(10000))
test_sampler = SubsetRandomSampler(range(10000))
# 创建Dataloader
train_loader = DataLoader(train_dataset, batch_size=128, sampler=train_sampler, num_workers=4)
val_loader = DataLoader(val_dataset, batch_size=128, sampler=val_sampler, num_workers=4)
test_loader = DataLoader(test_dataset, batch_size=128, sampler=test_sampler, num_workers=4)
# 定义神经网络模型
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1)
self.conv2 = nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1)
self.conv3 = nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=1)
self.fc1 = nn.Linear(256 * 8 * 8, 1024)
self.fc2 = nn.Linear(1024, 200)
def forward(self, x):
x = F.relu(self.conv1(x))
x = F.max_pool2d(x, kernel_size=2, stride=2)
x = F.relu(self.conv2(x))
x = F.max_pool2d(x, kernel_size=2, stride=2)
x = F.relu(self.conv3(x))
x = F.max_pool2d(x, kernel_size=2, stride=2)
x = x.view(-1, 256 * 8 * 8)
x = F.relu(self.fc1(x))
x = self.fc2(x)
return x
# 实例化神经网络模型、损失函数和优化器
net = Net()
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(net.parameters(), lr=0.001, momentum=0.9)
# 训练循环
for epoch in range(10):
running_loss = 0.0
for i, data in enumerate(train_loader, 0):
inputs, labels = data
optimizer.zero_grad()
outputs = net(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
if i % 100 == 99:
print('[%d, %5d] loss: %.3f' % (epoch + 1, i + 1, running_loss / 100))
running_loss = 0.0
# 验证和测试循环
correct = 0
total = 0
with torch.no_grad():
for data in val_loader:
images, labels = data
outputs = net(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
val_accuracy = correct / total
print('Validation accuracy: %.2f %%' % (100 * val_accuracy))
correct = 0
total = 0
with torch.no_grad():
for data in test_loader:
images, labels = data
outputs = net(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
test_accuracy = correct / total
print('Test accuracy: %.2f %%' % (100 * test_accuracy))
注意:上述代码仅供参考,实际使用时需要根据自己的需求进行修改。