<commit_before># Copyright (c) 2017-present, Facebook, Inc.
# All rights reserved.
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree. An additional grant
# of patent rights can be found in the PATENTS file in the same directory.

from parlai.core.fixed_data_teacher import FixedDataTeacher
from parlai.core.image_featurizers import ImageLoader
from .build import build, buildImage

import json
import os
from threading import Thread
import queue
import concurrent.futures


def _path(opt):
    build(opt)
    buildImage(opt)
    dt = opt['datatype'].split(':')[0]

    if dt == 'train':
        ques_suffix = 'MultipleChoice_mscoco_train2014'
        annotation_suffix = 'mscoco_train2014'
        img_suffix = os.path.join('train2014', 'COCO_train2014_')
    elif dt == 'valid':
        ques_suffix = 'MultipleChoice_mscoco_val2014'
        annotation_suffix = 'mscoco_val2014'
        img_suffix = os.path.join('val2014', 'COCO_val2014_')
    elif dt == 'test':
        ques_suffix = 'MultipleChoice_mscoco_test2015'
        annotation_suffix = 'None'
        img_suffix = os.path.join('test2015', 'COCO_test2015_')
    else:
        raise RuntimeError('Not valid datatype.')

    data_path = os.path.join(opt['datapath'], 'VQA-v1',
                             ques_suffix + '_questions.json')

    annotation_path = os.path.join(opt['datapath'], 'VQA-v1',
                                   annotation_suffix + '_annotations.json')

    image_path = os.path.join(opt['datapath'], 'COCO-IMG', img_suffix)

    return data_path, annotation_path, image_path


class MasterLoader(Thread):
    def __init__(self, opt):
        Thread.__init__(self, daemon=True)
        self.num_threads = opt.get('numthreads', 8)
        self.request_queue = queue.Queue()

    def __len__(self):
        return len(self.ques['questions'])

    def run(self):
        with concurrent.futures.ThreadPoolExecutor(max_workers=self.num_threads) as executor:
            while True:
                teacher, load_fn, data_req = self.request_queue.get()
                future = executor.submit(load_fn, data_req)
                teacher.receive(future)


class OeTeacher(FixedDataTeacher):
    """
    VQA Open-Ended teacher, which loads the json vqa data and implements its
    own `act` method for interacting with student agent.
    """
    def __init__(self, opt, shared=None):
        super().__init__(opt, shared)
        data_path, annotation_path, self.image_path = _path(opt)
        self.image_mode = opt.get('image_mode', 'none')

        if shared and 'ques' in shared:
            self.ques = shared['ques']
            if 'annotation' in shared:
                self.annotation = shared['annotation']
            self.image_loader = shared['image_loader']
            self.master_loader = shared['master_loader']
        else:
            self._setup_data(data_path, annotation_path)
            self.image_loader = ImageLoader(opt)
            self.master_loader = MasterLoader(opt)
            self.master_loader.start()

        self.example_queue = queue.Queue()
        self.reset()
        if self.image_mode != 'none':
            self.submit_example_request()

    def __len__(self):
        return len(self.ques['questions'])

    def submit_example_request(self):
        self.episode_idx, self.epochDone = self.next_episode_idx()
        image_id = self.ques['questions'][self.episode_idx]['image_id']
        img_path = self.image_path + '%012d.jpg' % (image_id)
        self.master_loader.request_queue.put(
            (self, self.image_loader.load, img_path))

    def receive(self, future):
        data = future.result()
        self.example_queue.put(data)

    def reset(self):
        # Reset the dialog so that it is at the start of the epoch,
        # and all metrics are reset.
        super().reset()
        self.example_queue = queue.Queue()

    def act(self):
        qa = self.ques['questions'][self.episode_idx]
        question = qa['question']
        image = None
        if self.image_mode != 'none':
            image = self.example_queue.get()

        action = {
            'image': image,
            'text': question,
            'episode_done': True
        }

        if not self.datatype.startswith('test'):
            anno = self.annotation['annotations'][self.episode_idx]
            answers = [ans['answer'] for ans in anno['answers']]
            self.lastY = answers
            if self.datatype.startswith('train'):
                action['labels'] = answers

        # Submit for next example before returning
        self.submit_example_request()
        return action<commit_msg>update vqa v1<commit_after># Copyright (c) 2017-present, Facebook, Inc.
# All rights reserved.
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree. An additional grant
# of patent rights can be found in the PATENTS file in the same directory.

from parlai.core.fixed_data_teacher import FixedDataTeacher
from parlai.core.image_featurizers import ImageLoader
from .build import build, buildImage

import json
import os
from threading import Thread
import queue
import concurrent.futures


def _path(opt):
    build(opt)
    buildImage(opt)
    dt = opt['datatype'].split(':')[0]

    if dt == 'train':
        ques_suffix = 'MultipleChoice_mscoco_train2014'
        annotation_suffix = 'mscoco_train2014'
        img_suffix = os.path.join('train2014', 'COCO_train2014_')
    elif dt == 'valid':
        ques_suffix = 'MultipleChoice_mscoco_val2014'
        annotation_suffix = 'mscoco_val2014'
        img_suffix = os.path.join('val2014', 'COCO_val2014_')
    elif dt == 'test':
        ques_suffix = 'MultipleChoice_mscoco_test2015'
        annotation_suffix = 'None'
        img_suffix = os.path.join('test2015', 'COCO_test2015_')
    else:
        raise RuntimeError('Not valid datatype.')

    data_path = os.path.join(opt['datapath'], 'VQA-v1',
                             ques_suffix + '_questions.json')

    annotation_path = os.path.join(opt['datapath'], 'VQA-v1',
                                   annotation_suffix + '_annotations.json')

    image_path = os.path.join(opt['datapath'], 'COCO-IMG', img_suffix)

    return data_path, annotation_path, image_path


class MasterLoader(Thread):
    def __init__(self, opt):
        Thread.__init__(self, daemon=True)
        self.num_threads = opt.get('numthreads', 8)
        self.request_queue = queue.Queue()

    def __len__(self):
        return len(self.ques['questions'])

    def run(self):
        with concurrent.futures.ThreadPoolExecutor(max_workers=self.num_threads) as executor:
            while True:
                teacher, load_fn, data_req = self.request_queue.get()
                future = executor.submit(load_fn, data_req)
                teacher.receive(future)


class OeTeacher(FixedDataTeacher):
    """
    VQA Open-Ended teacher, which loads the json vqa data and implements its
    own `act` method for interacting with student agent.
    """
    def __init__(self, opt, shared=None):
        super().__init__(opt, shared)
        data_path, annotation_path, self.image_path = _path(opt)
        self.image_mode = opt.get('image_mode', 'none')

        if shared and 'ques' in shared:
            self.ques = shared['ques']
            if 'annotation' in shared:
                self.annotation = shared['annotation']
            self.image_loader = shared['image_loader']
            self.master_loader = shared['master_loader']
        else:
            self._setup_data(data_path, annotation_path)
            self.image_loader = ImageLoader(opt)
            self.master_loader = MasterLoader(opt)
            self.master_loader.start()

        self.example_queue = queue.Queue()
        self.reset()

    def reset(self):
        super().reset()
        self.example = None
        self.example_queue = queue.Queue()
        # call this once to get the cache moving
        self.next_example()

    def __len__(self):
        """Number of examples in VQA-v1."""
        return len(self.ques['questions'])

    def num_episodes(self):
        # same as number of examples since all episodes are of length one
        return len(self)

    def submit_example_request(self, image_id):
        img_path = self.image_path + '%012d.jpg' % (image_id)
        self.master_loader.request_queue.put(
            (self, self.image_loader.load, img_path))

    def get(self, episode_idx, entry_idx=0):
        # queue up the next one
        qa = self.ques['questions'][episode_idx]
        question = qa['question']

        action = {
            'text': question,
            'image_id': qa['image_id'],
            'episode_done': True
        }

        if not self.datatype.startswith('test'):
            anno = self.annotation['annotations'][episode_idx]
            answers = [ans['answer'] for ans in anno['answers']]
            self.lastY = answers
            action['labels'] = answers

        return action

    def next_example(self):
        # save the currently queued example
        ready = None
        if self.example is not None:
            if self.image_mode != 'none':
                image = self.example_queue.get()
                self.example['image'] = image
            ready = (self.example, self.epochDone)
        # queue up the next example
        self.example, self.epochDone = super().next_example()
        image_id = self.example.pop('image_id')
        if self.image_mode != 'none':
            self.submit_example_request(image_id)
        return ready

    def receive(self, future):
        data = future.result()
        self.example_queue.put(data)
{'fraction_non_alphanumeric': 0.10604508196721311, 'fraction_numerical': 0.011577868852459016, 'mean_word_length': 2.5023322569070685, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '944381', 'n_tokens_mistral': 3025, 'n_tokens_neox': 2812, 'n_words': 690}