def main():

    means = (104, 117, 123)  # only support voc now

    exp_name = '{}-SSD-{}-{}-bs-{}-{}-lr-{:05d}'.format(
        args.net_type, args.dataset, args.input_type, args.batch_size,
        args.cfg['base'], int(args.lr * 100000))

    args.save_root += args.dataset + '/'
    args.listid = '01'  ## would be usefull in JHMDB-21
    print('Exp name', exp_name, args.listid)
    for iteration in [
            int(itr) for itr in args.eval_iter.split(',') if len(itr) > 0
    ]:
        log_file = open(
            args.save_root + 'cache/' + exp_name +
            "/testing-{:d}.log".format(iteration), "w", 1)
        log_file.write(exp_name + '\n')
        trained_model_path = args.save_root + 'cache/' + exp_name + '/ssd300_ucf24_' + repr(
            iteration) + '.pth'
        log_file.write(trained_model_path + '\n')
        num_classes = len(CLASSES) + 1  #7 +1 background
        if args.cfg['base'] == 'fpn':
            assert (args.ssd_dim == 512)
            net = FPNSSD512(num_classes, args.cfg)
        elif args.cfg['base'] == 'vgg16':
            if args.cfg['min_dim'][0] == 512:
                net = SSD512(num_classes, args.cfg)
                net.loc_layers.apply(weights_init)
                net.cls_layers.apply(weights_init)
            else:
                net = build_vgg_ssd(args.num_classes, args.cfg)
                net.loc.apply(weights_init)
                net.conf.apply(weights_init)
        elif args.cfg['base'] == 'mobile_v1_512':
            net = MobileSSD512(num_classes, args.cfg)
        elif args.cfg['base'] == 'fpn_mobile_512':
            net = MobileFPNSSD512(num_classes, args.cfg)
        elif args.cfg['base'] == 'yolov3':
            net = Darknet('model/yolov3/cfg/yolov3-spp.cfg', arc='CE')
        else:
            return

        net.load_state_dict(torch.load(trained_model_path))
        net.eval()
        if args.cuda:
            net = net.cuda()
            cudnn.benchmark = True
        print('Finished loading model %d !' % iteration)

        # evaluation
        torch.cuda.synchronize()
        log_file.write('Testing net \n')
        test_net(net, args.save_root, exp_name, args.input_type, iteration,
                 num_classes)
def main():
    all_versions = [v1, v2, v3, v4, v5]
    args.cfg = all_versions[int(args.version) - 1]
    args.basenet = args.cfg['base'] + '_reducedfc.pth'
    args.outshape = args.cfg['min_dim']
    args.train_sets = 'train'
    args.means = (104, 117, 123)
    CLASSES = UCF24_CLASSES if args.dataset == 'ucf24' else JHMDB_CLASSES
    num_classes = len(CLASSES) + 1
    args.num_classes = num_classes
    args.stepvalues = [int(val) for val in args.stepvalues.split(',')]
    args.loss_reset_step = 30
    args.eval_step = 10000
    args.print_step = 10

    ## Define the experiment Name will used to same directory and ENV for visdom
    args.exp_name = '{}-SSD-{}-{}-bs-{}-{}-lr-{:05d}'.format(
        args.net_type, args.dataset, args.input_type, args.batch_size,
        args.cfg['base'], int(args.lr * 100000))

    args.save_root += args.dataset + '/'
    args.save_root = args.save_root + 'cache/' + args.exp_name + '/'

    if not os.path.isdir(args.save_root):
        os.makedirs(args.save_root)

    def xavier(param):
        init.xavier_uniform(param)

    def weights_init(m):
        if isinstance(m, nn.Conv2d):
            xavier(m.weight.data)
            m.bias.data.zero_()

    if args.cfg['base'] == 'fpn':
        net = FPNSSD512(args.num_classes, args.cfg)
    elif args.cfg['base'] == 'fpn_cube':
        net = FPNSSD512CUBE(args.num_classes, args.cfg)
    elif args.cfg['base'] == 'vgg16':
        if args.cfg['min_dim'][0] == 512:
            net = SSD512(args.num_classes, args.cfg)
            net.loc_layers.apply(weights_init)
            net.cls_layers.apply(weights_init)
        else:
            net = build_vgg_ssd(args.num_classes, args.cfg)
            net.loc.apply(weights_init)
            net.conf.apply(weights_init)
    elif args.cfg['base'] == 'fpn_mobile':
        net = MobileFPNSSD512(args.num_classes, args.cfg)
    elif args.cfg['base'] == 'yolov3':
        net = Darknet('model/yolov3/cfg/yolov3-spp.cfg', arc='CE')
    else:
        return

    if args.input_type == 'fastOF':
        print(
            'Download pretrained brox flow trained model weights and place them at:::=> ',
            args.data_root + 'ucf24/train_data/brox_wieghts.pth')
        pretrained_weights = args.data_root + 'ucf24/train_data/brox_wieghts.pth'
        print('Loading base network...')
        net.fpn.load_state_dict(torch.load(pretrained_weights))
    elif args.cfg['base'] == 'vgg16' or args.cfg['base'] == 'fpn':
        vgg_weights = torch.load(args.data_root + 'ucf24/train_data/' +
                                 args.basenet)
        print('Loading base network...')
        net.load_weights(vgg_weights)

    if args.net_type != 'conv2d' and (args.cfg['base'] == 'vgg16'
                                      or args.cfg['base'] == 'fpn'):
        net.transform(args.net_type)

    if args.cuda:
        net = net.cuda()

    args.data_root += args.dataset + '/'

    parameter_dict = dict(net.named_parameters(
    ))  # Get parmeter of network in dictionary format wtih name being key
    params = []

    #Set different learning rate to bias layers and set their weight_decay to 0
    for name, param in parameter_dict.items():
        if name.find('bias') > -1:
            # print(name, 'layer parameters will be trained @ {}'.format(args.lr*2))
            params += [{
                'params': [param],
                'lr': args.lr * 2,
                'weight_decay': 0
            }]
        else:
            # print(name, 'layer parameters will be trained @ {}'.format(args.lr))
            params += [{
                'params': [param],
                'lr': args.lr,
                'weight_decay': args.weight_decay
            }]

    optimizer = optim.SGD(params,
                          lr=args.lr,
                          momentum=args.momentum,
                          weight_decay=args.weight_decay)
    criterion = SphMultiBoxLoss(args.num_classes, 0.5, True, 0, True, 3, 0.5,
                                False, args.cfg, args.cuda)
    scheduler = MultiStepLR(optimizer,
                            milestones=args.stepvalues,
                            gamma=args.gamma)
    train(args, net, optimizer, criterion, scheduler)
コード例 #3
0
def main():

    args.means = (104, 117, 123)  # only support voc now

    exp_name = '{}-SSD-{}-{}-bs-{}-{}-lr-{:05d}'.format(
        args.net_type, args.dataset, args.input_type, args.batch_size,
        args.cfg['base'], int(args.lr * 100000))

    args.save_root += args.dataset + '/'
    args.data_root += args.dataset + '/'
    args.listid = '01'  ## would be usefull in JHMDB-21
    print('Exp name', exp_name, args.listid)

    #
    for iteration in [
            int(itr) for itr in args.eval_iter.split(',') if len(itr) > 0
    ]:
        log_file = open(
            args.save_root + 'cache/' + exp_name +
            "/testing-{:d}-{:0.2f}.log".format(iteration, args.iou_thresh),
            "w", 1)
        log_file.write(exp_name + '\n')
        trained_model_path = args.save_root + 'cache/' + exp_name + '/ssd300_ucf24_' + repr(
            iteration) + '.pth'
        log_file.write(trained_model_path + '\n')
        num_classes = len(CLASSES) + 1  #7 +1 background
        if args.cfg['base'] == 'fpn':
            net = FPNSSD512(num_classes, args.cfg)
        else:
            if args.cfg['min_dim'][0] == 512:
                net = SSD512(num_classes, args.cfg)
            else:
                net = build_vgg_ssd(num_classes, args.cfg)
        net.load_state_dict(torch.load(trained_model_path))
        net.eval()
        if args.cuda:
            net = net.cuda()
            cudnn.benchmark = True
        print('Finished loading model %d !' % iteration)
        # Load dataset
        if args.dataset == 'ucf24':
            dataset = OmniUCF24(args.data_root,
                                'test',
                                BaseTransform(300, args.means),
                                AnnotationTransform(),
                                input_type=args.input_type,
                                outshape=args.outshape,
                                full_test=True)
        else:
            dataset = OmniJHMDB(args.data_root,
                                'test',
                                BaseTransform(300, None),
                                AnnotationTransform(),
                                outshape=args.outshape)

        # evaluation
        torch.cuda.synchronize()
        tt0 = time.perf_counter()
        log_file.write('Testing net \n')
        test_net(net, args.save_root, exp_name, args.input_type, dataset,
                 iteration, num_classes, log_file)

        log_file.close()