示例#1
0
def val_step(model, gpu, dataloader):
    model.train(False)
    apm = APMeter()
    tot_loss = 0.0
    error = 0.0
    num_iter = 0.
    num_preds = 0

    full_probs = {}

    # Iterate over data.
    for data in dataloader:
        num_iter += 1
        other = data[3]

        outputs, loss, probs, err = run_network(model, data, gpu)
        apm.add(probs.data.cpu().numpy()[0], data[2].numpy()[0])

        error += err.data[0]
        tot_loss += loss.data[0]

        # post-process preds
        outputs = outputs.squeeze()
        probs = probs.squeeze()
        fps = outputs.size()[1] / other[1][0]
        full_probs[other[0][0]] = (probs.data.cpu().numpy().T, fps)

    epoch_loss = tot_loss / num_iter
    error = error / num_iter
    print('val-map:', apm.value().mean())
    apm.reset()
    print('val-{} Loss: {:.4f} Acc: {:.4f}'.format(dataloader.root, epoch_loss,
                                                   error))

    return full_probs, epoch_loss
def val_step(model, gpu, dataloader):
    model.train(False)
    apm = APMeter()
    tot_loss = 0.0
    error = 0.0
    num_iter = 0.
    num_preds = 0

    full_probs = {}

    # Iterate over data.
    for data in dataloader:
        num_iter += 1
        other = data[3]

        outputs, loss, probs, err = run_network(model, data, gpu)
        apm.add(probs.data.cpu().numpy()[0], data[2].numpy()[0])

        error += err.item()  #data[0]
        tot_loss += loss.item()  #data[0]

        # post-process preds
        outputs = outputs.squeeze()
        probs = probs.squeeze()
        fps = outputs.size()[1] / other[1][0]
        full_probs[other[0][0]] = (probs.data.cpu().numpy().T, fps)

    epoch_loss = tot_loss / num_iter
    error = error / num_iter
    #print ('val-map:', apm.value().mean())
    #apm.reset()
    val_map = apm.value().mean()
    print('val-{} Loss: {:.4f} MAP: {:.4f}'.format(
        dataloader.root.split('/')[-1], epoch_loss, val_map))  #error
    #print('mu_x %f, sigma_x %f, mu_t %.10f, sigma_t %f, rho_xy %f'%(model.module.super_event.mu_x[0].item(), model.module.super_event.sigma_x[0].item(),
    #    model.module.super_event.mu_t[0].item(), model.module.super_event.sigma_t[0].item(), model.module.super_event.rho_xy[0].item()))
    #print ('LR:%f'%lr)
    #print('conv1 %f, fc1 %f'%(model.module.super_event.conv1.weight[0,0,0,0,0].item(), model.module.super_event.fc1.weight[0,0].item()))
    #print('sup_mat %f, per_frame %f'%(model.module.sup_mat[0][0][0].item(), model.module.per_frame.weight[0][0][0][0][0].item()))
    apm.reset()
    return full_probs, epoch_loss, val_map
def train_step(model, gpu, optimizer, dataloader, reg_margin):
    model.train(True)
    tot_loss = 0.0
    error = 0.0
    num_iter = 0.

    # Iterate over data.
    tr_apm = APMeter()
    for data in Bar(dataloader):
        optimizer.zero_grad()
        num_iter += 1
        reg = max(0.4,
                  0.05 * np.exp(-1 * num_iter / 500.) + (reg_margin - 0.05))
        #if num_iter<200: continue

        outputs, loss, probs, err = run_network(model,
                                                data,
                                                gpu,
                                                reg_margin=reg)
        #del outputs
        #print(err, loss)

        error += err.item()  #data[0]
        tot_loss += loss.item()  #data[0]

        loss.backward()
        optimizer.step()
        #print(probs.shape, data[2].shape)
        tr_apm.add(
            probs.view(-1, probs.shape[-1]).detach().cpu().numpy(),
            data[2].view(-1, data[2].shape[-1]).cpu().numpy())
    epoch_loss = tot_loss / num_iter
    error = error / num_iter
    print('train-{} Loss: {:.4f} MAP: {:.4f}'.format(
        dataloader.root.split('/')[-1], epoch_loss,
        tr_apm.value().mean()))  #error
    tr_apm.reset()
示例#4
0
def train_model(model, criterion, optimizer, num_epochs=50):
    since = time.time()
    val_res = {}
    best_acc = 0

    for epoch in range(num_epochs):
        print 'Epoch {}/{}'.format(epoch, num_epochs - 1)
        print '-' * 10

        # Each epoch has a training and validation phase
        for phase in ['train', 'val']:
            apm = APMeter()
            if phase == 'train':
                model.train(True)
            else:
                model.train(False)  # Set model to evaluate mode

            tot_loss = 0.0
            error = 0.0
            num_iter = 0.

            tr = {}
            # Iterate over data.
            for data in dataloaders[phase]:
                num_iter += 1
                # get the inputs
                features, mask, labels, name = data

                # wrap them in Variable
                features = Variable(features.cuda())
                labels = Variable(labels.float().cuda())
                mask = Variable(mask.cuda())  #.unsqueeze(1)

                # zero the parameter gradients
                optimizer.zero_grad()

                # forward

                #un-comment for max-pooling
                #features = torch.max(features, dim=2)[0].unsqueeze(2)
                #outputs = model(features)

                # un-comment for pyramid
                #b,c,t,h,w = features.size()
                #features = [pool(features,0,t), pool(features,0,t/2),pool(features,t/2,t),pool(features,0,t/4),pool(features,t/4,t/2),pool(features,t/2,3*t/4),pool(features,3*t/4,t)]
                #features = torch.cat(features, dim=1)
                #outputs = model(features)

                # sub-event learning
                outputs = model([features, torch.sum(mask, dim=1)])

                outputs = outputs.squeeze()  # remove spatial dims
                if features.size(0) == 1:
                    outputs = outputs.unsqueeze(0)
                #outputs = outputs.permute(0,2,1)

                # action-prediction loss
                loss = criterion(outputs, labels)

                probs = torch.sigmoid(outputs)
                apm.add(probs.data.cpu().numpy(),
                        (labels > 0.5).float().data.cpu().numpy())

                # backward + optimize only if in training phase
                if phase == 'train':
                    loss.backward()
                    optimizer.step()
                # statistics
                tot_loss += loss.data[0]

            epoch_loss = tot_loss / num_iter
            if phase == 'val' and apm.value().mean() > best_acc:
                best_acc = apm.value().mean()
                val_res = tr

            print '{} Loss: {:.4f} mAP: {:.4f}'.format(phase, epoch_loss,
                                                       apm.value().mean())
示例#5
0
        run([(model,0,dataloaders,optimizer, lr_sched, args.model_file)], num_epochs=60)

    else:
        print 'Evaluating...'
        rgb_model = nn.DataParallel(torch.load(args.rgb_model_file)
        rgb_model.train(False)
        
        dataloaders, datasets = load_data('', test_split, flow_root)

        rgb_results = eval_model(rgb_model, dataloaders['val'])

        flow_model = nn.DataParallel(torch.load(args.flow_model_files)
        flow_model.train(False)
            
        dataloaders, datasets = load_data('', test_split, flow_root)
            
        flow_results = eval_model(flow_model, dataloaders['val'])

        apm = APMeter()


        for vid in rgb_results.keys():
            o,p,l,fps = rgb_results[vid]
         
            if vid in flow_results:
                o2,p2,l2,fps = flow_results[vid]
                o = (o[:o2.shape[0]]*.5+o2*.5)
                p = (p[:p2.shape[0]]*.5+p2*.5)
            apm.add(sigmoid(o), l)
        print 'MAP:', apm.value().mean()
示例#6
0
    else:
        print('Evaluating...')
        rgb_model = torch.load(args.rgb_model_file)
        rgb_model.cuda()
        dataloaders, datasets = load_data('', test_split, rgb_root)
        rgb_results = eval_model(rgb_model, dataloaders['val'], baseline=True)

        flow_model = torch.load(args.flow_model_file)
        flow_model.cuda()
        dataloaders, datasets = load_data('', test_split, flow_root)
        flow_results = eval_model(flow_model,
                                  dataloaders['val'],
                                  baseline=True)

        rapm = APMeter()
        fapm = APMeter()
        tapm = APMeter()

        for vid in rgb_results.keys():
            o, p, l, fps = rgb_results[vid]
            rapm.add(sigmoid(o), l)
            fapm.add(sigmoid(flow_results[vid][0]), l)
            if vid in flow_results:
                o2, p2, l2, fps = flow_results[vid]
                o = (o[:o2.shape[0]] * .5 + o2 * .5)
                p = (p[:p2.shape[0]] * .5 + p2 * .5)
            tapm.add(sigmoid(o), l)
        print('rgb MAP:', rapm.value().mean())
        print('flow MAP:', fapm.value().mean())
        print('two-stream MAP:', tapm.value().mean())