[1] [Handwritten Chinese Font Generation with Collaborative Stroke Refinement]之数据增长实现

[1] [Handwritten Chinese Font Generation with Collaborative Stroke Refinement]之数据增长实现

1.原理

《Handwritten Chinese Font Generation with Collaborative Stroke Refinement》arxiv.org/abs/1904.13268

文中网络架构图如下:
在这里插入图片描述
图中的online zoom-augmentation是实现网络训练时,对输入数据进行水平垂直方向上的transform
在这里插入图片描述

2.编码实现

def hv_transform(self, image):
        '''
        '''
        orig_shp = image.shape

        image = tf.cast(image, tf.float32)

        rd = tf.random.uniform([])
        less_than_025 = tf.less(rd, tf.constant(0.25))
        less_than_050 = tf.less(rd, tf.constant(0.50))
        less_than_075 = tf.less(rd, tf.constant(0.75))

        def func_less_than_025():
            x = tf.image.resize_images(image, [orig_shp[0]//2, orig_shp[1]])

            h_off = tf.random.uniform([1], minval=0, maxval=orig_shp[0] - orig_shp[0]//2, dtype=tf.int32)
            x     = self.pad_to_bounding_box(
                                    x,
                                    offset_height  = h_off, 
                                    offset_width   = tf.constant([0], dtype=tf.int32), 
                                    target_height  = orig_shp[0], 
                                    target_width   = orig_shp[1])
            return x

        def func_less_than_050():
            x = tf.image.resize_images(image, [orig_shp[0], orig_shp[1]//2])
            
            w_off = tf.random.uniform([1], minval=0, maxval=orig_shp[1] - orig_shp[1]//2, dtype=tf.int32)
            x     = self.pad_to_bounding_box(
                                    x,
                                    offset_height  = tf.constant([0], dtype=tf.int32),
                                    offset_width   = w_off,
                                    target_height  = orig_shp[0], 
                                    target_width   = orig_shp[1])
            return x

        def func_less_than_075():
            x = tf.image.resize_images(image, [orig_shp[0]//2, orig_shp[1]//2])
            
            h_off = tf.random.uniform([1], minval=0, maxval=orig_shp[0] - orig_shp[0]//2, dtype=tf.int32)
            w_off = tf.random.uniform([1], minval=0, maxval=orig_shp[1] - orig_shp[1]//2, dtype=tf.int32)
            x     = self.pad_to_bounding_box(
                                    x,
                                    offset_height  = h_off,
                                    offset_width   = w_off,
                                    target_height  = orig_shp[0], 
                                    target_width   = orig_shp[1])
            return x

        def func_less_than_100():
            return image

        x = tf.case({less_than_025: func_less_than_025, less_than_050: func_less_than_050, less_than_075: func_less_than_075},
                default=func_less_than_100, exclusive=False)
        return x
  • 上面的代码实现是基于TensorFlow 1.13.1
  • TensorFlow编码时,通过random实现随机参数值的数据增长,注意通过tf.reshape()告诉TF当前数据的shape,否则会出现一些很难校正的错误
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章