marta-marta commited on
Commit
07b21c4
·
1 Parent(s): 5b65c9e

Created a strut based dataset generator.

Browse files
Data_Generation/Dataset_Generation_Functions.py CHANGED
@@ -1,17 +1,18 @@
1
  import numpy as np
2
 
3
- from Data_Generation.Shape_Generation_Functions import basic_box, diagonal_box_split, horizontal_vertical_box_split, \
4
- back_slash_box, forward_slash_box, back_slash_plus_box, forward_slash_plus_box, hot_dog_box, hamburger_box, \
5
- x_hamburger_box, x_hot_dog_box, x_plus_box
6
 
 
7
  import matplotlib.pyplot as plt
8
 
9
- from Data_Generation.Piecewise_Box_Functions import add_pixels
10
 
11
 
12
  ########################################################################################################################
13
  # Make the data using all the code in Shape_Generation_Functions.py
14
- def make_boxes(image_size, densities, shapes):
15
  """
16
  :param image_size: [int] - the pixel height and width of the generated arrays
17
  :param densities: [list] - of the values of each of the active pixels in each shape
@@ -19,7 +20,7 @@ def make_boxes(image_size, densities, shapes):
19
  :return: [list[tuple]] - [Array, Density, Thickness, Shape]
20
  """
21
 
22
- # matrix = []
23
  #
24
  # for function in shapes: # Adds different types of shapes
25
  #
@@ -38,83 +39,165 @@ def make_boxes(image_size, densities, shapes):
38
  # # Prevents solids shapes from being appended to the array
39
  # else:
40
  # break
41
- matrix = []
42
- base_shapes = []
43
- density_1 = []
44
- for function in shapes: # Create an array of the base shapes
45
- thickness = 0
46
- Array = function(thickness, 1, image_size)
47
- # density_1_tuple = np.array([Array, str(function.__name__), 1, thickness]) # Array, Shape, Density, Thickness
48
- # base_shapes.append(density_1_tuple)
49
-
50
- density_1 = np.append(density_1,(np.array([Array, str(function.__name__), 1, thickness])), axis=1) # Array, Shape, Density, Thickness
51
- # Add one to the thickness of the previous array
52
- # for j in range(image_size):
53
- while (np.where((Array == float(0)))[0] > 0).any():
54
- # Checks if there are any 0's left in the array to append
55
- # if (np.where((Array == float(0)))[0] > 0).any():
56
- # density_1.append(density_1_tuple, axis=0)
57
- thickness += 1
58
- if np.shape(density_1) == (4,):
59
- Array = add_pixels(density_1[0], 1) # will add 1 pixel to each previous array, rather than adding multiple and having to loop
60
-
61
- else:
62
- print(np.shape(density_1))
63
- print(density_1[-1][0])
64
- Array = add_pixels(density_1[-1][0], 1)
65
- # print(np.shape(Array))
66
- density_1_tuple = np.array([Array, str(function.__name__), 1, thickness])
67
- # else: # Prevents solids shapes from being appended to the array
68
- # break
69
- density_1 = np.vstack((density_1, density_1_tuple))
70
 
71
- matrix = []
72
- # print(np.shape(density_1[0]))
73
- # print(density_1[:][0])
 
 
 
74
  for i in range(len(densities)):
75
- some = np.multiply(density_1[:][0],densities[i]) #,density_1[:1])
76
- # print(np.shape(some))
77
- matrix.append(tuple(some))
78
-
79
-
80
- # # Adds different density values
81
- # for i in range(len(densities)):
82
- # # Loops through the possible thickness values
83
- # for j in range(image_size): # Adds additional Pixels
84
- # thickness = j
85
- # Array = (function(thickness, densities[i], image_size))
86
- #
87
- # # Checks if there are any 0's left in the array to append
88
- # if (np.where((Array == float(0)))[0] > 0).any():
89
- # the_tuple = (Array, str(function.__name__), densities[i], thickness)
90
- # matrix.append(the_tuple)
91
- #
92
- # # Prevents solids shapes from being appended to the array
93
- # else:
94
- # break
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
  return matrix
96
 
97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
  ########################################################################################################################
99
  # Testing
100
  image_size = 9
101
  densities = [1]
102
- shapes = [basic_box, diagonal_box_split, horizontal_vertical_box_split, back_slash_box, forward_slash_box,
103
- back_slash_plus_box, forward_slash_plus_box, hot_dog_box, hamburger_box, x_hamburger_box,
104
- x_hot_dog_box, x_plus_box]
105
 
106
- boxes = make_boxes(image_size, densities, shapes)
107
 
108
- # print(np.shape(boxes))
109
- desired_label = 'basic_box'
110
  desired_density = 1
111
- desired_thickness = 0
 
 
 
 
 
 
 
112
 
113
- box_arrays, box_shape, box_density, box_thickness, = list(zip(*boxes))[0], list(zip(*boxes))[1], list(zip(*boxes))[2], list(zip(*boxes))[3]
 
114
  # print(np.shape(box_arrays))
115
  # print(np.shape(box_shape))
116
  # print(np.shape(box_density))
117
 
118
- indices = [i for i in range(len(box_arrays)) if box_shape[i] == desired_label and box_density[i] == desired_density and box_thickness[i] == desired_thickness]
 
 
 
 
 
119
  plt.imshow(box_arrays[indices[0]])
120
  plt.show()
 
1
  import numpy as np
2
 
3
+ # from Data_Generation.Shape_Generation_Functions import basic_box, diagonal_box_split, horizontal_vertical_box_split, \
4
+ # back_slash_box, forward_slash_box, back_slash_plus_box, forward_slash_plus_box, hot_dog_box, hamburger_box, \
5
+ # x_hamburger_box, x_hot_dog_box, x_plus_box
6
 
7
+ from Piecewise_Box_Functions import basic_box_array, back_slash_array, forward_slash_array, hamburger_array, hot_dog_array
8
  import matplotlib.pyplot as plt
9
 
10
+
11
 
12
 
13
  ########################################################################################################################
14
  # Make the data using all the code in Shape_Generation_Functions.py
15
+ def make_boxes(image_size, densities):
16
  """
17
  :param image_size: [int] - the pixel height and width of the generated arrays
18
  :param densities: [list] - of the values of each of the active pixels in each shape
 
20
  :return: [list[tuple]] - [Array, Density, Thickness, Shape]
21
  """
22
 
23
+ matrix = []
24
  #
25
  # for function in shapes: # Adds different types of shapes
26
  #
 
39
  # # Prevents solids shapes from being appended to the array
40
  # else:
41
  # break
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
+ # Establish the maximum thickness for each type of strut
44
+ max_vert = int(np.ceil(1 / 2 * image_size) - 2)
45
+ max_diag = int(image_size - 3)
46
+ max_basic = int(np.ceil(1 / 2 * image_size) - 1)
47
+
48
+ # Adds different density values
49
  for i in range(len(densities)):
50
+ for j in range(1, max_basic): # basic box loop, always want a border
51
+ basic_box_thickness = j
52
+ array_1 = basic_box_array(image_size, basic_box_thickness)
53
+ if np.unique([array_1]).all() > 0: # Checks if there is a solid figure
54
+ break
55
+
56
+ for k in range(0, max_vert):
57
+ hamburger_box_thickness = k
58
+ array_2 = hamburger_array(image_size, hamburger_box_thickness) + array_1
59
+ array_2 =np.array(array_2 > 0, dtype=int) # Keep all values 0/1
60
+ if np.unique([array_2]).all() > 0:
61
+ break
62
+
63
+ for l in range(0, max_vert):
64
+ hot_dog_box_thickness = l
65
+ array_3 = hot_dog_array(image_size, hot_dog_box_thickness) + array_2
66
+ array_3 = np.array(array_3 > 0, dtype=int)
67
+ if np.unique([array_3]).all() > 0:
68
+ break
69
+
70
+ for m in range(0, max_diag):
71
+ forward_slash_box_thickness = m
72
+ array_4 = forward_slash_array(image_size, forward_slash_box_thickness) + array_3
73
+ array_4 = np.array(array_4 > 0, dtype=int)
74
+ if np.unique([array_4]).all() > 0:
75
+ break
76
+
77
+ for n in range(0, max_diag):
78
+ back_slash_box_thickness = n
79
+ array_5 = back_slash_array(image_size, back_slash_box_thickness) + array_4
80
+ array_5 = np.array(array_5 > 0, dtype=int)
81
+ if np.unique([array_5]).all() > 0:
82
+ break
83
+ the_tuple = (array_5*densities[i], densities[i], basic_box_thickness,
84
+ forward_slash_box_thickness, back_slash_box_thickness,
85
+ hot_dog_box_thickness, hamburger_box_thickness)
86
+ matrix.append(the_tuple)
87
+
88
+ # matrix = []
89
+ # base_shapes = []
90
+ #
91
+ #
92
+ #
93
+ #
94
+ #
95
+ # density_1 = []
96
+ # for function in shapes: # Create an array of the base shapes
97
+ # thickness = 0
98
+ # Array = function(thickness, 1, image_size)
99
+ # # density_1_tuple = np.array([Array, str(function.__name__), 1, thickness]) # Array, Shape, Density, Thickness
100
+ # # base_shapes.append(density_1_tuple)
101
+ #
102
+ # density_1 = np.append(density_1,(np.array([Array, str(function.__name__), 1, thickness])), axis=1) # Array, Shape, Density, Thickness
103
+ # # Add one to the thickness of the previous array
104
+ # # for j in range(image_size):
105
+ # while (np.where((Array == float(0)))[0] > 0).any():
106
+ # # Checks if there are any 0's left in the array to append
107
+ # # if (np.where((Array == float(0)))[0] > 0).any():
108
+ # # density_1.append(density_1_tuple, axis=0)
109
+ # thickness += 1
110
+ # if np.shape(density_1) == (4,):
111
+ # Array = add_pixels(density_1[0], 1) # will add 1 pixel to each previous array, rather than adding multiple and having to loop
112
+ #
113
+ # else:
114
+ # print(np.shape(density_1))
115
+ # print(density_1[-1][0])
116
+ # Array = add_pixels(density_1[-1][0], 1)
117
+ # # print(np.shape(Array))
118
+ # density_1_tuple = np.array([Array, str(function.__name__), 1, thickness])
119
+ # # else: # Prevents solids shapes from being appended to the array
120
+ # # break
121
+ # density_1 = np.vstack((density_1, density_1_tuple))
122
+ #
123
+ # matrix = []
124
+ # # print(np.shape(density_1[0]))
125
+ # # print(density_1[:][0])
126
+ # for i in range(len(densities)):
127
+ # some = np.multiply(density_1[:][0],densities[i]) #,density_1[:1])
128
+ # # print(np.shape(some))
129
+ # matrix.append(tuple(some))
130
+ #
131
+ #
132
+ # # # Adds different density values
133
+ # # for i in range(len(densities)):
134
+ # # # Loops through the possible thickness values
135
+ # # for j in range(image_size): # Adds additional Pixels
136
+ # # thickness = j
137
+ # # Array = (function(thickness, densities[i], image_size))
138
+ # #
139
+ # # # Checks if there are any 0's left in the array to append
140
+ # # if (np.where((Array == float(0)))[0] > 0).any():
141
+ # # the_tuple = (Array, str(function.__name__), densities[i], thickness)
142
+ # # matrix.append(the_tuple)
143
+ # #
144
+ # # # Prevents solids shapes from being appended to the array
145
+ # # else:
146
+ # # break
147
  return matrix
148
 
149
 
150
+ ########################################################################################################################
151
+ # # Testing
152
+ # image_size = 9
153
+ # densities = [1]
154
+ # shapes = [basic_box, diagonal_box_split, horizontal_vertical_box_split, back_slash_box, forward_slash_box,
155
+ # back_slash_plus_box, forward_slash_plus_box, hot_dog_box, hamburger_box, x_hamburger_box,
156
+ # x_hot_dog_box, x_plus_box]
157
+ #
158
+ # boxes = make_boxes(image_size, densities, shapes)
159
+ #
160
+ # # print(np.shape(boxes))
161
+ # desired_label = 'basic_box'
162
+ # desired_density = 1
163
+ # desired_thickness = 0
164
+ #
165
+ # box_arrays, box_shape, box_density, box_thickness, = list(zip(*boxes))[0], list(zip(*boxes))[1], list(zip(*boxes))[2], list(zip(*boxes))[3]
166
+ # # print(np.shape(box_arrays))
167
+ # # print(np.shape(box_shape))
168
+ # # print(np.shape(box_density))
169
+ #
170
+ # indices = [i for i in range(len(box_arrays)) if box_shape[i] == desired_label and box_density[i] == desired_density and box_thickness[i] == desired_thickness]
171
+ # plt.imshow(box_arrays[indices[0]])
172
+ # plt.show()
173
  ########################################################################################################################
174
  # Testing
175
  image_size = 9
176
  densities = [1]
 
 
 
177
 
178
+ boxes = make_boxes(image_size, densities)
179
 
 
 
180
  desired_density = 1
181
+ # desired_thickness = 0
182
+
183
+ desired_basic_box_thickness =1
184
+ desired_forward_slash_box_thickness=2
185
+ desired_back_slash_box_thickness=0
186
+ desired_hot_dog_box_thickness=0
187
+ desired_hamburger_box_thickness=0
188
+
189
 
190
+ box_arrays, box_density, basic_box_thickness, forward_slash_box_thickness, back_slash_box_thickness,hot_dog_box_thickness, hamburger_box_thickness\
191
+ = list(zip(*boxes))[0], list(zip(*boxes))[1], list(zip(*boxes))[2], list(zip(*boxes))[3], list(zip(*boxes))[4], list(zip(*boxes))[5], list(zip(*boxes))[6]
192
  # print(np.shape(box_arrays))
193
  # print(np.shape(box_shape))
194
  # print(np.shape(box_density))
195
 
196
+ indices = [i for i in range(len(box_arrays)) if box_density[i] == desired_density
197
+ and basic_box_thickness[i] == desired_basic_box_thickness
198
+ and forward_slash_box_thickness[i] == desired_forward_slash_box_thickness
199
+ and back_slash_box_thickness[i] == desired_back_slash_box_thickness
200
+ and hot_dog_box_thickness[i] == desired_hot_dog_box_thickness
201
+ and hamburger_box_thickness[i] == desired_hamburger_box_thickness]
202
  plt.imshow(box_arrays[indices[0]])
203
  plt.show()