Beispiel #1
0
def main(exp_const, data_const, model_const):
    io.mkdir_if_not_exists(exp_const.exp_dir, recursive=True)
    io.mkdir_if_not_exists(exp_const.log_dir)
    io.mkdir_if_not_exists(exp_const.model_dir)
    io.mkdir_if_not_exists(exp_const.vis_dir)
    configure(exp_const.log_dir)
    save_constants({
        'exp': exp_const,
        'data': data_const,
        'model': model_const
    }, exp_const.exp_dir)

    print('Creating network ...')
    model = Model()
    model.const = model_const
    model.net = NET(model.const.net)
    if model.const.model_num is not None:
        model.net.load_state_dict(torch.load(model.const.net_path))
    model.net.cuda()
    model.img_mean = np.array([0.485, 0.456, 0.406])
    model.img_std = np.array([0.229, 0.224, 0.225])
    model.to_file(os.path.join(exp_const.exp_dir, 'model.txt'))

    print('Creating dataloader ...')
    dataloaders = {}
    for mode, subset in exp_const.subset.items():
        data_const = copy.deepcopy(data_const)
        data_const.subset = subset
        dataset = DATASET(data_const)
        dataloaders[mode] = DataLoader(dataset,
                                       batch_size=exp_const.batch_size,
                                       shuffle=True,
                                       num_workers=exp_const.num_workers)

    train_model(model, dataloaders, exp_const)
Beispiel #2
0
def main(exp_const,data_const,model_const):
    io.mkdir_if_not_exists(exp_const.exp_dir,recursive=True)
    io.mkdir_if_not_exists(exp_const.log_dir)
    io.mkdir_if_not_exists(exp_const.model_dir)
    configure(exp_const.log_dir)
    save_constants({
        'exp': exp_const,
        'data': data_const,
        'model': model_const},
        exp_const.exp_dir)

    print('Creating network ...')
    model = Model()
    model.const = model_const
    model.encoder = Encoder(model.const.encoder).cuda()
    model.decoder = Decoder(model.const.decoder).cuda()

    encoder_path = os.path.join(
        exp_const.model_dir,
        f'encoder_{-1}')
    torch.save(model.encoder.state_dict(),encoder_path)

    decoder_path = os.path.join(
        exp_const.model_dir,
        f'decoder_{-1}')
    torch.save(model.decoder.state_dict(),decoder_path)

    print('Creating dataloader ...')
    dataset = VisualFeaturesDataset(data_const)
    dataloader = DataLoader(
        dataset,
        batch_size=exp_const.batch_size,
        shuffle=True)

    train_model(model,dataloader,exp_const)
def main(exp_const,
         data_const_train,
         data_const_val,
         model_const,
         data_sign='hico'):
    io.mkdir_if_not_exists(exp_const.exp_dir, recursive=True)
    io.mkdir_if_not_exists(exp_const.log_dir)
    io.mkdir_if_not_exists(exp_const.model_dir)
    configure(exp_const.log_dir)
    save_constants(
        {
            'exp': exp_const,
            'data_train': data_const_train,
            'data_val': data_const_val,
            'model': model_const
        }, exp_const.exp_dir)

    print('Creating model ...')
    model = Model()
    model.const = model_const
    model.hoi_classifier = HoiClassifier(model.const.hoi_classifier,
                                         data_sign).cuda()
    model.to_txt(exp_const.exp_dir, single_file=True)

    print('Creating data loaders ...')
    dataset_train = Features(data_const_train)
    dataset_val = Features(data_const_val)

    train_model(model, dataset_train, dataset_val, exp_const)
Beispiel #4
0
def main(exp_const, data_const, model_const):
    io.mkdir_if_not_exists(exp_const.exp_dir, recursive=True)
    io.mkdir_if_not_exists(exp_const.log_dir)
    io.mkdir_if_not_exists(exp_const.model_dir)
    configure(exp_const.log_dir)
    save_constants({
        'exp': exp_const,
        'data': data_const,
        'model': model_const
    }, exp_const.exp_dir)

    print('Creating model ...')
    model = Model()
    model.const = model_const
    model.concat_svm = ConcatSVM(model_const.concat_svm).cuda()
    model.to_txt(exp_const.exp_dir, single_file=True)

    print('Creating train data loader ...')
    train_data_const = copy.deepcopy(data_const)
    train_data_const.subset = 'train'
    train_data_loader = DataLoader(SemEval201810Dataset(train_data_const),
                                   batch_size=exp_const.batch_size,
                                   shuffle=True)

    print('Creating val data loader ...')
    val_data_const = copy.deepcopy(data_const)
    val_data_const.subset = 'val'
    val_data_loader = DataLoader(SemEval201810Dataset(val_data_const),
                                 batch_size=exp_const.batch_size,
                                 shuffle=False)

    print('Begin training ...')
    train_model(model, train_data_loader, val_data_loader, exp_const)
Beispiel #5
0
def main(exp_const, data_const, model_const):
    print('Creating network ...')
    model = Model()
    model.const = model_const
    model.encoder = Encoder(model.const.encoder).cuda()
    encoder_path = os.path.join(exp_const.model_dir,
                                'encoder_' + str(model.const.model_num))
    model.encoder.load_state_dict(torch.load(encoder_path))

    print('Creating dataloader ...')
    dataset = VisualFeaturesDataset(data_const)
    dataloader = DataLoader(dataset,
                            batch_size=exp_const.batch_size,
                            shuffle=True)

    print('Get features ...')
    features = get_visual_features(model, dataloader, exp_const)

    print('Save features h5py ...')
    word_features_h5py = h5py.File(
        os.path.join(exp_const.exp_dir, 'word_features.h5py'), 'w')
    word_features_h5py.create_dataset('features',
                                      data=features,
                                      chunks=(1, features.shape[1]))
    word_features_h5py.create_dataset('mean', data=np.mean(features, axis=0))
    word_features_h5py.close()

    print('Save features word idx json ...')
    word_to_idx_json = os.path.join(exp_const.exp_dir, 'word_to_idx.json')
    io.dump_json_object(dataloader.dataset.word_to_idx, word_to_idx_json)
Beispiel #6
0
def main(exp_const, data_const, model_const):
    io.mkdir_if_not_exists(exp_const.vis_dir)

    print('Creating network ...')
    model = Model()
    model.const = model_const

    model.net = ResnetModel(model.const.net)
    if model.const.model_num is not None:
        model.net.load_state_dict(torch.load(model.const.net_path))
    model.net.cuda()

    if exp_const.feedforward == False:
        model.AttributeEmbeddings = AttributeEmbeddings(
            model.const.AttributeEmbeddings)
        if model.const.model_num is not None:
            model.AttributeEmbeddings.load_state_dict(
                torch.load(model.const.AttributeEmbeddings_path))
        model.AttributeEmbeddings.cuda()

    model.img_mean = np.array([0.485, 0.456, 0.406])
    model.img_std = np.array([0.229, 0.224, 0.225])

    print('Creating dataloader ...')
    dataset = Cifar100Dataset(data_const)
    dataloader = DataLoader(dataset,
                            batch_size=exp_const.batch_size,
                            shuffle=True,
                            num_workers=exp_const.num_workers)

    eval_results = eval_model(model, dataloader, exp_const)

    confmat_npy = os.path.join(exp_const.exp_dir, 'confmat.npy')
    np.save(confmat_npy, eval_results['Conf Mat'])

    results = {
        'Avg Loss': eval_results['Avg Loss'],
        'Acc': eval_results['Acc']
    }

    print(results)
    results_json = os.path.join(exp_const.exp_dir, 'results.json')
    io.dump_json_object(results, results_json)

    embeddings_npy = os.path.join(exp_const.exp_dir, 'embeddings.npy')
    if exp_const.feedforward == True:
        np.save(embeddings_npy,
                model.net.resnet_layers.fc.weight.data.cpu().numpy())
    else:
        np.save(embeddings_npy,
                model.AttributeEmbeddings.embed.weight.data.cpu().numpy())

    labels_npy = os.path.join(exp_const.exp_dir, 'labels.npy')
    np.save(labels_npy, dataset.labels)
Beispiel #7
0
def main(exp_const, data_const, model_const):
    io.mkdir_if_not_exists(exp_const.exp_dir, recursive=True)
    io.mkdir_if_not_exists(exp_const.log_dir)
    io.mkdir_if_not_exists(exp_const.model_dir)
    io.mkdir_if_not_exists(exp_const.vis_dir)
    configure(exp_const.log_dir)
    if model_const.model_num is None:
        const_dict = {
            'exp': exp_const,
            'data': data_const,
            'model': model_const
        }
    else:
        const_dict = {
            f'exp_finetune_{model_const.model_num}': exp_const,
            f'data_finetune_{model_const.model_num}': data_const,
            f'model_finetune_{model_const.model_num}': model_const
        }
    save_constants(const_dict, exp_const.exp_dir)

    print('Creating network ...')
    model = Model()
    model.const = model_const
    model.net = LogBilinear(model.const.net)
    if model.const.model_num is not None:
        model.net.load_state_dict(torch.load(model.const.net_path))
    model.net.cuda()
    model.to_file(os.path.join(exp_const.exp_dir, 'model.txt'))

    print('Creating positive dataloader ...')
    dataset = MultiSenseCooccurDataset(data_const)
    collate_fn = dataset.create_collate_fn()
    dataloader = DataLoader(dataset,
                            batch_size=exp_const.batch_size,
                            shuffle=True,
                            num_workers=exp_const.num_workers,
                            collate_fn=collate_fn)

    print('Creating negative dataloader ...')
    neg_dataset = NegMultiSenseCooccurDataset(data_const)
    collate_fn = neg_dataset.create_collate_fn()
    neg_dataloader = DataLoader(neg_dataset,
                                batch_size=exp_const.batch_size,
                                shuffle=True,
                                num_workers=exp_const.num_workers,
                                collate_fn=collate_fn)

    err_msg = f'Num words mismatch (try {len(dataset.words)})'
    assert (len(dataset.words) == model.const.net.num_words), err_msg

    train_model(model, dataloader, neg_dataloader, exp_const)
Beispiel #8
0
def main(exp_const, data_const, model_const):
    print('Loading model ...')
    model = Model()
    model.const = model_const
    model.hoi_classifier = HoiClassifier(model.const.hoi_classifier).cuda()
    if model.const.model_num == -1:
        print(
            'No pretrained model will be loaded since model_num is set to -1')
    else:
        model.hoi_classifier.load_state_dict(
            torch.load(model.const.hoi_classifier.model_pth))

    print('Creating data loader ...')
    dataset = Features(data_const)

    eval_model(model, dataset, exp_const)
Beispiel #9
0
def main(exp_const,data_const,model_const):
    print('Creating network ...')
    model = Model()
    model.const = model_const
    model.net = LogBilinear(model.const.net)
    if model.const.model_num is not None:
        model.net.load_state_dict(torch.load(model.const.net_path))
    
    embeddings = 0.5*(model.net.embed1.W.weight + model.net.embed2.W.weight)
    embeddings = embeddings.data.numpy()
    embeddings_json = os.path.join(exp_const.exp_dir,'visual_embeddings.npy')
    np.save(embeddings_json,embeddings)

    print('Saving word_to_idx.json ...')
    dataset = MultiSenseCooccurDataset(data_const)
    word_to_idx = dataset.word_to_idx
    word_to_idx_json = os.path.join(exp_const.exp_dir,'word_to_idx.json')
    io.dump_json_object(word_to_idx,word_to_idx_json)
Beispiel #10
0
def main(exp_const, data_const, model_const):
    io.mkdir_if_not_exists(exp_const.exp_dir, recursive=True)
    io.mkdir_if_not_exists(exp_const.log_dir)
    io.mkdir_if_not_exists(exp_const.model_dir)
    io.mkdir_if_not_exists(exp_const.vis_dir)
    configure(exp_const.log_dir)
    save_constants({
        'exp': exp_const,
        'data': data_const,
        'model': model_const
    }, exp_const.exp_dir)

    print('Creating network ...')
    model = Model()
    model.const = model_const
    model.net = ResnetModel(model.const.net)
    model.embed2class = Embed2Class(model.const.embed2class)
    if model.const.model_num is not None:
        model.net.load_state_dict(torch.load(model.const.net_path))
        model.embed2class.load_state_dict(
            torch.load(model.const.embed2class_path))
    model.net.cuda()
    model.embed2class.cuda()
    model.img_mean = np.array([0.485, 0.456, 0.406])
    model.img_std = np.array([0.229, 0.224, 0.225])
    model.to_file(os.path.join(exp_const.exp_dir, 'model.txt'))

    print('Creating dataloader ...')
    dataloaders = {}
    for mode, subset in exp_const.subset.items():
        data_const = copy.deepcopy(data_const)
        if subset == 'train':
            data_const.train = True
        else:
            data_const.train = False
        dataset = Cifar100Dataset(data_const)
        collate_fn = dataset.get_collate_fn()
        dataloaders[mode] = DataLoader(dataset,
                                       batch_size=exp_const.batch_size,
                                       shuffle=True,
                                       num_workers=exp_const.num_workers,
                                       collate_fn=collate_fn)

    train_model(model, dataloaders, exp_const)
Beispiel #11
0
def main(exp_const, data_const, model_const):
    print('Creating model ...')
    model = Model()
    model.const = model_const
    model.concat_svm = ConcatSVM(model_const.concat_svm).cuda()

    print('Select best model ...')
    step_to_val_best_scores_tuple_json = os.path.join(
        exp_const.exp_dir, 'step_to_val_best_scores_tuple.json')
    step_to_val_best_scores_tuple = io.load_json_object(
        step_to_val_best_scores_tuple_json)
    best_step, best_scores_tuple = select_best_concat_svm(
        step_to_val_best_scores_tuple)
    model.concat_svm.const.thresh = best_scores_tuple[4]
    model_pth = os.path.join(exp_const.model_dir, f'{best_step}')
    model.concat_svm.load_state_dict(torch.load(model_pth))
    print(f'Selcted model at step: {best_step}')
    print(f'Selected thresh: {model.concat_svm.const.thresh}')

    print('Creating data loader ...')
    data_const = copy.deepcopy(data_const)
    data_loader = DataLoader(SemEval201810Dataset(data_const),
                             batch_size=exp_const.batch_size,
                             shuffle=False)

    print('Begin evaluation ...')
    result, correct_preds, incorrect_preds = eval_model(
        model, data_loader, exp_const)
    result_json = os.path.join(exp_const.exp_dir,
                               f'results_{data_const.subset}.json')
    io.dump_json_object(result, result_json)
    print(io.dumps_json_object(result))

    correct_preds_json = os.path.join(
        exp_const.exp_dir, f'correct_preds_{data_const.subset}.json')
    io.dump_json_object(correct_preds, correct_preds_json)

    incorrect_preds_json = os.path.join(
        exp_const.exp_dir, f'incorrect_preds_{data_const.subset}.json')
    io.dump_json_object(incorrect_preds, incorrect_preds_json)
def main(exp_const, data_const, model_const):
    print('Creating network ...')
    model = Model()
    model.const = model_const
    model.net = LogBilinear(model.const.net)
    if model.const.model_num is not None:
        model.net.load_state_dict(torch.load(model.const.net_path))

    embeddings = 0.5 * (model.net.embed1.W.weight + model.net.embed2.W.weight)

    print('Computing transformed embeddings ...')
    xformed_embeddings = []
    for cooccur_type in exp_const.cooccur_types:
        xform = getattr(model.net, f'xform_{cooccur_type}')
        xformed_embeddings.append(xform(embeddings).cpu().data.numpy())
        print(cooccur_type, xformed_embeddings[-1].shape)

    xformed_embeddings = np.concatenate(xformed_embeddings, 1)
    print('Concatenated xformed embedding shape', xformed_embeddings.shape)
    xformed_embeddings_json = os.path.join(exp_const.exp_dir,
                                           'visual_embeddings_xformed.npy')
    np.save(xformed_embeddings_json, xformed_embeddings)
Beispiel #13
0
def main(exp_const, data_const, model_const):
    print('Loading model ...')
    model = Model()
    model.const = model_const
    model.hoi_classifier = HoiClassifier(model.const.hoi_classifier).cuda()
    if model.const.model_num == -1:
        print(
            'No pretrained model will be loaded since model_num is set to -1')
    else:
        state = torch.load(model.const.hoi_classifier.model_pth
                           )  #---------------------------!!!!!!!!!!!!!!!!!!!
        state = {
            kk: state[kk]
            for kk in state.keys() if not 'mlp.embedding' in kk
        }
        #pdb.set_trace()
        model.hoi_classifier.load_state_dict(state)
        #model.hoi_classifier.load_state_dict(
        #    torch.load(model.const.hoi_classifier.model_pth))

    print('Creating data loader ...')
    dataset = Features(data_const)

    eval_model(model, dataset, exp_const)