我正在尝试做不同种类的(图像)数据增强来训练我的神经网络。在
我知道tf.图像提供了一些增强功能,但它们太简单了-例如,我只能将图像旋转90度,而不是任何角度。在
我也知道tf.keras.预处理图像提供随机旋转、随机剪切、随机移动和随机缩放。但是这些方法只能应用于numpy数组,而不能应用于张量。在
我知道我可以先读图像,使用tf.keras.预处理.image进行增强,然后将这些增强的numpy数组转换为张量。在
但是,我只是想知道是否有一种方法可以实现张量方面的增强,这样我就不必费心于“图像文件->张量->numpy数组->张量”过程。在
为那些想知道如何应用转换的人更新:
对于详细的源代码,您可能需要检查tf.contrib.image.transform和{a2}。在
这是我的代码:
def transformImg(imgIn,forward_transform):
t = tf.contrib.image.matrices_to_flat_transforms(tf.linalg.inv(forward_transform))
# please notice that forward_transform must be a float matrix,
# e.g. [[2.0,0,0],[0,1.0,0],[0,0,1]] will work
# but [[2,0,0],[0,1,0],[0,0,1]] will not
imgOut = tf.contrib.image.transform(imgIn, t, interpolation="BILINEAR",name=None)
return imgOut
基本上,上面的代码
例如,与x轴平行的shear transform是
因此,我们可以这样实现剪切变换(使用上面定义的transformImg()
):
(请注意,img
是张量,不包括将张量转换为图像文件的代码。)
p.S.
以上代码适用于tensorflow 1.10.1,可能不适用于将来的版本。
老实说,我真的不知道他们为什么要设计tf.contrib.image文件。以我们必须使用其他函数的方式进行转换(利纳格投资基金)为了得到我们想要的。我真的希望他们能改变tf.contrib.image文件。转换为在a more intuitive way中工作。
看看^{} 。它允许对图像应用一般的投影变换。在
您还需要查看^{} ,将仿射矩阵转换为
tf.contrib.image.transform
接受的投影格式。在我通常使用}和{}。
tf.data.Dataset
和{Dataset.prefetch
意味着通常没有时间开销(只要CPU上的预处理比在GPU上运行网络花费的时间少)。如果你是跨多个GPU操作,你可能需要重新考虑,但下面的工作对我在单GPU系统上很好。在为了简单起见,我假设您的所有图像都在磁盘上的不同文件中,尽管它可以很容易地适应zip存档或其他格式,如hdf5(不适用于
.tar
文件-不知道为什么,但我怀疑这是否是个好主意)您也可以在tensorflow中进行解码,并直接在
py_func
中使用any_cv2_or_numpy_augmentations
(尽管您没有避免您在问题中提到的张量->;numpy->;张量舞蹈)。我怀疑你会注意到性能的不同。在有关更多选项,请查看this answer。在
相关问题 更多 >
编程相关推荐