示例#1
0
 def test_fbnet_flops(self):
     for x in ["fbnet_a", "fbnet_cse", "dmasking_f1"]:
         print(f"model name: {x}")
         model = fbnet(x, pretrained=False)
         res = model.arch_def.get("input_size", 224)
         input = torch.zeros([1, 3, res, res])
         flops_utils.print_model_flops(model, input)
示例#2
0
 def test_fbnet_v2(self):
     load_pretrained = True
     for name in [
             "fbnet_a",
             "fbnet_b",
             "fbnet_c",
             "FBNetV2_F1",
             "FBNetV2_F2",
             "FBNetV2_F3",
             "FBNetV2_F4",
             "FBNetV2_L1",
             "FBNetV2_L2",
             "FBNetV3_A",
             "FBNetV3_B",
             "FBNetV3_C",
             "FBNetV3_D",
             "FBNetV3_E",
             "FBNetV3_F",
             "FBNetV3_G",
     ]:
         with self.subTest(arch=name):
             print(f"Testing {name}...")
             model = fbnet(name, pretrained=load_pretrained)
             res = model.arch_def.get("input_size", 224)
             print(f"Test res: {res}")
             data = torch.zeros([1, 3, res, res])
             out = model(data)
             self.assertEqual(out.size(), torch.Size([1, 1000]))
示例#3
0
 def test_fbnet_v2(self):
     load_pretrained = True
     for name in ["fbnet_cse", "dmasking_l2_hs"]:
         print(f"Testing {name}...")
         model = fbnet(name, pretrained=load_pretrained)
         res = model.arch_def.get("input_size", 224)
         print(f"Test res: {res}")
         data = torch.zeros([1, 3, res, res])
         out = model(data)
         self.assertEqual(out.size(), torch.Size([1, 1000]))
示例#4
0
 def test_fbnet_flops(self):
     for x in [
             "fbnet_c",
             # "FBNetV2_F1",
             # "FBNetV2_F5",
     ]:
         print(f"model name: {x}")
         model = fbnet(x, pretrained=False)
         res = model.arch_def.get("input_size", 224)
         inputs = (torch.zeros([1, 3, res, res]), )
         flops_utils.print_model_flops(model, inputs)
示例#5
0
def run_flops_estimation():
    # fbnet models, supported models could be found in
    # mobile_cv/model_zoo/models/model_info/fbnet_v2/*.json
    model_name = "dmasking_l2_hs"
    model = fbnet(model_name, pretrained=False)
    model.eval()

    res = model.arch_def.get("input_size", 224)
    input_batch = torch.zeros([1, 3, res, res])
    with torch.no_grad():
        flops_utils.print_model_flops(model, input_batch)
示例#6
0
    def test_fbnet_arch_def(self):
        model_arch = {
            "blocks": [
                # [c, s, n]
                # stage 0
                [["conv_k3_hs", 16, 2, 1]],
                # stage 1
                [["ir_k3", 16, 2, 1]],
                # stage 2
                [["ir_k3", 24, 2, 1]],
                # stage 3
                [["ir_pool_hs", 24, 1, 1]],
            ]
        }

        model = fbnet(model_arch, pretrained=False, num_classes=8)
        data = torch.zeros([1, 3, 32, 32])
        out = model(data)
        self.assertEqual(out.size(), torch.Size([1, 8]))
def run_fbnet_v2():
    # fbnet models, supported models could be found in
    # mobile_cv/model_zoo/models/model_info/fbnet_v2/*.json
    model_name = "dmasking_l3"

    # load model
    model = fbnet(model_name, pretrained=True)
    model.eval()
    preprocess = get_preprocess(model.arch_def.get("input_size", 224))

    # load and process input
    input_image = _get_input()
    input_tensor = preprocess(input_image)
    input_batch = input_tensor.unsqueeze(0)

    # run model
    with torch.no_grad():
        output = model(input_batch)
    output_softmax = torch.nn.functional.softmax(output[0], dim=0)
    print(output_softmax.max(0))
    cnt = 0
    for line in file.readlines():
        cnt = cnt + 1
        i = 29
        tmp_str = ''
        while (line[i] != '\n'):
            tmp_str += line[i]
            i = i + 1
        # print(tmp_str)
        label.append(int(tmp_str))
        if (cnt >= 50000):
            break
print('label read done! ')
# print('label = ',label)

model = fbnet("fbnet_a", pretrained=True)  # 47%
# model = models.resnet50(pretrained=True) # 57%
model.eval()  # set to not change the weights
total = 50000
hit = 0.
for i in range(1, total + 1):
    # get the input image
    if (i < 10):
        test_path = './test_image/' + 'ILSVRC2012_val_0000000' + str(
            i) + '.JPEG'
    elif (i < 100):
        test_path = './test_image/' + 'ILSVRC2012_val_000000' + str(
            i) + '.JPEG'
    elif (i < 1000):
        test_path = './test_image/' + 'ILSVRC2012_val_00000' + str(i) + '.JPEG'
    elif (i < 10000):
示例#9
0
from mobile_cv.model_zoo.models.fbnet_v2 import fbnet
from ptflops import get_model_complexity_info
import torch
from thop import profile

model_name = None  # set this by yourself. What's more, dmasking is FBNetv2's another name. fbnet is FBNetv1.
if model_name == "dmasking_f4_1.08_1.08_300":
    model = fbnet("dmasking_f4_1.08_1.08_300",
                  scale_factor=1.06,
                  pretrained=False)  # 300.03M
elif model_name == "dmasking_f4_1.2_1.2_400":
    model = fbnet("dmasking_f4_1.2_1.2_400",
                  scale_factor=1.2,
                  pretrained=False)  # 405.23M
elif model_name == "dmasking_f4_1.28_1.28_500":
    model = fbnet("dmasking_f4_1.28_1.28_500",
                  scale_factor=1.35,
                  pretrained=False)  # 500.15M
elif model_name == "fbnet_a_1.07_1.07_300":
    model = fbnet("fbnet_a_1.07_1.07_300",
                  scale_factor=1.125,
                  pretrained=False)  # 330.15M
elif model_name == "fbnet_a_1.2_1.17_400":
    model = fbnet("fbnet_a_1.2_1.17_400", scale_factor=1.2,
                  pretrained=False)  # 410.15M
elif model_name == "fbnet_a_1.25_1.26_500":
    model = fbnet("fbnet_a_1.25_1.26_500",
                  scale_factor=1.325,
                  pretrained=False)  # 508.15M
else:
    raise NotImplementedError
示例#10
0
            if isinstance(source_layer, nn.Conv2d):
                transpose_dims = [2,3,1,0]
                self.kModel.layers[target_layer].set_weights([source_layer.weight.data.numpy(
                ).transpose(transpose_dims), source_layer.bias.data.numpy()])
            elif isinstance(source_layer, nn.BatchNorm2d):
                self.kModel.layers[target_layer].set_weights([source_layer.weight.data.numpy(), source_layer.bias.data.numpy(),
                                                              source_layer.running_mean.data.numpy(), source_layer.running_var.data.numpy()])

    def save_model(self, output_file):
        self.kModel.save(output_file)

    def save_weights(self, output_file):
        self.kModel.save_weights(output_file, save_format='h5')


fbnet_pretrained_model = fbnet("fbnet_a", pretrained=True)
print('pretrained model loaded! ')
# print('model = ')
# print(fbnet_pretrained_model)

# get keras model
keras_model = KerasNet(input_shape=(224, 224, 3))
print('keras model loaded! ')

# get pytorch model
pytorch_model = fbnet_pretrained_model

# transfer weights
converter = PytorchToKeras(pytorch_model, keras_model)
converter.convert((3, 224, 224))
print('convert completed!')