[pycharm] PY-81250 Debugger: add pooling for >144M pixels arrays

GitOrigin-RevId: 6928287734dc307069f98c551651bd6d655d9bb5
This commit is contained in:
ekaterina.itsenko
2025-05-15 23:30:38 +02:00
committed by intellij-monorepo-bot
parent 1716b20a9e
commit 391137001e
2 changed files with 51 additions and 6 deletions

View File

@@ -11,6 +11,9 @@ try:
except ImportError:
pass
MAX_PIXELS = 144_000_000
def create_image(arr):
# type: (np.ndarray) -> str
try:
@@ -41,6 +44,14 @@ def create_image(arr):
elif arr_to_convert.ndim == 3 and arr_to_convert.shape[2] == 1:
arr_to_convert = arr_to_convert[:, :, 0]
h, w = arr_to_convert.shape[:2]
channels = arr_to_convert.shape[2] if arr_to_convert.ndim == 3 else 1
total_pixels = h * w * channels
if total_pixels > MAX_PIXELS:
scale = (MAX_PIXELS / total_pixels) ** 0.5
new_h, new_w = max(1, int(h * scale)), max(1, int(w * scale))
arr_to_convert = average_pooling(arr_to_convert, new_h, new_w)
arr_min, arr_max = arr_to_convert.min(), arr_to_convert.max()
is_float = np.issubdtype(arr_to_convert.dtype, np.floating)
is_bool = np.issubdtype(arr_to_convert.dtype, np.bool_)
@@ -54,10 +65,9 @@ def create_image(arr):
else: # values in [0; 255]
arr_to_convert = arr_to_convert.astype(np.uint8)
arr_to_convert_ndim = arr_to_convert.ndim
if arr_to_convert_ndim == 2:
if arr_to_convert.ndim == 2:
mode = GRAYSCALE_MODE
elif arr_to_convert_ndim == 3 and arr_to_convert.shape[2] == 4:
elif arr_to_convert.ndim == 3 and arr_to_convert.shape[2] == 4:
mode = RGBA_MODE
else:
mode = RGB_MODE
@@ -70,3 +80,16 @@ def create_image(arr):
return "Error: Only non-complex numeric array types are supported."
except Exception as e:
return "Error: {}".format(e)
def average_pooling(arr, target_h, target_w):
# type: (np.ndarray, int, int) -> np.ndarray
h, w = arr.shape[:2]
factor_h, factor_w = int(h / target_h), int(w / target_w)
arr_cropped = arr[:target_h * factor_h, :target_w * factor_w]
if arr.ndim == 2:
reshaped = arr_cropped.reshape(target_h, factor_h, target_w, factor_w)
elif arr.ndim == 3:
reshaped = arr_cropped.reshape(target_h, factor_h, target_w, factor_w, arr.shape[2])
return reshaped.mean(axis=(1, 3))

View File

@@ -2,6 +2,8 @@
import numpy as np
from _pydevd_bundle.tables.images.pydevd_image_loader import (save_image_to_storage, GRAYSCALE_MODE, RGB_MODE, RGBA_MODE)
MAX_PIXELS = 144_000_000
def create_image(arr):
# type: (np.ndarray) -> str
try:
@@ -21,6 +23,14 @@ def create_image(arr):
elif arr_to_convert.ndim == 3 and arr_to_convert.shape[2] == 1:
arr_to_convert = arr_to_convert[:, :, 0]
h, w = arr_to_convert.shape[:2]
channels = arr_to_convert.shape[2] if arr_to_convert.ndim == 3 else 1
total_pixels = h * w * channels
if total_pixels > MAX_PIXELS:
scale = (MAX_PIXELS / total_pixels) ** 0.5
new_h, new_w = max(1, int(h * scale)), max(1, int(w * scale))
arr_to_convert = average_pooling(arr_to_convert, new_h, new_w)
arr_min, arr_max = arr_to_convert.min(), arr_to_convert.max()
is_float = np.issubdtype(arr_to_convert.dtype, np.floating)
is_bool = np.issubdtype(arr_to_convert.dtype, np.bool_)
@@ -34,10 +44,9 @@ def create_image(arr):
else: # values in [0; 255]
arr_to_convert = arr_to_convert.astype(np.uint8)
arr_to_convert_ndim = arr_to_convert.ndim
if arr_to_convert_ndim == 2:
if arr_to_convert.ndim == 2:
mode = GRAYSCALE_MODE
elif arr_to_convert_ndim == 3 and arr_to_convert.shape[2] == 4:
elif arr_to_convert.ndim == 3 and arr_to_convert.shape[2] == 4:
mode = RGBA_MODE
else:
mode = RGB_MODE
@@ -50,3 +59,16 @@ def create_image(arr):
return "Error: Only non-complex numeric array types are supported."
except Exception as e:
return "Error: {}".format(e)
def average_pooling(arr, target_h, target_w):
# type: (np.ndarray, int, int) -> np.ndarray
h, w = arr.shape[:2]
factor_h, factor_w = int(h / target_h), int(w / target_w)
arr_cropped = arr[:target_h * factor_h, :target_w * factor_w]
if arr.ndim == 2:
reshaped = arr_cropped.reshape(target_h, factor_h, target_w, factor_w)
elif arr.ndim == 3:
reshaped = arr_cropped.reshape(target_h, factor_h, target_w, factor_w, arr.shape[2])
return reshaped.mean(axis=(1, 3))