Skip to content

Commit

Permalink
Revert in_place exif_transpose, f16 loads
Browse files Browse the repository at this point in the history
in_place requires a fairly recent version of pillow and performing the
performance benefit of it is minimal.

Loading as float16 does break other nodes so the change is getting
reverted.

Resolves #226
  • Loading branch information
AustinMroz committed Jun 6, 2024
1 parent 1ab2027 commit aad82e9
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[project]
name = "comfyui-videohelpersuite"
description = "Nodes related to video workflows"
version = "1.1.0"
version = "1.1.1"
license = "LICENSE"
dependencies = ["opencv-python", "imageio-ffmpeg"]

Expand Down
10 changes: 5 additions & 5 deletions videohelpersuite/load_images_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def images_generator(directory: str, image_load_cap: int = 0, skip_first_images:
for image_path in dir_files:
i = Image.open(image_path)
#exif_transpose can only ever rotate, but rotating can swap width/height
ImageOps.exif_transpose(i, in_place=True)
i = ImageOps.exif_transpose(i)
has_alpha |= 'A' in i.getbands()
count = sizes.get(i.size, 0)
sizes[i.size] = count +1
Expand All @@ -59,9 +59,9 @@ def images_generator(directory: str, image_load_cap: int = 0, skip_first_images:
iformat = "RGBA" if has_alpha else "RGB"
def load_image(file_path):
i = Image.open(file_path)
ImageOps.exif_transpose(i, in_place=True)
i = ImageOps.exif_transpose(i)
i = i.convert(iformat)
i = np.array(i, dtype=np.float16)
i = np.array(i, dtype=np.float32)
#This nonsense provides a nearly 50% speedup on my system
torch.from_numpy(i).div_(255)
if i.shape[0] != size[1] or i.shape[1] != size[0]:
Expand Down Expand Up @@ -104,13 +104,13 @@ def load_images(directory: str, image_load_cap: int = 0, skip_first_images: int

if meta_batch is not None:
gen = itertools.islice(gen, meta_batch.frames_per_batch)
images = torch.from_numpy(np.fromiter(gen, np.dtype((np.float16, (height, width, 3 + has_alpha)))))
images = torch.from_numpy(np.fromiter(gen, np.dtype((np.float32, (height, width, 3 + has_alpha)))))
if has_alpha:
#tensors are not continuous. Rewrite will be required if this is an issue
masks = images[:,:,:,3]
images = images[:,:,:,:3]
else:
masks = torch.zeros((images.size(0), 64, 64), dtype=torch.float16, device="cpu")
masks = torch.zeros((images.size(0), 64, 64), dtype=torch.float32, device="cpu")
if len(images) == 0:
raise FileNotFoundError(f"No images could be loaded from directory '{directory}'.")
return images, masks, images.size(0)
Expand Down
8 changes: 4 additions & 4 deletions videohelpersuite/load_video_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def cv_frame_generator(video, force_rate, frame_load_cap, skip_first_frames,
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# convert frame to comfyui's expected format
# TODO: frame contains no exif information. Check if opencv2 has already applied
frame = np.array(frame, dtype=np.float16)
frame = np.array(frame, dtype=np.float32)
torch.from_numpy(frame).div_(255)
if prev_frame is not None:
inp = yield prev_frame
Expand Down Expand Up @@ -142,8 +142,8 @@ def load_video_cv(video: str, force_rate: int, force_size: str,
#TODO: verify if garbage collection should be performed here.
#leaves ~128 MB unreserved for safety
memory_limit = (psutil.virtual_memory().available + psutil.swap_memory().free) - 2 ** 27
#space required to load as f16, exist as latent with wiggle room, decode to f32
max_loadable_frames = int(memory_limit//(width*height*3*(2+4+1/10)))
#space required to load as f32, exist as latent with wiggle room, decode to f32
max_loadable_frames = int(memory_limit//(width*height*3*(4+4+1/10)))
if meta_batch is not None:
if meta_batch.frames_per_batch > max_loadable_frames:
raise RuntimeError(f"Meta Batch set to {meta_batch.frames_per_batch} frames but only {max_loadable_frames} can fit in memory")
Expand All @@ -153,7 +153,7 @@ def load_video_cv(video: str, force_rate: int, force_size: str,
gen = itertools.islice(gen, max_loadable_frames)

#Some minor wizardry to eliminate a copy and reduce max memory by a factor of ~2
images = torch.from_numpy(np.fromiter(gen, np.dtype((np.float16, (height, width, 3)))))
images = torch.from_numpy(np.fromiter(gen, np.dtype((np.float32, (height, width, 3)))))
if meta_batch is None:
try:
next(original_gen)
Expand Down

0 comments on commit aad82e9

Please sign in to comment.