def __init__(self, config): self.config = config self.display_id = int(config['display_id']) self.use_html = config['status'] == "train" and bool( config['use_visdom']) == True self.win_size = int(config['display_winsize']) self.name = config['name'] self.port = int(config['display_port']) self.saved = False # using modify <display_port> and <display_server> to connect visdom server if self.display_id > 0: import visdom self.ncols = int(config['display_ncols']) self.vis = visdom.Visdom(server=config['display_server'], port=self.port, env=config['display_env']) if not self.vis.check_connection(): self.create_visdom_connections() # create html objerct in <checkpoints_dir>/web/>; images will be saved in <checkpoints_dir>/web/images/> if self.use_html: self.web_dir = os.path.join(config['checkpoints_dir'], self.name, 'web') self.img_dir = os.path.join(self.web_dir, 'images') print('create web directory %s...' % self.web_dir) tools.mkdirs([self.web_dir, self.img_dir]) # cerate a log file to save the training loss self.log_name = os.path.join(config['checkpoints_dir'], self.name, 'loss_log.txt') with open(self.log_name, "a") as log_file: now = time.strftime("%c") log_file.write( '================ Training Loss (%s) ================\n' % now)
def __init__(self, dataset, dir_save, args, overwrite=False, size_file=5000000, max_feat_len=3000): self.dataset = dataset self.max_feat_len = max_feat_len self.dir_save = dir_save mkdirs(self.dir_save) self.args = args self.save_multilabel = args.dirs.vocab_phone self.size_file = size_file self.dim_feature = dataset[0]['feature'].shape[-1] self.overwrite = overwrite
def main(timit_dir): for org_dir, new_dir, wav_scp in zip(['TRAIN', 'TEST'], ['train_wavs', 'test_wavs'], ['train_wav.scp', 'test_wav.scp']): print("Generating {} ...".format(wav_scp)) p = Path(timit_dir+org_dir) with open(timit_dir+wav_scp, 'w') as fw: for i, org_wav in enumerate(p.glob('*/*/*.WAV')): f_wav = Path(str(org_wav).replace(org_dir, new_dir).replace('WAV', 'wav')) mkdirs(f_wav) os.system('~/easton/projects/kaldi/tools/sph2pipe_v2.5/sph2pipe -f wav {} {}'.format(org_wav, f_wav)) # /data/sxu/easton/data/TIMIT/TRAIN/DR2/FAEM0/SI2022.WAV uttid = '_'.join(str(f_wav).split('.')[0].split('/')[-2:]) fw.write(uttid + ' ' + str(f_wav) + '\n') if i % 999 == 0: print('\tprocessed {} wavs'.format(i)) print("Generate {} success".format(str(timit_dir+wav_scp)))
def split_save(self, length_file='feature_length.txt', capacity=50000): num_token = 0 num_damaged_sample = 0 fw = open(self.dir_save / length_file, 'w') def serialize_example(feature, y): atts = { 'feature': self._bytes_feature(feature.tostring()), 'class': self._int_feature(y), } example_proto = tf.train.Example(features=tf.train.Features( feature=atts)) return example_proto.SerializeToString() def generator(): nonlocal fw, i, capacity # for sample, _ in zip(self.dataset, tqdm(range(len(self.dataset)))): for j in tqdm( range(i * capacity, min((i + 1) * capacity, len(self.dataset)))): sample = self.dataset[j] line = sample['uttid'] + ' ' + str(len(sample['feature'])) fw.write(line + '\n') yield serialize_example(sample['feature'], sample['class']) for i in range(len(self.dataset) // capacity + 1): # for i in [2,3,4]: dataset_tf = tf.data.Dataset.from_generator(generator=generator, output_types=tf.string, output_shapes=()) record_file = self.dir_save / '{}.recode'.format(i) mkdirs(record_file) writer = tf.data.experimental.TFRecordWriter(str(record_file)) writer.write(dataset_tf) with open(str(self.dir_save / 'tfdata.info'), 'w') as fw: fw.write('data_file {}\n'.format(self.dataset.file)) fw.write('dim_feature {}\n'.format(self.dim_feature)) fw.write('num_tokens {}\n'.format(num_token)) fw.write('size_dataset {}\n'.format( len(self.dataset) - num_damaged_sample)) fw.write('damaged samples: {}\n'.format(num_damaged_sample)) return
def print_configs(configs): message = '' message += '----------------- Configs ---------------\n' for k in sorted(configs): comment = '' message += '{:>25}: {:<30}{}\n'.format(str(k), str(configs[str(k)]), comment) message += '----------------- End -------------------' print(message) # save to disk expr_dir = os.path.join(configs['checkpoints_dir'], configs['name']) tools.mkdirs(expr_dir) file_name = os.path.join(expr_dir, 'configs.txt') with open(file_name, 'wt') as opt_file: opt_file.write(message) opt_file.write('\n')
# dirs dir_dataInfo = Path.cwd() / 'data' dir_exps = Path.cwd() / 'exps' / args.dirs.exp args.dir_exps = dir_exps / CONFIG_FILE.split('/')[-1].split('.')[0] args.dir_log = args.dir_exps / 'log' args.dir_checkpoint = args.dir_exps / 'checkpoint' if not dir_dataInfo.is_dir(): dir_dataInfo.mkdir() if not dir_exps.is_dir(): dir_exps.mkdir() if not args.dir_exps.is_dir(): args.dir_exps.mkdir() if not args.dir_log.is_dir(): args.dir_log.mkdir() if not args.dir_checkpoint.is_dir(): args.dir_checkpoint.mkdir() args.dirs.train.tfdata = Path(args.dirs.train.tfdata) args.dirs.dev.tfdata = Path(args.dirs.dev.tfdata) mkdirs(args.dirs.train.tfdata) mkdirs(args.dirs.dev.tfdata) args.dirs.train.feat_len = args.dirs.train.tfdata / 'feature_length.txt' args.dirs.dev.feat_len = args.dirs.dev.tfdata / 'feature_length.txt' try: args.dim_input = TFData.read_tfdata_info( args.dirs.dev.tfdata)['dim_feature'] args.data.train_size = TFData.read_tfdata_info( args.dirs.train.tfdata)['size_dataset'] args.data.dev_size = TFData.read_tfdata_info( args.dirs.dev.tfdata)['size_dataset'] except: print("have not converted to tfdata yet: ")