id
int64
0
190k
prompt
stringlengths
21
13.4M
docstring
stringlengths
1
12k
165,895
import contextlib import math import os from copy import copy from pathlib import Path from urllib.error import URLError import cv2 import matplotlib import matplotlib.pyplot as plt import numpy as np import pandas as pd import seaborn as sn import torch from PIL import Image, ImageDraw, ImageFont from utils import TryExcept, threaded from utils.general import (CONFIG_DIR, FONT, LOGGER, check_font, check_requirements, clip_boxes, increment_path, is_ascii, xywh2xyxy, xyxy2xywh) from utils.metrics import fitness from utils.segment.general import scale_image LOGGER = logging.getLogger(LOGGING_NAME) def denormalize(x, mean=IMAGENET_MEAN, std=IMAGENET_STD): # Denormalize RGB images x per ImageNet stats in BCHW format, i.e. = x * std + mean for i in range(3): x[:, i] = x[:, i] * std[i] + mean[i] return x def imshow_cls(im, labels=None, pred=None, names=None, nmax=25, verbose=False, f=Path('images.jpg')): # Show classification image grid with labels (optional) and predictions (optional) from utils.augmentations import denormalize names = names or [f'class{i}' for i in range(1000)] blocks = torch.chunk(denormalize(im.clone()).cpu().float(), len(im), dim=0) # select batch index 0, block by channels n = min(len(blocks), nmax) # number of plots m = min(8, round(n ** 0.5)) # 8 x 8 default fig, ax = plt.subplots(math.ceil(n / m), m) # 8 rows x n/8 cols ax = ax.ravel() if m > 1 else [ax] # plt.subplots_adjust(wspace=0.05, hspace=0.05) for i in range(n): ax[i].imshow(blocks[i].squeeze().permute((1, 2, 0)).numpy().clip(0.0, 1.0)) ax[i].axis('off') if labels is not None: s = names[labels[i]] + (f'—{names[pred[i]]}' if pred is not None else '') ax[i].set_title(s, fontsize=8, verticalalignment='top') plt.savefig(f, dpi=300, bbox_inches='tight') plt.close() if verbose: LOGGER.info(f'Saving {f}') if labels is not None: LOGGER.info('True: ' + ' '.join(f'{names[i]:3s}' for i in labels[:nmax])) if pred is not None: LOGGER.info('Predicted:' + ' '.join(f'{names[i]:3s}' for i in pred[:nmax])) return f
null
165,899
import contextlib import glob import hashlib import json import math import os import random import shutil import time from itertools import repeat from multiprocessing.pool import Pool, ThreadPool from pathlib import Path from threading import Thread from urllib.parse import urlparse import numpy as np import psutil import torch import torch.nn.functional as F import torchvision import yaml from PIL import ExifTags, Image, ImageOps from torch.utils.data import DataLoader, Dataset, dataloader, distributed from tqdm import tqdm from utils.augmentations import (Albumentations, augment_hsv, classify_albumentations, classify_transforms, copy_paste, letterbox, mixup, random_perspective) from utils.general import (DATASETS_DIR, LOGGER, NUM_THREADS, TQDM_BAR_FORMAT, check_dataset, check_requirements, check_yaml, clean_str, cv2, is_colab, is_kaggle, segments2boxes, unzip_file, xyn2xy, xywh2xyxy, xywhn2xyxy, xyxy2xywhn) from utils.torch_utils import torch_distributed_zero_first def get_hash(paths): # Returns a single hash value of a list of paths (files or dirs) size = sum(os.path.getsize(p) for p in paths if os.path.exists(p)) # sizes h = hashlib.sha256(str(size).encode()) # hash sizes h.update(''.join(paths).encode()) # hash paths return h.hexdigest() # return hash
null
165,903
import contextlib import glob import hashlib import json import math import os import random import shutil import time from itertools import repeat from multiprocessing.pool import Pool, ThreadPool from pathlib import Path from threading import Thread from urllib.parse import urlparse import numpy as np import psutil import torch import torch.nn.functional as F import torchvision import yaml from PIL import ExifTags, Image, ImageOps from torch.utils.data import DataLoader, Dataset, dataloader, distributed from tqdm import tqdm from utils.augmentations import (Albumentations, augment_hsv, classify_albumentations, classify_transforms, copy_paste, letterbox, mixup, random_perspective) from utils.general import (DATASETS_DIR, LOGGER, NUM_THREADS, TQDM_BAR_FORMAT, check_dataset, check_requirements, check_yaml, clean_str, cv2, is_colab, is_kaggle, segments2boxes, unzip_file, xyn2xy, xywh2xyxy, xywhn2xyxy, xyxy2xywhn) from utils.torch_utils import torch_distributed_zero_first IMG_FORMATS = 'bmp', 'dng', 'jpeg', 'jpg', 'mpo', 'png', 'tif', 'tiff', 'webp', 'pfm' def exif_size(img): # Returns exif-corrected PIL size s = img.size # (width, height) with contextlib.suppress(Exception): rotation = dict(img._getexif().items())[orientation] if rotation in [6, 8]: # rotation 270 or 90 s = (s[1], s[0]) return s def exif_transpose(image): """ Transpose a PIL image accordingly if it has an EXIF Orientation tag. Inplace version of https://github.com/python-pillow/Pillow/blob/master/src/PIL/ImageOps.py exif_transpose() :param image: The image to transpose. :return: An image. """ exif = image.getexif() orientation = exif.get(0x0112, 1) # default 1 if orientation > 1: method = { 2: Image.FLIP_LEFT_RIGHT, 3: Image.ROTATE_180, 4: Image.FLIP_TOP_BOTTOM, 5: Image.TRANSPOSE, 6: Image.ROTATE_270, 7: Image.TRANSVERSE, 8: Image.ROTATE_90}.get(orientation) if method is not None: image = image.transpose(method) del exif[0x0112] image.info['exif'] = exif.tobytes() return image def segments2boxes(segments): # Convert segment labels to box labels, i.e. (cls, xy1, xy2, ...) to (cls, xywh) boxes = [] for s in segments: x, y = s.T # segment xy boxes.append([x.min(), y.min(), x.max(), y.max()]) # cls, xyxy return xyxy2xywh(np.array(boxes)) # cls, xywh def verify_image_label(args): # Verify one image-label pair im_file, lb_file, prefix = args nm, nf, ne, nc, msg, segments = 0, 0, 0, 0, '', [] # number (missing, found, empty, corrupt), message, segments try: # verify images im = Image.open(im_file) im.verify() # PIL verify shape = exif_size(im) # image size assert (shape[0] > 9) & (shape[1] > 9), f'image size {shape} <10 pixels' assert im.format.lower() in IMG_FORMATS, f'invalid image format {im.format}' if im.format.lower() in ('jpg', 'jpeg'): with open(im_file, 'rb') as f: f.seek(-2, 2) if f.read() != b'\xff\xd9': # corrupt JPEG ImageOps.exif_transpose(Image.open(im_file)).save(im_file, 'JPEG', subsampling=0, quality=100) msg = f'{prefix}WARNING ⚠️ {im_file}: corrupt JPEG restored and saved' # verify labels if os.path.isfile(lb_file): nf = 1 # label found with open(lb_file) as f: lb = [x.split() for x in f.read().strip().splitlines() if len(x)] if any(len(x) > 6 for x in lb): # is segment classes = np.array([x[0] for x in lb], dtype=np.float32) segments = [np.array(x[1:], dtype=np.float32).reshape(-1, 2) for x in lb] # (cls, xy1...) lb = np.concatenate((classes.reshape(-1, 1), segments2boxes(segments)), 1) # (cls, xywh) lb = np.array(lb, dtype=np.float32) nl = len(lb) if nl: assert lb.shape[1] == 5, f'labels require 5 columns, {lb.shape[1]} columns detected' assert (lb >= 0).all(), f'negative label values {lb[lb < 0]}' assert (lb[:, 1:] <= 1).all(), f'non-normalized or out of bounds coordinates {lb[:, 1:][lb[:, 1:] > 1]}' _, i = np.unique(lb, axis=0, return_index=True) if len(i) < nl: # duplicate row check lb = lb[i] # remove duplicates if segments: segments = [segments[x] for x in i] msg = f'{prefix}WARNING ⚠️ {im_file}: {nl - len(i)} duplicate labels removed' else: ne = 1 # label empty lb = np.zeros((0, 5), dtype=np.float32) else: nm = 1 # label missing lb = np.zeros((0, 5), dtype=np.float32) return im_file, lb, shape, segments, nm, nf, ne, nc, msg except Exception as e: nc = 1 msg = f'{prefix}WARNING ⚠️ {im_file}: ignoring corrupt image/label: {e}' return [None, None, None, None, nm, nf, ne, nc, msg]
null
165,904
import contextlib import glob import hashlib import json import math import os import random import shutil import time from itertools import repeat from multiprocessing.pool import Pool, ThreadPool from pathlib import Path from threading import Thread from urllib.parse import urlparse import numpy as np import psutil import torch import torch.nn.functional as F import torchvision import yaml from PIL import ExifTags, Image, ImageOps from torch.utils.data import DataLoader, Dataset, dataloader, distributed from tqdm import tqdm from utils.augmentations import (Albumentations, augment_hsv, classify_albumentations, classify_transforms, copy_paste, letterbox, mixup, random_perspective) from utils.general import (DATASETS_DIR, LOGGER, NUM_THREADS, TQDM_BAR_FORMAT, check_dataset, check_requirements, check_yaml, clean_str, cv2, is_colab, is_kaggle, segments2boxes, unzip_file, xyn2xy, xywh2xyxy, xywhn2xyxy, xyxy2xywhn) from utils.torch_utils import torch_distributed_zero_first RANK = int(os.getenv('RANK', -1)) PIN_MEMORY = str(os.getenv('PIN_MEMORY', True)).lower() == 'true' def seed_worker(worker_id): # Set dataloader worker seed https://pytorch.org/docs/stable/notes/randomness.html#dataloader worker_seed = torch.initial_seed() % 2 ** 32 np.random.seed(worker_seed) random.seed(worker_seed) class InfiniteDataLoader(dataloader.DataLoader): """ Dataloader that reuses workers Uses same syntax as vanilla DataLoader """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) object.__setattr__(self, 'batch_sampler', _RepeatSampler(self.batch_sampler)) self.iterator = super().__iter__() def __len__(self): return len(self.batch_sampler.sampler) def __iter__(self): for _ in range(len(self)): yield next(self.iterator) class ClassificationDataset(torchvision.datasets.ImageFolder): """ YOLOv5 Classification Dataset. Arguments root: Dataset path transform: torchvision transforms, used by default album_transform: Albumentations transforms, used if installed """ def __init__(self, root, augment, imgsz, cache=False): super().__init__(root=root) self.torch_transforms = classify_transforms(imgsz) self.album_transforms = classify_albumentations(augment, imgsz) if augment else None self.cache_ram = cache is True or cache == 'ram' self.cache_disk = cache == 'disk' self.samples = [list(x) + [Path(x[0]).with_suffix('.npy'), None] for x in self.samples] # file, index, npy, im def __getitem__(self, i): f, j, fn, im = self.samples[i] # filename, index, filename.with_suffix('.npy'), image if self.cache_ram and im is None: im = self.samples[i][3] = cv2.imread(f) elif self.cache_disk: if not fn.exists(): # load npy np.save(fn.as_posix(), cv2.imread(f)) im = np.load(fn) else: # read image im = cv2.imread(f) # BGR if self.album_transforms: sample = self.album_transforms(image=cv2.cvtColor(im, cv2.COLOR_BGR2RGB))['image'] else: sample = self.torch_transforms(im) return sample, j def torch_distributed_zero_first(local_rank: int): # Decorator to make all processes in distributed training wait for each local_master to do something if local_rank not in [-1, 0]: dist.barrier(device_ids=[local_rank]) yield if local_rank == 0: dist.barrier(device_ids=[0]) def create_classification_dataloader(path, imgsz=224, batch_size=16, augment=True, cache=False, rank=-1, workers=8, shuffle=True): # Returns Dataloader object to be used with YOLOv5 Classifier with torch_distributed_zero_first(rank): # init dataset *.cache only once if DDP dataset = ClassificationDataset(root=path, imgsz=imgsz, augment=augment, cache=cache) batch_size = min(batch_size, len(dataset)) nd = torch.cuda.device_count() nw = min([os.cpu_count() // max(nd, 1), batch_size if batch_size > 1 else 0, workers]) sampler = None if rank == -1 else distributed.DistributedSampler(dataset, shuffle=shuffle) generator = torch.Generator() generator.manual_seed(6148914691236517205 + RANK) return InfiniteDataLoader(dataset, batch_size=batch_size, shuffle=shuffle and sampler is None, num_workers=nw, sampler=sampler, pin_memory=PIN_MEMORY, worker_init_fn=seed_worker, generator=generator) # or DataLoader(persistent_workers=True)
null
165,905
import glob import re from pathlib import Path import numpy as np import yaml from utils.plots import Annotator, colors The provided code snippet includes necessary dependencies for implementing the `construct_dataset` function. Write a Python function `def construct_dataset(clearml_info_string)` to solve the following problem: Load in a clearml dataset and fill the internal data_dict with its contents. Here is the function: def construct_dataset(clearml_info_string): """Load in a clearml dataset and fill the internal data_dict with its contents. """ dataset_id = clearml_info_string.replace('clearml://', '') dataset = Dataset.get(dataset_id=dataset_id) dataset_root_path = Path(dataset.get_local_copy()) # We'll search for the yaml file definition in the dataset yaml_filenames = list(glob.glob(str(dataset_root_path / '*.yaml')) + glob.glob(str(dataset_root_path / '*.yml'))) if len(yaml_filenames) > 1: raise ValueError('More than one yaml file was found in the dataset root, cannot determine which one contains ' 'the dataset definition this way.') elif len(yaml_filenames) == 0: raise ValueError('No yaml definition found in dataset root path, check that there is a correct yaml file ' 'inside the dataset root path.') with open(yaml_filenames[0]) as f: dataset_definition = yaml.safe_load(f) assert set(dataset_definition.keys()).issuperset( {'train', 'test', 'val', 'nc', 'names'} ), "The right keys were not found in the yaml file, make sure it at least has the following keys: ('train', 'test', 'val', 'nc', 'names')" data_dict = dict() data_dict['train'] = str( (dataset_root_path / dataset_definition['train']).resolve()) if dataset_definition['train'] else None data_dict['test'] = str( (dataset_root_path / dataset_definition['test']).resolve()) if dataset_definition['test'] else None data_dict['val'] = str( (dataset_root_path / dataset_definition['val']).resolve()) if dataset_definition['val'] else None data_dict['nc'] = dataset_definition['nc'] data_dict['names'] = dataset_definition['names'] return data_dict
Load in a clearml dataset and fill the internal data_dict with its contents.
165,906
import argparse import json import logging import os import sys from pathlib import Path import comet_ml ROOT = FILE.parents[3] from train import train from utils.callbacks import Callbacks from utils.general import increment_path from utils.torch_utils import select_device def get_args(known=False): parser = argparse.ArgumentParser() parser.add_argument('--weights', type=str, default=ROOT / 'yolov5s.pt', help='initial weights path') parser.add_argument('--cfg', type=str, default='', help='model.yaml path') parser.add_argument('--data', type=str, default=ROOT / 'data/coco128.yaml', help='dataset.yaml path') parser.add_argument('--hyp', type=str, default=ROOT / 'data/hyps/hyp.scratch-low.yaml', help='hyperparameters path') parser.add_argument('--epochs', type=int, default=300, help='total training epochs') parser.add_argument('--batch-size', type=int, default=16, help='total batch size for all GPUs, -1 for autobatch') parser.add_argument('--imgsz', '--img', '--img-size', type=int, default=640, help='train, val image size (pixels)') parser.add_argument('--rect', action='store_true', help='rectangular training') parser.add_argument('--resume', nargs='?', const=True, default=False, help='resume most recent training') parser.add_argument('--nosave', action='store_true', help='only save final checkpoint') parser.add_argument('--noval', action='store_true', help='only validate final epoch') parser.add_argument('--noautoanchor', action='store_true', help='disable AutoAnchor') parser.add_argument('--noplots', action='store_true', help='save no plot files') parser.add_argument('--evolve', type=int, nargs='?', const=300, help='evolve hyperparameters for x generations') parser.add_argument('--bucket', type=str, default='', help='gsutil bucket') parser.add_argument('--cache', type=str, nargs='?', const='ram', help='--cache images in "ram" (default) or "disk"') parser.add_argument('--image-weights', action='store_true', help='use weighted image selection for training') parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu') parser.add_argument('--multi-scale', action='store_true', help='vary img-size +/- 50%%') parser.add_argument('--single-cls', action='store_true', help='train multi-class data as single-class') parser.add_argument('--optimizer', type=str, choices=['SGD', 'Adam', 'AdamW'], default='SGD', help='optimizer') parser.add_argument('--sync-bn', action='store_true', help='use SyncBatchNorm, only available in DDP mode') parser.add_argument('--workers', type=int, default=8, help='max dataloader workers (per RANK in DDP mode)') parser.add_argument('--project', default=ROOT / 'runs/train', help='save to project/name') parser.add_argument('--name', default='exp', help='save to project/name') parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment') parser.add_argument('--quad', action='store_true', help='quad dataloader') parser.add_argument('--cos-lr', action='store_true', help='cosine LR scheduler') parser.add_argument('--label-smoothing', type=float, default=0.0, help='Label smoothing epsilon') parser.add_argument('--patience', type=int, default=100, help='EarlyStopping patience (epochs without improvement)') parser.add_argument('--freeze', nargs='+', type=int, default=[0], help='Freeze layers: backbone=10, first3=0 1 2') parser.add_argument('--save-period', type=int, default=-1, help='Save checkpoint every x epochs (disabled if < 1)') parser.add_argument('--seed', type=int, default=0, help='Global training seed') parser.add_argument('--local_rank', type=int, default=-1, help='Automatic DDP Multi-GPU argument, do not modify') # Weights & Biases arguments parser.add_argument('--entity', default=None, help='W&B: Entity') parser.add_argument('--upload_dataset', nargs='?', const=True, default=False, help='W&B: Upload data, "val" option') parser.add_argument('--bbox_interval', type=int, default=-1, help='W&B: Set bounding-box image logging interval') parser.add_argument('--artifact_alias', type=str, default='latest', help='W&B: Version of dataset artifact to use') # Comet Arguments parser.add_argument('--comet_optimizer_config', type=str, help='Comet: Path to a Comet Optimizer Config File.') parser.add_argument('--comet_optimizer_id', type=str, help='Comet: ID of the Comet Optimizer sweep.') parser.add_argument('--comet_optimizer_objective', type=str, help="Comet: Set to 'minimize' or 'maximize'.") parser.add_argument('--comet_optimizer_metric', type=str, help='Comet: Metric to Optimize.') parser.add_argument('--comet_optimizer_workers', type=int, default=1, help='Comet: Number of Parallel Workers to use with the Comet Optimizer.') return parser.parse_known_args()[0] if known else parser.parse_args()
null
165,907
import argparse import json import logging import os import sys from pathlib import Path import comet_ml from train import train from utils.callbacks import Callbacks from utils.general import increment_path from utils.torch_utils import select_device def train(hyp, opt, device, callbacks): # hyp is path/to/hyp.yaml or hyp dictionary save_dir, epochs, batch_size, weights, single_cls, evolve, data, cfg, resume, noval, nosave, workers, freeze = \ Path(opt.save_dir), opt.epochs, opt.batch_size, opt.weights, opt.single_cls, opt.evolve, opt.data, opt.cfg, \ opt.resume, opt.noval, opt.nosave, opt.workers, opt.freeze callbacks.run('on_pretrain_routine_start') # Directories w = save_dir / 'weights' # weights dir (w.parent if evolve else w).mkdir(parents=True, exist_ok=True) # make dir last, best = w / 'last.pt', w / 'best.pt' # Hyperparameters if isinstance(hyp, str): with open(hyp, errors='ignore') as f: hyp = yaml.safe_load(f) # load hyps dict LOGGER.info(colorstr('hyperparameters: ') + ', '.join(f'{k}={v}' for k, v in hyp.items())) opt.hyp = hyp.copy() # for saving hyps to checkpoints # Save run settings if not evolve: yaml_save(save_dir / 'hyp.yaml', hyp) yaml_save(save_dir / 'opt.yaml', vars(opt)) # Loggers data_dict = None if RANK in {-1, 0}: loggers = Loggers(save_dir, weights, opt, hyp, LOGGER) # loggers instance # Register actions for k in methods(loggers): callbacks.register_action(k, callback=getattr(loggers, k)) # Process custom dataset artifact link data_dict = loggers.remote_dataset if resume: # If resuming runs from remote artifact weights, epochs, hyp, batch_size = opt.weights, opt.epochs, opt.hyp, opt.batch_size # Config plots = not evolve and not opt.noplots # create plots cuda = device.type != 'cpu' init_seeds(opt.seed + 1 + RANK, deterministic=True) with torch_distributed_zero_first(LOCAL_RANK): data_dict = data_dict or check_dataset(data) # check if None train_path, val_path = data_dict['train'], data_dict['val'] nc = 1 if single_cls else int(data_dict['nc']) # number of classes names = {0: 'item'} if single_cls and len(data_dict['names']) != 1 else data_dict['names'] # class names is_coco = isinstance(val_path, str) and val_path.endswith('coco/val2017.txt') # COCO dataset # Model check_suffix(weights, '.pt') # check weights pretrained = weights.endswith('.pt') if pretrained: with torch_distributed_zero_first(LOCAL_RANK): weights = attempt_download(weights) # download if not found locally ckpt = torch.load(weights, map_location='cpu') # load checkpoint to CPU to avoid CUDA memory leak model = Model(cfg or ckpt['model'].yaml, ch=3, nc=nc, anchors=hyp.get('anchors')).to(device) # create exclude = ['anchor'] if (cfg or hyp.get('anchors')) and not resume else [] # exclude keys csd = ckpt['model'].float().state_dict() # checkpoint state_dict as FP32 csd = intersect_dicts(csd, model.state_dict(), exclude=exclude) # intersect model.load_state_dict(csd, strict=False) # load LOGGER.info(f'Transferred {len(csd)}/{len(model.state_dict())} items from {weights}') # report else: model = Model(cfg, ch=3, nc=nc, anchors=hyp.get('anchors')).to(device) # create amp = check_amp(model) # check AMP # Freeze freeze = [f'model.{x}.' for x in (freeze if len(freeze) > 1 else range(freeze[0]))] # layers to freeze for k, v in model.named_parameters(): v.requires_grad = True # train all layers # v.register_hook(lambda x: torch.nan_to_num(x)) # NaN to 0 (commented for erratic training results) if any(x in k for x in freeze): LOGGER.info(f'freezing {k}') v.requires_grad = False # Image size gs = max(int(model.stride.max()), 32) # grid size (max stride) imgsz = check_img_size(opt.imgsz, gs, floor=gs * 2) # verify imgsz is gs-multiple # Batch size if RANK == -1 and batch_size == -1: # single-GPU only, estimate best batch size batch_size = check_train_batch_size(model, imgsz, amp) loggers.on_params_update({'batch_size': batch_size}) # Optimizer nbs = 64 # nominal batch size accumulate = max(round(nbs / batch_size), 1) # accumulate loss before optimizing hyp['weight_decay'] *= batch_size * accumulate / nbs # scale weight_decay optimizer = smart_optimizer(model, opt.optimizer, hyp['lr0'], hyp['momentum'], hyp['weight_decay']) # Scheduler if opt.cos_lr: lf = one_cycle(1, hyp['lrf'], epochs) # cosine 1->hyp['lrf'] else: lf = lambda x: (1 - x / epochs) * (1.0 - hyp['lrf']) + hyp['lrf'] # linear scheduler = lr_scheduler.LambdaLR(optimizer, lr_lambda=lf) # plot_lr_scheduler(optimizer, scheduler, epochs) # EMA ema = ModelEMA(model) if RANK in {-1, 0} else None # Resume best_fitness, start_epoch = 0.0, 0 if pretrained: if resume: best_fitness, start_epoch, epochs = smart_resume(ckpt, optimizer, ema, weights, epochs, resume) del ckpt, csd # DP mode if cuda and RANK == -1 and torch.cuda.device_count() > 1: LOGGER.warning('WARNING ⚠️ DP not recommended, use torch.distributed.run for best DDP Multi-GPU results.\n' 'See Multi-GPU Tutorial at https://github.com/ultralytics/yolov5/issues/475 to get started.') model = torch.nn.DataParallel(model) # SyncBatchNorm if opt.sync_bn and cuda and RANK != -1: model = torch.nn.SyncBatchNorm.convert_sync_batchnorm(model).to(device) LOGGER.info('Using SyncBatchNorm()') # Trainloader train_loader, dataset = create_dataloader(train_path, imgsz, batch_size // WORLD_SIZE, gs, single_cls, hyp=hyp, augment=True, cache=None if opt.cache == 'val' else opt.cache, rect=opt.rect, rank=LOCAL_RANK, workers=workers, image_weights=opt.image_weights, quad=opt.quad, prefix=colorstr('train: '), shuffle=True, seed=opt.seed) labels = np.concatenate(dataset.labels, 0) mlc = int(labels[:, 0].max()) # max label class assert mlc < nc, f'Label class {mlc} exceeds nc={nc} in {data}. Possible class labels are 0-{nc - 1}' # Process 0 if RANK in {-1, 0}: val_loader = create_dataloader(val_path, imgsz, batch_size // WORLD_SIZE * 2, gs, single_cls, hyp=hyp, cache=None if noval else opt.cache, rect=True, rank=-1, workers=workers * 2, pad=0.5, prefix=colorstr('val: '))[0] if not resume: if not opt.noautoanchor: check_anchors(dataset, model=model, thr=hyp['anchor_t'], imgsz=imgsz) # run AutoAnchor model.half().float() # pre-reduce anchor precision callbacks.run('on_pretrain_routine_end', labels, names) # DDP mode if cuda and RANK != -1: model = smart_DDP(model) # Model attributes nl = de_parallel(model).model[-1].nl # number of detection layers (to scale hyps) hyp['box'] *= 3 / nl # scale to layers hyp['cls'] *= nc / 80 * 3 / nl # scale to classes and layers hyp['obj'] *= (imgsz / 640) ** 2 * 3 / nl # scale to image size and layers hyp['label_smoothing'] = opt.label_smoothing model.nc = nc # attach number of classes to model model.hyp = hyp # attach hyperparameters to model model.class_weights = labels_to_class_weights(dataset.labels, nc).to(device) * nc # attach class weights model.names = names # Start training t0 = time.time() nb = len(train_loader) # number of batches nw = max(round(hyp['warmup_epochs'] * nb), 100) # number of warmup iterations, max(3 epochs, 100 iterations) # nw = min(nw, (epochs - start_epoch) / 2 * nb) # limit warmup to < 1/2 of training last_opt_step = -1 maps = np.zeros(nc) # mAP per class results = (0, 0, 0, 0, 0, 0, 0) # P, R, mAP@.5, mAP@.5-.95, val_loss(box, obj, cls) scheduler.last_epoch = start_epoch - 1 # do not move scaler = torch.cuda.amp.GradScaler(enabled=amp) stopper, stop = EarlyStopping(patience=opt.patience), False compute_loss_ota = ComputeLossAuxOTA(model) # init loss class compute_loss = ComputeLoss(model) callbacks.run('on_train_start') LOGGER.info(f'Image sizes {imgsz} train, {imgsz} val\n' f'Using {train_loader.num_workers * WORLD_SIZE} dataloader workers\n' f"Logging results to {colorstr('bold', save_dir)}\n" f'Starting training for {epochs} epochs...') for epoch in range(start_epoch, epochs): # epoch ------------------------------------------------------------------ callbacks.run('on_train_epoch_start') model.train() # Update image weights (optional, single-GPU only) if opt.image_weights: cw = model.class_weights.cpu().numpy() * (1 - maps) ** 2 / nc # class weights iw = labels_to_image_weights(dataset.labels, nc=nc, class_weights=cw) # image weights dataset.indices = random.choices(range(dataset.n), weights=iw, k=dataset.n) # rand weighted idx # Update mosaic border (optional) # b = int(random.uniform(0.25 * imgsz, 0.75 * imgsz + gs) // gs * gs) # dataset.mosaic_border = [b - imgsz, -b] # height, width borders mloss = torch.zeros(3, device=device) # mean losses if RANK != -1: train_loader.sampler.set_epoch(epoch) pbar = enumerate(train_loader) LOGGER.info(('\n' + '%11s' * 7) % ('Epoch', 'GPU_mem', 'box_loss', 'obj_loss', 'cls_loss', 'Instances', 'Size')) if RANK in {-1, 0}: pbar = tqdm(pbar, total=nb, bar_format=TQDM_BAR_FORMAT) # progress bar optimizer.zero_grad() for i, (imgs, targets, paths, _) in pbar: # batch ------------------------------------------------------------- callbacks.run('on_train_batch_start') ni = i + nb * epoch # number integrated batches (since train start) imgs = imgs.to(device, non_blocking=True).float() / 255 # uint8 to float32, 0-255 to 0.0-1.0 # Warmup if ni <= nw: xi = [0, nw] # x interp # compute_loss.gr = np.interp(ni, xi, [0.0, 1.0]) # iou loss ratio (obj_loss = 1.0 or iou) accumulate = max(1, np.interp(ni, xi, [1, nbs / batch_size]).round()) for j, x in enumerate(optimizer.param_groups): # bias lr falls from 0.1 to lr0, all other lrs rise from 0.0 to lr0 x['lr'] = np.interp(ni, xi, [hyp['warmup_bias_lr'] if j == 0 else 0.0, x['initial_lr'] * lf(epoch)]) if 'momentum' in x: x['momentum'] = np.interp(ni, xi, [hyp['warmup_momentum'], hyp['momentum']]) # Multi-scale if opt.multi_scale: sz = random.randrange(imgsz * 0.5, imgsz * 1.5 + gs) // gs * gs # size sf = sz / max(imgs.shape[2:]) # scale factor if sf != 1: ns = [math.ceil(x * sf / gs) * gs for x in imgs.shape[2:]] # new shape (stretched to gs-multiple) imgs = nn.functional.interpolate(imgs, size=ns, mode='bilinear', align_corners=False) # Forward with torch.cuda.amp.autocast(amp): pred = model(imgs) # forward loss, loss_items = compute_loss_ota(pred, targets.to(device), imgs) # loss scaled by batch_size if RANK != -1: loss *= WORLD_SIZE # gradient averaged between devices in DDP mode if opt.quad: loss *= 4. # Backward scaler.scale(loss).backward() # Optimize - https://pytorch.org/docs/master/notes/amp_examples.html if ni - last_opt_step >= accumulate: scaler.unscale_(optimizer) # unscale gradients torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=10.0) # clip gradients scaler.step(optimizer) # optimizer.step scaler.update() optimizer.zero_grad() if ema: ema.update(model) last_opt_step = ni # Log if RANK in {-1, 0}: mloss = (mloss * i + loss_items) / (i + 1) # update mean losses mem = f'{torch.cuda.memory_reserved() / 1E9 if torch.cuda.is_available() else 0:.3g}G' # (GB) pbar.set_description(('%11s' * 2 + '%11.4g' * 5) % (f'{epoch}/{epochs - 1}', mem, *mloss, targets.shape[0], imgs.shape[-1])) callbacks.run('on_train_batch_end', model, ni, imgs, targets, paths, list(mloss)) if callbacks.stop_training: return # end batch ------------------------------------------------------------------------------------------------ # Scheduler lr = [x['lr'] for x in optimizer.param_groups] # for loggers scheduler.step() if RANK in {-1, 0}: # mAP callbacks.run('on_train_epoch_end', epoch=epoch) ema.update_attr(model, include=['yaml', 'nc', 'hyp', 'names', 'stride', 'class_weights']) final_epoch = (epoch + 1 == epochs) or stopper.possible_stop if not noval or final_epoch: # Calculate mAP results, maps, _ = validate.run(data_dict, batch_size=batch_size // WORLD_SIZE * 2, imgsz=imgsz, half=amp, model=ema.ema, single_cls=single_cls, dataloader=val_loader, save_dir=save_dir, plots=False, callbacks=callbacks, compute_loss=compute_loss) # Update best mAP fi = fitness(np.array(results).reshape(1, -1)) # weighted combination of [P, R, mAP@.5, mAP@.5-.95] stop = stopper(epoch=epoch, fitness=fi) # early stop check if fi > best_fitness: best_fitness = fi log_vals = list(mloss) + list(results) + lr callbacks.run('on_fit_epoch_end', log_vals, epoch, best_fitness, fi) # Save model if (not nosave) or (final_epoch and not evolve): # if save ckpt = { 'epoch': epoch, 'best_fitness': best_fitness, 'model': deepcopy(de_parallel(model)).half(), 'ema': deepcopy(ema.ema).half(), 'updates': ema.updates, 'optimizer': optimizer.state_dict(), 'opt': vars(opt), 'git': GIT_INFO, # {remote, branch, commit} if a git repo 'date': datetime.now().isoformat()} # Save last, best and delete torch.save(ckpt, last) if best_fitness == fi: torch.save(ckpt, best) if opt.save_period > 0 and epoch % opt.save_period == 0: torch.save(ckpt, w / f'epoch{epoch}.pt') del ckpt callbacks.run('on_model_save', last, epoch, final_epoch, best_fitness, fi) # EarlyStopping if RANK != -1: # if DDP training broadcast_list = [stop if RANK == 0 else None] dist.broadcast_object_list(broadcast_list, 0) # broadcast 'stop' to all ranks if RANK != 0: stop = broadcast_list[0] if stop: break # must break all DDP ranks # end epoch ---------------------------------------------------------------------------------------------------- # end training ----------------------------------------------------------------------------------------------------- if RANK in {-1, 0}: LOGGER.info(f'\n{epoch - start_epoch + 1} epochs completed in {(time.time() - t0) / 3600:.3f} hours.') for f in last, best: if f.exists(): strip_optimizer(f) # strip optimizers if f is best: LOGGER.info(f'\nValidating {f}...') results, _, _ = validate.run( data_dict, batch_size=batch_size // WORLD_SIZE * 2, imgsz=imgsz, model=attempt_load(f, device).half(), iou_thres=0.65 if is_coco else 0.60, # best pycocotools at iou 0.65 single_cls=single_cls, dataloader=val_loader, save_dir=save_dir, save_json=is_coco, verbose=True, plots=plots, callbacks=callbacks, compute_loss=compute_loss) # val best model with plots if is_coco: callbacks.run('on_fit_epoch_end', list(mloss) + list(results) + lr, epoch, best_fitness, fi) callbacks.run('on_train_end', last, best, epoch, results) torch.cuda.empty_cache() return results class Callbacks: """" Handles all registered callbacks for YOLOv5 Hooks """ def __init__(self): # Define the available callbacks self._callbacks = { 'on_pretrain_routine_start': [], 'on_pretrain_routine_end': [], 'on_train_start': [], 'on_train_epoch_start': [], 'on_train_batch_start': [], 'optimizer_step': [], 'on_before_zero_grad': [], 'on_train_batch_end': [], 'on_train_epoch_end': [], 'on_val_start': [], 'on_val_batch_start': [], 'on_val_image_end': [], 'on_val_batch_end': [], 'on_val_end': [], 'on_fit_epoch_end': [], # fit = train + val 'on_model_save': [], 'on_train_end': [], 'on_params_update': [], 'teardown': [],} self.stop_training = False # set True to interrupt training def register_action(self, hook, name='', callback=None): """ Register a new action to a callback hook Args: hook: The callback hook name to register the action to name: The name of the action for later reference callback: The callback to fire """ assert hook in self._callbacks, f"hook '{hook}' not found in callbacks {self._callbacks}" assert callable(callback), f"callback '{callback}' is not callable" self._callbacks[hook].append({'name': name, 'callback': callback}) def get_registered_actions(self, hook=None): """" Returns all the registered actions by callback hook Args: hook: The name of the hook to check, defaults to all """ return self._callbacks[hook] if hook else self._callbacks def run(self, hook, *args, thread=False, **kwargs): """ Loop through the registered actions and fire all callbacks on main thread Args: hook: The name of the hook to check, defaults to all args: Arguments to receive from YOLOv5 thread: (boolean) Run callbacks in daemon thread kwargs: Keyword Arguments to receive from YOLOv5 """ assert hook in self._callbacks, f"hook '{hook}' not found in callbacks {self._callbacks}" for logger in self._callbacks[hook]: if thread: threading.Thread(target=logger['callback'], args=args, kwargs=kwargs, daemon=True).start() else: logger['callback'](*args, **kwargs) def increment_path(path, exist_ok=False, sep='', mkdir=False): # Increment file or directory path, i.e. runs/exp --> runs/exp{sep}2, runs/exp{sep}3, ... etc. path = Path(path) # os-agnostic if path.exists() and not exist_ok: path, suffix = (path.with_suffix(''), path.suffix) if path.is_file() else (path, '') # Method 1 for n in range(2, 9999): p = f'{path}{sep}{n}{suffix}' # increment path if not os.path.exists(p): # break path = Path(p) # Method 2 (deprecated) # dirs = glob.glob(f"{path}{sep}*") # similar paths # matches = [re.search(rf"{path.stem}{sep}(\d+)", d) for d in dirs] # i = [int(m.groups()[0]) for m in matches if m] # indices # n = max(i) + 1 if i else 2 # increment number # path = Path(f"{path}{sep}{n}{suffix}") # increment path if mkdir: path.mkdir(parents=True, exist_ok=True) # make directory return path def select_device(device='', batch_size=0, newline=True): # device = None or 'cpu' or 0 or '0' or '0,1,2,3' s = f'YOLOv5 🚀 {git_describe() or file_date()} Python-{platform.python_version()} torch-{torch.__version__} ' device = str(device).strip().lower().replace('cuda:', '').replace('none', '') # to string, 'cuda:0' to '0' cpu = device == 'cpu' mps = device == 'mps' # Apple Metal Performance Shaders (MPS) if cpu or mps: os.environ['CUDA_VISIBLE_DEVICES'] = '-1' # force torch.cuda.is_available() = False elif device: # non-cpu device requested os.environ['CUDA_VISIBLE_DEVICES'] = device # set environment variable - must be before assert is_available() assert torch.cuda.is_available() and torch.cuda.device_count() >= len(device.replace(',', '')), \ f"Invalid CUDA '--device {device}' requested, use '--device cpu' or pass valid CUDA device(s)" if not cpu and not mps and torch.cuda.is_available(): # prefer GPU if available devices = device.split(',') if device else '0' # range(torch.cuda.device_count()) # i.e. 0,1,6,7 n = len(devices) # device count if n > 1 and batch_size > 0: # check batch_size is divisible by device_count assert batch_size % n == 0, f'batch-size {batch_size} not multiple of GPU count {n}' space = ' ' * (len(s) + 1) for i, d in enumerate(devices): p = torch.cuda.get_device_properties(i) s += f"{'' if i == 0 else space}CUDA:{d} ({p.name}, {p.total_memory / (1 << 20):.0f}MiB)\n" # bytes to MB arg = 'cuda:0' elif mps and getattr(torch, 'has_mps', False) and torch.backends.mps.is_available(): # prefer MPS if available s += 'MPS\n' arg = 'mps' else: # revert to CPU s += 'CPU\n' arg = 'cpu' if not newline: s = s.rstrip() LOGGER.info(s) return torch.device(arg) def run(parameters, opt): hyp_dict = {k: v for k, v in parameters.items() if k not in ['epochs', 'batch_size']} opt.save_dir = str(increment_path(Path(opt.project) / opt.name, exist_ok=opt.exist_ok or opt.evolve)) opt.batch_size = parameters.get('batch_size') opt.epochs = parameters.get('epochs') device = select_device(opt.device, batch_size=opt.batch_size) train(hyp_dict, opt, device, callbacks=Callbacks())
null
165,908
import logging import os from urllib.parse import urlparse try: import comet_ml except (ModuleNotFoundError, ImportError): comet_ml = None import yaml COMET_PREFIX = 'comet://' def download_model_checkpoint(opt, experiment): model_dir = f'{opt.project}/{experiment.name}' os.makedirs(model_dir, exist_ok=True) model_name = COMET_MODEL_NAME model_asset_list = experiment.get_model_asset_list(model_name) if len(model_asset_list) == 0: logger.error(f'COMET ERROR: No checkpoints found for model name : {model_name}') return model_asset_list = sorted( model_asset_list, key=lambda x: x['step'], reverse=True, ) logged_checkpoint_map = {asset['fileName']: asset['assetId'] for asset in model_asset_list} resource_url = urlparse(opt.weights) checkpoint_filename = resource_url.query if checkpoint_filename: asset_id = logged_checkpoint_map.get(checkpoint_filename) else: asset_id = logged_checkpoint_map.get(COMET_DEFAULT_CHECKPOINT_FILENAME) checkpoint_filename = COMET_DEFAULT_CHECKPOINT_FILENAME if asset_id is None: logger.error(f'COMET ERROR: Checkpoint {checkpoint_filename} not found in the given Experiment') return try: logger.info(f'COMET INFO: Downloading checkpoint {checkpoint_filename}') asset_filename = checkpoint_filename model_binary = experiment.get_asset(asset_id, return_type='binary', stream=False) model_download_path = f'{model_dir}/{asset_filename}' with open(model_download_path, 'wb') as f: f.write(model_binary) opt.weights = model_download_path except Exception as e: logger.warning('COMET WARNING: Unable to download checkpoint from Comet') logger.exception(e) try: import comet_ml # Project Configuration config = comet_ml.config.get_config() COMET_PROJECT_NAME = config.get_string(os.getenv('COMET_PROJECT_NAME'), 'comet.project_name', default='yolov5') except (ModuleNotFoundError, ImportError): comet_ml = None COMET_PROJECT_NAME = None The provided code snippet includes necessary dependencies for implementing the `check_comet_weights` function. Write a Python function `def check_comet_weights(opt)` to solve the following problem: Downloads model weights from Comet and updates the weights path to point to saved weights location Args: opt (argparse.Namespace): Command Line arguments passed to YOLOv5 training script Returns: None/bool: Return True if weights are successfully downloaded else return None Here is the function: def check_comet_weights(opt): """Downloads model weights from Comet and updates the weights path to point to saved weights location Args: opt (argparse.Namespace): Command Line arguments passed to YOLOv5 training script Returns: None/bool: Return True if weights are successfully downloaded else return None """ if comet_ml is None: return if isinstance(opt.weights, str): if opt.weights.startswith(COMET_PREFIX): api = comet_ml.API() resource = urlparse(opt.weights) experiment_path = f'{resource.netloc}{resource.path}' experiment = api.get(experiment_path) download_model_checkpoint(opt, experiment) return True return None
Downloads model weights from Comet and updates the weights path to point to saved weights location Args: opt (argparse.Namespace): Command Line arguments passed to YOLOv5 training script Returns: None/bool: Return True if weights are successfully downloaded else return None
165,909
import logging import os from urllib.parse import urlparse try: import comet_ml except (ModuleNotFoundError, ImportError): comet_ml = None import yaml COMET_PREFIX = 'comet://' def download_model_checkpoint(opt, experiment): model_dir = f'{opt.project}/{experiment.name}' os.makedirs(model_dir, exist_ok=True) model_name = COMET_MODEL_NAME model_asset_list = experiment.get_model_asset_list(model_name) if len(model_asset_list) == 0: logger.error(f'COMET ERROR: No checkpoints found for model name : {model_name}') return model_asset_list = sorted( model_asset_list, key=lambda x: x['step'], reverse=True, ) logged_checkpoint_map = {asset['fileName']: asset['assetId'] for asset in model_asset_list} resource_url = urlparse(opt.weights) checkpoint_filename = resource_url.query if checkpoint_filename: asset_id = logged_checkpoint_map.get(checkpoint_filename) else: asset_id = logged_checkpoint_map.get(COMET_DEFAULT_CHECKPOINT_FILENAME) checkpoint_filename = COMET_DEFAULT_CHECKPOINT_FILENAME if asset_id is None: logger.error(f'COMET ERROR: Checkpoint {checkpoint_filename} not found in the given Experiment') return try: logger.info(f'COMET INFO: Downloading checkpoint {checkpoint_filename}') asset_filename = checkpoint_filename model_binary = experiment.get_asset(asset_id, return_type='binary', stream=False) model_download_path = f'{model_dir}/{asset_filename}' with open(model_download_path, 'wb') as f: f.write(model_binary) opt.weights = model_download_path except Exception as e: logger.warning('COMET WARNING: Unable to download checkpoint from Comet') logger.exception(e) def set_opt_parameters(opt, experiment): """Update the opts Namespace with parameters from Comet's ExistingExperiment when resuming a run Args: opt (argparse.Namespace): Namespace of command line options experiment (comet_ml.APIExperiment): Comet API Experiment object """ asset_list = experiment.get_asset_list() resume_string = opt.resume for asset in asset_list: if asset['fileName'] == 'opt.yaml': asset_id = asset['assetId'] asset_binary = experiment.get_asset(asset_id, return_type='binary', stream=False) opt_dict = yaml.safe_load(asset_binary) for key, value in opt_dict.items(): setattr(opt, key, value) opt.resume = resume_string # Save hyperparameters to YAML file # Necessary to pass checks in training script save_dir = f'{opt.project}/{experiment.name}' os.makedirs(save_dir, exist_ok=True) hyp_yaml_path = f'{save_dir}/hyp.yaml' with open(hyp_yaml_path, 'w') as f: yaml.dump(opt.hyp, f) opt.hyp = hyp_yaml_path try: import comet_ml # Project Configuration config = comet_ml.config.get_config() COMET_PROJECT_NAME = config.get_string(os.getenv('COMET_PROJECT_NAME'), 'comet.project_name', default='yolov5') except (ModuleNotFoundError, ImportError): comet_ml = None COMET_PROJECT_NAME = None The provided code snippet includes necessary dependencies for implementing the `check_comet_resume` function. Write a Python function `def check_comet_resume(opt)` to solve the following problem: Restores run parameters to its original state based on the model checkpoint and logged Experiment parameters. Args: opt (argparse.Namespace): Command Line arguments passed to YOLOv5 training script Returns: None/bool: Return True if the run is restored successfully else return None Here is the function: def check_comet_resume(opt): """Restores run parameters to its original state based on the model checkpoint and logged Experiment parameters. Args: opt (argparse.Namespace): Command Line arguments passed to YOLOv5 training script Returns: None/bool: Return True if the run is restored successfully else return None """ if comet_ml is None: return if isinstance(opt.resume, str): if opt.resume.startswith(COMET_PREFIX): api = comet_ml.API() resource = urlparse(opt.resume) experiment_path = f'{resource.netloc}{resource.path}' experiment = api.get(experiment_path) set_opt_parameters(opt, experiment) download_model_checkpoint(opt, experiment) return True return None
Restores run parameters to its original state based on the model checkpoint and logged Experiment parameters. Args: opt (argparse.Namespace): Command Line arguments passed to YOLOv5 training script Returns: None/bool: Return True if the run is restored successfully else return None
165,910
import logging import os import sys from contextlib import contextmanager from pathlib import Path from utils.general import LOGGER, colorstr The provided code snippet includes necessary dependencies for implementing the `all_logging_disabled` function. Write a Python function `def all_logging_disabled(highest_level=logging.CRITICAL)` to solve the following problem: source - https://gist.github.com/simon-weber/7853144 A context manager that will prevent any logging messages triggered during the body from being processed. :param highest_level: the maximum logging level in use. This would only need to be changed if a custom level greater than CRITICAL is defined. Here is the function: def all_logging_disabled(highest_level=logging.CRITICAL): """ source - https://gist.github.com/simon-weber/7853144 A context manager that will prevent any logging messages triggered during the body from being processed. :param highest_level: the maximum logging level in use. This would only need to be changed if a custom level greater than CRITICAL is defined. """ previous_level = logging.root.manager.disable logging.disable(highest_level) try: yield finally: logging.disable(previous_level)
source - https://gist.github.com/simon-weber/7853144 A context manager that will prevent any logging messages triggered during the body from being processed. :param highest_level: the maximum logging level in use. This would only need to be changed if a custom level greater than CRITICAL is defined.
165,911
import argparse import io import torch from flask import Flask, request from PIL import Image models = {} def predict(model): if request.method != 'POST': return if request.files.get('image'): # Method 1 # with request.files["image"] as f: # im = Image.open(io.BytesIO(f.read())) # Method 2 im_file = request.files['image'] im_bytes = im_file.read() im = Image.open(io.BytesIO(im_bytes)) if model in models: results = models[model](im, size=640) # reduce size=320 for faster inference return results.pandas().xyxy[0].to_json(orient='records')
null
165,912
import contextlib import glob import inspect import logging import logging.config import math import os import platform import random import re import signal import subprocess import sys import time import urllib from copy import deepcopy from datetime import datetime from itertools import repeat from multiprocessing.pool import ThreadPool from pathlib import Path from subprocess import check_output from tarfile import is_tarfile from typing import Optional from zipfile import ZipFile, is_zipfile import cv2 import IPython import numpy as np import pandas as pd import pkg_resources as pkg import torch import torchvision import yaml from utils import TryExcept, emojis from utils.downloads import curl_download, gsutil_getsize from utils.metrics import box_iou, fitness def is_chinese(s='人工智能'): # Is string composed of any Chinese characters? return bool(re.search('[\u4e00-\u9fff]', str(s)))
null
165,913
import contextlib import glob import inspect import logging import logging.config import math import os import platform import random import re import signal import subprocess import sys import time import urllib from copy import deepcopy from datetime import datetime from itertools import repeat from multiprocessing.pool import ThreadPool from pathlib import Path from subprocess import check_output from tarfile import is_tarfile from typing import Optional from zipfile import ZipFile, is_zipfile import cv2 import IPython import numpy as np import pandas as pd import pkg_resources as pkg import torch import torchvision import yaml from utils import TryExcept, emojis from utils.downloads import curl_download, gsutil_getsize from utils.metrics import box_iou, fitness def is_colab(): # Is environment a Google Colab instance? return 'google.colab' in sys.modules
null
165,914
import contextlib import glob import inspect import logging import logging.config import math import os import platform import random import re import signal import subprocess import sys import time import urllib from copy import deepcopy from datetime import datetime from itertools import repeat from multiprocessing.pool import ThreadPool from pathlib import Path from subprocess import check_output from tarfile import is_tarfile from typing import Optional from zipfile import ZipFile, is_zipfile import cv2 import IPython import numpy as np import pandas as pd import pkg_resources as pkg import torch import torchvision import yaml from utils import TryExcept, emojis from utils.downloads import curl_download, gsutil_getsize from utils.metrics import box_iou, fitness os.environ['NUMEXPR_MAX_THREADS'] = str(NUM_THREADS) os.environ['OMP_NUM_THREADS'] = '1' if platform.system() == 'darwin' else str(NUM_THREADS) def is_kaggle(): # Is environment a Kaggle Notebook? return os.environ.get('PWD') == '/kaggle/working' and os.environ.get('KAGGLE_URL_BASE') == 'https://www.kaggle.com'
null
165,915
import contextlib import glob import inspect import logging import logging.config import math import os import platform import random import re import signal import subprocess import sys import time import urllib from copy import deepcopy from datetime import datetime from itertools import repeat from multiprocessing.pool import ThreadPool from pathlib import Path from subprocess import check_output from tarfile import is_tarfile from typing import Optional from zipfile import ZipFile, is_zipfile import cv2 import IPython import numpy as np import pandas as pd import pkg_resources as pkg import torch import torchvision import yaml from utils import TryExcept, emojis from utils.downloads import curl_download, gsutil_getsize from utils.metrics import box_iou, fitness os.environ['NUMEXPR_MAX_THREADS'] = str(NUM_THREADS) os.environ['OMP_NUM_THREADS'] = '1' if platform.system() == 'darwin' else str(NUM_THREADS) LOGGING_NAME = 'yolov5' def set_logging(name=LOGGING_NAME, verbose=True): # sets up logging for the given name rank = int(os.getenv('RANK', -1)) # rank in world for Multi-GPU trainings level = logging.INFO if verbose and rank in {-1, 0} else logging.ERROR logging.config.dictConfig({ 'version': 1, 'disable_existing_loggers': False, 'formatters': { name: { 'format': '%(message)s'}}, 'handlers': { name: { 'class': 'logging.StreamHandler', 'formatter': name, 'level': level,}}, 'loggers': { name: { 'level': level, 'handlers': [name], 'propagate': False,}}})
null
165,916
import contextlib import glob import inspect import logging import logging.config import math import os import platform import random import re import signal import subprocess import sys import time import urllib from copy import deepcopy from datetime import datetime from itertools import repeat from multiprocessing.pool import ThreadPool from pathlib import Path from subprocess import check_output from tarfile import is_tarfile from typing import Optional from zipfile import ZipFile, is_zipfile import cv2 import IPython import numpy as np import pandas as pd import pkg_resources as pkg import torch import torchvision import yaml from utils import TryExcept, emojis from utils.downloads import curl_download, gsutil_getsize from utils.metrics import box_iou, fitness os.environ['NUMEXPR_MAX_THREADS'] = str(NUM_THREADS) os.environ['OMP_NUM_THREADS'] = '1' if platform.system() == 'darwin' else str(NUM_THREADS) def is_writeable(dir, test=False): if platform.system() == 'Windows': for fn in LOGGER.info, LOGGER.warning: setattr(LOGGER, fn.__name__, lambda x: fn(emojis(x))) # emoji safe logging def user_config_dir(dir='Ultralytics', env_var='YOLOV5_CONFIG_DIR'): # Return path of user configuration directory. Prefer environment variable if exists. Make dir if required. env = os.getenv(env_var) if env: path = Path(env) # use environment variable else: cfg = {'Windows': 'AppData/Roaming', 'Linux': '.config', 'Darwin': 'Library/Application Support'} # 3 OS dirs path = Path.home() / cfg.get(platform.system(), '') # OS-specific config dir path = (path if is_writeable(path) else Path('/tmp')) / dir # GCP and AWS lambda fix, only /tmp is writeable path.mkdir(exist_ok=True) # make if required return path
null
165,917
import contextlib import glob import inspect import logging import logging.config import math import os import platform import random import re import signal import subprocess import sys import time import urllib from copy import deepcopy from datetime import datetime from itertools import repeat from multiprocessing.pool import ThreadPool from pathlib import Path from subprocess import check_output from tarfile import is_tarfile from typing import Optional from zipfile import ZipFile, is_zipfile import cv2 import IPython import numpy as np import pandas as pd import pkg_resources as pkg import torch import torchvision import yaml from utils import TryExcept, emojis from utils.downloads import curl_download, gsutil_getsize from utils.metrics import box_iou, fitness os.environ['NUMEXPR_MAX_THREADS'] = str(NUM_THREADS) os.environ['OMP_NUM_THREADS'] = '1' if platform.system() == 'darwin' else str(NUM_THREADS) def get_latest_run(search_dir='.'): # Return path to most recent 'last.pt' in /runs (i.e. to --resume from) last_list = glob.glob(f'{search_dir}/**/last*.pt', recursive=True) return max(last_list, key=os.path.getctime) if last_list else ''
null
165,918
import contextlib import glob import inspect import logging import logging.config import math import os import platform import random import re import signal import subprocess import sys import time import urllib from copy import deepcopy from datetime import datetime from itertools import repeat from multiprocessing.pool import ThreadPool from pathlib import Path from subprocess import check_output from tarfile import is_tarfile from typing import Optional from zipfile import ZipFile, is_zipfile import cv2 import IPython import numpy as np import pandas as pd import pkg_resources as pkg import torch import torchvision import yaml from utils import TryExcept, emojis from utils.downloads import curl_download, gsutil_getsize from utils.metrics import box_iou, fitness def file_age(path=__file__): # Return days since last file update dt = (datetime.now() - datetime.fromtimestamp(Path(path).stat().st_mtime)) # delta return dt.days # + dt.seconds / 86400 # fractional days
null
165,919
import contextlib import glob import inspect import logging import logging.config import math import os import platform import random import re import signal import subprocess import sys import time import urllib from copy import deepcopy from datetime import datetime from itertools import repeat from multiprocessing.pool import ThreadPool from pathlib import Path from subprocess import check_output from tarfile import is_tarfile from typing import Optional from zipfile import ZipFile, is_zipfile import cv2 import IPython import numpy as np import pandas as pd import pkg_resources as pkg import torch import torchvision import yaml from utils import TryExcept, emojis from utils.downloads import curl_download, gsutil_getsize from utils.metrics import box_iou, fitness LOGGER = logging.getLogger(LOGGING_NAME) def check_online(): # Check internet connectivity import socket def run_once(): # Check once try: socket.create_connection(('1.1.1.1', 443), 5) # check host accessibility return True except OSError: return False return run_once() or run_once() # check twice to increase robustness to intermittent connectivity issues def colorstr(*input): # Colors a string https://en.wikipedia.org/wiki/ANSI_escape_code, i.e. colorstr('blue', 'hello world') *args, string = input if len(input) > 1 else ('blue', 'bold', input[0]) # color arguments, string colors = { 'black': '\033[30m', # basic colors 'red': '\033[31m', 'green': '\033[32m', 'yellow': '\033[33m', 'blue': '\033[34m', 'magenta': '\033[35m', 'cyan': '\033[36m', 'white': '\033[37m', 'bright_black': '\033[90m', # bright colors 'bright_red': '\033[91m', 'bright_green': '\033[92m', 'bright_yellow': '\033[93m', 'bright_blue': '\033[94m', 'bright_magenta': '\033[95m', 'bright_cyan': '\033[96m', 'bright_white': '\033[97m', 'end': '\033[0m', # misc 'bold': '\033[1m', 'underline': '\033[4m'} return ''.join(colors[x] for x in args) + f'{string}' + colors['end'] def check_git_status(repo='ultralytics/yolov5', branch='master'): # YOLOv5 status check, recommend 'git pull' if code is out of date url = f'https://github.com/{repo}' msg = f', for updates see {url}' s = colorstr('github: ') # string assert Path('.git').exists(), s + 'skipping check (not a git repository)' + msg assert check_online(), s + 'skipping check (offline)' + msg splits = re.split(pattern=r'\s', string=check_output('git remote -v', shell=True).decode()) matches = [repo in s for s in splits] if any(matches): remote = splits[matches.index(True) - 1] else: remote = 'ultralytics' check_output(f'git remote add {remote} {url}', shell=True) check_output(f'git fetch {remote}', shell=True, timeout=5) # git fetch local_branch = check_output('git rev-parse --abbrev-ref HEAD', shell=True).decode().strip() # checked out n = int(check_output(f'git rev-list {local_branch}..{remote}/{branch} --count', shell=True)) # commits behind if n > 0: pull = 'git pull' if remote == 'origin' else f'git pull {remote} {branch}' s += f"⚠️ YOLOv5 is out of date by {n} commit{'s' * (n > 1)}. Use `{pull}` or `git clone {url}` to update." else: s += f'up to date with {url} ✅' LOGGER.info(s)
null
165,920
import contextlib import glob import inspect import logging import logging.config import math import os import platform import random import re import signal import subprocess import sys import time import urllib from copy import deepcopy from datetime import datetime from itertools import repeat from multiprocessing.pool import ThreadPool from pathlib import Path from subprocess import check_output from tarfile import is_tarfile from typing import Optional from zipfile import ZipFile, is_zipfile import cv2 import IPython import numpy as np import pandas as pd import pkg_resources as pkg import torch import torchvision import yaml from utils import TryExcept, emojis from utils.downloads import curl_download, gsutil_getsize from utils.metrics import box_iou, fitness def check_requirements(requirements=ROOT / 'requirements.txt', exclude=(), install=True, cmds=''): # Check installed dependencies meet YOLOv5 requirements (pass *.txt file or list of packages or single package str) prefix = colorstr('red', 'bold', 'requirements:') check_python() # check python version if isinstance(requirements, Path): # requirements.txt file file = requirements.resolve() assert file.exists(), f'{prefix} {file} not found, check failed.' with file.open() as f: requirements = [f'{x.name}{x.specifier}' for x in pkg.parse_requirements(f) if x.name not in exclude] elif isinstance(requirements, str): requirements = [requirements] s = '' n = 0 for r in requirements: try: pkg.require(r) except (pkg.VersionConflict, pkg.DistributionNotFound): # exception if requirements not met s += f'"{r}" ' n += 1 if s and install and AUTOINSTALL: # check environment variable LOGGER.info(f"{prefix} YOLOv5 requirement{'s' * (n > 1)} {s}not found, attempting AutoUpdate...") try: # assert check_online(), "AutoUpdate skipped (offline)" LOGGER.info(check_output(f'pip install {s} {cmds}', shell=True).decode()) source = file if 'file' in locals() else requirements s = f"{prefix} {n} package{'s' * (n > 1)} updated per {source}\n" \ f"{prefix} ⚠️ {colorstr('bold', 'Restart runtime or rerun command for updates to take effect')}\n" LOGGER.info(s) except Exception as e: LOGGER.warning(f'{prefix} ❌ {e}') def check_git_info(path='.'): # YOLOv5 git info check, return {remote, branch, commit} check_requirements('gitpython') import git try: repo = git.Repo(path) remote = repo.remotes.origin.url.replace('.git', '') # i.e. 'https://github.com/ultralytics/yolov5' commit = repo.head.commit.hexsha # i.e. '3134699c73af83aac2a481435550b968d5792c0d' try: branch = repo.active_branch.name # i.e. 'main' except TypeError: # not on any branch branch = None # i.e. 'detached HEAD' state return {'remote': remote, 'branch': branch, 'commit': commit} except git.exc.InvalidGitRepositoryError: # path is not a git dir return {'remote': None, 'branch': None, 'commit': None}
null
165,921
import contextlib import glob import inspect import logging import logging.config import math import os import platform import random import re import signal import subprocess import sys import time import urllib from copy import deepcopy from datetime import datetime from itertools import repeat from multiprocessing.pool import ThreadPool from pathlib import Path from subprocess import check_output from tarfile import is_tarfile from typing import Optional from zipfile import ZipFile, is_zipfile import cv2 import IPython import numpy as np import pandas as pd import pkg_resources as pkg import torch import torchvision import yaml from utils import TryExcept, emojis from utils.downloads import curl_download, gsutil_getsize from utils.metrics import box_iou, fitness def clean_str(s): # Cleans a string by replacing special characters with underscore _ return re.sub(pattern='[|@#!¡·$€%&()=?¿^*;:,¨´><+]', repl='_', string=s)
null
165,922
import contextlib import glob import inspect import logging import logging.config import math import os import platform import random import re import signal import subprocess import sys import time import urllib from copy import deepcopy from datetime import datetime from itertools import repeat from multiprocessing.pool import ThreadPool from pathlib import Path from subprocess import check_output from tarfile import is_tarfile from typing import Optional from zipfile import ZipFile, is_zipfile import cv2 import IPython import numpy as np import pandas as pd import pkg_resources as pkg import torch import torchvision import yaml from utils import TryExcept, emojis from utils.downloads import curl_download, gsutil_getsize from utils.metrics import box_iou, fitness torch.set_printoptions(linewidth=320, precision=5, profile='long') np.set_printoptions(linewidth=320, formatter={'float_kind': '{:11.5g}'.format}) def clip_boxes(boxes, shape): # Clip boxes (xyxy) to image shape (height, width) if isinstance(boxes, torch.Tensor): # faster individually boxes[..., 0].clamp_(0, shape[1]) # x1 boxes[..., 1].clamp_(0, shape[0]) # y1 boxes[..., 2].clamp_(0, shape[1]) # x2 boxes[..., 3].clamp_(0, shape[0]) # y2 else: # np.array (faster grouped) boxes[..., [0, 2]] = boxes[..., [0, 2]].clip(0, shape[1]) # x1, x2 boxes[..., [1, 3]] = boxes[..., [1, 3]].clip(0, shape[0]) # y1, y2 def xyxy2xywhn(x, w=640, h=640, clip=False, eps=0.0): # Convert nx4 boxes from [x1, y1, x2, y2] to [x, y, w, h] normalized where xy1=top-left, xy2=bottom-right if clip: clip_boxes(x, (h - eps, w - eps)) # warning: inplace clip y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x) y[..., 0] = ((x[..., 0] + x[..., 2]) / 2) / w # x center y[..., 1] = ((x[..., 1] + x[..., 3]) / 2) / h # y center y[..., 2] = (x[..., 2] - x[..., 0]) / w # width y[..., 3] = (x[..., 3] - x[..., 1]) / h # height return y
null
165,923
import contextlib import glob import inspect import logging import logging.config import math import os import platform import random import re import signal import subprocess import sys import time import urllib from copy import deepcopy from datetime import datetime from itertools import repeat from multiprocessing.pool import ThreadPool from pathlib import Path from subprocess import check_output from tarfile import is_tarfile from typing import Optional from zipfile import ZipFile, is_zipfile import cv2 import IPython import numpy as np import pandas as pd import pkg_resources as pkg import torch import torchvision import yaml from utils import TryExcept, emojis from utils.downloads import curl_download, gsutil_getsize from utils.metrics import box_iou, fitness torch.set_printoptions(linewidth=320, precision=5, profile='long') np.set_printoptions(linewidth=320, formatter={'float_kind': '{:11.5g}'.format}) def xyn2xy(x, w=640, h=640, padw=0, padh=0): # Convert normalized segments into pixel segments, shape (n,2) y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x) y[..., 0] = w * x[..., 0] + padw # top left x y[..., 1] = h * x[..., 1] + padh # top left y return y
null
165,924
import contextlib import glob import inspect import logging import logging.config import math import os import platform import random import re import signal import subprocess import sys import time import urllib from copy import deepcopy from datetime import datetime from itertools import repeat from multiprocessing.pool import ThreadPool from pathlib import Path from subprocess import check_output from tarfile import is_tarfile from typing import Optional from zipfile import ZipFile, is_zipfile import cv2 import IPython import numpy as np import pandas as pd import pkg_resources as pkg import torch import torchvision import yaml from utils import TryExcept, emojis from utils.downloads import curl_download, gsutil_getsize from utils.metrics import box_iou, fitness def clip_segments(segments, shape): # Clip segments (xy1,xy2,...) to image shape (height, width) if isinstance(segments, torch.Tensor): # faster individually segments[:, 0].clamp_(0, shape[1]) # x segments[:, 1].clamp_(0, shape[0]) # y else: # np.array (faster grouped) segments[:, 0] = segments[:, 0].clip(0, shape[1]) # x segments[:, 1] = segments[:, 1].clip(0, shape[0]) # y def scale_segments(img1_shape, segments, img0_shape, ratio_pad=None, normalize=False): # Rescale coords (xyxy) from img1_shape to img0_shape if ratio_pad is None: # calculate from img0_shape gain = min(img1_shape[0] / img0_shape[0], img1_shape[1] / img0_shape[1]) # gain = old / new pad = (img1_shape[1] - img0_shape[1] * gain) / 2, (img1_shape[0] - img0_shape[0] * gain) / 2 # wh padding else: gain = ratio_pad[0][0] pad = ratio_pad[1] segments[:, 0] -= pad[0] # x padding segments[:, 1] -= pad[1] # y padding segments /= gain clip_segments(segments, img0_shape) if normalize: segments[:, 0] /= img0_shape[1] # width segments[:, 1] /= img0_shape[0] # height return segments
null
165,925
import contextlib import glob import inspect import logging import logging.config import math import os import platform import random import re import signal import subprocess import sys import time import urllib from copy import deepcopy from datetime import datetime from itertools import repeat from multiprocessing.pool import ThreadPool from pathlib import Path from subprocess import check_output from tarfile import is_tarfile from typing import Optional from zipfile import ZipFile, is_zipfile import cv2 import IPython import numpy as np import pandas as pd import pkg_resources as pkg import torch import torchvision import yaml from utils import TryExcept, emojis from utils.downloads import curl_download, gsutil_getsize from utils.metrics import box_iou, fitness np.set_printoptions(linewidth=320, formatter={'float_kind': '{:11.5g}'.format}) pd.options.display.max_columns = 10 LOGGER = logging.getLogger(LOGGING_NAME) def colorstr(*input): # Colors a string https://en.wikipedia.org/wiki/ANSI_escape_code, i.e. colorstr('blue', 'hello world') *args, string = input if len(input) > 1 else ('blue', 'bold', input[0]) # color arguments, string colors = { 'black': '\033[30m', # basic colors 'red': '\033[31m', 'green': '\033[32m', 'yellow': '\033[33m', 'blue': '\033[34m', 'magenta': '\033[35m', 'cyan': '\033[36m', 'white': '\033[37m', 'bright_black': '\033[90m', # bright colors 'bright_red': '\033[91m', 'bright_green': '\033[92m', 'bright_yellow': '\033[93m', 'bright_blue': '\033[94m', 'bright_magenta': '\033[95m', 'bright_cyan': '\033[96m', 'bright_white': '\033[97m', 'end': '\033[0m', # misc 'bold': '\033[1m', 'underline': '\033[4m'} return ''.join(colors[x] for x in args) + f'{string}' + colors['end'] def gsutil_getsize(url=''): # gs://bucket/file size https://cloud.google.com/storage/docs/gsutil/commands/du output = subprocess.check_output(['gsutil', 'du', url], shell=True, encoding='utf-8') if output: return int(output.split()[0]) return 0 def fitness(x): # Model fitness as a weighted combination of metrics w = [0.0, 0.0, 0.1, 0.9] # weights for [P, R, mAP@0.5, mAP@0.5:0.95] return (x[:, :4] * w).sum(1) def print_mutation(keys, results, hyp, save_dir, bucket, prefix=colorstr('evolve: ')): evolve_csv = save_dir / 'evolve.csv' evolve_yaml = save_dir / 'hyp_evolve.yaml' keys = tuple(keys) + tuple(hyp.keys()) # [results + hyps] keys = tuple(x.strip() for x in keys) vals = results + tuple(hyp.values()) n = len(keys) # Download (optional) if bucket: url = f'gs://{bucket}/evolve.csv' if gsutil_getsize(url) > (evolve_csv.stat().st_size if evolve_csv.exists() else 0): subprocess.run(['gsutil', 'cp', f'{url}', f'{save_dir}']) # download evolve.csv if larger than local # Log to evolve.csv s = '' if evolve_csv.exists() else (('%20s,' * n % keys).rstrip(',') + '\n') # add header with open(evolve_csv, 'a') as f: f.write(s + ('%20.5g,' * n % vals).rstrip(',') + '\n') # Save yaml with open(evolve_yaml, 'w') as f: data = pd.read_csv(evolve_csv, skipinitialspace=True) data = data.rename(columns=lambda x: x.strip()) # strip keys i = np.argmax(fitness(data.values[:, :4])) # generations = len(data) f.write('# YOLOv5 Hyperparameter Evolution Results\n' + f'# Best generation: {i}\n' + f'# Last generation: {generations - 1}\n' + '# ' + ', '.join(f'{x.strip():>20s}' for x in keys[:7]) + '\n' + '# ' + ', '.join(f'{x:>20.5g}' for x in data.values[i, :7]) + '\n\n') yaml.safe_dump(data.loc[i][7:].to_dict(), f, sort_keys=False) # Print to screen LOGGER.info(prefix + f'{generations} generations finished, current result:\n' + prefix + ', '.join(f'{x.strip():>20s}' for x in keys) + '\n' + prefix + ', '.join(f'{x:20.5g}' for x in vals) + '\n\n') if bucket: subprocess.run(['gsutil', 'cp', f'{evolve_csv}', f'{evolve_yaml}', f'gs://{bucket}']) # upload
null
165,926
import contextlib import glob import inspect import logging import logging.config import math import os import platform import random import re import signal import subprocess import sys import time import urllib from copy import deepcopy from datetime import datetime from itertools import repeat from multiprocessing.pool import ThreadPool from pathlib import Path from subprocess import check_output from tarfile import is_tarfile from typing import Optional from zipfile import ZipFile, is_zipfile import cv2 import IPython import numpy as np import pandas as pd import pkg_resources as pkg import torch import torchvision import yaml from utils import TryExcept, emojis from utils.downloads import curl_download, gsutil_getsize from utils.metrics import box_iou, fitness torch.set_printoptions(linewidth=320, precision=5, profile='long') np.set_printoptions(linewidth=320, formatter={'float_kind': '{:11.5g}'.format}) cv2.setNumThreads(0) def xyxy2xywh(x): # Convert nx4 boxes from [x1, y1, x2, y2] to [x, y, w, h] where xy1=top-left, xy2=bottom-right y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x) y[..., 0] = (x[..., 0] + x[..., 2]) / 2 # x center y[..., 1] = (x[..., 1] + x[..., 3]) / 2 # y center y[..., 2] = x[..., 2] - x[..., 0] # width y[..., 3] = x[..., 3] - x[..., 1] # height return y def xywh2xyxy(x): # Convert nx4 boxes from [x, y, w, h] to [x1, y1, x2, y2] where xy1=top-left, xy2=bottom-right y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x) y[..., 0] = x[..., 0] - x[..., 2] / 2 # top left x y[..., 1] = x[..., 1] - x[..., 3] / 2 # top left y y[..., 2] = x[..., 0] + x[..., 2] / 2 # bottom right x y[..., 3] = x[..., 1] + x[..., 3] / 2 # bottom right y return y def scale_boxes(img1_shape, boxes, img0_shape, ratio_pad=None): # Rescale boxes (xyxy) from img1_shape to img0_shape if ratio_pad is None: # calculate from img0_shape gain = min(img1_shape[0] / img0_shape[0], img1_shape[1] / img0_shape[1]) # gain = old / new pad = (img1_shape[1] - img0_shape[1] * gain) / 2, (img1_shape[0] - img0_shape[0] * gain) / 2 # wh padding else: gain = ratio_pad[0][0] pad = ratio_pad[1] boxes[..., [0, 2]] -= pad[0] # x padding boxes[..., [1, 3]] -= pad[1] # y padding boxes[..., :4] /= gain clip_boxes(boxes, img0_shape) return boxes cv2.imread, cv2.imwrite, cv2.imshow = imread, imwrite, imshow def apply_classifier(x, model, img, im0): # Apply a second stage classifier to YOLO outputs # Example model = torchvision.models.__dict__['efficientnet_b0'](pretrained=True).to(device).eval() im0 = [im0] if isinstance(im0, np.ndarray) else im0 for i, d in enumerate(x): # per image if d is not None and len(d): d = d.clone() # Reshape and pad cutouts b = xyxy2xywh(d[:, :4]) # boxes b[:, 2:] = b[:, 2:].max(1)[0].unsqueeze(1) # rectangle to square b[:, 2:] = b[:, 2:] * 1.3 + 30 # pad d[:, :4] = xywh2xyxy(b).long() # Rescale boxes from img_size to im0 size scale_boxes(img.shape[2:], d[:, :4], im0[i].shape) # Classes pred_cls1 = d[:, 5].long() ims = [] for a in d: cutout = im0[i][int(a[1]):int(a[3]), int(a[0]):int(a[2])] im = cv2.resize(cutout, (224, 224)) # BGR im = im[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x416x416 im = np.ascontiguousarray(im, dtype=np.float32) # uint8 to float32 im /= 255 # 0 - 255 to 0.0 - 1.0 ims.append(im) pred_cls2 = model(torch.Tensor(ims).to(d.device)).argmax(1) # classifier prediction x[i] = x[i][pred_cls1 == pred_cls2] # retain matching class detections return x
null
165,927
import math import warnings from pathlib import Path import matplotlib.pyplot as plt import numpy as np import torch from utils import TryExcept, threaded def bbox_iou(box1, box2, xywh=True, GIoU=False, DIoU=False, CIoU=False, eps=1e-7): # Returns Intersection over Union (IoU) of box1(1,4) to box2(n,4) # Get the coordinates of bounding boxes if xywh: # transform from xywh to xyxy (x1, y1, w1, h1), (x2, y2, w2, h2) = box1.chunk(4, -1), box2.chunk(4, -1) w1_, h1_, w2_, h2_ = w1 / 2, h1 / 2, w2 / 2, h2 / 2 b1_x1, b1_x2, b1_y1, b1_y2 = x1 - w1_, x1 + w1_, y1 - h1_, y1 + h1_ b2_x1, b2_x2, b2_y1, b2_y2 = x2 - w2_, x2 + w2_, y2 - h2_, y2 + h2_ else: # x1, y1, x2, y2 = box1 b1_x1, b1_y1, b1_x2, b1_y2 = box1.chunk(4, -1) b2_x1, b2_y1, b2_x2, b2_y2 = box2.chunk(4, -1) w1, h1 = b1_x2 - b1_x1, (b1_y2 - b1_y1).clamp(eps) w2, h2 = b2_x2 - b2_x1, (b2_y2 - b2_y1).clamp(eps) # Intersection area inter = (b1_x2.minimum(b2_x2) - b1_x1.maximum(b2_x1)).clamp(0) * \ (b1_y2.minimum(b2_y2) - b1_y1.maximum(b2_y1)).clamp(0) # Union Area union = w1 * h1 + w2 * h2 - inter + eps # IoU iou = inter / union if CIoU or DIoU or GIoU: cw = b1_x2.maximum(b2_x2) - b1_x1.minimum(b2_x1) # convex (smallest enclosing box) width ch = b1_y2.maximum(b2_y2) - b1_y1.minimum(b2_y1) # convex height if CIoU or DIoU: # Distance or Complete IoU https://arxiv.org/abs/1911.08287v1 c2 = cw ** 2 + ch ** 2 + eps # convex diagonal squared rho2 = ((b2_x1 + b2_x2 - b1_x1 - b1_x2) ** 2 + (b2_y1 + b2_y2 - b1_y1 - b1_y2) ** 2) / 4 # center dist ** 2 if CIoU: # https://github.com/Zzh-tju/DIoU-SSD-pytorch/blob/master/utils/box/box_utils.py#L47 v = (4 / math.pi ** 2) * (torch.atan(w2 / h2) - torch.atan(w1 / h1)).pow(2) with torch.no_grad(): alpha = v / (v - iou + (1 + eps)) return iou - (rho2 / c2 + v * alpha) # CIoU return iou - rho2 / c2 # DIoU c_area = cw * ch + eps # convex area return iou - (c_area - union) / c_area # GIoU https://arxiv.org/pdf/1902.09630.pdf return iou # IoU
null
165,929
iou = bbox_mpdiou(pbox.T, tbox[i], x1y1x2y2=False, mpdiou_hw=pi.size(2) ** 2 + pi.size(3) ** 2, grid=torch.stack([gj, gi])) iou = bbox_mpdiou(pbox.T, selected_tbox, x1y1x2y2=False, mpdiou_hw=pi.size(2) ** 2 + pi.size(3) ** 2, grid=torch.stack([gj, gi])) def bbox_mpdiou(box1, box2, x1y1x2y2=True, mpdiou_hw=None, grid=None, eps=1e-7): # Returns the IoU of box1 to box2. box1 is 4, box2 is nx4 box2 = box2.T box1[:2] += grid box2[:2] += grid # Get the coordinates of bounding boxes if x1y1x2y2: # x1, y1, x2, y2 = box1 b1_x1, b1_y1, b1_x2, b1_y2 = box1[0], box1[1], box1[2], box1[3] b2_x1, b2_y1, b2_x2, b2_y2 = box2[0], box2[1], box2[2], box2[3] else: # transform from xywh to xyxy b1_x1, b1_x2 = box1[0] - box1[2] / 2, box1[0] + box1[2] / 2 b1_y1, b1_y2 = box1[1] - box1[3] / 2, box1[1] + box1[3] / 2 b2_x1, b2_x2 = box2[0] - box2[2] / 2, box2[0] + box2[2] / 2 b2_y1, b2_y2 = box2[1] - box2[3] / 2, box2[1] + box2[3] / 2 # Intersection area inter = (torch.min(b1_x2, b2_x2) - torch.max(b1_x1, b2_x1)).clamp(0) * \ (torch.min(b1_y2, b2_y2) - torch.max(b1_y1, b2_y1)).clamp(0) # Union Area w1, h1 = b1_x2 - b1_x1, b1_y2 - b1_y1 + eps w2, h2 = b2_x2 - b2_x1, b2_y2 - b2_y1 + eps union = w1 * h1 + w2 * h2 - inter + eps iou = inter / union d1 = (b2_x1 - b1_x1) ** 2 + (b2_y1 - b1_y1) ** 2 d2 = (b2_x2 - b1_x2) ** 2 + (b2_y2 - b1_y2) ** 2 return iou - d1 / mpdiou_hw - d2 / mpdiou_hw # MPDIoU
null
165,932
import torch import torch.nn as nn import torch.nn.functional as F import torch.utils.checkpoint as checkpoint import numpy as np from timm.models.layers import DropPath, to_2tuple, trunc_normal_ class SwinTransformer(nn.Module): def __init__(self, pretrain_img_size=224, patch_size=4, in_chans=3, embed_dim=96, depths=[2, 2, 6, 2], num_heads=[3, 6, 12, 24], window_size=7, mlp_ratio=4., qkv_bias=True, qk_scale=None, drop_rate=0., attn_drop_rate=0., drop_path_rate=0.2, norm_layer=nn.LayerNorm, ape=False, patch_norm=True, out_indices=(0, 1, 2, 3), frozen_stages=-1, use_checkpoint=False): def forward(self, x): def update_weight(model_dict, weight_dict): def SwinTransformer_Tiny(weights=''): model = SwinTransformer(depths=[2, 2, 6, 2], num_heads=[3, 6, 12, 24]) if weights: model.load_state_dict(update_weight(model.state_dict(), torch.load(weights)['model'])) return model
null
165,933
import torch, yaml import torch.nn as nn from timm.models.layers import DropPath, to_2tuple, trunc_normal_ from functools import partial from typing import List from torch import Tensor import copy import os import numpy as np class FasterNet(nn.Module): def __init__(self, in_chans=3, num_classes=1000, embed_dim=96, depths=(1, 2, 8, 2), mlp_ratio=2., n_div=4, patch_size=4, patch_stride=4, patch_size2=2, # for subsequent layers patch_stride2=2, patch_norm=True, feature_dim=1280, drop_path_rate=0.1, layer_scale_init_value=0, norm_layer='BN', act_layer='RELU', init_cfg=None, pretrained=None, pconv_fw_type='split_cat', **kwargs): super().__init__() if norm_layer == 'BN': norm_layer = nn.BatchNorm2d else: raise NotImplementedError if act_layer == 'GELU': act_layer = nn.GELU elif act_layer == 'RELU': act_layer = partial(nn.ReLU, inplace=True) else: raise NotImplementedError self.num_stages = len(depths) self.embed_dim = embed_dim self.patch_norm = patch_norm self.num_features = int(embed_dim * 2 ** (self.num_stages - 1)) self.mlp_ratio = mlp_ratio self.depths = depths # split image into non-overlapping patches self.patch_embed = PatchEmbed( patch_size=patch_size, patch_stride=patch_stride, in_chans=in_chans, embed_dim=embed_dim, norm_layer=norm_layer if self.patch_norm else None ) # stochastic depth decay rule dpr = [x.item() for x in torch.linspace(0, drop_path_rate, sum(depths))] # build layers stages_list = [] for i_stage in range(self.num_stages): stage = BasicStage(dim=int(embed_dim * 2 ** i_stage), n_div=n_div, depth=depths[i_stage], mlp_ratio=self.mlp_ratio, drop_path=dpr[sum(depths[:i_stage]):sum(depths[:i_stage + 1])], layer_scale_init_value=layer_scale_init_value, norm_layer=norm_layer, act_layer=act_layer, pconv_fw_type=pconv_fw_type ) stages_list.append(stage) # patch merging layer if i_stage < self.num_stages - 1: stages_list.append( PatchMerging(patch_size2=patch_size2, patch_stride2=patch_stride2, dim=int(embed_dim * 2 ** i_stage), norm_layer=norm_layer) ) self.stages = nn.Sequential(*stages_list) # add a norm layer for each output self.out_indices = [0, 2, 4, 6] for i_emb, i_layer in enumerate(self.out_indices): if i_emb == 0 and os.environ.get('FORK_LAST3', None): raise NotImplementedError else: layer = norm_layer(int(embed_dim * 2 ** i_emb)) layer_name = f'norm{i_layer}' self.add_module(layer_name, layer) self.channel = [i.size(1) for i in self.forward(torch.randn(1, 3, 640, 640))] def forward(self, x: Tensor) -> Tensor: # output the features of four stages for dense prediction x = self.patch_embed(x) outs = [] for idx, stage in enumerate(self.stages): x = stage(x) if idx in self.out_indices: norm_layer = getattr(self, f'norm{idx}') x_out = norm_layer(x) outs.append(x_out) return outs def update_weight(model_dict, weight_dict): idx, temp_dict = 0, {} for k, v in weight_dict.items(): if k in model_dict.keys() and np.shape(model_dict[k]) == np.shape(v): temp_dict[k] = v idx += 1 model_dict.update(temp_dict) print(f'loading weights... {idx}/{len(model_dict)} items') return model_dict def fasternet_t0(weights=None, cfg='models/faster_cfg/fasternet_t0.yaml'): with open(cfg) as f: cfg = yaml.load(f, Loader=yaml.SafeLoader) model = FasterNet(**cfg) if weights is not None: pretrain_weight = torch.load(weights, map_location='cpu') model.load_state_dict(update_weight(model.state_dict(), pretrain_weight)) return model
null
165,934
import torch, yaml import torch.nn as nn from timm.models.layers import DropPath, to_2tuple, trunc_normal_ from functools import partial from typing import List from torch import Tensor import copy import os import numpy as np class FasterNet(nn.Module): def __init__(self, in_chans=3, num_classes=1000, embed_dim=96, depths=(1, 2, 8, 2), mlp_ratio=2., n_div=4, patch_size=4, patch_stride=4, patch_size2=2, # for subsequent layers patch_stride2=2, patch_norm=True, feature_dim=1280, drop_path_rate=0.1, layer_scale_init_value=0, norm_layer='BN', act_layer='RELU', init_cfg=None, pretrained=None, pconv_fw_type='split_cat', **kwargs): def forward(self, x: Tensor) -> Tensor: def update_weight(model_dict, weight_dict): def fasternet_t1(weights=None, cfg='models/faster_cfg/fasternet_t1.yaml'): with open(cfg) as f: cfg = yaml.load(f, Loader=yaml.SafeLoader) model = FasterNet(**cfg) if weights is not None: pretrain_weight = torch.load(weights, map_location='cpu') model.load_state_dict(update_weight(model.state_dict(), pretrain_weight)) return model
null
165,935
import torch, yaml import torch.nn as nn from timm.models.layers import DropPath, to_2tuple, trunc_normal_ from functools import partial from typing import List from torch import Tensor import copy import os import numpy as np class FasterNet(nn.Module): def __init__(self, in_chans=3, num_classes=1000, embed_dim=96, depths=(1, 2, 8, 2), mlp_ratio=2., n_div=4, patch_size=4, patch_stride=4, patch_size2=2, # for subsequent layers patch_stride2=2, patch_norm=True, feature_dim=1280, drop_path_rate=0.1, layer_scale_init_value=0, norm_layer='BN', act_layer='RELU', init_cfg=None, pretrained=None, pconv_fw_type='split_cat', **kwargs): super().__init__() if norm_layer == 'BN': norm_layer = nn.BatchNorm2d else: raise NotImplementedError if act_layer == 'GELU': act_layer = nn.GELU elif act_layer == 'RELU': act_layer = partial(nn.ReLU, inplace=True) else: raise NotImplementedError self.num_stages = len(depths) self.embed_dim = embed_dim self.patch_norm = patch_norm self.num_features = int(embed_dim * 2 ** (self.num_stages - 1)) self.mlp_ratio = mlp_ratio self.depths = depths # split image into non-overlapping patches self.patch_embed = PatchEmbed( patch_size=patch_size, patch_stride=patch_stride, in_chans=in_chans, embed_dim=embed_dim, norm_layer=norm_layer if self.patch_norm else None ) # stochastic depth decay rule dpr = [x.item() for x in torch.linspace(0, drop_path_rate, sum(depths))] # build layers stages_list = [] for i_stage in range(self.num_stages): stage = BasicStage(dim=int(embed_dim * 2 ** i_stage), n_div=n_div, depth=depths[i_stage], mlp_ratio=self.mlp_ratio, drop_path=dpr[sum(depths[:i_stage]):sum(depths[:i_stage + 1])], layer_scale_init_value=layer_scale_init_value, norm_layer=norm_layer, act_layer=act_layer, pconv_fw_type=pconv_fw_type ) stages_list.append(stage) # patch merging layer if i_stage < self.num_stages - 1: stages_list.append( PatchMerging(patch_size2=patch_size2, patch_stride2=patch_stride2, dim=int(embed_dim * 2 ** i_stage), norm_layer=norm_layer) ) self.stages = nn.Sequential(*stages_list) # add a norm layer for each output self.out_indices = [0, 2, 4, 6] for i_emb, i_layer in enumerate(self.out_indices): if i_emb == 0 and os.environ.get('FORK_LAST3', None): raise NotImplementedError else: layer = norm_layer(int(embed_dim * 2 ** i_emb)) layer_name = f'norm{i_layer}' self.add_module(layer_name, layer) self.channel = [i.size(1) for i in self.forward(torch.randn(1, 3, 640, 640))] def forward(self, x: Tensor) -> Tensor: # output the features of four stages for dense prediction x = self.patch_embed(x) outs = [] for idx, stage in enumerate(self.stages): x = stage(x) if idx in self.out_indices: norm_layer = getattr(self, f'norm{idx}') x_out = norm_layer(x) outs.append(x_out) return outs def update_weight(model_dict, weight_dict): idx, temp_dict = 0, {} for k, v in weight_dict.items(): if k in model_dict.keys() and np.shape(model_dict[k]) == np.shape(v): temp_dict[k] = v idx += 1 model_dict.update(temp_dict) print(f'loading weights... {idx}/{len(model_dict)} items') return model_dict def fasternet_t2(weights=None, cfg='models/faster_cfg/fasternet_t2.yaml'): with open(cfg) as f: cfg = yaml.load(f, Loader=yaml.SafeLoader) model = FasterNet(**cfg) if weights is not None: pretrain_weight = torch.load(weights, map_location='cpu') model.load_state_dict(update_weight(model.state_dict(), pretrain_weight)) return model
null
165,936
import torch, yaml import torch.nn as nn from timm.models.layers import DropPath, to_2tuple, trunc_normal_ from functools import partial from typing import List from torch import Tensor import copy import os import numpy as np class FasterNet(nn.Module): def __init__(self, in_chans=3, num_classes=1000, embed_dim=96, depths=(1, 2, 8, 2), mlp_ratio=2., n_div=4, patch_size=4, patch_stride=4, patch_size2=2, # for subsequent layers patch_stride2=2, patch_norm=True, feature_dim=1280, drop_path_rate=0.1, layer_scale_init_value=0, norm_layer='BN', act_layer='RELU', init_cfg=None, pretrained=None, pconv_fw_type='split_cat', **kwargs): super().__init__() if norm_layer == 'BN': norm_layer = nn.BatchNorm2d else: raise NotImplementedError if act_layer == 'GELU': act_layer = nn.GELU elif act_layer == 'RELU': act_layer = partial(nn.ReLU, inplace=True) else: raise NotImplementedError self.num_stages = len(depths) self.embed_dim = embed_dim self.patch_norm = patch_norm self.num_features = int(embed_dim * 2 ** (self.num_stages - 1)) self.mlp_ratio = mlp_ratio self.depths = depths # split image into non-overlapping patches self.patch_embed = PatchEmbed( patch_size=patch_size, patch_stride=patch_stride, in_chans=in_chans, embed_dim=embed_dim, norm_layer=norm_layer if self.patch_norm else None ) # stochastic depth decay rule dpr = [x.item() for x in torch.linspace(0, drop_path_rate, sum(depths))] # build layers stages_list = [] for i_stage in range(self.num_stages): stage = BasicStage(dim=int(embed_dim * 2 ** i_stage), n_div=n_div, depth=depths[i_stage], mlp_ratio=self.mlp_ratio, drop_path=dpr[sum(depths[:i_stage]):sum(depths[:i_stage + 1])], layer_scale_init_value=layer_scale_init_value, norm_layer=norm_layer, act_layer=act_layer, pconv_fw_type=pconv_fw_type ) stages_list.append(stage) # patch merging layer if i_stage < self.num_stages - 1: stages_list.append( PatchMerging(patch_size2=patch_size2, patch_stride2=patch_stride2, dim=int(embed_dim * 2 ** i_stage), norm_layer=norm_layer) ) self.stages = nn.Sequential(*stages_list) # add a norm layer for each output self.out_indices = [0, 2, 4, 6] for i_emb, i_layer in enumerate(self.out_indices): if i_emb == 0 and os.environ.get('FORK_LAST3', None): raise NotImplementedError else: layer = norm_layer(int(embed_dim * 2 ** i_emb)) layer_name = f'norm{i_layer}' self.add_module(layer_name, layer) self.channel = [i.size(1) for i in self.forward(torch.randn(1, 3, 640, 640))] def forward(self, x: Tensor) -> Tensor: # output the features of four stages for dense prediction x = self.patch_embed(x) outs = [] for idx, stage in enumerate(self.stages): x = stage(x) if idx in self.out_indices: norm_layer = getattr(self, f'norm{idx}') x_out = norm_layer(x) outs.append(x_out) return outs def update_weight(model_dict, weight_dict): idx, temp_dict = 0, {} for k, v in weight_dict.items(): if k in model_dict.keys() and np.shape(model_dict[k]) == np.shape(v): temp_dict[k] = v idx += 1 model_dict.update(temp_dict) print(f'loading weights... {idx}/{len(model_dict)} items') return model_dict def fasternet_s(weights=None, cfg='models/faster_cfg/fasternet_s.yaml'): with open(cfg) as f: cfg = yaml.load(f, Loader=yaml.SafeLoader) model = FasterNet(**cfg) if weights is not None: pretrain_weight = torch.load(weights, map_location='cpu') model.load_state_dict(update_weight(model.state_dict(), pretrain_weight)) return model
null
165,937
import torch, yaml import torch.nn as nn from timm.models.layers import DropPath, to_2tuple, trunc_normal_ from functools import partial from typing import List from torch import Tensor import copy import os import numpy as np class FasterNet(nn.Module): def __init__(self, in_chans=3, num_classes=1000, embed_dim=96, depths=(1, 2, 8, 2), mlp_ratio=2., n_div=4, patch_size=4, patch_stride=4, patch_size2=2, # for subsequent layers patch_stride2=2, patch_norm=True, feature_dim=1280, drop_path_rate=0.1, layer_scale_init_value=0, norm_layer='BN', act_layer='RELU', init_cfg=None, pretrained=None, pconv_fw_type='split_cat', **kwargs): super().__init__() if norm_layer == 'BN': norm_layer = nn.BatchNorm2d else: raise NotImplementedError if act_layer == 'GELU': act_layer = nn.GELU elif act_layer == 'RELU': act_layer = partial(nn.ReLU, inplace=True) else: raise NotImplementedError self.num_stages = len(depths) self.embed_dim = embed_dim self.patch_norm = patch_norm self.num_features = int(embed_dim * 2 ** (self.num_stages - 1)) self.mlp_ratio = mlp_ratio self.depths = depths # split image into non-overlapping patches self.patch_embed = PatchEmbed( patch_size=patch_size, patch_stride=patch_stride, in_chans=in_chans, embed_dim=embed_dim, norm_layer=norm_layer if self.patch_norm else None ) # stochastic depth decay rule dpr = [x.item() for x in torch.linspace(0, drop_path_rate, sum(depths))] # build layers stages_list = [] for i_stage in range(self.num_stages): stage = BasicStage(dim=int(embed_dim * 2 ** i_stage), n_div=n_div, depth=depths[i_stage], mlp_ratio=self.mlp_ratio, drop_path=dpr[sum(depths[:i_stage]):sum(depths[:i_stage + 1])], layer_scale_init_value=layer_scale_init_value, norm_layer=norm_layer, act_layer=act_layer, pconv_fw_type=pconv_fw_type ) stages_list.append(stage) # patch merging layer if i_stage < self.num_stages - 1: stages_list.append( PatchMerging(patch_size2=patch_size2, patch_stride2=patch_stride2, dim=int(embed_dim * 2 ** i_stage), norm_layer=norm_layer) ) self.stages = nn.Sequential(*stages_list) # add a norm layer for each output self.out_indices = [0, 2, 4, 6] for i_emb, i_layer in enumerate(self.out_indices): if i_emb == 0 and os.environ.get('FORK_LAST3', None): raise NotImplementedError else: layer = norm_layer(int(embed_dim * 2 ** i_emb)) layer_name = f'norm{i_layer}' self.add_module(layer_name, layer) self.channel = [i.size(1) for i in self.forward(torch.randn(1, 3, 640, 640))] def forward(self, x: Tensor) -> Tensor: # output the features of four stages for dense prediction x = self.patch_embed(x) outs = [] for idx, stage in enumerate(self.stages): x = stage(x) if idx in self.out_indices: norm_layer = getattr(self, f'norm{idx}') x_out = norm_layer(x) outs.append(x_out) return outs def update_weight(model_dict, weight_dict): idx, temp_dict = 0, {} for k, v in weight_dict.items(): if k in model_dict.keys() and np.shape(model_dict[k]) == np.shape(v): temp_dict[k] = v idx += 1 model_dict.update(temp_dict) print(f'loading weights... {idx}/{len(model_dict)} items') return model_dict def fasternet_m(weights=None, cfg='models/faster_cfg/fasternet_m.yaml'): with open(cfg) as f: cfg = yaml.load(f, Loader=yaml.SafeLoader) model = FasterNet(**cfg) if weights is not None: pretrain_weight = torch.load(weights, map_location='cpu') model.load_state_dict(update_weight(model.state_dict(), pretrain_weight)) return model
null
165,938
import torch, yaml import torch.nn as nn from timm.models.layers import DropPath, to_2tuple, trunc_normal_ from functools import partial from typing import List from torch import Tensor import copy import os import numpy as np class FasterNet(nn.Module): def __init__(self, in_chans=3, num_classes=1000, embed_dim=96, depths=(1, 2, 8, 2), mlp_ratio=2., n_div=4, patch_size=4, patch_stride=4, patch_size2=2, # for subsequent layers patch_stride2=2, patch_norm=True, feature_dim=1280, drop_path_rate=0.1, layer_scale_init_value=0, norm_layer='BN', act_layer='RELU', init_cfg=None, pretrained=None, pconv_fw_type='split_cat', **kwargs): def forward(self, x: Tensor) -> Tensor: def update_weight(model_dict, weight_dict): def fasternet_l(weights=None, cfg='models/faster_cfg/fasternet_l.yaml'): with open(cfg) as f: cfg = yaml.load(f, Loader=yaml.SafeLoader) model = FasterNet(**cfg) if weights is not None: pretrain_weight = torch.load(weights, map_location='cpu') model.load_state_dict(update_weight(model.state_dict(), pretrain_weight)) return model
null
165,939
import torch.nn as nn import numpy as np from timm.models.layers import SqueezeExcite import torch def replace_batchnorm(net): for child_name, child in net.named_children(): if hasattr(child, 'fuse_self'): fused = child.fuse_self() setattr(net, child_name, fused) replace_batchnorm(fused) elif isinstance(child, torch.nn.BatchNorm2d): setattr(net, child_name, torch.nn.Identity()) else: replace_batchnorm(child)
null
165,940
import torch.nn as nn import numpy as np from timm.models.layers import SqueezeExcite import torch The provided code snippet includes necessary dependencies for implementing the `_make_divisible` function. Write a Python function `def _make_divisible(v, divisor, min_value=None)` to solve the following problem: This function is taken from the original tf repo. It ensures that all layers have a channel number that is divisible by 8 It can be seen here: https://github.com/tensorflow/models/blob/master/research/slim/nets/mobilenet/mobilenet.py :param v: :param divisor: :param min_value: :return: Here is the function: def _make_divisible(v, divisor, min_value=None): """ This function is taken from the original tf repo. It ensures that all layers have a channel number that is divisible by 8 It can be seen here: https://github.com/tensorflow/models/blob/master/research/slim/nets/mobilenet/mobilenet.py :param v: :param divisor: :param min_value: :return: """ if min_value is None: min_value = divisor new_v = max(min_value, int(v + divisor / 2) // divisor * divisor) # Make sure that round down does not go down by more than 10%. if new_v < 0.9 * v: new_v += divisor return new_v
This function is taken from the original tf repo. It ensures that all layers have a channel number that is divisible by 8 It can be seen here: https://github.com/tensorflow/models/blob/master/research/slim/nets/mobilenet/mobilenet.py :param v: :param divisor: :param min_value: :return:
165,941
import torch.nn as nn import numpy as np from timm.models.layers import SqueezeExcite import torch class RepViT(nn.Module): def __init__(self, cfgs): super(RepViT, self).__init__() # setting of inverted residual blocks self.cfgs = cfgs # building first layer input_channel = self.cfgs[0][2] patch_embed = torch.nn.Sequential(Conv2d_BN(3, input_channel // 2, 3, 2, 1), torch.nn.GELU(), Conv2d_BN(input_channel // 2, input_channel, 3, 2, 1)) layers = [patch_embed] # building inverted residual blocks block = RepViTBlock for k, t, c, use_se, use_hs, s in self.cfgs: output_channel = _make_divisible(c, 8) exp_size = _make_divisible(input_channel * t, 8) layers.append(block(input_channel, exp_size, output_channel, k, s, use_se, use_hs)) input_channel = output_channel self.features = nn.ModuleList(layers) self.channel = [i.size(1) for i in self.forward(torch.randn(1, 3, 640, 640))] def forward(self, x): input_size = x.size(2) scale = [4, 8, 16, 32] features = [None, None, None, None] for f in self.features: x = f(x) if input_size // x.size(2) in scale: features[scale.index(input_size // x.size(2))] = x return features def switch_to_deploy(self): replace_batchnorm(self) def update_weight(model_dict, weight_dict): idx, temp_dict = 0, {} for k, v in weight_dict.items(): # k = k[9:] if k in model_dict.keys() and np.shape(model_dict[k]) == np.shape(v): temp_dict[k] = v idx += 1 model_dict.update(temp_dict) print(f'loading weights... {idx}/{len(model_dict)} items') return model_dict The provided code snippet includes necessary dependencies for implementing the `repvit_m0_9` function. Write a Python function `def repvit_m0_9(weights='')` to solve the following problem: Constructs a MobileNetV3-Large model Here is the function: def repvit_m0_9(weights=''): """ Constructs a MobileNetV3-Large model """ cfgs = [ # k, t, c, SE, HS, s [3, 2, 48, 1, 0, 1], [3, 2, 48, 0, 0, 1], [3, 2, 48, 0, 0, 1], [3, 2, 96, 0, 0, 2], [3, 2, 96, 1, 0, 1], [3, 2, 96, 0, 0, 1], [3, 2, 96, 0, 0, 1], [3, 2, 192, 0, 1, 2], [3, 2, 192, 1, 1, 1], [3, 2, 192, 0, 1, 1], [3, 2, 192, 1, 1, 1], [3, 2, 192, 0, 1, 1], [3, 2, 192, 1, 1, 1], [3, 2, 192, 0, 1, 1], [3, 2, 192, 1, 1, 1], [3, 2, 192, 0, 1, 1], [3, 2, 192, 1, 1, 1], [3, 2, 192, 0, 1, 1], [3, 2, 192, 1, 1, 1], [3, 2, 192, 0, 1, 1], [3, 2, 192, 1, 1, 1], [3, 2, 192, 0, 1, 1], [3, 2, 192, 0, 1, 1], [3, 2, 384, 0, 1, 2], [3, 2, 384, 1, 1, 1], [3, 2, 384, 0, 1, 1] ] model = RepViT(cfgs) if weights: model.load_state_dict(update_weight(model.state_dict(), torch.load(weights)['model'])) return model
Constructs a MobileNetV3-Large model
165,942
import torch.nn as nn import numpy as np from timm.models.layers import SqueezeExcite import torch class RepViT(nn.Module): def __init__(self, cfgs): super(RepViT, self).__init__() # setting of inverted residual blocks self.cfgs = cfgs # building first layer input_channel = self.cfgs[0][2] patch_embed = torch.nn.Sequential(Conv2d_BN(3, input_channel // 2, 3, 2, 1), torch.nn.GELU(), Conv2d_BN(input_channel // 2, input_channel, 3, 2, 1)) layers = [patch_embed] # building inverted residual blocks block = RepViTBlock for k, t, c, use_se, use_hs, s in self.cfgs: output_channel = _make_divisible(c, 8) exp_size = _make_divisible(input_channel * t, 8) layers.append(block(input_channel, exp_size, output_channel, k, s, use_se, use_hs)) input_channel = output_channel self.features = nn.ModuleList(layers) self.channel = [i.size(1) for i in self.forward(torch.randn(1, 3, 640, 640))] def forward(self, x): input_size = x.size(2) scale = [4, 8, 16, 32] features = [None, None, None, None] for f in self.features: x = f(x) if input_size // x.size(2) in scale: features[scale.index(input_size // x.size(2))] = x return features def switch_to_deploy(self): replace_batchnorm(self) def update_weight(model_dict, weight_dict): idx, temp_dict = 0, {} for k, v in weight_dict.items(): # k = k[9:] if k in model_dict.keys() and np.shape(model_dict[k]) == np.shape(v): temp_dict[k] = v idx += 1 model_dict.update(temp_dict) print(f'loading weights... {idx}/{len(model_dict)} items') return model_dict The provided code snippet includes necessary dependencies for implementing the `repvit_m1_0` function. Write a Python function `def repvit_m1_0(weights='')` to solve the following problem: Constructs a MobileNetV3-Large model Here is the function: def repvit_m1_0(weights=''): """ Constructs a MobileNetV3-Large model """ cfgs = [ # k, t, c, SE, HS, s [3, 2, 56, 1, 0, 1], [3, 2, 56, 0, 0, 1], [3, 2, 56, 0, 0, 1], [3, 2, 112, 0, 0, 2], [3, 2, 112, 1, 0, 1], [3, 2, 112, 0, 0, 1], [3, 2, 112, 0, 0, 1], [3, 2, 224, 0, 1, 2], [3, 2, 224, 1, 1, 1], [3, 2, 224, 0, 1, 1], [3, 2, 224, 1, 1, 1], [3, 2, 224, 0, 1, 1], [3, 2, 224, 1, 1, 1], [3, 2, 224, 0, 1, 1], [3, 2, 224, 1, 1, 1], [3, 2, 224, 0, 1, 1], [3, 2, 224, 1, 1, 1], [3, 2, 224, 0, 1, 1], [3, 2, 224, 1, 1, 1], [3, 2, 224, 0, 1, 1], [3, 2, 224, 1, 1, 1], [3, 2, 224, 0, 1, 1], [3, 2, 224, 0, 1, 1], [3, 2, 448, 0, 1, 2], [3, 2, 448, 1, 1, 1], [3, 2, 448, 0, 1, 1] ] model = RepViT(cfgs) if weights: model.load_state_dict(update_weight(model.state_dict(), torch.load(weights)['model'])) return model
Constructs a MobileNetV3-Large model
165,943
import torch.nn as nn import numpy as np from timm.models.layers import SqueezeExcite import torch class RepViT(nn.Module): def __init__(self, cfgs): super(RepViT, self).__init__() # setting of inverted residual blocks self.cfgs = cfgs # building first layer input_channel = self.cfgs[0][2] patch_embed = torch.nn.Sequential(Conv2d_BN(3, input_channel // 2, 3, 2, 1), torch.nn.GELU(), Conv2d_BN(input_channel // 2, input_channel, 3, 2, 1)) layers = [patch_embed] # building inverted residual blocks block = RepViTBlock for k, t, c, use_se, use_hs, s in self.cfgs: output_channel = _make_divisible(c, 8) exp_size = _make_divisible(input_channel * t, 8) layers.append(block(input_channel, exp_size, output_channel, k, s, use_se, use_hs)) input_channel = output_channel self.features = nn.ModuleList(layers) self.channel = [i.size(1) for i in self.forward(torch.randn(1, 3, 640, 640))] def forward(self, x): input_size = x.size(2) scale = [4, 8, 16, 32] features = [None, None, None, None] for f in self.features: x = f(x) if input_size // x.size(2) in scale: features[scale.index(input_size // x.size(2))] = x return features def switch_to_deploy(self): replace_batchnorm(self) def update_weight(model_dict, weight_dict): idx, temp_dict = 0, {} for k, v in weight_dict.items(): # k = k[9:] if k in model_dict.keys() and np.shape(model_dict[k]) == np.shape(v): temp_dict[k] = v idx += 1 model_dict.update(temp_dict) print(f'loading weights... {idx}/{len(model_dict)} items') return model_dict The provided code snippet includes necessary dependencies for implementing the `repvit_m1_1` function. Write a Python function `def repvit_m1_1(weights='')` to solve the following problem: Constructs a MobileNetV3-Large model Here is the function: def repvit_m1_1(weights=''): """ Constructs a MobileNetV3-Large model """ cfgs = [ # k, t, c, SE, HS, s [3, 2, 64, 1, 0, 1], [3, 2, 64, 0, 0, 1], [3, 2, 64, 0, 0, 1], [3, 2, 128, 0, 0, 2], [3, 2, 128, 1, 0, 1], [3, 2, 128, 0, 0, 1], [3, 2, 128, 0, 0, 1], [3, 2, 256, 0, 1, 2], [3, 2, 256, 1, 1, 1], [3, 2, 256, 0, 1, 1], [3, 2, 256, 1, 1, 1], [3, 2, 256, 0, 1, 1], [3, 2, 256, 1, 1, 1], [3, 2, 256, 0, 1, 1], [3, 2, 256, 1, 1, 1], [3, 2, 256, 0, 1, 1], [3, 2, 256, 1, 1, 1], [3, 2, 256, 0, 1, 1], [3, 2, 256, 1, 1, 1], [3, 2, 256, 0, 1, 1], [3, 2, 256, 0, 1, 1], [3, 2, 512, 0, 1, 2], [3, 2, 512, 1, 1, 1], [3, 2, 512, 0, 1, 1] ] model = RepViT(cfgs) if weights: model.load_state_dict(update_weight(model.state_dict(), torch.load(weights)['model'])) return model
Constructs a MobileNetV3-Large model
165,944
import torch.nn as nn import numpy as np from timm.models.layers import SqueezeExcite import torch class RepViT(nn.Module): def __init__(self, cfgs): super(RepViT, self).__init__() # setting of inverted residual blocks self.cfgs = cfgs # building first layer input_channel = self.cfgs[0][2] patch_embed = torch.nn.Sequential(Conv2d_BN(3, input_channel // 2, 3, 2, 1), torch.nn.GELU(), Conv2d_BN(input_channel // 2, input_channel, 3, 2, 1)) layers = [patch_embed] # building inverted residual blocks block = RepViTBlock for k, t, c, use_se, use_hs, s in self.cfgs: output_channel = _make_divisible(c, 8) exp_size = _make_divisible(input_channel * t, 8) layers.append(block(input_channel, exp_size, output_channel, k, s, use_se, use_hs)) input_channel = output_channel self.features = nn.ModuleList(layers) self.channel = [i.size(1) for i in self.forward(torch.randn(1, 3, 640, 640))] def forward(self, x): input_size = x.size(2) scale = [4, 8, 16, 32] features = [None, None, None, None] for f in self.features: x = f(x) if input_size // x.size(2) in scale: features[scale.index(input_size // x.size(2))] = x return features def switch_to_deploy(self): replace_batchnorm(self) def update_weight(model_dict, weight_dict): idx, temp_dict = 0, {} for k, v in weight_dict.items(): # k = k[9:] if k in model_dict.keys() and np.shape(model_dict[k]) == np.shape(v): temp_dict[k] = v idx += 1 model_dict.update(temp_dict) print(f'loading weights... {idx}/{len(model_dict)} items') return model_dict The provided code snippet includes necessary dependencies for implementing the `repvit_m1_5` function. Write a Python function `def repvit_m1_5(weights='')` to solve the following problem: Constructs a MobileNetV3-Large model Here is the function: def repvit_m1_5(weights=''): """ Constructs a MobileNetV3-Large model """ cfgs = [ # k, t, c, SE, HS, s [3, 2, 64, 1, 0, 1], [3, 2, 64, 0, 0, 1], [3, 2, 64, 1, 0, 1], [3, 2, 64, 0, 0, 1], [3, 2, 64, 0, 0, 1], [3, 2, 128, 0, 0, 2], [3, 2, 128, 1, 0, 1], [3, 2, 128, 0, 0, 1], [3, 2, 128, 1, 0, 1], [3, 2, 128, 0, 0, 1], [3, 2, 128, 0, 0, 1], [3, 2, 256, 0, 1, 2], [3, 2, 256, 1, 1, 1], [3, 2, 256, 0, 1, 1], [3, 2, 256, 1, 1, 1], [3, 2, 256, 0, 1, 1], [3, 2, 256, 1, 1, 1], [3, 2, 256, 0, 1, 1], [3, 2, 256, 1, 1, 1], [3, 2, 256, 0, 1, 1], [3, 2, 256, 1, 1, 1], [3, 2, 256, 0, 1, 1], [3, 2, 256, 1, 1, 1], [3, 2, 256, 0, 1, 1], [3, 2, 256, 1, 1, 1], [3, 2, 256, 0, 1, 1], [3, 2, 256, 1, 1, 1], [3, 2, 256, 0, 1, 1], [3, 2, 256, 1, 1, 1], [3, 2, 256, 0, 1, 1], [3, 2, 256, 1, 1, 1], [3, 2, 256, 0, 1, 1], [3, 2, 256, 1, 1, 1], [3, 2, 256, 0, 1, 1], [3, 2, 256, 1, 1, 1], [3, 2, 256, 0, 1, 1], [3, 2, 256, 0, 1, 1], [3, 2, 512, 0, 1, 2], [3, 2, 512, 1, 1, 1], [3, 2, 512, 0, 1, 1], [3, 2, 512, 1, 1, 1], [3, 2, 512, 0, 1, 1] ] model = RepViT(cfgs) if weights: model.load_state_dict(update_weight(model.state_dict(), torch.load(weights)['model'])) return model
Constructs a MobileNetV3-Large model
165,945
import torch.nn as nn import numpy as np from timm.models.layers import SqueezeExcite import torch class RepViT(nn.Module): def __init__(self, cfgs): super(RepViT, self).__init__() # setting of inverted residual blocks self.cfgs = cfgs # building first layer input_channel = self.cfgs[0][2] patch_embed = torch.nn.Sequential(Conv2d_BN(3, input_channel // 2, 3, 2, 1), torch.nn.GELU(), Conv2d_BN(input_channel // 2, input_channel, 3, 2, 1)) layers = [patch_embed] # building inverted residual blocks block = RepViTBlock for k, t, c, use_se, use_hs, s in self.cfgs: output_channel = _make_divisible(c, 8) exp_size = _make_divisible(input_channel * t, 8) layers.append(block(input_channel, exp_size, output_channel, k, s, use_se, use_hs)) input_channel = output_channel self.features = nn.ModuleList(layers) self.channel = [i.size(1) for i in self.forward(torch.randn(1, 3, 640, 640))] def forward(self, x): input_size = x.size(2) scale = [4, 8, 16, 32] features = [None, None, None, None] for f in self.features: x = f(x) if input_size // x.size(2) in scale: features[scale.index(input_size // x.size(2))] = x return features def switch_to_deploy(self): replace_batchnorm(self) def update_weight(model_dict, weight_dict): idx, temp_dict = 0, {} for k, v in weight_dict.items(): # k = k[9:] if k in model_dict.keys() and np.shape(model_dict[k]) == np.shape(v): temp_dict[k] = v idx += 1 model_dict.update(temp_dict) print(f'loading weights... {idx}/{len(model_dict)} items') return model_dict The provided code snippet includes necessary dependencies for implementing the `repvit_m2_3` function. Write a Python function `def repvit_m2_3(weights='')` to solve the following problem: Constructs a MobileNetV3-Large model Here is the function: def repvit_m2_3(weights=''): """ Constructs a MobileNetV3-Large model """ cfgs = [ # k, t, c, SE, HS, s [3, 2, 80, 1, 0, 1], [3, 2, 80, 0, 0, 1], [3, 2, 80, 1, 0, 1], [3, 2, 80, 0, 0, 1], [3, 2, 80, 1, 0, 1], [3, 2, 80, 0, 0, 1], [3, 2, 80, 0, 0, 1], [3, 2, 160, 0, 0, 2], [3, 2, 160, 1, 0, 1], [3, 2, 160, 0, 0, 1], [3, 2, 160, 1, 0, 1], [3, 2, 160, 0, 0, 1], [3, 2, 160, 1, 0, 1], [3, 2, 160, 0, 0, 1], [3, 2, 160, 0, 0, 1], [3, 2, 320, 0, 1, 2], [3, 2, 320, 1, 1, 1], [3, 2, 320, 0, 1, 1], [3, 2, 320, 1, 1, 1], [3, 2, 320, 0, 1, 1], [3, 2, 320, 1, 1, 1], [3, 2, 320, 0, 1, 1], [3, 2, 320, 1, 1, 1], [3, 2, 320, 0, 1, 1], [3, 2, 320, 1, 1, 1], [3, 2, 320, 0, 1, 1], [3, 2, 320, 1, 1, 1], [3, 2, 320, 0, 1, 1], [3, 2, 320, 1, 1, 1], [3, 2, 320, 0, 1, 1], [3, 2, 320, 1, 1, 1], [3, 2, 320, 0, 1, 1], [3, 2, 320, 1, 1, 1], [3, 2, 320, 0, 1, 1], [3, 2, 320, 1, 1, 1], [3, 2, 320, 0, 1, 1], [3, 2, 320, 1, 1, 1], [3, 2, 320, 0, 1, 1], [3, 2, 320, 1, 1, 1], [3, 2, 320, 0, 1, 1], [3, 2, 320, 1, 1, 1], [3, 2, 320, 0, 1, 1], [3, 2, 320, 1, 1, 1], [3, 2, 320, 0, 1, 1], [3, 2, 320, 1, 1, 1], [3, 2, 320, 0, 1, 1], [3, 2, 320, 1, 1, 1], [3, 2, 320, 0, 1, 1], [3, 2, 320, 1, 1, 1], [3, 2, 320, 0, 1, 1], # [3, 2, 320, 1, 1, 1], # [3, 2, 320, 0, 1, 1], [3, 2, 320, 0, 1, 1], [3, 2, 640, 0, 1, 2], [3, 2, 640, 1, 1, 1], [3, 2, 640, 0, 1, 1], # [3, 2, 640, 1, 1, 1], # [3, 2, 640, 0, 1, 1] ] model = RepViT(cfgs) if weights: model.load_state_dict(update_weight(model.state_dict(), torch.load(weights)['model'])) return model
Constructs a MobileNetV3-Large model
165,946
import torch import torch.nn as nn import torch.nn.functional as F import torch.autograd def fuse_conv_bn(conv, bn): # Fuse convolution and batchnorm layers https://tehnokv.com/posts/fusing-batchnorm-and-conv/ fusedconv = ( nn.Conv2d( conv.in_channels, conv.out_channels, kernel_size=conv.kernel_size, stride=conv.stride, padding=conv.padding, groups=conv.groups, bias=True, ) .requires_grad_(False) .to(conv.weight.device) ) # prepare filters w_conv = conv.weight.clone().view(conv.out_channels, -1) w_bn = torch.diag(bn.weight.div(torch.sqrt(bn.eps + bn.running_var))) fusedconv.weight.copy_(torch.mm(w_bn, w_conv).view(fusedconv.weight.shape)) # prepare spatial bias b_conv = ( torch.zeros(conv.weight.size(0), device=conv.weight.device) if conv.bias is None else conv.bias ) b_bn = bn.bias - bn.weight.mul(bn.running_mean).div( torch.sqrt(bn.running_var + bn.eps) ) fusedconv.bias.copy_(torch.mm(w_bn, b_conv.reshape(-1, 1)).reshape(-1) + b_bn) return fusedconv
null
165,947
import torch from torch import nn import numpy as np from models.ODConv.odconv import ODConv2d def fuse_conv_bn(conv, bn): # Fuse convolution and batchnorm layers https://tehnokv.com/posts/fusing-batchnorm-and-conv/ fusedconv = ( nn.Conv2d( conv.in_channels, conv.out_channels, kernel_size=conv.kernel_size, stride=conv.stride, padding=conv.padding, groups=conv.groups, bias=True, ) .requires_grad_(False) .to(conv.weight.device) ) # prepare filters w_conv = conv.weight.clone().view(conv.out_channels, -1) w_bn = torch.diag(bn.weight.div(torch.sqrt(bn.eps + bn.running_var))) fusedconv.weight.copy_(torch.mm(w_bn, w_conv).view(fusedconv.weight.shape)) # prepare spatial bias b_conv = ( torch.zeros(conv.weight.size(0), device=conv.weight.device) if conv.bias is None else conv.bias ) b_bn = bn.bias - bn.weight.mul(bn.running_mean).div( torch.sqrt(bn.running_var + bn.eps) ) fusedconv.bias.copy_(torch.mm(w_bn, b_conv.reshape(-1, 1)).reshape(-1) + b_bn) return fusedconv
null
165,948
import torch from torch import nn import numpy as np from models.ODConv.odconv import ODConv2d The provided code snippet includes necessary dependencies for implementing the `_make_divisible` function. Write a Python function `def _make_divisible(v, divisor, min_value=None)` to solve the following problem: This function is taken from the original tf repo. It ensures that all layers have a channel number that is divisible by 8 It can be seen here: https://github.com/tensorflow/models/blob/master/research/slim/nets/mobilenet/mobilenet.py :param v: :param divisor: :param min_value: :return: Here is the function: def _make_divisible(v, divisor, min_value=None): """ This function is taken from the original tf repo. It ensures that all layers have a channel number that is divisible by 8 It can be seen here: https://github.com/tensorflow/models/blob/master/research/slim/nets/mobilenet/mobilenet.py :param v: :param divisor: :param min_value: :return: """ if min_value is None: min_value = divisor new_v = max(min_value, int(v + divisor / 2) // divisor * divisor) # Make sure that round down does not go down by more than 10%. if new_v < 0.9 * v: new_v += divisor return new_v
This function is taken from the original tf repo. It ensures that all layers have a channel number that is divisible by 8 It can be seen here: https://github.com/tensorflow/models/blob/master/research/slim/nets/mobilenet/mobilenet.py :param v: :param divisor: :param min_value: :return:
165,949
import torch from torch import nn import numpy as np from models.ODConv.odconv import ODConv2d class OD_MobileNetV2(nn.Module): def __init__(self, num_classes=1000, width_mult=1.0, inverted_residual_setting=None, round_nearest=8, block=InvertedResidual, norm_layer=nn.BatchNorm2d, dropout=0.2, reduction=0.0625, kernel_num=1, **kwargs): def net_update_temperature(self, temperature): def forward(self, x): def update_weight(model_dict, weight_dict): def od_mobilenetv2_050(weights=None, kernel_num=1): model = OD_MobileNetV2(width_mult=0.5, kernel_num=kernel_num) if weights is not None: pretrain_weight = torch.load(weights, map_location='cpu')['state_dict'] model.load_state_dict(update_weight(model.state_dict(), pretrain_weight)) return model
null
165,950
import torch from torch import nn import numpy as np from models.ODConv.odconv import ODConv2d class OD_MobileNetV2(nn.Module): def __init__(self, num_classes=1000, width_mult=1.0, inverted_residual_setting=None, round_nearest=8, block=InvertedResidual, norm_layer=nn.BatchNorm2d, dropout=0.2, reduction=0.0625, kernel_num=1, **kwargs): """ MobileNet V2 main class Args: num_classes (int): Number of classes width_mult (float): Width multiplier - adjusts number of channels in each layer by this amount inverted_residual_setting: Network structure round_nearest (int): Round the number of channels in each layer to be a multiple of this number Set to 1 to turn off rounding block: Module specifying inverted residual building block for mobilenet norm_layer: Module specifying the normalization layer to use """ super(OD_MobileNetV2, self).__init__() input_channel = 32 last_channel = 1280 if inverted_residual_setting is None: inverted_residual_setting = [ # t, c, n, s [1, 16, 1, 1], [6, 24, 2, 2], [6, 32, 3, 2], [6, 64, 4, 2], [6, 96, 3, 1], [6, 160, 3, 2], [6, 320, 1, 1], ] # only check the first element, assuming user knows t,c,n,s are required if len(inverted_residual_setting) == 0 or len(inverted_residual_setting[0]) != 4: raise ValueError("inverted_residual_setting should be non-empty " "or a 4-element list, got {}".format(inverted_residual_setting)) # building first layer input_channel = _make_divisible(input_channel * width_mult, round_nearest) self.last_channel = _make_divisible(last_channel * max(1.0, width_mult), round_nearest) features = [ConvBNReLU(3, input_channel, stride=2, norm_layer=norm_layer)] # building inverted residual blocks for t, c, n, s in inverted_residual_setting: output_channel = _make_divisible(c * width_mult, round_nearest) for i in range(n): stride = s if i == 0 else 1 features.append(block(input_channel, output_channel, stride, expand_ratio=t, norm_layer=norm_layer, reduction=reduction, kernel_num=kernel_num)) input_channel = output_channel # building last several layers features.append(ODConvBNReLU(input_channel, self.last_channel, kernel_size=1, norm_layer=norm_layer, reduction=reduction, kernel_num=kernel_num)) # make it nn.Sequential self.features = nn.Sequential(*features) # weight initialization for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.kaiming_normal_(m.weight, mode='fan_out') if m.bias is not None: nn.init.zeros_(m.bias) elif isinstance(m, (nn.BatchNorm2d, nn.GroupNorm)): nn.init.ones_(m.weight) nn.init.zeros_(m.bias) elif isinstance(m, nn.Linear): nn.init.normal_(m.weight, 0, 0.01) nn.init.zeros_(m.bias) self.channel = [i.size(1) for i in self.forward(torch.randn(2, 3, 640, 640))] def net_update_temperature(self, temperature): for m in self.modules(): if hasattr(m, "update_temperature"): m.update_temperature(temperature) def forward(self, x): input_size = x.size(2) scale = [4, 8, 16, 32] features = [None, None, None, None] for idx, layer in enumerate(self.features): x = layer(x) if input_size // x.size(2) in scale: features[scale.index(input_size // x.size(2))] = x return features def update_weight(model_dict, weight_dict): idx, temp_dict = 0, {} for k, v in weight_dict.items(): if k.replace('module.', '') in model_dict.keys() and np.shape(model_dict[k.replace('module.', '')]) == np.shape(v): temp_dict[k.replace('module.', '')] = v idx += 1 model_dict.update(temp_dict) print(f'loading weights... {idx}/{len(model_dict)} items') return model_dict def od_mobilenetv2_075(weights=None, kernel_num=1): model = OD_MobileNetV2(width_mult=0.75, kernel_num=kernel_num) if weights is not None: pretrain_weight = torch.load(weights, map_location='cpu')['state_dict'] model.load_state_dict(update_weight(model.state_dict(), pretrain_weight)) return model
null
165,951
import torch from torch import nn import numpy as np from models.ODConv.odconv import ODConv2d class OD_MobileNetV2(nn.Module): def __init__(self, num_classes=1000, width_mult=1.0, inverted_residual_setting=None, round_nearest=8, block=InvertedResidual, norm_layer=nn.BatchNorm2d, dropout=0.2, reduction=0.0625, kernel_num=1, **kwargs): """ MobileNet V2 main class Args: num_classes (int): Number of classes width_mult (float): Width multiplier - adjusts number of channels in each layer by this amount inverted_residual_setting: Network structure round_nearest (int): Round the number of channels in each layer to be a multiple of this number Set to 1 to turn off rounding block: Module specifying inverted residual building block for mobilenet norm_layer: Module specifying the normalization layer to use """ super(OD_MobileNetV2, self).__init__() input_channel = 32 last_channel = 1280 if inverted_residual_setting is None: inverted_residual_setting = [ # t, c, n, s [1, 16, 1, 1], [6, 24, 2, 2], [6, 32, 3, 2], [6, 64, 4, 2], [6, 96, 3, 1], [6, 160, 3, 2], [6, 320, 1, 1], ] # only check the first element, assuming user knows t,c,n,s are required if len(inverted_residual_setting) == 0 or len(inverted_residual_setting[0]) != 4: raise ValueError("inverted_residual_setting should be non-empty " "or a 4-element list, got {}".format(inverted_residual_setting)) # building first layer input_channel = _make_divisible(input_channel * width_mult, round_nearest) self.last_channel = _make_divisible(last_channel * max(1.0, width_mult), round_nearest) features = [ConvBNReLU(3, input_channel, stride=2, norm_layer=norm_layer)] # building inverted residual blocks for t, c, n, s in inverted_residual_setting: output_channel = _make_divisible(c * width_mult, round_nearest) for i in range(n): stride = s if i == 0 else 1 features.append(block(input_channel, output_channel, stride, expand_ratio=t, norm_layer=norm_layer, reduction=reduction, kernel_num=kernel_num)) input_channel = output_channel # building last several layers features.append(ODConvBNReLU(input_channel, self.last_channel, kernel_size=1, norm_layer=norm_layer, reduction=reduction, kernel_num=kernel_num)) # make it nn.Sequential self.features = nn.Sequential(*features) # weight initialization for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.kaiming_normal_(m.weight, mode='fan_out') if m.bias is not None: nn.init.zeros_(m.bias) elif isinstance(m, (nn.BatchNorm2d, nn.GroupNorm)): nn.init.ones_(m.weight) nn.init.zeros_(m.bias) elif isinstance(m, nn.Linear): nn.init.normal_(m.weight, 0, 0.01) nn.init.zeros_(m.bias) self.channel = [i.size(1) for i in self.forward(torch.randn(2, 3, 640, 640))] def net_update_temperature(self, temperature): for m in self.modules(): if hasattr(m, "update_temperature"): m.update_temperature(temperature) def forward(self, x): input_size = x.size(2) scale = [4, 8, 16, 32] features = [None, None, None, None] for idx, layer in enumerate(self.features): x = layer(x) if input_size // x.size(2) in scale: features[scale.index(input_size // x.size(2))] = x return features def update_weight(model_dict, weight_dict): idx, temp_dict = 0, {} for k, v in weight_dict.items(): if k.replace('module.', '') in model_dict.keys() and np.shape(model_dict[k.replace('module.', '')]) == np.shape(v): temp_dict[k.replace('module.', '')] = v idx += 1 model_dict.update(temp_dict) print(f'loading weights... {idx}/{len(model_dict)} items') return model_dict def od_mobilenetv2_100(weights=None, kernel_num=1): model = OD_MobileNetV2(width_mult=1.0, kernel_num=kernel_num) if weights is not None: pretrain_weight = torch.load(weights, map_location='cpu')['state_dict'] model.load_state_dict(update_weight(model.state_dict(), pretrain_weight)) return model
null
165,952
import torch import torch.nn as nn from models.ODConv.odconv import ODConv2d import numpy as np def odconv3x3(in_planes, out_planes, stride=1, reduction=0.0625, kernel_num=1): return ODConv2d(in_planes, out_planes, kernel_size=3, stride=stride, padding=1, reduction=reduction, kernel_num=kernel_num)
null
165,953
import torch import torch.nn as nn from models.ODConv.odconv import ODConv2d import numpy as np def odconv1x1(in_planes, out_planes, stride=1, reduction=0.0625, kernel_num=1): return ODConv2d(in_planes, out_planes, kernel_size=1, stride=stride, padding=0, reduction=reduction, kernel_num=kernel_num)
null
165,954
import torch import torch.nn as nn from models.ODConv.odconv import ODConv2d import numpy as np class BasicBlock(nn.Module): def __init__(self, inplanes, planes, stride=1, downsample=None, reduction=0.0625, kernel_num=1): def forward(self, x): class OD_ResNet(nn.Module): def __init__(self, block, layers, num_classes=1000, dropout=0.1, reduction=0.0625, kernel_num=1): def net_update_temperature(self, temperature): def _make_layer(self, block, planes, blocks, stride=1, reduction=0.625, kernel_num=1): def forward(self, x): def update_weight(model_dict, weight_dict): def od_resnet18(weights=None, kernel_num=1): model = OD_ResNet(BasicBlock, [2, 2, 2, 2], kernel_num=kernel_num) if weights is not None: pretrain_weight = torch.load(weights, map_location='cpu')['state_dict'] model.load_state_dict(update_weight(model.state_dict(), pretrain_weight)) return model
null
165,955
import torch import torch.nn as nn from models.ODConv.odconv import ODConv2d import numpy as np class BasicBlock(nn.Module): expansion = 1 def __init__(self, inplanes, planes, stride=1, downsample=None, reduction=0.0625, kernel_num=1): super(BasicBlock, self).__init__() self.conv1 = odconv3x3(inplanes, planes, stride, reduction=reduction, kernel_num=kernel_num) self.bn1 = nn.BatchNorm2d(planes) self.relu = nn.ReLU(inplace=True) self.conv2 = odconv3x3(planes, planes, reduction=reduction, kernel_num=kernel_num) self.bn2 = nn.BatchNorm2d(planes) self.downsample = downsample self.stride = stride def forward(self, x): identity = x out = self.conv1(x) out = self.bn1(out) out = self.relu(out) out = self.conv2(out) out = self.bn2(out) if self.downsample is not None: identity = self.downsample(x) out += identity out = self.relu(out) return out class OD_ResNet(nn.Module): def __init__(self, block, layers, num_classes=1000, dropout=0.1, reduction=0.0625, kernel_num=1): super(OD_ResNet, self).__init__() self.inplanes = 64 self.conv1 = nn.Conv2d(3, self.inplanes, kernel_size=7, stride=2, padding=3, bias=False) self.bn1 = nn.BatchNorm2d(self.inplanes) self.relu = nn.ReLU(inplace=True) self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) self.layer1 = self._make_layer(block, 64, layers[0], reduction=reduction, kernel_num=kernel_num) self.layer2 = self._make_layer(block, 128, layers[1], stride=2, reduction=reduction, kernel_num=kernel_num) self.layer3 = self._make_layer(block, 256, layers[2], stride=2, reduction=reduction, kernel_num=kernel_num) self.layer4 = self._make_layer(block, 512, layers[3], stride=2, reduction=reduction, kernel_num=kernel_num) for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') elif isinstance(m, (nn.BatchNorm2d, nn.GroupNorm)): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.Linear): nn.init.normal_(m.weight, 0, 0.01) nn.init.zeros_(m.bias) self.channel = [i.size(1) for i in self.forward(torch.randn(2, 3, 640, 640))] def net_update_temperature(self, temperature): for m in self.modules(): if hasattr(m, "update_temperature"): m.update_temperature(temperature) def _make_layer(self, block, planes, blocks, stride=1, reduction=0.625, kernel_num=1): downsample = None if stride != 1 or self.inplanes != planes * block.expansion: downsample = nn.Sequential( nn.Conv2d(self.inplanes, planes * block.expansion, kernel_size=1, stride=stride, padding=0, bias=False), nn.BatchNorm2d(planes * block.expansion), ) layers = [] layers.append(block(self.inplanes, planes, stride, downsample, reduction=reduction, kernel_num=kernel_num)) self.inplanes = planes * block.expansion for _ in range(1, blocks): layers.append(block(self.inplanes, planes, reduction=reduction, kernel_num=kernel_num)) return nn.Sequential(*layers) def forward(self, x): x = self.conv1(x) x = self.bn1(x) x1 = self.relu(x) x = self.maxpool(x1) x2 = self.layer1(x) x3 = self.layer2(x2) x4 = self.layer3(x3) x5 = self.layer4(x4) return [x1, x2, x3, x4, x5] def update_weight(model_dict, weight_dict): idx, temp_dict = 0, {} for k, v in weight_dict.items(): if k.replace('module.', '') in model_dict.keys() and np.shape(model_dict[k.replace('module.', '')]) == np.shape(v): temp_dict[k.replace('module.', '')] = v idx += 1 model_dict.update(temp_dict) print(f'loading weights... {idx}/{len(model_dict)} items') return model_dict def od_resnet34(weights=None, kernel_num=1): model = OD_ResNet(BasicBlock, [3, 4, 6, 3], kernel_num=kernel_num) if weights is not None: pretrain_weight = torch.load(weights, map_location='cpu')['state_dict'] model.load_state_dict(update_weight(model.state_dict(), pretrain_weight)) return model
null
165,956
import torch import torch.nn as nn from models.ODConv.odconv import ODConv2d import numpy as np class Bottleneck(nn.Module): expansion = 4 def __init__(self, inplanes, planes, stride=1, downsample=None, reduction=0.0625, kernel_num=1): super(Bottleneck, self).__init__() self.conv1 = odconv1x1(inplanes, planes, reduction=reduction, kernel_num=kernel_num) self.bn1 = nn.BatchNorm2d(planes) self.conv2 = odconv3x3(planes, planes, stride, reduction=reduction, kernel_num=kernel_num) self.bn2 = nn.BatchNorm2d(planes) self.conv3 = odconv1x1(planes, planes * self.expansion, reduction=reduction, kernel_num=kernel_num) self.bn3 = nn.BatchNorm2d(planes * self.expansion) self.relu = nn.ReLU(inplace=True) self.downsample = downsample self.stride = stride def forward(self, x): identity = x out = self.conv1(x) out = self.bn1(out) out = self.relu(out) out = self.conv2(out) out = self.bn2(out) out = self.relu(out) out = self.conv3(out) out = self.bn3(out) if self.downsample is not None: identity = self.downsample(x) out += identity out = self.relu(out) return out class OD_ResNet(nn.Module): def __init__(self, block, layers, num_classes=1000, dropout=0.1, reduction=0.0625, kernel_num=1): super(OD_ResNet, self).__init__() self.inplanes = 64 self.conv1 = nn.Conv2d(3, self.inplanes, kernel_size=7, stride=2, padding=3, bias=False) self.bn1 = nn.BatchNorm2d(self.inplanes) self.relu = nn.ReLU(inplace=True) self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) self.layer1 = self._make_layer(block, 64, layers[0], reduction=reduction, kernel_num=kernel_num) self.layer2 = self._make_layer(block, 128, layers[1], stride=2, reduction=reduction, kernel_num=kernel_num) self.layer3 = self._make_layer(block, 256, layers[2], stride=2, reduction=reduction, kernel_num=kernel_num) self.layer4 = self._make_layer(block, 512, layers[3], stride=2, reduction=reduction, kernel_num=kernel_num) for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') elif isinstance(m, (nn.BatchNorm2d, nn.GroupNorm)): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.Linear): nn.init.normal_(m.weight, 0, 0.01) nn.init.zeros_(m.bias) self.channel = [i.size(1) for i in self.forward(torch.randn(2, 3, 640, 640))] def net_update_temperature(self, temperature): for m in self.modules(): if hasattr(m, "update_temperature"): m.update_temperature(temperature) def _make_layer(self, block, planes, blocks, stride=1, reduction=0.625, kernel_num=1): downsample = None if stride != 1 or self.inplanes != planes * block.expansion: downsample = nn.Sequential( nn.Conv2d(self.inplanes, planes * block.expansion, kernel_size=1, stride=stride, padding=0, bias=False), nn.BatchNorm2d(planes * block.expansion), ) layers = [] layers.append(block(self.inplanes, planes, stride, downsample, reduction=reduction, kernel_num=kernel_num)) self.inplanes = planes * block.expansion for _ in range(1, blocks): layers.append(block(self.inplanes, planes, reduction=reduction, kernel_num=kernel_num)) return nn.Sequential(*layers) def forward(self, x): x = self.conv1(x) x = self.bn1(x) x1 = self.relu(x) x = self.maxpool(x1) x2 = self.layer1(x) x3 = self.layer2(x2) x4 = self.layer3(x3) x5 = self.layer4(x4) return [x1, x2, x3, x4, x5] def update_weight(model_dict, weight_dict): idx, temp_dict = 0, {} for k, v in weight_dict.items(): if k.replace('module.', '') in model_dict.keys() and np.shape(model_dict[k.replace('module.', '')]) == np.shape(v): temp_dict[k.replace('module.', '')] = v idx += 1 model_dict.update(temp_dict) print(f'loading weights... {idx}/{len(model_dict)} items') return model_dict def od_resnet50(weights=None, kernel_num=1): model = OD_ResNet(Bottleneck, [3, 4, 6, 3], kernel_num=kernel_num) if weights is not None: pretrain_weight = torch.load(weights, map_location='cpu')['state_dict'] model.load_state_dict(update_weight(model.state_dict(), pretrain_weight)) return model
null
165,957
import torch import torch.nn as nn from models.ODConv.odconv import ODConv2d import numpy as np class Bottleneck(nn.Module): expansion = 4 def __init__(self, inplanes, planes, stride=1, downsample=None, reduction=0.0625, kernel_num=1): super(Bottleneck, self).__init__() self.conv1 = odconv1x1(inplanes, planes, reduction=reduction, kernel_num=kernel_num) self.bn1 = nn.BatchNorm2d(planes) self.conv2 = odconv3x3(planes, planes, stride, reduction=reduction, kernel_num=kernel_num) self.bn2 = nn.BatchNorm2d(planes) self.conv3 = odconv1x1(planes, planes * self.expansion, reduction=reduction, kernel_num=kernel_num) self.bn3 = nn.BatchNorm2d(planes * self.expansion) self.relu = nn.ReLU(inplace=True) self.downsample = downsample self.stride = stride def forward(self, x): identity = x out = self.conv1(x) out = self.bn1(out) out = self.relu(out) out = self.conv2(out) out = self.bn2(out) out = self.relu(out) out = self.conv3(out) out = self.bn3(out) if self.downsample is not None: identity = self.downsample(x) out += identity out = self.relu(out) return out class OD_ResNet(nn.Module): def __init__(self, block, layers, num_classes=1000, dropout=0.1, reduction=0.0625, kernel_num=1): super(OD_ResNet, self).__init__() self.inplanes = 64 self.conv1 = nn.Conv2d(3, self.inplanes, kernel_size=7, stride=2, padding=3, bias=False) self.bn1 = nn.BatchNorm2d(self.inplanes) self.relu = nn.ReLU(inplace=True) self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) self.layer1 = self._make_layer(block, 64, layers[0], reduction=reduction, kernel_num=kernel_num) self.layer2 = self._make_layer(block, 128, layers[1], stride=2, reduction=reduction, kernel_num=kernel_num) self.layer3 = self._make_layer(block, 256, layers[2], stride=2, reduction=reduction, kernel_num=kernel_num) self.layer4 = self._make_layer(block, 512, layers[3], stride=2, reduction=reduction, kernel_num=kernel_num) for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') elif isinstance(m, (nn.BatchNorm2d, nn.GroupNorm)): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.Linear): nn.init.normal_(m.weight, 0, 0.01) nn.init.zeros_(m.bias) self.channel = [i.size(1) for i in self.forward(torch.randn(2, 3, 640, 640))] def net_update_temperature(self, temperature): for m in self.modules(): if hasattr(m, "update_temperature"): m.update_temperature(temperature) def _make_layer(self, block, planes, blocks, stride=1, reduction=0.625, kernel_num=1): downsample = None if stride != 1 or self.inplanes != planes * block.expansion: downsample = nn.Sequential( nn.Conv2d(self.inplanes, planes * block.expansion, kernel_size=1, stride=stride, padding=0, bias=False), nn.BatchNorm2d(planes * block.expansion), ) layers = [] layers.append(block(self.inplanes, planes, stride, downsample, reduction=reduction, kernel_num=kernel_num)) self.inplanes = planes * block.expansion for _ in range(1, blocks): layers.append(block(self.inplanes, planes, reduction=reduction, kernel_num=kernel_num)) return nn.Sequential(*layers) def forward(self, x): x = self.conv1(x) x = self.bn1(x) x1 = self.relu(x) x = self.maxpool(x1) x2 = self.layer1(x) x3 = self.layer2(x2) x4 = self.layer3(x3) x5 = self.layer4(x4) return [x1, x2, x3, x4, x5] def update_weight(model_dict, weight_dict): idx, temp_dict = 0, {} for k, v in weight_dict.items(): if k.replace('module.', '') in model_dict.keys() and np.shape(model_dict[k.replace('module.', '')]) == np.shape(v): temp_dict[k.replace('module.', '')] = v idx += 1 model_dict.update(temp_dict) print(f'loading weights... {idx}/{len(model_dict)} items') return model_dict def od_resnet101(weights=None, kernel_num=1): model = OD_ResNet(Bottleneck, [3, 4, 23, 3], kernel_num=kernel_num) if weights is not None: pretrain_weight = torch.load(weights, map_location='cpu')['state_dict'] model.load_state_dict(update_weight(model.state_dict(), pretrain_weight)) return model
null
165,958
import math import numpy as np import torch.nn as nn from einops import rearrange, reduce from timm.models.layers.activations import * from timm.models.layers import DropPath, trunc_normal_, create_attn from timm.models.efficientnet_blocks import num_groups, SqueezeExcite as SE from functools import partial def get_act(act_layer='relu'): act_dict = { 'none': nn.Identity, 'sigmoid': Sigmoid, 'swish': Swish, 'mish': Mish, 'hsigmoid': HardSigmoid, 'hswish': HardSwish, 'hmish': HardMish, 'tanh': Tanh, 'relu': nn.ReLU, 'relu6': nn.ReLU6, 'prelu': PReLU, 'gelu': GELU, 'silu': nn.SiLU } return act_dict[act_layer]
null
165,959
import math import numpy as np import torch.nn as nn from einops import rearrange, reduce from timm.models.layers.activations import * from timm.models.layers import DropPath, trunc_normal_, create_attn from timm.models.efficientnet_blocks import num_groups, SqueezeExcite as SE from functools import partial class LayerNorm2d(nn.Module): def __init__(self, normalized_shape, eps=1e-6, elementwise_affine=True): super().__init__() self.norm = nn.LayerNorm(normalized_shape, eps, elementwise_affine) def forward(self, x): x = rearrange(x, 'b c h w -> b h w c').contiguous() x = self.norm(x) x = rearrange(x, 'b h w c -> b c h w').contiguous() return x def get_norm(norm_layer='in_1d'): eps = 1e-6 norm_dict = { 'none': nn.Identity, 'in_1d': partial(nn.InstanceNorm1d, eps=eps), 'in_2d': partial(nn.InstanceNorm2d, eps=eps), 'in_3d': partial(nn.InstanceNorm3d, eps=eps), 'bn_1d': partial(nn.BatchNorm1d, eps=eps), 'bn_2d': partial(nn.BatchNorm2d, eps=eps), 'bn_3d': partial(nn.BatchNorm3d, eps=eps), 'gn': partial(nn.GroupNorm, eps=eps), 'ln_1d': partial(nn.LayerNorm, eps=eps), 'ln_2d': partial(LayerNorm2d, eps=eps), } return norm_dict[norm_layer]
null
165,960
import math import numpy as np import torch.nn as nn from einops import rearrange, reduce from timm.models.layers.activations import * from timm.models.layers import DropPath, trunc_normal_, create_attn from timm.models.efficientnet_blocks import num_groups, SqueezeExcite as SE from functools import partial class EMO(nn.Module): def __init__(self, dim_in=3, num_classes=1000, img_size=224, depths=[1, 2, 4, 2], stem_dim=16, embed_dims=[64, 128, 256, 512], exp_ratios=[4., 4., 4., 4.], norm_layers=['bn_2d', 'bn_2d', 'bn_2d', 'bn_2d'], act_layers=['relu', 'relu', 'relu', 'relu'], dw_kss=[3, 3, 5, 5], se_ratios=[0.0, 0.0, 0.0, 0.0], dim_heads=[32, 32, 32, 32], window_sizes=[7, 7, 7, 7], attn_ss=[False, False, True, True], qkv_bias=True, attn_drop=0., drop=0., drop_path=0., v_group=False, attn_pre=False, pre_dim=0): def _init_weights(self, m): def no_weight_decay(self): def no_weight_decay_keywords(self): def no_ft_keywords(self): def ft_head_keywords(self): def get_classifier(self): def reset_classifier(self, num_classes): def check_bn(self): def forward_features(self, x): def forward(self, x): def update_weight(model_dict, weight_dict): def EMO_1M(weights='', **kwargs): model = EMO( # dim_in=3, num_classes=1000, img_size=224, depths=[2, 2, 8, 3], stem_dim=24, embed_dims=[32, 48, 80, 168], exp_ratios=[2., 2.5, 3.0, 3.5], norm_layers=['bn_2d', 'bn_2d', 'ln_2d', 'ln_2d'], act_layers=['silu', 'silu', 'gelu', 'gelu'], dw_kss=[3, 3, 5, 5], dim_heads=[16, 16, 20, 21], window_sizes=[7, 7, 7, 7], attn_ss=[False, False, True, True], qkv_bias=True, attn_drop=0., drop=0., drop_path=0.04036, v_group=False, attn_pre=True, pre_dim=0, **kwargs) if weights: pretrained_weight = torch.load(weights) model.load_state_dict(update_weight(model.state_dict(), pretrained_weight)) return model
null
165,961
import math import numpy as np import torch.nn as nn from einops import rearrange, reduce from timm.models.layers.activations import * from timm.models.layers import DropPath, trunc_normal_, create_attn from timm.models.efficientnet_blocks import num_groups, SqueezeExcite as SE from functools import partial class EMO(nn.Module): def __init__(self, dim_in=3, num_classes=1000, img_size=224, depths=[1, 2, 4, 2], stem_dim=16, embed_dims=[64, 128, 256, 512], exp_ratios=[4., 4., 4., 4.], norm_layers=['bn_2d', 'bn_2d', 'bn_2d', 'bn_2d'], act_layers=['relu', 'relu', 'relu', 'relu'], dw_kss=[3, 3, 5, 5], se_ratios=[0.0, 0.0, 0.0, 0.0], dim_heads=[32, 32, 32, 32], window_sizes=[7, 7, 7, 7], attn_ss=[False, False, True, True], qkv_bias=True, attn_drop=0., drop=0., drop_path=0., v_group=False, attn_pre=False, pre_dim=0): super().__init__() self.num_classes = num_classes assert num_classes > 0 dprs = [x.item() for x in torch.linspace(0, drop_path, sum(depths))] self.stage0 = nn.ModuleList([ MSPatchEmb( # down to 112 dim_in, stem_dim, kernel_size=dw_kss[0], c_group=1, stride=2, dilations=[1], norm_layer=norm_layers[0], act_layer='none'), iRMB( # ds stem_dim, stem_dim, norm_in=False, has_skip=False, exp_ratio=1, norm_layer=norm_layers[0], act_layer=act_layers[0], v_proj=False, dw_ks=dw_kss[0], stride=1, dilation=1, se_ratio=1, dim_head=dim_heads[0], window_size=window_sizes[0], attn_s=False, qkv_bias=qkv_bias, attn_drop=attn_drop, drop=drop, drop_path=0., attn_pre=attn_pre ) ]) emb_dim_pre = stem_dim for i in range(len(depths)): layers = [] dpr = dprs[sum(depths[:i]):sum(depths[:i + 1])] for j in range(depths[i]): if j == 0: stride, has_skip, attn_s, exp_ratio = 2, False, False, exp_ratios[i] * 2 else: stride, has_skip, attn_s, exp_ratio = 1, True, attn_ss[i], exp_ratios[i] layers.append(iRMB( emb_dim_pre, embed_dims[i], norm_in=True, has_skip=has_skip, exp_ratio=exp_ratio, norm_layer=norm_layers[i], act_layer=act_layers[i], v_proj=True, dw_ks=dw_kss[i], stride=stride, dilation=1, se_ratio=se_ratios[i], dim_head=dim_heads[i], window_size=window_sizes[i], attn_s=attn_s, qkv_bias=qkv_bias, attn_drop=attn_drop, drop=drop, drop_path=dpr[j], v_group=v_group, attn_pre=attn_pre )) emb_dim_pre = embed_dims[i] self.__setattr__(f'stage{i + 1}', nn.ModuleList(layers)) self.norm = get_norm(norm_layers[-1])(embed_dims[-1]) self.apply(self._init_weights) self.channel = [i.size(1) for i in self.forward(torch.randn(1, 3, 640, 640))] def _init_weights(self, m): if isinstance(m, nn.Linear): trunc_normal_(m.weight, std=.02) if m.bias is not None: nn.init.zeros_(m.bias) elif isinstance(m, (nn.LayerNorm, nn.GroupNorm, nn.BatchNorm1d, nn.BatchNorm2d, nn.BatchNorm3d, nn.InstanceNorm1d, nn.InstanceNorm2d, nn.InstanceNorm3d)): nn.init.zeros_(m.bias) nn.init.ones_(m.weight) def no_weight_decay(self): return {'token'} def no_weight_decay_keywords(self): return {'alpha', 'gamma', 'beta'} def no_ft_keywords(self): # return {'head.weight', 'head.bias'} return {} def ft_head_keywords(self): return {'head.weight', 'head.bias'}, self.num_classes def get_classifier(self): return self.head def reset_classifier(self, num_classes): self.num_classes = num_classes self.head = nn.Linear(self.pre_dim, num_classes) if num_classes > 0 else nn.Identity() def check_bn(self): for name, m in self.named_modules(): if isinstance(m, nn.modules.batchnorm._NormBase): m.running_mean = torch.nan_to_num(m.running_mean, nan=0, posinf=1, neginf=-1) m.running_var = torch.nan_to_num(m.running_var, nan=0, posinf=1, neginf=-1) def forward_features(self, x): for blk in self.stage0: x = blk(x) x1 = x for blk in self.stage1: x = blk(x) x2 = x for blk in self.stage2: x = blk(x) x3 = x for blk in self.stage3: x = blk(x) x4 = x for blk in self.stage4: x = blk(x) x5 = x return [x1, x2, x3, x4, x5] def forward(self, x): x = self.forward_features(x) x[-1] = self.norm(x[-1]) return x def update_weight(model_dict, weight_dict): idx, temp_dict = 0, {} for k, v in weight_dict.items(): if k in model_dict.keys() and np.shape(model_dict[k]) == np.shape(v): temp_dict[k] = v idx += 1 model_dict.update(temp_dict) print(f'loading weights... {idx}/{len(model_dict)} items') return model_dict def EMO_2M(weights='', **kwargs): model = EMO( # dim_in=3, num_classes=1000, img_size=224, depths=[3, 3, 9, 3], stem_dim=24, embed_dims=[32, 48, 120, 200], exp_ratios=[2., 2.5, 3.0, 3.5], norm_layers=['bn_2d', 'bn_2d', 'ln_2d', 'ln_2d'], act_layers=['silu', 'silu', 'gelu', 'gelu'], dw_kss=[3, 3, 5, 5], dim_heads=[16, 16, 20, 20], window_sizes=[7, 7, 7, 7], attn_ss=[False, False, True, True], qkv_bias=True, attn_drop=0., drop=0., drop_path=0.05, v_group=False, attn_pre=True, pre_dim=0, **kwargs) if weights: pretrained_weight = torch.load(weights) model.load_state_dict(update_weight(model.state_dict(), pretrained_weight)) return model
null
165,962
import math import numpy as np import torch.nn as nn from einops import rearrange, reduce from timm.models.layers.activations import * from timm.models.layers import DropPath, trunc_normal_, create_attn from timm.models.efficientnet_blocks import num_groups, SqueezeExcite as SE from functools import partial class EMO(nn.Module): def __init__(self, dim_in=3, num_classes=1000, img_size=224, depths=[1, 2, 4, 2], stem_dim=16, embed_dims=[64, 128, 256, 512], exp_ratios=[4., 4., 4., 4.], norm_layers=['bn_2d', 'bn_2d', 'bn_2d', 'bn_2d'], act_layers=['relu', 'relu', 'relu', 'relu'], dw_kss=[3, 3, 5, 5], se_ratios=[0.0, 0.0, 0.0, 0.0], dim_heads=[32, 32, 32, 32], window_sizes=[7, 7, 7, 7], attn_ss=[False, False, True, True], qkv_bias=True, attn_drop=0., drop=0., drop_path=0., v_group=False, attn_pre=False, pre_dim=0): super().__init__() self.num_classes = num_classes assert num_classes > 0 dprs = [x.item() for x in torch.linspace(0, drop_path, sum(depths))] self.stage0 = nn.ModuleList([ MSPatchEmb( # down to 112 dim_in, stem_dim, kernel_size=dw_kss[0], c_group=1, stride=2, dilations=[1], norm_layer=norm_layers[0], act_layer='none'), iRMB( # ds stem_dim, stem_dim, norm_in=False, has_skip=False, exp_ratio=1, norm_layer=norm_layers[0], act_layer=act_layers[0], v_proj=False, dw_ks=dw_kss[0], stride=1, dilation=1, se_ratio=1, dim_head=dim_heads[0], window_size=window_sizes[0], attn_s=False, qkv_bias=qkv_bias, attn_drop=attn_drop, drop=drop, drop_path=0., attn_pre=attn_pre ) ]) emb_dim_pre = stem_dim for i in range(len(depths)): layers = [] dpr = dprs[sum(depths[:i]):sum(depths[:i + 1])] for j in range(depths[i]): if j == 0: stride, has_skip, attn_s, exp_ratio = 2, False, False, exp_ratios[i] * 2 else: stride, has_skip, attn_s, exp_ratio = 1, True, attn_ss[i], exp_ratios[i] layers.append(iRMB( emb_dim_pre, embed_dims[i], norm_in=True, has_skip=has_skip, exp_ratio=exp_ratio, norm_layer=norm_layers[i], act_layer=act_layers[i], v_proj=True, dw_ks=dw_kss[i], stride=stride, dilation=1, se_ratio=se_ratios[i], dim_head=dim_heads[i], window_size=window_sizes[i], attn_s=attn_s, qkv_bias=qkv_bias, attn_drop=attn_drop, drop=drop, drop_path=dpr[j], v_group=v_group, attn_pre=attn_pre )) emb_dim_pre = embed_dims[i] self.__setattr__(f'stage{i + 1}', nn.ModuleList(layers)) self.norm = get_norm(norm_layers[-1])(embed_dims[-1]) self.apply(self._init_weights) self.channel = [i.size(1) for i in self.forward(torch.randn(1, 3, 640, 640))] def _init_weights(self, m): if isinstance(m, nn.Linear): trunc_normal_(m.weight, std=.02) if m.bias is not None: nn.init.zeros_(m.bias) elif isinstance(m, (nn.LayerNorm, nn.GroupNorm, nn.BatchNorm1d, nn.BatchNorm2d, nn.BatchNorm3d, nn.InstanceNorm1d, nn.InstanceNorm2d, nn.InstanceNorm3d)): nn.init.zeros_(m.bias) nn.init.ones_(m.weight) def no_weight_decay(self): return {'token'} def no_weight_decay_keywords(self): return {'alpha', 'gamma', 'beta'} def no_ft_keywords(self): # return {'head.weight', 'head.bias'} return {} def ft_head_keywords(self): return {'head.weight', 'head.bias'}, self.num_classes def get_classifier(self): return self.head def reset_classifier(self, num_classes): self.num_classes = num_classes self.head = nn.Linear(self.pre_dim, num_classes) if num_classes > 0 else nn.Identity() def check_bn(self): for name, m in self.named_modules(): if isinstance(m, nn.modules.batchnorm._NormBase): m.running_mean = torch.nan_to_num(m.running_mean, nan=0, posinf=1, neginf=-1) m.running_var = torch.nan_to_num(m.running_var, nan=0, posinf=1, neginf=-1) def forward_features(self, x): for blk in self.stage0: x = blk(x) x1 = x for blk in self.stage1: x = blk(x) x2 = x for blk in self.stage2: x = blk(x) x3 = x for blk in self.stage3: x = blk(x) x4 = x for blk in self.stage4: x = blk(x) x5 = x return [x1, x2, x3, x4, x5] def forward(self, x): x = self.forward_features(x) x[-1] = self.norm(x[-1]) return x def update_weight(model_dict, weight_dict): idx, temp_dict = 0, {} for k, v in weight_dict.items(): if k in model_dict.keys() and np.shape(model_dict[k]) == np.shape(v): temp_dict[k] = v idx += 1 model_dict.update(temp_dict) print(f'loading weights... {idx}/{len(model_dict)} items') return model_dict def EMO_5M(weights='', **kwargs): model = EMO( # dim_in=3, num_classes=1000, img_size=224, depths=[3, 3, 9, 3], stem_dim=24, embed_dims=[48, 72, 160, 288], exp_ratios=[2., 3., 4., 4.], norm_layers=['bn_2d', 'bn_2d', 'ln_2d', 'ln_2d'], act_layers=['silu', 'silu', 'gelu', 'gelu'], dw_kss=[3, 3, 5, 5], dim_heads=[24, 24, 32, 32], window_sizes=[7, 7, 7, 7], attn_ss=[False, False, True, True], qkv_bias=True, attn_drop=0., drop=0., drop_path=0.05, v_group=False, attn_pre=True, pre_dim=0, **kwargs) if weights: pretrained_weight = torch.load(weights) model.load_state_dict(update_weight(model.state_dict(), pretrained_weight)) return model
null
165,963
import math import numpy as np import torch.nn as nn from einops import rearrange, reduce from timm.models.layers.activations import * from timm.models.layers import DropPath, trunc_normal_, create_attn from timm.models.efficientnet_blocks import num_groups, SqueezeExcite as SE from functools import partial class EMO(nn.Module): def __init__(self, dim_in=3, num_classes=1000, img_size=224, depths=[1, 2, 4, 2], stem_dim=16, embed_dims=[64, 128, 256, 512], exp_ratios=[4., 4., 4., 4.], norm_layers=['bn_2d', 'bn_2d', 'bn_2d', 'bn_2d'], act_layers=['relu', 'relu', 'relu', 'relu'], dw_kss=[3, 3, 5, 5], se_ratios=[0.0, 0.0, 0.0, 0.0], dim_heads=[32, 32, 32, 32], window_sizes=[7, 7, 7, 7], attn_ss=[False, False, True, True], qkv_bias=True, attn_drop=0., drop=0., drop_path=0., v_group=False, attn_pre=False, pre_dim=0): def _init_weights(self, m): def no_weight_decay(self): def no_weight_decay_keywords(self): def no_ft_keywords(self): def ft_head_keywords(self): def get_classifier(self): def reset_classifier(self, num_classes): def check_bn(self): def forward_features(self, x): def forward(self, x): def update_weight(model_dict, weight_dict): def EMO_6M(weights='', **kwargs): model = EMO( # dim_in=3, num_classes=1000, img_size=224, depths=[3, 3, 9, 3], stem_dim=24, embed_dims=[48, 72, 160, 320], exp_ratios=[2., 3., 4., 5.], norm_layers=['bn_2d', 'bn_2d', 'ln_2d', 'ln_2d'], act_layers=['silu', 'silu', 'gelu', 'gelu'], dw_kss=[3, 3, 5, 5], dim_heads=[16, 24, 20, 32], window_sizes=[7, 7, 7, 7], attn_ss=[False, False, True, True], qkv_bias=True, attn_drop=0., drop=0., drop_path=0.05, v_group=False, attn_pre=True, pre_dim=0, **kwargs) if weights: pretrained_weight = torch.load(weights) model.load_state_dict(update_weight(model.state_dict(), pretrained_weight)) return model
null
165,964
from typing import Sequence import torch import torch.nn as nn import numpy as np from mmcv.cnn.bricks import DropPath, build_activation_layer, build_norm_layer from mmengine.model import BaseModule class RIFormerBlock(BaseModule): """RIFormer Block. Args: dim (int): Embedding dim. mlp_ratio (float): Mlp expansion ratio. Defaults to 4. norm_cfg (dict): The config dict for norm layers. Defaults to ``dict(type='GN', num_groups=1)``. act_cfg (dict): The config dict for activation between pointwise convolution. Defaults to ``dict(type='GELU')``. drop (float): Dropout rate. Defaults to 0. drop_path (float): Stochastic depth rate. Defaults to 0. layer_scale_init_value (float): Init value for Layer Scale. Defaults to 1e-5. deploy (bool): Whether to switch the model structure to deployment mode. Default: False. """ def __init__(self, dim, mlp_ratio=4., norm_cfg=dict(type='GN', num_groups=1), act_cfg=dict(type='GELU'), drop=0., drop_path=0., layer_scale_init_value=1e-5, deploy=False): super().__init__() if deploy: self.norm_reparam = build_norm_layer(norm_cfg, dim)[1] else: self.norm1 = build_norm_layer(norm_cfg, dim)[1] self.token_mixer = Affine(in_features=dim) self.norm2 = build_norm_layer(norm_cfg, dim)[1] mlp_hidden_dim = int(dim * mlp_ratio) self.mlp = Mlp( in_features=dim, hidden_features=mlp_hidden_dim, act_cfg=act_cfg, drop=drop) # The following two techniques are useful to train deep RIFormers. self.drop_path = DropPath(drop_path) if drop_path > 0. \ else nn.Identity() self.layer_scale_1 = nn.Parameter( layer_scale_init_value * torch.ones((dim)), requires_grad=True) self.layer_scale_2 = nn.Parameter( layer_scale_init_value * torch.ones((dim)), requires_grad=True) self.norm_cfg = norm_cfg self.dim = dim self.deploy = deploy def forward(self, x): if hasattr(self, 'norm_reparam'): x = x + self.drop_path( self.layer_scale_1.unsqueeze(-1).unsqueeze(-1) * self.norm_reparam(x)) x = x + self.drop_path( self.layer_scale_2.unsqueeze(-1).unsqueeze(-1) * self.mlp(self.norm2(x))) else: x = x + self.drop_path( self.layer_scale_1.unsqueeze(-1).unsqueeze(-1) * self.token_mixer(self.norm1(x))) x = x + self.drop_path( self.layer_scale_2.unsqueeze(-1).unsqueeze(-1) * self.mlp(self.norm2(x))) return x def fuse_affine(self, norm, token_mixer): gamma_affn = token_mixer.affine.weight.reshape(-1) gamma_affn = gamma_affn - torch.ones_like(gamma_affn) beta_affn = token_mixer.affine.bias gamma_ln = norm.weight beta_ln = norm.bias return (gamma_ln * gamma_affn), (beta_ln * gamma_affn + beta_affn) def get_equivalent_scale_bias(self): eq_s, eq_b = self.fuse_affine(self.norm1, self.token_mixer) return eq_s, eq_b def switch_to_deploy(self): if self.deploy: return eq_s, eq_b = self.get_equivalent_scale_bias() self.norm_reparam = build_norm_layer(self.norm_cfg, self.dim)[1] self.norm_reparam.weight.data = eq_s self.norm_reparam.bias.data = eq_b self.__delattr__('norm1') if hasattr(self, 'token_mixer'): self.__delattr__('token_mixer') self.deploy = True The provided code snippet includes necessary dependencies for implementing the `basic_blocks` function. Write a Python function `def basic_blocks(dim, index, layers, mlp_ratio=4., norm_cfg=dict(type='GN', num_groups=1), act_cfg=dict(type='GELU'), drop_rate=.0, drop_path_rate=0., layer_scale_init_value=1e-5, deploy=False)` to solve the following problem: generate RIFormer blocks for a stage. Here is the function: def basic_blocks(dim, index, layers, mlp_ratio=4., norm_cfg=dict(type='GN', num_groups=1), act_cfg=dict(type='GELU'), drop_rate=.0, drop_path_rate=0., layer_scale_init_value=1e-5, deploy=False): """generate RIFormer blocks for a stage.""" blocks = [] for block_idx in range(layers[index]): block_dpr = drop_path_rate * (block_idx + sum(layers[:index])) / ( sum(layers) - 1) blocks.append( RIFormerBlock( dim, mlp_ratio=mlp_ratio, norm_cfg=norm_cfg, act_cfg=act_cfg, drop=drop_rate, drop_path=block_dpr, layer_scale_init_value=layer_scale_init_value, deploy=deploy, )) blocks = nn.Sequential(*blocks) return blocks
generate RIFormer blocks for a stage.
165,965
from typing import Sequence import torch import torch.nn as nn import numpy as np from mmcv.cnn.bricks import DropPath, build_activation_layer, build_norm_layer from mmengine.model import BaseModule def update_weight(model_dict, weight_dict): idx, temp_dict = 0, {} for k, v in weight_dict.items(): k = k[9:] if k in model_dict.keys() and np.shape(model_dict[k]) == np.shape(v): temp_dict[k] = v idx += 1 model_dict.update(temp_dict) print(f'loading weights... {idx}/{len(model_dict)} items') return model_dict
null
165,966
from typing import Dict, List, Tuple, Union, Optional, Type, Callable, Any from inspect import signature import torch import torch.nn as nn import torch.nn.functional as F import numpy as np def build_kwargs_from_config(config: Dict, target_func: Callable) -> Dict[str, Any]: valid_keys = list(signature(target_func).parameters) kwargs = {} for key in config: if key in valid_keys: kwargs[key] = config[key] return kwargs REGISTERED_NORM_DICT: Dict[str, Type] = { "bn2d": nn.BatchNorm2d, "ln": nn.LayerNorm, } def build_norm(name="bn2d", num_features=None, **kwargs) -> Optional[nn.Module]: if name == "ln": kwargs["normalized_shape"] = num_features else: kwargs["num_features"] = num_features if name in REGISTERED_NORM_DICT: norm_cls = REGISTERED_NORM_DICT[name] args = build_kwargs_from_config(kwargs, norm_cls) return norm_cls(**args) else: return None
null
165,967
from typing import Dict, List, Tuple, Union, Optional, Type, Callable, Any from inspect import signature import torch import torch.nn as nn import torch.nn.functional as F import numpy as np def build_kwargs_from_config(config: Dict, target_func: Callable) -> Dict[str, Any]: valid_keys = list(signature(target_func).parameters) kwargs = {} for key in config: if key in valid_keys: kwargs[key] = config[key] return kwargs REGISTERED_ACT_DICT: Dict[str, Type] = { "relu": nn.ReLU, "relu6": nn.ReLU6, "hswish": nn.Hardswish, } def build_act(name: str, **kwargs) -> Optional[nn.Module]: if name in REGISTERED_ACT_DICT: act_cls = REGISTERED_ACT_DICT[name] args = build_kwargs_from_config(kwargs, act_cls) return act_cls(**args) else: return None
null
165,968
from typing import Dict, List, Tuple, Union, Optional, Type, Callable, Any from inspect import signature import torch import torch.nn as nn import torch.nn.functional as F import numpy as np def get_same_padding(kernel_size: Union[int, Tuple[int, ...]]) -> Union[int, Tuple[int, ...]]: if isinstance(kernel_size, tuple): return tuple([get_same_padding(ks) for ks in kernel_size]) else: assert kernel_size % 2 > 0, "kernel size should be odd number" return kernel_size // 2
null
165,969
from typing import Dict, List, Tuple, Union, Optional, Type, Callable, Any from inspect import signature import torch import torch.nn as nn import torch.nn.functional as F import numpy as np def list_sum(x: List) -> Any: return x[0] if len(x) == 1 else x[0] + list_sum(x[1:]) def merge_tensor(x: List[torch.Tensor], mode="cat", dim=1) -> torch.Tensor: if mode == "cat": return torch.cat(x, dim=dim) elif mode == "add": return list_sum(x) else: raise NotImplementedError
null
165,970
from typing import Dict, List, Tuple, Union, Optional, Type, Callable, Any from inspect import signature import torch import torch.nn as nn import torch.nn.functional as F import numpy as np def resize( x: torch.Tensor, size: Optional[Any] = None, scale_factor: Optional[List[float]] = None, mode: str = "bicubic", align_corners: Optional[bool] = False, ) -> torch.Tensor: if mode in {"bilinear", "bicubic"}: return F.interpolate( x, size=size, scale_factor=scale_factor, mode=mode, align_corners=align_corners, ) elif mode in {"nearest", "area"}: return F.interpolate(x, size=size, scale_factor=scale_factor, mode=mode) else: raise NotImplementedError(f"resize(mode={mode}) not implemented.")
null
165,971
from typing import Dict, List, Tuple, Union, Optional, Type, Callable, Any from inspect import signature import torch import torch.nn as nn import torch.nn.functional as F import numpy as np def val2list(x: Union[List, Tuple, Any], repeat_time=1) -> List: if isinstance(x, (list, tuple)): return list(x) return [x for _ in range(repeat_time)] def val2tuple(x: Union[List, Tuple, Any], min_len: int = 1, idx_repeat: int = -1) -> Tuple: # convert to list first x = val2list(x) # repeat elements if necessary if len(x) > 0: x[idx_repeat:idx_repeat] = [x[idx_repeat] for _ in range(min_len - len(x))] return tuple(x)
null
165,972
from typing import Dict, List, Tuple, Union, Optional, Type, Callable, Any from inspect import signature import torch import torch.nn as nn import torch.nn.functional as F import numpy as np def build_kwargs_from_config(config: Dict, target_func: Callable) -> Dict[str, Any]: valid_keys = list(signature(target_func).parameters) kwargs = {} for key in config: if key in valid_keys: kwargs[key] = config[key] return kwargs class EfficientViTBackbone(nn.Module): def __init__(self, width_list: List[int], depth_list: List[int], in_channels=3, dim=32, expand_ratio=4, norm="bn2d", act_func="hswish") -> None: super().__init__() self.width_list = [] # input stem self.input_stem = [ ConvLayer( in_channels=3, out_channels=width_list[0], stride=2, norm=norm, act_func=act_func, ) ] for _ in range(depth_list[0]): block = self.build_local_block( in_channels=width_list[0], out_channels=width_list[0], stride=1, expand_ratio=1, norm=norm, act_func=act_func, ) self.input_stem.append(ResidualBlock(block, IdentityLayer())) in_channels = width_list[0] self.input_stem = OpSequential(self.input_stem) self.width_list.append(in_channels) # stages self.stages = [] for w, d in zip(width_list[1:3], depth_list[1:3]): stage = [] for i in range(d): stride = 2 if i == 0 else 1 block = self.build_local_block( in_channels=in_channels, out_channels=w, stride=stride, expand_ratio=expand_ratio, norm=norm, act_func=act_func, ) block = ResidualBlock(block, IdentityLayer() if stride == 1 else None) stage.append(block) in_channels = w self.stages.append(OpSequential(stage)) self.width_list.append(in_channels) for w, d in zip(width_list[3:], depth_list[3:]): stage = [] block = self.build_local_block( in_channels=in_channels, out_channels=w, stride=2, expand_ratio=expand_ratio, norm=norm, act_func=act_func, fewer_norm=True, ) stage.append(ResidualBlock(block, None)) in_channels = w for _ in range(d): stage.append( EfficientViTBlock( in_channels=in_channels, dim=dim, expand_ratio=expand_ratio, norm=norm, act_func=act_func, ) ) self.stages.append(OpSequential(stage)) self.width_list.append(in_channels) self.stages = nn.ModuleList(self.stages) self.channel = [i.size(1) for i in self.forward(torch.randn(1, 3, 224, 224))] def build_local_block(in_channels: int, out_channels: int, stride: int, expand_ratio: float, norm: str, act_func: str, fewer_norm: bool = False) -> nn.Module: if expand_ratio == 1: block = DSConv( in_channels=in_channels, out_channels=out_channels, stride=stride, use_bias=(True, False) if fewer_norm else False, norm=(None, norm) if fewer_norm else norm, act_func=(act_func, None), ) else: block = MBConv( in_channels=in_channels, out_channels=out_channels, stride=stride, expand_ratio=expand_ratio, use_bias=(True, True, False) if fewer_norm else False, norm=(None, None, norm) if fewer_norm else norm, act_func=(act_func, act_func, None), ) return block def forward(self, x: torch.Tensor) -> Dict[str, torch.Tensor]: res = [] x = self.input_stem(x) res.append(x) for stage_id, stage in enumerate(self.stages, 1): x = stage(x) res.append(x) return res def update_weight(model_dict, weight_dict): idx, temp_dict = 0, {} for k, v in weight_dict.items(): k = k[9:] if k in model_dict.keys() and np.shape(model_dict[k]) == np.shape(v): temp_dict[k] = v idx += 1 model_dict.update(temp_dict) print(f'loading weights... {idx}/{len(model_dict)} items') return model_dict def efficientvit_b0(weights='', **kwargs) -> EfficientViTBackbone: backbone = EfficientViTBackbone( width_list=[8, 16, 32, 64, 128], depth_list=[1, 2, 2, 2, 2], dim=16, **build_kwargs_from_config(kwargs, EfficientViTBackbone), ) if weights: backbone.load_state_dict(update_weight(backbone.state_dict(), torch.load(weights)['state_dict'])) return backbone
null
165,973
from typing import Dict, List, Tuple, Union, Optional, Type, Callable, Any from inspect import signature import torch import torch.nn as nn import torch.nn.functional as F import numpy as np def build_kwargs_from_config(config: Dict, target_func: Callable) -> Dict[str, Any]: class EfficientViTBackbone(nn.Module): def __init__(self, width_list: List[int], depth_list: List[int], in_channels=3, dim=32, expand_ratio=4, norm="bn2d", act_func="hswish") -> None: def build_local_block(in_channels: int, out_channels: int, stride: int, expand_ratio: float, norm: str, act_func: str, fewer_norm: bool = False) -> nn.Module: def forward(self, x: torch.Tensor) -> Dict[str, torch.Tensor]: def update_weight(model_dict, weight_dict): def efficientvit_b1(weights='', **kwargs) -> EfficientViTBackbone: backbone = EfficientViTBackbone( width_list=[16, 32, 64, 128, 256], depth_list=[1, 2, 3, 3, 4], dim=16, **build_kwargs_from_config(kwargs, EfficientViTBackbone), ) if weights: backbone.load_state_dict(update_weight(backbone.state_dict(), torch.load(weights)['state_dict'])) return backbone
null
165,974
from typing import Dict, List, Tuple, Union, Optional, Type, Callable, Any from inspect import signature import torch import torch.nn as nn import torch.nn.functional as F import numpy as np def build_kwargs_from_config(config: Dict, target_func: Callable) -> Dict[str, Any]: valid_keys = list(signature(target_func).parameters) kwargs = {} for key in config: if key in valid_keys: kwargs[key] = config[key] return kwargs class EfficientViTBackbone(nn.Module): def __init__(self, width_list: List[int], depth_list: List[int], in_channels=3, dim=32, expand_ratio=4, norm="bn2d", act_func="hswish") -> None: super().__init__() self.width_list = [] # input stem self.input_stem = [ ConvLayer( in_channels=3, out_channels=width_list[0], stride=2, norm=norm, act_func=act_func, ) ] for _ in range(depth_list[0]): block = self.build_local_block( in_channels=width_list[0], out_channels=width_list[0], stride=1, expand_ratio=1, norm=norm, act_func=act_func, ) self.input_stem.append(ResidualBlock(block, IdentityLayer())) in_channels = width_list[0] self.input_stem = OpSequential(self.input_stem) self.width_list.append(in_channels) # stages self.stages = [] for w, d in zip(width_list[1:3], depth_list[1:3]): stage = [] for i in range(d): stride = 2 if i == 0 else 1 block = self.build_local_block( in_channels=in_channels, out_channels=w, stride=stride, expand_ratio=expand_ratio, norm=norm, act_func=act_func, ) block = ResidualBlock(block, IdentityLayer() if stride == 1 else None) stage.append(block) in_channels = w self.stages.append(OpSequential(stage)) self.width_list.append(in_channels) for w, d in zip(width_list[3:], depth_list[3:]): stage = [] block = self.build_local_block( in_channels=in_channels, out_channels=w, stride=2, expand_ratio=expand_ratio, norm=norm, act_func=act_func, fewer_norm=True, ) stage.append(ResidualBlock(block, None)) in_channels = w for _ in range(d): stage.append( EfficientViTBlock( in_channels=in_channels, dim=dim, expand_ratio=expand_ratio, norm=norm, act_func=act_func, ) ) self.stages.append(OpSequential(stage)) self.width_list.append(in_channels) self.stages = nn.ModuleList(self.stages) self.channel = [i.size(1) for i in self.forward(torch.randn(1, 3, 224, 224))] def build_local_block(in_channels: int, out_channels: int, stride: int, expand_ratio: float, norm: str, act_func: str, fewer_norm: bool = False) -> nn.Module: if expand_ratio == 1: block = DSConv( in_channels=in_channels, out_channels=out_channels, stride=stride, use_bias=(True, False) if fewer_norm else False, norm=(None, norm) if fewer_norm else norm, act_func=(act_func, None), ) else: block = MBConv( in_channels=in_channels, out_channels=out_channels, stride=stride, expand_ratio=expand_ratio, use_bias=(True, True, False) if fewer_norm else False, norm=(None, None, norm) if fewer_norm else norm, act_func=(act_func, act_func, None), ) return block def forward(self, x: torch.Tensor) -> Dict[str, torch.Tensor]: res = [] x = self.input_stem(x) res.append(x) for stage_id, stage in enumerate(self.stages, 1): x = stage(x) res.append(x) return res def update_weight(model_dict, weight_dict): idx, temp_dict = 0, {} for k, v in weight_dict.items(): k = k[9:] if k in model_dict.keys() and np.shape(model_dict[k]) == np.shape(v): temp_dict[k] = v idx += 1 model_dict.update(temp_dict) print(f'loading weights... {idx}/{len(model_dict)} items') return model_dict def efficientvit_b2(weights='', **kwargs) -> EfficientViTBackbone: backbone = EfficientViTBackbone( width_list=[24, 48, 96, 192, 384], depth_list=[1, 3, 4, 4, 6], dim=32, **build_kwargs_from_config(kwargs, EfficientViTBackbone), ) if weights: backbone.load_state_dict(update_weight(backbone.state_dict(), torch.load(weights)['state_dict'])) return backbone
null
165,975
from typing import Dict, List, Tuple, Union, Optional, Type, Callable, Any from inspect import signature import torch import torch.nn as nn import torch.nn.functional as F import numpy as np def build_kwargs_from_config(config: Dict, target_func: Callable) -> Dict[str, Any]: valid_keys = list(signature(target_func).parameters) kwargs = {} for key in config: if key in valid_keys: kwargs[key] = config[key] return kwargs class EfficientViTBackbone(nn.Module): def __init__(self, width_list: List[int], depth_list: List[int], in_channels=3, dim=32, expand_ratio=4, norm="bn2d", act_func="hswish") -> None: super().__init__() self.width_list = [] # input stem self.input_stem = [ ConvLayer( in_channels=3, out_channels=width_list[0], stride=2, norm=norm, act_func=act_func, ) ] for _ in range(depth_list[0]): block = self.build_local_block( in_channels=width_list[0], out_channels=width_list[0], stride=1, expand_ratio=1, norm=norm, act_func=act_func, ) self.input_stem.append(ResidualBlock(block, IdentityLayer())) in_channels = width_list[0] self.input_stem = OpSequential(self.input_stem) self.width_list.append(in_channels) # stages self.stages = [] for w, d in zip(width_list[1:3], depth_list[1:3]): stage = [] for i in range(d): stride = 2 if i == 0 else 1 block = self.build_local_block( in_channels=in_channels, out_channels=w, stride=stride, expand_ratio=expand_ratio, norm=norm, act_func=act_func, ) block = ResidualBlock(block, IdentityLayer() if stride == 1 else None) stage.append(block) in_channels = w self.stages.append(OpSequential(stage)) self.width_list.append(in_channels) for w, d in zip(width_list[3:], depth_list[3:]): stage = [] block = self.build_local_block( in_channels=in_channels, out_channels=w, stride=2, expand_ratio=expand_ratio, norm=norm, act_func=act_func, fewer_norm=True, ) stage.append(ResidualBlock(block, None)) in_channels = w for _ in range(d): stage.append( EfficientViTBlock( in_channels=in_channels, dim=dim, expand_ratio=expand_ratio, norm=norm, act_func=act_func, ) ) self.stages.append(OpSequential(stage)) self.width_list.append(in_channels) self.stages = nn.ModuleList(self.stages) self.channel = [i.size(1) for i in self.forward(torch.randn(1, 3, 224, 224))] def build_local_block(in_channels: int, out_channels: int, stride: int, expand_ratio: float, norm: str, act_func: str, fewer_norm: bool = False) -> nn.Module: if expand_ratio == 1: block = DSConv( in_channels=in_channels, out_channels=out_channels, stride=stride, use_bias=(True, False) if fewer_norm else False, norm=(None, norm) if fewer_norm else norm, act_func=(act_func, None), ) else: block = MBConv( in_channels=in_channels, out_channels=out_channels, stride=stride, expand_ratio=expand_ratio, use_bias=(True, True, False) if fewer_norm else False, norm=(None, None, norm) if fewer_norm else norm, act_func=(act_func, act_func, None), ) return block def forward(self, x: torch.Tensor) -> Dict[str, torch.Tensor]: res = [] x = self.input_stem(x) res.append(x) for stage_id, stage in enumerate(self.stages, 1): x = stage(x) res.append(x) return res def update_weight(model_dict, weight_dict): idx, temp_dict = 0, {} for k, v in weight_dict.items(): k = k[9:] if k in model_dict.keys() and np.shape(model_dict[k]) == np.shape(v): temp_dict[k] = v idx += 1 model_dict.update(temp_dict) print(f'loading weights... {idx}/{len(model_dict)} items') return model_dict def efficientvit_b3(weights='', **kwargs) -> EfficientViTBackbone: backbone = EfficientViTBackbone( width_list=[32, 64, 128, 256, 512], depth_list=[1, 4, 6, 6, 9], dim=32, **build_kwargs_from_config(kwargs, EfficientViTBackbone), ) if weights: backbone.load_state_dict(update_weight(backbone.state_dict(), torch.load(weights)['state_dict'])) return backbone
null
165,982
import torch import torch.nn as nn from models.ODConv.odconv import ODConv2d import numpy as np class BasicBlock(nn.Module): expansion = 1 def __init__(self, inplanes, planes, stride=1, downsample=None, reduction=0.0625, kernel_num=1): super(BasicBlock, self).__init__() self.conv1 = odconv3x3(inplanes, planes, stride, reduction=reduction, kernel_num=kernel_num) self.bn1 = nn.BatchNorm2d(planes) self.relu = nn.ReLU(inplace=True) self.conv2 = odconv3x3(planes, planes, reduction=reduction, kernel_num=kernel_num) self.bn2 = nn.BatchNorm2d(planes) self.downsample = downsample self.stride = stride def forward(self, x): identity = x out = self.conv1(x) out = self.bn1(out) out = self.relu(out) out = self.conv2(out) out = self.bn2(out) if self.downsample is not None: identity = self.downsample(x) out += identity out = self.relu(out) return out class OD_ResNet(nn.Module): def __init__(self, block, layers, num_classes=1000, dropout=0.1, reduction=0.0625, kernel_num=1): super(OD_ResNet, self).__init__() self.inplanes = 64 self.conv1 = nn.Conv2d(3, self.inplanes, kernel_size=7, stride=2, padding=3, bias=False) self.bn1 = nn.BatchNorm2d(self.inplanes) self.relu = nn.ReLU(inplace=True) self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) self.layer1 = self._make_layer(block, 64, layers[0], reduction=reduction, kernel_num=kernel_num) self.layer2 = self._make_layer(block, 128, layers[1], stride=2, reduction=reduction, kernel_num=kernel_num) self.layer3 = self._make_layer(block, 256, layers[2], stride=2, reduction=reduction, kernel_num=kernel_num) self.layer4 = self._make_layer(block, 512, layers[3], stride=2, reduction=reduction, kernel_num=kernel_num) for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') elif isinstance(m, (nn.BatchNorm2d, nn.GroupNorm)): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.Linear): nn.init.normal_(m.weight, 0, 0.01) nn.init.zeros_(m.bias) self.channel = [i.size(1) for i in self.forward(torch.randn(2, 3, 640, 640))] def net_update_temperature(self, temperature): for m in self.modules(): if hasattr(m, "update_temperature"): m.update_temperature(temperature) def _make_layer(self, block, planes, blocks, stride=1, reduction=0.625, kernel_num=1): downsample = None if stride != 1 or self.inplanes != planes * block.expansion: downsample = nn.Sequential( nn.Conv2d(self.inplanes, planes * block.expansion, kernel_size=1, stride=stride, padding=0, bias=False), nn.BatchNorm2d(planes * block.expansion), ) layers = [] layers.append(block(self.inplanes, planes, stride, downsample, reduction=reduction, kernel_num=kernel_num)) self.inplanes = planes * block.expansion for _ in range(1, blocks): layers.append(block(self.inplanes, planes, reduction=reduction, kernel_num=kernel_num)) return nn.Sequential(*layers) def forward(self, x): x = self.conv1(x) x = self.bn1(x) x1 = self.relu(x) x = self.maxpool(x1) x2 = self.layer1(x) x3 = self.layer2(x2) x4 = self.layer3(x3) x5 = self.layer4(x4) return [x1, x2, x3, x4, x5] def update_weight(model_dict, weight_dict): idx, temp_dict = 0, {} for k, v in weight_dict.items(): if k.replace('module.', '') in model_dict.keys() and np.shape(model_dict[k.replace('module.', '')]) == np.shape(v): temp_dict[k.replace('module.', '')] = v idx += 1 model_dict.update(temp_dict) print(f'loading weights... {idx}/{len(model_dict)} items') return model_dict def od_resnet18(weights=None, kernel_num=1): model = OD_ResNet(BasicBlock, [2, 2, 2, 2], kernel_num=kernel_num) if weights is not None: pretrain_weight = torch.load(weights, map_location='cpu')['state_dict'] model.load_state_dict(update_weight(model.state_dict(), pretrain_weight)) return model
null
165,985
import torch import torch.nn as nn from models.ODConv.odconv import ODConv2d import numpy as np class Bottleneck(nn.Module): def __init__(self, inplanes, planes, stride=1, downsample=None, reduction=0.0625, kernel_num=1): def forward(self, x): class OD_ResNet(nn.Module): def __init__(self, block, layers, num_classes=1000, dropout=0.1, reduction=0.0625, kernel_num=1): def net_update_temperature(self, temperature): def _make_layer(self, block, planes, blocks, stride=1, reduction=0.625, kernel_num=1): def forward(self, x): def update_weight(model_dict, weight_dict): def od_resnet101(weights=None, kernel_num=1): model = OD_ResNet(Bottleneck, [3, 4, 23, 3], kernel_num=kernel_num) if weights is not None: pretrain_weight = torch.load(weights, map_location='cpu')['state_dict'] model.load_state_dict(update_weight(model.state_dict(), pretrain_weight)) return model
null
165,986
import torch import torch.nn as nn import torch.nn.functional as F from timm.models.layers import weight_init, DropPath import numpy as np class VanillaNet(nn.Module): def __init__(self, in_chans=3, num_classes=1000, dims=[96, 192, 384, 768], drop_rate=0, act_num=3, strides=[2,2,2,1], deploy=False, ada_pool=None, **kwargs): super().__init__() self.deploy = deploy if self.deploy: self.stem = nn.Sequential( nn.Conv2d(in_chans, dims[0], kernel_size=4, stride=4), activation(dims[0], act_num) ) else: self.stem1 = nn.Sequential( nn.Conv2d(in_chans, dims[0], kernel_size=4, stride=4), nn.BatchNorm2d(dims[0], eps=1e-6), ) self.stem2 = nn.Sequential( nn.Conv2d(dims[0], dims[0], kernel_size=1, stride=1), nn.BatchNorm2d(dims[0], eps=1e-6), activation(dims[0], act_num) ) self.act_learn = 1 self.stages = nn.ModuleList() for i in range(len(strides)): if not ada_pool: stage = Block(dim=dims[i], dim_out=dims[i+1], act_num=act_num, stride=strides[i], deploy=deploy) else: stage = Block(dim=dims[i], dim_out=dims[i+1], act_num=act_num, stride=strides[i], deploy=deploy, ada_pool=ada_pool[i]) self.stages.append(stage) self.depth = len(strides) self.apply(self._init_weights) self.channel = [i.size(1) for i in self.forward(torch.randn(1, 3, 640, 640))] def _init_weights(self, m): if isinstance(m, (nn.Conv2d, nn.Linear)): weight_init.trunc_normal_(m.weight, std=.02) nn.init.constant_(m.bias, 0) def change_act(self, m): for i in range(self.depth): self.stages[i].act_learn = m self.act_learn = m def forward(self, x): input_size = x.size(2) scale = [4, 8, 16, 32] features = [None, None, None, None] if self.deploy: x = self.stem(x) else: x = self.stem1(x) x = torch.nn.functional.leaky_relu(x,self.act_learn) x = self.stem2(x) if input_size // x.size(2) in scale: features[scale.index(input_size // x.size(2))] = x for i in range(self.depth): x = self.stages[i](x) if input_size // x.size(2) in scale: features[scale.index(input_size // x.size(2))] = x return features def _fuse_bn_tensor(self, conv, bn): kernel = conv.weight bias = conv.bias running_mean = bn.running_mean running_var = bn.running_var gamma = bn.weight beta = bn.bias eps = bn.eps std = (running_var + eps).sqrt() t = (gamma / std).reshape(-1, 1, 1, 1) return kernel * t, beta + (bias - running_mean) * gamma / std def switch_to_deploy(self): if not self.deploy: self.stem2[2].switch_to_deploy() kernel, bias = self._fuse_bn_tensor(self.stem1[0], self.stem1[1]) self.stem1[0].weight.data = kernel self.stem1[0].bias.data = bias kernel, bias = self._fuse_bn_tensor(self.stem2[0], self.stem2[1]) self.stem1[0].weight.data = torch.einsum('oi,icjk->ocjk', kernel.squeeze(3).squeeze(2), self.stem1[0].weight.data) self.stem1[0].bias.data = bias + (self.stem1[0].bias.data.view(1,-1,1,1)*kernel).sum(3).sum(2).sum(1) self.stem = torch.nn.Sequential(*[self.stem1[0], self.stem2[2]]) self.__delattr__('stem1') self.__delattr__('stem2') for i in range(self.depth): self.stages[i].switch_to_deploy() self.deploy = True def update_weight(model_dict, weight_dict): idx, temp_dict = 0, {} for k, v in weight_dict.items(): if k in model_dict.keys() and np.shape(model_dict[k]) == np.shape(v): temp_dict[k] = v idx += 1 model_dict.update(temp_dict) print(f'loading weights... {idx}/{len(model_dict)} items') return model_dict def vanillanet_5(pretrained='',in_22k=False, **kwargs): model = VanillaNet(dims=[128*4, 256*4, 512*4, 1024*4], strides=[2,2,2], **kwargs) if pretrained: weights = torch.load(pretrained)['model_ema'] model.load_state_dict(update_weight(model.state_dict(), weights)) return model
null
165,987
import torch import torch.nn as nn import torch.nn.functional as F from timm.models.layers import weight_init, DropPath import numpy as np class VanillaNet(nn.Module): def __init__(self, in_chans=3, num_classes=1000, dims=[96, 192, 384, 768], drop_rate=0, act_num=3, strides=[2,2,2,1], deploy=False, ada_pool=None, **kwargs): super().__init__() self.deploy = deploy if self.deploy: self.stem = nn.Sequential( nn.Conv2d(in_chans, dims[0], kernel_size=4, stride=4), activation(dims[0], act_num) ) else: self.stem1 = nn.Sequential( nn.Conv2d(in_chans, dims[0], kernel_size=4, stride=4), nn.BatchNorm2d(dims[0], eps=1e-6), ) self.stem2 = nn.Sequential( nn.Conv2d(dims[0], dims[0], kernel_size=1, stride=1), nn.BatchNorm2d(dims[0], eps=1e-6), activation(dims[0], act_num) ) self.act_learn = 1 self.stages = nn.ModuleList() for i in range(len(strides)): if not ada_pool: stage = Block(dim=dims[i], dim_out=dims[i+1], act_num=act_num, stride=strides[i], deploy=deploy) else: stage = Block(dim=dims[i], dim_out=dims[i+1], act_num=act_num, stride=strides[i], deploy=deploy, ada_pool=ada_pool[i]) self.stages.append(stage) self.depth = len(strides) self.apply(self._init_weights) self.channel = [i.size(1) for i in self.forward(torch.randn(1, 3, 640, 640))] def _init_weights(self, m): if isinstance(m, (nn.Conv2d, nn.Linear)): weight_init.trunc_normal_(m.weight, std=.02) nn.init.constant_(m.bias, 0) def change_act(self, m): for i in range(self.depth): self.stages[i].act_learn = m self.act_learn = m def forward(self, x): input_size = x.size(2) scale = [4, 8, 16, 32] features = [None, None, None, None] if self.deploy: x = self.stem(x) else: x = self.stem1(x) x = torch.nn.functional.leaky_relu(x,self.act_learn) x = self.stem2(x) if input_size // x.size(2) in scale: features[scale.index(input_size // x.size(2))] = x for i in range(self.depth): x = self.stages[i](x) if input_size // x.size(2) in scale: features[scale.index(input_size // x.size(2))] = x return features def _fuse_bn_tensor(self, conv, bn): kernel = conv.weight bias = conv.bias running_mean = bn.running_mean running_var = bn.running_var gamma = bn.weight beta = bn.bias eps = bn.eps std = (running_var + eps).sqrt() t = (gamma / std).reshape(-1, 1, 1, 1) return kernel * t, beta + (bias - running_mean) * gamma / std def switch_to_deploy(self): if not self.deploy: self.stem2[2].switch_to_deploy() kernel, bias = self._fuse_bn_tensor(self.stem1[0], self.stem1[1]) self.stem1[0].weight.data = kernel self.stem1[0].bias.data = bias kernel, bias = self._fuse_bn_tensor(self.stem2[0], self.stem2[1]) self.stem1[0].weight.data = torch.einsum('oi,icjk->ocjk', kernel.squeeze(3).squeeze(2), self.stem1[0].weight.data) self.stem1[0].bias.data = bias + (self.stem1[0].bias.data.view(1,-1,1,1)*kernel).sum(3).sum(2).sum(1) self.stem = torch.nn.Sequential(*[self.stem1[0], self.stem2[2]]) self.__delattr__('stem1') self.__delattr__('stem2') for i in range(self.depth): self.stages[i].switch_to_deploy() self.deploy = True def update_weight(model_dict, weight_dict): idx, temp_dict = 0, {} for k, v in weight_dict.items(): if k in model_dict.keys() and np.shape(model_dict[k]) == np.shape(v): temp_dict[k] = v idx += 1 model_dict.update(temp_dict) print(f'loading weights... {idx}/{len(model_dict)} items') return model_dict def vanillanet_6(pretrained='',in_22k=False, **kwargs): model = VanillaNet(dims=[128*4, 256*4, 512*4, 1024*4, 1024*4], strides=[2,2,2,1], **kwargs) if pretrained: weights = torch.load(pretrained)['model_ema'] model.load_state_dict(update_weight(model.state_dict(), weights)) return model
null
165,988
import torch import torch.nn as nn import torch.nn.functional as F from timm.models.layers import weight_init, DropPath import numpy as np class VanillaNet(nn.Module): def __init__(self, in_chans=3, num_classes=1000, dims=[96, 192, 384, 768], drop_rate=0, act_num=3, strides=[2,2,2,1], deploy=False, ada_pool=None, **kwargs): super().__init__() self.deploy = deploy if self.deploy: self.stem = nn.Sequential( nn.Conv2d(in_chans, dims[0], kernel_size=4, stride=4), activation(dims[0], act_num) ) else: self.stem1 = nn.Sequential( nn.Conv2d(in_chans, dims[0], kernel_size=4, stride=4), nn.BatchNorm2d(dims[0], eps=1e-6), ) self.stem2 = nn.Sequential( nn.Conv2d(dims[0], dims[0], kernel_size=1, stride=1), nn.BatchNorm2d(dims[0], eps=1e-6), activation(dims[0], act_num) ) self.act_learn = 1 self.stages = nn.ModuleList() for i in range(len(strides)): if not ada_pool: stage = Block(dim=dims[i], dim_out=dims[i+1], act_num=act_num, stride=strides[i], deploy=deploy) else: stage = Block(dim=dims[i], dim_out=dims[i+1], act_num=act_num, stride=strides[i], deploy=deploy, ada_pool=ada_pool[i]) self.stages.append(stage) self.depth = len(strides) self.apply(self._init_weights) self.channel = [i.size(1) for i in self.forward(torch.randn(1, 3, 640, 640))] def _init_weights(self, m): if isinstance(m, (nn.Conv2d, nn.Linear)): weight_init.trunc_normal_(m.weight, std=.02) nn.init.constant_(m.bias, 0) def change_act(self, m): for i in range(self.depth): self.stages[i].act_learn = m self.act_learn = m def forward(self, x): input_size = x.size(2) scale = [4, 8, 16, 32] features = [None, None, None, None] if self.deploy: x = self.stem(x) else: x = self.stem1(x) x = torch.nn.functional.leaky_relu(x,self.act_learn) x = self.stem2(x) if input_size // x.size(2) in scale: features[scale.index(input_size // x.size(2))] = x for i in range(self.depth): x = self.stages[i](x) if input_size // x.size(2) in scale: features[scale.index(input_size // x.size(2))] = x return features def _fuse_bn_tensor(self, conv, bn): kernel = conv.weight bias = conv.bias running_mean = bn.running_mean running_var = bn.running_var gamma = bn.weight beta = bn.bias eps = bn.eps std = (running_var + eps).sqrt() t = (gamma / std).reshape(-1, 1, 1, 1) return kernel * t, beta + (bias - running_mean) * gamma / std def switch_to_deploy(self): if not self.deploy: self.stem2[2].switch_to_deploy() kernel, bias = self._fuse_bn_tensor(self.stem1[0], self.stem1[1]) self.stem1[0].weight.data = kernel self.stem1[0].bias.data = bias kernel, bias = self._fuse_bn_tensor(self.stem2[0], self.stem2[1]) self.stem1[0].weight.data = torch.einsum('oi,icjk->ocjk', kernel.squeeze(3).squeeze(2), self.stem1[0].weight.data) self.stem1[0].bias.data = bias + (self.stem1[0].bias.data.view(1,-1,1,1)*kernel).sum(3).sum(2).sum(1) self.stem = torch.nn.Sequential(*[self.stem1[0], self.stem2[2]]) self.__delattr__('stem1') self.__delattr__('stem2') for i in range(self.depth): self.stages[i].switch_to_deploy() self.deploy = True def update_weight(model_dict, weight_dict): idx, temp_dict = 0, {} for k, v in weight_dict.items(): if k in model_dict.keys() and np.shape(model_dict[k]) == np.shape(v): temp_dict[k] = v idx += 1 model_dict.update(temp_dict) print(f'loading weights... {idx}/{len(model_dict)} items') return model_dict def vanillanet_7(pretrained='',in_22k=False, **kwargs): model = VanillaNet(dims=[128*4, 128*4, 256*4, 512*4, 1024*4, 1024*4], strides=[1,2,2,2,1], **kwargs) if pretrained: weights = torch.load(pretrained)['model_ema'] model.load_state_dict(update_weight(model.state_dict(), weights)) return model
null
165,989
import torch import torch.nn as nn import torch.nn.functional as F from timm.models.layers import weight_init, DropPath import numpy as np class VanillaNet(nn.Module): def __init__(self, in_chans=3, num_classes=1000, dims=[96, 192, 384, 768], drop_rate=0, act_num=3, strides=[2,2,2,1], deploy=False, ada_pool=None, **kwargs): super().__init__() self.deploy = deploy if self.deploy: self.stem = nn.Sequential( nn.Conv2d(in_chans, dims[0], kernel_size=4, stride=4), activation(dims[0], act_num) ) else: self.stem1 = nn.Sequential( nn.Conv2d(in_chans, dims[0], kernel_size=4, stride=4), nn.BatchNorm2d(dims[0], eps=1e-6), ) self.stem2 = nn.Sequential( nn.Conv2d(dims[0], dims[0], kernel_size=1, stride=1), nn.BatchNorm2d(dims[0], eps=1e-6), activation(dims[0], act_num) ) self.act_learn = 1 self.stages = nn.ModuleList() for i in range(len(strides)): if not ada_pool: stage = Block(dim=dims[i], dim_out=dims[i+1], act_num=act_num, stride=strides[i], deploy=deploy) else: stage = Block(dim=dims[i], dim_out=dims[i+1], act_num=act_num, stride=strides[i], deploy=deploy, ada_pool=ada_pool[i]) self.stages.append(stage) self.depth = len(strides) self.apply(self._init_weights) self.channel = [i.size(1) for i in self.forward(torch.randn(1, 3, 640, 640))] def _init_weights(self, m): if isinstance(m, (nn.Conv2d, nn.Linear)): weight_init.trunc_normal_(m.weight, std=.02) nn.init.constant_(m.bias, 0) def change_act(self, m): for i in range(self.depth): self.stages[i].act_learn = m self.act_learn = m def forward(self, x): input_size = x.size(2) scale = [4, 8, 16, 32] features = [None, None, None, None] if self.deploy: x = self.stem(x) else: x = self.stem1(x) x = torch.nn.functional.leaky_relu(x,self.act_learn) x = self.stem2(x) if input_size // x.size(2) in scale: features[scale.index(input_size // x.size(2))] = x for i in range(self.depth): x = self.stages[i](x) if input_size // x.size(2) in scale: features[scale.index(input_size // x.size(2))] = x return features def _fuse_bn_tensor(self, conv, bn): kernel = conv.weight bias = conv.bias running_mean = bn.running_mean running_var = bn.running_var gamma = bn.weight beta = bn.bias eps = bn.eps std = (running_var + eps).sqrt() t = (gamma / std).reshape(-1, 1, 1, 1) return kernel * t, beta + (bias - running_mean) * gamma / std def switch_to_deploy(self): if not self.deploy: self.stem2[2].switch_to_deploy() kernel, bias = self._fuse_bn_tensor(self.stem1[0], self.stem1[1]) self.stem1[0].weight.data = kernel self.stem1[0].bias.data = bias kernel, bias = self._fuse_bn_tensor(self.stem2[0], self.stem2[1]) self.stem1[0].weight.data = torch.einsum('oi,icjk->ocjk', kernel.squeeze(3).squeeze(2), self.stem1[0].weight.data) self.stem1[0].bias.data = bias + (self.stem1[0].bias.data.view(1,-1,1,1)*kernel).sum(3).sum(2).sum(1) self.stem = torch.nn.Sequential(*[self.stem1[0], self.stem2[2]]) self.__delattr__('stem1') self.__delattr__('stem2') for i in range(self.depth): self.stages[i].switch_to_deploy() self.deploy = True def update_weight(model_dict, weight_dict): idx, temp_dict = 0, {} for k, v in weight_dict.items(): if k in model_dict.keys() and np.shape(model_dict[k]) == np.shape(v): temp_dict[k] = v idx += 1 model_dict.update(temp_dict) print(f'loading weights... {idx}/{len(model_dict)} items') return model_dict def vanillanet_8(pretrained='', in_22k=False, **kwargs): model = VanillaNet(dims=[128*4, 128*4, 256*4, 512*4, 512*4, 1024*4, 1024*4], strides=[1,2,2,1,2,1], **kwargs) if pretrained: weights = torch.load(pretrained)['model_ema'] model.load_state_dict(update_weight(model.state_dict(), weights)) return model
null
165,990
import torch import torch.nn as nn import torch.nn.functional as F from timm.models.layers import weight_init, DropPath import numpy as np class VanillaNet(nn.Module): def __init__(self, in_chans=3, num_classes=1000, dims=[96, 192, 384, 768], drop_rate=0, act_num=3, strides=[2,2,2,1], deploy=False, ada_pool=None, **kwargs): super().__init__() self.deploy = deploy if self.deploy: self.stem = nn.Sequential( nn.Conv2d(in_chans, dims[0], kernel_size=4, stride=4), activation(dims[0], act_num) ) else: self.stem1 = nn.Sequential( nn.Conv2d(in_chans, dims[0], kernel_size=4, stride=4), nn.BatchNorm2d(dims[0], eps=1e-6), ) self.stem2 = nn.Sequential( nn.Conv2d(dims[0], dims[0], kernel_size=1, stride=1), nn.BatchNorm2d(dims[0], eps=1e-6), activation(dims[0], act_num) ) self.act_learn = 1 self.stages = nn.ModuleList() for i in range(len(strides)): if not ada_pool: stage = Block(dim=dims[i], dim_out=dims[i+1], act_num=act_num, stride=strides[i], deploy=deploy) else: stage = Block(dim=dims[i], dim_out=dims[i+1], act_num=act_num, stride=strides[i], deploy=deploy, ada_pool=ada_pool[i]) self.stages.append(stage) self.depth = len(strides) self.apply(self._init_weights) self.channel = [i.size(1) for i in self.forward(torch.randn(1, 3, 640, 640))] def _init_weights(self, m): if isinstance(m, (nn.Conv2d, nn.Linear)): weight_init.trunc_normal_(m.weight, std=.02) nn.init.constant_(m.bias, 0) def change_act(self, m): for i in range(self.depth): self.stages[i].act_learn = m self.act_learn = m def forward(self, x): input_size = x.size(2) scale = [4, 8, 16, 32] features = [None, None, None, None] if self.deploy: x = self.stem(x) else: x = self.stem1(x) x = torch.nn.functional.leaky_relu(x,self.act_learn) x = self.stem2(x) if input_size // x.size(2) in scale: features[scale.index(input_size // x.size(2))] = x for i in range(self.depth): x = self.stages[i](x) if input_size // x.size(2) in scale: features[scale.index(input_size // x.size(2))] = x return features def _fuse_bn_tensor(self, conv, bn): kernel = conv.weight bias = conv.bias running_mean = bn.running_mean running_var = bn.running_var gamma = bn.weight beta = bn.bias eps = bn.eps std = (running_var + eps).sqrt() t = (gamma / std).reshape(-1, 1, 1, 1) return kernel * t, beta + (bias - running_mean) * gamma / std def switch_to_deploy(self): if not self.deploy: self.stem2[2].switch_to_deploy() kernel, bias = self._fuse_bn_tensor(self.stem1[0], self.stem1[1]) self.stem1[0].weight.data = kernel self.stem1[0].bias.data = bias kernel, bias = self._fuse_bn_tensor(self.stem2[0], self.stem2[1]) self.stem1[0].weight.data = torch.einsum('oi,icjk->ocjk', kernel.squeeze(3).squeeze(2), self.stem1[0].weight.data) self.stem1[0].bias.data = bias + (self.stem1[0].bias.data.view(1,-1,1,1)*kernel).sum(3).sum(2).sum(1) self.stem = torch.nn.Sequential(*[self.stem1[0], self.stem2[2]]) self.__delattr__('stem1') self.__delattr__('stem2') for i in range(self.depth): self.stages[i].switch_to_deploy() self.deploy = True def update_weight(model_dict, weight_dict): idx, temp_dict = 0, {} for k, v in weight_dict.items(): if k in model_dict.keys() and np.shape(model_dict[k]) == np.shape(v): temp_dict[k] = v idx += 1 model_dict.update(temp_dict) print(f'loading weights... {idx}/{len(model_dict)} items') return model_dict def vanillanet_9(pretrained='', in_22k=False, **kwargs): model = VanillaNet(dims=[128*4, 128*4, 256*4, 512*4, 512*4, 512*4, 1024*4, 1024*4], strides=[1,2,2,1,1,2,1], **kwargs) if pretrained: weights = torch.load(pretrained)['model_ema'] model.load_state_dict(update_weight(model.state_dict(), weights)) return model
null
165,991
import torch import torch.nn as nn import torch.nn.functional as F from timm.models.layers import weight_init, DropPath import numpy as np class VanillaNet(nn.Module): def __init__(self, in_chans=3, num_classes=1000, dims=[96, 192, 384, 768], drop_rate=0, act_num=3, strides=[2,2,2,1], deploy=False, ada_pool=None, **kwargs): def _init_weights(self, m): def change_act(self, m): def forward(self, x): def _fuse_bn_tensor(self, conv, bn): def switch_to_deploy(self): def update_weight(model_dict, weight_dict): def vanillanet_10(pretrained='', in_22k=False, **kwargs): model = VanillaNet( dims=[128*4, 128*4, 256*4, 512*4, 512*4, 512*4, 512*4, 1024*4, 1024*4], strides=[1,2,2,1,1,1,2,1], **kwargs) if pretrained: weights = torch.load(pretrained)['model_ema'] model.load_state_dict(update_weight(model.state_dict(), weights)) return model
null
165,992
import torch import torch.nn as nn import torch.nn.functional as F from timm.models.layers import weight_init, DropPath import numpy as np class VanillaNet(nn.Module): def __init__(self, in_chans=3, num_classes=1000, dims=[96, 192, 384, 768], drop_rate=0, act_num=3, strides=[2,2,2,1], deploy=False, ada_pool=None, **kwargs): super().__init__() self.deploy = deploy if self.deploy: self.stem = nn.Sequential( nn.Conv2d(in_chans, dims[0], kernel_size=4, stride=4), activation(dims[0], act_num) ) else: self.stem1 = nn.Sequential( nn.Conv2d(in_chans, dims[0], kernel_size=4, stride=4), nn.BatchNorm2d(dims[0], eps=1e-6), ) self.stem2 = nn.Sequential( nn.Conv2d(dims[0], dims[0], kernel_size=1, stride=1), nn.BatchNorm2d(dims[0], eps=1e-6), activation(dims[0], act_num) ) self.act_learn = 1 self.stages = nn.ModuleList() for i in range(len(strides)): if not ada_pool: stage = Block(dim=dims[i], dim_out=dims[i+1], act_num=act_num, stride=strides[i], deploy=deploy) else: stage = Block(dim=dims[i], dim_out=dims[i+1], act_num=act_num, stride=strides[i], deploy=deploy, ada_pool=ada_pool[i]) self.stages.append(stage) self.depth = len(strides) self.apply(self._init_weights) self.channel = [i.size(1) for i in self.forward(torch.randn(1, 3, 640, 640))] def _init_weights(self, m): if isinstance(m, (nn.Conv2d, nn.Linear)): weight_init.trunc_normal_(m.weight, std=.02) nn.init.constant_(m.bias, 0) def change_act(self, m): for i in range(self.depth): self.stages[i].act_learn = m self.act_learn = m def forward(self, x): input_size = x.size(2) scale = [4, 8, 16, 32] features = [None, None, None, None] if self.deploy: x = self.stem(x) else: x = self.stem1(x) x = torch.nn.functional.leaky_relu(x,self.act_learn) x = self.stem2(x) if input_size // x.size(2) in scale: features[scale.index(input_size // x.size(2))] = x for i in range(self.depth): x = self.stages[i](x) if input_size // x.size(2) in scale: features[scale.index(input_size // x.size(2))] = x return features def _fuse_bn_tensor(self, conv, bn): kernel = conv.weight bias = conv.bias running_mean = bn.running_mean running_var = bn.running_var gamma = bn.weight beta = bn.bias eps = bn.eps std = (running_var + eps).sqrt() t = (gamma / std).reshape(-1, 1, 1, 1) return kernel * t, beta + (bias - running_mean) * gamma / std def switch_to_deploy(self): if not self.deploy: self.stem2[2].switch_to_deploy() kernel, bias = self._fuse_bn_tensor(self.stem1[0], self.stem1[1]) self.stem1[0].weight.data = kernel self.stem1[0].bias.data = bias kernel, bias = self._fuse_bn_tensor(self.stem2[0], self.stem2[1]) self.stem1[0].weight.data = torch.einsum('oi,icjk->ocjk', kernel.squeeze(3).squeeze(2), self.stem1[0].weight.data) self.stem1[0].bias.data = bias + (self.stem1[0].bias.data.view(1,-1,1,1)*kernel).sum(3).sum(2).sum(1) self.stem = torch.nn.Sequential(*[self.stem1[0], self.stem2[2]]) self.__delattr__('stem1') self.__delattr__('stem2') for i in range(self.depth): self.stages[i].switch_to_deploy() self.deploy = True def update_weight(model_dict, weight_dict): idx, temp_dict = 0, {} for k, v in weight_dict.items(): if k in model_dict.keys() and np.shape(model_dict[k]) == np.shape(v): temp_dict[k] = v idx += 1 model_dict.update(temp_dict) print(f'loading weights... {idx}/{len(model_dict)} items') return model_dict def vanillanet_11(pretrained='', in_22k=False, **kwargs): model = VanillaNet( dims=[128*4, 128*4, 256*4, 512*4, 512*4, 512*4, 512*4, 512*4, 1024*4, 1024*4], strides=[1,2,2,1,1,1,1,2,1], **kwargs) if pretrained: weights = torch.load(pretrained)['model_ema'] model.load_state_dict(update_weight(model.state_dict(), weights)) return model
null
165,993
import torch import torch.nn as nn import torch.nn.functional as F from timm.models.layers import weight_init, DropPath import numpy as np class VanillaNet(nn.Module): def __init__(self, in_chans=3, num_classes=1000, dims=[96, 192, 384, 768], drop_rate=0, act_num=3, strides=[2,2,2,1], deploy=False, ada_pool=None, **kwargs): super().__init__() self.deploy = deploy if self.deploy: self.stem = nn.Sequential( nn.Conv2d(in_chans, dims[0], kernel_size=4, stride=4), activation(dims[0], act_num) ) else: self.stem1 = nn.Sequential( nn.Conv2d(in_chans, dims[0], kernel_size=4, stride=4), nn.BatchNorm2d(dims[0], eps=1e-6), ) self.stem2 = nn.Sequential( nn.Conv2d(dims[0], dims[0], kernel_size=1, stride=1), nn.BatchNorm2d(dims[0], eps=1e-6), activation(dims[0], act_num) ) self.act_learn = 1 self.stages = nn.ModuleList() for i in range(len(strides)): if not ada_pool: stage = Block(dim=dims[i], dim_out=dims[i+1], act_num=act_num, stride=strides[i], deploy=deploy) else: stage = Block(dim=dims[i], dim_out=dims[i+1], act_num=act_num, stride=strides[i], deploy=deploy, ada_pool=ada_pool[i]) self.stages.append(stage) self.depth = len(strides) self.apply(self._init_weights) self.channel = [i.size(1) for i in self.forward(torch.randn(1, 3, 640, 640))] def _init_weights(self, m): if isinstance(m, (nn.Conv2d, nn.Linear)): weight_init.trunc_normal_(m.weight, std=.02) nn.init.constant_(m.bias, 0) def change_act(self, m): for i in range(self.depth): self.stages[i].act_learn = m self.act_learn = m def forward(self, x): input_size = x.size(2) scale = [4, 8, 16, 32] features = [None, None, None, None] if self.deploy: x = self.stem(x) else: x = self.stem1(x) x = torch.nn.functional.leaky_relu(x,self.act_learn) x = self.stem2(x) if input_size // x.size(2) in scale: features[scale.index(input_size // x.size(2))] = x for i in range(self.depth): x = self.stages[i](x) if input_size // x.size(2) in scale: features[scale.index(input_size // x.size(2))] = x return features def _fuse_bn_tensor(self, conv, bn): kernel = conv.weight bias = conv.bias running_mean = bn.running_mean running_var = bn.running_var gamma = bn.weight beta = bn.bias eps = bn.eps std = (running_var + eps).sqrt() t = (gamma / std).reshape(-1, 1, 1, 1) return kernel * t, beta + (bias - running_mean) * gamma / std def switch_to_deploy(self): if not self.deploy: self.stem2[2].switch_to_deploy() kernel, bias = self._fuse_bn_tensor(self.stem1[0], self.stem1[1]) self.stem1[0].weight.data = kernel self.stem1[0].bias.data = bias kernel, bias = self._fuse_bn_tensor(self.stem2[0], self.stem2[1]) self.stem1[0].weight.data = torch.einsum('oi,icjk->ocjk', kernel.squeeze(3).squeeze(2), self.stem1[0].weight.data) self.stem1[0].bias.data = bias + (self.stem1[0].bias.data.view(1,-1,1,1)*kernel).sum(3).sum(2).sum(1) self.stem = torch.nn.Sequential(*[self.stem1[0], self.stem2[2]]) self.__delattr__('stem1') self.__delattr__('stem2') for i in range(self.depth): self.stages[i].switch_to_deploy() self.deploy = True def update_weight(model_dict, weight_dict): idx, temp_dict = 0, {} for k, v in weight_dict.items(): if k in model_dict.keys() and np.shape(model_dict[k]) == np.shape(v): temp_dict[k] = v idx += 1 model_dict.update(temp_dict) print(f'loading weights... {idx}/{len(model_dict)} items') return model_dict def vanillanet_12(pretrained='', in_22k=False, **kwargs): model = VanillaNet( dims=[128*4, 128*4, 256*4, 512*4, 512*4, 512*4, 512*4, 512*4, 512*4, 1024*4, 1024*4], strides=[1,2,2,1,1,1,1,1,2,1], **kwargs) if pretrained: weights = torch.load(pretrained)['model_ema'] model.load_state_dict(update_weight(model.state_dict(), weights)) return model
null
165,994
import torch import torch.nn as nn import torch.nn.functional as F from timm.models.layers import weight_init, DropPath import numpy as np class VanillaNet(nn.Module): def __init__(self, in_chans=3, num_classes=1000, dims=[96, 192, 384, 768], drop_rate=0, act_num=3, strides=[2,2,2,1], deploy=False, ada_pool=None, **kwargs): def _init_weights(self, m): def change_act(self, m): def forward(self, x): def _fuse_bn_tensor(self, conv, bn): def switch_to_deploy(self): def update_weight(model_dict, weight_dict): def vanillanet_13(pretrained='', in_22k=False, **kwargs): model = VanillaNet( dims=[128*4, 128*4, 256*4, 512*4, 512*4, 512*4, 512*4, 512*4, 512*4, 512*4, 1024*4, 1024*4], strides=[1,2,2,1,1,1,1,1,1,2,1], **kwargs) if pretrained: weights = torch.load(pretrained)['model_ema'] model.load_state_dict(update_weight(model.state_dict(), weights)) return model
null
165,995
import torch import torch.nn as nn import torch.nn.functional as F from timm.models.layers import weight_init, DropPath import numpy as np class VanillaNet(nn.Module): def __init__(self, in_chans=3, num_classes=1000, dims=[96, 192, 384, 768], drop_rate=0, act_num=3, strides=[2,2,2,1], deploy=False, ada_pool=None, **kwargs): def _init_weights(self, m): def change_act(self, m): def forward(self, x): def _fuse_bn_tensor(self, conv, bn): def switch_to_deploy(self): def update_weight(model_dict, weight_dict): def vanillanet_13_x1_5(pretrained='', in_22k=False, **kwargs): model = VanillaNet( dims=[128*6, 128*6, 256*6, 512*6, 512*6, 512*6, 512*6, 512*6, 512*6, 512*6, 1024*6, 1024*6], strides=[1,2,2,1,1,1,1,1,1,2,1], **kwargs) if pretrained: weights = torch.load(pretrained)['model_ema'] model.load_state_dict(update_weight(model.state_dict(), weights)) return model
null
165,996
import torch import torch.nn as nn import torch.nn.functional as F from timm.models.layers import weight_init, DropPath import numpy as np class VanillaNet(nn.Module): def __init__(self, in_chans=3, num_classes=1000, dims=[96, 192, 384, 768], drop_rate=0, act_num=3, strides=[2,2,2,1], deploy=False, ada_pool=None, **kwargs): super().__init__() self.deploy = deploy if self.deploy: self.stem = nn.Sequential( nn.Conv2d(in_chans, dims[0], kernel_size=4, stride=4), activation(dims[0], act_num) ) else: self.stem1 = nn.Sequential( nn.Conv2d(in_chans, dims[0], kernel_size=4, stride=4), nn.BatchNorm2d(dims[0], eps=1e-6), ) self.stem2 = nn.Sequential( nn.Conv2d(dims[0], dims[0], kernel_size=1, stride=1), nn.BatchNorm2d(dims[0], eps=1e-6), activation(dims[0], act_num) ) self.act_learn = 1 self.stages = nn.ModuleList() for i in range(len(strides)): if not ada_pool: stage = Block(dim=dims[i], dim_out=dims[i+1], act_num=act_num, stride=strides[i], deploy=deploy) else: stage = Block(dim=dims[i], dim_out=dims[i+1], act_num=act_num, stride=strides[i], deploy=deploy, ada_pool=ada_pool[i]) self.stages.append(stage) self.depth = len(strides) self.apply(self._init_weights) self.channel = [i.size(1) for i in self.forward(torch.randn(1, 3, 640, 640))] def _init_weights(self, m): if isinstance(m, (nn.Conv2d, nn.Linear)): weight_init.trunc_normal_(m.weight, std=.02) nn.init.constant_(m.bias, 0) def change_act(self, m): for i in range(self.depth): self.stages[i].act_learn = m self.act_learn = m def forward(self, x): input_size = x.size(2) scale = [4, 8, 16, 32] features = [None, None, None, None] if self.deploy: x = self.stem(x) else: x = self.stem1(x) x = torch.nn.functional.leaky_relu(x,self.act_learn) x = self.stem2(x) if input_size // x.size(2) in scale: features[scale.index(input_size // x.size(2))] = x for i in range(self.depth): x = self.stages[i](x) if input_size // x.size(2) in scale: features[scale.index(input_size // x.size(2))] = x return features def _fuse_bn_tensor(self, conv, bn): kernel = conv.weight bias = conv.bias running_mean = bn.running_mean running_var = bn.running_var gamma = bn.weight beta = bn.bias eps = bn.eps std = (running_var + eps).sqrt() t = (gamma / std).reshape(-1, 1, 1, 1) return kernel * t, beta + (bias - running_mean) * gamma / std def switch_to_deploy(self): if not self.deploy: self.stem2[2].switch_to_deploy() kernel, bias = self._fuse_bn_tensor(self.stem1[0], self.stem1[1]) self.stem1[0].weight.data = kernel self.stem1[0].bias.data = bias kernel, bias = self._fuse_bn_tensor(self.stem2[0], self.stem2[1]) self.stem1[0].weight.data = torch.einsum('oi,icjk->ocjk', kernel.squeeze(3).squeeze(2), self.stem1[0].weight.data) self.stem1[0].bias.data = bias + (self.stem1[0].bias.data.view(1,-1,1,1)*kernel).sum(3).sum(2).sum(1) self.stem = torch.nn.Sequential(*[self.stem1[0], self.stem2[2]]) self.__delattr__('stem1') self.__delattr__('stem2') for i in range(self.depth): self.stages[i].switch_to_deploy() self.deploy = True def update_weight(model_dict, weight_dict): idx, temp_dict = 0, {} for k, v in weight_dict.items(): if k in model_dict.keys() and np.shape(model_dict[k]) == np.shape(v): temp_dict[k] = v idx += 1 model_dict.update(temp_dict) print(f'loading weights... {idx}/{len(model_dict)} items') return model_dict def vanillanet_13_x1_5_ada_pool(pretrained='', in_22k=False, **kwargs): model = VanillaNet( dims=[128*6, 128*6, 256*6, 512*6, 512*6, 512*6, 512*6, 512*6, 512*6, 512*6, 1024*6, 1024*6], strides=[1,2,2,1,1,1,1,1,1,2,1], ada_pool=[0,40,20,0,0,0,0,0,0,10,0], **kwargs) if pretrained: weights = torch.load(pretrained)['model_ema'] model.load_state_dict(update_weight(model.state_dict(), weights)) return model
null
165,997
import os import copy import torch import torch.nn as nn import numpy as np from timm.data import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD from timm.models.layers import DropPath, trunc_normal_, to_2tuple from timm.models.registry import register_model def _cfg(url='', **kwargs): return { 'url': url, 'num_classes': 1000, 'pool_size': None, 'crop_pct': .95, 'interpolation': 'bicubic', 'mean': IMAGENET_DEFAULT_MEAN, 'std': IMAGENET_DEFAULT_STD, 'classifier': 'head', **kwargs }
null
165,998
import os import copy import torch import torch.nn as nn import numpy as np from timm.data import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD from timm.models.layers import DropPath, trunc_normal_, to_2tuple from timm.models.registry import register_model class GroupNorm(nn.GroupNorm): """ Group Normalization with 1 group. Input: tensor in shape [B, C, H, W] """ def __init__(self, num_channels, **kwargs): super().__init__(1, num_channels, **kwargs) class PoolFormerBlock(nn.Module): """ Implementation of one PoolFormer block. --dim: embedding dim --pool_size: pooling size --mlp_ratio: mlp expansion ratio --act_layer: activation --norm_layer: normalization --drop: dropout rate --drop path: Stochastic Depth, refer to https://arxiv.org/abs/1603.09382 --use_layer_scale, --layer_scale_init_value: LayerScale, refer to https://arxiv.org/abs/2103.17239 """ def __init__(self, dim, pool_size=3, mlp_ratio=4., act_layer=nn.GELU, norm_layer=GroupNorm, drop=0., drop_path=0., use_layer_scale=True, layer_scale_init_value=1e-5): super().__init__() self.norm1 = norm_layer(dim) self.token_mixer = Pooling(pool_size=pool_size) self.norm2 = norm_layer(dim) mlp_hidden_dim = int(dim * mlp_ratio) self.mlp = Mlp(in_features=dim, hidden_features=mlp_hidden_dim, act_layer=act_layer, drop=drop) # The following two techniques are useful to train deep PoolFormers. self.drop_path = DropPath(drop_path) if drop_path > 0. \ else nn.Identity() self.use_layer_scale = use_layer_scale if use_layer_scale: self.layer_scale_1 = nn.Parameter( layer_scale_init_value * torch.ones((dim)), requires_grad=True) self.layer_scale_2 = nn.Parameter( layer_scale_init_value * torch.ones((dim)), requires_grad=True) def forward(self, x): if self.use_layer_scale: x = x + self.drop_path( self.layer_scale_1.unsqueeze(-1).unsqueeze(-1) * self.token_mixer(self.norm1(x))) x = x + self.drop_path( self.layer_scale_2.unsqueeze(-1).unsqueeze(-1) * self.mlp(self.norm2(x))) else: x = x + self.drop_path(self.token_mixer(self.norm1(x))) x = x + self.drop_path(self.mlp(self.norm2(x))) return x The provided code snippet includes necessary dependencies for implementing the `basic_blocks` function. Write a Python function `def basic_blocks(dim, index, layers, pool_size=3, mlp_ratio=4., act_layer=nn.GELU, norm_layer=GroupNorm, drop_rate=.0, drop_path_rate=0., use_layer_scale=True, layer_scale_init_value=1e-5)` to solve the following problem: generate PoolFormer blocks for a stage return: PoolFormer blocks Here is the function: def basic_blocks(dim, index, layers, pool_size=3, mlp_ratio=4., act_layer=nn.GELU, norm_layer=GroupNorm, drop_rate=.0, drop_path_rate=0., use_layer_scale=True, layer_scale_init_value=1e-5): """ generate PoolFormer blocks for a stage return: PoolFormer blocks """ blocks = [] for block_idx in range(layers[index]): block_dpr = drop_path_rate * ( block_idx + sum(layers[:index])) / (sum(layers) - 1) blocks.append(PoolFormerBlock( dim, pool_size=pool_size, mlp_ratio=mlp_ratio, act_layer=act_layer, norm_layer=norm_layer, drop=drop_rate, drop_path=block_dpr, use_layer_scale=use_layer_scale, layer_scale_init_value=layer_scale_init_value, )) blocks = nn.Sequential(*blocks) return blocks
generate PoolFormer blocks for a stage return: PoolFormer blocks
165,999
import os import copy import torch import torch.nn as nn import numpy as np from timm.data import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD from timm.models.layers import DropPath, trunc_normal_, to_2tuple from timm.models.registry import register_model default_cfgs = { 'poolformer_s': _cfg(crop_pct=0.9), 'poolformer_m': _cfg(crop_pct=0.95), } class PoolFormer(nn.Module): """ PoolFormer, the main class of our model --layers: [x,x,x,x], number of blocks for the 4 stages --embed_dims, --mlp_ratios, --pool_size: the embedding dims, mlp ratios and pooling size for the 4 stages --downsamples: flags to apply downsampling or not --norm_layer, --act_layer: define the types of normalization and activation --num_classes: number of classes for the image classification --in_patch_size, --in_stride, --in_pad: specify the patch embedding for the input image --down_patch_size --down_stride --down_pad: specify the downsample (patch embed.) --fork_feat: whether output features of the 4 stages, for dense prediction --init_cfg, --pretrained: for mmdetection and mmsegmentation to load pretrained weights """ def __init__(self, layers, embed_dims=None, mlp_ratios=None, downsamples=None, pool_size=3, norm_layer=GroupNorm, act_layer=nn.GELU, num_classes=1000, in_patch_size=7, in_stride=4, in_pad=2, down_patch_size=3, down_stride=2, down_pad=1, drop_rate=0., drop_path_rate=0., use_layer_scale=True, layer_scale_init_value=1e-5, fork_feat=True, init_cfg=None, pretrained=None, **kwargs): super().__init__() if not fork_feat: self.num_classes = num_classes self.fork_feat = fork_feat self.patch_embed = PatchEmbed( patch_size=in_patch_size, stride=in_stride, padding=in_pad, in_chans=3, embed_dim=embed_dims[0]) # set the main block in network network = [] for i in range(len(layers)): stage = basic_blocks(embed_dims[i], i, layers, pool_size=pool_size, mlp_ratio=mlp_ratios[i], act_layer=act_layer, norm_layer=norm_layer, drop_rate=drop_rate, drop_path_rate=drop_path_rate, use_layer_scale=use_layer_scale, layer_scale_init_value=layer_scale_init_value) network.append(stage) if i >= len(layers) - 1: break if downsamples[i] or embed_dims[i] != embed_dims[i+1]: # downsampling between two stages network.append( PatchEmbed( patch_size=down_patch_size, stride=down_stride, padding=down_pad, in_chans=embed_dims[i], embed_dim=embed_dims[i+1] ) ) self.network = nn.ModuleList(network) if self.fork_feat: # add a norm layer for each output self.out_indices = [0, 2, 4, 6] for i_emb, i_layer in enumerate(self.out_indices): if i_emb == 0 and os.environ.get('FORK_LAST3', None): # TODO: more elegant way """For RetinaNet, `start_level=1`. The first norm layer will not used. cmd: `FORK_LAST3=1 python -m torch.distributed.launch ...` """ layer = nn.Identity() else: layer = norm_layer(embed_dims[i_emb]) layer_name = f'norm{i_layer}' self.add_module(layer_name, layer) else: # Classifier head self.norm = norm_layer(embed_dims[-1]) self.head = nn.Linear( embed_dims[-1], num_classes) if num_classes > 0 \ else nn.Identity() self.init_cfg = copy.deepcopy(init_cfg) self.channel = [i.size(1) for i in self.forward(torch.randn(1, 3, 224, 224))] def reset_classifier(self, num_classes): self.num_classes = num_classes self.head = nn.Linear( self.embed_dim, num_classes) if num_classes > 0 else nn.Identity() def forward_embeddings(self, x): x = self.patch_embed(x) return x def forward_tokens(self, x): outs = [] for idx, block in enumerate(self.network): x = block(x) if self.fork_feat and idx in self.out_indices: norm_layer = getattr(self, f'norm{idx}') x_out = norm_layer(x) outs.append(x_out) return outs def forward(self, x): # input embedding x = self.forward_embeddings(x) # through backbone x = self.forward_tokens(x) return x model_urls = { "poolformer_s12": "https://github.com/sail-sg/poolformer/releases/download/v1.0/poolformer_s12.pth.tar", "poolformer_s24": "https://github.com/sail-sg/poolformer/releases/download/v1.0/poolformer_s24.pth.tar", "poolformer_s36": "https://github.com/sail-sg/poolformer/releases/download/v1.0/poolformer_s36.pth.tar", "poolformer_m36": "https://github.com/sail-sg/poolformer/releases/download/v1.0/poolformer_m36.pth.tar", "poolformer_m48": "https://github.com/sail-sg/poolformer/releases/download/v1.0/poolformer_m48.pth.tar", } def update_weight(model_dict, weight_dict): idx, temp_dict = 0, {} for k, v in weight_dict.items(): if k in model_dict.keys() and np.shape(model_dict[k]) == np.shape(v): temp_dict[k] = v idx += 1 model_dict.update(temp_dict) print(f'loading weights... {idx}/{len(model_dict)} items') return model_dict The provided code snippet includes necessary dependencies for implementing the `poolformer_s12` function. Write a Python function `def poolformer_s12(pretrained=False, **kwargs)` to solve the following problem: PoolFormer-S12 model, Params: 12M --layers: [x,x,x,x], numbers of layers for the four stages --embed_dims, --mlp_ratios: embedding dims and mlp ratios for the four stages --downsamples: flags to apply downsampling or not in four blocks Here is the function: def poolformer_s12(pretrained=False, **kwargs): """ PoolFormer-S12 model, Params: 12M --layers: [x,x,x,x], numbers of layers for the four stages --embed_dims, --mlp_ratios: embedding dims and mlp ratios for the four stages --downsamples: flags to apply downsampling or not in four blocks """ layers = [2, 2, 6, 2] embed_dims = [64, 128, 320, 512] mlp_ratios = [4, 4, 4, 4] downsamples = [True, True, True, True] model = PoolFormer( layers, embed_dims=embed_dims, mlp_ratios=mlp_ratios, downsamples=downsamples, **kwargs) model.default_cfg = default_cfgs['poolformer_s'] if pretrained: url = model_urls['poolformer_s12'] checkpoint = torch.hub.load_state_dict_from_url(url=url, map_location="cpu", check_hash=True) model.load_state_dict(update_weight(model.state_dict(), checkpoint)) return model
PoolFormer-S12 model, Params: 12M --layers: [x,x,x,x], numbers of layers for the four stages --embed_dims, --mlp_ratios: embedding dims and mlp ratios for the four stages --downsamples: flags to apply downsampling or not in four blocks
166,000
import os import copy import torch import torch.nn as nn import numpy as np from timm.data import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD from timm.models.layers import DropPath, trunc_normal_, to_2tuple from timm.models.registry import register_model default_cfgs = { 'poolformer_s': _cfg(crop_pct=0.9), 'poolformer_m': _cfg(crop_pct=0.95), } class PoolFormer(nn.Module): """ PoolFormer, the main class of our model --layers: [x,x,x,x], number of blocks for the 4 stages --embed_dims, --mlp_ratios, --pool_size: the embedding dims, mlp ratios and pooling size for the 4 stages --downsamples: flags to apply downsampling or not --norm_layer, --act_layer: define the types of normalization and activation --num_classes: number of classes for the image classification --in_patch_size, --in_stride, --in_pad: specify the patch embedding for the input image --down_patch_size --down_stride --down_pad: specify the downsample (patch embed.) --fork_feat: whether output features of the 4 stages, for dense prediction --init_cfg, --pretrained: for mmdetection and mmsegmentation to load pretrained weights """ def __init__(self, layers, embed_dims=None, mlp_ratios=None, downsamples=None, pool_size=3, norm_layer=GroupNorm, act_layer=nn.GELU, num_classes=1000, in_patch_size=7, in_stride=4, in_pad=2, down_patch_size=3, down_stride=2, down_pad=1, drop_rate=0., drop_path_rate=0., use_layer_scale=True, layer_scale_init_value=1e-5, fork_feat=True, init_cfg=None, pretrained=None, **kwargs): super().__init__() if not fork_feat: self.num_classes = num_classes self.fork_feat = fork_feat self.patch_embed = PatchEmbed( patch_size=in_patch_size, stride=in_stride, padding=in_pad, in_chans=3, embed_dim=embed_dims[0]) # set the main block in network network = [] for i in range(len(layers)): stage = basic_blocks(embed_dims[i], i, layers, pool_size=pool_size, mlp_ratio=mlp_ratios[i], act_layer=act_layer, norm_layer=norm_layer, drop_rate=drop_rate, drop_path_rate=drop_path_rate, use_layer_scale=use_layer_scale, layer_scale_init_value=layer_scale_init_value) network.append(stage) if i >= len(layers) - 1: break if downsamples[i] or embed_dims[i] != embed_dims[i+1]: # downsampling between two stages network.append( PatchEmbed( patch_size=down_patch_size, stride=down_stride, padding=down_pad, in_chans=embed_dims[i], embed_dim=embed_dims[i+1] ) ) self.network = nn.ModuleList(network) if self.fork_feat: # add a norm layer for each output self.out_indices = [0, 2, 4, 6] for i_emb, i_layer in enumerate(self.out_indices): if i_emb == 0 and os.environ.get('FORK_LAST3', None): # TODO: more elegant way """For RetinaNet, `start_level=1`. The first norm layer will not used. cmd: `FORK_LAST3=1 python -m torch.distributed.launch ...` """ layer = nn.Identity() else: layer = norm_layer(embed_dims[i_emb]) layer_name = f'norm{i_layer}' self.add_module(layer_name, layer) else: # Classifier head self.norm = norm_layer(embed_dims[-1]) self.head = nn.Linear( embed_dims[-1], num_classes) if num_classes > 0 \ else nn.Identity() self.init_cfg = copy.deepcopy(init_cfg) self.channel = [i.size(1) for i in self.forward(torch.randn(1, 3, 224, 224))] def reset_classifier(self, num_classes): self.num_classes = num_classes self.head = nn.Linear( self.embed_dim, num_classes) if num_classes > 0 else nn.Identity() def forward_embeddings(self, x): x = self.patch_embed(x) return x def forward_tokens(self, x): outs = [] for idx, block in enumerate(self.network): x = block(x) if self.fork_feat and idx in self.out_indices: norm_layer = getattr(self, f'norm{idx}') x_out = norm_layer(x) outs.append(x_out) return outs def forward(self, x): # input embedding x = self.forward_embeddings(x) # through backbone x = self.forward_tokens(x) return x model_urls = { "poolformer_s12": "https://github.com/sail-sg/poolformer/releases/download/v1.0/poolformer_s12.pth.tar", "poolformer_s24": "https://github.com/sail-sg/poolformer/releases/download/v1.0/poolformer_s24.pth.tar", "poolformer_s36": "https://github.com/sail-sg/poolformer/releases/download/v1.0/poolformer_s36.pth.tar", "poolformer_m36": "https://github.com/sail-sg/poolformer/releases/download/v1.0/poolformer_m36.pth.tar", "poolformer_m48": "https://github.com/sail-sg/poolformer/releases/download/v1.0/poolformer_m48.pth.tar", } def update_weight(model_dict, weight_dict): idx, temp_dict = 0, {} for k, v in weight_dict.items(): if k in model_dict.keys() and np.shape(model_dict[k]) == np.shape(v): temp_dict[k] = v idx += 1 model_dict.update(temp_dict) print(f'loading weights... {idx}/{len(model_dict)} items') return model_dict The provided code snippet includes necessary dependencies for implementing the `poolformer_s24` function. Write a Python function `def poolformer_s24(pretrained=False, **kwargs)` to solve the following problem: PoolFormer-S24 model, Params: 21M Here is the function: def poolformer_s24(pretrained=False, **kwargs): """ PoolFormer-S24 model, Params: 21M """ layers = [4, 4, 12, 4] embed_dims = [64, 128, 320, 512] mlp_ratios = [4, 4, 4, 4] downsamples = [True, True, True, True] model = PoolFormer( layers, embed_dims=embed_dims, mlp_ratios=mlp_ratios, downsamples=downsamples, **kwargs) model.default_cfg = default_cfgs['poolformer_s'] if pretrained: url = model_urls['poolformer_s24'] checkpoint = torch.hub.load_state_dict_from_url(url=url, map_location="cpu", check_hash=True) model.load_state_dict(update_weight(model.state_dict(), checkpoint)) return model
PoolFormer-S24 model, Params: 21M
166,001
import os import copy import torch import torch.nn as nn import numpy as np from timm.data import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD from timm.models.layers import DropPath, trunc_normal_, to_2tuple from timm.models.registry import register_model default_cfgs = { 'poolformer_s': _cfg(crop_pct=0.9), 'poolformer_m': _cfg(crop_pct=0.95), } class PoolFormer(nn.Module): """ PoolFormer, the main class of our model --layers: [x,x,x,x], number of blocks for the 4 stages --embed_dims, --mlp_ratios, --pool_size: the embedding dims, mlp ratios and pooling size for the 4 stages --downsamples: flags to apply downsampling or not --norm_layer, --act_layer: define the types of normalization and activation --num_classes: number of classes for the image classification --in_patch_size, --in_stride, --in_pad: specify the patch embedding for the input image --down_patch_size --down_stride --down_pad: specify the downsample (patch embed.) --fork_feat: whether output features of the 4 stages, for dense prediction --init_cfg, --pretrained: for mmdetection and mmsegmentation to load pretrained weights """ def __init__(self, layers, embed_dims=None, mlp_ratios=None, downsamples=None, pool_size=3, norm_layer=GroupNorm, act_layer=nn.GELU, num_classes=1000, in_patch_size=7, in_stride=4, in_pad=2, down_patch_size=3, down_stride=2, down_pad=1, drop_rate=0., drop_path_rate=0., use_layer_scale=True, layer_scale_init_value=1e-5, fork_feat=True, init_cfg=None, pretrained=None, **kwargs): super().__init__() if not fork_feat: self.num_classes = num_classes self.fork_feat = fork_feat self.patch_embed = PatchEmbed( patch_size=in_patch_size, stride=in_stride, padding=in_pad, in_chans=3, embed_dim=embed_dims[0]) # set the main block in network network = [] for i in range(len(layers)): stage = basic_blocks(embed_dims[i], i, layers, pool_size=pool_size, mlp_ratio=mlp_ratios[i], act_layer=act_layer, norm_layer=norm_layer, drop_rate=drop_rate, drop_path_rate=drop_path_rate, use_layer_scale=use_layer_scale, layer_scale_init_value=layer_scale_init_value) network.append(stage) if i >= len(layers) - 1: break if downsamples[i] or embed_dims[i] != embed_dims[i+1]: # downsampling between two stages network.append( PatchEmbed( patch_size=down_patch_size, stride=down_stride, padding=down_pad, in_chans=embed_dims[i], embed_dim=embed_dims[i+1] ) ) self.network = nn.ModuleList(network) if self.fork_feat: # add a norm layer for each output self.out_indices = [0, 2, 4, 6] for i_emb, i_layer in enumerate(self.out_indices): if i_emb == 0 and os.environ.get('FORK_LAST3', None): # TODO: more elegant way """For RetinaNet, `start_level=1`. The first norm layer will not used. cmd: `FORK_LAST3=1 python -m torch.distributed.launch ...` """ layer = nn.Identity() else: layer = norm_layer(embed_dims[i_emb]) layer_name = f'norm{i_layer}' self.add_module(layer_name, layer) else: # Classifier head self.norm = norm_layer(embed_dims[-1]) self.head = nn.Linear( embed_dims[-1], num_classes) if num_classes > 0 \ else nn.Identity() self.init_cfg = copy.deepcopy(init_cfg) self.channel = [i.size(1) for i in self.forward(torch.randn(1, 3, 224, 224))] def reset_classifier(self, num_classes): self.num_classes = num_classes self.head = nn.Linear( self.embed_dim, num_classes) if num_classes > 0 else nn.Identity() def forward_embeddings(self, x): x = self.patch_embed(x) return x def forward_tokens(self, x): outs = [] for idx, block in enumerate(self.network): x = block(x) if self.fork_feat and idx in self.out_indices: norm_layer = getattr(self, f'norm{idx}') x_out = norm_layer(x) outs.append(x_out) return outs def forward(self, x): # input embedding x = self.forward_embeddings(x) # through backbone x = self.forward_tokens(x) return x model_urls = { "poolformer_s12": "https://github.com/sail-sg/poolformer/releases/download/v1.0/poolformer_s12.pth.tar", "poolformer_s24": "https://github.com/sail-sg/poolformer/releases/download/v1.0/poolformer_s24.pth.tar", "poolformer_s36": "https://github.com/sail-sg/poolformer/releases/download/v1.0/poolformer_s36.pth.tar", "poolformer_m36": "https://github.com/sail-sg/poolformer/releases/download/v1.0/poolformer_m36.pth.tar", "poolformer_m48": "https://github.com/sail-sg/poolformer/releases/download/v1.0/poolformer_m48.pth.tar", } def update_weight(model_dict, weight_dict): idx, temp_dict = 0, {} for k, v in weight_dict.items(): if k in model_dict.keys() and np.shape(model_dict[k]) == np.shape(v): temp_dict[k] = v idx += 1 model_dict.update(temp_dict) print(f'loading weights... {idx}/{len(model_dict)} items') return model_dict The provided code snippet includes necessary dependencies for implementing the `poolformer_s36` function. Write a Python function `def poolformer_s36(pretrained=False, **kwargs)` to solve the following problem: PoolFormer-S36 model, Params: 31M Here is the function: def poolformer_s36(pretrained=False, **kwargs): """ PoolFormer-S36 model, Params: 31M """ layers = [6, 6, 18, 6] embed_dims = [64, 128, 320, 512] mlp_ratios = [4, 4, 4, 4] downsamples = [True, True, True, True] model = PoolFormer( layers, embed_dims=embed_dims, mlp_ratios=mlp_ratios, downsamples=downsamples, layer_scale_init_value=1e-6, **kwargs) model.default_cfg = default_cfgs['poolformer_s'] if pretrained: url = model_urls['poolformer_s36'] checkpoint = torch.hub.load_state_dict_from_url(url=url, map_location="cpu", check_hash=True) model.load_state_dict(update_weight(model.state_dict(), checkpoint)) return model
PoolFormer-S36 model, Params: 31M
166,002
import os import copy import torch import torch.nn as nn import numpy as np from timm.data import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD from timm.models.layers import DropPath, trunc_normal_, to_2tuple from timm.models.registry import register_model default_cfgs = { 'poolformer_s': _cfg(crop_pct=0.9), 'poolformer_m': _cfg(crop_pct=0.95), } class PoolFormer(nn.Module): """ PoolFormer, the main class of our model --layers: [x,x,x,x], number of blocks for the 4 stages --embed_dims, --mlp_ratios, --pool_size: the embedding dims, mlp ratios and pooling size for the 4 stages --downsamples: flags to apply downsampling or not --norm_layer, --act_layer: define the types of normalization and activation --num_classes: number of classes for the image classification --in_patch_size, --in_stride, --in_pad: specify the patch embedding for the input image --down_patch_size --down_stride --down_pad: specify the downsample (patch embed.) --fork_feat: whether output features of the 4 stages, for dense prediction --init_cfg, --pretrained: for mmdetection and mmsegmentation to load pretrained weights """ def __init__(self, layers, embed_dims=None, mlp_ratios=None, downsamples=None, pool_size=3, norm_layer=GroupNorm, act_layer=nn.GELU, num_classes=1000, in_patch_size=7, in_stride=4, in_pad=2, down_patch_size=3, down_stride=2, down_pad=1, drop_rate=0., drop_path_rate=0., use_layer_scale=True, layer_scale_init_value=1e-5, fork_feat=True, init_cfg=None, pretrained=None, **kwargs): super().__init__() if not fork_feat: self.num_classes = num_classes self.fork_feat = fork_feat self.patch_embed = PatchEmbed( patch_size=in_patch_size, stride=in_stride, padding=in_pad, in_chans=3, embed_dim=embed_dims[0]) # set the main block in network network = [] for i in range(len(layers)): stage = basic_blocks(embed_dims[i], i, layers, pool_size=pool_size, mlp_ratio=mlp_ratios[i], act_layer=act_layer, norm_layer=norm_layer, drop_rate=drop_rate, drop_path_rate=drop_path_rate, use_layer_scale=use_layer_scale, layer_scale_init_value=layer_scale_init_value) network.append(stage) if i >= len(layers) - 1: break if downsamples[i] or embed_dims[i] != embed_dims[i+1]: # downsampling between two stages network.append( PatchEmbed( patch_size=down_patch_size, stride=down_stride, padding=down_pad, in_chans=embed_dims[i], embed_dim=embed_dims[i+1] ) ) self.network = nn.ModuleList(network) if self.fork_feat: # add a norm layer for each output self.out_indices = [0, 2, 4, 6] for i_emb, i_layer in enumerate(self.out_indices): if i_emb == 0 and os.environ.get('FORK_LAST3', None): # TODO: more elegant way """For RetinaNet, `start_level=1`. The first norm layer will not used. cmd: `FORK_LAST3=1 python -m torch.distributed.launch ...` """ layer = nn.Identity() else: layer = norm_layer(embed_dims[i_emb]) layer_name = f'norm{i_layer}' self.add_module(layer_name, layer) else: # Classifier head self.norm = norm_layer(embed_dims[-1]) self.head = nn.Linear( embed_dims[-1], num_classes) if num_classes > 0 \ else nn.Identity() self.init_cfg = copy.deepcopy(init_cfg) self.channel = [i.size(1) for i in self.forward(torch.randn(1, 3, 224, 224))] def reset_classifier(self, num_classes): self.num_classes = num_classes self.head = nn.Linear( self.embed_dim, num_classes) if num_classes > 0 else nn.Identity() def forward_embeddings(self, x): x = self.patch_embed(x) return x def forward_tokens(self, x): outs = [] for idx, block in enumerate(self.network): x = block(x) if self.fork_feat and idx in self.out_indices: norm_layer = getattr(self, f'norm{idx}') x_out = norm_layer(x) outs.append(x_out) return outs def forward(self, x): # input embedding x = self.forward_embeddings(x) # through backbone x = self.forward_tokens(x) return x model_urls = { "poolformer_s12": "https://github.com/sail-sg/poolformer/releases/download/v1.0/poolformer_s12.pth.tar", "poolformer_s24": "https://github.com/sail-sg/poolformer/releases/download/v1.0/poolformer_s24.pth.tar", "poolformer_s36": "https://github.com/sail-sg/poolformer/releases/download/v1.0/poolformer_s36.pth.tar", "poolformer_m36": "https://github.com/sail-sg/poolformer/releases/download/v1.0/poolformer_m36.pth.tar", "poolformer_m48": "https://github.com/sail-sg/poolformer/releases/download/v1.0/poolformer_m48.pth.tar", } def update_weight(model_dict, weight_dict): idx, temp_dict = 0, {} for k, v in weight_dict.items(): if k in model_dict.keys() and np.shape(model_dict[k]) == np.shape(v): temp_dict[k] = v idx += 1 model_dict.update(temp_dict) print(f'loading weights... {idx}/{len(model_dict)} items') return model_dict The provided code snippet includes necessary dependencies for implementing the `poolformer_m36` function. Write a Python function `def poolformer_m36(pretrained=False, **kwargs)` to solve the following problem: PoolFormer-M36 model, Params: 56M Here is the function: def poolformer_m36(pretrained=False, **kwargs): """ PoolFormer-M36 model, Params: 56M """ layers = [6, 6, 18, 6] embed_dims = [96, 192, 384, 768] mlp_ratios = [4, 4, 4, 4] downsamples = [True, True, True, True] model = PoolFormer( layers, embed_dims=embed_dims, mlp_ratios=mlp_ratios, downsamples=downsamples, layer_scale_init_value=1e-6, **kwargs) model.default_cfg = default_cfgs['poolformer_m'] if pretrained: url = model_urls['poolformer_m36'] checkpoint = torch.hub.load_state_dict_from_url(url=url, map_location="cpu", check_hash=True) model.load_state_dict(update_weight(model.state_dict(), checkpoint)) return model
PoolFormer-M36 model, Params: 56M
166,003
import os import copy import torch import torch.nn as nn import numpy as np from timm.data import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD from timm.models.layers import DropPath, trunc_normal_, to_2tuple from timm.models.registry import register_model default_cfgs = { 'poolformer_s': _cfg(crop_pct=0.9), 'poolformer_m': _cfg(crop_pct=0.95), } class PoolFormer(nn.Module): """ PoolFormer, the main class of our model --layers: [x,x,x,x], number of blocks for the 4 stages --embed_dims, --mlp_ratios, --pool_size: the embedding dims, mlp ratios and pooling size for the 4 stages --downsamples: flags to apply downsampling or not --norm_layer, --act_layer: define the types of normalization and activation --num_classes: number of classes for the image classification --in_patch_size, --in_stride, --in_pad: specify the patch embedding for the input image --down_patch_size --down_stride --down_pad: specify the downsample (patch embed.) --fork_feat: whether output features of the 4 stages, for dense prediction --init_cfg, --pretrained: for mmdetection and mmsegmentation to load pretrained weights """ def __init__(self, layers, embed_dims=None, mlp_ratios=None, downsamples=None, pool_size=3, norm_layer=GroupNorm, act_layer=nn.GELU, num_classes=1000, in_patch_size=7, in_stride=4, in_pad=2, down_patch_size=3, down_stride=2, down_pad=1, drop_rate=0., drop_path_rate=0., use_layer_scale=True, layer_scale_init_value=1e-5, fork_feat=True, init_cfg=None, pretrained=None, **kwargs): super().__init__() if not fork_feat: self.num_classes = num_classes self.fork_feat = fork_feat self.patch_embed = PatchEmbed( patch_size=in_patch_size, stride=in_stride, padding=in_pad, in_chans=3, embed_dim=embed_dims[0]) # set the main block in network network = [] for i in range(len(layers)): stage = basic_blocks(embed_dims[i], i, layers, pool_size=pool_size, mlp_ratio=mlp_ratios[i], act_layer=act_layer, norm_layer=norm_layer, drop_rate=drop_rate, drop_path_rate=drop_path_rate, use_layer_scale=use_layer_scale, layer_scale_init_value=layer_scale_init_value) network.append(stage) if i >= len(layers) - 1: break if downsamples[i] or embed_dims[i] != embed_dims[i+1]: # downsampling between two stages network.append( PatchEmbed( patch_size=down_patch_size, stride=down_stride, padding=down_pad, in_chans=embed_dims[i], embed_dim=embed_dims[i+1] ) ) self.network = nn.ModuleList(network) if self.fork_feat: # add a norm layer for each output self.out_indices = [0, 2, 4, 6] for i_emb, i_layer in enumerate(self.out_indices): if i_emb == 0 and os.environ.get('FORK_LAST3', None): # TODO: more elegant way """For RetinaNet, `start_level=1`. The first norm layer will not used. cmd: `FORK_LAST3=1 python -m torch.distributed.launch ...` """ layer = nn.Identity() else: layer = norm_layer(embed_dims[i_emb]) layer_name = f'norm{i_layer}' self.add_module(layer_name, layer) else: # Classifier head self.norm = norm_layer(embed_dims[-1]) self.head = nn.Linear( embed_dims[-1], num_classes) if num_classes > 0 \ else nn.Identity() self.init_cfg = copy.deepcopy(init_cfg) self.channel = [i.size(1) for i in self.forward(torch.randn(1, 3, 224, 224))] def reset_classifier(self, num_classes): self.num_classes = num_classes self.head = nn.Linear( self.embed_dim, num_classes) if num_classes > 0 else nn.Identity() def forward_embeddings(self, x): x = self.patch_embed(x) return x def forward_tokens(self, x): outs = [] for idx, block in enumerate(self.network): x = block(x) if self.fork_feat and idx in self.out_indices: norm_layer = getattr(self, f'norm{idx}') x_out = norm_layer(x) outs.append(x_out) return outs def forward(self, x): # input embedding x = self.forward_embeddings(x) # through backbone x = self.forward_tokens(x) return x model_urls = { "poolformer_s12": "https://github.com/sail-sg/poolformer/releases/download/v1.0/poolformer_s12.pth.tar", "poolformer_s24": "https://github.com/sail-sg/poolformer/releases/download/v1.0/poolformer_s24.pth.tar", "poolformer_s36": "https://github.com/sail-sg/poolformer/releases/download/v1.0/poolformer_s36.pth.tar", "poolformer_m36": "https://github.com/sail-sg/poolformer/releases/download/v1.0/poolformer_m36.pth.tar", "poolformer_m48": "https://github.com/sail-sg/poolformer/releases/download/v1.0/poolformer_m48.pth.tar", } def update_weight(model_dict, weight_dict): idx, temp_dict = 0, {} for k, v in weight_dict.items(): if k in model_dict.keys() and np.shape(model_dict[k]) == np.shape(v): temp_dict[k] = v idx += 1 model_dict.update(temp_dict) print(f'loading weights... {idx}/{len(model_dict)} items') return model_dict The provided code snippet includes necessary dependencies for implementing the `poolformer_m48` function. Write a Python function `def poolformer_m48(pretrained=False, **kwargs)` to solve the following problem: PoolFormer-M48 model, Params: 73M Here is the function: def poolformer_m48(pretrained=False, **kwargs): """ PoolFormer-M48 model, Params: 73M """ layers = [8, 8, 24, 8] embed_dims = [96, 192, 384, 768] mlp_ratios = [4, 4, 4, 4] downsamples = [True, True, True, True] model = PoolFormer( layers, embed_dims=embed_dims, mlp_ratios=mlp_ratios, downsamples=downsamples, layer_scale_init_value=1e-6, **kwargs) model.default_cfg = default_cfgs['poolformer_m'] if pretrained: url = model_urls['poolformer_m48'] checkpoint = torch.hub.load_state_dict_from_url(url=url, map_location="cpu", check_hash=True) model.load_state_dict(update_weight(model.state_dict(), checkpoint)) return model
PoolFormer-M48 model, Params: 73M
166,004
from functools import partial import torch import torch.nn as nn import numpy as np from timm.data import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD from timm.models import checkpoint_seq, to_2tuple from timm.models.layers import trunc_normal_, DropPath from timm.models.registry import register_model def _cfg(url='', **kwargs): return { 'url': url, 'num_classes': 1000, 'input_size': (3, 224, 224), 'pool_size': (7, 7), 'crop_pct': 0.875, 'interpolation': 'bicubic', 'mean': IMAGENET_DEFAULT_MEAN, 'std': IMAGENET_DEFAULT_STD, 'first_conv': 'stem.0', 'classifier': 'head.fc', **kwargs }
null
166,005
from functools import partial import torch import torch.nn as nn import numpy as np from timm.data import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD from timm.models import checkpoint_seq, to_2tuple from timm.models.layers import trunc_normal_, DropPath from timm.models.registry import register_model def update_weight(model_dict, weight_dict): idx, temp_dict = 0, {} for k, v in weight_dict.items(): if k in model_dict.keys() and np.shape(model_dict[k]) == np.shape(v): temp_dict[k] = v idx += 1 model_dict.update(temp_dict) print(f'loading weights... {idx}/{len(model_dict)} items') return model_dict
null
166,006
from functools import partial import torch import torch.nn as nn import numpy as np from timm.data import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD from timm.models import checkpoint_seq, to_2tuple from timm.models.layers import trunc_normal_, DropPath from timm.models.registry import register_model class InceptionDWConv2d(nn.Module): def __init__(self, in_channels, square_kernel_size=3, band_kernel_size=11, branch_ratio=0.125): def forward(self, x): class MetaNeXt(nn.Module): def __init__( self, in_chans=3, num_classes=1000, depths=(3, 3, 9, 3), dims=(96, 192, 384, 768), token_mixers=nn.Identity, norm_layer=nn.BatchNorm2d, act_layer=nn.GELU, mlp_ratios=(4, 4, 4, 3), head_fn=MlpHead, drop_rate=0., drop_path_rate=0., ls_init_value=1e-6, **kwargs, ): def set_grad_checkpointing(self, enable=True): def no_weight_decay(self): def forward(self, x): def _init_weights(self, m): default_cfgs = dict( inceptionnext_tiny=_cfg( url='https://github.com/sail-sg/inceptionnext/releases/download/model/inceptionnext_tiny.pth', ), inceptionnext_small=_cfg( url='https://github.com/sail-sg/inceptionnext/releases/download/model/inceptionnext_small.pth', ), inceptionnext_base=_cfg( url='https://github.com/sail-sg/inceptionnext/releases/download/model/inceptionnext_base.pth', ), inceptionnext_base_384=_cfg( url='https://github.com/sail-sg/inceptionnext/releases/download/model/inceptionnext_base_384.pth', input_size=(3, 384, 384), crop_pct=1.0, ), ) def inceptionnext_tiny(pretrained=False, **kwargs): model = MetaNeXt(depths=(3, 3, 9, 3), dims=(96, 192, 384, 768), token_mixers=InceptionDWConv2d, **kwargs ) model.default_cfg = default_cfgs['inceptionnext_tiny'] if pretrained: state_dict = torch.hub.load_state_dict_from_url(url=model.default_cfg['url'], map_location="cpu", check_hash=True) model.load_state_dict(state_dict) return model
null
166,007
from functools import partial import torch import torch.nn as nn import numpy as np from timm.data import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD from timm.models import checkpoint_seq, to_2tuple from timm.models.layers import trunc_normal_, DropPath from timm.models.registry import register_model class InceptionDWConv2d(nn.Module): """ Inception depthweise convolution """ def __init__(self, in_channels, square_kernel_size=3, band_kernel_size=11, branch_ratio=0.125): super().__init__() gc = int(in_channels * branch_ratio) # channel numbers of a convolution branch self.dwconv_hw = nn.Conv2d(gc, gc, square_kernel_size, padding=square_kernel_size//2, groups=gc) self.dwconv_w = nn.Conv2d(gc, gc, kernel_size=(1, band_kernel_size), padding=(0, band_kernel_size//2), groups=gc) self.dwconv_h = nn.Conv2d(gc, gc, kernel_size=(band_kernel_size, 1), padding=(band_kernel_size//2, 0), groups=gc) self.split_indexes = (in_channels - 3 * gc, gc, gc, gc) def forward(self, x): x_id, x_hw, x_w, x_h = torch.split(x, self.split_indexes, dim=1) return torch.cat( (x_id, self.dwconv_hw(x_hw), self.dwconv_w(x_w), self.dwconv_h(x_h)), dim=1, ) class MetaNeXt(nn.Module): r""" MetaNeXt A PyTorch impl of : `InceptionNeXt: When Inception Meets ConvNeXt` - https://arxiv.org/pdf/2203.xxxxx.pdf Args: in_chans (int): Number of input image channels. Default: 3 num_classes (int): Number of classes for classification head. Default: 1000 depths (tuple(int)): Number of blocks at each stage. Default: (3, 3, 9, 3) dims (tuple(int)): Feature dimension at each stage. Default: (96, 192, 384, 768) token_mixers: Token mixer function. Default: nn.Identity norm_layer: Normalziation layer. Default: nn.BatchNorm2d act_layer: Activation function for MLP. Default: nn.GELU mlp_ratios (int or tuple(int)): MLP ratios. Default: (4, 4, 4, 3) head_fn: classifier head drop_rate (float): Head dropout rate drop_path_rate (float): Stochastic depth rate. Default: 0. ls_init_value (float): Init value for Layer Scale. Default: 1e-6. """ def __init__( self, in_chans=3, num_classes=1000, depths=(3, 3, 9, 3), dims=(96, 192, 384, 768), token_mixers=nn.Identity, norm_layer=nn.BatchNorm2d, act_layer=nn.GELU, mlp_ratios=(4, 4, 4, 3), head_fn=MlpHead, drop_rate=0., drop_path_rate=0., ls_init_value=1e-6, **kwargs, ): super().__init__() num_stage = len(depths) if not isinstance(token_mixers, (list, tuple)): token_mixers = [token_mixers] * num_stage if not isinstance(mlp_ratios, (list, tuple)): mlp_ratios = [mlp_ratios] * num_stage self.num_classes = num_classes self.drop_rate = drop_rate self.stem = nn.Sequential( nn.Conv2d(in_chans, dims[0], kernel_size=4, stride=4), norm_layer(dims[0]) ) self.stages = nn.Sequential() dp_rates = [x.tolist() for x in torch.linspace(0, drop_path_rate, sum(depths)).split(depths)] stages = [] prev_chs = dims[0] # feature resolution stages, each consisting of multiple residual blocks for i in range(num_stage): out_chs = dims[i] stages.append(MetaNeXtStage( prev_chs, out_chs, ds_stride=2 if i > 0 else 1, depth=depths[i], drop_path_rates=dp_rates[i], ls_init_value=ls_init_value, act_layer=act_layer, norm_layer=norm_layer, mlp_ratio=mlp_ratios[i], )) prev_chs = out_chs self.stages = nn.Sequential(*stages) self.num_features = prev_chs self.apply(self._init_weights) self.channel = [i.size(1) for i in self.forward(torch.randn(1, 3, 640, 640))] def set_grad_checkpointing(self, enable=True): for s in self.stages: s.grad_checkpointing = enable def no_weight_decay(self): return {'norm'} def forward(self, x): input_size = x.size(2) scale = [4, 8, 16, 32] features = [None, None, None, None] x = self.stem(x) features[scale.index(input_size // x.size(2))] = x for idx, layer in enumerate(self.stages): x = layer(x) if input_size // x.size(2) in scale: features[scale.index(input_size // x.size(2))] = x return features def _init_weights(self, m): if isinstance(m, (nn.Conv2d, nn.Linear)): trunc_normal_(m.weight, std=.02) if m.bias is not None: nn.init.constant_(m.bias, 0) default_cfgs = dict( inceptionnext_tiny=_cfg( url='https://github.com/sail-sg/inceptionnext/releases/download/model/inceptionnext_tiny.pth', ), inceptionnext_small=_cfg( url='https://github.com/sail-sg/inceptionnext/releases/download/model/inceptionnext_small.pth', ), inceptionnext_base=_cfg( url='https://github.com/sail-sg/inceptionnext/releases/download/model/inceptionnext_base.pth', ), inceptionnext_base_384=_cfg( url='https://github.com/sail-sg/inceptionnext/releases/download/model/inceptionnext_base_384.pth', input_size=(3, 384, 384), crop_pct=1.0, ), ) def inceptionnext_small(pretrained=False, **kwargs): model = MetaNeXt(depths=(3, 3, 27, 3), dims=(96, 192, 384, 768), token_mixers=InceptionDWConv2d, **kwargs ) model.default_cfg = default_cfgs['inceptionnext_small'] if pretrained: state_dict = torch.hub.load_state_dict_from_url(url=model.default_cfg['url'], map_location="cpu", check_hash=True) model.load_state_dict(state_dict) return model
null
166,008
from functools import partial import torch import torch.nn as nn import numpy as np from timm.data import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD from timm.models import checkpoint_seq, to_2tuple from timm.models.layers import trunc_normal_, DropPath from timm.models.registry import register_model class InceptionDWConv2d(nn.Module): """ Inception depthweise convolution """ def __init__(self, in_channels, square_kernel_size=3, band_kernel_size=11, branch_ratio=0.125): super().__init__() gc = int(in_channels * branch_ratio) # channel numbers of a convolution branch self.dwconv_hw = nn.Conv2d(gc, gc, square_kernel_size, padding=square_kernel_size//2, groups=gc) self.dwconv_w = nn.Conv2d(gc, gc, kernel_size=(1, band_kernel_size), padding=(0, band_kernel_size//2), groups=gc) self.dwconv_h = nn.Conv2d(gc, gc, kernel_size=(band_kernel_size, 1), padding=(band_kernel_size//2, 0), groups=gc) self.split_indexes = (in_channels - 3 * gc, gc, gc, gc) def forward(self, x): x_id, x_hw, x_w, x_h = torch.split(x, self.split_indexes, dim=1) return torch.cat( (x_id, self.dwconv_hw(x_hw), self.dwconv_w(x_w), self.dwconv_h(x_h)), dim=1, ) class MetaNeXt(nn.Module): r""" MetaNeXt A PyTorch impl of : `InceptionNeXt: When Inception Meets ConvNeXt` - https://arxiv.org/pdf/2203.xxxxx.pdf Args: in_chans (int): Number of input image channels. Default: 3 num_classes (int): Number of classes for classification head. Default: 1000 depths (tuple(int)): Number of blocks at each stage. Default: (3, 3, 9, 3) dims (tuple(int)): Feature dimension at each stage. Default: (96, 192, 384, 768) token_mixers: Token mixer function. Default: nn.Identity norm_layer: Normalziation layer. Default: nn.BatchNorm2d act_layer: Activation function for MLP. Default: nn.GELU mlp_ratios (int or tuple(int)): MLP ratios. Default: (4, 4, 4, 3) head_fn: classifier head drop_rate (float): Head dropout rate drop_path_rate (float): Stochastic depth rate. Default: 0. ls_init_value (float): Init value for Layer Scale. Default: 1e-6. """ def __init__( self, in_chans=3, num_classes=1000, depths=(3, 3, 9, 3), dims=(96, 192, 384, 768), token_mixers=nn.Identity, norm_layer=nn.BatchNorm2d, act_layer=nn.GELU, mlp_ratios=(4, 4, 4, 3), head_fn=MlpHead, drop_rate=0., drop_path_rate=0., ls_init_value=1e-6, **kwargs, ): super().__init__() num_stage = len(depths) if not isinstance(token_mixers, (list, tuple)): token_mixers = [token_mixers] * num_stage if not isinstance(mlp_ratios, (list, tuple)): mlp_ratios = [mlp_ratios] * num_stage self.num_classes = num_classes self.drop_rate = drop_rate self.stem = nn.Sequential( nn.Conv2d(in_chans, dims[0], kernel_size=4, stride=4), norm_layer(dims[0]) ) self.stages = nn.Sequential() dp_rates = [x.tolist() for x in torch.linspace(0, drop_path_rate, sum(depths)).split(depths)] stages = [] prev_chs = dims[0] # feature resolution stages, each consisting of multiple residual blocks for i in range(num_stage): out_chs = dims[i] stages.append(MetaNeXtStage( prev_chs, out_chs, ds_stride=2 if i > 0 else 1, depth=depths[i], drop_path_rates=dp_rates[i], ls_init_value=ls_init_value, act_layer=act_layer, norm_layer=norm_layer, mlp_ratio=mlp_ratios[i], )) prev_chs = out_chs self.stages = nn.Sequential(*stages) self.num_features = prev_chs self.apply(self._init_weights) self.channel = [i.size(1) for i in self.forward(torch.randn(1, 3, 640, 640))] def set_grad_checkpointing(self, enable=True): for s in self.stages: s.grad_checkpointing = enable def no_weight_decay(self): return {'norm'} def forward(self, x): input_size = x.size(2) scale = [4, 8, 16, 32] features = [None, None, None, None] x = self.stem(x) features[scale.index(input_size // x.size(2))] = x for idx, layer in enumerate(self.stages): x = layer(x) if input_size // x.size(2) in scale: features[scale.index(input_size // x.size(2))] = x return features def _init_weights(self, m): if isinstance(m, (nn.Conv2d, nn.Linear)): trunc_normal_(m.weight, std=.02) if m.bias is not None: nn.init.constant_(m.bias, 0) default_cfgs = dict( inceptionnext_tiny=_cfg( url='https://github.com/sail-sg/inceptionnext/releases/download/model/inceptionnext_tiny.pth', ), inceptionnext_small=_cfg( url='https://github.com/sail-sg/inceptionnext/releases/download/model/inceptionnext_small.pth', ), inceptionnext_base=_cfg( url='https://github.com/sail-sg/inceptionnext/releases/download/model/inceptionnext_base.pth', ), inceptionnext_base_384=_cfg( url='https://github.com/sail-sg/inceptionnext/releases/download/model/inceptionnext_base_384.pth', input_size=(3, 384, 384), crop_pct=1.0, ), ) def inceptionnext_base(pretrained=False, **kwargs): model = MetaNeXt(depths=(3, 3, 27, 3), dims=(128, 256, 512, 1024), token_mixers=InceptionDWConv2d, **kwargs ) model.default_cfg = default_cfgs['inceptionnext_base'] if pretrained: state_dict = torch.hub.load_state_dict_from_url(url=model.default_cfg['url'], map_location="cpu", check_hash=True) model.load_state_dict(state_dict) return model
null
166,009
from functools import partial import torch import torch.nn as nn import numpy as np from timm.data import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD from timm.models import checkpoint_seq, to_2tuple from timm.models.layers import trunc_normal_, DropPath from timm.models.registry import register_model class InceptionDWConv2d(nn.Module): """ Inception depthweise convolution """ def __init__(self, in_channels, square_kernel_size=3, band_kernel_size=11, branch_ratio=0.125): super().__init__() gc = int(in_channels * branch_ratio) # channel numbers of a convolution branch self.dwconv_hw = nn.Conv2d(gc, gc, square_kernel_size, padding=square_kernel_size//2, groups=gc) self.dwconv_w = nn.Conv2d(gc, gc, kernel_size=(1, band_kernel_size), padding=(0, band_kernel_size//2), groups=gc) self.dwconv_h = nn.Conv2d(gc, gc, kernel_size=(band_kernel_size, 1), padding=(band_kernel_size//2, 0), groups=gc) self.split_indexes = (in_channels - 3 * gc, gc, gc, gc) def forward(self, x): x_id, x_hw, x_w, x_h = torch.split(x, self.split_indexes, dim=1) return torch.cat( (x_id, self.dwconv_hw(x_hw), self.dwconv_w(x_w), self.dwconv_h(x_h)), dim=1, ) class MetaNeXt(nn.Module): r""" MetaNeXt A PyTorch impl of : `InceptionNeXt: When Inception Meets ConvNeXt` - https://arxiv.org/pdf/2203.xxxxx.pdf Args: in_chans (int): Number of input image channels. Default: 3 num_classes (int): Number of classes for classification head. Default: 1000 depths (tuple(int)): Number of blocks at each stage. Default: (3, 3, 9, 3) dims (tuple(int)): Feature dimension at each stage. Default: (96, 192, 384, 768) token_mixers: Token mixer function. Default: nn.Identity norm_layer: Normalziation layer. Default: nn.BatchNorm2d act_layer: Activation function for MLP. Default: nn.GELU mlp_ratios (int or tuple(int)): MLP ratios. Default: (4, 4, 4, 3) head_fn: classifier head drop_rate (float): Head dropout rate drop_path_rate (float): Stochastic depth rate. Default: 0. ls_init_value (float): Init value for Layer Scale. Default: 1e-6. """ def __init__( self, in_chans=3, num_classes=1000, depths=(3, 3, 9, 3), dims=(96, 192, 384, 768), token_mixers=nn.Identity, norm_layer=nn.BatchNorm2d, act_layer=nn.GELU, mlp_ratios=(4, 4, 4, 3), head_fn=MlpHead, drop_rate=0., drop_path_rate=0., ls_init_value=1e-6, **kwargs, ): super().__init__() num_stage = len(depths) if not isinstance(token_mixers, (list, tuple)): token_mixers = [token_mixers] * num_stage if not isinstance(mlp_ratios, (list, tuple)): mlp_ratios = [mlp_ratios] * num_stage self.num_classes = num_classes self.drop_rate = drop_rate self.stem = nn.Sequential( nn.Conv2d(in_chans, dims[0], kernel_size=4, stride=4), norm_layer(dims[0]) ) self.stages = nn.Sequential() dp_rates = [x.tolist() for x in torch.linspace(0, drop_path_rate, sum(depths)).split(depths)] stages = [] prev_chs = dims[0] # feature resolution stages, each consisting of multiple residual blocks for i in range(num_stage): out_chs = dims[i] stages.append(MetaNeXtStage( prev_chs, out_chs, ds_stride=2 if i > 0 else 1, depth=depths[i], drop_path_rates=dp_rates[i], ls_init_value=ls_init_value, act_layer=act_layer, norm_layer=norm_layer, mlp_ratio=mlp_ratios[i], )) prev_chs = out_chs self.stages = nn.Sequential(*stages) self.num_features = prev_chs self.apply(self._init_weights) self.channel = [i.size(1) for i in self.forward(torch.randn(1, 3, 640, 640))] def set_grad_checkpointing(self, enable=True): for s in self.stages: s.grad_checkpointing = enable def no_weight_decay(self): return {'norm'} def forward(self, x): input_size = x.size(2) scale = [4, 8, 16, 32] features = [None, None, None, None] x = self.stem(x) features[scale.index(input_size // x.size(2))] = x for idx, layer in enumerate(self.stages): x = layer(x) if input_size // x.size(2) in scale: features[scale.index(input_size // x.size(2))] = x return features def _init_weights(self, m): if isinstance(m, (nn.Conv2d, nn.Linear)): trunc_normal_(m.weight, std=.02) if m.bias is not None: nn.init.constant_(m.bias, 0) default_cfgs = dict( inceptionnext_tiny=_cfg( url='https://github.com/sail-sg/inceptionnext/releases/download/model/inceptionnext_tiny.pth', ), inceptionnext_small=_cfg( url='https://github.com/sail-sg/inceptionnext/releases/download/model/inceptionnext_small.pth', ), inceptionnext_base=_cfg( url='https://github.com/sail-sg/inceptionnext/releases/download/model/inceptionnext_base.pth', ), inceptionnext_base_384=_cfg( url='https://github.com/sail-sg/inceptionnext/releases/download/model/inceptionnext_base_384.pth', input_size=(3, 384, 384), crop_pct=1.0, ), ) def inceptionnext_base_384(pretrained=False, **kwargs): model = MetaNeXt(depths=[3, 3, 27, 3], dims=[128, 256, 512, 1024], mlp_ratios=[4, 4, 4, 3], token_mixers=InceptionDWConv2d, **kwargs ) model.default_cfg = default_cfgs['inceptionnext_base_384'] if pretrained: state_dict = torch.hub.load_state_dict_from_url(url=model.default_cfg['url'], map_location="cpu", check_hash=True) model.load_state_dict(state_dict) return model
null
166,010
import os import copy import torch import torch.nn as nn import torch.nn.functional as F import math from typing import Dict import itertools import numpy as np from timm.models.layers import DropPath, trunc_normal_, to_2tuple def stem(in_chs, out_chs, act_layer=nn.ReLU): return nn.Sequential( nn.Conv2d(in_chs, out_chs // 2, kernel_size=3, stride=2, padding=1), nn.BatchNorm2d(out_chs // 2), act_layer(), nn.Conv2d(out_chs // 2, out_chs, kernel_size=3, stride=2, padding=1), nn.BatchNorm2d(out_chs), act_layer(), )
null
166,011
import os import copy import torch import torch.nn as nn import torch.nn.functional as F import math from typing import Dict import itertools import numpy as np from timm.models.layers import DropPath, trunc_normal_, to_2tuple class AttnFFN(nn.Module): def __init__(self, dim, mlp_ratio=4., act_layer=nn.ReLU, norm_layer=nn.LayerNorm, drop=0., drop_path=0., use_layer_scale=True, layer_scale_init_value=1e-5, resolution=7, stride=None): super().__init__() self.token_mixer = Attention4D(dim, resolution=resolution, act_layer=act_layer, stride=stride) mlp_hidden_dim = int(dim * mlp_ratio) self.mlp = Mlp(in_features=dim, hidden_features=mlp_hidden_dim, act_layer=act_layer, drop=drop, mid_conv=True) self.drop_path = DropPath(drop_path) if drop_path > 0. \ else nn.Identity() self.use_layer_scale = use_layer_scale if use_layer_scale: self.layer_scale_1 = nn.Parameter( layer_scale_init_value * torch.ones(dim).unsqueeze(-1).unsqueeze(-1), requires_grad=True) self.layer_scale_2 = nn.Parameter( layer_scale_init_value * torch.ones(dim).unsqueeze(-1).unsqueeze(-1), requires_grad=True) def forward(self, x): if self.use_layer_scale: x = x + self.drop_path(self.layer_scale_1 * self.token_mixer(x)) x = x + self.drop_path(self.layer_scale_2 * self.mlp(x)) else: x = x + self.drop_path(self.token_mixer(x)) x = x + self.drop_path(self.mlp(x)) return x class FFN(nn.Module): def __init__(self, dim, pool_size=3, mlp_ratio=4., act_layer=nn.GELU, drop=0., drop_path=0., use_layer_scale=True, layer_scale_init_value=1e-5): super().__init__() mlp_hidden_dim = int(dim * mlp_ratio) self.mlp = Mlp(in_features=dim, hidden_features=mlp_hidden_dim, act_layer=act_layer, drop=drop, mid_conv=True) self.drop_path = DropPath(drop_path) if drop_path > 0. \ else nn.Identity() self.use_layer_scale = use_layer_scale if use_layer_scale: self.layer_scale_2 = nn.Parameter( layer_scale_init_value * torch.ones(dim).unsqueeze(-1).unsqueeze(-1), requires_grad=True) def forward(self, x): if self.use_layer_scale: x = x + self.drop_path(self.layer_scale_2 * self.mlp(x)) else: x = x + self.drop_path(self.mlp(x)) return x def eformer_block(dim, index, layers, pool_size=3, mlp_ratio=4., act_layer=nn.GELU, norm_layer=nn.LayerNorm, drop_rate=.0, drop_path_rate=0., use_layer_scale=True, layer_scale_init_value=1e-5, vit_num=1, resolution=7, e_ratios=None): blocks = [] for block_idx in range(layers[index]): block_dpr = drop_path_rate * ( block_idx + sum(layers[:index])) / (sum(layers) - 1) mlp_ratio = e_ratios[str(index)][block_idx] if index >= 2 and block_idx > layers[index] - 1 - vit_num: if index == 2: stride = 2 else: stride = None blocks.append(AttnFFN( dim, mlp_ratio=mlp_ratio, act_layer=act_layer, norm_layer=norm_layer, drop=drop_rate, drop_path=block_dpr, use_layer_scale=use_layer_scale, layer_scale_init_value=layer_scale_init_value, resolution=resolution, stride=stride, )) else: blocks.append(FFN( dim, pool_size=pool_size, mlp_ratio=mlp_ratio, act_layer=act_layer, drop=drop_rate, drop_path=block_dpr, use_layer_scale=use_layer_scale, layer_scale_init_value=layer_scale_init_value, )) blocks = nn.Sequential(*blocks) return blocks
null