This repository has been archived by the owner on Nov 3, 2023. It is now read-only.
/
build.py
130 lines (115 loc) · 4.07 KB
/
build.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python3
# Copyright (c) Facebook, Inc. and its affiliates.
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
import itertools
import json
import os
import random
import parlai.core.build_data as build_data
from parlai.utils.io import PathManager
def build(opt):
"""
Create train and validation data for synthetic shapes described by attributes.
"""
dpath = os.path.join(opt['datapath'], 'taskntalk')
if not build_data.built(dpath):
print('[building data: ' + dpath + ']')
build_data.make_dir(os.path.join(dpath, 'large'))
build_data.make_dir(os.path.join(dpath, 'small'))
# save training and validation data
to_save = {
'attributes': ['color', 'shape', 'style'],
'task_defn': [
['color', 'shape'],
['shape', 'color'],
['color', 'style'],
['style', 'color'],
['shape', 'style'],
['style', 'shape'],
],
}
split_data = {}
# small dataset properties
properties = {
'color': ['red', 'green', 'blue', 'purple'],
'shape': ['square', 'triangle', 'circle', 'star'],
'style': ['dotted', 'solid', 'filled', 'dashed'],
}
to_save['properties'] = properties
# properties.values() not used directly to maintain order
data_verbose = list(
itertools.product(*[properties[key] for key in to_save['attributes']])
)
# randomly select train and rest of it is valid
split_data['valid'] = random.sample(data_verbose, int(0.2 * len(data_verbose)))
split_data['train'] = [s for s in data_verbose if s not in split_data['valid']]
to_save['data'] = split_data['train']
with PathManager.open(
os.path.join(dpath, 'small', 'train.json'), 'w'
) as outfile:
json.dump(
to_save, outfile, indent=4, separators=(',', ': '), sort_keys=True
)
to_save['data'] = split_data['valid']
with PathManager.open(
os.path.join(dpath, 'small', 'valid.json'), 'w'
) as outfile:
json.dump(
to_save, outfile, indent=4, separators=(',', ': '), sort_keys=True
)
# large dataset properties
properties = {
'color': [
'red',
'green',
'blue',
'purple',
'yellow',
'cyan',
'orange',
'teal',
],
'shape': [
'square',
'triangle',
'circle',
'star',
'heart',
'spade',
'club',
'diamond',
],
'style': [
'dotted',
'solid',
'filled',
'dashed',
'hstripe',
'vstripe',
'hgrad',
'vgrad',
],
}
to_save['properties'] = properties
data_verbose = list(
itertools.product(*[properties[key] for key in to_save['attributes']])
)
split_data['valid'] = random.sample(data_verbose, int(0.8 * len(data_verbose)))
split_data['train'] = [s for s in data_verbose if s not in split_data['valid']]
to_save['data'] = split_data['train']
with PathManager.open(
os.path.join(dpath, 'large', 'train.json'), 'w'
) as outfile:
json.dump(
to_save, outfile, indent=4, separators=(',', ': '), sort_keys=True
)
to_save['data'] = split_data['valid']
with PathManager.open(
os.path.join(dpath, 'large', 'valid.json'), 'w'
) as outfile:
json.dump(
to_save, outfile, indent=4, separators=(',', ': '), sort_keys=True
)
# Mark the data as built.
build_data.mark_done(dpath)