1. 程式人生 > >Tensorflow教程學習筆記(一)----將自己的資料集轉換成TFRecord

Tensorflow教程學習筆記(一)----將自己的資料集轉換成TFRecord

import tensorflow as tf
import numpy as np
import os
import matplotlib.pyplot as plt
import skimage.io as io
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

# %%

def get_file(file_dir):
    '''Get full image directory and corresponding labels
    Args:
        file_dir: file directory
    Returns:
        images: image directories, list, string
        labels: label, list, int
    '''

    images = []  #存放每張圖片的路徑[‘./notMNIST_small/A/MDEtMDEtMDAudHRm.png’,...,'./notMNIST_small/G/R2FyYW1vbmRQcmVtclByby1NZWRJdERpc3Aub3Rm.png]
    temp = []    #存放資料集下每一個子檔案的路徑['./notMNIST_small/A','./notMNIST_small/B',...,'/notMNIST_small/J']
    for root, sub_folders, files in os.walk(file_dir):

        # image directories
        for name in files:
            images.append(os.path.join(root, name))
        # get 10 sub-folder names
        for name in sub_folders:
            temp.append(os.path.join(root, name))


    # assign 10 labels based on the folder names
    labels = []
    #迴圈資料集下每一個子資料夾
    for one_folder in temp:
        #獲得子資料夾中圖片的個數,os.listdir() 方法用於返回指定的資料夾包含的檔案或資料夾的名字的列表
        n_img = len(os.listdir(one_folder))
        #用‘/'來劃分子資料夾路徑,如./notMNIST_small/A,取最後一個元素,其實就是獲得ABCDEFGHIJ
        letter = one_folder.split('/')[-1]
        #按照子資料夾名字的不同,來劃分類,貼上標籤,共10類
        if letter == 'A':
            labels = np.append(labels, n_img * [1])
        elif letter == 'B':
            labels = np.append(labels, n_img * [2])
        elif letter == 'C':
            labels = np.append(labels, n_img * [3])
        elif letter == 'D':
            labels = np.append(labels, n_img * [4])
        elif letter == 'E':
            labels = np.append(labels, n_img * [5])
        elif letter == 'F':
            labels = np.append(labels, n_img * [6])
        elif letter == 'G':
            labels = np.append(labels, n_img * [7])
        elif letter == 'H':
            labels = np.append(labels, n_img * [8])
        elif letter == 'I':
            labels = np.append(labels, n_img * [9])
        else:
            labels = np.append(labels, n_img * [10])

    # shuffle
    temp = np.array([images, labels])#[['/notMNIST_small/A/MDEtMDEtMDAudHRm.png',...,'/notMNIST_small/J/SWNvbmUgTFQgUmVndWxhciBJdGFsaWMgT3NGLnR0Zg==.png'],[1,1,1...10,10,10]]
    temp = temp.transpose()#[['/notMNIST_small/A/MDEtMDEtMDAudHRm.png',1],...,['/notMNIST_small/J/SWNvbmUgTFQgUmVndWxhciBJdGFsaWMgT3NGLnR0Zg==.png',10]
    np.random.shuffle(temp)#打亂順序

    image_list = list(temp[:, 0])#讀取temp中第0列,即images
    label_list = list(temp[:, 1])#讀取temp中第1列,即labels
    label_list = [int(float(i)) for i in label_list]

    return image_list, label_list


# %%
#輸入的圖片跟標籤都是特徵,因為其型別不同,故將labels轉換成int64,將圖片轉換成bytes
#生成整數型的屬性
def int64_feature(value):
    """Wrapper for inserting int64 features into Example proto."""
    if not isinstance(value, list):
        value = [value]
    return tf.train.Feature(int64_list=tf.train.Int64List(value=value))

#生成字串型整數
def bytes_feature(value):
    return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))


# %%

def convert_to_tfrecord(images, labels, save_dir, name):
    '''convert all images and labels to one tfrecord file.
    Args:
        images: list of image directories, string type
        labels: list of labels, int type
        save_dir: the directory to save tfrecord file, e.g.: '/home/folder1/'
        name: the name of tfrecord file, string type, e.g.: 'train'
    Return:
        no return
    Note:
        converting needs some time, be patient...
    '''
    #生成tfrecords檔案儲存路徑
    filename = os.path.join(save_dir, name + '.tfrecords')
    n_samples = len(labels)

    if np.shape(images)[0] != n_samples:
        raise ValueError('Images size %d does not match label size %d.' % (images.shape[0], n_samples))

    # wait some time here, transforming need some time based on the size of your data.
    #向這個檔案中寫入
    writer = tf.python_io.TFRecordWriter(filename)
    print('\nTransform start......')
    #迴圈所有圖片
    for i in np.arange(0, n_samples):
        try:
            #讀圖,需轉換成array
            image = io.imread(images[i])  # type(image) must be array!
            #將影象矩陣轉化成一個字串
            image_raw = image.tostring()
            label = int(labels[i])
            #將每個圖片和其對應的label寫入每一個example
            example = tf.train.Example(features=tf.train.Features(feature={
                'label': int64_feature(label),
                'image_raw': bytes_feature(image_raw)}))
            writer.write(example.SerializeToString())
        #如果圖片損壞,將錯誤資訊打印出來
        except IOError as e:
            print('Could not read:', images[i])
            print('error: %s' % e)
            print('Skip it!\n')
    writer.close()
    print('Transform done!')


# %%
#讀取tfrecord檔案,並生成批次
def read_and_decode(tfrecords_file, batch_size):
    '''read and decode tfrecord file, generate (image, label) batches
    Args:
        tfrecords_file: the directory of tfrecord file
        batch_size: number of images in each batch
    Returns:
        image: 4D tensor - [batch_size, width, height, channel]
        label: 1D tensor - [batch_size]
    '''
    # make an input queue from the tfrecord file
    #將檔案生成一個佇列
    filename_queue = tf.train.string_input_producer([tfrecords_file])
    #建立一個reader來讀取TFRecord檔案
    reader = tf.TFRecordReader()
    #從檔案中獨處一個樣例。也可以使用read_up_to函式一次性讀取多個樣例
    _, serialized_example = reader.read(filename_queue)
    #解析每一個元素。如果需要解析多個樣例,可以用parse_example函式
    img_features = tf.parse_single_example(
        serialized_example,
        features={
            'label': tf.FixedLenFeature([], tf.int64),
            'image_raw': tf.FixedLenFeature([], tf.string),
        })
    #tf.decode_raw可以將字串解析成影象對應的畫素陣列
    image = tf.decode_raw(img_features['image_raw'], tf.uint8)

    ##########################################################
    # you can put data augmentation here, I didn't use it
    ##########################################################
    # all the images of notMNIST are 28*28, you need to change the image size if you use other dataset.

    image = tf.reshape(image, [28, 28])
    label = tf.cast(img_features['label'], tf.int32)
    image_batch, label_batch = tf.train.batch([image, label],
                                              batch_size=batch_size,
                                              num_threads=64,
                                              capacity=2000)
    return image_batch, tf.reshape(label_batch, [batch_size])


# %% Convert data to TFRecord

test_dir = 'F://TensorFlow--middleteach//TFRecord_notmnist//notMNIST_small//'
save_dir = 'F://TensorFlow--middleteach//TFRecord_notmnist//'
BATCH_SIZE = 25

# Convert test data: you just need to run it ONCE !
name_test = 'test'
images, labels = get_file(test_dir)
tfrecords_file_dir='F://TensorFlow--middleteach//TFRecord_notmnist//test.tfrecords'
if not os.path.exists(tfrecords_file_dir):
    convert_to_tfrecord(images, labels, save_dir, name_test)


# %% TO test train.tfrecord file
#一個batchsize讀取25張圖,展示成5行5列
def plot_images(images, labels):
    '''plot one batch size
    '''
    for i in np.arange(0, BATCH_SIZE):
        plt.subplot(5, 5, i + 1)
        #關閉座標軸顯示
        plt.axis('off')
        '''ord()函式主要用來返回對應字元的ascii碼,chr()主要用來表示ascii碼對應的字元他的輸入時數字
            print ord('a)
             #97
            print chr(97)
            #a
        '''
        plt.title(chr(ord('A') + labels[i] - 1), fontsize=14)
        # plt.subplots_adjust(top=0.5)
        plt.imshow(images[i])
    plt.show()


tfrecords_file = 'F://TensorFlow--middleteach//TFRecord_notmnist//test.tfrecords'
image_batch, label_batch = read_and_decode(tfrecords_file, batch_size=BATCH_SIZE)

with tf.Session()  as sess:
    i = 0
    #啟用多執行緒處理資料
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord)

    try:
        while not coord.should_stop() and i < 1:
            # just plot one batch size
            image, label = sess.run([image_batch, label_batch])
            plot_images(image, label)
            i += 1

    except tf.errors.OutOfRangeError:
        print('done!')
    finally:
        coord.request_stop()
    coord.join(threads)