def model_factory(arch_name, arch_kwargs, dataset, with_info=False): model = None if dataset.startswith('cifar'): if arch_name.startswith('resnet'): model = getattr(resnet_cifar, arch_name)(**arch_kwargs) elif arch_name.startswith('densenet'): model = CifarDenseNet(**arch_kwargs) elif arch_name.startswith('preresnet'): model = PreResNet(**arch_kwargs) else: try: model = getattr(init_model, arch_name) except AttributeError: raise AttributeError('未找到模型: <%s>, 请检查是否已将该模型注册到 <xmodel._init_model> .' % arch_name) model = model(**arch_kwargs) elif dataset.startswith('imagenet'): # 导入ImageNet模型,只需在 xmodel._init_model 中添加自己的模型即可,无需在此处添加循环分支 try: model = getattr(init_model, arch_name) except AttributeError: raise AttributeError('未找到模型: <%s>, 请检查是否已将该模型注册到 <xmodel._init_model> .' % arch_name) model = model(**arch_kwargs) if model is None: raise NotImplementedError('Unkown <arch_name:%s> for <dataset:%s>, ' 'check mdoel_factory.py' % (arch_name, dataset)) if with_info: params = xtils.calculate_params_scale(model, format='million') insize = 224 if dataset == 'imagenet' else 32 gflops = xtils.calculate_FLOPs_scale(model, input_size=insize, multiply_adds=True, use_gpu=False) depth = xtils.calculate_layers_num(model, layers=('conv2d', 'deconv2d', 'fc')) if with_info == 'return': return model, params, gflops, depth return model
'last_branch': 1, 'last_down': True, 'last_expand': 0, 'poolmode': 'avg', 'active': 'relu', 'summer': 'split', 'nclass': 10, 'afisok': False, 'afkeys': ('af1', 'af2'), 'convon': True, 'convlayers': 1, 'convdepth': 4} wv2s = {'stages': 2, 'rock': 'Q', 'branch': 3, 'indepth': 24, 'growth': 12, 'multiway': 4, 'layers': (3, 3, 0), 'blocks': ('D', 'D', '-'), 'bottle': (3, 3, 3), 'classify': (0, 0, 0), 'trans': ('A', 'A', '-'), 'reduction': (0.5, 0, 0), 'last_branch': 1, 'last_down': True, 'last_expand': 0, 'poolmode': 'avg', 'active': 'relu', 'summer': 'split', 'nclass': 10, 'afisok': False, 'afkeys': ('af1', 'af2'), 'convon': True, 'convlayers': 1, 'convdepth': 4} wv3s = {'stages': 3, 'rock': 'Q', 'branch': 3, 'indepth': 24, 'growth': 12, 'multiway': 4, 'layers': (10, 10, 10), 'blocks': ('D', 'D', 'S'), 'bottle': (0, 0, 0), 'classify': (0, 0, 0), 'trans': ('A', 'A', 'A'), 'reduction': (0.5, 0.5, 0), 'last_branch': 1, 'last_down': False, 'last_expand': 10, 'poolmode': 'avg', 'active': 'relu', 'summer': 'split', 'nclass': 10, 'afisok': False, 'afkeys': ('af1', 'af2', 'af3'), 'convon': True, 'convlayers': 1, 'convdepth': 4} model = WaveNet(**wv3s) print('\n', model, '\n') out = model(torch.randn(4, 3, 32, 32)) # utils.tensorboard_add_model(model, x) xtils.calculate_params_scale(model, format='million') xtils.calculate_FLOPs_scale(model, input_size=32, use_gpu=False, multiply_adds=False) xtils.calculate_layers_num(model, layers=('conv2d', 'deconv2d', 'fc')) xtils.calculate_time_cost(model, insize=32, toc=3, use_gpu=False)
""" arch_name_list = [ 'resnet18-5c106cde.pth', 'resnet34-333f7ec4.pth', 'resnet50-19c8e357.pth', 'resnet101-5d3b4d8f.pth', 'resnet152-b121ed2d.pth', 'densenet121-a639ec97.pth', 'densenet169-b2777c0a.pth', 'densenet201-c1103571.pth', 'densenet161-8d451a50.pth', ] arch_name = [name for name in arch_name_list if name.startswith(arch_name)] if len(arch_name) == 1: arch_name = arch_name[0] elif len(arch_name) > 1: raise Warning('too much choices for %s ... !' % arch_name) else: raise Warning('no checkpoint exist ... !') model_path = os.path.join(model_dir, arch_name) return model_path if __name__ == '__main__': import xtils # model = models.resnet50() model = models.densenet169() print(model) xtils.calculate_layers_num(model) xtils.calculate_params_scale(model, 'million')
def model_factory(arch_name, arch_kwargs, dataset, with_info=False): model = None if arch_name.startswith('resnet'): if dataset == 'cifar': model = getattr(resnet_cifar, arch_name)(**arch_kwargs) elif dataset == 'imagenet': # model = getattr(tvmodels, arch_name)(**arch_kwargs) # pretrained model_url model = getattr(tvm_resnet, arch_name)(**arch_kwargs) # pretrained model_path elif arch_name.startswith('densenet'): if dataset == 'cifar': model = CifarDenseNet(**arch_kwargs) elif dataset == 'imagenet': # model = getattr(tvmodels, arch_name)(**arch_kwargs) # pretrained model_url model = getattr(tvm_densenet, arch_name)(**arch_kwargs) # pretrained model_path elif arch_name.startswith('vgg'): if dataset == 'imagenet': # model = getattr(tvmodels, arch_name)(**arch_kwargs) # pretrained model_url model = getattr(tvm_vggs, arch_name)(**arch_kwargs) elif arch_name.startswith('preresnet'): if dataset == 'cifar10': model = PreResNet(**arch_kwargs) elif arch_name.startswith('fishnet'): if dataset == 'imagenet': model = getattr(fishnet, arch_name)(**arch_kwargs) # pretrained model_path elif arch_name.startswith('mobilev3'): if dataset == 'imagenet': model = MobileV3(**arch_kwargs) # pretrained model_path # 导入自定义模型,只需在 xmodel._init_model 中添加自己的模型即可,无需在此处添加循环分支 else: try: model = getattr(init_model, arch_name) except AttributeError: raise AttributeError( '未找到模型: <%s>, 请检查是否已将该模型注册到 <xmodel._init_model> .' % arch_name) model = model(**arch_kwargs) if model is None: raise NotImplementedError('Unkown <arch_name:%s> for <dataset:%s>, ' 'check mdoel_factory.py' % (arch_name, dataset)) if with_info: # print(model) parameters = xtils.calculate_params_scale(model, format='million') if dataset == 'imagenet' and True: gflops = xtils.calculate_FLOPs_scale(model, input_size=224, multiply_adds=True, use_gpu=False) model_depth = xtils.calculate_layers_num(model, layers=('conv2d', 'deconv2d', 'fc')) return model, parameters, model_depth return model