def extract(path_opts=None):
    Options(path_opts)
    utils.set_random_seed(Options()['misc']['seed'])

    assert Options(
    )['dataset']['eval_split'] is not None, 'eval_split must be set'
    assert Options(
    )['dataset']['train_split'] is None, 'train_split must be None'

    init_logs_options_files(Options()['exp']['dir'],
                            Options()['exp']['resume'])

    Logger().log_dict('options', Options(), should_print=True)
    Logger()(os.uname())
    if torch.cuda.is_available():
        cudnn.benchmark = True
        Logger()('Available GPUs: {}'.format(utils.available_gpu_ids()))

    engine = engines.factory()
    engine.dataset = datasets.factory(engine)
    engine.model = models.factory(engine)
    engine.view = views.factory(engine)

    # init extract directory
    dir_extract = os.path.join(Options()['exp']['dir'], 'extract',
                               Options()['dataset']['eval_split'])
    os.system('mkdir -p ' + dir_extract)
    path_img_embs = os.path.join(dir_extract, 'image_emdeddings.pth')
    path_rcp_embs = os.path.join(dir_extract, 'recipe_emdeddings.pth')
    img_embs = torch.FloatTensor(len(engine.dataset['eval']),
                                 Options()['model.network.dim_emb'])
    rcp_embs = torch.FloatTensor(len(engine.dataset['eval']),
                                 Options()['model.network.dim_emb'])

    def save_embeddings(module, input, out):
        nonlocal img_embs
        nonlocal rcp_embs
        batch = input[0]  # tuple of len=1
        for j, idx in enumerate(batch['recipe']['index']):
            # path_image = os.path.join(dir_image, '{}.pth'.format(idx))
            # path_recipe = os.path.join(dir_recipe, '{}.pth'.format(idx))
            # torch.save(out['image_embedding'][j].data.cpu(), path_image)
            # torch.save(out['recipe_embedding'][j].data.cpu(), path_recipe)
            img_embs[idx] = out['image_embedding'][j].data.cpu()
            rcp_embs[idx] = out['recipe_embedding'][j].data.cpu()

    engine.model.register_forward_hook(save_embeddings)
    engine.resume()
    engine.eval()

    Logger()('Saving image embeddings to {} ...'.format(path_img_embs))
    torch.save(img_embs, path_img_embs)

    Logger()('Saving recipe embeddings to {} ...'.format(path_rcp_embs))
    torch.save(rcp_embs, path_rcp_embs)
Esempio n. 2
0
def main(path_opts=None):
    # first call to Options() load the options yaml file from --path_opts command line argument if path_opts=None
    Options(path_opts)

    # make exp directory if resume=None
    init_experiment_directory(Options()['exp']['dir'],
                              Options()['exp']['resume'])

    # initialiaze seeds to be able to reproduce experiment on reload
    utils.set_random_seed(Options()['misc']['seed'])

    # display and save options as yaml file in exp dir
    Logger().log_dict('options', Options(), should_print=True)

    # display server name and GPU(s) id(s)
    Logger()(os.uname())
    if 'CUDA_VISIBLE_DEVICES' in os.environ:
        Logger()('CUDA_VISIBLE_DEVICES=' + os.environ['CUDA_VISIBLE_DEVICES'])

    # engine can train, eval, optimize the model
    # engine can save and load the model and optimizer
    engine = engines.factory()

    # dataset is a dictionnary which contains all the needed datasets indexed by splits
    # (example: dataset.keys() -> ['train','val'])
    dataset = datasets.factory()
    engine.dataset = dataset

    # model includes a network, a criterion and a metric
    # model can register engine hooks (begin epoch, end batch, end batch, etc.)
    # (example: "calculate mAP at the end of the evaluation epoch")
    model = models.factory(engine)
    engine.model = model

    # optimizer can register engine hooks
    optimizer = optimizers.factory(model, engine)
    engine.optimizer = optimizer

    # load the model and optimizer from a checkpoint
    if Options()['exp']['resume']:
        engine.resume()

    # if no training split, evaluate the model on the evaluation split
    # (example: $ python main.py --dataset.train_split --dataset.eval_split test)
    engine.eval()

    # optimize the model on the training split for several epochs
    # (example: $ python main.py --dataset.train_split train)
    # if evaluation split, evaluate the model after each epochs
    # (example: $ python main.py --dataset.train_split train --dataset.eval_split val)
    engine.train()
def api(path_opts=None):
    global engine
    global img_embs
    global rcp_embs
    global all_embs
    global all_ids

    Options(path_opts)
    utils.set_random_seed(Options()['misc']['seed'])

    assert Options(
    )['dataset']['eval_split'] is not None, 'eval_split must be set'
    assert Options(
    )['dataset']['train_split'] is None, 'train_split must be None'

    init_logs_options_files(Options()['exp']['dir'],
                            Options()['exp']['resume'])

    Logger().log_dict('options', Options(), should_print=True)
    Logger()(os.uname())
    if torch.cuda.is_available():
        cudnn.benchmark = True
        Logger()('Available GPUs: {}'.format(utils.available_gpu_ids()))

    engine = engines.factory()
    engine.dataset = datasets.factory(engine)
    engine.model = models.factory(engine)
    engine.model.eval()
    engine.resume()

    dir_extract = os.path.join(Options()['exp']['dir'], 'extract',
                               Options()['dataset']['eval_split'])
    path_img_embs = os.path.join(dir_extract, 'image_emdeddings.pth')
    path_rcp_embs = os.path.join(dir_extract, 'recipe_emdeddings.pth')
    img_embs = torch.load(path_img_embs)
    rcp_embs = torch.load(path_rcp_embs)
    all_embs = torch.cat([img_embs, rcp_embs], dim=0)
    all_ids = list(range(img_embs.shape[0])) + list(range(rcp_embs.shape[0]))

    my_local_ip = '132.227.204.160'  # localhost | 192.168.0.41 (hostname --ip-address)
    my_local_port = 3456  # 8080 | 3456
    run_simple(my_local_ip, my_local_port, application)
import bootstrap.engines as engines
import bootstrap.models as models
import bootstrap.datasets as datasets
from bootstrap.lib.logger import Logger
from bootstrap.lib.options import Options
from bootstrap.run import init_logs_options_files
from block.datasets.vqa_utils import tokenize_mcb

if __name__ == "__main__":
    Options()
    #print("seed: {}".format(Options()['misc']['seed']))
    utils.set_random_seed(Options()['misc']['seed'])
    init_logs_options_files(Options()['exp']['dir'],
                            Options()['exp']['resume'])

    engine = engines.factory()
    #print("engine: {}".format(engine))
    engine.dataset = datasets.factory(engine)
    engine.model = models.factory(engine)
    #print("engine.model: {}".format(engine.model))
    engine.model.eval()
    engine.resume()

    #img_emb_module = load_img_emb_module().cuda() #engine.model.network.image_embedding

    # inputs
    img = torch.randn(1, 3, 224, 244).cuda()
    question = 'What is the color of the white horse?'
    question_words = tokenize_mcb(question)
    question_wids = [
        engine.dataset['eval'].word_to_wid[word] for word in question_words