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

时间:2018-05-03 13:06:39

标签: python-3.x tensorflow conv-neural-network tensor

最近开始使用<div id="result"></div> <div id="result2"></div>tensorflow,我希望训练一个简单的网络向上旋转功能。

我有一个向上定向的1k图像数据集并使用cnn我喜欢用随机角度旋转它们。 RotNettensorflow.contrib.image.rotate代替tensorflow的内容。

我们的想法是从每个1k图像数据集创建keras个旋转训练样例。每个图像的形状为30x30x1(黑色和白色)。

N

现在的问题是,行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 需要很长时间才能执行。例如,每个1k只创建5个示例已运行超过30分钟(在cpu上) 如果我只是删除该行,则会在一分钟内生成图像。

我认为有一种方法可以将数据存储和处理为张量,而不是像我迄今为止所做的那样将它们转换回ndarrays,或者是否有更快的功能来评估张量?

1 个答案:

答案 0 :(得分:1)

问题是您正在为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