We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Return type ambiguous when extracting image features
The return type of the extracted image features (if the features file is present or not) is different. If the file is present then it returns a numpy.ndarray type object otherwise it returns a torch.autograd.variable.Variable object.
( https://github.com/facebookresearch/ParlAI/blob/3d86ccdbb4d87002cc6c4782afd0ee5277e742f1/parlai/core/image_featurizers.py#L149 )

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `parlai/tasks/vqa_v2/agents.py`
Content:
```
1 # Copyright (c) 2017-present, Facebook, Inc.
2 # All rights reserved.
3 # This source code is licensed under the BSD-style license found in the
4 # LICENSE file in the root directory of this source tree. An additional grant
5 # of patent rights can be found in the PATENTS file in the same directory.
6 
7 from parlai.core.agents import Teacher
8 from parlai.core.image_featurizers import ImageLoader
9 from .build import build, buildImage
10 
11 import json
12 import random
13 import os
14 
15 
16 def _path(opt):
17     build(opt)
18     buildImage(opt)
19     dt = opt['datatype'].split(':')[0]
20 
21     if dt == 'train':
22         ques_suffix = 'v2_OpenEnded_mscoco_train2014'
23         annotation_suffix = 'v2_mscoco_train2014'
24         img_suffix = os.path.join('train2014', 'COCO_train2014_')
25     elif dt == 'valid':
26         ques_suffix = 'v2_OpenEnded_mscoco_val2014'
27         annotation_suffix = 'v2_mscoco_val2014'
28         img_suffix = os.path.join('val2014', 'COCO_val2014_')
29     elif dt == 'test':
30         ques_suffix = 'v2_OpenEnded_mscoco_test2015'
31         annotation_suffix = 'None'
32         img_suffix = os.path.join('test2015', 'COCO_test2015_')
33     else:
34         raise RuntimeError('Not valid datatype.')
35 
36     data_path = os.path.join(opt['datapath'], 'VQA-v2',
37                              ques_suffix + '_questions.json')
38 
39     annotation_path = os.path.join(opt['datapath'], 'VQA-v2',
40                                    annotation_suffix + '_annotations.json')
41 
42     image_path = os.path.join(opt['datapath'], 'COCO-IMG', img_suffix)
43 
44     return data_path, annotation_path, image_path
45 
46 
47 class OeTeacher(Teacher):
48     """VQA v2.0 Open-Ended teacher, which loads the json VQA data and
49     implements its own `act` method for interacting with student agent.
50     agent.
51     """
52     def __init__(self, opt, shared=None):
53         super().__init__(opt)
54         self.datatype = opt['datatype']
55         data_path, annotation_path, self.image_path = _path(opt)
56 
57         if shared and 'ques' in shared:
58             self.ques = shared['ques']
59             if 'annotation' in shared:
60                 self.annotation = shared['annotation']
61         else:
62             self._setup_data(data_path, annotation_path)
63         self.len = len(self.ques['questions'])
64 
65         # for ordered data in batch mode (especially, for validation and
66         # testing), each teacher in the batch gets a start index and a step
67         # size so they all process disparate sets of the data
68         self.step_size = opt.get('batchsize', 1)
69         self.data_offset = opt.get('batchindex', 0)
70         self.image_loader = ImageLoader(opt)
71 
72         self.reset()
73 
74     def __len__(self):
75         return self.len
76 
77     def reset(self):
78         # Reset the dialog so that it is at the start of the epoch,
79         # and all metrics are reset.
80         super().reset()
81         self.lastY = None
82         self.episode_idx = self.data_offset - self.step_size
83 
84     def observe(self, observation):
85         """Process observation for metrics."""
86         if self.lastY is not None:
87             self.metrics.update(observation, self.lastY)
88             self.lastY = None
89         return observation
90 
91     def act(self):
92         if self.datatype == 'train':
93             self.episode_idx = random.randrange(self.len)
94         else:
95             self.episode_idx = (self.episode_idx + self.step_size) % len(self)
96             if self.episode_idx == len(self) - self.step_size:
97                 self.epochDone = True
98 
99         qa = self.ques['questions'][self.episode_idx]
100         question = qa['question']
101         image_id = qa['image_id']
102 
103         img_path = self.image_path + '%012d.jpg' % (image_id)
104 
105         action = {
106             'image': self.image_loader.load(img_path),
107             'text': question,
108             'episode_done': True
109         }
110 
111         if not self.datatype.startswith('test'):
112             anno = self.annotation['annotations'][self.episode_idx]
113             self.lastY = [ans['answer'] for ans in anno['answers']]
114 
115         if self.datatype.startswith('train'):
116             action['labels'] = self.lastY
117 
118         return action
119 
120     def share(self):
121         shared = super().share()
122         shared['ques'] = self.ques
123         if hasattr(self, 'annotation'):
124             shared['annotation'] = self.annotation
125         return shared
126 
127     def _setup_data(self, data_path, annotation_path):
128         print('loading: ' + data_path)
129         with open(data_path) as data_file:
130             self.ques = json.load(data_file)
131 
132         if self.datatype != 'test':
133             print('loading: ' + annotation_path)
134             with open(annotation_path) as data_file:
135                 self.annotation = json.load(data_file)
136 
137 
138 class DefaultTeacher(OeTeacher):
139     pass
140
```

Path: `parlai/tasks/vqa_v1/agents.py`
Content:
```
1 # Copyright (c) 2017-present, Facebook, Inc.
2 # All rights reserved.
3 # This source code is licensed under the BSD-style license found in the
4 # LICENSE file in the root directory of this source tree. An additional grant
5 # of patent rights can be found in the PATENTS file in the same directory.
6 
7 from parlai.core.agents import Teacher
8 from parlai.core.image_featurizers import ImageLoader
9 from .build import build, buildImage
10 
11 import json
12 import random
13 import os
14 
15 
16 def _path(opt):
17     build(opt)
18     buildImage(opt)
19     dt = opt['datatype'].split(':')[0]
20 
21     if dt == 'train':
22         ques_suffix = 'MultipleChoice_mscoco_train2014'
23         annotation_suffix = 'mscoco_train2014'
24         img_suffix = os.path.join('train2014', 'COCO_train2014_')
25     elif dt == 'valid':
26         ques_suffix = 'MultipleChoice_mscoco_val2014'
27         annotation_suffix = 'mscoco_val2014'
28         img_suffix = os.path.join('val2014', 'COCO_val2014_')
29     elif dt == 'test':
30         ques_suffix = 'MultipleChoice_mscoco_test2015'
31         annotation_suffix = 'None'
32         img_suffix = os.path.join('test2015', 'COCO_test2015_')
33     else:
34         raise RuntimeError('Not valid datatype.')
35 
36     data_path = os.path.join(opt['datapath'], 'VQA-v1',
37                              ques_suffix + '_questions.json')
38 
39     annotation_path = os.path.join(opt['datapath'], 'VQA-v1',
40                                    annotation_suffix + '_annotations.json')
41 
42     image_path = os.path.join(opt['datapath'], 'COCO-IMG', img_suffix)
43 
44     return data_path, annotation_path, image_path
45 
46 
47 class OeTeacher(Teacher):
48     """
49     VQA Open-Ended teacher, which loads the json vqa data and implements its
50     own `act` method for interacting with student agent.
51     """
52     def __init__(self, opt, shared=None):
53         super().__init__(opt, shared)
54         self.datatype = opt['datatype']
55         data_path, annotation_path, self.image_path = _path(opt)
56 
57         if shared and 'ques' in shared:
58             self.ques = shared['ques']
59             if 'annotation' in shared:
60                 self.annotation = shared['annotation']
61         else:
62             self._setup_data(data_path, annotation_path)
63 
64         # for ordered data in batch mode (especially, for validation and
65         # testing), each teacher in the batch gets a start index and a step
66         # size so they all process disparate sets of the data
67         self.step_size = opt.get('batchsize', 1)
68         self.data_offset = opt.get('batchindex', 0)
69         self.image_loader = ImageLoader(opt)
70         self.reset()
71 
72     def __len__(self):
73         return len(self.ques['questions'])
74 
75     def reset(self):
76         # Reset the dialog so that it is at the start of the epoch,
77         # and all metrics are reset.
78         super().reset()
79         self.lastY = None
80         self.episode_idx = self.data_offset - self.step_size
81 
82     def observe(self, observation):
83         """Process observation for metrics."""
84         if self.lastY is not None:
85             self.metrics.update(observation, self.lastY)
86             self.lastY = None
87         return observation
88 
89     def act(self):
90         if self.datatype == 'train':
91             self.episode_idx = random.randrange(len(self))
92         else:
93             self.episode_idx = (self.episode_idx + self.step_size) % len(self)
94             if self.episode_idx == len(self) - self.step_size:
95                 self.epochDone = True
96 
97         qa = self.ques['questions'][self.episode_idx]
98         question = qa['question']
99         image_id = qa['image_id']
100 
101         img_path = self.image_path + '%012d.jpg' % (image_id)
102 
103         action = {
104             'image': self.image_loader.load(img_path),
105             'text': question,
106             'episode_done': True
107         }
108 
109         if not self.datatype.startswith('test'):
110             anno = self.annotation['annotations'][self.episode_idx]
111             self.lastY = [ans['answer'] for ans in anno['answers']]
112 
113         if self.datatype.startswith('train'):
114             action['labels'] = self.lastY
115 
116         return action
117 
118     def share(self):
119         shared = super().share()
120         shared['ques'] = self.ques
121         if hasattr(self, 'annotation'):
122             shared['annotation'] = self.annotation
123         return shared
124 
125     def _setup_data(self, data_path, annotation_path):
126         print('loading: ' + data_path)
127         with open(data_path) as data_file:
128             self.ques = json.load(data_file)
129 
130         if self.datatype != 'test':
131             print('loading: ' + annotation_path)
132             with open(annotation_path) as data_file:
133                 self.annotation = json.load(data_file)
134 
135 
136 class McTeacher(OeTeacher):
137     """
138     VQA Multiple-Choice teacher, which inherits from OeTeacher but overrides
139     the label and label_candidates fields with multiple choice data.
140     """
141 
142     def act(self):
143         action = super().act()
144 
145         qa = self.ques['questions'][self.episode_idx]
146         multiple_choices = qa['multiple_choices']
147 
148         action['label_candidates'] = multiple_choices
149 
150         if not self.datatype.startswith('test'):
151             anno = self.annotation['annotations'][self.episode_idx]
152             self.lastY = [anno['multiple_choice_answer']]
153 
154         if self.datatype.startswith('train'):
155             action['labels'] = self.lastY
156 
157         return action
158 
159 
160 class DefaultTeacher(McTeacher):
161     # default to Multiple-Choice Teacher
162     pass
163
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/parlai/tasks/vqa_v1/agents.py b/parlai/tasks/vqa_v1/agents.py
--- a/parlai/tasks/vqa_v1/agents.py
+++ b/parlai/tasks/vqa_v1/agents.py
@@ -157,6 +157,30 @@
         return action
 
 
+class AllTeacher(OeTeacher):
+    """
+    VQA Teacher, which inherits from OeTeacher and gives access to
+    the multiple choices and the multiple choice answer.
+    """
+
+    def act(self):
+        action = super().act()
+
+        qa = self.ques['questions'][self.episode_idx]
+        multiple_choices = qa['multiple_choices']
+
+        action['label_candidates'] = multiple_choices
+
+        if not self.datatype.startswith('test'):
+            anno = self.annotation['annotations'][self.episode_idx]
+            self.mclabel = [anno['multiple_choice_answer']]
+
+        if self.datatype.startswith('train'):
+            action['mc_label'] = self.mclabel
+
+        return action
+
+
 class DefaultTeacher(McTeacher):
     # default to Multiple-Choice Teacher
     pass
diff --git a/parlai/tasks/vqa_v2/agents.py b/parlai/tasks/vqa_v2/agents.py
--- a/parlai/tasks/vqa_v2/agents.py
+++ b/parlai/tasks/vqa_v2/agents.py
@@ -135,5 +135,24 @@
                 self.annotation = json.load(data_file)
 
 
+class AllTeacher(OeTeacher):
+    """
+    VQA v2.0 Open-Ended teacher, which inherits from OeTeacher and 
+    gives access to the multiple choice answer.
+    """
+
+    def act(self):
+        action = super().act()
+
+        if not self.datatype.startswith('test'):
+            anno = self.annotation['annotations'][self.episode_idx]
+            self.mclabel = [anno['multiple_choice_answer']]
+
+        if self.datatype.startswith('train'):
+            action['mc_label'] = self.mclabel
+
+        return action
+
+
 class DefaultTeacher(OeTeacher):
     pass

{"golden_diff": "diff --git a/parlai/tasks/vqa_v1/agents.py b/parlai/tasks/vqa_v1/agents.py\n--- a/parlai/tasks/vqa_v1/agents.py\n+++ b/parlai/tasks/vqa_v1/agents.py\n@@ -157,6 +157,30 @@\n         return action\n \n \n+class AllTeacher(OeTeacher):\n+    \"\"\"\n+    VQA Teacher, which inherits from OeTeacher and gives access to\n+    the multiple choices and the multiple choice answer.\n+    \"\"\"\n+\n+    def act(self):\n+        action = super().act()\n+\n+        qa = self.ques['questions'][self.episode_idx]\n+        multiple_choices = qa['multiple_choices']\n+\n+        action['label_candidates'] = multiple_choices\n+\n+        if not self.datatype.startswith('test'):\n+            anno = self.annotation['annotations'][self.episode_idx]\n+            self.mclabel = [anno['multiple_choice_answer']]\n+\n+        if self.datatype.startswith('train'):\n+            action['mc_label'] = self.mclabel\n+\n+        return action\n+\n+\n class DefaultTeacher(McTeacher):\n     # default to Multiple-Choice Teacher\n     pass\ndiff --git a/parlai/tasks/vqa_v2/agents.py b/parlai/tasks/vqa_v2/agents.py\n--- a/parlai/tasks/vqa_v2/agents.py\n+++ b/parlai/tasks/vqa_v2/agents.py\n@@ -135,5 +135,24 @@\n                 self.annotation = json.load(data_file)\n \n \n+class AllTeacher(OeTeacher):\n+    \"\"\"\n+    VQA v2.0 Open-Ended teacher, which inherits from OeTeacher and \n+    gives access to the multiple choice answer.\n+    \"\"\"\n+\n+    def act(self):\n+        action = super().act()\n+\n+        if not self.datatype.startswith('test'):\n+            anno = self.annotation['annotations'][self.episode_idx]\n+            self.mclabel = [anno['multiple_choice_answer']]\n+\n+        if self.datatype.startswith('train'):\n+            action['mc_label'] = self.mclabel\n+\n+        return action\n+\n+\n class DefaultTeacher(OeTeacher):\n     pass\n", "issue": "Return type ambiguous when extracting image features\nThe return type of the extracted image features (if the features file is present or not) is different. If the file is present then it returns a numpy.ndarray type object otherwise it returns a torch.autograd.variable.Variable object.\r\n( https://github.com/facebookresearch/ParlAI/blob/3d86ccdbb4d87002cc6c4782afd0ee5277e742f1/parlai/core/image_featurizers.py#L149 )\n", "before_files": [{"content": "# Copyright (c) 2017-present, Facebook, Inc.\n# All rights reserved.\n# This source code is licensed under the BSD-style license found in the\n# LICENSE file in the root directory of this source tree. An additional grant\n# of patent rights can be found in the PATENTS file in the same directory.\n\nfrom parlai.core.agents import Teacher\nfrom parlai.core.image_featurizers import ImageLoader\nfrom .build import build, buildImage\n\nimport json\nimport random\nimport os\n\n\ndef _path(opt):\n    build(opt)\n    buildImage(opt)\n    dt = opt['datatype'].split(':')[0]\n\n    if dt == 'train':\n        ques_suffix = 'v2_OpenEnded_mscoco_train2014'\n        annotation_suffix = 'v2_mscoco_train2014'\n        img_suffix = os.path.join('train2014', 'COCO_train2014_')\n    elif dt == 'valid':\n        ques_suffix = 'v2_OpenEnded_mscoco_val2014'\n        annotation_suffix = 'v2_mscoco_val2014'\n        img_suffix = os.path.join('val2014', 'COCO_val2014_')\n    elif dt == 'test':\n        ques_suffix = 'v2_OpenEnded_mscoco_test2015'\n        annotation_suffix = 'None'\n        img_suffix = os.path.join('test2015', 'COCO_test2015_')\n    else:\n        raise RuntimeError('Not valid datatype.')\n\n    data_path = os.path.join(opt['datapath'], 'VQA-v2',\n                             ques_suffix + '_questions.json')\n\n    annotation_path = os.path.join(opt['datapath'], 'VQA-v2',\n                                   annotation_suffix + '_annotations.json')\n\n    image_path = os.path.join(opt['datapath'], 'COCO-IMG', img_suffix)\n\n    return data_path, annotation_path, image_path\n\n\nclass OeTeacher(Teacher):\n    \"\"\"VQA v2.0 Open-Ended teacher, which loads the json VQA data and\n    implements its own `act` method for interacting with student agent.\n    agent.\n    \"\"\"\n    def __init__(self, opt, shared=None):\n        super().__init__(opt)\n        self.datatype = opt['datatype']\n        data_path, annotation_path, self.image_path = _path(opt)\n\n        if shared and 'ques' in shared:\n            self.ques = shared['ques']\n            if 'annotation' in shared:\n                self.annotation = shared['annotation']\n        else:\n            self._setup_data(data_path, annotation_path)\n        self.len = len(self.ques['questions'])\n\n        # for ordered data in batch mode (especially, for validation and\n        # testing), each teacher in the batch gets a start index and a step\n        # size so they all process disparate sets of the data\n        self.step_size = opt.get('batchsize', 1)\n        self.data_offset = opt.get('batchindex', 0)\n        self.image_loader = ImageLoader(opt)\n\n        self.reset()\n\n    def __len__(self):\n        return self.len\n\n    def reset(self):\n        # Reset the dialog so that it is at the start of the epoch,\n        # and all metrics are reset.\n        super().reset()\n        self.lastY = None\n        self.episode_idx = self.data_offset - self.step_size\n\n    def observe(self, observation):\n        \"\"\"Process observation for metrics.\"\"\"\n        if self.lastY is not None:\n            self.metrics.update(observation, self.lastY)\n            self.lastY = None\n        return observation\n\n    def act(self):\n        if self.datatype == 'train':\n            self.episode_idx = random.randrange(self.len)\n        else:\n            self.episode_idx = (self.episode_idx + self.step_size) % len(self)\n            if self.episode_idx == len(self) - self.step_size:\n                self.epochDone = True\n\n        qa = self.ques['questions'][self.episode_idx]\n        question = qa['question']\n        image_id = qa['image_id']\n\n        img_path = self.image_path + '%012d.jpg' % (image_id)\n\n        action = {\n            'image': self.image_loader.load(img_path),\n            'text': question,\n            'episode_done': True\n        }\n\n        if not self.datatype.startswith('test'):\n            anno = self.annotation['annotations'][self.episode_idx]\n            self.lastY = [ans['answer'] for ans in anno['answers']]\n\n        if self.datatype.startswith('train'):\n            action['labels'] = self.lastY\n\n        return action\n\n    def share(self):\n        shared = super().share()\n        shared['ques'] = self.ques\n        if hasattr(self, 'annotation'):\n            shared['annotation'] = self.annotation\n        return shared\n\n    def _setup_data(self, data_path, annotation_path):\n        print('loading: ' + data_path)\n        with open(data_path) as data_file:\n            self.ques = json.load(data_file)\n\n        if self.datatype != 'test':\n            print('loading: ' + annotation_path)\n            with open(annotation_path) as data_file:\n                self.annotation = json.load(data_file)\n\n\nclass DefaultTeacher(OeTeacher):\n    pass\n", "path": "parlai/tasks/vqa_v2/agents.py"}, {"content": "# Copyright (c) 2017-present, Facebook, Inc.\n# All rights reserved.\n# This source code is licensed under the BSD-style license found in the\n# LICENSE file in the root directory of this source tree. An additional grant\n# of patent rights can be found in the PATENTS file in the same directory.\n\nfrom parlai.core.agents import Teacher\nfrom parlai.core.image_featurizers import ImageLoader\nfrom .build import build, buildImage\n\nimport json\nimport random\nimport os\n\n\ndef _path(opt):\n    build(opt)\n    buildImage(opt)\n    dt = opt['datatype'].split(':')[0]\n\n    if dt == 'train':\n        ques_suffix = 'MultipleChoice_mscoco_train2014'\n        annotation_suffix = 'mscoco_train2014'\n        img_suffix = os.path.join('train2014', 'COCO_train2014_')\n    elif dt == 'valid':\n        ques_suffix = 'MultipleChoice_mscoco_val2014'\n        annotation_suffix = 'mscoco_val2014'\n        img_suffix = os.path.join('val2014', 'COCO_val2014_')\n    elif dt == 'test':\n        ques_suffix = 'MultipleChoice_mscoco_test2015'\n        annotation_suffix = 'None'\n        img_suffix = os.path.join('test2015', 'COCO_test2015_')\n    else:\n        raise RuntimeError('Not valid datatype.')\n\n    data_path = os.path.join(opt['datapath'], 'VQA-v1',\n                             ques_suffix + '_questions.json')\n\n    annotation_path = os.path.join(opt['datapath'], 'VQA-v1',\n                                   annotation_suffix + '_annotations.json')\n\n    image_path = os.path.join(opt['datapath'], 'COCO-IMG', img_suffix)\n\n    return data_path, annotation_path, image_path\n\n\nclass OeTeacher(Teacher):\n    \"\"\"\n    VQA Open-Ended teacher, which loads the json vqa data and implements its\n    own `act` method for interacting with student agent.\n    \"\"\"\n    def __init__(self, opt, shared=None):\n        super().__init__(opt, shared)\n        self.datatype = opt['datatype']\n        data_path, annotation_path, self.image_path = _path(opt)\n\n        if shared and 'ques' in shared:\n            self.ques = shared['ques']\n            if 'annotation' in shared:\n                self.annotation = shared['annotation']\n        else:\n            self._setup_data(data_path, annotation_path)\n\n        # for ordered data in batch mode (especially, for validation and\n        # testing), each teacher in the batch gets a start index and a step\n        # size so they all process disparate sets of the data\n        self.step_size = opt.get('batchsize', 1)\n        self.data_offset = opt.get('batchindex', 0)\n        self.image_loader = ImageLoader(opt)\n        self.reset()\n\n    def __len__(self):\n        return len(self.ques['questions'])\n\n    def reset(self):\n        # Reset the dialog so that it is at the start of the epoch,\n        # and all metrics are reset.\n        super().reset()\n        self.lastY = None\n        self.episode_idx = self.data_offset - self.step_size\n\n    def observe(self, observation):\n        \"\"\"Process observation for metrics.\"\"\"\n        if self.lastY is not None:\n            self.metrics.update(observation, self.lastY)\n            self.lastY = None\n        return observation\n\n    def act(self):\n        if self.datatype == 'train':\n            self.episode_idx = random.randrange(len(self))\n        else:\n            self.episode_idx = (self.episode_idx + self.step_size) % len(self)\n            if self.episode_idx == len(self) - self.step_size:\n                self.epochDone = True\n\n        qa = self.ques['questions'][self.episode_idx]\n        question = qa['question']\n        image_id = qa['image_id']\n\n        img_path = self.image_path + '%012d.jpg' % (image_id)\n\n        action = {\n            'image': self.image_loader.load(img_path),\n            'text': question,\n            'episode_done': True\n        }\n\n        if not self.datatype.startswith('test'):\n            anno = self.annotation['annotations'][self.episode_idx]\n            self.lastY = [ans['answer'] for ans in anno['answers']]\n\n        if self.datatype.startswith('train'):\n            action['labels'] = self.lastY\n\n        return action\n\n    def share(self):\n        shared = super().share()\n        shared['ques'] = self.ques\n        if hasattr(self, 'annotation'):\n            shared['annotation'] = self.annotation\n        return shared\n\n    def _setup_data(self, data_path, annotation_path):\n        print('loading: ' + data_path)\n        with open(data_path) as data_file:\n            self.ques = json.load(data_file)\n\n        if self.datatype != 'test':\n            print('loading: ' + annotation_path)\n            with open(annotation_path) as data_file:\n                self.annotation = json.load(data_file)\n\n\nclass McTeacher(OeTeacher):\n    \"\"\"\n    VQA Multiple-Choice teacher, which inherits from OeTeacher but overrides\n    the label and label_candidates fields with multiple choice data.\n    \"\"\"\n\n    def act(self):\n        action = super().act()\n\n        qa = self.ques['questions'][self.episode_idx]\n        multiple_choices = qa['multiple_choices']\n\n        action['label_candidates'] = multiple_choices\n\n        if not self.datatype.startswith('test'):\n            anno = self.annotation['annotations'][self.episode_idx]\n            self.lastY = [anno['multiple_choice_answer']]\n\n        if self.datatype.startswith('train'):\n            action['labels'] = self.lastY\n\n        return action\n\n\nclass DefaultTeacher(McTeacher):\n    # default to Multiple-Choice Teacher\n    pass\n", "path": "parlai/tasks/vqa_v1/agents.py"}], "after_files": [{"content": "# Copyright (c) 2017-present, Facebook, Inc.\n# All rights reserved.\n# This source code is licensed under the BSD-style license found in the\n# LICENSE file in the root directory of this source tree. An additional grant\n# of patent rights can be found in the PATENTS file in the same directory.\n\nfrom parlai.core.agents import Teacher\nfrom parlai.core.image_featurizers import ImageLoader\nfrom .build import build, buildImage\n\nimport json\nimport random\nimport os\n\n\ndef _path(opt):\n    build(opt)\n    buildImage(opt)\n    dt = opt['datatype'].split(':')[0]\n\n    if dt == 'train':\n        ques_suffix = 'v2_OpenEnded_mscoco_train2014'\n        annotation_suffix = 'v2_mscoco_train2014'\n        img_suffix = os.path.join('train2014', 'COCO_train2014_')\n    elif dt == 'valid':\n        ques_suffix = 'v2_OpenEnded_mscoco_val2014'\n        annotation_suffix = 'v2_mscoco_val2014'\n        img_suffix = os.path.join('val2014', 'COCO_val2014_')\n    elif dt == 'test':\n        ques_suffix = 'v2_OpenEnded_mscoco_test2015'\n        annotation_suffix = 'None'\n        img_suffix = os.path.join('test2015', 'COCO_test2015_')\n    else:\n        raise RuntimeError('Not valid datatype.')\n\n    data_path = os.path.join(opt['datapath'], 'VQA-v2',\n                             ques_suffix + '_questions.json')\n\n    annotation_path = os.path.join(opt['datapath'], 'VQA-v2',\n                                   annotation_suffix + '_annotations.json')\n\n    image_path = os.path.join(opt['datapath'], 'COCO-IMG', img_suffix)\n\n    return data_path, annotation_path, image_path\n\n\nclass OeTeacher(Teacher):\n    \"\"\"VQA v2.0 Open-Ended teacher, which loads the json VQA data and\n    implements its own `act` method for interacting with student agent.\n    agent.\n    \"\"\"\n    def __init__(self, opt, shared=None):\n        super().__init__(opt)\n        self.datatype = opt['datatype']\n        data_path, annotation_path, self.image_path = _path(opt)\n\n        if shared and 'ques' in shared:\n            self.ques = shared['ques']\n            if 'annotation' in shared:\n                self.annotation = shared['annotation']\n        else:\n            self._setup_data(data_path, annotation_path)\n        self.len = len(self.ques['questions'])\n\n        # for ordered data in batch mode (especially, for validation and\n        # testing), each teacher in the batch gets a start index and a step\n        # size so they all process disparate sets of the data\n        self.step_size = opt.get('batchsize', 1)\n        self.data_offset = opt.get('batchindex', 0)\n        self.image_loader = ImageLoader(opt)\n\n        self.reset()\n\n    def __len__(self):\n        return self.len\n\n    def reset(self):\n        # Reset the dialog so that it is at the start of the epoch,\n        # and all metrics are reset.\n        super().reset()\n        self.lastY = None\n        self.episode_idx = self.data_offset - self.step_size\n\n    def observe(self, observation):\n        \"\"\"Process observation for metrics.\"\"\"\n        if self.lastY is not None:\n            self.metrics.update(observation, self.lastY)\n            self.lastY = None\n        return observation\n\n    def act(self):\n        if self.datatype == 'train':\n            self.episode_idx = random.randrange(self.len)\n        else:\n            self.episode_idx = (self.episode_idx + self.step_size) % len(self)\n            if self.episode_idx == len(self) - self.step_size:\n                self.epochDone = True\n\n        qa = self.ques['questions'][self.episode_idx]\n        question = qa['question']\n        image_id = qa['image_id']\n\n        img_path = self.image_path + '%012d.jpg' % (image_id)\n\n        action = {\n            'image': self.image_loader.load(img_path),\n            'text': question,\n            'episode_done': True\n        }\n\n        if not self.datatype.startswith('test'):\n            anno = self.annotation['annotations'][self.episode_idx]\n            self.lastY = [ans['answer'] for ans in anno['answers']]\n\n        if self.datatype.startswith('train'):\n            action['labels'] = self.lastY\n\n        return action\n\n    def share(self):\n        shared = super().share()\n        shared['ques'] = self.ques\n        if hasattr(self, 'annotation'):\n            shared['annotation'] = self.annotation\n        return shared\n\n    def _setup_data(self, data_path, annotation_path):\n        print('loading: ' + data_path)\n        with open(data_path) as data_file:\n            self.ques = json.load(data_file)\n\n        if self.datatype != 'test':\n            print('loading: ' + annotation_path)\n            with open(annotation_path) as data_file:\n                self.annotation = json.load(data_file)\n\n\nclass AllTeacher(OeTeacher):\n    \"\"\"\n    VQA v2.0 Open-Ended teacher, which inherits from OeTeacher and \n    gives access to the multiple choice answer.\n    \"\"\"\n\n    def act(self):\n        action = super().act()\n\n        if not self.datatype.startswith('test'):\n            anno = self.annotation['annotations'][self.episode_idx]\n            self.mclabel = [anno['multiple_choice_answer']]\n\n        if self.datatype.startswith('train'):\n            action['mc_label'] = self.mclabel\n\n        return action\n\n\nclass DefaultTeacher(OeTeacher):\n    pass\n", "path": "parlai/tasks/vqa_v2/agents.py"}, {"content": "# Copyright (c) 2017-present, Facebook, Inc.\n# All rights reserved.\n# This source code is licensed under the BSD-style license found in the\n# LICENSE file in the root directory of this source tree. An additional grant\n# of patent rights can be found in the PATENTS file in the same directory.\n\nfrom parlai.core.agents import Teacher\nfrom parlai.core.image_featurizers import ImageLoader\nfrom .build import build, buildImage\n\nimport json\nimport random\nimport os\n\n\ndef _path(opt):\n    build(opt)\n    buildImage(opt)\n    dt = opt['datatype'].split(':')[0]\n\n    if dt == 'train':\n        ques_suffix = 'MultipleChoice_mscoco_train2014'\n        annotation_suffix = 'mscoco_train2014'\n        img_suffix = os.path.join('train2014', 'COCO_train2014_')\n    elif dt == 'valid':\n        ques_suffix = 'MultipleChoice_mscoco_val2014'\n        annotation_suffix = 'mscoco_val2014'\n        img_suffix = os.path.join('val2014', 'COCO_val2014_')\n    elif dt == 'test':\n        ques_suffix = 'MultipleChoice_mscoco_test2015'\n        annotation_suffix = 'None'\n        img_suffix = os.path.join('test2015', 'COCO_test2015_')\n    else:\n        raise RuntimeError('Not valid datatype.')\n\n    data_path = os.path.join(opt['datapath'], 'VQA-v1',\n                             ques_suffix + '_questions.json')\n\n    annotation_path = os.path.join(opt['datapath'], 'VQA-v1',\n                                   annotation_suffix + '_annotations.json')\n\n    image_path = os.path.join(opt['datapath'], 'COCO-IMG', img_suffix)\n\n    return data_path, annotation_path, image_path\n\n\nclass OeTeacher(Teacher):\n    \"\"\"\n    VQA Open-Ended teacher, which loads the json vqa data and implements its\n    own `act` method for interacting with student agent.\n    \"\"\"\n    def __init__(self, opt, shared=None):\n        super().__init__(opt, shared)\n        self.datatype = opt['datatype']\n        data_path, annotation_path, self.image_path = _path(opt)\n\n        if shared and 'ques' in shared:\n            self.ques = shared['ques']\n            if 'annotation' in shared:\n                self.annotation = shared['annotation']\n        else:\n            self._setup_data(data_path, annotation_path)\n\n        # for ordered data in batch mode (especially, for validation and\n        # testing), each teacher in the batch gets a start index and a step\n        # size so they all process disparate sets of the data\n        self.step_size = opt.get('batchsize', 1)\n        self.data_offset = opt.get('batchindex', 0)\n        self.image_loader = ImageLoader(opt)\n        self.reset()\n\n    def __len__(self):\n        return len(self.ques['questions'])\n\n    def reset(self):\n        # Reset the dialog so that it is at the start of the epoch,\n        # and all metrics are reset.\n        super().reset()\n        self.lastY = None\n        self.episode_idx = self.data_offset - self.step_size\n\n    def observe(self, observation):\n        \"\"\"Process observation for metrics.\"\"\"\n        if self.lastY is not None:\n            self.metrics.update(observation, self.lastY)\n            self.lastY = None\n        return observation\n\n    def act(self):\n        if self.datatype == 'train':\n            self.episode_idx = random.randrange(len(self))\n        else:\n            self.episode_idx = (self.episode_idx + self.step_size) % len(self)\n            if self.episode_idx == len(self) - self.step_size:\n                self.epochDone = True\n\n        qa = self.ques['questions'][self.episode_idx]\n        question = qa['question']\n        image_id = qa['image_id']\n\n        img_path = self.image_path + '%012d.jpg' % (image_id)\n\n        action = {\n            'image': self.image_loader.load(img_path),\n            'text': question,\n            'episode_done': True\n        }\n\n        if not self.datatype.startswith('test'):\n            anno = self.annotation['annotations'][self.episode_idx]\n            self.lastY = [ans['answer'] for ans in anno['answers']]\n\n        if self.datatype.startswith('train'):\n            action['labels'] = self.lastY\n\n        return action\n\n    def share(self):\n        shared = super().share()\n        shared['ques'] = self.ques\n        if hasattr(self, 'annotation'):\n            shared['annotation'] = self.annotation\n        return shared\n\n    def _setup_data(self, data_path, annotation_path):\n        print('loading: ' + data_path)\n        with open(data_path) as data_file:\n            self.ques = json.load(data_file)\n\n        if self.datatype != 'test':\n            print('loading: ' + annotation_path)\n            with open(annotation_path) as data_file:\n                self.annotation = json.load(data_file)\n\n\nclass McTeacher(OeTeacher):\n    \"\"\"\n    VQA Multiple-Choice teacher, which inherits from OeTeacher but overrides\n    the label and label_candidates fields with multiple choice data.\n    \"\"\"\n\n    def act(self):\n        action = super().act()\n\n        qa = self.ques['questions'][self.episode_idx]\n        multiple_choices = qa['multiple_choices']\n\n        action['label_candidates'] = multiple_choices\n\n        if not self.datatype.startswith('test'):\n            anno = self.annotation['annotations'][self.episode_idx]\n            self.lastY = [anno['multiple_choice_answer']]\n\n        if self.datatype.startswith('train'):\n            action['labels'] = self.lastY\n\n        return action\n\n\nclass AllTeacher(OeTeacher):\n    \"\"\"\n    VQA Teacher, which inherits from OeTeacher and gives access to\n    the multiple choices and the multiple choice answer.\n    \"\"\"\n\n    def act(self):\n        action = super().act()\n\n        qa = self.ques['questions'][self.episode_idx]\n        multiple_choices = qa['multiple_choices']\n\n        action['label_candidates'] = multiple_choices\n\n        if not self.datatype.startswith('test'):\n            anno = self.annotation['annotations'][self.episode_idx]\n            self.mclabel = [anno['multiple_choice_answer']]\n\n        if self.datatype.startswith('train'):\n            action['mc_label'] = self.mclabel\n\n        return action\n\n\nclass DefaultTeacher(McTeacher):\n    # default to Multiple-Choice Teacher\n    pass\n", "path": "parlai/tasks/vqa_v1/agents.py"}]}