def show_resolution(seed): rng = np.random.RandomState(seed) kwargs = dict(alphabet=LANGUAGE_MAP['english'].get_alphabet(), is_bold=False, is_slant=False, inverse_color=False, pixel_noise_scale=0) attr_list = [ basic_attribute_sampler(resolution=(8, 8), char='b', font='arial', scale=0.9, rotation=0, background=SolidColor((0, 0, 0)), foreground=SolidColor((0.5, 0.5, 0)), **kwargs), basic_attribute_sampler(resolution=(16, 16), char='x', font='time', scale=0.7, **kwargs), basic_attribute_sampler(resolution=(32, 32), char='g', font='flavors', scale=0.6, rotation=1, **kwargs), basic_attribute_sampler(resolution=(64, 64), scale=0.3, n_symbols=5, **kwargs), basic_attribute_sampler(resolution=(128, 128), scale=0.1, n_symbols=30, **kwargs), ] def attr_sampler(): for attr in attr_list: yield attr(seed=rand_seed(rng)) x, y = pack_dataset_resample(dataset_generator(attr_sampler(), 1000)) plot_dataset(x, y, h_axis='rotation', v_axis=None, n_col=5, n_row=1, hide_axis=True) ax = plt.gca() ax.set_xticks((np.arange(len(attr_list)) + 0.5) * x.shape[1]) ax.set_xticklabels(['8 x 8', '16 x 16', '32 x 32', '64 x 64', '128 x 128'], rotation=0) ax.get_xaxis().set_visible(True) plt.xlabel('')
def show_background(seed): rng = np.random.RandomState(seed) kwargs = dict(resolution=(128, 128), alphabet=LANGUAGE_MAP['english'].get_alphabet(), char='a', inverse_color=False, pixel_noise_scale=0) attr_list = [ basic_attribute_sampler(background=SolidColor((0.2, 0.2, 0)), foreground=SolidColor((0.8, 0, 0.8)), **kwargs), basic_attribute_sampler(background=lambda _rng: Gradient( types=('radial', ), seed=rand_seed(_rng)), foreground=lambda _rng: Gradient( types=('radial', ), seed=rand_seed(_rng)), **kwargs), basic_attribute_sampler( background=lambda _rng: Camouflage(stroke_angle=np.pi / 4, seed=rand_seed(_rng)), foreground=lambda _rng: Camouflage(stroke_angle=np.pi * 3 / 4, seed=rand_seed(_rng)), **kwargs), basic_attribute_sampler( background=lambda _rng: ImagePattern(seed=rand_seed(_rng)), foreground=lambda _rng: ImagePattern(seed=rand_seed(_rng)), **kwargs), add_occlusion(basic_attribute_sampler(**kwargs), n_occlusion=3, scale=lambda _rng: 0.3 * np.exp(_rng.randn() * 0.1), translation=lambda _rng: tuple(_rng.rand(2) * 2 - 1)) ] def attr_sampler(): for attr in attr_list: yield attr(seed=rand_seed(rng)) x, _, y = pack_dataset( dataset_generator(attr_sampler(), 1000, flatten_mask_except_first)) plot_dataset(x, y, h_axis='scale', v_axis=None, n_col=5, n_row=1, hide_axis=True) ax = plt.gca() ax.set_xticks((np.arange(5) + 0.5) * x.shape[1]) ax.set_xticklabels( ['Solid', 'Gradient', 'Camouflage', 'Natural', 'Occlusions'], rotation=0) ax.get_xaxis().set_visible(True) plt.xlabel('')
def test_drawing(self): basic_attribute_sampler() surface, ctxt = drawing._make_surface(32, 32) symbol = drawing.Symbol(alphabet='english', char='c', font='arial', foreground=drawing.SolidColor((0, 0, 0)), is_slant=False, is_bold=False, rotation=0., scale=0.9, translation=(0, 0)) drawing.draw_symbol(ctxt, symbol) array = drawing._surface_to_array(surface) self.assertEqual((32, 32, 3), array.shape)
def prepare_environment(font_model_remote_path, char_model_remote_path, n_samples=100000, target_dir='/tmp'): font_model_path = os.path.join(target_dir, os.path.basename(font_model_remote_path)) char_model_path = os.path.join(target_dir, os.path.basename(char_model_remote_path)) sp.run(["wget", "--continue", font_model_remote_path], cwd=target_dir) sp.run(["wget", "--continue", char_model_remote_path], cwd=target_dir) synbols_default_bw_path = os.path.join( target_dir, "synbols_default-bw_n=%d.h5py" % n_samples) if not os.path.exists(synbols_default_bw_path): attr_sampler = basic_attribute_sampler( alphabet=LANGUAGE_MAP['english'].get_alphabet(auxiliary=False), background=SolidColor((0, 0, 0)), foreground=SolidColor((1, 1, 1))) ds_generator = dataset_generator(attr_sampler, n_samples) write_h5(synbols_default_bw_path, ds_generator, n_samples) else: logger.info("Reusing existing dataset %s." % synbols_default_bw_path) return font_model_path, char_model_path, synbols_default_bw_path
def attr_sampler(): for char in 'abCD': for font in font_list: yield basic_attribute_sampler( alphabet=LANGUAGE_MAP['english'], char=char, font=font, is_bold=False, is_slant=False, resolution=(128, 128), pixel_noise_scale=0)(seed=rand_seed(rng))
def attr_sampler(): for lang in language_list: alphabet = LANGUAGE_MAP[lang].get_alphabet() for i in range(4): yield basic_attribute_sampler( alphabet=alphabet, char=lambda rng: rng.choice(alphabet.symbols), font=lambda rng: rng.choice(alphabet.fonts), is_bold=False, is_slant=False, resolution=(128, 128), pixel_noise_scale=0)(seed=rand_seed(rng))
def cluster_to_img_grid(font_cluster): bg = SolidColor((0, 0, 0)) fg = SolidColor((1, 1, 1)) img_grid = [] for font, _d in font_cluster: img_list = [] for char in 'abcdefghijkl': img = basic_attribute_sampler(font=font, char=char, is_bold=False, is_slant=False, scale=1., translation=(0, 0), background=bg, foreground=fg, rotation=0, inverse_color=False, resolution=(128, 128))() img_list.append(img.make_image()) img_grid.append(np.hstack(img_list)) return np.vstack(img_grid)
def attribute_sampler(seed=None): """Makes brightness dependent on scale and color dependant on symbol.""" char = np.random.choice(list('abc')) color_index = np.random.choice(len(colors), p=color_distr_dict[char]) color = colors[color_index] scale = 0.6 * np.exp(np.random.randn() * 0.2) brightness = 0.4 * np.exp(np.random.randn() * 0.2) / scale color = tuple(np.array(color) * brightness) fg = drawing.SolidColor(color) attr_sampler = basic_attribute_sampler(char=char, foreground=fg, background=drawing.NoPattern(), inverse_color=False, scale=scale, resolution=(64, 64), max_contrast=False) return attr_sampler( ) # returns a single sample from this specific attribute sampler
''' We use this file to bootstrap the font and matplotlib caches. This allows faster execution of the synbols command. ''' from synbols.generate import dataset_generator, basic_attribute_sampler from synbols.data_io import pack_dataset attr_sampler = basic_attribute_sampler() ds_generator = dataset_generator(attr_sampler, 2) pack_dataset(ds_generator)
""" Example Usage: ```` $ cd /my/dataset/directory $ synbols /path/to/this/script/basic_usage.py ``` The synbols command will launch the docker containing the fonts (and download it if necessary). It will also mount the current directory for saving the dataset and execute this script. Alternatively, you can mount any extra directory with the arguement `--mount-path`, and have the dataset written at this location by changing the `dataset_path` variable to point to the appropriate location. """ from synbols.generate import generate_and_write_dataset, basic_attribute_sampler dataset_path = "./large_translation_dataset" n_samples = 1000 def translation(rng): """Generates translations uniformly from (-2, 2), going outside of the box.""" return tuple(rng.uniform(low=-2, high=2, size=2)) # Modifies the default attribute sampler to fix the scale to a constant and the (x,y) translation to a new distribution attr_sampler = basic_attribute_sampler(scale=0.5, translation=translation) generate_and_write_dataset(dataset_path, attr_sampler, n_samples)
from synbols import generate from synbols import drawing from synbols.visualization import plot_dataset import matplotlib.pyplot as plt # bg = drawing.Camouflage(stroke_angle=1.) # bg = drawing.NoPattern() bg = drawing.MultiGradient(alpha=0.5, n_gradients=2, types=('linear', 'radial')) # bg = drawing.Gradient(types=('linear',), random_color=drawing.color_sampler(brightness_range=(0.1, 0.9))) # fg = drawing.Camouflage(stroke_angle=0.5) # fg = drawing.SolidColor((1, 1, 1)) fg = drawing.Gradient( types=('radial', ), random_color=drawing.color_sampler(brightness_range=(0.1, 0.9))) attr_sampler = generate.basic_attribute_sampler(resolution=(64, 64), foreground=fg, background=bg) # attr_sampler = generate.add_occlusion(attr_sampler) x, mask, y = pack_dataset( generate.dataset_generator(attr_sampler, 500, generate.flatten_mask)) plt.figure('dataset') plot_dataset(x, y, h_axis='char', v_axis='font', n_row=10, n_col=20) plt.savefig('view_generator.png')