示例#1
0
    def __loadClsModel(self):

        # Specify model architecture
        cls_model = Inception3(num_classes=2,
                               aux_logits=True,
                               transform_input=False)
        cls_model = cls_model.to(self.device)

        # Load old parameters
        checkpoint = torch.load(self.cls_checkpoint_path,
                                map_location=self.device)

        if self.cls_checkpoint_path[
                -4:] == '.tar':  # it is a checkpoint dictionary rather than just model parameters

            cls_model.load_state_dict(checkpoint['model_state_dict'])

        else:

            cls_model.load_state_dict(checkpoint)

        # Put model into inference mode
        cls_model.eval()

        return cls_model
示例#2
0
def InceptionV3_backbone(num_classes, regression=False):
    base_model = Inception3(num_classes=num_classes, aux_logits=False)
    num_ftrs = base_model.fc.in_features
    if regression:
        base_model.fc = nn.Sequential(nn.Linear(num_ftrs, 1), nn.ReLU())
    else:
        base_model.fc = nn.Linear(num_ftrs, num_classes)
    return base_model.cuda()
示例#3
0
 def __init__(self, path: str):
     self._transforms = Compose([
         ToTensor(),
         Resize((299, 299)),
         Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
     ])
     self._module = Inception3(5, init_weights=False)
     self._module.load_state_dict(load(path))
     self._module.eval()
示例#4
0
def inception_v3(num_classes=1000, in_chans=3, pretrained=False, **kwargs):
    # original PyTorch weights, ported from Tensorflow but modified
    default_cfg = default_cfgs['inception_v3']
    assert in_chans == 3
    _assert_default_kwargs(kwargs)
    model = Inception3(num_classes=num_classes, aux_logits=True, transform_input=False)
    if pretrained:
        load_pretrained(model, default_cfg, num_classes, in_chans)
    model.default_cfg = default_cfg
    return model
示例#5
0
def tf_inception_v3(num_classes=1000, in_chans=3, pretrained=False, **kwargs):
    # my port of Tensorflow SLIM weights (http://download.tensorflow.org/models/inception_v3_2016_08_28.tar.gz)
    default_cfg = default_cfgs['tf_inception_v3']
    assert in_chans == 3
    _assert_default_kwargs(kwargs)
    model = Inception3(num_classes=num_classes, aux_logits=False, transform_input=False)
    if pretrained:
        load_pretrained(model, default_cfg, num_classes, in_chans)
    model.default_cfg = default_cfg
    return model
    def __init__(self, pretrained=False, out_features=2):
        super().__init__()

        self.net = Inception3(num_classes=1000)
        if pretrained:
            self.net.load_state_dict(
                model_zoo.load_url(model_urls['inception_v3']))
        in_features = self.net.fc.in_features
        self.net.fc = nn.Linear(in_features, out_features)
        self.out_features = out_features
示例#7
0
def inception_v3(pretrained=False, **kwargs):  # 299*299
    if pretrained:
        if 'transform_input' not in kwargs:
            kwargs['transform_input'] = True
        model = Inception3(**kwargs)
        pretrained_state_dict = torch.load(
            './Authority/inception_v3_google-1a9a5a14.pth'
        )  # load_url函数根据model_urls字典下载或导入相应的预训练模型
        now_state_dict = model.state_dict()  # 返回model模块的字典
        pretrained_state_dict.pop('AuxLogits.fc.weight')
        pretrained_state_dict.pop('AuxLogits.fc.bias')
        pretrained_state_dict.pop('fc.weight')
        pretrained_state_dict.pop('fc.bias')
        now_state_dict.update(pretrained_state_dict)
        model.load_state_dict(now_state_dict)
        # 最后通过调用model的load_state_dict方法用预训练的模型参数来初始化你构建的网络结构,
        # 这个方法就是PyTorch中通用的用一个模型的参数初始化另一个模型的层的操作。load_state_dict方法还有一个重要的参数是strict,
        # 该参数默认是True,表示预训练模型的层和你的网络结构层严格对应相等(比如层名和维度)
        return model
    return Inception3(**kwargs)
示例#8
0
def gluon_inception_v3(num_classes=1000, in_chans=3, pretrained=False, **kwargs):
    # from gluon pretrained models, best performing in terms of accuracy/loss metrics
    # https://gluon-cv.mxnet.io/model_zoo/classification.html
    default_cfg = default_cfgs['gluon_inception_v3']
    assert in_chans == 3
    _assert_default_kwargs(kwargs)
    model = Inception3(num_classes=num_classes, aux_logits=False, transform_input=False)
    if pretrained:
        load_pretrained(model, default_cfg, num_classes, in_chans)
    model.default_cfg = default_cfg
    return model
示例#9
0
def adv_inception_v3(num_classes=1000, in_chans=3, pretrained=False, **kwargs):
    # my port of Tensorflow adversarially trained Inception V3 from
    # http://download.tensorflow.org/models/adv_inception_v3_2017_08_18.tar.gz
    default_cfg = default_cfgs['adv_inception_v3']
    assert in_chans == 3
    _assert_default_kwargs(kwargs)
    model = Inception3(num_classes=num_classes, aux_logits=False, transform_input=False)
    if pretrained:
        load_pretrained(model, default_cfg, num_classes, in_chans)
    model.default_cfg = default_cfg
    return model
示例#10
0
    def loadModel(self):

        # Specify model architecture
        model = Inception3(num_classes=2, aux_logits=True, transform_input=False)
        model = model.to(self.device)

        # Load old parameters
        checkpoint = torch.load(self.checkpoint_path, map_location=self.device)

        if self.checkpoint_path[-4:] == '.tar':  # it is a checkpoint dictionary rather than just model parameters

            model.load_state_dict(checkpoint['model_state_dict'])

        else:

            model.load_state_dict(checkpoint)

        return model
示例#11
0
 def test_regist_config(self):
     log = Loger()
     param = [torch.ones(3, 3, requires_grad=True)] * 5
     opt = Optimizer(param,
                     lr=0.999,
                     weight_decay=0.03,
                     momentum=0.5,
                     betas=(0.1, 0.4),
                     opt_name="RMSprop")
     log.regist_config(1, opt)
     print(log.__dict__["Optimizer"])
     opt.do_lr_decay()
     log.regist_config(2, opt)
     print(log.__dict__["Optimizer"])
     log.regist_config(3, opt)
     print(log.__dict__["Optimizer"])
     net_G = Model(Inception3(4))
     log.regist_config(1, net_G)
    def __init__(self, num_classes, aux_logits=True, transform_input=False):
        super(SEInception3, self).__init__()
        model = Inception3(num_classes=num_classes,
                           aux_logits=aux_logits,
                           transform_input=transform_input)
        model.Mixed_5b.add_module("SELayer", SELayer(192))
        model.Mixed_5c.add_module("SELayer", SELayer(256))
        model.Mixed_5d.add_module("SELayer", SELayer(288))
        model.Mixed_6a.add_module("SELayer", SELayer(288))
        model.Mixed_6b.add_module("SELayer", SELayer(768))
        model.Mixed_6c.add_module("SELayer", SELayer(768))
        model.Mixed_6d.add_module("SELayer", SELayer(768))
        model.Mixed_6e.add_module("SELayer", SELayer(768))
        if aux_logits:
            model.AuxLogits.add_module("SELayer", SELayer(768))
        model.Mixed_7a.add_module("SELayer", SELayer(768))
        model.Mixed_7b.add_module("SELayer", SELayer(1280))
        model.Mixed_7c.add_module("SELayer", SELayer(2048))

        self.model = model
示例#13
0
 def __init__(self, num_classes):
     super(InceptionV3, self).__init__()
     self.model_name = "InceptionV3"
     self.model = Inception3(num_classes=num_classes)
示例#14
0
transform_test = transforms.Compose([
    transforms.Resize(input_size),
    transforms.ToTensor(),
    transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
])

if __name__ == '__main__':
    # data
    dataset_test = datasets.ImageFolder(data_dir, transform_test)
    dataloader_test = DataLoader(dataset_test,
                                 batch_size=batch_size,
                                 shuffle=True,
                                 num_workers=4)
    # model
    model = Inception3(num_classes=2, aux_logits=True, transform_input=False)
    model = model.to(device)
    # load old parameters
    checkpoint = torch.load(old_ckpt_path, map_location=device)
    if old_ckpt_path[
            -4:] == '.tar':  # it is a checkpoint dictionary rather than just model parameters
        model.load_state_dict(checkpoint['model_state_dict'])
    else:
        model.load_state_dict(checkpoint)
    print('Old checkpoint loaded: ' + old_ckpt_path)
    stats, metric_value = test_model(model,
                                     dataloader_test,
                                     metrics,
                                     threshold=threshold)
    precision = (stats['TP'] + 0.00001) * 1.0 / (stats['TP'] + stats['FP'] +
                                                 0.00001)
示例#15
0
def test():
    net_G = Model(Inception3(4))
    net_G.checkPoint("test_model", 32)
    net_G.loadPoint("test_model", 32)
    net_G = Model()
    net_G.loadPoint("test_model", 32)
示例#16
0
    return model


def create_ef_model(weights_name, arch='efficientnet-b6'):
    model_ef = EfficientNet.from_name(arch,
                                      override_params={'num_classes': 34})
    model_ef.load_state_dict(torch.load(weights_name))
    print("load pretrained weights")
    model_ef = model_ef.cuda()
    # model = torch.nn.parallel.DistributedDataParallel(model, find_unused_parameters=False)
    # model = torch.nn.parallel.DistributedDataParallel(model, find_unused_parameters=True)
    model_ef.eval()
    return model_ef


model_in0 = Inception3(num_classes=len(os.listdir(labelpath_Net1)),
                       aux_logits=False)
model_in1 = Inception3(num_classes=classify, aux_logits=False)
model_in2 = Inception3(num_classes=classify, aux_logits=False)
model_in3 = Inception3(num_classes=classify, aux_logits=False)
model_in4 = Inception3(num_classes=len(os.listdir(labelpath_polyp)),
                       aux_logits=False)
weights0 = r'../bin/weight_iv3_ft_' + 'Net1_sus-2' + '.pkl'
weights1 = r'../bin/weight_iv3_ft_' + 'polyp-fake-img-95-3' + '.pkl'
weights2 = r'../bin/weight_iv3_ft_' + 'polyp-fake-img-95' + '.pkl'
weights3 = r'../bin/weight_iv3_ft_' + 'polyp-fake-sus-95' + '.pkl'
weights4 = r'../bin/weight_iv3_ft_' + 'polyp-fake-sus-95' + '.pkl'
weights5 = r'../bin/weight_iv3_ft_' + 'polyp-fake-rd' + '.pkl'
weights6 = r'../bin/weight_iv3_ft_' + 'polyp-sus' + '.pkl'

#===========net1 test===============
# Net1 label
示例#17
0
        transforms.CenterCrop(224),
        transforms.ToTensor(), normalize
    ])

    return transf


def get_input_tensors(img):
    transf = get_input_transform()
    # unsqeeze converts single image to batch of 1
    return transf(img).unsqueeze(0)


# model = models.inception_v3(pretrained=True)

model = Inception3()
checkpoint = torch.load('../Data/inception_v3_google-1a9a5a14.pth')

model.load_state_dict(checkpoint)
# model.load_state_dict(model_zoo.load_url(model_urls['inception_v3_google']))

idx2label, cls2label, cls2idx = [], {}, {}
with open('../Data/imagenet_class_index.json', 'r') as read_file:
    class_idx = json.load(read_file)
    idx2label = [class_idx[str(k)][1] for k in range(len(class_idx))]
    cls2label = {
        class_idx[str(k)][0]: class_idx[str(k)][1]
        for k in range(len(class_idx))
    }

    cls2idx = {class_idx[str(k)][0]: k for k in range(len(class_idx))}