-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGenerateData.py
103 lines (90 loc) · 3.79 KB
/
GenerateData.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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Generate Data for Experimentation.
-> Data is created in a way to unify Single and Multi instance.
-> SISL and MISL share the same data format.
-> SIML and MIML share the same data format.
@author: josemiguelarrieta
"""
from __future__ import print_function
import sys
import numpy as np
import pickle
from utils_dagm import WeakLabeling,load_image_dagm
from LabelExtractFeatures import get_data_MISL, get_data_MIML
#Path
path = '/Users/josemiguelarrieta/Dropbox/11_Semestre/Jovenes_Investigadores/images/Experiment_1_DAGM/Class'
defects =['AB','NO']
ClassNumber = sys.argv[1]
####################
# Single Label #
####################
LabelType = 'SL'
BagsSL = []
BagLabelsSL = np.empty((0,1), int)
InstanceBagLabelSL =[]
for defect in defects:
for i in range (1,100):
num = i #Image number
if defect == 'AB':
cropped,cropped_maskA,cropped_maskB = WeakLabeling(path,num,ClassNumber,defect = defect,exp = True)
baglabel, bag, instanceLabels = get_data_MISL(cropped,cropped_maskA,cropped_maskB)
elif defect == 'NO':
image = load_image_dagm(path,num,ClassNumber,defect = defect,exp = True)
baglabel, bag, instanceLabels = get_data_MISL(image)
else:
cropped,cropped_mask = WeakLabeling(path,num,ClassNumber,defect = defect,exp = True)
baglabel, bag, instanceLabels = get_data_MISL(cropped,cropped_mask)
BagsSL.append(bag)
InstanceBagLabelSL.append(instanceLabels)
BagLabelsSL = np.concatenate((BagLabelsSL, np.array([baglabel]).reshape(1,1)), axis=0)
#Save Data
path_data = '/Users/josemiguelarrieta/Documents/SIVA/ExperimentsData2/defectAB/'+'class'+str(ClassNumber)+'/'+LabelType+'/'
f = open(path_data+'BagsSL.pckl', 'wb')
pickle.dump(BagsSL, f)
f.close()
f = open(path_data+'BagLabelsSL.pckl', 'wb')
pickle.dump(BagLabelsSL, f)
f.close()
f = open(path_data+'InstanceBagLabelSL.pckl', 'wb')
pickle.dump(InstanceBagLabelSL, f)
f.close()
print("SL data saved.")
###################
# Multi Label #
###################
LabelType = 'ML'
BagsML = []
BagLabelsML = np.empty((0,2), int)
InstanceBagLabelML = []
for defect in defects:
for i in range (1,100):
num = i #Image number
if defect == 'AB':
cropped,cropped_maskA,cropped_maskB = WeakLabeling(path,num,ClassNumber,defect = defect,exp = True)
baglabels, bag, instancelabels = get_data_MIML(cropped,cropped_maskA,cropped_maskB)
elif defect == 'NO':
image = load_image_dagm(path,num,ClassNumber,defect = defect,exp = True)
baglabels, bag, instancelabels = get_data_MIML(image,cropped_maskA=None,cropped_maskB=None)
elif defect == 'A':
cropped,cropped_maskA = WeakLabeling(path,num,ClassNumber,defect = defect,exp = True)
baglabels, bag, instancelabels = get_data_MIML(cropped,cropped_maskA=cropped_maskA,cropped_maskB=None)
elif defect == 'B':
cropped,cropped_maskB = WeakLabeling(path,num,ClassNumber,defect = defect,exp = True)
baglabels, bag, instancelabels = get_data_MIML(cropped,cropped_maskA=None,cropped_maskB=cropped_maskB)
BagsML.append(bag)
InstanceBagLabelML.append(instancelabels)
BagLabelsML = np.concatenate((BagLabelsML, baglabels), axis=0)
#Save Data
path_data = '/Users/josemiguelarrieta/Documents/SIVA/ExperimentsData2/defectAB/'+'class'+str(ClassNumber)+'/'+LabelType+'/'
f = open(path_data+'BagsML.pckl', 'wb')
pickle.dump(BagsML, f)
f.close()
f = open(path_data+'BagLabelsML.pckl', 'wb')
pickle.dump(BagLabelsML, f)
f.close()
f = open(path_data+'InstanceBagLabelML.pckl', 'wb')
pickle.dump(InstanceBagLabelML, f)
f.close()
print("ML data saved.")