-
Notifications
You must be signed in to change notification settings - Fork 8
/
format_unseen.py
306 lines (248 loc) · 13.4 KB
/
format_unseen.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# Usage: `python3 format_unseen.py path/to/dataset output_dim path/to/ouput/folder`
from sys import argv, path
import argparse
import glob, os, yaml
import tensorflow as tf
path.append('utils')
path.append('utils/image')
path.append('utils/video')
path.append('utils/series')
path.append('utils/automl_format')
path.append('utils/text')
STARTING_KIT_DIR = '../autodl/codalab_competition_bundle/AutoDL_starting_kit'
LOG_FILE = 'baseline_log.txt'
path.append(STARTING_KIT_DIR)
path.append(os.path.join(STARTING_KIT_DIR, 'AutoDL_ingestion_program'))
import dataset_manager
from dataset_formatter import UniMediaDatasetFormatter
from data_manager import DataManager
import pandas as pd
import format_image
import format_video
import format_series
import format_automl_new as format_tabular
import nlp_to_tfrecords
import shutil
import run_local_test
import data_browser
import re
verbose = False
def format_data(input_dir, output_dir, fake_name, effective_sample_num,
train_size=0.65,
num_channels=3,
classes_list=None,
domain='image', output_dim=None, input_name=None):
""" Transform data into TFRecords
"""
print('format_data: Formatting... {} samples'.format(effective_sample_num))
if effective_sample_num != 0:
if domain == 'image':
format_image.format_data(input_dir, output_dir, fake_name,
train_size=0,
max_num_examples=effective_sample_num,
num_channels=num_channels,
classes_list=classes_list, output_dim=output_dim)
elif domain == 'video':
format_video.format_data(input_dir, output_dir, fake_name,
train_size=0,
max_num_examples=effective_sample_num,
num_channels=num_channels,
classes_list=classes_list, output_dim=output_dim)
elif domain == 'series':
format_series.format_data(input_dir, output_dir, fake_name,
train_size=0,
max_num_examples=effective_sample_num,
num_channels=num_channels,
classes_list=classes_list, output_dim=output_dim)
elif domain == 'tabular':
D = DataManager(input_name, input_dir, replace_missing=False, verbose=verbose)
new_dataset_name = "unlabelled"
if not os.path.isdir(output_dir):
os.mkdir(output_dir)
dataset_dir = os.path.join(output_dir, new_dataset_name)
if not os.path.isdir(dataset_dir):
os.mkdir(dataset_dir)
# Format test set
set_type = 'test'
filepath = os.path.join(dataset_dir, "sample-unlabelled.tfrecord")
metadata, features, labels = format_tabular._prepare_metadata_features_and_labels(D, set_type=set_type)
format_tabular.convert_vectors_to_sequence_example(filepath, metadata, features, labels, D.info,
max_num_examples=effective_sample_num)
elif domain == 'text':
name=fake_name
language = nlp_to_tfrecords.get_language(os.path.join(input_dir, 'meta.json'))
train_data = nlp_to_tfrecords.read_file(os.path.join(input_dir, 'train.data'))
train_solution = nlp_to_tfrecords.read_file(os.path.join(input_dir, 'train.solution'))
test_data = nlp_to_tfrecords.read_file(os.path.join(input_dir, 'test.data'))
test_solution = nlp_to_tfrecords.read_file(os.path.join(input_dir, 'test.solution'))
# Create vocabulary
vocabulary = nlp_to_tfrecords.create_vocabulary(train_data+test_data, language)
# Convert data into sequences of integers
features_labels_pairs_train = nlp_to_tfrecords.get_features_labels_pairs(train_data, train_solution,
vocabulary, language, format=format)
features_labels_pairs_test = nlp_to_tfrecords.get_features_labels_pairs(test_data, test_solution,
vocabulary, language, format=format)
# Write data in TFRecords and vocabulary in metadata
output_dim = nlp_to_tfrecords.get_output_dim(train_solution)
col_count, row_count = 1, 1
sequence_size = -1
num_channels = 1 #len(vocabulary)
num_examples_train = len(train_data)
num_examples_test = len(test_data)
new_dataset_name = name # same name
classes_list = None
dataset_formatter = UniMediaDatasetFormatter(name,
output_dir,
features_labels_pairs_train,
features_labels_pairs_test,
output_dim,
col_count,
row_count,
sequence_size=sequence_size, # for strides=2
num_channels=num_channels,
num_examples_train=num_examples_train,
num_examples_test=num_examples_test,
is_sequence_col='false',
is_sequence_row='false',
has_locality_col='true',
has_locality_row='true',
format='DENSE',
label_format='DENSE',
is_sequence='false',
sequence_size_func=None,
new_dataset_name=new_dataset_name,
classes_list=classes_list,
channels_dict=vocabulary)
dataset_formatter.press_a_button_and_give_me_an_AutoDL_dataset()
print('format_data: done.')
if __name__=="__main__":
if len(argv)==3 or len(argv)==4:
input_dir = argv[1]
input_dir = os.path.normpath(input_dir)
output_dim = int(argv[2])
if len(argv)==3:
output_dir = input_dir + '_unlabelled_formatted'
else:
output_dir = os.path.normpath(argv[3])
else:
print('Please enter a dataset directory and an output dimension. Usage: `python3 format_unseen.py path/to/dataset output_dim`')
exit()
domain = input("Domain? 'image', 'video', 'series', 'text' or 'tabular' [Default='image'] ")
if domain == '':
domain = 'image'
num_samples=0
if domain in ['image', 'video', 'series']:
data_csv_files = [file for file in glob.glob(os.path.join(input_dir, '*data*.csv'))]
if len(data_csv_files) > 1:
raise ValueError("Ambiguous data file! Several of them found: {}".format(data_csv_files))
elif len(data_csv_files) < 1:
raise ValueError("No label file found! The name of this file should follow the glob pattern `*data*.csv` (e.g. monkeys_data_file_format.csv).")
else:
data_csv_file = data_csv_files[0]
fake_labels_file = os.path.join(input_dir, 'fake_labels.csv')
with open(fake_labels_file, 'w') as fl_file:
fl_file.write('FileName,Labels\n')
with open(data_csv_file, 'r') as dc_file:
for l in dc_file:
num_samples += 1
line = l.strip()+',0\n'
fl_file.write(line)
label_name = None
label_file = os.path.join(input_dir, 'label.name')
if os.path.exists(label_file):
print('First rows of label names:')
label_name = pd.read_csv(label_file, header=None)
print(label_name.head())
print()
if domain=='tabular':
data_csv_files = [file for file in glob.glob(os.path.join(input_dir, '*data*.csv'))]
if len(data_csv_files) > 1:
raise ValueError("Ambiguous data file! Several of them found: {}".format(data_csv_files))
elif len(data_csv_files) < 1:
raise ValueError("No label file found! The name of this file should follow the glob pattern `*data*.csv` (e.g. monkeys_data_file_format.csv).")
else:
data_csv_file = data_csv_files[0]
fake_labels_file = os.path.join(input_dir, 'unlabelled_test.solution')
shutil.copyfile(data_csv_file, os.path.join(input_dir,"unlabelled_test.data"))
n_features = 0
first_line = ""
with open(fake_labels_file, 'w') as fl_file:
with open(data_csv_file, 'r') as dc_file:
for l in dc_file:
num_samples += 1
line = ('1 '+('0 ')*(output_dim-1)).strip()+'\n'
fl_file.write(line)
if num_samples==1:
n_features = len(l.split())
first_line = l
with open(os.path.join(input_dir, 'unlabelled_train.solution'), 'w') as train_sol_file:
line = ('1 '+('0 ')*(output_dim-1)).strip()+'\n'
train_sol_file.write(line)
with open(os.path.join(input_dir, 'unlabelled_train.data'), 'w') as train_data_file:
line = ('0 '*(n_features)).strip()+'\n'
train_data_file.write(line)
open(os.path.join(input_dir, 'unlabelled_valid.data'), 'a').close()
open(os.path.join(input_dir, 'unlabelled_valid.solution'), 'a').close()
label_name = None
label_file = os.path.join(input_dir, 'label.name')
if os.path.exists(label_file):
print('First rows of label names:')
label_name = pd.read_csv(label_file, header=None)
print(label_name.head())
print()
if domain=='text':
fake_labels_file = os.path.join(input_dir, 'test.solution')
data_file = os.path.join(input_dir, 'unlabelled.data')
shutil.copyfile(data_file, os.path.join(input_dir, 'test.data'))
with open(fake_labels_file, 'w') as fl_file:
with open(data_file, 'r') as dc_file:
for l in dc_file:
num_samples += 1
line = ('1 '+('0 ')*(output_dim-1)).strip()+'\n'
fl_file.write(line)
label_name = None
label_file = os.path.join(input_dir, 'label.name')
if os.path.exists(label_file):
print('First rows of label names:')
label_name = pd.read_csv(label_file, header=None)
print(label_name.head())
print()
# num channels
num_channels = input('Number of channels? [Default=3] ')
if num_channels == '':
num_channels = 3
try:
num_channels = int(num_channels)
except Exception as e:
print('Number of channels must be an Integer:', e)
exit()
# format data in TFRecords
print('Label list:')
if label_name is None:
flat_label_list = None
else:
label_list = label_name.values.tolist()
flat_label_list = [item for sublist in label_list for item in sublist]
print(flat_label_list)
effective_sample_num = num_samples
fake_name = "unseen_formatted"
print(effective_sample_num)
if domain in ['image', 'video', 'series', 'text']:
format_data(input_dir, output_dir, fake_name, effective_sample_num, num_channels=num_channels, classes_list=flat_label_list, domain=domain, output_dim=output_dim, input_name="unlabelled")
os.remove(os.path.join(output_dir, fake_name, fake_name+".solution"))
if os.path.exists(os.path.join(output_dir, "unlabelled")):
shutil.rmtree(os.path.join(output_dir, "unlabelled"))
os.mkdir(os.path.join(output_dir, "unlabelled"))
for file in glob.glob(os.path.join(output_dir, fake_name, fake_name+".data", "test", "*")):
shutil.move(file, os.path.join(output_dir, "unlabelled"))
shutil.rmtree(os.path.join(output_dir, fake_name))
os.rename(os.path.join(output_dir, "unlabelled", "sample-"+fake_name+"-test.tfrecord"), os.path.join(output_dir, "unlabelled", "sample-unlabelled.tfrecord"))
if domain != 'text':
os.remove(os.path.join(input_dir, 'fake_labels.csv'))
elif domain == 'tabular':
format_data(input_dir, output_dir, fake_name, effective_sample_num, num_channels=num_channels, classes_list=flat_label_list, domain=domain, output_dim=output_dim, input_name="unlabelled")
os.remove(os.path.join(output_dir,"unlabelled","unlabelled.solution"))
files_to_remove = [os.path.join(input_dir, f) for f in os.listdir(input_dir) if re.search(r'(valid|train|test)', f)]
for file in files_to_remove:
if os.path.exists(file):
os.remove(file)