|
| 1 | +from typing import Any, Callable, Dict, Optional, Tuple |
| 2 | +import torch |
| 3 | +from ..helpers.config_helper import update_config, check_hasattr |
| 4 | + |
| 5 | +__TASK__ = "object-detection" |
| 6 | + |
| 7 | + |
| 8 | +def reduce_model_config(config: Any) -> Dict[str, Any]: |
| 9 | + """Reduces a model size.""" |
| 10 | + check_hasattr(config, ("num_hidden_layers", "hidden_sizes")) |
| 11 | + kwargs = dict( |
| 12 | + num_hidden_layers=( |
| 13 | + min(config.num_hidden_layers, 2) |
| 14 | + if hasattr(config, "num_hidden_layers") |
| 15 | + else len(config.hidden_sizes) |
| 16 | + ) |
| 17 | + ) |
| 18 | + update_config(config, kwargs) |
| 19 | + return kwargs |
| 20 | + |
| 21 | + |
| 22 | +def get_inputs( |
| 23 | + model: torch.nn.Module, |
| 24 | + config: Optional[Any], |
| 25 | + input_width: int, |
| 26 | + input_height: int, |
| 27 | + input_channels: int, |
| 28 | + batch_size: int = 2, |
| 29 | + dynamic_rope: bool = False, |
| 30 | + add_second_input: bool = False, |
| 31 | + **kwargs, # unused |
| 32 | +): |
| 33 | + """ |
| 34 | + Generates inputs for task ``object-detection``. |
| 35 | +
|
| 36 | + :param model: model to get the missing information |
| 37 | + :param config: configuration used to generate the model |
| 38 | + :param batch_size: batch size |
| 39 | + :param input_channels: input channel |
| 40 | + :param input_width: input width |
| 41 | + :param input_height: input height |
| 42 | + :return: dictionary |
| 43 | + """ |
| 44 | + assert isinstance( |
| 45 | + input_width, int |
| 46 | + ), f"Unexpected type for input_width {type(input_width)}{config}" |
| 47 | + assert isinstance( |
| 48 | + input_width, int |
| 49 | + ), f"Unexpected type for input_height {type(input_height)}{config}" |
| 50 | + |
| 51 | + shapes = { |
| 52 | + "pixel_values": { |
| 53 | + 0: torch.export.Dim("batch", min=1, max=1024), |
| 54 | + 2: "width", |
| 55 | + 3: "height", |
| 56 | + } |
| 57 | + } |
| 58 | + inputs = dict( |
| 59 | + pixel_values=torch.randn(batch_size, input_channels, input_width, input_height).clamp( |
| 60 | + -1, 1 |
| 61 | + ), |
| 62 | + ) |
| 63 | + res = dict(inputs=inputs, dynamic_shapes=shapes) |
| 64 | + if add_second_input: |
| 65 | + res["inputs2"] = get_inputs( |
| 66 | + model=model, |
| 67 | + config=config, |
| 68 | + input_width=input_width + 1, |
| 69 | + input_height=input_height + 1, |
| 70 | + input_channels=input_channels, |
| 71 | + batch_size=batch_size + 1, |
| 72 | + dynamic_rope=dynamic_rope, |
| 73 | + **kwargs, |
| 74 | + )["inputs"] |
| 75 | + return res |
| 76 | + |
| 77 | + |
| 78 | +def random_input_kwargs(config: Any) -> Tuple[Dict[str, Any], Callable]: |
| 79 | + """ |
| 80 | + Inputs kwargs. |
| 81 | +
|
| 82 | + If the configuration is None, the function selects typical dimensions. |
| 83 | + """ |
| 84 | + if config is not None: |
| 85 | + if ( |
| 86 | + hasattr(config, "model_type") |
| 87 | + and config.model_type == "timm_wrapper" |
| 88 | + and not hasattr(config, "num_hidden_layers") |
| 89 | + ): |
| 90 | + input_size = config.pretrained_cfg["input_size"] |
| 91 | + kwargs = dict( |
| 92 | + batch_size=2, |
| 93 | + input_width=input_size[-2], |
| 94 | + input_height=input_size[-1], |
| 95 | + input_channels=input_size[-3], |
| 96 | + ) |
| 97 | + return kwargs, get_inputs |
| 98 | + |
| 99 | + check_hasattr(config, ("image_size", "architectures"), "num_channels") |
| 100 | + if config is not None: |
| 101 | + if hasattr(config, "image_size"): |
| 102 | + image_size = config.image_size |
| 103 | + else: |
| 104 | + assert config.architectures, f"empty architecture in {config}" |
| 105 | + from ..torch_models.hghub.hub_api import get_architecture_default_values |
| 106 | + |
| 107 | + default_values = get_architecture_default_values(config.architectures[0]) |
| 108 | + image_size = default_values["image_size"] |
| 109 | + if config is None or isinstance(image_size, int): |
| 110 | + kwargs = dict( |
| 111 | + batch_size=2, |
| 112 | + input_width=224 if config is None else image_size, |
| 113 | + input_height=224 if config is None else image_size, |
| 114 | + input_channels=3 if config is None else config.num_channels, |
| 115 | + ) |
| 116 | + else: |
| 117 | + kwargs = dict( |
| 118 | + batch_size=2, |
| 119 | + input_width=config.image_size[0], |
| 120 | + input_height=config.image_size[1], |
| 121 | + input_channels=config.num_channels, |
| 122 | + ) |
| 123 | + return kwargs, get_inputs |
0 commit comments