asysbang

 找回密码
 立即注册
查看: 3200|回复: 0
打印 上一主题 下一主题

Fashion MNIST数据解压图片

[复制链接]

520

主题

2

好友

6551

积分

管理员

Rank: 80Rank: 80Rank: 80Rank: 80Rank: 80

最佳新人 活跃会员 热心会员 推广达人 宣传达人 灌水之王 突出贡献 优秀版主 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2019-11-22 11:39:45 |只看该作者 |倒序浏览
import cv2
import numpy as np
import os
img_path = '/home/ai/.keras/datasets/fashion-mnist/train-images-idx3-ubyte'
label_path = '/home/ai/.keras/datasets/fashion-mnist/train-labels-idx1-ubyte'
save_path = '/home/ai/.keras/datasets/fashion-mnist/traindata'
img_path2 = '/home/ai/.keras/datasets/fashion-mnist/t10k-images-idx3-ubyte'
label_path2 = '/home/ai/.keras/datasets/fashion-mnist/t10k-labels-idx1-ubyte'
save_path2 = '/home/ai/.keras/datasets/fashion-mnist/testdata'


classes = { 0: 'T-shirt_or_top',
            1:        'Trouser',
            2:        'Pullover',
            3:        'Dress',
            4:        'Coat',
            5:        'Sandal',
            6:        'Shirt',
            7:        'Sneaker',
            8:        'Bag',
            9:        'Ankle boot'}

def save(img_path ,label_path, savepath, num):
    f = open(img_path,'rb')
    la_f = open(label_path,'rb')
    la_f.read(8)
    f.read(16)
    dict = {}
    for n in range(num):
        image = []
        for i in range(28*28):
            image.append(ord(f.read(1)))
        image = np.array(image).reshape(28,28)
        name = classes[ord(la_f.read(1))]
        filepath = os.path.join(savepath, name)
        if not os.path.isdir(filepath):
            os.makedirs(filepath)
        if name not in dict:
            dict[name] = 1
        else:
            dict[name] += 1
        png = str(dict[name])+'.png'
        save_path = os.path.join(filepath, png)
        cv2.imwrite(save_path, image)
    la_f.close()
    f.close()

save(img_path,label_path,save_path,num=60000)
save(img_path2,label_path2,save_path2,num=10000)


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

Archiver|手机版|aSys-帮 ( 京ICP备13033689号 )

GMT+8, 2026-1-28 16:08 , Processed in 0.041033 second(s), 19 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部