Skip to content

Commit

Permalink
improve simple image loader
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardyehuang committed Feb 15, 2022
1 parent dbc8521 commit 71a06b8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 23 deletions.
2 changes: 1 addition & 1 deletion utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
# ================================================================

from iseg.utils.sugars import *
from iseg.utils.common import resize_image, simple_load_image
from iseg.utils.common import resize_image
23 changes: 1 addition & 22 deletions utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,25 +73,4 @@ def down_size_image_by_scale(images, scale, method=None, name=None):
target_height = image_height // scale + is_height_odd
target_width = image_width // scale + is_width_odd

return resize_image(images=images, size=(target_height, target_width), method=method, name=name)


def simple_load_image(image_path, min_size_unit=1):

image_tensor = tf.image.decode_jpeg(tf.io.read_file(image_path), channels=3)
image_tensor = tf.expand_dims(tf.cast(image_tensor, tf.float32), axis=0) # [1, H, W, 3]

image_size = tf.shape(image_tensor)[1:3]

pad_height = tf.cast(tf.math.ceil(image_size[0] / 32) * 32, tf.int32)
pad_width = tf.cast(tf.math.ceil(image_size[1] / 32) * 32, tf.int32)

pad_height = pad_height if pad_height % 2 != 0 else pad_height + 1
pad_width = pad_height if pad_width % 2 != 0 else pad_width + 1

from iseg.data_process.utils import pad_to_bounding_box, normalize_value_range

pad_image_tensor = pad_to_bounding_box(image_tensor, 0, 0, pad_height, pad_width, pad_value=[127.5, 127.5, 127.5])
pad_image_tensor = normalize_value_range(pad_image_tensor)

return pad_image_tensor, image_size
return resize_image(images=images, size=(target_height, target_width), method=method, name=name)
24 changes: 24 additions & 0 deletions utils/data_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from PIL import Image

from iseg.data_process.augments.pad_to_odd_augment import pad_to_odd


def load_label_to_tensor(label_path):
label_tensor = tf.py_function(__load_label_to_tensor_internel, [label_path], tf.int32)
Expand Down Expand Up @@ -33,3 +35,25 @@ def load_image_tensor_from_path(image_path, label_path=None):
label_tensor = load_label_to_tensor(label_path)

return image_tensor, label_tensor


def simple_load_image(image_path):

image_tensor, _ = load_image_tensor_from_path(image_path)
image_tensor = tf.expand_dims(tf.cast(image_tensor, tf.float32), axis=0) # [1, H, W, 3]

image_size = tf.shape(image_tensor)[1:3]

pad_height = tf.cast(tf.math.ceil(image_size[0] / 32) * 32, tf.int32)
pad_width = tf.cast(tf.math.ceil(image_size[1] / 32) * 32, tf.int32)

pad_height = pad_height if pad_height % 2 != 0 else pad_height + 1
pad_width = pad_height if pad_width % 2 != 0 else pad_width + 1

from iseg.data_process.utils import pad_to_bounding_box, normalize_value_range

pad_image_tensor = pad_to_bounding_box(image_tensor, 0, 0, pad_height, pad_width, pad_value=[127.5, 127.5, 127.5])
pad_image_tensor = normalize_value_range(pad_image_tensor)

return pad_image_tensor, image_size

0 comments on commit 71a06b8

Please sign in to comment.