针对 tf.image.decode_jpeg 和 tf.train.shuffle_batch 归一化的图像像素值?
Image pixel value normalized for tf.image.decode_jpeg and tf.train.shuffle_batch?
我正在尝试使用 tensorflow 的 tf.train.shuffle_batch 函数,然后我需要先使用 tf.image.decode_jpeg 加载图像(或其他类似的函数来加载 png 和 jpg)。但我刚刚发现图像是作为概率图加载的,这意味着像素值的最大值为 1,像素值的最小值为 0。下面是我从 github 更新的代码回购。不知道为什么像素的值要归一化为[0,1],也没找到tensorflow的相关文档。谁能帮帮我?谢谢。
def load_examples(self, input_dir, flip, scale_size, batch_size, min_queue_examples):
input_paths = get_image_paths(input_dir)
with tf.name_scope("load_images"):
path_queue = tf.train.string_input_producer(input_paths)
reader = tf.WholeFileReader()
paths, contents = reader.read(path_queue)
# note this is important for truncated images
raw_input = tf.image.decode_jpeg(contents,try_recover_truncated = True, acceptable_fraction=0.5)
raw_input = tf.image.convert_image_dtype(raw_input, dtype=tf.float32)
raw_input.set_shape([None, None, 3])
# break apart image pair and move to range [-1, 1]
width = tf.shape(raw_input)[1] # [height, width, channels]
a_images = preprocess(raw_input[:, :width // 2, :])
b_images = raw_input[:, width // 2:, :]
inputs, targets = [a_images, b_images]
def transform(image):
r = image
r = tf.image.resize_images(r, [self.image_height, self.image_width], method=tf.image.ResizeMethod.AREA)
return r
def transform_gaze(image):
r = image
r = tf.image.resize_images(r, [self.gaze_height, self.gaze_width], method=tf.image.ResizeMethod.AREA)
return r
with tf.name_scope("input_images"):
input_images = transform(inputs)
with tf.name_scope("target_images"):
target_images = transform(targets)
total_image_count = len(input_paths)
# target_images = tf.image.per_image_standardization(target_images)
target_images = target_images[:,:,0]
target_images = tf.expand_dims(target_images, 2)
inputs_batch, targets_batch = tf.train.shuffle_batch([input_images, target_images],
batch_size=batch_size,
num_threads=1,
capacity=min_queue_examples + 3 * batch_size,
min_after_dequeue=min_queue_examples)
# inputs_batch, targets_batch = tf.train.batch([input_images, target_images],batch_size=batch_size)
return inputs_batch, targets_batch, total_image_count
值在 [0,1] 中,因为 tf.image.decode_*
方法就是这样做的。
一般来说,当一个方法 returns 一个浮点张量时,它的值应该在 [0,1] 范围内,而如果返回的张量是一个 uint8 值应该在 [0,1] 范围内[0,255] 范围。
此外,当您使用 tf.image.convert_image_dtype
方法转换输入图像的 dtype 时,您正在应用该转换规则。
如果您的输入图像是 uint8 图像并且您将其转换为 float32,则值在 [0,1] 范围内缩放。如果您的图像已经是浮点数,则它的值应该在该范围内并且什么都不做。
我正在尝试使用 tensorflow 的 tf.train.shuffle_batch 函数,然后我需要先使用 tf.image.decode_jpeg 加载图像(或其他类似的函数来加载 png 和 jpg)。但我刚刚发现图像是作为概率图加载的,这意味着像素值的最大值为 1,像素值的最小值为 0。下面是我从 github 更新的代码回购。不知道为什么像素的值要归一化为[0,1],也没找到tensorflow的相关文档。谁能帮帮我?谢谢。
def load_examples(self, input_dir, flip, scale_size, batch_size, min_queue_examples):
input_paths = get_image_paths(input_dir)
with tf.name_scope("load_images"):
path_queue = tf.train.string_input_producer(input_paths)
reader = tf.WholeFileReader()
paths, contents = reader.read(path_queue)
# note this is important for truncated images
raw_input = tf.image.decode_jpeg(contents,try_recover_truncated = True, acceptable_fraction=0.5)
raw_input = tf.image.convert_image_dtype(raw_input, dtype=tf.float32)
raw_input.set_shape([None, None, 3])
# break apart image pair and move to range [-1, 1]
width = tf.shape(raw_input)[1] # [height, width, channels]
a_images = preprocess(raw_input[:, :width // 2, :])
b_images = raw_input[:, width // 2:, :]
inputs, targets = [a_images, b_images]
def transform(image):
r = image
r = tf.image.resize_images(r, [self.image_height, self.image_width], method=tf.image.ResizeMethod.AREA)
return r
def transform_gaze(image):
r = image
r = tf.image.resize_images(r, [self.gaze_height, self.gaze_width], method=tf.image.ResizeMethod.AREA)
return r
with tf.name_scope("input_images"):
input_images = transform(inputs)
with tf.name_scope("target_images"):
target_images = transform(targets)
total_image_count = len(input_paths)
# target_images = tf.image.per_image_standardization(target_images)
target_images = target_images[:,:,0]
target_images = tf.expand_dims(target_images, 2)
inputs_batch, targets_batch = tf.train.shuffle_batch([input_images, target_images],
batch_size=batch_size,
num_threads=1,
capacity=min_queue_examples + 3 * batch_size,
min_after_dequeue=min_queue_examples)
# inputs_batch, targets_batch = tf.train.batch([input_images, target_images],batch_size=batch_size)
return inputs_batch, targets_batch, total_image_count
值在 [0,1] 中,因为 tf.image.decode_*
方法就是这样做的。
一般来说,当一个方法 returns 一个浮点张量时,它的值应该在 [0,1] 范围内,而如果返回的张量是一个 uint8 值应该在 [0,1] 范围内[0,255] 范围。
此外,当您使用 tf.image.convert_image_dtype
方法转换输入图像的 dtype 时,您正在应用该转换规则。
如果您的输入图像是 uint8 图像并且您将其转换为 float32,则值在 [0,1] 范围内缩放。如果您的图像已经是浮点数,则它的值应该在该范围内并且什么都不做。