Ejemplo n.º 1
0
def VGG16(include_top=True,
          pretrained=True,
          freeze_features=True,
          input_shape=None,
          classes=1000,
          **kwargs):
    if input_shape is not None and len(input_shape) == 3:
        input_shape = tuple(input_shape)
    else:
        input_shape = (3, 224, 224)
    vgg16 = make_vgg_layers(cfgs['D'], classes)
    vgg16.input_shape = input_shape
    if pretrained == True:
        download_model_from_google_drive('1uXiH5MSy1rvxrHjW4uB9E2BHMM8b0Fwr',
                                         dirname, 'vgg16.pth')
        recovery_model = load(os.path.join(dirname, 'vgg16.pth'))
        recovery_model = fix_layer(recovery_model)
        recovery_model.name = 'vgg16'
        recovery_model = _make_recovery_model_include_top(
            recovery_model,
            input_shape=input_shape,
            include_top=include_top,
            classes=classes,
            freeze_features=freeze_features)
        vgg16.model = recovery_model

    else:
        vgg16.model = _make_recovery_model_include_top(vgg16.model,
                                                       include_top=include_top,
                                                       classes=classes,
                                                       freeze_features=True)

        vgg16.model.input_shape = input_shape
        vgg16.model.to(_device)
    return vgg16
Ejemplo n.º 2
0
def VGG19(include_top=True,
          pretrained=True,
          freeze_features=True,
          input_shape=None,
          classes=1000,
          **kwargs):
    if input_shape is not None and len(input_shape) == 3:
        input_shape = tuple(input_shape)
    else:
        input_shape = (3, 224, 224)
    vgg19 = make_vgg_layers(cfgs['E'], classes)
    vgg19.input_shape = input_shape
    if pretrained == True:
        download_model_from_google_drive('1nqQJLYMzeiUX9hji39-rrBUG42YyjhYg',
                                         dirname, 'vgg19.pth')
        recovery_model = load(os.path.join(dirname, 'vgg19.pth'))
        recovery_model = fix_layer(recovery_model)
        recovery_model.name = 'vgg19'
        recovery_model = _make_recovery_model_include_top(
            recovery_model,
            input_shape=input_shape,
            include_top=include_top,
            classes=classes,
            freeze_features=freeze_features)
        vgg19.model = recovery_model

    else:
        vgg19.model = _make_recovery_model_include_top(vgg19.model,
                                                       include_top=include_top,
                                                       classes=classes,
                                                       freeze_features=True)

        vgg19.model.input_shape = input_shape
        vgg19.model.to(_device)
    return vgg19
Ejemplo n.º 3
0
    def load(cls):
        # 從google drive載入模型
        st = datetime.datetime.now()
        download_model_from_google_drive('13XZPWh8QhEsC8EdIp1niLtZz0ipatSGC',
                                         dirname, 'word2vec_chinese.pth')
        recovery_model = fix_layer(
            load(os.path.join(dirname, 'word2vec_chinese.pth')))

        recovery_model.locale = locale.getdefaultlocale()[0].lower()
        recovery_model.to(get_device())
        download_file_from_google_drive(
            file_id='16yDlJJ4-O9pHF-ZbXy7XPZZk6vo3aw4e',
            dirname=os.path.join(_trident_dir, 'download'),
            filename='vocabs_tw.txt')
        if not hasattr(recovery_model,
                       'tw2cn') or recovery_model.tw2cn is None:
            with open(download_path, 'r', encoding='utf-8-sig') as f:
                vocabs_tw = f.readlines()
                vocabs_tw = [
                    s.replace('\n', '') for s in vocabs_tw if s != '\n'
                ]
                recovery_model.tw2cn = OrderedDict()
                recovery_model.cn2tw = OrderedDict()

                for i, (w, w_cn) in tqdm(
                        enumerate(zip(vocabs_tw,
                                      recovery_model._vocabs.keys()))):
                    if w not in recovery_model.tw2cn:
                        recovery_model.tw2cn[w] = w_cn
                    recovery_model.cn2tw[w_cn] = w

        et = datetime.datetime.now()
        print('total loading time:{0}'.format(et - st))
        return recovery_model
Ejemplo n.º 4
0
def VGG19(include_top=True,
             pretrained=True,
            freeze_features=False,
             input_shape=None,
             classes=1000,
             **kwargs):
    if input_shape is not None and len(input_shape)==3:
        input_shape=tuple(input_shape)
    else:
        input_shape=(3, 224, 224)
    vgg19 =make_vgg_layers(cfgs['E'], classes)
    vgg19.input_shape =input_shape
    if pretrained==True:
        download_model_from_google_drive('1nqQJLYMzeiUX9hji39-rrBUG42YyjhYg',dirname,'vgg19.pth')
        recovery_model=load(os.path.join(dirname,'vgg19.pth'))
        recovery_model = fix_layer(recovery_model)
        recovery_model.name = 'vgg19'
        recovery_model.eval()
        if freeze_features:
            recovery_model.trainable = False
            recovery_model.fc1.trainable = True
            recovery_model.fc2.trainable = True
            recovery_model.fc3.trainable = True

        if include_top==False:
            [recovery_model.remove_at(-1) for i in range(7)]
            vgg19.class_names = []
        else:
            if classes!=1000:
                recovery_model.remove_at(-1)
                recovery_model.add_module('fc3', Dense(classes, use_bias=True, activation='softmax'))
                vgg19.class_names = []
        recovery_model.to(_device)
        vgg19.model=recovery_model
    return vgg19
Ejemplo n.º 5
0
def ResNet152(include_top=True,
             pretrained=True,
            freeze_features=False,
             input_shape=None,
             classes=1000,
             **kwargs):
    if input_shape is not None and len(input_shape)==3:
        input_shape=tuple(input_shape)
    else:
        input_shape=(3, 224, 224)
    resnet152 =ResNet(bottleneck, [3, 8, 36, 3], input_shape,num_classes=classes,include_top=include_top, model_name='resnet152')
    if pretrained==True:
        download_model_from_google_drive(model_urls['resnet152'],dirname,'resnet152.pth')
        recovery_model=load(os.path.join(dirname,'resnet152.pth'))
        recovery_model = fix_layer(recovery_model)
        recovery_model = _make_recovery_model_include_top(recovery_model,input_shape=input_shape, include_top=include_top, classes=classes, freeze_features=freeze_features)
        resnet152.model = recovery_model
    else:
        resnet152.model = _make_recovery_model_include_top(resnet152.model, include_top=include_top, classes=classes, freeze_features=False)

    resnet152.model.input_shape = input_shape
    resnet152.model.to(_device)
    return resnet152


#
#
# resnet34=ResNet(basic_block, [3, 4, 6, 3], (3, 224, 224))
# resnet50=ResNet(bottleneck, [3, 4, 6, 3], (3, 224, 224))
# resnet101=ResNet(bottleneck, [3, 4, 23, 3], (3, 224, 224))
# resnet152=ResNet(bottleneck, [3, 8, 36, 3], (3, 224, 224))
Ejemplo n.º 6
0
def VGG13(include_top=True,
             pretrained=True,
            freeze_features=False,
             input_shape=None,
             classes=1000,
             **kwargs):
    if input_shape is not None and len(input_shape)==3:
        input_shape=tuple(input_shape)
    else:
        input_shape=(3, 224, 224)
    vgg13 =make_vgg_layers(cfgs['B'], classes)

    if pretrained==True:
        download_model_from_google_drive('1wx67gmQ8eHWXs2mhJmNl-t-cFNw7dJ7O',dirname,'vgg13.pth')
        recovery_model=load(os.path.join(dirname,'vgg13.pth'))
        recovery_model = fix_layer(recovery_model)
        recovery_model.name = 'vgg13'
        recovery_model.eval()
        recovery_model.to(_device)
        if freeze_features:
            recovery_model.trainable = False
            recovery_model.fc1.trainable = True
            recovery_model.fc2.trainable = True
            recovery_model.fc3.trainable = True

        if include_top==False:
            [recovery_model.remove_at(-1) for i in range(7)]
            vgg13.class_names = []
        else:
            if classes!=1000:
                recovery_model.remove_at(-1)
                recovery_model.add_module('fc3', Dense(classes, use_bias=True, activation='softmax'))
                vgg13.class_names = []
        vgg13.model=recovery_model
    return vgg13
Ejemplo n.º 7
0
def RfbNet(include_top=True,
           pretrained=True,
           input_shape=None,
           base_filters=16,
           num_classes=1,
           num_regressors=4,
           **kwargs):
    if input_shape is not None and len(input_shape) == 3:
        input_shape = tuple(input_shape)
    else:
        input_shape = (3, 480, 640)
    rfbnet = SsdDetectionModel(input_shape=(3, 480, 640),
                               output=RFBnet(base_filters=base_filters,
                                             num_classes=num_classes,
                                             num_regressors=num_regressors))
    rfbnet.palette[0] = (128, 255, 128)
    rfbnet.palette[1] = (128, 255, 128)
    rfbnet.preprocess_flow = [
        resize((480, 640), True),
        normalize(127.5, 127.5)
    ]
    if pretrained == True:
        download_model_from_google_drive('1T_0VYOHaxoyuG1fAxY-6g0C7pfXiujns',
                                         dirname, 'version-RFB-640.pth')
        recovery_model = torch.load(
            os.path.join(dirname, 'version-RFB-640.pth'))
        recovery_model = fix_layer(recovery_model)
        recovery_model.name = 'rfb640'
        recovery_model.eval()
        recovery_model.to(_device)
        rfbnet.model = recovery_model
    return rfbnet
Ejemplo n.º 8
0
def VGG11(include_top=True,
          pretrained=True,
          freeze_features=True,
          input_shape=None,
          classes=1000,
          **kwargs):
    if input_shape is not None and len(input_shape) == 3:
        input_shape = tuple(input_shape)
    else:
        input_shape = (3, 224, 224)
    vgg11 = make_vgg_layers(cfgs['A'], classes)
    vgg11.input_shape = input_shape
    if pretrained:
        download_model_from_google_drive('1PV9-AwgD1v-JxDRzduOjjGduIR7MDhPW',
                                         dirname, 'vgg11.pth')
        recovery_model = load(os.path.join(dirname, 'vgg11.pth'))
        recovery_model = fix_layer(recovery_model)
        recovery_model.name = 'vgg11'
        recovery_model = _make_recovery_model_include_top(
            recovery_model,
            input_shape=input_shape,
            include_top=include_top,
            classes=classes,
            freeze_features=freeze_features)
        vgg11.model = recovery_model

    else:
        vgg11.model = _make_recovery_model_include_top(vgg11.model,
                                                       include_top=include_top,
                                                       classes=classes,
                                                       freeze_features=True)

        vgg11.model.input_shape = input_shape
        vgg11.model.to(_device)
    return vgg11
Ejemplo n.º 9
0
def DenseNet121(include_top=True,
             pretrained=True,
            freeze_features=False,
             input_shape=(3,224,224),
             classes=1000,
             **kwargs):
    """
    Constructor the image classicication model with DenseNet121 as backbond

    Args:
        freeze_features ():
        include_top ():
        pretrained (bool): If True, returns a model pre-trained on ImageNet.
        input_shape (tuple or list): the default input image size in CHW order (C, H, W)
        classes (int): number of classes

    References
        Densely Connected Convolutional Networks" <https://arxiv.org/pdf/1608.06993.pdf

    Returns:
        the image classicication model with DenseNet121

    Examples:
        >>> dense121 = DenseNet121(include_top=True,pretrained=True,input_shape=(3,224,224),classes=1000)
        >>> 'n02124075' in dense121.infer_single_image(get_image_from_google_drive('1SwablQsZO8mBuB84xnr1IoOisE3pm03l'),1).key_list[0]
        True

    """

    if input_shape is not None and len(input_shape)==3:
        input_shape=tuple(input_shape)

    densenet121 =DenseNet([6, 12, 24, 16],32,64, include_top=include_top, pretrained=True,input_shape=input_shape, num_classes=classes,name='densenet121')
    if pretrained==True:
        download_model_from_google_drive('16N2BECErDMRTV5JqESEBWyylXbQmKAIk',dirname,'densenet121.pth')
        recovery_model=load(os.path.join(dirname,'densenet121.pth'))
        recovery_model=fix_layer(recovery_model)
        recovery_model.name = 'densenet121'
        recovery_model.eval()
        recovery_model.to(_device)
        if freeze_features:
            recovery_model.trainable = False
            recovery_model.classifier.trainable = True

        if include_top==False:
            recovery_model.remove_at(-1)
            recovery_model.remove_at(-1)
            recovery_model.remove_at(-1)
            densenet121.class_names = []
        else:
            if classes!=1000:
                recovery_model.remove_at(-1)
                recovery_model.add_module('classifier', Dense(classes, activation=None, name='classifier'))

                densenet121.class_names = []
        densenet121.model=recovery_model

        densenet121.signature = get_signature(densenet121.model.forward)
    return densenet121
Ejemplo n.º 10
0
def ResNet152(include_top=True,
              pretrained=True,
              freeze_features=False,
              input_shape=None,
              classes=1000,
              **kwargs):
    if input_shape is not None and len(input_shape) == 3:
        input_shape = tuple(input_shape)
    else:
        input_shape = (3, 224, 224)
    resnet152 = ResNet(bottleneck, [3, 8, 36, 3],
                       input_shape,
                       num_classes=classes,
                       include_top=include_top,
                       model_name='resnet152')
    if pretrained == True:
        download_model_from_google_drive(model_urls['resnet152'], dirname,
                                         'resnet152.pth')
        recovery_model = load(os.path.join(dirname, 'resnet152.pth'))
        recovery_model = fix_layer(recovery_model)
        if freeze_features:
            recovery_model.trainable = False
            recovery_model.fc.trainable = True
        recovery_model.name = 'resnet152'
        recovery_model.eval()
        recovery_model.to(_device)
        if include_top == False:
            recovery_model.remove_at(-1)
            recovery_model.remove_at(-1)
            recovery_model.remove_at(-1)
            resnet152.class_names = []
        else:
            if classes != 1000:
                resnet152.class_names = []
                recovery_model.remove_at(-1)
                recovery_model.remove_at(-1)
                recovery_model.add_module(
                    'fc', Dense(classes, activation=None, name='fc'))
                recovery_model.add_module('softmax', SoftMax())
        resnet152.model = recovery_model
    return resnet152


#
#
# resnet34=ResNet(basic_block, [3, 4, 6, 3], (3, 224, 224))
# resnet50=ResNet(bottleneck, [3, 4, 6, 3], (3, 224, 224))
# resnet101=ResNet(bottleneck, [3, 4, 23, 3], (3, 224, 224))
# resnet152=ResNet(bottleneck, [3, 8, 36, 3], (3, 224, 224))
Ejemplo n.º 11
0
def Onet(pretrained=True, input_shape=(3, 48, 48), **kwargs):
    if input_shape is not None and len(input_shape) == 3:
        input_shape = tuple(input_shape)
    else:
        input_shape = (3, 48, 48)
    onet = ImageDetectionModel(input_shape=(3, 48, 48), output=o_net())
    onet.preprocess_flow = [Normalize(0, 255), image_backend_adaption]
    if pretrained == True:
        download_model_from_google_drive('1a1dAlSzJOAfIz77Ic38JMQJYWDG_b7-_',
                                         dirname, 'onet.pth')
        recovery_model = load(os.path.join(dirname, 'onet.pth'))
        recovery_model = fix_layer(recovery_model)
        recovery_model.to(_device)
        onet.model = recovery_model
    return onet
Ejemplo n.º 12
0
def Rnet(pretrained=True, input_shape=(3, 24, 24), **kwargs):
    if input_shape is not None and len(input_shape) == 3:
        input_shape = tuple(input_shape)
    else:
        input_shape = (3, 24, 24)
    rnet = ImageDetectionModel(input_shape=(3, 24, 24), output=r_net())
    rnet.preprocess_flow = [Normalize(0, 255), image_backend_adaption]
    if pretrained == True:
        download_model_from_google_drive('1CH7z133_KrcWMx9zXAblMCV8luiQ3wph',
                                         dirname, 'rnet.pth')
        recovery_model = load(os.path.join(dirname, 'rnet.pth'))
        recovery_model = fix_layer(recovery_model)
        recovery_model.to(_device)
        rnet.model = recovery_model
    return rnet
Ejemplo n.º 13
0
def YoLoV4(pretrained=True,
            freeze_features=False,
            input_shape=(3, 608, 608),
             classes=80,
             **kwargs):
    detector = YoloDetectionModel(input_shape=input_shape, output=yolo4_body(classes, input_shape[-1]))

    if pretrained:
        download_model_from_google_drive('1CcbyinE8gQFjMjt05arSg2W0LLUwjsdt', dirname, 'pretrained_yolov4_mscoco.pth')
        recovery_model = fix_layer(load(os.path.join(dirname, 'pretrained_yolov4_mscoco.pth')))
        detector.model = recovery_model

    detector.model .input_shape = input_shape
    detector.model .to(get_device())
    return detector
Ejemplo n.º 14
0
def EfficientNetB0(include_top=True,
                   pretrained=True,
                   freeze_features=False,
                   input_shape=(3, 224, 224),
                   classes=1000,
                   **kwargs):
    if input_shape is not None and len(input_shape) == 3:
        input_shape = tuple(input_shape)
    else:
        input_shape = (3, 224, 224)
    effb0 = EfficientNet(1.0,
                         1.0,
                         input_shape,
                         0.2,
                         model_name='efficientnet-b0',
                         include_top=include_top,
                         num_classes=classes)
    if pretrained == True:
        download_model_from_google_drive('1bxnoDerzoNfiZZLft4ocD3DAgx4v6aTN',
                                         dirname, 'efficientnet-b0.pth')
        recovery_model = fix_layer(
            load(os.path.join(dirname, 'efficientnet-b0.pth')))
        recovery_model.input_shape = input_shape
        if freeze_features:
            recovery_model.trainable = False
            recovery_model.fc.trainable = True

        if include_top == False:
            recovery_model.remove_at(-1)
            recovery_model.remove_at(-1)
            recovery_model.remove_at(-1)
            recovery_model.remove_at(-1)
            effb0.class_names = []
        else:
            if classes != 1000:
                recovery_model.remove_at(-1)
                recovery_model.remove_at(-1)
                recovery_model.add_module(
                    'fc', Dense(classes, activation=None, name='fc'))
                recovery_model.add_module('softmax', SoftMax())
                effb0.class_names = []

        recovery_model.to(_device)
        effb0.model = recovery_model

    return effb0
Ejemplo n.º 15
0
def Pnet(pretrained=True,
         input_shape=(3, 12, 12),
         freeze_features=True,
         **kwargs):
    if input_shape is not None and len(input_shape) == 3:
        input_shape = tuple(input_shape)
    else:
        input_shape = (3, 224, 224)
    pnet = ImageDetectionModel(input_shape=(3, 12, 12), output=p_net())
    if pretrained == True:
        download_model_from_google_drive('1w9ahipO8D9U1dAXMc2BewuL0UqIBYWSX',
                                         dirname, 'pnet.pth')
        recovery_model = fix_layer(load(os.path.join(dirname, 'pnet.pth')))
        pnet.model = recovery_model

    pnet.model.input_shape = input_shape
    pnet.model.to(_device)
    return pnet
Ejemplo n.º 16
0
def EfficientNetB1(include_top=True,
                   pretrained=True,
                   freeze_features=False,
                   input_shape=(3, 240, 240),
                   classes=1000,
                   **kwargs):
    if input_shape is not None and len(input_shape) == 3:
        input_shape = tuple(input_shape)
    else:
        input_shape = (3, 240, 240)
    effb1 = EfficientNet(1.0,
                         1.1,
                         240,
                         0.2,
                         model_name='efficientnet-b1',
                         include_top=include_top,
                         num_classes=classes)
    if pretrained == True:
        download_model_from_google_drive('1F3BtnAjmDz4G9RS9Q0hqU_K7WWXCni1G',
                                         dirname, 'efficientnet-b1.pth')
        recovery_model = fix_layer(
            load(os.path.join(dirname, 'efficientnet-b1.pth')))
        recovery_model.input_shape = input_shape
        recovery_model.eval()
        recovery_model.to(_device)
        if freeze_features:
            recovery_model.trainable = False
            recovery_model.fc.trainable = True
        if include_top == False:
            recovery_model.remove_at(-1)
            recovery_model.remove_at(-1)
            recovery_model.remove_at(-1)
            recovery_model.remove_at(-1)
            effb1.class_names = []
        else:
            if classes != 1000:
                recovery_model.remove_at(-1)
                recovery_model.remove_at(-1)
                recovery_model.add_module(
                    'fc', Dense(classes, activation=None, name='fc'))
                recovery_model.add_module('softmax', SoftMax())
                effb1.class_names = []
        effb1.model = recovery_model
    return effb1
Ejemplo n.º 17
0
def EfficientNetB6(include_top=True,
                   pretrained=True,
                   freeze_features=False,
                   input_shape=(3, 528, 528),
                   classes=1000,
                   **kwargs):
    if input_shape is not None and len(input_shape) == 3:
        input_shape = tuple(input_shape)
    else:
        input_shape = (3, 528, 528)
    effb6 = EfficientNet(1.8,
                         2.6,
                         528,
                         0.5,
                         model_name='efficientnet-b6',
                         include_top=include_top,
                         num_classes=classes)
    if pretrained == True:
        download_model_from_google_drive('1XJrKmcmMObN_nnjP2Z-YH_BQ3img58qF',
                                         dirname, 'efficientnet-b6.pth')
        recovery_model = fix_layer(
            load(os.path.join(dirname, 'efficientnet-b6.pth')))
        recovery_model.input_shape = input_shape
        recovery_model.to(_device)

        if freeze_features:
            recovery_model.trainable = False
            recovery_model.fc.trainable = True
        if include_top == False:
            recovery_model.remove_at(-1)
            recovery_model.remove_at(-1)
            recovery_model.remove_at(-1)
            recovery_model.remove_at(-1)
            effb6.class_names = []
        else:
            if classes != 1000:
                recovery_model.remove_at(-1)
                recovery_model.remove_at(-1)
                recovery_model.add_module(
                    'fc', Dense(classes, activation=None, name='fc'))
                recovery_model.add_module('softmax', SoftMax())
                effb6.class_names = []
        effb6.model = recovery_model
    return effb6
Ejemplo n.º 18
0
def EfficientNetB4(include_top=True,
                   pretrained=True,
                   freeze_features=False,
                   input_shape=(3, 380, 380),
                   classes=1000,
                   **kwargs):
    if input_shape is not None and len(input_shape) == 3:
        input_shape = tuple(input_shape)
    else:
        input_shape = (3, 380, 380)
    effb4 = EfficientNet(1.4,
                         1.8,
                         380,
                         0.4,
                         model_name='efficientnet-b4',
                         include_top=include_top,
                         num_classes=classes)
    if pretrained == True:
        download_model_from_google_drive('1X4ZOBR_ETRHZJeffJHvCmWTTy9_aW8SP',
                                         dirname, 'efficientnet-b4.pth')
        recovery_model = fix_layer(
            load(sanitize_path(os.path.join(dirname, 'efficientnet-b4.pth'))))
        recovery_model.input_shape = input_shape
        recovery_model.to(_device)
        if freeze_features:
            recovery_model.trainable = False
            recovery_model.fc.trainable = True
        if include_top == False:
            recovery_model.remove_at(-1)
            recovery_model.remove_at(-1)
            recovery_model.remove_at(-1)
            recovery_model.remove_at(-1)
            effb4.class_names = []

        else:
            if classes != 1000:
                recovery_model.remove_at(-1)
                recovery_model.remove_at(-1)
                recovery_model.add_module(
                    'fc', Dense(classes, activation=None, name='fc'))
                recovery_model.add_module('softmax', SoftMax())
                effb4.class_names = []
        effb4.model = recovery_model
    return effb4
Ejemplo n.º 19
0
def EfficientNetB7(include_top=True,
                   pretrained=True,
                   freeze_features=False,
                   input_shape=(3, 600, 600),
                   classes=1000,
                   **kwargs):
    if input_shape is not None and len(input_shape) == 3:
        input_shape = tuple(input_shape)
    else:
        input_shape = (3, 600, 600)
    effb7 = EfficientNet(2.0,
                         3.1,
                         600,
                         0.5,
                         model_name='efficientnet-b7',
                         include_top=include_top,
                         num_classes=classes)
    if pretrained == True:
        download_model_from_google_drive('1M2DfvsNPRCWSo_CeXnUCQOR46rvOrhLl',
                                         dirname, 'efficientnet-b7.pth')
        recovery_model = fix_layer(
            load(os.path.join(dirname, 'efficientnet-b7.pth')))
        recovery_model.input_shape = input_shape
        recovery_model.to(_device)
        if freeze_features:
            recovery_model.trainable = False
            recovery_model.fc.trainable = True
        if include_top == False:
            recovery_model.remove_at(-1)
            recovery_model.remove_at(-1)
            recovery_model.remove_at(-1)
            recovery_model.remove_at(-1)
            effb7.class_names = []
        else:
            if classes != 1000:
                recovery_model.remove_at(-1)
                recovery_model.remove_at(-1)
                recovery_model.add_module(
                    'fc', Dense(classes, activation=None, name='fc'))
                recovery_model.add_module('softmax', SoftMax())
                effb7.class_names = []
        effb7.model = recovery_model
    return effb7
Ejemplo n.º 20
0
def EfficientNetB5(include_top=True,
                   pretrained=True,
                   freeze_features=False,
                   input_shape=(3, 456, 456),
                   classes=1000,
                   **kwargs):
    if input_shape is not None and len(input_shape) == 3:
        input_shape = tuple(input_shape)
    else:
        input_shape = (3, 456, 456)
    effb5 = EfficientNet(1.6,
                         2.2,
                         456,
                         0.4,
                         model_name='efficientnet-b5',
                         include_top=include_top,
                         num_classes=classes)
    if pretrained == True:
        download_model_from_google_drive('17iTD12G9oW3jYAui84MKtdY4gjd9vpgG',
                                         dirname, 'efficientnet-b5.pth')
        recovery_model = fix_layer(
            load(os.path.join(dirname, 'efficientnet-b5.pth')))
        recovery_model.input_shape = input_shape
        recovery_model.to(_device)
        if freeze_features:
            recovery_model.trainable = False
            recovery_model.fc.trainable = True
        if include_top == False:
            recovery_model.remove_at(-1)
            recovery_model.remove_at(-1)
            recovery_model.remove_at(-1)
            recovery_model.remove_at(-1)
            effb5.class_names = []
        else:
            if classes != 1000:
                recovery_model.remove_at(-1)
                recovery_model.remove_at(-1)
                recovery_model.add_module(
                    'fc', Dense(classes, activation=None, name='fc'))
                recovery_model.add_module('softmax', SoftMax())
                effb5.class_names = []
        effb5.model = recovery_model
    return effb5
Ejemplo n.º 21
0
def EfficientNetB3(include_top=True,
                   pretrained=True,
                   freeze_features=False,
                   input_shape=(3, 300, 300),
                   classes=1000,
                   **kwargs):
    if input_shape is not None and len(input_shape) == 3:
        input_shape = tuple(input_shape)
    else:
        input_shape = (3, 300, 300)
    effb3 = EfficientNet(1.2,
                         1.4,
                         300,
                         0.3,
                         model_name='efficientnet-b3',
                         include_top=include_top,
                         num_classes=classes)
    if pretrained == True:
        download_model_from_google_drive('11tMxdYdFfaEREwnESO4cwjtcoEB42zB_',
                                         dirname, 'efficientnet-b3.pth')
        recovery_model = fix_layer(
            load(os.path.join(dirname, 'efficientnet-b3.pth')))
        recovery_model.input_shape = input_shape
        recovery_model.to(_device)
        if freeze_features:
            recovery_model.trainable = False
            recovery_model.fc.trainable = True
        if include_top == False:
            recovery_model.remove_at(-1)
            recovery_model.remove_at(-1)
            recovery_model.remove_at(-1)
            recovery_model.remove_at(-1)
            effb3.class_names = []
        else:
            if classes != 1000:
                recovery_model.remove_at(-1)
                recovery_model.remove_at(-1)
                recovery_model.add_module(
                    'fc', Dense(classes, activation=None, name='fc'))
                recovery_model.add_module('softmax', SoftMax())
                effb3.class_names = []
        effb3.model = recovery_model
    return effb3
Ejemplo n.º 22
0
def EfficientNetB2(include_top=True,
                   pretrained=True,
                   freeze_features=False,
                   input_shape=(3, 260, 260),
                   classes=1000,
                   **kwargs):
    if input_shape is not None and len(input_shape) == 3:
        input_shape = tuple(input_shape)
    else:
        input_shape = (3, 260, 260)
    effb2 = EfficientNet(1.1,
                         1.2,
                         260,
                         0.3,
                         model_name='efficientnet-b2',
                         include_top=include_top,
                         num_classes=classes)
    if pretrained == True:
        download_model_from_google_drive('1PjqhB7WJasF_hqOwYtSBNSXSGBY-cRLU',
                                         dirname, 'efficientnet-b2.pth')
        recovery_model = fix_layer(
            load(os.path.join(dirname, 'efficientnet-b2.pth')))
        recovery_model.input_shape = input_shape
        recovery_model.to(_device)
        if freeze_features:
            recovery_model.trainable = False
            recovery_model.fc.trainable = True
        if include_top == False:
            recovery_model.remove_at(-1)
            recovery_model.remove_at(-1)
            recovery_model.remove_at(-1)
            recovery_model.remove_at(-1)
            effb2.class_names = []
        else:
            if classes != 1000:
                recovery_model.remove_at(-1)
                recovery_model.remove_at(-1)
                recovery_model.add_module(
                    'fc', Dense(classes, activation=None, name='fc'))
                recovery_model.add_module('softmax', SoftMax())
                effb2.class_names = []
        effb2.model = recovery_model
    return effb2
Ejemplo n.º 23
0
def MobileNetV2(include_top=True,
                pretrained=True,
                freeze_features=False,
                input_shape=(3, 224, 224),
                classes=1000,
                **kwargs):
    if input_shape is not None and len(input_shape) == 3:
        input_shape = tuple(input_shape)
    else:
        input_shape = (3, 224, 224)
    mob = MobileNet(input_shape=(3, 224, 224),
                    classes=classes,
                    use_bias=False,
                    width_mult=1.0,
                    round_nearest=8,
                    include_top=include_top,
                    model_name='mobilenet')
    if pretrained == True:
        download_model_from_google_drive('1ULenXTjOO5PdT3fHv6N8bPXEfoJAn5yL',
                                         dirname, 'mobilenet_v2.pth')
        recovery_model = load(os.path.join(dirname, 'mobilenet_v2.pth'))
        recovery_model = fix_layer(recovery_model)
        recovery_model.eval()
        recovery_model.to(_device)
        if freeze_features:
            recovery_model.trainable = False
            recovery_model.fc.trainable = True

        if include_top == False:
            recovery_model.remove_at(-1)
            recovery_model.remove_at(-1)
            recovery_model.remove_at(-1)
            recovery_model.remove_at(-1)
            mob.class_names = []
        else:
            if classes != 1000:
                recovery_model.remove_at(-1)
                recovery_model.add_module(
                    'fc', Dense(classes, activation=None, name='fc'))
                mob.class_names = []
        mob.model = recovery_model
    return mob
def SEResNet50_IR_512(pretrained=True, input_shape=(3, 112, 112), **kwargs):
    if input_shape is not None and len(input_shape) == 3:
        input_shape = tuple(input_shape)
    else:
        input_shape = (3, 112, 112)
    seresnet = SEResNet_IR(num_layers=50,
                           Bottleneck=BottleNeck_IR_SE,
                           drop_ratio=0.4,
                           feature_dim=512)
    if pretrained == True:
        download_model_from_google_drive('1aLYbFvtvsV2gQ16D_vwzrKdbCgij7IoZ',
                                         dirname, 'arcface_se_50_512.pth')
        recovery_model = torch.load(
            os.path.join(dirname, 'arcface_se_50_512.pth'))
        recovery_model = fix_layer(recovery_model)
        recovery_model.name = 'arcface_se_50_512'
        recovery_model.eval()
        recovery_model.to(_device)
        seresnet.model = recovery_model
    return seresnet
Ejemplo n.º 25
0
def SE_ResNet50(include_top=True,
                pretrained=True,
                freeze_features=True,
                input_shape=None,
                classes=1000,
                **kwargs):
    if input_shape is not None and len(input_shape) == 3:
        input_shape = tuple(input_shape)
    else:
        input_shape = (3, 224, 224)
    resnet50 = SE_ResNet(se_bottleneck, [3, 4, 6, 3],
                         input_shape,
                         num_classes=classes,
                         include_top=include_top,
                         model_name='se_resnet50')
    if pretrained == True:
        download_model_from_google_drive(model_urls['se_resnet50'], dirname,
                                         'se_resnet50.pth')
        recovery_model = load(os.path.join(dirname, 'se_resnet50.pth'))
        recovery_model = fix_layer(recovery_model)
        recovery_model.name = 'se_resnet50'
        recovery_model = _make_recovery_model_include_top(
            recovery_model,
            include_top=include_top,
            classes=classes,
            default_shape=(3, 224, 224),
            input_shape=input_shape,
            freeze_features=freeze_features)
        resnet50.model = recovery_model
    else:
        resnet50.model = _make_recovery_model_include_top(
            resnet50.model,
            include_top=include_top,
            classes=classes,
            default_shape=(3, 224, 224),
            input_shape=input_shape,
            freeze_features=True)

    resnet50.model.input_shape = input_shape
    resnet50.model.to(_device)
    return resnet50
Ejemplo n.º 26
0
def MobileNetV2(include_top=True,
                pretrained=True,
                freeze_features=True,
                input_shape=(3, 224, 224),
                classes=1000,
                **kwargs):
    if input_shape is not None and len(input_shape) == 3:
        input_shape = tuple(input_shape)
    else:
        input_shape = (3, 224, 224)
    mob = MobileNet(input_shape=(3, 224, 224),
                    classes=classes,
                    use_bias=False,
                    width_mult=1.0,
                    round_nearest=8,
                    include_top=include_top,
                    model_name='mobilenet')
    if pretrained == True:
        download_model_from_google_drive('1ULenXTjOO5PdT3fHv6N8bPXEfoJAn5yL',
                                         dirname, 'mobilenet_v2.pth')
        recovery_model = load(os.path.join(dirname, 'mobilenet_v2.pth'))
        recovery_model = fix_layer(recovery_model)
        recovery_model = _make_recovery_model_include_top(
            recovery_model,
            input_shape=input_shape,
            include_top=include_top,
            classes=classes,
            freeze_features=freeze_features)
        mob.model = recovery_model

    else:
        mob.model = _make_recovery_model_include_top(mob.model,
                                                     include_top=include_top,
                                                     classes=classes,
                                                     freeze_features=True)

        mob.model.input_shape = input_shape
        mob.model.to(_device)
    return mob
Ejemplo n.º 27
0
def SE_ResNet101(include_top=True,
                 pretrained=True,
                 freeze_features=False,
                 input_shape=None,
                 classes=1000,
                 **kwargs):
    if input_shape is not None and len(input_shape) == 3:
        input_shape = tuple(input_shape)
    else:
        input_shape = (3, 224, 224)
    resnet101 = SE_ResNet(se_bottleneck, [3, 4, 23, 3],
                          input_shape,
                          num_classes=classes,
                          include_top=include_top,
                          model_name='resnet101')
    if pretrained == True:
        download_model_from_google_drive('1BO92ONsWnLQPWs9JVS-4SdpoPo5M4ABR',
                                         dirname, 'se_resnet101.pth')
        recovery_model = load(os.path.join(dirname, 'se_resnet101.pth'))
        recovery_model = fix_layer(recovery_model)
        recovery_model.name = 'se_resnet101'
        recovery_model.eval()
        recovery_model.to(_device)
        if freeze_features:
            recovery_model.trainable = False
            recovery_model.fc.trainable = True
        if include_top == False:
            recovery_model.remove_at(-1)
            recovery_model.remove_at(-1)
            recovery_model.remove_at(-1)
            resnet101.class_names = []
        else:
            if classes != 1000:
                resnet101.class_names = []
                recovery_model.fc = Dense(classes, activation=None, name='fc')
                recovery_model.fc.input_shape = recovery_model.avg_pool.output_shape

        resnet101.model = recovery_model
    return resnet101
Ejemplo n.º 28
0
def EfficientNetB7(include_top=True,
                   pretrained=True,
                   freeze_features=False,
                   input_shape=(3, 600, 600),
                   classes=1000,
                   **kwargs):
    if input_shape is not None and len(input_shape) == 3:
        input_shape = tuple(input_shape)
    else:
        input_shape = (3, 600, 600)
    effb7 = EfficientNet(2.0,
                         3.1,
                         input_shape,
                         0.5,
                         model_name='efficientnet-b7',
                         include_top=include_top,
                         num_classes=classes)
    if pretrained:
        download_model_from_google_drive('1M2DfvsNPRCWSo_CeXnUCQOR46rvOrhLl',
                                         dirname, 'efficientnet-b7.pth')
        recovery_model = fix_layer(
            load(os.path.join(dirname, 'efficientnet-b7.pth')))
        recovery_model = _make_recovery_model_include_top(
            recovery_model,
            input_shape=input_shape,
            include_top=include_top,
            classes=classes,
            freeze_features=freeze_features)
        effb7.model = recovery_model
    else:
        effb7.model = _make_recovery_model_include_top(effb7.model,
                                                       include_top=include_top,
                                                       classes=classes,
                                                       freeze_features=False)

    effb7.model.input_shape = input_shape
    effb7.model.to(get_device())
    return effb7
Ejemplo n.º 29
0
def EfficientNetB6(include_top=True,
                   pretrained=True,
                   freeze_features=False,
                   input_shape=(3, 528, 528),
                   classes=1000,
                   **kwargs):
    if input_shape is not None and len(input_shape) == 3:
        input_shape = tuple(input_shape)
    else:
        input_shape = (3, 528, 528)
    effb6 = EfficientNet(1.8,
                         2.6,
                         input_shape,
                         0.5,
                         model_name='efficientnet-b6',
                         include_top=include_top,
                         num_classes=classes)
    if pretrained:
        download_model_from_google_drive('1XJrKmcmMObN_nnjP2Z-YH_BQ3img58qF',
                                         dirname, 'efficientnet-b6.pth')
        recovery_model = fix_layer(
            load(os.path.join(dirname, 'efficientnet-b6.pth')))
        recovery_model = _make_recovery_model_include_top(
            recovery_model,
            input_shape=input_shape,
            include_top=include_top,
            classes=classes,
            freeze_features=freeze_features)
        effb6.model = recovery_model
    else:
        effb6.model = _make_recovery_model_include_top(effb6.model,
                                                       include_top=include_top,
                                                       classes=classes,
                                                       freeze_features=False)

    effb6.model.input_shape = input_shape
    effb6.model.to(get_device())
    return effb6
Ejemplo n.º 30
0
def EfficientNetB5(include_top=True,
                   pretrained=True,
                   freeze_features=False,
                   input_shape=(3, 456, 456),
                   classes=1000,
                   **kwargs):
    if input_shape is not None and len(input_shape) == 3:
        input_shape = tuple(input_shape)
    else:
        input_shape = (3, 456, 456)
    effb5 = EfficientNet(1.6,
                         2.2,
                         input_shape,
                         0.4,
                         model_name='efficientnet-b5',
                         include_top=include_top,
                         num_classes=classes)
    if pretrained:
        download_model_from_google_drive('17iTD12G9oW3jYAui84MKtdY4gjd9vpgG',
                                         dirname, 'efficientnet-b5.pth')
        recovery_model = fix_layer(
            load(os.path.join(dirname, 'efficientnet-b5.pth')))
        recovery_model = _make_recovery_model_include_top(
            recovery_model,
            input_shape=input_shape,
            include_top=include_top,
            classes=classes,
            freeze_features=freeze_features)
        effb5.model = recovery_model
    else:
        effb5.model = _make_recovery_model_include_top(effb5.model,
                                                       include_top=include_top,
                                                       classes=classes,
                                                       freeze_features=False)

    effb5.model.input_shape = input_shape
    effb5.model.to(get_device())
    return effb5