-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtools.py
More file actions
158 lines (116 loc) · 4.74 KB
/
Copy pathtools.py
File metadata and controls
158 lines (116 loc) · 4.74 KB
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
import os
from shutil import copyfile
import numpy as np
import pandas as pd
from sklearn.utils import shuffle
from tqdm import tqdm
from config import dw_path, grabcad_path
from database import queries
from database.agent import read
from utils.mat_api import MatlabAPI
from utils.utils import make_dir
def get_keywords():
return read(queries.select_keywords)
def purify(string):
return string.lower().replace(' ', '_').replace('-', '_').replace('/', '_or_')
def make_dataset_1(path):
data = read(queries.select_dataset)
with open(os.path.join(path, 'label.csv'), 'w') as f:
for idx, item in tqdm(data.iterrows()):
filename = "%08d" % item['id'] + '.obj'
if os.path.isfile(item['file']):
copyfile(item['file'], os.path.join(path, filename))
item['subcategory'] = item['subcategory'] if item['category'] else 'null'
f.write(','.join([filename, 'null', purify(item['category']), purify(item['subcategory'])]) + '\n')
else:
print(filename, item['id'])
def make_dataset_2(path, categories=None):
data = read(queries.select_dataset)
if categories:
data = data[data['category'].isin(categories)]
eval_portion = 0.1
test_portion = 0.2
data['subcategory'] = [item if item else data.loc[idx, 'category'] for idx, item in enumerate(data['subcategory'])]
subcategories = list(set(data['subcategory'].tolist()))
f_train = open(os.path.join(path, 'train.csv'), 'w')
f_test = open(os.path.join(path, 'test.csv'), 'w')
f_eval = open(os.path.join(path, 'eval.csv'), 'w')
for subcategory in tqdm(subcategories):
if subcategory == 'None':
continue
subcat_set = data[data['subcategory'] == subcategory]
subcat_set = shuffle(subcat_set)
num_subcat_set = len(subcat_set)
num_subset_eval_set = int(num_subcat_set * eval_portion)
num_subset_test_set = int(num_subcat_set * test_portion)
if num_subset_eval_set < 3:
continue
for idx, (_, item) in enumerate(subcat_set.iterrows()):
filename = "%08d" % item['id'] + '.obj'
item['subcategory'] = purify(item['subcategory'])
item['category'] = purify(item['category'])
if idx < num_subset_eval_set:
data_type = 'eval'
file = f_eval
elif idx < num_subset_eval_set + num_subset_test_set:
data_type = 'test'
file = f_test
else:
data_type = 'train'
file = f_train
dst_path = os.path.join(path, data_type, item['subcategory'])
make_dir(dst_path)
obj_file = os.path.join(dst_path, filename)
copyfile(item['file'], obj_file)
normalized_mesh = normalize_mesh(obj_file)
with open(obj_file, 'w') as f:
f.write(normalized_mesh)
file.write(','.join([filename, 'null', item['category'], item['subcategory']]) + '\n')
f_test.close()
f_train.close()
f_eval.close()
def create_all_image():
for cad_dir in [dw_path, grabcad_path]:
for directory in os.listdir(cad_dir):
create_image(os.path.join(cad_dir, directory))
def create_image(directory):
matlab_api = MatlabAPI()
if os.path.isdir(directory):
matlab_api.make_image(directory)
matlab_api.close()
def normalize_vertices(pc):
centroid = np.mean(pc, axis=0)
pc = pc - centroid
m = np.max(np.sqrt(np.sum(pc ** 2, axis=1)))
pc = pc / m
return pc
def normalize_mesh(mesh_file, output_type='obj'):
import trimesh
mesh = trimesh.load(mesh_file)
mesh.vertices = normalize_vertices(mesh.vertices)
return mesh.export(file_type=output_type)
def evaluation(source_dir):
data_type = 'train'
data_list = pd.read_csv(f'{source_dir}/{data_type}.csv', header=None)
train_file_list = []
for idx, item in data_list.iterrows():
file_path = os.path.join(source_dir, data_type, item[3], item[0])
if not os.path.exists(file_path):
print(file_path + 'not exists')
else:
train_file_list.append(file_path)
data_type = 'test'
data_list = pd.read_csv(f'{source_dir}/{data_type}.csv', header=None)
test_file_list = []
for idx, item in data_list.iterrows():
file_path = f'{source_dir}/{data_type}/{item[3]}/{item[0]}'
# print(file_path)
if not os.path.exists(file_path):
print(file_path + 'not exists')
else:
test_file_list.append(file_path)
for test_file in test_file_list:
if test_file in train_file_list:
print(test_file)
if __name__ == "__main__":
make_dataset_2("/mnt/data/ENGR_data/dataset")