Spaces:
Runtime error
Runtime error
anas commited on
Commit ·
ef36c4f
1
Parent(s): 86c87ce
Remove detectron2 dependency for inference
Browse files- Replace detectron2.utils.registry.Registry with minimal implementation
- Replace detectron2 transforms with pure numpy/cv2 resize+pad
- Make datasets/__init__.py lazy-import poly_data (training only)
Made-with: Cursor
Files changed (3)
- app.py+13-15
- datasets/__init__.py+2-3
- util/bf_utils.py+20-7
10 unmodified lines1112131415161718200 unmodified lines21922022122222322422522622722822923023123223323423523623723823924024110 unmodified linesfrom shapely.geometry import Polygonfrom datasets.discrete_tokenizer import DiscreteTokenizerfrom datasets.transforms import ResizeAndPadfrom detectron2.data import transforms as Tfrom models import build_modelfrom util.plot_utils import plot_semantic_rich_floorplan_opencv200 unmodified linesMODEL = load_model()print("Model loaded.")DATA_TRANSFORM = T.AugmentationList([ResizeAndPad((MODEL_ARGS.image_size, MODEL_ARGS.image_size), pad_value=255)])def preprocess_image(pil_image: Image.Image) -> torch.Tensor:image_np = np.array(pil_image.convert("RGB"))aug_input = T.AugInput(image_np)DATA_TRANSFORM(aug_input)image_np = aug_input.imageif len(image_np.shape) == 2:tensor = np.expand_dims(image_np, 0)else:tensor = image_np.transpose((2, 0, 1))return (1 / 255) * torch.as_tensor(tensor, dtype=torch.float32)def predict_floorplan(image: Image.Image):10 unmodified lines111213141516200 unmodified lines21721821922022122222322422522622722822923023123223323423523623723823910 unmodified linesfrom shapely.geometry import Polygonfrom datasets.discrete_tokenizer import DiscreteTokenizerfrom models import build_modelfrom util.plot_utils import plot_semantic_rich_floorplan_opencv200 unmodified linesMODEL = load_model()print("Model loaded.")def preprocess_image(pil_image: Image.Image) -> torch.Tensor:"""Resize preserving aspect ratio + pad to (image_size, image_size)."""target = MODEL_ARGS.image_sizeimage_np = np.array(pil_image.convert("RGB"))h, w = image_np.shape[:2]scale = min(target / h, target / w)new_h, new_w = int(h * scale), int(w * scale)resized = cv2.resize(image_np, (new_w, new_h), interpolation=cv2.INTER_CUBIC)padded = np.full((target, target, 3), 255, dtype=np.uint8)top = (target - new_h) // 2left = (target - new_w) // 2padded[top:top + new_h, left:left + new_w] = resizedtensor = padded.transpose((2, 0, 1)).astype(np.float32) / 255.0return torch.as_tensor(tensor)def predict_floorplan(image: Image.Image):
1234567from .poly_data import build as build_polydef build_dataset(image_set, args):if args.dataset_name in ["stru3d", "cubicasa", "waffle", "r2g"]:print(f"Build {args.dataset_name} {image_set} dataset")return build_poly(image_set, args)123456def build_dataset(image_set, args):from .poly_data import build as build_polyif args.dataset_name in ["stru3d", "cubicasa", "waffle", "r2g"]:print(f"Build {args.dataset_name} {image_set} dataset")return build_poly(image_set, args)
5 unmodified lines67891011121314151617185 unmodified linesimport torchfrom torch import nnfrom detectron2.utils.registry import Registry# need an easier place to avoid circular dependencies.POLY_LOSS_REGISTRY = Registry("POLY_LOSS")POLY_LOSS_REGISTRY.__doc__ = """Registry for loss computations on predicted polygons."""def box_cxcywh_to_xyxy(x):5 unmodified lines6789101112131415161718192021222324252627282930315 unmodified linesimport torchfrom torch import nnclass _Registry:"""Minimal replacement for detectron2.utils.registry.Registry."""def __init__(self, name):self._name = nameself._obj_map = {}def register(self, obj=None):if obj is None:def decorator(func_or_class):self._obj_map[func_or_class.__name__] = func_or_classreturn func_or_classreturn decoratorself._obj_map[obj.__name__] = objreturn objdef get(self, name):return self._obj_map[name]POLY_LOSS_REGISTRY = _Registry("POLY_LOSS")def box_cxcywh_to_xyxy(x):
app.py+13-15
datasets/__init__.py+2-3
util/bf_utils.py+20-7