def generate_set(image_n, image_size, name): def initialize(): X = np.zeros((image_n, image_size, image_size, 3)) Y = np.zeros((image_n, 9)) return X, Y def rand(): return np.random.uniform(0., 1.), np.random.uniform(0., 1.), np.random.uniform(0., 1.), \ np.random.uniform(0., 1.), np.random.uniform(0., 1.), np.random.uniform(0., 1.), \ np.random.uniform(0., 1.), np.random.uniform(0., 1.), np.random.uniform(0., 1.) X, Y = initialize() image = Image(initializer.new_image(image_size, image_size)) white_image_np = np.ones((image_size, image_size, 3)) action = Environment.Action.LINE for i in tqdm.tqdm(range(image_n), "Generating %s set" % name): r, g, b, a, x1, y1, x2, y2, size = rand() image.perform_action(action, (r, g, b, a, x1, y1, x2, y2, size)) array = image.array X[i] = (array / 255.) - white_image_np Y[i] = [r, g, b, a, x1, y1, x2, y2, size] image.clear() path = "./{}_{}.npy" np.save(path.format(name, "X"), X) np.save(path.format(name, "Y"), Y)
def generate_set(image_n, image_size, name, shape, without_rotation): def initialize(): X = np.zeros((image_n, image_size, image_size, 3)) Y = np.zeros((image_n, 9)) return X, Y def rand(): return np.random.uniform(0., 1.), np.random.uniform(0., 1.), np.random.uniform(0., 1.), \ np.random.uniform(0., 1.), np.random.uniform(0., 1.), np.random.uniform(0., 1.), \ np.random.uniform(0. + (1. / image_size), 1.), \ np.random.uniform(0. + (1. / image_size), 1.), np.random.uniform(0., 1.) X, Y = initialize() image = Image(initializer.new_image(image_size, image_size)) white_image_np = np.ones((image_size, image_size, 3)) action = Environment.Action.ELLIPSE if shape == 'ellipse' else Environment.Action.RECTANGLE for i in tqdm.tqdm(range(image_n), "Generating %s set" % name): r, g, b, a, x, y, w, h, rotation = rand() w = min(w, 1 - x) h = min(h, 1 - y) rotation = 0.5 if without_rotation else rotation image.perform_action(action, (r, g, b, a, x, y, w, h, rotation)) array = image.array X[i] = (array / 255.) - white_image_np Y[i] = [r, g, b, a, x, y, w, h, rotation] image.clear() path = "./{}_{}.npy" np.save(path.format(name, "X"), X) np.save(path.format(name, "Y"), Y)
def generate_set(num_images, image_size, name, max_examples_per_part, without_rotation): def initialize(num_examples): X = np.zeros((num_examples, image_size, image_size, 3)) Y = np.zeros((num_examples, 4)) return X, Y def save(X, Y, p): path = "./{}_{}_{}.npy" np.save(path.format(name, "X", p), X) np.save(path.format(name, "Y", p), Y) def generate_args(a): if a == 0 or a == 1: return generate_args_for_selection_shape() elif a == 2: return generate_args_for_line() else: return generate_args_for_triangle() def generate_args_for_selection_shape(): def rand(): return np.random.uniform(0., 1.), np.random.uniform(0., 1.), np.random.uniform(0., 1.), \ np.random.uniform(0., 1.), np.random.uniform(0., 1.), np.random.uniform(0., 1.), \ np.random.uniform(0. + (1. / image_size), 1.), \ np.random.uniform(0. + (1. / image_size), 1.), np.random.uniform(0., 1.) r, g, b, a, x, y, w, h, rotation = rand() w = min(w, 1 - x) h = min(h, 1 - y) rotation = 0.5 if without_rotation else rotation return r, g, b, a, x, y, w, h, rotation def generate_args_for_line(): def rand(): return np.random.uniform(0., 1.), np.random.uniform(0., 1.), np.random.uniform(0., 1.), \ np.random.uniform(0., 1.), np.random.uniform(0., 1.), np.random.uniform(0., 1.), \ np.random.uniform(0., 1.), np.random.uniform(0., 1.), np.random.uniform(0., 1.) return rand() def generate_args_for_triangle(): def rand(): return np.random.uniform(0., 1.), np.random.uniform(0., 1.), np.random.uniform(0., 1.), \ np.random.uniform(0., 1.), np.random.uniform(0., 1.), np.random.uniform(0., 1.), \ np.random.uniform(0., 1.), np.random.uniform(0., 1.), np.random.uniform(0., 1.), \ np.random.uniform(0., 1.) return rand() remaining_examples = num_images part = 1 image = Image(initializer.new_image(image_size, image_size)) white_image_np = np.ones((image_size, image_size, 3)) while remaining_examples > 0: examples_in_current_part = min(remaining_examples, max_examples_per_part) X, Y = initialize(examples_in_current_part) for i in tqdm.tqdm(range(examples_in_current_part), "Generating {} set, part {}".format(name, part)): action = np.random.randint(0, 4) args = generate_args(action) image.perform_action(action, args) array = image.array X[i] = (array / 255.) - white_image_np Y[i][action] = 1 image.clear() save(X, Y, part) remaining_examples -= examples_in_current_part part += 1