Python 数据扩充完成后会发生什么?
我使用了Kaggle的“狗与猫”,并遵循TensorFlow的cifar-10教程(我没有为了方便而使用体重衰减、移动平均和L2损耗),我成功地训练了我的网络,但当我在代码中添加数据增强部分时,奇怪的事情发生了,即使经过上千步,损耗也从未下降(添加前,一切正常)。代码如下:Python 数据扩充完成后会发生什么?,python,machine-learning,tensorflow,deep-learning,Python,Machine Learning,Tensorflow,Deep Learning,我使用了Kaggle的“狗与猫”,并遵循TensorFlow的cifar-10教程(我没有为了方便而使用体重衰减、移动平均和L2损耗),我成功地训练了我的网络,但当我在代码中添加数据增强部分时,奇怪的事情发生了,即使经过上千步,损耗也从未下降(添加前,一切正常)。代码如下: def get_batch(image, label, image_w, image_h, batch_size, capacity, test_flag=False): ''' Args: image:
def get_batch(image, label, image_w, image_h, batch_size, capacity, test_flag=False):
'''
Args:
image: list type
label: list type
image_w: image width
image_h: image height
batch_size: batch size
capacity: the maximum elements in queue
test_flag: create training batch or test batch
Returns:
image_batch: 4D tensor [batch_size, width, height, 3], dtype=tf.float32
label_batch: 1D tensor [batch_size], dtype=tf.int32
'''
image = tf.cast(image, tf.string)
label = tf.cast(label, tf.int32)
# make an input queue
input_queue = tf.train.slice_input_producer([image, label])
label = input_queue[1]
image_contents = tf.read_file(input_queue[0])
image = tf.image.decode_jpeg(image_contents, channels=3)
####################################################################
# Data argumentation should go to here
# but when we want to do test, stay the images what they are
if not test_flag:
image = tf.image.resize_image_with_crop_or_pad(image, RESIZED_IMG, RESIZED_IMG)
# Randomly crop a [height, width] section of the image.
distorted_image = tf.random_crop(image, [image_w, image_h, 3])
# Randomly flip the image horizontally.
distorted_image = tf.image.random_flip_left_right(distorted_image)
# Because these operations are not commutative, consider randomizing
# the order their operation.
# NOTE: since per_image_standardization zeros the mean and makes
# the stddev unit, this likely has no effect see tensorflow#1458.
distorted_image = tf.image.random_brightness(distorted_image, max_delta=63)
image = tf.image.random_contrast(distorted_image, lower=0.2, upper=1.8)
else:
image = tf.image.resize_image_with_crop_or_pad(image, image_w, image_h)
######################################################################
# Subtract off the mean and divide by the variance of the pixels.
image = tf.image.per_image_standardization(image)
# Set the shapes of tensors.
image.set_shape([image_h, image_w, 3])
# label.set_shape([1])
image_batch, label_batch = tf.train.batch([image, label],
batch_size=batch_size,
num_threads=64,
capacity=capacity)
label_batch = tf.reshape(label_batch, [batch_size])
image_batch = tf.cast(image_batch, tf.float32)
return image_batch, label_batch
请确保您使用的限制(例如,亮度限制为
max_delta=63
,对比度限制为upper=1.8
)足够低,以便图像仍然可以识别。另一个问题可能是,一次又一次地应用了增强,因此经过几次迭代后,它完全失真(尽管我没有在您的代码片段中发现此错误)
我建议您做的是将数据可视化添加到中。要可视化图像,请使用方法。您将能够清楚地看到增强的结果
tf.summary.image('input', image_batch, 10)
可以作为一个例子。确保您使用的限制(例如,亮度的限制
max_delta=63
,对比度的限制upper=1.8
)足够低,因此图像仍然可以识别。另一个问题可能是,不断地应用增强,因此经过几次迭代后,图像会完全失真(尽管我在你的代码片段中没有发现这个bug)
我建议您做的是将数据可视化添加到中。要可视化图像,请使用方法。您将能够清楚地看到增强的结果
tf.summary.image('input', image_batch, 10)
可以作为示例。TensorFlow的cifar-10教程的链接TensorFlow的cifar-10教程的链接