def _create_example(pathuni): """Bulk of dataset processing. Converts str path to serialized tf.Example.""" path, uni, binary_fp = pathuni final = {} # zoom out path = svg_utils.zoom_out(path) # make clockwise path = svg_utils.canonicalize(path) # render path for training final['rendered'] = svg_utils.per_step_render(path, absolute=True) # make path relative path = svg_utils.make_relative(path) # convert to vector vector = svg_utils.path_to_vector(path, categorical=True) # make simple vector vector = np.array(vector) vector = np.concatenate( [np.take(vector, [0, 4, 5, 9], axis=-1), vector[..., -6:]], axis=-1) # count some stats final['seq_len'] = np.shape(vector)[0] final['class'] = int(svg_utils.map_uni_to_alphanum(uni)) final['binary_fp'] = str(binary_fp) # append eos vector = svg_utils.append_eos(vector.tolist(), True, 10) # pad path to 51 (with eos) final['sequence'] = np.concatenate( (vector, np.zeros(((50 - final['seq_len']), 10))), 0) # make pure list: final['rendered'] = np.reshape(final['rendered'][..., 0], [64 * 64]).astype(np.float32).tolist() final['sequence'] = np.reshape(final['sequence'], [51 * 10]).astype(np.float32).tolist() final['class'] = np.reshape(final['class'], [1]).astype(np.int64).tolist() final['seq_len'] = np.reshape(final['seq_len'], [1]).astype(np.int64).tolist() return generator_utils.to_example(final).SerializeToString()
def _create_example(pathuni): """Bulk of dataset processing. Converts str path to serialized tf.Example.""" path, uni, binary_fp = pathuni final = {} # print(path) #f = open('/home1/wangyz/magenta-master/data-anypair/all_paths_' + str(uni) +'.txt','a') #f.write(str(path) + '\n') # f.close() f = open('/home1/wangyz/magenta-master/data-anypair/all_paths_' + str(uni) + '.txt', 'r') lines = f.read().split('\n') idx = random.randint(0, len(lines)-2) print(uni, binary_fp) #source_path = list(lines[idx]) # print(ast.literal_eval(lines[idx])) source_path = ast.literal_eval(lines[idx]) # input() # zoom out path = svg_utils.zoom_out(path) source_path = svg_utils.zoom_out(source_path) # make clockwise path = svg_utils.canonicalize(path) source_path = svg_utils.canonicalize(source_path) # render path for training final['rendered'] = svg_utils.per_step_render(path, absolute=True) final['source_rendered'] = svg_utils.per_step_render( source_path, absolute=True) # make path relative path = svg_utils.make_relative(path) source_path = svg_utils.make_relative(source_path) # convert to vector vector = svg_utils.path_to_vector(path, categorical=True) source_vector = svg_utils.path_to_vector(source_path, categorical=True) # make simple vector vector = np.array(vector) source_vector = np.array(source_vector) vector = np.concatenate( [np.take(vector, [0, 4, 5, 9], axis=-1), vector[..., -6:]], axis=-1) source_vector = np.concatenate( [np.take(source_vector, [0, 4, 5, 9], axis=-1), source_vector[..., -6:]], axis=-1) # count some stats # print(np.shape(vector)) # print(np.shape(source_vector)) # input() final['seq_len'] = np.shape(vector)[0] final['source_seq_len'] = np.shape(source_vector)[0] final['class'] = int(svg_utils.map_uni_to_alphanum(uni)) final['binary_fp'] = str(binary_fp) # append eos vector = svg_utils.append_eos(vector.tolist(), True, 10) source_vector = svg_utils.append_eos(source_vector.tolist(), True, 10) # pad path to 51 (with eos) final['sequence'] = np.concatenate( (vector, np.zeros(((50 - final['seq_len']), 10))), 0) final['source_sequence'] = np.concatenate( (source_vector, np.zeros(((50 - final['source_seq_len']), 10))), 0) # make pure list: final['rendered'] = np.reshape(final['rendered'][..., 0], [64*64]).astype(np.float32).tolist() final['source_rendered'] = np.reshape(final['source_rendered'][..., 0], [64*64]).astype(np.float32).tolist() final['sequence'] = np.reshape(final['sequence'], [51*10]).astype(np.float32).tolist() final['source_sequence'] = np.reshape(final['source_sequence'], [51*10]).astype(np.float32).tolist() final['class'] = np.reshape(final['class'], [1]).astype(np.int64).tolist() final['seq_len'] = np.reshape(final['seq_len'], [1]).astype(np.int64).tolist() final['source_seq_len'] = np.reshape(final['source_seq_len'], [1]).astype(np.int64).tolist() return generator_utils.to_example(final).SerializeToString()