def load_pytorch_model(model_name): import torchvision.models as models switcher = { 'alexnet': lambda: models.alexnet(pretrained=True).eval(), "vgg11": lambda: models.vgg11(pretrained=True).eval(), "vgg11_bn": lambda: models.vgg11_bn(pretrained=True).eval(), "vgg13": lambda: models.vgg13(pretrained=True).eval(), "vgg13_bn": lambda: models.vgg13_bn(pretrained=True).eval(), "vgg16": lambda: models.vgg16(pretrained=True).eval(), "vgg16_bn": lambda: models.vgg16_bn(pretrained=True).eval(), "vgg19": lambda: models.vgg19(pretrained=True).eval(), "vgg19_bn": lambda: models.vgg19_bn(pretrained=True).eval(), "resnet18": lambda: models.resnet18(pretrained=True).eval(), "resnet34": lambda: models.resnet34(pretrained=True).eval(), "resnet50": lambda: models.resnet50(pretrained=True).eval(), "resnet101": lambda: models.resnet101(pretrained=True).eval(), "resnet152": lambda: models.resnet152(pretrained=True).eval(), "squeezenet1_0": lambda: models.squeezenet1_0(pretrained=True).eval(), "squeezenet1_1": lambda: models.squeezenet1_1(pretrained=True).eval(), "densenet121": lambda: models.densenet121(pretrained=True).eval(), "densenet161": lambda: models.densenet161(pretrained=True).eval(), "densenet201": lambda: models.densenet201(pretrained=True).eval(), "inception_v3": lambda: models.inception_v3(pretrained=True).eval(), } _load_model = switcher.get(model_name, None) _model = _load_model() return _model
def _load_pytorch_model(model_name, summary): import torchvision.models as models switcher = { 'alexnet': lambda: models.alexnet(pretrained=True).eval(), "vgg11": lambda: models.vgg11(pretrained=True).eval(), "vgg11_bn": lambda: models.vgg11_bn(pretrained=True).eval(), "vgg13": lambda: models.vgg13(pretrained=True).eval(), "vgg13_bn": lambda: models.vgg13_bn(pretrained=True).eval(), "vgg16": lambda: models.vgg16(pretrained=True).eval(), "vgg16_bn": lambda: models.vgg16_bn(pretrained=True).eval(), "vgg19": lambda: models.vgg19(pretrained=True).eval(), "vgg19_bn": lambda: models.vgg19_bn(pretrained=True).eval(), "resnet18": lambda: models.resnet18(pretrained=True).eval(), "resnet34": lambda: models.resnet34(pretrained=True).eval(), "resnet50": lambda: models.resnet50(pretrained=True).eval(), "resnet101": lambda: models.resnet101(pretrained=True).eval(), "resnet152": lambda: models.resnet152(pretrained=True).eval(), "squeezenet1_0": lambda: models.squeezenet1_0(pretrained=True).eval(), "squeezenet1_1": lambda: models.squeezenet1_1(pretrained=True).eval(), "densenet121": lambda: models.densenet121(pretrained=True).eval(), "densenet161": lambda: models.densenet161(pretrained=True).eval(), "densenet201": lambda: models.densenet201(pretrained=True).eval(), "inception_v3": lambda: models.inception_v3(pretrained=True).eval(), } _load_model = switcher.get(model_name, None) _model = _load_model() import torch if torch.cuda.is_available(): _model = _model.cuda() from perceptron.models.classification.pytorch import PyTorchModel as ClsPyTorchModel import numpy as np mean = np.array([0.485, 0.456, 0.406]).reshape((3, 1, 1)) std = np.array([0.229, 0.224, 0.225]).reshape((3, 1, 1)) pmodel = ClsPyTorchModel(_model, bounds=(0, 1), num_classes=1000, preprocessing=(mean, std)) return pmodel
bbox_pred = Dense(nb_rois * 4, name="bbox_pred")(x) return cls_score, bbox_pred TEST_FULL_IMG = mpimg.imread("test1.jpg") imgs = np.array([TEST_FULL_IMG]) nb_anchors = len(anchor_box_scales) * len(anchor_box_ratio) print("imgs shape", imgs.shape) img_input = Input(shape=(None, None, 3)) block5_conv3 = vgg16(input_tensor=img_input) rpn_class, rpn_regr, rpn_conv = rpn(block5_conv3, nb_anchors) cls_score, bbox_pred = rcnn(block5_conv3, rpn_regr, rpn_conv, 21, 21, trainable=True) rpn_model = Model(img_input, [rpn_regr, rpn_conv], name='rpn') fasterRcnn = Model(img_input, [cls_score, bbox_pred], name="fasterRcnn") model = load_vgg_weights(model) res = model.predict(imgs) print("res shape", res.shape)