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)
# def test(): # net = MobileNetV3_Small() # x = torch.randn(2, 3, 224, 224) # y = net(x) # print(y.size()) # test() if __name__ == '__main__': import xtils, os model = [MobileNetV3_Large(), MobileNetV3_Small()][0] print('\n', model, '\n') ckpt = ['mobilev3_x_large.pth.tar', 'mobilev3_x_small.pth.tar'][0] ckpt = os.path.join('/data/zhangjp/PreTrainedModels', ckpt) model = xtils.load_ckpt_weights(model, ckptf=ckpt, device='cpu', mgpus_to_sxpu='m2s') # utils.tensorboard_add_model(model, x) xtils.calculate_params_scale(model, format='million') xtils.calculate_FLOPs_scale(model, use_gpu=False, input_size=224, multiply_adds=True) xtils.calculate_layers_num(model, layers=('conv2d', 'deconv2d', 'fc')) xtils.calculate_time_cost(model, insize=224, 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)
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
model_path = os.path.join(model_dir, model_ckpt_map[arch]) model = xtils.load_ckpt_weights(model, model_path, device='cpu', mgpus_to_sxpu='auto') elif model_path == 'download': # model_url_map = {} # import torch.utils.model_zoo as model_zoo # model.load_state_dict(model_zoo.load_url(model_url_map['arch'], model_dir)) raise NotImplementedError else: assert model_path == '', '<model_path> must refer to "local" or "download" or "" or "model.ckpt".' return model if __name__ == '__main__': import xtils import time, os # import xmodels.tvm_densenet as dense # model = dense.densenet169() # 14.15M 3.40G 169L 1.42s # model = [fishnet99(), fishnet150(), fishnet201()][0] model = FISHNets(**fish150) print(model) # utils.tensorboard_add_model(model, x) xtils.calculate_params_scale(model, format='million') xtils.calculate_FLOPs_scale(model, input_size=224) xtils.calculate_layers_num(model, layers=('conv2d', 'deconv2d', 'fc')) xtils.calculate_time_cost(model, insize=224, toc=1, use_gpu=False, pritout=True)