mosaic数据增强_TensorPipe:高性能Tensorflow数据管道

作者:kartik4949

编译:ronghuaiyang

导读

给大家介绍一个非常好用的TensorFlow数据pipeline工具。

5d4399134e3510b1957985dd306c5acc.png

高性能的Tensorflow Data Pipeline,使用SOTA的增强和底层优化。

安装方法

pip install tensorflow-addons==0.11.2pip install tensorflow==2.2.0pip install sklearn

功能

  • [x] High Performance tf.data pipline
  • [x] Core tensorflow support for high performance
  • [x] Classification data support
  • [ ] Bbox data support
  • [ ] Keypoints data support
  • [ ] Segmentation data support
  • [x] GridMask in core tf2.x
  • [x] Mosiac Augmentation in core tf2.x
  • [x] CutOut in core tf2.x
  • [x] Flexible and easy configuration
  • [x] Gin-config support

高级用户部分

用例1,为训练创建数据Pipeline

from pipe import Funnel                                                         from bunch import Bunch                                                         """                                                                             Create a Funnel for the Pipeline!                                               """                                                                             # Config for Funnelconfig = {                                                                          "batch_size": 2,                                                                "image_size": [512,512],                                                        "transformations": {                                                                "flip_left_right": None,                                                        "gridmask": None,                                                               "random_rotate":None,                                                       },                                                                              "categorical_encoding":"labelencoder"                                       }                                                                               config = Bunch(config)                                                          pipeline = Funnel(data_path="testdata", config=config, datatype="categorical")  pipeline = pipeline.dataset(type="train")                                                                                                                       # Pipline ready to use, iter over it to use.# Custom loop example.for data in pipeline:    image_batch , label_batch = data[0], data[1]    # you can use _loss = loss(label_batch,model.predict(image_batch))    # calculate gradients on loss and optimize the model.    print(image_batch,label_batch)                                      

用例2,为验证创建数据Pipeline

from pipe import Funnel                                                         from bunch import Bunch                                                         """                                                                             Create a Funnel for the Pipeline!                                               """                                                                             # Config for Funnelconfig = {                                                                          "batch_size": 1,                                                                "image_size": [512,512],                                                        "transformations": {                                                                                                           },                                                                              "categorical_encoding":"labelencoder"                                       }                                                                               config = Bunch(config)                                                          pipeline = Funnel(data_path="testdata", config=config, datatype="categorical", training=False)  pipeline = pipeline.dataset(type="val")                                       # use pipeline to validate your data on model.loss = []for data in pipeline:    image_batch , actual_label_batch = data[0], data[1]    # pred_label_batch = model.predict(image_batch)    # loss.append(calc_loss(actual_label_batch,pred_label_batch))    print(image_batch,label_batch)                                     

初学者部分

Keras 兼容性,使用keras model.fit来构建非常简单的pipeline。

import tensorflow as tffrom pipe import Funnel"""Create a Funnel for the Pipeline!"""config = {    "batch_size": 2,    "image_size": [100, 100],    "transformations": {        "flip_left_right": None,        "gridmask": None,        "random_rotate": None,    },    "categorical_encoding": "labelencoder",}pipeline = Funnel(data_path="testdata", config=config, datatype="categorical")pipeline = pipeline.dataset(type="train")# Create Keras modelmodel = tf.keras.applications.VGG16(    include_top=True, weights=None,input_shape=(100,100,3),    pooling=None, classes=2, classifier_activation='sigmoid')# compilemodel.compile(loss='mse', optimizer='adam')# pass pipeline as iterablemodel.fit(pipeline , batch_size=2,steps_per_epoch=5,verbose=1)

配置

  • image_size - pipeline的图像尺寸。
  • batch_size - pipeline的Batch size。
  • transformations - 应用数据增强字典中的对应关键字。
  • categorical_encoding - 对类别数据进行编码 - ('labelencoder' , 'onehotencoder').

增强:

GridMask

在输入图像上创建gridmask,并在范围内定义旋转。

  • 参数:
    • ratio - 空间上的网格比例
    • fill - 填充值fill value
    • rotate - 旋转的角度范围

MixUp

使用给定的alpha值,将两个随机采样的图像和标签进行混合。

  • 参数:
    • alpha - 在混合时使用的值。

RandomErase

在给定的图像上的随机位置擦除一个随机的矩形区域。

  • 参数:
    • prob - 在图像上进行随机的概率。

CutMix

在给定图像上对另一个随机采样的图像进行随机的缩放,再以完全覆盖的方式贴到这个给定图像上。

  • params:
    • prob - 在图像上进行CutMix的概率。

Mosaic

把4张输入图像组成一张马赛克图像。

  • 参数:
    • prob - 进行Mosaic的概率。

CutMix , CutOut, MixUp

f89d767dd2cdf70e038b0823398be9e1.png

Mosaic

b16057b2bc0da8764d4e28cc2d4fbc4a.png

Grid Mask

974cfd9e853dc997dd841a7eb1e3c715.png

英文原文:https://github.com/kartik4949/TensorPipe

更多内容,请关注微信公众号“AI公园”。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值