Tensorflow高性能数据优化增强工具Pipeline使用详解

2022-11-02 20:58:17
目录
安装方法功能高级用户部分用例1,为训练创建数据Pipeline用例2,为验证创建数据Pipeline初学者部分Keras 兼容性配置增强:GridMaskMixUpRandomEraseCutMixMosaicCutMix , CutOut, MixUpMosaicGrid Mask

安装方法

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

高性能的Tensorflow>

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

功能

    High>Core tensorflow support for high performanceClassification data supportBbox data supportKeypoints data supportSegmentation data supportGridMask in core tf2.xMosiac Augmentation in core tf2.xCutOut in core tf2.xFlexible and easy configurationGin-config support

    高级用户部分

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

    from pipe import Funnel                                                         
    from bunch import Bunch                                                         
    """                                                                             
    Create a Funnel for the Pipeline!                                               
    """                                                                             
    # Config for Funnel
    config = {                                                                      
        "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 Funnel
    config = {                                                                      
        "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 tf
    from 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 model
    model = tf.keras.applications.VGG16(
        include_top=True, weights=None,input_shape=(100,100,3),
        pooling=None, classes=2, classifier_activation='sigmoid'
    )
    # compile
    model.compile(loss='mse', optimizer='adam')
    # pass pipeline as iterable
    model.fit(pipeline , batch_size=2,steps_per_epoch=5,verbose=1)
    

    配置

      image_size ->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 ->

      Mosaic

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

      参数:

      prob ->

      CutMix>

      Mosaic

      Grid>

      以上就是Tensorflow高性能数据优化增强工具Pipeline使用详解的详细内容,更多关于Tensorflow数据工具Pipeline的资料请关注易采站长站其它相关文章!