1 from ..utils import common_annotator_call, create_node_input_types, run_script 2 import comfy.model_management as model_management 3 import os, sys 4 import subprocess, threading 5 6 def install_deps(): 7 try: 8 import mediapipe
3 import numpy as np 4 import torch 5 from einops import rearrange 6 import os, sys 7 import subprocess, threading 8 import scipy.ndimage 9 import cv2 10 import torch.nn.functional as F
26 27 CATEGORY = "ControlNet Preprocessors/Optical Flow" 28 29 def estimate(self, image, ckpt_name, backward_flow=False, bidirectional_flow=False): 30 assert len(image) > 1, "[Unimatch] Requiring as least two frames as a optical flow estimator. Only use this node on video input." 31 from controlnet_aux.unimatch import UnimatchDetector 32 tensor_images = image 33 model = UnimatchDetector.from_pretrained(filename=ckpt_name).to(model_management.get_torch_device())
54 CATEGORY = "ControlNet Preprocessors/Optical Flow" 55 56 def mask_opt_flow(self, optical_flow, mask): 57 from controlnet_aux.unimatch import flow_to_image 58 assert len(mask) >= len(optical_flow), f"Not enough masks to mask optical flow: {len(mask)} vs {len(optical_flow)}" 59 mask = mask[:optical_flow.shape[0]] 60 mask = F.interpolate(mask, optical_flow.shape[1:3]) 61 mask = rearrange(mask, "n 1 h w -> n h w 1")
60 original_type = type(box) 61 is_numpy = isinstance(box, np.ndarray) 62 single_box = isinstance(box, (list, tuple)) 63 if single_box: 64 assert len(box) == 4 or len(box) == 5, ( 65 "BoxMode.convert takes either a k-tuple/list or an Nxk array/tensor," 66 " where k == 4 or 5" 67 ) 68 arr = torch.tensor(box)[None, :] 69 else: 70 # avoid modifying the input box
72 arr = torch.from_numpy(np.asarray(box)).clone() 73 else: 74 arr = box.clone() 75 76 assert to_mode not in [BoxMode.XYXY_REL, BoxMode.XYWH_REL] and from_mode not in [ 77 BoxMode.XYXY_REL, 78 BoxMode.XYWH_REL, 79 ], "Relative mode not yet supported!" 80 81 if from_mode == BoxMode.XYWHA_ABS and to_mode == BoxMode.XYXY_ABS: 82 assert (
78 BoxMode.XYWH_REL, 79 ], "Relative mode not yet supported!" 80 81 if from_mode == BoxMode.XYWHA_ABS and to_mode == BoxMode.XYXY_ABS: 82 assert ( 83 arr.shape[-1] == 5 84 ), "The last dimension of input shape must be 5 for XYWHA format" 85 original_dtype = arr.dtype 86 arr = arr.double() 87
185 matrix = cv2.resize(matrix, (w, h), self.interp_method_matrix) 186 return mask, matrix 187 188 def _check_image(self, image_rgb): 189 assert len(image_rgb.shape) == 3 190 assert image_rgb.shape[2] == 3 191 assert image_rgb.dtype == np.uint8 192
186 return mask, matrix 187 188 def _check_image(self, image_rgb): 189 assert len(image_rgb.shape) == 3 190 assert image_rgb.shape[2] == 3 191 assert image_rgb.dtype == np.uint8 192 193 def _check_mask_matrix(self, mask, matrix):
187 188 def _check_image(self, image_rgb): 189 assert len(image_rgb.shape) == 3 190 assert image_rgb.shape[2] == 3 191 assert image_rgb.dtype == np.uint8 192 193 def _check_mask_matrix(self, mask, matrix): 194 assert len(matrix.shape) == 2
190 assert image_rgb.shape[2] == 3 191 assert image_rgb.dtype == np.uint8 192 193 def _check_mask_matrix(self, mask, matrix): 194 assert len(matrix.shape) == 2 195 assert len(mask.shape) == 2 196 assert mask.dtype == np.uint8 197
191 assert image_rgb.dtype == np.uint8 192 193 def _check_mask_matrix(self, mask, matrix): 194 assert len(matrix.shape) == 2 195 assert len(mask.shape) == 2 196 assert mask.dtype == np.uint8 197 198 class DensePoseResultsVisualizer:
192 193 def _check_mask_matrix(self, mask, matrix): 194 assert len(matrix.shape) == 2 195 assert len(mask.shape) == 2 196 assert mask.dtype == np.uint8 197 198 class DensePoseResultsVisualizer: 199 def visualize(
138 class DPT_DINOv2(nn.Module): 139 def __init__(self, encoder='vitl', features=256, out_channels=[256, 512, 1024, 1024], use_bn=False, use_clstoken=False, localhub=True): 140 super(DPT_DINOv2, self).__init__() 141 142 assert encoder in ['vits', 'vitb', 'vitl'] 143 144 # in case the Internet connection is not stable, please load the DINOv2 locally 145 if localhub:
26 masks_list = [] 27 for i in range(0, n_samples_masked): 28 prob_min = probs[i] 29 prob_max = probs[i + 1] 30 masks_list.append(torch.BoolTensor(mask_generator(int(N * random.uniform(prob_min, prob_max))))) 31 upperbound += int(N * prob_max) 32 for i in range(n_samples_masked, B): 33 masks_list.append(torch.BoolTensor(mask_generator(0)))
44 basename = f"ILSVRC2012_{self.value}_{actual_index:08d}" 45 return os.path.join(dirname, basename + ".JPEG") 46 47 def parse_image_relpath(self, image_relpath: str) -> Tuple[str, int]: 48 assert self != _Split.TEST 49 dirname, filename = os.path.split(image_relpath) 50 class_id = os.path.split(dirname)[-1] 51 basename, _ = os.path.splitext(filename)
105 106 def _get_entries(self) -> np.ndarray: 107 if self._entries is None: 108 self._entries = self._load_extra(self._entries_path) 109 assert self._entries is not None 110 return self._entries 111 112 def _get_class_ids(self) -> np.ndarray:
110 return self._entries 111 112 def _get_class_ids(self) -> np.ndarray: 113 if self._split == _Split.TEST: 114 assert False, "Class IDs are not available in TEST split" 115 if self._class_ids is None: 116 self._class_ids = self._load_extra(self._class_ids_path) 117 assert self._class_ids is not None
113 if self._split == _Split.TEST: 114 assert False, "Class IDs are not available in TEST split" 115 if self._class_ids is None: 116 self._class_ids = self._load_extra(self._class_ids_path) 117 assert self._class_ids is not None 118 return self._class_ids 119 120 def _get_class_names(self) -> np.ndarray:
118 return self._class_ids 119 120 def _get_class_names(self) -> np.ndarray: 121 if self._split == _Split.TEST: 122 assert False, "Class names are not available in TEST split" 123 if self._class_names is None: 124 self._class_names = self._load_extra(self._class_names_path) 125 assert self._class_names is not None
121 if self._split == _Split.TEST: 122 assert False, "Class names are not available in TEST split" 123 if self._class_names is None: 124 self._class_names = self._load_extra(self._class_names_path) 125 assert self._class_names is not None 126 return self._class_names 127 128 def find_class_id(self, class_index: int) -> str:
165 return None if self.split == _Split.TEST else str(class_name) 166 167 def __len__(self) -> int: 168 entries = self._get_entries() 169 assert len(entries) == self.split.length 170 return len(entries) 171 172 def _load_labels(self, labels_path: str) -> List[Tuple[str, str]]:
232 entries_array[index] = (actual_index, class_index, class_id, class_name) 233 else: 234 class_names = {class_id: class_name for class_id, class_name in labels} 235 236 assert dataset 237 old_percent = -1 238 for index in range(sample_count): 239 percent = 100 * (index + 1) // sample_count
166 class_entries.append(class_entry) 167 except OSError as e: 168 raise RuntimeError(f'can not read blocks file "{path}"') from e 169 170 assert class_entries[-1].maybe_filename is None 171 172 for class_entry1, class_entry2 in zip(class_entries, class_entries[1:]): 173 assert class_entry1.block_offset <= class_entry2.block_offset
169 170 assert class_entries[-1].maybe_filename is None 171 172 for class_entry1, class_entry2 in zip(class_entries, class_entries[1:]): 173 assert class_entry1.block_offset <= class_entry2.block_offset 174 start_offset = 512 * class_entry1.block_offset 175 end_offset = 512 * class_entry2.block_offset 176 assert class_entry1.maybe_filename is not None
172 for class_entry1, class_entry2 in zip(class_entries, class_entries[1:]): 173 assert class_entry1.block_offset <= class_entry2.block_offset 174 start_offset = 512 * class_entry1.block_offset 175 end_offset = 512 * class_entry2.block_offset 176 assert class_entry1.maybe_filename is not None 177 filename = class_entry1.maybe_filename 178 entry = _Entry(class_index, start_offset, end_offset, filename) 179 # Skip invalid image files (PIL throws UnidentifiedImageError)
211 mapped_data = class_mmap[start_offset:end_offset] 212 data = mapped_data[512:] # Skip entry header block 213 214 if len(data) >= 2 and tuple(data[:2]) == (0x1F, 0x8B): 215 assert index in self._gzipped_indices, f"unexpected gzip header for sample {index}" 216 with GzipFile(fileobj=BytesIO(data)) as g: 217 data = g.read() 218 except Exception as e:
49 kwargs = {} 50 51 for token in tokens[1:]: 52 key, value = token.split("=") 53 assert key in ("root", "extra", "split") 54 kwargs[key] = value 55 56 if name == "ImageNet":
49 50 def _mask(self, mask, max_mask_patches): 51 delta = 0 52 for _ in range(10): 53 target_area = random.uniform(self.min_num_patches, max_mask_patches) 54 aspect_ratio = math.exp(random.uniform(*self.log_aspect_ratio)) 55 h = int(round(math.sqrt(target_area * aspect_ratio))) 56 w = int(round(math.sqrt(target_area / aspect_ratio)))
50 def _mask(self, mask, max_mask_patches): 51 delta = 0 52 for _ in range(10): 53 target_area = random.uniform(self.min_num_patches, max_mask_patches) 54 aspect_ratio = math.exp(random.uniform(*self.log_aspect_ratio)) 55 h = int(round(math.sqrt(target_area * aspect_ratio))) 56 w = int(round(math.sqrt(target_area / aspect_ratio))) 57 if w < self.width and h < self.height:
54 aspect_ratio = math.exp(random.uniform(*self.log_aspect_ratio)) 55 h = int(round(math.sqrt(target_area * aspect_ratio))) 56 w = int(round(math.sqrt(target_area / aspect_ratio))) 57 if w < self.width and h < self.height: 58 top = random.randint(0, self.height - h) 59 left = random.randint(0, self.width - w) 60 61 num_masked = mask[top : top + h, left : left + w].sum()
55 h = int(round(math.sqrt(target_area * aspect_ratio))) 56 w = int(round(math.sqrt(target_area / aspect_ratio))) 57 if w < self.width and h < self.height: 58 top = random.randint(0, self.height - h) 59 left = random.randint(0, self.width - w) 60 61 num_masked = mask[top : top + h, left : left + w].sum() 62 # Overlap
102 103 yield from itertools.islice(iterator, self._advance, None) 104 105 def _iterator(self): 106 assert not self._shuffle 107 108 while True: 109 iterable = range(self._sample_count)
109 iterable = range(self._sample_count) 110 yield from itertools.islice(iterable, self._start, None, self._step) 111 112 def _shuffled_iterator(self): 113 assert self._shuffle 114 115 # Instantiate a generator here (rather than in the ctor) to keep the class 116 # picklable (requirement of mp.spawn)
199 200 yield from itertools.islice(iterator, self._advance, None) 201 202 def _iterator(self): 203 assert not self._shuffle 204 205 while True: 206 iterable = range(self._sample_count)
206 iterable = range(self._sample_count) 207 yield from itertools.islice(iterable, self._start, None, self._step) 208 209 def _shuffled_iterator(self): 210 assert self._shuffle 211 212 # Instantiate a generator here (rather than in the ctor) to be keep the class 213 # picklable (requirement of mp.spawn)
47 The rank of the current process within the local (per-machine) process group. 48 """ 49 if not is_enabled(): 50 return 0 51 assert 0 <= _LOCAL_RANK < _LOCAL_WORLD_SIZE 52 return _LOCAL_RANK 53 54
59 i.e. the number of processes per machine. 60 """ 61 if not is_enabled(): 62 return 1 63 assert 0 <= _LOCAL_RANK < _LOCAL_WORLD_SIZE 64 return _LOCAL_WORLD_SIZE 65 66
92 MIN_MASTER_PORT, MAX_MASTER_PORT = (20_000, 60_000) 93 94 master_port_str = os.environ.get("MASTER_PORT") 95 if master_port_str is None: 96 rng = random.Random(seed) 97 return rng.randint(MIN_MASTER_PORT, MAX_MASTER_PORT) 98 99 return int(master_port_str)
183 # logger.info("Initialization from Slurm environment") 184 job_id = int(os.environ["SLURM_JOB_ID"]) 185 node_count = int(os.environ["SLURM_JOB_NUM_NODES"]) 186 nodes = _parse_slurm_node_list(os.environ["SLURM_JOB_NODELIST"]) 187 assert len(nodes) == node_count 188 189 self.master_addr = nodes[0] 190 self.master_port = _get_master_port(seed=job_id)
189 self.master_addr = nodes[0] 190 self.master_port = _get_master_port(seed=job_id) 191 self.rank = int(os.environ["SLURM_PROCID"]) 192 self.world_size = int(os.environ["SLURM_NTASKS"]) 193 assert self.rank < self.world_size 194 self.local_rank = int(os.environ["SLURM_LOCALID"]) 195 self.local_world_size = self.world_size // node_count 196 assert self.local_rank < self.local_world_size
192 self.world_size = int(os.environ["SLURM_NTASKS"]) 193 assert self.rank < self.world_size 194 self.local_rank = int(os.environ["SLURM_LOCALID"]) 195 self.local_world_size = self.world_size // node_count 196 assert self.local_rank < self.local_world_size 197 198 # Single node job with preset environment (i.e. torchrun) 199 def _set_from_preset_env(self):
201 self.master_addr = os.environ["MASTER_ADDR"] 202 self.master_port = os.environ["MASTER_PORT"] 203 self.rank = int(os.environ["RANK"]) 204 self.world_size = int(os.environ["WORLD_SIZE"]) 205 assert self.rank < self.world_size 206 self.local_rank = int(os.environ["LOCAL_RANK"]) 207 self.local_world_size = int(os.environ["LOCAL_WORLD_SIZE"]) 208 assert self.local_rank < self.local_world_size
204 self.world_size = int(os.environ["WORLD_SIZE"]) 205 assert self.rank < self.world_size 206 self.local_rank = int(os.environ["LOCAL_RANK"]) 207 self.local_world_size = int(os.environ["LOCAL_WORLD_SIZE"]) 208 assert self.local_rank < self.local_world_size 209 210 # Single node and GPU job (i.e. local script run) 211 def _set_from_local(self):
171 def forward(self, features_rank): 172 """ 173 Compute the results on all values of `self.nb_knn` neighbors from the full `self.max_k` 174 """ 175 assert all(k <= self.max_k for k in self.nb_knn) 176 177 topk_sims, neighbors_labels = self.compute_neighbors(features_rank) 178 batch_size = neighbors_labels.shape[0]
488 test_dataset_strs = [val_dataset_str] 489 if test_metric_types is None: 490 test_metric_types = [val_metric_type] * len(test_dataset_strs) 491 else: 492 assert len(test_metric_types) == len(test_dataset_strs) 493 assert len(test_dataset_strs) == len(test_class_mapping_fpaths) 494 495 train_transform = make_classification_train_transform()
489 if test_metric_types is None: 490 test_metric_types = [val_metric_type] * len(test_dataset_strs) 491 else: 492 assert len(test_metric_types) == len(test_dataset_strs) 493 assert len(test_dataset_strs) == len(test_class_mapping_fpaths) 494 495 train_transform = make_classification_train_transform() 496 train_dataset = make_dataset(
141 142 logger.info(f"Features shape: {tuple(features.shape)}") 143 logger.info(f"Labels shape: {tuple(all_labels.shape)}") 144 145 assert torch.all(all_labels > -1) 146 147 return features, all_labels
105 data.update(kwargs) 106 107 basename = f"{name}.{rankstr()}.pth" 108 save_file = os.path.join(self.save_dir, basename) 109 assert os.path.basename(save_file) == basename, basename 110 self.logger.info("Saving checkpoint to {}".format(save_file)) 111 with self.path_manager.open(save_file, "wb") as f: 112 torch.save(data, f)
58 59 class MemEffAttention(Attention): 60 def forward(self, x: Tensor, attn_bias=None) -> Tensor: 61 if not XFORMERS_AVAILABLE: 62 assert attn_bias is None, "xFormers is required for nested tensors usage" 63 return super().forward(x) 64 65 B, N, C = x.shape
198 def forward_nested(self, x_list: List[Tensor]) -> List[Tensor]: 199 """ 200 x_list contains a list of tensors to nest together and run 201 """ 202 assert isinstance(self.attn, MemEffAttention) 203 204 if self.training and self.sample_drop_ratio > 0.0: 205
238 def forward(self, x_or_x_list): 239 if isinstance(x_or_x_list, Tensor): 240 return super().forward(x_or_x_list) 241 elif isinstance(x_or_x_list, list): 242 assert XFORMERS_AVAILABLE, "Please install xFormers for nested tensors usage" 243 return self.forward_nested(x_or_x_list) 244 else: 245 raise AssertionError
15 16 17 def make_2tuple(x): 18 if isinstance(x, tuple): 19 assert len(x) == 2 20 return x 21 22 assert isinstance(x, int)
18 if isinstance(x, tuple): 19 assert len(x) == 2 20 return x 21 22 assert isinstance(x, int) 23 return (x, x) 24 25
69 def forward(self, x: Tensor) -> Tensor: 70 _, _, H, W = x.shape 71 patch_H, patch_W = self.patch_size 72 73 assert H % patch_H == 0, f"Input image height {H} is not a multiple of patch height {patch_H}" 74 assert W % patch_W == 0, f"Input image width {W} is not a multiple of patch width: {patch_W}" 75 76 x = self.proj(x) # B C H W
70 _, _, H, W = x.shape 71 patch_H, patch_W = self.patch_size 72 73 assert H % patch_H == 0, f"Input image height {H} is not a multiple of patch height {patch_H}" 74 assert W % patch_W == 0, f"Input image width {W} is not a multiple of patch width: {patch_W}" 75 76 x = self.proj(x) # B C H W 77 H, W = x.size(2), x.size(3)
27 def update(self, **kwargs): 28 for k, v in kwargs.items(): 29 if isinstance(v, torch.Tensor): 30 v = v.item() 31 assert isinstance(v, (float, int)) 32 self.meters[k].update(v) 33 34 def __getattr__(self, attr):
183 scale_factor=(w0 / math.sqrt(N), h0 / math.sqrt(N)), 184 mode="bicubic", 185 ) 186 187 assert int(w0) == patch_pos_embed.shape[-2] and int(h0) == patch_pos_embed.shape[-1] 188 patch_pos_embed = patch_pos_embed.permute(0, 2, 3, 1).view(1, -1, dim) 189 return torch.cat((class_pos_embed.unsqueeze(0), patch_pos_embed), dim=1).to(previous_dtype) 190
243 for i, blk in enumerate(self.blocks): 244 x = blk(x) 245 if i in blocks_to_take: 246 output.append(x) 247 assert len(output) == len(blocks_to_take), f"only {len(output)} / {len(blocks_to_take)} blocks found" 248 return output 249 250 def _get_intermediate_layers_chunked(self, x, n=1):
257 x = blk(x) 258 if i in blocks_to_take: 259 output.append(x) 260 i += 1 261 assert len(output) == len(blocks_to_take), f"only {len(output)} / {len(blocks_to_take)} blocks found" 262 return output 263 264 def get_intermediate_layers(
50 args = args_parser.parse_args() 51 52 setup_logging() 53 54 assert os.path.exists(args.config_file), "Configuration file does not exist!" 55 submit_jobs(Evaluator, args, name="dinov2:knn") 56 return 0 57
50 args = args_parser.parse_args() 51 52 setup_logging() 53 54 assert os.path.exists(args.config_file), "Configuration file does not exist!" 55 submit_jobs(Evaluator, args, name="dinov2:linear") 56 return 0 57
50 args = args_parser.parse_args() 51 52 setup_logging() 53 54 assert os.path.exists(args.config_file), "Configuration file does not exist!" 55 submit_jobs(Evaluator, args, name="dinov2:logreg") 56 return 0 57
50 args = args_parser.parse_args() 51 52 setup_logging() 53 54 assert os.path.exists(args.config_file), "Configuration file does not exist!" 55 submit_jobs(Trainer, args, name="dinov2:train") 56 return 0 57
24 25 XFORMERS_AVAILABLE = True 26 except ImportError: 27 XFORMERS_AVAILABLE = False 28 assert XFORMERS_AVAILABLE, "xFormers is required for DINOv2 training" 29 30 31 logger = logging.getLogger("dinov2")
90 logger.info(f"OPTIONS -- IBOT masking -- ibot_mask_ratio_tuple: {cfg.ibot.mask_ratio_min_max}") 91 logger.info(f"OPTIONS -- IBOT masking -- ibot_mask_sample_probability: {cfg.ibot.mask_sample_probability}") 92 if self.do_ibot: 93 self.ibot_loss_weight = cfg.ibot.loss_weight 94 assert max(cfg.ibot.mask_ratio_min_max) > 0, "please provide a positive mask ratio tuple for ibot" 95 assert cfg.ibot.mask_sample_probability > 0, "please provide a positive mask probability for ibot" 96 self.ibot_out_dim = cfg.ibot.head_n_prototypes if self.ibot_separate_head else cfg.dino.head_n_prototypes 97 self.ibot_patch_loss = iBOTPatchLoss(self.ibot_out_dim)
91 logger.info(f"OPTIONS -- IBOT masking -- ibot_mask_sample_probability: {cfg.ibot.mask_sample_probability}") 92 if self.do_ibot: 93 self.ibot_loss_weight = cfg.ibot.loss_weight 94 assert max(cfg.ibot.mask_ratio_min_max) > 0, "please provide a positive mask ratio tuple for ibot" 95 assert cfg.ibot.mask_sample_probability > 0, "please provide a positive mask probability for ibot" 96 self.ibot_out_dim = cfg.ibot.head_n_prototypes if self.ibot_separate_head else cfg.dino.head_n_prototypes 97 self.ibot_patch_loss = iBOTPatchLoss(self.ibot_out_dim) 98 if self.ibot_separate_head:
133 loss.backward() 134 135 def forward_backward(self, images, teacher_temp): 136 n_global_crops = 2 137 assert n_global_crops == 2 138 n_local_crops = self.cfg.crops.local_crops_number 139 140 global_crops = images["collated_global_crops"].cuda(non_blocking=True)
54 if checkpoint_path is None: 55 return None 56 57 username = os.environ.get("USER") 58 assert username is not None 59 return checkpoint_path / username 60 61
33 if isinstance(dtype, torch.dtype): 34 return dtype 35 if isinstance(dtype, str): 36 dtype = np.dtype(dtype) 37 assert isinstance(dtype, np.dtype), f"Expected an instance of nunpy dtype, got {type(dtype)}" 38 return _NUMPY_TO_TORCH_DTYPE[dtype]
6 7 import logging 8 import os 9 import random 10 import subprocess 11 from urllib.parse import urlparse 12 13 import numpy as np
47 def get_sha(): 48 cwd = os.path.dirname(os.path.abspath(__file__)) 49 50 def _run(command): 51 return subprocess.check_output(command, cwd=cwd).decode("ascii").strip() 52 53 sha = "N/A" 54 diff = "clean"
54 diff = "clean" 55 branch = "N/A" 56 try: 57 sha = _run(["git", "rev-parse", "HEAD"]) 58 subprocess.check_output(["git", "diff"], cwd=cwd) 59 diff = _run(["git", "diff-index", "HEAD"]) 60 diff = "has uncommitted changes" if diff else "clean" 61 branch = _run(["git", "rev-parse", "--abbrev-ref", "HEAD"])
54 diff = "clean" 55 branch = "N/A" 56 try: 57 sha = _run(["git", "rev-parse", "HEAD"]) 58 subprocess.check_output(["git", "diff"], cwd=cwd) 59 diff = _run(["git", "diff-index", "HEAD"]) 60 diff = "has uncommitted changes" if diff else "clean" 61 branch = _run(["git", "rev-parse", "--abbrev-ref", "HEAD"])
58 subprocess.check_output(["git", "diff"], cwd=cwd) 59 diff = _run(["git", "diff-index", "HEAD"]) 60 diff = "has uncommitted changes" if diff else "clean" 61 branch = _run(["git", "rev-parse", "--abbrev-ref", "HEAD"]) 62 except Exception: 63 pass 64 message = f"sha: {sha}, status: {diff}, branch: {branch}" 65 return message 66
78 iters = np.arange(total_iters - warmup_iters - freeze_iters) 79 schedule = final_value + 0.5 * (base_value - final_value) * (1 + np.cos(np.pi * iters / len(iters))) 80 self.schedule = np.concatenate((freeze_schedule, warmup_schedule, schedule)) 81 82 assert len(self.schedule) == self.total_iters 83 84 def __getitem__(self, it): 85 if it >= self.total_iters:
107 num_patches = self.patch_embed.num_patches 108 109 self.cls_token = nn.Parameter(torch.zeros(1, 1, embed_dim)) 110 self.pos_embed = nn.Parameter(torch.zeros(1, num_patches + self.num_tokens, embed_dim)) 111 assert num_register_tokens >= 0 112 self.register_tokens = ( 113 nn.Parameter(torch.zeros(1, num_register_tokens, embed_dim)) if num_register_tokens else None 114 )
203 mode="bicubic", 204 antialias=self.interpolate_antialias 205 ) 206 207 assert int(w0) == patch_pos_embed.shape[-2] 208 assert int(h0) == patch_pos_embed.shape[-1] 209 patch_pos_embed = patch_pos_embed.permute(0, 2, 3, 1).view(1, -1, dim) 210 return torch.cat((class_pos_embed.unsqueeze(0), patch_pos_embed), dim=1).to(previous_dtype)
204 antialias=self.interpolate_antialias 205 ) 206 207 assert int(w0) == patch_pos_embed.shape[-2] 208 assert int(h0) == patch_pos_embed.shape[-1] 209 patch_pos_embed = patch_pos_embed.permute(0, 2, 3, 1).view(1, -1, dim) 210 return torch.cat((class_pos_embed.unsqueeze(0), patch_pos_embed), dim=1).to(previous_dtype) 211
276 for i, blk in enumerate(self.blocks): 277 x = blk(x) 278 if i in blocks_to_take: 279 output.append(x) 280 assert len(output) == len(blocks_to_take), f"only {len(output)} / {len(blocks_to_take)} blocks found" 281 return output 282 283 def _get_intermediate_layers_chunked(self, x, n=1):
290 x = blk(x) 291 if i in blocks_to_take: 292 output.append(x) 293 i += 1 294 assert len(output) == len(blocks_to_take), f"only {len(output)} / {len(blocks_to_take)} blocks found" 295 return output 296 297 def get_intermediate_layers(
12 import torchvision.transforms as transforms 13 import torchvision.datasets as datasets 14 from typing import Any, Callable, Optional, Tuple 15 import os 16 import pickle 17 import numpy as np 18 import copy 19 import custom_albumentations as albumentations
111 # print("lb_data.size:", lb_data.size) 112 lb = np.array(lb_data, dtype=np.float32) 113 if lb.ndim == 3: 114 lb = np.squeeze(lb[:, :, 0]) 115 assert lb.ndim == 2 116 threshold = self.threshold 117 lb = lb[np.newaxis, :, :] 118
86 self.image_size = image_size 87 self.train_sample = train_sample 88 self.objective = objective 89 self.start_dist = start_dist 90 assert start_dist in ['normal', 'uniform'] 91 92 assert objective in {'pred_noise', 'pred_x0', 'pred_v', 'pred_delta', 'pred_KC'}, 'objective must be either pred_noise (predict noise) or pred_x0 (predict image start) or pred_v (predict v [v-parameterization as defined in appendix D of progressive distillation paper, used in imagen-video successfully])' 93
88 self.objective = objective 89 self.start_dist = start_dist 90 assert start_dist in ['normal', 'uniform'] 91 92 assert objective in {'pred_noise', 'pred_x0', 'pred_v', 'pred_delta', 'pred_KC'}, 'objective must be either pred_noise (predict noise) or pred_x0 (predict image start) or pred_v (predict v [v-parameterization as defined in appendix D of progressive distillation paper, used in imagen-video successfully])' 93 94 if beta_schedule == 'linear': 95 betas = linear_beta_schedule(timesteps)
148 149 # calculate p2 reweighting 150 151 register_buffer('p2_loss_weight', (p2_loss_weight_k + alphas_cumprod / (1 - alphas_cumprod)) ** -p2_loss_weight_gamma) 152 assert not torch.isnan(self.p2_loss_weight).all() 153 if self.objective == "pred_noise": 154 lvlb_weights = self.betas ** 2 / ( 155 2 * (self.posterior_variance+1e-5) * alphas * (1 - self.alphas_cumprod))
161 lvlb_weights = 0.5 * torch.sqrt(alphas_cumprod) / (2. * 1 - alphas_cumprod) 162 elif self.objective == "pred_v": 163 lvlb_weights = 0.5 * torch.sqrt(alphas_cumprod) / (2. * 1 - alphas_cumprod) 164 self.register_buffer('lvlb_weights', lvlb_weights, persistent=False) 165 assert not torch.isnan(self.lvlb_weights).all() 166 self.use_l1 = use_l1 167 168 self.perceptual_weight = perceptual_weight
266 def interpolate(self, x1, x2, mask, t = None, lam = 0.5): 267 b, *_, device = *x1.shape, x1.device 268 t = default(t, self.num_timesteps - 1) 269 270 assert x1.shape == x2.shape 271 272 t_batched = torch.stack([torch.tensor(t, device = device)] * b) 273 xt1, xt2 = map(lambda x: self.q_sample(x, t = t_batched), (x1, x2))
277 img = self.p_sample(img, mask, torch.full((b,), i, device=device, dtype=torch.long)) 278 return img 279 280 def get_input(self, batch, return_first_stage_outputs=False, return_original_cond=False): 281 assert 'image' in self.input_keys; 282 if len(self.input_keys) > len(batch.keys()): 283 x, *_ = batch.values() 284 else:
559 ckpt_path = kwargs.pop("ckpt_path", None) 560 ignore_keys = kwargs.pop("ignore_keys", []) 561 only_model = kwargs.pop("only_model", False) 562 super().__init__(*args, **kwargs) 563 assert self.num_timesteps_cond <= self.num_timesteps 564 if not scale_by_std: 565 self.scale_factor = scale_factor 566 else:
567 self.register_buffer('scale_factor', torch.tensor(scale_factor)) 568 if self.scale_by_softsign: 569 self.scale_by_std = False 570 print('### USING SOFTSIGN RESCALING') 571 assert (self.scale_by_std and self.scale_by_softsign) is False; 572 573 self.init_first_stage(auto_encoder) 574 # self.instantiate_cond_stage(cond_stage_config)
573 self.init_first_stage(auto_encoder) 574 # self.instantiate_cond_stage(cond_stage_config) 575 self.input_keys = input_keys 576 self.clip_denoised = False 577 assert sample_type in ['p_loop', 'ddim', 'dpm', 'transformer'] ### 'dpm' is not availible now, suggestion 'ddim' 578 self.sample_type = sample_type 579 580 if ckpt_path is not None:
620 def on_train_batch_start(self, batch): 621 # only for the first batch 622 if self.scale_by_std and (not self.scale_by_softsign): 623 if not self.default_scale: 624 assert self.scale_factor == 1., 'rather not use custom rescaling and std-rescaling simultaneously' 625 # set rescale weight to 1./std of encodings 626 print("### USING STD-RESCALING ###") 627 x, *_ = batch.values()
637 print(f'### USING SOFTSIGN SCALE !') 638 639 @torch.no_grad() 640 def get_input(self, batch, return_first_stage_outputs=False, return_original_cond=False): 641 assert 'image' in self.input_keys; 642 # if len(self.input_keys) > len(batch.keys()): 643 # x, cond, *_ = batch.values() 644 # else:
7 return val is not None 8 9 10 def clamp(value, min_value=None, max_value=None): 11 assert exists(min_value) or exists(max_value) 12 if exists(min_value): 13 value = max(value, min_value) 14
90 self.inv_gamma = inv_gamma 91 self.power = power 92 self.min_value = min_value 93 94 assert isinstance(param_or_buffer_names_no_ema, (set, list)) 95 self.param_or_buffer_names_no_ema = param_or_buffer_names_no_ema # parameter or buffer 96 97 self.ignore_names = ignore_names
34 Build sinusoidal embeddings. 35 This matches the implementation in tensor2tensor, but differs slightly 36 from the description in Section 3.5 of "Attention Is All You Need". 37 """ 38 assert len(timesteps.shape) == 1 39 40 half_dim = embedding_dim // 2 41 emb = math.log(10000) / (half_dim - 1)
220 return x+h_ 221 222 223 def make_attn(in_channels, attn_type="vanilla"): 224 assert attn_type in ["vanilla", "linear", "none"], f'attn_type {attn_type} unknown' 225 print(f"making attention of type '{attn_type}' with {in_channels} in_channels") 226 if attn_type == "vanilla": 227 return AttnBlock(in_channels)
337 # assume aligned context, cat along channel axis 338 x = torch.cat((x, context), dim=1) 339 if self.use_timestep: 340 # timestep embedding 341 assert t is not None 342 temb = get_timestep_embedding(t, self.ch) 343 temb = self.temb.dense[0](temb) 344 temb = nonlinearity(temb)
745 746 class Upsampler(nn.Module): 747 def __init__(self, in_size, out_size, in_channels, out_channels, ch_mult=2): 748 super().__init__() 749 assert out_size >= in_size 750 num_blocks = int(np.log2(out_size//in_size))+1 751 factor_up = 1.+ (out_size % in_size) 752 print(f"Building {self.__class__.__name__} with in_size: {in_size} --> out_size {out_size} and factor {factor_up}")
769 self.mode = mode 770 if self.with_conv: 771 print(f"Note: {self.__class__.__name} uses learned downsampling and will ignore the fixed {mode} mode") 772 raise NotImplementedError() 773 assert in_channels is not None 774 # no asymmetric padding in torch conv, must do it ourselves 775 self.conv = torch.nn.Conv2d(in_channels, 776 in_channels,
794 dropout=0., 795 pretrained_config=None): 796 super().__init__() 797 if pretrained_config is None: 798 assert pretrained_model is not None, 'Either "pretrained_model" or "pretrained_config" must not be None' 799 self.pretrained_model = pretrained_model 800 else: 801 assert pretrained_config is not None, 'Either "pretrained_model" or "pretrained_config" must not be None'
797 if pretrained_config is None: 798 assert pretrained_model is not None, 'Either "pretrained_model" or "pretrained_config" must not be None' 799 self.pretrained_model = pretrained_model 800 else: 801 assert pretrained_config is not None, 'Either "pretrained_model" or "pretrained_config" must not be None' 802 self.instantiate_pretrained(pretrained_config) 803 804 self.do_reshape = reshape
907 self.encoder = Encoder(**ddconfig) 908 self.decoder = Decoder(**ddconfig) 909 self.down_ratio = 2 ** (len(ddconfig['ch_mult']) - 1) 910 self.loss = LPIPSWithDiscriminator(**lossconfig) 911 assert ddconfig["double_z"] 912 self.quant_conv = torch.nn.Conv2d(2*ddconfig["z_channels"], 2*embed_dim, 1) 913 self.post_quant_conv = torch.nn.Conv2d(embed_dim, ddconfig["z_channels"], 1) 914 self.embed_dim = embed_dim
912 self.quant_conv = torch.nn.Conv2d(2*ddconfig["z_channels"], 2*embed_dim, 1) 913 self.post_quant_conv = torch.nn.Conv2d(embed_dim, ddconfig["z_channels"], 1) 914 self.embed_dim = embed_dim 915 if colorize_nlabels is not None: 916 assert type(colorize_nlabels)==int 917 self.register_buffer("colorize", torch.randn(3, colorize_nlabels, 1, 1)) 918 if monitor is not None: 919 self.monitor = monitor
1 import os, yaml, pickle, shutil, tarfile, glob 2 import cv2 3 import custom_albumentations as albumentations 4 import PIL 5 import numpy as np 6 import torchvision.transforms.functional as TF 7 # from omegaconf import OmegaConf 8 from functools import partial
18 19 20 def synset2idx(path_to_yaml="data/index_synset.yaml"): 21 with open(path_to_yaml) as f: 22 di2s = yaml.load(f) 23 return dict((v,k) for k,v in di2s.items()) 24 25
83 if (not os.path.exists(self.human2integer)): 84 download(URL, self.human2integer) 85 with open(self.human2integer, "r") as f: 86 lines = f.read().splitlines() 87 assert len(lines) == 1000 88 self.human2integer_dict = dict() 89 for line in lines: 90 value, key = line.split(":")
169 path = os.path.join(self.root, self.FILES[0]) 170 if not os.path.exists(path) or not os.path.getsize(path)==self.SIZES[0]: 171 import academictorrents as at 172 atpath = at.get(self.AT_HASH, datastore=self.root) 173 assert atpath == path 174 175 print("Extracting {} to {}".format(path, datadir)) 176 os.makedirs(datadir, exist_ok=True)
174 175 print("Extracting {} to {}".format(path, datadir)) 176 os.makedirs(datadir, exist_ok=True) 177 with tarfile.open(path, "r:") as tar: 178 tar.extractall(path=datadir) 179 180 print("Extracting sub-tars.") 181 subpaths = sorted(glob.glob(os.path.join(datadir, "*.tar")))
182 for subpath in tqdm(subpaths): 183 subdir = subpath[:-len(".tar")] 184 os.makedirs(subdir, exist_ok=True) 185 with tarfile.open(subpath, "r:") as tar: 186 tar.extractall(path=subdir) 187 188 filelist = glob.glob(os.path.join(datadir, "**", "*.JPEG")) 189 filelist = [os.path.relpath(p, start=datadir) for p in filelist]
234 path = os.path.join(self.root, self.FILES[0]) 235 if not os.path.exists(path) or not os.path.getsize(path)==self.SIZES[0]: 236 import academictorrents as at 237 atpath = at.get(self.AT_HASH, datastore=self.root) 238 assert atpath == path 239 240 print("Extracting {} to {}".format(path, datadir)) 241 os.makedirs(datadir, exist_ok=True)
239 240 print("Extracting {} to {}".format(path, datadir)) 241 os.makedirs(datadir, exist_ok=True) 242 with tarfile.open(path, "r:") as tar: 243 tar.extractall(path=datadir) 244 245 vspath = os.path.join(self.root, self.FILES[1]) 246 if not os.path.exists(vspath) or not os.path.getsize(vspath)==self.SIZES[1]:
290 :param data_root: 291 :param random_crop: 292 """ 293 self.base = self.get_base() 294 assert size 295 assert (size / downscale_f).is_integer() 296 self.size = size 297 self.LR_size = int(size / downscale_f)
291 :param random_crop: 292 """ 293 self.base = self.get_base() 294 assert size 295 assert (size / downscale_f).is_integer() 296 self.size = size 297 self.LR_size = int(size / downscale_f) 298 self.min_crop_f = min_crop_f
296 self.size = size 297 self.LR_size = int(size / downscale_f) 298 self.min_crop_f = min_crop_f 299 self.max_crop_f = max_crop_f 300 assert(max_crop_f <= 1.) 301 self.center_crop = not random_crop 302 303 self.image_rescaler = albumentations.SmallestMaxSize(max_size=size, interpolation=cv2.INTER_AREA)
378 super().__init__(**kwargs) 379 380 def get_base(self): 381 with open("data/imagenet_train_hr_indices.p", "rb") as f: 382 indices = pickle.load(f) 383 dset = ImageNetTrain(process_images=False,) 384 return Subset(dset, indices) 385
389 super().__init__(**kwargs) 390 391 def get_base(self): 392 with open("data/imagenet_val_hr_indices.p", "rb") as f: 393 indices = pickle.load(f) 394 dset = ImageNetValidation(process_images=False,) 395 return Subset(dset, indices)
11 perceptual_weight=1.0, use_actnorm=False, disc_conditional=False, 12 disc_loss="hinge"): 13 14 super().__init__() 15 assert disc_loss in ["hinge", "vanilla"] 16 self.kl_weight = kl_weight 17 self.pixel_weight = pixelloss_weight 18 self.perceptual_loss = LPIPS().eval()
64 # now the GAN part 65 if optimizer_idx == 0: 66 # generator update 67 if cond is None: 68 assert not self.disc_conditional 69 logits_fake = self.discriminator(reconstructions.contiguous()) 70 else: 71 assert self.disc_conditional
67 if cond is None: 68 assert not self.disc_conditional 69 logits_fake = self.discriminator(reconstructions.contiguous()) 70 else: 71 assert self.disc_conditional 72 logits_fake = self.discriminator(torch.cat((reconstructions.contiguous(), cond), dim=1)) 73 g_loss = -torch.mean(logits_fake) 74
75 if self.disc_factor > 0.0: 76 try: 77 d_weight = self.calculate_adaptive_weight(nll_loss, g_loss, last_layer=last_layer) 78 except RuntimeError: 79 assert not self.training 80 d_weight = torch.tensor(0.0) 81 else: 82 d_weight = torch.tensor(0.0)
41 num_pos_feats = num_feats // 2 42 # mask = tensor_list.mask 43 mask = torch.zeros(x.shape[0], x.shape[1], x.shape[2], device=x.device).to(torch.bool) 44 batch = mask.shape[0] 45 assert mask is not None 46 not_mask = ~mask 47 y_embed = not_mask.cumsum(1, dtype=torch.float32) 48 x_embed = not_mask.cumsum(2, dtype=torch.float32)
414 """ https://github.com/crowsonkb/v-diffusion-jax/blob/master/diffusion/models/danbooru_128.py#L8 """ 415 416 def __init__(self, dim, is_random = False): 417 super().__init__() 418 assert (dim % 2) == 0 419 half_dim = dim // 2 420 self.weights = nn.Parameter(torch.randn(half_dim), requires_grad = not is_random) 421
35 36 class weight_quantize_fn(nn.Module): 37 def __init__(self, bit_w): 38 super(weight_quantize_fn, self).__init__() 39 assert bit_w > 0 40 41 self.bit_w = bit_w - 1 42 self.weight_q = weight_quantization(b=self.bit_w)
87 self.bit_a = bit_a 88 self.signed = signed 89 if signed: 90 self.bit_a -= 1 91 assert bit_a > 0 92 93 self.act_q = act_quantization(b=self.bit_a, signed=signed) 94 self.register_parameter('a_alpha', Parameter(torch.tensor(8.0), requires_grad=True))
122 """ https://github.com/crowsonkb/v-diffusion-jax/blob/master/diffusion/models/danbooru_128.py#L8 """ 123 124 def __init__(self, dim, is_random = False): 125 super().__init__() 126 assert (dim % 2) == 0 127 half_dim = dim // 2 128 self.weights = nn.Parameter(torch.randn(half_dim), requires_grad = not is_random) 129
12 from pathlib import Path 13 14 def load_conf(config_file, conf={}): 15 with open(config_file) as f: 16 exp_conf = yaml.load(f, Loader=yaml.FullLoader) 17 for k, v in exp_conf.items(): 18 conf[k] = v 19 return conf
191 (int(x1), int(preds.shape[3] - x2), int(y1), 192 int(preds.shape[2] - y2))) 193 count_mat[:, :, y1:y2, x1:x2] += 1 194 195 assert (count_mat == 0).sum() == 0 196 seg_logits = preds / count_mat 197 return seg_logits
43 self.img_id_to_captions = dict() 44 self.img_id_to_filepath = dict() 45 self.img_id_to_segmentation_filepath = dict() 46 47 assert data_json.split("/")[-1] in ["captions_train2017.json", 48 "captions_val2017.json"] 49 if self.stuffthing: 50 self.segmentation_prefix = ( 51 "data/cocostuffthings/val2017" if
103 if not self.onehot and not segmentation.mode == "RGB": 104 segmentation = segmentation.convert("RGB") 105 segmentation = np.array(segmentation).astype(np.uint8) 106 if self.onehot: 107 assert self.stuffthing 108 # stored in caffe format: unlabeled==255. stuff and thing from 109 # 0-181. to be compatible with the labels in 110 # https://github.com/nightrome/cocostuff/blob/master/labels.txt
110 # https://github.com/nightrome/cocostuff/blob/master/labels.txt 111 # we shift stuffthing one to the right and put unlabeled in zero 112 # as long as segmentation is uint8 shifting to right handles the 113 # latter too 114 assert segmentation.dtype == np.uint8 115 segmentation = segmentation + 1 116 117 processed = self.preprocessor(image=image, segmentation=segmentation)
118 image, segmentation = processed["image"], processed["segmentation"] 119 image = (image / 127.5 - 1.0).astype(np.float32) 120 121 if self.onehot: 122 assert segmentation.dtype == np.uint8 123 # make it one hot 124 n_labels = 183 125 flatseg = np.ravel(segmentation)
32 if self.encode_crop: 33 crop_coordinates = self.bbox_from_token_pair(conditional_list[-2], conditional_list[-1]) 34 conditional_list = conditional_list[:-2] 35 object_triples = grouper(conditional_list, 3) 36 assert conditional.shape[0] == self.embedding_dim 37 return [ 38 (object_triple[0], self.bbox_from_token_pair(object_triple[1], object_triple[2])) 39 for object_triple in object_triples if object_triple[0] != self.none
76 if self.encode_crop: 77 crop_coordinates = self.bbox_from_token_pair(conditional_list[-2], conditional_list[-1]) 78 conditional_list = conditional_list[:-2] 79 table_of_content = grouper(conditional_list, self.object_descriptor_length) 80 assert conditional.shape[0] == self.embedding_dim 81 return [ 82 (object_tuple[0], self.coordinates_from_token(object_tuple[1])) 83 for object_tuple in table_of_content if object_tuple[0] != self.none
162 extra = [] 163 164 object_tuples = self._make_object_descriptors(annotations) 165 flattened = [token for tuple_ in object_tuples for token in tuple_] + extra 166 assert len(flattened) == self.embedding_dim 167 assert all(0 <= value < self.no_tokens for value in flattened) 168 return LongTensor(flattened)
163 164 object_tuples = self._make_object_descriptors(annotations) 165 flattened = [token for tuple_ in object_tuples for token in tuple_] + extra 166 assert len(flattened) == self.embedding_dim 167 assert all(0 <= value < self.no_tokens for value in flattened) 168 return LongTensor(flattened)
74 max_size = min(width, height) 75 if max_size <= self.min_size: 76 size = max_size 77 else: 78 size = random.randint(self.min_size, max_size) 79 top = random.randint(0, height - size) 80 left = random.randint(0, width - size) 81 bbox = left / width, top / height, size / width, size / height
75 if max_size <= self.min_size: 76 size = max_size 77 else: 78 size = random.randint(self.min_size, max_size) 79 top = random.randint(0, height - size) 80 left = random.randint(0, width - size) 81 bbox = left / width, top / height, size / width, size / height 82 return bbox, F.crop(img, top, left, size, size)
76 size = max_size 77 else: 78 size = random.randint(self.min_size, max_size) 79 top = random.randint(0, height - size) 80 left = random.randint(0, width - size) 81 bbox = left / width, top / height, size / width, size / height 82 return bbox, F.crop(img, top, left, size, size) 83
14 15 def give_synsets_from_indices(indices, path_to_yaml="data/imagenet_idx_to_synset.yaml"): 16 synsets = [] 17 with open(path_to_yaml) as f: 18 di2s = yaml.load(f) 19 for idx in indices: 20 synsets.append(str(di2s[idx])) 21 print("Using {} different synsets for construction of Restriced Imagenet.".format(len(synsets)))
23 24 25 def str_to_indices(string): 26 """Expects a string in the format '32-123, 256, 280-321'""" 27 assert not string.endswith(","), "provided string '{}' ends with a comma, pls remove it".format(string) 28 subs = string.split(",") 29 indices = [] 30 for sub in subs:
28 subs = string.split(",") 29 indices = [] 30 for sub in subs: 31 subsubs = sub.split("-") 32 assert len(subsubs) > 0 33 if len(subsubs) == 1: 34 indices.append(int(subsubs[0])) 35 else:
148 path = os.path.join(self.root, self.FILES[0]) 149 if not os.path.exists(path) or not os.path.getsize(path)==self.SIZES[0]: 150 import academictorrents as at 151 atpath = at.get(self.AT_HASH, datastore=self.root) 152 assert atpath == path 153 154 print("Extracting {} to {}".format(path, datadir)) 155 os.makedirs(datadir, exist_ok=True)
153 154 print("Extracting {} to {}".format(path, datadir)) 155 os.makedirs(datadir, exist_ok=True) 156 with tarfile.open(path, "r:") as tar: 157 tar.extractall(path=datadir) 158 159 print("Extracting sub-tars.") 160 subpaths = sorted(glob.glob(os.path.join(datadir, "*.tar")))
161 for subpath in tqdm(subpaths): 162 subdir = subpath[:-len(".tar")] 163 os.makedirs(subdir, exist_ok=True) 164 with tarfile.open(subpath, "r:") as tar: 165 tar.extractall(path=subdir) 166 167 168 filelist = glob.glob(os.path.join(datadir, "**", "*.JPEG"))
206 path = os.path.join(self.root, self.FILES[0]) 207 if not os.path.exists(path) or not os.path.getsize(path)==self.SIZES[0]: 208 import academictorrents as at 209 atpath = at.get(self.AT_HASH, datastore=self.root) 210 assert atpath == path 211 212 print("Extracting {} to {}".format(path, datadir)) 213 os.makedirs(datadir, exist_ok=True)
211 212 print("Extracting {} to {}".format(path, datadir)) 213 os.makedirs(datadir, exist_ok=True) 214 with tarfile.open(path, "r:") as tar: 215 tar.extractall(path=datadir) 216 217 vspath = os.path.join(self.root, self.FILES[1]) 218 if not os.path.exists(vspath) or not os.path.getsize(vspath)==self.SIZES[1]:
270 return preprocessor 271 272 273 def rgba_to_depth(x): 274 assert x.dtype == np.uint8 275 assert len(x.shape) == 3 and x.shape[2] == 4 276 y = x.copy() 277 y.dtype = np.float32
271 272 273 def rgba_to_depth(x): 274 assert x.dtype == np.uint8 275 assert len(x.shape) == 3 and x.shape[2] == 4 276 y = x.copy() 277 y.dtype = np.float32 278 y = y.reshape(x.shape[:2])
417 if factor is None or factor==1: 418 return x 419 420 dtype = x.dtype 421 assert dtype in [np.float32, np.float64] 422 assert x.min() >= -1 423 assert x.max() <= 1 424
418 return x 419 420 dtype = x.dtype 421 assert dtype in [np.float32, np.float64] 422 assert x.min() >= -1 423 assert x.max() <= 1 424 425 keepmode = {"nearest": Image.NEAREST, "bilinear": Image.BILINEAR,
419 420 dtype = x.dtype 421 assert dtype in [np.float32, np.float64] 422 assert x.min() >= -1 423 assert x.max() <= 1 424 425 keepmode = {"nearest": Image.NEAREST, "bilinear": Image.BILINEAR, 426 "bicubic": Image.BICUBIC}[keepmode]
431 432 h, w, _ = x.shape 433 nh = h//factor 434 nw = w//factor 435 assert nh > 0 and nw > 0, (nh, nw) 436 437 lr = lr.resize((nw,nh), Image.BICUBIC) 438 if keepshapes:
60 image = np.array(image).astype(np.uint8) 61 if self.size is not None: 62 image = self.image_rescaler(image=image)["image"] 63 segmentation = Image.open(example["segmentation_path_"]) 64 assert segmentation.mode == "L", segmentation.mode 65 segmentation = np.array(segmentation).astype(np.uint8) 66 if self.shift_segmentation: 67 # used to support segmentations containing unlabeled==255 label
15 16 def unpack(path): 17 if path.endswith("tar.gz"): 18 with tarfile.open(path, "r:gz") as tar: 19 tar.extractall(path=os.path.split(path)[0]) 20 elif path.endswith("tar"): 21 with tarfile.open(path, "r:") as tar: 22 tar.extractall(path=os.path.split(path)[0])
18 with tarfile.open(path, "r:gz") as tar: 19 tar.extractall(path=os.path.split(path)[0]) 20 elif path.endswith("tar"): 21 with tarfile.open(path, "r:") as tar: 22 tar.extractall(path=os.path.split(path)[0]) 23 elif path.endswith("zip"): 24 with zipfile.ZipFile(path, "r") as f: 25 f.extractall(path=os.path.split(path)[0])
21 with tarfile.open(path, "r:") as tar: 22 tar.extractall(path=os.path.split(path)[0]) 23 elif path.endswith("zip"): 24 with zipfile.ZipFile(path, "r") as f: 25 f.extractall(path=os.path.split(path)[0]) 26 else: 27 raise NotImplementedError( 28 "Unknown file extension: {}".format(os.path.splitext(path)[1])
80 os.makedirs(target_dir, exist_ok=True) 81 with tqdm( 82 unit="B", unit_scale=True, unit_divisor=1024, miniters=1, desc=file_ 83 ) as bar: 84 urllib.request.urlretrieve(url, targetpath, reporthook=reporthook(bar)) 85 return targetpath 86 87
12 Build sinusoidal embeddings. 13 This matches the implementation in tensor2tensor, but differs slightly 14 from the description in Section 3.5 of "Attention Is All You Need". 15 """ 16 assert len(timesteps.shape) == 1 17 18 half_dim = embedding_dim // 2 19 emb = math.log(10000) / (half_dim - 1)
296 #assert x.shape[2] == x.shape[3] == self.resolution 297 298 if self.use_timestep: 299 # timestep embedding 300 assert t is not None 301 temb = get_timestep_embedding(t, self.ch) 302 temb = self.temb.dense[0](temb) 303 temb = nonlinearity(temb)
646 #assert x.shape[2] == x.shape[3] == self.resolution 647 648 if self.use_timestep: 649 # timestep embedding 650 assert t is not None 651 temb = get_timestep_embedding(t, self.ch) 652 temb = self.temb.dense[0](temb) 653 temb = nonlinearity(temb)
16 17 18 def download(url, local_path, chunk_size=1024): 19 os.makedirs(os.path.split(local_path)[0], exist_ok=True) 20 with requests.get(url, stream=True) as r: 21 total_size = int(r.headers.get("content-length", 0)) 22 with tqdm(total=total_size, unit="B", unit_scale=True) as pbar: 23 with open(local_path, "wb") as f:
29 30 def md5_hash(path): 31 with open(path, "rb") as f: 32 content = f.read() 33 return hashlib.md5(content).hexdigest() 34 35 36 def get_ckpt_path(name, root, check=False):
33 return hashlib.md5(content).hexdigest() 34 35 36 def get_ckpt_path(name, root, check=False): 37 assert name in URL_MAP 38 path = os.path.join(root, CKPT_MAP[name]) 39 if not os.path.exists(path) or (check and not md5_hash(path) == MD5_MAP[name]): 40 print("Downloading {} model from {} to {}".format(name, URL_MAP[name], path))
39 if not os.path.exists(path) or (check and not md5_hash(path) == MD5_MAP[name]): 40 print("Downloading {} model from {} to {}".format(name, URL_MAP[name], path)) 41 download(URL_MAP[name], path) 42 md5 = md5_hash(path) 43 assert md5 == MD5_MAP[name], md5 44 return path 45 46
36 disc_num_layers=3, disc_in_channels=3, disc_factor=1.0, disc_weight=1.0, 37 perceptual_weight=1.0, use_actnorm=False, disc_conditional=False, 38 disc_ndf=64, disc_loss="hinge"): 39 super().__init__() 40 assert disc_loss in ["hinge", "vanilla"] 41 self.codebook_weight = codebook_weight 42 self.pixel_weight = pixelloss_weight 43 self.perceptual_loss = LPIPS().eval()
89 # now the GAN part 90 if optimizer_idx == 0: 91 # generator update 92 if cond is None: 93 assert not self.disc_conditional 94 logits_fake = self.discriminator(reconstructions.contiguous()) 95 else: 96 assert self.disc_conditional
92 if cond is None: 93 assert not self.disc_conditional 94 logits_fake = self.discriminator(reconstructions.contiguous()) 95 else: 96 assert self.disc_conditional 97 logits_fake = self.discriminator(torch.cat((reconstructions.contiguous(), cond), dim=1)) 98 g_loss = -torch.mean(logits_fake) 99
99 100 try: 101 d_weight = self.calculate_adaptive_weight(nll_loss, g_loss, last_layer=last_layer) 102 except RuntimeError: 103 assert not self.training 104 d_weight = torch.tensor(0.0) 105 106 disc_factor = adopt_weight(self.disc_factor, global_step, threshold=self.discriminator_iter_start)
9 return self 10 11 def encode(self, c): 12 """fake vqmodel interface""" 13 assert 0.0 <= c.min() and c.max() <= 1.0 14 b,ch,h,w = c.shape 15 assert ch == 1 16
11 def encode(self, c): 12 """fake vqmodel interface""" 13 assert 0.0 <= c.min() and c.max() <= 1.0 14 b,ch,h,w = c.shape 15 assert ch == 1 16 17 c = torch.nn.functional.interpolate(c, scale_factor=1/self.down_factor, 18 mode="area")
9 10 class ActNorm(nn.Module): 11 def __init__(self, num_features, logdet=False, affine=True, 12 allow_reverse_init=False): 13 assert affine 14 super().__init__() 15 self.logdet = logdet 16 self.loc = nn.Parameter(torch.zeros(1, num_features, 1, 1))
145 self.re_embed = n_embed 146 147 def remap_to_used(self, inds): 148 ishape = inds.shape 149 assert len(ishape)>1 150 inds = inds.reshape(ishape[0],-1) 151 used = self.used.to(inds) 152 match = (inds[:,:,None]==used[None,None,...]).long()
159 return new.reshape(ishape) 160 161 def unmap_to_all(self, inds): 162 ishape = inds.shape 163 assert len(ishape)>1 164 inds = inds.reshape(ishape[0],-1) 165 used = self.used.to(inds) 166 if self.re_embed > self.used.shape[0]: # extra token
200 return z_q, diff, ind 201 202 def get_codebook_entry(self, indices, shape): 203 b, h, w, c = shape 204 assert b*h*w == indices.shape[0] 205 indices = rearrange(indices, '(b h w) -> b h w', b=b, h=h, w=w) 206 if self.remap is not None: 207 indices = self.unmap_to_all(indices)
245 self.sane_index_shape = sane_index_shape 246 247 def remap_to_used(self, inds): 248 ishape = inds.shape 249 assert len(ishape)>1 250 inds = inds.reshape(ishape[0],-1) 251 used = self.used.to(inds) 252 match = (inds[:,:,None]==used[None,None,...]).long()
259 return new.reshape(ishape) 260 261 def unmap_to_all(self, inds): 262 ishape = inds.shape 263 assert len(ishape)>1 264 inds = inds.reshape(ishape[0],-1) 265 used = self.used.to(inds) 266 if self.re_embed > self.used.shape[0]: # extra token
268 back=torch.gather(used[None,:][inds.shape[0]*[0],:], 1, inds) 269 return back.reshape(ishape) 270 271 def forward(self, z, temp=None, rescale_logits=False, return_logits=False): 272 assert temp is None or temp==1.0, "Only for interface compatible with Gumbel" 273 assert rescale_logits==False, "Only for interface compatible with Gumbel" 274 assert return_logits==False, "Only for interface compatible with Gumbel" 275 # reshape z -> (batch, height, width, channel) and flatten
269 return back.reshape(ishape) 270 271 def forward(self, z, temp=None, rescale_logits=False, return_logits=False): 272 assert temp is None or temp==1.0, "Only for interface compatible with Gumbel" 273 assert rescale_logits==False, "Only for interface compatible with Gumbel" 274 assert return_logits==False, "Only for interface compatible with Gumbel" 275 # reshape z -> (batch, height, width, channel) and flatten 276 z = rearrange(z, 'b c h w -> b h w c').contiguous()
270 271 def forward(self, z, temp=None, rescale_logits=False, return_logits=False): 272 assert temp is None or temp==1.0, "Only for interface compatible with Gumbel" 273 assert rescale_logits==False, "Only for interface compatible with Gumbel" 274 assert return_logits==False, "Only for interface compatible with Gumbel" 275 # reshape z -> (batch, height, width, channel) and flatten 276 z = rearrange(z, 'b c h w -> b h w c').contiguous() 277 z_flattened = z.view(-1, self.e_dim)
381 self.re_embed = n_embed 382 383 def remap_to_used(self, inds): 384 ishape = inds.shape 385 assert len(ishape)>1 386 inds = inds.reshape(ishape[0],-1) 387 used = self.used.to(inds) 388 match = (inds[:,:,None]==used[None,None,...]).long()
395 return new.reshape(ishape) 396 397 def unmap_to_all(self, inds): 398 ishape = inds.shape 399 assert len(ishape)>1 400 inds = inds.reshape(ishape[0],-1) 401 used = self.used.to(inds) 402 if self.re_embed > self.used.shape[0]: # extra token
16 17 18 def download(url, local_path, chunk_size=1024): 19 os.makedirs(os.path.split(local_path)[0], exist_ok=True) 20 with requests.get(url, stream=True) as r: 21 total_size = int(r.headers.get("content-length", 0)) 22 with tqdm(total=total_size, unit="B", unit_scale=True) as pbar: 23 with open(local_path, "wb") as f:
29 30 def md5_hash(path): 31 with open(path, "rb") as f: 32 content = f.read() 33 return hashlib.md5(content).hexdigest() 34 35 36 def get_ckpt_path(name, root, check=False):
33 return hashlib.md5(content).hexdigest() 34 35 36 def get_ckpt_path(name, root, check=False): 37 assert name in URL_MAP 38 path = os.path.join(root, CKPT_MAP[name]) 39 if not os.path.exists(path) or (check and not md5_hash(path) == MD5_MAP[name]): 40 print("Downloading {} model from {} to {}".format(name, URL_MAP[name], path))
39 if not os.path.exists(path) or (check and not md5_hash(path) == MD5_MAP[name]): 40 print("Downloading {} model from {} to {}".format(name, URL_MAP[name], path)) 41 download(URL_MAP[name], path) 42 md5 = md5_hash(path) 43 assert md5 == MD5_MAP[name], md5 44 return path 45 46
67 _OVERRIDE_FN[name] = fn 68 69 70 def update_override_act_fn(overrides): 71 assert isinstance(overrides, dict) 72 global _OVERRIDE_FN 73 _OVERRIDE_FN.update(overrides) 74
82 _OVERRIDE_LAYER[name] = fn 83 84 85 def update_override_act_layer(overrides): 86 assert isinstance(overrides, dict) 87 global _OVERRIDE_LAYER 88 _OVERRIDE_LAYER.update(overrides) 89
141 kwargs.setdefault('bias', False) 142 padding, is_dynamic = get_padding_value(padding, kernel_size, **kwargs) 143 if is_dynamic: 144 if is_exportable(): 145 assert not is_scriptable() 146 return Conv2dSameExport(in_chs, out_chs, kernel_size, **kwargs) 147 else: 148 return Conv2dSame(in_chs, out_chs, kernel_size, **kwargs)
287 return out 288 289 290 def select_conv2d(in_chs, out_chs, kernel_size, **kwargs): 291 assert 'groups' not in kwargs # only use 'depthwise' bool arg 292 if isinstance(kernel_size, list): 293 assert 'num_experts' not in kwargs # MixNet + CondConv combo not supported currently 294 # We're going to use only lists for defining the MixedConv2d kernel groups,
289 290 def select_conv2d(in_chs, out_chs, kernel_size, **kwargs): 291 assert 'groups' not in kwargs # only use 'depthwise' bool arg 292 if isinstance(kernel_size, list): 293 assert 'num_experts' not in kwargs # MixNet + CondConv combo not supported currently 294 # We're going to use only lists for defining the MixedConv2d kernel groups, 295 # ints, tuples, other iterables will continue to pass to normal conv and specify h, w. 296 m = MixedConv2d(in_chs, out_chs, kernel_size, **kwargs)
58 if not se_kwargs.pop('reduce_mid'): 59 se_kwargs['reduced_base_chs'] = in_chs 60 # act_layer override, if it remains None, the containing block's act_layer will be used 61 if se_kwargs['act_layer'] is None: 62 assert act_layer is not None 63 se_kwargs['act_layer'] = act_layer 64 return se_kwargs 65
122 class ConvBnAct(nn.Module): 123 def __init__(self, in_chs, out_chs, kernel_size, 124 stride=1, pad_type='', act_layer=nn.ReLU, norm_layer=nn.BatchNorm2d, norm_kwargs=None): 125 super(ConvBnAct, self).__init__() 126 assert stride in [1, 2] 127 norm_kwargs = norm_kwargs or {} 128 self.conv = select_conv2d(in_chs, out_chs, kernel_size, stride=stride, padding=pad_type) 129 self.bn1 = norm_layer(out_chs, **norm_kwargs)
145 stride=1, pad_type='', act_layer=nn.ReLU, noskip=False, 146 pw_kernel_size=1, pw_act=False, se_ratio=0., se_kwargs=None, 147 norm_layer=nn.BatchNorm2d, norm_kwargs=None, drop_connect_rate=0.): 148 super(DepthwiseSeparableConv, self).__init__() 149 assert stride in [1, 2] 150 norm_kwargs = norm_kwargs or {} 151 self.has_residual = (stride == 1 and in_chs == out_chs) and not noskip 152 self.drop_connect_rate = drop_connect_rate
396 ba['norm_kwargs'] = self.norm_kwargs 397 ba['pad_type'] = self.pad_type 398 # block act fn overrides the model default 399 ba['act_layer'] = ba['act_layer'] if ba['act_layer'] is not None else self.act_layer 400 assert ba['act_layer'] is not None 401 if bt == 'ir': 402 ba['drop_connect_rate'] = self.drop_connect_rate * self.block_idx / self.block_count 403 ba['se_kwargs'] = self.se_kwargs
415 block = EdgeResidual(**ba) 416 elif bt == 'cn': 417 block = ConvBnAct(**ba) 418 else: 419 assert False, 'Uknkown block type (%s) while building model.' % bt 420 self.in_chs = ba['out_chs'] # update in_chs for arg of next block 421 return block 422
446 self.block_idx = 0 447 blocks = [] 448 # outer list of block_args defines the stacks ('stages' by some conventions) 449 for stack_idx, stack in enumerate(block_args): 450 assert isinstance(stack, list) 451 stack = self._make_stack(stack) 452 blocks.append(stack) 453 return blocks
484 A list of block args (dicts) 485 Raises: 486 ValueError: if the string def not properly specified (TODO) 487 """ 488 assert isinstance(block_str, str) 489 ops = block_str.split('_') 490 block_type = ops[0] # take the block type off the front 491 ops = ops[1:]
573 stride=int(options['s']), 574 act_layer=act_layer, 575 ) 576 else: 577 assert False, 'Unknown block type (%s)' % block_type 578 579 return block_args, num_repeat 580
619 620 def decode_arch_def(arch_def, depth_multiplier=1.0, depth_trunc='ceil', experts_multiplier=1, fix_first_last=False): 621 arch_args = [] 622 for stack_idx, block_strings in enumerate(arch_def): 623 assert isinstance(block_strings, list) 624 stack_args = [] 625 repeats = [] 626 for block_str in block_strings:
623 assert isinstance(block_strings, list) 624 stack_args = [] 625 repeats = [] 626 for block_str in block_strings: 627 assert isinstance(block_str, str) 628 ba, rep = _decode_block_str(block_str) 629 if ba.get('num_experts', 0) > 0 and experts_multiplier > 1: 630 ba['num_experts'] *= experts_multiplier
26 graphs = [] 27 num_nodes = 0 28 for node in graph.node: 29 pn, gs = onnx.helper.printable_node(node, indent, subgraphs=True) 30 assert isinstance(gs, list) 31 content.append(pn) 32 graphs.extend(gs) 33 num_nodes += 1
9 # Get the long description from the README file 10 with open(path.join(here, 'README.md'), encoding='utf-8') as f: 11 long_description = f.read() 12 13 exec(open('geffnet/version.py').read()) 14 setup( 15 name='geffnet', 16 version=__version__,
44 outdir_inc = outdir + '-' + str(count) 45 while os.path.exists(outdir_inc): 46 count = count + 1 47 outdir_inc = outdir + '-' + str(count) 48 assert count < 100 49 outdir = outdir_inc 50 os.makedirs(outdir) 51 return outdir
86 def decompress_keypoints(numbers: Optional[List[float]]) -> Optional[List[Optional[Keypoint]]]: 87 if not numbers: 88 return None 89 90 assert len(numbers) % 3 == 0 91 92 def create_keypoint(x, y, c): 93 if c < 1.0:
79 def __call__(self, input_image, detect_resolution=512, safe=False, output_type="pil", scribble=False, upscale_method="INTER_CUBIC", **kwargs): 80 input_image, output_type = common_input_validate(input_image, output_type, **kwargs) 81 input_image, remove_pad = resize_image_with_pad(input_image, detect_resolution, upscale_method) 82 83 assert input_image.ndim == 3 84 H, W, C = input_image.shape 85 with torch.no_grad(): 86 image_hed = torch.from_numpy(input_image).float().to(self.device)
108 109 Return an initialized network. 110 """ 111 if len(gpu_ids) > 0: 112 assert(torch.cuda.is_available()) 113 net.to(gpu_ids[0]) 114 net = torch.nn.DataParallel(net, gpu_ids) # multi-GPUs 115 init_weights(net, init_type, init_gain=init_gain)
337 use_dropout (bool) -- if use dropout layers 338 n_blocks (int) -- the number of ResNet blocks 339 padding_type (str) -- the name of padding layer in conv layers: reflect | replicate | zero 340 """ 341 assert(n_blocks >= 0) 342 super(ResnetGenerator, self).__init__() 343 if type(norm_layer) == functools.partial: 344 use_bias = norm_layer.func == nn.InstanceNorm2d
121 input_image, output_type = common_input_validate(input_image, output_type, **kwargs) 122 detected_map, remove_pad = resize_image_with_pad(input_image, detect_resolution, upscale_method) 123 124 model = self.model_coarse if coarse else self.model 125 assert detected_map.ndim == 3 126 with torch.no_grad(): 127 image = torch.from_numpy(detected_map).float().to(self.device) 128 image = image / 255.0
101 refine_landmarks=True, 102 min_detection_confidence=min_confidence, 103 ) as facemesh: 104 img_height, img_width, img_channels = img_rgb.shape 105 assert(img_channels == 3) 106 107 results = facemesh.process(img_rgb).multi_face_landmarks 108
126 #logger.info("Update config parameter {}: {} -> {}".format(param, config_param, arg_param)) 127 setattr(config, param, arg_param) 128 129 # init a transformer encoder and append it to a list 130 assert config.hidden_size % config.num_attention_heads == 0 131 model = model_class(config=config) 132 #logger.info("Init model from scratch.") 133 trans_encoder.append(model)
267 tri_index = intersector.intersects_first(rays_o, rays_d) 268 269 tri_index = tri_index[index_ray] 270 271 assert len(index_ray) == len(tri_index) 272 273 discriminator = (np.sum(mesh.face_normals[tri_index]* rays_d[index_ray], axis=-1)<= 0) 274 points = points[discriminator] # ray intesects in interior faces, discard them
52 resize_mode = "upper_bound" 53 normalization = NormalizeImage(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) 54 55 else: 56 assert False, f"model_type '{model_type}' not implemented, use: --model_type large" 57 58 transform = Compose( 59 [
120 ) 121 122 else: 123 print(f"model_type '{model_type}' not implemented, use: --model_type large") 124 assert False 125 126 transform = Compose( 127 [
156 ] 157 158 def __init__(self, model_type, model_path): 159 super().__init__() 160 assert (model_type in self.MODEL_TYPES_ISL) 161 model, _ = load_model(model_type, model_path) 162 self.model = model 163 self.model.train = disabled_train
39 lines = pred_lines(img, self.model, [img.shape[0], img.shape[1]], thr_v, thr_d) 40 for line in lines: 41 x_start, y_start, x_end, y_end = [int(val) for val in line] 42 cv2.line(img_output, (x_start, y_start), (x_end, y_end), [255, 255, 255], 1) 43 except Exception as e: 44 pass 45 46 detected_map = remove_pad(HWC3(img_output[:, :, 0])) 47
124 class InvertedResidual(nn.Module): 125 def __init__(self, inp, oup, stride, expand_ratio): 126 super(InvertedResidual, self).__init__() 127 self.stride = stride 128 assert stride in [1, 2] 129 130 hidden_dim = int(round(inp * expand_ratio)) 131 self.use_res_connect = self.stride == 1 and inp == oup
123 class InvertedResidual(nn.Module): 124 def __init__(self, inp, oup, stride, expand_ratio): 125 super(InvertedResidual, self).__init__() 126 self.stride = stride 127 assert stride in [1, 2] 128 129 hidden_dim = int(round(inp * expand_ratio)) 130 self.use_res_connect = self.stride == 1 and inp == oup
22 center: tpMap[1, 0, :, :] 23 displacement: tpMap[1, 1:5, :, :] 24 ''' 25 b, c, h, w = tpMap.shape 26 assert b==1, 'only support bsize==1' 27 displacement = tpMap[:, 1:5, :, :][0] 28 center = tpMap[:, 0, :, :] 29 heat = torch.sigmoid(center)
551 sorted_idx = np.argsort(score_array)[::-1] 552 score_array = score_array[sorted_idx] 553 squares = squares[sorted_idx] 554 555 except Exception as e: 556 pass 557 558 '''return list 559 merged_lines, squares, scores
67 _OVERRIDE_FN[name] = fn 68 69 70 def update_override_act_fn(overrides): 71 assert isinstance(overrides, dict) 72 global _OVERRIDE_FN 73 _OVERRIDE_FN.update(overrides) 74
82 _OVERRIDE_LAYER[name] = fn 83 84 85 def update_override_act_layer(overrides): 86 assert isinstance(overrides, dict) 87 global _OVERRIDE_LAYER 88 _OVERRIDE_LAYER.update(overrides) 89
141 kwargs.setdefault('bias', False) 142 padding, is_dynamic = get_padding_value(padding, kernel_size, **kwargs) 143 if is_dynamic: 144 if is_exportable(): 145 assert not is_scriptable() 146 return Conv2dSameExport(in_chs, out_chs, kernel_size, **kwargs) 147 else: 148 return Conv2dSame(in_chs, out_chs, kernel_size, **kwargs)
287 return out 288 289 290 def select_conv2d(in_chs, out_chs, kernel_size, **kwargs): 291 assert 'groups' not in kwargs # only use 'depthwise' bool arg 292 if isinstance(kernel_size, list): 293 assert 'num_experts' not in kwargs # MixNet + CondConv combo not supported currently 294 # We're going to use only lists for defining the MixedConv2d kernel groups,
289 290 def select_conv2d(in_chs, out_chs, kernel_size, **kwargs): 291 assert 'groups' not in kwargs # only use 'depthwise' bool arg 292 if isinstance(kernel_size, list): 293 assert 'num_experts' not in kwargs # MixNet + CondConv combo not supported currently 294 # We're going to use only lists for defining the MixedConv2d kernel groups, 295 # ints, tuples, other iterables will continue to pass to normal conv and specify h, w. 296 m = MixedConv2d(in_chs, out_chs, kernel_size, **kwargs)
58 if not se_kwargs.pop('reduce_mid'): 59 se_kwargs['reduced_base_chs'] = in_chs 60 # act_layer override, if it remains None, the containing block's act_layer will be used 61 if se_kwargs['act_layer'] is None: 62 assert act_layer is not None 63 se_kwargs['act_layer'] = act_layer 64 return se_kwargs 65
122 class ConvBnAct(nn.Module): 123 def __init__(self, in_chs, out_chs, kernel_size, 124 stride=1, pad_type='', act_layer=nn.ReLU, norm_layer=nn.BatchNorm2d, norm_kwargs=None): 125 super(ConvBnAct, self).__init__() 126 assert stride in [1, 2] 127 norm_kwargs = norm_kwargs or {} 128 self.conv = select_conv2d(in_chs, out_chs, kernel_size, stride=stride, padding=pad_type) 129 self.bn1 = norm_layer(out_chs, **norm_kwargs)
145 stride=1, pad_type='', act_layer=nn.ReLU, noskip=False, 146 pw_kernel_size=1, pw_act=False, se_ratio=0., se_kwargs=None, 147 norm_layer=nn.BatchNorm2d, norm_kwargs=None, drop_connect_rate=0.): 148 super(DepthwiseSeparableConv, self).__init__() 149 assert stride in [1, 2] 150 norm_kwargs = norm_kwargs or {} 151 self.has_residual = (stride == 1 and in_chs == out_chs) and not noskip 152 self.drop_connect_rate = drop_connect_rate
396 ba['norm_kwargs'] = self.norm_kwargs 397 ba['pad_type'] = self.pad_type 398 # block act fn overrides the model default 399 ba['act_layer'] = ba['act_layer'] if ba['act_layer'] is not None else self.act_layer 400 assert ba['act_layer'] is not None 401 if bt == 'ir': 402 ba['drop_connect_rate'] = self.drop_connect_rate * self.block_idx / self.block_count 403 ba['se_kwargs'] = self.se_kwargs
415 block = EdgeResidual(**ba) 416 elif bt == 'cn': 417 block = ConvBnAct(**ba) 418 else: 419 assert False, 'Uknkown block type (%s) while building model.' % bt 420 self.in_chs = ba['out_chs'] # update in_chs for arg of next block 421 return block 422
446 self.block_idx = 0 447 blocks = [] 448 # outer list of block_args defines the stacks ('stages' by some conventions) 449 for stack_idx, stack in enumerate(block_args): 450 assert isinstance(stack, list) 451 stack = self._make_stack(stack) 452 blocks.append(stack) 453 return blocks
484 A list of block args (dicts) 485 Raises: 486 ValueError: if the string def not properly specified (TODO) 487 """ 488 assert isinstance(block_str, str) 489 ops = block_str.split('_') 490 block_type = ops[0] # take the block type off the front 491 ops = ops[1:]
573 stride=int(options['s']), 574 act_layer=act_layer, 575 ) 576 else: 577 assert False, 'Unknown block type (%s)' % block_type 578 579 return block_args, num_repeat 580
619 620 def decode_arch_def(arch_def, depth_multiplier=1.0, depth_trunc='ceil', experts_multiplier=1, fix_first_last=False): 621 arch_args = [] 622 for stack_idx, block_strings in enumerate(arch_def): 623 assert isinstance(block_strings, list) 624 stack_args = [] 625 repeats = [] 626 for block_str in block_strings:
623 assert isinstance(block_strings, list) 624 stack_args = [] 625 repeats = [] 626 for block_str in block_strings: 627 assert isinstance(block_str, str) 628 ba, rep = _decode_block_str(block_str) 629 if ba.get('num_experts', 0) > 0 and experts_multiplier > 1: 630 ba['num_experts'] *= experts_multiplier
26 graphs = [] 27 num_nodes = 0 28 for node in graph.node: 29 pn, gs = onnx.helper.printable_node(node, indent, subgraphs=True) 30 assert isinstance(gs, list) 31 content.append(pn) 32 graphs.extend(gs) 33 num_nodes += 1
9 # Get the long description from the README file 10 with open(path.join(here, 'README.md'), encoding='utf-8') as f: 11 long_description = f.read() 12 13 exec(open('geffnet/version.py').read()) 14 setup( 15 name='geffnet', 16 version=__version__,
44 outdir_inc = outdir + '-' + str(count) 45 while os.path.exists(outdir_inc): 46 count = count + 1 47 outdir_inc = outdir + '-' + str(count) 48 assert count < 100 49 outdir = outdir_inc 50 os.makedirs(outdir) 51 return outdir
295 }, 296 } 297 298 def createConvFunc(op_type): 299 assert op_type in ['cv', 'cd', 'ad', 'rd'], 'unknown op type: %s' % str(op_type) 300 if op_type == 'cv': 301 return F.conv2d 302
301 return F.conv2d 302 303 if op_type == 'cd': 304 def func(x, weights, bias=None, stride=1, padding=0, dilation=1, groups=1): 305 assert dilation in [1, 2], 'dilation for cd_conv should be in 1 or 2' 306 assert weights.size(2) == 3 and weights.size(3) == 3, 'kernel size for cd_conv should be 3x3' 307 assert padding == dilation, 'padding for cd_conv set wrong' 308
302 303 if op_type == 'cd': 304 def func(x, weights, bias=None, stride=1, padding=0, dilation=1, groups=1): 305 assert dilation in [1, 2], 'dilation for cd_conv should be in 1 or 2' 306 assert weights.size(2) == 3 and weights.size(3) == 3, 'kernel size for cd_conv should be 3x3' 307 assert padding == dilation, 'padding for cd_conv set wrong' 308 309 weights_c = weights.sum(dim=[2, 3], keepdim=True)
303 if op_type == 'cd': 304 def func(x, weights, bias=None, stride=1, padding=0, dilation=1, groups=1): 305 assert dilation in [1, 2], 'dilation for cd_conv should be in 1 or 2' 306 assert weights.size(2) == 3 and weights.size(3) == 3, 'kernel size for cd_conv should be 3x3' 307 assert padding == dilation, 'padding for cd_conv set wrong' 308 309 weights_c = weights.sum(dim=[2, 3], keepdim=True) 310 yc = F.conv2d(x, weights_c, stride=stride, padding=0, groups=groups)
312 return y - yc 313 return func 314 elif op_type == 'ad': 315 def func(x, weights, bias=None, stride=1, padding=0, dilation=1, groups=1): 316 assert dilation in [1, 2], 'dilation for ad_conv should be in 1 or 2' 317 assert weights.size(2) == 3 and weights.size(3) == 3, 'kernel size for ad_conv should be 3x3' 318 assert padding == dilation, 'padding for ad_conv set wrong' 319
313 return func 314 elif op_type == 'ad': 315 def func(x, weights, bias=None, stride=1, padding=0, dilation=1, groups=1): 316 assert dilation in [1, 2], 'dilation for ad_conv should be in 1 or 2' 317 assert weights.size(2) == 3 and weights.size(3) == 3, 'kernel size for ad_conv should be 3x3' 318 assert padding == dilation, 'padding for ad_conv set wrong' 319 320 shape = weights.shape
314 elif op_type == 'ad': 315 def func(x, weights, bias=None, stride=1, padding=0, dilation=1, groups=1): 316 assert dilation in [1, 2], 'dilation for ad_conv should be in 1 or 2' 317 assert weights.size(2) == 3 and weights.size(3) == 3, 'kernel size for ad_conv should be 3x3' 318 assert padding == dilation, 'padding for ad_conv set wrong' 319 320 shape = weights.shape 321 weights = weights.view(shape[0], shape[1], -1)
324 return y 325 return func 326 elif op_type == 'rd': 327 def func(x, weights, bias=None, stride=1, padding=0, dilation=1, groups=1): 328 assert dilation in [1, 2], 'dilation for rd_conv should be in 1 or 2' 329 assert weights.size(2) == 3 and weights.size(3) == 3, 'kernel size for rd_conv should be 3x3' 330 padding = 2 * dilation 331
325 return func 326 elif op_type == 'rd': 327 def func(x, weights, bias=None, stride=1, padding=0, dilation=1, groups=1): 328 assert dilation in [1, 2], 'dilation for rd_conv should be in 1 or 2' 329 assert weights.size(2) == 3 and weights.size(3) == 3, 'kernel size for rd_conv should be 3x3' 330 padding = 2 * dilation 331 332 shape = weights.shape
497 def __init__(self, inplane, pdcs, dil=None, sa=False, convert=False): 498 super(PiDiNet, self).__init__() 499 self.sa = sa 500 if dil is not None: 501 assert isinstance(dil, int), 'dil should be an int' 502 self.dil = dil 503 504 self.fuseplanes = []
647 return outputs 648 649 def config_model(model): 650 model_options = list(nets.keys()) 651 assert model in model_options, \ 652 'unrecognized model, please choose from %s' % str(model_options) 653 654 # print(str(nets[model])) 655
30 class Recolorizer: 31 def __call__(self, input_image=None, mode="luminance", gamma_correction=1.0, detect_resolution=512, output_type=None, upscale_method="INTER_CUBIC", **kwargs): 32 input_image, output_type = common_input_validate(input_image, output_type, **kwargs) 33 input_image, remove_pad = resize_image_with_pad(input_image, detect_resolution, upscale_method) 34 assert mode in recolor_methods.keys() 35 detected_map = recolor_methods[mode](input_image, gamma_correction) 36 detected_map = HWC3(remove_pad(detected_map)) 37 if output_type == "pil":
94 For large resolutions, 'binary_mask' may consume large amounts of 95 memory. 96 """ 97 98 assert (points_per_side is None) != ( 99 point_grids is None 100 ), "Exactly one of points_per_side or point_grid must be provided." 101 if points_per_side is not None: 102 self.point_grids = build_all_layer_point_grids( 103 points_per_side,
108 self.point_grids = point_grids 109 else: 110 raise ValueError("Can't have both points_per_side and point_grid be None.") 111 112 assert output_mode in [ 113 "binary_mask", 114 "uncompressed_rle", 115 "coco_rle", 116 ], f"Unknown output_mode {output_mode}." 117 if output_mode == "coco_rle": 118 from custom_pycocotools import mask as mask_utils # type: ignore # noqa: F401 119
213 self.proj = nn.Linear(dim, dim) 214 215 self.use_rel_pos = use_rel_pos 216 if self.use_rel_pos: 217 assert ( 218 input_size is not None 219 ), "Input size must be provided if using relative positional encoding." 220 # initialize relative positional embeddings 221 self.rel_pos_h = nn.Parameter(torch.zeros(2 * input_size[0] - 1, head_dim)) 222 self.rel_pos_w = nn.Parameter(torch.zeros(2 * input_size[1] - 1, head_dim))
215 resolution=(14, 14), 216 ): 217 super().__init__() 218 # (h, w) 219 assert isinstance(resolution, tuple) and len(resolution) == 2 220 self.num_heads = num_heads 221 self.scale = key_dim ** -0.5 222 self.key_dim = key_dim
306 super().__init__() 307 self.dim = dim 308 self.input_resolution = input_resolution 309 self.num_heads = num_heads 310 assert window_size > 0, 'window_size must be greater than 0' 311 self.window_size = window_size 312 self.mlp_ratio = mlp_ratio 313
313 314 self.drop_path = DropPath( 315 drop_path) if drop_path > 0. else nn.Identity() 316 317 assert dim % num_heads == 0, 'dim must be divisible by num_heads' 318 head_dim = dim // num_heads 319 320 window_resolution = (window_size, window_size)
332 333 def forward(self, x): 334 H, W = self.input_resolution 335 B, L, C = x.shape 336 assert L == H * W, "input feature has wrong size" 337 res_x = x 338 if H == self.window_size and W == self.window_size: 339 x = self.attn(x)
568 i += 1 569 if layer.downsample is not None: 570 layer.downsample.apply( 571 lambda x: _set_lr_scale(x, lr_scales[i - 1])) 572 assert i == depth 573 for m in [self.norm_head, self.head]: 574 m.apply(lambda x: _set_lr_scale(x, lr_scales[-1])) 575
577 p.param_name = k 578 579 def _check_lr_scale(m): 580 for p in m.parameters(): 581 assert hasattr(p, 'lr_scale'), p.param_name 582 583 self.apply(_check_lr_scale) 584
636 def fn_wrapper(pretrained=False, **kwargs): 637 model = fn() 638 if pretrained: 639 model_name = fn.__name__ 640 assert model_name in _provided_checkpoints, \ 641 f'Sorry that the checkpoint `{model_name}` is not provided yet.' 642 url = _checkpoint_url_format.format( 643 _provided_checkpoints[model_name]) 644 checkpoint = torch.hub.load_state_dict_from_url(
197 super().__init__() 198 self.embedding_dim = embedding_dim 199 self.internal_dim = embedding_dim // downsample_rate 200 self.num_heads = num_heads 201 assert self.internal_dim % num_heads == 0, "num_heads must divide embedding_dim." 202 203 self.q_proj = nn.Linear(embedding_dim, self.internal_dim) 204 self.k_proj = nn.Linear(embedding_dim, self.internal_dim)
44 image (np.ndarray): The image for calculating masks. Expects an 45 image in HWC uint8 format, with pixel values in [0, 255]. 46 image_format (str): The color format of the image, in ['RGB', 'BGR']. 47 """ 48 assert image_format in [ 49 "RGB", 50 "BGR", 51 ], f"image_format must be in ['RGB', 'BGR'], is {image_format}." 52 if image_format != self.model.image_format: 53 image = image[..., ::-1] 54
75 1x3xHxW, which has been transformed with ResizeLongestSide. 76 original_image_size (tuple(int, int)): The size of the image 77 before transformation, in (H, W) format. 78 """ 79 assert ( 80 len(transformed_image.shape) == 4 81 and transformed_image.shape[1] == 3 82 and max(*transformed_image.shape[2:]) == self.model.image_encoder.img_size 83 ), f"set_torch_image input must be BCHW with long side {self.model.image_encoder.img_size}." 84 self.reset_image() 85 86 self.original_size = original_image_size
135 136 # Transform input prompts 137 coords_torch, labels_torch, box_torch, mask_input_torch = None, None, None, None 138 if point_coords is not None: 139 assert ( 140 point_labels is not None 141 ), "point_labels must be supplied if point_coords is supplied." 142 point_coords = self.transform.apply_coords(point_coords, self.original_size) 143 coords_torch = torch.as_tensor(point_coords, dtype=torch.float, device=self.device) 144 labels_torch = torch.as_tensor(point_labels, dtype=torch.int, device=self.device)
251 if not self.is_image_set: 252 raise RuntimeError( 253 "An image must be set with .set_image(...) to generate an embedding." 254 ) 255 assert self.features is not None, "Features must exist if an image has been set." 256 return self.features 257 258 @property
20 """ 21 22 def __init__(self, **kwargs) -> None: 23 for v in kwargs.values(): 24 assert isinstance( 25 v, (list, np.ndarray, torch.Tensor) 26 ), "MaskData only supports list, numpy arrays, and torch tensors." 27 self._stats = dict(**kwargs) 28 29 def __setitem__(self, key: str, item: Any) -> None:
26 ), "MaskData only supports list, numpy arrays, and torch tensors." 27 self._stats = dict(**kwargs) 28 29 def __setitem__(self, key: str, item: Any) -> None: 30 assert isinstance( 31 item, (list, np.ndarray, torch.Tensor) 32 ), "MaskData only supports list, numpy arrays, and torch tensors." 33 self._stats[key] = item 34 35 def __delitem__(self, key: str) -> None:
95 return box_xywh 96 97 98 def batch_iterator(batch_size: int, *args) -> Generator[List[Any], None, None]: 99 assert len(args) > 0 and all( 100 len(a) == len(args[0]) for a in args 101 ), "Batched iteration must have inputs of all the same size." 102 n_batches = len(args[0]) // batch_size + int(len(args[0]) % batch_size != 0) 103 for b in range(n_batches): 104 yield [arg[b * batch_size : (b + 1) * batch_size] for arg in args]
272 mask and an indicator of if the mask has been modified. 273 """ 274 import cv2 # type: ignore 275 276 assert mode in ["holes", "islands"] 277 correct_holes = mode == "holes" 278 working_mask = (correct_holes ^ mask).astype(np.uint8) 279 n_labels, regions, stats, _ = cv2.connectedComponentsWithStats(working_mask, 8)
54 def __call__(self, img): 55 eps = 1e-5 56 X = img.astype(np.float32) 57 r, g, b = X[:, :, 0], X[:, :, 1], X[:, :, 2] 58 kr, kg, kb = [random.random() + eps for _ in range(3)] 59 ks = kr + kg + kb 60 kr /= ks 61 kg /= ks
119 self.up_factor = 2 120 self.constant_features = 16 121 122 layers = self.make_deconv_layers(in_features, up_scale) 123 assert layers is not None, layers 124 self.features = nn.Sequential(*layers) 125 126 def make_deconv_layers(self, in_features, up_scale):
237 238 def crop_bdcn(data1, h, w, crop_h, crop_w): 239 # Based on BDCN Implementation @ https://github.com/pkuCactus/BDCN 240 _, _, h1, w1 = data1.size() 241 assert (h <= h1 and w <= w1) 242 data = data1[:, :, crop_h:crop_h + h, crop_w:crop_w + w] 243 return data 244
243 return data 244 245 246 def forward(self, x, single_test=False): 247 assert x.ndim == 4, x.shape 248 # supose the image size is 352x352 249 250 # Block 1
39 return Processor(request.param) 40 41 42 def test_processor_init(processor): 43 assert isinstance(processor.processor, MODELS[processor.processor_id]['class']) 44 assert isinstance(processor.params, dict) 45 46
40 41 42 def test_processor_init(processor): 43 assert isinstance(processor.processor, MODELS[processor.processor_id]['class']) 44 assert isinstance(processor.params, dict) 45 46 47 def test_processor_call(processor):
62 W = int(np.round(W / 64.0)) * 64 63 64 # Test processing 65 processed_image = processor(image) 66 assert isinstance(processed_image, Image.Image) 67 assert processed_image.size == (W, H) 68 69
63 64 # Test processing 65 processed_image = processor(image) 66 assert isinstance(processed_image, Image.Image) 67 assert processed_image.size == (W, H) 68 69 70 def test_processor_call_bytes(processor):
73 image_bytes = f.read() 74 75 # Test processing 76 processed_image_bytes = processor(image_bytes, to_pil=False) 77 assert isinstance(processed_image_bytes, bytes) 78 assert len(processed_image_bytes) > 0
74 75 # Test processing 76 processed_image_bytes = processor(image_bytes, to_pil=False) 77 assert isinstance(processed_image_bytes, bytes) 78 assert len(processed_image_bytes) > 0
184 def get_external_models(): 185 mmcv_home = _get_mmcv_home() 186 default_json_path = osp.join(mmcv.__path__[0], 'model_zoo/open_mmlab.json') 187 default_urls = load_file(default_json_path) 188 assert isinstance(default_urls, dict) 189 external_json_path = osp.join(mmcv_home, 'open_mmlab.json') 190 if osp.exists(external_json_path): 191 external_urls = load_file(external_json_path)
188 assert isinstance(default_urls, dict) 189 external_json_path = osp.join(mmcv_home, 'open_mmlab.json') 190 if osp.exists(external_json_path): 191 external_urls = load_file(external_json_path) 192 assert isinstance(external_urls, dict) 193 default_urls.update(external_urls) 194 195 return default_urls
205 def get_deprecated_model_names(): 206 deprecate_json_path = osp.join(mmcv.__path__[0], 207 'model_zoo/deprecated.json') 208 deprecate_urls = load_file(deprecate_json_path) 209 assert isinstance(deprecate_urls, dict) 210 211 return deprecate_urls 212
43 nearest_size = [int(np.ceil(image1.size(-2) / padding_factor)) * padding_factor, 44 int(np.ceil(image1.size(-1) / padding_factor)) * padding_factor] 45 # resize to nearest size or specified size 46 inference_size = nearest_size if fixed_inference_size is None else fixed_inference_size 47 assert isinstance(inference_size, list) or isinstance(inference_size, tuple) 48 ori_size = image1.shape[-2:] 49 50 # resize before inference
82 vis_image = flow_to_image(flow) 83 84 # also predict backward flow 85 if pred_bidir_flow: 86 assert flow_pr.size(0) == 2 # [2, H, W, 2] 87 flow_bwd = flow_pr[1].permute(1, 2, 0).cpu().numpy() # [H, W, 2] 88 vis_image = flow_to_image(flow_bwd) 89 flow = flow_bwd
158 for key in list(MODEL_CONFIGS.keys())[::-1]: 159 if key in filename: 160 config_args = MODEL_CONFIGS[key] 161 break 162 assert config_args, f"Couldn't find hardcoded Unimatch config for {filename}" 163 164 model = UniMatch(feature_channels=config_args.feature_channels, 165 num_scales=config_args.num_scales,
179 self.device = device 180 return self 181 182 def __call__(self, image1, image2, detect_resolution=512, output_type="pil", upscale_method="INTER_CUBIC", pred_bwd_flow=False, pred_bidir_flow=False, **kwargs): 183 assert image1.shape == image2.shape, f"[Unimatch] image1 and image2 must have the same size, got {image1.shape} and {image2.shape}" 184 185 image1, output_type = common_input_validate(image1, output_type, **kwargs) 186 #image1, remove_pad = resize_image_with_pad(image1, detect_resolution, upscale_method)
6 7 8 def single_head_full_attention(q, k, v): 9 # q, k, v: [B, L, C] 10 assert q.dim() == k.dim() == v.dim() == 3 11 12 scores = torch.matmul(q, k.permute(0, 2, 1)) / (q.size(2) ** .5) # [B, L, L] 13 attn = torch.softmax(scores, dim=2) # [B, L, L]
21 w=None, 22 ): 23 # q, k, v: [B, L, C] 24 25 assert h is not None and w is not None 26 assert q.size(1) == h * w 27 28 b, _, c = q.size()
22 ): 23 # q, k, v: [B, L, C] 24 25 assert h is not None and w is not None 26 assert q.size(1) == h * w 27 28 b, _, c = q.size() 29
50 attn_mask=None, 51 ): 52 # ref: https://github.com/microsoft/Swin-Transformer/blob/main/models/swin_transformer.py 53 # q, k, v: [B, L, C] 54 assert q.dim() == k.dim() == v.dim() == 3 55 56 assert h is not None and w is not None 57 assert q.size(1) == h * w
52 # ref: https://github.com/microsoft/Swin-Transformer/blob/main/models/swin_transformer.py 53 # q, k, v: [B, L, C] 54 assert q.dim() == k.dim() == v.dim() == 3 55 56 assert h is not None and w is not None 57 assert q.size(1) == h * w 58 59 b, _, c = q.size()
53 # q, k, v: [B, L, C] 54 assert q.dim() == k.dim() == v.dim() == 3 55 56 assert h is not None and w is not None 57 assert q.size(1) == h * w 58 59 b, _, c = q.size() 60
69 70 scale_factor = c ** 0.5 71 72 if with_shift: 73 assert attn_mask is not None # compute once 74 shift_size_h = window_size_h // 2 75 shift_size_w = window_size_w // 2 76
113 attn_mask=None, 114 ): 115 # q, k, v: [B, L, C] 116 117 assert h is not None and w is not None 118 assert q.size(1) == h * w 119 120 b, _, c = q.size()
114 ): 115 # q, k, v: [B, L, C] 116 117 assert h is not None and w is not None 118 assert q.size(1) == h * w 119 120 b, _, c = q.size() 121
129 130 scale_factor = c ** 0.5 131 132 if with_shift: 133 assert attn_mask is not None # compute once 134 shift_size_w = window_size_w // 2 135 136 q = torch.roll(q, shifts=-shift_size_w, dims=1)
216 217 def forward_local_window_attn(self, feature0, flow, 218 local_window_radius=1, 219 ): 220 assert flow.size(1) == 2 or flow.size(1) == 1 # flow or disparity or depth 221 assert local_window_radius > 0 222 223 b, c, h, w = feature0.size()
217 def forward_local_window_attn(self, feature0, flow, 218 local_window_radius=1, 219 ): 220 assert flow.size(1) == 2 or flow.size(1) == 1 # flow or disparity or depth 221 assert local_window_radius > 0 222 223 b, c, h, w = feature0.size() 224
21 return grid 22 23 24 def generate_window_grid(h_min, h_max, w_min, w_max, len_h, len_w, device=None): 25 assert device is not None 26 27 x, y = torch.meshgrid([torch.linspace(w_min, w_max, len_w, device=device), 28 torch.linspace(h_min, h_max, len_h, device=device)],
63 64 65 def flow_warp(feature, flow, mask=False, padding_mode='zeros'): 66 b, c, h, w = feature.size() 67 assert flow.size(1) == 2 68 69 grid = coords_grid(b, h, w).to(flow.device) + flow # [B, 2, H, W] 70
77 beta=0.5 78 ): 79 # fwd_flow, bwd_flow: [B, 2, H, W] 80 # alpha and beta values are following UnFlow (https://arxiv.org/abs/1711.07837) 81 assert fwd_flow.dim() == 4 and bwd_flow.dim() == 4 82 assert fwd_flow.size(1) == 2 and bwd_flow.size(1) == 2 83 flow_mag = torch.norm(fwd_flow, dim=1) + torch.norm(bwd_flow, dim=1) # [B, H, W] 84
78 ): 79 # fwd_flow, bwd_flow: [B, 2, H, W] 80 # alpha and beta values are following UnFlow (https://arxiv.org/abs/1711.07837) 81 assert fwd_flow.dim() == 4 and bwd_flow.dim() == 4 82 assert fwd_flow.size(1) == 2 and bwd_flow.size(1) == 2 83 flow_mag = torch.norm(fwd_flow, dim=1) + torch.norm(bwd_flow, dim=1) # [B, H, W] 84 85 warped_bwd_flow = flow_warp(bwd_flow, fwd_flow) # [B, 2, H, W]
106 if not isinstance(flow, float): 107 sample_coords = sample_coords + flow.view( 108 b, 2, -1).permute(0, 2, 1).unsqueeze(-2) # [B, H*W, (2R+1)^2, 2] 109 else: 110 assert flow == 0. 111 112 # normalize coordinates to [-1, 1] 113 sample_coords_norm = normalize_coords(sample_coords, h, w) # [-1, 1]
207 depth_from_argmax=False, 208 pred_bidir_depth=False, 209 ): 210 b, c, h, w = feature0.size() 211 assert depth_candidates.dim() == 4 # [B, D, H, W] 212 scale_factor = c ** 0.5 213 214 if pred_bidir_depth:
245 pose: [B, 4, 4] 246 depth: [B, D, H, W] 247 """ 248 249 assert intrinsics.size(1) == intrinsics.size(2) == 3 250 assert pose.size(1) == pose.size(2) == 4 251 assert depth.dim() == 4 252
246 depth: [B, D, H, W] 247 """ 248 249 assert intrinsics.size(1) == intrinsics.size(2) == 3 250 assert pose.size(1) == pose.size(2) == 4 251 assert depth.dim() == 4 252 253 b, d, h, w = depth.size()
247 """ 248 249 assert intrinsics.size(1) == intrinsics.size(2) == 3 250 assert pose.size(1) == pose.size(2) == 4 251 assert depth.dim() == 4 252 253 b, d, h, w = depth.size() 254 c = feature1.size(1)
115 # full 2d attn 116 message = single_head_full_attention(query, key, value) # [N, L, C] 117 else: 118 if attn_num_splits > 1: 119 assert shifted_window_attn_mask_1d is not None 120 # cross attn 1d shift 121 message = single_head_split_window_attention_1d(query, key, value, 122 num_splits=attn_num_splits,
229 **kwargs, 230 ): 231 232 b, c, h, w = feature0.shape 233 assert self.d_model == c 234 235 feature0 = feature0.flatten(-2).permute(0, 2, 1) # [B, H*W, C] 236 feature1 = feature1.flatten(-2).permute(0, 2, 1) # [B, H*W, C]
46 self.test_branch_idx = test_branch_idx 47 self.norm = norm 48 self.activation = activation 49 50 assert len({self.num_branch, len(self.paddings), len(self.strides)}) == 1 51 52 self.weight = nn.Parameter( 53 torch.Tensor(out_channels, in_channels // groups, *self.kernel_size)
62 nn.init.constant_(self.bias, 0) 63 64 def forward(self, inputs): 65 num_branch = self.num_branch if self.training or self.test_branch_idx == -1 else 1 66 assert len(inputs) == num_branch 67 68 if self.training or self.test_branch_idx == -1: 69 outputs = [
110 **kwargs, 111 ): 112 113 if pred_bidir_flow: 114 assert task == 'flow' 115 116 if task == 'depth': 117 assert self.num_scales == 1 # multi-scale depth model is not supported yet
113 if pred_bidir_flow: 114 assert task == 'flow' 115 116 if task == 'depth': 117 assert self.num_scales == 1 # multi-scale depth model is not supported yet 118 119 results_dict = {} 120 flow_preds = []
128 129 flow = None 130 131 if task != 'depth': 132 assert len(attn_splits_list) == len(corr_radius_list) == len(prop_radius_list) == self.num_scales 133 else: 134 assert len(attn_splits_list) == len(prop_radius_list) == self.num_scales == 1 135
130 131 if task != 'depth': 132 assert len(attn_splits_list) == len(corr_radius_list) == len(prop_radius_list) == self.num_scales 133 else: 134 assert len(attn_splits_list) == len(prop_radius_list) == self.num_scales == 1 135 136 for scale_idx in range(self.num_scales): 137 feature0, feature1 = feature0_list[scale_idx], feature1_list[scale_idx]
149 intrinsics_curr = intrinsics.clone() 150 intrinsics_curr[:, :2] = intrinsics_curr[:, :2] / upsample_factor 151 152 if scale_idx > 0: 153 assert task != 'depth' # not supported for multi-scale depth model 154 flow = F.interpolate(flow, scale_factor=2, mode='bilinear', align_corners=True) * 2 155 156 if flow is not None:
153 assert task != 'depth' # not supported for multi-scale depth model 154 flow = F.interpolate(flow, scale_factor=2, mode='bilinear', align_corners=True) * 2 155 156 if flow is not None: 157 assert task != 'depth' 158 flow = flow.detach() 159 160 if task == 'stereo':
268 upsample_factor=upsample_factor, 269 is_depth=task == 'depth') 270 flow_preds.append(flow_up) 271 272 assert num_reg_refine > 0 273 for refine_iter_idx in range(num_reg_refine): 274 flow = flow.detach() 275
3 from .position import PositionEmbeddingSine 4 5 6 def generate_window_grid(h_min, h_max, w_min, w_max, len_h, len_w, device=None): 7 assert device is not None 8 9 x, y = torch.meshgrid([torch.linspace(w_min, w_max, len_w, device=device), 10 torch.linspace(h_min, h_max, len_h, device=device)],
36 channel_last=False, 37 ): 38 if channel_last: # [B, H, W, C] 39 b, h, w, c = feature.size() 40 assert h % num_splits == 0 and w % num_splits == 0 41 42 b_new = b * num_splits * num_splits 43 h_new = h // num_splits
46 feature = feature.view(b, num_splits, h // num_splits, num_splits, w // num_splits, c 47 ).permute(0, 1, 3, 2, 4, 5).reshape(b_new, h_new, w_new, c) # [B*K*K, H/K, W/K, C] 48 else: # [B, C, H, W] 49 b, c, h, w = feature.size() 50 assert h % num_splits == 0 and w % num_splits == 0 51 52 b_new = b * num_splits * num_splits 53 h_new = h // num_splits
156 num_splits=2, 157 ): 158 # feature: [B, W, C] 159 b, w, c = feature.size() 160 assert w % num_splits == 0 161 162 b_new = b * num_splits 163 w_new = w // num_splits
1 # Copyright (c) OpenMMLab. All rights reserved. 2 # https://github.com/open-mmlab/mmcv/blob/7540cf73ac7e5d1e14d0ffbd9b6759e83929ecfc/mmcv/runner/dist_utils.py 3 4 import os 5 import subprocess 6 7 import torch 8 import torch.multiprocessing as mp
51 ntasks = int(os.environ['SLURM_NTASKS']) 52 node_list = os.environ['SLURM_NODELIST'] 53 num_gpus = torch.cuda.device_count() 54 torch.cuda.set_device(proc_id % num_gpus) 55 addr = subprocess.getoutput( 56 f'scontrol show hostname {node_list} | head -n1') 57 # specify master port 58 if port is not None: 59 os.environ['MASTER_PORT'] = str(port)
201 202 203 def extract_video(video_name): 204 cap = cv2.VideoCapture(video_name) 205 assert cap.isOpened(), f'Failed to load video file {video_name}' 206 # get video info 207 size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), 208 int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
117 :param clip_flow: float, maximum clipping value for flow 118 :return: 119 ''' 120 121 assert flow_uv.ndim == 3, 'input flow must have three dimensions' 122 assert flow_uv.shape[2] == 2, 'input flow must have shape [H,W,2]' 123 124 if clip_flow is not None:
118 :return: 119 ''' 120 121 assert flow_uv.ndim == 3, 'input flow must have three dimensions' 122 assert flow_uv.shape[2] == 2, 'input flow must have shape [H,W,2]' 123 124 if clip_flow is not None: 125 flow_uv = np.clip(flow_uv, 0, clip_flow)
76 """ 77 nBands = 2 78 79 if v is None: 80 assert (uv.ndim == 3) 81 assert (uv.shape[2] == 2) 82 u = uv[:, :, 0] 83 v = uv[:, :, 1]
77 nBands = 2 78 79 if v is None: 80 assert (uv.ndim == 3) 81 assert (uv.shape[2] == 2) 82 u = uv[:, :, 0] 83 v = uv[:, :, 1] 84 else:
83 v = uv[:, :, 1] 84 else: 85 u = uv 86 87 assert (u.shape == v.shape) 88 height, width = u.shape 89 f = open(filename, 'wb') 90 # write the header
143 # In G, flow along x-axis normalized by image width and quantized to [0;2^16 – 1] 144 # B = 0 for invalid flow (e.g., sky pixels) 145 bgr = cv2.imread(filename, cv2.IMREAD_ANYCOLOR | cv2.IMREAD_ANYDEPTH) 146 h, w, _c = bgr.shape 147 assert bgr.dtype == np.uint16 and _c == 3 148 # b == invalid flow flag == 0 for sky or other invalid flow 149 invalid = bgr[:, :, 0] == 0 150 # g,r == flow_y,x normalized by height,width and scaled to [0;2**16 – 1]
36 xgrid, ygrid = coords.split([1, 1], dim=-1) 37 38 # To handle H or W equals to 1 by explicitly defining height and width 39 if H == 1: 40 assert ygrid.abs().max() < 1e-8 41 H = 10 42 if W == 1: 43 assert xgrid.abs().max() < 1e-8
39 if H == 1: 40 assert ygrid.abs().max() < 1e-8 41 H = 10 42 if W == 1: 43 assert xgrid.abs().max() < 1e-8 44 W = 10 45 46 xgrid = 2 * xgrid / (W - 1) - 1
77 78 79 def compute_out_of_boundary_mask(flow, downsample_factor=None): 80 # flow: [B, 2, H, W] 81 assert flow.dim() == 4 and flow.size(1) == 2 82 b, _, h, w = flow.shape 83 init_coords = coords_grid(b, h, w).to(flow.device) 84 corres = init_coords + flow # [B, 2, H, W]
83 init_coords = coords_grid(b, h, w).to(flow.device) 84 corres = init_coords + flow # [B, 2, H, W] 85 86 if downsample_factor is not None: 87 assert w % downsample_factor == 0 and h % downsample_factor == 0 88 # the actual max disp can predict is in the downsampled feature resolution, then upsample 89 max_w = (w // downsample_factor - 1) * downsample_factor 90 max_h = (h // downsample_factor - 1) * downsample_factor
107 """Normalize coordinates of image scale to [-1, 1] 108 Args: 109 grid: [B, 2, H, W] 110 """ 111 assert grid.size(1) == 2 112 h, w = grid.size()[2:] 113 grid[:, 0, :, :] = 2 * (grid[:, 0, :, :].clone() / (w - 1)) - 1 # x: [-1, 1] 114 grid[:, 1, :, :] = 2 * (grid[:, 1, :, :].clone() / (h - 1)) - 1 # y: [-1, 1]
117 118 119 def flow_warp(feature, flow, mask=False, padding_mode='zeros'): 120 b, c, h, w = feature.size() 121 assert flow.size(1) == 2 122 123 grid = coords_grid(b, h, w).to(flow.device) + flow # [B, 2, H, W] 124
130 return 8 * F.interpolate(flow, size=new_size, mode=mode, align_corners=True) 131 132 133 def bilinear_upflow(flow, scale_factor=8): 134 assert flow.size(1) == 2 135 flow = F.interpolate(flow, scale_factor=scale_factor, 136 mode='bilinear', align_corners=True) * scale_factor 137
90 91 92 def viz_depth_tensor(disp, return_numpy=False, colormap='plasma'): 93 # visualize inverse depth 94 assert isinstance(disp, torch.Tensor) 95 96 disp = disp.numpy() 97 vmax = np.percentile(disp, 95)
51 52 here = Path(__file__).parent.resolve() 53 54 def HWC3(x): 55 assert x.dtype == np.uint8 56 if x.ndim == 2: 57 x = x[:, :, None] 58 assert x.ndim == 3
54 def HWC3(x): 55 assert x.dtype == np.uint8 56 if x.ndim == 2: 57 x = x[:, :, None] 58 assert x.ndim == 3 59 H, W, C = x.shape 60 assert C == 1 or C == 3 or C == 4 61 if C == 3:
56 if x.ndim == 2: 57 x = x[:, :, None] 58 assert x.ndim == 3 59 H, W, C = x.shape 60 assert C == 1 or C == 3 or C == 4 61 if C == 3: 62 return x 63 if C == 1:
113 return y 114 115 116 def img2mask(img, H, W, low=10, high=90): 117 assert img.ndim == 3 or img.ndim == 2 118 assert img.dtype == np.uint8 119 120 if img.ndim == 3:
114 115 116 def img2mask(img, H, W, low=10, high=90): 117 assert img.ndim == 3 or img.ndim == 2 118 assert img.dtype == np.uint8 119 120 if img.ndim == 3: 121 y = img[:, :, random.randrange(0, img.shape[2])]
117 assert img.ndim == 3 or img.ndim == 2 118 assert img.dtype == np.uint8 119 120 if img.ndim == 3: 121 y = img[:, :, random.randrange(0, img.shape[2])] 122 else: 123 y = img 124
123 y = img 124 125 y = cv2.resize(y, (W, H), interpolation=cv2.INTER_CUBIC) 126 127 if random.uniform(0, 1) < 0.5: 128 y = 255 - y 129 130 return y < np.percentile(y, random.randrange(low, high))
126 127 if random.uniform(0, 1) < 0.5: 128 y = 255 - y 129 130 return y < np.percentile(y, random.randrange(low, high)) 131 132 def safer_memory(x): 133 # Fix many MAC/AMD problems
134 return np.ascontiguousarray(x.copy()).copy() 135 136 UPSCALE_METHODS = ["INTER_NEAREST", "INTER_LINEAR", "INTER_AREA", "INTER_CUBIC", "INTER_LANCZOS4"] 137 def get_upscale_method(method_str): 138 assert method_str in UPSCALE_METHODS, f"Method {method_str} not found in {UPSCALE_METHODS}" 139 return getattr(cv2, method_str) 140 141 def pad64(x):
271 download_url_to_file(url = model_url, dst = model_path) 272 except: 273 warnings.warn(f"SSL verify failed, try use HTTP instead. {filename}'s hash will be checked") 274 download_url_to_file(url = model_url, dst = model_path) 275 assert check_hash_from_torch_hub(model_path, filename), f"Hash check failed as file {filename} is corrupted" 276 print("Hash check passed") 277 278 print(f"model_path is {model_path}")
351 return depth_anything_core 352 353 @staticmethod 354 def parse_img_size(config): 355 assert 'img_size' in config 356 if isinstance(config['img_size'], str): 357 assert "," in config['img_size'], "img_size should be a string with comma separated img_size=H,W" 358 config['img_size'] = list(map(int, config['img_size'].split(",")))
353 @staticmethod 354 def parse_img_size(config): 355 assert 'img_size' in config 356 if isinstance(config['img_size'], str): 357 assert "," in config['img_size'], "img_size should be a string with comma separated img_size=H,W" 358 config['img_size'] = list(map(int, config['img_size'].split(","))) 359 assert len( 360 config['img_size']) == 2, "img_size should be a string with comma separated img_size=H,W"
355 assert 'img_size' in config 356 if isinstance(config['img_size'], str): 357 assert "," in config['img_size'], "img_size should be a string with comma separated img_size=H,W" 358 config['img_size'] = list(map(int, config['img_size'].split(","))) 359 assert len( 360 config['img_size']) == 2, "img_size should be a string with comma separated img_size=H,W" 361 elif isinstance(config['img_size'], int): 362 config['img_size'] = [config['img_size'], config['img_size']] 363 else:
360 config['img_size']) == 2, "img_size should be a string with comma separated img_size=H,W" 361 elif isinstance(config['img_size'], int): 362 config['img_size'] = [config['img_size'], config['img_size']] 363 else: 364 assert isinstance(config['img_size'], list) and len( 365 config['img_size']) == 2, "img_size should be a list of H,W" 366 return config 367 368
357 return MidasCore.build(**config) 358 359 @staticmethod 360 def parse_img_size(config): 361 assert 'img_size' in config 362 if isinstance(config['img_size'], str): 363 assert "," in config['img_size'], "img_size should be a string with comma separated img_size=H,W" 364 config['img_size'] = list(map(int, config['img_size'].split(",")))
359 @staticmethod 360 def parse_img_size(config): 361 assert 'img_size' in config 362 if isinstance(config['img_size'], str): 363 assert "," in config['img_size'], "img_size should be a string with comma separated img_size=H,W" 364 config['img_size'] = list(map(int, config['img_size'].split(","))) 365 assert len( 366 config['img_size']) == 2, "img_size should be a string with comma separated img_size=H,W"
361 assert 'img_size' in config 362 if isinstance(config['img_size'], str): 363 assert "," in config['img_size'], "img_size should be a string with comma separated img_size=H,W" 364 config['img_size'] = list(map(int, config['img_size'].split(","))) 365 assert len( 366 config['img_size']) == 2, "img_size should be a string with comma separated img_size=H,W" 367 elif isinstance(config['img_size'], int): 368 config['img_size'] = [config['img_size'], config['img_size']] 369 else:
366 config['img_size']) == 2, "img_size should be a string with comma separated img_size=H,W" 367 elif isinstance(config['img_size'], int): 368 config['img_size'] = [config['img_size'], config['img_size']] 369 else: 370 assert isinstance(config['img_size'], list) and len( 371 config['img_size']) == 2, "img_size should be a list of H,W" 372 return config 373 374
72 Returns: 73 torch.Tensor: output tensor of shape (b, 1, h, w) 74 """ 75 # assert x is nchw and c = 3 76 assert x.dim() == 4, "x must be 4 dimensional, got {}".format(x.dim()) 77 assert x.shape[1] == 3, "x must have 3 channels, got {}".format(x.shape[1]) 78 79 if pad_input:
73 torch.Tensor: output tensor of shape (b, 1, h, w) 74 """ 75 # assert x is nchw and c = 3 76 assert x.dim() == 4, "x must be 4 dimensional, got {}".format(x.dim()) 77 assert x.shape[1] == 3, "x must have 3 channels, got {}".format(x.shape[1]) 78 79 if pad_input: 80 assert fh > 0 or fw > 0, "atlease one of fh and fw must be greater than 0"
76 assert x.dim() == 4, "x must be 4 dimensional, got {}".format(x.dim()) 77 assert x.shape[1] == 3, "x must have 3 channels, got {}".format(x.shape[1]) 78 79 if pad_input: 80 assert fh > 0 or fw > 0, "atlease one of fh and fw must be greater than 0" 81 pad_h = int(np.sqrt(x.shape[2]/2) * fh) 82 pad_w = int(np.sqrt(x.shape[3]/2) * fw) 83 padding = [pad_w, pad_w]
240 core = MidasCore.build(midas_model_type=midas_model_type, use_pretrained_midas=use_pretrained_midas, 241 train_midas=train_midas, fetch_features=True, freeze_bn=freeze_midas_bn, **kwargs) 242 model = ZoeDepth(core, **kwargs) 243 if pretrained_resource: 244 assert isinstance(pretrained_resource, str), "pretrained_resource must be a string" 245 model = load_state_from_resource(model, pretrained_resource) 246 return model 247
254 train_midas=train_midas, fetch_features=True, freeze_bn=freeze_midas_bn, **kwargs) 255 256 model = ZoeDepth(core, **kwargs) 257 if pretrained_resource: 258 assert isinstance(pretrained_resource, str), "pretrained_resource must be a string" 259 model = load_state_from_resource(model, pretrained_resource) 260 return model 261
323 core = MidasCore.build(midas_model_type=midas_model_type, use_pretrained_midas=use_pretrained_midas, 324 train_midas=train_midas, fetch_features=True, freeze_bn=freeze_midas_bn, **kwargs) 325 model = ZoeDepthNK(core, **kwargs) 326 if pretrained_resource: 327 assert isinstance(pretrained_resource, str), "pretrained_resource must be a string" 328 model = load_state_from_resource(model, pretrained_resource) 329 return model 330
284 for key, value in kwargs.items(): 285 if key.startswith("__"): 286 keys = key.split("__")[1:] 287 values = value.split(";") 288 assert len(keys) == len( 289 values), f"Combined arguments should have equal number of keys and values. Keys are separated by '__' and Values are separated with ';'. For example, '__n_bins__lr=256;0.001. Given (keys,values) is ({keys}, {values})" 290 for k, v in zip(keys, values): 291 new_kwargs[k] = v 292 return new_kwargs
298 """ 299 if key in config: 300 if isinstance(config[key], str): 301 config[key] = list(map(dtype, config[key].split(','))) 302 assert isinstance(config[key], list) and all([isinstance(e, dtype) for e in config[key]] 303 ), f"{key} should be a list of values dtype {dtype}. Given {config[key]} of type {type(config[key])} with values of type {[type(e) for e in config[key]]}." 304 305 306 def get_model_config(model_name, model_version=None):
40 def apply(self, img: np.ndarray, ksize: int = 3, **params) -> np.ndarray: 41 return F.blur(img, ksize) 42 43 def get_params(self) -> Dict[str, Any]: 44 return {"ksize": int(random.choice(list(range(self.blur_limit[0], self.blur_limit[1] + 1, 2))))} 45 46 def get_transform_init_args_names(self) -> Tuple[str, ...]: 47 return ("blur_limit",)
83 def apply(self, img: np.ndarray, kernel: np.ndarray = None, **params) -> np.ndarray: # type: ignore 84 return FMain.convolve(img, kernel=kernel) 85 86 def get_params(self) -> Dict[str, Any]: 87 ksize = random.choice(list(range(self.blur_limit[0], self.blur_limit[1] + 1, 2))) 88 if ksize <= 2: 89 raise ValueError("ksize must be > 2. Got: {}".format(ksize)) 90 kernel = np.zeros((ksize, ksize), dtype=np.uint8)
87 ksize = random.choice(list(range(self.blur_limit[0], self.blur_limit[1] + 1, 2))) 88 if ksize <= 2: 89 raise ValueError("ksize must be > 2. Got: {}".format(ksize)) 90 kernel = np.zeros((ksize, ksize), dtype=np.uint8) 91 x1, x2 = random.randint(0, ksize - 1), random.randint(0, ksize - 1) 92 if x1 == x2: 93 y1, y2 = random.sample(range(ksize), 2) 94 else:
87 ksize = random.choice(list(range(self.blur_limit[0], self.blur_limit[1] + 1, 2))) 88 if ksize <= 2: 89 raise ValueError("ksize must be > 2. Got: {}".format(ksize)) 90 kernel = np.zeros((ksize, ksize), dtype=np.uint8) 91 x1, x2 = random.randint(0, ksize - 1), random.randint(0, ksize - 1) 92 if x1 == x2: 93 y1, y2 = random.sample(range(ksize), 2) 94 else:
91 x1, x2 = random.randint(0, ksize - 1), random.randint(0, ksize - 1) 92 if x1 == x2: 93 y1, y2 = random.sample(range(ksize), 2) 94 else: 95 y1, y2 = random.randint(0, ksize - 1), random.randint(0, ksize - 1) 96 97 def make_odd_val(v1, v2): 98 len_v = abs(v1 - v2) + 1
91 x1, x2 = random.randint(0, ksize - 1), random.randint(0, ksize - 1) 92 if x1 == x2: 93 y1, y2 = random.sample(range(ksize), 2) 94 else: 95 y1, y2 = random.randint(0, ksize - 1), random.randint(0, ksize - 1) 96 97 def make_odd_val(v1, v2): 98 len_v = abs(v1 - v2) + 1
194 def apply(self, img: np.ndarray, ksize: int = 3, sigma: float = 0, **params) -> np.ndarray: 195 return F.gaussian_blur(img, ksize, sigma=sigma) 196 197 def get_params(self) -> Dict[str, float]: 198 ksize = random.randrange(self.blur_limit[0], self.blur_limit[1] + 1) 199 if ksize != 0 and ksize % 2 != 1: 200 ksize = (ksize + 1) % (self.blur_limit[1] + 1) 201
198 ksize = random.randrange(self.blur_limit[0], self.blur_limit[1] + 1) 199 if ksize != 0 and ksize % 2 != 1: 200 ksize = (ksize + 1) % (self.blur_limit[1] + 1) 201 202 return {"ksize": ksize, "sigma": random.uniform(*self.sigma_limit)} 203 204 def get_transform_init_args_names(self) -> Tuple[str, str]: 205 return ("blur_limit", "sigma_limit")
248 self.iterations = iterations 249 self.mode = mode 250 251 def apply(self, img: np.ndarray, dxy: np.ndarray = None, **params) -> np.ndarray: # type: ignore 252 assert dxy is not None 253 return F.glass_blur(img, self.sigma, self.max_delta, self.iterations, dxy, self.mode) 254 255 def get_params_dependent_on_targets(self, params: Dict[str, Any]) -> Dict[str, np.ndarray]:
344 def apply(self, img: np.ndarray, kernel: np.ndarray = np.array(None), **params) -> np.ndarray: 345 return FMain.convolve(img, kernel=kernel) 346 347 def get_params(self) -> Dict[str, np.ndarray]: 348 ksize = random.randrange(self.blur_limit[0], self.blur_limit[1] + 1, 2) 349 sigmaX = random.uniform(*self.sigmaX_limit) 350 sigmaY = random.uniform(*self.sigmaY_limit) 351 angle = np.deg2rad(random.uniform(*self.rotate_limit))
345 return FMain.convolve(img, kernel=kernel) 346 347 def get_params(self) -> Dict[str, np.ndarray]: 348 ksize = random.randrange(self.blur_limit[0], self.blur_limit[1] + 1, 2) 349 sigmaX = random.uniform(*self.sigmaX_limit) 350 sigmaY = random.uniform(*self.sigmaY_limit) 351 angle = np.deg2rad(random.uniform(*self.rotate_limit)) 352
346 347 def get_params(self) -> Dict[str, np.ndarray]: 348 ksize = random.randrange(self.blur_limit[0], self.blur_limit[1] + 1, 2) 349 sigmaX = random.uniform(*self.sigmaX_limit) 350 sigmaY = random.uniform(*self.sigmaY_limit) 351 angle = np.deg2rad(random.uniform(*self.rotate_limit)) 352 353 # Split into 2 cases to avoid selection of narrow kernels (beta > 1) too often.
347 def get_params(self) -> Dict[str, np.ndarray]: 348 ksize = random.randrange(self.blur_limit[0], self.blur_limit[1] + 1, 2) 349 sigmaX = random.uniform(*self.sigmaX_limit) 350 sigmaY = random.uniform(*self.sigmaY_limit) 351 angle = np.deg2rad(random.uniform(*self.rotate_limit)) 352 353 # Split into 2 cases to avoid selection of narrow kernels (beta > 1) too often. 354 if random.random() < 0.5:
350 sigmaY = random.uniform(*self.sigmaY_limit) 351 angle = np.deg2rad(random.uniform(*self.rotate_limit)) 352 353 # Split into 2 cases to avoid selection of narrow kernels (beta > 1) too often. 354 if random.random() < 0.5: 355 beta = random.uniform(self.beta_limit[0], 1) 356 else: 357 beta = random.uniform(1, self.beta_limit[1])
351 angle = np.deg2rad(random.uniform(*self.rotate_limit)) 352 353 # Split into 2 cases to avoid selection of narrow kernels (beta > 1) too often. 354 if random.random() < 0.5: 355 beta = random.uniform(self.beta_limit[0], 1) 356 else: 357 beta = random.uniform(1, self.beta_limit[1]) 358
353 # Split into 2 cases to avoid selection of narrow kernels (beta > 1) too often. 354 if random.random() < 0.5: 355 beta = random.uniform(self.beta_limit[0], 1) 356 else: 357 beta = random.uniform(1, self.beta_limit[1]) 358 359 noise_matrix = random_utils.uniform(self.noise_limit[0], self.noise_limit[1], size=[ksize, ksize]) 360
473 if self.step_factor[0] <= 0: 474 raise ValueError("Step factor must be positive") 475 476 def apply(self, img: np.ndarray, zoom_factors: np.ndarray = np.array(None), **params) -> np.ndarray: 477 assert zoom_factors is not None 478 return F.zoom_blur(img, zoom_factors) 479 480 def get_params(self) -> Dict[str, Any]:
477 assert zoom_factors is not None 478 return F.zoom_blur(img, zoom_factors) 479 480 def get_params(self) -> Dict[str, Any]: 481 max_factor = random.uniform(self.max_factor[0], self.max_factor[1]) 482 step_factor = random.uniform(self.step_factor[0], self.step_factor[1]) 483 return {"zoom_factors": np.arange(1.0, max_factor, step_factor)} 484
478 return F.zoom_blur(img, zoom_factors) 479 480 def get_params(self) -> Dict[str, Any]: 481 max_factor = random.uniform(self.max_factor[0], self.max_factor[1]) 482 step_factor = random.uniform(self.step_factor[0], self.step_factor[1]) 483 return {"zoom_factors": np.arange(1.0, max_factor, step_factor)} 484 485 def get_transform_init_args_names(self) -> Tuple[str, str]:
54 def apply(self, img, h_start=0, w_start=0, **params): 55 return F.random_crop(img, self.height, self.width, h_start, w_start) 56 57 def get_params(self): 58 return {"h_start": random.random(), "w_start": random.random()} 59 60 def apply_to_bbox(self, bbox, **params): 61 return F.bbox_random_crop(bbox, self.height, self.width, **params)
54 def apply(self, img, h_start=0, w_start=0, **params): 55 return F.random_crop(img, self.height, self.width, h_start, w_start) 56 57 def get_params(self): 58 return {"h_start": random.random(), "w_start": random.random()} 59 60 def apply_to_bbox(self, bbox, **params): 61 return F.bbox_random_crop(bbox, self.height, self.width, **params)
220 221 if mask.any(): 222 mask = mask.sum(axis=-1) if mask.ndim == 3 else mask 223 non_zero_yx = np.argwhere(mask) 224 y, x = random.choice(non_zero_yx) 225 x_min = x - random.randint(0, self.width - 1) 226 y_min = y - random.randint(0, self.height - 1) 227 x_min = np.clip(x_min, 0, mask_width - self.width)
221 if mask.any(): 222 mask = mask.sum(axis=-1) if mask.ndim == 3 else mask 223 non_zero_yx = np.argwhere(mask) 224 y, x = random.choice(non_zero_yx) 225 x_min = x - random.randint(0, self.width - 1) 226 y_min = y - random.randint(0, self.height - 1) 227 x_min = np.clip(x_min, 0, mask_width - self.width) 228 y_min = np.clip(y_min, 0, mask_height - self.height)
222 mask = mask.sum(axis=-1) if mask.ndim == 3 else mask 223 non_zero_yx = np.argwhere(mask) 224 y, x = random.choice(non_zero_yx) 225 x_min = x - random.randint(0, self.width - 1) 226 y_min = y - random.randint(0, self.height - 1) 227 x_min = np.clip(x_min, 0, mask_width - self.width) 228 y_min = np.clip(y_min, 0, mask_height - self.height) 229 else:
226 y_min = y - random.randint(0, self.height - 1) 227 x_min = np.clip(x_min, 0, mask_width - self.width) 228 y_min = np.clip(y_min, 0, mask_height - self.height) 229 else: 230 x_min = random.randint(0, mask_width - self.width) 231 y_min = random.randint(0, mask_height - self.height) 232 233 x_max = x_min + self.width
227 x_min = np.clip(x_min, 0, mask_width - self.width) 228 y_min = np.clip(y_min, 0, mask_height - self.height) 229 else: 230 x_min = random.randint(0, mask_width - self.width) 231 y_min = random.randint(0, mask_height - self.height) 232 233 x_max = x_min + self.width 234 y_max = y_min + self.height
293 self.min_max_height = min_max_height 294 self.w2h_ratio = w2h_ratio 295 296 def get_params(self): 297 crop_height = random.randint(self.min_max_height[0], self.min_max_height[1]) 298 return { 299 "h_start": random.random(), 300 "w_start": random.random(),
295 296 def get_params(self): 297 crop_height = random.randint(self.min_max_height[0], self.min_max_height[1]) 298 return { 299 "h_start": random.random(), 300 "w_start": random.random(), 301 "crop_height": crop_height, 302 "crop_width": int(crop_height * self.w2h_ratio),
296 def get_params(self): 297 crop_height = random.randint(self.min_max_height[0], self.min_max_height[1]) 298 return { 299 "h_start": random.random(), 300 "w_start": random.random(), 301 "crop_height": crop_height, 302 "crop_width": int(crop_height * self.w2h_ratio), 303 }
346 img = params["image"] 347 area = img.shape[0] * img.shape[1] 348 349 for _attempt in range(10): 350 target_area = random.uniform(*self.scale) * area 351 log_ratio = (math.log(self.ratio[0]), math.log(self.ratio[1])) 352 aspect_ratio = math.exp(random.uniform(*log_ratio)) 353
348 349 for _attempt in range(10): 350 target_area = random.uniform(*self.scale) * area 351 log_ratio = (math.log(self.ratio[0]), math.log(self.ratio[1])) 352 aspect_ratio = math.exp(random.uniform(*log_ratio)) 353 354 w = int(round(math.sqrt(target_area * aspect_ratio))) # skipcq: PTC-W0028 355 h = int(round(math.sqrt(target_area / aspect_ratio))) # skipcq: PTC-W0028
354 w = int(round(math.sqrt(target_area * aspect_ratio))) # skipcq: PTC-W0028 355 h = int(round(math.sqrt(target_area / aspect_ratio))) # skipcq: PTC-W0028 356 357 if 0 < w <= img.shape[1] and 0 < h <= img.shape[0]: 358 i = random.randint(0, img.shape[0] - h) 359 j = random.randint(0, img.shape[1] - w) 360 return { 361 "crop_height": h,
355 h = int(round(math.sqrt(target_area / aspect_ratio))) # skipcq: PTC-W0028 356 357 if 0 < w <= img.shape[1] and 0 < h <= img.shape[0]: 358 i = random.randint(0, img.shape[0] - h) 359 j = random.randint(0, img.shape[1] - w) 360 return { 361 "crop_height": h, 362 "crop_width": w,
442 bbox = params[self.cropping_bbox_key] 443 h_max_shift = round((bbox[3] - bbox[1]) * self.max_part_shift[0]) 444 w_max_shift = round((bbox[2] - bbox[0]) * self.max_part_shift[1]) 445 446 x_min = bbox[0] - random.randint(-w_max_shift, w_max_shift) 447 x_max = bbox[2] + random.randint(-w_max_shift, w_max_shift) 448 449 y_min = bbox[1] - random.randint(-h_max_shift, h_max_shift)
443 h_max_shift = round((bbox[3] - bbox[1]) * self.max_part_shift[0]) 444 w_max_shift = round((bbox[2] - bbox[0]) * self.max_part_shift[1]) 445 446 x_min = bbox[0] - random.randint(-w_max_shift, w_max_shift) 447 x_max = bbox[2] + random.randint(-w_max_shift, w_max_shift) 448 449 y_min = bbox[1] - random.randint(-h_max_shift, h_max_shift) 450 y_max = bbox[3] + random.randint(-h_max_shift, h_max_shift)
445 446 x_min = bbox[0] - random.randint(-w_max_shift, w_max_shift) 447 x_max = bbox[2] + random.randint(-w_max_shift, w_max_shift) 448 449 y_min = bbox[1] - random.randint(-h_max_shift, h_max_shift) 450 y_max = bbox[3] + random.randint(-h_max_shift, h_max_shift) 451 452 x_min = max(0, x_min)
446 x_min = bbox[0] - random.randint(-w_max_shift, w_max_shift) 447 x_max = bbox[2] + random.randint(-w_max_shift, w_max_shift) 448 449 y_min = bbox[1] - random.randint(-h_max_shift, h_max_shift) 450 y_max = bbox[3] + random.randint(-h_max_shift, h_max_shift) 451 452 x_min = max(0, x_min) 453 y_min = max(0, y_min)
497 def get_params_dependent_on_targets(self, params): 498 img_h, img_w = params["image"].shape[:2] 499 if len(params["bboxes"]) == 0: # less likely, this class is for use with bboxes. 500 erosive_h = int(img_h * (1.0 - self.erosion_rate)) 501 crop_height = img_h if erosive_h >= img_h else random.randint(erosive_h, img_h) 502 return { 503 "h_start": random.random(), 504 "w_start": random.random(),
499 if len(params["bboxes"]) == 0: # less likely, this class is for use with bboxes. 500 erosive_h = int(img_h * (1.0 - self.erosion_rate)) 501 crop_height = img_h if erosive_h >= img_h else random.randint(erosive_h, img_h) 502 return { 503 "h_start": random.random(), 504 "w_start": random.random(), 505 "crop_height": crop_height, 506 "crop_width": int(crop_height * img_w / img_h),
500 erosive_h = int(img_h * (1.0 - self.erosion_rate)) 501 crop_height = img_h if erosive_h >= img_h else random.randint(erosive_h, img_h) 502 return { 503 "h_start": random.random(), 504 "w_start": random.random(), 505 "crop_height": crop_height, 506 "crop_width": int(crop_height * img_w / img_h), 507 }
509 x, y, x2, y2 = union_of_bboxes( 510 width=img_w, height=img_h, bboxes=params["bboxes"], erosion_rate=self.erosion_rate 511 ) 512 # find bigger region 513 bx, by = x * random.random(), y * random.random() 514 bx2, by2 = x2 + (1 - x2) * random.random(), y2 + (1 - y2) * random.random() 515 bw, bh = bx2 - bx, by2 - by 516 crop_height = img_h if bh >= 1.0 else int(img_h * bh)
509 x, y, x2, y2 = union_of_bboxes( 510 width=img_w, height=img_h, bboxes=params["bboxes"], erosion_rate=self.erosion_rate 511 ) 512 # find bigger region 513 bx, by = x * random.random(), y * random.random() 514 bx2, by2 = x2 + (1 - x2) * random.random(), y2 + (1 - y2) * random.random() 515 bw, bh = bx2 - bx, by2 - by 516 crop_height = img_h if bh >= 1.0 else int(img_h * bh)
510 width=img_w, height=img_h, bboxes=params["bboxes"], erosion_rate=self.erosion_rate 511 ) 512 # find bigger region 513 bx, by = x * random.random(), y * random.random() 514 bx2, by2 = x2 + (1 - x2) * random.random(), y2 + (1 - y2) * random.random() 515 bw, bh = bx2 - bx, by2 - by 516 crop_height = img_h if bh >= 1.0 else int(img_h * bh) 517 crop_width = img_w if bw >= 1.0 else int(img_w * bw)
510 width=img_w, height=img_h, bboxes=params["bboxes"], erosion_rate=self.erosion_rate 511 ) 512 # find bigger region 513 bx, by = x * random.random(), y * random.random() 514 bx2, by2 = x2 + (1 - x2) * random.random(), y2 + (1 - y2) * random.random() 515 bw, bh = bx2 - bx, by2 - by 516 crop_height = img_h if bh >= 1.0 else int(img_h * bh) 517 crop_width = img_w if bw >= 1.0 else int(img_w * bw)
824 if isinstance(self.px, int): 825 params = [self.px] * 4 826 elif len(self.px) == 2: 827 if self.sample_independently: 828 params = [random.randrange(*self.px) for _ in range(4)] 829 else: 830 px = random.randrange(*self.px) 831 params = [px] * 4
826 elif len(self.px) == 2: 827 if self.sample_independently: 828 params = [random.randrange(*self.px) for _ in range(4)] 829 else: 830 px = random.randrange(*self.px) 831 params = [px] * 4 832 else: 833 params = [i if isinstance(i, int) else random.randrange(*i) for i in self.px] # type: ignore
829 else: 830 px = random.randrange(*self.px) 831 params = [px] * 4 832 else: 833 params = [i if isinstance(i, int) else random.randrange(*i) for i in self.px] # type: ignore 834 835 return params # [top, right, bottom, left] 836
841 if isinstance(self.percent, float): 842 params = [self.percent] * 4 843 elif len(self.percent) == 2: 844 if self.sample_independently: 845 params = [random.uniform(*self.percent) for _ in range(4)] 846 else: 847 px = random.uniform(*self.percent) 848 params = [px] * 4
843 elif len(self.percent) == 2: 844 if self.sample_independently: 845 params = [random.uniform(*self.percent) for _ in range(4)] 846 else: 847 px = random.uniform(*self.percent) 848 params = [px] * 4 849 else: 850 params = [i if isinstance(i, (int, float)) else random.uniform(*i) for i in self.percent]
846 else: 847 px = random.uniform(*self.percent) 848 params = [px] * 4 849 else: 850 params = [i if isinstance(i, (int, float)) else random.uniform(*i) for i in self.percent] 851 852 return params # params = [top, right, bottom, left] 853
858 859 if len(pad_value) == 2: 860 a, b = pad_value 861 if isinstance(a, int) and isinstance(b, int): 862 return random.randint(a, b) 863 864 return random.uniform(a, b) 865
860 a, b = pad_value 861 if isinstance(a, int) and isinstance(b, int): 862 return random.randint(a, b) 863 864 return random.uniform(a, b) 865 866 return random.choice(pad_value) 867
862 return random.randint(a, b) 863 864 return random.uniform(a, b) 865 866 return random.choice(pad_value) 867 868 def get_transform_init_args_names(self) -> Tuple[str, ...]: 869 return (
915 self.crop_bottom = crop_bottom 916 917 def get_params_dependent_on_targets(self, params): 918 img = params["image"] 919 x_min = random.randint(0, int(self.crop_left * img.shape[1])) 920 x_max = random.randint(max(x_min + 1, int((1 - self.crop_right) * img.shape[1])), img.shape[1]) 921 y_min = random.randint(0, int(self.crop_top * img.shape[0])) 922 y_max = random.randint(max(y_min + 1, int((1 - self.crop_bottom) * img.shape[0])), img.shape[0])
916 917 def get_params_dependent_on_targets(self, params): 918 img = params["image"] 919 x_min = random.randint(0, int(self.crop_left * img.shape[1])) 920 x_max = random.randint(max(x_min + 1, int((1 - self.crop_right) * img.shape[1])), img.shape[1]) 921 y_min = random.randint(0, int(self.crop_top * img.shape[0])) 922 y_max = random.randint(max(y_min + 1, int((1 - self.crop_bottom) * img.shape[0])), img.shape[0]) 923 return {"x_min": x_min, "x_max": x_max, "y_min": y_min, "y_max": y_max}
917 def get_params_dependent_on_targets(self, params): 918 img = params["image"] 919 x_min = random.randint(0, int(self.crop_left * img.shape[1])) 920 x_max = random.randint(max(x_min + 1, int((1 - self.crop_right) * img.shape[1])), img.shape[1]) 921 y_min = random.randint(0, int(self.crop_top * img.shape[0])) 922 y_max = random.randint(max(y_min + 1, int((1 - self.crop_bottom) * img.shape[0])), img.shape[0]) 923 return {"x_min": x_min, "x_max": x_max, "y_min": y_min, "y_max": y_max} 924
918 img = params["image"] 919 x_min = random.randint(0, int(self.crop_left * img.shape[1])) 920 x_max = random.randint(max(x_min + 1, int((1 - self.crop_right) * img.shape[1])), img.shape[1]) 921 y_min = random.randint(0, int(self.crop_top * img.shape[0])) 922 y_max = random.randint(max(y_min + 1, int((1 - self.crop_bottom) * img.shape[0])), img.shape[0]) 923 return {"x_min": x_min, "x_max": x_max, "y_min": y_min, "y_max": y_max} 924 925 def apply(self, img, x_min=0, x_max=0, y_min=0, y_max=0, **params):
166 return apply_histogram(img, reference_image, blend_ratio) 167 168 def get_params(self): 169 return { 170 "reference_image": self.read_fn(random.choice(self.reference_images)), 171 "blend_ratio": random.uniform(self.blend_ratio[0], self.blend_ratio[1]), 172 } 173
167 168 def get_params(self): 169 return { 170 "reference_image": self.read_fn(random.choice(self.reference_images)), 171 "blend_ratio": random.uniform(self.blend_ratio[0], self.blend_ratio[1]), 172 } 173 174 def get_transform_init_args_names(self):
227 return fourier_domain_adaptation(img=img, target_img=target_image, beta=beta) 228 229 def get_params_dependent_on_targets(self, params): 230 img = params["image"] 231 target_img = self.read_fn(random.choice(self.reference_images)) 232 target_img = cv2.resize(target_img, dsize=(img.shape[1], img.shape[0])) 233 234 return {"target_image": target_img}
233 234 return {"target_image": target_img} 235 236 def get_params(self): 237 return {"beta": random.uniform(self.beta_limit[0], self.beta_limit[1])} 238 239 @property 240 def targets_as_params(self):
325 return adapted 326 327 def get_params(self): 328 return { 329 "reference_image": self.read_fn(random.choice(self.reference_images)), 330 "blend_ratio": random.uniform(self.blend_ratio[0], self.blend_ratio[1]), 331 } 332
326 327 def get_params(self): 328 return { 329 "reference_image": self.read_fn(random.choice(self.reference_images)), 330 "blend_ratio": random.uniform(self.blend_ratio[0], self.blend_ratio[1]), 331 } 332 333 def get_transform_init_args_names(self):
57 58 if self.max_channels >= num_channels: 59 raise ValueError("Can not drop all channels in ChannelDropout.") 60 61 num_drop_channels = random.randint(self.min_channels, self.max_channels) 62 63 channels_to_drop = random.sample(range(num_channels), k=num_drop_channels) 64
110 img = params["image"] 111 height, width = img.shape[:2] 112 113 holes = [] 114 for _n in range(random.randint(self.min_holes, self.max_holes)): 115 if all( 116 [ 117 isinstance(self.min_height, int),
119 isinstance(self.max_height, int), 120 isinstance(self.max_width, int), 121 ] 122 ): 123 hole_height = random.randint(self.min_height, self.max_height) 124 hole_width = random.randint(self.min_width, self.max_width) 125 elif all( 126 [
120 isinstance(self.max_width, int), 121 ] 122 ): 123 hole_height = random.randint(self.min_height, self.max_height) 124 hole_width = random.randint(self.min_width, self.max_width) 125 elif all( 126 [ 127 isinstance(self.min_height, float),
129 isinstance(self.max_height, float), 130 isinstance(self.max_width, float), 131 ] 132 ): 133 hole_height = int(height * random.uniform(self.min_height, self.max_height)) 134 hole_width = int(width * random.uniform(self.min_width, self.max_width)) 135 else: 136 raise ValueError(
130 isinstance(self.max_width, float), 131 ] 132 ): 133 hole_height = int(height * random.uniform(self.min_height, self.max_height)) 134 hole_width = int(width * random.uniform(self.min_width, self.max_width)) 135 else: 136 raise ValueError( 137 "Min width, max width, \
146 ] 147 ) 148 ) 149 150 y1 = random.randint(0, height - hole_height) 151 x1 = random.randint(0, width - hole_width) 152 y2 = y1 + hole_height 153 x2 = x1 + hole_width
147 ) 148 ) 149 150 y1 = random.randint(0, height - hole_height) 151 x1 = random.randint(0, width - hole_width) 152 y2 = y1 + hole_height 153 x2 = x1 + hole_width 154 holes.append((x1, y1, x2, y2))
59 height, width = img.shape[:2] 60 61 holes = [] 62 for _n in range(self.num_holes): 63 y = random.randint(0, height) 64 x = random.randint(0, width) 65 66 y1 = np.clip(y - self.max_h_size // 2, 0, height)
60 61 holes = [] 62 for _n in range(self.num_holes): 63 y = random.randint(0, height) 64 x = random.randint(0, width) 65 66 y1 = np.clip(y - self.max_h_size // 2, 0, height) 67 y2 = np.clip(y1 + self.max_h_size, 0, height)
90 if not 2 <= self.unit_size_min <= self.unit_size_max: 91 raise ValueError("Max unit size should be >= min size, both at least 2 pixels.") 92 if self.unit_size_max > min(height, width): 93 raise ValueError("Grid size limits must be within the shortest image edge.") 94 unit_width = random.randint(self.unit_size_min, self.unit_size_max + 1) 95 unit_height = unit_width 96 else: 97 # set grid using holes numbers
122 shift_y = 0 123 else: 124 shift_y = min(max(0, self.shift_y), unit_height - hole_height) 125 if self.random_offset: 126 shift_x = random.randint(0, unit_width - hole_width) 127 shift_y = random.randint(0, unit_height - hole_height) 128 holes = [] 129 for i in range(width // unit_width + 1):
123 else: 124 shift_y = min(max(0, self.shift_y), unit_height - hole_height) 125 if self.random_offset: 126 shift_x = random.randint(0, unit_width - hole_width) 127 shift_y = random.randint(0, unit_height - hole_height) 128 holes = [] 129 for i in range(width // unit_width + 1): 130 for j in range(height // unit_height + 1):
57 58 if num_labels == 0: 59 dropout_mask = None 60 else: 61 objects_to_drop = random.randint(int(self.max_objects[0]), int(self.max_objects[1])) 62 objects_to_drop = min(num_labels, objects_to_drop) 63 64 if objects_to_drop == num_labels:
1368 coef = MAX_VALUES_BY_DTYPE[img.dtype] 1369 img = img.astype(np.float32) * (1 / coef) 1370 1371 if mode == "rain": 1372 assert rain is not None 1373 img = img + rain 1374 elif mode == "mud": 1375 assert non_mud is not None and mud is not None
1371 if mode == "rain": 1372 assert rain is not None 1373 img = img + rain 1374 elif mode == "mud": 1375 assert non_mud is not None and mud is not None 1376 img = img * non_mud + mud 1377 else: 1378 raise ValueError("Unsupported spatter mode: " + str(mode))
40 self.scale_limit = to_tuple(scale_limit, bias=1.0) 41 self.interpolation = interpolation 42 43 def get_params(self): 44 return {"scale": random.uniform(self.scale_limit[0], self.scale_limit[1])} 45 46 def apply(self, img, scale=0, interpolation=cv2.INTER_LINEAR, **params): 47 return F.scale(img, scale, interpolation)
100 scale = max_size / max([height, width]) 101 return F.keypoint_scale(keypoint, scale, scale) 102 103 def get_params(self) -> Dict[str, int]: 104 return {"max_size": self.max_size if isinstance(self.max_size, int) else random.choice(self.max_size)} 105 106 def get_transform_init_args_names(self) -> Tuple[str, ...]: 107 return ("max_size", "interpolation")
149 scale = max_size / min([height, width]) 150 return F.keypoint_scale(keypoint, scale, scale) 151 152 def get_params(self) -> Dict[str, int]: 153 return {"max_size": self.max_size if isinstance(self.max_size, int) else random.choice(self.max_size)} 154 155 def get_transform_init_args_names(self) -> Tuple[str, ...]: 156 return ("max_size", "interpolation")
39 return np.ascontiguousarray(np.rot90(img, factor)) 40 41 def get_params(self): 42 # Random int in the range [0, 3] 43 return {"factor": random.randint(0, 3)} 44 45 def apply_to_bbox(self, bbox, factor=0, **params): 46 return F.bbox_rot90(bbox, factor, **params)
170 def targets_as_params(self) -> List[str]: 171 return ["image"] 172 173 def get_params_dependent_on_targets(self, params: Dict[str, Any]) -> Dict[str, Any]: 174 out_params = {"angle": random.uniform(self.limit[0], self.limit[1])} 175 if self.crop_border: 176 h, w = params["image"].shape[:2] 177 out_params.update(self._rotated_rect_with_max_area(h, w, out_params["angle"]))
252 def targets_as_params(self) -> List[str]: 253 return ["image"] 254 255 def get_params_dependent_on_targets(self, params: Dict[str, Any]) -> Dict[str, Any]: 256 angle = random.uniform(self.limit[0], self.limit[1]) 257 258 image = params["image"] 259 h, w = image.shape[:2]
118 return F.keypoint_shift_scale_rotate(keypoint, angle, scale, dx, dy, rows, cols) 119 120 def get_params(self): 121 return { 122 "angle": random.uniform(self.rotate_limit[0], self.rotate_limit[1]), 123 "scale": random.uniform(self.scale_limit[0], self.scale_limit[1]), 124 "dx": random.uniform(self.shift_limit_x[0], self.shift_limit_x[1]), 125 "dy": random.uniform(self.shift_limit_y[0], self.shift_limit_y[1]),
119 120 def get_params(self): 121 return { 122 "angle": random.uniform(self.rotate_limit[0], self.rotate_limit[1]), 123 "scale": random.uniform(self.scale_limit[0], self.scale_limit[1]), 124 "dx": random.uniform(self.shift_limit_x[0], self.shift_limit_x[1]), 125 "dy": random.uniform(self.shift_limit_y[0], self.shift_limit_y[1]), 126 }
120 def get_params(self): 121 return { 122 "angle": random.uniform(self.rotate_limit[0], self.rotate_limit[1]), 123 "scale": random.uniform(self.scale_limit[0], self.scale_limit[1]), 124 "dx": random.uniform(self.shift_limit_x[0], self.shift_limit_x[1]), 125 "dy": random.uniform(self.shift_limit_y[0], self.shift_limit_y[1]), 126 } 127
121 return { 122 "angle": random.uniform(self.rotate_limit[0], self.rotate_limit[1]), 123 "scale": random.uniform(self.scale_limit[0], self.scale_limit[1]), 124 "dx": random.uniform(self.shift_limit_x[0], self.shift_limit_x[1]), 125 "dy": random.uniform(self.shift_limit_y[0], self.shift_limit_y[1]), 126 } 127 128 def apply_to_bbox(self, bbox, angle, scale, dx, dy, **params):
252 bbox_returned = F.normalize_bbox(bbox_returned, rows, cols) 253 return bbox_returned 254 255 def get_params(self): 256 return {"random_state": random.randint(0, 10000)} 257 258 def get_transform_init_args_names(self): 259 return (
696 matrix: Optional[skimage.transform.ProjectiveTransform] = None, 697 scale: Optional[dict] = None, 698 **params 699 ) -> KeypointInternalType: 700 assert scale is not None and matrix is not None 701 return F.keypoint_affine(keypoint, matrix=matrix, scale=scale) 702 703 @property
708 h, w = params["image"].shape[:2] 709 710 translate: Dict[str, Union[int, float]] 711 if self.translate_px is not None: 712 translate = {key: random.randint(*value) for key, value in self.translate_px.items()} 713 elif self.translate_percent is not None: 714 translate = {key: random.uniform(*value) for key, value in self.translate_percent.items()} 715 translate["x"] = translate["x"] * w
710 translate: Dict[str, Union[int, float]] 711 if self.translate_px is not None: 712 translate = {key: random.randint(*value) for key, value in self.translate_px.items()} 713 elif self.translate_percent is not None: 714 translate = {key: random.uniform(*value) for key, value in self.translate_percent.items()} 715 translate["x"] = translate["x"] * w 716 translate["y"] = translate["y"] * h 717 else:
717 else: 718 translate = {"x": 0, "y": 0} 719 720 # Look to issue https://github.com/albumentations-team/albumentations/issues/1079 721 shear = {key: -random.uniform(*value) for key, value in self.shear.items()} 722 scale = {key: random.uniform(*value) for key, value in self.scale.items()} 723 if self.keep_ratio: 724 scale["y"] = scale["x"]
718 translate = {"x": 0, "y": 0} 719 720 # Look to issue https://github.com/albumentations-team/albumentations/issues/1079 721 shear = {key: -random.uniform(*value) for key, value in self.shear.items()} 722 scale = {key: random.uniform(*value) for key, value in self.scale.items()} 723 if self.keep_ratio: 724 scale["y"] = scale["x"] 725
723 if self.keep_ratio: 724 scale["y"] = scale["x"] 725 726 # Look to issue https://github.com/albumentations-team/albumentations/issues/1079 727 rotate = -random.uniform(*self.rotate) 728 729 # for images we use additional shifts of (0.5, 0.5) as otherwise 730 # we get an ugly black border for 90deg rotations
900 901 def get_params_dependent_on_targets(self, params) -> dict: 902 h, w = params["image"].shape[:2] 903 904 nb_rows = np.clip(random.randint(*self.nb_rows), 2, None) 905 nb_cols = np.clip(random.randint(*self.nb_cols), 2, None) 906 nb_cells = nb_cols * nb_rows 907 scale = random.uniform(*self.scale)
901 def get_params_dependent_on_targets(self, params) -> dict: 902 h, w = params["image"].shape[:2] 903 904 nb_rows = np.clip(random.randint(*self.nb_rows), 2, None) 905 nb_cols = np.clip(random.randint(*self.nb_cols), 2, None) 906 nb_cells = nb_cols * nb_rows 907 scale = random.uniform(*self.scale) 908
903 904 nb_rows = np.clip(random.randint(*self.nb_rows), 2, None) 905 nb_cols = np.clip(random.randint(*self.nb_cols), 2, None) 906 nb_cells = nb_cols * nb_rows 907 scale = random.uniform(*self.scale) 908 909 jitter: np.ndarray = random_utils.normal(0, scale, (nb_cells, 2)) 910 if not np.any(jitter > 0):
1182 1183 elif self.position == PadIfNeeded.PositionType.RANDOM: 1184 h_pad = h_top + h_bottom 1185 w_pad = w_left + w_right 1186 h_top = random.randint(0, h_pad) 1187 h_bottom = h_pad - h_top 1188 w_left = random.randint(0, w_pad) 1189 w_right = w_pad - w_left
1184 h_pad = h_top + h_bottom 1185 w_pad = w_left + w_right 1186 h_top = random.randint(0, h_pad) 1187 h_bottom = h_pad - h_top 1188 w_left = random.randint(0, w_pad) 1189 w_right = w_pad - w_left 1190 1191 return h_top, h_bottom, w_left, w_right
1270 return F.random_flip(img, d) 1271 1272 def get_params(self): 1273 # Random int in the range [-1, 1] 1274 return {"d": random.randint(-1, 1)} 1275 1276 def apply_to_bbox(self, bbox: BoxInternalType, **params) -> BoxInternalType: 1277 return F.bbox_flip(bbox, **params)
1374 return bbox_returned 1375 1376 def get_params(self): 1377 return { 1378 "k": random.uniform(self.distort_limit[0], self.distort_limit[1]), 1379 "dx": round(random.uniform(self.shift_limit[0], self.shift_limit[1])), 1380 "dy": round(random.uniform(self.shift_limit[0], self.shift_limit[1])), 1381 }
1375 1376 def get_params(self): 1377 return { 1378 "k": random.uniform(self.distort_limit[0], self.distort_limit[1]), 1379 "dx": round(random.uniform(self.shift_limit[0], self.shift_limit[1])), 1380 "dy": round(random.uniform(self.shift_limit[0], self.shift_limit[1])), 1381 } 1382
1376 def get_params(self): 1377 return { 1378 "k": random.uniform(self.distort_limit[0], self.distort_limit[1]), 1379 "dx": round(random.uniform(self.shift_limit[0], self.shift_limit[1])), 1380 "dy": round(random.uniform(self.shift_limit[0], self.shift_limit[1])), 1381 } 1382 1383 def get_transform_init_args_names(self):
1486 1487 def get_params_dependent_on_targets(self, params): 1488 h, w = params["image"].shape[:2] 1489 1490 stepsx = [1 + random.uniform(self.distort_limit[0], self.distort_limit[1]) for _ in range(self.num_steps + 1)] 1491 stepsy = [1 + random.uniform(self.distort_limit[0], self.distort_limit[1]) for _ in range(self.num_steps + 1)] 1492 1493 if self.normalized:
1487 def get_params_dependent_on_targets(self, params): 1488 h, w = params["image"].shape[:2] 1489 1490 stepsx = [1 + random.uniform(self.distort_limit[0], self.distort_limit[1]) for _ in range(self.num_steps + 1)] 1491 stepsy = [1 + random.uniform(self.distort_limit[0], self.distort_limit[1]) for _ in range(self.num_steps + 1)] 1492 1493 if self.normalized: 1494 return self._normalize(h, w, stepsx, stepsy)
273 if self.compression_type == ImageCompression.ImageCompressionType.WEBP: 274 image_type = ".webp" 275 276 return { 277 "quality": random.randint(self.quality_lower, self.quality_upper), 278 "image_type": image_type, 279 } 280
363 def apply(self, image, snow_point=0.1, **params): 364 return F.add_snow(image, snow_point, self.brightness_coeff) 365 366 def get_params(self): 367 return {"snow_point": random.uniform(self.snow_point_lower, self.snow_point_upper)} 368 369 def get_transform_init_args_names(self): 370 return ("snow_point_lower", "snow_point_upper", "brightness_coeff")
560 return ["image"] 561 562 def get_params_dependent_on_targets(self, params): 563 img = params["image"] 564 slant = int(random.uniform(self.slant_lower, self.slant_upper)) 565 566 height, width = img.shape[:2] 567 area = height * width
582 rain_drops = [] 583 584 for _i in range(num_drops): # If You want heavy rain, try increasing this 585 if slant < 0: 586 x = random.randint(slant, width) 587 else: 588 x = random.randint(0, width - slant) 589
584 for _i in range(num_drops): # If You want heavy rain, try increasing this 585 if slant < 0: 586 x = random.randint(slant, width) 587 else: 588 x = random.randint(0, width - slant) 589 590 y = random.randint(0, height - drop_length) 591
586 x = random.randint(slant, width) 587 else: 588 x = random.randint(0, width - slant) 589 590 y = random.randint(0, height - drop_length) 591 592 rain_drops.append((x, y)) 593
654 return ["image"] 655 656 def get_params_dependent_on_targets(self, params): 657 img = params["image"] 658 fog_coef = random.uniform(self.fog_coef_lower, self.fog_coef_upper) 659 660 height, width = imshape = img.shape[:2] 661
667 index = 1 668 669 while midx > -hw or midy > -hw: 670 for _i in range(hw // 10 * index): 671 x = random.randint(midx, width - midx - hw) 672 y = random.randint(midy, height - midy - hw) 673 haze_list.append((x, y)) 674
668 669 while midx > -hw or midy > -hw: 670 for _i in range(hw // 10 * index): 671 x = random.randint(midx, width - midx - hw) 672 y = random.randint(midy, height - midy - hw) 673 haze_list.append((x, y)) 674 675 midx -= 3 * hw * width // sum(imshape)
774 def get_params_dependent_on_targets(self, params): 775 img = params["image"] 776 height, width = img.shape[:2] 777 778 angle = 2 * math.pi * random.uniform(self.angle_lower, self.angle_upper) 779 780 flare_center_x = random.uniform(self.flare_center_lower_x, self.flare_center_upper_x) 781 flare_center_y = random.uniform(self.flare_center_lower_y, self.flare_center_upper_y)
776 height, width = img.shape[:2] 777 778 angle = 2 * math.pi * random.uniform(self.angle_lower, self.angle_upper) 779 780 flare_center_x = random.uniform(self.flare_center_lower_x, self.flare_center_upper_x) 781 flare_center_y = random.uniform(self.flare_center_lower_y, self.flare_center_upper_y) 782 783 flare_center_x = int(width * flare_center_x)
777 778 angle = 2 * math.pi * random.uniform(self.angle_lower, self.angle_upper) 779 780 flare_center_x = random.uniform(self.flare_center_lower_x, self.flare_center_upper_x) 781 flare_center_y = random.uniform(self.flare_center_lower_y, self.flare_center_upper_y) 782 783 flare_center_x = int(width * flare_center_x) 784 flare_center_y = int(height * flare_center_y)
782 783 flare_center_x = int(width * flare_center_x) 784 flare_center_y = int(height * flare_center_y) 785 786 num_circles = random.randint(self.num_flare_circles_lower, self.num_flare_circles_upper) 787 788 circles = [] 789
798 x.append(rand_x) 799 y.append(rand_y) 800 801 for _i in range(num_circles): 802 alpha = random.uniform(0.05, 0.2) 803 r = random.randint(0, len(x) - 1) 804 rad = random.randint(1, max(height // 100 - 2, 2)) 805
799 y.append(rand_y) 800 801 for _i in range(num_circles): 802 alpha = random.uniform(0.05, 0.2) 803 r = random.randint(0, len(x) - 1) 804 rad = random.randint(1, max(height // 100 - 2, 2)) 805 806 r_color = random.randint(max(self.src_color[0] - 50, 0), self.src_color[0])
800 801 for _i in range(num_circles): 802 alpha = random.uniform(0.05, 0.2) 803 r = random.randint(0, len(x) - 1) 804 rad = random.randint(1, max(height // 100 - 2, 2)) 805 806 r_color = random.randint(max(self.src_color[0] - 50, 0), self.src_color[0]) 807 g_color = random.randint(max(self.src_color[1] - 50, 0), self.src_color[1])
802 alpha = random.uniform(0.05, 0.2) 803 r = random.randint(0, len(x) - 1) 804 rad = random.randint(1, max(height // 100 - 2, 2)) 805 806 r_color = random.randint(max(self.src_color[0] - 50, 0), self.src_color[0]) 807 g_color = random.randint(max(self.src_color[1] - 50, 0), self.src_color[1]) 808 b_color = random.randint(max(self.src_color[2] - 50, 0), self.src_color[2]) 809
803 r = random.randint(0, len(x) - 1) 804 rad = random.randint(1, max(height // 100 - 2, 2)) 805 806 r_color = random.randint(max(self.src_color[0] - 50, 0), self.src_color[0]) 807 g_color = random.randint(max(self.src_color[1] - 50, 0), self.src_color[1]) 808 b_color = random.randint(max(self.src_color[2] - 50, 0), self.src_color[2]) 809 810 circles += [
804 rad = random.randint(1, max(height // 100 - 2, 2)) 805 806 r_color = random.randint(max(self.src_color[0] - 50, 0), self.src_color[0]) 807 g_color = random.randint(max(self.src_color[1] - 50, 0), self.src_color[1]) 808 b_color = random.randint(max(self.src_color[2] - 50, 0), self.src_color[2]) 809 810 circles += [ 811 (
899 def get_params_dependent_on_targets(self, params): 900 img = params["image"] 901 height, width = img.shape[:2] 902 903 num_shadows = random.randint(self.num_shadows_lower, self.num_shadows_upper) 904 905 x_min, y_min, x_max, y_max = self.shadow_roi 906
913 914 for _index in range(num_shadows): 915 vertex = [] 916 for _dimension in range(self.shadow_dimension): 917 vertex.append((random.randint(x_min, x_max), random.randint(y_min, y_max))) 918 919 vertices = np.array([vertex], dtype=np.int32) 920 vertices_list.append(vertices)
913 914 for _index in range(num_shadows): 915 vertex = [] 916 for _dimension in range(self.shadow_dimension): 917 vertex.append((random.randint(x_min, x_max), random.randint(y_min, y_max))) 918 919 vertices = np.array([vertex], dtype=np.int32) 920 vertices_list.append(vertices)
1006 return F.shift_hsv(image, hue_shift, sat_shift, val_shift) 1007 1008 def get_params(self): 1009 return { 1010 "hue_shift": random.uniform(self.hue_shift_limit[0], self.hue_shift_limit[1]), 1011 "sat_shift": random.uniform(self.sat_shift_limit[0], self.sat_shift_limit[1]), 1012 "val_shift": random.uniform(self.val_shift_limit[0], self.val_shift_limit[1]), 1013 }
1007 1008 def get_params(self): 1009 return { 1010 "hue_shift": random.uniform(self.hue_shift_limit[0], self.hue_shift_limit[1]), 1011 "sat_shift": random.uniform(self.sat_shift_limit[0], self.sat_shift_limit[1]), 1012 "val_shift": random.uniform(self.val_shift_limit[0], self.val_shift_limit[1]), 1013 } 1014
1008 def get_params(self): 1009 return { 1010 "hue_shift": random.uniform(self.hue_shift_limit[0], self.hue_shift_limit[1]), 1011 "sat_shift": random.uniform(self.sat_shift_limit[0], self.sat_shift_limit[1]), 1012 "val_shift": random.uniform(self.val_shift_limit[0], self.val_shift_limit[1]), 1013 } 1014 1015 def get_transform_init_args_names(self):
1042 def apply(self, image, threshold=0, **params): 1043 return F.solarize(image, threshold) 1044 1045 def get_params(self): 1046 return {"threshold": random.uniform(self.threshold[0], self.threshold[1])} 1047 1048 def get_transform_init_args_names(self): 1049 return ("threshold",)
1082 return F.posterize(image, num_bits) 1083 1084 def get_params(self): 1085 if len(self.num_bits) == 3: 1086 return {"num_bits": [random.randint(i[0], i[1]) for i in self.num_bits]} 1087 return {"num_bits": random.randint(self.num_bits[0], self.num_bits[1])} 1088 1089 def get_transform_init_args_names(self):
1083 1084 def get_params(self): 1085 if len(self.num_bits) == 3: 1086 return {"num_bits": [random.randint(i[0], i[1]) for i in self.num_bits]} 1087 return {"num_bits": random.randint(self.num_bits[0], self.num_bits[1])} 1088 1089 def get_transform_init_args_names(self): 1090 return ("num_bits",)
1183 return F.shift_rgb(image, r_shift, g_shift, b_shift) 1184 1185 def get_params(self): 1186 return { 1187 "r_shift": random.uniform(self.r_shift_limit[0], self.r_shift_limit[1]), 1188 "g_shift": random.uniform(self.g_shift_limit[0], self.g_shift_limit[1]), 1189 "b_shift": random.uniform(self.b_shift_limit[0], self.b_shift_limit[1]), 1190 }
1184 1185 def get_params(self): 1186 return { 1187 "r_shift": random.uniform(self.r_shift_limit[0], self.r_shift_limit[1]), 1188 "g_shift": random.uniform(self.g_shift_limit[0], self.g_shift_limit[1]), 1189 "b_shift": random.uniform(self.b_shift_limit[0], self.b_shift_limit[1]), 1190 } 1191
1185 def get_params(self): 1186 return { 1187 "r_shift": random.uniform(self.r_shift_limit[0], self.r_shift_limit[1]), 1188 "g_shift": random.uniform(self.g_shift_limit[0], self.g_shift_limit[1]), 1189 "b_shift": random.uniform(self.b_shift_limit[0], self.b_shift_limit[1]), 1190 } 1191 1192 def get_transform_init_args_names(self):
1229 return F.brightness_contrast_adjust(img, alpha, beta, self.brightness_by_max) 1230 1231 def get_params(self): 1232 return { 1233 "alpha": 1.0 + random.uniform(self.contrast_limit[0], self.contrast_limit[1]), 1234 "beta": 0.0 + random.uniform(self.brightness_limit[0], self.brightness_limit[1]), 1235 } 1236
1230 1231 def get_params(self): 1232 return { 1233 "alpha": 1.0 + random.uniform(self.contrast_limit[0], self.contrast_limit[1]), 1234 "beta": 0.0 + random.uniform(self.brightness_limit[0], self.brightness_limit[1]), 1235 } 1236 1237 def get_transform_init_args_names(self):
1333 return F.gauss_noise(img, gauss=gauss) 1334 1335 def get_params_dependent_on_targets(self, params): 1336 image = params["image"] 1337 var = random.uniform(self.var_limit[0], self.var_limit[1]) 1338 sigma = var**0.5 1339 1340 if self.per_channel:
1381 return F.iso_noise(img, color_shift, intensity, np.random.RandomState(random_state)) 1382 1383 def get_params(self): 1384 return { 1385 "color_shift": random.uniform(self.color_shift[0], self.color_shift[1]), 1386 "intensity": random.uniform(self.intensity[0], self.intensity[1]), 1387 "random_state": random.randint(0, 65536), 1388 }
1382 1383 def get_params(self): 1384 return { 1385 "color_shift": random.uniform(self.color_shift[0], self.color_shift[1]), 1386 "intensity": random.uniform(self.intensity[0], self.intensity[1]), 1387 "random_state": random.randint(0, 65536), 1388 } 1389
1383 def get_params(self): 1384 return { 1385 "color_shift": random.uniform(self.color_shift[0], self.color_shift[1]), 1386 "intensity": random.uniform(self.intensity[0], self.intensity[1]), 1387 "random_state": random.randint(0, 65536), 1388 } 1389 1390 def get_transform_init_args_names(self):
1418 1419 return F.clahe(img, clip_limit, self.tile_grid_size) 1420 1421 def get_params(self): 1422 return {"clip_limit": random.uniform(self.clip_limit[0], self.clip_limit[1])} 1423 1424 def get_transform_init_args_names(self): 1425 return ("clip_limit", "tile_grid_size")
1498 def apply(self, img, gamma=1, **params): 1499 return F.gamma_transform(img, gamma=gamma) 1500 1501 def get_params(self): 1502 return {"gamma": random.uniform(self.gamma_limit[0], self.gamma_limit[1]) / 100.0} 1503 1504 def get_transform_init_args_names(self): 1505 return ("gamma_limit", "eps")
1722 up_interpolation=self.interpolation.upscale, 1723 ) 1724 1725 def get_params(self) -> Dict[str, Any]: 1726 return {"scale": random.uniform(self.scale_min, self.scale_max)} 1727 1728 def get_transform_init_args_names(self) -> Tuple[str, str]: 1729 return "scale_min", "scale_max"
1978 1979 return value 1980 1981 def get_params(self): 1982 brightness = random.uniform(self.brightness[0], self.brightness[1]) 1983 contrast = random.uniform(self.contrast[0], self.contrast[1]) 1984 saturation = random.uniform(self.saturation[0], self.saturation[1]) 1985 hue = random.uniform(self.hue[0], self.hue[1])
1979 return value 1980 1981 def get_params(self): 1982 brightness = random.uniform(self.brightness[0], self.brightness[1]) 1983 contrast = random.uniform(self.contrast[0], self.contrast[1]) 1984 saturation = random.uniform(self.saturation[0], self.saturation[1]) 1985 hue = random.uniform(self.hue[0], self.hue[1]) 1986
1980 1981 def get_params(self): 1982 brightness = random.uniform(self.brightness[0], self.brightness[1]) 1983 contrast = random.uniform(self.contrast[0], self.contrast[1]) 1984 saturation = random.uniform(self.saturation[0], self.saturation[1]) 1985 hue = random.uniform(self.hue[0], self.hue[1]) 1986 1987 order = [0, 1, 2, 3]
1981 def get_params(self): 1982 brightness = random.uniform(self.brightness[0], self.brightness[1]) 1983 contrast = random.uniform(self.contrast[0], self.contrast[1]) 1984 saturation = random.uniform(self.saturation[0], self.saturation[1]) 1985 hue = random.uniform(self.hue[0], self.hue[1]) 1986 1987 order = [0, 1, 2, 3] 1988 random.shuffle(order)
2042 matrix = (1 - alpha_sample) * matrix_nochange + alpha_sample * matrix_effect 2043 return matrix 2044 2045 def get_params(self): 2046 alpha = random.uniform(*self.alpha) 2047 lightness = random.uniform(*self.lightness) 2048 sharpening_matrix = self.__generate_sharpening_matrix(alpha_sample=alpha, lightness_sample=lightness) 2049 return {"sharpening_matrix": sharpening_matrix}
2043 return matrix 2044 2045 def get_params(self): 2046 alpha = random.uniform(*self.alpha) 2047 lightness = random.uniform(*self.lightness) 2048 sharpening_matrix = self.__generate_sharpening_matrix(alpha_sample=alpha, lightness_sample=lightness) 2049 return {"sharpening_matrix": sharpening_matrix} 2050
2093 matrix = (1 - alpha_sample) * matrix_nochange + alpha_sample * matrix_effect 2094 return matrix 2095 2096 def get_params(self): 2097 alpha = random.uniform(*self.alpha) 2098 strength = random.uniform(*self.strength) 2099 emboss_matrix = self.__generate_emboss_matrix(alpha_sample=alpha, strength_sample=strength) 2100 return {"emboss_matrix": emboss_matrix}
2094 return matrix 2095 2096 def get_params(self): 2097 alpha = random.uniform(*self.alpha) 2098 strength = random.uniform(*self.strength) 2099 emboss_matrix = self.__generate_emboss_matrix(alpha_sample=alpha, strength_sample=strength) 2100 return {"emboss_matrix": emboss_matrix} 2101
2170 def get_transform_init_args_names(self) -> Tuple[str, str, str, str]: 2171 return ("p_replace", "n_segments", "max_size", "interpolation") 2172 2173 def get_params(self) -> dict: 2174 n_segments = random.randint(*self.n_segments) 2175 p = random.uniform(*self.p_replace) 2176 return {"replace_samples": random_utils.random(n_segments) < p, "n_segments": n_segments} 2177
2171 return ("p_replace", "n_segments", "max_size", "interpolation") 2172 2173 def get_params(self) -> dict: 2174 n_segments = random.randint(*self.n_segments) 2175 p = random.uniform(*self.p_replace) 2176 return {"replace_samples": random_utils.random(n_segments) < p, "n_segments": n_segments} 2177 2178 def apply(self, img: np.ndarray, replace_samples: Sequence[bool] = (False,), n_segments: int = 1, **kwargs):
2221 return F.add_weighted(img, img_weight, template, template_weight) 2222 2223 def get_params(self): 2224 return { 2225 "img_weight": random.uniform(self.img_weight[0], self.img_weight[1]), 2226 "template_weight": random.uniform(self.template_weight[0], self.template_weight[1]), 2227 } 2228
2222 2223 def get_params(self): 2224 return { 2225 "img_weight": random.uniform(self.img_weight[0], self.img_weight[1]), 2226 "template_weight": random.uniform(self.template_weight[0], self.template_weight[1]), 2227 } 2228 2229 def get_params_dependent_on_targets(self, params):
2227 } 2228 2229 def get_params_dependent_on_targets(self, params): 2230 img = params["image"] 2231 template = random.choice(self.templates) 2232 2233 if self.template_transform is not None: 2234 template = self.template_transform(image=template)["image"]
2310 raise ValueError(f"{name} values should be between {bounds}") 2311 return value 2312 2313 def get_params(self): 2314 ksize = random.randrange(self.blur_limit[0], self.blur_limit[1] + 1, 2) 2315 if ksize % 2 == 0: 2316 raise ValueError(f"Kernel size must be odd. Got: {ksize}") 2317
2314 ksize = random.randrange(self.blur_limit[0], self.blur_limit[1] + 1, 2) 2315 if ksize % 2 == 0: 2316 raise ValueError(f"Kernel size must be odd. Got: {ksize}") 2317 2318 cutoff = random.uniform(*self.cutoff) 2319 2320 # From dsp.stackexchange.com/questions/58301/2-d-circularly-symmetric-low-pass-filter 2321 with np.errstate(divide="ignore", invalid="ignore"):
2398 return value 2399 2400 def get_params(self): 2401 return { 2402 "ksize": random.randrange(self.blur_limit[0], self.blur_limit[1] + 1, 2), 2403 "sigma": random.uniform(*self.sigma_limit), 2404 "alpha": random.uniform(*self.alpha), 2405 }
2399 2400 def get_params(self): 2401 return { 2402 "ksize": random.randrange(self.blur_limit[0], self.blur_limit[1] + 1, 2), 2403 "sigma": random.uniform(*self.sigma_limit), 2404 "alpha": random.uniform(*self.alpha), 2405 } 2406
2400 def get_params(self): 2401 return { 2402 "ksize": random.randrange(self.blur_limit[0], self.blur_limit[1] + 1, 2), 2403 "sigma": random.uniform(*self.sigma_limit), 2404 "alpha": random.uniform(*self.alpha), 2405 } 2406 2407 def apply(self, img, ksize=3, sigma=0, alpha=0.2, **params):
2480 def get_params_dependent_on_targets(self, params: Dict[str, Any]) -> Dict[str, Any]: 2481 img = params["image"] 2482 shape = img.shape if self.per_channel else img.shape[:2] 2483 2484 rnd = np.random.RandomState(random.randint(0, 1 << 31)) 2485 # Use choice to create boolean matrix, if we will use binomial after that we will need type conversion 2486 drop_mask = rnd.choice([True, False], shape, p=[self.dropout_prob, 1 - self.dropout_prob]) 2487
2615 2616 def get_params_dependent_on_targets(self, params: Dict[str, Any]) -> Dict[str, Any]: 2617 h, w = params["image"].shape[:2] 2618 2619 mean = random.uniform(self.mean[0], self.mean[1]) 2620 std = random.uniform(self.std[0], self.std[1]) 2621 cutout_threshold = random.uniform(self.cutout_threshold[0], self.cutout_threshold[1]) 2622 sigma = random.uniform(self.gauss_sigma[0], self.gauss_sigma[1])
2616 def get_params_dependent_on_targets(self, params: Dict[str, Any]) -> Dict[str, Any]: 2617 h, w = params["image"].shape[:2] 2618 2619 mean = random.uniform(self.mean[0], self.mean[1]) 2620 std = random.uniform(self.std[0], self.std[1]) 2621 cutout_threshold = random.uniform(self.cutout_threshold[0], self.cutout_threshold[1]) 2622 sigma = random.uniform(self.gauss_sigma[0], self.gauss_sigma[1]) 2623 mode = random.choice(self.mode)
2617 h, w = params["image"].shape[:2] 2618 2619 mean = random.uniform(self.mean[0], self.mean[1]) 2620 std = random.uniform(self.std[0], self.std[1]) 2621 cutout_threshold = random.uniform(self.cutout_threshold[0], self.cutout_threshold[1]) 2622 sigma = random.uniform(self.gauss_sigma[0], self.gauss_sigma[1]) 2623 mode = random.choice(self.mode) 2624 intensity = random.uniform(self.intensity[0], self.intensity[1])
2618 2619 mean = random.uniform(self.mean[0], self.mean[1]) 2620 std = random.uniform(self.std[0], self.std[1]) 2621 cutout_threshold = random.uniform(self.cutout_threshold[0], self.cutout_threshold[1]) 2622 sigma = random.uniform(self.gauss_sigma[0], self.gauss_sigma[1]) 2623 mode = random.choice(self.mode) 2624 intensity = random.uniform(self.intensity[0], self.intensity[1]) 2625 color = np.array(self.color[mode]) / 255.0
2619 mean = random.uniform(self.mean[0], self.mean[1]) 2620 std = random.uniform(self.std[0], self.std[1]) 2621 cutout_threshold = random.uniform(self.cutout_threshold[0], self.cutout_threshold[1]) 2622 sigma = random.uniform(self.gauss_sigma[0], self.gauss_sigma[1]) 2623 mode = random.choice(self.mode) 2624 intensity = random.uniform(self.intensity[0], self.intensity[1]) 2625 color = np.array(self.color[mode]) / 255.0 2626
2620 std = random.uniform(self.std[0], self.std[1]) 2621 cutout_threshold = random.uniform(self.cutout_threshold[0], self.cutout_threshold[1]) 2622 sigma = random.uniform(self.gauss_sigma[0], self.gauss_sigma[1]) 2623 mode = random.choice(self.mode) 2624 intensity = random.uniform(self.intensity[0], self.intensity[1]) 2625 color = np.array(self.color[mode]) / 255.0 2626 2627 liquid_layer = random_utils.normal(size=(h, w), loc=mean, scale=std)
192 if args: 193 raise KeyError("You have to pass data to augmentations as named arguments, for example: aug(image=image)") 194 if self.is_check_args: 195 self._check_args(**data) 196 assert isinstance(force_apply, (bool, int)), "force_apply must have bool or int type" 197 need_to_run = force_apply or random.random() < self.p 198 for p in self.processors.values(): 199 p.ensure_data_valid(data)
193 raise KeyError("You have to pass data to augmentations as named arguments, for example: aug(image=image)") 194 if self.is_check_args: 195 self._check_args(**data) 196 assert isinstance(force_apply, (bool, int)), "force_apply must have bool or int type" 197 need_to_run = force_apply or random.random() < self.p 198 for p in self.processors.values(): 199 p.ensure_data_valid(data) 200 transforms = self.transforms if need_to_run else get_always_apply(self.transforms)
319 for t in self.transforms: 320 data = t(**data) 321 return data 322 323 if self.transforms_ps and (force_apply or random.random() < self.p): 324 idx: int = random_utils.choice(len(self.transforms), p=self.transforms_ps) 325 t = self.transforms[idx] 326 data = t(force_apply=True, **data)
351 for t in self.transforms: 352 data = t(**data) 353 return data 354 355 if self.transforms_ps and (force_apply or random.random() < self.p): 356 idx = random_utils.choice(len(self.transforms), size=self.n, replace=self.replace, p=self.transforms_ps) 357 for i in idx: # type: ignore 358 t = self.transforms[i]
388 for t in self.transforms: 389 data = t(**data) 390 return data 391 392 if random.random() < self.p: 393 return self.transforms[0](force_apply=True, **data) 394 395 return self.transforms[-1](force_apply=True, **data)
411 super(PerChannel, self).__init__(transforms, p) 412 self.channels = channels 413 414 def __call__(self, *args, force_apply: bool = False, **data) -> typing.Dict[str, typing.Any]: 415 if force_apply or random.random() < self.p: 416 image = data["image"] 417 418 # Expand mono images to have a single channel
97 return self.apply_with_params(self.params, **kwargs) 98 99 return kwargs 100 101 if (random.random() < self.p) or self.always_apply or force_apply: 102 params = self.get_params() 103 104 if self.targets_as_params:
101 if (random.random() < self.p) or self.always_apply or force_apply: 102 params = self.get_params() 103 104 if self.targets_as_params: 105 assert all(key in kwargs for key in self.targets_as_params), "{} requires {}".format( 106 self.__class__.__name__, self.targets_as_params 107 ) 108 targets_as_params = {k: kwargs[k] for k in self.targets_as_params} 109 params_dependent_on_targets = self.get_params_dependent_on_targets(targets_as_params) 110 params.update(params_dependent_on_targets)
133 res[key] = None 134 return res 135 136 def set_deterministic(self, flag: bool, save_key: str = "replay") -> "BasicTransform": 137 assert save_key != "params", "params save_key is reserved" 138 self.deterministic = flag 139 self.save_key = save_key 140 return self
114 if self.params.label_fields is None: 115 return data 116 for data_name in self.data_fields: 117 for field in self.params.label_fields: 118 assert len(data[data_name]) == len(data[field]) 119 data_with_added_field = [] 120 for d, field_value in zip(data[data_name], data[field]): 121 data_with_added_field.append(list(d) + [field_value])
12 Size = Union[int, Sequence[int]] 13 14 15 def get_random_state() -> np.random.RandomState: 16 return np.random.RandomState(py_random.randint(0, (1 << 32) - 1)) 17 18 19 def uniform(
173 174 # -------------------------------------------------------------------------- 175 # Done with replacements 176 # -------------------------------------------------------------------------- 177 assert len(set(layer_keys)) == len(layer_keys) 178 assert len(original_keys) == len(layer_keys) 179 180 new_weights = {}
174 # -------------------------------------------------------------------------- 175 # Done with replacements 176 # -------------------------------------------------------------------------- 177 assert len(set(layer_keys)) == len(layer_keys) 178 assert len(original_keys) == len(layer_keys) 179 180 new_weights = {} 181 new_keys_to_original_keys = {}
274 ) 275 ) 276 continue 277 278 assert key_model not in result_state_dict 279 result_state_dict[key_model] = value_ckpt 280 if key_ckpt in matched_keys: # already added to matched_keys 281 logger.error(
1 # Copyright (c) Facebook, Inc. and its affiliates. 2 import logging 3 import os 4 import pickle 5 from urllib.parse import parse_qs, urlparse 6 import torch 7 from fvcore.common.checkpoint import Checkpointer 8 from torch.nn.parallel import DistributedDataParallel
31 self.path_manager = PathManager 32 self._parsed_url_during_load = None 33 34 def load(self, path, *args, **kwargs): 35 assert self._parsed_url_during_load is None 36 need_sync = False 37 logger = logging.getLogger(__name__) 38 logger.info("[DetectionCheckpointer] Loading from {} ...".format(path))
71 72 def _load_file(self, filename): 73 if filename.endswith(".pkl"): 74 with PathManager.open(filename, "rb") as f: 75 data = pickle.load(f, encoding="latin1") 76 if "model" in data and "__author__" in data: 77 # file is in Detectron2 model zoo format 78 self.logger.info("Reading a file from '{}'".format(data["__author__"]))
87 elif filename.endswith(".pyth"): 88 # assume file is from pycls; no one else seems to use the ".pyth" extension 89 with PathManager.open(filename, "rb") as f: 90 data = torch.load(f) 91 assert ( 92 "model_state" in data 93 ), f"Cannot load .pyth file {filename}; pycls checkpoints must contain 'model_state'." 94 model_state = { 95 k: v 96 for k, v in data["model_state"].items()
100 101 loaded = self._torch_load(filename) 102 if "model" not in loaded: 103 loaded = {"model": loaded} 104 assert self._parsed_url_during_load is not None, "`_load_file` must be called inside `load`" 105 parsed_url = self._parsed_url_during_load 106 queries = parse_qs(parsed_url.query) 107 if queries.pop("matching_heuristics", "False") == ["True"]:
41 cfg = cfg.clone() 42 if to_version is None: 43 to_version = _C.VERSION 44 45 assert cfg.VERSION <= to_version, "Cannot upgrade from v{} to v{}!".format( 46 cfg.VERSION, to_version 47 ) 48 for k in range(cfg.VERSION, to_version): 49 converter = globals()["ConverterV" + str(k + 1)] 50 converter.upgrade(cfg)
68 Therefore, the implementation only needs to fill in the default values 69 in the old version when a general downgrade is not possible. 70 """ 71 cfg = cfg.clone() 72 assert cfg.VERSION >= to_version, "Cannot downgrade from v{} to v{}!".format( 73 cfg.VERSION, to_version 74 ) 75 for k in range(cfg.VERSION, to_version, -1): 76 converter = globals()["ConverterV" + str(k)] 77 converter.downgrade(cfg)
41 Args: 42 cfg_filename: config filename 43 allow_unsafe: allow unsafe yaml syntax 44 """ 45 assert PathManager.isfile(cfg_filename), f"Config file '{cfg_filename}' does not exist!" 46 loaded_cfg = self.load_yaml_with_base(cfg_filename, allow_unsafe=allow_unsafe) 47 loaded_cfg = type(self)(loaded_cfg) 48
49 # defaults.py needs to import CfgNode 50 from .defaults import _C 51 52 latest_ver = _C.VERSION 53 assert ( 54 latest_ver == self.VERSION 55 ), "CfgNode.merge_from_file is only allowed on a config object of latest version!" 56 57 logger = logging.getLogger(__name__) 58
60 if loaded_ver is None: 61 from .compat import guess_version 62 63 loaded_ver = guess_version(loaded_cfg, cfg_filename) 64 assert loaded_ver <= self.VERSION, "Cannot merge a v{} config into a v{} config.".format( 65 loaded_ver, self.VERSION 66 ) 67 68 if loaded_ver == self.VERSION: 69 self.merge_from_other_cfg(loaded_cfg)
167 as its first argument. 168 """ 169 170 if init_func is not None: 171 assert ( 172 inspect.isfunction(init_func) 173 and from_config is None 174 and init_func.__name__ == "__init__" 175 ), "Incorrect use of @configurable. Check API documentation for examples." 176 177 @functools.wraps(init_func) 178 def wrapped(self, *args, **kwargs):
195 196 else: 197 if from_config is None: 198 return configurable # @configurable() is made equivalent to @configurable 199 assert inspect.isfunction( 200 from_config 201 ), "from_config argument of configurable must be a function!" 202 203 def wrapper(orig_func): 204 @functools.wraps(orig_func)
19 20 Returns: 21 dict 22 """ 23 assert dataclasses.is_dataclass(obj) and not isinstance( 24 obj, type 25 ), "dump_dataclass() requires an instance of a dataclass." 26 ret = {"_target_": _convert_target_to_string(type(obj))} 27 for f in dataclasses.fields(obj): 28 v = getattr(obj, f.name)
70 71 if isinstance(cls, str): 72 cls_name = cls 73 cls = locate(cls_name) 74 assert cls is not None, cls_name 75 else: 76 try: 77 cls_name = cls.__module__ + "." + cls.__qualname__
77 cls_name = cls.__module__ + "." + cls.__qualname__ 78 except Exception: 79 # target could be anything, so the above could fail 80 cls_name = str(cls) 81 assert callable(cls), f"_target_ {cls} does not define a callable object" 82 try: 83 return cls(**cfg) 84 except TypeError:
157 module = importlib.util.module_from_spec(spec) 158 module.__file__ = cur_file 159 with PathManager.open(cur_file) as f: 160 content = f.read() 161 exec(compile(content, cur_file, "exec"), module.__dict__) 162 for name in fromlist: # turn imported dict into DictConfig automatically 163 val = _cast_to_config(module.__dict__[name]) 164 module.__dict__[name] = val
186 accepts filename as a string, so more characters are allowed in the filename. 187 """ 188 caller_frame = inspect.stack()[1] 189 caller_fname = caller_frame[0].f_code.co_filename 190 assert caller_fname != "<string>", "load_rel Unable to find caller" 191 caller_dir = os.path.dirname(caller_fname) 192 filename = os.path.join(caller_dir, filename) 193 return LazyConfig.load(filename, keys)
219 content = f.read() 220 # Compile first with filename to: 221 # 1. make filename appears in stacktrace 222 # 2. make load_rel able to find its parent's (possibly remote) location 223 exec(compile(content, filename, "exec"), module_namespace) 224 225 ret = module_namespace 226 else:
261 """ 262 logger = logging.getLogger(__name__) 263 try: 264 cfg = deepcopy(cfg) 265 except Exception: 266 pass 267 else: 268 # if it's deep-copyable, then... 269 def _replace_type_by_name(x):
363 # Fallback. Does not support all the features and error checking like hydra. 364 for o in overrides: 365 key, value = o.split("=") 366 try: 367 value = eval(value, {}) 368 except NameError: 369 pass 370 safe_update(cfg, key, value)
2 import itertools 3 import logging 4 import numpy as np 5 import operator 6 import pickle 7 from typing import Any, Callable, Dict, List, Optional, Union 8 import torch 9 import torch.utils.data as torchdata
129 logger = logging.getLogger(__name__) 130 logger.info("Loading proposals from: {}".format(proposal_file)) 131 132 with PathManager.open(proposal_file, "rb") as f: 133 proposals = pickle.load(f, encoding="latin1") 134 135 # Rename the key names in D1 proposal files 136 rename_keys = {"indexes": "ids", "scores": "objectness_logits"}
175 classes = np.asarray( 176 [x["category_id"] for x in annos if not x.get("iscrowd", 0)], dtype=np.int 177 ) 178 if len(classes): 179 assert classes.min() >= 0, f"Got an invalid category_id={classes.min()}" 180 assert ( 181 classes.max() < num_classes 182 ), f"Got an invalid category_id={classes.max()} for a dataset of {num_classes} classes"
176 [x["category_id"] for x in annos if not x.get("iscrowd", 0)], dtype=np.int 177 ) 178 if len(classes): 179 assert classes.min() >= 0, f"Got an invalid category_id={classes.min()}" 180 assert ( 181 classes.max() < num_classes 182 ), f"Got an invalid category_id={classes.max()} for a dataset of {num_classes} classes" 183 histogram += np.histogram(classes, bins=hist_bins)[0] 184 185 N_COLS = min(6, len(class_names) * 2)
236 list[dict]: a list of dicts following the standard dataset dict format. 237 """ 238 if isinstance(names, str): 239 names = [names] 240 assert len(names), names 241 dataset_dicts = [DatasetCatalog.get(dataset_name) for dataset_name in names] 242 243 if isinstance(dataset_dicts[0], torchdata.Dataset):
248 return torchdata.ConcatDataset(dataset_dicts) 249 return dataset_dicts[0] 250 251 for dataset_name, dicts in zip(names, dataset_dicts): 252 assert len(dicts), "Dataset '{}' is empty!".format(dataset_name) 253 254 if proposal_files is not None: 255 assert len(names) == len(proposal_files)
251 for dataset_name, dicts in zip(names, dataset_dicts): 252 assert len(dicts), "Dataset '{}' is empty!".format(dataset_name) 253 254 if proposal_files is not None: 255 assert len(names) == len(proposal_files) 256 # load precomputed proposals from proposal files 257 dataset_dicts = [ 258 load_proposals_into_dataset(dataset_i_dicts, proposal_file)
274 print_instances_class_histogram(dataset_dicts, class_names) 275 except AttributeError: # class names are not available for this dataset 276 pass 277 278 assert len(dataset_dicts), "No valid data found in {}.".format(",".join(names)) 279 return dataset_dicts 280 281
304 iterable[list]. Length of each list is the batch size of the current 305 GPU. Each element in the list comes from the dataset. 306 """ 307 world_size = get_world_size() 308 assert ( 309 total_batch_size > 0 and total_batch_size % world_size == 0 310 ), "Total batch size ({}) must be divisible by the number of gpus ({}).".format( 311 total_batch_size, world_size 312 ) 313 batch_size = total_batch_size // world_size 314 315 if isinstance(dataset, torchdata.IterableDataset):
312 ) 313 batch_size = total_batch_size // world_size 314 315 if isinstance(dataset, torchdata.IterableDataset): 316 assert sampler is None, "sampler must be None if dataset is IterableDataset" 317 else: 318 dataset = ToIterableDataset(dataset, sampler) 319
434 if mapper is not None: 435 dataset = MapDataset(dataset, mapper) 436 437 if isinstance(dataset, torchdata.IterableDataset): 438 assert sampler is None, "sampler must be None if dataset is IterableDataset" 439 else: 440 if sampler is None: 441 sampler = TrainingSampler(len(dataset))
438 assert sampler is None, "sampler must be None if dataset is IterableDataset" 439 else: 440 if sampler is None: 441 sampler = TrainingSampler(len(dataset)) 442 assert isinstance(sampler, torchdata.Sampler), f"Expect a Sampler but got {type(sampler)}" 443 return build_batch_data_loader( 444 dataset, 445 sampler,
529 dataset = DatasetFromList(dataset, copy=False) 530 if mapper is not None: 531 dataset = MapDataset(dataset, mapper) 532 if isinstance(dataset, torchdata.IterableDataset): 533 assert sampler is None, "sampler must be None if dataset is IterableDataset" 534 else: 535 if sampler is None: 536 sampler = InferenceSampler(len(dataset))
32 name (str): the name that identifies a dataset, e.g. "coco_2014_train". 33 func (callable): a callable which takes no arguments and returns a list of dicts. 34 It must return the same results if called multiple times. 35 """ 36 assert callable(func), "You must register a function with `DatasetCatalog.register`!" 37 assert name not in self, "Dataset '{}' is already registered!".format(name) 38 self[name] = func 39
33 func (callable): a callable which takes no arguments and returns a list of dicts. 34 It must return the same results if called multiple times. 35 """ 36 assert callable(func), "You must register a function with `DatasetCatalog.register`!" 37 assert name not in self, "Dataset '{}' is already registered!".format(name) 38 self[name] = func 39 40 def get(self, name):
144 145 # Ensure that metadata of the same name stays consistent 146 try: 147 oldval = getattr(self, key) 148 assert oldval == val, ( 149 "Attribute '{}' in the metadata of '{}' cannot be set " 150 "to a different value!\n{} != {}".format(key, self.name, oldval, val) 151 ) 152 except AttributeError: 153 super().__setattr__(key, val) 154
199 Returns: 200 Metadata: The :class:`Metadata` instance associated with this name, 201 or create an empty one if none is available. 202 """ 203 assert len(name) 204 r = super().get(name, None) 205 if r is None: 206 r = self[name] = Metadata(name=name)
3 import copy 4 import itertools 5 import logging 6 import numpy as np 7 import pickle 8 import random 9 from typing import Callable, Union 10 import torch
70 """ 71 self._dataset = dataset 72 self._map_func = PicklableWrapper(map_func) # wrap so that a lambda will work 73 74 self._rng = random.Random(42) 75 self._fallback_candidates = set(range(len(dataset))) 76 77 def __new__(cls, dataset, map_func):
152 end_addr = self._addr[idx].item() 153 bytes = memoryview(self._lst[start_addr:end_addr].numpy()) 154 155 # @lint-ignore PYTHONPICKLEISBAD 156 return pickle.loads(bytes) 157 158 159 _DEFAULT_DATASET_FROM_LIST_SERIALIZE_METHOD = _TorchSerializedList
237 Most samplers (like our TrainingSampler) do not shard based on dataloader worker id 238 and this argument should be set to True. But certain samplers may be already 239 sharded, in that case this argument should be set to False. 240 """ 241 assert not isinstance(dataset, data.IterableDataset), dataset 242 assert isinstance(sampler, Sampler), sampler 243 self.dataset = dataset 244 self.sampler = sampler
238 and this argument should be set to True. But certain samplers may be already 239 sharded, in that case this argument should be set to False. 240 """ 241 assert not isinstance(dataset, data.IterableDataset), dataset 242 assert isinstance(sampler, Sampler), sampler 243 self.dataset = dataset 244 self.sampler = sampler 245 self.shard_sampler = shard_sampler
65 recompute_boxes: whether to overwrite bounding box annotations 66 by computing tight bounding boxes from instance mask annotations. 67 """ 68 if recompute_boxes: 69 assert use_instance_mask, "recompute_boxes requires instance masks" 70 # fmt: off 71 self.is_train = is_train 72 self.augmentations = T.AugmentationList(augmentations)
234 235 def _get_coco_instances_meta(): 236 thing_ids = [k["id"] for k in COCO_CATEGORIES if k["isthing"] == 1] 237 thing_colors = [k["color"] for k in COCO_CATEGORIES if k["isthing"] == 1] 238 assert len(thing_ids) == 80, len(thing_ids) 239 # Mapping from the incontiguous COCO category id to an id in [0, 79] 240 thing_dataset_id_to_contiguous_id = {k: i for i, k in enumerate(thing_ids)} 241 thing_classes = [k["name"] for k in COCO_CATEGORIES if k["isthing"] == 1]
251 """ 252 Returns metadata for "separated" version of the panoptic segmentation dataset. 253 """ 254 stuff_ids = [k["id"] for k in COCO_CATEGORIES if k["isthing"] == 0] 255 assert len(stuff_ids) == 53, len(stuff_ids) 256 257 # For semantic segmentation, this mapping maps from contiguous stuff id 258 # (in [0, 53], used in models) to ids in the dataset (used for processing results)
35 for basename in PathManager.ls(city_img_dir): 36 image_file = os.path.join(city_img_dir, basename) 37 38 suffix = "leftImg8bit.png" 39 assert basename.endswith(suffix), basename 40 basename = basename[: -len(suffix)] 41 42 instance_file = os.path.join(city_gt_dir, basename + "gtFine_instanceIds.png")
43 label_file = os.path.join(city_gt_dir, basename + "gtFine_labelIds.png") 44 json_file = os.path.join(city_gt_dir, basename + "gtFine_polygons.json") 45 46 files.append((image_file, instance_file, label_file, json_file)) 47 assert len(files), "No images found in {}".format(image_dir) 48 for f in files[0]: 49 assert PathManager.isfile(f), f 50 return files
45 46 files.append((image_file, instance_file, label_file, json_file)) 47 assert len(files), "No images found in {}".format(image_dir) 48 for f in files[0]: 49 assert PathManager.isfile(f), f 50 return files 51 52
63 list[dict]: a list of dicts in Detectron2 standard format. (See 64 `Using Custom Datasets </tutorials/datasets.html>`_ ) 65 """ 66 if from_json: 67 assert to_polygons, ( 68 "Cityscapes's json annotations are in polygon format. " 69 "Converting to mask format is not supported now." 70 ) 71 files = _get_cityscapes_files(image_dir, gt_dir) 72 73 logger.info("Preprocessing cityscapes annotations ...")
117 "height": jsonobj["imgHeight"], 118 "width": jsonobj["imgWidth"], 119 } 120 ) 121 assert len(ret), f"No images found in {image_dir}!" 122 assert PathManager.isfile( 123 ret[0]["sem_seg_file_name"] 124 ), "Please generate labelTrainIds.png with cityscapesscripts/preparation/createTrainIdLabelImgs.py" # noqa
118 "width": jsonobj["imgWidth"], 119 } 120 ) 121 assert len(ret), f"No images found in {image_dir}!" 122 assert PathManager.isfile( 123 ret[0]["sem_seg_file_name"] 124 ), "Please generate labelTrainIds.png with cityscapesscripts/preparation/createTrainIdLabelImgs.py" # noqa 125 return ret 126 127
26 for basename in PathManager.ls(city_img_dir): 27 image_file = os.path.join(city_img_dir, basename) 28 29 suffix = "_leftImg8bit.png" 30 assert basename.endswith(suffix), basename 31 basename = os.path.basename(basename)[: -len(suffix)] 32 33 image_dict[basename] = image_file
33 image_dict[basename] = image_file 34 35 for ann in json_info["annotations"]: 36 image_file = image_dict.get(ann["image_id"], None) 37 assert image_file is not None, "No image {} found for annotation {}".format( 38 ann["image_id"], ann["file_name"] 39 ) 40 label_file = os.path.join(gt_dir, ann["file_name"]) 41 segments_info = ann["segments_info"] 42
41 segments_info = ann["segments_info"] 42 43 files.append((image_file, label_file, segments_info)) 44 45 assert len(files), "No images found in {}".format(image_dir) 46 assert PathManager.isfile(files[0][0]), files[0][0] 47 assert PathManager.isfile(files[0][1]), files[0][1] 48 return files
42 43 files.append((image_file, label_file, segments_info)) 44 45 assert len(files), "No images found in {}".format(image_dir) 46 assert PathManager.isfile(files[0][0]), files[0][0] 47 assert PathManager.isfile(files[0][1]), files[0][1] 48 return files 49
43 files.append((image_file, label_file, segments_info)) 44 45 assert len(files), "No images found in {}".format(image_dir) 46 assert PathManager.isfile(files[0][0]), files[0][0] 47 assert PathManager.isfile(files[0][1]), files[0][1] 48 return files 49 50
75 segment_info["category_id"] 76 ] 77 return segment_info 78 79 assert os.path.exists( 80 gt_json 81 ), "Please run `python cityscapesscripts/preparation/createPanopticImgs.py` to generate label files." # noqa 82 with open(gt_json) as f: 83 json_info = json.load(f) 84 files = get_cityscapes_panoptic_files(image_dir, gt_dir, json_info)
98 "pan_seg_file_name": label_file, 99 "segments_info": segments_info, 100 } 101 ) 102 assert len(ret), f"No images found in {image_dir}!" 103 assert PathManager.isfile( 104 ret[0]["sem_seg_file_name"] 105 ), "Please generate labelTrainIds.png with cityscapesscripts/preparation/createTrainIdLabelImgs.py" # noqa
99 "segments_info": segments_info, 100 } 101 ) 102 assert len(ret), f"No images found in {image_dir}!" 103 assert PathManager.isfile( 104 ret[0]["sem_seg_file_name"] 105 ), "Please generate labelTrainIds.png with cityscapesscripts/preparation/createTrainIdLabelImgs.py" # noqa 106 assert PathManager.isfile( 107 ret[0]["pan_seg_file_name"] 108 ), "Please generate panoptic annotation with python cityscapesscripts/preparation/createPanopticImgs.py" # noqa
102 assert len(ret), f"No images found in {image_dir}!" 103 assert PathManager.isfile( 104 ret[0]["sem_seg_file_name"] 105 ), "Please generate labelTrainIds.png with cityscapesscripts/preparation/createTrainIdLabelImgs.py" # noqa 106 assert PathManager.isfile( 107 ret[0]["pan_seg_file_name"] 108 ), "Please generate panoptic annotation with python cityscapesscripts/preparation/createPanopticImgs.py" # noqa 109 return ret 110 111
136 # The popular valminusminival & minival annotations for COCO2014 contain this bug. 137 # However the ratio of buggy annotations there is tiny and does not affect accuracy. 138 # Therefore we explicitly white-list them. 139 ann_ids = [ann["id"] for anns_per_image in anns for ann in anns_per_image] 140 assert len(set(ann_ids)) == len(ann_ids), "Annotation ids in '{}' are not unique!".format( 141 json_file 142 ) 143 144 imgs_anns = list(zip(imgs, anns)) 145 logger.info("Loaded {} images in COCO format from {}".format(len(imgs_anns), json_file))
165 166 # The original COCO valminusminival2014 & minival2014 annotation files 167 # actually contains bugs that, together with certain ways of using COCO API, 168 # can trigger this assertion. 169 assert anno["image_id"] == image_id 170 171 assert anno.get("ignore", 0) == 0, '"ignore" in COCO json file is not supported.' 172
167 # actually contains bugs that, together with certain ways of using COCO API, 168 # can trigger this assertion. 169 assert anno["image_id"] == image_id 170 171 assert anno.get("ignore", 0) == 0, '"ignore" in COCO json file is not supported.' 172 173 obj = {key: anno[key] for key in ann_keys if key in anno} 174 if "bbox" in obj and len(obj["bbox"]) == 0:
270 (os.path.join(gt_root, f) for f in PathManager.ls(gt_root) if f.endswith(gt_ext)), 271 key=lambda file_path: file2id(gt_root, file_path), 272 ) 273 274 assert len(gt_files) > 0, "No annotations found in {}.".format(gt_root) 275 276 # Use the intersection, so that val2017_100 annotations can run smoothly with val2017 images 277 if len(input_files) != len(gt_files):
492 leave it as an empty dict. 493 json_file (str): path to the json instance annotation file. 494 image_root (str or path-like): directory which contains all the images. 495 """ 496 assert isinstance(name, str), name 497 assert isinstance(json_file, (str, os.PathLike)), json_file 498 assert isinstance(image_root, (str, os.PathLike)), image_root 499 # 1. register a function which returns dicts
493 json_file (str): path to the json instance annotation file. 494 image_root (str or path-like): directory which contains all the images. 495 """ 496 assert isinstance(name, str), name 497 assert isinstance(json_file, (str, os.PathLike)), json_file 498 assert isinstance(image_root, (str, os.PathLike)), image_root 499 # 1. register a function which returns dicts 500 DatasetCatalog.register(name, lambda: load_coco_json(json_file, image_root, name))
494 image_root (str or path-like): directory which contains all the images. 495 """ 496 assert isinstance(name, str), name 497 assert isinstance(json_file, (str, os.PathLike)), json_file 498 assert isinstance(image_root, (str, os.PathLike)), image_root 499 # 1. register a function which returns dicts 500 DatasetCatalog.register(name, lambda: load_coco_json(json_file, image_root, name)) 501
522 import custom_detectron2.data.datasets # noqa # add pre-defined metadata 523 import sys 524 525 logger = setup_logger(name=__name__) 526 assert sys.argv[3] in DatasetCatalog.list() 527 meta = MetadataCatalog.get(sys.argv[3]) 528 529 dicts = load_coco_json(sys.argv[1], sys.argv[2], sys.argv[3])
56 "pan_seg_file_name": label_file, 57 "segments_info": segments_info, 58 } 59 ) 60 assert len(ret), f"No images found in {image_dir}!" 61 assert PathManager.isfile(ret[0]["file_name"]), ret[0]["file_name"] 62 assert PathManager.isfile(ret[0]["pan_seg_file_name"]), ret[0]["pan_seg_file_name"] 63 return ret
57 "segments_info": segments_info, 58 } 59 ) 60 assert len(ret), f"No images found in {image_dir}!" 61 assert PathManager.isfile(ret[0]["file_name"]), ret[0]["file_name"] 62 assert PathManager.isfile(ret[0]["pan_seg_file_name"]), ret[0]["pan_seg_file_name"] 63 return ret 64
58 } 59 ) 60 assert len(ret), f"No images found in {image_dir}!" 61 assert PathManager.isfile(ret[0]["file_name"]), ret[0]["file_name"] 62 assert PathManager.isfile(ret[0]["pan_seg_file_name"]), ret[0]["pan_seg_file_name"] 63 return ret 64 65
180 The function assumes that the same key in different dicts has the same value. 181 """ 182 results = [] 183 sem_seg_file_to_entry = {x["file_name"]: x for x in sem_seg_dicts} 184 assert len(sem_seg_file_to_entry) > 0 185 186 for det_dict in detection_dicts: 187 dic = copy.copy(det_dict)
208 from PIL import Image 209 import numpy as np 210 211 logger = setup_logger(name=__name__) 212 assert sys.argv[4] in DatasetCatalog.list() 213 meta = MetadataCatalog.get(sys.argv[4]) 214 215 dicts = load_coco_panoptic_json(sys.argv[3], sys.argv[1], sys.argv[2], meta.as_dict())
101 anns = [lvis_api.img_ann_map[img_id] for img_id in img_ids] 102 103 # Sanity check that each annotation has a unique id 104 ann_ids = [ann["id"] for anns_per_image in anns for ann in anns_per_image] 105 assert len(set(ann_ids)) == len(ann_ids), "Annotation ids in '{}' are not unique".format( 106 json_file 107 ) 108 109 imgs_anns = list(zip(imgs, anns)) 110
139 for anno in anno_dict_list: 140 # Check that the image_id in this annotation is the same as 141 # the image_id we're looking at. 142 # This fails only when the data parsing logic or the annotation file is buggy. 143 assert anno["image_id"] == image_id 144 obj = {"bbox": anno["bbox"], "bbox_mode": BoxMode.XYWH_ABS} 145 # LVIS data loader can be used to load COCO dataset categories. In this case `meta` 146 # variable will have a field with COCO-specific category mapping.
150 obj["category_id"] = anno["category_id"] - 1 # Convert 1-indexed to 0-indexed 151 segm = anno["segmentation"] # list[list[float]] 152 # filter out invalid polygons (< 3 points) 153 valid_segm = [poly for poly in segm if len(poly) % 2 == 0 and len(poly) >= 6] 154 assert len(segm) == len( 155 valid_segm 156 ), "Annotation contains an invalid polygon with < 3 points" 157 assert len(segm) > 0 158 obj["segmentation"] = segm 159 for extra_ann_key in extra_annotation_keys:
153 valid_segm = [poly for poly in segm if len(poly) % 2 == 0 and len(poly) >= 6] 154 assert len(segm) == len( 155 valid_segm 156 ), "Annotation contains an invalid polygon with < 3 points" 157 assert len(segm) > 0 158 obj["segmentation"] = segm 159 for extra_ann_key in extra_annotation_keys: 160 obj[extra_ann_key] = anno[extra_ann_key]
184 raise ValueError("No built-in metadata for dataset {}".format(dataset_name)) 185 186 187 def _get_lvis_instances_meta_v0_5(): 188 assert len(LVIS_V0_5_CATEGORIES) == 1230 189 cat_ids = [k["id"] for k in LVIS_V0_5_CATEGORIES] 190 assert min(cat_ids) == 1 and max(cat_ids) == len( 191 cat_ids
186 187 def _get_lvis_instances_meta_v0_5(): 188 assert len(LVIS_V0_5_CATEGORIES) == 1230 189 cat_ids = [k["id"] for k in LVIS_V0_5_CATEGORIES] 190 assert min(cat_ids) == 1 and max(cat_ids) == len( 191 cat_ids 192 ), "Category ids are not in [1, #categories], as expected" 193 # Ensure that the category list is sorted by id 194 lvis_categories = sorted(LVIS_V0_5_CATEGORIES, key=lambda x: x["id"]) 195 thing_classes = [k["synonyms"][0] for k in lvis_categories]
197 return meta 198 199 200 def _get_lvis_instances_meta_v1(): 201 assert len(LVIS_V1_CATEGORIES) == 1203 202 cat_ids = [k["id"] for k in LVIS_V1_CATEGORIES] 203 assert min(cat_ids) == 1 and max(cat_ids) == len( 204 cat_ids
199 200 def _get_lvis_instances_meta_v1(): 201 assert len(LVIS_V1_CATEGORIES) == 1203 202 cat_ids = [k["id"] for k in LVIS_V1_CATEGORIES] 203 assert min(cat_ids) == 1 and max(cat_ids) == len( 204 cat_ids 205 ), "Category ids are not in [1, #categories], as expected" 206 # Ensure that the category list is sorted by id 207 lvis_categories = sorted(LVIS_V1_CATEGORIES, key=lambda x: x["id"]) 208 thing_classes = [k["synonyms"][0] for k in lvis_categories]
2 # Copyright (c) Facebook, Inc. and its affiliates. 3 4 import numpy as np 5 import os 6 import xml.etree.ElementTree as ET 7 from typing import List, Tuple, Union 8 9 from custom_detectron2.data import DatasetCatalog, MetadataCatalog
41 anno_file = os.path.join(annotation_dirname, fileid + ".xml") 42 jpeg_file = os.path.join(dirname, "JPEGImages", fileid + ".jpg") 43 44 with PathManager.open(anno_file) as f: 45 tree = ET.parse(f) 46 47 r = { 48 "file_name": jpeg_file,
312 elif isinstance(segm, dict): 313 # RLE 314 mask = mask_util.decode(segm) 315 mask = transforms.apply_segmentation(mask) 316 assert tuple(mask.shape[:2]) == image_size 317 annotation["segmentation"] = mask 318 else: 319 raise ValueError(
418 raise ValueError( 419 "Failed to use mask_format=='polygon' from the given annotations!" 420 ) from e 421 else: 422 assert mask_format == "bitmask", mask_format 423 masks = [] 424 for segm in segms: 425 if isinstance(segm, list):
428 elif isinstance(segm, dict): 429 # COCO RLE 430 masks.append(mask_util.decode(segm)) 431 elif isinstance(segm, np.ndarray): 432 assert segm.ndim == 2, "Expect segmentation of 2 dimensions, got {}.".format( 433 segm.ndim 434 ) 435 # mask array 436 masks.append(segm) 437 else:
499 Returns: 500 Instances: the filtered instances. 501 tensor[bool], optional: boolean mask of filtered instances 502 """ 503 assert by_box or by_mask 504 r = [] 505 if by_box: 506 r.append(instances.gt_boxes.nonempty(threshold=box_threshold))
580 """ 581 crop_size = np.asarray(crop_size, dtype=np.int32) 582 bbox = BoxMode.convert(instance["bbox"], instance["bbox_mode"], BoxMode.XYXY_ABS) 583 center_yx = (bbox[1] + bbox[3]) * 0.5, (bbox[0] + bbox[2]) * 0.5 584 assert ( 585 image_size[0] >= center_yx[0] and image_size[1] >= center_yx[1] 586 ), "The annotation bounding box is outside of the image!" 587 assert ( 588 image_size[0] >= crop_size[0] and image_size[1] >= crop_size[1] 589 ), "Crop size is larger than image size!"
583 center_yx = (bbox[1] + bbox[3]) * 0.5, (bbox[0] + bbox[2]) * 0.5 584 assert ( 585 image_size[0] >= center_yx[0] and image_size[1] >= center_yx[1] 586 ), "The annotation bounding box is outside of the image!" 587 assert ( 588 image_size[0] >= crop_size[0] and image_size[1] >= crop_size[1] 589 ), "Crop size is larger than image size!" 590 591 min_yx = np.maximum(np.floor(center_yx).astype(np.int32) - crop_size, 0) 592 max_yx = np.maximum(np.asarray(image_size, dtype=np.int32) - crop_size, 0)
97 among workers (require synchronization among all workers). 98 """ 99 super().__init__(size=size, shuffle=shuffle, seed=seed_shuffle) 100 101 assert 0.0 < subset_ratio <= 1.0 102 self._size_subset = int(size * subset_ratio) 103 assert self._size_subset > 0 104 if seed_subset is None:
99 super().__init__(size=size, shuffle=shuffle, seed=seed_shuffle) 100 101 assert 0.0 < subset_ratio <= 1.0 102 self._size_subset = int(size * subset_ratio) 103 assert self._size_subset > 0 104 if seed_subset is None: 105 seed_subset = comm.shared_random_seed() 106 self._seed_subset = int(seed_subset)
255 Args: 256 size (int): the total number of data of the underlying dataset to sample from 257 """ 258 self._size = size 259 assert size > 0 260 self._rank = comm.get_rank() 261 self._world_size = comm.get_world_size() 262 self._local_indices = self._get_local_indices(size, self._world_size, self._rank)
26 "torch.utils.data.Sampler, but got sampler={}".format(sampler) 27 ) 28 self.sampler = sampler 29 self.group_ids = np.asarray(group_ids) 30 assert self.group_ids.ndim == 1 31 self.batch_size = batch_size 32 groups = np.unique(self.group_ids).tolist() 33
24 ] 25 26 27 def _check_img_dtype(img): 28 assert isinstance(img, np.ndarray), "[Augmentation] Needs an numpy array, but got a {}!".format( 29 type(img) 30 ) 31 assert not isinstance(img.dtype, np.integer) or ( 32 img.dtype == np.uint8 33 ), "[Augmentation] Got image of type {}, use uint8 or floating points instead!".format(
27 def _check_img_dtype(img): 28 assert isinstance(img, np.ndarray), "[Augmentation] Needs an numpy array, but got a {}!".format( 29 type(img) 30 ) 31 assert not isinstance(img.dtype, np.integer) or ( 32 img.dtype == np.uint8 33 ), "[Augmentation] Got image of type {}, use uint8 or floating points instead!".format( 34 img.dtype 35 ) 36 assert img.ndim in [2, 3], img.ndim 37 38
32 img.dtype == np.uint8 33 ), "[Augmentation] Got image of type {}, use uint8 or floating points instead!".format( 34 img.dtype 35 ) 36 assert img.ndim in [2, 3], img.ndim 37 38 39 def _get_aug_input_args(aug, aug_input) -> List[Any]:
165 Transform: the transform that is applied on the input. 166 """ 167 args = _get_aug_input_args(self, aug_input) 168 tfm = self.get_transform(*args) 169 assert isinstance(tfm, (Transform, TransformList)), ( 170 f"{type(self)}.get_transform must return an instance of Transform! " 171 f"Got {type(tfm)} instead." 172 ) 173 aug_input.transform(tfm) 174 return tfm 175
192 sig = inspect.signature(self.__init__) 193 classname = type(self).__name__ 194 argstr = [] 195 for name, param in sig.parameters.items(): 196 assert ( 197 param.kind != param.VAR_POSITIONAL and param.kind != param.VAR_KEYWORD 198 ), "The default __repr__ doesn't support *args or **kwargs" 199 assert hasattr(self, name), ( 200 "Attribute {} not found! " 201 "Default __repr__ only works if attributes match the constructor.".format(name)
195 for name, param in sig.parameters.items(): 196 assert ( 197 param.kind != param.VAR_POSITIONAL and param.kind != param.VAR_KEYWORD 198 ), "The default __repr__ doesn't support *args or **kwargs" 199 assert hasattr(self, name), ( 200 "Attribute {} not found! " 201 "Default __repr__ only works if attributes match the constructor.".format(name) 202 ) 203 attr = getattr(self, name) 204 default = param.default 205 if default is attr:
233 """ 234 Wrap Transform into Augmentation. 235 Private, used internally to implement augmentations. 236 """ 237 assert isinstance(tfm_or_aug, (Transform, Augmentation)), tfm_or_aug 238 if isinstance(tfm_or_aug, Augmentation): 239 return tfm_or_aug 240 else:
60 the wrapper transformation is applied 61 """ 62 super().__init__() 63 self.aug = _transform_to_aug(tfm_or_aug) 64 assert 0.0 <= prob <= 1.0, f"Probablity must be between 0.0 and 1.0 (given: {prob})" 65 self.prob = prob 66 67 def get_transform(self, *args):
151 max_size (int): maximum allowed longest edge length. 152 sample_style (str): either "range" or "choice". 153 """ 154 super().__init__() 155 assert sample_style in ["range", "choice"], sample_style 156 157 self.is_range = sample_style == "range" 158 if isinstance(short_edge_length, int):
157 self.is_range = sample_style == "range" 158 if isinstance(short_edge_length, int): 159 short_edge_length = (short_edge_length, short_edge_length) 160 if self.is_range: 161 assert len(short_edge_length) == 2, ( 162 "short_edge_length must be two values using 'range' sample style." 163 f" Got {short_edge_length}!" 164 ) 165 self._init(locals()) 166 167 @torch.jit.unused
271 Default: None, which means that the center of rotation is the center of the image 272 center has no effect if expand=True because it only affects shifting 273 """ 274 super().__init__() 275 assert sample_style in ["range", "choice"], sample_style 276 self.is_range = sample_style == "range" 277 if isinstance(angle, (float, int)): 278 angle = (angle, angle)
393 """ 394 # TODO style of relative_range and absolute_range are not consistent: 395 # one takes (h, w) but another takes (min, max) 396 super().__init__() 397 assert crop_type in ["relative_range", "relative", "absolute", "absolute_range"] 398 self._init(locals()) 399 400 def get_transform(self, image):
399 400 def get_transform(self, image): 401 h, w = image.shape[:2] 402 croph, cropw = self.get_crop_size((h, w)) 403 assert h >= croph and w >= cropw, "Shape computation in {} has bugs.".format(self) 404 h0 = np.random.randint(h - croph + 1) 405 w0 = np.random.randint(w - cropw + 1) 406 return CropTransform(w0, h0, cropw, croph)
423 return int(h * ch + 0.5), int(w * cw + 0.5) 424 elif self.crop_type == "absolute": 425 return (min(self.crop_size[0], h), min(self.crop_size[1], w)) 426 elif self.crop_type == "absolute_range": 427 assert self.crop_size[0] <= self.crop_size[1] 428 ch = np.random.randint(min(h, self.crop_size[0]), min(h, self.crop_size[1]) + 1) 429 cw = np.random.randint(min(w, self.crop_size[0]), min(w, self.crop_size[1]) + 1) 430 return ch, cw
597 super().__init__() 598 self._init(locals()) 599 600 def get_transform(self, image): 601 assert image.shape[-1] == 3, "RandomSaturation only works on RGB images" 602 w = np.random.uniform(self.intensity_min, self.intensity_max) 603 grayscale = image.dot([0.299, 0.587, 0.114])[:, :, np.newaxis] 604 return BlendTransform(src_image=grayscale, src_weight=1 - w, dst_weight=w)
625 ) 626 self.eigen_vals = np.array([0.2175, 0.0188, 0.0045]) 627 628 def get_transform(self, image): 629 assert image.shape[-1] == 3, "RandomLighting only works on RGB images" 630 weights = np.random.normal(scale=self.scale, size=3) 631 return BlendTransform( 632 src_image=self.eigen_vecs.dot(weights * self.eigen_vals), src_weight=1.0, dst_weight=1.0
109 interp = Image.BILINEAR 110 self._set_attributes(locals()) 111 112 def apply_image(self, img, interp=None): 113 assert img.shape[:2] == (self.h, self.w) 114 assert len(img.shape) <= 4 115 interp_method = interp if interp is not None else self.interp 116
110 self._set_attributes(locals()) 111 112 def apply_image(self, img, interp=None): 113 assert img.shape[:2] == (self.h, self.w) 114 assert len(img.shape) <= 4 115 interp_method = interp if interp is not None else self.interp 116 117 if img.dtype == np.uint8:
202 img should be a numpy array, formatted as Height * Width * Nchannels 203 """ 204 if len(img) == 0 or self.angle % 360 == 0: 205 return img 206 assert img.shape[:2] == (self.h, self.w) 207 interp = interp if interp is not None else self.interp 208 return cv2.warpAffine(img, self.rm_image, (self.bound_w, self.bound_h), flags=interp) 209
291 [cfg.INPUT.MIN_SIZE_TEST, cfg.INPUT.MIN_SIZE_TEST], cfg.INPUT.MAX_SIZE_TEST 292 ) 293 294 self.input_format = cfg.INPUT.FORMAT 295 assert self.input_format in ["RGB", "BGR"], self.input_format 296 297 def __call__(self, original_image): 298 """
482 OrderedDict of results, if evaluation is enabled. Otherwise None. 483 """ 484 super().train(self.start_iter, self.max_iter) 485 if len(self.cfg.TEST.EXPECTED_RESULTS) and comm.is_main_process(): 486 assert hasattr( 487 self, "_last_eval_results" 488 ), "No evaluation results obtained during training!" 489 verify_results(self.cfg, self._last_eval_results) 490 return self._last_eval_results 491
592 logger = logging.getLogger(__name__) 593 if isinstance(evaluators, DatasetEvaluator): 594 evaluators = [evaluators] 595 if evaluators is not None: 596 assert len(cfg.DATASETS.TEST) == len(evaluators), "{} != {}".format( 597 len(cfg.DATASETS.TEST), len(evaluators) 598 ) 599 600 results = OrderedDict() 601 for idx, dataset_name in enumerate(cfg.DATASETS.TEST):
616 continue 617 results_i = inference_on_dataset(model, data_loader, evaluator) 618 results[dataset_name] = results_i 619 if comm.is_main_process(): 620 assert isinstance( 621 results_i, dict 622 ), "Evaluator must return a dict on the main process. Got {} instead.".format( 623 results_i 624 ) 625 logger.info("Evaluation results for {} in csv format:".format(dataset_name)) 626 print_csv_format(results_i) 627
677 cfg = cfg.clone() 678 frozen = cfg.is_frozen() 679 cfg.defrost() 680 681 assert ( 682 cfg.SOLVER.IMS_PER_BATCH % old_world_size == 0 683 ), "Invalid REFERENCE_WORLD_SIZE in config!" 684 scale = num_workers / old_world_size 685 bs = cfg.SOLVER.IMS_PER_BATCH = int(round(cfg.SOLVER.IMS_PER_BATCH * scale)) 686 lr = cfg.SOLVER.BASE_LR = cfg.SOLVER.BASE_LR * scale
169 period (int): 170 """ 171 self._writers = writers 172 for w in writers: 173 assert isinstance(w, EventWriter), w 174 self._period = period 175 176 def after_step(self):
234 """ 235 self._logger = logging.getLogger(__name__) 236 self._period = eval_period 237 self._val_metric = val_metric 238 assert mode in [ 239 "max", 240 "min", 241 ], f'Mode "{mode}" to `BestCheckpointer` is unknown. It should be one of {"max", "min"}.' 242 if mode == "max": 243 self._compare = operator.gt 244 else:
528 def _do_eval(self): 529 results = self._func() 530 531 if results: 532 assert isinstance( 533 results, dict 534 ), "Eval function must return a dict. Got {} instead.".format(results) 535 536 flattened_results = flatten_results_dict(results) 537 for k, v in flattened_results.items():
56 # https://github.com/pytorch/pytorch/pull/14391 57 # TODO prctl in spawned processes 58 59 if dist_url == "auto": 60 assert num_machines == 1, "dist_url=auto not supported in multi-machine jobs." 61 port = _find_free_port() 62 dist_url = f"tcp://127.0.0.1:{port}" 63 if num_machines > 1 and dist_url.startswith("file://"):
95 timeout=DEFAULT_TIMEOUT, 96 ): 97 has_gpu = torch.cuda.is_available() 98 if has_gpu: 99 assert num_gpus_per_machine <= torch.cuda.device_count() 100 global_rank = machine_rank * num_gpus_per_machine + local_rank 101 try: 102 dist.init_process_group(
127 hooks (list[Optional[HookBase]]): list of hooks 128 """ 129 hooks = [h for h in hooks if h is not None] 130 for h in hooks: 131 assert isinstance(h, HookBase) 132 # To avoid circular reference, hooks and trainer cannot own each other. 133 # This normally does not matter, but will cause memory leak if the 134 # involved objects contain __del__:
272 def run_step(self): 273 """ 274 Implement the standard training logic described above. 275 """ 276 assert self.model.training, "[SimpleTrainer] model was changed to eval mode!" 277 start = time.perf_counter() 278 """ 279 If you want to do something with the data, you can wrap the dataloader.
411 precision: torch.dtype as the target precision to cast to in computations 412 """ 413 unsupported = "AMPTrainer does not support single-process multi-device training!" 414 if isinstance(model, DistributedDataParallel): 415 assert not (model.device_ids and len(model.device_ids) > 1), unsupported 416 assert not isinstance(model, DataParallel), unsupported 417 418 super().__init__(model, data_loader, optimizer, gather_metric_period)
412 """ 413 unsupported = "AMPTrainer does not support single-process multi-device training!" 414 if isinstance(model, DistributedDataParallel): 415 assert not (model.device_ids and len(model.device_ids) > 1), unsupported 416 assert not isinstance(model, DataParallel), unsupported 417 418 super().__init__(model, data_loader, optimizer, gather_metric_period) 419
428 def run_step(self): 429 """ 430 Implement the AMP training logic. 431 """ 432 assert self.model.training, "[AMPTrainer] model was changed to eval mode!" 433 assert torch.cuda.is_available(), "[AMPTrainer] CUDA is required for AMP training!" 434 from torch.cuda.amp import autocast 435
429 """ 430 Implement the AMP training logic. 431 """ 432 assert self.model.training, "[AMPTrainer] model was changed to eval mode!" 433 assert torch.cuda.is_available(), "[AMPTrainer] CUDA is required for AMP training!" 434 from torch.cuda.amp import autocast 435 436 start = time.perf_counter()
35 self._working_dir = tempfile.TemporaryDirectory(prefix="cityscapes_eval_") 36 self._temp_dir = self._working_dir.name 37 # All workers will write to the same results directory 38 # TODO this does not work in distributed training 39 assert ( 40 comm.get_local_size() == comm.get_world_size() 41 ), "CityscapesEvaluator currently do not work with multiple machines." 42 self._temp_dir = comm.all_gather(self._temp_dir)[0] 43 if self._temp_dir != self._working_dir.name: 44 self._working_dir.cleanup()
110 # These lines are adopted from 111 # https://github.com/mcordts/cityscapesScripts/blob/master/cityscapesscripts/evaluation/evalInstanceLevelSemanticLabeling.py # noqa 112 gt_dir = PathManager.get_local_path(self._metadata.gt_dir) 113 groundTruthImgList = glob.glob(os.path.join(gt_dir, "*", "*_gtFine_instanceIds.png")) 114 assert len( 115 groundTruthImgList 116 ), "Cannot find any ground truth images to use for evaluation. Searched for: {}".format( 117 cityscapes_eval.args.groundTruthSearch 118 ) 119 predictionImgList = [] 120 for gt in groundTruthImgList: 121 predictionImgList.append(cityscapes_eval.getPrediction(gt, cityscapes_eval.args))
174 # These lines are adopted from 175 # https://github.com/mcordts/cityscapesScripts/blob/master/cityscapesscripts/evaluation/evalPixelLevelSemanticLabeling.py # noqa 176 gt_dir = PathManager.get_local_path(self._metadata.gt_dir) 177 groundTruthImgList = glob.glob(os.path.join(gt_dir, "*", "*_gtFine_labelIds.png")) 178 assert len( 179 groundTruthImgList 180 ), "Cannot find any ground truth images to use for evaluation. Searched for: {}".format( 181 cityscapes_eval.args.groundTruthSearch 182 ) 183 predictionImgList = [] 184 for gt in groundTruthImgList: 185 predictionImgList.append(cityscapes_eval.getPrediction(cityscapes_eval.args, gt))
6 import json 7 import logging 8 import numpy as np 9 import os 10 import pickle 11 from collections import OrderedDict 12 import custom_pycocotools.mask as mask_util 13 import torch
231 if hasattr(self._metadata, "thing_dataset_id_to_contiguous_id"): 232 dataset_id_to_contiguous_id = self._metadata.thing_dataset_id_to_contiguous_id 233 all_contiguous_ids = list(dataset_id_to_contiguous_id.values()) 234 num_classes = len(all_contiguous_ids) 235 assert min(all_contiguous_ids) == 0 and max(all_contiguous_ids) == num_classes - 1 236 237 reverse_id_mapping = {v: k for k, v in dataset_id_to_contiguous_id.items()} 238 for result in coco_results:
236 237 reverse_id_mapping = {v: k for k, v in dataset_id_to_contiguous_id.items()} 238 for result in coco_results: 239 category_id = result["category_id"] 240 assert category_id < num_classes, ( 241 f"A prediction has class={category_id}, " 242 f"but the dataset only has {num_classes} classes and " 243 f"predicted class id should be in [0, {num_classes - 1}]." 244 ) 245 result["category_id"] = reverse_id_mapping[category_id] 246 247 if self._output_dir:
260 "unofficial" if self._use_fast_impl else "official" 261 ) 262 ) 263 for task in sorted(tasks): 264 assert task in {"bbox", "segm", "keypoints"}, f"Got unknown task: {task}!" 265 coco_eval = ( 266 _evaluate_predictions_on_coco( 267 self._coco_api,
360 # Compute per-category AP 361 # from https://github.com/facebookresearch/Detectron/blob/a6a835f5b8208c45d0dce217ce9bbda915f44df7/detectron/datasets/json_dataset_evaluator.py#L222-L252 # noqa 362 precisions = coco_eval.eval["precision"] 363 # precision has dims (iou, recall, cls, area range, max dets) 364 assert len(class_names) == precisions.shape[2] 365 366 results_per_category = [] 367 for idx, name in enumerate(class_names):
480 [128**2, 256**2], # 128-256 481 [256**2, 512**2], # 256-512 482 [512**2, 1e5**2], 483 ] # 512-inf 484 assert area in areas, "Unknown area range: {}".format(area) 485 area_range = area_ranges[areas[area]] 486 gt_overlaps = [] 487 num_pos = 0
528 max_overlaps, argmax_overlaps = overlaps.max(dim=0) 529 530 # find which gt box is 'best' covered (i.e. 'best' = most iou) 531 gt_ovr, gt_ind = max_overlaps.max(dim=0) 532 assert gt_ovr >= 0 533 # find the proposal box that covers the best covered gt box 534 box_ind = argmax_overlaps[gt_ind] 535 # record the iou coverage of this gt box
533 # find the proposal box that covers the best covered gt box 534 box_ind = argmax_overlaps[gt_ind] 535 # record the iou coverage of this gt box 536 _gt_overlaps[j] = overlaps[box_ind, gt_ind] 537 assert _gt_overlaps[j] == gt_ovr 538 # mark the proposal box and the gt box as used 539 overlaps[box_ind, :] = -1 540 overlaps[:, gt_ind] = -1
575 ): 576 """ 577 Evaluate the coco results using COCOEval API. 578 """ 579 assert len(coco_results) > 0 580 581 if iou_type == "segm": 582 coco_results = copy.deepcopy(coco_results)
592 # For COCO, the default max_dets_per_image is [1, 10, 100]. 593 if max_dets_per_image is None: 594 max_dets_per_image = [1, 10, 100] # Default from COCOEval 595 else: 596 assert ( 597 len(max_dets_per_image) >= 3 598 ), "COCOeval requires maxDets (and max_dets_per_image) to have length at least 3" 599 # In the case that user supplies a custom input for max_dets_per_image, 600 # apply COCOevalMaxDets to evaluate AP with the custom input. 601 if max_dets_per_image[2] != 100:
608 609 if iou_type == "keypoints": 610 # Use the COCO default keypoint OKS sigmas unless overrides are specified 611 if kpt_oks_sigmas: 612 assert hasattr(coco_eval.params, "kpt_oks_sigmas"), "custom_pycocotools is too old!" 613 coco_eval.params.kpt_oks_sigmas = np.array(kpt_oks_sigmas) 614 # COCOAPI requires every detection and every gt to have keypoints, so 615 # we just take the first entry from both
615 # we just take the first entry from both 616 num_keypoints_dt = len(coco_results[0]["keypoints"]) // 3 617 num_keypoints_gt = len(next(iter(coco_gt.anns.values()))["keypoints"]) // 3 618 num_keypoints_oks = len(coco_eval.params.kpt_oks_sigmas) 619 assert num_keypoints_oks == num_keypoints_dt == num_keypoints_gt, ( 620 f"[COCOEvaluator] Prediction contain {num_keypoints_dt} keypoints. " 621 f"Ground truth contains {num_keypoints_gt} keypoints. " 622 f"The length of cfg.TEST.KEYPOINT_OKS_SIGMAS is {num_keypoints_oks}. " 623 "They have to agree with each other. For meaning of OKS, please refer to " 624 "http://cocodataset.org/#keypoints-eval." 625 ) 626 627 coco_eval.evaluate() 628 coco_eval.accumulate()
92 for evaluator in self._evaluators: 93 result = evaluator.evaluate() 94 if is_main_process() and result is not None: 95 for k, v in result.items(): 96 assert ( 97 k not in results 98 ), "Different evaluators produce results with the same key {}".format(k) 99 results[k] = v 100 return results 101
101 support changing parameter settings from those used by self.evaluate() 102 """ 103 logger.info("Accumulating evaluation results...") 104 tic = time.time() 105 assert hasattr( 106 self, "_evalImgs_cpp" 107 ), "evaluate() must be called before accmulate() is called." 108 109 self.eval = _C.COCOevalAccumulate(self._paramsEval, self._evalImgs_cpp) 110
3 import itertools 4 import json 5 import logging 6 import os 7 import pickle 8 from collections import OrderedDict 9 import torch 10
246 [128**2, 256**2], # 128-256 247 [256**2, 512**2], # 256-512 248 [512**2, 1e5**2], 249 ] # 512-inf 250 assert area in areas, "Unknown area range: {}".format(area) 251 area_range = area_ranges[areas[area]] 252 gt_overlaps = [] 253 num_pos = 0
292 max_overlaps, argmax_overlaps = overlaps.max(dim=0) 293 294 # find which gt box is 'best' covered (i.e. 'best' = most iou) 295 gt_ovr, gt_ind = max_overlaps.max(dim=0) 296 assert gt_ovr >= 0 297 # find the proposal box that covers the best covered gt box 298 box_ind = argmax_overlaps[gt_ind] 299 # record the iou coverage of this gt box
297 # find the proposal box that covers the best covered gt box 298 box_ind = argmax_overlaps[gt_ind] 299 # record the iou coverage of this gt box 300 _gt_overlaps[j] = overlaps[box_ind, gt_ind] 301 assert _gt_overlaps[j] == gt_ovr 302 # mark the proposal box and the gt box as used 303 overlaps[box_ind, :] = -1 304 overlaps[:, gt_ind] = -1
4 import logging 5 import numpy as np 6 import os 7 import tempfile 8 import xml.etree.ElementTree as ET 9 from collections import OrderedDict, defaultdict 10 from functools import lru_cache 11 import torch
42 ) 43 self._anno_file_template = os.path.join(annotation_dir_local, "{}.xml") 44 self._image_set_path = os.path.join(meta.dirname, "ImageSets", "Main", meta.split + ".txt") 45 self._class_names = meta.thing_classes 46 assert meta.year in [2007, 2012], meta.year 47 self._is_2007 = meta.year == 2007 48 self._cpu_device = torch.device("cpu") 49 self._logger = logging.getLogger(__name__)
131 @lru_cache(maxsize=None) 132 def parse_rec(filename): 133 """Parse a PASCAL VOC xml file.""" 134 with PathManager.open(filename) as f: 135 tree = ET.parse(f) 136 objects = [] 137 for obj in tree.findall("object"): 138 obj_struct = {}
56 57 def compute_iou_dt_gt(self, dt, gt, is_crowd): 58 if self.is_rotated(dt) or self.is_rotated(gt): 59 # TODO: take is_crowd into consideration 60 assert all(c == 0 for c in is_crowd) 61 dt = RotatedBoxes(self.boxlist_to_tensor(dt, output_box_dim=5)) 62 gt = RotatedBoxes(self.boxlist_to_tensor(gt, output_box_dim=5)) 63 return pairwise_iou_rotated(dt, gt)
79 dt = [dt[i] for i in inds] 80 if len(dt) > p.maxDets[-1]: 81 dt = dt[0 : p.maxDets[-1]] 82 83 assert p.iouType == "bbox", "unsupported iouType for iou computation" 84 85 g = [g["bbox"] for g in gt] 86 d = [d["bbox"] for d in dt]
173 return 174 175 self._logger.info("Evaluating predictions ...") 176 177 assert self._tasks is None or set(self._tasks) == { 178 "bbox" 179 }, "[RotatedCOCOEvaluator] Only bbox evaluation is supported" 180 coco_eval = ( 181 self._evaluate_predictions_on_coco(self._coco_api, coco_results) 182 if len(coco_results) > 0
192 def _evaluate_predictions_on_coco(self, coco_gt, coco_results): 193 """ 194 Evaluate the coco results using COCOEval API. 195 """ 196 assert len(coco_results) > 0 197 198 coco_dt = coco_gt.loadRes(coco_results) 199
89 self._class_names = meta.stuff_classes 90 self.sem_seg_loading_fn = sem_seg_loading_fn 91 self._num_classes = len(meta.stuff_classes) 92 if num_classes is not None: 93 assert self._num_classes == num_classes, f"{self._num_classes} != {num_classes}" 94 self._ignore_label = ignore_label if ignore_label is not None else meta.ignore_label 95 96 # This is because cv2.erode did not work for int datatype. Only works for uint8.
236 """ 237 json_list = [] 238 for label in np.unique(sem_seg): 239 if self._contiguous_id_to_dataset_id is not None: 240 assert ( 241 label in self._contiguous_id_to_dataset_id 242 ), "Label {} is not in the metadata info for {}".format(label, self._dataset_name) 243 dataset_id = self._contiguous_id_to_dataset_id[label] 244 else: 245 dataset_id = int(label)
251 ) 252 return json_list 253 254 def _mask_to_boundary(self, mask: np.ndarray, dilation_ratio=0.02): 255 assert mask.ndim == 2, "mask_to_boundary expects a 2-dimensional image" 256 h, w = mask.shape 257 diag_len = np.sqrt(h**2 + w**2) 258 dilation = max(1, int(round(dilation_ratio * diag_len)))
14 Args: 15 results (OrderedDict[dict]): task_name -> {metric -> score} 16 unordered dict can also be printed, but in arbitrary order 17 """ 18 assert isinstance(results, Mapping) or not len(results), results 19 logger = logging.getLogger(__name__) 20 for task, res in results.items(): 21 if isinstance(res, Mapping):
52 inputs: sample inputs that the given model takes for inference. 53 Will be used to trace the model. For most models, random inputs with 54 no detected objects will not work as they lead to wrong traces. 55 """ 56 assert isinstance(cfg, CfgNode), cfg 57 assert isinstance(model, torch.nn.Module), type(model) 58 59 # TODO make it support custom models, by passing in c2 model directly
53 Will be used to trace the model. For most models, random inputs with 54 no detected objects will not work as they lead to wrong traces. 55 """ 56 assert isinstance(cfg, CfgNode), cfg 57 assert isinstance(model, torch.nn.Module), type(model) 58 59 # TODO make it support custom models, by passing in c2 model directly 60 C2MetaArch = META_ARCH_CAFFE2_EXPORT_TYPE_MAP[cfg.MODEL.META_ARCHITECTURE]
27 (batch index + 5 coordinates) for RotatedBoxes. 28 """ 29 30 def __init__(self, tensor): 31 assert isinstance(tensor, torch.Tensor) 32 assert tensor.dim() == 2 and tensor.size(-1) in [4, 5, 6], tensor.size() 33 # TODO: make tensor immutable when dim is Nx5 for Boxes, 34 # and Nx6 for RotatedBoxes?
28 """ 29 30 def __init__(self, tensor): 31 assert isinstance(tensor, torch.Tensor) 32 assert tensor.dim() == 2 and tensor.size(-1) in [4, 5, 6], tensor.size() 33 # TODO: make tensor immutable when dim is Nx5 for Boxes, 34 # and Nx6 for RotatedBoxes? 35 self.tensor = tensor
83 data_len = value.shape[0] 84 else: 85 data_len = len(value) 86 if len(self.batch_extra_fields): 87 assert ( 88 len(self) == data_len 89 ), "Adding a field of length {} to a Instances of length {}".format(data_len, len(self)) 90 self.batch_extra_fields[name] = value 91 92 def __getattr__(self, name):
112 Convert InstancesList to List[Instances]. The input `instances_list` can 113 also be a List[Instances], in this case this method is a non-op. 114 """ 115 if not isinstance(instances_list, InstancesList): 116 assert all(isinstance(x, Instances) for x in instances_list) 117 return instances_list 118 119 ret = []
129 instances.set(k, v[ids, -4:]) 130 continue 131 132 target_type, tensor_source = v 133 assert isinstance(tensor_source, torch.Tensor) 134 assert tensor_source.shape[0] == instances_list.indices.shape[0] 135 tensor_source = tensor_source[ids] 136
130 continue 131 132 target_type, tensor_source = v 133 assert isinstance(tensor_source, torch.Tensor) 134 assert tensor_source.shape[0] == instances_list.indices.shape[0] 135 tensor_source = tensor_source[ids] 136 137 if issubclass(target_type, Boxes):
167 class Caffe2RPN(Caffe2Compatible, rpn.RPN): 168 @classmethod 169 def from_config(cls, cfg, input_shape: Dict[str, ShapeSpec]): 170 ret = super(Caffe2Compatible, cls).from_config(cfg, input_shape) 171 assert tuple(cfg.MODEL.RPN.BBOX_REG_WEIGHTS) == (1.0, 1.0, 1.0, 1.0) or tuple( 172 cfg.MODEL.RPN.BBOX_REG_WEIGHTS 173 ) == (1.0, 1.0, 1.0, 1.0, 1.0) 174 return ret 175 176 def _generate_proposals(
175 176 def _generate_proposals( 177 self, images, objectness_logits_pred, anchor_deltas_pred, gt_instances=None 178 ): 179 assert isinstance(images, ImageList) 180 if self.tensor_mode: 181 im_info = images.image_sizes 182 else:
182 else: 183 im_info = torch.tensor([[im_sz[0], im_sz[1], 1.0] for im_sz in images.image_sizes]).to( 184 images.tensor.device 185 ) 186 assert isinstance(im_info, torch.Tensor) 187 188 rpn_rois_list = [] 189 rpn_roi_probs_list = []
224 if len(objectness_logits_pred) == 1: 225 rpn_rois = rpn_rois_list[0] 226 rpn_roi_probs = rpn_roi_probs_list[0] 227 else: 228 assert len(rpn_rois_list) == len(rpn_roi_probs_list) 229 rpn_post_nms_topN = self.post_nms_topk[self.training] 230 231 device = rpn_rois_list[0].device
235 # is not needed in CollectRpnProposals. 236 feature_strides = list(self.anchor_generator.strides) 237 rpn_min_level = int(math.log2(feature_strides[0])) 238 rpn_max_level = int(math.log2(feature_strides[-1])) 239 assert (rpn_max_level - rpn_min_level + 1) == len( 240 rpn_rois_list 241 ), "CollectRpnProposals requires continuous levels" 242 243 rpn_rois = torch.ops._caffe2.CollectRpnProposals( 244 input_list,
256 proposals = self.c2_postprocess(im_info, rpn_rois, rpn_roi_probs, self.tensor_mode) 257 return proposals, {} 258 259 def forward(self, images, features, gt_instances=None): 260 assert not self.training 261 features = [features[f] for f in self.in_features] 262 objectness_logits_pred, anchor_deltas_pred = self.rpn_head(features) 263 return self._generate_proposals(
286 287 class Caffe2ROIPooler(Caffe2Compatible, poolers.ROIPooler): 288 @staticmethod 289 def c2_preprocess(box_lists): 290 assert all(isinstance(x, Boxes) for x in box_lists) 291 if all(isinstance(x, Caffe2Boxes) for x in box_lists): 292 # input is pure-tensor based 293 assert len(box_lists) == 1
289 def c2_preprocess(box_lists): 290 assert all(isinstance(x, Boxes) for x in box_lists) 291 if all(isinstance(x, Caffe2Boxes) for x in box_lists): 292 # input is pure-tensor based 293 assert len(box_lists) == 1 294 pooler_fmt_boxes = box_lists[0].tensor 295 else: 296 pooler_fmt_boxes = poolers.convert_boxes_to_pooler_format(box_lists)
296 pooler_fmt_boxes = poolers.convert_boxes_to_pooler_format(box_lists) 297 return pooler_fmt_boxes 298 299 def forward(self, x, box_lists): 300 assert not self.training 301 302 pooler_fmt_boxes = self.c2_preprocess(box_lists) 303 num_level_assignments = len(self.level_poolers)
326 ) 327 return out 328 329 device = pooler_fmt_boxes.device 330 assert ( 331 self.max_level - self.min_level + 1 == 4 332 ), "Currently DistributeFpnProposals only support 4 levels" 333 fpn_outputs = torch.ops._caffe2.DistributeFpnProposals( 334 to_device(pooler_fmt_boxes, "cpu"), 335 roi_canonical_scale=self.canonical_box_size,
367 ) 368 roi_feat_fpn_list.append(roi_feat_fpn) 369 370 roi_feat_shuffled = cat(roi_feat_fpn_list, dim=0) 371 assert roi_feat_shuffled.numel() > 0 and rois_idx_restore_int32.numel() > 0, ( 372 "Caffe2 export requires tracing with a model checkpoint + input that can produce valid" 373 " detections. But no detections were obtained with the given checkpoint and input!" 374 ) 375 roi_feat = torch.ops._caffe2.BatchPermutation(roi_feat_shuffled, rois_idx_restore_int32) 376 return roi_feat 377
389 is_rotated = len(box_predictor.box2box_transform.weights) == 5 390 391 if is_rotated: 392 box_dim = 5 393 assert box_predictor.box2box_transform.weights[4] == 1, ( 394 "The weights for Rotated BBoxTransform in C2 have only 4 dimensions," 395 + " thus enforcing the angle weight to be 1 for now" 396 ) 397 box2box_transform_weights = box_predictor.box2box_transform.weights[:4] 398 else: 399 box_dim = 4
402 class_logits, box_regression = predictions 403 if num_classes + 1 == class_logits.shape[1]: 404 class_prob = F.softmax(class_logits, -1) 405 else: 406 assert num_classes == class_logits.shape[1] 407 class_prob = F.sigmoid(class_logits) 408 # BoxWithNMSLimit will infer num_classes from the shape of the class_prob 409 # So append a zero column as placeholder for the background class
408 # BoxWithNMSLimit will infer num_classes from the shape of the class_prob 409 # So append a zero column as placeholder for the background class 410 class_prob = torch.cat((class_prob, torch.zeros(class_prob.shape[0], 1)), dim=1) 411 412 assert box_regression.shape[1] % box_dim == 0 413 cls_agnostic_bbox_reg = box_regression.shape[1] // box_dim == 1 414 415 input_tensor_mode = proposals[0].proposal_boxes.tensor.shape[1] == box_dim + 1
525 class Caffe2MaskRCNNInference: 526 def __call__(self, pred_mask_logits, pred_instances): 527 """equivalent to mask_head.mask_rcnn_inference""" 528 if all(isinstance(x, InstancesList) for x in pred_instances): 529 assert len(pred_instances) == 1 530 mask_probs_pred = pred_mask_logits.sigmoid() 531 mask_probs_pred = alias(mask_probs_pred, "mask_fcn_probs") 532 pred_instances[0].set("pred_masks", mask_probs_pred)
542 # just return the keypoint heatmap for now, 543 # there will be option to call HeatmapMaxKeypointOp 544 output = alias(pred_keypoint_logits, "kps_score") 545 if all(isinstance(x, InstancesList) for x in pred_instances): 546 assert len(pred_instances) == 1 547 if self.use_heatmap_max_keypoint: 548 device = output.device 549 output = torch.ops._caffe2.HeatmapMaxKeypoint(
41 42 Returns: 43 an onnx model 44 """ 45 assert isinstance(model, torch.nn.Module) 46 47 # make sure all modules are in eval mode, onnx may change the training state 48 # of the module if the states are not consistent
46 47 # make sure all modules are in eval mode, onnx may change the training state 48 # of the module if the states are not consistent 49 def _check_eval(module): 50 assert not module.training 51 52 model.apply(_check_eval) 53
84 device option for each op in order to make it runable on GPU runtime. 85 """ 86 87 def _get_device_type(torch_tensor): 88 assert torch_tensor.device.type in ["cpu", "cuda"] 89 assert torch_tensor.device.index == 0 90 return torch_tensor.device.type 91
85 """ 86 87 def _get_device_type(torch_tensor): 88 assert torch_tensor.device.type in ["cpu", "cuda"] 89 assert torch_tensor.device.index == 0 90 return torch_tensor.device.type 91 92 def _assign_op_device_option(net_proto, net_ssa, blob_device_types):
94 if op.type in ["CopyCPUToGPU", "CopyGPUToCPU"]: 95 op.device_option.CopyFrom(core.DeviceOption(caffe2_pb2.CUDA, 0)) 96 else: 97 devices = [blob_device_types[b] for b in ssa_i[0] + ssa_i[1]] 98 assert all(d == devices[0] for d in devices) 99 if devices[0] == "cuda": 100 op.device_option.CopyFrom(core.DeviceOption(caffe2_pb2.CUDA, 0)) 101
130 model: a caffe2-compatible version of detectron2 model, defined in caffe2_modeling.py 131 tensor_inputs: a list of tensors that caffe2 model takes as input. 132 """ 133 model = copy.deepcopy(model) 134 assert isinstance(model, torch.nn.Module) 135 assert hasattr(model, "encode_additional_info") 136 137 # Export via ONNX
131 tensor_inputs: a list of tensors that caffe2 model takes as input. 132 """ 133 model = copy.deepcopy(model) 134 assert isinstance(model, torch.nn.Module) 135 assert hasattr(model, "encode_additional_info") 136 137 # Export via ONNX 138 logger.info(
25 26 def __init__(self, predict_net, init_net): 27 logger.info(f"Initializing ProtobufModel for: {predict_net.name} ...") 28 super().__init__() 29 assert isinstance(predict_net, caffe2_pb2.NetDef) 30 assert isinstance(init_net, caffe2_pb2.NetDef) 31 # create unique temporary workspace for each instance 32 self.ws_name = "__tmp_ProtobufModel_{}__".format(next(self._ids))
26 def __init__(self, predict_net, init_net): 27 logger.info(f"Initializing ProtobufModel for: {predict_net.name} ...") 28 super().__init__() 29 assert isinstance(predict_net, caffe2_pb2.NetDef) 30 assert isinstance(init_net, caffe2_pb2.NetDef) 31 # create unique temporary workspace for each instance 32 self.ws_name = "__tmp_ProtobufModel_{}__".format(next(self._ids)) 33 self.net = core.Net(predict_net)
51 list[str]: list of device for each external output 52 """ 53 54 def _get_device_type(torch_tensor): 55 assert torch_tensor.device.type in ["cpu", "cuda"] 56 assert torch_tensor.device.index == 0 57 return torch_tensor.device.type 58
52 """ 53 54 def _get_device_type(torch_tensor): 55 assert torch_tensor.device.type in ["cpu", "cuda"] 56 assert torch_tensor.device.index == 0 57 return torch_tensor.device.type 58 59 predict_net = self.net.Proto()
75 76 Returns: 77 tuple[torch.Tensor] 78 """ 79 assert len(inputs) == len(self._input_blobs), ( 80 f"Length of inputs ({len(inputs)}) " 81 f"doesn't match the required input blobs: {self._input_blobs}" 82 ) 83 84 with ScopedWS(self.ws_name, is_reset=False, is_cleanup=False) as ws: 85 for b, tensor in zip(self._input_blobs, inputs):
42 43 batch_splits = tensor_outputs.get("batch_splits", None) 44 if batch_splits: 45 raise NotImplementedError() 46 assert len(image_sizes) == 1 47 result = results[0] 48 49 bbox_nms = tensor_outputs["bbox_nms"]
49 bbox_nms = tensor_outputs["bbox_nms"] 50 score_nms = tensor_outputs["score_nms"] 51 class_nms = tensor_outputs["class_nms"] 52 # Detection will always success because Conv support 0-batch 53 assert bbox_nms is not None 54 assert score_nms is not None 55 assert class_nms is not None 56 if bbox_nms.shape[1] == 5:
50 score_nms = tensor_outputs["score_nms"] 51 class_nms = tensor_outputs["class_nms"] 52 # Detection will always success because Conv support 0-batch 53 assert bbox_nms is not None 54 assert score_nms is not None 55 assert class_nms is not None 56 if bbox_nms.shape[1] == 5: 57 result.pred_boxes = RotatedBoxes(bbox_nms)
51 class_nms = tensor_outputs["class_nms"] 52 # Detection will always success because Conv support 0-batch 53 assert bbox_nms is not None 54 assert score_nms is not None 55 assert class_nms is not None 56 if bbox_nms.shape[1] == 5: 57 result.pred_boxes = RotatedBoxes(bbox_nms) 58 else:
107 def convert_batched_inputs_to_c2_format(batched_inputs, size_divisibility, device): 108 """ 109 See get_caffe2_inputs() below. 110 """ 111 assert all(isinstance(x, dict) for x in batched_inputs) 112 assert all(x["image"].dim() == 3 for x in batched_inputs) 113 114 images = [x["image"] for x in batched_inputs]
108 """ 109 See get_caffe2_inputs() below. 110 """ 111 assert all(isinstance(x, dict) for x in batched_inputs) 112 assert all(x["image"].dim() == 3 for x in batched_inputs) 113 114 images = [x["image"] for x in batched_inputs] 115 images = ImageList.from_tensors(images, size_divisibility)
243 244 245 class Caffe2GeneralizedRCNN(Caffe2MetaArch): 246 def __init__(self, cfg, torch_model): 247 assert isinstance(torch_model, meta_arch.GeneralizedRCNN) 248 torch_model = patch_generalized_rcnn(torch_model) 249 super().__init__(cfg, torch_model) 250
287 288 289 class Caffe2RetinaNet(Caffe2MetaArch): 290 def __init__(self, cfg, torch_model): 291 assert isinstance(torch_model, meta_arch.RetinaNet) 292 super().__init__(cfg, torch_model) 293 294 @mock_torch_nn_functional_interpolate()
292 super().__init__(cfg, torch_model) 293 294 @mock_torch_nn_functional_interpolate() 295 def forward(self, inputs): 296 assert self.tensor_mode 297 images = self._caffe2_preprocess_image(inputs) 298 299 # explicitly return the images sizes to avoid removing "im_info" by ONNX
33 self.replaceCls = replaceCls 34 35 def create_from(self, module): 36 # update module's class to the new class 37 assert isinstance(module, torch.nn.Module) 38 if issubclass(self.replaceCls, GenericMixin): 39 # replaceCls should act as mixin, create a new class on-the-fly 40 new_class = type(
86 side_effect=Caffe2FastRCNNOutputsInference(tensor_mode), 87 ) as mocked_func: 88 yield 89 if check: 90 assert mocked_func.call_count > 0 91 92 93 @contextlib.contextmanager
96 "{}.mask_rcnn_inference".format(patched_module), side_effect=Caffe2MaskRCNNInference() 97 ) as mocked_func: 98 yield 99 if check: 100 assert mocked_func.call_count > 0 101 102 103 @contextlib.contextmanager
107 side_effect=Caffe2KeypointRCNNInference(use_heatmap_max_keypoint), 108 ) as mocked_func: 109 yield 110 if check: 111 assert mocked_func.call_count > 0 112 113 114 class ROIHeadsPatcher:
44 def _concat(values): 45 ret = () 46 sizes = [] 47 for v in values: 48 assert isinstance(v, tuple), "Flattened results must be a tuple" 49 ret = ret + v 50 sizes.append(len(v)) 51 return ret, sizes
53 @staticmethod 54 def _split(values, sizes): 55 if len(sizes): 56 expected_len = sum(sizes) 57 assert ( 58 len(values) == expected_len 59 ), f"Values has length {len(values)} but expect length {expected_len}." 60 ret = [] 61 for k in range(len(sizes)): 62 begin, end = sum(sizes[:k]), sum(sizes[: k + 1])
309 else: # schema is valid 310 if self.outputs_schema is None: 311 self.outputs_schema = schema 312 else: 313 assert self.outputs_schema == schema, ( 314 "Model should always return outputs with the same " 315 "structure so it can be traced!" 316 ) 317 return flattened_outputs 318 319 def _create_wrapper(self, traced_model):
44 45 46 # Note: borrowed from vision/detection/fair/detectron/detectron/modeling/detector.py 47 def BilinearInterpolation(tensor_in, up_scale): 48 assert up_scale % 2 == 0, "Scale should be even" 49 50 def upsample_filt(size): 51 factor = (size + 1) // 2
87 if input.dim() == 4: 88 if isinstance(scale_factor, (int, float)): 89 height_scale, width_scale = (scale_factor, scale_factor) 90 else: 91 assert isinstance(scale_factor, (tuple, list)) 92 assert len(scale_factor) == 2 93 height_scale, width_scale = scale_factor 94
88 if isinstance(scale_factor, (int, float)): 89 height_scale, width_scale = (scale_factor, scale_factor) 90 else: 91 assert isinstance(scale_factor, (tuple, list)) 92 assert len(scale_factor) == 2 93 height_scale, width_scale = scale_factor 94 95 assert not align_corners, "No matching C2 op for align_corners == True"
91 assert isinstance(scale_factor, (tuple, list)) 92 assert len(scale_factor) == 2 93 height_scale, width_scale = scale_factor 94 95 assert not align_corners, "No matching C2 op for align_corners == True" 96 if mode == "nearest": 97 return torch.ops._caffe2.ResizeNearest( 98 input, order="NCHW", width_scale=width_scale, height_scale=height_scale
103 " because there's no such C2 op, this may cause significant" 104 " slowdown and the boundary pixels won't be as same as" 105 " using F.interpolate due to padding." 106 ) 107 assert height_scale == width_scale 108 return BilinearInterpolation(input, up_scale=height_scale) 109 logger.warning("Output size is not static, it might cause ONNX conversion issue") 110
209 def check_set_pb_arg(pb, arg_name, arg_attr, arg_value, allow_override=False): 210 arg = get_pb_arg(pb, arg_name) 211 if arg is None: 212 arg = putils.MakeArgument(arg_name, arg_value) 213 assert hasattr(arg, arg_attr) 214 pb.arg.extend([arg]) 215 if allow_override and getattr(arg, arg_attr) != arg_value: 216 logger.warning(
217 "Override argument {}: {} -> {}".format(arg_name, getattr(arg, arg_attr), arg_value) 218 ) 219 setattr(arg, arg_attr, arg_value) 220 else: 221 assert arg is not None 222 assert getattr(arg, arg_attr) == arg_value, "Existing value {}, new value {}".format( 223 getattr(arg, arg_attr), arg_value 224 )
218 ) 219 setattr(arg, arg_attr, arg_value) 220 else: 221 assert arg is not None 222 assert getattr(arg, arg_attr) == arg_value, "Existing value {}, new value {}".format( 223 getattr(arg, arg_attr), arg_value 224 ) 225 226 227 def _create_const_fill_op_from_numpy(name, tensor, device_option=None):
224 ) 225 226 227 def _create_const_fill_op_from_numpy(name, tensor, device_option=None): 228 assert type(tensor) == np.ndarray 229 kTypeNameMapper = { 230 np.dtype("float32"): "GivenTensorFill", 231 np.dtype("int32"): "GivenTensorIntFill",
245 return core.CreateOperator(kTypeNameMapper[tensor.dtype], [], [name], **args_dict) 246 247 248 def _create_const_fill_op_from_c2_int8_tensor(name, int8_tensor): 249 assert type(int8_tensor) == workspace.Int8Tensor 250 kTypeNameMapper = { 251 np.dtype("int32"): "Int8GivenIntTensorFill", 252 np.dtype("uint8"): "Int8GivenTensorFill",
252 np.dtype("uint8"): "Int8GivenTensorFill", 253 } 254 255 tensor = int8_tensor.data 256 assert tensor.dtype in [np.dtype("uint8"), np.dtype("int32")] 257 values = tensor.tobytes() if tensor.dtype == np.dtype("uint8") else tensor 258 259 return core.CreateOperator(
277 as constant. Currently support NumPy tensor and Caffe2 Int8Tensor. 278 """ 279 280 tensor_type = type(blob) 281 assert tensor_type in [ 282 np.ndarray, 283 workspace.Int8Tensor, 284 ], 'Error when creating const fill op for "{}", unsupported blob type: {}'.format( 285 name, type(blob) 286 ) 287 288 if tensor_type == np.ndarray: 289 return _create_const_fill_op_from_numpy(name, blob, device_option)
287 288 if tensor_type == np.ndarray: 289 return _create_const_fill_op_from_numpy(name, blob, device_option) 290 elif tensor_type == workspace.Int8Tensor: 291 assert device_option is None 292 return _create_const_fill_op_from_c2_int8_tensor(name, blob) 293 294
401 versioned_ext_output = [(b, versions[b]) for b in predict_net.external_output] 402 all_versioned_blobs = set().union(*[set(x[0] + x[1]) for x in ssa]) 403 404 allowed_vbs = all_versioned_blobs.union(versioned_ext_input).union(versioned_ext_output) 405 assert all(k in allowed_vbs for k in known_status) 406 assert all(v is not None for v in known_status.values()) 407 _known_status = copy.deepcopy(known_status) 408
402 all_versioned_blobs = set().union(*[set(x[0] + x[1]) for x in ssa]) 403 404 allowed_vbs = all_versioned_blobs.union(versioned_ext_input).union(versioned_ext_output) 405 assert all(k in allowed_vbs for k in known_status) 406 assert all(v is not None for v in known_status.values()) 407 _known_status = copy.deepcopy(known_status) 408 409 def _check_and_update(key, value):
406 assert all(v is not None for v in known_status.values()) 407 _known_status = copy.deepcopy(known_status) 408 409 def _check_and_update(key, value): 410 assert value is not None 411 if key in _known_status: 412 if not _known_status[key] == value: 413 raise RuntimeError(
456 device_name_style: str = "caffe2", 457 ) -> Dict[Tuple[str, int], str]: 458 """Return the device type ("cpu" or "gpu"/"cuda") of each (versioned) blob""" 459 460 assert device_name_style in ["caffe2", "pytorch"] 461 _CPU_STR = "cpu" 462 _GPU_STR = "gpu" if device_name_style == "caffe2" else "cuda" 463
596 597 def alias(x, name, is_backward=False): 598 if not torch.onnx.is_in_onnx_export(): 599 return x 600 assert isinstance(x, torch.Tensor) 601 return torch.ops._caffe2.AliasWithName(x, name, is_backward=is_backward) 602 603
605 """Remove AliasWithName placeholder and rename the input/output of it""" 606 # First we finish all the re-naming 607 for i, op in enumerate(predict_net.op): 608 if op.type == "AliasWithName": 609 assert len(op.input) == 1 610 assert len(op.output) == 1 611 name = get_pb_arg_vals(op, "name", None).decode() 612 is_backward = bool(get_pb_arg_vali(op, "is_backward", 0))
606 # First we finish all the re-naming 607 for i, op in enumerate(predict_net.op): 608 if op.type == "AliasWithName": 609 assert len(op.input) == 1 610 assert len(op.output) == 1 611 name = get_pb_arg_vals(op, "name", None).decode() 612 is_backward = bool(get_pb_arg_vali(op, "is_backward", 0)) 613 rename_op_input(predict_net, init_net, i, 0, name, from_producer=is_backward)
619 if op.type != "AliasWithName": 620 new_ops.append(op) 621 else: 622 # safety check 623 assert op.input == op.output 624 assert op.input[0] == op.arg[0].s.decode() 625 del predict_net.op[:] 626 predict_net.op.extend(new_ops)
620 new_ops.append(op) 621 else: 622 # safety check 623 assert op.input == op.output 624 assert op.input[0] == op.arg[0].s.decode() 625 del predict_net.op[:] 626 predict_net.op.extend(new_ops) 627
680 - This function modifies predict_net and init_net in-place. 681 - When from_producer is enable, this also updates other operators that consumes 682 the same input. Be cautious because may trigger unintended behavior. 683 """ 684 assert isinstance(predict_net, caffe2_pb2.NetDef) 685 assert isinstance(init_net, caffe2_pb2.NetDef) 686 687 init_net_ssa, init_net_versions = core.get_ssa(init_net)
681 - When from_producer is enable, this also updates other operators that consumes 682 the same input. Be cautious because may trigger unintended behavior. 683 """ 684 assert isinstance(predict_net, caffe2_pb2.NetDef) 685 assert isinstance(init_net, caffe2_pb2.NetDef) 686 687 init_net_ssa, init_net_versions = core.get_ssa(init_net) 688 predict_net_ssa, predict_net_versions = core.get_ssa(
738 external_output and if necessary. 739 - It allows multiple consumers of its output. 740 - This function modifies predict_net in-place, doesn't need init_net. 741 """ 742 assert isinstance(predict_net, caffe2_pb2.NetDef) 743 744 ssa, blob_versions = core.get_ssa(predict_net) 745
874 875 ret = [] 876 for i, op in enumerate(predict_net.op): 877 if op.type == "Reshape": 878 assert len(op.input) == 2 879 input_ssa = ssa[i][0] 880 data_source = input_ssa[0] 881 shape_source = input_ssa[1]
902 reshape_sub_graphs = identify_reshape_sub_graph(predict_net) 903 sub_graphs_to_remove = [] 904 for reshape_sub_graph in reshape_sub_graphs: 905 reshape_op_id = reshape_sub_graph[-1] 906 assert predict_net.op[reshape_op_id].type == "Reshape" 907 ssa, _ = core.get_ssa(predict_net) 908 reshape_output = ssa[reshape_op_id][1][0] 909 consumers = [i for i in range(len(ssa)) if reshape_output in ssa[i][0]]
934 rename_op_output(predict_net, reshape_op_id, 0, new_reshap_output) 935 ext_inputs, ext_outputs = get_sub_graph_external_input_output(predict_net, sub_graph) 936 non_params_ext_inputs = [inp for inp in ext_inputs if inp[1] != 0] 937 params_ext_inputs = [inp for inp in ext_inputs if inp[1] == 0] 938 assert len(non_params_ext_inputs) == 1 and len(ext_outputs) == 1 939 assert ext_outputs[0][0] == non_params_ext_inputs[0][0] 940 assert ext_outputs[0][1] == non_params_ext_inputs[0][1] + 1 941 remove_op_ids.extend(sub_graph)
935 ext_inputs, ext_outputs = get_sub_graph_external_input_output(predict_net, sub_graph) 936 non_params_ext_inputs = [inp for inp in ext_inputs if inp[1] != 0] 937 params_ext_inputs = [inp for inp in ext_inputs if inp[1] == 0] 938 assert len(non_params_ext_inputs) == 1 and len(ext_outputs) == 1 939 assert ext_outputs[0][0] == non_params_ext_inputs[0][0] 940 assert ext_outputs[0][1] == non_params_ext_inputs[0][1] + 1 941 remove_op_ids.extend(sub_graph) 942 params_to_remove.extend(params_ext_inputs)
936 non_params_ext_inputs = [inp for inp in ext_inputs if inp[1] != 0] 937 params_ext_inputs = [inp for inp in ext_inputs if inp[1] == 0] 938 assert len(non_params_ext_inputs) == 1 and len(ext_outputs) == 1 939 assert ext_outputs[0][0] == non_params_ext_inputs[0][0] 940 assert ext_outputs[0][1] == non_params_ext_inputs[0][1] + 1 941 remove_op_ids.extend(sub_graph) 942 params_to_remove.extend(params_ext_inputs) 943
46 47 Returns: 48 torch.jit.ScriptModule: the model in torchscript format 49 """ 50 assert ( 51 not model.training 52 ), "Currently we only support exporting models in evaluation mode to torchscript" 53 54 with freeze_training_mode(model), patch_instances(fields): 55 scripted_model = torch.jit.script(model)
39 fields = instances.get_fields() 40 image_size = instances.image_size 41 ret = newInstances(image_size) 42 for name, val in fields.items(): 43 assert hasattr(ret, f"_{name}"), f"No attribute named {name} in {cls_name}" 44 setattr(ret, name, deepcopy(val)) 45 return ret 46
94 """ 95 96 class _FieldType: 97 def __init__(self, name, type_): 98 assert isinstance(name, str), f"Field name must be str, got {name}" 99 self.name = name 100 self.type_ = type_ 101 self.annotation = f"{type_.__module__}.{type_.__name__}"
52 use_depthwise_separable_conv (bool): use DepthwiseSeparableConv2d 53 for 3x3 convs in ASPP, proposed in :paper:`DeepLabV3+`. 54 """ 55 super(ASPP, self).__init__() 56 assert len(dilations) == 3, "ASPP expects 3 dilations, got {}".format(len(dilations)) 57 self.pool_kernel_size = pool_kernel_size 58 self.dropout = dropout 59 use_bias = norm == ""
178 """ 179 180 def __init__(self, *args, stats_mode="", **kwargs): 181 super().__init__(*args, **kwargs) 182 assert stats_mode in ["", "N"] 183 self._stats_mode = stats_mode 184 185 def forward(self, input):
195 mean = torch.mean(input, dim=[0, 2, 3]) 196 meansqr = torch.mean(input * input, dim=[0, 2, 3]) 197 198 if self._stats_mode == "": 199 assert B > 0, 'SyncBatchNorm(stats_mode="") does not support zero batch size.' 200 vec = torch.cat([mean, meansqr], dim=0) 201 vec = differentiable_all_reduce(vec) * (1.0 / dist.get_world_size()) 202 mean, meansqr = torch.split(vec, C)
56 input, offset, weight, stride=stride, padding=padding, dilation=dilation 57 ) 58 else: 59 cur_im2col_step = _DeformConv._cal_im2col_step(input.shape[0], ctx.im2col_step) 60 assert (input.shape[0] % cur_im2col_step) == 0, "im2col step must divide batchsize" 61 62 _C.deform_conv_forward( 63 input,
90 if not grad_output.is_cuda: 91 raise NotImplementedError("Deformable Conv is not supported on CPUs!") 92 else: 93 cur_im2col_step = _DeformConv._cal_im2col_step(input.shape[0], ctx.im2col_step) 94 assert (input.shape[0] % cur_im2col_step) == 0, "im2col step must divide batchsize" 95 96 if ctx.needs_input_grad[0] or ctx.needs_input_grad[1]: 97 grad_input = torch.zeros_like(input)
339 activation (callable(Tensor) -> Tensor): a callable activation function 340 """ 341 super(DeformConv, self).__init__() 342 343 assert not bias 344 assert in_channels % groups == 0, "in_channels {} cannot be divisible by groups {}".format( 345 in_channels, groups 346 )
340 """ 341 super(DeformConv, self).__init__() 342 343 assert not bias 344 assert in_channels % groups == 0, "in_channels {} cannot be divisible by groups {}".format( 345 in_channels, groups 346 ) 347 assert ( 348 out_channels % groups == 0 349 ), "out_channels {} cannot be divisible by groups {}".format(out_channels, groups)
343 assert not bias 344 assert in_channels % groups == 0, "in_channels {} cannot be divisible by groups {}".format( 345 in_channels, groups 346 ) 347 assert ( 348 out_channels % groups == 0 349 ), "out_channels {} cannot be divisible by groups {}".format(out_channels, groups) 350 351 self.in_channels = in_channels 352 self.out_channels = out_channels
23 x1, y1, x2, y2 = boxes1.unbind(dim=-1) 24 x1g, y1g, x2g, y2g = boxes2.unbind(dim=-1) 25 26 # TODO: use torch._assert_async() when pytorch 1.8 support is dropped 27 assert (x2 >= x1).all(), "bad box: x1 larger than x2" 28 assert (y2 >= y1).all(), "bad box: y1 larger than y2" 29 30 # Intersection keypoints
24 x1g, y1g, x2g, y2g = boxes2.unbind(dim=-1) 25 26 # TODO: use torch._assert_async() when pytorch 1.8 support is dropped 27 assert (x2 >= x1).all(), "bad box: x1 larger than x2" 28 assert (y2 >= y1).all(), "bad box: y1 larger than y2" 29 30 # Intersection keypoints 31 xkis1 = torch.max(x1, x1g)
84 x1, y1, x2, y2 = boxes1.unbind(dim=-1) 85 x1g, y1g, x2g, y2g = boxes2.unbind(dim=-1) 86 87 # TODO: use torch._assert_async() when pytorch 1.8 support is dropped 88 assert (x2 >= x1).all(), "bad box: x1 larger than x2" 89 assert (y2 >= y1).all(), "bad box: y1 larger than y2" 90 91 # Intersection keypoints
85 x1g, y1g, x2g, y2g = boxes2.unbind(dim=-1) 86 87 # TODO: use torch._assert_async() when pytorch 1.8 support is dropped 88 assert (x2 >= x1).all(), "bad box: x1 larger than x2" 89 assert (y2 >= y1).all(), "bad box: y1 larger than y2" 90 91 # Intersection keypoints 92 xkis1 = torch.max(x1, x1g)
99 number of detected object instances and Himage, Wimage are the image width 100 and height. img_masks[i] is a binary mask for object instance i. 101 """ 102 103 assert masks.shape[-1] == masks.shape[-2], "Only square mask predictions are supported" 104 N = len(masks) 105 if N == 0: 106 return masks.new_empty((0,) + image_shape, dtype=torch.uint8)
106 return masks.new_empty((0,) + image_shape, dtype=torch.uint8) 107 if not isinstance(boxes, torch.Tensor): 108 boxes = boxes.tensor 109 device = boxes.device 110 assert len(boxes) == N, boxes.shape 111 112 img_h, img_w = image_shape 113
120 else: 121 # GPU benefits from parallelism for larger chunks, but may have memory issue 122 # int(img_h) because shape may be tensors in tracing 123 num_chunks = int(np.ceil(N * int(img_h) * int(img_w) * BYTES_PER_FLOAT / GPU_MEM_LIMIT)) 124 assert ( 125 num_chunks <= N 126 ), "Default GPU_MEM_LIMIT in mask_ops.py is too small; try increasing it" 127 chunks = torch.chunk(torch.arange(N, device=device), num_chunks) 128 129 img_masks = torch.zeros(
11 ): 12 """ 13 Same as torchvision.ops.boxes.batched_nms, but with float(). 14 """ 15 assert boxes.shape[-1] == 4 16 # Note: Torchvision already has a strategy (https://github.com/pytorch/vision/issues/1311) 17 # to decide whether to use coordinate trick or for loop to implement batched_nms. So we 18 # just call it directly.
116 Tensor: 117 int64 tensor with the indices of the elements that have been kept 118 by NMS, sorted in decreasing order of scores 119 """ 120 assert boxes.shape[-1] == 5 121 122 if boxes.numel() == 0: 123 return torch.empty((0,), dtype=torch.int64, device=boxes.device)
43 from torchvision import __version__ 44 45 version = tuple(int(x) for x in __version__.split(".")[:2]) 46 # https://github.com/pytorch/vision/pull/2438 47 assert version >= (0, 7), "Require torchvision >= 0.7" 48 49 def forward(self, input, rois): 50 """
51 Args: 52 input: NCHW images 53 rois: Bx5 boxes. First column is the index into N. The other 4 columns are xyxy. 54 """ 55 assert rois.dim() == 2 and rois.size(1) == 5 56 if input.is_quantized: 57 input = input.dequantize() 58 return roi_align(
72 input: NCHW images 73 rois: Bx6 boxes. First column is the index into N. 74 The other 5 columns are (x_ctr, y_ctr, width, height, angle_degrees). 75 """ 76 assert rois.dim() == 2 and rois.size(1) == 6 77 orig_dtype = input.dtype 78 if orig_dtype == torch.float16: 79 input = input.float()
26 """ 27 if torch.jit.is_scripting(): 28 return torch.as_tensor(x, device=device) 29 if torch.jit.is_tracing(): 30 assert all( 31 [isinstance(t, torch.Tensor) for t in x] 32 ), "Shape should be tensor during tracing!" 33 # as_tensor should not be used in tracing because it records a constant 34 ret = torch.stack(x) 35 if ret.device != device: # avoid recording a hard-coded device if not necessary
50 def cat(tensors: List[torch.Tensor], dim: int = 0): 51 """ 52 Efficient version of torch.cat that avoids a copy if there is only a single element in a list 53 """ 54 assert isinstance(tensors, (list, tuple)) 55 if len(tensors) == 1: 56 return tensors[0] 57 return torch.cat(tensors, dim)
119 if not is_dynamo_compiling: 120 with warnings.catch_warnings(record=True): 121 if x.numel() == 0 and self.training: 122 # https://github.com/pytorch/pytorch/issues/12013 123 assert not isinstance( 124 self.norm, torch.nn.SyncBatchNorm 125 ), "SyncBatchNorm does not support empty inputs!" 126 127 x = F.conv2d( 128 x, self.weight, self.bias, self.stride, self.padding, self.dilation, self.groups
66 67 Returns: 68 list[list[float]]: param for each feature 69 """ 70 assert isinstance( 71 params, collections.abc.Sequence 72 ), f"{name} in anchor generator has to be a list! Got {params}." 73 assert len(params), f"{name} in anchor generator cannot be empty!" 74 if not isinstance(params[0], collections.abc.Sequence): # params is list[float] 75 return [params] * num_features
69 """ 70 assert isinstance( 71 params, collections.abc.Sequence 72 ), f"{name} in anchor generator has to be a list! Got {params}." 73 assert len(params), f"{name} in anchor generator cannot be empty!" 74 if not isinstance(params[0], collections.abc.Sequence): # params is list[float] 75 return [params] * num_features 76 if len(params) == 1:
74 if not isinstance(params[0], collections.abc.Sequence): # params is list[float] 75 return [params] * num_features 76 if len(params) == 1: 77 return list(params) * num_features 78 assert len(params) == num_features, ( 79 f"Got {name} of length {len(params)} in anchor generator, " 80 f"but the number of input features is {num_features}!" 81 ) 82 return params 83 84
121 aspect_ratios = _broadcast_params(aspect_ratios, self.num_features, "aspect_ratios") 122 self.cell_anchors = self._calculate_anchors(sizes, aspect_ratios) 123 124 self.offset = offset 125 assert 0.0 <= self.offset < 1.0, self.offset 126 127 @classmethod 128 def from_config(cls, cfg, input_shape: List[ShapeSpec]):
273 angles = _broadcast_params(angles, self.num_features, "angles") 274 self.cell_anchors = self._calculate_anchors(sizes, aspect_ratios, angles) 275 276 self.offset = offset 277 assert 0.0 <= self.offset < 1.0, self.offset 278 279 @classmethod 280 def from_config(cls, cfg, input_shape: List[ShapeSpec]):
28 input_shape = ShapeSpec(channels=len(cfg.MODEL.PIXEL_MEAN)) 29 30 backbone_name = cfg.MODEL.BACKBONE.NAME 31 backbone = BACKBONE_REGISTRY.get(backbone_name)(cfg, input_shape) 32 assert isinstance(backbone, Backbone) 33 return backbone
56 which takes the element-wise mean of the two. 57 square_pad (int): If > 0, require input images to be padded to specific square size. 58 """ 59 super(FPN, self).__init__() 60 assert isinstance(bottom_up, Backbone) 61 assert in_features, in_features 62 63 # Feature map strides and channels from the bottom up network (e.g. ResNet)
57 square_pad (int): If > 0, require input images to be padded to specific square size. 58 """ 59 super(FPN, self).__init__() 60 assert isinstance(bottom_up, Backbone) 61 assert in_features, in_features 62 63 # Feature map strides and channels from the bottom up network (e.g. ResNet) 64 input_shapes = bottom_up.output_shape()
111 self._out_features = list(self._out_feature_strides.keys()) 112 self._out_feature_channels = {k: out_channels for k in self._out_features} 113 self._size_divisibility = strides[-1] 114 self._square_pad = square_pad 115 assert fuse_type in {"avg", "sum"} 116 self._fuse_type = fuse_type 117 118 @property
162 top_block_in_feature = bottom_up_features[self.top_block.in_feature] 163 else: 164 top_block_in_feature = results[self._out_features.index(self.top_block.in_feature)] 165 results.extend(self.top_block(top_block_in_feature)) 166 assert len(self._out_features) == len(results) 167 return {f: res for f, res in zip(self._out_features, results)} 168 169 def output_shape(self):
179 """ 180 Assert that each stride is 2x times its preceding stride, i.e. "contiguous in log2". 181 """ 182 for i, stride in enumerate(strides[1:], 1): 183 assert stride == 2 * strides[i - 1], "Strides {} {} are not log2 contiguous".format( 184 stride, strides[i - 1] 185 ) 186 187 188 class LastLevelMaxPool(nn.Module):
114 115 self.use_rel_pos = use_rel_pos 116 if self.use_rel_pos: 117 # initialize relative positional embeddings 118 assert input_size[0] == input_size[1] 119 size = input_size[0] 120 rel_dim = 2 * max(size // stride_q, size // stride_kv) - 1 121 self.rel_pos_h = nn.Parameter(torch.zeros(rel_dim, head_dim))
26 27 28 def conv2d(w_in, w_out, k, *, stride=1, groups=1, bias=False): 29 """Helper for building a conv2d layer.""" 30 assert k % 2 == 1, "Only odd size kernels supported to avoid padding issues." 31 s, p, g, b = stride, (k - 1) // 2, groups, bias 32 return nn.Conv2d(w_in, w_out, k, stride=s, padding=p, groups=g, bias=b) 33
38 39 40 def pool2d(k, *, stride=1): 41 """Helper for building a pool2d layer.""" 42 assert k % 2 == 1, "Only odd size kernels supported to avoid padding issues." 43 return nn.MaxPool2d(k, stride=stride, padding=(k - 1) // 2) 44 45
293 294 if out_features is None: 295 out_features = [name] 296 self._out_features = out_features 297 assert len(self._out_features) 298 children = [x[0] for x in self.named_children()] 299 for out_feature in self._out_features: 300 assert out_feature in children, "Available children: {} does not include {}".format(
296 self._out_features = out_features 297 assert len(self._out_features) 298 children = [x[0] for x in self.named_children()] 299 for out_feature in self._out_features: 300 assert out_feature in children, "Available children: {} does not include {}".format( 301 ", ".join(children), out_feature 302 ) 303 self.freeze(freeze_at) 304 305 def forward(self, x):
309 310 Returns: 311 dict[str->Tensor]: names and the corresponding features 312 """ 313 assert x.dim() == 4, f"Model takes an input of shape (N, C, H, W). Got {x.shape} instead!" 314 outputs = {} 315 x = self.stem(x) 316 if "stem" in self._out_features:
354 355 356 def adjust_block_compatibility(ws, bs, gs): 357 """Adjusts the compatibility of widths, bottlenecks, and groups.""" 358 assert len(ws) == len(bs) == len(gs) 359 assert all(w > 0 and b > 0 and g > 0 for w, b, g in zip(ws, bs, gs)) 360 vs = [int(max(1, w * b)) for w, b in zip(ws, bs)] 361 gs = [int(min(g, v)) for g, v in zip(gs, vs)]
355 356 def adjust_block_compatibility(ws, bs, gs): 357 """Adjusts the compatibility of widths, bottlenecks, and groups.""" 358 assert len(ws) == len(bs) == len(gs) 359 assert all(w > 0 and b > 0 and g > 0 for w, b, g in zip(ws, bs, gs)) 360 vs = [int(max(1, w * b)) for w, b in zip(ws, bs)] 361 gs = [int(min(g, v)) for g, v in zip(gs, vs)] 362 ms = [np.lcm(g, b) if b > 1 else g for g, b in zip(gs, bs)]
361 gs = [int(min(g, v)) for g, v in zip(gs, vs)] 362 ms = [np.lcm(g, b) if b > 1 else g for g, b in zip(gs, bs)] 363 vs = [max(m, int(round(v / m) * m)) for v, m in zip(vs, ms)] 364 ws = [int(v / b) for v, b in zip(vs, bs)] 365 assert all(w * b % g == 0 for w, b, g in zip(ws, bs, gs)) 366 return ws, bs, gs 367 368
367 368 369 def generate_regnet_parameters(w_a, w_0, w_m, d, q=8): 370 """Generates per stage widths and depths from RegNet parameters.""" 371 assert w_a >= 0 and w_0 > 0 and w_m > 1 and w_0 % q == 0 372 # Generate continuous per-block ws 373 ws_cont = np.arange(d) * w_a + w_0 374 # Generate quantized per-block ws
395 [{"res2": 1, "res3": 2, "res4": 3, "res5": 4}.get(f, 0) for f in out_features] 396 ) 397 stages = stages[:num_stages] 398 for i, blocks in enumerate(stages): 399 assert len(blocks) > 0, len(blocks) 400 for block in blocks: 401 assert isinstance(block, CNNBlockBase), block 402
397 stages = stages[:num_stages] 398 for i, blocks in enumerate(stages): 399 assert len(blocks) > 0, len(blocks) 400 for block in blocks: 401 assert isinstance(block, CNNBlockBase), block 402 403 name = "res" + str(i + 2) 404 stage = nn.Sequential(*blocks)
425 426 if out_features is None: 427 out_features = [name] 428 self._out_features = out_features 429 assert len(self._out_features) 430 children = [x[0] for x in self.named_children()] 431 for out_feature in self._out_features: 432 assert out_feature in children, "Available children: {}".format(", ".join(children))
428 self._out_features = out_features 429 assert len(self._out_features) 430 children = [x[0] for x in self.named_children()] 431 for out_feature in self._out_features: 432 assert out_feature in children, "Available children: {}".format(", ".join(children)) 433 self.freeze(freeze_at) 434 435 def forward(self, x):
439 440 Returns: 441 dict[str->Tensor]: names and the corresponding features 442 """ 443 assert x.dim() == 4, f"ResNet takes an input of shape (N, C, H, W). Got {x.shape} instead!" 444 outputs = {} 445 x = self.stem(x) 446 if "stem" in self._out_features:
527 for i in range(num_blocks): 528 curr_kwargs = {} 529 for k, v in kwargs.items(): 530 if k.endswith("_per_block"): 531 assert len(v) == num_blocks, ( 532 f"Argument '{k}' of make_stage should have the " 533 f"same length as num_blocks={num_blocks}." 534 ) 535 newk = k[: -len("_per_block")] 536 assert newk not in kwargs, f"Cannot call make_stage with both {k} and {newk}!" 537 curr_kwargs[newk] = v[i]
532 f"Argument '{k}' of make_stage should have the " 533 f"same length as num_blocks={num_blocks}." 534 ) 535 newk = k[: -len("_per_block")] 536 assert newk not in kwargs, f"Cannot call make_stage with both {k} and {newk}!" 537 curr_kwargs[newk] = v[i] 538 else: 539 curr_kwargs[k] = v
640 deform_on_per_stage = cfg.MODEL.RESNETS.DEFORM_ON_PER_STAGE 641 deform_modulated = cfg.MODEL.RESNETS.DEFORM_MODULATED 642 deform_num_groups = cfg.MODEL.RESNETS.DEFORM_NUM_GROUPS 643 # fmt: on 644 assert res5_dilation in {1, 2}, "res5_dilation cannot be {}.".format(res5_dilation) 645 646 num_blocks_per_stage = { 647 18: [2, 2, 2, 2],
651 152: [3, 8, 36, 3], 652 }[depth] 653 654 if depth in [18, 34]: 655 assert out_channels == 64, "Must set MODEL.RESNETS.RES2_OUT_CHANNELS = 64 for R18/R34" 656 assert not any( 657 deform_on_per_stage 658 ), "MODEL.RESNETS.DEFORM_ON_PER_STAGE unsupported for R18/R34"
652 }[depth] 653 654 if depth in [18, 34]: 655 assert out_channels == 64, "Must set MODEL.RESNETS.RES2_OUT_CHANNELS = 64 for R18/R34" 656 assert not any( 657 deform_on_per_stage 658 ), "MODEL.RESNETS.DEFORM_ON_PER_STAGE unsupported for R18/R34" 659 assert res5_dilation == 1, "Must set MODEL.RESNETS.RES5_DILATION = 1 for R18/R34" 660 assert num_groups == 1, "Must set MODEL.RESNETS.NUM_GROUPS = 1 for R18/R34" 661
655 assert out_channels == 64, "Must set MODEL.RESNETS.RES2_OUT_CHANNELS = 64 for R18/R34" 656 assert not any( 657 deform_on_per_stage 658 ), "MODEL.RESNETS.DEFORM_ON_PER_STAGE unsupported for R18/R34" 659 assert res5_dilation == 1, "Must set MODEL.RESNETS.RES5_DILATION = 1 for R18/R34" 660 assert num_groups == 1, "Must set MODEL.RESNETS.NUM_GROUPS = 1 for R18/R34" 661 662 stages = []
656 assert not any( 657 deform_on_per_stage 658 ), "MODEL.RESNETS.DEFORM_ON_PER_STAGE unsupported for R18/R34" 659 assert res5_dilation == 1, "Must set MODEL.RESNETS.RES5_DILATION = 1 for R18/R34" 660 assert num_groups == 1, "Must set MODEL.RESNETS.NUM_GROUPS = 1 for R18/R34" 661 662 stages = [] 663
214 self.num_heads = num_heads 215 self.window_size = window_size 216 self.shift_size = shift_size 217 self.mlp_ratio = mlp_ratio 218 assert 0 <= self.shift_size < self.window_size, "shift_size must in 0-window_size" 219 220 self.norm1 = norm_layer(dim) 221 self.attn = WindowAttention(
251 mask_matrix: Attention mask for cyclic shift. 252 """ 253 B, L, C = x.shape 254 H, W = self.H, self.W 255 assert L == H * W, "input feature has wrong size" 256 257 shortcut = x 258 x = self.norm1(x)
325 x: Input feature, tensor size (B, H*W, C). 326 H, W: Spatial resolution of the input feature. 327 """ 328 B, L, C = x.shape 329 assert L == H * W, "input feature has wrong size" 330 331 x = x.view(B, H, W, C) 332
141 if has_cls_token: 142 abs_pos = abs_pos[:, 1:] 143 xy_num = abs_pos.shape[1] 144 size = int(math.sqrt(xy_num)) 145 assert size * size == xy_num 146 147 if size != h or size != w: 148 new_abs_pos = F.interpolate(
395 norm (str): the normalization to use. 396 square_pad (int): If > 0, require input images to be padded to specific square size. 397 """ 398 super(SimpleFeaturePyramid, self).__init__() 399 assert isinstance(net, Backbone) 400 401 self.scale_factors = scale_factors 402
498 top_block_in_feature = bottom_up_features[self.top_block.in_feature] 499 else: 500 top_block_in_feature = results[self._out_features.index(self.top_block.in_feature)] 501 results.extend(self.top_block(top_block_in_feature)) 502 assert len(self._out_features) == len(results) 503 return {f: res for f, res in zip(self._out_features, results)} 504 505
51 src_boxes (Tensor): source boxes, e.g., object proposals 52 target_boxes (Tensor): target of the transformation, e.g., ground-truth 53 boxes. 54 """ 55 assert isinstance(src_boxes, torch.Tensor), type(src_boxes) 56 assert isinstance(target_boxes, torch.Tensor), type(target_boxes) 57 58 src_widths = src_boxes[:, 2] - src_boxes[:, 0]
52 target_boxes (Tensor): target of the transformation, e.g., ground-truth 53 boxes. 54 """ 55 assert isinstance(src_boxes, torch.Tensor), type(src_boxes) 56 assert isinstance(target_boxes, torch.Tensor), type(target_boxes) 57 58 src_widths = src_boxes[:, 2] - src_boxes[:, 0] 59 src_heights = src_boxes[:, 3] - src_boxes[:, 1]
71 dw = ww * torch.log(target_widths / src_widths) 72 dh = wh * torch.log(target_heights / src_heights) 73 74 deltas = torch.stack((dx, dy, dw, dh), dim=1) 75 assert (src_widths > 0).all().item(), "Input boxes to Box2BoxTransform are not valid!" 76 return deltas 77 78 def apply_deltas(self, deltas, boxes):
153 src_boxes (Tensor): Nx5 source boxes, e.g., object proposals 154 target_boxes (Tensor): Nx5 target of the transformation, e.g., ground-truth 155 boxes. 156 """ 157 assert isinstance(src_boxes, torch.Tensor), type(src_boxes) 158 assert isinstance(target_boxes, torch.Tensor), type(target_boxes) 159 160 src_ctr_x, src_ctr_y, src_widths, src_heights, src_angles = torch.unbind(src_boxes, dim=1)
154 target_boxes (Tensor): Nx5 target of the transformation, e.g., ground-truth 155 boxes. 156 """ 157 assert isinstance(src_boxes, torch.Tensor), type(src_boxes) 158 assert isinstance(target_boxes, torch.Tensor), type(target_boxes) 159 160 src_ctr_x, src_ctr_y, src_widths, src_heights, src_angles = torch.unbind(src_boxes, dim=1) 161
174 da = (da + 180.0) % 360.0 - 180.0 # make it in [-180, 180) 175 da *= wa * math.pi / 180.0 176 177 deltas = torch.stack((dx, dy, dw, dh, da), dim=1) 178 assert ( 179 (src_widths > 0).all().item() 180 ), "Input boxes to Box2BoxTransformRotated are not valid!" 181 return deltas 182 183 def apply_deltas(self, deltas, boxes):
188 deltas (Tensor): transformation deltas of shape (N, k*5). 189 deltas[i] represents box transformation for the single box boxes[i]. 190 boxes (Tensor): boxes to transform, of shape (N, 5) 191 """ 192 assert deltas.shape[1] % 5 == 0 and boxes.shape[1] == 5 193 194 boxes = boxes.to(deltas.dtype).unsqueeze(2) 195
251 src_boxes (Tensor): square source boxes, e.g., anchors 252 target_boxes (Tensor): target of the transformation, e.g., ground-truth 253 boxes. 254 """ 255 assert isinstance(src_boxes, torch.Tensor), type(src_boxes) 256 assert isinstance(target_boxes, torch.Tensor), type(target_boxes) 257 258 src_ctr_x = 0.5 * (src_boxes[:, 0] + src_boxes[:, 2])
252 target_boxes (Tensor): target of the transformation, e.g., ground-truth 253 boxes. 254 """ 255 assert isinstance(src_boxes, torch.Tensor), type(src_boxes) 256 assert isinstance(target_boxes, torch.Tensor), type(target_boxes) 257 258 src_ctr_x = 0.5 * (src_boxes[:, 0] + src_boxes[:, 2]) 259 src_ctr_y = 0.5 * (src_boxes[:, 1] + src_boxes[:, 3])
47 thus will be considered as true positives. 48 """ 49 # Add -inf and +inf to first and last position in thresholds 50 thresholds = thresholds[:] 51 assert thresholds[0] > 0 52 thresholds.insert(0, -float("inf")) 53 thresholds.append(float("inf")) 54 # Currently torchscript does not support all + generator
51 assert thresholds[0] > 0 52 thresholds.insert(0, -float("inf")) 53 thresholds.append(float("inf")) 54 # Currently torchscript does not support all + generator 55 assert all([low <= high for (low, high) in zip(thresholds[:-1], thresholds[1:])]) 56 assert all([l in [-1, 0, 1] for l in labels]) 57 assert len(labels) == len(thresholds) - 1 58 self.thresholds = thresholds
52 thresholds.insert(0, -float("inf")) 53 thresholds.append(float("inf")) 54 # Currently torchscript does not support all + generator 55 assert all([low <= high for (low, high) in zip(thresholds[:-1], thresholds[1:])]) 56 assert all([l in [-1, 0, 1] for l in labels]) 57 assert len(labels) == len(thresholds) - 1 58 self.thresholds = thresholds 59 self.labels = labels
53 thresholds.append(float("inf")) 54 # Currently torchscript does not support all + generator 55 assert all([low <= high for (low, high) in zip(thresholds[:-1], thresholds[1:])]) 56 assert all([l in [-1, 0, 1] for l in labels]) 57 assert len(labels) == len(thresholds) - 1 58 self.thresholds = thresholds 59 self.labels = labels 60 self.allow_low_quality_matches = allow_low_quality_matches
72 ground-truth index in [0, M) 73 match_labels (Tensor[int8]): a vector of length N, where pred_labels[i] indicates 74 whether a prediction is a true or false positive or ignored 75 """ 76 assert match_quality_matrix.dim() == 2 77 if match_quality_matrix.numel() == 0: 78 default_matches = match_quality_matrix.new_full( 79 (match_quality_matrix.size(1),), 0, dtype=torch.int64
85 (match_quality_matrix.size(1),), self.labels[0], dtype=torch.int8 86 ) 87 return default_matches, default_match_labels 88 89 assert torch.all(match_quality_matrix >= 0) 90 91 # match_quality_matrix is M (gt) x N (predicted) 92 # Max over gt elements (dim 0) to find best gt candidate for each prediction
15 def permute_to_N_HWA_K(tensor, K: int): 16 """ 17 Transpose/reshape a tensor from (N, (Ai x K), H, W) to (N, (HxWxAi), K) 18 """ 19 assert tensor.dim() == 4, tensor.shape 20 N, _, H, W = tensor.shape 21 tensor = tensor.view(N, -1, K, H, W) 22 tensor = tensor.permute(0, 3, 4, 1, 2)
97 features = [features[f] for f in self.head_in_features] 98 predictions = self.head(features) 99 100 if self.training: 101 assert not torch.jit.is_scripting(), "Not supported" 102 assert "instances" in batched_inputs[0], "Instance annotations are missing in training!" 103 gt_instances = [x["instances"].to(self.device) for x in batched_inputs] 104 return self.forward_training(images, features, predictions, gt_instances)
98 predictions = self.head(features) 99 100 if self.training: 101 assert not torch.jit.is_scripting(), "Not supported" 102 assert "instances" in batched_inputs[0], "Instance annotations are missing in training!" 103 gt_instances = [x["instances"].to(self.device) for x in batched_inputs] 104 return self.forward_training(images, features, predictions, gt_instances) 105 else:
149 150 Returns: 151 List[List[Tensor]]: each prediction is transposed to (N, Hi x Wi x Ai, K). 152 """ 153 assert len(predictions) == len(dims_per_anchor) 154 res: List[List[Tensor]] = [] 155 for pred, dim_per_anchor in zip(predictions, dims_per_anchor): 156 pred = [permute_to_N_HWA_K(x, dim_per_anchor) for x in pred]
269 results (List[Instances]): a list of #images elements returned by forward_inference(). 270 """ 271 from custom_detectron2.utils.visualizer import Visualizer 272 273 assert len(batched_inputs) == len( 274 results 275 ), "Cannot visualize inputs and results of different sizes" 276 storage = get_event_storage() 277 max_boxes = 20 278
315 torch.nn.init.normal_(self.ctrness.weight, std=0.01) 316 torch.nn.init.constant_(self.ctrness.bias, 0) 317 318 def forward(self, features): 319 assert len(features) == self._num_features 320 logits = [] 321 bbox_reg = [] 322 ctrness = []
115 return self.inference(batched_inputs) 116 images = self.preprocess_image(batched_inputs) 117 features = self.backbone(images.tensor) 118 119 assert "sem_seg" in batched_inputs[0] 120 gt_sem_seg = [x["sem_seg"].to(self.device) for x in batched_inputs] 121 gt_sem_seg = ImageList.from_tensors( 122 gt_sem_seg,
59 60 self.input_format = input_format 61 self.vis_period = vis_period 62 if vis_period > 0: 63 assert input_format is not None, "input_format is required for visualization!" 64 65 self.register_buffer("pixel_mean", torch.tensor(pixel_mean).view(-1, 1, 1), False) 66 self.register_buffer("pixel_std", torch.tensor(pixel_std).view(-1, 1, 1), False)
63 assert input_format is not None, "input_format is required for visualization!" 64 65 self.register_buffer("pixel_mean", torch.tensor(pixel_mean).view(-1, 1, 1), False) 66 self.register_buffer("pixel_std", torch.tensor(pixel_std).view(-1, 1, 1), False) 67 assert ( 68 self.pixel_mean.shape == self.pixel_std.shape 69 ), f"{self.pixel_mean} and {self.pixel_std} have different shapes!" 70 71 @classmethod 72 def from_config(cls, cfg):
159 160 if self.proposal_generator is not None: 161 proposals, proposal_losses = self.proposal_generator(images, features, gt_instances) 162 else: 163 assert "proposals" in batched_inputs[0] 164 proposals = [x["proposals"].to(self.device) for x in batched_inputs] 165 proposal_losses = {} 166
197 Returns: 198 When do_postprocess=True, same as in :meth:`forward`. 199 Otherwise, a list[Instances] containing raw network outputs. 200 """ 201 assert not self.training 202 203 images = self.preprocess_image(batched_inputs) 204 features = self.backbone(images.tensor)
206 if detected_instances is None: 207 if self.proposal_generator is not None: 208 proposals, _ = self.proposal_generator(images, features, None) 209 else: 210 assert "proposals" in batched_inputs[0] 211 proposals = [x["proposals"].to(self.device) for x in batched_inputs] 212 213 results, _ = self.roi_heads(images, features, proposals, None)
215 detected_instances = [x.to(self.device) for x in detected_instances] 216 results = self.roi_heads.forward_with_given_boxes(features, detected_instances) 217 218 if do_postprocess: 219 assert not torch.jit.is_scripting(), "Scripting is not supported for postprocess." 220 return GeneralizedRCNN._postprocess(results, batched_inputs, images.image_sizes) 221 return results 222
399 400 @classmethod 401 def from_config(cls, cfg, input_shape: List[ShapeSpec]): 402 num_anchors = build_anchor_generator(cfg, input_shape).num_cell_anchors 403 assert ( 404 len(set(num_anchors)) == 1 405 ), "Using different number of anchors between levels is not currently supported!" 406 num_anchors = num_anchors[0] 407 408 return {
429 The tensor predicts 4-vector (dx,dy,dw,dh) box 430 regression values for every anchor. These values are the 431 relative offset between the anchor and the ground truth box. 432 """ 433 assert len(features) == self._num_features 434 logits = [] 435 bbox_reg = [] 436 for feature in features:
100 def forward(self, x) -> Dict[str, Tensor]: 101 outs = self.backbone(x) 102 if self.neck is not None: 103 outs = self.neck(outs) 104 assert isinstance( 105 outs, (list, tuple) 106 ), "mmdet backbone should return a list/tuple of tensors!" 107 if len(outs) != len(self._output_shapes): 108 raise ValueError( 109 "Length of output_shapes does not match outputs from the mmdet backbone: "
149 self.size_divisibility = size_divisibility 150 151 self.register_buffer("pixel_mean", torch.tensor(pixel_mean).view(-1, 1, 1), False) 152 self.register_buffer("pixel_std", torch.tensor(pixel_std).view(-1, 1, 1), False) 153 assert ( 154 self.pixel_mean.shape == self.pixel_std.shape 155 ), f"{self.pixel_mean} and {self.pixel_std} have different shapes!" 156 157 def forward(self, batched_inputs: List[Dict[str, torch.Tensor]]): 158 images = [x["image"].to(self.device) for x in batched_inputs]
155 super().__init__() 156 157 if isinstance(output_size, int): 158 output_size = (output_size, output_size) 159 assert len(output_size) == 2 160 assert isinstance(output_size[0], int) and isinstance(output_size[1], int) 161 self.output_size = output_size 162
156 157 if isinstance(output_size, int): 158 output_size = (output_size, output_size) 159 assert len(output_size) == 2 160 assert isinstance(output_size[0], int) and isinstance(output_size[1], int) 161 self.output_size = output_size 162 163 if pooler_type == "ROIAlign":
189 # Map scale (defined as 1 / stride) to its feature map level under the 190 # assumption that stride is a power of 2. 191 min_level = -(math.log2(scales[0])) 192 max_level = -(math.log2(scales[-1])) 193 assert math.isclose(min_level, int(min_level)) and math.isclose( 194 max_level, int(max_level) 195 ), "Featuremap stride is not power of 2!" 196 self.min_level = int(min_level) 197 self.max_level = int(max_level) 198 assert (
194 max_level, int(max_level) 195 ), "Featuremap stride is not power of 2!" 196 self.min_level = int(min_level) 197 self.max_level = int(max_level) 198 assert ( 199 len(scales) == self.max_level - self.min_level + 1 200 ), "[ROIPooler] Sizes of input featuremaps do not form a pyramid!" 201 assert 0 <= self.min_level and self.min_level <= self.max_level 202 self.canonical_level = canonical_level 203 assert canonical_box_size > 0
197 self.max_level = int(max_level) 198 assert ( 199 len(scales) == self.max_level - self.min_level + 1 200 ), "[ROIPooler] Sizes of input featuremaps do not form a pyramid!" 201 assert 0 <= self.min_level and self.min_level <= self.max_level 202 self.canonical_level = canonical_level 203 assert canonical_box_size > 0 204 self.canonical_box_size = canonical_box_size
199 len(scales) == self.max_level - self.min_level + 1 200 ), "[ROIPooler] Sizes of input featuremaps do not form a pyramid!" 201 assert 0 <= self.min_level and self.min_level <= self.max_level 202 self.canonical_level = canonical_level 203 assert canonical_box_size > 0 204 self.canonical_box_size = canonical_box_size 205 206 def forward(self, x: List[torch.Tensor], box_lists: List[Boxes]):
49 elif results.has("proposal_boxes"): 50 output_boxes = results.proposal_boxes 51 else: 52 output_boxes = None 53 assert output_boxes is not None, "Predictions must contain boxes!" 54 55 output_boxes.scale(scale_x, scale_y) 56 output_boxes.clip(results.image_size)
150 Returns: 151 list[Instances]: list of N Instances. Each is the proposals for the image, 152 with field "proposal_boxes" and "objectness_logits". 153 """ 154 assert gt is not None 155 156 if len(proposals) != len(gt): 157 raise ValueError("proposals and gt should have the same length as the number of images!")
193 gt_proposal.proposal_boxes = gt_boxes 194 gt_proposal.objectness_logits = gt_logits 195 196 for key in proposals.get_fields().keys(): 197 assert gt_proposal.has( 198 key 199 ), "The attribute '{}' in `proposals` does not exist in `gt`".format(key) 200 201 # NOTE: Instances.cat only use fields from the first item. Extra fields in latter items 202 # will be thrown away.
136 @classmethod 137 def from_config(cls, cfg, input_shape): 138 # Standard RPN is shared across levels: 139 in_channels = [s.channels for s in input_shape] 140 assert len(set(in_channels)) == 1, "Each level must have the same channel!" 141 in_channels = in_channels[0] 142 143 # RPNHead should take the same input as anchor generator
144 # NOTE: it assumes that creating an anchor generator does not have unwanted side effect. 145 anchor_generator = build_anchor_generator(cfg, input_shape) 146 num_anchors = anchor_generator.num_anchors 147 box_dim = anchor_generator.box_dim 148 assert ( 149 len(set(num_anchors)) == 1 150 ), "Each level must have the same number of anchors per spatial position" 151 return { 152 "in_channels": in_channels, 153 "num_anchors": num_anchors[0],
466 for x in pred_anchor_deltas 467 ] 468 469 if self.training: 470 assert gt_instances is not None, "RPN requires gt_instances in training!" 471 gt_labels, gt_boxes = self.label_and_sample_anchors(anchors, gt_instances) 472 losses = self.losses( 473 anchors, pred_objectness_logits, gt_labels, pred_anchor_deltas, gt_boxes
43 conv_norm (str or callable): normalization for the conv layers. 44 See :func:`detectron2.layers.get_norm` for supported types. 45 """ 46 super().__init__() 47 assert len(conv_dims) + len(fc_dims) > 0 48 49 self._output_size = (input_shape.channels, input_shape.height, input_shape.width) 50
56 match boxes with ground truth for each stage. The first matcher matches 57 RPN proposals with ground truth, the other matchers use boxes predicted 58 by the previous stage as proposals and match them with ground truth. 59 """ 60 assert "proposal_matcher" not in kwargs, ( 61 "CascadeROIHeads takes 'proposal_matchers=' for each stage instead " 62 "of one 'proposal_matcher='." 63 ) 64 # The first matcher matches RPN proposals with ground truth, done in the base class 65 kwargs["proposal_matcher"] = proposal_matchers[0] 66 num_stages = self.num_cascade_stages = len(box_heads)
65 kwargs["proposal_matcher"] = proposal_matchers[0] 66 num_stages = self.num_cascade_stages = len(box_heads) 67 box_heads = nn.ModuleList(box_heads) 68 box_predictors = nn.ModuleList(box_predictors) 69 assert len(box_predictors) == num_stages, f"{len(box_predictors)} != {num_stages}!" 70 assert len(proposal_matchers) == num_stages, f"{len(proposal_matchers)} != {num_stages}!" 71 super().__init__( 72 box_in_features=box_in_features,
66 num_stages = self.num_cascade_stages = len(box_heads) 67 box_heads = nn.ModuleList(box_heads) 68 box_predictors = nn.ModuleList(box_predictors) 69 assert len(box_predictors) == num_stages, f"{len(box_predictors)} != {num_stages}!" 70 assert len(proposal_matchers) == num_stages, f"{len(proposal_matchers)} != {num_stages}!" 71 super().__init__( 72 box_in_features=box_in_features, 73 box_pooler=box_pooler,
92 sampling_ratio = cfg.MODEL.ROI_BOX_HEAD.POOLER_SAMPLING_RATIO 93 pooler_type = cfg.MODEL.ROI_BOX_HEAD.POOLER_TYPE 94 cascade_bbox_reg_weights = cfg.MODEL.ROI_BOX_CASCADE_HEAD.BBOX_REG_WEIGHTS 95 cascade_ious = cfg.MODEL.ROI_BOX_CASCADE_HEAD.IOUS 96 assert len(cascade_bbox_reg_weights) == len(cascade_ious) 97 assert cfg.MODEL.ROI_BOX_HEAD.CLS_AGNOSTIC_BBOX_REG, \ 98 "CascadeROIHeads only support class-agnostic regression now!" 99 assert cascade_ious[0] == cfg.MODEL.ROI_HEADS.IOU_THRESHOLDS[0]
93 pooler_type = cfg.MODEL.ROI_BOX_HEAD.POOLER_TYPE 94 cascade_bbox_reg_weights = cfg.MODEL.ROI_BOX_CASCADE_HEAD.BBOX_REG_WEIGHTS 95 cascade_ious = cfg.MODEL.ROI_BOX_CASCADE_HEAD.IOUS 96 assert len(cascade_bbox_reg_weights) == len(cascade_ious) 97 assert cfg.MODEL.ROI_BOX_HEAD.CLS_AGNOSTIC_BBOX_REG, \ 98 "CascadeROIHeads only support class-agnostic regression now!" 99 assert cascade_ious[0] == cfg.MODEL.ROI_HEADS.IOU_THRESHOLDS[0] 100 # fmt: on 101
95 cascade_ious = cfg.MODEL.ROI_BOX_CASCADE_HEAD.IOUS 96 assert len(cascade_bbox_reg_weights) == len(cascade_ious) 97 assert cfg.MODEL.ROI_BOX_HEAD.CLS_AGNOSTIC_BBOX_REG, \ 98 "CascadeROIHeads only support class-agnostic regression now!" 99 assert cascade_ious[0] == cfg.MODEL.ROI_HEADS.IOU_THRESHOLDS[0] 100 # fmt: on 101 102 in_channels = [input_shape[f].channels for f in in_features]
100 # fmt: on 101 102 in_channels = [input_shape[f].channels for f in in_features] 103 # Check all channel counts are equal 104 assert len(set(in_channels)) == 1, in_channels 105 in_channels = in_channels[0] 106 107 box_pooler = ROIPooler(
256 self.use_sigmoid_ce = use_sigmoid_ce 257 self.fed_loss_num_classes = fed_loss_num_classes 258 259 if self.use_fed_loss: 260 assert self.use_sigmoid_ce, "Please use sigmoid cross entropy loss with federated loss" 261 fed_loss_cls_weights = get_fed_loss_cls_weights() 262 assert ( 263 len(fed_loss_cls_weights) == self.num_classes
258 259 if self.use_fed_loss: 260 assert self.use_sigmoid_ce, "Please use sigmoid cross entropy loss with federated loss" 261 fed_loss_cls_weights = get_fed_loss_cls_weights() 262 assert ( 263 len(fed_loss_cls_weights) == self.num_classes 264 ), "Please check the provided fed_loss_cls_weights. Their size should match num_classes" 265 self.register_buffer("fed_loss_cls_weights", fed_loss_cls_weights) 266 267 @classmethod
325 326 # parse box regression outputs 327 if len(proposals): 328 proposal_boxes = cat([p.proposal_boxes.tensor for p in proposals], dim=0) # Nx4 329 assert not proposal_boxes.requires_grad, "Proposals should not require gradients!" 330 # If "gt_boxes" does not exist, the proposals must be all negative and 331 # should not be included in regression loss computation. 332 # Here we just use proposal_boxes as an arbitrary placeholder because its
153 """ 154 super().__init__() 155 self.num_keypoints = num_keypoints 156 self.loss_weight = loss_weight 157 assert loss_normalizer == "visible" or isinstance(loss_normalizer, float), loss_normalizer 158 self.loss_normalizer = loss_normalizer 159 160 @classmethod
50 """ 51 cls_agnostic_mask = pred_mask_logits.size(1) == 1 52 total_num_masks = pred_mask_logits.size(0) 53 mask_side_len = pred_mask_logits.size(2) 54 assert pred_mask_logits.size(2) == pred_mask_logits.size(3), "Mask prediction must be square!" 55 56 gt_classes = [] 57 gt_masks = []
232 conv_norm (str or callable): normalization for the conv layers. 233 See :func:`detectron2.layers.get_norm` for supported types. 234 """ 235 super().__init__(**kwargs) 236 assert len(conv_dims) >= 1, "conv_dims have to be non-empty!" 237 238 self.conv_norm_relus = [] 239
60 list[Instances]: N Instances, each contains only the selected foreground instances. 61 list[Tensor]: N boolean vector, correspond to the selection mask of 62 each Instances object. True for selected instances. 63 """ 64 assert isinstance(proposals, (list, tuple)) 65 assert isinstance(proposals[0], Instances) 66 assert proposals[0].has("gt_classes") 67 fg_proposals = []
61 list[Tensor]: N boolean vector, correspond to the selection mask of 62 each Instances object. True for selected instances. 63 """ 64 assert isinstance(proposals, (list, tuple)) 65 assert isinstance(proposals[0], Instances) 66 assert proposals[0].has("gt_classes") 67 fg_proposals = [] 68 fg_selection_masks = []
62 each Instances object. True for selected instances. 63 """ 64 assert isinstance(proposals, (list, tuple)) 65 assert isinstance(proposals[0], Instances) 66 assert proposals[0].has("gt_classes") 67 fg_proposals = [] 68 fg_selection_masks = [] 69 for proposals_per_image in proposals:
393 pooler_scales = (1.0 / input_shape[in_features[0]].stride, ) 394 sampling_ratio = cfg.MODEL.ROI_BOX_HEAD.POOLER_SAMPLING_RATIO 395 mask_on = cfg.MODEL.MASK_ON 396 # fmt: on 397 assert not cfg.MODEL.KEYPOINT_ON 398 assert len(in_features) == 1 399 400 ret["pooler"] = ROIPooler(
394 sampling_ratio = cfg.MODEL.ROI_BOX_HEAD.POOLER_SAMPLING_RATIO 395 mask_on = cfg.MODEL.MASK_ON 396 # fmt: on 397 assert not cfg.MODEL.KEYPOINT_ON 398 assert len(in_features) == 1 399 400 ret["pooler"] = ROIPooler( 401 output_size=pooler_resolution,
434 bottleneck_channels = num_groups * width_per_group * stage_channel_factor 435 out_channels = cfg.MODEL.RESNETS.RES2_OUT_CHANNELS * stage_channel_factor 436 stride_in_1x1 = cfg.MODEL.RESNETS.STRIDE_IN_1X1 437 norm = cfg.MODEL.RESNETS.NORM 438 assert not cfg.MODEL.RESNETS.DEFORM_ON_PER_STAGE[-1], \ 439 "Deformable conv is not yet supported in res5 head." 440 # fmt: on 441 442 blocks = ResNet.make_stage(
468 """ 469 del images 470 471 if self.training: 472 assert targets 473 proposals = self.label_and_sample_proposals(proposals, targets) 474 del targets 475
514 instances (Instances): 515 the same `Instances` object, with extra 516 fields such as `pred_masks` or `pred_keypoints`. 517 """ 518 assert not self.training 519 assert instances[0].has("pred_boxes") and instances[0].has("pred_classes") 520 521 if self.mask_on:
515 the same `Instances` object, with extra 516 fields such as `pred_masks` or `pred_keypoints`. 517 """ 518 assert not self.training 519 assert instances[0].has("pred_boxes") and instances[0].has("pred_classes") 520 521 if self.mask_on: 522 feature_list = [features[f] for f in self.in_features]
627 # If StandardROIHeads is applied on multiple feature maps (as in FPN), 628 # then we share the same predictors and therefore the channel counts must be the same 629 in_channels = [input_shape[f].channels for f in in_features] 630 # Check all channel counts are equal 631 assert len(set(in_channels)) == 1, in_channels 632 in_channels = in_channels[0] 633 634 box_pooler = ROIPooler(
730 See :class:`ROIHeads.forward`. 731 """ 732 del images 733 if self.training: 734 assert targets, "'targets' argument is required during training" 735 proposals = self.label_and_sample_proposals(proposals, targets) 736 del targets 737
769 list[Instances]: 770 the same `Instances` objects, with extra 771 fields such as `pred_masks` or `pred_keypoints`. 772 """ 773 assert not self.training 774 assert instances[0].has("pred_boxes") and instances[0].has("pred_classes") 775 776 instances = self._forward_mask(features, instances)
770 the same `Instances` objects, with extra 771 fields such as `pred_masks` or `pred_keypoints`. 772 """ 773 assert not self.training 774 assert instances[0].has("pred_boxes") and instances[0].has("pred_classes") 775 776 instances = self._forward_mask(features, instances) 777 instances = self._forward_keypoint(features, instances)
177 """ 178 NOTE: this interface is experimental. 179 """ 180 super().__init__(**kwargs) 181 assert ( 182 not self.mask_on and not self.keypoint_on 183 ), "Mask/Keypoints not supported in Rotated ROIHeads." 184 assert not self.train_on_pred_boxes, "train_on_pred_boxes not implemented for RROIHeads!" 185 186 @classmethod
180 super().__init__(**kwargs) 181 assert ( 182 not self.mask_on and not self.keypoint_on 183 ), "Mask/Keypoints not supported in Rotated ROIHeads." 184 assert not self.train_on_pred_boxes, "train_on_pred_boxes not implemented for RROIHeads!" 185 186 @classmethod 187 def _init_box_head(cls, cfg, input_shape):
191 pooler_scales = tuple(1.0 / input_shape[k].stride for k in in_features) 192 sampling_ratio = cfg.MODEL.ROI_BOX_HEAD.POOLER_SAMPLING_RATIO 193 pooler_type = cfg.MODEL.ROI_BOX_HEAD.POOLER_TYPE 194 # fmt: on 195 assert pooler_type in ["ROIAlignRotated"], pooler_type 196 # assume all channel counts are equal 197 in_channels = [input_shape[f].channels for f in in_features][0] 198
116 """ 117 super().__init__() 118 if isinstance(model, DistributedDataParallel): 119 model = model.module 120 assert isinstance( 121 model, GeneralizedRCNN 122 ), "TTA is only supported on GeneralizedRCNN. Got a model of type {}".format(type(model)) 123 self.cfg = cfg.clone() 124 assert not self.cfg.MODEL.KEYPOINT_ON, "TTA for keypoint is not supported yet" 125 assert (
120 assert isinstance( 121 model, GeneralizedRCNN 122 ), "TTA is only supported on GeneralizedRCNN. Got a model of type {}".format(type(model)) 123 self.cfg = cfg.clone() 124 assert not self.cfg.MODEL.KEYPOINT_ON, "TTA for keypoint is not supported yet" 125 assert ( 126 not self.cfg.MODEL.LOAD_PROPOSALS 127 ), "TTA for pre-computed proposals is not supported yet"
121 model, GeneralizedRCNN 122 ), "TTA is only supported on GeneralizedRCNN. Got a model of type {}".format(type(model)) 123 self.cfg = cfg.clone() 124 assert not self.cfg.MODEL.KEYPOINT_ON, "TTA for keypoint is not supported yet" 125 assert ( 126 not self.cfg.MODEL.LOAD_PROPOSALS 127 ), "TTA for pre-computed proposals is not supported yet" 128 129 self.model = model 130
107 deform_modulated = cfg.MODEL.RESNETS.DEFORM_MODULATED 108 deform_num_groups = cfg.MODEL.RESNETS.DEFORM_NUM_GROUPS 109 res5_multi_grid = cfg.MODEL.RESNETS.RES5_MULTI_GRID 110 # fmt: on 111 assert res4_dilation in {1, 2}, "res4_dilation cannot be {}.".format(res4_dilation) 112 assert res5_dilation in {1, 2, 4}, "res5_dilation cannot be {}.".format(res5_dilation) 113 if res4_dilation == 2: 114 # Always dilate res5 if res4 is dilated.
108 deform_num_groups = cfg.MODEL.RESNETS.DEFORM_NUM_GROUPS 109 res5_multi_grid = cfg.MODEL.RESNETS.RES5_MULTI_GRID 110 # fmt: on 111 assert res4_dilation in {1, 2}, "res4_dilation cannot be {}.".format(res4_dilation) 112 assert res5_dilation in {1, 2, 4}, "res5_dilation cannot be {}.".format(res5_dilation) 113 if res4_dilation == 2: 114 # Always dilate res5 if res4 is dilated. 115 assert res5_dilation == 4
111 assert res4_dilation in {1, 2}, "res4_dilation cannot be {}.".format(res4_dilation) 112 assert res5_dilation in {1, 2, 4}, "res5_dilation cannot be {}.".format(res5_dilation) 113 if res4_dilation == 2: 114 # Always dilate res5 if res4 is dilated. 115 assert res5_dilation == 4 116 117 num_blocks_per_stage = {50: [3, 4, 6, 3], 101: [3, 4, 23, 3], 152: [3, 8, 36, 3]}[depth] 118
80 self.decoder_only = num_classes is None 81 self.use_depthwise_separable_conv = use_depthwise_separable_conv 82 # fmt: on 83 84 assert ( 85 len(project_channels) == len(self.in_features) - 1 86 ), "Expected {} project_channels, got {}".format( 87 len(self.in_features) - 1, len(project_channels) 88 ) 89 assert len(decoder_channels) == len( 90 self.in_features 91 ), "Expected {} decoder_channels, got {}".format(
85 len(project_channels) == len(self.in_features) - 1 86 ), "Expected {} project_channels, got {}".format( 87 len(self.in_features) - 1, len(project_channels) 88 ) 89 assert len(decoder_channels) == len( 90 self.in_features 91 ), "Expected {} decoder_channels, got {}".format( 92 len(self.in_features), len(decoder_channels) 93 ) 94 self.decoder = nn.ModuleDict() 95 96 use_bias = norm == ""
189 190 @classmethod 191 def from_config(cls, cfg, input_shape): 192 if cfg.INPUT.CROP.ENABLED: 193 assert cfg.INPUT.CROP.TYPE == "absolute" 194 train_size = cfg.INPUT.CROP.SIZE 195 else: 196 train_size = None
285 aspp_dropout = cfg.MODEL.SEM_SEG_HEAD.ASPP_DROPOUT 286 use_depthwise_separable_conv = cfg.MODEL.SEM_SEG_HEAD.USE_DEPTHWISE_SEPARABLE_CONV 287 # fmt: on 288 289 assert len(self.in_features) == 1 290 assert len(in_channels) == 1 291 292 # ASPP module
286 use_depthwise_separable_conv = cfg.MODEL.SEM_SEG_HEAD.USE_DEPTHWISE_SEPARABLE_CONV 287 # fmt: on 288 289 assert len(self.in_features) == 1 290 assert len(in_channels) == 1 291 292 # ASPP module 293 if cfg.INPUT.CROP.ENABLED:
290 assert len(in_channels) == 1 291 292 # ASPP module 293 if cfg.INPUT.CROP.ENABLED: 294 assert cfg.INPUT.CROP.TYPE == "absolute" 295 train_crop_h, train_crop_w = train_crop_size 296 if train_crop_h % self.common_stride or train_crop_w % self.common_stride: 297 raise ValueError("Crop size need to be divisible by output stride.")
55 """ 56 Dynamically creates a new type that inherits the type of a given instance 57 and overrides the `step` method to add gradient clipping 58 """ 59 assert ( 60 per_param_clipper is None or global_clipper is None 61 ), "Not allowed to use both per-parameter clipping and global clipping" 62 63 def optimizer_wgc_step(self, closure=None): 64 if per_param_clipper is not None:
101 return optimizer 102 if isinstance(optimizer, torch.optim.Optimizer): 103 optimizer_type = type(optimizer) 104 else: 105 assert issubclass(optimizer, torch.optim.Optimizer), optimizer 106 optimizer_type = optimizer 107 108 grad_clipper = _create_gradient_clipper(cfg.SOLVER.CLIP_GRADIENTS)
239 # Transform parameter groups into per-parameter structure. 240 # Later items in `params` can overwrite parameters set in previous items. 241 ret = defaultdict(dict) 242 for item in params: 243 assert "params" in item 244 cur_params = {x: y for x, y in item.items() if x != "params"} 245 for param in item["params"]: 246 ret[param].update({"params": [param], **cur_params})
287 num_updates=cfg.SOLVER.MAX_ITER, 288 ) 289 elif name == "WarmupCosineLR": 290 end_value = cfg.SOLVER.BASE_LR_END / cfg.SOLVER.BASE_LR 291 assert end_value >= 0.0 and end_value <= 1.0, end_value 292 sched = CosineParamScheduler(1, end_value) 293 elif name == "WarmupStepWithFixedGammaLR": 294 sched = StepWithFixedGammaParamScheduler(
56 original_type = type(box) 57 is_numpy = isinstance(box, np.ndarray) 58 single_box = isinstance(box, (list, tuple)) 59 if single_box: 60 assert len(box) == 4 or len(box) == 5, ( 61 "BoxMode.convert takes either a k-tuple/list or an Nxk array/tensor," 62 " where k == 4 or 5" 63 ) 64 arr = torch.tensor(box)[None, :] 65 else: 66 # avoid modifying the input box
68 arr = torch.from_numpy(np.asarray(box)).clone() 69 else: 70 arr = box.clone() 71 72 assert to_mode not in [BoxMode.XYXY_REL, BoxMode.XYWH_REL] and from_mode not in [ 73 BoxMode.XYXY_REL, 74 BoxMode.XYWH_REL, 75 ], "Relative mode not yet supported!" 76 77 if from_mode == BoxMode.XYWHA_ABS and to_mode == BoxMode.XYXY_ABS: 78 assert (
74 BoxMode.XYWH_REL, 75 ], "Relative mode not yet supported!" 76 77 if from_mode == BoxMode.XYWHA_ABS and to_mode == BoxMode.XYXY_ABS: 78 assert ( 79 arr.shape[-1] == 5 80 ), "The last dimension of input shape must be 5 for XYWHA format" 81 original_dtype = arr.dtype 82 arr = arr.double() 83
151 if tensor.numel() == 0: 152 # Use reshape, so we don't end up creating a new tensor that does not depend on 153 # the inputs (and consequently confuses jit) 154 tensor = tensor.reshape((-1, 4)).to(dtype=torch.float32) 155 assert tensor.dim() == 2 and tensor.size(-1) == 4, tensor.size() 156 157 self.tensor = tensor 158
187 188 Args: 189 box_size (height, width): The clipping box's size. 190 """ 191 assert torch.isfinite(self.tensor).all(), "Box tensor contains infinite or NaN!" 192 h, w = box_size 193 x1 = self.tensor[:, 0].clamp(min=0, max=w) 194 y1 = self.tensor[:, 1].clamp(min=0, max=h)
232 """ 233 if isinstance(item, int): 234 return Boxes(self.tensor[item].view(1, -1)) 235 b = self.tensor[item] 236 assert b.dim() == 2, "Indexing on Boxes with {} failed to return a matrix!".format(item) 237 return Boxes(b) 238 239 def __len__(self) -> int:
285 286 Returns: 287 Boxes: the concatenated Boxes 288 """ 289 assert isinstance(boxes_list, (list, tuple)) 290 if len(boxes_list) == 0: 291 return cls(torch.empty(0)) 292 assert all([isinstance(box, Boxes) for box in boxes_list])
288 """ 289 assert isinstance(boxes_list, (list, tuple)) 290 if len(boxes_list) == 0: 291 return cls(torch.empty(0)) 292 assert all([isinstance(box, Boxes) for box in boxes_list]) 293 294 # use torch.cat (v.s. layers.cat) so the returned boxes never share storage with input 295 cat_boxes = cls(torch.cat([b.tensor for b in boxes_list], dim=0))
408 boxes2 (Boxes): same length as boxes1 409 Returns: 410 Tensor: iou, sized [N]. 411 """ 412 assert len(boxes1) == len( 413 boxes2 414 ), "boxlists should have the same" "number of entries, got {}, {}".format( 415 len(boxes1), len(boxes2) 416 ) 417 area1 = boxes1.area() # [N] 418 area2 = boxes2.area() # [N] 419 box1, box2 = boxes1.tensor, boxes2.tensor
77 square padding size if `square_size` > 0. 78 Returns: 79 an `ImageList`. 80 """ 81 assert len(tensors) > 0 82 assert isinstance(tensors, (tuple, list)) 83 for t in tensors: 84 assert isinstance(t, torch.Tensor), type(t)
78 Returns: 79 an `ImageList`. 80 """ 81 assert len(tensors) > 0 82 assert isinstance(tensors, (tuple, list)) 83 for t in tensors: 84 assert isinstance(t, torch.Tensor), type(t) 85 assert t.shape[:-2] == tensors[0].shape[:-2], t.shape
80 """ 81 assert len(tensors) > 0 82 assert isinstance(tensors, (tuple, list)) 83 for t in tensors: 84 assert isinstance(t, torch.Tensor), type(t) 85 assert t.shape[:-2] == tensors[0].shape[:-2], t.shape 86 87 image_sizes = [(im.shape[-2], im.shape[-1]) for im in tensors]
81 assert len(tensors) > 0 82 assert isinstance(tensors, (tuple, list)) 83 for t in tensors: 84 assert isinstance(t, torch.Tensor), type(t) 85 assert t.shape[:-2] == tensors[0].shape[:-2], t.shape 86 87 image_sizes = [(im.shape[-2], im.shape[-1]) for im in tensors] 88 image_sizes_tensor = [shapes_to_tensor(x) for x in image_sizes]
74 """ 75 with warnings.catch_warnings(record=True): 76 data_len = len(value) 77 if len(self._fields): 78 assert ( 79 len(self) == data_len 80 ), "Adding a field of length {} to a Instances of length {}".format(data_len, len(self)) 81 self._fields[name] = value 82 83 def has(self, name: str) -> bool:
158 159 Returns: 160 Instances 161 """ 162 assert all(isinstance(i, Instances) for i in instance_lists) 163 assert len(instance_lists) > 0 164 if len(instance_lists) == 1: 165 return instance_lists[0]
159 Returns: 160 Instances 161 """ 162 assert all(isinstance(i, Instances) for i in instance_lists) 163 assert len(instance_lists) > 0 164 if len(instance_lists) == 1: 165 return instance_lists[0] 166
166 167 image_size = instance_lists[0].image_size 168 if not isinstance(image_size, torch.Tensor): # could be a tensor in tracing 169 for i in instance_lists[1:]: 170 assert i.image_size == image_size 171 ret = Instances(image_size) 172 for k in instance_lists[0]._fields.keys(): 173 values = [i.get(k) for i in instance_lists]
26 instances, and K is the number of keypoints per instance. 27 """ 28 device = keypoints.device if isinstance(keypoints, torch.Tensor) else torch.device("cpu") 29 keypoints = torch.as_tensor(keypoints, dtype=torch.float32, device=device) 30 assert keypoints.dim() == 3 and keypoints.shape[2] == 3, keypoints.shape 31 self.tensor = keypoints 32 33 def __len__(self) -> int:
90 91 Returns: 92 Keypoints: the concatenated Keypoints 93 """ 94 assert isinstance(keypoints_list, (list, tuple)) 95 assert len(keypoints_list) > 0 96 assert all(isinstance(keypoints, Keypoints) for keypoints in keypoints_list) 97
91 Returns: 92 Keypoints: the concatenated Keypoints 93 """ 94 assert isinstance(keypoints_list, (list, tuple)) 95 assert len(keypoints_list) > 0 96 assert all(isinstance(keypoints, Keypoints) for keypoints in keypoints_list) 97 98 cat_kpts = type(keypoints_list[0])(
92 Keypoints: the concatenated Keypoints 93 """ 94 assert isinstance(keypoints_list, (list, tuple)) 95 assert len(keypoints_list) > 0 96 assert all(isinstance(keypoints, Keypoints) for keypoints in keypoints_list) 97 98 cat_kpts = type(keypoints_list[0])( 99 torch.cat([kpts.tensor for kpts in keypoints_list], dim=0)
218 219 x_int = pos % w 220 y_int = (pos - x_int) // w 221 222 assert ( 223 roi_map_scores[keypoints_idx, y_int, x_int] 224 == roi_map_scores.view(num_keypoints, -1).max(1)[0] 225 ).all() 226 227 x = (x_int.float() + 0.5) * width_corrections[i] 228 y = (y_int.float() + 0.5) * height_corrections[i]
102 if isinstance(tensor, torch.Tensor): 103 tensor = tensor.to(torch.bool) 104 else: 105 tensor = torch.as_tensor(tensor, dtype=torch.bool, device=torch.device("cpu")) 106 assert tensor.dim() == 3, tensor.size() 107 self.image_size = tensor.shape[1:] 108 self.tensor = tensor 109
133 """ 134 if isinstance(item, int): 135 return BitMasks(self.tensor[item].unsqueeze(0)) 136 m = self.tensor[item] 137 assert m.dim() == 3, "Indexing on BitMasks with {} returns a tensor with shape {}!".format( 138 item, m.shape 139 ) 140 return BitMasks(m) 141 142 @torch.jit.unused
204 Tensor: 205 A bool tensor of shape (N, mask_size, mask_size), where 206 N is the number of predicted boxes for this image. 207 """ 208 assert len(boxes) == len(self), "{} != {}".format(len(boxes), len(self)) 209 device = self.tensor.device 210 211 batch_inds = torch.arange(len(boxes), device=device).to(dtype=boxes.dtype)[:, None]
249 250 Returns: 251 BitMasks: the concatenated BitMasks 252 """ 253 assert isinstance(bitmasks_list, (list, tuple)) 254 assert len(bitmasks_list) > 0 255 assert all(isinstance(bitmask, BitMasks) for bitmask in bitmasks_list) 256
250 Returns: 251 BitMasks: the concatenated BitMasks 252 """ 253 assert isinstance(bitmasks_list, (list, tuple)) 254 assert len(bitmasks_list) > 0 255 assert all(isinstance(bitmask, BitMasks) for bitmask in bitmasks_list) 256 257 cat_bitmasks = type(bitmasks_list[0])(torch.cat([bm.tensor for bm in bitmasks_list], dim=0))
251 BitMasks: the concatenated BitMasks 252 """ 253 assert isinstance(bitmasks_list, (list, tuple)) 254 assert len(bitmasks_list) > 0 255 assert all(isinstance(bitmask, BitMasks) for bitmask in bitmasks_list) 256 257 cat_bitmasks = type(bitmasks_list[0])(torch.cat([bm.tensor for bm in bitmasks_list], dim=0)) 258 return cat_bitmasks
365 selected_polygons = [self.polygons[i] for i in item] 366 elif isinstance(item, torch.Tensor): 367 # Polygons is a list, so we have to move the indices back to CPU. 368 if item.dtype == torch.bool: 369 assert item.dim() == 1, item.shape 370 item = item.nonzero().squeeze(1).cpu().numpy().tolist() 371 elif item.dtype in [torch.int32, torch.int64]: 372 item = item.cpu().numpy().tolist()
403 Returns: 404 Tensor: A bool tensor of shape (N, mask_size, mask_size), where 405 N is the number of predicted boxes for this image. 406 """ 407 assert len(boxes) == len(self), "{} != {}".format(len(boxes), len(self)) 408 409 device = boxes.device 410 # Put boxes on the CPU, as the polygon representation is not efficient GPU-wise
452 453 Returns: 454 PolygonMasks: the concatenated PolygonMasks 455 """ 456 assert isinstance(polymasks_list, (list, tuple)) 457 assert len(polymasks_list) > 0 458 assert all(isinstance(polymask, PolygonMasks) for polymask in polymasks_list) 459
453 Returns: 454 PolygonMasks: the concatenated PolygonMasks 455 """ 456 assert isinstance(polymasks_list, (list, tuple)) 457 assert len(polymasks_list) > 0 458 assert all(isinstance(polymask, PolygonMasks) for polymask in polymasks_list) 459 460 cat_polymasks = type(polymasks_list[0])(
454 PolygonMasks: the concatenated PolygonMasks 455 """ 456 assert isinstance(polymasks_list, (list, tuple)) 457 assert len(polymasks_list) > 0 458 assert all(isinstance(polymask, PolygonMasks) for polymask in polymasks_list) 459 460 cat_polymasks = type(polymasks_list[0])( 461 list(itertools.chain.from_iterable(pm.polygons for pm in polymasks_list))
215 if tensor.numel() == 0: 216 # Use reshape, so we don't end up creating a new tensor that does not depend on 217 # the inputs (and consequently confuses jit) 218 tensor = tensor.reshape((0, 5)).to(dtype=torch.float32, device=device) 219 assert tensor.dim() == 2 and tensor.size(-1) == 5, tensor.size() 220 221 self.tensor = tensor 222
334 """ 335 if isinstance(item, int): 336 return RotatedBoxes(self.tensor[item].view(1, -1)) 337 b = self.tensor[item] 338 assert b.dim() == 2, "Indexing on RotatedBoxes with {} failed to return a matrix!".format( 339 item 340 ) 341 return RotatedBoxes(b) 342 343 def __len__(self) -> int:
465 466 Returns: 467 RotatedBoxes: the concatenated RotatedBoxes 468 """ 469 assert isinstance(boxes_list, (list, tuple)) 470 if len(boxes_list) == 0: 471 return cls(torch.empty(0)) 472 assert all([isinstance(box, RotatedBoxes) for box in boxes_list])
468 """ 469 assert isinstance(boxes_list, (list, tuple)) 470 if len(boxes_list) == 0: 471 return cls(torch.empty(0)) 472 assert all([isinstance(box, RotatedBoxes) for box in boxes_list]) 473 474 # use torch.cat (v.s. layers.cat) so the returned boxes never share storage with input 475 cat_boxes = cls(torch.cat([b.tensor for b in boxes_list], dim=0))
65 cfg: D2 CfgNode, config file 66 Return: 67 dictionary storing arguments for __init__ method 68 """ 69 assert "VIDEO_HEIGHT" in cfg.TRACKER_HEADS 70 assert "VIDEO_WIDTH" in cfg.TRACKER_HEADS 71 video_height = cfg.TRACKER_HEADS.get("VIDEO_HEIGHT") 72 video_width = cfg.TRACKER_HEADS.get("VIDEO_WIDTH")
66 Return: 67 dictionary storing arguments for __init__ method 68 """ 69 assert "VIDEO_HEIGHT" in cfg.TRACKER_HEADS 70 assert "VIDEO_WIDTH" in cfg.TRACKER_HEADS 71 video_height = cfg.TRACKER_HEADS.get("VIDEO_HEIGHT") 72 video_width = cfg.TRACKER_HEADS.get("VIDEO_WIDTH") 73 max_num_instances = cfg.TRACKER_HEADS.get("MAX_NUM_INSTANCES", 200)
96 97 def _process_matched_idx( 98 self, instances: Instances, matched_idx: np.ndarray, matched_prev_idx: np.ndarray 99 ) -> Instances: 100 assert matched_idx.size == matched_prev_idx.size 101 for i in range(matched_idx.size): 102 instances.ID[matched_idx[i]] = self._prev_instances.ID[matched_prev_idx[i]] 103 instances.ID_period[matched_idx[i]] = (
65 cfg: D2 CfgNode, config file 66 Return: 67 dictionary storing arguments for __init__ method 68 """ 69 assert "VIDEO_HEIGHT" in cfg.TRACKER_HEADS 70 assert "VIDEO_WIDTH" in cfg.TRACKER_HEADS 71 video_height = cfg.TRACKER_HEADS.get("VIDEO_HEIGHT") 72 video_width = cfg.TRACKER_HEADS.get("VIDEO_WIDTH")
66 Return: 67 dictionary storing arguments for __init__ method 68 """ 69 assert "VIDEO_HEIGHT" in cfg.TRACKER_HEADS 70 assert "VIDEO_WIDTH" in cfg.TRACKER_HEADS 71 video_height = cfg.TRACKER_HEADS.get("VIDEO_HEIGHT") 72 video_width = cfg.TRACKER_HEADS.get("VIDEO_WIDTH") 73 max_num_instances = cfg.TRACKER_HEADS.get("MAX_NUM_INSTANCES", 200)
67 cfg: D2 CfgNode, config file 68 Return: 69 dictionary storing arguments for __init__ method 70 """ 71 assert "VIDEO_HEIGHT" in cfg.TRACKER_HEADS 72 assert "VIDEO_WIDTH" in cfg.TRACKER_HEADS 73 video_height = cfg.TRACKER_HEADS.get("VIDEO_HEIGHT") 74 video_width = cfg.TRACKER_HEADS.get("VIDEO_WIDTH")
68 Return: 69 dictionary storing arguments for __init__ method 70 """ 71 assert "VIDEO_HEIGHT" in cfg.TRACKER_HEADS 72 assert "VIDEO_WIDTH" in cfg.TRACKER_HEADS 73 video_height = cfg.TRACKER_HEADS.get("VIDEO_HEIGHT") 74 video_width = cfg.TRACKER_HEADS.get("VIDEO_WIDTH") 75 max_num_instances = cfg.TRACKER_HEADS.get("MAX_NUM_INSTANCES", 200)
99 100 Return: 101 the cost matrix in numpy array 102 """ 103 assert instances is not None and prev_instances is not None 104 # calculate IoU of all bbox pairs 105 iou_all = pairwise_iou( 106 boxes1=instances.pred_boxes,
132 supported_ops = {k: lambda *args, **kwargs: {} for k in _IGNORED_OPS} 133 supported_ops.update(kwargs.pop("supported_ops", {})) 134 kwargs["supported_ops"] = supported_ops 135 136 assert len(inputs) == 1, "Please use batch size=1" 137 tensor_input = inputs[0]["image"] 138 inputs = [{"image": tensor_input}] # remove other keys, in case there are any 139
166 167 Returns: 168 list[str]: the name of unused parameters 169 """ 170 assert model.training 171 for _, prm in model.named_parameters(): 172 prm.grad = None 173
2 import importlib 3 import numpy as np 4 import os 5 import re 6 import subprocess 7 import sys 8 from collections import defaultdict 9 import PIL
35 try: 36 cuobjdump = os.path.join(CUDA_HOME, "bin", "cuobjdump") 37 if os.path.isfile(cuobjdump): 38 output = subprocess.check_output( 39 "'{}' --list-elf '{}'".format(cuobjdump, so_file), shell=True 40 ) 41 output = output.decode("utf-8").strip().split("\n") 42 arch = [] 43 for line in output: 44 line = re.findall(r"\.sm_([0-9]*)\.", line)[0]
89 if sys.platform != "win32": # don't know what to do for windows 90 try: 91 # this is how torch/utils/cpp_extensions.py choose compiler 92 cxx = os.environ.get("CXX", "c++") 93 cxx = subprocess.check_output("'{}' --version".format(cxx), shell=True) 94 cxx = cxx.decode("utf-8").strip().split("\n")[0] 95 except subprocess.SubprocessError: 96 cxx = "Not found"
98 99 if has_cuda and CUDA_HOME is not None: 100 try: 101 nvcc = os.path.join(CUDA_HOME, "bin", "nvcc") 102 nvcc = subprocess.check_output("'{}' -V".format(nvcc), shell=True) 103 nvcc = nvcc.decode("utf-8").strip().split("\n")[-1] 104 except subprocess.SubprocessError: 105 nvcc = "Not found"
126 data.append(("PyTorch", torch_version + " @" + os.path.dirname(torch.__file__))) 127 data.append(("PyTorch debug build", torch.version.debug)) 128 try: 129 data.append(("torch._C._GLIBCXX_USE_CXX11_ABI", torch._C._GLIBCXX_USE_CXX11_ABI)) 130 except Exception: 131 pass 132 133 if not has_gpu: 134 has_gpu_text = "No: torch.cuda.is_available() == False"
151 try: 152 from torch.utils.collect_env import get_nvidia_driver_version, run as _run 153 154 data.append(("Driver version", get_nvidia_driver_version(_run))) 155 except Exception: 156 pass 157 msg = " - invalid!" if not (CUDA_HOME and os.path.isdir(CUDA_HOME)) else "" 158 data.append(("CUDA_HOME", str(CUDA_HOME) + msg)) 159
205 206 207 def test_nccl_ops(): 208 num_gpu = torch.cuda.device_count() 209 if os.access("/tmp", os.W_OK): 210 import torch.multiprocessing as mp 211 212 dist_url = "file:///tmp/nccl_tmp_file"
101 102 Returns: 103 ndarray: a float32 array of Nx3 colors, in range [0, 255] or [0, 1] 104 """ 105 assert maximum in [255, 1], maximum 106 c = _COLORS * maximum 107 if not rgb: 108 c = c[:, ::-1]
49 num_workers_per_machine: the number of worker processes per machine. Typically 50 the number of GPUs. 51 """ 52 global _LOCAL_PROCESS_GROUP 53 assert _LOCAL_PROCESS_GROUP is None 54 assert get_world_size() % num_workers_per_machine == 0 55 num_machines = get_world_size() // num_workers_per_machine 56 machine_rank = get_rank() // num_workers_per_machine
50 the number of GPUs. 51 """ 52 global _LOCAL_PROCESS_GROUP 53 assert _LOCAL_PROCESS_GROUP is None 54 assert get_world_size() % num_workers_per_machine == 0 55 num_machines = get_world_size() // num_workers_per_machine 56 machine_rank = get_rank() // num_workers_per_machine 57 for i in range(num_machines):
67 A torch process group which only includes processes that are on the same 68 machine as the current process. This group can be useful for communication 69 within a machine, e.g. a per-machine SyncBN. 70 """ 71 assert _LOCAL_PROCESS_GROUP is not None, _MISSING_LOCAL_PG_ERROR 72 return _LOCAL_PROCESS_GROUP 73 74
80 if not dist.is_available(): 81 return 0 82 if not dist.is_initialized(): 83 return 0 84 assert _LOCAL_PROCESS_GROUP is not None, _MISSING_LOCAL_PG_ERROR 85 return dist.get_rank(group=_LOCAL_PROCESS_GROUP) 86 87
94 if not dist.is_available(): 95 return 1 96 if not dist.is_initialized(): 97 return 1 98 assert _LOCAL_PROCESS_GROUP is not None, _MISSING_LOCAL_PG_ERROR 99 return dist.get_world_size(group=_LOCAL_PROCESS_GROUP) 100 101
82 def get_version(module, digit=2): 83 return tuple(map(int, module.__version__.split(".")[:digit])) 84 85 # fmt: off 86 assert get_version(torch) >= (1, 4), "Requires torch>=1.4" 87 import fvcore 88 assert get_version(fvcore, 3) >= (0, 1, 2), "Requires fvcore>=0.1.2" 89 import yaml
84 85 # fmt: off 86 assert get_version(torch) >= (1, 4), "Requires torch>=1.4" 87 import fvcore 88 assert get_version(fvcore, 3) >= (0, 1, 2), "Requires fvcore>=0.1.2" 89 import yaml 90 assert get_version(yaml) >= (5, 1), "Requires pyyaml>=5.1" 91 # fmt: on
86 assert get_version(torch) >= (1, 4), "Requires torch>=1.4" 87 import fvcore 88 assert get_version(fvcore, 3) >= (0, 1, 2), "Requires fvcore>=0.1.2" 89 import yaml 90 assert get_version(yaml) >= (5, 1), "Requires pyyaml>=5.1" 91 # fmt: on 92 93
124 if custom_module.endswith(".py"): 125 module = _import_file("detectron2.utils.env.custom_module", custom_module) 126 else: 127 module = importlib.import_module(custom_module) 128 assert hasattr(module, "setup_environment") and callable(module.setup_environment), ( 129 "Custom environment module defined in {} does not have the " 130 "required callable attribute 'setup_environment'." 131 ).format(custom_module) 132 module.setup_environment() 133 134
28 Returns: 29 The :class:`EventStorage` object that's currently being used. 30 Throws an error if no :class:`EventStorage` is currently enabled. 31 """ 32 assert len( 33 _CURRENT_STORAGE_STACK 34 ), "get_event_storage() has to be called inside a 'with EventStorage(...)' context!" 35 return _CURRENT_STORAGE_STACK[-1] 36 37
355 self._latest_scalars[name] = (value, self._iter) 356 357 existing_hint = self._smoothing_hints.get(name) 358 if existing_hint is not None: 359 assert ( 360 existing_hint == smoothing_hint 361 ), "Scalar {} was put with a different smoothing_hint!".format(name) 362 else: 363 self._smoothing_hints[name] = smoothing_hint 364
503 _CURRENT_STORAGE_STACK.append(self) 504 return self 505 506 def __exit__(self, exc_type, exc_val, exc_tb): 507 assert _CURRENT_STORAGE_STACK[-1] == self 508 _CURRENT_STORAGE_STACK.pop() 509 510 @contextmanager
157 will not log only if the same caller has logged the same message before. 158 """ 159 if isinstance(key, str): 160 key = (key,) 161 assert len(key) > 0 162 163 caller_module, caller_key = _find_caller() 164 hash_key = ()
80 def convert_scripted_instances(instances): 81 """ 82 Convert a scripted Instances object to a regular :class:`Instances` object 83 """ 84 assert hasattr( 85 instances, "image_size" 86 ), f"Expect an Instances object, but got {type(instances)}!" 87 ret = Instances(instances.image_size) 88 for name in instances._field_names: 89 val = getattr(instances, "_" + name, None)
110 msg = msg.rstrip() + " " 111 112 size_error_msg = msg + f"image_size is {input.image_size} vs. {other.image_size}!" 113 if size_as_tensor: 114 assert torch.equal( 115 torch.tensor(input.image_size), torch.tensor(other.image_size) 116 ), size_error_msg 117 else: 118 assert input.image_size == other.image_size, size_error_msg 119 fields = sorted(input.get_fields().keys())
114 assert torch.equal( 115 torch.tensor(input.image_size), torch.tensor(other.image_size) 116 ), size_error_msg 117 else: 118 assert input.image_size == other.image_size, size_error_msg 119 fields = sorted(input.get_fields().keys()) 120 fields_other = sorted(other.get_fields().keys()) 121 assert fields == fields_other, msg + f"Fields are {fields} vs {fields_other}!"
117 else: 118 assert input.image_size == other.image_size, size_error_msg 119 fields = sorted(input.get_fields().keys()) 120 fields_other = sorted(other.get_fields().keys()) 121 assert fields == fields_other, msg + f"Fields are {fields} vs {fields_other}!" 122 123 for f in fields: 124 val1, val2 = input.get(f), other.get(f)
123 for f in fields: 124 val1, val2 = input.get(f), other.get(f) 125 if isinstance(val1, (Boxes, ROIMasks)): 126 # boxes in the range of O(100) and can have a larger tolerance 127 assert torch.allclose(val1.tensor, val2.tensor, atol=100 * rtol), ( 128 msg + f"Field {f} differs too much!" 129 ) 130 elif isinstance(val1, torch.Tensor): 131 if val1.dtype.is_floating_point: 132 mag = torch.abs(val1).max().cpu().item()
129 ) 130 elif isinstance(val1, torch.Tensor): 131 if val1.dtype.is_floating_point: 132 mag = torch.abs(val1).max().cpu().item() 133 assert torch.allclose(val1, val2, atol=mag * rtol), ( 134 msg + f"Field {f} differs too much!" 135 ) 136 else: 137 assert torch.equal(val1, val2), msg + f"Field {f} is different!" 138 else:
133 assert torch.allclose(val1, val2, atol=mag * rtol), ( 134 msg + f"Field {f} differs too much!" 135 ) 136 else: 137 assert torch.equal(val1, val2), msg + f"Field {f} is different!" 138 else: 139 raise ValueError(f"Don't know how to compare type {type(val1)}") 140
443 if repeats_sizes[0] is None: 444 return sym_help._onnx_opset_unsupported_detailed( 445 "repeat_interleave", 9, 13, "Unsupported for cases with dynamic repeats" 446 ) 447 assert ( 448 repeats_sizes[0] == input_sizes[dim] 449 ), "repeats must have the same size as input along dim" 450 reps = repeats_sizes[0] 451 else: 452 raise RuntimeError("repeats must be 0-dim or 1-dim tensor")
56 try: 57 if isinstance(condition, str): 58 caller_frame = inspect.currentframe().f_back 59 torch._assert( 60 eval(condition, caller_frame.f_globals, caller_frame.f_locals), message 61 ) 62 return torch.ones(1) 63 else:
45 metadata (MetadataCatalog): image metadata. 46 """ 47 self.metadata = metadata 48 self._old_instances = [] 49 assert instance_mode in [ 50 ColorMode.IMAGE, 51 ColorMode.IMAGE_BW, 52 ], "Other mode not supported yet." 53 self._instance_mode = instance_mode 54 self._max_num_instances = self.metadata.get("max_num_instances", 74) 55 self._assigned_colors = {}
187 num_instances = len(masks) 188 masks_rles = mask_util.encode( 189 np.asarray(np.asarray(masks).transpose(1, 2, 0), dtype=np.uint8, order="F") 190 ) 191 assert len(masks_rles) == num_instances 192 193 category_ids = [x["category_id"] for x in sinfo] 194 detected = [
219 220 # Compute iou with either boxes or masks: 221 is_crowd = np.zeros((len(instances),), dtype=bool) 222 if instances[0].bbox is None: 223 assert instances[0].mask_rle is not None 224 # use mask iou only when box iou is None 225 # because box seems good enough 226 rles_old = [x.mask_rle for x in self._old_instances]
272 if id in self._assigned_colors: 273 colors.append(self._color_pool[self._assigned_colors[id]]) 274 untracked_ids.remove(id) 275 else: 276 assert ( 277 len(self._color_idx_set) >= 1 278 ), f"Number of id exceeded maximum, \ 279 max = {self._max_num_instances}" 280 idx = self._color_idx_set.pop() 281 color = self._color_pool[idx] 282 self._assigned_colors[id] = idx
71 72 m = mask_or_polygons 73 if isinstance(m, dict): 74 # RLEs 75 assert "counts" in m and "size" in m 76 if isinstance(m["counts"], list): # uncompressed RLEs 77 h, w = m["size"] 78 assert h == height and w == width
74 # RLEs 75 assert "counts" in m and "size" in m 76 if isinstance(m["counts"], list): # uncompressed RLEs 77 h, w = m["size"] 78 assert h == height and w == width 79 m = mask_util.frPyObjects(m, h, w) 80 self._mask = mask_util.decode(m)[:, :] 81 return
84 self._polygons = [np.asarray(x).reshape(-1) for x in m] 85 return 86 87 if isinstance(m, np.ndarray): # assumed to be a binary mask 88 assert m.shape[1] != 2, m.shape 89 assert m.shape == ( 90 height, 91 width,
85 return 86 87 if isinstance(m, np.ndarray): # assumed to be a binary mask 88 assert m.shape[1] != 2, m.shape 89 assert m.shape == ( 90 height, 91 width, 92 ), f"mask shape: {m.shape}, target dims: {height}, {width}" 93 self._mask = m.astype("uint8") 94 return 95
158 """ 159 160 def __init__(self, panoptic_seg, segments_info, metadata=None): 161 if segments_info is None: 162 assert metadata is not None 163 # If "segments_info" is None, we assume "panoptic_img" is a 164 # H*W int32 image storing the panoptic_id in the format of 165 # category_id * label_divisor + instance_id. We reserve -1 for
203 if id not in self._sinfo: 204 empty_ids.append(id) 205 if len(empty_ids) == 0: 206 return np.zeros(self._seg.shape, dtype=np.uint8) 207 assert ( 208 len(empty_ids) == 1 209 ), ">1 ids corresponds to no labels. This is currently not supported" 210 return (self._seg != empty_ids[0]).numpy().astype(bool) 211 212 def semantic_masks(self):
648 num_instances = len(boxes) 649 if masks is not None: 650 masks = self._convert_masks(masks) 651 if num_instances: 652 assert len(masks) == num_instances 653 else: 654 num_instances = len(masks) 655 if keypoints is not None:
653 else: 654 num_instances = len(masks) 655 if keypoints is not None: 656 if num_instances: 657 assert len(keypoints) == num_instances 658 else: 659 num_instances = len(keypoints) 660 keypoints = self._convert_keypoints(keypoints)
658 else: 659 num_instances = len(keypoints) 660 keypoints = self._convert_keypoints(keypoints) 661 if labels is not None: 662 assert len(labels) == num_instances 663 if assigned_colors is None: 664 assigned_colors = [random_color(rgb=True, maximum=1) for _ in range(num_instances)] 665 if num_instances == 0:
1192 Returns: 1193 modified_color (tuple[double]): a tuple containing the RGB values of the 1194 modified color. Each value in the tuple is in the [0.0, 1.0] range. 1195 """ 1196 assert brightness_factor >= -1.0 and brightness_factor <= 1.0 1197 color = mplc.to_rgb(color) 1198 polygon_color = colorsys.rgb_to_hls(*mplc.to_rgb(color)) 1199 modified_lightness = polygon_color[1] + (brightness_factor * polygon_color[1])
1 import datetime 2 import os 3 import pickle 4 import subprocess 5 import sys 6 7 8 def print_args(args):
1 import datetime 2 import os 3 import pickle 4 import subprocess 5 import sys 6 7 8 def print_args(args):
31 opt_file.write('launched {} at {}\n'.format( 32 str(sys.argv[0]), str(datetime.datetime.now()))) 33 34 # Add git info 35 label = subprocess.check_output(["git", "describe", 36 "--always"]).strip() 37 if subprocess.call( 38 ["git", "branch"], 39 stderr=subprocess.STDOUT,
31 opt_file.write('launched {} at {}\n'.format( 32 str(sys.argv[0]), str(datetime.datetime.now()))) 33 34 # Add git info 35 label = subprocess.check_output(["git", "describe", 36 "--always"]).strip() 37 if subprocess.call( 38 ["git", "branch"], 39 stderr=subprocess.STDOUT,
33 34 # Add git info 35 label = subprocess.check_output(["git", "describe", 36 "--always"]).strip() 37 if subprocess.call( 38 ["git", "branch"], 39 stderr=subprocess.STDOUT, 40 stdout=open(os.devnull, 'w')) == 0: 41 opt_file.write('=== Git info ====\n') 42 opt_file.write('{}\n'.format(label)) 43 commit = subprocess.check_output(['git', 'rev-parse', 'HEAD'])
33 34 # Add git info 35 label = subprocess.check_output(["git", "describe", 36 "--always"]).strip() 37 if subprocess.call( 38 ["git", "branch"], 39 stderr=subprocess.STDOUT, 40 stdout=open(os.devnull, 'w')) == 0: 41 opt_file.write('=== Git info ====\n') 42 opt_file.write('{}\n'.format(label)) 43 commit = subprocess.check_output(['git', 'rev-parse', 'HEAD'])
39 stderr=subprocess.STDOUT, 40 stdout=open(os.devnull, 'w')) == 0: 41 opt_file.write('=== Git info ====\n') 42 opt_file.write('{}\n'.format(label)) 43 commit = subprocess.check_output(['git', 'rev-parse', 'HEAD']) 44 opt_file.write('commit : {}\n'.format(commit.strip())) 45 46 opt_picklename = '{}.pkl'.format(opt_prefix)
39 stderr=subprocess.STDOUT, 40 stdout=open(os.devnull, 'w')) == 0: 41 opt_file.write('=== Git info ====\n') 42 opt_file.write('{}\n'.format(label)) 43 commit = subprocess.check_output(['git', 'rev-parse', 'HEAD']) 44 opt_file.write('commit : {}\n'.format(commit.strip())) 45 46 opt_picklename = '{}.pkl'.format(opt_prefix)
154 root_rot = rot6d.robust_compute_rotation_matrix_from_ortho6d(th_full_pose[:, :6]) 155 else: 156 root_rot = rot6d.compute_rotation_matrix_from_ortho6d(th_full_pose[:, :6]) 157 else: 158 assert th_pose_coeffs.dim() == 4, ( 159 'When not self.use_pca, ' 160 'th_pose_coeffs should have 4 dims, got {}'.format( 161 th_pose_coeffs.dim())) 162 assert th_pose_coeffs.shape[2:4] == (3, 3), ( 163 'When not self.use_pca, th_pose_coeffs have 3x3 matrix for two' 164 'last dims, got {}'.format(th_pose_coeffs.shape[2:4]))
158 assert th_pose_coeffs.dim() == 4, ( 159 'When not self.use_pca, ' 160 'th_pose_coeffs should have 4 dims, got {}'.format( 161 th_pose_coeffs.dim())) 162 assert th_pose_coeffs.shape[2:4] == (3, 3), ( 163 'When not self.use_pca, th_pose_coeffs have 3x3 matrix for two' 164 'last dims, got {}'.format(th_pose_coeffs.shape[2:4])) 165 th_pose_rots = rotproj.batch_rotprojs(th_pose_coeffs) 166 th_rot_map = th_pose_rots[:, 1:].view(batch_size, -1) 167 th_pose_map = subtract_flat_id(th_rot_map)
46 y = y.view(-1, 3, 1) 47 z = z.view(-1, 3, 1) 48 matrix = torch.cat((x, y, z), 2) # batch*3*3 49 # Check for reflection in matrix ! If found, flip last vector TODO 50 assert (torch.stack([torch.det(mat) for mat in matrix ])< 0).sum() == 0 51 return matrix 52 53
28 return result 29 30 def ready_arguments(fname_or_dict, posekey4vposed='pose'): 31 import numpy as np 32 import pickle 33 from custom_manopth.posemapper import posemap 34 35 if not isinstance(fname_or_dict, dict):
32 import pickle 33 from custom_manopth.posemapper import posemap 34 35 if not isinstance(fname_or_dict, dict): 36 dd = pickle.load(open(fname_or_dict, 'rb'), encoding='latin1') 37 # dd = pickle.load(open(fname_or_dict, 'rb')) 38 else: 39 dd = fname_or_dict
54 ]: 55 if (s in dd) and not hasattr(dd[s], 'dterms'): 56 dd[s] = np.array(dd[s]) 57 58 assert (posekey4vposed in dd) 59 if want_shapemodel: 60 dd['v_shaped'] = dd['shapedirs'].dot(dd['betas']) + dd['v_template'] 61 v_shaped = dd['v_shaped']
78 and replaces the pose DOFS by ncomps from PCA''' 79 80 from custom_manopth.verts import verts_core 81 import numpy as np 82 import pickle 83 import scipy.sparse as sp 84 np.random.seed(1) 85
83 import scipy.sparse as sp 84 np.random.seed(1) 85 86 if not isinstance(fname_or_dict, dict): 87 smpl_data = pickle.load(open(fname_or_dict, 'rb'), encoding='latin1') 88 # smpl_data = pickle.load(open(fname_or_dict, 'rb')) 89 else: 90 smpl_data = fname_or_dict
46 for which in [ 47 trans, pose, v_template, weights, posedirs, betas, shapedirs 48 ]: 49 if which is not None: 50 assert ischumpy(which) 51 52 v = v_template 53
70 J_tmpy = np.matmul(J_regressor, v_shaped[:, 1]) 71 J_tmpz = np.matmul(J_regressor, v_shaped[:, 2]) 72 J = np.vstack((J_tmpx, J_tmpy, J_tmpz)).T 73 else: 74 assert (ischumpy(J)) 75 76 assert (bs_style == 'lbs') 77 result, Jtr = lbs.verts_core(
72 J = np.vstack((J_tmpx, J_tmpy, J_tmpz)).T 73 else: 74 assert (ischumpy(J)) 75 76 assert (bs_style == 'lbs') 77 result, Jtr = lbs.verts_core( 78 pose, v, J, weights, kintree_table, want_Jtr=True, xp=np) 79
111 bs_style, 112 want_Jtr=False, 113 xp=np): 114 115 assert (bs_style == 'lbs') 116 result = lbs.verts_core(pose, v, J, weights, kintree_table, want_Jtr, xp) 117 return result
18 print(yaml_file) 19 if not op.isfile(yaml_file): 20 yaml_file = op.join(args.data_dir, yaml_file) 21 # code.interact(local=locals()) 22 assert op.isfile(yaml_file) 23 return MeshTSVYamlDataset(yaml_file, is_train, False, scale_factor) 24 25
110 print(yaml_file) 111 if not op.isfile(yaml_file): 112 yaml_file = op.join(args.data_dir, yaml_file) 113 # code.interact(local=locals()) 114 assert op.isfile(yaml_file) 115 return HandMeshTSVYamlDataset(args, yaml_file, is_train, False, scale_factor) 116 117
33 self.label_tsv = None if label_file is None else self.get_tsv_file(label_file) 34 self.hw_tsv = None if hw_file is None else self.get_tsv_file(hw_file) 35 36 if self.is_composite: 37 assert op.isfile(self.linelist_file) 38 self.line_list = [i for i in range(self.hw_tsv.num_rows())] 39 else: 40 self.line_list = load_linelist_file(linelist_file)
31 self.label_tsv = None if label_file is None else self.get_tsv_file(label_file) 32 self.hw_tsv = None if hw_file is None else self.get_tsv_file(hw_file) 33 34 if self.is_composite: 35 assert op.isfile(self.linelist_file) 36 self.line_list = [i for i in range(self.hw_tsv.num_rows())] 37 else: 38 self.line_list = load_linelist_file(linelist_file)
9 import torch.nn as nn 10 import numpy as np 11 import scipy.sparse 12 try: 13 import cPickle as pickle 14 except ImportError: 15 import pickle 16
11 import scipy.sparse 12 try: 13 import cPickle as pickle 14 except ImportError: 15 import pickle 16 17 from custom_mesh_graphormer.utils.geometric_layers import rodrigues 18 import custom_mesh_graphormer.modeling.data.config as cfg
56 torch.save(model_to_save.state_dict(), op.join(checkpoint_dir, 'state_dict.bin')) 57 torch.save(args, op.join(checkpoint_dir, 'training_args.bin')) 58 logger.info("Save checkpoint to {}".format(checkpoint_dir)) 59 break 60 except: 61 pass 62 else: 63 logger.info("Failed to save checkpoint after {} trails.".format(num_trial)) 64 return checkpoint_dir
677 logger.info("Update config parameter {}: {} -> {}".format(param, config_param, arg_param)) 678 setattr(config, param, arg_param) 679 680 # init a transformer encoder and append it to a list 681 assert config.hidden_size % config.num_attention_heads == 0 682 model = model_class(config=config) 683 logger.info("Init model from scratch.") 684 trans_encoder.append(model)
269 logger.info("Update config parameter {}: {} -> {}".format(param, config_param, arg_param)) 270 setattr(config, param, arg_param) 271 272 # init a transformer encoder and append it to a list 273 assert config.hidden_size % config.num_attention_heads == 0 274 model = model_class(config=config) 275 logger.info("Init model from scratch.") 276 trans_encoder.append(model)
56 torch.save(model_to_save.state_dict(), op.join(checkpoint_dir, 'state_dict.bin')) 57 torch.save(args, op.join(checkpoint_dir, 'training_args.bin')) 58 logger.info("Save checkpoint to {}".format(checkpoint_dir)) 59 break 60 except: 61 pass 62 else: 63 logger.info("Failed to save checkpoint after {} trails.".format(num_trial)) 64 return checkpoint_dir
339 output_zip_file = args.output_dir + 'ckpt' + azure_ckpt_name + '-' + inference_setting +'-pred.zip' 340 341 resolved_submit_cmd = 'zip ' + output_zip_file + ' ' + output_json_file 342 print(resolved_submit_cmd) 343 os.system(resolved_submit_cmd) 344 resolved_submit_cmd = 'rm %s'%(output_json_file) 345 print(resolved_submit_cmd) 346 os.system(resolved_submit_cmd)
342 print(resolved_submit_cmd) 343 os.system(resolved_submit_cmd) 344 resolved_submit_cmd = 'rm %s'%(output_json_file) 345 print(resolved_submit_cmd) 346 os.system(resolved_submit_cmd) 347 if world_size > 1: 348 torch.distributed.barrier() 349
405 run_ckpt_name = args.resume_checkpoint.split('/')[-2].split('-')[1] 406 inference_setting = 'sc%02d_rot%s'%(int(args.sc*10),str(int(args.rot))) 407 resolved_submit_cmd = 'zip ' + args.output_dir + run_exp_name + '-ckpt'+ run_ckpt_name + '-' + inference_setting +'-pred.zip ' + 'pred.json' 408 print(resolved_submit_cmd) 409 os.system(resolved_submit_cmd) 410 resolved_submit_cmd = 'rm pred.json' 411 print(resolved_submit_cmd) 412 os.system(resolved_submit_cmd)
408 print(resolved_submit_cmd) 409 os.system(resolved_submit_cmd) 410 resolved_submit_cmd = 'rm pred.json' 411 print(resolved_submit_cmd) 412 os.system(resolved_submit_cmd) 413 return 414 415 def visualize_mesh( renderer,
651 logger.info("Update config parameter {}: {} -> {}".format(param, config_param, arg_param)) 652 setattr(config, param, arg_param) 653 654 # init a transformer encoder and append it to a list 655 assert config.hidden_size % config.num_attention_heads == 0 656 model = model_class(config=config) 657 logger.info("Init model from scratch.") 658 trans_encoder.append(model)
261 logger.info("Update config parameter {}: {} -> {}".format(param, config_param, arg_param)) 262 setattr(config, param, arg_param) 263 264 # init a transformer encoder and append it to a list 265 assert config.hidden_size % config.num_attention_heads == 0 266 model = model_class(config=config) 267 logger.info("Init model from scratch.") 268 trans_encoder.append(model)
78 79 filepath = output_dir+'ckpt200-multisc-pred.zip' 80 resolved_submit_cmd = 'zip ' + filepath + ' ' + 'pred.json' 81 print(resolved_submit_cmd) 82 os.system(resolved_submit_cmd) 83 resolved_submit_cmd = 'rm pred.json' 84 print(resolved_submit_cmd) 85 os.system(resolved_submit_cmd)
81 print(resolved_submit_cmd) 82 os.system(resolved_submit_cmd) 83 resolved_submit_cmd = 'rm pred.json' 84 print(resolved_submit_cmd) 85 os.system(resolved_submit_cmd) 86 87 88 def run_multiscale_inference(model_path, mode, output_dir):
114 for s in scale: 115 for r in rotations: 116 resolved_submit_cmd = job_cmd%(model_path, r, s, output_dir) 117 print(resolved_submit_cmd) 118 os.system(resolved_submit_cmd) 119 120 def main(args): 121 model_path = args.model_path
5 This file contains primitives for multi-gpu communication. 6 This is useful when doing distributed training. 7 """ 8 9 import pickle 10 import time 11 12 import torch
97 data_list = [] 98 for tensor in tensor_list: 99 buffer = tensor.cpu().numpy().tobytes() 100 del tensor 101 data_list.append(pickle.loads(buffer)) 102 del buffer 103 104 return data_list
141 142 data_list = [] 143 for size, tensor in zip(size_list, tensor_list): 144 buffer = tensor.cpu().numpy().tobytes()[:size] 145 data_list.append(pickle.loads(buffer)) 146 147 return data_list 148
62 63 64 def load_from_yaml_file(file_name): 65 with open(file_name, 'r') as fp: 66 return yaml.load(fp, Loader=yaml.CLoader)
27 raise ValueError('`auto_bound` conflicts with `center`') 28 h, w = img.shape[:2] 29 if center is None: 30 center = ((w - 1) * 0.5, (h - 1) * 0.5) 31 assert isinstance(center, tuple) 32 33 matrix = cv2.getRotationMatrix2D(center, angle, scale) 34 if auto_bound:
19 if S1.shape[0] != 3 and S1.shape[0] != 2: 20 S1 = S1.T 21 S2 = S2.T 22 transposed = True 23 assert(S2.shape[1] == S1.shape[1]) 24 25 # 1. Remove mean. 26 mu1 = S1.mean(axis=1, keepdims=True)
155 156 157 def print_and_run_cmd(cmd): 158 print(cmd) 159 os.system(cmd) 160 161 162 def write_to_yaml_file(context, file_name):
165 166 167 def load_from_yaml_file(yaml_file): 168 with open(yaml_file, 'r') as fp: 169 return yaml.load(fp, Loader=yaml.CLoader) 170 171
26 def read_to_character(fp, c): 27 result = [] 28 while True: 29 s = fp.read(32) 30 assert s != '' 31 if c in s: 32 result.append(s[: s.index(c)]) 33 break
111 def __init__(self, file_list, seq_file, root='.'): 112 if isinstance(file_list, str): 113 self.file_list = load_list_file(file_list) 114 else: 115 assert isinstance(file_list, list) 116 self.file_list = file_list 117 118 self.seq_file = seq_file
41 idx = 0 42 tsv_file_tmp = tsv_file + '.tmp' 43 lineidx_file_tmp = lineidx_file + '.tmp' 44 with open(tsv_file_tmp, 'w') as fp, open(lineidx_file_tmp, 'w') as fpidx: 45 assert values is not None 46 for value in values: 47 assert value is not None 48 value = [v if type(v)!=bytes else v.decode('utf-8') for v in value]
43 lineidx_file_tmp = lineidx_file + '.tmp' 44 with open(tsv_file_tmp, 'w') as fp, open(lineidx_file_tmp, 'w') as fpidx: 45 assert values is not None 46 for value in values: 47 assert value is not None 48 value = [v if type(v)!=bytes else v.decode('utf-8') for v in value] 49 v = '{0}\n'.format(sep.join(map(str, value))) 50 fp.write(v)
101 tsv_writer(line_list, save_file) 102 103 def load_from_yaml_file(yaml_file): 104 with open(yaml_file, 'r') as fp: 105 return yaml.load(fp, Loader=yaml.CLoader) 106 107 def find_file_path_in_yaml(fname, root): 108 if fname is not None:
60 return hook 61 62 63 def forward_default(pretrained, x, function_name="forward_features"): 64 exec(f"pretrained.model.{function_name}(x)") 65 66 layer_1 = pretrained.activations["1"] 67 layer_2 = pretrained.activations["2"]
82 83 def forward_adapted_unflatten(pretrained, x, function_name="forward_features"): 84 b, c, h, w = x.shape 85 86 exec(f"glob = pretrained.model.{function_name}(x)") 87 88 layer_1 = pretrained.activations["1"] 89 layer_2 = pretrained.activations["2"]
133 readout_oper = [ 134 ProjectReadout(vit_features, start_index) for out_feat in features 135 ] 136 else: 137 assert ( 138 False 139 ), "wrong operation for readout token, use_readout can be 'ignore', 'add', or 'project'" 140 141 return readout_oper 142
146 readout_oper = get_readout_oper(vit_features, features, use_readout, start_index) 147 148 for s in range(used_number_stages): 149 value = nn.Sequential(nn.Identity(), nn.Identity(), nn.Identity()) 150 exec(f"pretrained.act_postprocess{s + 1}=value") 151 for s in range(used_number_stages, 4): 152 if s < number_stages: 153 final_layer = nn.ConvTranspose2d(
186 if final_layer is not None: 187 layers.append(final_layer) 188 189 value = nn.Sequential(*layers) 190 exec(f"pretrained.act_postprocess{s + 1}=value") 191 192 pretrained.model.start_index = start_index 193 pretrained.model.patch_size = patch_size
124 pretrained = _make_pretrained_efficientnet_lite3(use_pretrained, exportable=exportable) 125 scratch = _make_scratch([32, 48, 136, 384], features, groups=groups, expand=expand) # efficientnet_lite3 126 else: 127 print(f"Backbone '{backbone}' not implemented") 128 assert False 129 130 return pretrained, scratch 131
195 ) 196 197 else: 198 print(f"model_type '{model_type}' not implemented, use: --model_type large") 199 assert False 200 201 if not "openvino" in model_type: 202 print("Model loaded, number of parameters = {:.0f}M".format(sum(p.numel() for p in model.parameters()) / 1e6))
38 ratio, 39 pooling_type='att', 40 fusion_types=('channel_add', )): 41 super(ContextBlock, self).__init__() 42 assert pooling_type in ['avg', 'att'] 43 assert isinstance(fusion_types, (list, tuple)) 44 valid_fusion_types = ['channel_add', 'channel_mul'] 45 assert all([f in valid_fusion_types for f in fusion_types])
39 pooling_type='att', 40 fusion_types=('channel_add', )): 41 super(ContextBlock, self).__init__() 42 assert pooling_type in ['avg', 'att'] 43 assert isinstance(fusion_types, (list, tuple)) 44 valid_fusion_types = ['channel_add', 'channel_mul'] 45 assert all([f in valid_fusion_types for f in fusion_types]) 46 assert len(fusion_types) > 0, 'at least one fusion should be used'
41 super(ContextBlock, self).__init__() 42 assert pooling_type in ['avg', 'att'] 43 assert isinstance(fusion_types, (list, tuple)) 44 valid_fusion_types = ['channel_add', 'channel_mul'] 45 assert all([f in valid_fusion_types for f in fusion_types]) 46 assert len(fusion_types) > 0, 'at least one fusion should be used' 47 self.in_channels = in_channels 48 self.ratio = ratio
42 assert pooling_type in ['avg', 'att'] 43 assert isinstance(fusion_types, (list, tuple)) 44 valid_fusion_types = ['channel_add', 'channel_mul'] 45 assert all([f in valid_fusion_types for f in fusion_types]) 46 assert len(fusion_types) > 0, 'at least one fusion should be used' 47 self.in_channels = in_channels 48 self.ratio = ratio 49 self.planes = int(in_channels * ratio)
83 with_spectral_norm=False, 84 padding_mode='zeros', 85 order=('conv', 'norm', 'act')): 86 super(ConvModule, self).__init__() 87 assert conv_cfg is None or isinstance(conv_cfg, dict) 88 assert norm_cfg is None or isinstance(norm_cfg, dict) 89 assert act_cfg is None or isinstance(act_cfg, dict) 90 official_padding_mode = ['zeros', 'circular']
84 padding_mode='zeros', 85 order=('conv', 'norm', 'act')): 86 super(ConvModule, self).__init__() 87 assert conv_cfg is None or isinstance(conv_cfg, dict) 88 assert norm_cfg is None or isinstance(norm_cfg, dict) 89 assert act_cfg is None or isinstance(act_cfg, dict) 90 official_padding_mode = ['zeros', 'circular'] 91 self.conv_cfg = conv_cfg
85 order=('conv', 'norm', 'act')): 86 super(ConvModule, self).__init__() 87 assert conv_cfg is None or isinstance(conv_cfg, dict) 88 assert norm_cfg is None or isinstance(norm_cfg, dict) 89 assert act_cfg is None or isinstance(act_cfg, dict) 90 official_padding_mode = ['zeros', 'circular'] 91 self.conv_cfg = conv_cfg 92 self.norm_cfg = norm_cfg
94 self.inplace = inplace 95 self.with_spectral_norm = with_spectral_norm 96 self.with_explicit_padding = padding_mode not in official_padding_mode 97 self.order = order 98 assert isinstance(self.order, tuple) and len(self.order) == 3 99 assert set(order) == set(['conv', 'norm', 'act']) 100 101 self.with_norm = norm_cfg is not None
95 self.with_spectral_norm = with_spectral_norm 96 self.with_explicit_padding = padding_mode not in official_padding_mode 97 self.order = order 98 assert isinstance(self.order, tuple) and len(self.order) == 3 99 assert set(order) == set(['conv', 'norm', 'act']) 100 101 self.with_norm = norm_cfg is not None 102 self.with_activation = act_cfg is not None
59 pw_norm_cfg='default', 60 pw_act_cfg='default', 61 **kwargs): 62 super(DepthwiseSeparableConvModule, self).__init__() 63 assert 'groups' not in kwargs, 'groups should not be specified' 64 65 # if norm/activation config of depthwise/pointwise ConvModule is not 66 # specified, use default config.
23 def __init__(self, bias=1.0, divisor=2.0, min_value=0.0, max_value=1.0): 24 super(HSigmoid, self).__init__() 25 self.bias = bias 26 self.divisor = divisor 27 assert self.divisor != 0 28 self.min_value = min_value 29 self.max_value = max_value 30
99 100 norm_layer = NORM_LAYERS.get(layer_type) 101 abbr = infer_abbr(norm_layer) 102 103 assert isinstance(postfix, (int, str)) 104 name = abbr + str(postfix) 105 106 requires_grad = cfg_.pop('requires_grad', True)
109 layer = norm_layer(num_features, **cfg_) 110 if layer_type == 'SyncBN' and hasattr(layer, '_specify_ddp_gpu_num'): 111 layer._specify_ddp_gpu_num(1) 112 else: 113 assert 'num_groups' in cfg_ 114 layer = norm_layer(num_channels=num_features, **cfg_) 115 116 for param in layer.parameters():
79 80 plugin_layer = PLUGIN_LAYERS.get(layer_type) 81 abbr = infer_abbr(plugin_layer) 82 83 assert isinstance(postfix, (int, str)) 84 name = abbr + str(postfix) 85 86 layer = plugin_layer(**kwargs, **cfg_)
241 add_identity=True, 242 init_cfg=None, 243 **kwargs): 244 super(FFN, self).__init__(init_cfg) 245 assert num_fcs >= 2, 'num_fcs should be no less ' \ 246 f'than 2. got {num_fcs}.' 247 self.embed_dims = embed_dims 248 self.feedforward_channels = feedforward_channels 249 self.num_fcs = num_fcs
348 super(BaseTransformerLayer, self).__init__(init_cfg) 349 350 self.batch_first = batch_first 351 352 assert set(operation_order) & set( 353 ['self_attn', 'norm', 'ffn', 'cross_attn']) == \ 354 set(operation_order), f'The operation_order of' \ 355 f' {self.__class__.__name__} should ' \ 356 f'contains all four operation type ' \ 357 f"{['self_attn', 'norm', 'ffn', 'cross_attn']}" 358 359 num_attn = operation_order.count('self_attn') + operation_order.count( 360 'cross_attn')
360 'cross_attn') 361 if isinstance(attn_cfgs, dict): 362 attn_cfgs = [copy.deepcopy(attn_cfgs) for _ in range(num_attn)] 363 else: 364 assert num_attn == len(attn_cfgs), f'The length ' \ 365 f'of attn_cfg {num_attn} is ' \ 366 f'not consistent with the number of attention' \ 367 f'in operation_order {operation_order}.' 368 369 self.num_attn = num_attn 370 self.operation_order = operation_order
375 index = 0 376 for operation_name in operation_order: 377 if operation_name in ['self_attn', 'cross_attn']: 378 if 'batch_first' in attn_cfgs[index]: 379 assert self.batch_first == attn_cfgs[index]['batch_first'] 380 else: 381 attn_cfgs[index]['batch_first'] = self.batch_first 382 attention = build_attention(attn_cfgs[index])
393 if isinstance(ffn_cfgs, dict): 394 ffn_cfgs = ConfigDict(ffn_cfgs) 395 if isinstance(ffn_cfgs, dict): 396 ffn_cfgs = [copy.deepcopy(ffn_cfgs) for _ in range(num_ffns)] 397 assert len(ffn_cfgs) == num_ffns 398 for ffn_index in range(num_ffns): 399 if 'embed_dims' not in ffn_cfgs[ffn_index]: 400 ffn_cfgs['embed_dims'] = self.embed_dims
398 for ffn_index in range(num_ffns): 399 if 'embed_dims' not in ffn_cfgs[ffn_index]: 400 ffn_cfgs['embed_dims'] = self.embed_dims 401 else: 402 assert ffn_cfgs[ffn_index]['embed_dims'] == self.embed_dims 403 self.ffns.append( 404 build_feedforward_network(ffn_cfgs[ffn_index], 405 dict(type='FFN')))
462 ] 463 warnings.warn(f'Use same attn_mask in all attentions in ' 464 f'{self.__class__.__name__} ') 465 else: 466 assert len(attn_masks) == self.num_attn, f'The length of ' \ 467 f'attn_masks {len(attn_masks)} must be equal ' \ 468 f'to the number of attention in ' \ 469 f'operation_order {self.num_attn}' 470 471 for layer in self.operation_order: 472 if layer == 'self_attn':
536 transformerlayers = [ 537 copy.deepcopy(transformerlayers) for _ in range(num_layers) 538 ] 539 else: 540 assert isinstance(transformerlayers, list) and \ 541 len(transformerlayers) == num_layers 542 self.num_layers = num_layers 543 self.layers = ModuleList() 544 for i in range(num_layers):
30 downsample=None, 31 style='pytorch', 32 with_cp=False): 33 super(BasicBlock, self).__init__() 34 assert style in ['pytorch', 'caffe'] 35 self.conv1 = conv3x3(inplanes, planes, stride, dilation) 36 self.bn1 = nn.BatchNorm2d(planes) 37 self.relu = nn.ReLU(inplace=True)
39 self.bn2 = nn.BatchNorm2d(planes) 40 self.downsample = downsample 41 self.stride = stride 42 self.dilation = dilation 43 assert not with_cp 44 45 def forward(self, x): 46 residual = x
77 If style is "pytorch", the stride-two layer is the 3x3 conv layer, if 78 it is "caffe", the stride-two layer is the first 1x1 conv layer. 79 """ 80 super(Bottleneck, self).__init__() 81 assert style in ['pytorch', 'caffe'] 82 if style == 'pytorch': 83 conv1_stride = 1 84 conv2_stride = stride
220 with_cp=False): 221 super(ResNet, self).__init__() 222 if depth not in self.arch_settings: 223 raise KeyError(f'invalid depth {depth} for resnet') 224 assert num_stages >= 1 and num_stages <= 4 225 block, stage_blocks = self.arch_settings[depth] 226 stage_blocks = stage_blocks[:num_stages] 227 assert len(strides) == len(dilations) == num_stages
223 raise KeyError(f'invalid depth {depth} for resnet') 224 assert num_stages >= 1 and num_stages <= 4 225 block, stage_blocks = self.arch_settings[depth] 226 stage_blocks = stage_blocks[:num_stages] 227 assert len(strides) == len(dilations) == num_stages 228 assert max(out_indices) < num_stages 229 230 self.out_indices = out_indices
224 assert num_stages >= 1 and num_stages <= 4 225 block, stage_blocks = self.arch_settings[depth] 226 stage_blocks = stage_blocks[:num_stages] 227 assert len(strides) == len(dilations) == num_stages 228 assert max(out_indices) < num_stages 229 230 self.out_indices = out_indices 231 self.style = style
80 tuple[float | str]: If ``as_strings`` is set to True, it will return 81 FLOPs and parameter counts in a string format. otherwise, it will 82 return those in a float number format. 83 """ 84 assert type(input_shape) is tuple 85 assert len(input_shape) >= 1 86 assert isinstance(model, nn.Module) 87 flops_model = add_flops_counting_methods(model)
81 FLOPs and parameter counts in a string format. otherwise, it will 82 return those in a float number format. 83 """ 84 assert type(input_shape) is tuple 85 assert len(input_shape) >= 1 86 assert isinstance(model, nn.Module) 87 flops_model = add_flops_counting_methods(model) 88 flops_model.eval()
82 return those in a float number format. 83 """ 84 assert type(input_shape) is tuple 85 assert len(input_shape) >= 1 86 assert isinstance(model, nn.Module) 87 flops_model = add_flops_counting_methods(model) 88 flops_model.eval() 89 flops_model.start_flops_count()
289 flops_extra_repr = flops_repr.__get__(m) 290 if m.extra_repr != flops_extra_repr: 291 m.original_extra_repr = m.extra_repr 292 m.extra_repr = flops_extra_repr 293 assert m.extra_repr != m.original_extra_repr 294 295 def del_extra_repr(m): 296 if hasattr(m, 'original_extra_repr'):
22 attribute `_params_init_info` which records the initialization 23 information. 24 init_info (str): The string that describes the initialization. 25 """ 26 assert hasattr( 27 module, 28 '_params_init_info'), f'Can not find `_params_init_info` in {module}' 29 for name, param in module.named_parameters(): 30 31 assert param in module._params_init_info, (
27 module, 28 '_params_init_info'), f'Can not find `_params_init_info` in {module}' 29 for name, param in module.named_parameters(): 30 31 assert param in module._params_init_info, ( 32 f'Find a new :obj:`Parameter` ' 33 f'named `{name}` during executing the ' 34 f'`init_weights` of ' 35 f'`{module.__class__.__name__}`. ' 36 f'Please do not add or ' 37 f'replace parameters during executing ' 38 f'the `init_weights`. ') 39 40 # The parameter has been changed during executing the 41 # `init_weights` of module
52 nn.init.constant_(module.bias, bias) 53 54 55 def xavier_init(module, gain=1, bias=0, distribution='normal'): 56 assert distribution in ['uniform', 'normal'] 57 if hasattr(module, 'weight') and module.weight is not None: 58 if distribution == 'uniform': 59 nn.init.xavier_uniform_(module.weight, gain=gain)
94 mode='fan_out', 95 nonlinearity='relu', 96 bias=0, 97 distribution='normal'): 98 assert distribution in ['uniform', 'normal'] 99 if hasattr(module, 'weight') and module.weight is not None: 100 if distribution == 'uniform': 101 nn.init.kaiming_uniform_(
72 with_last_pool=True): 73 super(VGG, self).__init__() 74 if depth not in self.arch_settings: 75 raise KeyError(f'invalid depth {depth} for vgg') 76 assert num_stages >= 1 and num_stages <= 5 77 stage_blocks = self.arch_settings[depth] 78 self.stage_blocks = stage_blocks[:num_stages] 79 assert len(dilations) == num_stages
75 raise KeyError(f'invalid depth {depth} for vgg') 76 assert num_stages >= 1 and num_stages <= 5 77 stage_blocks = self.arch_settings[depth] 78 self.stage_blocks = stage_blocks[:num_stages] 79 assert len(dilations) == num_stages 80 assert max(out_indices) <= num_stages 81 82 self.num_classes = num_classes
76 assert num_stages >= 1 and num_stages <= 5 77 stage_blocks = self.arch_settings[depth] 78 self.stage_blocks = stage_blocks[:num_stages] 79 assert len(dilations) == num_stages 80 assert max(out_indices) <= num_stages 81 82 self.num_classes = num_classes 83 self.out_indices = out_indices
1 # Copyright (c) OpenMMLab. All rights reserved. 2 import os.path as osp 3 import pickle 4 import shutil 5 import tempfile 6 import time 7 8 import torch
187 188 if rank == 0: 189 part_list = [] 190 for recv, shape in zip(part_recv_list, shape_list): 191 part_result = pickle.loads(recv[:shape[0]].cpu().numpy().tobytes()) 192 # When data is severely insufficient, an empty part_result 193 # on a certain gpu could makes the overall outputs empty. 194 if part_result:
65 66 warnings.warn( 67 'CephBackend will be deprecated, please use PetrelBackend instead') 68 self._client = ceph.S3Client() 69 assert isinstance(path_mapping, dict) or path_mapping is None 70 self.path_mapping = path_mapping 71 72 def get(self, filepath):
114 raise ImportError('Please install petrel_client to enable ' 115 'PetrelBackend.') 116 117 self._client = client.Client(enable_mc=enable_mc) 118 assert isinstance(path_mapping, dict) or path_mapping is None 119 self.path_mapping = path_mapping 120 121 def _map_path(self, filepath: Union[str, Path]) -> str:
318 Iterable[str]: Only yield one temporary path. 319 """ 320 filepath = self._map_path(filepath) 321 filepath = self._format_path(filepath) 322 assert self.isfile(filepath) 323 try: 324 f = tempfile.NamedTemporaryFile(delete=False) 325 f.write(self.get(filepath))
691 class HTTPBackend(BaseStorageBackend): 692 """HTTP and HTTPS storage bachend.""" 693 694 def get(self, filepath): 695 value_buf = urlopen(filepath).read() 696 return value_buf 697 698 def get_text(self, filepath, encoding='utf-8'):
695 value_buf = urlopen(filepath).read() 696 return value_buf 697 698 def get_text(self, filepath, encoding='utf-8'): 699 value_buf = urlopen(filepath).read() 700 return value_buf.decode(encoding) 701 702 @contextmanager
841 Returns: 842 str | None: Return the prefix of uri if the uri contains '://' 843 else ``None``. 844 """ 845 assert is_filepath(uri) 846 uri = str(uri) 847 if '://' not in uri: 848 return None
874 875 Returns: 876 FileClient: Instantiated FileClient object. 877 """ 878 assert file_client_args is not None or uri is not None 879 if file_client_args is None: 880 file_prefix = cls.parse_uri_prefix(uri) # type: ignore 881 return cls(prefix=file_prefix)
905 if prefixes is not None: 906 if isinstance(prefixes, str): 907 prefixes = [prefixes] 908 else: 909 assert isinstance(prefixes, (list, tuple)) 910 for prefix in prefixes: 911 if prefix not in cls._prefix_to_backends: 912 cls._prefix_to_backends[prefix] = backend
1 # Copyright (c) OpenMMLab. All rights reserved. 2 from .base import BaseFileHandler 3 from .json_handler import JsonHandler 4 from .pickle_handler import PickleHandler 5 from .yaml_handler import YamlHandler 6 7 __all__ = ['BaseFileHandler', 'JsonHandler', 'PickleHandler', 'YamlHandler']
1 # Copyright (c) OpenMMLab. All rights reserved. 2 import pickle 3 4 from .base import BaseFileHandler 5 6 7 class PickleHandler(BaseFileHandler): 8
8 9 str_like = False 10 11 def load_from_fileobj(self, file, **kwargs): 12 return pickle.load(file, **kwargs) 13 14 def load_from_path(self, filepath, **kwargs): 15 return super(PickleHandler, self).load_from_path(
12 class YamlHandler(BaseFileHandler): 13 14 def load_from_fileobj(self, file, **kwargs): 15 kwargs.setdefault('Loader', Loader) 16 return yaml.load(file, **kwargs) 17 18 def dump_to_fileobj(self, obj, file, **kwargs): 19 kwargs.setdefault('Dumper', Dumper)
89 file_client = FileClient.infer_client(file_client_args, filename) 90 with StringIO(file_client.get_text(filename, encoding)) as f: 91 for line in f: 92 items = line.rstrip('\n').split() 93 assert len(items) >= 2 94 key = key_type(items[0]) 95 val = items[1:] if len(items) > 2 else items[1] 96 mapping[key] = val
79 raise ValueError(f'backend: {backend} is not supported for resize.' 80 f"Supported backends are 'cv2', 'pillow'") 81 82 if backend == 'pillow': 83 assert img.dtype == np.uint8, 'Pillow backend only support uint8 type' 84 pil_image = Image.fromarray(img) 85 pil_image = pil_image.resize(size, pillow_interp_codes[interpolation]) 86 resized_img = np.array(pil_image)
259 260 Returns: 261 ndarray: The flipped image. 262 """ 263 assert direction in ['horizontal', 'vertical', 'diagonal'] 264 if direction == 'horizontal': 265 return np.flip(img, axis=1) 266 elif direction == 'vertical':
279 280 Returns: 281 ndarray: The flipped image (inplace). 282 """ 283 assert direction in ['horizontal', 'vertical', 'diagonal'] 284 if direction == 'horizontal': 285 return cv2.flip(img, 1, img) 286 elif direction == 'vertical':
318 raise ValueError('`auto_bound` conflicts with `center`') 319 h, w = img.shape[:2] 320 if center is None: 321 center = ((w - 1) * 0.5, (h - 1) * 0.5) 322 assert isinstance(center, tuple) 323 324 matrix = cv2.getRotationMatrix2D(center, -angle, scale) 325 if auto_bound:
348 349 Returns: 350 ndarray: Clipped bboxes. 351 """ 352 assert bboxes.shape[-1] % 4 == 0 353 cmin = np.empty(bboxes.shape[-1], dtype=bboxes.dtype) 354 cmin[0::2] = img_shape[1] - 1 355 cmin[1::2] = img_shape[0] - 1
402 chn = 1 if img.ndim == 2 else img.shape[2] 403 if pad_fill is not None: 404 if isinstance(pad_fill, (int, float)): 405 pad_fill = [pad_fill for _ in range(chn)] 406 assert len(pad_fill) == chn 407 408 _bboxes = bboxes[None, ...] if bboxes.ndim == 1 else bboxes 409 scaled_bboxes = bbox_scaling(_bboxes, scale).astype(np.int32)
476 Returns: 477 ndarray: The padded image. 478 """ 479 480 assert (shape is not None) ^ (padding is not None) 481 if shape is not None: 482 padding = (0, 0, shape[1] - img.shape[1], shape[0] - img.shape[0]) 483
482 padding = (0, 0, shape[1] - img.shape[1], shape[0] - img.shape[0]) 483 484 # check pad_val 485 if isinstance(pad_val, tuple): 486 assert len(pad_val) == img.shape[-1] 487 elif not isinstance(pad_val, numbers.Number): 488 raise TypeError('pad_val must be a int or a tuple. ' 489 f'But received {type(pad_val)}')
498 raise ValueError('Padding must be a int or a 2, or 4 element tuple.' 499 f'But received {padding}') 500 501 # check padding mode 502 assert padding_mode in ['constant', 'edge', 'reflect', 'symmetric'] 503 504 border_type = { 505 'constant': cv2.BORDER_CONSTANT,
552 channels = 1 if img.ndim == 2 else img.shape[2] 553 if isinstance(shape, int): 554 cut_h, cut_w = shape, shape 555 else: 556 assert isinstance(shape, tuple) and len(shape) == 2, \ 557 f'shape must be a int or a tuple with length 2, but got type ' \ 558 f'{type(shape)} instead.' 559 cut_h, cut_w = shape 560 if isinstance(pad_val, (int, float)): 561 pad_val = tuple([pad_val] * channels)
559 cut_h, cut_w = shape 560 if isinstance(pad_val, (int, float)): 561 pad_val = tuple([pad_val] * channels) 562 elif isinstance(pad_val, tuple): 563 assert len(pad_val) == channels, \ 564 'Expected the num of elements in tuple equals the channels' \ 565 'of input image. Found {} vs {}'.format( 566 len(pad_val), channels) 567 else: 568 raise TypeError(f'Invalid type {type(pad_val)} for `pad_val`') 569
627 628 Returns: 629 ndarray: The sheared image. 630 """ 631 assert direction in ['horizontal', 632 'vertical'], f'Invalid direction: {direction}' 633 height, width = img.shape[:2] 634 if img.ndim == 2: 635 channels = 1
637 channels = img.shape[-1] 638 if isinstance(border_value, int): 639 border_value = tuple([border_value] * channels) 640 elif isinstance(border_value, tuple): 641 assert len(border_value) == channels, \ 642 'Expected the num of elements in tuple equals the channels' \ 643 'of input image. Found {} vs {}'.format( 644 len(border_value), channels) 645 else: 646 raise ValueError( 647 f'Invalid type {type(border_value)} for `border_value`')
696 697 Returns: 698 ndarray: The translated image. 699 """ 700 assert direction in ['horizontal', 701 'vertical'], f'Invalid direction: {direction}' 702 height, width = img.shape[:2] 703 if img.ndim == 2: 704 channels = 1
706 channels = img.shape[-1] 707 if isinstance(border_value, int): 708 border_value = tuple([border_value] * channels) 709 elif isinstance(border_value, tuple): 710 assert len(border_value) == channels, \ 711 'Expected the num of elements in tuple equals the channels' \ 712 'of input image. Found {} vs {}'.format( 713 len(border_value), channels) 714 else: 715 raise ValueError( 716 f'Invalid type {type(border_value)} for `border_value`.')
48 `pillow`, `turbojpeg` (see https://github.com/lilohuang/PyTurboJPEG) 49 and `tifffile`. `turbojpeg` is faster but it only supports `.jpeg` 50 file format. 51 """ 52 assert backend in supported_backends 53 global imread_backend 54 imread_backend = backend 55 if imread_backend == 'turbojpeg':
27 """ 28 29 if torch is None: 30 raise RuntimeError('pytorch is not installed') 31 assert torch.is_tensor(tensor) and tensor.ndim == 4 32 assert len(mean) == 3 33 assert len(std) == 3 34
28 29 if torch is None: 30 raise RuntimeError('pytorch is not installed') 31 assert torch.is_tensor(tensor) and tensor.ndim == 4 32 assert len(mean) == 3 33 assert len(std) == 3 34 35 num_imgs = tensor.size(0)
29 if torch is None: 30 raise RuntimeError('pytorch is not installed') 31 assert torch.is_tensor(tensor) and tensor.ndim == 4 32 assert len(mean) == 3 33 assert len(std) == 3 34 35 num_imgs = tensor.size(0) 36 mean = np.array(mean, dtype=np.float32)
34 Returns: 35 ndarray: The normalized image. 36 """ 37 # cv2 inplace normalization does not accept uint8 38 assert img.dtype != np.uint8 39 mean = np.float64(mean.reshape(1, -1)) 40 stdinv = 1 / np.float64(std.reshape(1, -1)) 41 if to_rgb:
45 return img 46 47 48 def imdenormalize(img, mean, std, to_bgr=True): 49 assert img.dtype != np.uint8 50 mean = mean.reshape(1, -1).astype(np.float64) 51 std = std.reshape(1, -1).astype(np.float64) 52 img = cv2.multiply(img, std) # make a copy
279 280 if isinstance(cutoff, (int, float)): 281 cutoff = (cutoff, cutoff) 282 else: 283 assert isinstance(cutoff, tuple), 'cutoff must be of type int, ' \ 284 f'float or tuple, but got {type(cutoff)} instead.' 285 # Auto adjusts contrast for each channel independently and then stacks 286 # the result. 287 s1 = _auto_contrast_channel(img, 0, cutoff)
321 322 if kernel is None: 323 # adopted from PIL.ImageFilter.SMOOTH 324 kernel = np.array([[1., 1., 1.], [1., 5., 1.], [1., 1., 1.]]) / 13 325 assert isinstance(kernel, np.ndarray), \ 326 f'kernel must be of type np.ndarray, but got {type(kernel)} instead.' 327 assert kernel.ndim == 2, \ 328 f'kernel must have a dimension of 2, but got {kernel.ndim} instead.' 329
323 # adopted from PIL.ImageFilter.SMOOTH 324 kernel = np.array([[1., 1., 1.], [1., 5., 1.], [1., 1., 1.]]) / 13 325 assert isinstance(kernel, np.ndarray), \ 326 f'kernel must be of type np.ndarray, but got {type(kernel)} instead.' 327 assert kernel.ndim == 2, \ 328 f'kernel must have a dimension of 2, but got {kernel.ndim} instead.' 329 330 degenerated = cv2.filter2D(img, -1, kernel) 331 sharpened_img = cv2.addWeighted(
354 355 Returns: 356 ndarray: The adjusted image. 357 """ 358 assert isinstance(eigval, np.ndarray) and isinstance(eigvec, np.ndarray), \ 359 f'eigval and eigvec should both be of type np.ndarray, got ' \ 360 f'{type(eigval)} and {type(eigvec)} instead.' 361 362 assert eigval.ndim == 1 and eigvec.ndim == 2 363 assert eigvec.shape == (3, eigval.shape[0])
358 assert isinstance(eigval, np.ndarray) and isinstance(eigvec, np.ndarray), \ 359 f'eigval and eigvec should both be of type np.ndarray, got ' \ 360 f'{type(eigval)} and {type(eigvec)} instead.' 361 362 assert eigval.ndim == 1 and eigvec.ndim == 2 363 assert eigvec.shape == (3, eigval.shape[0]) 364 n_eigval = eigval.shape[0] 365 assert isinstance(alphastd, float), 'alphastd should be of type float, ' \
359 f'eigval and eigvec should both be of type np.ndarray, got ' \ 360 f'{type(eigval)} and {type(eigvec)} instead.' 361 362 assert eigval.ndim == 1 and eigvec.ndim == 2 363 assert eigvec.shape == (3, eigval.shape[0]) 364 n_eigval = eigval.shape[0] 365 assert isinstance(alphastd, float), 'alphastd should be of type float, ' \ 366 f'got {type(alphastd)} instead.'
361 362 assert eigval.ndim == 1 and eigvec.ndim == 2 363 assert eigvec.shape == (3, eigval.shape[0]) 364 n_eigval = eigval.shape[0] 365 assert isinstance(alphastd, float), 'alphastd should be of type float, ' \ 366 f'got {type(alphastd)} instead.' 367 368 img = img.copy().astype(np.float32) 369 if to_rgb:
393 394 Returns: 395 ndarray: The transformed image. 396 """ 397 assert isinstance(img, np.ndarray) 398 assert 0 <= np.min(img) and np.max(img) <= 255 399 assert isinstance(lut_table, np.ndarray) 400 assert lut_table.shape == (256, )
394 Returns: 395 ndarray: The transformed image. 396 """ 397 assert isinstance(img, np.ndarray) 398 assert 0 <= np.min(img) and np.max(img) <= 255 399 assert isinstance(lut_table, np.ndarray) 400 assert lut_table.shape == (256, ) 401
395 ndarray: The transformed image. 396 """ 397 assert isinstance(img, np.ndarray) 398 assert 0 <= np.min(img) and np.max(img) <= 255 399 assert isinstance(lut_table, np.ndarray) 400 assert lut_table.shape == (256, ) 401 402 return cv2.LUT(np.array(img, dtype=np.uint8), lut_table)
396 """ 397 assert isinstance(img, np.ndarray) 398 assert 0 <= np.min(img) and np.max(img) <= 255 399 assert isinstance(lut_table, np.ndarray) 400 assert lut_table.shape == (256, ) 401 402 return cv2.LUT(np.array(img, dtype=np.uint8), lut_table) 403
417 418 Returns: 419 ndarray: The processed image. 420 """ 421 assert isinstance(img, np.ndarray) 422 assert img.ndim == 2 423 assert isinstance(clip_limit, (float, int)) 424 assert is_tuple_of(tile_grid_size, int)
418 Returns: 419 ndarray: The processed image. 420 """ 421 assert isinstance(img, np.ndarray) 422 assert img.ndim == 2 423 assert isinstance(clip_limit, (float, int)) 424 assert is_tuple_of(tile_grid_size, int) 425 assert len(tile_grid_size) == 2
419 ndarray: The processed image. 420 """ 421 assert isinstance(img, np.ndarray) 422 assert img.ndim == 2 423 assert isinstance(clip_limit, (float, int)) 424 assert is_tuple_of(tile_grid_size, int) 425 assert len(tile_grid_size) == 2 426
420 """ 421 assert isinstance(img, np.ndarray) 422 assert img.ndim == 2 423 assert isinstance(clip_limit, (float, int)) 424 assert is_tuple_of(tile_grid_size, int) 425 assert len(tile_grid_size) == 2 426 427 clahe = cv2.createCLAHE(clip_limit, tile_grid_size)
421 assert isinstance(img, np.ndarray) 422 assert img.ndim == 2 423 assert isinstance(clip_limit, (float, int)) 424 assert is_tuple_of(tile_grid_size, int) 425 assert len(tile_grid_size) == 2 426 427 clahe = cv2.createCLAHE(clip_limit, tile_grid_size) 428 return clahe.apply(np.array(img, dtype=np.uint8))
24 Returns: 25 Tensor: (B, npoint, nsample) tensor with the indices of 26 the features that form the query balls. 27 """ 28 assert center_xyz.is_contiguous() 29 assert xyz.is_contiguous() 30 assert min_radius < max_radius 31
25 Tensor: (B, npoint, nsample) tensor with the indices of 26 the features that form the query balls. 27 """ 28 assert center_xyz.is_contiguous() 29 assert xyz.is_contiguous() 30 assert min_radius < max_radius 31 32 B, N, _ = xyz.size()
26 the features that form the query balls. 27 """ 28 assert center_xyz.is_contiguous() 29 assert xyz.is_contiguous() 30 assert min_radius < max_radius 31 32 B, N, _ = xyz.size() 33 npoint = center_xyz.size(1)
47 >>> assert tuple(bbox_overlaps(empty, empty).shape) == (0, 0) 48 """ 49 50 mode_dict = {'iou': 0, 'iof': 1} 51 assert mode in mode_dict.keys() 52 mode_flag = mode_dict[mode] 53 # Either the boxes are empty or the length of boxes' last dimension is 4 54 assert (bboxes1.size(-1) == 4 or bboxes1.size(0) == 0)
50 mode_dict = {'iou': 0, 'iof': 1} 51 assert mode in mode_dict.keys() 52 mode_flag = mode_dict[mode] 53 # Either the boxes are empty or the length of boxes' last dimension is 4 54 assert (bboxes1.size(-1) == 4 or bboxes1.size(0) == 0) 55 assert (bboxes2.size(-1) == 4 or bboxes2.size(0) == 0) 56 assert offset == 1 or offset == 0 57
51 assert mode in mode_dict.keys() 52 mode_flag = mode_dict[mode] 53 # Either the boxes are empty or the length of boxes' last dimension is 4 54 assert (bboxes1.size(-1) == 4 or bboxes1.size(0) == 0) 55 assert (bboxes2.size(-1) == 4 or bboxes2.size(0) == 0) 56 assert offset == 1 or offset == 0 57 58 rows = bboxes1.size(0)
52 mode_flag = mode_dict[mode] 53 # Either the boxes are empty or the length of boxes' last dimension is 4 54 assert (bboxes1.size(-1) == 4 or bboxes1.size(0) == 0) 55 assert (bboxes2.size(-1) == 4 or bboxes2.size(0) == 0) 56 assert offset == 1 or offset == 0 57 58 rows = bboxes1.size(0) 59 cols = bboxes2.size(0)
57 58 rows = bboxes1.size(0) 59 cols = bboxes2.size(0) 60 if aligned: 61 assert rows == cols 62 63 if rows * cols == 0: 64 return bboxes1.new(rows, 1) if aligned else bboxes1.new(rows, cols)
24 def forward(ctx, input, boxes, pool_size): 25 ctx.pool_size = pool_size 26 ctx.input_shape = input.size() 27 28 assert boxes.ndim == 3, 'boxes must be with shape [B, H*W, 4]' 29 assert boxes.size(2) == 4, \ 30 'the last dimension of boxes must be (x1, y1, x2, y2)' 31 assert input.size(1) % 4 == 0, \
25 ctx.pool_size = pool_size 26 ctx.input_shape = input.size() 27 28 assert boxes.ndim == 3, 'boxes must be with shape [B, H*W, 4]' 29 assert boxes.size(2) == 4, \ 30 'the last dimension of boxes must be (x1, y1, x2, y2)' 31 assert input.size(1) % 4 == 0, \ 32 'the channel for input feature must be divisible by factor 4' 33
27 28 assert boxes.ndim == 3, 'boxes must be with shape [B, H*W, 4]' 29 assert boxes.size(2) == 4, \ 30 'the last dimension of boxes must be (x1, y1, x2, y2)' 31 assert input.size(1) % 4 == 0, \ 32 'the channel for input feature must be divisible by factor 4' 33 34 # [B, C//4, H*W, 4] 35 output_shape = (input.size(0), input.size(1) // 4, boxes.size(1), 4)
26 27 Returns: 28 ious(Tensor): shape (N, M) if aligned == False else shape (N,) 29 """ 30 assert mode in ['iou', 'iof'] 31 mode_dict = {'iou': 0, 'iof': 1} 32 mode_flag = mode_dict[mode] 33 rows = bboxes1.size(0)
27 scale_factor_f=scale_factor) 28 29 @staticmethod 30 def forward(ctx, features, masks, kernel_size, group_size, scale_factor): 31 assert scale_factor >= 1 32 assert masks.size(1) == kernel_size * kernel_size * group_size 33 assert masks.size(-1) == features.size(-1) * scale_factor 34 assert masks.size(-2) == features.size(-2) * scale_factor
28 29 @staticmethod 30 def forward(ctx, features, masks, kernel_size, group_size, scale_factor): 31 assert scale_factor >= 1 32 assert masks.size(1) == kernel_size * kernel_size * group_size 33 assert masks.size(-1) == features.size(-1) * scale_factor 34 assert masks.size(-2) == features.size(-2) * scale_factor 35 assert features.size(1) % group_size == 0
29 @staticmethod 30 def forward(ctx, features, masks, kernel_size, group_size, scale_factor): 31 assert scale_factor >= 1 32 assert masks.size(1) == kernel_size * kernel_size * group_size 33 assert masks.size(-1) == features.size(-1) * scale_factor 34 assert masks.size(-2) == features.size(-2) * scale_factor 35 assert features.size(1) % group_size == 0 36 assert (kernel_size - 1) % 2 == 0 and kernel_size >= 1
30 def forward(ctx, features, masks, kernel_size, group_size, scale_factor): 31 assert scale_factor >= 1 32 assert masks.size(1) == kernel_size * kernel_size * group_size 33 assert masks.size(-1) == features.size(-1) * scale_factor 34 assert masks.size(-2) == features.size(-2) * scale_factor 35 assert features.size(1) % group_size == 0 36 assert (kernel_size - 1) % 2 == 0 and kernel_size >= 1 37 ctx.kernel_size = kernel_size
31 assert scale_factor >= 1 32 assert masks.size(1) == kernel_size * kernel_size * group_size 33 assert masks.size(-1) == features.size(-1) * scale_factor 34 assert masks.size(-2) == features.size(-2) * scale_factor 35 assert features.size(1) % group_size == 0 36 assert (kernel_size - 1) % 2 == 0 and kernel_size >= 1 37 ctx.kernel_size = kernel_size 38 ctx.group_size = group_size
32 assert masks.size(1) == kernel_size * kernel_size * group_size 33 assert masks.size(-1) == features.size(-1) * scale_factor 34 assert masks.size(-2) == features.size(-2) * scale_factor 35 assert features.size(1) % group_size == 0 36 assert (kernel_size - 1) % 2 == 0 and kernel_size >= 1 37 ctx.kernel_size = kernel_size 38 ctx.group_size = group_size 39 ctx.scale_factor = scale_factor
55 return output 56 57 @staticmethod 58 def backward(ctx, grad_output): 59 assert grad_output.is_cuda 60 61 features, masks = ctx.saved_tensors 62 kernel_size = ctx.kernel_size
85 86 def __init__(self, kernel_size, group_size, scale_factor): 87 super(CARAFENaive, self).__init__() 88 89 assert isinstance(kernel_size, int) and isinstance( 90 group_size, int) and isinstance(scale_factor, int) 91 self.kernel_size = kernel_size 92 self.group_size = group_size 93 self.scale_factor = scale_factor
110 scale_factor_f=scale_factor) 111 112 @staticmethod 113 def forward(ctx, features, masks, kernel_size, group_size, scale_factor): 114 assert scale_factor >= 1 115 assert masks.size(1) == kernel_size * kernel_size * group_size 116 assert masks.size(-1) == features.size(-1) * scale_factor 117 assert masks.size(-2) == features.size(-2) * scale_factor
111 112 @staticmethod 113 def forward(ctx, features, masks, kernel_size, group_size, scale_factor): 114 assert scale_factor >= 1 115 assert masks.size(1) == kernel_size * kernel_size * group_size 116 assert masks.size(-1) == features.size(-1) * scale_factor 117 assert masks.size(-2) == features.size(-2) * scale_factor 118 assert features.size(1) % group_size == 0
112 @staticmethod 113 def forward(ctx, features, masks, kernel_size, group_size, scale_factor): 114 assert scale_factor >= 1 115 assert masks.size(1) == kernel_size * kernel_size * group_size 116 assert masks.size(-1) == features.size(-1) * scale_factor 117 assert masks.size(-2) == features.size(-2) * scale_factor 118 assert features.size(1) % group_size == 0 119 assert (kernel_size - 1) % 2 == 0 and kernel_size >= 1
113 def forward(ctx, features, masks, kernel_size, group_size, scale_factor): 114 assert scale_factor >= 1 115 assert masks.size(1) == kernel_size * kernel_size * group_size 116 assert masks.size(-1) == features.size(-1) * scale_factor 117 assert masks.size(-2) == features.size(-2) * scale_factor 118 assert features.size(1) % group_size == 0 119 assert (kernel_size - 1) % 2 == 0 and kernel_size >= 1 120 ctx.kernel_size = kernel_size
114 assert scale_factor >= 1 115 assert masks.size(1) == kernel_size * kernel_size * group_size 116 assert masks.size(-1) == features.size(-1) * scale_factor 117 assert masks.size(-2) == features.size(-2) * scale_factor 118 assert features.size(1) % group_size == 0 119 assert (kernel_size - 1) % 2 == 0 and kernel_size >= 1 120 ctx.kernel_size = kernel_size 121 ctx.group_size = group_size
115 assert masks.size(1) == kernel_size * kernel_size * group_size 116 assert masks.size(-1) == features.size(-1) * scale_factor 117 assert masks.size(-2) == features.size(-2) * scale_factor 118 assert features.size(1) % group_size == 0 119 assert (kernel_size - 1) % 2 == 0 and kernel_size >= 1 120 ctx.kernel_size = kernel_size 121 ctx.group_size = group_size 122 ctx.scale_factor = scale_factor
144 return output 145 146 @staticmethod 147 def backward(ctx, grad_output): 148 assert grad_output.is_cuda 149 150 features, masks, rfeatures = ctx.saved_tensors 151 kernel_size = ctx.kernel_size
193 194 def __init__(self, kernel_size, group_size, scale_factor): 195 super(CARAFE, self).__init__() 196 197 assert isinstance(kernel_size, int) and isinstance( 198 group_size, int) and isinstance(scale_factor, int) 199 self.kernel_size = kernel_size 200 self.group_size = group_size 201 self.scale_factor = scale_factor
22 23 Returns: 24 label (list): The instance index map with size hxw. 25 """ 26 assert isinstance(kernel_mask, (torch.Tensor, np.ndarray)) 27 assert isinstance(internal_kernel_label, (torch.Tensor, np.ndarray)) 28 assert isinstance(min_kernel_area, int) 29 assert isinstance(kernel_num, int)
23 Returns: 24 label (list): The instance index map with size hxw. 25 """ 26 assert isinstance(kernel_mask, (torch.Tensor, np.ndarray)) 27 assert isinstance(internal_kernel_label, (torch.Tensor, np.ndarray)) 28 assert isinstance(min_kernel_area, int) 29 assert isinstance(kernel_num, int) 30
24 label (list): The instance index map with size hxw. 25 """ 26 assert isinstance(kernel_mask, (torch.Tensor, np.ndarray)) 27 assert isinstance(internal_kernel_label, (torch.Tensor, np.ndarray)) 28 assert isinstance(min_kernel_area, int) 29 assert isinstance(kernel_num, int) 30 31 if isinstance(kernel_mask, np.ndarray):
25 """ 26 assert isinstance(kernel_mask, (torch.Tensor, np.ndarray)) 27 assert isinstance(internal_kernel_label, (torch.Tensor, np.ndarray)) 28 assert isinstance(min_kernel_area, int) 29 assert isinstance(kernel_num, int) 30 31 if isinstance(kernel_mask, np.ndarray): 32 kernel_mask = torch.from_numpy(kernel_mask)
134 } 135 136 def __init__(self, mode): 137 super(CornerPool, self).__init__() 138 assert mode in self.pool_functions 139 self.mode = mode 140 self.corner_pool = self.pool_functions[mode] 141
141 142 def forward(self, x): 143 if torch.__version__ != 'parrots' and torch.__version__ >= '1.5.0': 144 if torch.onnx.is_in_onnx_export(): 145 assert torch.__version__ >= '1.7.0', \ 146 'When `cummax` serves as an intermediate component whose '\ 147 'outputs is used as inputs for another modules, it\'s '\ 148 'expected that pytorch version must be >= 1.7.0, '\ 149 'otherwise Error appears like: `RuntimeError: tuple '\ 150 'appears in op that does not forward tuples, unsupported '\ 151 'kind: prim::PythonOp`.' 152 153 dim, flip = self.cummax_dim_flip[self.mode] 154 if flip:
61 if input is not None and input.dim() != 4: 62 raise ValueError( 63 f'Expected 4D tensor as input, got {input.dim()}D tensor \ 64 instead.') 65 assert bias is False, 'Only support bias is False.' 66 ctx.stride = _pair(stride) 67 ctx.padding = _pair(padding) 68 ctx.dilation = _pair(dilation)
86 87 ctx.bufs_ = [input.new_empty(0), input.new_empty(0)] # columns, ones 88 89 cur_im2col_step = min(ctx.im2col_step, input.size(0)) 90 assert (input.size(0) % 91 cur_im2col_step) == 0, 'im2col step must divide batchsize' 92 ext_module.deform_conv_forward( 93 input, 94 weight,
116 117 grad_input = grad_offset = grad_weight = None 118 119 cur_im2col_step = min(ctx.im2col_step, input.size(0)) 120 assert (input.size(0) % cur_im2col_step 121 ) == 0, 'batch size must be divisible by im2col_step' 122 123 grad_output = grad_output.contiguous() 124 if ctx.needs_input_grad[0] or ctx.needs_input_grad[1]:
237 bias: bool = False, 238 im2col_step: int = 32) -> None: 239 super(DeformConv2d, self).__init__() 240 241 assert not bias, \ 242 f'bias={bias} is not supported in DeformConv2d.' 243 assert in_channels % groups == 0, \ 244 f'in_channels {in_channels} cannot be divisible by groups {groups}' 245 assert out_channels % groups == 0, \
239 super(DeformConv2d, self).__init__() 240 241 assert not bias, \ 242 f'bias={bias} is not supported in DeformConv2d.' 243 assert in_channels % groups == 0, \ 244 f'in_channels {in_channels} cannot be divisible by groups {groups}' 245 assert out_channels % groups == 0, \ 246 f'out_channels {out_channels} cannot be divisible by groups \ 247 {groups}'
241 assert not bias, \ 242 f'bias={bias} is not supported in DeformConv2d.' 243 assert in_channels % groups == 0, \ 244 f'in_channels {in_channels} cannot be divisible by groups {groups}' 245 assert out_channels % groups == 0, \ 246 f'out_channels {out_channels} cannot be divisible by groups \ 247 {groups}' 248 249 self.in_channels = in_channels 250 self.out_channels = out_channels
41 ctx.spatial_scale = float(spatial_scale) 42 ctx.sampling_ratio = int(sampling_ratio) 43 ctx.gamma = float(gamma) 44 45 assert rois.size(1) == 5, 'RoI must be (idx, x1, y1, x2, y2)!' 46 47 output_shape = (rois.size(0), input.size(1), ctx.output_size[0], 48 ctx.output_size[1])
135 self.offset_fc[-1].weight.data.zero_() 136 self.offset_fc[-1].bias.data.zero_() 137 138 def forward(self, input, rois): 139 assert input.size(1) == self.output_channels 140 x = deform_roi_pool(input, rois, None, self.output_size, 141 self.spatial_scale, self.sampling_ratio, 142 self.gamma)
187 self.mask_fc[2].weight.data.zero_() 188 self.mask_fc[2].bias.data.zero_() 189 190 def forward(self, input, rois): 191 assert input.size(1) == self.output_channels 192 x = deform_roi_pool(input, rois, None, self.output_size, 193 self.spatial_scale, self.sampling_ratio, 194 self.gamma)
33 alpha=0.25, 34 weight=None, 35 reduction='mean'): 36 37 assert isinstance(target, (torch.LongTensor, torch.cuda.LongTensor)) 38 assert input.dim() == 2 39 assert target.dim() == 1 40 assert input.size(0) == target.size(0)
34 weight=None, 35 reduction='mean'): 36 37 assert isinstance(target, (torch.LongTensor, torch.cuda.LongTensor)) 38 assert input.dim() == 2 39 assert target.dim() == 1 40 assert input.size(0) == target.size(0) 41 if weight is None:
35 reduction='mean'): 36 37 assert isinstance(target, (torch.LongTensor, torch.cuda.LongTensor)) 38 assert input.dim() == 2 39 assert target.dim() == 1 40 assert input.size(0) == target.size(0) 41 if weight is None: 42 weight = input.new_empty(0)
36 37 assert isinstance(target, (torch.LongTensor, torch.cuda.LongTensor)) 38 assert input.dim() == 2 39 assert target.dim() == 1 40 assert input.size(0) == target.size(0) 41 if weight is None: 42 weight = input.new_empty(0) 43 else:
40 assert input.size(0) == target.size(0) 41 if weight is None: 42 weight = input.new_empty(0) 43 else: 44 assert weight.dim() == 1 45 assert input.size(1) == weight.size(0) 46 ctx.reduction_dict = {'none': 0, 'mean': 1, 'sum': 2} 47 assert reduction in ctx.reduction_dict.keys()
41 if weight is None: 42 weight = input.new_empty(0) 43 else: 44 assert weight.dim() == 1 45 assert input.size(1) == weight.size(0) 46 ctx.reduction_dict = {'none': 0, 'mean': 1, 'sum': 2} 47 assert reduction in ctx.reduction_dict.keys() 48
43 else: 44 assert weight.dim() == 1 45 assert input.size(1) == weight.size(0) 46 ctx.reduction_dict = {'none': 0, 'mean': 1, 'sum': 2} 47 assert reduction in ctx.reduction_dict.keys() 48 49 ctx.gamma = float(gamma) 50 ctx.alpha = float(alpha)
127 alpha=0.25, 128 weight=None, 129 reduction='mean'): 130 131 assert isinstance(target, (torch.LongTensor, torch.cuda.LongTensor)) 132 assert input.dim() == 2 133 assert target.dim() == 1 134 assert input.size(0) == target.size(0)
128 weight=None, 129 reduction='mean'): 130 131 assert isinstance(target, (torch.LongTensor, torch.cuda.LongTensor)) 132 assert input.dim() == 2 133 assert target.dim() == 1 134 assert input.size(0) == target.size(0) 135 if weight is None:
129 reduction='mean'): 130 131 assert isinstance(target, (torch.LongTensor, torch.cuda.LongTensor)) 132 assert input.dim() == 2 133 assert target.dim() == 1 134 assert input.size(0) == target.size(0) 135 if weight is None: 136 weight = input.new_empty(0)
130 131 assert isinstance(target, (torch.LongTensor, torch.cuda.LongTensor)) 132 assert input.dim() == 2 133 assert target.dim() == 1 134 assert input.size(0) == target.size(0) 135 if weight is None: 136 weight = input.new_empty(0) 137 else:
134 assert input.size(0) == target.size(0) 135 if weight is None: 136 weight = input.new_empty(0) 137 else: 138 assert weight.dim() == 1 139 assert input.size(1) == weight.size(0) 140 ctx.reduction_dict = {'none': 0, 'mean': 1, 'sum': 2} 141 assert reduction in ctx.reduction_dict.keys()
135 if weight is None: 136 weight = input.new_empty(0) 137 else: 138 assert weight.dim() == 1 139 assert input.size(1) == weight.size(0) 140 ctx.reduction_dict = {'none': 0, 'mean': 1, 'sum': 2} 141 assert reduction in ctx.reduction_dict.keys() 142
137 else: 138 assert weight.dim() == 1 139 assert input.size(1) == weight.size(0) 140 ctx.reduction_dict = {'none': 0, 'mean': 1, 'sum': 2} 141 assert reduction in ctx.reduction_dict.keys() 142 143 ctx.gamma = float(gamma) 144 ctx.alpha = float(alpha)
23 24 Returns: 25 Tensor: (B, num_points) indices of the sampled points. 26 """ 27 assert points_xyz.is_contiguous() 28 29 B, N = points_xyz.size()[:2] 30 output = torch.cuda.IntTensor(B, num_points)
61 62 Returns: 63 Tensor: (B, num_points) indices of the sampled points. 64 """ 65 assert points_dist.is_contiguous() 66 67 B, N, _ = points_dist.size() 68 output = points_dist.new_zeros([B, num_points], dtype=torch.int32)
256 257 def bias_leakyrelu_ref(x, bias, negative_slope=0.2, scale=2**0.5): 258 259 if bias is not None: 260 assert bias.ndim == 1 261 assert bias.shape[0] == x.shape[1] 262 x = x + bias.reshape([-1 if i == 1 else 1 for i in range(x.ndim)]) 263
257 def bias_leakyrelu_ref(x, bias, negative_slope=0.2, scale=2**0.5): 258 259 if bias is not None: 260 assert bias.ndim == 1 261 assert bias.shape[0] == x.shape[1] 262 x = x + bias.reshape([-1 if i == 1 else 1 for i in range(x.ndim)]) 263 264 x = F.leaky_relu(x, negative_slope)
20 21 Returns: 22 Tensor: (B, C, M) where M is the number of points. 23 """ 24 assert features.is_contiguous() 25 assert indices.is_contiguous() 26 27 B, npoint = indices.size()
21 Returns: 22 Tensor: (B, C, M) where M is the number of points. 23 """ 24 assert features.is_contiguous() 25 assert indices.is_contiguous() 26 27 B, npoint = indices.size() 28 _, C, N = features.size()
56 self.uniform_sample = uniform_sample 57 self.return_unique_cnt = return_unique_cnt 58 self.return_grouped_idx = return_grouped_idx 59 if self.return_unique_cnt: 60 assert self.uniform_sample, \ 61 'uniform_sample should be True when ' \ 62 'returning the count of unique samples' 63 if self.max_radius is None: 64 assert not self.normalize_xyz, \ 65 'can not normalize grouped xyz when max_radius is None'
60 assert self.uniform_sample, \ 61 'uniform_sample should be True when ' \ 62 'returning the count of unique samples' 63 if self.max_radius is None: 64 assert not self.normalize_xyz, \ 65 'can not normalize grouped xyz when max_radius is None' 66 67 def forward(self, points_xyz, center_xyz, features=None): 68 """
113 dim=1) 114 else: 115 new_features = grouped_features 116 else: 117 assert (self.use_xyz 118 ), 'Cannot have not features and not use xyz as a feature!' 119 new_features = grouped_xyz_diff 120 121 ret = [new_features]
46 47 Returns: 48 torch.Tensor: Indexes after NMS. 49 """ 50 assert boxes.size(1) == 5, 'Input boxes shape should be [N, 5]' 51 order = scores.sort(0, descending=True)[1] 52 53 if pre_max_size is not None:
74 75 Returns: 76 torch.Tensor: Remaining indices with scores in descending order. 77 """ 78 assert boxes.shape[1] == 5, 'Input boxes shape should be [N, 5]' 79 order = scores.sort(0, descending=True)[1] 80 81 boxes = boxes[order].contiguous()
36 Returns: 37 Tensor: (B, k, npoint) tensor with the indices of 38 the features that form k-nearest neighbours. 39 """ 40 assert (k > 0) & (k < 100), 'k should be in range(0, 100)' 41 42 if center_xyz is None: 43 center_xyz = xyz
45 if transposed: 46 xyz = xyz.transpose(2, 1).contiguous() 47 center_xyz = center_xyz.transpose(2, 1).contiguous() 48 49 assert xyz.is_contiguous() # [B, N, 3] 50 assert center_xyz.is_contiguous() # [B, npoint, 3] 51 52 center_xyz_device = center_xyz.get_device()
46 xyz = xyz.transpose(2, 1).contiguous() 47 center_xyz = center_xyz.transpose(2, 1).contiguous() 48 49 assert xyz.is_contiguous() # [B, N, 3] 50 assert center_xyz.is_contiguous() # [B, npoint, 3] 51 52 center_xyz_device = center_xyz.get_device() 53 assert center_xyz_device == xyz.get_device(), \
49 assert xyz.is_contiguous() # [B, N, 3] 50 assert center_xyz.is_contiguous() # [B, npoint, 3] 51 52 center_xyz_device = center_xyz.get_device() 53 assert center_xyz_device == xyz.get_device(), \ 54 'center_xyz and xyz should be put on the same device' 55 if torch.cuda.current_device() != center_xyz_device: 56 torch.cuda.set_device(center_xyz_device) 57
27 stride_i=stride) 28 29 @staticmethod 30 def forward(ctx, features, mask, weight, bias, padding=0, stride=1): 31 assert mask.dim() == 3 and mask.size(0) == 1 32 assert features.dim() == 4 and features.size(0) == 1 33 assert features.size()[2:] == mask.size()[1:] 34 pad_h, pad_w = _pair(padding)
28 29 @staticmethod 30 def forward(ctx, features, mask, weight, bias, padding=0, stride=1): 31 assert mask.dim() == 3 and mask.size(0) == 1 32 assert features.dim() == 4 and features.size(0) == 1 33 assert features.size()[2:] == mask.size()[1:] 34 pad_h, pad_w = _pair(padding) 35 stride_h, stride_w = _pair(stride)
29 @staticmethod 30 def forward(ctx, features, mask, weight, bias, padding=0, stride=1): 31 assert mask.dim() == 3 and mask.size(0) == 1 32 assert features.dim() == 4 and features.size(0) == 1 33 assert features.size()[2:] == mask.size()[1:] 34 pad_h, pad_w = _pair(padding) 35 stride_h, stride_w = _pair(stride) 36 if stride_h != 1 or stride_w != 1:
53 input_conv_cfg=None, 54 input_norm_cfg=None, 55 upsample_mode='nearest'): 56 super(BaseMergeCell, self).__init__() 57 assert upsample_mode in ['nearest', 'bilinear'] 58 self.with_out_conv = with_out_conv 59 self.with_input1_conv = with_input1_conv 60 self.with_input2_conv = with_input2_conv
94 return x 95 elif x.shape[-2:] < size: 96 return F.interpolate(x, size=size, mode=self.upsample_mode) 97 else: 98 assert x.shape[-2] % size[-2] == 0 and x.shape[-1] % size[-1] == 0 99 kernel_size = x.shape[-1] // size[-1] 100 x = F.max_pool2d(x, kernel_size=kernel_size, stride=kernel_size) 101 return x
100 x = F.max_pool2d(x, kernel_size=kernel_size, stride=kernel_size) 101 return x 102 103 def forward(self, x1, x2, out_size=None): 104 assert x1.shape[:2] == x2.shape[:2] 105 assert out_size is None or len(out_size) == 2 106 if out_size is None: # resize to larger one 107 out_size = max(x1.size()[2:], x2.size()[2:])
101 return x 102 103 def forward(self, x1, x2, out_size=None): 104 assert x1.shape[:2] == x2.shape[:2] 105 assert out_size is None or len(out_size) == 2 106 if out_size is None: # resize to larger one 107 out_size = max(x1.size()[2:], x2.size()[2:]) 108
309 value = value.permute(1, 0, 2) 310 311 bs, num_query, _ = query.shape 312 bs, num_value, _ = value.shape 313 assert (spatial_shapes[:, 0] * spatial_shapes[:, 1]).sum() == num_value 314 315 value = self.value_proj(value) 316 if key_padding_mask is not None:
99 @staticmethod 100 def symbolic(g, boxes, scores, iou_threshold, sigma, min_score, method, 101 offset): 102 from packaging import version 103 assert version.parse(torch.__version__) >= version.parse('1.7.0') 104 nms_out = g.op( 105 'mmcv::SoftNonMaxSuppression', 106 boxes,
147 >>> iou_threshold = 0.6 148 >>> dets, inds = nms(boxes, scores, iou_threshold) 149 >>> assert len(inds) == len(dets) == 3 150 """ 151 assert isinstance(boxes, (torch.Tensor, np.ndarray)) 152 assert isinstance(scores, (torch.Tensor, np.ndarray)) 153 is_numpy = False 154 if isinstance(boxes, np.ndarray):
148 >>> dets, inds = nms(boxes, scores, iou_threshold) 149 >>> assert len(inds) == len(dets) == 3 150 """ 151 assert isinstance(boxes, (torch.Tensor, np.ndarray)) 152 assert isinstance(scores, (torch.Tensor, np.ndarray)) 153 is_numpy = False 154 if isinstance(boxes, np.ndarray): 155 is_numpy = True
155 is_numpy = True 156 boxes = torch.from_numpy(boxes) 157 if isinstance(scores, np.ndarray): 158 scores = torch.from_numpy(scores) 159 assert boxes.size(1) == 4 160 assert boxes.size(0) == scores.size(0) 161 assert offset in (0, 1) 162
156 boxes = torch.from_numpy(boxes) 157 if isinstance(scores, np.ndarray): 158 scores = torch.from_numpy(scores) 159 assert boxes.size(1) == 4 160 assert boxes.size(0) == scores.size(0) 161 assert offset in (0, 1) 162 163 if torch.__version__ == 'parrots':
157 if isinstance(scores, np.ndarray): 158 scores = torch.from_numpy(scores) 159 assert boxes.size(1) == 4 160 assert boxes.size(0) == scores.size(0) 161 assert offset in (0, 1) 162 163 if torch.__version__ == 'parrots': 164 indata_list = [boxes, scores]
215 >>> dets, inds = soft_nms(boxes, scores, iou_threshold, sigma=0.5) 216 >>> assert len(inds) == len(dets) == 5 217 """ 218 219 assert isinstance(boxes, (torch.Tensor, np.ndarray)) 220 assert isinstance(scores, (torch.Tensor, np.ndarray)) 221 is_numpy = False 222 if isinstance(boxes, np.ndarray):
216 >>> assert len(inds) == len(dets) == 5 217 """ 218 219 assert isinstance(boxes, (torch.Tensor, np.ndarray)) 220 assert isinstance(scores, (torch.Tensor, np.ndarray)) 221 is_numpy = False 222 if isinstance(boxes, np.ndarray): 223 is_numpy = True
223 is_numpy = True 224 boxes = torch.from_numpy(boxes) 225 if isinstance(scores, np.ndarray): 226 scores = torch.from_numpy(scores) 227 assert boxes.size(1) == 4 228 assert boxes.size(0) == scores.size(0) 229 assert offset in (0, 1) 230 method_dict = {'naive': 0, 'linear': 1, 'gaussian': 2}
224 boxes = torch.from_numpy(boxes) 225 if isinstance(scores, np.ndarray): 226 scores = torch.from_numpy(scores) 227 assert boxes.size(1) == 4 228 assert boxes.size(0) == scores.size(0) 229 assert offset in (0, 1) 230 method_dict = {'naive': 0, 'linear': 1, 'gaussian': 2} 231 assert method in method_dict.keys()
225 if isinstance(scores, np.ndarray): 226 scores = torch.from_numpy(scores) 227 assert boxes.size(1) == 4 228 assert boxes.size(0) == scores.size(0) 229 assert offset in (0, 1) 230 method_dict = {'naive': 0, 'linear': 1, 'gaussian': 2} 231 assert method in method_dict.keys() 232
227 assert boxes.size(1) == 4 228 assert boxes.size(0) == scores.size(0) 229 assert offset in (0, 1) 230 method_dict = {'naive': 0, 'linear': 1, 'gaussian': 2} 231 assert method in method_dict.keys() 232 233 if torch.__version__ == 'parrots': 234 dets = boxes.new_empty((boxes.size(0), 5), device='cpu')
298 offsets = idxs.to(boxes) * (max_coordinate + torch.tensor(1).to(boxes)) 299 boxes_for_nms = boxes + offsets[:, None] 300 301 nms_type = nms_cfg_.pop('type', 'nms') 302 nms_op = eval(nms_type) 303 304 split_thr = nms_cfg_.pop('split_thr', 10000) 305 # Won't split to multiple nms nodes when exporting to onnx
354 """ 355 if dets.shape[0] == 0: 356 matched = [] 357 else: 358 assert dets.shape[-1] == 5, 'inputs dets.shape should be (N, 5), ' \ 359 f'but get {dets.shape}' 360 if isinstance(dets, torch.Tensor): 361 dets_t = dets.detach().cpu() 362 else:
28 pixel_assignment (List[List[float]]): The instance coordinate list. 29 Each element consists of averaged confidence, pixel number, and 30 coordinates (x_i, y_i for all pixels) in order. 31 """ 32 assert isinstance(score, (torch.Tensor, np.ndarray)) 33 assert isinstance(mask, (torch.Tensor, np.ndarray)) 34 assert isinstance(embedding, (torch.Tensor, np.ndarray)) 35 assert isinstance(kernel_label, (torch.Tensor, np.ndarray))
29 Each element consists of averaged confidence, pixel number, and 30 coordinates (x_i, y_i for all pixels) in order. 31 """ 32 assert isinstance(score, (torch.Tensor, np.ndarray)) 33 assert isinstance(mask, (torch.Tensor, np.ndarray)) 34 assert isinstance(embedding, (torch.Tensor, np.ndarray)) 35 assert isinstance(kernel_label, (torch.Tensor, np.ndarray)) 36 assert isinstance(kernel_contour, (torch.Tensor, np.ndarray))
30 coordinates (x_i, y_i for all pixels) in order. 31 """ 32 assert isinstance(score, (torch.Tensor, np.ndarray)) 33 assert isinstance(mask, (torch.Tensor, np.ndarray)) 34 assert isinstance(embedding, (torch.Tensor, np.ndarray)) 35 assert isinstance(kernel_label, (torch.Tensor, np.ndarray)) 36 assert isinstance(kernel_contour, (torch.Tensor, np.ndarray)) 37 assert isinstance(kernel_region_num, int)
31 """ 32 assert isinstance(score, (torch.Tensor, np.ndarray)) 33 assert isinstance(mask, (torch.Tensor, np.ndarray)) 34 assert isinstance(embedding, (torch.Tensor, np.ndarray)) 35 assert isinstance(kernel_label, (torch.Tensor, np.ndarray)) 36 assert isinstance(kernel_contour, (torch.Tensor, np.ndarray)) 37 assert isinstance(kernel_region_num, int) 38 assert isinstance(distance_threshold, float)
32 assert isinstance(score, (torch.Tensor, np.ndarray)) 33 assert isinstance(mask, (torch.Tensor, np.ndarray)) 34 assert isinstance(embedding, (torch.Tensor, np.ndarray)) 35 assert isinstance(kernel_label, (torch.Tensor, np.ndarray)) 36 assert isinstance(kernel_contour, (torch.Tensor, np.ndarray)) 37 assert isinstance(kernel_region_num, int) 38 assert isinstance(distance_threshold, float) 39
33 assert isinstance(mask, (torch.Tensor, np.ndarray)) 34 assert isinstance(embedding, (torch.Tensor, np.ndarray)) 35 assert isinstance(kernel_label, (torch.Tensor, np.ndarray)) 36 assert isinstance(kernel_contour, (torch.Tensor, np.ndarray)) 37 assert isinstance(kernel_region_num, int) 38 assert isinstance(distance_threshold, float) 39 40 if isinstance(score, np.ndarray):
34 assert isinstance(embedding, (torch.Tensor, np.ndarray)) 35 assert isinstance(kernel_label, (torch.Tensor, np.ndarray)) 36 assert isinstance(kernel_contour, (torch.Tensor, np.ndarray)) 37 assert isinstance(kernel_region_num, int) 38 assert isinstance(distance_threshold, float) 39 40 if isinstance(score, np.ndarray): 41 score = torch.from_numpy(score)
26 torch.Tensor: A tensor with sampled points, shape (N, C, Hg, Wg) 27 """ 28 n, c, h, w = im.shape 29 gn, gh, gw, _ = grid.shape 30 assert n == gn 31 32 x = grid[:, :, :, 0] 33 y = grid[:, :, :, 1]
146 Tensor: Image based absolute point coordinates, shape (N, P, 2) 147 """ 148 149 with torch.no_grad(): 150 assert rel_roi_points.size(0) == rois.size(0) 151 assert rois.dim() == 2 152 assert rel_roi_points.dim() == 3 153 assert rel_roi_points.size(2) == 2
147 """ 148 149 with torch.no_grad(): 150 assert rel_roi_points.size(0) == rois.size(0) 151 assert rois.dim() == 2 152 assert rel_roi_points.dim() == 3 153 assert rel_roi_points.size(2) == 2 154 # remove batch idx
148 149 with torch.no_grad(): 150 assert rel_roi_points.size(0) == rois.size(0) 151 assert rois.dim() == 2 152 assert rel_roi_points.dim() == 3 153 assert rel_roi_points.size(2) == 2 154 # remove batch idx 155 if rois.size(1) == 5:
149 with torch.no_grad(): 150 assert rel_roi_points.size(0) == rois.size(0) 151 assert rois.dim() == 2 152 assert rel_roi_points.dim() == 3 153 assert rel_roi_points.size(2) == 2 154 # remove batch idx 155 if rois.size(1) == 5: 156 rois = rois[:, 1:]
197 Tensor: Image based relative point coordinates for sampling, 198 shape (N, P, 2) 199 """ 200 201 assert (isinstance(img, tuple) and len(img) == 2) or \ 202 (isinstance(img, torch.Tensor) and len(img.shape) == 4) 203 204 if isinstance(img, tuple): 205 h, w = img
19 20 Returns: 21 box_idxs_of_pts (torch.Tensor): (B, M), default background = -1 22 """ 23 assert points.shape[0] == boxes.shape[0], \ 24 'Points and boxes should have the same batch size, ' \ 25 f'but got {points.shape[0]} and {boxes.shape[0]}' 26 assert boxes.shape[2] == 7, \ 27 'boxes dimension should be 7, ' \ 28 f'but got unexpected shape {boxes.shape[2]}'
22 """ 23 assert points.shape[0] == boxes.shape[0], \ 24 'Points and boxes should have the same batch size, ' \ 25 f'but got {points.shape[0]} and {boxes.shape[0]}' 26 assert boxes.shape[2] == 7, \ 27 'boxes dimension should be 7, ' \ 28 f'but got unexpected shape {boxes.shape[2]}' 29 assert points.shape[2] == 3, \ 30 'points dimension should be 3, ' \ 31 f'but got unexpected shape {points.shape[2]}'
25 f'but got {points.shape[0]} and {boxes.shape[0]}' 26 assert boxes.shape[2] == 7, \ 27 'boxes dimension should be 7, ' \ 28 f'but got unexpected shape {boxes.shape[2]}' 29 assert points.shape[2] == 3, \ 30 'points dimension should be 3, ' \ 31 f'but got unexpected shape {points.shape[2]}' 32 batch_size, num_points, _ = points.shape 33 34 box_idxs_of_pts = points.new_zeros((batch_size, num_points),
42 # as the device of the tensors if it was not. 43 # Please refer to https://github.com/open-mmlab/mmdetection3d/issues/305 44 # for the incorrect output before the fix. 45 points_device = points.get_device() 46 assert points_device == boxes.get_device(), \ 47 'Points and boxes should be put on the same device' 48 if torch.cuda.current_device() != points_device: 49 torch.cuda.set_device(points_device) 50
68 69 Returns: 70 box_idxs_of_pts (torch.Tensor): (B, M, T), default background = 0. 71 """ 72 assert points.shape[0] == boxes.shape[0], \ 73 'Points and boxes should have the same batch size, ' \ 74 f'but got {points.shape[0]} and {boxes.shape[0]}' 75 assert boxes.shape[2] == 7, \ 76 'boxes dimension should be 7, ' \ 77 f'but got unexpected shape {boxes.shape[2]}'
71 """ 72 assert points.shape[0] == boxes.shape[0], \ 73 'Points and boxes should have the same batch size, ' \ 74 f'but got {points.shape[0]} and {boxes.shape[0]}' 75 assert boxes.shape[2] == 7, \ 76 'boxes dimension should be 7, ' \ 77 f'but got unexpected shape {boxes.shape[2]}' 78 assert points.shape[2] == 3, \ 79 'points dimension should be 3, ' \ 80 f'but got unexpected shape {points.shape[2]}'
74 f'but got {points.shape[0]} and {boxes.shape[0]}' 75 assert boxes.shape[2] == 7, \ 76 'boxes dimension should be 7, ' \ 77 f'but got unexpected shape {boxes.shape[2]}' 78 assert points.shape[2] == 3, \ 79 'points dimension should be 3, ' \ 80 f'but got unexpected shape {points.shape[2]}' 81 batch_size, num_points, _ = points.shape 82 num_boxes = boxes.shape[1] 83
103 104 Returns: 105 box_idxs_of_pts (torch.Tensor): (B, M, T), default background = 0. 106 """ 107 assert boxes.shape[0] == points.shape[0], \ 108 'Points and boxes should have the same batch size, ' \ 109 f'but got {boxes.shape[0]} and {boxes.shape[0]}' 110 assert boxes.shape[2] == 7, \ 111 'boxes dimension should be 7, ' \ 112 f'but got unexpected shape {boxes.shape[2]}'
106 """ 107 assert boxes.shape[0] == points.shape[0], \ 108 'Points and boxes should have the same batch size, ' \ 109 f'but got {boxes.shape[0]} and {boxes.shape[0]}' 110 assert boxes.shape[2] == 7, \ 111 'boxes dimension should be 7, ' \ 112 f'but got unexpected shape {boxes.shape[2]}' 113 assert points.shape[2] == 3, \ 114 'points dimension should be 3, ' \ 115 f'but got unexpected shape {points.shape[2]}'
109 f'but got {boxes.shape[0]} and {boxes.shape[0]}' 110 assert boxes.shape[2] == 7, \ 111 'boxes dimension should be 7, ' \ 112 f'but got unexpected shape {boxes.shape[2]}' 113 assert points.shape[2] == 3, \ 114 'points dimension should be 3, ' \ 115 f'but got unexpected shape {points.shape[2]}' 116 batch_size, num_points, _ = points.shape 117 num_boxes = boxes.shape[1] 118
120 dtype=torch.int).fill_(0) 121 122 # Same reason as line 25-32 123 points_device = points.get_device() 124 assert points_device == boxes.get_device(), \ 125 'Points and boxes should be put on the same device' 126 if torch.cuda.current_device() != points_device: 127 torch.cuda.set_device(points_device) 128
78 super().__init__() 79 # FPS would be applied to different fps_mod in the list, 80 # so the length of the num_point should be equal to 81 # fps_mod_list and fps_sample_range_list. 82 assert len(num_point) == len(fps_mod_list) == len( 83 fps_sample_range_list) 84 self.num_point = num_point 85 self.fps_sample_range_list = fps_sample_range_list 86 self.samplers = nn.ModuleList()
102 last_fps_end_index = 0 103 104 for fps_sample_range, sampler, npoint in zip( 105 self.fps_sample_range_list, self.samplers, self.num_point): 106 assert fps_sample_range < points_xyz.shape[1] 107 108 if fps_sample_range == -1: 109 sample_points_xyz = points_xyz[:, last_fps_end_index:]
149 super().__init__() 150 151 def forward(self, points, features, npoint): 152 """Sampling points with F-FPS.""" 153 assert features is not None, \ 154 'feature input to FFPS_Sampler should not be None' 155 features_for_fps = torch.cat([points, features.transpose(1, 2)], dim=2) 156 features_dist = calc_square_dist( 157 features_for_fps, features_for_fps, norm=False)
166 super().__init__() 167 168 def forward(self, points, features, npoint): 169 """Sampling points with FS_Sampling.""" 170 assert features is not None, \ 171 'feature input to FS_Sampler should not be None' 172 ffps_sampler = FFPSSampler() 173 dfps_sampler = DFPSSampler() 174 fps_idx_ffps = ffps_sampler(points, features, npoint)
26 ctx.save_for_backward(input) 27 28 h_mask, w_mask = ctx.mask_size 29 batch_size, channels, h_feature, w_feature = input.size() 30 assert channels == h_mask * w_mask 31 output = input.new_zeros( 32 (batch_size, h_feature * w_feature, h_feature, w_feature)) 33
72 class PSAMask(nn.Module): 73 74 def __init__(self, psa_type, mask_size=None): 75 super(PSAMask, self).__init__() 76 assert psa_type in ['collect', 'distribute'] 77 if psa_type == 'collect': 78 psa_type_enum = 0 79 else:
71 aligned=True): 72 ctx.output_size = _pair(output_size) 73 ctx.spatial_scale = spatial_scale 74 ctx.sampling_ratio = sampling_ratio 75 assert pool_mode in ('max', 'avg') 76 ctx.pool_mode = 0 if pool_mode == 'max' else 1 77 ctx.aligned = aligned 78 ctx.input_shape = input.size()
76 ctx.pool_mode = 0 if pool_mode == 'max' else 1 77 ctx.aligned = aligned 78 ctx.input_shape = input.size() 79 80 assert rois.size(1) == 5, 'RoI must be (idx, x1, y1, x2, y2)!' 81 82 output_shape = (rois.size(0), input.size(1), ctx.output_size[0], 83 ctx.output_size[1])
16 if isinstance(out_size, int): 17 out_h = out_size 18 out_w = out_size 19 elif isinstance(out_size, tuple): 20 assert len(out_size) == 2 21 assert isinstance(out_size[0], int) 22 assert isinstance(out_size[1], int) 23 out_h, out_w = out_size
17 out_h = out_size 18 out_w = out_size 19 elif isinstance(out_size, tuple): 20 assert len(out_size) == 2 21 assert isinstance(out_size[0], int) 22 assert isinstance(out_size[1], int) 23 out_h, out_w = out_size 24 else:
18 out_w = out_size 19 elif isinstance(out_size, tuple): 20 assert len(out_size) == 2 21 assert isinstance(out_size[0], int) 22 assert isinstance(out_size[1], int) 23 out_h, out_w = out_size 24 else: 25 raise TypeError(
47 if isinstance(out_size, int): 48 out_h = out_size 49 out_w = out_size 50 elif isinstance(out_size, tuple): 51 assert len(out_size) == 2 52 assert isinstance(out_size[0], int) 53 assert isinstance(out_size[1], int) 54 out_h, out_w = out_size
48 out_h = out_size 49 out_w = out_size 50 elif isinstance(out_size, tuple): 51 assert len(out_size) == 2 52 assert isinstance(out_size[0], int) 53 assert isinstance(out_size[1], int) 54 out_h, out_w = out_size 55 else:
49 out_w = out_size 50 elif isinstance(out_size, tuple): 51 assert len(out_size) == 2 52 assert isinstance(out_size[0], int) 53 assert isinstance(out_size[1], int) 54 out_h, out_w = out_size 55 else: 56 raise TypeError(
85 aligned = ctx.aligned 86 clockwise = ctx.clockwise 87 sample_num = ctx.sample_num 88 rois = ctx.saved_tensors[0] 89 assert feature_size is not None 90 batch_size, num_channels, data_height, data_width = feature_size 91 92 out_w = grad_output.size(3)
27 ctx.output_size = _pair(output_size) 28 ctx.spatial_scale = spatial_scale 29 ctx.input_shape = input.size() 30 31 assert rois.size(1) == 5, 'RoI must be (idx, x1, y1, x2, y2)!' 32 33 output_shape = (rois.size(0), input.size(1), ctx.output_size[0], 34 ctx.output_size[1])
29 super().__init__() 30 31 self.out_size = out_size 32 self.max_pts_per_voxel = max_pts_per_voxel 33 assert mode in ['max', 'avg'] 34 pool_mapping = {'max': 0, 'avg': 1} 35 self.mode = pool_mapping[mode] 36
76 77 if isinstance(out_size, int): 78 out_x = out_y = out_z = out_size 79 else: 80 assert len(out_size) == 3 81 assert mmcv.is_tuple_of(out_size, int) 82 out_x, out_y, out_z = out_size 83
77 if isinstance(out_size, int): 78 out_x = out_y = out_z = out_size 79 else: 80 assert len(out_size) == 3 81 assert mmcv.is_tuple_of(out_size, int) 82 out_x, out_y, out_z = out_size 83 84 num_rois = rois.shape[0]
55 pooled_features (torch.Tensor): The output pooled features whose 56 shape is (B, M, 512, 3 + C). 57 pooled_empty_flag (torch.Tensor): Empty flag whose shape is (B, M). 58 """ 59 assert len(points.shape) == 3 and points.shape[2] == 3 60 batch_size, boxes_num, feature_len = points.shape[0], boxes3d.shape[ 61 1], point_features.shape[2] 62 pooled_boxes3d = boxes3d.view(batch_size, -1, 7)
42 self.group = group 43 self.group_size = group_size 44 self.stats_mode = stats_mode 45 46 assert isinstance( 47 input, (torch.HalfTensor, torch.FloatTensor, 48 torch.cuda.HalfTensor, torch.cuda.FloatTensor)), \ 49 f'only support Half or Float Tensor, but {input.type()}' 50 output = torch.zeros_like(input) 51 input3d = input.flatten(start_dim=2) 52 output3d = output.view_as(input3d)
206 self.track_running_stats = track_running_stats 207 group = dist.group.WORLD if group is None else group 208 self.group = group 209 self.group_size = dist.get_world_size(group) 210 assert stats_mode in ['default', 'N'], \ 211 f'"stats_mode" only accepts "default" and "N", got "{stats_mode}"' 212 self.stats_mode = stats_mode 213 if self.affine: 214 self.weight = Parameter(torch.Tensor(num_features))
29 30 Returns: 31 Tensor: (B, C, N) tensor of the interpolated features 32 """ 33 assert features.is_contiguous() 34 assert indices.is_contiguous() 35 assert weight.is_contiguous() 36
30 Returns: 31 Tensor: (B, C, N) tensor of the interpolated features 32 """ 33 assert features.is_contiguous() 34 assert indices.is_contiguous() 35 assert weight.is_contiguous() 36 37 B, c, m = features.size()
31 Tensor: (B, C, N) tensor of the interpolated features 32 """ 33 assert features.is_contiguous() 34 assert indices.is_contiguous() 35 assert weight.is_contiguous() 36 37 B, c, m = features.size() 38 n = indices.size(1)
32 return DataContainer( 33 stacked, batch[0].stack, batch[0].padding_value, cpu_only=True) 34 elif batch[0].stack: 35 for i in range(0, len(batch), samples_per_gpu): 36 assert isinstance(batch[i].data, torch.Tensor) 37 38 if batch[i].pad_dims is not None: 39 ndim = batch[i].dim()
36 assert isinstance(batch[i].data, torch.Tensor) 37 38 if batch[i].pad_dims is not None: 39 ndim = batch[i].dim() 40 assert ndim > batch[i].pad_dims 41 max_shape = [0 for _ in range(batch[i].pad_dims)] 42 for dim in range(1, batch[i].pad_dims + 1): 43 max_shape[dim - 1] = batch[i].size(-dim)
42 for dim in range(1, batch[i].pad_dims + 1): 43 max_shape[dim - 1] = batch[i].size(-dim) 44 for sample in batch[i:i + samples_per_gpu]: 45 for dim in range(0, ndim - batch[i].pad_dims): 46 assert batch[i].size(dim) == sample.size(dim) 47 for dim in range(1, batch[i].pad_dims + 1): 48 max_shape[dim - 1] = max(max_shape[dim - 1], 49 sample.size(-dim))
43 self._data = data 44 self._cpu_only = cpu_only 45 self._stack = stack 46 self._padding_value = padding_value 47 assert pad_dims in [None, 1, 2, 3] 48 self._pad_dims = pad_dims 49 50 def __repr__(self):
50 # convert data containers as those in GPU inference 51 inputs, kwargs = self.scatter(inputs, kwargs, [-1]) 52 return self.module.train_step(*inputs[0], **kwargs[0]) 53 54 assert len(self.device_ids) == 1, \ 55 ('MMDataParallel only supports single GPU training, if you need to' 56 ' train with multiple GPUs, please use MMDistributedDataParallel' 57 'instead.') 58 59 for t in chain(self.module.parameters(), self.module.buffers()): 60 if t.device != self.src_device_obj:
72 # convert data containers as those in GPU inference 73 inputs, kwargs = self.scatter(inputs, kwargs, [-1]) 74 return self.module.val_step(*inputs[0], **kwargs[0]) 75 76 assert len(self.device_ids) == 1, \ 77 ('MMDataParallel only supports single GPU training, if you need to' 78 ' train with multiple GPUs, please use MMDistributedDataParallel' 79 ' instead.') 80 81 for t in chain(self.module.parameters(), self.module.buffers()): 82 if t.device != self.src_device_obj:
73 raise RuntimeError( 74 'batch_processor and model.train_step()/model.val_step() ' 75 'cannot be both available.') 76 else: 77 assert hasattr(model, 'train_step') 78 79 # check the type of `optimizer` 80 if isinstance(optimizer, dict):
264 hook (:obj:`Hook`): The hook to be registered. 265 priority (int or str or :obj:`Priority`): Hook priority. 266 Lower value means higher priority. 267 """ 268 assert isinstance(hook, Hook) 269 if hasattr(hook, 'priority'): 270 raise ValueError('"priority" is a reserved attribute for hooks') 271 priority = get_priority(priority)
399 def register_lr_hook(self, lr_config): 400 if lr_config is None: 401 return 402 elif isinstance(lr_config, dict): 403 assert 'policy' in lr_config 404 policy_type = lr_config.pop('policy') 405 # If the type of policy is all in lower case, e.g., 'cyclic', 406 # then its first letter will be capitalized, e.g., to be 'Cyclic'.
420 def register_momentum_hook(self, momentum_config): 421 if momentum_config is None: 422 return 423 if isinstance(momentum_config, dict): 424 assert 'policy' in momentum_config 425 policy_type = momentum_config.pop('policy') 426 # If the type of policy is all in lower case, e.g., 'cyclic', 427 # then its first letter will be capitalized, e.g., to be 'Cyclic'.
121 def get_external_models(): 122 mmcv_home = _get_mmcv_home() 123 default_json_path = osp.join(mmcv.__path__[0], 'model_zoo/open_mmlab.json') 124 default_urls = load_file(default_json_path) 125 assert isinstance(default_urls, dict) 126 external_json_path = osp.join(mmcv_home, 'open_mmlab.json') 127 if osp.exists(external_json_path): 128 external_urls = load_file(external_json_path)
125 assert isinstance(default_urls, dict) 126 external_json_path = osp.join(mmcv_home, 'open_mmlab.json') 127 if osp.exists(external_json_path): 128 external_urls = load_file(external_json_path) 129 assert isinstance(external_urls, dict) 130 default_urls.update(external_urls) 131 132 return default_urls
142 def get_deprecated_model_names(): 143 deprecate_json_path = osp.join(mmcv.__path__[0], 144 'model_zoo/deprecated.json') 145 deprecate_urls = load_file(deprecate_json_path) 146 assert isinstance(deprecate_urls, dict) 147 148 return deprecate_urls 149
168 def _register_scheme(cls, prefixes, loader, force=False): 169 if isinstance(prefixes, str): 170 prefixes = [prefixes] 171 else: 172 assert isinstance(prefixes, (list, tuple)) 173 for prefix in prefixes: 174 if (prefix not in cls._schemes) or force: 175 cls._schemes[prefix] = loader
304 305 Returns: 306 dict or OrderedDict: The loaded checkpoint. 307 """ 308 assert filename.startswith('pavi://'), \ 309 f'Expected filename startswith `pavi://`, but get {filename}' 310 model_path = filename[7:] 311 312 try:
495 k[prefix_len:]: v 496 for k, v in state_dict.items() if k.startswith(prefix) 497 } 498 499 assert state_dict, f'{prefix} is not in the pretrained model' 500 return state_dict 501 502
1 # Copyright (c) OpenMMLab. All rights reserved. 2 import functools 3 import os 4 import subprocess 5 from collections import OrderedDict 6 7 import torch 8 import torch.multiprocessing as mp
55 ntasks = int(os.environ['SLURM_NTASKS']) 56 node_list = os.environ['SLURM_NODELIST'] 57 num_gpus = torch.cuda.device_count() 58 torch.cuda.set_device(proc_id % num_gpus) 59 addr = subprocess.getoutput( 60 f'scontrol show hostname {node_list} | head -n1') 61 # specify master port 62 if port is not None: 63 os.environ['MASTER_PORT'] = str(port)
79 running order and epochs. E.g, [('train', 2), ('val', 1)] means 80 running 2 epochs for training and 1 epoch for validation, 81 iteratively. 82 """ 83 assert isinstance(data_loaders, list) 84 assert mmcv.is_list_of(workflow, tuple) 85 assert len(data_loaders) == len(workflow) 86 if max_epochs is not None:
80 running 2 epochs for training and 1 epoch for validation, 81 iteratively. 82 """ 83 assert isinstance(data_loaders, list) 84 assert mmcv.is_list_of(workflow, tuple) 85 assert len(data_loaders) == len(workflow) 86 if max_epochs is not None: 87 warnings.warn(
81 iteratively. 82 """ 83 assert isinstance(data_loaders, list) 84 assert mmcv.is_list_of(workflow, tuple) 85 assert len(data_loaders) == len(workflow) 86 if max_epochs is not None: 87 warnings.warn( 88 'setting max_epochs in run is deprecated, '
88 'setting max_epochs in run is deprecated, ' 89 'please set max_epochs in runner_config', DeprecationWarning) 90 self._max_epochs = max_epochs 91 92 assert self._max_epochs is not None, ( 93 'max_epochs must be specified during instantiation') 94 95 for i, flow in enumerate(workflow): 96 mode, epochs = flow
338 scale_factor=2., 339 scale_window=1000): 340 self.cur_scale = init_scale 341 self.cur_iter = 0 342 assert mode in ('dynamic', 343 'static'), 'mode can only be dynamic or static' 344 self.mode = mode 345 self.last_overflow_iter = -1 346 self.scale_factor = scale_factor
5 @HOOKS.register_module() 6 class ClosureHook(Hook): 7 8 def __init__(self, fn_name, fn): 9 assert hasattr(self, fn_name) 10 assert callable(fn) 11 setattr(self, fn_name, fn)
6 class ClosureHook(Hook): 7 8 def __init__(self, fn_name, fn): 9 assert hasattr(self, fn_name) 10 assert callable(fn) 11 setattr(self, fn_name, fn)
30 momentum=0.0002, 31 interval=1, 32 warm_up=100, 33 resume_from=None): 34 assert isinstance(interval, int) and interval > 0 35 self.warm_up = warm_up 36 self.interval = interval 37 assert momentum > 0 and momentum < 1
33 resume_from=None): 34 assert isinstance(interval, int) and interval > 0 35 self.warm_up = warm_up 36 self.interval = interval 37 assert momentum > 0 and momentum < 1 38 self.momentum = momentum**interval 39 self.checkpoint = resume_from 40
102 if interval <= 0: 103 raise ValueError(f'interval must be a positive number, ' 104 f'but got {interval}') 105 106 assert isinstance(by_epoch, bool), '``by_epoch`` should be a boolean' 107 108 if start is not None and start < 0: 109 raise ValueError(f'The evaluation start epoch {start} is smaller '
113 self.interval = interval 114 self.start = start 115 self.by_epoch = by_epoch 116 117 assert isinstance(save_best, str) or save_best is None, \ 118 '""save_best"" should be a str or None ' \ 119 f'rather than {type(save_best)}' 120 self.save_best = save_best 121 self.eval_kwargs = eval_kwargs 122 self.initial_flag = True
131 self.greater_keys = self._default_greater_keys 132 else: 133 if not isinstance(greater_keys, (list, tuple)): 134 greater_keys = (greater_keys, ) 135 assert is_seq_of(greater_keys, str) 136 self.greater_keys = greater_keys 137 138 if less_keys is None:
139 self.less_keys = self._default_less_keys 140 else: 141 if not isinstance(less_keys, (list, tuple)): 142 less_keys = (less_keys, ) 143 assert is_seq_of(less_keys, str) 144 self.less_keys = less_keys 145 146 if self.save_best is not None:
48 self.init_kwargs['model'] = runner._model_name 49 if runner.meta is not None: 50 if 'config_dict' in runner.meta: 51 config_dict = runner.meta['config_dict'] 52 assert isinstance( 53 config_dict, 54 dict), ('meta["config_dict"] has to be of a dict, ' 55 f'but got {type(config_dict)}') 56 elif 'config_file' in runner.meta: 57 config_file = runner.meta['config_file'] 58 config_dict = dict(mmcv.Config.fromfile(config_file))
217 cur_lr = runner.current_lr() 218 if isinstance(cur_lr, list): 219 log_dict['lr'] = cur_lr[0] 220 else: 221 assert isinstance(cur_lr, dict) 222 log_dict['lr'] = {} 223 for k, lr_ in cur_lr.items(): 224 assert isinstance(lr_, list)
220 else: 221 assert isinstance(cur_lr, dict) 222 log_dict['lr'] = {} 223 for k, lr_ in cur_lr.items(): 224 assert isinstance(lr_, list) 225 log_dict['lr'].update({k: lr_[0]}) 226 227 if 'time' in runner.log_buffer.output:
34 raise ValueError( 35 f'"{warmup}" is not a supported type for warming up, valid' 36 ' types are "constant" and "linear"') 37 if warmup is not None: 38 assert warmup_iters > 0, \ 39 '"warmup_iters" must be a positive integer' 40 assert 0 < warmup_ratio <= 1.0, \ 41 '"warmup_ratio" must be in range (0,1]' 42
36 ' types are "constant" and "linear"') 37 if warmup is not None: 38 assert warmup_iters > 0, \ 39 '"warmup_iters" must be a positive integer' 40 assert 0 < warmup_ratio <= 1.0, \ 41 '"warmup_ratio" must be in range (0,1]' 42 43 self.by_epoch = by_epoch 44 self.warmup = warmup
178 """ 179 180 def __init__(self, step, gamma=0.1, min_lr=None, **kwargs): 181 if isinstance(step, list): 182 assert mmcv.is_list_of(step, int) 183 assert all([s > 0 for s in step]) 184 elif isinstance(step, int): 185 assert step > 0
179 180 def __init__(self, step, gamma=0.1, min_lr=None, **kwargs): 181 if isinstance(step, list): 182 assert mmcv.is_list_of(step, int) 183 assert all([s > 0 for s in step]) 184 elif isinstance(step, int): 185 assert step > 0 186 else:
181 if isinstance(step, list): 182 assert mmcv.is_list_of(step, int) 183 assert all([s > 0 for s in step]) 184 elif isinstance(step, int): 185 assert step > 0 186 else: 187 raise TypeError('"step" must be a list or integer') 188 self.step = step
257 @HOOKS.register_module() 258 class CosineAnnealingLrUpdaterHook(LrUpdaterHook): 259 260 def __init__(self, min_lr=None, min_lr_ratio=None, **kwargs): 261 assert (min_lr is None) ^ (min_lr_ratio is None) 262 self.min_lr = min_lr 263 self.min_lr_ratio = min_lr_ratio 264 super(CosineAnnealingLrUpdaterHook, self).__init__(**kwargs)
299 start_percent=0.75, 300 min_lr=None, 301 min_lr_ratio=None, 302 **kwargs): 303 assert (min_lr is None) ^ (min_lr_ratio is None) 304 if start_percent < 0 or start_percent > 1 or not isinstance( 305 start_percent, float): 306 raise ValueError(
351 restart_weights=[1], 352 min_lr=None, 353 min_lr_ratio=None, 354 **kwargs): 355 assert (min_lr is None) ^ (min_lr_ratio is None) 356 self.periods = periods 357 self.min_lr = min_lr 358 self.min_lr_ratio = min_lr_ratio
356 self.periods = periods 357 self.min_lr = min_lr 358 self.min_lr_ratio = min_lr_ratio 359 self.restart_weights = restart_weights 360 assert (len(self.periods) == len(self.restart_weights) 361 ), 'periods and restart_weights should have the same length.' 362 super(CosineRestartLrUpdaterHook, self).__init__(**kwargs) 363 364 self.cumulative_periods = [
446 else: 447 raise ValueError('target_ratio should be either float ' 448 f'or tuple, got {type(target_ratio)}') 449 450 assert len(target_ratio) == 2, \ 451 '"target_ratio" must be list or tuple of two floats' 452 assert 0 <= step_ratio_up < 1.0, \ 453 '"step_ratio_up" must be in range [0,1)' 454
448 f'or tuple, got {type(target_ratio)}') 449 450 assert len(target_ratio) == 2, \ 451 '"target_ratio" must be list or tuple of two floats' 452 assert 0 <= step_ratio_up < 1.0, \ 453 '"step_ratio_up" must be in range [0,1)' 454 455 self.target_ratio = target_ratio 456 self.cyclic_times = cyclic_times
464 self.anneal_func = annealing_cos 465 elif anneal_strategy == 'linear': 466 self.anneal_func = annealing_linear 467 468 assert not by_epoch, \ 469 'currently only support "by_epoch" = False' 470 super(CyclicLrUpdaterHook, self).__init__(by_epoch, **kwargs) 471 472 def before_run(self, runner):
541 # validate by_epoch, currently only support by_epoch = False 542 if 'by_epoch' not in kwargs: 543 kwargs['by_epoch'] = False 544 else: 545 assert not kwargs['by_epoch'], \ 546 'currently only support "by_epoch" = False' 547 if not isinstance(max_lr, (numbers.Number, list, dict)): 548 raise ValueError('the type of max_lr must be the one of list or ' 549 f'dict, but got {type(max_lr)}')
17 raise ValueError( 18 f'"{warmup}" is not a supported type for warming up, valid' 19 ' types are "constant" and "linear"') 20 if warmup is not None: 21 assert warmup_iters > 0, \ 22 '"warmup_iters" must be a positive integer' 23 assert 0 < warmup_ratio <= 1.0, \ 24 '"warmup_momentum" must be in range (0,1]' 25
19 ' types are "constant" and "linear"') 20 if warmup is not None: 21 assert warmup_iters > 0, \ 22 '"warmup_iters" must be a positive integer' 23 assert 0 < warmup_ratio <= 1.0, \ 24 '"warmup_momentum" must be in range (0,1]' 25 26 self.by_epoch = by_epoch 27 self.warmup = warmup
166 """ 167 168 def __init__(self, step, gamma=0.5, min_momentum=None, **kwargs): 169 if isinstance(step, list): 170 assert mmcv.is_list_of(step, int) 171 assert all([s > 0 for s in step]) 172 elif isinstance(step, int): 173 assert step > 0
167 168 def __init__(self, step, gamma=0.5, min_momentum=None, **kwargs): 169 if isinstance(step, list): 170 assert mmcv.is_list_of(step, int) 171 assert all([s > 0 for s in step]) 172 elif isinstance(step, int): 173 assert step > 0 174 else:
169 if isinstance(step, list): 170 assert mmcv.is_list_of(step, int) 171 assert all([s > 0 for s in step]) 172 elif isinstance(step, int): 173 assert step > 0 174 else: 175 raise TypeError('"step" must be a list or integer') 176 self.step = step
201 @HOOKS.register_module() 202 class CosineAnnealingMomentumUpdaterHook(MomentumUpdaterHook): 203 204 def __init__(self, min_momentum=None, min_momentum_ratio=None, **kwargs): 205 assert (min_momentum is None) ^ (min_momentum_ratio is None) 206 self.min_momentum = min_momentum 207 self.min_momentum_ratio = min_momentum_ratio 208 super(CosineAnnealingMomentumUpdaterHook, self).__init__(**kwargs)
255 else: 256 raise ValueError('target_ratio should be either float ' 257 f'or tuple, got {type(target_ratio)}') 258 259 assert len(target_ratio) == 2, \ 260 '"target_ratio" must be list or tuple of two floats' 261 assert 0 <= step_ratio_up < 1.0, \ 262 '"step_ratio_up" must be in range [0,1)' 263
257 f'or tuple, got {type(target_ratio)}') 258 259 assert len(target_ratio) == 2, \ 260 '"target_ratio" must be list or tuple of two floats' 261 assert 0 <= step_ratio_up < 1.0, \ 262 '"step_ratio_up" must be in range [0,1)' 263 264 self.target_ratio = target_ratio 265 self.cyclic_times = cyclic_times
265 self.cyclic_times = cyclic_times 266 self.step_ratio_up = step_ratio_up 267 self.momentum_phases = [] # init momentum_phases 268 # currently only support by_epoch=False 269 assert not by_epoch, \ 270 'currently only support "by_epoch" = False' 271 super(CyclicMomentumUpdaterHook, self).__init__(by_epoch, **kwargs) 272 273 def before_run(self, runner):
340 # validate by_epoch, currently only support by_epoch=False 341 if 'by_epoch' not in kwargs: 342 kwargs['by_epoch'] = False 343 else: 344 assert not kwargs['by_epoch'], \ 345 'currently only support "by_epoch" = False' 346 if not isinstance(base_momentum, (float, list, dict)): 347 raise ValueError('base_momentum must be the type among of float,' 348 'list or dict.')
63 64 def __init__(self, cumulative_iters=1, **kwargs): 65 super(GradientCumulativeOptimizerHook, self).__init__(**kwargs) 66 67 assert isinstance(cumulative_iters, int) and cumulative_iters > 0, \ 68 f'cumulative_iters only accepts positive int, but got ' \ 69 f'{type(cumulative_iters)} instead.' 70 71 self.cumulative_iters = cumulative_iters 72 self.divisible_iters = 0
68 except ImportError: 69 raise ImportError('profiler is the new feature of torch1.8.1, ' 70 f'but your version is {torch.__version__}') 71 72 assert isinstance(by_epoch, bool), '``by_epoch`` should be a boolean.' 73 self.by_epoch = by_epoch 74 75 if profile_iters < 1:
94 running order and iterations. E.g, [('train', 10000), 95 ('val', 1000)] means running 10000 iterations for training and 96 1000 iterations for validation, iteratively. 97 """ 98 assert isinstance(data_loaders, list) 99 assert mmcv.is_list_of(workflow, tuple) 100 assert len(data_loaders) == len(workflow) 101 if max_iters is not None:
95 ('val', 1000)] means running 10000 iterations for training and 96 1000 iterations for validation, iteratively. 97 """ 98 assert isinstance(data_loaders, list) 99 assert mmcv.is_list_of(workflow, tuple) 100 assert len(data_loaders) == len(workflow) 101 if max_iters is not None: 102 warnings.warn(
96 1000 iterations for validation, iteratively. 97 """ 98 assert isinstance(data_loaders, list) 99 assert mmcv.is_list_of(workflow, tuple) 100 assert len(data_loaders) == len(workflow) 101 if max_iters is not None: 102 warnings.warn( 103 'setting max_iters in run is deprecated, '
102 warnings.warn( 103 'setting max_iters in run is deprecated, ' 104 'please set max_iters in runner_config', DeprecationWarning) 105 self._max_iters = max_iters 106 assert self._max_iters is not None, ( 107 'max_iters must be specified during instantiation') 108 109 work_dir = self.work_dir if self.work_dir is not None else 'NONE' 110 self.logger.info('Start running, host: %s, work_dir: %s',
21 self.output.clear() 22 self.ready = False 23 24 def update(self, vars, count=1): 25 assert isinstance(vars, dict) 26 for key, var in vars.items(): 27 if key not in self.val_history: 28 self.val_history[key] = []
31 self.n_history[key].append(count) 32 33 def average(self, n=0): 34 """Average latest n values or all values.""" 35 assert n >= 0 36 for key in self.val_history: 37 values = np.array(self.val_history[key][-n:]) 38 nums = np.array(self.n_history[key][-n:])
125 if self.base_wd is None: 126 raise ValueError('base_wd should not be None') 127 128 def _is_in(self, param_group, param_group_list): 129 assert is_list_of(param_group_list, dict) 130 param = set(param_group['params']) 131 param_set = set() 132 for group in param_group_list:
48 49 Returns: 50 any type: Object built from the dict. 51 """ 52 assert isinstance(info, dict) and 'type' in info 53 assert isinstance(default_args, dict) or default_args is None 54 args = info.copy() 55 obj_type = args.pop('type')
49 Returns: 50 any type: Object built from the dict. 51 """ 52 assert isinstance(info, dict) and 'type' in info 53 assert isinstance(default_args, dict) or default_args is None 54 args = info.copy() 55 obj_type = args.pop('type') 56 if mmcv.is_str(obj_type):
644 If no ',' is found in the string, return the string length. All 645 chars inside '()' and '[]' are treated as one element and thus ',' 646 inside these brackets are ignored. 647 """ 648 assert (string.count('(') == string.count(')')) and ( 649 string.count('[') == string.count(']')), \ 650 f'Imbalanced brackets exist in {string}' 651 end = len(string) 652 for idx, char in enumerate(string): 653 pre = string[:idx]
1 # Copyright (c) OpenMMLab. All rights reserved. 2 """This file holding some environment constant for sharing by other files.""" 3 4 import os.path as osp 5 import subprocess 6 import sys 7 from collections import defaultdict 8
56 if CUDA_HOME is not None and osp.isdir(CUDA_HOME): 57 try: 58 nvcc = osp.join(CUDA_HOME, 'bin/nvcc') 59 nvcc = subprocess.check_output( 60 f'"{nvcc}" -V | tail -n1', shell=True) 61 nvcc = nvcc.decode('utf-8').strip() 62 except subprocess.SubprocessError: 63 nvcc = 'Not Available' 64 env_info['NVCC'] = nvcc
63 nvcc = 'Not Available' 64 env_info['NVCC'] = nvcc 65 66 try: 67 gcc = subprocess.check_output('gcc --version | head -n1', shell=True) 68 gcc = gcc.decode('utf-8').strip() 69 env_info['GCC'] = gcc 70 except subprocess.CalledProcessError: # gcc is unavailable
63 nvcc = 'Not Available' 64 env_info['NVCC'] = nvcc 65 66 try: 67 gcc = subprocess.check_output('gcc --version | head -n1', shell=True) 68 gcc = gcc.decode('utf-8').strip() 69 env_info['GCC'] = gcc 70 except subprocess.CalledProcessError: # gcc is unavailable
11 12 def load_ext(name, funcs): 13 ext = importlib.import_module('custom_mmcv.' + name) 14 for fun in funcs: 15 assert hasattr(ext, fun), f'{fun} miss in module {name}' 16 return ext 17 else: 18 from parrots import extension
1 # Copyright (c) OpenMMLab. All rights reserved. 2 import collections.abc 3 import functools 4 import itertools 5 import subprocess 6 import warnings 7 from collections import abc 8 from importlib import import_module
138 """ 139 if seq_type is None: 140 exp_seq_type = abc.Sequence 141 else: 142 assert isinstance(seq_type, type) 143 exp_seq_type = seq_type 144 if not isinstance(seq, exp_seq_type): 145 return False
175 Returns: 176 list: A list of sliced list. 177 """ 178 if isinstance(lens, int): 179 assert len(in_list) % lens == 0 180 lens = [lens] * int(len(in_list) / lens) 181 if not isinstance(lens, list): 182 raise TypeError('"indices" must be an integer or a list of integers')
250 return True 251 252 253 def _check_executable(cmd): 254 if subprocess.call(f'which {cmd}', shell=True) != 0: 255 return False 256 else: 257 return True
320 if kwargs: 321 for src_arg_name, dst_arg_name in name_dict.items(): 322 if src_arg_name in kwargs: 323 324 assert dst_arg_name not in kwargs, ( 325 f'The expected behavior is to replace ' 326 f'the deprecated key `{src_arg_name}` to ' 327 f'new key `{dst_arg_name}`, but got them ' 328 f'in the arguments at the same time, which ' 329 f'is confusing. `{src_arg_name} will be ' 330 f'deprecated in the future, please ' 331 f'use `{dst_arg_name}` instead.') 332 333 warnings.warn( 334 f'"{src_arg_name}" is deprecated in '
352 method (str): the method name to check. 353 base_class (type): the class of the base class. 354 derived_class (type | Any): the class or instance of the derived class. 355 """ 356 assert isinstance(base_class, type), \ 357 "base_class doesn't accept instance, Please pass class instead." 358 359 if not isinstance(derived_class, type): 360 derived_class = derived_class.__class__
32 self.file.flush() 33 self.timer = Timer() 34 35 def update(self, num_tasks=1): 36 assert num_tasks > 0 37 self.completed += num_tasks 38 elapsed = self.timer.since_start() 39 if elapsed > 0:
75 Returns: 76 list: The task results. 77 """ 78 if isinstance(tasks, tuple): 79 assert len(tasks) == 2 80 assert isinstance(tasks[0], Iterable) 81 assert isinstance(tasks[1], int) 82 task_num = tasks[1]
76 list: The task results. 77 """ 78 if isinstance(tasks, tuple): 79 assert len(tasks) == 2 80 assert isinstance(tasks[0], Iterable) 81 assert isinstance(tasks[1], int) 82 task_num = tasks[1] 83 tasks = tasks[0]
77 """ 78 if isinstance(tasks, tuple): 79 assert len(tasks) == 2 80 assert isinstance(tasks[0], Iterable) 81 assert isinstance(tasks[1], int) 82 task_num = tasks[1] 83 tasks = tasks[0] 84 elif isinstance(tasks, Iterable):
141 Returns: 142 list: The task results. 143 """ 144 if isinstance(tasks, tuple): 145 assert len(tasks) == 2 146 assert isinstance(tasks[0], Iterable) 147 assert isinstance(tasks[1], int) 148 task_num = tasks[1]
142 list: The task results. 143 """ 144 if isinstance(tasks, tuple): 145 assert len(tasks) == 2 146 assert isinstance(tasks[0], Iterable) 147 assert isinstance(tasks[1], int) 148 task_num = tasks[1] 149 tasks = tasks[0]
143 """ 144 if isinstance(tasks, tuple): 145 assert len(tasks) == 2 146 assert isinstance(tasks[0], Iterable) 147 assert isinstance(tasks[1], int) 148 task_num = tasks[1] 149 tasks = tasks[0] 150 elif isinstance(tasks, Iterable):
190 Yields: 191 list: The task results. 192 """ 193 if isinstance(tasks, tuple): 194 assert len(tasks) == 2 195 assert isinstance(tasks[0], Iterable) 196 assert isinstance(tasks[1], int) 197 task_num = tasks[1]
191 list: The task results. 192 """ 193 if isinstance(tasks, tuple): 194 assert len(tasks) == 2 195 assert isinstance(tasks[0], Iterable) 196 assert isinstance(tasks[1], int) 197 task_num = tasks[1] 198 tasks = tasks[0]
192 """ 193 if isinstance(tasks, tuple): 194 assert len(tasks) == 2 195 assert isinstance(tasks[0], Iterable) 196 assert isinstance(tasks[1], int) 197 task_num = tasks[1] 198 tasks = tasks[0] 199 elif isinstance(tasks, Iterable):
102 self.build_func = build_from_cfg 103 else: 104 self.build_func = build_func 105 if parent is not None: 106 assert isinstance(parent, Registry) 107 parent._add_children(self) 108 self.parent = parent 109 else:
225 >>> pass 226 >>> resnet = models.build(dict(type='mmdet.ResNet')) 227 """ 228 229 assert isinstance(registry, Registry) 230 assert registry.scope is not None 231 assert registry.scope not in self.children, \ 232 f'scope {registry.scope} exists in {self.name} registry'
226 >>> resnet = models.build(dict(type='mmdet.ResNet')) 227 """ 228 229 assert isinstance(registry, Registry) 230 assert registry.scope is not None 231 assert registry.scope not in self.children, \ 232 f'scope {registry.scope} exists in {self.name} registry' 233 self.children[registry.scope] = registry
227 """ 228 229 assert isinstance(registry, Registry) 230 assert registry.scope is not None 231 assert registry.scope not in self.children, \ 232 f'scope {registry.scope} exists in {self.name} registry' 233 self.children[registry.scope] = registry 234 235 def _register_module(self, module_class, module_name=None, force=False):
1 # Copyright (c) OpenMMLab. All rights reserved. 2 import os 3 import subprocess 4 import warnings 5 6 from packaging.version import parse 7 8
18 19 Returns: 20 tuple[int]: The version info in digits (integers). 21 """ 22 assert 'parrots' not in version_str 23 version = parse(version_str) 24 assert version.release, f'failed to parse version {version_str}' 25 release = list(version.release)
20 tuple[int]: The version info in digits (integers). 21 """ 22 assert 'parrots' not in version_str 23 version = parse(version_str) 24 assert version.release, f'failed to parse version {version_str}' 25 release = list(version.release) 26 release = release[:length] 27 if len(release) < length:
57 # LANGUAGE is used on win32 58 env['LANGUAGE'] = 'C' 59 env['LANG'] = 'C' 60 env['LC_ALL'] = 'C' 61 out = subprocess.Popen( 62 cmd, stdout=subprocess.PIPE, env=env).communicate()[0] 63 return out 64 65
15 (2, 0, 0, 0, 'rc', 1) (when length is set to 4). 16 """ 17 from packaging.version import parse 18 version = parse(version_str) 19 assert version.release, f'failed to parse version {version_str}' 20 release = list(version.release) 21 release = release[:length] 22 if len(release) < length:
65 # Check whether the video path is a url 66 if not filename.startswith(('https://', 'http://')): 67 check_file_exist(filename, 'Video file not found: ' + filename) 68 self._vcap = cv2.VideoCapture(filename) 69 assert cache_capacity > 0 70 self._cache = Cache(cache_capacity) 71 self._position = 0 72 # get basic info
44 w = np.fromfile(f, np.int32, 1).squeeze() 45 h = np.fromfile(f, np.int32, 1).squeeze() 46 flow = np.fromfile(f, np.float32, w * h * 2).reshape((h, w, 2)) 47 else: 48 assert concat_axis in [0, 1] 49 cat_flow = imread(flow_or_path, flag='unchanged') 50 if cat_flow.ndim != 2: 51 raise IOError(
50 if cat_flow.ndim != 2: 51 raise IOError( 52 f'{flow_or_path} is not a valid quantized flow file, ' 53 f'its dimension is {cat_flow.ndim}.') 54 assert cat_flow.shape[concat_axis] % 2 == 0 55 dx, dy = np.split(cat_flow, 2, axis=concat_axis) 56 flow = dequantize_flow(dx, dy, *args, **kwargs) 57
81 flow = flow.astype(np.float32) 82 flow.tofile(f) 83 f.flush() 84 else: 85 assert concat_axis in [0, 1] 86 dx, dy = quantize_flow(flow, *args, **kwargs) 87 dxdy = np.concatenate((dx, dy), axis=concat_axis) 88 imwrite(dxdy, filename)
127 128 Returns: 129 ndarray: Dequantized flow. 130 """ 131 assert dx.shape == dy.shape 132 assert dx.ndim == 2 or (dx.ndim == 3 and dx.shape[-1] == 1) 133 134 dx, dy = [dequantize(d, -max_val, max_val, 255) for d in [dx, dy]]
128 Returns: 129 ndarray: Dequantized flow. 130 """ 131 assert dx.shape == dy.shape 132 assert dx.ndim == 2 or (dx.ndim == 3 and dx.shape[-1] == 1) 133 134 dx, dy = [dequantize(d, -max_val, max_val, 255) for d in [dx, dy]] 135
154 ndarray: Warped image with the same shape of img 155 """ 156 warnings.warn('This function is just for prototyping and cannot ' 157 'guarantee the computational efficiency.') 158 assert flow.ndim == 3, 'Flow must be in 3D arrays.' 159 height = flow.shape[0] 160 width = flow.shape[1] 161 channels = img.shape[2]
1 # Copyright (c) OpenMMLab. All rights reserved. 2 import os 3 import os.path as osp 4 import subprocess 5 import tempfile 6 7 from custom_mmpkg.custom_mmcv.utils import requires_executable 8
36 if isinstance(v, bool): 37 if v: 38 options.append(f'-{k}') 39 elif k == 'log_level': 40 assert v in [ 41 'quiet', 'panic', 'fatal', 'error', 'warning', 'info', 42 'verbose', 'debug', 'trace' 43 ] 44 options.append(f'-loglevel {v}') 45 else: 46 options.append(f'-{k} {v}')
47 cmd = f'ffmpeg -y {pre_options} -i {in_file} {" ".join(options)} ' \ 48 f'{out_file}' 49 if print_cmd: 50 print(cmd) 51 subprocess.call(cmd, shell=True) 52 53 54 @requires_executable('ffmpeg')
34 return Color[color].value 35 elif isinstance(color, Color): 36 return color.value 37 elif isinstance(color, tuple): 38 assert len(color) == 3 39 for channel in color: 40 assert 0 <= channel <= 255 41 return color
36 return color.value 37 elif isinstance(color, tuple): 38 assert len(color) == 3 39 for channel in color: 40 assert 0 <= channel <= 255 41 return color 42 elif isinstance(color, int): 43 assert 0 <= color <= 255
39 for channel in color: 40 assert 0 <= channel <= 255 41 return color 42 elif isinstance(color, int): 43 assert 0 <= color <= 255 44 return color, color, color 45 elif isinstance(color, np.ndarray): 46 assert color.ndim == 1 and color.size == 3
42 elif isinstance(color, int): 43 assert 0 <= color <= 255 44 return color, color, color 45 elif isinstance(color, np.ndarray): 46 assert color.ndim == 1 and color.size == 3 47 assert np.all((color >= 0) & (color <= 255)) 48 color = color.astype(np.uint8) 49 return tuple(color)
43 assert 0 <= color <= 255 44 return color, color, color 45 elif isinstance(color, np.ndarray): 46 assert color.ndim == 1 and color.size == 3 47 assert np.all((color >= 0) & (color <= 255)) 48 color = color.astype(np.uint8) 49 return tuple(color) 50 else:
59 bboxes = [bboxes] 60 if not isinstance(colors, list): 61 colors = [colors for _ in range(len(bboxes))] 62 colors = [color_val(c) for c in colors] 63 assert len(bboxes) == len(colors) 64 65 for i, _bboxes in enumerate(bboxes): 66 _bboxes = _bboxes.astype(np.int32)
114 115 Returns: 116 ndarray: The image with bboxes drawn on it. 117 """ 118 assert bboxes.ndim == 2 119 assert labels.ndim == 1 120 assert bboxes.shape[0] == labels.shape[0] 121 assert bboxes.shape[1] == 4 or bboxes.shape[1] == 5
115 Returns: 116 ndarray: The image with bboxes drawn on it. 117 """ 118 assert bboxes.ndim == 2 119 assert labels.ndim == 1 120 assert bboxes.shape[0] == labels.shape[0] 121 assert bboxes.shape[1] == 4 or bboxes.shape[1] == 5 122 img = imread(img)
116 ndarray: The image with bboxes drawn on it. 117 """ 118 assert bboxes.ndim == 2 119 assert labels.ndim == 1 120 assert bboxes.shape[0] == labels.shape[0] 121 assert bboxes.shape[1] == 4 or bboxes.shape[1] == 5 122 img = imread(img) 123 img = np.ascontiguousarray(img)
117 """ 118 assert bboxes.ndim == 2 119 assert labels.ndim == 1 120 assert bboxes.shape[0] == labels.shape[0] 121 assert bboxes.shape[1] == 4 or bboxes.shape[1] == 5 122 img = imread(img) 123 img = np.ascontiguousarray(img) 124
122 img = imread(img) 123 img = np.ascontiguousarray(img) 124 125 if score_thr > 0: 126 assert bboxes.shape[1] == 5 127 scores = bboxes[:, -1] 128 inds = scores > score_thr 129 bboxes = bboxes[inds, :]
33 34 Returns: 35 ndarray: RGB image that can be visualized. 36 """ 37 assert flow.ndim == 3 and flow.shape[-1] == 2 38 if color_wheel is None: 39 color_wheel = make_color_wheel() 40 assert color_wheel.ndim == 2 and color_wheel.shape[1] == 3
36 """ 37 assert flow.ndim == 3 and flow.shape[-1] == 2 38 if color_wheel is None: 39 color_wheel = make_color_wheel() 40 assert color_wheel.ndim == 2 and color_wheel.shape[1] == 3 41 num_bins = color_wheel.shape[0] 42 43 dx = flow[:, :, 0].copy()
87 ndarray: Color wheel of shape (total_bins, 3). 88 """ 89 if bins is None: 90 bins = [15, 6, 4, 11, 13, 6] 91 assert len(bins) == 6 92 93 RY, YG, GC, CB, BM, MR = tuple(bins) 94
1 import os.path as osp 2 import pickle 3 import shutil 4 import tempfile 5 6 import custom_mmpkg.custom_mmcv as mmcv 7 import numpy as np 8 import torch
65 if show or out_dir: 66 img_tensor = data['img'][0] 67 img_metas = data['img_metas'][0].data[0] 68 imgs = tensor2imgs(img_tensor, **img_metas[0]['img_norm_cfg']) 69 assert len(imgs) == len(img_metas) 70 71 for img, img_meta in zip(imgs, img_metas): 72 h, w, _ = img_meta['img_shape']
227 if rank == 0: 228 part_list = [] 229 for recv, shape in zip(part_recv_list, shape_list): 230 part_list.append( 231 pickle.loads(recv[:shape[0]].cpu().numpy().tobytes())) 232 # sort the results 233 ordered_results = [] 234 for res in zip(*part_list):
126 alias2name[alias] = name 127 128 if mmcv.is_str(dataset): 129 if dataset in alias2name: 130 labels = eval(alias2name[dataset] + '_classes()') 131 else: 132 raise ValueError(f'Unrecognized dataset: {dataset}') 133 else:
143 alias2name[alias] = name 144 145 if mmcv.is_str(dataset): 146 if dataset in alias2name: 147 labels = eval(alias2name[dataset] + '_palette()') 148 else: 149 raise ValueError(f'Unrecognized dataset: {dataset}') 150 else:
111 ndarray: The prediction histogram on all classes. 112 ndarray: The ground truth histogram on all classes. 113 """ 114 num_imgs = len(results) 115 assert len(gt_seg_maps) == num_imgs 116 total_area_intersect = torch.zeros((num_classes, ), dtype=torch.float64) 117 total_area_union = torch.zeros((num_classes, ), dtype=torch.float64) 118 total_area_pred_label = torch.zeros((num_classes, ), dtype=torch.float64)
22 23 def __init__(self, context, thresh=None, min_kept=100000): 24 super(OHEMPixelSampler, self).__init__() 25 self.context = context 26 assert min_kept > 1 27 self.thresh = thresh 28 self.min_kept = min_kept 29
37 Returns: 38 torch.Tensor: segmentation weight, shape (N, H, W) 39 """ 40 with torch.no_grad(): 41 assert seg_logit.shape[2:] == seg_label.shape[2:] 42 assert seg_label.shape[1] == 1 43 seg_label = seg_label.squeeze(1).long() 44 batch_kept = self.min_kept * seg_label.size(0)
38 torch.Tensor: segmentation weight, shape (N, H, W) 39 """ 40 with torch.no_grad(): 41 assert seg_logit.shape[2:] == seg_label.shape[2:] 42 assert seg_label.shape[1] == 1 43 seg_label = seg_label.squeeze(1).long() 44 batch_kept = self.min_kept * seg_label.size(0) 45 valid_mask = seg_label != self.context.ignore_index
29 num_split = len(split) if isinstance(split, (list, tuple)) else 1 30 else: 31 num_split = 0 32 if num_img_dir > 1: 33 assert num_img_dir == num_ann_dir or num_ann_dir == 0 34 assert num_img_dir == num_split or num_split == 0 35 else: 36 assert num_split == num_ann_dir or num_ann_dir <= 1
30 else: 31 num_split = 0 32 if num_img_dir > 1: 33 assert num_img_dir == num_ann_dir or num_ann_dir == 0 34 assert num_img_dir == num_split or num_split == 0 35 else: 36 assert num_split == num_ann_dir or num_ann_dir <= 1 37 num_dset = max(num_split, num_img_dir)
32 if num_img_dir > 1: 33 assert num_img_dir == num_ann_dir or num_ann_dir == 0 34 assert num_img_dir == num_split or num_split == 0 35 else: 36 assert num_split == num_ann_dir or num_ann_dir <= 1 37 num_dset = max(num_split, num_img_dir) 38 39 datasets = []
119 init_fn = partial( 120 worker_init_fn, num_workers=num_workers, rank=rank, 121 seed=seed) if seed is not None else None 122 123 assert dataloader_type in ( 124 'DataLoader', 125 'PoolDataLoader'), f'unsupported dataloader {dataloader_type}' 126 127 if dataloader_type == 'PoolDataLoader': 128 dataloader = PoolDataLoader
23 img_suffix='.png', 24 seg_map_suffix='_1stHO.png', 25 reduce_zero_label=False, 26 **kwargs) 27 assert osp.exists(self.img_dir)
106 the image paths, tmp_dir is the temporal directory created 107 for saving json/png files when img_prefix is not specified. 108 """ 109 110 assert isinstance(results, list), 'results must be a list' 111 assert len(results) == len(self), ( 112 'The length of results is not equal to the dataset len: ' 113 f'{len(results)} != {len(self)}')
107 for saving json/png files when img_prefix is not specified. 108 """ 109 110 assert isinstance(results, list), 'results must be a list' 111 assert len(results) == len(self), ( 112 'The length of results is not equal to the dataset len: ' 113 f'{len(results)} != {len(self)}') 114 115 if imgfile_prefix is None: 116 tmp_dir = tempfile.TemporaryDirectory()
23 img_suffix='.png', 24 seg_map_suffix='_manual1.png', 25 reduce_zero_label=False, 26 **kwargs) 27 assert osp.exists(self.img_dir)
23 img_suffix='.png', 24 seg_map_suffix='.png', 25 reduce_zero_label=False, 26 **kwargs) 27 assert osp.exists(self.img_dir)
50 seg_map_suffix='.png', 51 split=split, 52 reduce_zero_label=False, 53 **kwargs) 54 assert osp.exists(self.img_dir) and self.split is not None 55 56 57 @DATASETS.register_module()
99 seg_map_suffix='.png', 100 split=split, 101 reduce_zero_label=True, 102 **kwargs) 103 assert osp.exists(self.img_dir) and self.split is not None
14 config dict to be composed. 15 """ 16 17 def __init__(self, transforms): 18 assert isinstance(transforms, collections.abc.Sequence) 19 self.transforms = [] 20 for transform in transforms: 21 if isinstance(transform, dict):
59 self.transforms = Compose(transforms) 60 if img_ratios is not None: 61 img_ratios = img_ratios if isinstance(img_ratios, 62 list) else [img_ratios] 63 assert mmcv.is_list_of(img_ratios, float) 64 if img_scale is None: 65 # mode 1: given img_scale=None and a range of image ratio 66 self.img_scale = None
63 assert mmcv.is_list_of(img_ratios, float) 64 if img_scale is None: 65 # mode 1: given img_scale=None and a range of image ratio 66 self.img_scale = None 67 assert mmcv.is_list_of(img_ratios, float) 68 elif isinstance(img_scale, tuple) and mmcv.is_list_of( 69 img_ratios, float): 70 assert len(img_scale) == 2
66 self.img_scale = None 67 assert mmcv.is_list_of(img_ratios, float) 68 elif isinstance(img_scale, tuple) and mmcv.is_list_of( 69 img_ratios, float): 70 assert len(img_scale) == 2 71 # mode 2: given a scale and a range of image ratio 72 self.img_scale = [(int(img_scale[0] * ratio), 73 int(img_scale[1] * ratio))
75 else: 76 # mode 3: given multiple scales 77 self.img_scale = img_scale if isinstance(img_scale, 78 list) else [img_scale] 79 assert mmcv.is_list_of(self.img_scale, tuple) or self.img_scale is None 80 self.flip = flip 81 self.img_ratios = img_ratios 82 self.flip_direction = flip_direction if isinstance(
80 self.flip = flip 81 self.img_ratios = img_ratios 82 self.flip_direction = flip_direction if isinstance( 83 flip_direction, list) else [flip_direction] 84 assert mmcv.is_list_of(self.flip_direction, str) 85 if not self.flip and self.flip_direction != ['horizontal']: 86 warnings.warn( 87 'flip_direction has no effect when flip is set to False')
49 if isinstance(img_scale, list): 50 self.img_scale = img_scale 51 else: 52 self.img_scale = [img_scale] 53 assert mmcv.is_list_of(self.img_scale, tuple) 54 55 if ratio_range is not None: 56 # mode 1: given img_scale=None and a range of image ratio
54 55 if ratio_range is not None: 56 # mode 1: given img_scale=None and a range of image ratio 57 # mode 2: given a scale and a range of image ratio 58 assert self.img_scale is None or len(self.img_scale) == 1 59 else: 60 # mode 3 and 4: given multiple scales or a range of scales 61 assert multiscale_mode in ['value', 'range']
57 # mode 2: given a scale and a range of image ratio 58 assert self.img_scale is None or len(self.img_scale) == 1 59 else: 60 # mode 3 and 4: given multiple scales or a range of scales 61 assert multiscale_mode in ['value', 'range'] 62 63 self.multiscale_mode = multiscale_mode 64 self.ratio_range = ratio_range
76 where ``img_scale`` is the selected image scale and 77 ``scale_idx`` is the selected index in the given candidates. 78 """ 79 80 assert mmcv.is_list_of(img_scales, tuple) 81 scale_idx = np.random.randint(len(img_scales)) 82 img_scale = img_scales[scale_idx] 83 return img_scale, scale_idx
96 ``img_scale`` is sampled scale and None is just a placeholder 97 to be consistent with :func:`random_select`. 98 """ 99 100 assert mmcv.is_list_of(img_scales, tuple) and len(img_scales) == 2 101 img_scale_long = [max(s) for s in img_scales] 102 img_scale_short = [min(s) for s in img_scales] 103 long_edge = np.random.randint(
128 None is just a placeholder to be consistent with 129 :func:`random_select`. 130 """ 131 132 assert isinstance(img_scale, tuple) and len(img_scale) == 2 133 min_ratio, max_ratio = ratio_range 134 assert min_ratio <= max_ratio 135 ratio = np.random.random_sample() * (max_ratio - min_ratio) + min_ratio
130 """ 131 132 assert isinstance(img_scale, tuple) and len(img_scale) == 2 133 min_ratio, max_ratio = ratio_range 134 assert min_ratio <= max_ratio 135 ratio = np.random.random_sample() * (max_ratio - min_ratio) + min_ratio 136 scale = int(img_scale[0] * ratio), int(img_scale[1] * ratio) 137 return scale, None
252 def __init__(self, prob=None, direction='horizontal'): 253 self.prob = prob 254 self.direction = direction 255 if prob is not None: 256 assert prob >= 0 and prob <= 1 257 assert direction in ['horizontal', 'vertical'] 258 259 def __call__(self, results):
253 self.prob = prob 254 self.direction = direction 255 if prob is not None: 256 assert prob >= 0 and prob <= 1 257 assert direction in ['horizontal', 'vertical'] 258 259 def __call__(self, results): 260 """Call function to flip bounding boxes, masks, semantic segmentation
314 self.size_divisor = size_divisor 315 self.pad_val = pad_val 316 self.seg_pad_val = seg_pad_val 317 # only one of size and size_divisor should be valid 318 assert size is not None or size_divisor is not None 319 assert size is None or size_divisor is None 320 321 def _pad_img(self, results):
315 self.pad_val = pad_val 316 self.seg_pad_val = seg_pad_val 317 # only one of size and size_divisor should be valid 318 assert size is not None or size_divisor is not None 319 assert size is None or size_divisor is None 320 321 def _pad_img(self, results): 322 """Pad images according to ``self.size``."""
413 Default: 255. 414 """ 415 416 def __init__(self, min_value=0, max_value=255): 417 assert isinstance(min_value, float) or isinstance(min_value, int) 418 assert isinstance(max_value, float) or isinstance(max_value, int) 419 assert min_value < max_value 420 self.min_value = min_value
414 """ 415 416 def __init__(self, min_value=0, max_value=255): 417 assert isinstance(min_value, float) or isinstance(min_value, int) 418 assert isinstance(max_value, float) or isinstance(max_value, int) 419 assert min_value < max_value 420 self.min_value = min_value 421 self.max_value = max_value
415 416 def __init__(self, min_value=0, max_value=255): 417 assert isinstance(min_value, float) or isinstance(min_value, int) 418 assert isinstance(max_value, float) or isinstance(max_value, int) 419 assert min_value < max_value 420 self.min_value = min_value 421 self.max_value = max_value 422
432 img = results['img'] 433 img_min_value = np.min(img) 434 img_max_value = np.max(img) 435 436 assert img_min_value < img_max_value 437 # rerange to [0, 1] 438 img = (img - img_min_value) / (img_max_value - img_min_value) 439 # rerange to [min_value, max_value]
462 It defines the number of tiles in row and column. Default: (8, 8). 463 """ 464 465 def __init__(self, clip_limit=40.0, tile_grid_size=(8, 8)): 466 assert isinstance(clip_limit, (float, int)) 467 self.clip_limit = clip_limit 468 assert is_tuple_of(tile_grid_size, int) 469 assert len(tile_grid_size) == 2
464 465 def __init__(self, clip_limit=40.0, tile_grid_size=(8, 8)): 466 assert isinstance(clip_limit, (float, int)) 467 self.clip_limit = clip_limit 468 assert is_tuple_of(tile_grid_size, int) 469 assert len(tile_grid_size) == 2 470 self.tile_grid_size = tile_grid_size 471
465 def __init__(self, clip_limit=40.0, tile_grid_size=(8, 8)): 466 assert isinstance(clip_limit, (float, int)) 467 self.clip_limit = clip_limit 468 assert is_tuple_of(tile_grid_size, int) 469 assert len(tile_grid_size) == 2 470 self.tile_grid_size = tile_grid_size 471 472 def __call__(self, results):
503 occupy. 504 """ 505 506 def __init__(self, crop_size, cat_max_ratio=1., ignore_index=255): 507 assert crop_size[0] > 0 and crop_size[1] > 0 508 self.crop_size = crop_size 509 self.cat_max_ratio = cat_max_ratio 510 self.ignore_index = ignore_index
592 seg_pad_val=255, 593 center=None, 594 auto_bound=False): 595 self.prob = prob 596 assert prob >= 0 and prob <= 1 597 if isinstance(degree, (float, int)): 598 assert degree > 0, f'degree {degree} should be positive' 599 self.degree = (-degree, degree)
594 auto_bound=False): 595 self.prob = prob 596 assert prob >= 0 and prob <= 1 597 if isinstance(degree, (float, int)): 598 assert degree > 0, f'degree {degree} should be positive' 599 self.degree = (-degree, degree) 600 else: 601 self.degree = degree
598 assert degree > 0, f'degree {degree} should be positive' 599 self.degree = (-degree, degree) 600 else: 601 self.degree = degree 602 assert len(self.degree) == 2, f'degree {self.degree} should be a ' \ 603 f'tuple of (min, max)' 604 self.pal_val = pad_val 605 self.seg_pad_val = seg_pad_val 606 self.center = center
665 Default: (0.299, 0.587, 0.114). 666 """ 667 668 def __init__(self, out_channels=None, weights=(0.299, 0.587, 0.114)): 669 assert out_channels is None or out_channels > 0 670 self.out_channels = out_channels 671 assert isinstance(weights, tuple) 672 for item in weights:
667 668 def __init__(self, out_channels=None, weights=(0.299, 0.587, 0.114)): 669 assert out_channels is None or out_channels > 0 670 self.out_channels = out_channels 671 assert isinstance(weights, tuple) 672 for item in weights: 673 assert isinstance(item, (float, int)) 674 self.weights = weights
669 assert out_channels is None or out_channels > 0 670 self.out_channels = out_channels 671 assert isinstance(weights, tuple) 672 for item in weights: 673 assert isinstance(item, (float, int)) 674 self.weights = weights 675 676 def __call__(self, results):
682 Returns: 683 dict: Result dict with grayscale image. 684 """ 685 img = results['img'] 686 assert len(img.shape) == 3 687 assert img.shape[2] == len(self.weights) 688 weights = np.array(self.weights).reshape((1, 1, -1)) 689 img = (img * weights).sum(2, keepdims=True)
683 dict: Result dict with grayscale image. 684 """ 685 img = results['img'] 686 assert len(img.shape) == 3 687 assert img.shape[2] == len(self.weights) 688 weights = np.array(self.weights).reshape((1, 1, -1)) 689 img = (img * weights).sum(2, keepdims=True) 690 if self.out_channels is None:
713 Default: 1.0. 714 """ 715 716 def __init__(self, gamma=1.0): 717 assert isinstance(gamma, float) or isinstance(gamma, int) 718 assert gamma > 0 719 self.gamma = gamma 720 inv_gamma = 1.0 / gamma
714 """ 715 716 def __init__(self, gamma=1.0): 717 assert isinstance(gamma, float) or isinstance(gamma, int) 718 assert gamma > 0 719 self.gamma = gamma 720 inv_gamma = 1.0 / gamma 721 self.table = np.array([(i / 255.0)**inv_gamma * 255
23 img_suffix='.png', 24 seg_map_suffix='.ah.png', 25 reduce_zero_label=False, 26 **kwargs) 27 assert osp.exists(self.img_dir)
25 26 def __init__(self, split, **kwargs): 27 super(PascalVOCDataset, self).__init__( 28 img_suffix='.jpg', seg_map_suffix='.png', split=split, **kwargs) 29 assert osp.exists(self.img_dir) and self.split is not None
26 def __init__(self, channel, reduction=16, with_cp=False): 27 super(GlobalContextExtractor, self).__init__() 28 self.channel = channel 29 self.reduction = reduction 30 assert reduction >= 1 and channel >= reduction 31 self.with_cp = with_cp 32 self.avg_pool = nn.AdaptiveAvgPool2d(1) 33 self.fc = nn.Sequential(
226 227 super(CGNet, self).__init__() 228 self.in_channels = in_channels 229 self.num_channels = num_channels 230 assert isinstance(self.num_channels, tuple) and len( 231 self.num_channels) == 3 232 self.num_blocks = num_blocks 233 assert isinstance(self.num_blocks, tuple) and len(self.num_blocks) == 2 234 self.dilations = dilations
229 self.num_channels = num_channels 230 assert isinstance(self.num_channels, tuple) and len( 231 self.num_channels) == 3 232 self.num_blocks = num_blocks 233 assert isinstance(self.num_blocks, tuple) and len(self.num_blocks) == 2 234 self.dilations = dilations 235 assert isinstance(self.dilations, tuple) and len(self.dilations) == 2 236 self.reductions = reductions
231 self.num_channels) == 3 232 self.num_blocks = num_blocks 233 assert isinstance(self.num_blocks, tuple) and len(self.num_blocks) == 2 234 self.dilations = dilations 235 assert isinstance(self.dilations, tuple) and len(self.dilations) == 2 236 self.reductions = reductions 237 assert isinstance(self.reductions, tuple) and len(self.reductions) == 2 238 self.conv_cfg = conv_cfg
233 assert isinstance(self.num_blocks, tuple) and len(self.num_blocks) == 2 234 self.dilations = dilations 235 assert isinstance(self.dilations, tuple) and len(self.dilations) == 2 236 self.reductions = reductions 237 assert isinstance(self.reductions, tuple) and len(self.reductions) == 2 238 self.conv_cfg = conv_cfg 239 self.norm_cfg = norm_cfg 240 self.act_cfg = act_cfg
118 super(GlobalFeatureExtractor, self).__init__() 119 self.conv_cfg = conv_cfg 120 self.norm_cfg = norm_cfg 121 self.act_cfg = act_cfg 122 assert len(block_channels) == len(num_blocks) == 3 123 self.bottleneck1 = self._make_layer(in_channels, block_channels[0], 124 num_blocks[0], strides[0], 125 expand_ratio)
56 super(MobileNetV2, self).__init__() 57 self.widen_factor = widen_factor 58 self.strides = strides 59 self.dilations = dilations 60 assert len(strides) == len(dilations) == len(self.arch_settings) 61 self.out_indices = out_indices 62 for index in out_indices: 63 if index not in range(0, 7):
76 reduction_factor=1, 77 norm_eval=False, 78 with_cp=False): 79 super(MobileNetV3, self).__init__() 80 assert arch in self.arch_settings 81 assert isinstance(reduction_factor, int) and reduction_factor > 0 82 assert mmcv.is_tuple_of(out_indices, int) 83 for index in out_indices:
77 norm_eval=False, 78 with_cp=False): 79 super(MobileNetV3, self).__init__() 80 assert arch in self.arch_settings 81 assert isinstance(reduction_factor, int) and reduction_factor > 0 82 assert mmcv.is_tuple_of(out_indices, int) 83 for index in out_indices: 84 if index not in range(0, len(self.arch_settings[arch]) + 2):
78 with_cp=False): 79 super(MobileNetV3, self).__init__() 80 assert arch in self.arch_settings 81 assert isinstance(reduction_factor, int) and reduction_factor > 0 82 assert mmcv.is_tuple_of(out_indices, int) 83 for index in out_indices: 84 if index not in range(0, len(self.arch_settings[arch]) + 2): 85 raise ValueError(
78 fallback_on_stride = False 79 if self.with_dcn: 80 fallback_on_stride = self.dcn.pop('fallback_on_stride', False) 81 if self.with_dcn and not fallback_on_stride: 82 assert conv_cfg is None, 'conv_cfg must be None for DCN' 83 conv_cfg = dcn 84 self.conv = build_conv_layer( 85 conv_cfg,
27 norm_cfg=dict(type='BN'), 28 dcn=None, 29 plugins=None): 30 super(BasicBlock, self).__init__() 31 assert dcn is None, 'Not implemented yet.' 32 assert plugins is None, 'Not implemented yet.' 33 34 self.norm1_name, norm1 = build_norm_layer(norm_cfg, planes, postfix=1)
28 dcn=None, 29 plugins=None): 30 super(BasicBlock, self).__init__() 31 assert dcn is None, 'Not implemented yet.' 32 assert plugins is None, 'Not implemented yet.' 33 34 self.norm1_name, norm1 = build_norm_layer(norm_cfg, planes, postfix=1) 35 self.norm2_name, norm2 = build_norm_layer(norm_cfg, planes, postfix=2)
115 norm_cfg=dict(type='BN'), 116 dcn=None, 117 plugins=None): 118 super(Bottleneck, self).__init__() 119 assert style in ['pytorch', 'caffe'] 120 assert dcn is None or isinstance(dcn, dict) 121 assert plugins is None or isinstance(plugins, list) 122 if plugins is not None:
116 dcn=None, 117 plugins=None): 118 super(Bottleneck, self).__init__() 119 assert style in ['pytorch', 'caffe'] 120 assert dcn is None or isinstance(dcn, dict) 121 assert plugins is None or isinstance(plugins, list) 122 if plugins is not None: 123 allowed_position = ['after_conv1', 'after_conv2', 'after_conv3']
117 plugins=None): 118 super(Bottleneck, self).__init__() 119 assert style in ['pytorch', 'caffe'] 120 assert dcn is None or isinstance(dcn, dict) 121 assert plugins is None or isinstance(plugins, list) 122 if plugins is not None: 123 allowed_position = ['after_conv1', 'after_conv2', 'after_conv3'] 124 assert all(p['position'] in allowed_position for p in plugins)
120 assert dcn is None or isinstance(dcn, dict) 121 assert plugins is None or isinstance(plugins, list) 122 if plugins is not None: 123 allowed_position = ['after_conv1', 'after_conv2', 'after_conv3'] 124 assert all(p['position'] in allowed_position for p in plugins) 125 126 self.inplanes = inplanes 127 self.planes = planes
184 padding=dilation, 185 dilation=dilation, 186 bias=False) 187 else: 188 assert self.conv_cfg is None, 'conv_cfg must be None for DCN' 189 self.conv2 = build_conv_layer( 190 dcn, 191 planes,
225 226 Returns: 227 list[str]: List of the names of plugin. 228 """ 229 assert isinstance(plugins, list) 230 plugin_names = [] 231 for plugin in plugins: 232 plugin = plugin.copy()
233 name, layer = build_plugin_layer( 234 plugin, 235 in_channels=in_channels, 236 postfix=plugin.pop('postfix', '')) 237 assert not hasattr(self, name), f'duplicate plugin {name}' 238 self.add_module(name, layer) 239 plugin_names.append(name) 240 return plugin_names
399 self.depth = depth 400 self.stem_channels = stem_channels 401 self.base_channels = base_channels 402 self.num_stages = num_stages 403 assert num_stages >= 1 and num_stages <= 4 404 self.strides = strides 405 self.dilations = dilations 406 assert len(strides) == len(dilations) == num_stages
402 self.num_stages = num_stages 403 assert num_stages >= 1 and num_stages <= 4 404 self.strides = strides 405 self.dilations = dilations 406 assert len(strides) == len(dilations) == num_stages 407 self.out_indices = out_indices 408 assert max(out_indices) < num_stages 409 self.style = style
404 self.strides = strides 405 self.dilations = dilations 406 assert len(strides) == len(dilations) == num_stages 407 self.out_indices = out_indices 408 assert max(out_indices) < num_stages 409 self.style = style 410 self.deep_stem = deep_stem 411 self.avg_down = avg_down
416 self.norm_eval = norm_eval 417 self.dcn = dcn 418 self.stage_with_dcn = stage_with_dcn 419 if dcn is not None: 420 assert len(stage_with_dcn) == num_stages 421 self.plugins = plugins 422 self.multi_grid = multi_grid 423 self.contract_dilation = contract_dilation
512 stage_plugins = [] 513 for plugin in plugins: 514 plugin = plugin.copy() 515 stages = plugin.pop('stages', None) 516 assert stages is None or len(stages) == self.num_stages 517 # whether to insert plugin into current stage 518 if stages is None or stages[stage_idx]: 519 stage_plugins.append(plugin)
60 dilation=self.dilation, 61 groups=groups, 62 bias=False) 63 else: 64 assert self.conv_cfg is None, 'conv_cfg must be None for DCN' 65 self.conv2 = build_conv_layer( 66 self.dcn, 67 width,
52 act_cfg=dict(type='ReLU'), 53 dcn=None, 54 plugins=None): 55 super(BasicConvBlock, self).__init__() 56 assert dcn is None, 'Not implemented yet.' 57 assert plugins is None, 'Not implemented yet.' 58 59 self.with_cp = with_cp
53 dcn=None, 54 plugins=None): 55 super(BasicConvBlock, self).__init__() 56 assert dcn is None, 'Not implemented yet.' 57 assert plugins is None, 'Not implemented yet.' 58 59 self.with_cp = with_cp 60 convs = []
112 kernel_size=4, 113 scale_factor=2): 114 super(DeconvModule, self).__init__() 115 116 assert (kernel_size - scale_factor >= 0) and\ 117 (kernel_size - scale_factor) % 2 == 0,\ 118 f'kernel_size should be greater than or equal to scale_factor '\ 119 f'and (kernel_size - scale_factor) should be even numbers, '\ 120 f'while the kernel size is {kernel_size} and scale_factor is '\ 121 f'{scale_factor}.' 122 123 stride = scale_factor 124 padding = (kernel_size - scale_factor) // 2
292 norm_eval=False, 293 dcn=None, 294 plugins=None): 295 super(UNet, self).__init__() 296 assert dcn is None, 'Not implemented yet.' 297 assert plugins is None, 'Not implemented yet.' 298 assert len(strides) == num_stages, \ 299 'The length of strides should be equal to num_stages, '\
293 dcn=None, 294 plugins=None): 295 super(UNet, self).__init__() 296 assert dcn is None, 'Not implemented yet.' 297 assert plugins is None, 'Not implemented yet.' 298 assert len(strides) == num_stages, \ 299 'The length of strides should be equal to num_stages, '\ 300 f'while the strides is {strides}, the length of '\
294 plugins=None): 295 super(UNet, self).__init__() 296 assert dcn is None, 'Not implemented yet.' 297 assert plugins is None, 'Not implemented yet.' 298 assert len(strides) == num_stages, \ 299 'The length of strides should be equal to num_stages, '\ 300 f'while the strides is {strides}, the length of '\ 301 f'strides is {len(strides)}, and the num_stages is '\ 302 f'{num_stages}.' 303 assert len(enc_num_convs) == num_stages, \ 304 'The length of enc_num_convs should be equal to num_stages, '\ 305 f'while the enc_num_convs is {enc_num_convs}, the length of '\
299 'The length of strides should be equal to num_stages, '\ 300 f'while the strides is {strides}, the length of '\ 301 f'strides is {len(strides)}, and the num_stages is '\ 302 f'{num_stages}.' 303 assert len(enc_num_convs) == num_stages, \ 304 'The length of enc_num_convs should be equal to num_stages, '\ 305 f'while the enc_num_convs is {enc_num_convs}, the length of '\ 306 f'enc_num_convs is {len(enc_num_convs)}, and the num_stages is '\ 307 f'{num_stages}.' 308 assert len(dec_num_convs) == (num_stages-1), \ 309 'The length of dec_num_convs should be equal to (num_stages-1), '\ 310 f'while the dec_num_convs is {dec_num_convs}, the length of '\
304 'The length of enc_num_convs should be equal to num_stages, '\ 305 f'while the enc_num_convs is {enc_num_convs}, the length of '\ 306 f'enc_num_convs is {len(enc_num_convs)}, and the num_stages is '\ 307 f'{num_stages}.' 308 assert len(dec_num_convs) == (num_stages-1), \ 309 'The length of dec_num_convs should be equal to (num_stages-1), '\ 310 f'while the dec_num_convs is {dec_num_convs}, the length of '\ 311 f'dec_num_convs is {len(dec_num_convs)}, and the num_stages is '\ 312 f'{num_stages}.' 313 assert len(downsamples) == (num_stages-1), \ 314 'The length of downsamples should be equal to (num_stages-1), '\ 315 f'while the downsamples is {downsamples}, the length of '\
309 'The length of dec_num_convs should be equal to (num_stages-1), '\ 310 f'while the dec_num_convs is {dec_num_convs}, the length of '\ 311 f'dec_num_convs is {len(dec_num_convs)}, and the num_stages is '\ 312 f'{num_stages}.' 313 assert len(downsamples) == (num_stages-1), \ 314 'The length of downsamples should be equal to (num_stages-1), '\ 315 f'while the downsamples is {downsamples}, the length of '\ 316 f'downsamples is {len(downsamples)}, and the num_stages is '\ 317 f'{num_stages}.' 318 assert len(enc_dilations) == num_stages, \ 319 'The length of enc_dilations should be equal to num_stages, '\ 320 f'while the enc_dilations is {enc_dilations}, the length of '\
314 'The length of downsamples should be equal to (num_stages-1), '\ 315 f'while the downsamples is {downsamples}, the length of '\ 316 f'downsamples is {len(downsamples)}, and the num_stages is '\ 317 f'{num_stages}.' 318 assert len(enc_dilations) == num_stages, \ 319 'The length of enc_dilations should be equal to num_stages, '\ 320 f'while the enc_dilations is {enc_dilations}, the length of '\ 321 f'enc_dilations is {len(enc_dilations)}, and the num_stages is '\ 322 f'{num_stages}.' 323 assert len(dec_dilations) == (num_stages-1), \ 324 'The length of dec_dilations should be equal to (num_stages-1), '\ 325 f'while the dec_dilations is {dec_dilations}, the length of '\
319 'The length of enc_dilations should be equal to num_stages, '\ 320 f'while the enc_dilations is {enc_dilations}, the length of '\ 321 f'enc_dilations is {len(enc_dilations)}, and the num_stages is '\ 322 f'{num_stages}.' 323 assert len(dec_dilations) == (num_stages-1), \ 324 'The length of dec_dilations should be equal to (num_stages-1), '\ 325 f'while the dec_dilations is {dec_dilations}, the length of '\ 326 f'dec_dilations is {len(dec_dilations)}, and the num_stages is '\ 327 f'{num_stages}.' 328 self.num_stages = num_stages 329 self.strides = strides 330 self.downsamples = downsamples
401 whole_downsample_rate = 1 402 for i in range(1, self.num_stages): 403 if self.strides[i] == 2 or self.downsamples[i - 1]: 404 whole_downsample_rate *= 2 405 assert (h % whole_downsample_rate == 0) \ 406 and (w % whole_downsample_rate == 0),\ 407 f'The input image size {(h, w)} should be divisible by the whole '\ 408 f'downsample rate {whole_downsample_rate}, when num_stages is '\ 409 f'{self.num_stages}, strides is {self.strides}, and downsamples '\ 410 f'is {self.downsamples}.' 411 412 def init_weights(self, pretrained=None): 413 """Initialize the weights in backbone.
370 shape of [B, L2, c]. 371 Return: 372 torch.Tensor: The pos encoded image feature. 373 """ 374 assert patched_img.ndim == 3 and pos_embed.ndim == 3, \ 375 'the shapes of patched_img and pos_embed must be [B, L, C]' 376 x_len, pos_len = patched_img.shape[1], pos_embed.shape[1] 377 if x_len != pos_len: 378 if pos_len == (self.img_size[0] // self.patch_size) * (
400 patch_size (int): Patch size. 401 Return: 402 torch.Tensor: The resized pos_embed of shape [B, L_new, C] 403 """ 404 assert pos_embed.ndim == 3, 'shape of pos_embed must be [B, L, C]' 405 input_h, input_w = input_shpae 406 pos_h, pos_w = pos_shape 407 cls_token_weight = pos_embed[:, 0]
37 if train_cfg is not None or test_cfg is not None: 38 warnings.warn( 39 'train_cfg and test_cfg is deprecated, ' 40 'please specify them in model', UserWarning) 41 assert cfg.get('train_cfg') is None or train_cfg is None, \ 42 'train_cfg specified in both outer field and model field ' 43 assert cfg.get('test_cfg') is None or test_cfg is None, \ 44 'test_cfg specified in both outer field and model field ' 45 return SEGMENTORS.build(
39 'train_cfg and test_cfg is deprecated, ' 40 'please specify them in model', UserWarning) 41 assert cfg.get('train_cfg') is None or train_cfg is None, \ 42 'train_cfg specified in both outer field and model field ' 43 assert cfg.get('test_cfg') is None or test_cfg is None, \ 44 'test_cfg specified in both outer field and model field ' 45 return SEGMENTORS.build( 46 cfg, default_args=dict(train_cfg=train_cfg, test_cfg=test_cfg))
201 key_pool_scales=(1, 3, 6, 8), 202 **kwargs): 203 super(ANNHead, self).__init__( 204 input_transform='multiple_select', **kwargs) 205 assert len(self.in_channels) == 2 206 low_in_channels, high_in_channels = self.in_channels 207 self.project_channels = project_channels 208 self.fusion = AFNB(
122 """ 123 124 def __init__(self, pool_scales=(1, 2, 3, 6), fusion=True, **kwargs): 125 super(APCHead, self).__init__(**kwargs) 126 assert isinstance(pool_scales, (list, tuple)) 127 self.pool_scales = pool_scales 128 self.fusion = fusion 129 acm_modules = []
62 """ 63 64 def __init__(self, dilations=(1, 6, 12, 18), **kwargs): 65 super(ASPPHead, self).__init__(**kwargs) 66 assert isinstance(dilations, (list, tuple)) 67 self.dilations = dilations 68 self.image_pool = nn.Sequential( 69 nn.AdaptiveAvgPool2d(1),
113 None: Only one select feature map is allowed. 114 """ 115 116 if input_transform is not None: 117 assert input_transform in ['resize_concat', 'multiple_select'] 118 self.input_transform = input_transform 119 self.in_index = in_index 120 if input_transform is not None:
117 assert input_transform in ['resize_concat', 'multiple_select'] 118 self.input_transform = input_transform 119 self.in_index = in_index 120 if input_transform is not None: 121 assert isinstance(in_channels, (list, tuple)) 122 assert isinstance(in_index, (list, tuple)) 123 assert len(in_channels) == len(in_index) 124 if input_transform == 'resize_concat':
118 self.input_transform = input_transform 119 self.in_index = in_index 120 if input_transform is not None: 121 assert isinstance(in_channels, (list, tuple)) 122 assert isinstance(in_index, (list, tuple)) 123 assert len(in_channels) == len(in_index) 124 if input_transform == 'resize_concat': 125 self.in_channels = sum(in_channels)
119 self.in_index = in_index 120 if input_transform is not None: 121 assert isinstance(in_channels, (list, tuple)) 122 assert isinstance(in_index, (list, tuple)) 123 assert len(in_channels) == len(in_index) 124 if input_transform == 'resize_concat': 125 self.in_channels = sum(in_channels) 126 else:
125 self.in_channels = sum(in_channels) 126 else: 127 self.in_channels = in_channels 128 else: 129 assert isinstance(in_channels, int) 130 assert isinstance(in_index, int) 131 self.in_channels = in_channels 132
126 else: 127 self.in_channels = in_channels 128 else: 129 assert isinstance(in_channels, int) 130 assert isinstance(in_index, int) 131 self.in_channels = in_channels 132 133 def init_weights(self):
104 """ 105 106 def __init__(self, filter_sizes=(1, 3, 5, 7), fusion=False, **kwargs): 107 super(DMHead, self).__init__(**kwargs) 108 assert isinstance(filter_sizes, (list, tuple)) 109 self.filter_sizes = filter_sizes 110 self.fusion = fusion 111 dcm_modules = []
29 """ 30 31 def __init__(self, channels, num_bases, num_stages, momentum): 32 super(EMAModule, self).__init__() 33 assert num_stages >= 1, 'num_stages must be at least 1!' 34 self.num_bases = num_bases 35 self.num_stages = num_stages 36 self.momentum = momentum
25 kernel_size=3, 26 concat_input=True, 27 dilation=1, 28 **kwargs): 29 assert num_convs >= 0 and dilation > 0 and isinstance(dilation, int) 30 self.num_convs = num_convs 31 self.concat_input = concat_input 32 self.kernel_size = kernel_size
31 self.concat_input = concat_input 32 self.kernel_size = kernel_size 33 super(FCNHead, self).__init__(**kwargs) 34 if num_convs == 0: 35 assert self.in_channels == self.channels 36 37 conv_padding = (kernel_size // 2) * dilation 38 convs = []
22 23 def __init__(self, feature_strides, **kwargs): 24 super(FPNHead, self).__init__( 25 input_transform='multiple_select', **kwargs) 26 assert len(feature_strides) == len(self.in_channels) 27 assert min(feature_strides) == feature_strides[0] 28 self.feature_strides = feature_strides 29
23 def __init__(self, feature_strides, **kwargs): 24 super(FPNHead, self).__init__( 25 input_transform='multiple_select', **kwargs) 26 assert len(feature_strides) == len(self.in_channels) 27 assert min(feature_strides) == feature_strides[0] 28 self.feature_strides = feature_strides 29 30 self.scale_heads = nn.ModuleList()
25 if self.input_transform != 'multiple_select': 26 raise ValueError('in Lite R-ASPP (LRASPP) head, input_transform ' 27 f'must be \'multiple_select\'. But received ' 28 f'\'{self.input_transform}\'') 29 assert is_tuple_of(branch_channels, int) 30 assert len(branch_channels) == len(self.in_channels) - 1 31 self.branch_channels = branch_channels 32
26 raise ValueError('in Lite R-ASPP (LRASPP) head, input_transform ' 27 f'must be \'multiple_select\'. But received ' 28 f'\'{self.input_transform}\'') 29 assert is_tuple_of(branch_channels, int) 30 assert len(branch_channels) == len(self.in_channels) - 1 31 self.branch_channels = branch_channels 32 33 self.convs = nn.Sequential()
275 """ 276 num_points = cfg.num_points 277 oversample_ratio = cfg.oversample_ratio 278 importance_sample_ratio = cfg.importance_sample_ratio 279 assert oversample_ratio >= 1 280 assert 0 <= importance_sample_ratio <= 1 281 batch_size = seg_logits.shape[0] 282 num_sampled = int(num_points * oversample_ratio)
276 num_points = cfg.num_points 277 oversample_ratio = cfg.oversample_ratio 278 importance_sample_ratio = cfg.importance_sample_ratio 279 assert oversample_ratio >= 1 280 assert 0 <= importance_sample_ratio <= 1 281 batch_size = seg_logits.shape[0] 282 num_sampled = int(num_points * oversample_ratio) 283 point_coords = torch.rand(
42 **kwargs): 43 if PSAMask is None: 44 raise RuntimeError('Please install mmcv-full for PSAMask ops') 45 super(PSAHead, self).__init__(**kwargs) 46 assert psa_type in ['collect', 'distribute', 'bi-direction'] 47 self.psa_type = psa_type 48 self.compact = compact 49 self.shrink_factor = shrink_factor
70 """ 71 72 def __init__(self, pool_scales=(1, 2, 3, 6), **kwargs): 73 super(PSPHead, self).__init__(**kwargs) 74 assert isinstance(pool_scales, (list, tuple)) 75 self.pool_scales = pool_scales 76 self.psp_modules = PPM( 77 self.pool_scales,
40 """ 41 42 def __init__(self, c1_in_channels, c1_channels, **kwargs): 43 super(DepthwiseSeparableASPPHead, self).__init__(**kwargs) 44 assert c1_in_channels >= 0 45 self.aspp_modules = DepthwiseSeparableASPPModule( 46 dilations=self.dilations, 47 in_channels=self.in_channels,
19 ``topk`` is a tuple containing multiple integers, the 20 function will return a tuple containing accuracies of 21 each ``topk`` number. 22 """ 23 assert isinstance(topk, (int, tuple)) 24 if isinstance(topk, int): 25 topk = (topk, ) 26 return_single = True
30 maxk = max(topk) 31 if pred.size(0) == 0: 32 accu = [pred.new_tensor(0.) for i in range(len(topk))] 33 return accu[0] if return_single else accu 34 assert pred.ndim == target.ndim + 1 35 assert pred.size(0) == target.size(0) 36 assert maxk <= pred.size(1), \ 37 f'maxk {maxk} exceeds pred dimension {pred.size(1)}'
31 if pred.size(0) == 0: 32 accu = [pred.new_tensor(0.) for i in range(len(topk))] 33 return accu[0] if return_single else accu 34 assert pred.ndim == target.ndim + 1 35 assert pred.size(0) == target.size(0) 36 assert maxk <= pred.size(1), \ 37 f'maxk {maxk} exceeds pred dimension {pred.size(1)}' 38 pred_value, pred_label = pred.topk(maxk, dim=1)
32 accu = [pred.new_tensor(0.) for i in range(len(topk))] 33 return accu[0] if return_single else accu 34 assert pred.ndim == target.ndim + 1 35 assert pred.size(0) == target.size(0) 36 assert maxk <= pred.size(1), \ 37 f'maxk {maxk} exceeds pred dimension {pred.size(1)}' 38 pred_value, pred_label = pred.topk(maxk, dim=1) 39 # transpose to shape (maxk, N, ...) 40 pred_label = pred_label.transpose(0, 1)
77 Returns: 78 torch.Tensor: The calculated loss 79 """ 80 if pred.dim() != label.dim(): 81 assert (pred.dim() == 2 and label.dim() == 1) or ( 82 pred.dim() == 4 and label.dim() == 3), \ 83 'Only pred shape [N, C], label shape [N] or pred shape [N, C, ' \ 84 'H, W], label shape [N, H, W] are supported' 85 label, weight = _expand_onehot_labels(label, weight, pred.shape, 86 ignore_index) 87
124 125 Returns: 126 torch.Tensor: The calculated loss 127 """ 128 assert ignore_index is None, 'BCE loss does not support ignore_index' 129 # TODO: handle these two reserved arguments 130 assert reduction == 'mean' and avg_factor is None 131 num_rois = pred.size()[0]
126 torch.Tensor: The calculated loss 127 """ 128 assert ignore_index is None, 'BCE loss does not support ignore_index' 129 # TODO: handle these two reserved arguments 130 assert reduction == 'mean' and avg_factor is None 131 num_rois = pred.size()[0] 132 inds = torch.arange(0, num_rois, dtype=torch.long, device=pred.device) 133 pred_slice = pred[inds, label].squeeze(1)
157 reduction='mean', 158 class_weight=None, 159 loss_weight=1.0): 160 super(CrossEntropyLoss, self).__init__() 161 assert (use_sigmoid is False) or (use_mask is False) 162 self.use_sigmoid = use_sigmoid 163 self.use_mask = use_mask 164 self.reduction = reduction
179 avg_factor=None, 180 reduction_override=None, 181 **kwargs): 182 """Forward function.""" 183 assert reduction_override in (None, 'none', 'mean', 'sum') 184 reduction = ( 185 reduction_override if reduction_override else self.reduction) 186 if self.class_weight is not None:
15 smooth=1, 16 exponent=2, 17 class_weight=None, 18 ignore_index=255): 19 assert pred.shape[0] == target.shape[0] 20 total_loss = 0 21 num_classes = pred.shape[1] 22 for i in range(num_classes):
34 35 36 @weighted_loss 37 def binary_dice_loss(pred, target, valid_mask, smooth=1, exponent=2, **kwards): 38 assert pred.shape[0] == target.shape[0] 39 pred = pred.reshape(pred.shape[0], -1) 40 target = target.reshape(target.shape[0], -1) 41 valid_mask = valid_mask.reshape(valid_mask.shape[0], -1)
90 target, 91 avg_factor=None, 92 reduction_override=None, 93 **kwards): 94 assert reduction_override in (None, 'none', 'mean', 'sum') 95 reduction = ( 96 reduction_override if reduction_override else self.reduction) 97 if self.class_weight is not None:
252 reduction='mean', 253 class_weight=None, 254 loss_weight=1.0): 255 super(LovaszLoss, self).__init__() 256 assert loss_type in ('binary', 'multi_class'), "loss_type should be \ 257 'binary' or 'multi_class'." 258 259 if loss_type == 'binary': 260 self.cls_criterion = lovasz_hinge
259 if loss_type == 'binary': 260 self.cls_criterion = lovasz_hinge 261 else: 262 self.cls_criterion = lovasz_softmax 263 assert classes in ('all', 'present') or mmcv.is_list_of(classes, int) 264 if not per_image: 265 assert reduction == 'none', "reduction should be 'none' when \ 266 per_image is False."
261 else: 262 self.cls_criterion = lovasz_softmax 263 assert classes in ('all', 'present') or mmcv.is_list_of(classes, int) 264 if not per_image: 265 assert reduction == 'none', "reduction should be 'none' when \ 266 per_image is False." 267 268 self.classes = classes 269 self.per_image = per_image
278 avg_factor=None, 279 reduction_override=None, 280 **kwargs): 281 """Forward function.""" 282 assert reduction_override in (None, 'none', 'mean', 'sum') 283 reduction = ( 284 reduction_override if reduction_override else self.reduction) 285 if self.class_weight is not None:
56 Tensor: Processed loss values. 57 """ 58 # if weight is specified, apply element-wise weight 59 if weight is not None: 60 assert weight.dim() == loss.dim() 61 if weight.dim() > 1: 62 assert weight.size(1) == 1 or weight.size(1) == loss.size(1) 63 loss = loss * weight
58 # if weight is specified, apply element-wise weight 59 if weight is not None: 60 assert weight.dim() == loss.dim() 61 if weight.dim() > 1: 62 assert weight.size(1) == 1 or weight.size(1) == loss.size(1) 63 loss = loss * weight 64 65 # if avg_factor is not specified, just reduce the loss
74 norm_cfg=None, 75 act_cfg=None, 76 upsample_cfg=dict(mode='nearest')): 77 super(FPN, self).__init__() 78 assert isinstance(in_channels, list) 79 self.in_channels = in_channels 80 self.out_channels = out_channels 81 self.num_ins = len(in_channels)
86 self.upsample_cfg = upsample_cfg.copy() 87 88 if end_level == -1: 89 self.backbone_end_level = self.num_ins 90 assert num_outs >= self.num_ins - start_level 91 else: 92 # if end_level < inputs, no extra level is allowed 93 self.backbone_end_level = end_level
90 assert num_outs >= self.num_ins - start_level 91 else: 92 # if end_level < inputs, no extra level is allowed 93 self.backbone_end_level = end_level 94 assert end_level <= len(in_channels) 95 assert num_outs == end_level - start_level 96 self.start_level = start_level 97 self.end_level = end_level
91 else: 92 # if end_level < inputs, no extra level is allowed 93 self.backbone_end_level = end_level 94 assert end_level <= len(in_channels) 95 assert num_outs == end_level - start_level 96 self.start_level = start_level 97 self.end_level = end_level 98 self.add_extra_convs = add_extra_convs
95 assert num_outs == end_level - start_level 96 self.start_level = start_level 97 self.end_level = end_level 98 self.add_extra_convs = add_extra_convs 99 assert isinstance(add_extra_convs, (str, bool)) 100 if isinstance(add_extra_convs, str): 101 # Extra_convs_source choices: 'on_input', 'on_lateral', 'on_output' 102 assert add_extra_convs in ('on_input', 'on_lateral', 'on_output')
98 self.add_extra_convs = add_extra_convs 99 assert isinstance(add_extra_convs, (str, bool)) 100 if isinstance(add_extra_convs, str): 101 # Extra_convs_source choices: 'on_input', 'on_lateral', 'on_output' 102 assert add_extra_convs in ('on_input', 'on_lateral', 'on_output') 103 elif add_extra_convs: # True 104 if extra_convs_on_inputs: 105 # For compatibility with previous release
159 if isinstance(m, nn.Conv2d): 160 xavier_init(m, distribution='uniform') 161 162 def forward(self, inputs): 163 assert len(inputs) == len(self.in_channels) 164 165 # build laterals 166 laterals = [
25 scales=[0.5, 1, 2, 4], 26 norm_cfg=None, 27 act_cfg=None): 28 super(MultiLevelNeck, self).__init__() 29 assert isinstance(in_channels, list) 30 self.in_channels = in_channels 31 self.out_channels = out_channels 32 self.scales = scales
52 norm_cfg=norm_cfg, 53 act_cfg=act_cfg)) 54 55 def forward(self, inputs): 56 assert len(inputs) == len(self.in_channels) 57 print(inputs[0].shape) 58 inputs = [ 59 lateral_conv(inputs[i])
95 # all images in the same aug batch all of the same ori_shape and pad 96 # shape 97 for img_meta in img_metas: 98 ori_shapes = [_['ori_shape'] for _ in img_meta] 99 assert all(shape == ori_shapes[0] for shape in ori_shapes) 100 img_shapes = [_['img_shape'] for _ in img_meta] 101 assert all(shape == img_shapes[0] for shape in img_shapes) 102 pad_shapes = [_['pad_shape'] for _ in img_meta]
97 for img_meta in img_metas: 98 ori_shapes = [_['ori_shape'] for _ in img_meta] 99 assert all(shape == ori_shapes[0] for shape in ori_shapes) 100 img_shapes = [_['img_shape'] for _ in img_meta] 101 assert all(shape == img_shapes[0] for shape in img_shapes) 102 pad_shapes = [_['pad_shape'] for _ in img_meta] 103 assert all(shape == pad_shapes[0] for shape in pad_shapes) 104
99 assert all(shape == ori_shapes[0] for shape in ori_shapes) 100 img_shapes = [_['img_shape'] for _ in img_meta] 101 assert all(shape == img_shapes[0] for shape in img_shapes) 102 pad_shapes = [_['pad_shape'] for _ in img_meta] 103 assert all(shape == pad_shapes[0] for shape in pad_shapes) 104 105 if num_augs == 1: 106 return self.simple_test(imgs[0], img_metas[0], **kwargs)
245 0, 255, size=(len(self.CLASSES), 3)) 246 else: 247 palette = self.PALETTE 248 palette = np.array(palette) 249 assert palette.shape[0] == len(self.CLASSES) 250 assert palette.shape[1] == 3 251 assert len(palette.shape) == 2 252 assert 0 < opacity <= 1.0
246 else: 247 palette = self.PALETTE 248 palette = np.array(palette) 249 assert palette.shape[0] == len(self.CLASSES) 250 assert palette.shape[1] == 3 251 assert len(palette.shape) == 2 252 assert 0 < opacity <= 1.0 253 color_seg = np.zeros((seg.shape[0], seg.shape[1], 3), dtype=np.uint8)
247 palette = self.PALETTE 248 palette = np.array(palette) 249 assert palette.shape[0] == len(self.CLASSES) 250 assert palette.shape[1] == 3 251 assert len(palette.shape) == 2 252 assert 0 < opacity <= 1.0 253 color_seg = np.zeros((seg.shape[0], seg.shape[1], 3), dtype=np.uint8) 254 for label, color in enumerate(palette):
248 palette = np.array(palette) 249 assert palette.shape[0] == len(self.CLASSES) 250 assert palette.shape[1] == 3 251 assert len(palette.shape) == 2 252 assert 0 < opacity <= 1.0 253 color_seg = np.zeros((seg.shape[0], seg.shape[1], 3), dtype=np.uint8) 254 for label, color in enumerate(palette): 255 color_seg[seg == label, :] = color
36 pretrained=pretrained) 37 38 def _init_decode_head(self, decode_head): 39 """Initialize ``decode_head``""" 40 assert isinstance(decode_head, list) 41 assert len(decode_head) == self.num_stages 42 self.decode_head = nn.ModuleList() 43 for i in range(self.num_stages):
37 38 def _init_decode_head(self, decode_head): 39 """Initialize ``decode_head``""" 40 assert isinstance(decode_head, list) 41 assert len(decode_head) == self.num_stages 42 self.decode_head = nn.ModuleList() 43 for i in range(self.num_stages): 44 self.decode_head.append(builder.build_head(decode_head[i]))
37 self.test_cfg = test_cfg 38 39 self.init_weights(pretrained=pretrained) 40 41 assert self.with_decode_head 42 43 def _init_decode_head(self, decode_head): 44 """Initialize ``decode_head``"""
195 (int(x1), int(preds.shape[3] - x2), int(y1), 196 int(preds.shape[2] - y2))) 197 198 count_mat[:, :, y1:y2, x1:x2] += 1 199 assert (count_mat == 0).sum() == 0 200 if torch.onnx.is_in_onnx_export(): 201 # cast count_mat to constant while exporting to ONNX 202 count_mat = torch.from_numpy(
245 Returns: 246 Tensor: The output segmentation map. 247 """ 248 249 assert self.test_cfg.mode in ['slide', 'whole'] 250 ori_shape = img_meta[0]['ori_shape'] 251 assert all(_['ori_shape'] == ori_shape for _ in img_meta) 252 if self.test_cfg.mode == 'slide':
247 """ 248 249 assert self.test_cfg.mode in ['slide', 'whole'] 250 ori_shape = img_meta[0]['ori_shape'] 251 assert all(_['ori_shape'] == ori_shape for _ in img_meta) 252 if self.test_cfg.mode == 'slide': 253 seg_logit = self.slide_inference(img, img_meta, rescale) 254 else:
256 output = F.softmax(seg_logit, dim=1) 257 flip = img_meta[0]['flip'] 258 if flip: 259 flip_direction = img_meta[0]['flip_direction'] 260 assert flip_direction in ['horizontal', 'vertical'] 261 if flip_direction == 'horizontal': 262 output = output.flip(dims=(3, )) 263 elif flip_direction == 'vertical':
283 284 Only rescale=True is supported. 285 """ 286 # aug_test rescale all imgs back to ori_shape for now 287 assert rescale 288 # to save memory, we get augmented seg logit inplace 289 seg_logit = self.inference(imgs[0], img_metas[0], rescale) 290 for i in range(1, len(imgs)):
39 act_cfg=dict(type='ReLU6'), 40 with_cp=False): 41 super(InvertedResidual, self).__init__() 42 self.stride = stride 43 assert stride in [1, 2], f'stride must in [1, 2]. ' \ 44 f'But received {stride}.' 45 self.with_cp = with_cp 46 self.use_res_connect = self.stride == 1 and in_channels == out_channels 47 hidden_dim = int(round(in_channels * expand_ratio))
134 act_cfg=dict(type='ReLU'), 135 with_cp=False): 136 super(InvertedResidualV3, self).__init__() 137 self.with_res_shortcut = (stride == 1 and in_channels == out_channels) 138 assert stride in [1, 2] 139 self.with_cp = with_cp 140 self.with_se = se_cfg is not None 141 self.with_expand_conv = with_expand_conv
140 self.with_se = se_cfg is not None 141 self.with_expand_conv = with_expand_conv 142 143 if self.with_se: 144 assert isinstance(se_cfg, dict) 145 if not self.with_expand_conv: 146 assert mid_channels == in_channels 147
142 143 if self.with_se: 144 assert isinstance(se_cfg, dict) 145 if not self.with_expand_conv: 146 assert mid_channels == in_channels 147 148 if self.with_expand_conv: 149 self.expand_conv = ConvModule(
31 dict(type='HSigmoid', bias=3.0, divisor=6.0))): 32 super(SELayer, self).__init__() 33 if isinstance(act_cfg, dict): 34 act_cfg = (act_cfg, act_cfg) 35 assert len(act_cfg) == 2 36 assert mmcv.is_tuple_of(act_cfg, dict) 37 self.global_avgpool = nn.AdaptiveAvgPool2d(1) 38 self.conv1 = ConvModule(
32 super(SELayer, self).__init__() 33 if isinstance(act_cfg, dict): 34 act_cfg = (act_cfg, act_cfg) 35 assert len(act_cfg) == 2 36 assert mmcv.is_tuple_of(act_cfg, dict) 37 self.global_avgpool = nn.AdaptiveAvgPool2d(1) 38 self.conv1 = ConvModule( 39 in_channels=channels,
35 key_query_norm, value_out_norm, matmul_norm, with_out, 36 conv_cfg, norm_cfg, act_cfg): 37 super(SelfAttentionBlock, self).__init__() 38 if share_key_query: 39 assert key_in_channels == query_in_channels 40 self.key_in_channels = key_in_channels 41 self.query_in_channels = query_in_channels 42 self.out_channels = out_channels
56 upsample_cfg=dict(type='InterpConv'), 57 dcn=None, 58 plugins=None): 59 super(UpConvBlock, self).__init__() 60 assert dcn is None, 'Not implemented yet.' 61 assert plugins is None, 'Not implemented yet.' 62 63 self.conv_block = conv_block(
57 dcn=None, 58 plugins=None): 59 super(UpConvBlock, self).__init__() 60 assert dcn is None, 'Not implemented yet.' 61 assert plugins is None, 'Not implemented yet.' 62 63 self.conv_block = conv_block( 64 in_channels=2 * skip_channels,
54 (expanded_x - reshaped_codewords)).sum(dim=1) 55 return encoded_feat 56 57 def forward(self, x): 58 assert x.dim() == 4 and x.size(1) == self.channels 59 # [batch_size, channels, height, width] 60 batch_size = x.size(0) 61 # [batch_size, height x width, channels]
102 dataset = DatasetFromList(dataset, copy=False) 103 if mapper is not None: 104 dataset = MapDataset(dataset, mapper) 105 if isinstance(dataset, torchdata.IterableDataset): 106 assert sampler is None, "sampler must be None if dataset is IterableDataset" 107 else: 108 if sampler is None: 109 sampler = InferenceSampler(len(dataset))
26 Now it includes resizing and flipping. 27 Returns: 28 list[Augmentation] 29 """ 30 assert is_train, "Only support training augmentation" 31 image_size = cfg.INPUT.IMAGE_SIZE 32 min_scale = cfg.INPUT.MIN_SCALE 33 max_scale = cfg.INPUT.MAX_SCALE
68 recompute_boxes: whether to overwrite bounding box annotations 69 by computing tight bounding boxes from instance mask annotations. 70 """ 71 if recompute_boxes: 72 assert use_instance_mask, "recompute_boxes requires instance masks" 73 # fmt: off 74 self.is_train = is_train 75 self.augmentations = T.AugmentationList(augmentations)
81 self.proposal_topk = precomputed_proposal_topk 82 self.recompute_boxes = recompute_boxes 83 self.task_tokenizer = Tokenize(SimpleTokenizer(), max_seq_len=task_seq_len) 84 self.task = task 85 assert self.task in ["panoptic", "semantic", "instance"] 86 87 # fmt: on 88 logger = logging.getLogger(__name__)
275 276 Returns: 277 dict: a format that builtin models in detectron2 accept 278 """ 279 assert self.is_train, "OneFormerUnifiedDatasetMapper should only be used for training!" 280 281 dataset_dict = copy.deepcopy(dataset_dict) # it will be modified by code below 282 image = utils.read_image(dataset_dict["file_name"], format=self.img_format)
29 30 31 def _get_ade_instances_meta(): 32 thing_ids = [k["id"] for k in ADE_CATEGORIES] 33 assert len(thing_ids) == 100, len(thing_ids) 34 # Mapping from the incontiguous ADE category id to an id in [0, 99] 35 thing_dataset_id_to_contiguous_id = {k: i for i, k in enumerate(thing_ids)} 36 thing_classes = [k["name"] for k in ADE_CATEGORIES]
264 "sem_seg_file_name": sem_label_file, 265 "segments_info": segments_info, 266 } 267 ) 268 assert len(ret), f"No images found in {image_dir}!" 269 assert PathManager.isfile(ret[0]["file_name"]), ret[0]["file_name"] 270 assert PathManager.isfile(ret[0]["pan_seg_file_name"]), ret[0]["pan_seg_file_name"] 271 assert PathManager.isfile(ret[0]["sem_seg_file_name"]), ret[0]["sem_seg_file_name"]
265 "segments_info": segments_info, 266 } 267 ) 268 assert len(ret), f"No images found in {image_dir}!" 269 assert PathManager.isfile(ret[0]["file_name"]), ret[0]["file_name"] 270 assert PathManager.isfile(ret[0]["pan_seg_file_name"]), ret[0]["pan_seg_file_name"] 271 assert PathManager.isfile(ret[0]["sem_seg_file_name"]), ret[0]["sem_seg_file_name"] 272 return ret
266 } 267 ) 268 assert len(ret), f"No images found in {image_dir}!" 269 assert PathManager.isfile(ret[0]["file_name"]), ret[0]["file_name"] 270 assert PathManager.isfile(ret[0]["pan_seg_file_name"]), ret[0]["pan_seg_file_name"] 271 assert PathManager.isfile(ret[0]["sem_seg_file_name"]), ret[0]["sem_seg_file_name"] 272 return ret 273
267 ) 268 assert len(ret), f"No images found in {image_dir}!" 269 assert PathManager.isfile(ret[0]["file_name"]), ret[0]["file_name"] 270 assert PathManager.isfile(ret[0]["pan_seg_file_name"]), ret[0]["pan_seg_file_name"] 271 assert PathManager.isfile(ret[0]["sem_seg_file_name"]), ret[0]["sem_seg_file_name"] 272 return ret 273 274
30 for basename in PathManager.ls(city_img_dir): 31 image_file = os.path.join(city_img_dir, basename) 32 33 suffix = "_leftImg8bit.png" 34 assert basename.endswith(suffix), basename 35 basename = os.path.basename(basename)[: -len(suffix)] 36 37 image_dict[basename] = image_file
37 image_dict[basename] = image_file 38 39 for ann in json_info["annotations"]: 40 image_file = image_dict.get(ann["image_id"], None) 41 assert image_file is not None, "No image {} found for annotation {}".format( 42 ann["image_id"], ann["file_name"] 43 ) 44 label_file = os.path.join(gt_dir, ann["file_name"]) 45 segments_info = ann["segments_info"] 46 files.append((image_file, label_file, segments_info))
44 label_file = os.path.join(gt_dir, ann["file_name"]) 45 segments_info = ann["segments_info"] 46 files.append((image_file, label_file, segments_info)) 47 48 assert len(files), "No images found in {}".format(image_dir) 49 assert PathManager.isfile(files[0][0]), files[0][0] 50 assert PathManager.isfile(files[0][1]), files[0][1] 51 return files
45 segments_info = ann["segments_info"] 46 files.append((image_file, label_file, segments_info)) 47 48 assert len(files), "No images found in {}".format(image_dir) 49 assert PathManager.isfile(files[0][0]), files[0][0] 50 assert PathManager.isfile(files[0][1]), files[0][1] 51 return files 52
46 files.append((image_file, label_file, segments_info)) 47 48 assert len(files), "No images found in {}".format(image_dir) 49 assert PathManager.isfile(files[0][0]), files[0][0] 50 assert PathManager.isfile(files[0][1]), files[0][1] 51 return files 52 53
78 segment_info["category_id"] 79 ] 80 return segment_info 81 82 assert os.path.exists( 83 gt_json 84 ), "Please run `python cityscapesscripts/preparation/createPanopticImgs.py` to generate label files." # noqa 85 86 87 with open(gt_json) as f:
104 "pan_seg_file_name": label_file, 105 "segments_info": segments_info, 106 } 107 ) 108 assert len(ret), f"No images found in {image_dir}!" 109 assert PathManager.isfile( 110 ret[0]["sem_seg_file_name"] 111 ), "Please generate labelTrainIds.png with cityscapesscripts/preparation/createTrainIdLabelImgs.py" # noqa
105 "segments_info": segments_info, 106 } 107 ) 108 assert len(ret), f"No images found in {image_dir}!" 109 assert PathManager.isfile( 110 ret[0]["sem_seg_file_name"] 111 ), "Please generate labelTrainIds.png with cityscapesscripts/preparation/createTrainIdLabelImgs.py" # noqa 112 assert PathManager.isfile( 113 ret[0]["pan_seg_file_name"] 114 ), "Please generate panoptic annotation with python cityscapesscripts/preparation/createPanopticImgs.py" # noqa
108 assert len(ret), f"No images found in {image_dir}!" 109 assert PathManager.isfile( 110 ret[0]["sem_seg_file_name"] 111 ), "Please generate labelTrainIds.png with cityscapesscripts/preparation/createTrainIdLabelImgs.py" # noqa 112 assert PathManager.isfile( 113 ret[0]["pan_seg_file_name"] 114 ), "Please generate panoptic annotation with python cityscapesscripts/preparation/createPanopticImgs.py" # noqa 115 return ret 116 117
116 # The popular valminusminival & minival annotations for COCO2014 contain this bug. 117 # However the ratio of buggy annotations there is tiny and does not affect accuracy. 118 # Therefore we explicitly white-list them. 119 ann_ids = [ann["id"] for anns_per_image in anns for ann in anns_per_image] 120 assert len(set(ann_ids)) == len(ann_ids), "Annotation ids in '{}' are not unique!".format( 121 json_file 122 ) 123 124 imgs_anns = list(zip(imgs, anns)) 125 logger.info("Loaded {} images in COCO format from {}".format(len(imgs_anns), json_file))
145 146 # The original COCO valminusminival2014 & minival2014 annotation files 147 # actually contains bugs that, together with certain ways of using COCO API, 148 # can trigger this assertion. 149 assert anno["image_id"] == image_id 150 151 assert anno.get("ignore", 0) == 0, '"ignore" in COCO json file is not supported.' 152
147 # actually contains bugs that, together with certain ways of using COCO API, 148 # can trigger this assertion. 149 assert anno["image_id"] == image_id 150 151 assert anno.get("ignore", 0) == 0, '"ignore" in COCO json file is not supported.' 152 153 obj = {key: anno[key] for key in ann_keys if key in anno} 154 if "bbox" in obj and len(obj["bbox"]) == 0:
299 "segments_info": segments_info, 300 "annotations": instance_data_dicts[image_id]["annotations"], 301 } 302 ) 303 assert len(ret), f"No images found in {image_dir}!" 304 assert PathManager.isfile(ret[0]["file_name"]), ret[0]["file_name"] 305 assert PathManager.isfile(ret[0]["pan_seg_file_name"]), ret[0]["pan_seg_file_name"] 306 assert PathManager.isfile(ret[0]["sem_seg_file_name"]), ret[0]["sem_seg_file_name"]
300 "annotations": instance_data_dicts[image_id]["annotations"], 301 } 302 ) 303 assert len(ret), f"No images found in {image_dir}!" 304 assert PathManager.isfile(ret[0]["file_name"]), ret[0]["file_name"] 305 assert PathManager.isfile(ret[0]["pan_seg_file_name"]), ret[0]["pan_seg_file_name"] 306 assert PathManager.isfile(ret[0]["sem_seg_file_name"]), ret[0]["sem_seg_file_name"] 307 return ret
301 } 302 ) 303 assert len(ret), f"No images found in {image_dir}!" 304 assert PathManager.isfile(ret[0]["file_name"]), ret[0]["file_name"] 305 assert PathManager.isfile(ret[0]["pan_seg_file_name"]), ret[0]["pan_seg_file_name"] 306 assert PathManager.isfile(ret[0]["sem_seg_file_name"]), ret[0]["sem_seg_file_name"] 307 return ret 308
302 ) 303 assert len(ret), f"No images found in {image_dir}!" 304 assert PathManager.isfile(ret[0]["file_name"]), ret[0]["file_name"] 305 assert PathManager.isfile(ret[0]["pan_seg_file_name"]), ret[0]["pan_seg_file_name"] 306 assert PathManager.isfile(ret[0]["sem_seg_file_name"]), ret[0]["sem_seg_file_name"] 307 return ret 308 309
115 maximum (int): either 255 or 1 116 Returns: 117 ndarray: a float32 array of Nx3 colors, in range [0, 255] or [0, 1] 118 """ 119 assert maximum in [255, 1], maximum 120 c = _COLORS * maximum 121 if not rgb: 122 c = c[:, ::-1]
49 [cfg.INPUT.MIN_SIZE_TEST, cfg.INPUT.MIN_SIZE_TEST], cfg.INPUT.MAX_SIZE_TEST 50 ) 51 52 self.input_format = cfg.INPUT.FORMAT 53 assert self.input_format in ["RGB", "BGR"], self.input_format 54 55 def __call__(self, original_image, task): 56 """
84 85 m = mask_or_polygons 86 if isinstance(m, dict): 87 # RLEs 88 assert "counts" in m and "size" in m 89 if isinstance(m["counts"], list): # uncompressed RLEs 90 h, w = m["size"] 91 assert h == height and w == width
87 # RLEs 88 assert "counts" in m and "size" in m 89 if isinstance(m["counts"], list): # uncompressed RLEs 90 h, w = m["size"] 91 assert h == height and w == width 92 m = mask_util.frPyObjects(m, h, w) 93 self._mask = mask_util.decode(m)[:, :] 94 return
97 self._polygons = [np.asarray(x).reshape(-1) for x in m] 98 return 99 100 if isinstance(m, np.ndarray): # assumed to be a binary mask 101 assert m.shape[1] != 2, m.shape 102 assert m.shape == ( 103 height, 104 width,
98 return 99 100 if isinstance(m, np.ndarray): # assumed to be a binary mask 101 assert m.shape[1] != 2, m.shape 102 assert m.shape == ( 103 height, 104 width, 105 ), f"mask shape: {m.shape}, target dims: {height}, {width}" 106 self._mask = m.astype("uint8") 107 return 108
171 """ 172 173 def __init__(self, panoptic_seg, segments_info, metadata=None): 174 if segments_info is None: 175 assert metadata is not None 176 # If "segments_info" is None, we assume "panoptic_img" is a 177 # H*W int32 image storing the panoptic_id in the format of 178 # category_id * label_divisor + instance_id. We reserve -1 for
216 if id not in self._sinfo: 217 empty_ids.append(id) 218 if len(empty_ids) == 0: 219 return np.zeros(self._seg.shape, dtype=np.uint8) 220 assert ( 221 len(empty_ids) == 1 222 ), ">1 ids corresponds to no labels. This is currently not supported" 223 return (self._seg != empty_ids[0]).numpy().astype(np.bool) 224 225 def semantic_masks(self):
420 classes = labels.tolist() 421 scores = scores.tolist() 422 labels = _create_text_labels(classes, scores, self.metadata.get("stuff_classes", None)) 423 num_instances = len(boxes) 424 assert len(labels) == num_instances 425 if assigned_colors is None: 426 # assigned_colors = [random_color(rgb=True, maximum=1) for _ in range(num_instances)] 427 assigned_colors = [instance_color(rgb=True, idx=i, maximum=1) for i in range(num_instances)]
744 num_instances = len(boxes) 745 if masks is not None: 746 masks = self._convert_masks(masks) 747 if num_instances: 748 assert len(masks) == num_instances 749 else: 750 num_instances = len(masks) 751 if keypoints is not None:
749 else: 750 num_instances = len(masks) 751 if keypoints is not None: 752 if num_instances: 753 assert len(keypoints) == num_instances 754 else: 755 num_instances = len(keypoints) 756 keypoints = self._convert_keypoints(keypoints)
754 else: 755 num_instances = len(keypoints) 756 keypoints = self._convert_keypoints(keypoints) 757 if labels is not None: 758 assert len(labels) == num_instances 759 if assigned_colors is None: 760 # assigned_colors = [random_color(rgb=True, maximum=1) for _ in range(num_instances)] 761 assigned_colors = [instance_color(rgb=True, idx=i, maximum=1) for i in range(num_instances)]
1276 Returns: 1277 modified_color (tuple[double]): a tuple containing the RGB values of the 1278 modified color. Each value in the tuple is in the [0.0, 1.0] range. 1279 """ 1280 assert brightness_factor >= -1.0 and brightness_factor <= 1.0 1281 color = mplc.to_rgb(color) 1282 polygon_color = colorsys.rgb_to_hls(*mplc.to_rgb(color)) 1283 modified_lightness = polygon_color[1] + (brightness_factor * polygon_color[1])
39 self._working_dir = tempfile.TemporaryDirectory(prefix="cityscapes_eval_") 40 self._temp_dir = self._working_dir.name 41 # All workers will write to the same results directory 42 # TODO this does not work in distributed training 43 assert ( 44 comm.get_local_size() == comm.get_world_size() 45 ), "CityscapesEvaluator currently do not work with multiple machines." 46 self._temp_dir = comm.all_gather(self._temp_dir)[0] 47 if self._temp_dir != self._working_dir.name: 48 self._working_dir.cleanup()
114 # These lines are adopted from 115 # https://github.com/mcordts/cityscapesScripts/blob/master/cityscapesscripts/evaluation/evalInstanceLevelSemanticLabeling.py # noqa 116 gt_dir = PathManager.get_local_path(self._metadata.gt_dir) 117 groundTruthImgList = glob.glob(os.path.join(gt_dir, "*", "*_gtFine_instanceIds.png")) 118 assert len( 119 groundTruthImgList 120 ), "Cannot find any ground truth images to use for evaluation. Searched for: {}".format( 121 cityscapes_eval.args.groundTruthSearch 122 ) 123 predictionImgList = [] 124 for gt in groundTruthImgList: 125 predictionImgList.append(cityscapes_eval.getPrediction(gt, cityscapes_eval.args))
178 # These lines are adopted from 179 # https://github.com/mcordts/cityscapesScripts/blob/master/cityscapesscripts/evaluation/evalPixelLevelSemanticLabeling.py # noqa 180 gt_dir = PathManager.get_local_path(self._metadata.gt_dir) 181 groundTruthImgList = glob.glob(os.path.join(gt_dir, "*", "*_gtFine_labelIds.png")) 182 assert len( 183 groundTruthImgList 184 ), "Cannot find any ground truth images to use for evaluation. Searched for: {}".format( 185 cityscapes_eval.args.groundTruthSearch 186 ) 187 predictionImgList = [] 188 for gt in groundTruthImgList: 189 predictionImgList.append(cityscapes_eval.getPrediction(cityscapes_eval.args, gt))
10 import json 11 import logging 12 import numpy as np 13 import os 14 import pickle 15 from collections import OrderedDict 16 import custom_pycocotools.mask as mask_util 17 import torch
230 if hasattr(self._metadata, "thing_dataset_id_to_contiguous_id"): 231 dataset_id_to_contiguous_id = self._metadata.thing_dataset_id_to_contiguous_id 232 all_contiguous_ids = list(dataset_id_to_contiguous_id.values()) 233 num_classes = len(all_contiguous_ids) 234 assert min(all_contiguous_ids) == 0 and max(all_contiguous_ids) == num_classes - 1 235 236 reverse_id_mapping = {v: k for k, v in dataset_id_to_contiguous_id.items()} 237 for result in coco_results:
235 236 reverse_id_mapping = {v: k for k, v in dataset_id_to_contiguous_id.items()} 237 for result in coco_results: 238 category_id = result["category_id"] 239 assert category_id < num_classes, ( 240 f"A prediction has class={category_id}, " 241 f"but the dataset only has {num_classes} classes and " 242 f"predicted class id should be in [0, {num_classes - 1}]." 243 ) 244 result["category_id"] = reverse_id_mapping[category_id] 245 246 if self._output_dir:
259 "unofficial" if self._use_fast_impl else "official" 260 ) 261 ) 262 for task in sorted(tasks): 263 assert task in {"segm", "keypoints"}, f"Got unknown task: {task}!" 264 coco_eval = ( 265 _evaluate_predictions_on_coco( 266 self._coco_api,
319 # Compute per-category AP 320 # from https://github.com/facebookresearch/Detectron/blob/a6a835f5b8208c45d0dce217ce9bbda915f44df7/detectron/datasets/json_dataset_evaluator.py#L222-L252 # noqa 321 precisions = coco_eval.eval["precision"] 322 # precision has dims (iou, recall, cls, area range, max dets) 323 assert len(class_names) == precisions.shape[2] 324 325 results_per_category = [] 326 for idx, name in enumerate(class_names):
416 ): 417 """ 418 Evaluate the coco results using COCOEval API. 419 """ 420 assert len(coco_results) > 0 421 422 if iou_type == "segm": 423 coco_results = copy.deepcopy(coco_results)
433 # For COCO, the default max_dets_per_image is [1, 10, 100]. 434 if max_dets_per_image is None: 435 max_dets_per_image = [1, 10, 100] # Default from COCOEval 436 else: 437 assert ( 438 len(max_dets_per_image) >= 3 439 ), "COCOeval requires maxDets (and max_dets_per_image) to have length at least 3" 440 # In the case that user supplies a custom input for max_dets_per_image, 441 # apply COCOevalMaxDets to evaluate AP with the custom input. 442 if max_dets_per_image[2] != 100:
449 450 if iou_type == "keypoints": 451 # Use the COCO default keypoint OKS sigmas unless overrides are specified 452 if kpt_oks_sigmas: 453 assert hasattr(coco_eval.params, "kpt_oks_sigmas"), "custom_pycocotools is too old!" 454 coco_eval.params.kpt_oks_sigmas = np.array(kpt_oks_sigmas) 455 # COCOAPI requires every detection and every gt to have keypoints, so 456 # we just take the first entry from both
456 # we just take the first entry from both 457 num_keypoints_dt = len(coco_results[0]["keypoints"]) // 3 458 num_keypoints_gt = len(next(iter(coco_gt.anns.values()))["keypoints"]) // 3 459 num_keypoints_oks = len(coco_eval.params.kpt_oks_sigmas) 460 assert num_keypoints_oks == num_keypoints_dt == num_keypoints_gt, ( 461 f"[COCOEvaluator] Prediction contain {num_keypoints_dt} keypoints. " 462 f"Ground truth contains {num_keypoints_gt} keypoints. " 463 f"The length of cfg.TEST.KEYPOINT_OKS_SIGMAS is {num_keypoints_oks}. " 464 "They have to agree with each other. For meaning of OKS, please refer to " 465 "http://cocodataset.org/#keypoints-eval." 466 ) 467 468 coco_eval.evaluate() 469 coco_eval.accumulate()
10 import json 11 import logging 12 import numpy as np 13 import os 14 import pickle 15 from collections import OrderedDict 16 import custom_pycocotools.mask as mask_util 17 import torch
233 if hasattr(self._metadata, "thing_dataset_id_to_contiguous_id"): 234 dataset_id_to_contiguous_id = self._metadata.thing_dataset_id_to_contiguous_id 235 all_contiguous_ids = list(dataset_id_to_contiguous_id.values()) 236 num_classes = len(all_contiguous_ids) 237 assert min(all_contiguous_ids) == 0 and max(all_contiguous_ids) == num_classes - 1 238 239 reverse_id_mapping = {v: k for k, v in dataset_id_to_contiguous_id.items()} 240 for result in coco_results:
238 239 reverse_id_mapping = {v: k for k, v in dataset_id_to_contiguous_id.items()} 240 for result in coco_results: 241 category_id = result["category_id"] 242 assert category_id < num_classes, ( 243 f"A prediction has class={category_id}, " 244 f"but the dataset only has {num_classes} classes and " 245 f"predicted class id should be in [0, {num_classes - 1}]." 246 ) 247 result["category_id"] = reverse_id_mapping[category_id] 248 249 if self._output_dir:
262 "unofficial" if self._use_fast_impl else "official" 263 ) 264 ) 265 for task in sorted(tasks): 266 assert task in {"bbox", "keypoints"}, f"Got unknown task: {task}!" 267 coco_eval = ( 268 _evaluate_predictions_on_coco( 269 self._coco_api,
361 # Compute per-category AP 362 # from https://github.com/facebookresearch/Detectron/blob/a6a835f5b8208c45d0dce217ce9bbda915f44df7/detectron/datasets/json_dataset_evaluator.py#L222-L252 # noqa 363 precisions = coco_eval.eval["precision"] 364 # precision has dims (iou, recall, cls, area range, max dets) 365 assert len(class_names) == precisions.shape[2] 366 367 results_per_category = [] 368 for idx, name in enumerate(class_names):
481 [128**2, 256**2], # 128-256 482 [256**2, 512**2], # 256-512 483 [512**2, 1e5**2], 484 ] # 512-inf 485 assert area in areas, "Unknown area range: {}".format(area) 486 area_range = area_ranges[areas[area]] 487 gt_overlaps = [] 488 num_pos = 0
529 max_overlaps, argmax_overlaps = overlaps.max(dim=0) 530 531 # find which gt box is 'best' covered (i.e. 'best' = most iou) 532 gt_ovr, gt_ind = max_overlaps.max(dim=0) 533 assert gt_ovr >= 0 534 # find the proposal box that covers the best covered gt box 535 box_ind = argmax_overlaps[gt_ind] 536 # record the iou coverage of this gt box
534 # find the proposal box that covers the best covered gt box 535 box_ind = argmax_overlaps[gt_ind] 536 # record the iou coverage of this gt box 537 _gt_overlaps[j] = overlaps[box_ind, gt_ind] 538 assert _gt_overlaps[j] == gt_ovr 539 # mark the proposal box and the gt box as used 540 overlaps[box_ind, :] = -1 541 overlaps[:, gt_ind] = -1
576 ): 577 """ 578 Evaluate the coco results using COCOEval API. 579 """ 580 assert len(coco_results) > 0 581 582 if iou_type == "segm": 583 coco_results = copy.deepcopy(coco_results)
593 # For COCO, the default max_dets_per_image is [1, 10, 100]. 594 if max_dets_per_image is None: 595 max_dets_per_image = [1, 10, 100] # Default from COCOEval 596 else: 597 assert ( 598 len(max_dets_per_image) >= 3 599 ), "COCOeval requires maxDets (and max_dets_per_image) to have length at least 3" 600 # In the case that user supplies a custom input for max_dets_per_image, 601 # apply COCOevalMaxDets to evaluate AP with the custom input. 602 if max_dets_per_image[2] != 100:
609 610 if iou_type == "keypoints": 611 # Use the COCO default keypoint OKS sigmas unless overrides are specified 612 if kpt_oks_sigmas: 613 assert hasattr(coco_eval.params, "kpt_oks_sigmas"), "custom_pycocotools is too old!" 614 coco_eval.params.kpt_oks_sigmas = np.array(kpt_oks_sigmas) 615 # COCOAPI requires every detection and every gt to have keypoints, so 616 # we just take the first entry from both
616 # we just take the first entry from both 617 num_keypoints_dt = len(coco_results[0]["keypoints"]) // 3 618 num_keypoints_gt = len(next(iter(coco_gt.anns.values()))["keypoints"]) // 3 619 num_keypoints_oks = len(coco_eval.params.kpt_oks_sigmas) 620 assert num_keypoints_oks == num_keypoints_dt == num_keypoints_gt, ( 621 f"[COCOEvaluator] Prediction contain {num_keypoints_dt} keypoints. " 622 f"Ground truth contains {num_keypoints_gt} keypoints. " 623 f"The length of cfg.TEST.KEYPOINT_OKS_SIGMAS is {num_keypoints_oks}. " 624 "They have to agree with each other. For meaning of OKS, please refer to " 625 "http://cocodataset.org/#keypoints-eval." 626 ) 627 628 coco_eval.evaluate() 629 coco_eval.accumulate()
96 for evaluator in self._evaluators: 97 result = evaluator.evaluate() 98 if is_main_process() and result is not None: 99 for k, v in result.items(): 100 assert ( 101 k not in results 102 ), "Different evaluators produce results with the same key {}".format(k) 103 results[k] = v 104 return results 105
9 import json 10 import logging 11 import numpy as np 12 import os 13 import pickle 14 from collections import OrderedDict 15 import custom_pycocotools.mask as mask_util 16 import torch
65 # f"A prediction has class={category_id}, " 66 # f"but the dataset only has {num_classes} classes and " 67 # f"predicted class id should be in [0, {num_classes - 1}]." 68 # ) 69 assert category_id in reverse_id_mapping, ( 70 f"A prediction has class={category_id}, " 71 f"but the dataset only has class ids in {dataset_id_to_contiguous_id}." 72 ) 73 result["category_id"] = reverse_id_mapping[category_id] 74 75 if self._output_dir:
88 "unofficial" if self._use_fast_impl else "official" 89 ) 90 ) 91 for task in sorted(tasks): 92 assert task in {"bbox", "segm", "keypoints"}, f"Got unknown task: {task}!" 93 coco_eval = ( 94 _evaluate_predictions_on_coco( 95 self._coco_api,
300 x: Tensor of shape (N,C,H,W). H, W must be a multiple of ``self.size_divisibility``. 301 Returns: 302 dict[str->Tensor]: names and the corresponding features 303 """ 304 assert ( 305 x.dim() == 4 306 ), f"DiNAT takes an input of shape (N, C, H, W). Got {x.shape} instead!" 307 outputs = {} 308 y = super().forward(x) 309 for k in y.keys():
209 self.num_heads = num_heads 210 self.window_size = window_size 211 self.shift_size = shift_size 212 self.mlp_ratio = mlp_ratio 213 assert 0 <= self.shift_size < self.window_size, "shift_size must in 0-window_size" 214 215 self.norm1 = norm_layer(dim) 216 self.attn = WindowAttention(
241 mask_matrix: Attention mask for cyclic shift. 242 """ 243 B, L, C = x.shape 244 H, W = self.H, self.W 245 assert L == H * W, "input feature has wrong size" 246 247 shortcut = x 248 x = self.norm1(x)
315 x: Input feature, tensor size (B, H*W, C). 316 H, W: Spatial resolution of the input feature. 317 """ 318 B, L, C = x.shape 319 assert L == H * W, "input feature has wrong size" 320 321 x = x.view(B, H, W, C) 322
747 x: Tensor of shape (N,C,H,W). H, W must be a multiple of ``self.size_divisibility``. 748 Returns: 749 dict[str->Tensor]: names and the corresponding features 750 """ 751 assert ( 752 x.dim() == 4 753 ), f"SwinTransformer takes an input of shape (N, C, H, W). Got {x.shape} instead!" 754 outputs = {} 755 y = super().forward(x) 756 for k in y.keys():
111 self.cost_class = cost_class 112 self.cost_mask = cost_mask 113 self.cost_dice = cost_dice 114 115 assert cost_class != 0 or cost_mask != 0 or cost_dice != 0, "all costs cant be 0" 116 117 self.num_points = num_points 118
123 if self.transformer_in_feature == "multi_scale_pixel_decoder": 124 predictions = self.predictor(multi_scale_features, mask_features, tasks, mask) 125 else: 126 if self.transformer_in_feature == "transformer_encoder": 127 assert ( 128 transformer_encoder_features is not None 129 ), "Please use the TransformerEncoderPixelDecoder." 130 predictions = self.predictor(transformer_encoder_features, mask_features, mask) 131 elif self.transformer_in_feature == "pixel_embedding": 132 predictions = self.predictor(mask_features, mask_features, mask)
91 :return output (N, Length_{query}, C) 92 """ 93 N, Len_q, _ = query.shape 94 N, Len_in, _ = input_flatten.shape 95 assert (input_spatial_shapes[:, 0] * input_spatial_shapes[:, 1]).sum() == Len_in 96 97 value = self.value_proj(input_flatten) 98 if input_padding_mask is not None:
290 channels and hidden dim is identical 291 """ 292 super().__init__() 293 294 assert mask_classification, "Only support mask classification model" 295 self.mask_classification = mask_classification 296 self.is_train = is_train 297 self.use_task_norm = use_task_norm
388 # we add minus 1 to decoder layers to be consistent with our loss 389 # implementation: that is, number of auxiliary losses is always 390 # equal to number of decoder layers. With learnable query features, the number of 391 # auxiliary losses equals number of decoders plus 1. 392 assert cfg.MODEL.ONE_FORMER.DEC_LAYERS >= 1 393 ret["dec_layers"] = cfg.MODEL.ONE_FORMER.DEC_LAYERS - 1 394 ret["class_dec_layers"] = cfg.MODEL.ONE_FORMER.CLASS_DEC_LAYERS 395 ret["enc_layers"] = cfg.MODEL.ONE_FORMER.ENC_LAYERS
403 return ret 404 405 def forward(self, x, mask_features, tasks, mask = None): 406 # x is a list of multi-scale feature 407 assert len(x) == self.num_feature_levels 408 src = [] 409 pos = [] 410 size_list = []
474 outputs_class, outputs_mask, attn_mask = self.forward_prediction_heads(output, mask_features, attn_mask_target_size=size_list[(i + 1) % self.num_feature_levels], i=i+1) 475 predictions_class.append(outputs_class) 476 predictions_mask.append(outputs_mask) 477 478 assert len(predictions_class) == self.num_layers + 1 479 if self.is_train: 480 query_class = out.permute(1, 0, 2) 481 else:
47 self.proj_drop = nn.Dropout(proj_drop) 48 49 def forward(self, q, k, v): 50 B, N, C = q.shape 51 assert k.shape == v.shape 52 B, M, C = k.shape 53 q = self.q_proj(q).reshape(B, N, self.num_heads, C // self.num_heads) 54 k = self.k_proj(k).reshape(B, M, self.num_heads, C // self.num_heads)
113 114 self.thing_indices = [k for k in self.metadata.thing_dataset_id_to_contiguous_id.keys()] 115 116 if not self.semantic_on: 117 assert self.sem_seg_postprocess_before_inference 118 119 @classmethod 120 def from_config(cls, cfg):
205 def device(self): 206 return self.pixel_mean.device 207 208 def encode_text(self, text): 209 assert text.ndim in [2, 3], text.ndim 210 b = text.shape[0] 211 squeeze_dim = False 212 num_text = 1
46 and M = len(boxes2) 47 """ 48 # degenerate boxes gives inf / nan results 49 # so do an early check 50 assert (boxes1[:, 2:] >= boxes1[:, :2]).all() 51 assert (boxes2[:, 2:] >= boxes2[:, :2]).all() 52 # except: 53 # import ipdb; ipdb.set_trace()
47 """ 48 # degenerate boxes gives inf / nan results 49 # so do an early check 50 assert (boxes1[:, 2:] >= boxes1[:, :2]).all() 51 assert (boxes2[:, 2:] >= boxes2[:, :2]).all() 52 # except: 53 # import ipdb; ipdb.set_trace() 54 iou, union = box_iou(boxes1, boxes2)
89 - giou: N, 4 90 """ 91 # degenerate boxes gives inf / nan results 92 # so do an early check 93 assert (boxes1[:, 2:] >= boxes1[:, :2]).all() 94 assert (boxes2[:, 2:] >= boxes2[:, :2]).all() 95 assert boxes1.shape == boxes2.shape 96 iou, union = box_iou_pairwise(boxes1, boxes2) # N, 4
90 """ 91 # degenerate boxes gives inf / nan results 92 # so do an early check 93 assert (boxes1[:, 2:] >= boxes1[:, :2]).all() 94 assert (boxes2[:, 2:] >= boxes2[:, :2]).all() 95 assert boxes1.shape == boxes2.shape 96 iou, union = box_iou_pairwise(boxes1, boxes2) # N, 4 97
91 # degenerate boxes gives inf / nan results 92 # so do an early check 93 assert (boxes1[:, 2:] >= boxes1[:, :2]).all() 94 assert (boxes2[:, 2:] >= boxes2[:, :2]).all() 95 assert boxes1.shape == boxes2.shape 96 iou, union = box_iou_pairwise(boxes1, boxes2) # N, 4 97 98 lt = torch.min(boxes1[:, :2], boxes2[:, :2])
117 # type: (Device) -> NestedTensor # noqa 118 cast_tensor = self.tensors.to(device) 119 mask = self.mask 120 if mask is not None: 121 assert mask is not None 122 cast_mask = mask.to(device) 123 else: 124 cast_mask = None
32 return pos_embed 33 34 35 def get_2d_sincos_pos_embed_from_grid(embed_dim, grid): 36 assert embed_dim % 2 == 0 37 38 # use half of dimensions to encode grid_h 39 emb_h = get_1d_sincos_pos_embed_from_grid(embed_dim // 2, grid[0]) # (H*W, D/2)
48 embed_dim: output dimension for each position 49 pos: a list of positions to be encoded: size (M,) 50 out: (M, D) 51 """ 52 assert embed_dim % 2 == 0 53 omega = np.arange(embed_dim // 2, dtype=np.float) 54 omega /= embed_dim / 2.0 55 omega = 1.0 / 10000 ** omega # (D/2,)
79 print('loading annotations into memory...') 80 tic = time.time() 81 with open(annotation_file, 'r') as f: 82 dataset = json.load(f) 83 assert type(dataset)==dict, 'annotation file format {} not supported'.format(type(dataset)) 84 print('Done (t={:0.2f}s)'.format(time.time()- tic)) 85 self.dataset = dataset 86 self.createIndex()
321 elif type(resFile) == np.ndarray: 322 anns = self.loadNumpyAnnotations(resFile) 323 else: 324 anns = resFile 325 assert type(anns) == list, 'results in not an array of objects' 326 annsImgIds = [ann['image_id'] for ann in anns] 327 assert set(annsImgIds) == (set(annsImgIds) & set(self.getImgIds())), \ 328 'Results do not correspond to current coco set'
323 else: 324 anns = resFile 325 assert type(anns) == list, 'results in not an array of objects' 326 annsImgIds = [ann['image_id'] for ann in anns] 327 assert set(annsImgIds) == (set(annsImgIds) & set(self.getImgIds())), \ 328 'Results do not correspond to current coco set' 329 if 'caption' in anns[0]: 330 imgIds = set([img['id'] for img in res.dataset['images']]) & set([ann['image_id'] for ann in anns]) 331 res.dataset['images'] = [img for img in res.dataset['images'] if img['id'] in imgIds]
386 for i, img in enumerate(imgs): 387 tic = time.time() 388 fname = os.path.join(tarDir, img['file_name']) 389 if not os.path.exists(fname): 390 urlretrieve(img['coco_url'], fname) 391 print('downloaded {}/{} images (t={:0.1f}s)'.format(i, N, time.time()- tic)) 392 393 def loadNumpyAnnotations(self, data):
396 :param data (numpy.ndarray) 397 :return: annotations (python nested list) 398 """ 399 print('Converting ndarray to lists...') 400 assert(type(data) == np.ndarray) 401 print(data.shape) 402 assert(data.shape[1] == 7) 403 N = data.shape[0]
398 """ 399 print('Converting ndarray to lists...') 400 assert(type(data) == np.ndarray) 401 print(data.shape) 402 assert(data.shape[1] == 7) 403 N = data.shape[0] 404 ann = [] 405 for i in range(N):
403 try: 404 for ri, pi in enumerate(inds): 405 q[ri] = pr[pi] 406 ss[ri] = dtScoresSorted[pi] 407 except: 408 pass 409 precision[t,:,k,a,m] = np.array(q) 410 scores[t,:,k,a,m] = np.array(ss) 411 self.eval = {
46 47 def _interpolation(kwargs): 48 interpolation = kwargs.pop('resample', _DEFAULT_INTERPOLATION) 49 if isinstance(interpolation, (list, tuple)): 50 return random.choice(interpolation) 51 else: 52 return interpolation 53
176 177 178 def _randomly_negate(v): 179 """With 50% prob, negate the value""" 180 return -v if random.random() > 0.5 else v 181 182 183 def _rotate_level_to_arg(level, _hparams):
340 self.magnitude_std = self.hparams.get('magnitude_std', 0) 341 self.magnitude_max = self.hparams.get('magnitude_max', None) 342 343 def __call__(self, img): 344 if self.prob < 1.0 and random.random() > self.prob: 345 return img 346 magnitude = self.magnitude 347 if self.magnitude_std > 0:
346 magnitude = self.magnitude 347 if self.magnitude_std > 0: 348 # magnitude randomization enabled 349 if self.magnitude_std == float('inf'): 350 magnitude = random.uniform(0, magnitude) 351 elif self.magnitude_std > 0: 352 magnitude = random.gauss(magnitude, self.magnitude_std) 353 # default upper_bound for the timm RA impl is _LEVEL_DENOM (10)
509 return auto_augment_policy_v0(hparams) 510 elif name == 'v0r': 511 return auto_augment_policy_v0r(hparams) 512 else: 513 assert False, 'Unknown AA policy (%s)' % name 514 515 516 class AutoAugment:
518 def __init__(self, policy): 519 self.policy = policy 520 521 def __call__(self, img): 522 sub_policy = random.choice(self.policy) 523 for op in sub_policy: 524 img = op(img) 525 return img
559 if key == 'mstd': 560 # noise param injected via hparams for now 561 hparams.setdefault('magnitude_std', float(val)) 562 else: 563 assert False, 'Unknown AutoAugment config section' 564 aa_policy = auto_augment_policy(policy_name, hparams=hparams) 565 return AutoAugment(aa_policy) 566
628 629 630 def _select_rand_weights(weight_idx=0, transforms=None): 631 transforms = transforms or _RAND_TRANSFORMS 632 assert weight_idx == 0 # only one set of weights currently 633 rand_weights = _RAND_CHOICE_WEIGHTS_0 634 probs = [rand_weights[k] for k in transforms] 635 probs /= np.sum(probs)
689 num_layers = 2 # default to 2 ops per image 690 weight_idx = None # default to no probability weights for op choice 691 transforms = _RAND_TRANSFORMS 692 config = config_str.split('-') 693 assert config[0] == 'rand' 694 config = config[1:] 695 for c in config: 696 cs = re.split(r'(\d.*)', c)
716 num_layers = int(val) 717 elif key == 'w': 718 weight_idx = int(val) 719 else: 720 assert False, 'Unknown RandAugment config section' 721 ra_ops = rand_augment_ops(magnitude=magnitude, hparams=hparams, transforms=transforms) 722 choice_weights = None if weight_idx is None else _select_rand_weights(weight_idx) 723 return RandAugment(ra_ops, num_layers, choice_weights=choice_weights)
842 depth = -1 843 alpha = 1. 844 blended = False 845 config = config_str.split('-') 846 assert config[0] == 'augmix' 847 config = config[1:] 848 for c in config: 849 cs = re.split(r'(\d.*)', c)
863 alpha = float(val) 864 elif key == 'b': 865 blended = bool(val) 866 else: 867 assert False, 'Unknown AugMix config section' 868 hparams.setdefault('magnitude_std', float('inf')) # default to uniform sampling (if not set via mstd arg) 869 ops = augmix_ops(magnitude=magnitude, hparams=hparams) 870 return AugMixAugment(ops, alpha=alpha, width=width, depth=depth, blended=blended)
17 in_chans = args['chans'] 18 19 input_size = (in_chans, 224, 224) 20 if 'input_size' in args and args['input_size'] is not None: 21 assert isinstance(args['input_size'], (tuple, list)) 22 assert len(args['input_size']) == 3 23 input_size = tuple(args['input_size']) 24 in_chans = input_size[0] # input_size overrides in_chans
18 19 input_size = (in_chans, 224, 224) 20 if 'input_size' in args and args['input_size'] is not None: 21 assert isinstance(args['input_size'], (tuple, list)) 22 assert len(args['input_size']) == 3 23 input_size = tuple(args['input_size']) 24 in_chans = input_size[0] # input_size overrides in_chans 25 elif 'img_size' in args and args['img_size'] is not None:
22 assert len(args['input_size']) == 3 23 input_size = tuple(args['input_size']) 24 in_chans = input_size[0] # input_size overrides in_chans 25 elif 'img_size' in args and args['img_size'] is not None: 26 assert isinstance(args['img_size'], int) 27 input_size = (in_chans, args['img_size'], args['img_size']) 28 else: 29 if use_test_size and 'test_input_size' in default_cfg:
45 mean = tuple(args['mean']) 46 if len(mean) == 1: 47 mean = tuple(list(mean) * in_chans) 48 else: 49 assert len(mean) == in_chans 50 new_config['mean'] = mean 51 elif 'mean' in default_cfg: 52 new_config['mean'] = default_cfg['mean']
57 std = tuple(args['std']) 58 if len(std) == 1: 59 std = tuple(list(std) * in_chans) 60 else: 61 assert len(std) == in_chans 62 new_config['std'] = std 63 elif 'std' in default_cfg: 64 new_config['std'] = default_cfg['std']
79 download=False, 80 transform=None, 81 target_transform=None, 82 ): 83 assert parser is not None 84 if isinstance(parser, str): 85 self.parser = create_parser( 86 parser, root=root, split=split, is_training=is_training,
105 else: 106 return 0 107 108 def filename(self, index, basename=False, absolute=False): 109 assert False, 'Filename lookup by index not supported, use filenames().' 110 111 def filenames(self, basename=False, absolute=False): 112 return self.parser.filenames(basename, absolute)
123 self._set_transforms(self.dataset.transform) 124 self.num_splits = num_splits 125 126 def _set_transforms(self, x): 127 assert isinstance(x, (list, tuple)) and len(x) == 3, 'Expecting a tuple/list of 3 transforms' 128 self.dataset.transform = x[0] 129 self.augmentation = x[1] 130 self.normalize = x[2]
97 ds_class = _TORCH_BASIC_DS[name] 98 use_train = split in _TRAIN_SYNONYM 99 ds = ds_class(train=use_train, **torch_kwargs) 100 elif name == 'inaturalist' or name == 'inat': 101 assert has_inaturalist, 'Please update to PyTorch 1.10, torchvision 0.11+ for Inaturalist' 102 target_type = 'full' 103 split_split = split.split('/') 104 if len(split_split) > 1:
111 elif split in _EVAL_SYNONYM: 112 split = '2021_valid' 113 ds = INaturalist(version=split, target_type=target_type, **torch_kwargs) 114 elif name == 'places365': 115 assert has_places365, 'Please update to a newer PyTorch and torchvision for Places365 dataset.' 116 if split in _TRAIN_SYNONYM: 117 split = 'train-standard' 118 elif split in _EVAL_SYNONYM:
128 # look for split specific sub-folder in root 129 root = _search_split(root, split) 130 ds = ImageFolder(root, **kwargs) 131 else: 132 assert False, f"Unknown torchvision dataset {name}" 133 elif name.startswith('tfds/'): 134 ds = IterableImageDataset( 135 root, parser=name, split=split, is_training=is_training,
38 indices = list(range(len(self.dataset))) 39 40 # add extra samples to make it evenly divisible 41 indices += indices[:(self.total_size - len(indices))] 42 assert len(indices) == self.total_size 43 44 # subsample 45 indices = indices[self.rank:self.total_size:self.num_replicas]
42 assert len(indices) == self.total_size 43 44 # subsample 45 indices = indices[self.rank:self.total_size:self.num_replicas] 46 assert len(indices) == self.num_samples 47 48 return iter(indices) 49
118 # add extra samples to make it evenly divisible 119 padding_size = self.total_size - len(indices) 120 if padding_size > 0: 121 indices += indices[:padding_size] 122 assert len(indices) == self.total_size 123 124 # subsample per rank 125 indices = indices[self.rank:self.total_size:self.num_replicas]
122 assert len(indices) == self.total_size 123 124 # subsample per rank 125 indices = indices[self.rank:self.total_size:self.num_replicas] 126 assert len(indices) == self.num_samples 127 128 # return up to num selected samples 129 return iter(indices[:self.num_selected_samples])
21 22 23 def fast_collate(batch): 24 """ A fast collation function optimized for uint8 images (np array or torch) and int64 targets (labels)""" 25 assert isinstance(batch[0], tuple) 26 batch_size = len(batch) 27 if isinstance(batch[0][0], tuple): 28 # This branch 'deinterleaves' and flattens tuples of input tensors into one tensor ordered by position
31 flattened_batch_size = batch_size * inner_tuple_size 32 targets = torch.zeros(flattened_batch_size, dtype=torch.int64) 33 tensor = torch.zeros((flattened_batch_size, *batch[0][0][0].shape), dtype=torch.uint8) 34 for i in range(batch_size): 35 assert len(batch[i][0]) == inner_tuple_size # all input tensor tuples must be same length 36 for j in range(inner_tuple_size): 37 targets[i + j * batch_size] = batch[i][1] 38 tensor[i + j * batch_size] += torch.from_numpy(batch[i][0][j])
38 tensor[i + j * batch_size] += torch.from_numpy(batch[i][0][j]) 39 return tensor, targets 40 elif isinstance(batch[0][0], np.ndarray): 41 targets = torch.tensor([b[1] for b in batch], dtype=torch.int64) 42 assert len(targets) == batch_size 43 tensor = torch.zeros((batch_size, *batch[0][0].shape), dtype=torch.uint8) 44 for i in range(batch_size): 45 tensor[i] += torch.from_numpy(batch[i][0])
45 tensor[i] += torch.from_numpy(batch[i][0]) 46 return tensor, targets 47 elif isinstance(batch[0][0], torch.Tensor): 48 targets = torch.tensor([b[1] for b in batch], dtype=torch.int64) 49 assert len(targets) == batch_size 50 tensor = torch.zeros((batch_size, *batch[0][0].shape), dtype=torch.uint8) 51 for i in range(batch_size): 52 tensor[i].copy_(batch[i][0])
51 for i in range(batch_size): 52 tensor[i].copy_(batch[i][0]) 53 return tensor, targets 54 else: 55 assert False 56 57 58 def expand_to_chs(x, n):
60 x = tuple(repeat(x, n)) 61 elif len(x) == 1: 62 x = x * n 63 else: 64 assert len(x) == n, 'normalization stats must match image channels' 65 return x 66 67
147 148 149 def _worker_init(worker_id, worker_seeding='all'): 150 worker_info = torch.utils.data.get_worker_info() 151 assert worker_info.id == worker_id 152 if isinstance(worker_seeding, Callable): 153 seed = worker_seeding(worker_info) 154 random.seed(seed)
154 random.seed(seed) 155 torch.manual_seed(seed) 156 np.random.seed(seed % (2 ** 32 - 1)) 157 else: 158 assert worker_seeding in ('all', 'part') 159 # random / torch seed already called in dataloader iter class w/ worker_info.seed 160 # to reproduce some old results (same seed + hparam combo), partial seeding is required (skip numpy re-seed) 161 if worker_seeding == 'all':
233 # This will add extra duplicate entries to result in equal num 234 # of samples per-process, will slightly alter validation results 235 sampler = OrderedDistributedSampler(dataset) 236 else: 237 assert num_aug_repeats == 0, "RepeatAugment not currently supported in non-distributed or IterableDataset use" 238 239 if collate_fn is None: 240 collate_fn = fast_collate if use_prefetcher else torch.utils.data.dataloader.default_collate
62 img_shape (tuple): Image shape as tuple 63 minmax (tuple or list): Min and max bbox ratios (as percent of image size) 64 count (int): Number of bbox to generate 65 """ 66 assert len(minmax) == 2 67 img_h, img_w = img_shape[-2:] 68 cut_h = np.random.randint(int(img_h * minmax[0]), int(img_h * minmax[1]), size=count) 69 cut_w = np.random.randint(int(img_w * minmax[0]), int(img_w * minmax[1]), size=count)
106 self.mixup_alpha = mixup_alpha 107 self.cutmix_alpha = cutmix_alpha 108 self.cutmix_minmax = cutmix_minmax 109 if self.cutmix_minmax is not None: 110 assert len(self.cutmix_minmax) == 2 111 # force cutmix alpha == 1.0 when minmax active to keep logic simple & safe 112 self.cutmix_alpha = 1.0 113 self.mix_prob = prob
133 elif self.cutmix_alpha > 0.: 134 use_cutmix = np.ones(batch_size, dtype=np.bool) 135 lam_mix = np.random.beta(self.cutmix_alpha, self.cutmix_alpha, size=batch_size) 136 else: 137 assert False, "One of mixup_alpha > 0., cutmix_alpha > 0., cutmix_minmax not None should be true." 138 lam = np.where(np.random.rand(batch_size) < self.mix_prob, lam_mix.astype(np.float32), lam) 139 return lam, use_cutmix 140
151 elif self.cutmix_alpha > 0.: 152 use_cutmix = True 153 lam_mix = np.random.beta(self.cutmix_alpha, self.cutmix_alpha) 154 else: 155 assert False, "One of mixup_alpha > 0., cutmix_alpha > 0., cutmix_minmax not None should be true." 156 lam = float(lam_mix) 157 return lam, use_cutmix 158
206 x.mul_(lam).add_(x_flipped) 207 return lam 208 209 def __call__(self, x, target): 210 assert len(x) % 2 == 0, 'Batch size should be even when using this' 211 if self.mode == 'elem': 212 lam = self._mix_elem(x) 213 elif self.mode == 'pair':
226 227 def _mix_elem_collate(self, output, batch, half=False): 228 batch_size = len(batch) 229 num_elem = batch_size // 2 if half else batch_size 230 assert len(output) == num_elem 231 lam_batch, use_cutmix = self._params_per_elem(num_elem) 232 for i in range(num_elem): 233 j = batch_size - i - 1
256 j = batch_size - i - 1 257 lam = lam_batch[i] 258 mixed_i = batch[i][0] 259 mixed_j = batch[j][0] 260 assert 0 <= lam <= 1.0 261 if lam < 1.: 262 if use_cutmix[i]: 263 (yl, yh, xl, xh), lam = cutmix_bbox_and_lam(
297 return lam 298 299 def __call__(self, batch, _=None): 300 batch_size = len(batch) 301 assert batch_size % 2 == 0, 'Batch size should be even when using this' 302 half = 'half' in self.mode 303 if half: 304 batch_size //= 2
1 import os 2 import pickle 3 4 def load_class_map(map_or_filename, root=''): 5 if isinstance(map_or_filename, dict): 6 assert dict, 'class_map dict must be non-empty' 7 return map_or_filename 8 class_map_path = map_or_filename
2 import pickle 3 4 def load_class_map(map_or_filename, root=''): 5 if isinstance(map_or_filename, dict): 6 assert dict, 'class_map dict must be non-empty' 7 return map_or_filename 8 class_map_path = map_or_filename 9 if not os.path.exists(class_map_path):
7 return map_or_filename 8 class_map_path = map_or_filename 9 if not os.path.exists(class_map_path): 10 class_map_path = os.path.join(root, class_map_path) 11 assert os.path.exists(class_map_path), 'Cannot locate specified class map file (%s)' % map_or_filename 12 class_map_ext = os.path.splitext(map_or_filename)[-1].lower() 13 if class_map_ext == '.txt': 14 with open(class_map_path) as f:
14 with open(class_map_path) as f: 15 class_to_idx = {v.strip(): k for k, v in enumerate(f)} 16 elif class_map_ext == '.pkl': 17 with open(class_map_path,'rb') as f: 18 class_to_idx = pickle.load(f) 19 else: 20 assert False, f'Unsupported class map file extension ({class_map_ext}).' 21 return class_to_idx
16 elif class_map_ext == '.pkl': 17 with open(class_map_path,'rb') as f: 18 class_to_idx = pickle.load(f) 19 else: 20 assert False, f'Unsupported class map file extension ({class_map_ext}).' 21 return class_to_idx 22
27 return deepcopy(_IMG_EXTENSIONS_SET if as_set else IMG_EXTENSIONS) 28 29 30 def set_img_extensions(extensions): 31 assert len(extensions) 32 for x in extensions: 33 assert _valid_extension(x) 34 _set_extensions(extensions)
29 30 def set_img_extensions(extensions): 31 assert len(extensions) 32 for x in extensions: 33 assert _valid_extension(x) 34 _set_extensions(extensions) 35 36
37 def add_img_extensions(ext): 38 if not isinstance(ext, (list, tuple, set)): 39 ext = (ext,) 40 for x in ext: 41 assert _valid_extension(x) 42 extensions = IMG_EXTENSIONS + tuple(ext) 43 _set_extensions(extensions) 44
17 if prefix == 'tfds': 18 from .parser_tfds import ParserTfds # defer tensorflow import 19 parser = ParserTfds(root, name, split=split, **kwargs) 20 else: 21 assert os.path.exists(root) 22 # default fallback path (backwards compat), use image tar if root is a .tar file, otherwise image folder 23 # FIXME support split here, in parser? 24 if os.path.isfile(root) and os.path.splitext(root)[1] == '.tar':
10 Hacked together by / Copyright 2020 Ross Wightman 11 """ 12 import logging 13 import os 14 import pickle 15 import tarfile 16 from glob import glob 17 from typing import List, Tuple, Dict, Set, Optional, Union
69 ): 70 extensions = get_img_extensions(as_set=True) if not extensions else set(extensions) 71 root_is_tar = False 72 if os.path.isfile(root): 73 assert os.path.splitext(root)[-1].lower() == '.tar' 74 tar_filenames = [root] 75 root, root_name = os.path.split(root) 76 root_name = os.path.splitext(root_name)[0]
79 root_name = root.strip(os.path.sep).split(os.path.sep)[-1] 80 tar_filenames = glob(os.path.join(root, '*.tar'), recursive=True) 81 num_tars = len(tar_filenames) 82 tar_bytes = sum([os.path.getsize(f) for f in tar_filenames]) 83 assert num_tars, f'No .tar files found at specified path ({root}).' 84 85 _logger.info(f'Scanning {tar_bytes/1024**2:.2f}MB of tar files...') 86 info = dict(tartrees=[])
92 cache_path = os.path.join(root, cache_filename) 93 if os.path.exists(cache_path): 94 _logger.info(f'Reading tar info from cache file {cache_path}.') 95 with open(cache_path, 'rb') as pf: 96 info = pickle.load(pf) 97 assert len(info['tartrees']) == num_tars, "Cached tartree len doesn't match number of tarfiles" 98 else: 99 for i, fn in enumerate(tar_filenames):
93 if os.path.exists(cache_path): 94 _logger.info(f'Reading tar info from cache file {cache_path}.') 95 with open(cache_path, 'rb') as pf: 96 info = pickle.load(pf) 97 assert len(info['tartrees']) == num_tars, "Cached tartree len doesn't match number of tarfiles" 98 else: 99 for i, fn in enumerate(tar_filenames): 100 path = '' if root_is_tar else os.path.splitext(os.path.basename(fn))[0]
48 49 class_to_idx = None 50 if class_map: 51 class_to_idx = load_class_map(class_map, root) 52 assert os.path.isfile(root) 53 self.root = root 54 55 with tarfile.open(root) as tf: # cannot keep this open across processes, reopen later
116 self.root = root 117 self.split = split 118 self.is_training = is_training 119 if self.is_training: 120 assert batch_size is not None, \ 121 "Must specify batch_size in training mode for reasonable behaviour w/ TFDS wrapper" 122 self.batch_size = batch_size 123 self.repeats = repeats 124 self.common_seed = seed # a seed that's fixed across all worker / distributed instances
278 # complete worker & replica info (not available until init in dataloader). 279 return math.ceil(max(1, self.repeats) * self.num_examples / self.dist_num_replicas) 280 281 def _filename(self, index, basename=False, absolute=False): 282 assert False, "Not supported" # no random access to examples 283 284 def filenames(self, basename=False, absolute=False): 285 """ Return all filenames in dataset, overrides base"""
295 name = sample['filename'] 296 elif 'id' in sample: 297 name = sample['id'] 298 else: 299 assert False, "No supported name field present" 300 names.append(name) 301 return names
61 self.rand_color = True # per block random normal 62 elif self.mode == 'pixel': 63 self.per_pixel = True # per pixel random normal 64 else: 65 assert not self.mode or self.mode == 'const' 66 self.device = device 67 68 def _erase(self, img, chan, img_h, img_w, dtype):
65 assert not self.mode or self.mode == 'const' 66 self.device = device 67 68 def _erase(self, img, chan, img_h, img_w, dtype): 69 if random.random() > self.probability: 70 return 71 area = img_h * img_w 72 count = self.min_count if self.min_count == self.max_count else \
69 if random.random() > self.probability: 70 return 71 area = img_h * img_w 72 count = self.min_count if self.min_count == self.max_count else \ 73 random.randint(self.min_count, self.max_count) 74 for _ in range(count): 75 for attempt in range(10): 76 target_area = random.uniform(self.min_area, self.max_area) * area / count
72 count = self.min_count if self.min_count == self.max_count else \ 73 random.randint(self.min_count, self.max_count) 74 for _ in range(count): 75 for attempt in range(10): 76 target_area = random.uniform(self.min_area, self.max_area) * area / count 77 aspect_ratio = math.exp(random.uniform(*self.log_aspect_ratio)) 78 h = int(round(math.sqrt(target_area * aspect_ratio))) 79 w = int(round(math.sqrt(target_area / aspect_ratio)))
73 random.randint(self.min_count, self.max_count) 74 for _ in range(count): 75 for attempt in range(10): 76 target_area = random.uniform(self.min_area, self.max_area) * area / count 77 aspect_ratio = math.exp(random.uniform(*self.log_aspect_ratio)) 78 h = int(round(math.sqrt(target_area * aspect_ratio))) 79 w = int(round(math.sqrt(target_area / aspect_ratio))) 80 if w < img_w and h < img_h:
77 aspect_ratio = math.exp(random.uniform(*self.log_aspect_ratio)) 78 h = int(round(math.sqrt(target_area * aspect_ratio))) 79 w = int(round(math.sqrt(target_area / aspect_ratio))) 80 if w < img_w and h < img_h: 81 top = random.randint(0, img_h - h) 82 left = random.randint(0, img_w - w) 83 img[:, top:top + h, left:left + w] = _get_pixels( 84 self.per_pixel, self.rand_color, (chan, h, w),
78 h = int(round(math.sqrt(target_area * aspect_ratio))) 79 w = int(round(math.sqrt(target_area / aspect_ratio))) 80 if w < img_w and h < img_h: 81 top = random.randint(0, img_h - h) 82 left = random.randint(0, img_w - w) 83 img[:, top:top + h, left:left + w] = _get_pixels( 84 self.per_pixel, self.rand_color, (chan, h, w), 85 dtype=dtype, device=self.device)
16 real_labels = json.load(real_labels) 17 real_labels = {f'ILSVRC2012_val_{i + 1:08d}.JPEG': labels for i, labels in enumerate(real_labels)} 18 self.real_labels = real_labels 19 self.filenames = filenames 20 assert len(self.filenames) == len(self.real_labels) 21 self.topk = topk 22 self.is_correct = {k: [] for k in topk} 23 self.sample_idx = 0
142 """ 143 area = img.size[0] * img.size[1] 144 145 for attempt in range(10): 146 target_area = random.uniform(*scale) * area 147 log_ratio = (math.log(ratio[0]), math.log(ratio[1])) 148 aspect_ratio = math.exp(random.uniform(*log_ratio)) 149
144 145 for attempt in range(10): 146 target_area = random.uniform(*scale) * area 147 log_ratio = (math.log(ratio[0]), math.log(ratio[1])) 148 aspect_ratio = math.exp(random.uniform(*log_ratio)) 149 150 w = int(round(math.sqrt(target_area * aspect_ratio))) 151 h = int(round(math.sqrt(target_area / aspect_ratio)))
150 w = int(round(math.sqrt(target_area * aspect_ratio))) 151 h = int(round(math.sqrt(target_area / aspect_ratio))) 152 153 if w <= img.size[0] and h <= img.size[1]: 154 i = random.randint(0, img.size[1] - h) 155 j = random.randint(0, img.size[0] - w) 156 return i, j, h, w 157
151 h = int(round(math.sqrt(target_area / aspect_ratio))) 152 153 if w <= img.size[0] and h <= img.size[1]: 154 i = random.randint(0, img.size[1] - h) 155 j = random.randint(0, img.size[0] - w) 156 return i, j, h, w 157 158 # Fallback to central crop
179 PIL Image: Randomly cropped and resized image. 180 """ 181 i, j, h, w = self.get_params(img, self.scale, self.ratio) 182 if isinstance(self.interpolation, (tuple, list)): 183 interpolation = random.choice(self.interpolation) 184 else: 185 interpolation = self.interpolation 186 return F.resized_crop(img, i, j, h, w, self.size, interpolation)
76 primary_tfl += [transforms.RandomVerticalFlip(p=vflip)] 77 78 secondary_tfl = [] 79 if auto_augment: 80 assert isinstance(auto_augment, str) 81 if isinstance(img_size, (tuple, list)): 82 img_size_min = min(img_size) 83 else:
99 # color jitter is enabled when not using AA 100 if isinstance(color_jitter, (list, tuple)): 101 # color jitter should be a 3-tuple/list if spec brightness/contrast/saturation 102 # or 4 if also augmenting hue 103 assert len(color_jitter) in (3, 4) 104 else: 105 # if it's a scalar, duplicate for brightness, contrast, and saturation, no hue 106 color_jitter = (float(color_jitter),) * 3
136 std=IMAGENET_DEFAULT_STD): 137 crop_pct = crop_pct or DEFAULT_CROP_PCT 138 139 if isinstance(img_size, (tuple, list)): 140 assert len(img_size) == 2 141 if img_size[-1] == img_size[-2]: 142 # fall-back to older behaviour so Resize scales to shortest edge if target is square 143 scale_size = int(math.floor(img_size[0] / crop_pct))
191 else: 192 img_size = input_size 193 194 if tf_preprocessing and use_prefetcher: 195 assert not separate, "Separate transforms not supported for TF preprocessing" 196 from custom_timm.data.tf_preprocessing import TfPreprocessTransform 197 transform = TfPreprocessTransform( 198 is_training=is_training, size=img_size, interpolation=interpolation)
197 transform = TfPreprocessTransform( 198 is_training=is_training, size=img_size, interpolation=interpolation) 199 else: 200 if is_training and no_aug: 201 assert not separate, "Cannot perform split augmentation with no_aug" 202 transform = transforms_noaug_train( 203 img_size, 204 interpolation=interpolation,
223 re_count=re_count, 224 re_num_splits=re_num_splits, 225 separate=separate) 226 else: 227 assert not separate, "Separate transforms not supported for validation preprocessing" 228 transform = transforms_imagenet_eval( 229 img_size, 230 interpolation=interpolation,
16 def __init__( 17 self, smoothing=0.1, target_threshold: Optional[float] = None, weight: Optional[torch.Tensor] = None, 18 reduction: str = 'mean', pos_weight: Optional[torch.Tensor] = None): 19 super(BinaryCrossEntropy, self).__init__() 20 assert 0. <= smoothing < 1.0 21 self.smoothing = smoothing 22 self.target_threshold = target_threshold 23 self.reduction = reduction
24 self.register_buffer('weight', weight) 25 self.register_buffer('pos_weight', pos_weight) 26 27 def forward(self, x: torch.Tensor, target: torch.Tensor) -> torch.Tensor: 28 assert x.shape[0] == target.shape[0] 29 if target.shape != x.shape: 30 # NOTE currently assume smoothing or other label softening is applied upstream if targets are already sparse 31 num_classes = x.shape[-1]
12 """ NLL loss with label smoothing. 13 """ 14 def __init__(self, smoothing=0.1): 15 super(LabelSmoothingCrossEntropy, self).__init__() 16 assert smoothing < 1.0 17 self.smoothing = smoothing 18 self.confidence = 1. - smoothing 19
24 self.cross_entropy_loss = torch.nn.CrossEntropyLoss() 25 26 def __call__(self, output, target): 27 split_size = output.shape[0] // self.num_splits 28 assert split_size * self.num_splits == output.shape[0] 29 logits_split = torch.split(output, split_size) 30 31 # Cross-entropy is only computed on clean images
221 types: Tuple[str, str], d, every: Union[int, List[int]] = 1, first: bool = False, **kwargs 222 ) -> Tuple[ByoBlockCfg]: 223 """ interleave 2 block types in stack 224 """ 225 assert len(types) == 2 226 if isinstance(every, int): 227 every = list(range(0 if first else every, d, every + 1)) 228 if not every:
913 if not group_size: # 0 or None 914 return 1 # normal conv with 1 group 915 else: 916 # NOTE group_size == 1 -> depthwise conv 917 assert channels % group_size == 0 918 return channels // group_size 919 920
944 return self.conv(self.pool(x)) 945 946 947 def create_shortcut(downsample_type, layers: LayerFn, in_chs, out_chs, stride, dilation, **kwargs): 948 assert downsample_type in ('avg', 'conv1x1', '') 949 if in_chs != out_chs or stride != 1 or dilation[0] != dilation[1]: 950 if not downsample_type: 951 return None # no shortcut
1207 self, in_chs, out_chs, kernel_size=3, stride=1, dilation=(1, 1), bottle_ratio=1., group_size=None, 1208 downsample='avg', extra_conv=False, linear_out=False, bottle_in=False, post_attn_na=True, 1209 feat_size=None, layers: LayerFn = None, drop_block=None, drop_path_rate=0.): 1210 super(SelfAttnBlock, self).__init__() 1211 assert layers is not None 1212 mid_chs = make_divisible((in_chs if bottle_in else out_chs) * bottle_ratio) 1213 groups = num_groups(group_size, mid_chs) 1214
1266 1267 def create_block(block: Union[str, nn.Module], **kwargs): 1268 if isinstance(block, (nn.Module, partial)): 1269 return block(**kwargs) 1270 assert block in _block_registry, f'Unknown block type ({block}' 1271 return _block_registry[block](**kwargs) 1272 1273
1276 def __init__( 1277 self, in_chs, out_chs, kernel_size=3, stride=4, pool='maxpool', 1278 num_rep=3, num_act=None, chs_decay=0.5, layers: LayerFn = None): 1279 super().__init__() 1280 assert stride in (2, 4) 1281 layers = layers or LayerFn() 1282 1283 if isinstance(out_chs, (list, tuple)):
1315 curr_stride *= 2 1316 prev_feat = 'pool' 1317 1318 self.feature_info.append(dict(num_chs=prev_chs, reduction=curr_stride, module=prev_feat)) 1319 assert curr_stride == stride 1320 1321 1322 def create_byob_stem(in_chs, out_chs, stem_type='', pool_type='', feat_prefix='stem', layers: LayerFn = None):
1320 1321 1322 def create_byob_stem(in_chs, out_chs, stem_type='', pool_type='', feat_prefix='stem', layers: LayerFn = None): 1323 layers = layers or LayerFn() 1324 assert stem_type in ('', 'quad', 'quad2', 'tiered', 'deep', 'rep', '7x7', '3x3') 1325 if 'quad' in stem_type: 1326 # based on NFNet stem, stack of 4 3x3 convs 1327 num_act = 2 if 'quad2' in stem_type else None
1492 self.drop_rate = drop_rate 1493 self.grad_checkpointing = False 1494 layers = get_layer_fns(cfg) 1495 if cfg.fixed_input_size: 1496 assert img_size is not None, 'img_size argument is required for fixed input size model' 1497 feat_size = to_2tuple(img_size) if img_size is not None else None 1498 1499 self.feature_info = []
218 depth_token_only=2, 219 mlp_ratio_token_only=4.0 220 ): 221 super().__init__() 222 assert global_pool in ('', 'token', 'avg') 223 224 self.num_classes = num_classes 225 self.global_pool = global_pool
300 301 def reset_classifier(self, num_classes, global_pool=None): 302 self.num_classes = num_classes 303 if global_pool is not None: 304 assert global_pool in ('', 'token', 'avg') 305 self.global_pool = global_pool 306 self.head = nn.Linear(self.num_features, num_classes) if num_classes > 0 else nn.Identity() 307
257 self.norm22 = norm_layer(dims[1]) 258 self.norm23 = norm_layer(dims[2]) 259 self.norm24 = norm_layer(dims[3]) 260 # In parallel block, we assume dimensions are the same and share the linear transformation. 261 assert dims[1] == dims[2] == dims[3] 262 assert mlp_ratios[1] == mlp_ratios[2] == mlp_ratios[3] 263 mlp_hidden_dim = int(dims[1] * mlp_ratios[1]) 264 self.mlp2 = self.mlp3 = self.mlp4 = Mlp(
258 self.norm23 = norm_layer(dims[2]) 259 self.norm24 = norm_layer(dims[3]) 260 # In parallel block, we assume dimensions are the same and share the linear transformation. 261 assert dims[1] == dims[2] == dims[3] 262 assert mlp_ratios[1] == mlp_ratios[2] == mlp_ratios[3] 263 mlp_hidden_dim = int(dims[1] * mlp_ratios[1]) 264 self.mlp2 = self.mlp3 = self.mlp4 = Mlp( 265 in_features=dims[1], hidden_features=mlp_hidden_dim, act_layer=act_layer, drop=drop)
332 serial_depths=(0, 0, 0, 0), parallel_depth=0, num_heads=0, mlp_ratios=(0, 0, 0, 0), qkv_bias=True, 333 drop_rate=0., attn_drop_rate=0., drop_path_rate=0., norm_layer=partial(nn.LayerNorm, eps=1e-6), 334 return_interm_layers=False, out_features=None, crpe_window=None, global_pool='token'): 335 super().__init__() 336 assert global_pool in ('token', 'avg') 337 crpe_window = crpe_window or {3: 2, 5: 3, 7: 3} 338 self.return_interm_layers = return_interm_layers 339 self.out_features = out_features
376 self.crpe4 = ConvRelPosEnc(Ch=embed_dims[3] // num_heads, h=num_heads, window=crpe_window) 377 378 # Disable stochastic depth. 379 dpr = drop_path_rate 380 assert dpr == 0.0 381 382 # Serial blocks 1. 383 self.serial_blocks1 = nn.ModuleList([
443 self.norm4 = norm_layer(embed_dims[3]) 444 445 if self.parallel_depth > 0: 446 # CoaT series: Aggregate features of last three scales for classification. 447 assert embed_dims[1] == embed_dims[2] == embed_dims[3] 448 self.aggregate = torch.nn.Conv1d(in_channels=3, out_channels=1, kernel_size=1) 449 self.head = nn.Linear(self.num_features, num_classes) if num_classes > 0 else nn.Identity() 450 else:
473 return {'cls_token1', 'cls_token2', 'cls_token3', 'cls_token4'} 474 475 @torch.jit.ignore 476 def set_grad_checkpointing(self, enable=True): 477 assert not enable, 'gradient checkpointing not supported' 478 479 @torch.jit.ignore 480 def group_matcher(self, coarse=False):
500 501 def reset_classifier(self, num_classes, global_pool=None): 502 self.num_classes = num_classes 503 if global_pool is not None: 504 assert global_pool in ('token', 'avg') 505 self.global_pool = global_pool 506 self.head = nn.Linear(self.num_features, num_classes) if num_classes > 0 else nn.Identity() 507
587 return [x2, x3, x4] 588 589 def forward_head(self, x_feat: Union[torch.Tensor, List[torch.Tensor]], pre_logits: bool = False): 590 if isinstance(x_feat, list): 591 assert self.aggregate is not None 592 if self.global_pool == 'avg': 593 x = torch.cat([xl[:, 1:].mean(dim=1, keepdim=True) for xl in x_feat], dim=1) # [B, 3, C] 594 else:
225 embed_dim=768, depth=12, num_heads=12, mlp_ratio=4., qkv_bias=False, drop_rate=0., attn_drop_rate=0., 226 drop_path_rate=0., hybrid_backbone=None, norm_layer=nn.LayerNorm, 227 local_up_to_layer=3, locality_strength=1., use_pos_embed=True): 228 super().__init__() 229 assert global_pool in ('', 'avg', 'token') 230 embed_dim *= num_heads 231 self.num_classes = num_classes 232 self.global_pool = global_pool
297 ) 298 299 @torch.jit.ignore 300 def set_grad_checkpointing(self, enable=True): 301 assert not enable, 'gradient checkpointing not supported' 302 303 @torch.jit.ignore 304 def get_classifier(self):
306 307 def reset_classifier(self, num_classes, global_pool=None): 308 self.num_classes = num_classes 309 if global_pool is not None: 310 assert global_pool in ('', 'token', 'avg') 311 self.global_pool = global_pool 312 self.head = nn.Linear(self.embed_dim, num_classes) if num_classes > 0 else nn.Identity() 313
287 drop_rate=0., 288 drop_path_rate=0., 289 ): 290 super().__init__() 291 assert output_stride in (8, 16, 32) 292 kernel_sizes = to_ntuple(4)(kernel_sizes) 293 if norm_layer is None: 294 norm_layer = LayerNorm2d
293 if norm_layer is None: 294 norm_layer = LayerNorm2d 295 norm_layer_cl = norm_layer if conv_mlp else LayerNorm 296 else: 297 assert conv_mlp,\ 298 'If a norm_layer is specified, conv MLP must be used so all norm expect rank-4, channels-first input' 299 norm_layer_cl = norm_layer 300 301 self.num_classes = num_classes
301 self.num_classes = num_classes 302 self.drop_rate = drop_rate 303 self.feature_info = [] 304 305 assert stem_type in ('patch', 'overlap', 'overlap_tiered') 306 if stem_type == 'patch': 307 # NOTE: this stem is a minimal form of ViT PatchEmbed, as used in SwinTransformer w/ patch_size = 4 308 self.stem = nn.Sequential(
292 multi_conv=False, crop_scale=False, qkv_bias=True, drop_rate=0., attn_drop_rate=0., drop_path_rate=0., 293 norm_layer=partial(nn.LayerNorm, eps=1e-6), global_pool='token', 294 ): 295 super().__init__() 296 assert global_pool in ('token', 'avg') 297 298 self.num_classes = num_classes 299 self.global_pool = global_pool
369 ) 370 371 @torch.jit.ignore 372 def set_grad_checkpointing(self, enable=True): 373 assert not enable, 'gradient checkpointing not supported' 374 375 @torch.jit.ignore 376 def get_classifier(self):
378 379 def reset_classifier(self, num_classes, global_pool=None): 380 self.num_classes = num_classes 381 if global_pool is not None: 382 assert global_pool in ('token', 'avg') 383 self.global_pool = global_pool 384 self.head = nn.ModuleList( 385 [nn.Linear(self.embed_dim[i], num_classes) if num_classes > 0 else nn.Identity() for i in
145 down_growth: Union[bool, Tuple[bool, ...]] = False 146 147 def __post_init__(self): 148 n = len(self.depth) 149 assert len(self.out_chs) == n 150 self.stride = _pad_arg(self.stride, n) 151 self.groups = _pad_arg(self.groups, n) 152 self.block_ratio = _pad_arg(self.block_ratio, n)
723 feature_info = [] 724 if not isinstance(out_chs, (tuple, list)): 725 out_chs = [out_chs] 726 stem_depth = len(out_chs) 727 assert stem_depth 728 assert stride in (1, 2, 4) 729 prev_feat = None 730 prev_chs = in_chans
724 if not isinstance(out_chs, (tuple, list)): 725 out_chs = [out_chs] 726 stem_depth = len(out_chs) 727 assert stem_depth 728 assert stride in (1, 2, 4) 729 prev_feat = None 730 prev_chs = in_chans 731 last_idx = stem_depth - 1
745 stem_stride *= conv_stride 746 prev_chs = chs 747 prev_feat = dict(num_chs=prev_chs, reduction=stem_stride, module='.'.join(['stem', conv_name])) 748 if pool: 749 assert stride > 2 750 if prev_feat is not None: 751 feature_info.append(prev_feat) 752 if aa_layer is not None:
763 764 765 def _get_stage_fn(stage_args): 766 stage_type = stage_args.pop('stage_type') 767 assert stage_type in ('dark', 'csp', 'cs3') 768 if stage_type == 'dark': 769 stage_args.pop('expand_ratio', None) 770 stage_args.pop('cross_linear', None)
778 779 780 def _get_block_fn(stage_args): 781 block_type = stage_args.pop('block_type') 782 assert block_type in ('dark', 'edge', 'bottle') 783 if block_type == 'dark': 784 return DarkBlock, stage_args 785 elif block_type == 'edge':
874 ): 875 super().__init__() 876 self.num_classes = num_classes 877 self.drop_rate = drop_rate 878 assert output_stride in (8, 16, 32) 879 layer_args = dict( 880 act_layer=cfg.act_layer, 881 norm_layer=cfg.norm_layer,
917 return matcher 918 919 @torch.jit.ignore 920 def set_grad_checkpointing(self, enable=True): 921 assert not enable, 'gradient checkpointing not supported' 922 923 @torch.jit.ignore 924 def get_classifier(self):
115 116 def __init__(self, *args, **kwargs): 117 weight_init = kwargs.pop('weight_init', '') 118 super().__init__(*args, **kwargs, weight_init='skip') 119 assert self.global_pool in ('token',) 120 121 self.num_prefix_tokens = 2 122 self.dist_token = nn.Parameter(torch.zeros(1, 1, self.embed_dim))
269 self.num_classes = num_classes 270 self.cardinality = cardinality 271 self.base_width = base_width 272 self.drop_rate = drop_rate 273 assert output_stride == 32 # FIXME support dilation 274 275 self.base_layer = nn.Sequential( 276 nn.Conv2d(in_chans, channels[0], kernel_size=7, stride=1, padding=3, bias=False),
331 return matcher 332 333 @torch.jit.ignore 334 def set_grad_checkpointing(self, enable=True): 335 assert not enable, 'gradient checkpointing not supported' 336 337 @torch.jit.ignore 338 def get_classifier(self):
93 elif block_type == 'down': 94 self.key_stride = 2 95 self.has_proj = True 96 else: 97 assert block_type == 'normal' 98 self.key_stride = 1 99 self.has_proj = False 100
173 super(DPN, self).__init__() 174 self.num_classes = num_classes 175 self.drop_rate = drop_rate 176 self.b = b 177 assert output_stride == 32 # FIXME look into dilation support 178 norm_layer = partial(BatchNormAct2d, eps=.001) 179 fc_norm_layer = partial(BatchNormAct2d, eps=.001, act_layer=fc_act_layer, inplace=False) 180 bw_factor = 1 if small else 4
252 return matcher 253 254 @torch.jit.ignore 255 def set_grad_checkpointing(self, enable=True): 256 assert not enable, 'gradient checkpointing not supported' 257 258 @torch.jit.ignore 259 def get_classifier(self):
363 norm_layer = partial(LayerNorm2d, eps=1e-6) 364 norm_layer_cl = partial(nn.LayerNorm, eps=1e-6) 365 self.feature_info = [] 366 367 assert stem_type in ('patch', 'overlap') 368 if stem_type == 'patch': 369 self.stem = nn.Sequential( 370 nn.Conv2d(in_chans, dims[0], kernel_size=4, stride=4, bias=conv_bias),
314 if downsample: 315 self.downsample = Downsample(in_chs=dim, out_chs=dim_out, norm_layer=norm_layer) 316 dim = dim_out 317 else: 318 assert dim == dim_out 319 self.downsample = nn.Identity() 320 321 blocks = []
18 if not group_size: # 0 or None 19 return 1 # normal conv with 1 group 20 else: 21 # NOTE group_size == 1 -> depthwise conv 22 assert channels % group_size == 0 23 return channels // group_size 24 25
97 A list of block args (dicts) 98 Raises: 99 ValueError: if the string def not properly specified (TODO) 100 """ 101 assert isinstance(block_str, str) 102 ops = block_str.split('_') 103 block_type = ops[0] # take the block type off the front 104 ops = ops[1:]
181 kernel_size=int(options['k']), 182 skip=skip is True, 183 )) 184 else: 185 assert False, 'Unknown block type (%s)' % block_type 186 if 'gs' in options: 187 block_args['group_size'] = options['gs'] 188
249 list of list of block kwargs 250 """ 251 arch_args = [] 252 if isinstance(depth_multiplier, tuple): 253 assert len(depth_multiplier) == len(arch_def) 254 else: 255 depth_multiplier = (depth_multiplier,) * len(arch_def) 256 for stack_idx, (block_strings, multiplier) in enumerate(zip(arch_def, depth_multiplier)):
253 assert len(depth_multiplier) == len(arch_def) 254 else: 255 depth_multiplier = (depth_multiplier,) * len(arch_def) 256 for stack_idx, (block_strings, multiplier) in enumerate(zip(arch_def, depth_multiplier)): 257 assert isinstance(block_strings, list) 258 stack_args = [] 259 repeats = [] 260 for block_str in block_strings:
257 assert isinstance(block_strings, list) 258 stack_args = [] 259 repeats = [] 260 for block_str in block_strings: 261 assert isinstance(block_str, str) 262 ba, rep = _decode_block_str(block_str) 263 if ba.get('num_experts', 0) > 0 and experts_multiplier > 1: 264 ba['num_experts'] *= experts_multiplier
301 # old 'depthwise' mode renamed 'expansion' to match TF impl, old expansion mode didn't make sense 302 _logger.warning("feature_location=='depthwise' is deprecated, using 'expansion'") 303 feature_location = 'expansion' 304 self.feature_location = feature_location 305 assert feature_location in ('bottleneck', 'expansion', '') 306 self.verbose = _DEBUG_BUILDER 307 308 # state updated during build, consumed by model
319 ba['force_in_chs'] = self.round_chs_fn(ba['force_in_chs']) 320 ba['pad_type'] = self.pad_type 321 # block act fn overrides the model default 322 ba['act_layer'] = ba['act_layer'] if ba['act_layer'] is not None else self.act_layer 323 assert ba['act_layer'] is not None 324 ba['norm_layer'] = self.norm_layer 325 ba['drop_path_rate'] = drop_path_rate 326 if bt != 'cn':
346 elif bt == 'cn': 347 _log_info_if(' ConvBnAct {}, Args: {}'.format(block_idx, str(ba)), self.verbose) 348 block = ConvBnAct(**ba) 349 else: 350 assert False, 'Uknkown block type (%s) while building model.' % bt 351 352 self.in_chs = ba['out_chs'] # update in_chs for arg of next block 353 return block
378 # outer list of block_args defines the stacks 379 for stack_idx, stack_args in enumerate(model_block_args): 380 last_stack = stack_idx + 1 == len(model_block_args) 381 _log_info_if('Stack: {}'.format(stack_idx), self.verbose) 382 assert isinstance(stack_args, list) 383 384 blocks = [] 385 # each stack (stage of blocks) contains a list of block arguments
386 for block_idx, block_args in enumerate(stack_args): 387 last_block = block_idx + 1 == len(stack_args) 388 _log_info_if(' Block: {}'.format(block_idx), self.verbose) 389 390 assert block_args['stride'] in (1, 2) 391 if block_idx >= 1: # only the first block in any stack can have a stride > 1 392 block_args['stride'] = 1 393
9 10 def parse_model_name(model_name): 11 model_name = model_name.replace('hf_hub', 'hf-hub') # NOTE for backwards compat, to deprecate hf_hub use 12 parsed = urlsplit(model_name) 13 assert parsed.scheme in ('', 'timm', 'hf-hub') 14 if parsed.scheme == 'hf-hub': 15 # FIXME may use fragment as revision, currently `@` in URI path 16 return parsed.scheme, parsed.path
22 def __init__(self, feature_info: List[Dict], out_indices: Tuple[int]): 23 prev_reduction = 1 24 for fi in feature_info: 25 # sanity check the mandatory fields, there may be additional fields depending on the model 26 assert 'num_chs' in fi and fi['num_chs'] > 0 27 assert 'reduction' in fi and fi['reduction'] >= prev_reduction 28 prev_reduction = fi['reduction'] 29 assert 'module' in fi
23 prev_reduction = 1 24 for fi in feature_info: 25 # sanity check the mandatory fields, there may be additional fields depending on the model 26 assert 'num_chs' in fi and fi['num_chs'] > 0 27 assert 'reduction' in fi and fi['reduction'] >= prev_reduction 28 prev_reduction = fi['reduction'] 29 assert 'module' in fi 30 self.out_indices = out_indices
25 # sanity check the mandatory fields, there may be additional fields depending on the model 26 assert 'num_chs' in fi and fi['num_chs'] > 0 27 assert 'reduction' in fi and fi['reduction'] >= prev_reduction 28 prev_reduction = fi['reduction'] 29 assert 'module' in fi 30 self.out_indices = out_indices 31 self.info = feature_info 32
102 m.register_forward_pre_hook(hook_fn) 103 elif hook_type == 'forward': 104 m.register_forward_hook(hook_fn) 105 else: 106 assert False, "Unsupported hook type" 107 self._feature_outputs = defaultdict(OrderedDict) 108 109 def _collect_output_hook(self, hook_id, *args):
138 return feature_info.from_other(out_indices) 139 elif isinstance(feature_info, (list, tuple)): 140 return FeatureInfo(net.feature_info, out_indices) 141 else: 142 assert False, "Provided feature_info is not valid" 143 144 145 def _get_return_layers(feature_info, out_map):
192 self.return_layers[new_name] = str(return_layers[old_name]) 193 remaining.remove(old_name) 194 if not remaining: 195 break 196 assert not remaining and len(self.return_layers) == len(return_layers), \ 197 f'Return layers ({remaining}) are not present in model' 198 self.update(layers) 199 200 def _collect(self, x) -> (Dict[str, torch.Tensor]):
249 self, model, 250 out_indices=(0, 1, 2, 3, 4), out_map=None, out_as_dict=False, no_rewrite=False, 251 feature_concat=False, flatten_sequential=False, default_hook_type='forward'): 252 super(FeatureHookNet, self).__init__() 253 assert not torch.jit.is_scripting() 254 self.feature_info = _get_feature_info(model, out_indices) 255 self.out_as_dict = out_as_dict 256 layers = OrderedDict()
255 self.out_as_dict = out_as_dict 256 layers = OrderedDict() 257 hooks = [] 258 if no_rewrite: 259 assert not flatten_sequential 260 if hasattr(model, 'reset_classifier'): # make sure classifier is removed? 261 model.reset_classifier(0) 262 layers['body'] = model
272 hooks.append(dict(module=fn, hook_type=remaining[fn])) 273 del remaining[fn] 274 if not remaining: 275 break 276 assert not remaining, f'Return layers ({remaining}) are not present in model' 277 self.update(layers) 278 self.hooks = FeatureHooks(hooks, model.named_modules(), out_map=out_map) 279
55 return func 56 57 58 def create_feature_extractor(model: nn.Module, return_nodes: Union[Dict[str, str], List[str]]): 59 assert has_fx_feature_extraction, 'Please update to PyTorch 1.10+, torchvision 0.11+ for FX feature extraction' 60 return _create_feature_extractor( 61 model, return_nodes, 62 tracer_kwargs={'leaf_modules': list(_leaf_modules), 'autowrap_functions': list(_autowrap_functions)}
67 """ A FX Graph based feature extractor that works with the model feature_info metadata 68 """ 69 def __init__(self, model, out_indices, out_map=None): 70 super().__init__() 71 assert has_fx_feature_extraction, 'Please update to PyTorch 1.10+, torchvision 0.11+ for FX feature extraction' 72 self.feature_info = _get_feature_info(model, out_indices) 73 if out_map is not None: 74 assert len(out_map) == len(out_indices)
70 super().__init__() 71 assert has_fx_feature_extraction, 'Please update to PyTorch 1.10+, torchvision 0.11+ for FX feature extraction' 72 self.feature_info = _get_feature_info(model, out_indices) 73 if out_map is not None: 74 assert len(out_map) == len(out_indices) 75 return_nodes = { 76 info['module']: out_map[i] if out_map is not None else info['module'] 77 for i, info in enumerate(self.feature_info) if i in out_indices}
112 dim_out = dim_out or dim 113 114 self.norm1 = norm_layer(dim) if norm_layer is not None else nn.Identity() 115 self.conv_block = MbConvBlock(dim, act_layer=act_layer) 116 assert reduction in ('conv', 'max', 'avg') 117 if reduction == 'conv': 118 self.reduction = nn.Conv2d(dim, dim_out, 3, 2, 1, bias=False) 119 elif reduction == 'max':
116 assert reduction in ('conv', 'max', 'avg') 117 if reduction == 'conv': 118 self.reduction = nn.Conv2d(dim, dim_out, 3, 2, 1, bias=False) 119 elif reduction == 'max': 120 assert dim == dim_out 121 self.reduction = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) 122 else: 123 assert dim == dim_out
119 elif reduction == 'max': 120 assert dim == dim_out 121 self.reduction = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) 122 else: 123 assert dim == dim_out 124 self.reduction = nn.AvgPool2d(kernel_size=2) 125 self.norm2 = norm_layer(dim_out) if norm_layer is not None else nn.Identity() 126
433 self.num_features = int(embed_dim * 2 ** (num_stages - 1)) 434 if window_size is not None: 435 window_size = to_ntuple(num_stages)(window_size) 436 else: 437 assert window_ratio is not None 438 window_size = tuple([(img_size[0] // r, img_size[1] // r) for r in to_ntuple(num_stages)(window_ratio)]) 439 440 self.stem = Stem(
136 def __init__( 137 self, cfgs, num_classes=1000, width=1.0, in_chans=3, output_stride=32, global_pool='avg', drop_rate=0.2): 138 super(GhostNet, self).__init__() 139 # setting of inverted residual blocks 140 assert output_stride == 32, 'only output_stride==32 is valid, dilation not supported' 141 self.cfgs = cfgs 142 self.num_classes = num_classes 143 self.drop_rate = drop_rate
67 class Block(nn.Module): 68 def __init__(self, inplanes, planes, stride=1, dilation=1, start_with_relu=True, norm_layer=None): 69 super(Block, self).__init__() 70 if isinstance(planes, (list, tuple)): 71 assert len(planes) == 3 72 else: 73 planes = (planes,) * 3 74 outplanes = planes[-1]
192 return matcher 193 194 @torch.jit.ignore 195 def set_grad_checkpointing(self, enable=True): 196 assert not enable, "gradient checkpointing not supported" 197 198 @torch.jit.ignore 199 def get_classifier(self):
123 pretrained_loc = '' 124 if cfg_source == 'hf-hub' and has_hf_hub(necessary=True): 125 # hf-hub specified as source via model identifier 126 load_from = 'hf-hub' 127 assert hf_hub_id 128 pretrained_loc = hf_hub_id 129 else: 130 # default source == timm or unspecified
199 conv_weight = conv_weight.float() # Some weights are in torch.half, ensure it's float for sum on CPU 200 O, I, J, K = conv_weight.shape 201 if in_chans == 1: 202 if I > 3: 203 assert conv_weight.shape[1] % 3 == 0 204 # For models with space2depth stems 205 conv_weight = conv_weight.reshape(O, I // 3, 3, J, K) 206 conv_weight = conv_weight.sum(dim=2, keepdim=False)
422 # pretrained_cfg has one input_size=(C, H ,W) entry 423 if n == 'img_size': 424 input_size = pretrained_cfg.get('input_size', None) 425 if input_size is not None: 426 assert len(input_size) == 3 427 kwargs.setdefault(n, input_size[-2:]) 428 elif n == 'in_chans': 429 input_size = pretrained_cfg.get('input_size', None)
427 kwargs.setdefault(n, input_size[-2:]) 428 elif n == 'in_chans': 429 input_size = pretrained_cfg.get('input_size', None) 430 if input_size is not None: 431 assert len(input_size) == 3 432 kwargs.setdefault(n, input_size[0]) 433 else: 434 default_val = pretrained_cfg.get(n, None)
567 feature_cls = FeatureHookNet 568 elif feature_cls == 'fx': 569 feature_cls = FeatureGraphNet 570 else: 571 assert False, f'Unknown feature class {feature_cls}' 572 model = feature_cls(model, **feature_cfg) 573 model.pretrained_cfg = pretrained_cfg_for_features(pretrained_cfg) # add back default_cfg 574 model.default_cfg = model.pretrained_cfg # alias for backwards compat
669 lid += 1 670 layer_id_to_param[lid].extend(grouping[k]) 671 672 if reverse: 673 assert not output_values, "reverse mapping only sensible for name output" 674 # output reverse mapping 675 param_to_layer_id = {} 676 for lid, lm in layer_id_to_param.items():
694 return matcher 695 696 @torch.jit.ignore 697 def set_grad_checkpointing(self, enable=True): 698 assert not enable, "gradient checkpointing not supported" 699 700 @torch.jit.ignore 701 def get_classifier(self):
763 """ 764 765 def __init__(self, cfg, in_chans=3, num_classes=1000, global_pool='avg', drop_rate=0.0, 766 feature_location='incre', out_indices=(0, 1, 2, 3, 4)): 767 assert feature_location in ('incre', '') 768 super(HighResolutionNetFeatures, self).__init__( 769 cfg, in_chans=in_chans, num_classes=num_classes, global_pool=global_pool, 770 drop_rate=drop_rate, head=feature_location)
771 self.feature_info = FeatureInfo(self.feature_info, out_indices) 772 self._out_idx = {i for i in out_indices} 773 774 def forward_features(self, x): 775 assert False, 'Not supported' 776 777 def forward(self, x) -> List[torch.tensor]: 778 out = []
69 70 def hf_split(hf_id): 71 # FIXME I may change @ -> # and be parsed as fragment in a URI model name scheme 72 rev_split = hf_id.split('@') 73 assert 0 < len(rev_split) <= 2, 'hf_hub id should only contain one @ character to identify revision.' 74 hf_model_id = rev_split[0] 75 hf_revision = rev_split[-1] if len(rev_split) > 1 else None 76 return hf_model_id, hf_revision
87 return hf_hub_download(hf_model_id, filename, revision=hf_revision) 88 89 90 def load_model_config_from_hf(model_id: str): 91 assert has_hf_hub(True) 92 cached_file = _download_from_hf(model_id, 'config.json') 93 pretrained_cfg = load_cfg_from_json(cached_file) 94 pretrained_cfg['hf_hub_id'] = model_id # insert hf_hub id for pretrained weight load during model creation
97 return pretrained_cfg, model_name 98 99 100 def load_state_dict_from_hf(model_id: str, filename: str = 'pytorch_model.bin'): 101 assert has_hf_hub(True) 102 cached_file = _download_from_hf(model_id, filename) 103 state_dict = torch.load(cached_file, map_location='cpu') 104 return state_dict
104 return state_dict 105 106 107 def save_for_hf(model, save_directory, model_config=None): 108 assert has_hf_hub(True) 109 model_config = model_config or {} 110 save_directory = Path(save_directory) 111 save_directory.mkdir(exist_ok=True, parents=True)
228 super(InceptionResnetV2, self).__init__() 229 self.drop_rate = drop_rate 230 self.num_classes = num_classes 231 self.num_features = 1536 232 assert output_stride == 32 233 234 self.conv2d_1a = BasicConv2d(in_chans, 32, kernel_size=3, stride=2) 235 self.conv2d_2a = BasicConv2d(32, 32, kernel_size=3, stride=1)
320 return _matcher 321 322 @torch.jit.ignore 323 def set_grad_checkpointing(self, enable=True): 324 assert not enable, "checkpointing not supported" 325 326 @torch.jit.ignore 327 def get_classifier(self):
354 return _matcher 355 356 @torch.jit.ignore 357 def set_grad_checkpointing(self, enable=True): 358 assert not enable, 'gradient checkpointing not supported' 359 360 @torch.jit.ignore 361 def get_classifier(self):
430 def _create_inception_v3(variant, pretrained=False, **kwargs): 431 pretrained_cfg = resolve_pretrained_cfg(variant, pretrained_cfg=kwargs.pop('pretrained_cfg', None)) 432 aux_logits = kwargs.pop('aux_logits', False) 433 if aux_logits: 434 assert not kwargs.pop('features_only', False) 435 model_cls = InceptionV3Aux 436 load_strict = pretrained_cfg['has_aux'] 437 else:
243 244 class InceptionV4(nn.Module): 245 def __init__(self, num_classes=1000, in_chans=3, output_stride=32, drop_rate=0., global_pool='avg'): 246 super(InceptionV4, self).__init__() 247 assert output_stride == 32 248 self.drop_rate = drop_rate 249 self.num_classes = num_classes 250 self.num_features = 1536
291 ) 292 293 @torch.jit.ignore 294 def set_grad_checkpointing(self, enable=True): 295 assert not enable, 'gradient checkpointing not supported' 296 297 @torch.jit.ignore 298 def get_classifier(self):
44 x = adaptive_catavgmax_pool2d(x, output_size) 45 elif pool_type == 'max': 46 x = F.adaptive_max_pool2d(x, output_size) 47 else: 48 assert False, 'Invalid pool type: %s' % pool_type 49 return x 50 51
85 self.flatten = nn.Flatten(1) if flatten else nn.Identity() 86 if pool_type == '': 87 self.pool = nn.Identity() # pass through 88 elif pool_type == 'fast': 89 assert output_size == 1 90 self.pool = FastAdaptiveAvgPool2d(flatten) 91 self.flatten = nn.Identity() 92 elif pool_type == 'avg':
97 self.pool = AdaptiveCatAvgMaxPool2d(output_size) 98 elif pool_type == 'max': 99 self.pool = nn.AdaptiveMaxPool2d(output_size) 100 else: 101 assert False, 'Invalid pool type: %s' % pool_type 102 103 def is_identity(self): 104 return not self.pool_type
40 out_features = out_features or in_features 41 self.qkv = nn.Linear(in_features, embed_dim * 3, bias=qkv_bias) 42 self.proj = nn.Linear(embed_dim, out_features) 43 self.num_heads = num_heads 44 assert embed_dim % num_heads == 0 45 self.head_dim = embed_dim // num_heads 46 self.scale = self.head_dim ** -0.5 47 self.pos_embed = RotaryEmbedding(self.head_dim)
97 super().__init__() 98 99 embed_dim = embed_dim or in_features 100 out_features = out_features or in_features 101 assert embed_dim % num_heads == 0 102 self.feat_size = to_2tuple(feat_size) 103 self.qkv = nn.Linear(in_features, embed_dim * 3, bias=qkv_bias) 104 self.proj = nn.Linear(embed_dim, out_features)
114 115 def forward(self, x): 116 B, _, H, W = x.shape 117 N = H * W 118 assert self.feat_size[0] == H 119 assert self.feat_size[1] == W 120 x = x.reshape(B, -1, N).permute(0, 2, 1) 121 x = torch.cat([x.mean(1, keepdim=True), x], dim=1)
115 def forward(self, x): 116 B, _, H, W = x.shape 117 N = H * W 118 assert self.feat_size[0] == H 119 assert self.feat_size[1] == W 120 x = x.reshape(B, -1, N).permute(0, 2, 1) 121 x = torch.cat([x.mean(1, keepdim=True), x], dim=1) 122 x = x + self.pos_embed.unsqueeze(0).to(x.dtype)
27 torch.Tensor: the transformed tensor. 28 """ 29 def __init__(self, channels, filt_size=3, stride=2) -> None: 30 super(BlurPool2d, self).__init__() 31 assert filt_size > 1 32 self.channels = channels 33 self.filt_size = filt_size 34 self.stride = stride
106 def __init__( 107 self, dim, dim_out=None, feat_size=None, stride=1, num_heads=4, dim_head=None, 108 qk_ratio=1.0, qkv_bias=False, scale_pos_embed=False): 109 super().__init__() 110 assert feat_size is not None, 'A concrete feature size matching expected input (H, W) is required' 111 dim_out = dim_out or dim 112 assert dim_out % num_heads == 0 113 self.num_heads = num_heads
108 qk_ratio=1.0, qkv_bias=False, scale_pos_embed=False): 109 super().__init__() 110 assert feat_size is not None, 'A concrete feature size matching expected input (H, W) is required' 111 dim_out = dim_out or dim 112 assert dim_out % num_heads == 0 113 self.num_heads = num_heads 114 self.dim_head_qk = dim_head or make_divisible(dim_out * qk_ratio, divisor=8) // num_heads 115 self.dim_head_v = dim_out // self.num_heads
10 11 def _create_pool(num_features, num_classes, pool_type='avg', use_conv=False): 12 flatten_in_pool = not use_conv # flatten when we use a Linear layer after pooling 13 if not pool_type: 14 assert num_classes == 0 or use_conv,\ 15 'Pooling can only be disabled if classifier is also removed or conv classifier is used' 16 flatten_in_pool = False # disable flattening if pooling is pass-through (no pooling) 17 global_pool = SelectAdaptivePool2d(pool_type=pool_type, flatten=flatten_in_pool) 18 num_pooled_features = num_features * global_pool.feat_mult()
71 module_cls = BatNonLocalAttn 72 73 # Woops! 74 else: 75 assert False, "Invalid attn module (%s)" % attn_type 76 elif isinstance(attn_type, bool): 77 if attn_type: 78 module_cls = SEModule
14 15 Used extensively by EfficientNet, MobileNetv3 and related networks. 16 """ 17 if isinstance(kernel_size, list): 18 assert 'num_experts' not in kwargs # MixNet + CondConv combo not supported currently 19 if 'groups' in kwargs: 20 groups = kwargs.pop('groups') 21 if groups == in_channels:
20 groups = kwargs.pop('groups') 21 if groups == in_channels: 22 kwargs['depthwise'] = True 23 else: 24 assert groups == 1 25 # We're going to use only lists for defining the MixedConv2d kernel groups, 26 # ints, tuples, other iterables will continue to pass to normal conv and specify h, w. 27 m = MixedConv2d(in_channels, out_channels, kernel_size, **kwargs)
29 return layer_instance 30 31 32 def get_norm_layer(norm_layer): 33 assert isinstance(norm_layer, (type, str, types.FunctionType, functools.partial)) 34 norm_kwargs = {} 35 36 # unbind partial fn, so args can be rebound later
48 norm_layer = norm_layer 49 else: 50 type_name = norm_layer.__name__.lower().replace('_', '') 51 norm_layer = _NORM_MAP.get(type_name, None) 52 assert norm_layer is not None, f"No equivalent norm layer for {type_name}" 53 54 if norm_kwargs: 55 norm_layer = functools.partial(norm_layer, **norm_kwargs) # bind/rebind args
49 return layer_instance 50 51 52 def get_norm_act_layer(norm_layer, act_layer=None): 53 assert isinstance(norm_layer, (type, str, types.FunctionType, functools.partial)) 54 assert act_layer is None or isinstance(act_layer, (type, str, types.FunctionType, functools.partial)) 55 norm_act_kwargs = {} 56
50 51 52 def get_norm_act_layer(norm_layer, act_layer=None): 53 assert isinstance(norm_layer, (type, str, types.FunctionType, functools.partial)) 54 assert act_layer is None or isinstance(act_layer, (type, str, types.FunctionType, functools.partial)) 55 norm_act_kwargs = {} 56 57 # unbind partial fn, so args can be rebound later
79 norm_act_layer = LayerNormAct2d 80 elif type_name.startswith('layernorm'): 81 norm_act_layer = LayerNormAct 82 else: 83 assert False, f"No equivalent norm_act layer for {type_name}" 84 85 if norm_act_layer in _NORM_ACT_REQUIRES_ARG: 86 # pass `act_layer` through for backwards compat where `act_layer=None` implies no activation.
63 super(EcaModule, self).__init__() 64 if channels is not None: 65 t = int(abs(math.log(channels, 2) + beta) / gamma) 66 kernel_size = max(t if t % 2 else t + 1, 3) 67 assert kernel_size % 2 == 1 68 padding = (kernel_size - 1) // 2 69 if use_mlp: 70 # NOTE 'mlp' mode is a timm experiment, not in paper
67 assert kernel_size % 2 == 1 68 padding = (kernel_size - 1) // 2 69 if use_mlp: 70 # NOTE 'mlp' mode is a timm experiment, not in paper 71 assert channels is not None 72 if rd_channels is None: 73 rd_channels = make_divisible(channels * rd_ratio, divisor=rd_divisor) 74 act_layer = act_layer or nn.ReLU
123 if channels is not None: 124 t = int(abs(math.log(channels, 2) + beta) / gamma) 125 kernel_size = max(t if t % 2 else t + 1, 3) 126 has_act = act_layer is not None 127 assert kernel_size % 2 == 1 128 129 # PyTorch circular padding mode is buggy as of pytorch 1.4 130 # see https://github.com/pytorch/pytorch/pull/17240
209 def __init__(self, num_features, groups=32, group_size=None, apply_act=True, eps=1e-5, **_): 210 super().__init__() 211 self.apply_act = apply_act # apply activation (non-linearity) 212 if group_size: 213 assert num_features % group_size == 0 214 self.groups = num_features // group_size 215 else: 216 self.groups = groups
264 self.act = create_act_layer(act_layer) 265 else: 266 self.act = nn.Identity() 267 if group_size: 268 assert num_features % group_size == 0 269 self.groups = num_features // group_size 270 else: 271 self.groups = groups
314 self.act = create_act_layer(act_layer) 315 else: 316 self.act = nn.Identity() 317 if group_size: 318 assert num_features % group_size == 0 319 self.groups = num_features // group_size 320 else: 321 self.groups = groups
35 self.extent = extent 36 if extra_params: 37 self.gather = nn.Sequential() 38 if extent == 0: 39 assert feat_size is not None, 'spatial feature size must be specified for global extent w/ params' 40 self.gather.add_module( 41 'conv1', create_conv2d(channels, channels, kernel_size=feat_size, stride=1, depthwise=True)) 42 if norm_layer:
41 'conv1', create_conv2d(channels, channels, kernel_size=feat_size, stride=1, depthwise=True)) 42 if norm_layer: 43 self.gather.add_module(f'norm1', nn.BatchNorm2d(channels)) 44 else: 45 assert extent % 2 == 0 46 num_conv = int(math.log2(extent)) 47 for i in range(num_conv): 48 self.gather.add_module(
57 if self.extent == 0: 58 self.gk = 0 59 self.gs = 0 60 else: 61 assert extent % 2 == 0 62 self.gk = self.extent * 2 - 1 63 self.gs = self.extent 64
126 self, dim, dim_out=None, feat_size=None, stride=1, num_heads=8, dim_head=None, block_size=8, halo_size=3, 127 qk_ratio=1.0, qkv_bias=False, avg_down=False, scale_pos_embed=False): 128 super().__init__() 129 dim_out = dim_out or dim 130 assert dim_out % num_heads == 0 131 assert stride in (1, 2) 132 self.num_heads = num_heads 133 self.dim_head_qk = dim_head or make_divisible(dim_out * qk_ratio, divisor=8) // num_heads
127 qk_ratio=1.0, qkv_bias=False, avg_down=False, scale_pos_embed=False): 128 super().__init__() 129 dim_out = dim_out or dim 130 assert dim_out % num_heads == 0 131 assert stride in (1, 2) 132 self.num_heads = num_heads 133 self.dim_head_qk = dim_head or make_divisible(dim_out * qk_ratio, divisor=8) // num_heads 134 self.dim_head_v = dim_out // self.num_heads
45 self.eps = eps 46 self.momentum = momentum 47 if apply_act: 48 if isinstance(act_layer, str): 49 assert act_layer in ('leaky_relu', 'elu', 'identity', '') 50 self.act_name = act_layer if act_layer else 'identity' 51 else: 52 # convert act layer passed as type to string
56 self.act_name = 'leaky_relu' 57 elif act_layer is None or act_layer == nn.Identity: 58 self.act_name = 'identity' 59 else: 60 assert False, f'Invalid act layer {act_layer.__name__} for IABN' 61 else: 62 self.act_name = 'identity' 63 self.act_param = act_param
68 self, dim, dim_out=None, feat_size=None, stride=1, num_heads=4, dim_head=16, r=9, 69 qk_ratio=1.0, qkv_bias=False): 70 super().__init__() 71 dim_out = dim_out or dim 72 assert dim_out % num_heads == 0, ' should be divided by num_heads' 73 self.dim_qk = dim_head or make_divisible(dim_out * qk_ratio, divisor=8) // num_heads 74 self.num_heads = num_heads 75 self.dim_v = dim_out // num_heads
87 self.pos_emb = None 88 self.rel_pos_indices = None 89 else: 90 # relative pos embedding 91 assert feat_size is not None 92 feat_size = to_2tuple(feat_size) 93 rel_size = [2 * s - 1 for s in feat_size] 94 self.conv_lambda = None
39 def __init__(self, in_features, hidden_features=None, out_features=None, act_layer=nn.Sigmoid, bias=True, drop=0.): 40 super().__init__() 41 out_features = out_features or in_features 42 hidden_features = hidden_features or in_features 43 assert hidden_features % 2 == 0 44 bias = to_2tuple(bias) 45 drop_probs = to_2tuple(drop) 46
81 self.fc1 = nn.Linear(in_features, hidden_features, bias=bias[0]) 82 self.act = act_layer() 83 self.drop1 = nn.Dropout(drop_probs[0]) 84 if gate_layer is not None: 85 assert hidden_features % 2 == 0 86 self.gate = gate_layer(hidden_features) 87 hidden_features = hidden_features // 2 # FIXME base reduction on gate property? 88 else:
170 171 172 def _num_groups(num_channels, num_groups, group_size): 173 if group_size: 174 assert num_channels % group_size == 0 175 return num_channels // group_size 176 return num_groups 177
62 return AvgPool2dSame(kernel_size, stride=stride, **kwargs) 63 elif pool_type == 'max': 64 return MaxPool2dSame(kernel_size, stride=stride, **kwargs) 65 else: 66 assert False, f'Unsupported pool type {pool_type}' 67 else: 68 if pool_type == 'avg': 69 return nn.AvgPool2d(kernel_size, stride=stride, padding=padding, **kwargs)
69 return nn.AvgPool2d(kernel_size, stride=stride, padding=padding, **kwargs) 70 elif pool_type == 'max': 71 return nn.MaxPool2d(kernel_size, stride=stride, padding=padding, **kwargs) 72 else: 73 assert False, f'Unsupported pool type {pool_type}'
52 53 Returns: 54 55 """ 56 assert dim % 4 == 0, 'Embed dimension must be divisible by 4 for sin-cos 2D position embedding' 57 pos_dim = dim // 4 58 bands = inv_freq_bands(pos_dim, temperature=temperature, step=1, dtype=dtype, device=device) 59
15 def _kernel_valid(k): 16 if isinstance(k, (list, tuple)): 17 for ki in k: 18 return _kernel_valid(ki) 19 assert k >= 3 and k % 2 20 21 22 class SelectiveKernelAttn(nn.Module):
91 self.in_channels = in_channels 92 self.out_channels = out_channels 93 self.split_input = split_input 94 if self.split_input: 95 assert in_channels % self.num_paths == 0 96 in_channels = in_channels // self.num_paths 97 groups = min(out_channels, groups) 98
4 5 class SpaceToDepth(nn.Module): 6 def __init__(self, block_size=4): 7 super().__init__() 8 assert block_size == 4 9 self.bs = block_size 10 11 def forward(self, x):
19 20 def __init__(self, num_features, eps=1e-5, momentum=0.1, affine=True, 21 track_running_stats=True, num_splits=2): 22 super().__init__(num_features, eps, momentum, affine, track_running_stats) 23 assert num_splits > 1, 'Should have at least one aux BN layer (num_splits at least 2)' 24 self.num_splits = num_splits 25 self.aux_bn = nn.ModuleList([ 26 nn.BatchNorm2d(num_features, eps, momentum, affine, track_running_stats) for _ in range(num_splits - 1)])
27 28 def forward(self, input: torch.Tensor): 29 if self.training: # aux BN only relevant while training 30 split_size = input.shape[0] // self.num_splits 31 assert input.shape[0] == split_size * self.num_splits, "batch size must be evenly divisible by num_splits" 32 split_input = input.split(split_size) 33 x = [super().forward(split_input[0])] 34 for i, a in enumerate(self.aux_bn):
1 try: 2 from torch import _assert 3 except ImportError: 4 def _assert(condition: bool, message: str): 5 assert condition, message 6 7 8 def _float_to_int(x: float) -> int:
424 self.use_conv = use_conv 425 if isinstance(img_size, tuple): 426 # FIXME origin impl passes single img/res dim through whole hierarchy, 427 # not sure this model will be used enough to spend time fixing it. 428 assert img_size[0] == img_size[1] 429 img_size = img_size[0] 430 self.num_classes = num_classes 431 self.global_pool = global_pool
433 self.embed_dim = embed_dim 434 self.grad_checkpointing = False 435 436 num_stages = len(embed_dim) 437 assert len(depth) == len(num_heads) == num_stages 438 key_dim = to_ntuple(num_stages)(key_dim) 439 attn_ratio = to_ntuple(num_stages)(attn_ratio) 440 mlp_ratio = to_ntuple(num_stages)(mlp_ratio)
210 norm_eps: Optional[float] = None 211 212 def __post_init__(self): 213 # mbconv vs convnext blocks have different defaults, set in post_init to avoid explicit config args 214 assert self.block_type in ('mbconv', 'convnext') 215 use_mbconv = self.block_type == 'mbconv' 216 if not self.norm_layer: 217 self.norm_layer = 'batchnorm2d' if use_mbconv else 'layernorm2d'
743 ): 744 super().__init__() 745 dim_out = dim_out or dim 746 dim_attn = dim_out if expand_first and dim_out > dim else dim 747 assert dim_attn % dim_head == 0, 'attn dim should be divisible by head_dim' 748 self.num_heads = dim_attn // dim_head 749 self.dim_head = dim_head 750 self.scale = dim_head ** -0.5
812 pool_type: str = 'avg2', 813 bias: bool = True, 814 ): 815 super().__init__() 816 assert pool_type in ('max', 'max2', 'avg', 'avg2') 817 if pool_type == 'max': 818 self.pool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) 819 elif pool_type == 'max2':
887 ('norm', norm_layer(dim)), 888 ('down', Downsample2d(dim, dim, pool_type=cfg.pool_type)), 889 ])) 890 else: 891 assert dim == dim_out 892 self.shortcut = nn.Identity() 893 self.norm1 = norm_layer(dim) 894
950 if not group_size: # 0 or None 951 return 1 # normal conv with 1 group 952 else: 953 # NOTE group_size == 1 -> depthwise conv 954 assert channels % group_size == 0 955 return channels // group_size 956 957
976 self.shortcut = Downsample2d(in_chs, out_chs, pool_type=cfg.pool_type, bias=cfg.output_bias) 977 else: 978 self.shortcut = nn.Identity() 979 980 assert cfg.stride_mode in ('pool', '1x1', 'dw') 981 stride_pool, stride_1, stride_2 = 1, 1, 1 982 if cfg.stride_mode == 'pool': 983 # NOTE this is not described in paper, experiment to find faster option that doesn't stride in 1x1
1066 if conv_mlp: 1067 norm_layer = partial(get_norm_layer(cfg.norm_layer), eps=cfg.norm_eps) 1068 mlp_layer = ConvMlp 1069 else: 1070 assert 'layernorm' in cfg.norm_layer 1071 norm_layer = LayerNorm 1072 mlp_layer = Mlp 1073 self.use_conv_mlp = conv_mlp
1078 self.shortcut = nn.Conv2d(in_chs, out_chs, kernel_size=1, bias=cfg.output_bias) 1079 else: 1080 self.shortcut = nn.Identity() 1081 1082 assert cfg.stride_mode in ('pool', 'dw') 1083 stride_pool, stride_dw = 1, 1 1084 # FIXME handle dilation? 1085 if cfg.stride_mode == 'pool':
1241 cfg: MaxxVitTransformerCfg = MaxxVitTransformerCfg(), 1242 drop_path: float = 0., 1243 ): 1244 super().__init__() 1245 assert dim % 2 == 0 1246 norm_layer = partial(get_norm_layer(cfg.norm_layer_cl), eps=cfg.norm_eps) # NOTE this block is channels-last 1247 act_layer = get_act_layer(cfg.act_layer) 1248
1245 assert dim % 2 == 0 1246 norm_layer = partial(get_norm_layer(cfg.norm_layer_cl), eps=cfg.norm_eps) # NOTE this block is channels-last 1247 act_layer = get_act_layer(cfg.act_layer) 1248 1249 assert cfg.window_size == cfg.grid_size 1250 self.partition_size = to_2tuple(cfg.window_size) 1251 rel_pos_cls = get_rel_pos_cls(cfg, self.partition_size) 1252
1501 block_types = extend_tuple(block_types, depth) 1502 blocks = [] 1503 for i, t in enumerate(block_types): 1504 block_stride = stride if i == 0 else 1 1505 assert t in ('C', 'T', 'M', 'PM') 1506 if t == 'C': 1507 conv_cls = ConvNeXtBlock if conv_cfg.block_type == 'convnext' else MbConvBlock 1508 blocks += [conv_cls(
1585 1586 1587 def cfg_window_size(cfg: MaxxVitTransformerCfg, img_size: Tuple[int, int]): 1588 if cfg.window_size is not None: 1589 assert cfg.grid_size 1590 return cfg 1591 partition_size = img_size[0] // cfg.partition_ratio, img_size[1] // cfg.partition_ratio 1592 cfg = replace(cfg, window_size=partition_size, grid_size=partition_size)
1630 stride = self.stem.stride 1631 feat_size = tuple([i // s for i, s in zip(img_size, to_2tuple(stride))]) 1632 1633 num_stages = len(cfg.embed_dim) 1634 assert len(cfg.depths) == num_stages 1635 dpr = [x.tolist() for x in torch.linspace(0, drop_path_rate, sum(cfg.depths)).split(cfg.depths)] 1636 in_chs = self.stem.out_chs 1637 stages = []
1659 # Classifier head 1660 self.head = ClassifierHead(self.num_features, num_classes, pool_type=global_pool, drop_rate=drop_rate) 1661 1662 # Weight init (default PyTorch init works well for AdamW if scheme not set) 1663 assert cfg.weight_init in ('', 'normal', 'trunc_normal', 'xavier_normal', 'vit_eff') 1664 if cfg.weight_init: 1665 named_apply(partial(self._init_weights, scheme=cfg.weight_init), self) 1666
304 305 def reset_classifier(self, num_classes, global_pool=None): 306 self.num_classes = num_classes 307 if global_pool is not None: 308 assert global_pool in ('', 'avg') 309 self.global_pool = global_pool 310 self.head = nn.Linear(self.embed_dim, num_classes) if num_classes > 0 else nn.Identity() 311
401 # x_prev = [B, C, P, M] 402 batch_size, in_dim, kv_patch_area, kv_num_patches = x.shape 403 q_patch_area, q_num_patches = x.shape[-2:] 404 405 assert ( 406 kv_patch_area == q_patch_area 407 ), "The number of pixels in a patch for query and key_value should be the same" 408 409 # compute query, key, and value 410 # [B, C, P, M] --> [B, 1 + d, P, M]
91 def __post_init__(self): 92 num_stages = len(self.depths) 93 if not isinstance(self.embed_dim, (tuple, list)): 94 self.embed_dim = tuple(self.embed_dim * 2 ** i for i in range(num_stages)) 95 assert len(self.embed_dim) == num_stages 96 97 if not isinstance(self.num_heads, (tuple, list)): 98 self.num_heads = tuple(self.num_heads * 2 ** i for i in range(num_stages))
95 assert len(self.embed_dim) == num_stages 96 97 if not isinstance(self.num_heads, (tuple, list)): 98 self.num_heads = tuple(self.num_heads * 2 ** i for i in range(num_stages)) 99 assert len(self.num_heads) == num_stages 100 101 if self.stride_kv_adaptive is not None and self.stride_kv is None: 102 _stride_kv = self.stride_kv_adaptive
339 340 # relative pos embedding 341 self.rel_pos_type = rel_pos_type 342 if self.rel_pos_type == 'spatial': 343 assert feat_size[0] == feat_size[1] 344 size = feat_size[0] 345 q_size = size // stride_q[1] if len(stride_q) > 0 else size 346 kv_size = size // stride_kv[1] if len(stride_kv) > 0 else size
507 508 # relative pos embedding 509 self.rel_pos_type = rel_pos_type 510 if self.rel_pos_type == 'spatial': 511 assert feat_size[0] == feat_size[1] 512 size = feat_size[0] 513 q_size = size // stride_q[1] if len(stride_q) > 0 else size 514 kv_size = size // stride_kv[1] if len(stride_kv) > 0 else size
925 state_dict = state_dict['model_state'] 926 927 depths = getattr(model, 'depths', None) 928 expand_attn = getattr(model, 'expand_attn', True) 929 assert depths is not None, 'model requires depth attribute to remap checkpoints' 930 depth_map = {} 931 block_idx = 0 932 for stage_idx, d in enumerate(depths):
415 self.stem_size = stem_size 416 self.num_features = num_features 417 self.channel_multiplier = channel_multiplier 418 self.drop_rate = drop_rate 419 assert output_stride == 32 420 421 channels = self.num_features // 24 422 # 24 is default value for the architecture
517 return matcher 518 519 @torch.jit.ignore 520 def set_grad_checkpointing(self, enable=True): 521 assert not enable, 'gradient checkpointing not supported' 522 523 @torch.jit.ignore 524 def get_classifier(self):
189 self.pool = nn.Identity() 190 191 # Transformer encoder 192 if len(drop_path_rates): 193 assert len(drop_path_rates) == depth, 'Must provide as many drop path rates as there are transformer layers' 194 self.transformer_encoder = nn.Sequential(*[ 195 TransformerLayer( 196 dim=embed_dim, num_heads=num_heads, mlp_ratio=mlp_ratio, qkv_bias=qkv_bias,
259 260 for param_name in ['embed_dims', 'num_heads', 'depths']: 261 param_value = locals()[param_name] 262 if isinstance(param_value, collections.abc.Sequence): 263 assert len(param_value) == num_levels, f'Require `len({param_name}) == num_levels`' 264 265 embed_dims = to_ntuple(num_levels)(embed_dims) 266 num_heads = to_ntuple(num_levels)(num_heads)
272 act_layer = act_layer or nn.GELU 273 self.drop_rate = drop_rate 274 self.num_levels = num_levels 275 if isinstance(img_size, collections.abc.Sequence): 276 assert img_size[0] == img_size[1], 'Model only handles square inputs' 277 img_size = img_size[0] 278 assert img_size % patch_size == 0, '`patch_size` must divide `img_size` evenly' 279 self.patch_size = patch_size
274 self.num_levels = num_levels 275 if isinstance(img_size, collections.abc.Sequence): 276 assert img_size[0] == img_size[1], 'Model only handles square inputs' 277 img_size = img_size[0] 278 assert img_size % patch_size == 0, '`patch_size` must divide `img_size` evenly' 279 self.patch_size = patch_size 280 281 # Number of blocks at each level
279 self.patch_size = patch_size 280 281 # Number of blocks at each level 282 self.num_blocks = (4 ** torch.arange(num_levels)).flip(0).tolist() 283 assert (img_size // patch_size) % math.sqrt(self.num_blocks[0]) == 0, \ 284 'First level blocks don\'t fit evenly. Check `img_size`, `patch_size`, and `num_levels`' 285 286 # Block edge size in units of patches 287 # Hint: (img_size // patch_size) gives number of patches along edge of image. sqrt(self.num_blocks[0]) is the
318 self.init_weights(weight_init) 319 320 @torch.jit.ignore 321 def init_weights(self, mode=''): 322 assert mode in ('nlhb', '') 323 head_bias = -math.log(self.num_classes) if 'nlhb' in mode else 0. 324 for level in self.levels: 325 trunc_normal_(level.pos_embed, std=.02, a=-2, b=2)
370 def create_stem(in_chs, out_chs, stem_type='', conv_layer=None, act_layer=None, preact_feature=True): 371 stem_stride = 2 372 stem_feature = dict(num_chs=out_chs, reduction=2, module='stem.conv') 373 stem = OrderedDict() 374 assert stem_type in ('', 'deep', 'deep_tiered', 'deep_quad', '3x3', '7x7', 'deep_pool', '3x3_pool', '7x7_pool') 375 if 'deep' in stem_type: 376 if 'quad' in stem_type: 377 # 4 deep conv stack as in NFNet-F models
374 assert stem_type in ('', 'deep', 'deep_tiered', 'deep_quad', '3x3', '7x7', 'deep_pool', '3x3_pool', '7x7_pool') 375 if 'deep' in stem_type: 376 if 'quad' in stem_type: 377 # 4 deep conv stack as in NFNet-F models 378 assert not 'pool' in stem_type 379 stem_chs = (out_chs // 8, out_chs // 4, out_chs // 2, out_chs) 380 strides = (2, 1, 1, 2) 381 stem_stride = 4
457 self.num_classes = num_classes 458 self.drop_rate = drop_rate 459 self.grad_checkpointing = False 460 461 assert cfg.act_layer in _nonlin_gamma, f"Please add non-linearity constants for activation ({cfg.act_layer})." 462 conv_layer = ScaledStdConv2dSame if cfg.same_padding else ScaledStdConv2d 463 if cfg.gamma_in_act: 464 act_layer = act_with_gamma(cfg.act_layer, gamma=_nonlin_gamma[cfg.act_layer])
151 self, img_size, patch_size, stride, base_dims, depth, heads, 152 mlp_ratio, num_classes=1000, in_chans=3, global_pool='token', 153 distilled=False, attn_drop_rate=.0, drop_rate=.0, drop_path_rate=.0): 154 super(PoolingVisionTransformer, self).__init__() 155 assert global_pool in ('token',) 156 157 padding = 0 158 img_size = to_2tuple(img_size)
214 self.distilled_training = enable 215 216 @torch.jit.ignore 217 def set_grad_checkpointing(self, enable=True): 218 assert not enable, 'gradient checkpointing not supported' 219 220 def get_classifier(self): 221 if self.head_dist is not None:
238 return cls_tokens 239 240 def forward_head(self, x, pre_logits: bool = False) -> torch.Tensor: 241 if self.head_dist is not None: 242 assert self.global_pool == 'token' 243 x, x_dist = x[:, 0], x[:, 1] 244 if not pre_logits: 245 x = self.head(x)
240 super(PNASNet5Large, self).__init__() 241 self.num_classes = num_classes 242 self.drop_rate = drop_rate 243 self.num_features = 4320 244 assert output_stride == 32 245 246 self.conv_0 = ConvNormAct( 247 in_chans, 96, kernel_size=3, stride=2, padding=0,
301 return dict(stem=r'^conv_0|cell_stem_[01]', blocks=r'^cell_(\d+)') 302 303 @torch.jit.ignore 304 def set_grad_checkpointing(self, enable=True): 305 assert not enable, 'gradient checkpointing not supported' 306 307 @torch.jit.ignore 308 def get_classifier(self):
91 attn_drop=0., 92 proj_drop=0. 93 ): 94 super().__init__() 95 assert dim % num_heads == 0, f"dim {dim} should be divided by num_heads {num_heads}." 96 97 self.dim = dim 98 self.num_heads = num_heads
189 """ 190 def __init__(self, patch_size=7, stride=4, in_chans=3, embed_dim=768): 191 super().__init__() 192 patch_size = to_2tuple(patch_size) 193 assert max(patch_size) > stride, "Set larger patch_size than stride" 194 self.patch_size = patch_size 195 self.proj = nn.Conv2d( 196 in_chans, embed_dim, kernel_size=patch_size, stride=stride,
231 stride=2, 232 in_chans=dim, 233 embed_dim=dim_out) 234 else: 235 assert dim == dim_out 236 self.downsample = None 237 238 self.blocks = nn.ModuleList([Block(
283 norm_layer=nn.LayerNorm, 284 ): 285 super().__init__() 286 self.num_classes = num_classes 287 assert global_pool in ('avg', '') 288 self.global_pool = global_pool 289 self.depths = depths 290 num_stages = len(depths)
290 num_stages = len(depths) 291 mlp_ratios = to_ntuple(num_stages)(mlp_ratios) 292 num_heads = to_ntuple(num_stages)(num_heads) 293 sr_ratios = to_ntuple(num_stages)(sr_ratios) 294 assert(len(embed_dims)) == num_stages 295 296 self.patch_embed = OverlapPatchEmbed( 297 patch_size=7,
365 366 def reset_classifier(self, num_classes, global_pool=None): 367 self.num_classes = num_classes 368 if global_pool is not None: 369 assert global_pool in ('avg', '') 370 self.global_pool = global_pool 371 self.head = nn.Linear(self.embed_dim, num_classes) if num_classes > 0 else nn.Identity() 372
120 Args: 121 model_name (str) - name of model to check 122 module_names (tuple, list, set) - names of modules to search in 123 """ 124 assert isinstance(module_names, (tuple, list, set)) 125 return any(model_name in _module_to_models[n] for n in module_names) 126 127
185 186 187 def generate_regnet(width_slope, width_initial, width_mult, depth, group_size, q=8): 188 """Generates per block widths from RegNet parameters.""" 189 assert width_slope >= 0 and width_initial > 0 and width_mult > 1 and width_initial % q == 0 190 # TODO dWr scaling? 191 # depth = int(depth * (scale ** 0.1)) 192 # width_scale = scale ** 0.4 # dWr scale, exp 0.8 / 2, applied to both group and layer widths
226 227 228 def create_shortcut( 229 downsample_type, in_chs, out_chs, kernel_size, stride, dilation=(1, 1), norm_layer=None, preact=False): 230 assert downsample_type in ('avg', 'conv1x1', '', None) 231 if in_chs != out_chs or stride != 1 or dilation[0] != dilation[1]: 232 dargs = dict(stride=stride, dilation=dilation[0], norm_layer=norm_layer, preact=preact) 233 if not downsample_type:
382 drop_rate=0., drop_path_rate=0., zero_init_last=True): 383 super().__init__() 384 self.num_classes = num_classes 385 self.drop_rate = drop_rate 386 assert output_stride in (8, 16, 32) 387 388 # Construct the stem 389 stem_width = cfg.stem_width
398 prev_width = stem_width 399 curr_stride = 2 400 per_stage_args, common_args = self._get_stage_args( 401 cfg, output_stride=output_stride, drop_path_rate=drop_path_rate) 402 assert len(per_stage_args) == 4 403 block_fn = PreBottleneck if cfg.preact else Bottleneck 404 for i, stage_args in enumerate(per_stage_args): 405 stage_name = "s{}".format(i + 1)
62 radix=1, cardinality=1, base_width=64, avd=False, avd_first=False, is_first=False, 63 reduce_first=1, dilation=1, first_dilation=None, act_layer=nn.ReLU, norm_layer=nn.BatchNorm2d, 64 attn_layer=None, aa_layer=None, drop_block=None, drop_path=None): 65 super(ResNestBottleneck, self).__init__() 66 assert reduce_first == 1 # not supported 67 assert attn_layer is None # not supported 68 assert aa_layer is None # TODO not yet supported 69 assert drop_path is None # TODO not yet supported
63 reduce_first=1, dilation=1, first_dilation=None, act_layer=nn.ReLU, norm_layer=nn.BatchNorm2d, 64 attn_layer=None, aa_layer=None, drop_block=None, drop_path=None): 65 super(ResNestBottleneck, self).__init__() 66 assert reduce_first == 1 # not supported 67 assert attn_layer is None # not supported 68 assert aa_layer is None # TODO not yet supported 69 assert drop_path is None # TODO not yet supported 70
64 attn_layer=None, aa_layer=None, drop_block=None, drop_path=None): 65 super(ResNestBottleneck, self).__init__() 66 assert reduce_first == 1 # not supported 67 assert attn_layer is None # not supported 68 assert aa_layer is None # TODO not yet supported 69 assert drop_path is None # TODO not yet supported 70 71 group_width = int(planes * (base_width / 64.)) * cardinality
65 super(ResNestBottleneck, self).__init__() 66 assert reduce_first == 1 # not supported 67 assert attn_layer is None # not supported 68 assert aa_layer is None # TODO not yet supported 69 assert drop_path is None # TODO not yet supported 70 71 group_width = int(planes * (base_width / 64.)) * cardinality 72 first_dilation = first_dilation or dilation
339 reduce_first=1, dilation=1, first_dilation=None, act_layer=nn.ReLU, norm_layer=nn.BatchNorm2d, 340 attn_layer=None, aa_layer=None, drop_block=None, drop_path=None): 341 super(BasicBlock, self).__init__() 342 343 assert cardinality == 1, 'BasicBlock only supports cardinality of 1' 344 assert base_width == 64, 'BasicBlock does not support changing base width' 345 first_planes = planes // reduce_first 346 outplanes = planes * self.expansion
340 attn_layer=None, aa_layer=None, drop_block=None, drop_path=None): 341 super(BasicBlock, self).__init__() 342 343 assert cardinality == 1, 'BasicBlock only supports cardinality of 1' 344 assert base_width == 64, 'BasicBlock does not support changing base width' 345 first_planes = planes // reduce_first 346 outplanes = planes * self.expansion 347 first_dilation = first_dilation or dilation
612 down_kernel_size=1, avg_down=False, act_layer=nn.ReLU, norm_layer=nn.BatchNorm2d, aa_layer=None, 613 drop_rate=0.0, drop_path_rate=0., drop_block_rate=0., zero_init_last=True, block_args=None): 614 super(ResNet, self).__init__() 615 block_args = block_args or dict() 616 assert output_stride in (8, 16, 32) 617 self.num_classes = num_classes 618 self.drop_rate = drop_rate 619 self.grad_checkpointing = False
312 def create_resnetv2_stem( 313 in_chs, out_chs=64, stem_type='', preact=True, 314 conv_layer=StdConv2d, norm_layer=partial(GroupNormAct, num_groups=32)): 315 stem = OrderedDict() 316 assert stem_type in ('', 'fixed', 'same', 'deep', 'deep_fixed', 'deep_same', 'tiered') 317 318 # NOTE conv padding mode can be changed by overriding the conv_layer def 319 if is_stem_deep(stem_type):
153 self.num_classes = num_classes 154 self.drop_rate = drop_rate 155 self.grad_checkpointing = False 156 157 assert output_stride == 32 # FIXME support dilation 158 stem_base_chs = 32 / width_mult if width_mult < 1.0 else 32 159 stem_chs = make_divisible(round(stem_base_chs * width_mult), divisor=ch_div) 160 self.stem = ConvNormAct(in_chans, stem_chs, 3, stride=2, act_layer=act_layer)
111 def __init__(self, in_chs, skip_chs, mid_chs, out_chs, is_first, stride, dilation=1): 112 super(SelecSLSBlock, self).__init__() 113 self.stride = stride 114 self.is_first = is_first 115 assert stride in [1, 2] 116 117 # Process input with 4 conv blocks with the same number of input and output channels 118 self.conv1 = conv_bn(in_chs, mid_chs, 3, stride, dilation=dilation)
124 125 def forward(self, x: List[torch.Tensor]) -> List[torch.Tensor]: 126 if not isinstance(x, list): 127 x = [x] 128 assert len(x) in [1, 2] 129 130 d1 = self.conv1(x[0]) 131 d2 = self.conv3(self.conv2(d1))
183 ) 184 185 @torch.jit.ignore 186 def set_grad_checkpointing(self, enable=True): 187 assert not enable, 'gradient checkpointing not supported' 188 189 @torch.jit.ignore 190 def get_classifier(self):
364 return matcher 365 366 @torch.jit.ignore 367 def set_grad_checkpointing(self, enable=True): 368 assert not enable, 'gradient checkpointing not supported' 369 370 @torch.jit.ignore 371 def get_classifier(self):
74 def get_stage( 75 index, layers, patch_sizes, embed_dims, hidden_sizes, mlp_ratios, block_layer, rnn_layer, mlp_layer, 76 norm_layer, act_layer, num_layers, bidirectional, union, 77 with_fc, drop=0., drop_path_rate=0., **kwargs): 78 assert len(layers) == len(patch_sizes) == len(embed_dims) == len(hidden_sizes) == len(mlp_ratios) 79 blocks = [] 80 for block_idx in range(layers[index]): 81 drop_path = drop_path_rate * (block_idx + sum(layers[:index])) / (sum(layers) - 1)
283 nlhb=False, 284 stem_norm=False, 285 ): 286 super().__init__() 287 assert global_pool in ('', 'avg') 288 self.num_classes = num_classes 289 self.global_pool = global_pool 290 self.num_features = embed_dims[-1] # num_features for consistency with other models
325 ) 326 327 @torch.jit.ignore 328 def set_grad_checkpointing(self, enable=True): 329 assert not enable, 'gradient checkpointing not supported' 330 331 @torch.jit.ignore 332 def get_classifier(self):
334 335 def reset_classifier(self, num_classes, global_pool=None): 336 self.num_classes = num_classes 337 if global_pool is not None: 338 assert global_pool in ('', 'avg') 339 self.global_pool = global_pool 340 self.head = nn.Linear(self.embed_dim, num_classes) if num_classes > 0 else nn.Identity() 341
53 super(SelectiveKernelBasic, self).__init__() 54 55 sk_kwargs = sk_kwargs or {} 56 conv_kwargs = dict(act_layer=act_layer, norm_layer=norm_layer) 57 assert cardinality == 1, 'BasicBlock only supports cardinality of 1' 58 assert base_width == 64, 'BasicBlock doest not support changing base width' 59 first_planes = planes // reduce_first 60 outplanes = planes * self.expansion
54 55 sk_kwargs = sk_kwargs or {} 56 conv_kwargs = dict(act_layer=act_layer, norm_layer=norm_layer) 57 assert cardinality == 1, 'BasicBlock only supports cardinality of 1' 58 assert base_width == 64, 'BasicBlock doest not support changing base width' 59 first_planes = planes // reduce_first 60 outplanes = planes * self.expansion 61 first_dilation = first_dilation or dilation
251 if min(self.input_resolution) <= self.window_size: 252 # if window size is larger than input resolution, we don't partition windows 253 self.shift_size = 0 254 self.window_size = min(self.input_resolution) 255 assert 0 <= self.shift_size < self.window_size, "shift_size must in 0-window_size" 256 257 self.norm1 = norm_layer(dim) 258 self.attn = WindowAttention(
454 window_size=7, mlp_ratio=4., qkv_bias=True, 455 drop_rate=0., attn_drop_rate=0., drop_path_rate=0.1, 456 norm_layer=nn.LayerNorm, ape=False, patch_norm=True, weight_init='', **kwargs): 457 super().__init__() 458 assert global_pool in ('', 'avg') 459 self.num_classes = num_classes 460 self.global_pool = global_pool 461 self.num_layers = len(depths)
508 self.init_weights(weight_init) 509 510 @torch.jit.ignore 511 def init_weights(self, mode=''): 512 assert mode in ('jax', 'jax_nlhb', 'moco', '') 513 if self.absolute_pos_embed is not None: 514 trunc_normal_(self.absolute_pos_embed, std=.02) 515 head_bias = -math.log(self.num_classes) if 'nlhb' in mode else 0.
545 546 def reset_classifier(self, num_classes, global_pool=None): 547 self.num_classes = num_classes 548 if global_pool is not None: 549 assert global_pool in ('', 'avg') 550 self.global_pool = global_pool 551 self.head = nn.Linear(self.num_features, num_classes) if num_classes > 0 else nn.Identity() 552
499 pretrained_window_sizes=(0, 0, 0, 0), **kwargs): 500 super().__init__() 501 502 self.num_classes = num_classes 503 assert global_pool in ('', 'avg') 504 self.global_pool = global_pool 505 self.num_layers = len(depths) 506 self.embed_dim = embed_dim
589 590 def reset_classifier(self, num_classes, global_pool=None): 591 self.num_classes = num_classes 592 if global_pool is not None: 593 assert global_pool in ('', 'avg') 594 self.global_pool = global_pool 595 self.head = nn.Linear(self.num_features, num_classes) if num_classes > 0 else nn.Identity() 596
167 meta_hidden_dim: int = 384, # FIXME what's the optimal value? 168 sequential_attn: bool = False, 169 ) -> None: 170 super(WindowMultiHeadAttention, self).__init__() 171 assert dim % num_heads == 0, \ 172 "The number of input features (in_features) are not divisible by the number of heads (num_heads)." 173 self.in_features: int = dim 174 self.window_size: Tuple[int, int] = window_size 175 self.num_heads: int = num_heads
236 ) -> torch.Tensor: 237 """ 238 """ 239 # FIXME TODO figure out 'sequential' attention mentioned in paper (should reduce GPU memory) 240 assert False, "not implemented" 241 242 def _forward_batch( 243 self,
159 self, img_size=224, patch_size=16, in_chans=3, num_classes=1000, global_pool='token', 160 embed_dim=768, in_dim=48, depth=12, num_heads=12, in_num_head=4, mlp_ratio=4., qkv_bias=False, 161 drop_rate=0., attn_drop_rate=0., drop_path_rate=0., norm_layer=nn.LayerNorm, first_stride=4): 162 super().__init__() 163 assert global_pool in ('', 'token', 'avg') 164 self.num_classes = num_classes 165 self.global_pool = global_pool 166 self.num_features = self.embed_dim = embed_dim # num_features for consistency with other models
233 234 def reset_classifier(self, num_classes, global_pool=None): 235 self.num_classes = num_classes 236 if global_pool is not None: 237 assert global_pool in ('', 'token', 'avg') 238 self.head = nn.Linear(self.embed_dim, num_classes) if num_classes > 0 else nn.Identity() 239 240 def forward_features(self, x):
253 return matcher 254 255 @torch.jit.ignore 256 def set_grad_checkpointing(self, enable=True): 257 assert not enable, 'gradient checkpointing not supported' 258 259 @torch.jit.ignore 260 def get_classifier(self):
67 class LocallyGroupedAttn(nn.Module): 68 """ LSA: self attention within a group 69 """ 70 def __init__(self, dim, num_heads=8, attn_drop=0., proj_drop=0., ws=1): 71 assert ws != 1 72 super(LocallyGroupedAttn, self).__init__() 73 assert dim % num_heads == 0, f"dim {dim} should be divided by num_heads {num_heads}." 74
69 """ 70 def __init__(self, dim, num_heads=8, attn_drop=0., proj_drop=0., ws=1): 71 assert ws != 1 72 super(LocallyGroupedAttn, self).__init__() 73 assert dim % num_heads == 0, f"dim {dim} should be divided by num_heads {num_heads}." 74 75 self.dim = dim 76 self.num_heads = num_heads
152 """ GSA: using a key to summarize the information for a group to be efficient. 153 """ 154 def __init__(self, dim, num_heads=8, attn_drop=0., proj_drop=0., sr_ratio=1): 155 super().__init__() 156 assert dim % num_heads == 0, f"dim {dim} should be divided by num_heads {num_heads}." 157 158 self.dim = dim 159 self.num_heads = num_heads
250 patch_size = to_2tuple(patch_size) 251 252 self.img_size = img_size 253 self.patch_size = patch_size 254 assert img_size[0] % patch_size[0] == 0 and img_size[1] % patch_size[1] == 0, \ 255 f"img_size {img_size} should be divided by patch_size {patch_size}." 256 self.H, self.W = img_size[0] // patch_size[0], img_size[1] // patch_size[1] 257 self.num_patches = self.H * self.W 258 self.proj = nn.Conv2d(in_chans, embed_dim, kernel_size=patch_size, stride=patch_size)
338 return matcher 339 340 @torch.jit.ignore 341 def set_grad_checkpointing(self, enable=True): 342 assert not enable, 'gradient checkpointing not supported' 343 344 @torch.jit.ignore 345 def get_classifier(self):
347 348 def reset_classifier(self, num_classes, global_pool=None): 349 self.num_classes = num_classes 350 if global_pool is not None: 351 assert global_pool in ('', 'avg') 352 self.global_pool = global_pool 353 self.head = nn.Linear(self.num_features, num_classes) if num_classes > 0 else nn.Identity() 354
96 global_pool: str = 'avg', 97 drop_rate: float = 0., 98 ) -> None: 99 super(VGG, self).__init__() 100 assert output_stride == 32 101 self.num_classes = num_classes 102 self.num_features = 4096 103 self.drop_rate = drop_rate
139 return dict(stem=r'^features\.0', blocks=r'^features\.(\d+)') 140 141 @torch.jit.ignore 142 def set_grad_checkpointing(self, enable=True): 143 assert not enable, 'gradient checkpointing not supported' 144 145 @torch.jit.ignore 146 def get_classifier(self):
201 202 class Attention(nn.Module): 203 def __init__(self, dim, num_heads=8, qkv_bias=False, attn_drop=0., proj_drop=0.): 204 super().__init__() 205 assert dim % num_heads == 0, 'dim should be divisible by num_heads' 206 self.num_heads = num_heads 207 head_dim = dim // num_heads 208 self.scale = head_dim ** -0.5
418 norm_layer: (nn.Module): normalization layer 419 act_layer: (nn.Module): MLP activation layer 420 """ 421 super().__init__() 422 assert global_pool in ('', 'avg', 'token') 423 assert class_token or global_pool != 'token' 424 use_fc_norm = global_pool == 'avg' if fc_norm is None else fc_norm 425 norm_layer = norm_layer or partial(nn.LayerNorm, eps=1e-6)
419 act_layer: (nn.Module): MLP activation layer 420 """ 421 super().__init__() 422 assert global_pool in ('', 'avg', 'token') 423 assert class_token or global_pool != 'token' 424 use_fc_norm = global_pool == 'avg' if fc_norm is None else fc_norm 425 norm_layer = norm_layer or partial(nn.LayerNorm, eps=1e-6) 426 act_layer = act_layer or nn.GELU
471 if weight_init != 'skip': 472 self.init_weights(weight_init) 473 474 def init_weights(self, mode=''): 475 assert mode in ('jax', 'jax_nlhb', 'moco', '') 476 head_bias = -math.log(self.num_classes) if 'nlhb' in mode else 0. 477 trunc_normal_(self.pos_embed, std=.02) 478 if self.cls_token is not None:
508 509 def reset_classifier(self, num_classes: int, global_pool=None): 510 self.num_classes = num_classes 511 if global_pool is not None: 512 assert global_pool in ('', 'avg', 'token') 513 self.global_pool = global_pool 514 self.head = nn.Linear(self.embed_dim, num_classes) if num_classes > 0 else nn.Identity() 515
700 posemb_prefix, posemb_grid = posemb[:, :0], posemb[0] 701 gs_old = int(math.sqrt(len(posemb_grid))) 702 if not len(gs_new): # backwards compatibility 703 gs_new = [int(math.sqrt(ntok_new))] * 2 704 assert len(gs_new) >= 2 705 _logger.info('Position embedding grid-size from %s to %s', [gs_old, gs_old], gs_new) 706 posemb_grid = posemb_grid.reshape(1, gs_old, gs_old, -1).permute(0, 3, 1, 2) 707 posemb_grid = F.interpolate(posemb_grid, size=gs_new, mode='bicubic', align_corners=False)
111 embed_dim=768, 112 bias=True, 113 ): 114 super().__init__() 115 assert isinstance(backbone, nn.Module) 116 img_size = to_2tuple(img_size) 117 patch_size = to_2tuple(patch_size) 118 self.img_size = img_size
135 if hasattr(self.backbone, 'feature_info'): 136 feature_dim = self.backbone.feature_info.channels()[-1] 137 else: 138 feature_dim = self.backbone.num_features 139 assert feature_size[0] % patch_size[0] == 0 and feature_size[1] % patch_size[1] == 0 140 self.grid_size = (feature_size[0] // patch_size[0], feature_size[1] // patch_size[1]) 141 self.num_patches = self.grid_size[0] * self.grid_size[1] 142 self.proj = nn.Conv2d(feature_dim, embed_dim, kernel_size=patch_size, stride=patch_size, bias=bias)
101 win_size: Tuple[int, int], 102 pretrained_win_size: Tuple[int, int] = (0, 0), 103 mode='swin', 104 ): 105 assert mode in ('swin', 'cr', 'rw') 106 # as per official swin-v2 impl, supporting timm specific 'cr' and 'rw' log coords as well 107 relative_coords_h = torch.arange(-(win_size[0] - 1), win_size[0], dtype=torch.float32) 108 relative_coords_w = torch.arange(-(win_size[1] - 1), win_size[1], dtype=torch.float32)
205 class RelPosBias(nn.Module): 206 207 def __init__(self, window_size, num_heads, prefix_tokens=0): 208 super().__init__() 209 assert prefix_tokens <= 1 210 self.window_size = window_size 211 self.window_area = window_size[0] * window_size[1] 212 self.bias_shape = (self.window_area + prefix_tokens,) * 2 + (num_heads,)
236 237 class RelPosAttention(nn.Module): 238 def __init__(self, dim, num_heads=8, qkv_bias=False, rel_pos_cls=None, attn_drop=0., proj_drop=0.): 239 super().__init__() 240 assert dim % num_heads == 0, 'dim should be divisible by num_heads' 241 self.num_heads = num_heads 242 head_dim = dim // num_heads 243 self.scale = head_dim ** -0.5
394 norm_layer: (nn.Module): normalization layer 395 act_layer: (nn.Module): MLP activation layer 396 """ 397 super().__init__() 398 assert global_pool in ('', 'avg', 'token') 399 assert class_token or global_pool != 'token' 400 norm_layer = norm_layer or partial(nn.LayerNorm, eps=1e-6) 401 act_layer = act_layer or nn.GELU
395 act_layer: (nn.Module): MLP activation layer 396 """ 397 super().__init__() 398 assert global_pool in ('', 'avg', 'token') 399 assert class_token or global_pool != 'token' 400 norm_layer = norm_layer or partial(nn.LayerNorm, eps=1e-6) 401 act_layer = act_layer or nn.GELU 402
446 if weight_init != 'skip': 447 self.init_weights(weight_init) 448 449 def init_weights(self, mode=''): 450 assert mode in ('jax', 'moco', '') 451 if self.cls_token is not None: 452 nn.init.normal_(self.cls_token, std=1e-6) 453 # FIXME weight init scheme using PyTorch defaults curently
474 475 def reset_classifier(self, num_classes: int, global_pool=None): 476 self.num_classes = num_classes 477 if global_pool is not None: 478 assert global_pool in ('', 'avg', 'token') 479 self.global_pool = global_pool 480 self.head = nn.Linear(self.embed_dim, num_classes) if num_classes > 0 else nn.Identity() 481
299 def __init__( 300 self, img_size=224, stem_conv=False, stem_stride=1, 301 patch_size=8, in_chans=3, hidden_dim=64, embed_dim=384): 302 super().__init__() 303 assert patch_size in [4, 8, 16] 304 if stem_conv: 305 self.conv = nn.Sequential( 306 nn.Conv2d(in_chans, hidden_dim, kernel_size=7, stride=stem_stride, padding=3, bias=False), # 112x112
422 self.pooling_scale = pooling_scale 423 self.num_features = embed_dims[-1] 424 if use_mix_token: # enable token mixing, see token labeling for details. 425 self.beta = 1.0 426 assert global_pool == 'token', "return all tokens if mix_token is enabled" 427 self.grad_checkpointing = False 428 429 self.patch_embed = PatchEmbed(
188 conv_kwargs = dict(norm_layer=norm_layer, act_layer=act_layer) 189 190 next_in_chs = in_chs 191 if self.depthwise and next_in_chs != mid_chs: 192 assert not residual 193 self.conv_reduction = ConvNormAct(next_in_chs, mid_chs, 1, **conv_kwargs) 194 else: 195 self.conv_reduction = None
274 """ 275 super(VovNet, self).__init__() 276 self.num_classes = num_classes 277 self.drop_rate = drop_rate 278 assert stem_stride in (4, 2) 279 assert output_stride == 32 # FIXME support dilation 280 281 stem_chs = cfg["stem_chs"]
275 super(VovNet, self).__init__() 276 self.num_classes = num_classes 277 self.drop_rate = drop_rate 278 assert stem_stride in (4, 2) 279 assert output_stride == 32 # FIXME support dilation 280 281 stem_chs = cfg["stem_chs"] 282 stage_conv_chs = cfg["stage_conv_chs"]
183 ) 184 185 @torch.jit.ignore 186 def set_grad_checkpointing(self, enable=True): 187 assert not enable, "gradient checkpointing not supported" 188 189 @torch.jit.ignore 190 def get_classifier(self):
179 def __init__( 180 self, block_cfg, num_classes=1000, in_chans=3, output_stride=32, preact=False, 181 act_layer=nn.ReLU, norm_layer=nn.BatchNorm2d, drop_rate=0., global_pool='avg'): 182 super(XceptionAligned, self).__init__() 183 assert output_stride in (8, 16, 32) 184 self.num_classes = num_classes 185 self.drop_rate = drop_rate 186 self.grad_checkpointing = False
356 - Although `layer_norm` is user specifiable, there are hard-coded `BatchNorm2d`s in the local patch 357 interaction (class LPI) and the patch embedding (class ConvPatchEmbed) 358 """ 359 super().__init__() 360 assert global_pool in ('', 'avg', 'token') 361 img_size = to_2tuple(img_size) 362 assert (img_size[0] % patch_size == 0) and (img_size[0] % patch_size == 0), \ 363 '`patch_size` should divide image dimensions evenly'
358 """ 359 super().__init__() 360 assert global_pool in ('', 'avg', 'token') 361 img_size = to_2tuple(img_size) 362 assert (img_size[0] % patch_size == 0) and (img_size[0] % patch_size == 0), \ 363 '`patch_size` should divide image dimensions evenly' 364 norm_layer = norm_layer or partial(nn.LayerNorm, eps=1e-6) 365 act_layer = act_layer or nn.GELU 366
426 427 def reset_classifier(self, num_classes, global_pool=''): 428 self.num_classes = num_classes 429 if global_pool is not None: 430 assert global_pool in ('', 'avg', 'token') 431 self.global_pool = global_pool 432 self.head = nn.Linear(self.num_features, num_classes) if num_classes > 0 else nn.Identity() 433
239 opt_lower = opt.lower() 240 opt_split = opt_lower.split('_') 241 opt_lower = opt_split[-1] 242 if 'fused' in opt_lower: 243 assert has_apex and torch.cuda.is_available(), 'APEX and CUDA required for fused optimizers' 244 245 opt_args = dict(weight_decay=weight_decay, **kwargs) 246 if lr is not None:
329 opt_args.setdefault('betas', (0.95, 0.98)) 330 optimizer = FusedNovoGrad(parameters, **opt_args) 331 332 else: 333 assert False and "Invalid optimizer" 334 raise ValueError 335 336 if len(opt_split) > 1:
47 optimizer, param_group_field="lr", 48 noise_range_t=noise_range_t, noise_pct=noise_pct, noise_std=noise_std, noise_seed=noise_seed, 49 initialize=initialize) 50 51 assert t_initial > 0 52 assert lr_min >= 0 53 if t_initial == 1 and cycle_mul == 1 and cycle_decay == 1: 54 _logger.warning("Cosine annealing scheduler will have no effect on the learning "
48 noise_range_t=noise_range_t, noise_pct=noise_pct, noise_std=noise_std, noise_seed=noise_seed, 49 initialize=initialize) 50 51 assert t_initial > 0 52 assert lr_min >= 0 53 if t_initial == 1 and cycle_mul == 1 and cycle_decay == 1: 54 _logger.warning("Cosine annealing scheduler will have no effect on the learning " 55 "rate since t_initial = t_mul = eta_mul = 1.")
43 optimizer, param_group_field="lr", 44 noise_range_t=noise_range_t, noise_pct=noise_pct, noise_std=noise_std, noise_seed=noise_seed, 45 initialize=initialize) 46 47 assert t_initial > 0 48 assert lr_min >= 0 49 if t_initial == 1 and cycle_mul == 1 and cycle_decay == 1: 50 _logger.warning("Cosine annealing scheduler will have no effect on the learning "
44 noise_range_t=noise_range_t, noise_pct=noise_pct, noise_std=noise_std, noise_seed=noise_seed, 45 initialize=initialize) 46 47 assert t_initial > 0 48 assert lr_min >= 0 49 if t_initial == 1 and cycle_mul == 1 and cycle_decay == 1: 50 _logger.warning("Cosine annealing scheduler will have no effect on the learning " 51 "rate since t_initial = t_mul = eta_mul = 1.")
43 optimizer, param_group_field="lr", 44 noise_range_t=noise_range_t, noise_pct=noise_pct, noise_std=noise_std, noise_seed=noise_seed, 45 initialize=initialize) 46 47 assert t_initial > 0 48 assert lr_min >= 0 49 assert lb < ub 50 assert cycle_limit >= 0
44 noise_range_t=noise_range_t, noise_pct=noise_pct, noise_std=noise_std, noise_seed=noise_seed, 45 initialize=initialize) 46 47 assert t_initial > 0 48 assert lr_min >= 0 49 assert lb < ub 50 assert cycle_limit >= 0 51 assert warmup_t >= 0
45 initialize=initialize) 46 47 assert t_initial > 0 48 assert lr_min >= 0 49 assert lb < ub 50 assert cycle_limit >= 0 51 assert warmup_t >= 0 52 assert warmup_lr_init >= 0
46 47 assert t_initial > 0 48 assert lr_min >= 0 49 assert lb < ub 50 assert cycle_limit >= 0 51 assert warmup_t >= 0 52 assert warmup_lr_init >= 0 53 self.lb = lb
47 assert t_initial > 0 48 assert lr_min >= 0 49 assert lb < ub 50 assert cycle_limit >= 0 51 assert warmup_t >= 0 52 assert warmup_lr_init >= 0 53 self.lb = lb 54 self.ub = ub
48 assert lr_min >= 0 49 assert lb < ub 50 assert cycle_limit >= 0 51 assert warmup_t >= 0 52 assert warmup_lr_init >= 0 53 self.lb = lb 54 self.ub = ub 55 self.t_initial = t_initial
57 self.decreasing = decreasing # a lower metric is better if True 58 self.cmp = operator.lt if decreasing else operator.gt # True if lhs better than rhs 59 self.max_history = max_history 60 self.unwrap_fn = unwrap_fn 61 assert self.max_history >= 1 62 63 def save_checkpoint(self, epoch, metric=None): 64 assert epoch >= 0
60 self.unwrap_fn = unwrap_fn 61 assert self.max_history >= 1 62 63 def save_checkpoint(self, epoch, metric=None): 64 assert epoch >= 0 65 tmp_save_path = os.path.join(self.checkpoint_dir, 'tmp' + self.extension) 66 last_save_path = os.path.join(self.checkpoint_dir, 'last' + self.extension) 67 self._save(tmp_save_path, epoch, metric)
129 _logger.error("Exception '{}' while deleting checkpoint".format(e)) 130 self.checkpoint_files = self.checkpoint_files[:delete_index] 131 132 def save_recovery(self, epoch, batch_idx=0): 133 assert epoch >= 0 134 filename = '-'.join([self.recovery_prefix, str(epoch), str(batch_idx)]) + self.extension 135 save_path = os.path.join(self.recovery_dir, filename) 136 self._save(save_path, epoch)
18 torch.nn.utils.clip_grad_value_(parameters, value) 19 elif mode == 'agc': 20 adaptive_clip_grad(parameters, value, norm_type=norm_type) 21 else: 22 assert False, f"Unknown clip mode ({mode})." 23
41 42 def __call__(self, loss, optimizer, clip_grad=None, clip_mode='norm', parameters=None, create_graph=False): 43 self._scaler.scale(loss).backward(create_graph=create_graph) 44 if clip_grad is not None: 45 assert parameters is not None 46 self._scaler.unscale_(optimizer) # unscale the gradients of optimizer's assigned params in-place 47 dispatch_clip_grad(parameters, clip_grad, mode=clip_mode) 48 self._scaler.step(optimizer)
12 This is hopefully a temporary need in 1.5/1.5.1/1.6 to restore performance due to changes 13 in the JIT exectutor. These API are not supported so could change. 14 """ 15 # 16 assert hasattr(torch._C, '_jit_set_profiling_executor'), "Old JIT behavior doesn't exist!" 17 torch._C._jit_set_profiling_executor(False) 18 torch._C._jit_set_profiling_mode(False) 19 torch._C._jit_override_can_fuse_on_gpu(True)
29 torch._C._jit_override_can_fuse_on_gpu(True) 30 torch._C._jit_set_texpr_fuser_enabled(True) 31 try: 32 torch._C._jit_set_nvfuser_enabled(False) 33 except Exception: 34 pass 35 elif fuser == "old" or fuser == "legacy": 36 torch._C._jit_set_profiling_executor(False) 37 torch._C._jit_set_profiling_mode(False)
38 torch._C._jit_override_can_fuse_on_gpu(True) 39 torch._C._jit_set_texpr_fuser_enabled(False) 40 try: 41 torch._C._jit_set_nvfuser_enabled(False) 42 except Exception: 43 pass 44 elif fuser == "nvfuser" or fuser == "nvf": 45 os.environ['PYTORCH_NVFUSER_DISABLE_FALLBACK'] = '1' 46 #os.environ['PYTORCH_NVFUSER_DISABLE_FMA'] = '1'
54 torch._C._jit_override_can_fuse_on_gpu(False) 55 torch._C._jit_set_nvfuser_guard_mode(True) 56 torch._C._jit_set_nvfuser_enabled(True) 57 else: 58 assert False, f"Invalid jit fuser ({fuser})"
176 include_bn_running_stats (bool): Whether to also (un)freeze the running statistics of batch norm 2d layers. 177 Defaults to `True`. 178 mode (bool): Whether to freeze ("freeze") or unfreeze ("unfreeze"). Defaults to `"freeze"`. 179 """ 180 assert mode in ["freeze", "unfreeze"], '`mode` must be one of "freeze" or "unfreeze"' 181 182 if isinstance(root_module, (torch.nn.modules.batchnorm.BatchNorm2d, torch.nn.modules.batchnorm.SyncBatchNorm)): 183 # Raise assertion here because we can't convert it in place
49 p.requires_grad_(False) 50 51 def _load_checkpoint(self, checkpoint_path): 52 checkpoint = torch.load(checkpoint_path, map_location='cpu') 53 assert isinstance(checkpoint, dict) 54 if 'state_dict_ema' in checkpoint: 55 new_state_dict = OrderedDict() 56 for k, v in checkpoint['state_dict_ema'].items():
19 outdir_inc = outdir + '-' + str(count) 20 while os.path.exists(outdir_inc): 21 count = count + 1 22 outdir_inc = outdir + '-' + str(count) 23 assert count < 100 24 outdir = outdir_inc 25 os.makedirs(outdir) 26 return outdir
35 if os.path.exists(OUTPUT_DIR): 36 shutil.rmtree(OUTPUT_DIR) 37 os.mkdir(OUTPUT_DIR) 38 url = "https://huggingface.co/lllyasviel/sd-controlnet-openpose/resolve/main/images/pose.png" 39 response = requests.get(url) 40 img = Image.open(BytesIO(response.content)).convert("RGB").resize((512, 512)) 41 return img 42
5 import yaml 6 from pathlib import Path 7 from enum import Enum 8 from .log import log 9 import subprocess 10 import threading 11 import comfy 12 import tempfile
15 16 config_path = Path(here, "config.yaml") 17 18 if os.path.exists(config_path): 19 config = yaml.load(open(config_path, "r"), Loader=yaml.FullLoader) 20 21 annotator_ckpts_path = str(Path(here, config["annotator_ckpts_path"])) 22 TEMP_DIR = config["custom_temp_path"]
115 elif self == ResizeMode.INNER_FIT: 116 return 1 117 elif self == ResizeMode.OUTER_FIT: 118 return 2 119 assert False, "NOTREACHED" 120 121 #https://github.com/Mikubill/sd-webui-controlnet/blob/e67e017731aad05796b9615dc6eadce911298ea1/internal_controlnet/external_code.py#L89 122 #Replaced logger with internal log
197 print(prefix, line, end="") 198 199 200 def run_script(cmd, cwd='.'): 201 process = subprocess.Popen(cmd, cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, bufsize=1) 202 203 stdout_thread = threading.Thread(target=handle_stream, args=(process.stdout, "")) 204 stderr_thread = threading.Thread(target=handle_stream, args=(process.stderr, "[!]"))