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)
return model def preresnet110(num_classes=10): """Constructs a ResNet-110 model for CIFAR-10 (by default) Args: num_classes (uint): number of classes """ model = CifarPreResNet(ResNetBasicblock, 110, num_classes) return model def PreResNet(block=ResNetBasicblock, depth=20, num_classes=10): return CifarPreResNet(block, depth, num_classes) if __name__ == '__main__': import xtils # model = CifarPreResNet(ResNetBasicblock, 20, 10) model = preresnet20(10) # model = resnet50(False) # model = densenet121(False) print(model) xtils.calculate_layers_num(model, layers=('conv2d', 'classifier')) xtils.calculate_FLOPs_scale(model, input_size=32, multiply_adds=True, use_gpu=False) xtils.calculate_params_scale(model, format='million')
model = MultiScaleNet(**ms6) print(model) model.set_train_which(part=['bone+mhead', 'auxhead', 'summary'][2], name_which='head-8-2-2@6') # model.set_eval_which(part=['bone+mhead', 'bone+mhead+auxhead', 'bone+mhead+auxhead+summary'][1]) # paramsx = [p for p in model.parameters() if p.requires_grad] # # model.set_train_which(part=['bone+mhead', 'auxhead', 'summary'][0]) # paramsy = [p for p in model.parameters() if p.requires_grad] # # share = list(set(paramsx).intersection(set(paramsy))) insize = [32, 224][model.dataset == 'imagenet'] x = torch.randn(1, 3, insize, insize) z = model(x) print('z-->', len(z), '\n') xtils.calculate_layers_num(model) xtils.calculate_FLOPs_scale(model, input_size=insize, use_gpu=False, multiply_adds=False) xtils.calculate_params_scale(model, format='million') xtils.calculate_time_cost(model, insize=insize, toc=1, pritout=True, use_gpu=False)
'NUM_BRANCHES': 4, 'BLOCK': 'BASIC', 'NUM_BLOCKS': [4, 4, 4, 4], 'NUM_CHANNELS': [48, 96, 192, 384], 'FUSE_METHOD': 'SUM' } } } } # 测试结果 # hrw18 [email protected] [email protected] if __name__ == '__main__': import xtils hrarch = [hrw18, hrw30, hrw32, hrw40, hrw44, hrw48][0] # model = get_cls_hrnet(hrarch) # model = HighResolutionNet(**{'cfg': hrarch}) model = HRNets(**hrarch) print(model) xtils.calculate_layers_num(model, layers=('conv2d', 'linear')) xtils.calculate_FLOPs_scale(model, input_size=224, multiply_adds=False) xtils.calculate_params_scale(model, format='million') xtils.calculate_time_cost(model, insize=224, use_gpu=False, toc=1, pritout=True)
elif netis == 'mobilev3': model = MobileV3(**mbvdl) elif netis == 'fishnet': # fish99: 16.63M 4.31G 100L 693M 0.01121s # fish150: 24.96M 6.45G 151L 804M 0.01696S model = [fishnet99(), fishnet150(), fishnet201()][0] elif netis == 'hrnet': model = HRNets(**hrw18) elif netis == 'effnet': model = EFFNets(**effb3) else: raise NotImplementedError xtils.calculate_params_scale(model, format='million') xtils.calculate_FLOPs_scale(model, input_size=224, multiply_adds=True) xtils.calculate_layers_num(model, layers=['conv2d', 'deconv2d', 'linear']) model = model.to(device) model.eval() bn, c, h, w = 1, 3, 224, 224 x = torch.Tensor(bn, c, h, w) x = x.to(device) x.requires_grad = False N = 2000 tic = time.time() for i in range(N): out = model(x) toc = time.time() - tic
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