Exemplo n.º 1
0
def sample(sid=None):
    session_init(session, db)
    samples = []
    opts = {}
    for sdat in get_sample_list(db):
        if int(sid) == int(sdat['id']):
            opts = sdat
    return render_template('sample.html', sid=sid, **opts)
Exemplo n.º 2
0
def sample(sid=None):
    session_init(session, db)
    samples = []
    opts = {}
    for sdat in get_sample_list(db):
        if int(sid) == int(sdat['id']):
            opts = sdat
    return render_template('sample.html', sid=sid, **opts)
Exemplo n.º 3
0
def edit_sample(sid=None):
    session_init(session, db)
    error = None
    if session['username'] is None:
        error = 'must be logged in to edit sample'
        return render_template('ptable.html', error=error)

    opts = {}
    for sdat in get_sample_list(db):
        if int(sid) == int(sdat['id']):
            opts = sdat
    return render_template('editsample.html', sid=sid, **opts)
Exemplo n.º 4
0
def edit_sample(sid=None):
    session_init(session, db)
    error=None
    if session['username'] is None:
        error='must be logged in to edit sample'
        return render_template('ptable.html', error=error)

    opts = {}
    for sdat in get_sample_list(db):
        if int(sid) == int(sdat['id']):
            opts = sdat
    return render_template('editsample.html', sid=sid, **opts)
Exemplo n.º 5
0
def edit_spectrum(spid=None):
    session_init(session, db)
    error = None
    if session['username'] is None:
        error = 'must be logged in to edit spectrum'
        return render_template('ptable.html', error=error)

    s = db.get_spectrum(spid)
    if s is None:
        error = 'Could not find Spectrum #%i' % spid
        return render_template('ptable.html', error=error)

    opts = parse_spectrum(s, db)
    return render_template('editspectrum.html',
                           error=error,
                           elems=get_element_list(db),
                           eunits=get_energy_units_list(db),
                           edges=get_edge_list(db),
                           beamlines=get_beamline_list(db),
                           samples=get_sample_list(db),
                           **opts)
Exemplo n.º 6
0
def edit_spectrum(spid=None):
    session_init(session, db)
    error=None
    if session['username'] is None:
        error='must be logged in to edit spectrum'
        return render_template('ptable.html',
                               error=error)

    s  = db.get_spectrum(spid)
    if s is None:
        error = 'Could not find Spectrum #%i' % spid
        return render_template('ptable.html', error=error)

    opts = parse_spectrum(s, db)
    return render_template('editspectrum.html', error=error,
                           elems=get_element_list(db),
                           eunits=get_energy_units_list(db),
                           edges=get_edge_list(db),
                           beamlines=get_beamline_list(db),
                           samples=get_sample_list(db),
                           **opts)
Exemplo n.º 7
0
    def my_train(train_train_loader, train_test_loader, test_test_loader,
                 modality, naming, label_2_video, num_of_video):

        assert (modality in ['both', 'rgb', 'flow'])

        log_dir = os.path.join('logs', naming, modality)
        if not os.path.exists(log_dir):
            os.makedirs(log_dir)
        logger = Logger(log_dir)

        save_dir = os.path.join('models', naming)
        if not os.path.exists(save_dir):
            os.makedirs(save_dir)

        if modality == 'both':
            model = BackboneNet(in_features=feature_dim * 2,
                                **model_params).to(device)
        else:
            model = BackboneNet(in_features=feature_dim,
                                **model_params).to(device)

        optimizer = optim.Adam(model.parameters(),
                               lr=learning_rate,
                               weight_decay=weight_decay)

        if learning_rate_decay:
            scheduler = optim.lr_scheduler.MultiStepLR(
                optimizer, milestones=[max_step_num // 2], gamma=0.1)

        optimizer.zero_grad()

        criterion = nn.CrossEntropyLoss(reduction='mean')

        update_step_idx = 0
        single_video_idx = 0
        loss_recorder = {
            'cls': 0,
            'div': 0,
            'norm': 0,
            'sum': 0,
            'sep': 0,
            'trip': 0
        }

        # add
        all_class = list(label_2_video.keys())
        all_class.sort()

        # add state sample video

        while update_step_idx < max_step_num:

            idx, label_idx = get_sample_list(num_of_video, label_2_video,
                                             batch_size, n_similar)

            print("sample index")
            print(idx)
            print("label index")
            print(label_idx)
            atten_set = []
            fea_set = []
            global_atten_set = []
            base_fea_set = []

            loss = torch.Tensor([0]).to(device)
            for cnt in range(len(idx)):
                tmp_id = idx[cnt]
                tmp_label = None
                if cnt < len(label_idx):
                    tmp_label = label_idx[cnt]

                data = train_train_loader[tmp_id]

                model.train()

                single_video_idx += 1

                label = torch.from_numpy(data['label']).long().to(device)
                weight = torch.from_numpy(data['weight']).float().to(device)

                if modality == 'both':
                    rgb = torch.from_numpy(data['rgb']).float().to(device)
                    flow = torch.from_numpy(data['flow']).float().to(device)
                    if len(rgb.shape) == 2:
                        rgb = rgb.unsqueeze(0)
                    if len(flow.shape) == 2:
                        flow = flow.unsqueeze(0)

                    model_input = torch.cat([rgb, flow], dim=2)
                elif modality == 'rgb':
                    model_input = torch.from_numpy(
                        data['rgb']).float().to(device)
                else:
                    model_input = torch.from_numpy(
                        data['flow']).float().to(device)

                # print(model_input.shape)
                if len(model_input.shape) == 2:
                    model_input = model_input.unsqueeze(0)
                if len(label.shape) == 0:
                    label = label.unsqueeze(0)
                    weight = weight.unsqueeze(0)

                # print(model_input.shape)
                model_input = model_input.transpose(2, 1)
                avg_score, att_weight, out, scores, feature_dict = model(
                    model_input)

                # add
                if tmp_label is not None:
                    atten_set.append(avg_score[:, :, tmp_label:tmp_label + 1])
                    fea_set.append(feature_dict['fuse_feature'])
                    global_atten_set.append(att_weight)

                    base_fea_set.append(feature_dict['base_feature'])

                loss_cls = criterion(out, label) * weight

                # add sep flag
                sep_flag = (single_video_idx % batch_size == 0)

                if sep_flag:
                    sep_loss_weight = 1
                    trip_loss_weight = 0

                    loss = loss + loss_cls
                    loss_recorder['cls'] += loss_cls.item()

                    if len(global_atten_set) > 0:
                        loss_sep = sep_loss_weight * sep_loss(
                            atten_set, fea_set, device)

                        # add separation loss and cluster loss
                        loss_trip = trip_loss_weight * triplet_loss(
                            global_atten_set, base_fea_set, label_idx, device)
                        loss = loss + loss_sep + loss_trip

                        loss_recorder['sep'] = loss_sep.item()
                        loss_recorder['trip'] = loss_trip.item()

                    loss.backward()

                else:
                    loss = loss + loss_cls
                    loss_recorder['cls'] += loss_cls.item()

                # loss is the cumulative sum
                loss_recorder['sum'] = loss.item()

                # Test and Update
                if single_video_idx % batch_size == 0:
                    # calculate sep loss

                    # Test
                    if update_step_idx % log_freq == 0:
                        pass

                    # Batch Update
                    update_step_idx += 1

                    for k, v in loss_recorder.items():
                        logger.scalar_summary('Loss_{}_ps'.format(k),
                                              v / batch_size, update_step_idx)

                        loss_recorder[k] = 0

                    optimizer.step()
                    optimizer.zero_grad()

                    if learning_rate_decay:
                        scheduler.step()

                    if update_step_idx in check_points:
                        torch.save(
                            model.state_dict(),
                            os.path.join(
                                save_dir,
                                'model-{}-{}'.format(modality,
                                                     update_step_idx)))

                    if update_step_idx >= max_step_num:
                        break