Example #1
0
    def __init__(self, num_classes, img_scale=448):
        super(RACNN, self).__init__()

        self.b1 = mobilenet.mobilenet_v2(num_classes=num_classes)
        self.b2 = mobilenet.mobilenet_v2(num_classes=num_classes)
        self.b3 = mobilenet.mobilenet_v2(num_classes=num_classes)
        self.classifier1 = nn.Linear(320, num_classes)
        self.classifier2 = nn.Linear(320, num_classes)
        self.classifier3 = nn.Linear(320, num_classes)
        self.feature_pool = torch.nn.AdaptiveAvgPool2d(output_size=1)
        self.atten_pool = nn.MaxPool2d(kernel_size=2, stride=2)
        self.crop_resize = AttentionCropLayer()
        self.apn1 = nn.Sequential(
            nn.Linear(320 * 14 * 14, 1024),
            nn.Tanh(),
            nn.Linear(1024, 3),
            nn.Sigmoid(),
        )
        self.apn2 = nn.Sequential(
            nn.Linear(320 * 7 * 7, 1024),
            nn.Tanh(),
            nn.Linear(1024, 3),
            nn.Sigmoid(),
        )
        self.echo = None
    def __init__(self, cfg):
        super().__init__()

        # set pretrained=True while training
        self.backbone = mobilenet_v2(pretrained=True)

        num_history_channels = (cfg["model_params"]["history_num_frames"] +
                                1) * 2
        num_in_channels = 3 + num_history_channels

        self.backbone.features[0][0] = nn.Conv2d(
            # num_in_channels = 25
            num_in_channels,
            self.backbone.features[0][0].out_channels,
            kernel_size=self.backbone.features[0][0].kernel_size,
            stride=self.backbone.features[0][0].stride,
            padding=self.backbone.features[0][0].padding,
            bias=False,
        )

        # X, Y coords for the future positions (output shape: Bx50x2)
        num_targets = 2 * cfg["model_params"]["future_num_frames"]

        # Fully connected layer.
        self.backbone.classifier[1] = nn.Linear(
            in_features=self.backbone.classifier[1].in_features,
            out_features=num_targets)
Example #3
0
    def __init__(self,
                 subtype='mobilenet_v2',
                 out_stages=[3, 5, 7],
                 backbone_path=None):
        super(MobileNetV2, self).__init__()
        self.out_stages = out_stages
        self.backbone_path = backbone_path

        if subtype == 'mobilenet_v2':
            features = mobilenet_v2(pretrained=not self.backbone_path).features
            self.out_channels = [32, 16, 24, 32, 64, 96, 160, 320]
        else:
            raise NotImplementedError

        self.out_channels = self.out_channels[self.out_stages[0]:self.
                                              out_stages[-1] + 1]

        self.conv1 = nn.Sequential(list(features.children())[0])
        self.stage1 = nn.Sequential(list(features.children())[1])
        self.stage2 = nn.Sequential(*list(features.children())[2:4])
        self.stage3 = nn.Sequential(*list(features.children())[4:7])
        self.stage4 = nn.Sequential(*list(features.children())[7:11])
        self.stage5 = nn.Sequential(*list(features.children())[11:14])
        self.stage6 = nn.Sequential(*list(features.children())[14:17])
        self.stage7 = nn.Sequential(list(features.children())[17])

        if self.backbone_path:
            self.backbone.load_state_dict(torch.load(self.backbone_path))
        else:
            self.init_weights()
Example #4
0
    def __init__(self, pretrained=False, num_classes=5):
        super().__init__()
        # Pretrained model
        self.datapath = os.path.join(os.getcwd(), 'data')

        if pretrained:
            self.model = mobilenet_v2(pretrained=True)
            for params in self.model.features.parameters(recurse=True):
                params.requires_grad = False
            output_features_nb = 1280
            fc_layer = nn.Linear(output_features_nb, num_classes, bias=True)
            self.model.classifier = nn.Sequential(nn.Dropout(0.2), fc_layer)
            torch.nn.init.xavier_normal_(fc_layer.weight, gain=1.0)
            torch.nn.init.zeros_(fc_layer.bias)

            # self.model = resnet34(pretrained=True)
            # for params in self.model.parameters(recurse=True):
            #     params.requires_grad = False
            # output_features_nb = self.model.fc.weight.size(1)
            # self.model.fc = nn.Linear(output_features_nb, num_classes, bias=True)
            # for params in self.model.fc.parameters(recurse=True):
            #     params.requires_grad = True
            # torch.nn.init.xavier_normal_(self.model.fc.weight, gain=1.0)
            # torch.nn.init.zeros_(self.model.fc.bias)

        # From scratch model
        else:
            features = MobileNetV2(num_classes=num_classes)
            # Freeze the network except the classifier
            for params in features.features.parameters(recurse=True):
                params.requires_grad = False
            self.model = features

        self.softmax = nn.Softmax(dim=1)
Example #5
0
    def __init__(self):
        super(Model, self).__init__()
        base = mobilenet_v2(pretrained=True)

        self.feature = base.features
        self.classification_1 = nn.Sequential(nn.Linear(1280 * 7 * 7, 17))
        self.classification_2 = nn.Sequential(nn.Linear(1280 * 7 * 7, 11))
        self.classification_3 = nn.Sequential(nn.Linear(1280 * 7 * 7, 4))
        self.classification_4 = nn.Sequential(nn.Linear(1280 * 7 * 7, 39))
def mobilenet(pretrained=False, fixed_feature=True):
	""" Mobile-net V2 model from torchvision's resnet model.

	:param pretrained: if true, return a model pretrained on ImageNet
	:param fixed_feature: if true and pretrained is true, model features are fixed while training.
	"""
	from torchvision.models.mobilenet import mobilenet_v2
	model = mobilenet_v2(pretrained)
	features = model.features[:-1]

	ff = True if pretrained and fixed_feature else False
	return _Mobilenet(model, features, ff)
Example #7
0
    def __init__(self, kernel_size=3, pretrained=True):
        super(MobileHairNet, self).__init__()
        mobilenet = mobilenet_v2(pretrained=True)
#######################################################################################################################
#                                                                                                                     #
#                                               ENCODER                                                               #
#                                                                                                                     #
#######################################################################################################################
        self.encode_layer1 = nn.Sequential(*list(mobilenet.features)[:2])
        self.encode_layer2 = nn.Sequential(*list(mobilenet.features)[2:4])
        self.encode_layer3 = nn.Sequential(*list(mobilenet.features)[4:7])
        self.encode_layer4 = nn.Sequential(*list(mobilenet.features)[7:14])
        self.encode_layer5 = nn.Sequential(*list(mobilenet.features)[14:19])

#######################################################################################################################
#                                                                                                                     #
#                                               DECODER                                                               #
#                                                                                                                     #
#######################################################################################################################
        self.decode_layer1 = nn.Upsample(scale_factor=2)
        self.decode_layer2 = nn.Sequential(
            nn.Conv2d(in_channels=1280, out_channels=64, kernel_size=1),
            _Layer_Depwise_Decode(in_channel=64, out_channel=64, kernel_size=kernel_size),
            nn.Upsample(scale_factor=2)
        )
        self.decode_layer3 = nn.Sequential(
            _Layer_Depwise_Decode(in_channel=64, out_channel=64, kernel_size=kernel_size),
            nn.Upsample(scale_factor=2)
        )
        self.decode_layer4 = nn.Sequential(
            _Layer_Depwise_Decode(in_channel=64, out_channel=64, kernel_size=kernel_size),
            nn.Upsample(scale_factor=2)
        )
        self.decode_layer5 = nn.Sequential(
            _Layer_Depwise_Decode(in_channel=64, out_channel=64, kernel_size=kernel_size),
            nn.Upsample(scale_factor=2),
            _Layer_Depwise_Decode(in_channel=64, out_channel=64, kernel_size=kernel_size),
            nn.Conv2d(in_channels=64, out_channels=2, kernel_size=kernel_size, padding=1)
        )
        self.encode_to_decoder4 = nn.Conv2d(in_channels=96, out_channels=1280, kernel_size=1)
        self.encode_to_decoder3 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=1)
        self.encode_to_decoder2 = nn.Conv2d(in_channels=24, out_channels=64, kernel_size=1)
        self.encode_to_decoder1 = nn.Conv2d(in_channels=16, out_channels=64, kernel_size=1)

        self.soft_max = nn.Softmax(dim=1)

        if pretrained:
            self._init_weight()
Example #8
0
def main():

    # hyper parameters
    epochs = 200
    batch_size = 8
    w = 10
    alpha = 100

    print("Loading all detected objects in dataset...")

    train_path = "/media/lab/TOSHIBAEXT/BuildingData/training"
    dataset = Dataset(train_path)  # 自定义的数据集

    params = {"batch_size": batch_size, "shuffle": True, "num_workers": 6}

    generator = data.DataLoader(dataset, **params)  # 读取Dataset中的数据

    base_model = mobilenet.mobilenet_v2(pretrained=True)  # 加载模型并设置为预训练模式
    model = Model(features=base_model).cuda()

    # 选择不同的优化方法
    opt_Momentum = torch.optim.SGD(model.parameters(), lr = 0.0001, momentum = 0.9)
      = torch.optim.RMSprop(model.parameters(), lr = 0.0001, alpha = 0.9)
Example #9
0
    def __init__(self, backbone, num_color, num_style, num_season,
                 num_category):
        super(Model, self).__init__()
        if backbone == 'mobilenet_v2':
            base_network = mobilenet_v2(pretrained=True)
            self.features = base_network.features
            self.out_filters = base_network.last_channel
        elif backbone == 'vgg11_bn':
            base_network = vgg11_bn(pretrained=True)
            self.features = base_network.features
            self.out_filters = 512
        else:
            raise Exception(f"[!] Invalid model: {backbone}")

        self.avg_pool = nn.Sequential(nn.ReflectionPad2d(1),
                                      nn.Conv2d(self.out_filters, 512, 3),
                                      nn.ReLU(), nn.AdaptiveAvgPool2d((7, 7)))

        self.color_classifier = nn.Linear(512 * 7 * 7, num_color)
        self.style_classifier = nn.Linear(512 * 7 * 7, num_style)
        #        self.part_classifier = nn.Linear(512 * 7 * 7, num_part)
        self.season_classifier = nn.Linear(512 * 7 * 7, num_season)
        self.category_classifier = nn.Linear(512 * 7 * 7, num_category)
Example #10
0
def main():

    weights_path = '/home/lab/Desktop/wzndeep/posenet-build--eular/contrast_experiment/weight/'
    model_lst = [
        x for x in sorted(os.listdir(weights_path)) if x.endswith('.pkl')
    ]
    if len(model_lst) == 0:
        print('No previous model found, please train first!')
        exit()
    else:
        print('Using previous model %s' % model_lst[-1])
        base_model = mobilenet.mobilenet_v2(pretrained=True)  # 加载模型并设置为预训练模式
        # model = Model(features=base_model).cuda()
        # base_model = MobileNetV3_Large()
        # state_dict = model_dict()
        # base_model.load_state_dict(state_dict)

        model = Model(features=base_model).cuda()
        checkpoint = torch.load(weights_path + '/%s' % model_lst[-1])
        model.load_state_dict(checkpoint['model_state_dict'])
        model.eval()

    # defaults to /eval
    eval_path = '/media/lab/TOSHIBAEXT/BuildingData/testing'
    dataset = Dataset(eval_path)  # 自定义的数据集

    all_images = dataset.all_objects()

    rot_errors_arccos = []
    rot_errors_single = []
    adds = []

    for key in sorted(all_images.keys()):

        start_time = time.time()

        data = all_images[key]

        truth_img = data['Image']
        img = np.copy(truth_img)
        objects = data['Objects']
        cam_to_img = data['Calib']

        for detectedObject in objects:
            label = detectedObject.label
            theta_ray = detectedObject.theta_ray
            input_img = detectedObject.img

            input_tensor = torch.zeros([1, 3, 224, 224]).cuda()
            input_tensor[0, :, :, :] = input_img
            input_tensor.cuda()

            ori_out = model(input_tensor)

            ori_out = F.normalize(ori_out, p=2, dim=1)
            ori_out = ori_out.squeeze(0).detach().cpu().numpy()

            gt_rot = label['Qu']

            # 四元数的反余弦距离
            rot_errors_arccos.append(rot_err_arccos(gt_rot, ori_out))
            # 单独方向的误差
            rot_errors_single.append(rot_err_single(gt_rot, ori_out))

        # print('Estimated patch|Truth patch: {:.3f}/{:.3f}'.format(
        #     patch, r_x))
        # print(
        #     'Estimated yaw|Truth yaw: {:.3f}/{:.3f}'.format(yaw, r_y))

    mean_rot_error_arccos = np.mean(rot_errors_arccos)
    mean_rot_error_single = np.mean(rot_errors_single, axis=0)
    # mean_add = np.mean(adds)

    print('=' * 50)
    print('Got %s poses in %.3f seconds\n' %
          (len(objects), time.time() - start_time))
    print("\tMean Rotation Error Norm: {:.3f}".format(mean_rot_error_arccos))
    print("\tMean Rotation Errors: patch: {:.3f}, yaw: {:.3f}, roll: {:.3f}".
          format(np.rad2deg(mean_rot_error_single[0]),
                 np.rad2deg(mean_rot_error_single[1]),
                 np.rad2deg(mean_rot_error_single[2])))
Example #11
0
def main():

    # hyper parameters
    epochs = 200
    batch_size = 8
    num_epochs_decay = 50
    sx = 0
    sq = 1
    lr = 0.0001
    learn_beta = True

    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

    print("Loading all detected objects in dataset...")

    train_path = "/media/lab/TOSHIBAEXT/BuildingData/training"
    dataset = Dataset(train_path)  # 自定义的数据集

    params = {'batch_size': batch_size,
              'shuffle': True,
              'num_workers': 6}

    generator = data.DataLoader(dataset, **params)  # 读取Dataset中的数据
    print('---------------')

    base_model = mobilenet.mobilenet_v2(pretrained=True)  # 加载模型并设置为预训练模式
    model = Model(features=base_model, fixed_weight=True).cuda()


    # criterion = PoseLoss(device, sq, learn_beta).to(device)

    criterion = nn.MSELoss().cuda()

    optimizer = torch.optim.SGD(model.parameters(), lr=0.0001, momentum=0.9)
    scheduler = lr_scheduler.StepLR(
        optimizer, step_size=num_epochs_decay, gamma=0.1)

    # load any previous weights
    model_path = '/home/lab/Desktop/wzndeep/posenet-build--eular/contrast_experiment/weight/'
    latest_model = None
    first_epoch = 0
    if not os.path.isdir(model_path):
        os.mkdir(model_path)
    else:
        try:
            latest_model = [x for x in sorted(
                os.listdir(model_path)) if x.endswith('.pkl')][-1]
        except:
            pass

    if latest_model is not None:
        # 解序列化一个pickled对象并加载到内存中
        checkpoint = torch.load(model_path + latest_model)
        # 加载一个state_dict对象,加载模型用于训练或验证
        model.load_state_dict(checkpoint['model_state_dict'])
        optimizer.load_state_dict(checkpoint['optimizer_state_dict'])  # 同上
        first_epoch = checkpoint['epoch']
        loss = checkpoint['loss']

        print('Found previous checkpoint: %s at epoch %s' %
              (latest_model, first_epoch))
        print('Resuming training....')

    # 训练网络

    total_num_batches = int(len(dataset) / batch_size)

    writer = SummaryWriter(
        '/home/lab/Desktop/wzndeep/posenet-build--eular/contrast_experiment/run')

    for epoch in range(first_epoch+1, epochs+1):  # 多批次循环
        curr_batch = 0
        passes = 0
        for local_batch, local_labels in generator:  # 获取输入数据

            ori_true = local_labels['Qu'].float().cuda()

            local_batch = local_batch.float().cuda()
            # pos_out, ori_out = model(local_batch)
            ori_out = model(local_batch)

            ori_out = F.normalize(ori_out, p=2, dim=1)
            ori_true = F.normalize(ori_true, p=2, dim=1)

            loss = criterion(ori_out, ori_true)

            writer.add_scalar('loss_total', loss, epoch)

            optimizer.zero_grad()  # 梯度置0
            loss.backward()  # 反向传播
            optimizer.step()  # 优化

            if passes % 10 == 0:  # 10轮显示一次,打印状态信息
                # print(
                #     "---{} ---Loss: total loss {:.3f} / ori loss {:.3f}".format(
                #         epoch, loss, loss_ori_print
                #     )
                # )
                print(
                    "---{} ---Loss: total loss {:.3f}".format(
                        epoch, loss
                    )
                )
                passes = 0

            passes += 1
            curr_batch += 1

        # save after every 10 epochs
        if epoch % 10 == 0:
            name = model_path + 'epoch_%s.pkl' % epoch
            print("====================")
            print("Done with epoch %s!" % epoch)
            print("Saving weights as %s ..." % name)
            torch.save({
                'epoch': epoch,
                'model_state_dict': model.state_dict(),
                'optimizer_state_dict': optimizer.state_dict(),
                'loss': loss
            }, name)
            print("====================")

    writer.close()
"""

this using Pilgrim convert MobileNetV3 to TensorRT engine

"""
from torch2trt_dynamic.torch2trt import torch2trt
import torch
from torch import nn
from torchvision.models.resnet import resnet50
from torchvision.models.mobilenet import mobilenet_v2

# create some regular pytorch model...
model = mobilenet_v2().cuda().eval()

# create example data
x = torch.ones((1, 3, 224, 224)).cuda()

# convert to TensorRT feeding sample data as input
opt_shape_param = [
    [
        [1, 3, 128, 128],   # min
        [1, 3, 256, 256],   # opt
        [1, 3, 512, 512]    # max
    ]
]
model_trt = torch2trt(model, [x], fp16_mode=False)

print('serialize engine...')
engine_path = 'mbv2.trt'
with open(engine_path, "wb") as f:
    f.write(model_trt.engine.serialize())
Example #13
0
 def __init__(self, width):
     super(TupleUnpack_backbone, self).__init__()
     self.model_backbone = mobilenet_v2(pretrained=False,
                                        width_mult=width,
                                        num_classes=3)
Example #14
0
# ============================ step 2/5 模型 ============================
device = torch.device(DEVICE)  # cpu, cuda:0

# net = LeNet(classes=2)
# net.initialize_weights()

if BACKBONE == 'resnet18':
    net = resnet18(num_classes=num_classes).to(device)
elif BACKBONE == 'resnet34':
    net = resnet34(num_classes=num_classes).to(device)
elif BACKBONE == 'resnet50':
    net = resnet50(num_classes=num_classes).to(device)
elif BACKBONE == 'densenet121':
    net = densenet121(num_classes=num_classes).to(device)
elif BACKBONE == 'mobilenet_v2':
    net = mobilenet_v2(num_classes=num_classes, width_mult=1.0, inverted_residual_setting=None, round_nearest=8).to(device)
elif BACKBONE == 'shufflenet_v2_x1_5':
    net = shufflenet_v2_x1_5(num_classes=num_classes).to(device)
elif BACKBONE == 'squeezenet1_0':
    net = squeezenet1_0(num_classes=num_classes).to(device)
elif BACKBONE == 'squeezenet1_1':
    net = squeezenet1_1(num_classes=num_classes).to(device)
elif BACKBONE == 'mnasnet0_5':
    net = mnasnet0_5(num_classes=num_classes).to(device)
elif BACKBONE == 'mnasnet1_0':
    net = mnasnet1_0(num_classes=num_classes).to(device)
elif BACKBONE == 'mobilenet_v1':
    net = MobileNetV1(num_classes=num_classes).to(device)
else:
    raise Exception('unknow backbone: {}'.format(BACKBONE))
Example #15
0
    transforms.Resize(
        (int(input_size * margin_scale), int(input_size * margin_scale))),
    transforms.CenterCrop(input_size),
    transforms.ToTensor(),
    transforms.Normalize(norm_mean, norm_std),
])

# test_data = dataset.MaskFaceDataset(test_dir, transform=valid_transform)
#
# test_loader = DataLoader(dataset=test_data, batch_size=BATCH_SIZE)

device = torch.device(DEVICE)  # cpu, cuda:0

# net = MobileNetV1(num_classes=2)  # .to(device)
net = mobilenet_v2(num_classes=2,
                   width_mult=0.35,
                   inverted_residual_setting=None,
                   round_nearest=8)  # .to(device)

# ============================ step 3/5 损失函数 ============================
criterion = nn.CrossEntropyLoss()  # .to(device)

# model_path = '/disk1/home/xiaj/dev/proml/maskface/save_model/20200313-041207-mobilenet_v2/acc0.9710-loss0.0034-epoch113.pth'
# model_path = '/disk1/home/xiaj/dev/proml/maskface/save_model/20200325-013239-mobilenet_v1/acc0.9551-loss0.0042-epoch29.pth'
# model_path = '/home/xiajun/dev/proml/maskface/save_model/20200325-021920-mobilenet_v1/acc0.9676-loss0.0037-epoch122.pth'
# model_path = '/disk1/home/xiaj/dev/proml/maskface/save_model/20200327-030712-mobilenet_v1/acc0.9602-loss0.0043-epoch128.pth'
# model_path = '/disk1/home/xiaj/dev/proml/maskface/save_model/20200327-052039-mobilenet_v1/acc0.9565-loss0.0043-epoch112.pth'
model_path = '/disk1/home/xiaj/dev/proml/maskface/save_model/20200402-235510-mobilenet_v2/acc0.9540-loss0.0043-epoch102.pth'

# model_path = '/home/xiajun/dev/proml/maskface/save_model/20200327-052039-mobilenet_v1/acc0.9565-loss0.0043-epoch112.pth'
net.load_state_dict(torch.load(model_path, map_location=device))
Example #16
0
def main():

    # hyper parameters
    epochs = 200
    batch_size = 8
    w = 1
    alpha = 1

    print("Loading all detected objects in dataset...")

    train_path = "/media/lab/TOSHIBAEXT/BuildingData/training"
    dataset = Dataset(train_path)  # 自定义的数据集

    params = {"batch_size": batch_size, "shuffle": True, "num_workers": 6}

    generator = data.DataLoader(dataset, **params)  # 读取Dataset中的数据

    base_model = mobilenet.mobilenet_v2(pretrained=True)  # 加载模型并设置为预训练模式
    # base_model = MobileNetV3_Large()
    # state_dict = model_dict()
    # base_model.load_state_dict(state_dict)

    model = Model(features=base_model).cuda()

    # 选择不同的优化方法
    opt_Momentum = torch.optim.SGD(model.parameters(), lr=0.0001, momentum=0.9)
    opt_RMSprop = torch.optim.RMSprop(model.parameters(), lr=0.0001, alpha=0.9)
    opt_Adam = torch.optim.Adam(model.parameters(),
                                lr=0.0001,
                                betas=(0.9, 0.99))
    opt_SGD = torch.optim.SGD(model.parameters(), lr=0.0001, momentum=0.9)

    conf_loss_func = nn.CrossEntropyLoss().cuda()
    orient_loss_func = OrientationLoss

    # load any previous weights
    model_path = ("/home/lab/Desktop/wzndeep/posenet-build--eular/weights/")
    latest_model = None
    first_epoch = 0
    if not os.path.isdir(model_path):
        os.mkdir(model_path)
    else:
        try:
            latest_model = [
                x for x in sorted(os.listdir(model_path)) if x.endswith(".pkl")
            ][-1]
        except:
            pass

    if latest_model is not None:
        # 解序列化一个pickled对象并加载到内存中
        checkpoint = torch.load(model_path + latest_model)
        # 加载一个state_dict对象,加载模型用于训练或验证
        model.load_state_dict(checkpoint["model_state_dict"])
        opt_SGD.load_state_dict(checkpoint["optimizer_state_dict"])  # 同上
        first_epoch = checkpoint["epoch"]
        loss = checkpoint["loss"]

        print("Found previous checkpoint: %s at epoch %s" %
              (latest_model, first_epoch))
        print("Resuming training....")

    # 训练网络

    total_num_batches = int(len(dataset) / batch_size)

    writer = SummaryWriter(
        "/home/lab/Desktop/wzndeep/posenet-build--eular/runs/")

    for epoch in range(first_epoch + 1, epochs + 1):  # 多批次循环
        curr_batch = 0
        passes = 0
        for local_batch, local_labels in generator:  # 获取输入数据

            truth_orient_patch = local_labels["Orientation_patch"].float(
            ).cuda()
            truth_conf_patch = local_labels["Confidence_patch"].long().cuda()
            truth_orient_yaw = local_labels["Orientation_yaw"].float().cuda()
            truth_conf_yaw = local_labels["Confidence_yaw"].long().cuda()

            local_batch = local_batch.float().cuda()
            [orient_patch, conf_patch, orient_yaw,
             conf_yaw] = model(local_batch)

            orient_patch_loss = orient_loss_func(orient_patch,
                                                 truth_orient_patch,
                                                 truth_conf_patch)

            # softmax函数的输出值进行操作,每行——>返回每行最大值的索引
            truth_conf_patch = torch.max(truth_conf_patch, dim=1)[1]
            conf_patch_loss = conf_loss_func(conf_patch, truth_conf_patch)

            loss_patch = conf_patch_loss + w * orient_patch_loss

            orient_yaw_loss = orient_loss_func(orient_yaw, truth_orient_yaw,
                                               truth_conf_yaw)

            # softmax函数的输出值进行操作,每行——>返回每行最大值的索引
            truth_conf_yaw = torch.max(truth_conf_yaw, dim=1)[1]
            conf_yaw_loss = conf_loss_func(conf_yaw, truth_conf_yaw)

            loss_yaw = conf_yaw_loss + w * orient_yaw_loss

            total_loss = alpha * loss_patch + loss_yaw

            opt_RMSprop.zero_grad()  # 梯度置0
            total_loss.backward()  # 反向传播
            opt_RMSprop.step()  # 优化

            if passes % 10 == 0:  # 10轮显示一次,打印状态信息
                print(
                    "--- epoch %s | batch %s/%s --- [loss_patch: %s] | [loss_yaw: %s] | [total_loss: %s]"
                    % (
                        epoch,
                        curr_batch,
                        total_num_batches,
                        loss_patch.item(),
                        loss_yaw.item(),
                        total_loss.item(),
                    ))
                passes = 0

            passes += 1
            curr_batch += 1
        writer.add_scalar("loss_total", total_loss, epoch)
        writer.add_scalar("loss_patch", loss_patch, epoch)
        writer.add_scalar("orient_patch_loss", orient_patch_loss, epoch)
        writer.add_scalar("conf_patch_loss", conf_patch_loss, epoch)

        # save after every 10 epochs
        if epoch % 20 == 0:
            name = model_path + "epoch_%s.pkl" % epoch
            print("====================")
            print("Done with epoch %s!" % epoch)
            print("Saving weights as %s ..." % name)
            torch.save(
                {
                    "epoch": epoch,
                    "model_state_dict": model.state_dict(),
                    "optimizer_state_dict": opt_SGD.state_dict(),
                    "loss": total_loss,
                },
                name,
            )
            print("====================")

    writer.close()
def main():

    weights_path = '/home/lab/Desktop/wzndeep/posenet-build--eular/weights/'
    model_lst = [x for x in sorted(
        os.listdir(weights_path)) if x.endswith('.pkl')]
    if len(model_lst) == 0:
        print('No previous model found, please train first!')
        exit()
    else:
        print('Using previous model %s' % model_lst[-1])
        base_model = mobilenet.mobilenet_v2(pretrained=True)  # 加载模型并设置为预训练模式
        # model = Model(features=base_model).cuda()
        # base_model = MobileNetV3_Large()
        # state_dict = model_dict()
        # base_model.load_state_dict(state_dict)

        model = Model(features=base_model).cuda()
        checkpoint = torch.load(weights_path + '/%s' % model_lst[-1])
        model.load_state_dict(checkpoint['model_state_dict'])
        model.eval()

    # defaults to /eval
    eval_path = '/media/lab/TOSHIBAEXT/BuildingData/testing'
    dataset = Dataset(eval_path)  # 自定义的数据集

    all_images = dataset.all_objects()

    trans_errors_norm = []
    trans_errors_single = []
    rot_errors_arccos = []
    rot_errors_single = []
    adds = []

    for key in sorted(all_images.keys()):

        start_time = time.time()

        data = all_images[key]

        truth_img = data['Image']
        image = Image.open(truth_img)
        objects = data['Objects']
        cam_to_img = data['Calib']

        for detectedObject in objects:
            label = detectedObject.label
            theta_ray = detectedObject.theta_ray
            input_img = detectedObject.img

            input_tensor = torch.zeros([1, 3, 224, 224]).cuda()
            input_tensor[0, :, :, :] = input_img
            input_tensor.cuda()

            [orient_patch, conf_patch, orient_yaw,
                conf_yaw, est_trans] = model(input_tensor)
            orient_patch = orient_patch.cpu().data.numpy()[0, :, :]
            conf_patch = conf_patch.cpu().data.numpy()[0, :]
            orient_yaw = orient_yaw.cpu().data.numpy()[0, :, :]
            conf_yaw = conf_yaw.cpu().data.numpy()[0, :]
            est_trans = est_trans.cpu().data.numpy()

            argmax_patch = np.argmax(conf_patch)
            orient_patch = orient_patch[argmax_patch, :]
            cos = orient_patch[0]
            sin = orient_patch[1]
            patch = np.arctan2(sin, cos)
            patch += dataset.angle_bins[argmax_patch]

            argmax_yaw = np.argmax(conf_yaw)
            orient_yaw = orient_yaw[argmax_yaw, :]
            cos_yaw = orient_yaw[0]
            sin_yaw = orient_yaw[1]
            yaw = np.arctan2(sin_yaw, cos_yaw)
            yaw += dataset.angle_bins[argmax_yaw]
            if (yaw > (2 * np.pi)):
                yaw -= (2 * np.pi)
            # yaw -= np.pi

            roll = 0.

            r_x = label['Patch']
            r_y = label['Yaw']
            r_z = label['Roll']
            gt_trans = label['Location']

            gt_rot = np.array([r_x, r_y, r_z])
            est_rot = np.array([patch, yaw, roll])

            trans_errors = trans_error(gt_trans, est_trans)
            trans_errors_norm.append(trans_errors[0])
            trans_errors_single.append(trans_errors[1])

            rot_errors = rot_error(gt_rot, est_rot)
            rot_errors_arccos.append(rot_errors[0])
            rot_errors_single.append(rot_errors[1])

            dim = label['Dimensions']

            adds.append(add_err(dim, gt_trans, est_trans, gt_rot, est_rot))

            # 画图
            # dim = label['Dimensions']
            # bbox = label['Box_2D']
            # draw(image, bbox, cam_to_img, dim, gt_trans, est_trans, r_x, r_y, r_z, patch, yaw, roll)

        print('Estimated patch|Truth patch: {:.3f}/{:.3f}'.format(
            patch, r_x))
        print(
            'Estimated loction', est_trans)
        print(
            'Truth loction', gt_trans)
        print('----------')

        # plt.show()
        # plt.savefig(
        #     '/home/lab/Desktop/wzndeep/posenet-build--eular/model_26/{}_proj'.format(key))
        # plt.close()

    mean_rot_error_arccos = np.mean(rot_errors_arccos)
    mean_rot_error_single = np.mean(rot_errors_single, axis=0)
    mean_trans_error_norm = np.mean(trans_errors_norm)
    mean_trans_error_single = np.mean(trans_errors_single, axis=0)
    mean_add = np.mean(adds)

    print('=' * 50)
    print('Got %s poses in %.3f seconds\n' %
          (len(objects), time.time() - start_time))
    print("\tMean Rotation Error Norm: {:.3f}".format(mean_rot_error_arccos))
    print("\tMean Rotation Errors: patch: {:.3f}, yaw: {:.3f}, roll: {:.3f}".format(
        np.rad2deg(mean_rot_error_single[0]), np.rad2deg(mean_rot_error_single[1]),
        np.rad2deg(mean_rot_error_single[2])))
    print("\tMean Trans Error Norm: {:.3f}".format(mean_trans_error_norm))
    print("\tMean Trans Errors: X: {:.3f}, Y: {:.3f}, Z: {:.3f}".format(
        mean_trans_error_single[0][0], mean_trans_error_single[0][1],
        mean_trans_error_single[0][2]))
    print("\tMean ADD: {:.3f}".format(mean_add))
Example #18
0
def main_worker(gpu, ngpus_per_node, args):
    global best_acc1
    args.gpu = gpu

    if args.gpu is not None:
        print("Use GPU: {} for training".format(args.gpu))

    if args.distributed:
        if args.dist_url == "env://" and args.rank == -1:
            args.rank = int(os.environ["RANK"])
        if args.multiprocessing_distributed:
            # For multiprocessing distributed training, rank needs to be the
            # global rank among all the processes
            args.rank = args.rank * ngpus_per_node + gpu
        dist.init_process_group(backend=args.dist_backend,
                                init_method=args.dist_url,
                                world_size=args.world_size,
                                rank=args.rank)

    if not torch.cuda.is_available():
        print('using CPU, this will be slow')
    elif args.distributed:
        # For multiprocessing distributed, DistributedDataParallel constructor
        # should always set the single device scope, otherwise,
        # DistributedDataParallel will use all available devices.
        if args.gpu is not None:
            torch.cuda.set_device(args.gpu)

            args.teacher_model = mobilenet_v2(pretrained=True)

            args.teacher_model.cuda(args.gpu)

            inputs = torch.randn([1, 3, 224, 224], dtype=torch.float32).cuda()

            ofa_pruner = OFAPruner(args.teacher_model, inputs)
            ofa_model = ofa_pruner.ofa_model(args.expand_ratio,
                                             args.channel_divisible,
                                             args.excludes)

            model = ofa_model.cuda(args.gpu)

            # When using a single GPU per process and per
            # DistributedDataParallel, we need to divide the batch size
            # ourselves based on the total number of GPUs we have
            args.batch_size = int(args.batch_size / ngpus_per_node)
            args.workers = int(
                (args.workers + ngpus_per_node - 1) / ngpus_per_node)
            model = torch.nn.parallel.DistributedDataParallel(
                model, device_ids=[args.gpu])

    # define loss function (criterion and kd loss) and optimizer
    criterion = nn.CrossEntropyLoss().cuda(args.gpu)
    soft_criterion = cross_entropy_loss_with_soft_target().cuda(args.gpu)

    optimizer = sgd_optimizer(model, args.lr, args.momentum, args.weight_decay)

    lr_scheduler = CosineAnnealingLR(
        optimizer=optimizer,
        T_max=args.epochs * IMAGENET_TRAINSET_SIZE // args.batch_size //
        ngpus_per_node)

    # optionally resume from a checkpoint
    if args.resume:
        if os.path.isfile(args.resume):
            print("=> loading checkpoint '{}'".format(args.resume))
            if args.gpu is None:
                checkpoint = torch.load(args.resume)
            else:
                # Map model to be loaded to specified single gpu.
                loc = 'cuda:{}'.format(args.gpu)
                checkpoint = torch.load(args.resume, map_location=loc)
            args.start_epoch = checkpoint['epoch']
            best_acc1 = checkpoint['best_acc1']
            if args.gpu is not None:
                # best_acc1 may be from a checkpoint from a different GPU
                best_acc1 = best_acc1.to(args.gpu)
            model.load_state_dict(checkpoint['state_dict'])
            optimizer.load_state_dict(checkpoint['optimizer'])
            lr_scheduler.load_state_dict(checkpoint['scheduler'])
            print("=> loaded checkpoint '{}' (epoch {})".format(
                args.resume, checkpoint['epoch']))
        else:
            print("=> no checkpoint found at '{}'".format(args.resume))

    cudnn.benchmark = True

    # Data loading code
    traindir = os.path.join(args.data, 'train')
    valdir = os.path.join(args.data, 'val')
    normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                     std=[0.229, 0.224, 0.225])

    train_dataset = datasets.ImageFolder(
        traindir,
        transforms.Compose([
            transforms.RandomResizedCrop(224),
            transforms.RandomHorizontalFlip(),
            transforms.ToTensor(),
            normalize,
        ]))

    if args.distributed:
        train_sampler = torch.utils.data.distributed.DistributedSampler(
            train_dataset)
    else:
        train_sampler = None

    train_loader = torch.utils.data.DataLoader(train_dataset,
                                               batch_size=args.batch_size,
                                               shuffle=(train_sampler is None),
                                               num_workers=args.workers,
                                               pin_memory=True,
                                               sampler=train_sampler)

    val_dataset = datasets.ImageFolder(
        valdir,
        transforms.Compose([
            transforms.Resize(256),
            transforms.CenterCrop(224),
            transforms.ToTensor(),
            normalize,
        ]))
    val_loader = torch.utils.data.DataLoader(val_dataset,
                                             batch_size=args.batch_size,
                                             shuffle=False,
                                             num_workers=args.workers,
                                             pin_memory=True)

    for epoch in range(args.start_epoch, args.epochs):
        if args.distributed:
            train_sampler.set_epoch(epoch)

        if epoch == args.start_epoch:
            validate_subnet(train_loader, val_loader, model, ofa_pruner,
                            criterion, args, False)

        # train for one epoch
        train(train_loader, model, ofa_pruner, criterion, soft_criterion,
              optimizer, epoch, args, lr_scheduler)

        # evaluate on validation set
        validate_subnet(train_loader, val_loader, model, ofa_pruner, criterion,
                        args, True)

        if not args.multiprocessing_distributed or (
                args.multiprocessing_distributed
                and args.rank % ngpus_per_node == 0):
            save_checkpoint({
                'epoch': epoch + 1,
                'state_dict': model.state_dict(),
                'best_acc1': best_acc1,
                'optimizer': optimizer.state_dict(),
                'scheduler': lr_scheduler.state_dict(),
            })
Example #19
0
    k = 0
    for idx, layer in enumerate(params):
        # print(type(layer))  # torch.nn.parameter.Parameter, Parameter(torch.Tensor)
        # layer is actually a tensor
        print('layer (%d):' % idx, layer.size(), end=', ')
        mul = 1
        for v in layer.size():  # [out_C, in_C, kernel_w, kernel_h]
            mul *= v
        print('params:', mul)
        k += mul
    print('total params:', k)


if __name__ == '__main__':
    m1 = resnet18(pretrained=False)
    m2 = mobilenet_v2(
        pretrained=False)  # If True, returns a model pre-trained on ImageNet

    # model_params(m1)
    model_params(m2)

# load/save model


def load_model(model,
               model_path,
               optimizer=None,
               resume=False,
               lr=None,
               lr_step=None):
    """
    load a model from pretrain, same layer use pretrain, different layer use default
Example #20
0
def main(*args, **kwargs):

    # ----------------------------------------
    # General settings
    # ----------------------------------------
    args = parse_arguments()
    torch.manual_seed(args.seed)
    torch.backends.cudnn.deterministic = True
    torch.backends.cudnn.benchmark = False
    np.random.seed(args.seed)
    use_cuda = not args.no_cuda and torch.cuda.is_available()
    device = torch.device("cuda" if use_cuda else "cpu")

    chkpt_dir = 'CHECKPOINTS/{}{}/{}'.format(args.config_name, args.postfix,
                                             args.target_mode)
    os.makedirs(chkpt_dir, exist_ok=True)
    set_logger(os.path.join(chkpt_dir, 'run.log'))

    # Convert the pretrained mobilenetv2 from torchvision
    if args.config_name == 'MobileNetV2_ImageNet' and args.target_mode == 'baseline':

        from torchvision.models import mobilenet
        train_loader, test_loader, num_classes = dataset_handler['ImageNet'](
            128, 128, 224, num_workers=24, pin_memory=True)

        orig_model = mobilenet.mobilenet_v2(pretrained=True)
        model = network_handler['MobileNetV2'](num_classes=1000)
        model = model.to(device)
        model.load_state_dict(orig_model.state_dict())
        criterion = nn.CrossEntropyLoss()

        test_loss, test_acc = test(model, device, test_loader, criterion, -1)
        content = {}
        content['state_dict'] = model.state_dict()
        content['test_acc'] = test_acc
        content['training_records'] = {
            'trn_loss': [],
            'trn_acc': [],
            'tst_loss': [],
            'tst_acc': []
        }
        content['epoch'] = -1

        chkpt_dir = 'CHECKPOINTS/MobileNetV2_ImageNet/baseline'
        os.makedirs(chkpt_dir, exist_ok=True)

        torch.save(content, os.path.join(chkpt_dir, 'baseline.pth'))

        return  # Don't need to train after model conversion

    config = imp.load_source('', 'configs/' + args.config_name + '.py').config
    mode_spec = config['modes'][args.target_mode]
    assert (mode_spec['type'] in ['baseline', 'prune'])

    # ----------------------------------------
    # Build the data loaders
    # ----------------------------------------
    dataset_name = config['dataset']
    batch_size = mode_spec['other_opt']['batch_size']
    train_loader, test_loader, num_classes = dataset_handler[dataset_name](
        batch_size,
        batch_size,
        mode_spec['other_opt']['image_size'],
        num_workers=mode_spec['other_opt']['num_workers'],
        pin_memory=True)

    # ----------------------------------------
    # Build the network
    # ----------------------------------------
    network_name = config['network']
    model = network_handler[network_name](num_classes=num_classes)
    if use_cuda:
        model = nn.DataParallel(model)
    model = model.to(device)
    print(model)

    # ----------------------------------------
    # Runing trainval loops
    # ----------------------------------------
    if mode_spec['type'] == 'baseline':
        run_baseline(mode_spec, model, device, train_loader, test_loader,
                     chkpt_dir)
    else:
        run_prune(mode_spec, model, device, train_loader, test_loader,
                  chkpt_dir, network_name)
    return
      if i % 50 == 0:
        progress.display(i)

    # TODO: this should also be done with the ProgressMeter
    print(' * Acc@1 {top1.avg:.3f} Acc@5 {top5.avg:.3f}'.format(
        top1=top1, top5=top5))

  return float(top1.avg), float(top5.avg)

if __name__ == '__main__':

  gpus = get_gpus(args.gpus)

  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

  model = mobilenet_v2(pretrained=True)

  traindir = os.path.join(args.data_dir, 'train')
  valdir = os.path.join(args.data_dir, 'validation')

  normalize = transforms.Normalize(
      mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])

  train_dataset = datasets.ImageFolder(
      traindir,
      transforms.Compose([
          transforms.RandomResizedCrop(224),
          transforms.RandomHorizontalFlip(),
          transforms.ToTensor(),
          normalize,
      ]))
Example #22
0
# -*- coding: utf-8 -*-
# @Author  : DevinYang([email protected])

import torch
from torchtoolbox.tools import summary
from torchvision.models.resnet import resnet50
from torchvision.models.mobilenet import mobilenet_v2

model1 = resnet50()
model2 = mobilenet_v2()


def test_summary():
    summary(model1, torch.rand((1, 3, 224, 224)), True)
    summary(model2, torch.rand((1, 3, 224, 224)))
Example #23
0
 def __init__(self, num_classes=2):
     super().__init__()
     self.net = mobilenet_v2(pretrained=True)  #     backbone + neck + head
     self.avg_pool = nn.AdaptiveAvgPool2d(1)
     self.logit = nn.Linear(1280, len(
         args.classes))  # [bs, 1280] -> [bs, classes]
Example #24
0
 def test_mobilenet(self):
     x = Variable(torch.randn(BATCH_SIZE, 3, 224, 224).fill_(1.0))
     self.exportTest(toC(mobilenet_v2()), toC(x), rtol=1e-3, atol=1e-5)
Example #25
0
                return score_box_point_loss(scores, deltas, shapes, logits,
                                            pred_deltas, pred_shapes, mask)
            else:
                return score_box_loss(scores, deltas, logits, pred_deltas)
        else:
            scores = torch.sigmoid(logits)
            bboxes = self.decode_bboxes(pred_deltas)
            shapes = self.decode_points(pred_shapes)
            return scores, bboxes, shapes


if __name__ == '__main__':

    from torchvision.models.mobilenet import mobilenet_v2

    backbone = mobilenet_v2()

    from projects.retinanet.config import cfg
    prior = BBoxShapePrior(1, 10, cfg.anchors, 0.35, None, None)

    detector = Detector(prior, backbone)

    state = detector.state_dict()
    detector = Detector(BBoxShapePrior(), backbone)
    detector.load_state_dict(state)
    detector.cuda()

    torch.save({
        'prior': prior.state_dict(),
    }, 'prior.pt')
Example #26
0
def main():

    weights_path = '/home/lab/Desktop/wzndeep/posenet-build--eular/contrast_experiment/weights/'
    model_lst = [x for x in sorted(
        os.listdir(weights_path)) if x.endswith('.pkl')]
    if len(model_lst) == 0:
        print('No previous model found, please train first!')
        exit()
    else:
        print('Using previous model %s' % model_lst[-1])
        base_model = mobilenet.mobilenet_v2(pretrained=True)  # 加载模型并设置为预训练模式
        # model = Model(features=base_model).cuda()
        # base_model = MobileNetV3_Large()
        # state_dict = model_dict()
        # base_model.load_state_dict(state_dict)

        model = Model(features=base_model).cuda()
        checkpoint = torch.load(weights_path + '/%s' % model_lst[-1])
        model.load_state_dict(checkpoint['model_state_dict'])
        model.eval()

    # defaults to /eval
    eval_path = '/media/lab/TOSHIBAEXT/BuildingData/testing'
    dataset = Dataset(eval_path)  # 自定义的数据集

    all_images = dataset.all_objects()

    trans_errors_norm = []
    trans_errors_single = []
    rot_errors_arccos = []
    rot_errors_single = []
    adds = []

    for key in sorted(all_images.keys()):

        start_time = time.time()

        data = all_images[key]

        truth_img = data['Image']
        img = np.copy(truth_img)
        objects = data['Objects']
        cam_to_img = data['Calib']

        for detectedObject in objects:
            label = detectedObject.label
            theta_ray = detectedObject.theta_ray
            input_img = detectedObject.img

            input_tensor = torch.zeros([1, 3, 224, 224]).cuda()
            input_tensor[0, :, :, :] = input_img
            input_tensor.cuda()

            rotation = model(input_tensor)
            rotation = rotation.cpu().data.numpy()
            print(rotation)

            patch_sin = rotation[0][0]
            patch_cos = rotation[0][1]
            yaw_sin = rotation[0][2]
            yaw_cos = rotation[0][3]
            patch = np.arctan2(patch_sin, patch_cos)
            yaw = np.arctan2(yaw_sin, yaw_cos)
            if yaw >= 1.57:
                yaw -= 1.57

            roll = 0.

            r_x = label['Patch']
            r_y = label['Yaw']
            r_z = label['Roll']

            gt_rot = np.array([r_x, r_y, r_z])
            est_rot = np.array([patch, yaw, roll])

            rot_errors = rot_error(gt_rot, est_rot)
            rot_errors_arccos.append(rot_errors[0])
            rot_errors_single.append(rot_errors[1])

        print('Estimated patch|Truth patch: {:.3f}/{:.3f}'.format(
            patch, r_x))
        print(
            'Estimated yaw|Truth yaw: {:.3f}/{:.3f}'.format(yaw, r_y))

    mean_rot_error_arccos = np.mean(rot_errors_arccos)
    mean_rot_error_single = np.mean(rot_errors_single, axis=0)
    # mean_add = np.mean(adds)

    print('=' * 50)
    print('Got %s poses in %.3f seconds\n' %
          (len(objects), time.time() - start_time))
    print("\tMean Rotation Error Norm: {:.3f}".format(mean_rot_error_arccos))
    print("\tMean Rotation Errors: patch: {:.3f}, yaw: {:.3f}, roll: {:.3f}".format(
        np.rad2deg(mean_rot_error_single[0]), np.rad2deg(mean_rot_error_single[1]),
        np.rad2deg(mean_rot_error_single[2])))
def quantization(title='optimize', model_name='', file_path=''):

    data_dir = args.data_dir
    quant_mode = args.quant_mode
    finetune = True
    deploy = args.deploy
    batch_size = args.batch_size
    subset_len = args.subset_len
    if quant_mode != 'test' and deploy:
        deploy = False
        print(
            r'Warning: Exporting xmodel needs to be done in quantization test mode, turn off it in this running!'
        )
    if deploy and (batch_size != 1 or subset_len != 1):
        print(
            r'Warning: Exporting xmodel needs batch size to be 1 and only 1 iteration of inference, change them automatically!'
        )
        batch_size = 1
        subset_len = 1

    model = mobilenet_v2().cpu()
    model.load_state_dict(torch.load(file_path))

    input = torch.randn([batch_size, 3, 224, 224])
    if quant_mode == 'float':
        quant_model = model
    else:
        ## new api
        ####################################################################################
        quantizer = torch_quantizer(quant_mode, model, (input), device=device)

        quant_model = quantizer.quant_model
        #####################################################################################

    # to get loss value after evaluation
    loss_fn = torch.nn.CrossEntropyLoss().to(device)

    val_loader, _ = load_data(subset_len=subset_len,
                              train=False,
                              batch_size=batch_size,
                              sample_method='random',
                              data_dir=data_dir,
                              model_name=model_name)

    # fast finetune model or load finetuned parameter before test
    if finetune == True:
        ft_loader, _ = load_data(subset_len=1024,
                                 train=False,
                                 batch_size=batch_size,
                                 sample_method=None,
                                 data_dir=data_dir,
                                 model_name=model_name)
        if quant_mode == 'calib':
            quantizer.fast_finetune(evaluate,
                                    (quant_model, ft_loader, loss_fn))
        elif quant_mode == 'test':
            quantizer.load_ft_param()

    # record  modules float model accuracy
    # add modules float model accuracy here
    acc_org1 = 0.0
    acc_org5 = 0.0
    loss_org = 0.0

    #register_modification_hooks(model_gen, train=False)
    acc1_gen, acc5_gen, loss_gen = evaluate(quant_model, val_loader, loss_fn)

    # logging accuracy
    print('loss: %g' % (loss_gen))
    print('top-1 / top-5 accuracy: %g / %g' % (acc1_gen, acc5_gen))

    # handle quantization result
    if quant_mode == 'calib':
        quantizer.export_quant_config()
    if deploy:
        quantizer.export_xmodel(deploy_check=False)
Example #28
0
 def __init__(self):
     super(MobileNetv2, self).__init__()
     mobilenet = mobilenet_v2(pretrained=True)
     self.features = mobilenet.features
Example #29
0
 def __init__(self):
     super().__init__()
     self.feature_extractor = mobilenet_v2(pretrained=True).features
     self.feature_extractor[14].conv[0][2].register_forward_hook(
         self.get_activation())
Example #30
0
def loss_selector(loss_net):
    #base
    if loss_net == "vgg16":
        from torchvision.models.vgg import vgg16
        net = vgg16(pretrained=True)
        loss_network = nn.Sequential(*list(net.features)[:31]).eval()
        return loss_network
    elif loss_net == "vgg16_bn":
        from torchvision.models.vgg import vgg16_bn
        net = vgg16_bn(pretrained=True)
        loss_network = nn.Sequential(*list(net.features)[:44]).eval()
        return loss_network
    elif loss_net == "resnet50":
        from torchvision.models.resnet import resnet50
        net=resnet50(pretrained=True)
        loss_network=nn.Sequential(*[child_module for child_module in net.children()][:-2]).eval()
        return loss_network
    elif loss_net == "resnet101":
        from torchvision.models.resnet import resnet101
        net=resnet101(pretrained=True)
        loss_network=nn.Sequential(*[child_module for child_module in net.children()][:-2]).eval()
        return loss_network
    elif loss_net == "resnet152":
        from torchvision.models.resnet import resnet152
        net=resnet152(pretrained=True)
        loss_network=nn.Sequential(*[child_module for child_module in net.children()][:-2]).eval()
        return loss_network
    elif loss_net == "squeezenet1_1":
        from torchvision.models.squeezenet import squeezenet1_1
        net=squeezenet1_1(pretrained=True)
        classifier=[item for item in net.classifier.modules()][1:-1]
        loss_network=nn.Sequential(*[net.features,*classifier]).eval()
        return loss_network
    elif loss_net == "densenet121":
        from torchvision.models.densenet import densenet121
        net=densenet121(pretrained=True)
        loss_network=nn.Sequential(*[net.features,nn.ReLU()]).eval()
        return loss_network
    elif loss_net == "densenet169":
        from torchvision.models.densenet import densenet169
        net=densenet169(pretrained=True)
        loss_network=nn.Sequential(*[net.features,nn.ReLU()]).eval()
        return loss_network
    elif loss_net == "densenet201":
        from torchvision.models.densenet import densenet201
        net=densenet201(pretrained=True)
        loss_network=nn.Sequential(*[net.features,nn.ReLU()]).eval()
        return loss_network        
    elif loss_net == "mobilenet_v2":
        from torchvision.models.mobilenet import mobilenet_v2
        net=mobilenet_v2(pretrained=True)
        loss_network=nn.Sequential(*[net.features]).eval()
        return loss_network                
    elif loss_net == "resnext50_32x4d":
        from torchvision.models.resnet import resnext50_32x4d
        net=resnext50_32x4d(pretrained=True)
        loss_network=nn.Sequential(*[child_module for child_module in net.children()][:-2]).eval()
        return loss_network      
    elif loss_net == "resnext101_32x8d":
        from torchvision.models.resnet import resnext101_32x8d
        net=resnext101_32x8d(pretrained=True)
        loss_network=nn.Sequential(*[child_module for child_module in net.children()][:-2]).eval()
        return loss_network
    elif loss_net == "wide_resnet50_2":
        from torchvision.models.resnet import wide_resnet50_2
        net=wide_resnet50_2(pretrained=True)
        loss_network=nn.Sequential(*[child_module for child_module in net.children()][:-2]).eval()
        return loss_network
    elif loss_net == "wide_resnet101_2":
        from torchvision.models.resnet import wide_resnet101_2
        net=wide_resnet101_2(pretrained=True)
        loss_network=nn.Sequential(*[child_module for child_module in net.children()][:-2]).eval()
        return loss_network
    elif loss_net == "inception_v3":