def feature_extract(args):
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'  # close the warning
    os.environ["CUDA_VISIBLE_DEVICES"] = args.gpus
    cudnn.benchmark = True
    # == dataset config==
    num_class, data_length, image_tmpl = data_config(args)
    train_transforms, test_transforms, _ = augmentation_config(args)
    train_data_loader, val_data_loader, _, _, _, _ = data_loader_init(
        args, data_length, image_tmpl, train_transforms, test_transforms, _)
    # == model config==
    model = model_config(args, num_class)
    tc = TC(args)
    features = single_extract(tc, val_data_loader, model)
    return features
def main():
    os.environ["CUDA_VISIBLE_DEVICES"] = args.gpus
    # == dataset config==
    num_class, data_length, image_tmpl = data_config(args)
    train_transforms, test_transforms, eval_transforms = augmentation_config(args)
    _, _, eval_data_loader, _, _, _ = data_loader_init(args, data_length, image_tmpl, train_transforms,
                                                                test_transforms, eval_transforms)
    model = model_config(args, num_class)
    output = []
    total_num = len(eval_data_loader)
    for i, (data, label, index) in enumerate(eval_data_loader):
        proc_start_time = time.time()
        rst = eval_video(model, (i, data, label))
        output.append(rst)
        cnt_time = time.time() - proc_start_time
        if i % 10 == 0:
            print('video {} done, total {}/{}, average {} sec/video'.format(i, i + 1,
                                                                            total_num,
                                                                            float(cnt_time) / (i + 1)))
    date = datetime.datetime.today().strftime('%m-%d-%H%M')
    # =====output: every video's num and every video's label
    # =====x[0]:softmax value x[1]:label
    if not os.path.isdir("../experiments/evaluation/{}/{}".format(args.dataset,date)):
        os.mkdir("../experiments/evaluation/{}/{}".format(args.dataset,date))
    video_pred = [np.argmax(x[0]) for x in output]
    np.save("../experiments/evaluation/{}/{}/video_pred.npy".format(args.dataset,date), video_pred)
    video_labels = [x[1] for x in output]
    np.save("../experiments/evaluation/{}/{}/video_labels.npy".format(args.dataset, date), video_labels)
    cf = confusion_matrix(video_labels, video_pred).astype(float)
    np.save("../experiments/evaluation/{}/{}/confusion.npy".format(args.dataset, date), cf)
    cf_name = "../experiments/evaluation/{}/{}/confusion.npy".format(args.dataset, date)
    cls_cnt = cf.sum(axis=1)
    cls_hit = np.diag(cf)
    cls_acc = cls_hit / cls_cnt
    print(cls_acc)
    print('Accuracy {:.02f}%'.format(np.mean(cls_acc) * 100))

    name_list = [x.strip().split()[0] for x in open(args.val_list)]
    order_dict = {e: i for i, e in enumerate(sorted(name_list))}
    reorder_output = [None] * len(output)
    reorder_label = [None] * len(output)
    for i in range(len(output)):
        idx = order_dict[name_list[i]]
        reorder_output[idx] = output[i]
        reorder_label[idx] = video_labels[i]
    np.savez('../experiments/evaluation/' + args.dataset + '/' + date + "/" + args.mode +  'res',
             scores=reorder_output, labels=reorder_label)
    return cf_name
Esempio n. 3
0
def main():
    # =
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'  # close the warning
    os.environ["CUDA_VISIBLE_DEVICES"] = args.gpus
    torch.manual_seed(1)
    cudnn.benchmark = True
    # == dataset config==
    num_class, data_length, image_tmpl = data_config(args)
    train_transforms, test_transforms, eval_transforms = augmentation_config(
        args)
    train_data_loader, val_data_loader, _, _, _, _ = data_loader_init(
        args, data_length, image_tmpl, train_transforms, test_transforms,
        eval_transforms)
    # == model config==
    model = model_config(args, num_class)
    pos_aug = GenPositive()
    # == train and eval==
    test(train_data_loader, model, pos_aug)
    return 1
Esempio n. 4
0
def main():

    args = parse_option()
    if args.gpu is not None:
        print("Use GPU: {} for training".format(args.gpu))
    # create model and optimizer
    # == dataset config==
    """
    CUDA_VISIBLE_DEVICES=0,1 python train_temporal_dis.py \
     --batch_size 16 --num_workers 8 --nce_k 3569 --softmax --moco
    """

    # args.dataset = 'hmdb51'
    # args.train_list = '../datasets/lists/hmdb51/hmdb51_rgb_train_split_1.txt'
    # args.val_list = '../datasets/lists/hmdb51/hmdb51_rgb_val_split_1.txt'
    """
    CUDA_VISIBLE_DEVICES=1 python train_temporal_dis.py \
     --batch_size 16 --num_workers 8 --nce_k 9536 --softmax --moco
    """
    # args.print_freq = 100
    # args.dataset = 'ucf101'
    # args.train_list = '../datasets/lists/ucf101/ucf101_rgb_train_split_1.txt'
    # args.val_list = '../datasets/lists/ucf101/ucf101_rgb_val_split_1.txt'

    # args.print_freq = 1000
    # args.dataset = 'kinetics'
    # args.train_list = '../datasets/lists/kinetics-400/ssd_kinetics_video_trainlist.txt'
    # args.val_list = '../datasets/lists/kinetics-400/ssd_kinetics_video_vallist.txt'

    args.dropout = 0.5
    args.clips = 1
    args.data_length = 16
    args.stride = 4
    args.spatial_size = 224
    args.root = ""
    args.mode = 'rgb'
    args.eval_indict = 'loss'
    args.pt_loss = 'TemporalDis'
    args.workers = 4
    # args.arch = 'i3d' # 'r2p1d'
    num_class, data_length, image_tmpl = data_config(args)
    train_transforms, test_transforms, eval_transforms = augmentation_config(
        args)
    train_loader, val_loader, eval_loader, train_samples, val_samples, eval_samples = data_loader_init(
        args, data_length, image_tmpl, train_transforms, test_transforms,
        eval_transforms)

    n_data = len(train_loader)
    if args.arch == 'i3d':
        model = I3D(num_classes=101,
                    modality=args.mode,
                    dropout_prob=args.dropout,
                    with_classifier=False)
        model_ema = I3D(num_classes=101,
                        modality=args.mode,
                        dropout_prob=args.dropout,
                        with_classifier=False)
    elif args.arch == 'r2p1d':
        model = R2Plus1DNet((1, 1, 1, 1),
                            num_classes=num_class,
                            with_classifier=False)
        model_ema = R2Plus1DNet((1, 1, 1, 1),
                                num_classes=num_class,
                                with_classifier=False)
    elif args.arch == 'r3d':
        from model.r3d import resnet18
        model = resnet18(num_classes=num_class, with_classifier=False)
        model_ema = resnet18(num_classes=num_class, with_classifier=False)
    else:
        Exception("Not implemene error!")
    model = torch.nn.DataParallel(model)
    model_ema = torch.nn.DataParallel(model_ema)
    # random initialization
    model.apply(weights_init)
    model_ema.apply(weights_init)
    # copy weights from `model' to `model_ema'
    moment_update(model, model_ema, 0)
    contrast = MemoryMoCo(128, n_data, args.nce_k, args.nce_t,
                          args.softmax).cuda(args.gpu)
    # contrast2 = MemoryMoCo(128, n_data, args.nce_k, args.nce_t, args.softmax).cuda(args.gpu)
    criterion = NCESoftmaxLoss() if args.softmax else NCECriterion(n_data)
    criterion = criterion.cuda(args.gpu)
    cls_criterion = nn.CrossEntropyLoss().cuda()

    model = model.cuda()
    if args.moco:
        model_ema = model_ema.cuda()

    optimizer = torch.optim.SGD(model.parameters(),
                                lr=args.learning_rate,
                                momentum=args.momentum,
                                weight_decay=args.weight_decay)

    cudnn.benchmark = True

    if args.amp:
        model, optimizer = amp.initialize(model,
                                          optimizer,
                                          opt_level=args.opt_level)
        if args.moco:
            optimizer_ema = torch.optim.SGD(model_ema.parameters(),
                                            lr=0,
                                            momentum=0,
                                            weight_decay=0)
            model_ema, optimizer_ema = amp.initialize(model_ema,
                                                      optimizer_ema,
                                                      opt_level=args.opt_level)

    # optionally resume from a checkpoint
    args.start_epoch = 1
    if args.resume:
        if os.path.isfile(args.resume):
            print("=> loading checkpoint '{}'".format(args.resume))
            checkpoint = torch.load(args.resume, map_location='cpu')
            # checkpoint = torch.load(args.resume)
            args.start_epoch = checkpoint['epoch'] + 1
            model.load_state_dict(checkpoint['model'])
            optimizer.load_state_dict(checkpoint['optimizer'])
            contrast.load_state_dict(checkpoint['contrast'])
            if args.moco:
                model_ema.load_state_dict(checkpoint['model_ema'])

            if args.amp and checkpoint['opt'].amp:
                print('==> resuming amp state_dict')
                amp.load_state_dict(checkpoint['amp'])

            print("=> loaded successfully '{}' (epoch {})".format(
                args.resume, checkpoint['epoch']))
            del checkpoint
            torch.cuda.empty_cache()
        else:
            print("=> no checkpoint found at '{}'".format(args.resume))

    # tensorboard
    logger = tb_logger.Logger(logdir=args.tb_folder, flush_secs=2)
    logger2 = tb_logger.Logger(logdir=args.tb_folder2, flush_secs=2)

    #==================================== our data augmentation method=================================
    pos_aug = GenPositive()
    neg_aug = GenNegative()

    # routine
    for epoch in range(args.start_epoch, args.epochs + 1):

        adjust_learning_rate(epoch, args, optimizer)
        print("==> training...")

        time1 = time.time()
        loss, prob = train_moco(epoch, train_loader, model, model_ema,
                                contrast, criterion, optimizer, args, pos_aug,
                                neg_aug)
        time2 = time.time()
        print('epoch {}, total time {:.2f}'.format(epoch, time2 - time1))
        saving(logger, loss, epoch, optimizer, args, model, contrast, prob,
               model_ema, 'TemporalDis')
Esempio n. 5
0
def train_and_eval(args):
    # =
    global lowest_val_loss, best_prec1
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'  # close the warning
    os.environ["CUDA_VISIBLE_DEVICES"] = args.gpus
    torch.manual_seed(1)
    cudnn.benchmark = True
    timer = Timer()
    recorder = Record(args)
    # == dataset config==
    num_class, data_length, image_tmpl = data_config(args)
    train_transforms, test_transforms = augmentation_config(args)
    train_data_loader, val_data_loader = data_loader_init(
        args, data_length, image_tmpl, train_transforms, test_transforms)
    # == model config==
    models = []
    optimizers = []
    for i in range(args.mutual_num):
        model = model_config(args, num_class)
        models.append(model)
    recorder.record_message('a', '=' * 100)
    recorder.record_message('a', str(model.module))
    recorder.record_message('a', '=' * 100)
    # == optim config==
    for i in range(args.mutual_num):
        train_criterion, val_criterion, optimizer = optim_init(args, model)
        optimizers.append(optimizer)
    # == data augmentation(self-supervised) config==
    tc = TC(args)
    # == train and eval==
    for epoch in range(args.start_epoch, args.epochs):
        timer.tic()
        for i in range(args.mutual_num):
            adjust_learning_rate(optimizers[i], args.lr, epoch, args.lr_steps)
        if args.eval_indict == 'acc':
            train_prec1, train_loss = train(args, tc, train_data_loader,
                                            models, train_criterion,
                                            optimizers, epoch, recorder)
            # train_prec1, train_loss = random.random() * 100, random.random()
            recorder.record_train(train_loss / 5.0, train_prec1 / 100.0)
        else:
            train_loss = train(args, tc, train_data_loader, models,
                               train_criterion, optimizers, epoch, recorder)
            # train_prec1, train_loss = random.random() * 100, random.random()
            recorder.record_train(train_loss)
        if (epoch + 1) % args.eval_freq == 0:
            if args.eval_indict == 'acc':
                val_prec1, val_loss = validate(args, tc, val_data_loader,
                                               models, val_criterion, recorder)
                # val_prec1, val_loss = random.random() * 100, random.random()
                recorder.record_val(val_loss / 5.0, val_prec1 / 100.0)
                is_best = val_prec1 > best_prec1
                best_prec1 = max(val_prec1, best_prec1)
                checkpoint = {
                    'epoch': epoch + 1,
                    'arch': "i3d",
                    'state_dict': model.state_dict(),
                    'best_prec1': best_prec1
                }
            else:
                val_loss = validate(args, tc, val_data_loader, models,
                                    val_criterion, recorder)
                # val_loss = random.random()
                # val_prec1, val_loss = random.random() * 100, random.random()
                recorder.record_val(val_loss)
                is_best = val_loss < lowest_val_loss
                lowest_val_loss = min(val_loss, lowest_val_loss)
                checkpoint = {
                    'epoch': epoch + 1,
                    'arch': "i3d",
                    'state_dict': model.state_dict(),
                    'lowest_val': lowest_val_loss
                }
        recorder.save_model(checkpoint, is_best)
        timer.toc()
        left_time = timer.average_time * (args.epochs - epoch)

        if args.eval_indict == 'acc':
            message = "best_prec1 is: {} left time is : {}".format(
                best_prec1, timer.format(left_time))
        else:
            message = "lowest_val_loss is: {} left time is : {}".format(
                lowest_val_loss, timer.format(left_time))
        print(message)
        recorder.record_message('a', message)
    # return recorder.best_name
    return recorder.filename