通过张量流旋转创建增强训练数据

Creating augmented training data by tensorflow rotation

最近开始使用 tensorflowcnn,我希望训练一个简单的网络来向上旋转特征。

我有一个 1k 的向上图像数据集,使用 tensorflow.contrib.image.rotate 我想以随机角度旋转它们。 RotNet 行内的内容,但使用 tensorflow 而不是 keras

想法是从每个 1k 图像数据集中创建 N 个旋转训练示例。每个图像的形状都是 30x30x1(黑白)。

with tf.Session() as sess:
    for curr in range(oriented_data.shape[0]):
        curr_image = loaded_oriented_data[curr]
        for i in range(augment_each_image):
            rotation_angle = np.random.randint(360)
            rotated_image = tfci.rotate(curr_image, np.float(rotation_angle) * math.pi/180.)
            training_data[curr + i] = sess.run(rotated_image)
            labels[curr + i] = rotation_angle

现在的问题是 sess.run(rotated_image) 行需要很长时间才能执行。例如,为 1k 中的每一个仅创建 5 个示例已经 运行 超过 30 分钟(在 cpu)。
如果我简单地删除该行,图像会在一分钟内生成。

我想有一种方法可以将数据作为张量存储和使用,而不是像我目前所做的那样将它们转换回 ndarrays,或者是否有更快的函数来评估张量?

问题是您正在为 augment_each_image 中的每个图像创建一个旋转运算符,从而产生一个可能非常大的网络。

解决方案是创建一个单个 旋转操作,您可以连续将其应用于图像。类似的东西:

im_ph = tf.placeholder(...)
ang_ph = tf.placeholder(...)
rot_op = tfci.rotate(im_ph, ang_ph)

with tf.Session() as sess:
  for curr in range(oriented_data.shape[0]):
    curr_image = loaded_oriented_data[curr]
      for i in range(augment_each_image):
        rotation_angle = np.random.randint(360)
        rotated_image = sess.run(rot_op, {im_ph: curr_image, ang_ph: np.float(rotation_angle) * math.pi/180.})
        training_data[curr + i] = rotated_image
        labels[curr + i] = rotation_angle