Files
LayoutMatch/data_units.py
2025-03-31 14:41:26 +08:00

42 lines
2.0 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import numpy as np
from PIL import Image
import torchvision.transforms as transforms
def layout_transforms():
"""Define data augmentation and preprocessing."""
return transforms.Compose([
transforms.Resize((256, 256)), # 调整尺寸到固定大小
transforms.RandomRotation(30), # 随机旋转(增强方向不变性)
transforms.ColorJitter(brightness=0.2, contrast=0.2), # 彩色抖动(如果使用图像数据)
transforms.ToTensor(), # 转换为张量
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) # 标准化如ImageNet均值和方差
])
def layout_to_tensor(layout_path, target_size=(256, 256)):
"""Convert layout to normalized tensor."""
img = Image.open(layout_path).convert('L') # Convert to grayscale
img = img.resize(target_size, resample=Image.BILINEAR)
return np.array(img) / 255.0 # Normalize to [0,1]
def tile_layout(large_layout, block_size=64):
"""将大版图分割为小块(滑动窗口方式)"""
height, width = large_layout.shape
stride = block_size // 2 # 步长设置重叠区域
tiles = []
for y in range(0, height - block_size +1, stride):
for x in range(0, width - block_size +1, stride):
tile = large_layout[y:y+block_size, x:x+block_size]
tiles.append((x, y, tile))
return tiles
"""将大版图分割为小块(滑动窗口方式)"""
def tile_layout(large_layout, block_size=64, overlap_ratio=0.5):
"""Split large layout into tiles with specified overlap."""
height, width = large_layout.shape
stride = block_size // 2 # 步长设置重叠区域
stride = int(block_size * (1 - overlap_ratio)) # Calculate step size based on overlap ratio
tiles = []
for y in range(0, height - block_size + 1, stride):
for x in range(0, width - block_size + 1, stride):
tile = large_layout[y:y+block_size, x:x+block_size]
tiles.append((x, y, tile))
return tiles