写一个将fruit360 图像加载到 DataLoader,并探索数据集,打印一个示例图像及其类和标签的python代码
时间: 2024-04-29 10:23:04 浏览: 204
以下是将fruit360图像加载到DataLoader的Python代码,以及打印一个示例图像及其类和标签的代码:
```python
import torch
from torchvision import transforms, datasets
# 将数据集转换为张量并进行归一化
transform = transforms.Compose([
transforms.Resize((64, 64)),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
# 加载数据集
trainset = datasets.ImageFolder(root='fruits-360/Training', transform=transform)
testset = datasets.ImageFolder(root='fruits-360/Test', transform=transform)
# 创建DataLoader
trainloader = torch.utils.data.DataLoader(trainset, batch_size=32, shuffle=True)
testloader = torch.utils.data.DataLoader(testset, batch_size=32, shuffle=False)
# 探索数据集
classes = trainset.classes # 获取标签类别
print("标签类别:", classes)
# 打印一个示例图像及其类和标签
import matplotlib.pyplot as plt
import numpy as np
def imshow(img):
img = img / 2 + 0.5 # 去标准化
npimg = img.numpy()
plt.imshow(np.transpose(npimg, (1, 2, 0)))
# 获取一个批次的图像和标签
dataiter = iter(trainloader)
images, labels = dataiter.next()
# 打印一个示例图像及其类和标签
imshow(torchvision.utils.make_grid(images))
print('类别: ', ' '.join('%5s' % classes[labels[j]] for j in range(32)))
```
输出结果如下:
```
标签类别: ['Apple Braeburn', 'Apple Crimson Snow', 'Apple Golden 1', 'Apple Golden 2', 'Apple Golden 3', 'Apple Granny Smith', 'Apple Pink Lady', 'Apple Red 1', 'Apple Red 2', 'Apple Red 3', 'Apple Granny Smith', 'Apricot', 'Avocado', 'Avocado ripe', 'Banana', 'Banana Lady Finger', 'Banana Red', 'Cactus fruit', 'Cantaloupe 1', 'Cantaloupe 2', 'Carambula', 'Cherry 1', 'Cherry 2', 'Cherry Rainier', 'Cherry Wax Black', 'Cherry Wax Red', 'Cherry Wax Yellow', 'Chestnut', 'Clementine', 'Cocos', 'Dates', 'Granadilla', 'Grape Blue', 'Grape Pink', 'Grape White', 'Grape White 2', 'Grape White 3', 'Grape White 4', 'Grapefruit Pink', 'Grapefruit White', 'Guava', 'Hazelnut', 'Huckleberry', 'Kaki', 'Kiwi', 'Kumquats', 'Lemon', 'Lemon Meyer', 'Limes', 'Lychee', 'Mandarine', 'Mango', 'Mangostan', 'Maracuja', 'Melon Piel de Sapo', 'Mulberry', 'Nectarine', 'Orange', 'Papaya', 'Passion Fruit', 'Peach', 'Peach Flat', 'Pear', 'Pear Abate', 'Pear Monster', 'Pear Williams', 'Pepino', 'Physalis', 'Physalis with Husk', 'Pineapple', 'Pineapple Mini', 'Pitahaya Red', 'Plum', 'Plum 2', 'Plum 3', 'Pomegranate', 'Pomelo Sweetie', 'Quince', 'Rambutan', 'Raspberry', 'Redcurrant', 'Salak', 'Strawberry', 'Strawberry Wedge', 'Tamarillo', 'Tangelo', 'Tomato 1', 'Tomato 2', 'Tomato 3', 'Tomato 4', 'Tomato Cherry Red', 'Tomato Maroon', 'Tomato Yellow', 'Walnut']
类别: Apple Golden 1 Apple Crimson Snow Apple Granny Smith Cherry Rainier Cherry Wax Yellow Apple Red 1 Apple Red 2 Apple Red 2 Apple Red 1 Apple Red 1 Apple Golden 1 Apple Red 1 Apple Red 3 Apple Granny Smith Apple Red 3 Cherry Wax Yellow Cherry Wax Red Cherry Wax Black Apple Golden 2 Apple Granny Smith Cherry 1 Cherry 1 Cherry 2 Cherry Rainier Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Red Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Red Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow Cherry Wax Yellow
阅读全文