作者:三八依依2010 | 来源:互联网 | 2023-02-04 10:44
我正在使用一组32x32x32灰度图像,我想在图像上应用随机旋转作为数据增强的一部分,同时通过tflearn + tensorflow训练CNN.我使用以下代码来执行此操作:
# Real-time data preprocessing
img_prep = ImagePreprocessing()
img_prep.add_featurewise_zero_center()
img_prep.add_featurewise_stdnorm()
# Real-time data augmentation
img_aug = ImageAugmentation()
img_aug.add_random_rotation(max_angle=360.)
# Input data
with tf.name_scope('Input'):
X = tf.placeholder(tf.float32, shape=(None, image_size,
image_size, image_size, num_channels), name='x-input')
Y = tf.placeholder(tf.float32, shape=(None, label_cnt), name='y-input')
# Convolutional network building
network = input_data(shape=[None, 32, 32, 32, 1],
placeholder = X,
data_preprocessing=img_prep,
data_augmentation=img_aug)
(我正在使用tensorflow和tflearn的组合来使用两者的功能,所以请耐心等待.如果我使用占位符等方式出现问题,请告诉我.)
我发现使用add_random_rotation(它本身使用scipy.ndimage.interpolation.rotate)将我的灰度图像的第三维视为通道(如RGB通道),并通过围绕z轴的随机角度旋转第三维的所有32个图像(将我的3D图像视为具有32个通道的2D图像).但我希望图像在空间中旋转(围绕所有三个轴).你知道我怎么能这样做吗?是否有用于在空间中轻松旋转3D图像的功能或包装?!
1> Ary..:
def random_rotation_3d(batch, max_angle):
""" Randomly rotate an image by a random angle (-max_angle, max_angle).
Arguments:
max_angle: `float`. The maximum rotation angle.
Returns:
batch of rotated 3D images
"""
size = batch.shape
batch = np.squeeze(batch)
batch_rot = np.zeros(batch.shape)
for i in range(batch.shape[0]):
if bool(random.getrandbits(1)):
image1 = np.squeeze(batch[i])
# rotate along z-axis
angle = random.uniform(-max_angle, max_angle)
image2 = scipy.ndimage.interpolation.rotate(image1, angle, mode='nearest', axes=(0, 1), reshape=False)
# rotate along y-axis
angle = random.uniform(-max_angle, max_angle)
image3 = scipy.ndimage.interpolation.rotate(image2, angle, mode='nearest', axes=(0, 2), reshape=False)
# rotate along x-axis
angle = random.uniform(-max_angle, max_angle)
batch_rot[i] = scipy.ndimage.interpolation.rotate(image3, angle, mode='nearest', axes=(1, 2), reshape=False)
# print(i)
else:
batch_rot[i] = batch[i]
return batch_rot.reshape(size)