def read_and_batchify_image(image_path, shape, image_type="jpg"):
"""Return the original image as read from image_path and the image splitted as a batch tensor.
Args:
image_path: image path
shape: batch shape, like: [no_patches_per_side**2, patch_side, patch_side, 3]
image_type: image type
Returns:
original_image, patches
where original image is a tensor in the format [widht, height 3]
and patches is a tensor of processed images, ready to be classified, with size
[batch_size, w, h, 3]"""
original_image = read_image(image_path, 3, image_type)
# extract values from shape
patch_side = shape[1]
no_patches_per_side = int(math.sqrt(shape[0]))
resized_input_side = patch_side * no_patches_per_side
resized_image = resize_bl(original_image, resized_input_side)
resized_image = tf.expand_dims(resized_image, 0)
patches = tf.space_to_depth(resized_image, patch_side)
print(patches)
patches = tf.squeeze(patches, [0]) #4,4,192*192*3
print(patches)
patches = tf.reshape(patches,
[no_patches_per_side**2, patch_side, patch_side, 3])
print(patches)
patches_a = tf.split(0, no_patches_per_side**2, patches)
print(patches_a)
normalized_patches = []
for patch in patches_a:
patch_as_input_image = zm_mp(
tf.reshape(tf.squeeze(patch, [0]), [patch_side, patch_side, 3]))
print(patch_as_input_image)
normalized_patches.append(patch_as_input_image)
# the last patch is not a "patch" but the whole image resized to patch_side² x 3
# to give a glance to the whole image, in parallel with the patch analysis
normalized_patches.append(zm_mp(resize_bl(original_image, patch_side)))
batch_of_patches = tf.pack(normalized_patches)
return tf.image.convert_image_dtype(original_image,
tf.uint8), batch_of_patches
评论列表
文章目录