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(image_n, image_size, name): def scale(x): return x / float(image_size) def initialize(): X = np.zeros((image_n, image_size, image_size)) Y = np.zeros((image_n, 9)) return X, Y X, Y = initialize() image = Image(initializer.new_image(image_size, image_size)) white_image_np = np.ones((image_size, image_size)) for i in tqdm.tqdm(range(image_n), "Generating %s set" % name): square_size = np.random.randint(3, image_size) x_coor = np.random.randint(0, image_size - square_size) y_coor = np.random.randint(0, image_size - square_size) image.perform_action( RECTANGLE, (RED, GREEN, BLUE, ALPHA, scale(x_coor), scale(y_coor), scale(square_size), scale(square_size), ROTATION)) array = np.sum(image.array, axis=2) / 3 X[i] = (array / 255.) - white_image_np # draw white rectangle to "reset" image image.perform_action_without_array_update( RECTANGLE, (1., 1., 1., ALPHA, scale(x_coor), scale(y_coor), scale(square_size), scale(square_size), ROTATION)) Y[i][0] = RED Y[i][1] = GREEN Y[i][2] = BLUE Y[i][3] = ALPHA Y[i][4] = scale(x_coor) Y[i][5] = scale(y_coor) Y[i][6] = scale(square_size) Y[i][7] = scale(square_size) Y[i][8] = ROTATION path = "./{}_{}.npy" np.save(path.format(name, "X"), X) np.save(path.format(name, "Y"), Y)
def plugin_main(name, size, r, g, b, a, x1, y1, x2, y2, x3, y3): image = Image(initializer.new_image(size, size)) action = Environment.Action.TRIANGLE image.perform_action(action, (r, g, b, a, x1, y1, x2, y2, x3, y3)) image.save("%s/%s" % (PATH, name))
def plugin_main(name, size, r, g, b, a, x, y, w, h, rotation, shape): image = Image(initializer.new_image(size, size)) action = Environment.Action.ELLIPSE if shape == 'ellipse' else Environment.Action.RECTANGLE image.perform_action(action, (r, g, b, a, x, y, w, h, rotation)) image.save("%s/%s" % (PATH, name))
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