def get_model(cfg): if cfg.base == "timm": if cfg.model_name not in list_models(pretrained=cfg.pretrained): print(list_models(pretrained=cfg.pretrained)) assert True, "Not Found Model: {}".format(cfg.model_name) net = create_model( model_name=cfg.model_name, pretrained=cfg.pretrained, num_classes=cfg.num_classes, in_chans=cfg.in_chans, ) return net else: assert True, "Not Found Model Base: {}".format(cfg.base)
def __init__(self, hparams): super().__init__() model_names = sorted(name for name in models.__dict__ if name.islower() and not name.startswith("__") and callable(models.__dict__[name])) local_model_names = sorted( name for name in local_models.__dict__ if name.islower() and not name.startswith("__") and callable(local_models.__dict__[name])) timm_model_names = timm_model_names = [ 'timm_' + x for x in timm.list_models() ] valid_models = model_names + local_model_names + timm_model_names self.hparams = vars(hparams) if type(hparams) is not dict else hparams if self.hparams['act_func'] == 'swish': self.act_funct = Swish() elif self.hparams['act_func'] == 'mish': self.act_funct = Mish() elif self.hparams['act_func'] == 'relu': self.act_funct = nn.ReLU(inplace=True) # initiate model print("=> creating new model '{}'".format(self.hparams['model'])) if self.hparams['model'] in model_names: cv_model = models.__dict__[self.hparams['model']]( pretrained=False, num_classes=self.hparams['num_classes']) elif self.hparams['model'] in local_model_names: cv_model = local_models.__dict__[self.hparams['model']]( pretrained=False, activation=self.act_funct, num_classes=self.hparams['num_classes']) elif self.hparams['model'] in timm_model_names: # we need this as timm overlaps with other model names sometimes timm_name = self.hparams['model'][5:] cv_model = timm.create_model( timm_name, pretrained=False, num_classes=self.hparams['num_classes']) if self.hparams['model'] == 'inception_v3': cv_model.aux_logits = False self.model = cv_model self.criterion = nn.CrossEntropyLoss() self.save_hyperparameters() self.tr_accuracy = pl.metrics.Accuracy() self.vl_accuracy = pl.metrics.Accuracy() self.test_accuracy = pl.metrics.Accuracy() # for tensorboard graph logger #self.example_input_array = torch.rand((self.input_dim)) self.example_input_array = torch.rand([1, 3, 224, 224])
def get_model(args): total_names = ['efficientnet-b' + str(i) for i in range(8) ] + pretrainedmodels.model_names + timm.list_models() if not args['model_name'] in total_names: print('Nope! Available models are:', total_names) # Load from pretrained first if 'efficientnet' in args['model_name']: try: backbone = EfficientNet.from_pretrained(args['model_name'], 10) except: print('efficientnet-bx x~[0-7] please') raise NotImplementedError num_features = backbone._fc.weight.shape[1] backbone._fc = nn.Sequential() elif args['model_name'] in pretrainedmodels.model_names: backbone = pretrainedmodels.__dict__[args['model_name']]( pretrained='imagenet') num_features = backbone.last_linear.weight.shape[1] backbone.last_linear = nn.Sequential() else: backbone = timm.create_model(args['model_name'], pretrained=True) for child_name, child in list(backbone.named_children())[::-1]: if isinstance(child, nn.Linear): num_features = child.weight.shape[1] setattr(backbone, child_name, nn.Sequential()) break model = add_tail(backbone, num_features) if args['mish'] == 1: to_mish(model) return model
def __init__( self, architecture: str, dropout_rate: float = 0.0, global_pool: str = "avg", num_classes: int = 102, batch_size: int = 64, optimizer_config: DictConfig = DEFAULT_OPTIMIZER, lr_scheduler_config: DictConfig = None, pretrained: bool = True, ): super().__init__() self.save_hyperparameters() self.optimizer_config = optimizer_config self.lr_scheduler_config = lr_scheduler_config # sanity check values pool_options = {"avg", "max", "avgmax", "avgmaxc"} model_options = timm.list_models(pretrained=True) assert global_pool in pool_options, f"global_pool must be one of: {pool_options}" assert architecture in model_options, "model architecture not recognized" # define training objects self.network = timm.create_model( architecture, pretrained=pretrained, num_classes=num_classes, drop_rate=dropout_rate, global_pool=global_pool, ) self.criterion = nn.CrossEntropyLoss() self.accuracy_metric = pl.metrics.Accuracy()
def get_args(): parser = argparse.ArgumentParser() parser.add_argument( "-ds", "--dataset", type=str, default='cifar10', choices=['cifar10', 'cifar100'], help="dataset name in torchvision classificaion dataset") parser.add_argument("-ep", "--epochs", type=int, default=10) parser.add_argument("-bs", "--batch_size", type=int, default=32) parser.add_argument("-lr", "--learning_rate", type=float, default=3e-4) parser.add_argument("-wd", "--weight_decay", type=float, default=0) parser.add_argument("-sd", "--scheduler", type=str, default="", choices=["", "step", "cosine"]) parser.add_argument("-seed", "--seed", type=int, default=2020) parser.add_argument("-md", "--model", type=str, default="efficientnet_b0", choices=timm.list_models(), help="model name in timm models list") parser.add_argument("-wt", "--warmup_type", type=str, default="", choices=["", "linear", "exponential", 'radam']) parser.add_argument("-ws", "--warmup_step", type=float, default=0.0) parser.add_argument( "-opt", "--optimizer", type=str, default='adam', choices=['adam', 'adamw', 'rmsprop', 'sgd', 'radam', 'adamp']) parser.add_argument( "-rt", "--repeat_times", type=int, default=1, help= "how many times of training and testing will be repeated given a group of hyperparameters" ) args = parser.parse_args() dataset_dict = {'cifar10': CIFAR10, 'cifar100': CIFAR100} opt_dict = { 'adam': Adam, 'adamw': AdamW, 'rmsprop': RMSprop, 'sgd': SGD, 'radam': RAdam, 'adamp': AdamP } args.dataset = dataset_dict[args.dataset] args.optimizer = opt_dict[args.optimizer] return args
def check_backbone(self, backbone): model_list = timm.list_models() if backbone not in model_list: closest_matches = difflib.get_close_matches(backbone, model_list, n=10) raise Exception( f"Backbone not in list {model_list}\nClosest matches to {backbone} are: {closest_matches}" )
def register_timm_backbones(register: FlashRegistry): if _TIMM_AVAILABLE: for model_name in timm.list_models(): if model_name in TORCHVISION_MODELS: continue register( fn=catch_url_error(partial(_fn_timm, model_name)), name=model_name, namespace="vision", package="timm", providers=_TIMM, )
def build_multi_predictor( cfg, flags: Flags, device: torch.device, in_channels: int, target_scale: Optional[torch.Tensor] = None, num_modes: int = 3, ) -> nn.Module: model_name = flags.model_name print("model_name", model_name, "model_kwargs", flags.model_kwargs, "num_modes", num_modes) if model_name == "resnet18": print("Building LyftMultiModel") base_model = LyftMultiModel(cfg, num_modes=num_modes, in_channels=in_channels) elif "efficientnet" in model_name: print("Building EfficientNetMulti") base_model = EfficientNetMulti( cfg, num_modes=num_modes, model_name=model_name, in_channels=in_channels, **flags.model_kwargs) elif "resnest" in model_name: print("Building ResNeStMulti") base_model = ResNeStMulti( cfg, num_modes=num_modes, model_name=model_name, in_channels=in_channels, **flags.model_kwargs) elif model_name in pretrainedmodels.__dict__.keys(): print("Building PretrainedCNNMulti") base_model = PretrainedCNNMulti( cfg, num_modes=num_modes, model_name=model_name, in_channels=in_channels, **flags.model_kwargs) elif model_name in timm.list_models(): print("Building TimmMulti") base_model = TimmMulti( cfg, in_channels=in_channels, num_modes=num_modes, backbone=model_name, **flags.model_kwargs ) else: raise ValueError(f"[ERROR] Unexpected value model_name={model_name}") predictor = LyftMultiModelPredictor(base_model, cfg, num_modes=num_modes, target_scale=target_scale) # --- Forward once to initialize lazy params --- bs = 2 height, width = cfg["raster_params"]["raster_size"] in_channels = predictor.in_channels x = torch.rand((bs, in_channels, height, width), dtype=torch.float32).to(device) predictor.to(device) if flags.feat_mode == "agent_type": feat_channels = flags.model_kwargs["feat_channels"] x_feat = torch.rand((bs, feat_channels), dtype=torch.float32).to(device) predictor(x, x_feat) else: predictor(x) # --- Done --- return predictor
def generate_model(self, model_name: str, in_chans: int): if isinstance(model_name, str): model_enum = ModelsAvailable[model_name.lower()] if model_enum.value in timm.list_models(pretrained=True): extras = dict(in_chans=in_chans) self.model = timm.create_model(model_enum.value, pretrained=True, num_classes=10, **extras) elif model_enum == ModelsAvailable.alexnet: self.model = AlexNet(in_chans=in_chans) elif model_enum == ModelsAvailable.googlenet: self.model = GoogleNet(in_chans=in_chans)
def _get_backbone_model(project_parameters): if project_parameters.backbone_model in timm.list_models(): backbone_model = timm.create_model( model_name=project_parameters.backbone_model, pretrained=True, num_classes=project_parameters.num_classes, in_chans=1) elif '.py' in project_parameters.backbone_model: backbone_model = _get_backbone_model_from_file( filepath=project_parameters.backbone_model, num_classes=project_parameters.num_classes) else: assert False, 'please check the backbone model. the backbone model: {}'.format( project_parameters.backbone_model) return backbone_model
def build_backbone_from_cfg(config) -> Tuple[torch.nn.Module, int]: args = config.copy() backbone_type_name = args.pop('type') if hasattr(Backbones, backbone_type_name): backbone = getattr(Backbones, backbone_type_name)(**args) elif backbone_type_name in pretrainedmodels.__dict__: backbone = pretrainedmodels.__dict__[backbone_type_name](**args) backbone.forward = backbone.features elif backbone_type_name in timm.list_models(): backbone = timm.create_model(backbone_type_name, **args) backbone.forward = backbone.forward_features elif hasattr(segmentation_models_pytorch, backbone_type_name): backbone = getattr(segmentation_models_pytorch, backbone_type_name)(**args) else: assert False, f'{backbone_type_name} not found in backbones factory' return backbone
def get_model(config): all_model_list = [f[:-3] for f in os.listdir(os.path.dirname(__file__))] if 'timm_' in config.model_name: print(f"Using pretrained timm model {config.model_name}") from .timm_created_models import Model timm_all_pretrained_model_names = timm.list_models(pretrained=True) model_name = config.model_name[5:] assert model_name in timm_all_pretrained_model_names, "sry, this model is not in timm pretrained model list" model = Model(model_name, pretrained=config.timm_pretrained) elif config.model_name in all_model_list: print(f"Using model {config.model_name}") module = importlib.import_module(f".{config.model_name}", package="models") model = module.Model(config) # raise NotImplementedError("Please define your own model here!") else: raise NotImplementedError( f"Not find a proper model={config.model_name}! Please check your configs file." ) return model
def build_backbone_from_cfg(config) -> Tuple[torch.nn.Module, int]: args = config.copy() backbone_type_name = args.pop('type') if hasattr(Backbones, backbone_type_name): backbone = getattr(Backbones, backbone_type_name)(**args) output_channels = backbone.output_channels elif backbone_type_name in pretrainedmodels.__dict__: backbone = pretrainedmodels.__dict__[backbone_type_name](**args) if 'squeezenet' in backbone_type_name: backbone = backbone.features output_channels = 512 else: backbone.forward = backbone.features output_channels = backbone.last_linear.in_features elif backbone_type_name in timm.list_models(): backbone = timm.create_model(backbone_type_name, **args) backbone.forward = backbone.forward_features output_channels = backbone.classifier.in_features else: assert False, f'{backbone_type_name} not found in backbones factory' return backbone, output_channels
def _get_supported_models(): try: import mxnet as _mxnet except ImportError: _mxnet = None if _mxnet is not None: from gluoncv.model_zoo import get_model_list all_models = get_model_list() blacklist = [ 'ssd', 'faster_rcnn', 'mask_rcnn', 'fcn', 'deeplab', 'psp', 'icnet', 'fastscnn', 'danet', 'yolo', 'pose', 'center_net', 'siamrpn', 'monodepth', 'ucf101', 'kinetics', 'voc', 'coco', 'citys', 'mhpv1', 'ade', 'hmdb51', 'sthsth', 'otb' ] cls_models = [ m for m in all_models if not any(x in m for x in blacklist) ] else: cls_models = [] # add timm backend supported models try: import torch as _torch except ImportError: _torch = None try: import timm as _timm except ImportError: _timm = None if _timm is not None: cls_models += list(_timm.list_models()) elif _torch is None: logger.warning('timm installed but torch is required to enable it.') else: logger.warning( 'cannot import timm, possibly due to missing torchvision') return cls_models
def __init__(self, name="mobilenetv2_100",num_classes=21,pretrained="", pretrained_backbone=True,sc=False,filter_multiplier=1.0): super(Deeplab3P,self).__init__() output_stride = 16 num_filters = int(256*filter_multiplier) num_low_filters = int(48*filter_multiplier) try: self.backbone=timm.create_model(name, features_only=True, output_stride=output_stride, out_indices=(1, 4),pretrained=pretrained_backbone and pretrained =="") except RuntimeError: print("no model") print(timm.list_models()) raise RuntimeError() channels=self.backbone.feature_info.channels() self.head16=get_ASSP(channels[1], output_stride,num_filters) self.head4=torch.nn.Sequential( nn.Conv2d(channels[0], num_low_filters, 1, bias=False), nn.BatchNorm2d(num_low_filters), nn.ReLU(inplace=True)) self.decoder= nn.Sequential( nn.Conv2d(num_low_filters+num_filters, num_filters, 3, padding=1, bias=False), nn.BatchNorm2d(num_filters), nn.ReLU(inplace=True), nn.Conv2d(num_filters, num_filters, 3, padding=1, bias=False), nn.BatchNorm2d(num_filters), nn.ReLU(inplace=True), nn.Conv2d(num_filters, num_classes, 1) ) if sc: self.decoder = convert_to_separable_conv(self.decoder) if pretrained != "": dic = torch.load(pretrained, map_location='cpu') if type(dic)==dict: self.load_state_dict(dic['model']) else: self.load_state_dict(dic)
def _train_image_classification(args, reporter): """ Parameters ---------- args: <class 'autogluon.utils.edict.EasyDict'> """ tic = time.time() try: task_id = int(args.task_id) except: task_id = 0 problem_type = args.pop('problem_type', MULTICLASS) final_fit = args.pop('final_fit', False) # train, val data train_data = args.pop('train_data') val_data = args.pop('val_data') # wall clock tick limit wall_clock_tick = args.pop('wall_clock_tick') log_dir = args.pop('log_dir', os.getcwd()) # exponential batch size for Int() space batch sizes try: exp_batch_size = args.pop('exp_batch_size') except AttributeError: exp_batch_size = False if exp_batch_size and 'batch_size' in args: args['batch_size'] = 2 ** args['batch_size'] try: task = args.pop('task') dataset = args.pop('dataset') num_trials = args.pop('num_trials') except AttributeError: task = None # mxnet and torch dispatcher dispatcher = None torch_model_list = None mxnet_model_list = None custom_net = None if args.get('custom_net', None): custom_net = args.get('custom_net') if torch and timm: if isinstance(custom_net, torch.nn.Module): dispatcher = 'torch' if mx: if isinstance(custom_net, mx.gluon.Block): dispatcher = 'mxnet' else: if torch and timm: torch_model_list = timm.list_models() if mx: mxnet_model_list = list(get_model_list()) model = args.get('model', None) if model: # timm model has higher priority if torch_model_list and model in torch_model_list: dispatcher = 'torch' elif mxnet_model_list and model in mxnet_model_list: dispatcher = 'mxnet' else: if not torch_model_list: raise ValueError('Model not found in gluoncv model zoo. Install torch and timm if it supports the model.') elif not mxnet_model_list: raise ValueError('Model not found in timm model zoo. Install mxnet if it supports the model.') else: raise ValueError('Model not supported because it does not exist in both timm and gluoncv model zoo.') assert dispatcher in ('torch', 'mxnet'), 'custom net needs to be of type either torch.nn.Module or mx.gluon.Block' args['estimator'] = TorchImageClassificationEstimator if dispatcher=='torch' else ImageClassificationEstimator # convert user defined config to nested form args = config_to_nested(args) if wall_clock_tick < tic and not final_fit: return {'traceback': 'timeout', 'args': str(args), 'time': 0, 'train_acc': -1, 'valid_acc': -1} try: valid_summary_file = 'fit_summary_img_cls.ag' estimator_cls = args.pop('estimator', None) assert estimator_cls in (ImageClassificationEstimator, TorchImageClassificationEstimator) if final_fit: # load from previous dumps estimator = None if os.path.isdir(log_dir): is_valid_dir_fn = lambda d : d.startswith('.trial_') and os.path.isdir(os.path.join(log_dir, d)) trial_dirs = [d for d in os.listdir(log_dir) if is_valid_dir_fn(d)] best_checkpoint = '' best_acc = -1 result = {} for dd in trial_dirs: try: with open(os.path.join(log_dir, dd, valid_summary_file), 'r') as f: result = json.load(f) acc = result.get('valid_acc', -1) if acc > best_acc and os.path.isfile(os.path.join(log_dir, dd, _BEST_CHECKPOINT_FILE)): best_checkpoint = os.path.join(log_dir, dd, _BEST_CHECKPOINT_FILE) best_acc = acc except: pass if best_checkpoint: estimator = estimator_cls.load(best_checkpoint) if estimator is None: if wall_clock_tick < tic: result.update({'traceback': 'timeout'}) else: # unknown error yet, try reproduce it final_fit = False if not final_fit: # create independent log_dir for each trial trial_log_dir = os.path.join(log_dir, '.trial_{}'.format(task_id)) args['log_dir'] = trial_log_dir custom_optimizer = args.pop('custom_optimizer', None) estimator = estimator_cls(args, problem_type=problem_type, reporter=reporter, net=custom_net, optimizer=custom_optimizer) # training result = estimator.fit(train_data=train_data, val_data=val_data, time_limit=wall_clock_tick-tic) with open(os.path.join(trial_log_dir, valid_summary_file), 'w') as f: json.dump(result, f) # save config and result if task is not None: trial_log = {} trial_log.update(args) trial_log.update(result) json_str = json.dumps(trial_log) time_str = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()) json_file_name = task + '_dataset-' + dataset + '_trials-' + str(num_trials) + '_' + time_str + '.json' with open(json_file_name, 'w') as json_file: json_file.write(json_str) logging.info('Config and result in this trial have been saved to %s.', json_file_name) except: import traceback return {'traceback': traceback.format_exc(), 'args': str(args), 'time': time.time() - tic, 'train_acc': -1, 'valid_acc': -1} if estimator: try: result.update({'model_checkpoint': pickle.dumps(estimator)}) except pickle.PicklingError: result.update({'model_checkpoint': estimator}) result.update({'estimator': estimator_cls}) return result
paper_arxiv_id='1512.00567'), tf_mixnet_l=_attrib(paper_model_name='MixNet-L', paper_arxiv_id='1907.09595'), tf_mixnet_m=_attrib(paper_model_name='MixNet-M', paper_arxiv_id='1907.09595'), tf_mixnet_s=_attrib(paper_model_name='MixNet-S', paper_arxiv_id='1907.09595'), #tv_resnet34=_attrib(paper_model_name=, paper_arxiv_id=), # same weights as torchvision #tv_resnet50=_attrib(paper_model_name=, paper_arxiv_id=), # same weights as torchvision #tv_resnext50_32x4d=_attrib(paper_model_name=, paper_arxiv_id=), # same weights as torchvision #wide_resnet50_2=_attrib(paper_model_name=, paper_arxiv_id=), # same weights as torchvision #wide_resnet101_2=_attrib(paper_model_name=, paper_arxiv_id=), # same weights as torchvision xception=_attrib(paper_model_name='Xception', paper_arxiv_id='1610.02357'), ) model_names = list_models(pretrained=True) for model_name in model_names: if model_name not in model_map: print('Skipping %s' % model_name) continue # create model from name model = create_model(model_name, pretrained=True) param_count = sum([m.numel() for m in model.parameters()]) print('Model %s created, param count: %d' % (model_name, param_count)) # get appropriate transform for model's default pretrained config data_config = resolve_data_config(dict(), model=model, verbose=True) input_transform = create_transform(**data_config)
import pytest import torch from timm import list_models, create_model @pytest.mark.timeout(300) @pytest.mark.parametrize('model_name', list_models()) @pytest.mark.parametrize('batch_size', [1]) def test_model_forward(model_name, batch_size): """Run a single forward pass with each model""" model = create_model(model_name, pretrained=False) model.eval() inputs = torch.randn((batch_size, *model.default_cfg['input_size'])) outputs = model(inputs) assert outputs.shape[0] == batch_size assert not torch.isnan(outputs).any(), 'Output included NaNs'
def __post_init__(self): available_models = timm.list_models(pretrained=True) assert self.model in available_models
# no need for the fusion performance here torch._C._jit_set_profiling_executor(True) torch._C._jit_set_profiling_mode(False) if 'GITHUB_ACTIONS' in os.environ: # and 'Linux' in platform.system(): # GitHub Linux runner is slower and hits memory limits sooner than MacOS, exclude bigger models EXCLUDE_FILTERS = ['*efficientnet_l2*', '*resnext101_32x48d', 'vit_*'] else: EXCLUDE_FILTERS = ['vit_*'] MAX_FWD_SIZE = 384 MAX_BWD_SIZE = 128 MAX_FWD_FEAT_SIZE = 448 @pytest.mark.timeout(120) @pytest.mark.parametrize('model_name', list_models(exclude_filters=EXCLUDE_FILTERS)) @pytest.mark.parametrize('batch_size', [1]) def test_model_forward(model_name, batch_size): """Run a single forward pass with each model""" model = create_model(model_name, pretrained=False) model.eval() input_size = model.default_cfg['input_size'] if any([x > MAX_FWD_SIZE for x in input_size]): # cap forward test at max res 448 * 448 to keep resource down input_size = tuple([min(x, MAX_FWD_SIZE) for x in input_size]) inputs = torch.randn((batch_size, *input_size)) outputs = model(inputs) assert outputs.shape[0] == batch_size assert not torch.isnan(outputs).any(), 'Output included NaNs'
pretrained: bool = True) -> Tuple[nn.Module, int]: model: nn.Module = getattr(torchvision.models, model_name, None)(pretrained) backbone = nn.Sequential(*model.features, nn.ReLU(inplace=True)) num_features = model.classifier.in_features return backbone, num_features IMAGE_CLASSIFIER_BACKBONES(fn=catch_url_error( partial(_fn_densenet, model_name)), name=model_name, namespace="vision", package="torchvision", type="densenet") if _TIMM_AVAILABLE: for model_name in timm.list_models(): if model_name in TORCHVISION_MODELS: continue def _fn_timm( model_name: str, pretrained: bool = True, num_classes: int = 0, global_pool: str = '', ) -> Tuple[nn.Module, int]: backbone = timm.create_model(model_name, pretrained=pretrained, num_classes=num_classes, global_pool=global_pool) num_features = backbone.num_features
"densenet161", "googlenet", "mobilenet_v2", "mobilenet_v3_large", "mobilenet_v3_small", "mnasnet0_5", "mnasnet0_75", "mnasnet1_0", "mnasnet1_3", "shufflenet_v2_x0_5", "shufflenet_v2_x1_0", "shufflenet_v2_x1_5", "shufflenet_v2_x2_0", ] tv_models = set(MODEL_NAMES) - set(timm.list_models()) def make_wrapper_func(wrapper_fn_name, model_name_key): @MODEL_WRAPPER_REGISTRY.register(model_name=model_name_key, dataset_name='imagenet', task_type='classification') def wrapper_func(pretrained=False, progress=True, device="cuda", num_classes=1000): model = torchvision.models.__dict__[model_name_key]( pretrained=pretrained, num_classes=num_classes) return model.to(device) wrapper_func.__name__ = wrapper_fn_name
def _train_image_classification(args, train_data, val_data, problem_type, wall_clock_tick, log_dir, reporter=None): """ Parameters ---------- args: <class 'autogluon.utils.edict.EasyDict'> """ tic = time.time() args = args.copy() try: task_id = int(args['task_id']) except: task_id = 0 final_fit = args.pop('final_fit', False) # exponential batch size for Int() space batch sizes exp_batch_size = args.pop('exp_batch_size', False) if exp_batch_size and 'batch_size' in args: args['batch_size'] = 2 ** args['batch_size'] # mxnet and torch dispatcher dispatcher = None torch_model_list = None mxnet_model_list = None custom_net = None if args.get('custom_net', None): custom_net = args.get('custom_net') if torch and timm: if isinstance(custom_net, torch.nn.Module): dispatcher = 'torch' if mx: if isinstance(custom_net, mx.gluon.Block): dispatcher = 'mxnet' else: if torch and timm: torch_model_list = timm.list_models() if mx: mxnet_model_list = list(get_model_list()) model = args.get('model', None) if model: # timm model has higher priority if torch_model_list and model in torch_model_list: dispatcher = 'torch' elif mxnet_model_list and model in mxnet_model_list: dispatcher = 'mxnet' else: if not torch_model_list: raise ValueError('Model not found in gluoncv model zoo. Install torch and timm if it supports the model.') elif not mxnet_model_list: raise ValueError('Model not found in timm model zoo. Install mxnet if it supports the model.') else: raise ValueError('Model not supported because it does not exist in both timm and gluoncv model zoo.') assert dispatcher in ('torch', 'mxnet'), 'custom net needs to be of type either torch.nn.Module or mx.gluon.Block' if dispatcher == 'mxnet': logger.log(30, '=============================================================================\n' 'WARNING: Using MXNet models in ImagePredictor is deprecated as of v0.4.0 and may contain various bugs and issues!\n' 'In v0.6.0, ImagePredictor will no longer support training MXNet models. Please consider switching to specifying Torch models instead.\n' 'Users should ensure they update their code that depends on ImagePredictor when upgrading to future AutoGluon releases.\n' 'For more information, refer to this GitHub issue: https://github.com/awslabs/autogluon/issues/1560\n' '=============================================================================\n') args['estimator'] = TorchImageClassificationEstimator if dispatcher=='torch' else ImageClassificationEstimator # convert user defined config to nested form args = config_to_nested(args) if wall_clock_tick < tic and not final_fit: return {'traceback': 'timeout', 'args': str(args), 'time': 0, 'train_acc': -1, 'valid_acc': -1} try: valid_summary_file = 'fit_summary_img_cls.ag' estimator_cls = args.pop('estimator', None) assert estimator_cls in (ImageClassificationEstimator, TorchImageClassificationEstimator) if final_fit: # load from previous dumps estimator = None if os.path.isdir(log_dir): is_valid_dir_fn = lambda d : d.startswith('.trial_') and os.path.isdir(os.path.join(log_dir, d)) trial_dirs = [d for d in os.listdir(log_dir) if is_valid_dir_fn(d)] best_checkpoint = '' best_acc = -1 result = {} for dd in trial_dirs: try: with open(os.path.join(log_dir, dd, valid_summary_file), 'r') as f: result = json.load(f) acc = result.get('valid_acc', -1) if acc > best_acc and os.path.isfile(os.path.join(log_dir, dd, _BEST_CHECKPOINT_FILE)): best_checkpoint = os.path.join(log_dir, dd, _BEST_CHECKPOINT_FILE) best_acc = acc except: pass if best_checkpoint: estimator = estimator_cls.load(best_checkpoint) if estimator is None: if wall_clock_tick < tic: result.update({'traceback': 'timeout'}) else: # unknown error yet, try reproduce it final_fit = False if not final_fit: # create independent log_dir for each trial trial_log_dir = os.path.join(log_dir, '.trial_{}'.format(task_id)) args['log_dir'] = trial_log_dir custom_optimizer = args.pop('custom_optimizer', None) estimator = estimator_cls(args, problem_type=problem_type, reporter=reporter, net=custom_net, optimizer=custom_optimizer) # training result = estimator.fit(train_data=train_data, val_data=val_data, time_limit=wall_clock_tick-tic) with open(os.path.join(trial_log_dir, valid_summary_file), 'w') as f: json.dump(result, f) except: import traceback return {'traceback': traceback.format_exc(), 'args': str(args), 'time': time.time() - tic, 'train_acc': -1, 'valid_acc': -1} if estimator: result.update({'model_checkpoint': estimator}) result.update({'estimator': estimator_cls}) return result
from timm import list_models, create_model, set_scriptable if 'GITHUB_ACTIONS' in os.environ and 'Linux' in platform.system(): # GitHub Linux runner is slower and hits memory limits sooner than MacOS, exclude bigger models EXCLUDE_FILTERS = ['*efficientnet_l2*', '*resnext101_32x48d'] else: EXCLUDE_FILTERS = [] MAX_FWD_SIZE = 384 MAX_BWD_SIZE = 128 MAX_FWD_FEAT_SIZE = 448 @pytest.mark.timeout(120) @pytest.mark.parametrize('model_name', list_models(exclude_filters=EXCLUDE_FILTERS)) @pytest.mark.parametrize('batch_size', [1]) def test_model_forward(model_name, batch_size): """Run a single forward pass with each model""" model = create_model(model_name, pretrained=False) model.eval() input_size = model.default_cfg['input_size'] if any([x > MAX_FWD_SIZE for x in input_size]): # cap forward test at max res 448 * 448 to keep resource down input_size = tuple([min(x, MAX_FWD_SIZE) for x in input_size]) inputs = torch.randn((batch_size, *input_size)) outputs = model(inputs) assert outputs.shape[0] == batch_size assert not torch.isnan(outputs).any(), 'Output included NaNs'
# Pytorch Image Model # https://rwightman.github.io/pytorch-image-models/ import timm from timm import create_model class Timm_model(nn.Module): def __init__(self, model_name, pretrained=True, out_dim=5): super(Timm_model, self).__init__() self.base = create_model(model_name, pretrained=pretrained) if 'efficientnet' in model_name: self.base.classifier = nn.Linear( in_features=self.base.classifier.in_features, out_features=out_dim) elif 'vit' in model_name: self.base.head = nn.Linear(in_features=self.base.head.in_features, out_features=out_dim) else: self.base.fc = nn.Linear(in_features=self.base.fc.in_features, out_features=out_dim) def forward(self, x): return self.base(x) if __name__ == '__main__': # Print Timm Models model_names = timm.list_models(pretrained=True) print(model_names)
2) + " |\n" table = l1 + top + l2 for k in sorted(data.keys()): support = "✅".center( max_len2 - 3) if data[k]["has_dilation"] else " ".center(max_len2 - 2) table += "| " + k.ljust(max_len1 - 2) + " | " + support + " |\n" table += l1 return table if __name__ == "__main__": supported_models = {} with tqdm(timm.list_models()) as names: for name in names: try: check_features_and_reduction(name) has_dilation = has_dilation_support(name) supported_models[name] = dict(has_dilation=has_dilation) except Exception: continue table = make_table(supported_models) print(table) print(f"Total encoders: {len(supported_models.keys())}")
def get_model_names(): return sorted( name for name in models.__dict__ if name.islower() and not name.startswith("__") and callable(models.__dict__[name]) ) + timm.list_models()
if fixed_input_size: return input_size if min_input_size: if target and max(input_size) > target: input_size = min_input_size else: if target and max(input_size) > target: input_size = tuple([min(x, target) for x in input_size]) return input_size @pytest.mark.timeout(120) @pytest.mark.parametrize('model_name', list_models(exclude_filters=EXCLUDE_FILTERS)) @pytest.mark.parametrize('batch_size', [1]) def test_model_forward(model_name, batch_size): """Run a single forward pass with each model""" model = create_model(model_name, pretrained=False) model.eval() input_size = _get_input_size(model=model, target=TARGET_FWD_SIZE) if max(input_size) > MAX_FWD_SIZE: pytest.skip("Fixed input size model > limit.") inputs = torch.randn((batch_size, *input_size)) outputs = model(inputs) assert outputs.shape[0] == batch_size assert not torch.isnan(outputs).any(), 'Output included NaNs'
# GitHub Linux runner is slower and hits memory limits sooner than MacOS, exclude bigger models EXCLUDE_FILTERS = [ '*efficientnet_l2*', '*resnext101_32x48d', '*in21k', '*152x4_bitm', '*nfnet_f3*', '*nfnet_f4*', '*nfnet_f5*', '*nfnet_f6*', '*nfnet_f7*' ] + NON_STD_FILTERS else: EXCLUDE_FILTERS = NON_STD_FILTERS MAX_FWD_SIZE = 384 MAX_BWD_SIZE = 128 MAX_FWD_FEAT_SIZE = 448 @pytest.mark.timeout(120) @pytest.mark.parametrize( 'model_name', list_models(exclude_filters=EXCLUDE_FILTERS[:-NUM_NON_STD])) @pytest.mark.parametrize('batch_size', [1]) def test_model_forward(model_name, batch_size): """Run a single forward pass with each model""" model = create_model(model_name, pretrained=False) model.eval() input_size = model.default_cfg['input_size'] if any([x > MAX_FWD_SIZE for x in input_size]): # cap forward test at max res 448 * 448 to keep resource down input_size = tuple([min(x, MAX_FWD_SIZE) for x in input_size]) inputs = torch.randn((batch_size, *input_size)) outputs = model(inputs) assert outputs.shape[0] == batch_size assert not torch.isnan(outputs).any(), 'Output included NaNs'
import torch from torch import nn from torch.nn import * from torch.nn import functional as F from torchvision import models from pytorchcv.model_provider import get_model as ptcv_get_model # from .utils import get_cadene_model from typing import Optional from .utils import * from .triple_attention import * from losses.arcface import ArcMarginProduct import timm from pprint import pprint model_names = timm.list_models('*resnext*') print(model_names) # print(torch.hub.list('zhanghang1989/ResNeSt', force_reload=True)) class Resne_t(nn.Module): def __init__(self, model_name='resnest50_fast_1s1x64d', num_class=1): super().__init__() # self.backbone = timm.create_model(model_name, pretrained=True) # self.backbone = torch.hub.load('zhanghang1989/ResNeSt', model_name, pretrained=True) # print(self.backbone) self.backbone = timm.create_model(model_name, pretrained=True) # print(self.backbone) # self.in_features = 2048 self.in_features = self.backbone.fc.in_features self.head = Head(self.in_features, num_class, activation='mish')