MCPcopy Create free account
hub / github.com/OpenBMB/AgentCPM-GUI / slice_image

Function slice_image

sft/dataset.py:440–496  ·  view source on GitHub ↗
(
    image, max_slice_nums=9, scale_resolution=448, patch_size=14, never_split=False
)

Source from the content-addressed store, hash-verified

438
439
440def slice_image(
441 image, max_slice_nums=9, scale_resolution=448, patch_size=14, never_split=False
442):
443 original_size = image.size
444 original_width, original_height = original_size
445 log_ratio = math.log(original_width / original_height)
446 ratio = original_width * original_height / \
447 (scale_resolution * scale_resolution)
448 multiple = min(math.ceil(ratio), max_slice_nums)
449
450 source_image = None
451 best_grid = None
452 patches = []
453
454 if multiple <= 1 or never_split:
455 # dont need to slice, upsample
456 best_size = find_best_resize(
457 original_size, scale_resolution, patch_size, allow_upscale=True
458 )
459 source_image = image.resize(best_size, Image.Resampling.BICUBIC)
460 else:
461 candidate_split_grids_nums = []
462 for i in [multiple - 1, multiple, multiple + 1]:
463 if i == 1 or i > max_slice_nums:
464 continue
465 candidate_split_grids_nums.append(i)
466
467 # source image, down-sampling and ensure divided by patch_size
468 best_resize = find_best_resize(
469 original_size, scale_resolution, patch_size)
470 source_image = image.copy().resize(best_resize, Image.Resampling.BICUBIC)
471 candidate_grids = []
472
473 # find best grid
474 for split_grids_nums in candidate_split_grids_nums:
475 m = 1
476 while m <= split_grids_nums:
477 if split_grids_nums % m == 0:
478 candidate_grids.append([m, split_grids_nums // m])
479 m += 1
480
481 best_grid = [1, 1]
482 min_error = float("inf")
483 for grid in candidate_grids:
484 error = abs(log_ratio - math.log(grid[0] / grid[1]))
485 if error < min_error:
486 best_grid = grid
487 min_error = error
488
489 refine_size = get_refine_size(
490 original_size, best_grid, scale_resolution, patch_size, allow_upscale=True
491 )
492
493 refine_image = image.resize(refine_size, Image.Resampling.BICUBIC)
494 patches = split_to_patches(refine_image, best_grid)
495
496 return source_image, patches, best_grid
497

Callers 1

preprocessFunction · 0.85

Calls 4

find_best_resizeFunction · 0.85
get_refine_sizeFunction · 0.85
split_to_patchesFunction · 0.85
logMethod · 0.80

Tested by

no test coverage detected