修改路径即可运行,当然应该也可以保存为其他格式如png:
(当然下面的库要自行安装)
import os
import idx2numpy
from PIL import Image
from tqdm import tqdm
def save_images(images, labels, target_dir):
for label in range(10):
label_dir = os.path.join(target_dir, str(label))
os.makedirs(label_dir, exist_ok=True)
# 获取当前标签的所有图像
label_images = images[labels == label]
# 为当前标签的每张图像显示进度条
for i, img in enumerate(tqdm(label_images, desc=f"Processing {target_dir}/{label}", ascii=True)):
img_path = os.path.join(label_dir, f"{i}.jpg")
img = Image.fromarray(img)
img.save(img_path)
#! MNIST数据集文件路径
train_img_path = r'D:\000.RemoteRepository\data_set\MNIST_data\MNIST\raw/train-images-idx3-ubyte'
train_lbl_path = r'D:\000.RemoteRepository\data_set\MNIST_data\MNIST\raw/train-labels-idx1-ubyte'
test_img_path = r'D:\000.RemoteRepository\data_set\MNIST_data\MNIST\raw/t10k-images-idx3-ubyte'
test_lbl_path = r'D:\000.RemoteRepository\data_set\MNIST_data\MNIST\raw/t10k-labels-idx1-ubyte'
# 读取数据集
train_images = idx2numpy.convert_from_file(train_img_path)
train_labels = idx2numpy.convert_from_file(train_lbl_path)
test_images = idx2numpy.convert_from_file(test_img_path)
test_labels = idx2numpy.convert_from_file(test_lbl_path)
# 保存图像
save_images(train_images, train_labels, 'train')
save_images(test_images, test_labels, 'test')
运行结果:
另外,一些注意的事项,数据集中的数据,都是在图片中间的,如果你写了一个数字,是在图片角落,有可能识别错误(原图是28x28,但是 当你放到模型训练的 图片的尺寸被你缩放到128 或者256;此时可能预测不是那么准确,估计需要调整预训练方法---(有大佬知道可以答复一下) )
比如下面预测 这张512的图片(我训练的时候会把原图28 缩放到 128来训练),其写在了左上角。就会预测错误,
下载这张写在中间的,就会预测正确: