예제 #1
0
파일: fishnet.py 프로젝트: zhjpqq/scalenet
def FISHNets(arch='fish99', cfg='', model_path='', **kwargs):
    """
    自定义接口 for model_factory
    指定arch=官方模型,即可从map中调用官方模型的名称name,配置cfg,以及预训练参数ckpt
    指定arch=自定义模型,即可使用传入的模型名称name,配置cfg,以及预训练参数ckpt
    """
    model_name_map = {
        'fish99': 'fishnet99',
        'fish150': 'fishnet150',
        'fish201': 'fishnet201',
    }
    model_cfg_map = {
        'fish99': fish99_cfg,
        'fish150': fish150_cfg,
        'fish201': fish201_cfg,
    }
    model_ckpt_map = {
        'fish99': 'fishnet99_ckpt.tar',
        'fish150': 'fishnet150_ckpt.tar',
        'fish201': 'fishnet20x_ckpt_welltrain-==fishnet201.tar',
    }

    try:
        # 调用官方模型
        name = model_name_map[arch]
    except:
        # 使用自定义模型,如fish33, fish55
        name = arch

    if cfg == '':
        # 调用官方配置
        cfg = model_cfg_map[arch]
    else:
        # 使用自定义配置
        assert isinstance(cfg, dict)
        cfg = cfg

    cfg = {**cfg, **kwargs}
    model = FishNet(Bottleneck, **cfg)

    model_dir = xtils.get_pretrained_models()

    if os.path.isfile(model_path):
        model = xtils.load_ckpt_weights(model, model_path, device='cpu', mgpus_to_sxpu='auto')
    elif model_path == 'local':
        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
예제 #2
0
class Config(object):
    gpu_ids = [0, 1, 2, 3]

    device = torch.device('cuda:{}'.format(gpu_ids[0]))

    dataset = 'imagenet'

    data_root = xtils.get_data_root(data='imagenet')

    data_augment = {'train': 'rotate-rresize-1crop', 'val': '1resize-1crop',
                    'imsize': 256, 'insize': 224, 'color': True,
                    'degree': (0, 0), 'scale': (0.08, 1), 'ratio': (3. / 4, 4. / 3)}

    arch_name = ['hrnet', 'resnet50', 'resnet152', 'fishnet150', 'scalenet'][-1]

    arch_kwargs = [hrw18, hrw30, hrw32, vo69, vo72][-2]

    ckpt_file = os.path.join(xtils.get_pretrained_models(),
                             ['hrnetv2_w18_imagenet_pretrained.pth',
                              'fishnet150_ckpt.tar',
                              'resnet50-19c8e357.pth', 'resnet152-b121ed2d.pth',
                              'scale103-5.09M-71.63-vo69.pth',
                              'scale59-30.52M-74.86-vo72.pth'][-2])

    mgpus_to_sxpu = ['m2s', 's2m', 'none'][-1]

    image_size = 256

    input_size = int(0.875 * image_size)

    bsize_val = 256

    num_workers = 8

    print_freq = 20

    valid_total_time = 0

    random_seed = random.randint(0, 100) or None

    # 特殊模型的补充字段
    mode_custom = False
    xfc_which = -1
예제 #3
0
파일: tvm_vggs.py 프로젝트: zhjpqq/scalenet
def vgg19_bn(pretrained=False, **kwargs):
    """VGG 19-layer model (configuration 'E') with batch normalization

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    if pretrained:
        kwargs['init_weights'] = False
    model = VGG(make_layers(cfg['E'], batch_norm=True), **kwargs)
    if pretrained:
        model.load_state_dict(model_zoo.load_url(model_urls['vgg19_bn']))
    return model


# 统一接口 interface for model_factory ##########################################

model_dir = xtils.get_pretrained_models()


def VGGNets(arch, model_path=None):
    pretrained = True if model_path else False

    if arch == 'vgg19':
        return vgg19(pretrained, model_path)
    elif arch == 'vgg16':
        return vgg16(pretrained, model_path)
    elif arch == 'vgg19_bn':
        return vgg19_bn(pretrained)
    else:
        raise NotImplementedError
예제 #4
0
batch_size_val = 4
batch_nums = math.ceil(train_size / batch_size)
BN = batch_nums  # =>> Unit    #5005

cfgmsnet = {
    # experiment config
    'exp_version': 'exp.xxx',
    'train_val_test': (False, True, False),

    # device config
    'gpu_ids': [0, 1, 2, 3, 4, 5, 6, 7, 8][0:4],

    # model config
    'arch_name': 'msnet',
    'arch_kwargs': {},
    'resume': os.path.join(xtils.get_pretrained_models(), ''),
    'resume_config': False,
    'resume_optimizer': False,
    'mgpus_to_sxpu': ['m2s', 's2m', 'none', 'auto'][3],

    # data config
    'dataset': 'imagenet',
    'data_info': {'train_size': train_size, 'val_size': 50000, 'test_size': 50000},
    'data_root': xtils.get_data_root(data='imagenet'),
    'data_augment': {'train': 'rotate-rresize-1crop', 'val': '1resize-1crop',
                     'imsize': 256, 'insize': 224, 'color': True,
                     'degree': (0, 0), 'scale': (0.08, 1), 'ratio': (3. / 4, 4. / 3)},
    'data_kwargs': {},
    'data_workers': 12,

    # path config
예제 #5
0
def RESNets(arch, cfg, model_path=''):
    """
    自定义接口 for model_factory
    :param arch: res18, res34, res50, res101, res152
    :param cfg:  arch configs
    :param model_path: state_dict.pth
    :return: a blank model or pre-trained model
    """
    _block = {'BasicBlock': BasicBlock, 'Bottleneck': Bottleneck}

    model_name_map = {
        'res18': 'resnet18',
        'res34': 'resnet34',
        'res50': 'resnet50',
        'res101': 'resnet101',
        'res152': 'resnet152',
    }
    model_cfg_map = {
        'resnet18': res18['cfg'],
        'resnet34': res34['cfg'],
        'resnet50': res50['cfg'],
        'resnet101': res101['cfg'],
        'resnet152': res152['cfg'],
    }
    model_ckpt_map = {
        'resnet18': 'resnet18-5c106cde.pth',
        'resnet34': 'resnet34-333f7ec4.pth',
        'resnet50': 'resnet50-19c8e357.pth',
        'resnet101': 'resnet101-5d3b4d8f.pth',
        'resnet152': 'resnet152-b121ed2d.pth',
    }

    try:
        # 调用官方模型
        name = model_name_map[arch]
    except:
        # 使用自定义模型,如fish33, fish55
        name = arch

    if cfg == '':
        # 调用官方配置
        cfg = model_cfg_map[name]
    else:
        # 使用自定义配置
        assert isinstance(cfg, dict)
        cfg = cfg

    if cfg['block'] in _block.keys():
        cfg['block'] = _block[cfg['block']]

    model = ResNet(**cfg)

    model_dir = xtils.get_pretrained_models()
    if os.path.isfile(model_path):
        print('\n=> loading model.pth from %s.' % model_path)
        model.load_state_dict(torch.load(model_path))
        print('\nSuccess: loaded model.pth from %s.\n' % model_path)
    elif model_path == 'local':
        model_path = os.path.join(model_dir, model_ckpt_map[name])
        print('\n=> loading model.pth from %s.' % model_path)
        model.load_state_dict(torch.load(model_path))
        print('\nSuccess: loaded model.pth from %s.\n' % model_path)
    elif model_path == 'download':
        print('\n=> downloading model.pth from %s.' % model_urls[name])
        model.load_state_dict(model_zoo.load_url(model_urls[name], model_dir))
        print('\nSuccess: downloaded model.pth from %s.\n' % model_urls[name])
    else:
        assert model_path == '', '<model_path> must refer to valid-model.ckpt || ' 'download' ' || "".'

    return model
예제 #6
0
파일: mobilev3.py 프로젝트: zhjpqq/scalenet
def MobileV3(arch='mbvdl', cfg='', model_path=''):
    """
    自定义接口 for model_factory
    指定arch=官方模型,即可从map中调用官方模型的名称name,配置cfg,以及预训练参数ckpt
    指定arch=自定义模型,即可使用传入的模型名称name,配置cfg,以及预训练参数ckpt
    """
    model_name_map = {
        'mbvdl': 'MobileV3D-large',
        'mbvds': 'MobileV3D-small',
        'mbvxl': 'MobileV3X-large',
        'mbvxs': 'MobileV3X-small',
        'mbvyl': 'MobileV3Y-large',
        'mbvys': 'MobileV3Y-small',
    }
    model_cfg_map = {
        'mbvdl': 'large',
        'mbvds': 'small',
        'mbvxl': 'large',
        'mbvxs': 'small',
        'mbvyl': 'large',
        'mbvys': 'small',
    }
    model_ckpt_map = {
        'mbvdl': 'mobilev3_d_large.pth.tar',
        'mbvds': 'mobilev3_d_small.pth.tar',
        'mbvxl': 'mobilev3_x_large.pth.tar',
        'mbvxs': 'mobilev3_x_small.pth.tar',
        'mbvyl': 'mobilev3_y_large.pth.tar',
        'mbvys': 'mobilev3_y_small.pth.tar',
    }

    try:
        # 调用官方模型
        name = model_name_map[arch]
    except:
        # 使用自定义模型,如mbvd33, mbvd22
        name = arch

    if cfg == '':
        # 调用官方配置
        cfg = model_cfg_map[arch]
    else:
        # 使用自定义配置
        assert isinstance(cfg, dict)
        raise NotImplementedError

    if name.startswith('MobileV3D'):
        model = MobileV3D(cfg)
    elif name.startswith('MobileV3X'):
        model = MobileV3X(cfg)
    elif name.startswith('MobileV3Y'):
        model = MobileV3Y(cfg)
    elif name.startswith('MobileV3Z'):
        model = MobileV3Z(cfg)
    else:
        raise NotImplementedError

    model_dir = xtils.get_pretrained_models()

    if os.path.isfile(model_path):
        model = xtils.load_ckpt_weights(model,
                                        model_path,
                                        device='cpu',
                                        mgpus_to_sxpu='auto')
    elif model_path == 'local':
        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
예제 #7
0
batch_size_val = 4
batch_nums = math.ceil(train_size / batch_size)
BN = batch_nums  # =>> Unit    #5005

cfgscale = {
    # experiment config
    'exp_version': 'exp.xxx',
    'train_val_test': (False, True, False),

    # device config
    'gpu_ids': [0, 1, 2, 3, 4, 5, 6, 7, 8][0:4],

    # model config
    'arch_name': 'scalenet',
    'arch_kwargs': {},
    'resume': os.path.join(xtils.get_pretrained_models(), 'scale103-5.09M-71.63-vo69.pth'),
    'resume_config': False,
    'resume_optimizer': False,
    'mgpus_to_sxpu': ['m2s', 's2m', 'none', 'auto'][3],

    # data config
    'dataset': 'imagenet',
    'data_info': {'train_size': train_size, 'val_size': 50000, 'test_size': 50000},
    'data_root': xtils.get_data_root(data='imagenet'),
    'data_augment': {'train': 'rotate-rresize-1crop', 'val': '1resize-1crop',
                     'imsize': 256, 'insize': 224, 'color': True,
                     'degree': (0, 0), 'scale': (0.08, 1), 'ratio': (3. / 4, 4. / 3)},
    'data_kwargs': {},
    'data_workers': 4,

    # path config
예제 #8
0
def EFFNets(arch='effb0', cfg='', model_path='', override_params=None):
    """
    自定义接口 for model_factory
    指定arch=官方模型,即可从map中调用官方模型的名称name,配置cfg,以及预训练参数ckpt
    指定arch=自定义模型,即可使用传入的模型名称name,配置cfg,以及预训练参数ckpt
    """
    model_name_map = {
        'effb0': 'efficientnet-b0',
        'effb1': 'efficientnet-b1',
        'effb2': 'efficientnet-b2',
        'effb3': 'efficientnet-b3',
        'effb4': 'efficientnet-b4',
        'effb5': 'efficientnet-b5',
        'effb6': 'efficientnet-b6',
        'effb7': 'efficientnet-b7',
    }
    model_cfg_map = {"""no-use"""}
    model_ckpt_map = {
        'effb0': 'efficientnet-b0-08094119.pth',
        'effb1': 'efficientnet-b1-dbc7070a.pth',
        'effb2': 'efficientnet-b2-27687264.pth',
        'effb3': 'efficientnet-b3-c8376fa2.pth',
        'effb4': '',
        'effb5': '',
        'effb6': '',
        'effb7': '',
    }

    try:
        # 调用官方模型
        model_name = model_name_map[arch]
    except:
        # 使用自定义模型,如effb11, effb888
        model_name = arch

    if cfg == '':
        # 调用官方配置
        # cfg = model_cfg_map[arch]
        pass
    else:
        # 使用自定义配置
        assert isinstance(cfg, dict)
        # cfg = cfg

    blocks_args, global_params = get_model_params(model_name, override_params)
    model = EfficientNet(blocks_args, global_params, model_name)

    model_dir = xtils.get_pretrained_models()

    if os.path.isfile(model_path):
        model = xtils.load_ckpt_weights(model,
                                        model_path,
                                        device='cpu',
                                        mgpus_to_sxpu='auto')
    elif model_path == 'local':
        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 = {
            'efficientnet-b0':
            'http://storage.googleapis.com/public-models/efficientnet-b0-08094119.pth',
            'efficientnet-b1':
            'http://storage.googleapis.com/public-models/efficientnet-b1-dbc7070a.pth',
            'efficientnet-b2':
            'http://storage.googleapis.com/public-models/efficientnet-b2-27687264.pth',
            'efficientnet-b3':
            'http://storage.googleapis.com/public-models/efficientnet-b3-c8376fa2.pth',
        }
        import torch.utils.model_zoo as model_zoo
        model.load_state_dict(
            model_zoo.load_url(model_url_map[model_name], model_dir))
    else:
        assert model_path == '', '<model_path> must refer to "local" or "download" or "" or "model.ckpt".'

    return model