如何读取(解码).tfrecords文件,查看内部图像并进行扩充? [英] How to read (decode) .tfrecords file, see the images inside and do augmentation?

查看:92
本文介绍了如何读取(解码).tfrecords文件,查看内部图像并进行扩充?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 .tfrecords 文件,我想提取文件,查看文件中的图像并进行扩充.我正在使用

噪声:

模糊:

噪声模糊:

噪声模糊镜像:

增强后每个标签的图像数量(每个标签精确平衡30张图像):


与上面相同的扩充,但是对于带有标签图像的输入和输出文件夹,而不是TFRecordDataset,请将 c_inp_dir c_out_dir 更改为您的文件夹路径:

  import osos.environ ['TF_CPP_MIN_LOG_LEVEL'] ='3'将tensorflow导入为tf,将tensorflow_addons导入为tfa,将PIL.Image,将numpy导入为np,将数学,将matplotlib.pyplot导入为pltc_inp_dir ='./images/'c_out_dir ='./images_out/'c_augment_types =('noise','blur','noise_blur','noise_blur_mirror')c_res_class_size = None#如果为None,则自动配置为最大类大小def calc_labels(dirn = None):如果dirn为None:礼服= c_inp_dircnts,标签= {},[]对于已排序的标签(os.listdir(f'{dirn}')):label = int(标签)labels.append(标签)cnts [label] = len(os.listdir(f'{dirn}/{label}/'))返回cnts,标签def img_gen():cnts = {}对于已排序的标签(os.listdir(c_inp_dir)):label = int(标签)对于已排序的fname(os.listdir(f'{c_inp_dir}/{label}/')):img_arr = np.array(PIL.Image.open(f'{c_inp_dir}/{label}/{fname}'))产量标签,img_arr,fnamedef gaussian_noise(inp,stddev):噪声= tf.random.normal(shape = tf.shape(inp),均值= 0.0,stddev = stddev,dtype = inp.dtype)返回inp +噪声def扩充(a,cnt):min_noise_stddev,max_noise_stddev = 5.,20.blur_kern,min_blur_stddev,max_blur_stddev = 3、1、5.断言cnt> = 1pad_a = lambda x:np.pad(x,((0,2 ** math.ceil(math.log(x.shape [0])/math.log(2))-x.shape [0]),(0,2 ** math.ceil(math.log(x.shape [1])/math.log(2))-x.shape [1]),(0,0)),constant_values = 0)post_a = lambda x:np.clip(x [:a.shape [0],:a.shape [1]],0,255).astype(np.uint8)产生'orig',a中位数-= 1res = []fcnt = math.ceil(cnt/len(c_augment_types))linsp = lambda l,r,c:[(l +(i +1)*(r-l)/(c +1))对于范围(c)中的i]对于zip(linsp(min_noise_stddev,max_noise_stddev,fcnt),linsp(min_blur_stddev,max_blur_stddev,fcnt)中的noise_stddev,blur_stddev :)如果c_augment_types中的噪声"为:#yield'noise',post_a(tf.keras.layers.GaussianNoise(stddev = noise_stddev)(prep_a,training = True).numpy())res.append(('noise',post_a(gaussian_noise(a.astype(np.float32),stddev = noise_stddev).numpy())))如果c_augment_types中的模糊":res.append(('blur',post_a(tfa.image.gaussian_filter2d(pad_a(a).astype(np.float32),filter_shape = blur_kern,sigma = blur_stddev).numpy())))如果c_augment_types中为'noise_blur'或c_augment_types中为'noise_blur_mirror':nbr = post_a(tfa.image.gaussian_filter2d(pad_a(gaussian_noise(a.astype(np.float32),stddev = noise_stddev).numpy()),filter_shape = blur_kern,sigma = blur_stddev).numpy())如果c_augment_types中的'noise_blur':res.append(('noise_blur',nbr))如果c_augment_types中的'noise_blur_mirror':res.append(('noise_blur_mirror',tf.image.flip_left_right(nbr).numpy().astype(np.uint8)))断言cnt< = len(res)< = cnt + len(c_augment_types),(cnt,len(res),len(c_augment_types))res的收益[:cnt]def process():labels_cnts,标签= calc_labels()max_class_size = max(labels_cnts.values())如果c_res_class_size不为None:断言max_class_size< = c_res_class_size,f'最大类大小为{max_class_size},而请求的res类大小更小,{c_res_class_size}!"class_size = c_res_class_size别的:class_size = max_class_sizecur_labels_cnts = {}对于枚举(img_gen())中的iimg((label,imga,fname)):os.makedirs(f'{c_out_dir}/{label}/',exist_ok = True)cur_labels_cnts [label] = cur_labels_cnts.get(label,0)+ 1need_cnt = class_size//labels_cnts [label] + int(cur_labels_cnts [label]< = class_size%labels_cnts [label])对于iaug,(taug,aug)枚举(augment(imga,need_cnt)):PIL.Image.fromarray(aug).save(f'{c_out_dir}/{label}/{fname}.{iaug} _ {taug} .png')如果(iimg%10)== 0:打印(iimg,'',sep ='',end ='',flush = True)def plot_cnts(dirn):labels_cnts = calc_labels(dirn)[0]x,y = zip(* sorted(labels_cnts.items(),key = lambda e:e [0]))plt.xlabel('标签')plt.ylabel('num images')plt.plot(x,y)plt.xticks(x)plt.show()def main():过程()plot_cnts(c_inp_dir)plot_cnts(c_out_dir)主要的() 

I have a .tfrecords file and I want to extract, see the images in the file and augment them. I am using https://colab.research.google.com TensorFlow version: 2.3.0

And for the following code

raw_dataset = tf.data.TFRecordDataset("*path.tfrecords")

for raw_record in raw_dataset.take(1):
    example = tf.train.Example()
    example.ParseFromString(raw_record.numpy())
    print(example)

I am facing the following output:

features {
  feature {
    key: "depth"
    value {
      int64_list {
        value: 3
      }
    }
  }
  feature {
    key: "height"
    value {
      int64_list {
        value: 333
      }
    }
  }
  feature {
    key: "image_raw"
    value {
      bytes_list {
        value:
      }
    }
  }
  feature {
    key: "label"
    value {
      int64_list {
        value: 16
      }
    }
  }
  feature {
    key: "width"
    value {
      int64_list {
        value: 500
      }
    }
  }
}

解决方案

Here is a simple code that can extract your .tfrecord images as .png format.

To run next codes you need to install one time pip modules through pip install tensorflow tensorflow_addons pillow numpy matplotlib.

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 
import tensorflow as tf, PIL.Image, numpy as np

raw_dataset = tf.data.TFRecordDataset('max_32_set.tfrecords')

for i, raw_record in enumerate(raw_dataset.take(3)):
    example = tf.train.Example()
    example.ParseFromString(raw_record.numpy())
    info = {}
    for k, v in example.features.feature.items():
        if k == 'image_raw':
            info[k] = v.bytes_list.value[0]
        elif k in ['depth', 'height', 'width']:
            info[k] = v.int64_list.value[0]
    img_arr = np.frombuffer(info['image_raw'], dtype = np.uint8).reshape(
        info['height'], info['width'], info['depth']
    )
    # You can use img_arr numpy array above to directly augment/preprocess
    # your image without saving it to .png.
    img = PIL.Image.fromarray(img_arr)
    img.save(f'max_32_set.tfrecords.{str(i).zfill(5)}.png')

First image from dataset:

Below is code for drawing number of images per each label. Labels inside max_32_set.tfrecords file are represented as integers (not string names), probably names of labels are located in separate small file with meta information about dataset.

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 
import tensorflow as tf, numpy as np, matplotlib.pyplot as plt

raw_dataset = tf.data.TFRecordDataset('max_32_set.tfrecords')

labels_cnts = {}
for i, raw_record in enumerate(raw_dataset.as_numpy_iterator()):
    example = tf.train.Example()
    example.ParseFromString(raw_record)
    info = {}
    for k, v in example.features.feature.items():
        if k == 'label':
            info[k] = v.int64_list.value[0]
    labels_cnts[info['label']] = labels_cnts.get(info['label'], 0) + 1

x, y = zip(*sorted(labels_cnts.items(), key = lambda e: e[0]))
plt.xlabel('label')
plt.ylabel('num images')
plt.plot(x, y)
plt.xticks(x)
plt.show()

Plot for max_32_set.tfrecords:

Next code does augmentation using gaussian noise and gaussian blur, augmented tfrecord dataset is saved to max_32_set.augmented.tfrecords file:

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 
import tensorflow as tf, tensorflow_addons as tfa, PIL.Image, numpy as np, math

c_inp_fname = 'max_32_set.tfrecords'
c_out_fname = 'max_32_set.augmented.tfrecords'
c_augment_types = ('noise', 'blur', 'noise_blur', 'noise_blur_mirror')
c_res_class_size = None # If None then auto configured to maximal class size

def calc_labels():
    raw_dataset = tf.data.TFRecordDataset(c_inp_fname)
    cnts, labels = {}, []
    for i, raw_record in enumerate(raw_dataset):
        example = tf.train.Example()
        example.ParseFromString(raw_record.numpy())
        label = example.features.feature['label'].int64_list.value[0]
        cnts[label] = cnts.get(label, 0) + 1
        labels.append(label)
    return cnts, labels

def img_gen():
    raw_dataset = tf.data.TFRecordDataset(c_inp_fname)
    for i, raw_record in enumerate(raw_dataset):
        example = tf.train.Example()
        example.ParseFromString(raw_record.numpy())
        info = {}
        for k, v in example.features.feature.items():
            if k == 'image_raw':
                info[k] = v.bytes_list.value[0]
            elif k in ['depth', 'height', 'width']:
                info[k] = v.int64_list.value[0]
        img_arr = np.frombuffer(info['image_raw'], dtype = np.uint8).reshape(
            info['height'], info['width'], info['depth']
        )
        yield example, img_arr
        
def gaussian_noise(inp, stddev):
    noise = tf.random.normal(shape = tf.shape(inp), mean = 0.0, stddev = stddev, dtype = inp.dtype)
    return inp + noise
        
def augment(a, cnt):
    min_noise_stddev, max_noise_stddev = 5., 20.
    blur_kern, min_blur_stddev, max_blur_stddev = 3, 1., 5.
    
    assert cnt >= 1
    pad_a = lambda x: np.pad(x, (
        (0, 2 ** math.ceil(math.log(x.shape[0]) / math.log(2)) - x.shape[0]),
        (0, 2 ** math.ceil(math.log(x.shape[1]) / math.log(2)) - x.shape[1]),
        (0, 0)), constant_values = 0)
    post_a = lambda x: np.clip(x[:a.shape[0], :a.shape[1]], 0, 255).astype(np.uint8)
    yield 'orig', a
    cnt -= 1
    res = []
    fcnt = math.ceil(cnt / len(c_augment_types))
    linsp = lambda l, r, c: [(l + (i + 1) * (r - l) / (c + 1)) for i in range(c)]
    for noise_stddev, blur_stddev in zip(linsp(min_noise_stddev, max_noise_stddev, fcnt), linsp(min_blur_stddev, max_blur_stddev, fcnt)):
        if 'noise' in c_augment_types:
            #yield 'noise', post_a(tf.keras.layers.GaussianNoise(stddev = noise_stddev)(prep_a, training = True).numpy())
            res.append(('noise', post_a(gaussian_noise(a.astype(np.float32), stddev = noise_stddev).numpy())))
        if 'blur' in c_augment_types:
            res.append(('blur', post_a(tfa.image.gaussian_filter2d(pad_a(a).astype(np.float32), filter_shape = blur_kern, sigma = blur_stddev).numpy())))
        if 'noise_blur' in c_augment_types or 'noise_blur_mirror' in c_augment_types:
            nbr = post_a(tfa.image.gaussian_filter2d(
                pad_a(gaussian_noise(a.astype(np.float32), stddev = noise_stddev).numpy()),
                filter_shape = blur_kern, sigma = blur_stddev).numpy())
            if 'noise_blur' in c_augment_types:
                res.append(('noise_blur', nbr))
            if 'noise_blur_mirror' in c_augment_types:
                res.append(('noise_blur_mirror', tf.image.flip_left_right(nbr).numpy().astype(np.uint8)))
    assert cnt <= len(res) <= cnt + len(c_augment_types), (cnt, len(res), len(c_augment_types))
    yield from res[:cnt]

def process():
    labels_cnts, labels = calc_labels()
    max_class_size = max(labels_cnts.values())
    if c_res_class_size is not None:
        assert max_class_size <= c_res_class_size, f'Maximal class size is {max_class_size}, while requested res class size is smaller, {c_res_class_size}!'
        class_size = c_res_class_size
    else:
        class_size = max_class_size
    cur_labels_cnts = {}
    for iimg, (proto, imga) in enumerate(img_gen()):
        label = proto.features.feature['label'].int64_list.value[0]
        cur_labels_cnts[label] = cur_labels_cnts.get(label, 0) + 1
        need_cnt = class_size // labels_cnts[label] + int(cur_labels_cnts[label] <= class_size % labels_cnts[label])
        for iaug, (taug, aug) in enumerate(augment(imga, need_cnt)):
            #PIL.Image.fromarray(aug).save(f'max_32_set.tfrecords.aug.{str(iimg).zfill(5)}.{iaug}_{taug}.png')
            protoc = type(proto)()
            protoc.ParseFromString(proto.SerializeToString())
            protoc.features.feature['image_raw'].bytes_list.value[0] = aug.tobytes()
            yield protoc.SerializeToString()
        if (iimg % 10) == 0:
            print(iimg, ' ', sep = '', end = '', flush = True)
            
def main():
    assert tf.executing_eagerly()
    tf.data.experimental.TFRecordWriter(c_out_fname).write(
        tf.data.TFRecordDataset.from_generator(process, tf.string)
    )

main()

Example augmented images:

Original:

Noised:

Blurred:

Noised-blurred:

Noised-blurred-mirrored:

Number of images per label after augmentation (exactly balanced 30 images per label):


Same augmentation as above but for the case of input and output folders with labeled images, instead of TFRecordDataset, change c_inp_dir and c_out_dir to your folders paths:

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 
import tensorflow as tf, tensorflow_addons as tfa, PIL.Image, numpy as np, math, matplotlib.pyplot as plt

c_inp_dir = './images/'
c_out_dir = './images_out/'
c_augment_types = ('noise', 'blur', 'noise_blur', 'noise_blur_mirror')
c_res_class_size = None # If None then auto configured to maximal class size

def calc_labels(dirn = None):
    if dirn is None:
        dirn = c_inp_dir
    cnts, labels = {}, []
    for label in sorted(os.listdir(f'{dirn}')):
        label = int(label)
        labels.append(label)
        cnts[label] = len(os.listdir(f'{dirn}/{label}/'))
    return cnts, labels

def img_gen():
    cnts = {}
    for label in sorted(os.listdir(c_inp_dir)):
        label = int(label)
        for fname in sorted(os.listdir(f'{c_inp_dir}/{label}/')):
            img_arr = np.array(PIL.Image.open(f'{c_inp_dir}/{label}/{fname}'))
            yield label, img_arr, fname
        
def gaussian_noise(inp, stddev):
    noise = tf.random.normal(shape = tf.shape(inp), mean = 0.0, stddev = stddev, dtype = inp.dtype)
    return inp + noise
        
def augment(a, cnt):
    min_noise_stddev, max_noise_stddev = 5., 20.
    blur_kern, min_blur_stddev, max_blur_stddev = 3, 1., 5.
    
    assert cnt >= 1
    pad_a = lambda x: np.pad(x, (
        (0, 2 ** math.ceil(math.log(x.shape[0]) / math.log(2)) - x.shape[0]),
        (0, 2 ** math.ceil(math.log(x.shape[1]) / math.log(2)) - x.shape[1]),
        (0, 0)), constant_values = 0)
    post_a = lambda x: np.clip(x[:a.shape[0], :a.shape[1]], 0, 255).astype(np.uint8)
    yield 'orig', a
    cnt -= 1
    res = []
    fcnt = math.ceil(cnt / len(c_augment_types))
    linsp = lambda l, r, c: [(l + (i + 1) * (r - l) / (c + 1)) for i in range(c)]
    for noise_stddev, blur_stddev in zip(linsp(min_noise_stddev, max_noise_stddev, fcnt), linsp(min_blur_stddev, max_blur_stddev, fcnt)):
        if 'noise' in c_augment_types:
            #yield 'noise', post_a(tf.keras.layers.GaussianNoise(stddev = noise_stddev)(prep_a, training = True).numpy())
            res.append(('noise', post_a(gaussian_noise(a.astype(np.float32), stddev = noise_stddev).numpy())))
        if 'blur' in c_augment_types:
            res.append(('blur', post_a(tfa.image.gaussian_filter2d(pad_a(a).astype(np.float32), filter_shape = blur_kern, sigma = blur_stddev).numpy())))
        if 'noise_blur' in c_augment_types or 'noise_blur_mirror' in c_augment_types:
            nbr = post_a(tfa.image.gaussian_filter2d(
                pad_a(gaussian_noise(a.astype(np.float32), stddev = noise_stddev).numpy()),
                filter_shape = blur_kern, sigma = blur_stddev).numpy())
            if 'noise_blur' in c_augment_types:
                res.append(('noise_blur', nbr))
            if 'noise_blur_mirror' in c_augment_types:
                res.append(('noise_blur_mirror', tf.image.flip_left_right(nbr).numpy().astype(np.uint8)))
    assert cnt <= len(res) <= cnt + len(c_augment_types), (cnt, len(res), len(c_augment_types))
    yield from res[:cnt]

def process():
    labels_cnts, labels = calc_labels()
    max_class_size = max(labels_cnts.values())
    if c_res_class_size is not None:
        assert max_class_size <= c_res_class_size, f'Maximal class size is {max_class_size}, while requested res class size is smaller, {c_res_class_size}!'
        class_size = c_res_class_size
    else:
        class_size = max_class_size
    
    cur_labels_cnts = {}
    for iimg, (label, imga, fname) in enumerate(img_gen()):
        os.makedirs(f'{c_out_dir}/{label}/', exist_ok = True)
        cur_labels_cnts[label] = cur_labels_cnts.get(label, 0) + 1
        need_cnt = class_size // labels_cnts[label] + int(cur_labels_cnts[label] <= class_size % labels_cnts[label])
        for iaug, (taug, aug) in enumerate(augment(imga, need_cnt)):
            PIL.Image.fromarray(aug).save(f'{c_out_dir}/{label}/{fname}.{iaug}_{taug}.png')
        if (iimg % 10) == 0:
            print(iimg, ' ', sep = '', end = '', flush = True)
            
def plot_cnts(dirn):
    labels_cnts = calc_labels(dirn)[0]
    x, y = zip(*sorted(labels_cnts.items(), key = lambda e: e[0]))
    plt.xlabel('label')
    plt.ylabel('num images')
    plt.plot(x, y)
    plt.xticks(x)
    plt.show()
            
def main():
    process()
    plot_cnts(c_inp_dir)
    plot_cnts(c_out_dir)

main()

这篇关于如何读取(解码).tfrecords文件,查看内部图像并进行扩充?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆