import torch import torchvision import numpy as np import cv2 import matplotlib.pyplot as plt

定义Fashion-MNIST数据集

test_dataset = torchvision.datasets.FashionMNIST(root='./data', train=False, download=True, transform=torchvision.transforms.ToTensor())

定义数据加载器

test_dataloader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=1, shuffle=True)

选择一张图像进行旋转

for i, (images, labels) in enumerate(test_dataloader): if i == 0: # 将图像转换成numpy数组 img = np.array(images[0].reshape(28, 28)) # 旋转45度 img_rotate45 = cv2.rotate(img, cv2.ROTATE_45) # 绘制原图像和旋转后的图像 fig, axs = plt.subplots(1, 2, figsize=(8, 5)) axs[0].imshow(img, cmap='gray') axs[0].set_title("Original") axs[1].imshow(img_rotate45, cmap='gray') axs[1].set_title("Rotate 45") plt.show(

请修改正确下面代码完成从二进制格式的Fashion-MNIST中读取数据选择一张图像进行45度旋转。# 选择一张图像进行旋转和翻转for i images labels in enumeratetest_dataloader if i == 0 # 将图像转换成numpy数组 img = nparrayimages0reshape28 28 # 旋转

原文地址: https://www.cveoy.top/t/topic/efDV 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录