# Copyright 2024 MIT Han Lab | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
# | |
# SPDX-License-Identifier: Apache-2.0 | |
def val2list(x: list or tuple or any, repeat_time=1) -> list: # type: ignore | |
"""Repeat `val` for `repeat_time` times and return the list or val if list/tuple.""" | |
if isinstance(x, (list, tuple)): | |
return list(x) | |
return [x for _ in range(repeat_time)] | |
def val2tuple(x: list or tuple or any, min_len: int = 1, idx_repeat: int = -1) -> tuple: # type: ignore | |
"""Return tuple with min_len by repeating element at idx_repeat.""" | |
# convert to list first | |
x = val2list(x) | |
# repeat elements if necessary | |
if len(x) > 0: | |
x[idx_repeat:idx_repeat] = [x[idx_repeat] for _ in range(min_len - len(x))] | |
return tuple(x) | |
def get_same_padding(kernel_size: int or tuple[int, ...]) -> int or tuple[int, ...]: | |
if isinstance(kernel_size, tuple): | |
return tuple([get_same_padding(ks) for ks in kernel_size]) | |
else: | |
assert kernel_size % 2 > 0, f"kernel size {kernel_size} should be odd number" | |
return kernel_size // 2 | |