介绍

本文主要介绍Tensorflow读取本地数据的方式,目前给出了将本地图片数据制作成类似于Cifar10二进制数据集的程序。
其他数据导入方式持续更新中...

将本地图片数据制作成相同于数据集Cifar10 - binary version的形式

根据Cifar10数据集官网的描述,一张图片在二进制文件里存在的形式为(1Byte Label + 3072Byte Image),其中每个通道分别占有32 * 32 = 1024 个Byte.

Data -- a 10000x3072 numpy array of uint8s. Each row of the array stores a 32x32 colour image. The first 1024 entries contain the red channel values, the next 1024 the green, and the final 1024 the blue. The image is stored in row-major order, so that the first 32 entries of the array are the red channel values of the first row of the image.
Labels -- a list of 10000 numbers in the range 0-9. The number at index i indicates the label of the ith image in the array data.

完整程序如下:

import os
import cv2
import numpy as np
import time

file_dir = "Your image dir"

for file in os.listdir(file_dir):

    img = cv2.imread(file_dir + file)
    img = cv2.resize(img,(32, 32))
    if img is None:
        continue

    b = img[:,:,0].flatten()
    g = img[:,:,1].flatten()
    r = img[:,:,2].flatten()
    label = [0]

    out = np.array(list(label) + list(r) + list(g) + list(b),np.uint8)
    print(out)
    out.tofile("Your output dir")

print("All set!")
Last modification:January 10, 2023
您赞赏,我就多写点儿。