[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,否則會出現一些很難校正的錯誤