提示:个人分类数据集预处理:
MNIST是经典的手写数字分类数据集,数据集中的图像是灰度图像,图像格式为png,图像尺寸为28*28,最主要的是MNIST数据集格式如下图所示。
我们怎么把自己制作的分类数据集中jpg或者png格式的图片及标签转化为上面那种ubyte格式,因为,很多算法用如下代码来加载MNIST数据,所以我们也可以把自己的数据格式转化为MNIST格式,也这样调用。
train_dataset = torchvision.datasets.MNIST(root= data_path, train=True, download=True, transform=transforms.ToTensor())
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True, num_workers=0)
test_set = torchvision.datasets.MNIST(root= data_path, train=False, download=True, transform=transforms.ToTensor())
test_loader = torch.utils.data.DataLoader(test_set, batch_size=batch_size, shuffle=False, num_workers=0)
提示:以下是本篇文章正文内容,下面案例可供参考
自建的交通标志分类部分数据如下截图;
右转交通标志
左转交通标志
我的分类数据集为10类交通标志图片,类别分别为
[‘ahead’, ‘clearway_no_stopping’, ‘crosswalk’, ‘motorway’, ‘non-motorized_lane’, ‘speed_limit_50’, ‘split-way’, ‘turn_left’, ‘turn_right’, ‘warning_sign’]
10类交通标志分类数据集下载链接---->10类4000张交通标志数据集
有需要的可以下载
from PIL import Image
import os
INPUT_PATH = r'F:\DL_xian_data\tools\training-images\9' #原始图像存储路径
OUPUT_PATH = r'F:\DL_xian_data\tools\training-images\9_gray' #转化为灰度图像 存储路径
files_list = os.listdir(INPUT_PATH) # 读取列表信息,可打印查看
for file in files_list:
# Version1
I = Image.open(INPUT_PATH + "/" + file)
L = I.convert('L') # Image包内将“L“代表灰度
L.save(OUPUT_PATH + "/" + file)
import os
import os.path
from PIL import Image
#准备拉伸的原图片存储路径
infile = r'F:\DL_xian_data\SNN_model\dataset\turn_left'
#拉伸后的图片存储路径
outfile = r'F:\DL_xian_data\SNN_model\dataset_28_28\7'
list_img = os.listdir(infile)
n = 0
l = len(list_img)
for each_img in list_img:
# 每个图像全路径
print(each_img)
image_input_fullname = infile + '/' + each_img
resize_img = Image.open(image_input_fullname)
#可以自定义大小
out_w = 28
out_h = 28
out_img = resize_img.resize((out_w,out_h), Image.ANTIALIAS)
# 裁剪后每个图像的路径+名称
image_output_fullname = outfile + "/" + each_img
out_img.convert('RGB')
out_img.save(image_output_fullname)
n += 1
print('%d/%d img has been resized!' %(n,l))
print('total_num is {%d} success resized img!' %len(list_img))
import os
from PIL import Image
from array import *
from random import shuffle
# Load from and save to
Names = [['train-images','train'], ['t10k-images','t10k']] #train-images为训练集存储文件夹 t10k-images为测试集存储文件夹
for name in Names:
data_image = array('B')
data_label = array('B')
FileList = []
for dirname in os.listdir(name[0]): # [1:] Excludes .DS_Store from Mac OS
path = os.path.join(name[0],dirname)
for filename in os.listdir(path):
if filename.endswith(".png"):
FileList.append(os.path.join(name[0],dirname,filename))
shuffle(FileList) # Usefull for further segmenting the validation set
for filename in FileList:
print(FileList)
print(filename)
label = int(filename.split('\\')[1])
print(label)
Im = Image.open(filename)
pixel = Im.load()
width, height = Im.size
for x in range(0,width):
for y in range(0,height):
#data_image.append(pixel[y,x]) #使用这个会报错 “IndexError: list index out of range”
data_image.append(Im.getpixel((x, y))) # 改动的地方,能够完美运行不同尺寸的图像
data_label.append(label) # labels start (one unsigned byte each)
hexval = "{0:#0{1}x}".format(len(FileList),6) # number of files in HEX
# header for label array
header = array('B')
header.extend([0,0,8,1,0,0])
header.append(int('0x'+hexval[2:][:2],16))
header.append(int('0x'+hexval[2:][2:],16))
data_label = header + data_label
# additional header for images array
if max([width,height]) <= 256:
header.extend([0,0,0,width,0,0,0,height])
else:
raise ValueError('Image exceeds maximum size: 256x256 pixels');
header[3] = 3 # Changing MSB for image data (0x00000803)
data_image = header + data_image
output_file = open(name[1]+'-images-idx3-ubyte', 'wb')
data_image.tofile(output_file)
output_file.close()
output_file = open(name[1]+'-labels-idx1-ubyte', 'wb')
data_label.tofile(output_file)
output_file.close()
# gzip resulting files
for name in Names:
os.system('gzip '+name[1]+'-images-idx3-ubyte')
os.system('gzip '+name[1]+'-labels-idx1-ubyte')
MNIST格式的10类交通标志数据集下载链接---->交通标志数据集10类下载链接MNIST格式
以上就是今天要讲的内容,本文仅仅简单介绍了自建分类数据集转换为MNIST格式数据的过程,作者转换过程遇到一些坑,所以特此记录,供需要的人参考。