default='parametric', choices=['euclidian', 'cosine', 'parametric']) parser.add_argument('--device', type=str, default='cuda') args = parser.parse_args() ############################### args.name = f'DTN_hallu{args.hallu_m}_shot{args.shot}_trainway{args.train_way}'+\ f'_validway{args.valid_way}_{args.distance}' wandb.init(config=args, project='dlcv_proto_net', name=args.name) # Image transform train_trans, valid_trans = get_transform() # Base Train data base_train_set = MiniImageNet_Dataset('../hw4_data/train/', train_trans) base_train_loader = DataLoader(base_train_set, batch_size=64, num_workers=6, shuffle=True) # Train data train_set = MiniImageNet_Dataset('../hw4_data/train/', train_trans) train_sampler = CategoriesSampler(train_set.label, n_batch=args.n_batch, n_ways=args.train_way, n_shot=args.shot + args.query) train_loader = DataLoader(train_set, batch_sampler=train_sampler, num_workers=6, worker_init_fn=worker_init_fn) # Valid data valid_set = MiniImageNet_Dataset('../hw4_data/val/', valid_trans)
if __name__ == '__main__': args = parse_args() os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu #0:1080ti 1:1070 warnings.filterwarnings("ignore") ## load dataset train_dataset = VCTK_Dataset('preprocess/vctk.h5', 'preprocess/sample_segments/train_samples', seg_len=128, mode='train') valid_dataset = VCTK_Dataset('preprocess/vctk.h5', 'preprocess/sample_segments/valid_samples', seg_len=128, mode='test') train_dataloader = DataLoader( train_dataset, batch_size=args.batch_size, #num_workers = args.num_workers, shuffle=True) valid_dataloader = DataLoader( valid_dataset, batch_size=args.batch_size * 4, ) #num_workers = args.num_workers) ## train train(args, train_dataloader, valid_dataloader)
transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) ]) root = '../hw3_data/digits/' # dataset source, target = args.source, args.target train_source_dataset = Digits_Dataset(root + f'{source}/train', source, t0) train_target_dataset = Digits_Dataset(root + f'{target}/train', target, t0) valid_source_dataset = Digits_Dataset(root + f'{source}/test', source, t1) valid_target_dataset = Digits_Dataset(root + f'{target}/test', target, t1) # dataloaders train_source_dataloader = DataLoader( train_source_dataset, batch_size=args.bsize, num_workers=args.num_workers, collate_fn=train_source_dataset.collate_fn, shuffle=True) train_target_dataloader = DataLoader( train_target_dataset, batch_size=args.bsize, num_workers=args.num_workers, collate_fn=train_target_dataset.collate_fn, shuffle=True) valid_source_dataloader = DataLoader(valid_source_dataset, batch_size=args.bsize * 4, num_workers=args.num_workers) valid_target_dataloader = DataLoader(valid_target_dataset, batch_size=args.bsize * 4, num_workers=args.num_workers)
def build_model(self): """ DataLoader """ train_transform = [ transforms.RandomHorizontalFlip(), transforms.Resize((self.img_size + 30, self.img_size + 30)), transforms.RandomCrop(self.img_size), transforms.ToArray(), transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]), transforms.ToTensor() ] test_transform = [ transforms.Resize((self.img_size, self.img_size)), transforms.ToArray(), transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]), transforms.ToTensor() ] self.trainA = os.path.join('dataset', self.dataset, 'trainA') self.trainB = os.path.join('dataset', self.dataset, 'trainB') self.testA = os.path.join('dataset', self.dataset, 'testA') self.testB = os.path.join('dataset', self.dataset, 'testB') self.trainA_loader = DataLoader(self.trainA, batch_size=self.batch_size, transforms=train_transform, shuffle=True) self.trainB_loader = DataLoader(self.trainB, batch_size=self.batch_size, transforms=train_transform, shuffle=True) self.testA_loader = DataLoader(self.testA, batch_size=1, transforms=test_transform, shuffle=False) self.testB_loader = DataLoader(self.testB, batch_size=1, transforms=test_transform, shuffle=False) """ Define Generator, Discriminator """ self.genA2B = ResnetGenerator(input_nc=3, output_nc=3, ngf=self.ch, n_blocks=self.n_res, img_size=self.img_size, light=self.light) self.genB2A = ResnetGenerator(input_nc=3, output_nc=3, ngf=self.ch, n_blocks=self.n_res, img_size=self.img_size, light=self.light) self.disGA = Discriminator(input_nc=3, ndf=self.ch, n_layers=7) self.disGB = Discriminator(input_nc=3, ndf=self.ch, n_layers=7) self.disLA = Discriminator(input_nc=3, ndf=self.ch, n_layers=5) self.disLB = Discriminator(input_nc=3, ndf=self.ch, n_layers=5) """ Define Loss """ self.L1_loss = dygraph.L1Loss() self.MSE_loss = layers.mse_loss self.BCELoss = bce_loss # BCELoss should be called with Normalize=True, use seperately """ Trainer """ self.G_optim = optimizer.Adam(learning_rate=self.lr, beta1=0.5, beta2=0.999, parameter_list=self.genA2B.parameters() + self.genB2A.parameters()) self.D_optim = optimizer.Adam(learning_rate=self.lr, beta1=0.5, beta2=0.999, parameter_list=self.disGA.parameters() + self.disLB.parameters())
default='cuda:0', help='cpu or cuda:0 or cuda:1') args = parser.parse_args() if string is None else parser.parse_args(string) return args if __name__ == '__main__': args = parse_args() wandb.init(config=args, project='dlcv_gan_face') transform = transforms.Compose([ transforms.Resize(args.img_size), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize([0.5] * 3, [0.5] * 3) ]) train_dataset = Face_Dataset('../hw3_data/face/train', transform) valid_dataset = Face_Dataset('../hw3_data/face/test', transform) train_dataloader = DataLoader(train_dataset, batch_size=args.batch, shuffle=True, num_workers=args.num_workers) valid_dataloader = DataLoader(valid_dataset, batch_size=args.batch, num_workers=args.num_workers) train(args, train_dataloader, valid_dataloader)
import copy # from depth_CPM import CPM3D from model import FSRCNN import torch.utils.data as data from torch import nn from train import train_model from tensorboardX import SummaryWriter from dataset import DataLoader from torch.optim.lr_scheduler import ExponentialLR import torch import torch.backends.cudnn as cudnn #training code if args.phase == 'train': dataloaders = data.DataLoader(DataLoader(args), batch_size=args.batch_size, shuffle=True, num_workers=args.workers, pin_memory=True) device = torch.device("cuda:0" if ( torch.cuda.is_available() and args.ngpu > 0) else "cpu") print("constructing model ....") model = FSRCNN() model = nn.DataParallel(model.to(device), gpuids) if args.resume: model.load_state_dict(torch.load(args.model_path))
def run_experiment(args): train_distortion, val_distortion = None, None if args.distortion: train_distortion = Distorter(loc=0, sd=args.distortion_sd, seed=args.seed) val_distortion = Distorter(loc=0, sd=args.distortion_sd, seed=args.seed + 1) train_params = dict( distortion=train_distortion, variable_binning=args.variable_binning, start=args.start, varying_start_value=args.varying_start_value, n_agents=args.n_agents, timesteps=args.timesteps, compute_fiv=args.compute_frequency_increment_values, ) train_loader = DataLoader( train_params, batch_size=args.batch_size, seed=args.seed, distortion=args.distortion, n_sims=args.n_sims, train=True, n_workers=args.n_workers, normalize_samples=args.normalize_samples, ) val_params = dict( distortion=val_distortion, variable_binning=args.variable_binning, start=args.start, varying_start_value=args.varying_start_value, n_agents=args.n_agents, timesteps=args.timesteps, compute_fiv=args.compute_frequency_increment_values, ) val_loader = DataLoader( val_params, batch_size=args.batch_size, seed=args.seed + 1, distortion=args.distortion, n_sims=int(args.val_size * args.n_sims), train=False, n_workers=args.n_workers, normalize_samples=args.normalize_samples, ) if args.cuda and torch.cuda.is_available(): device = torch.device("cuda") print("Using GPU") else: device = torch.device("cpu") if args.model == "FCN": model = FCN(1, 1) elif args.model == "LSTMFCN": model = LSTMFCN(args.hidden_size, 1, args.num_layers, 1, args.dropout, args.rnn_dropout, args.bidirectional) elif args.model == "INCEPTION": model = InceptionTime(1, 1) else: model = ResNet(1) model = model.to(device) trainer = Trainer(model, train_loader, val_loader, device=device) trainer.fit(args.n_epochs, learning_rate=args.learning_rate, lr_patience=args.learning_rate_patience, early_stop_patience=args.early_stop_patience, evaluation_maximum=args.evaluation_maximum) run_id = str(uuid.uuid1())[:8] if args.outfile is None else args.outfile trainer.model.eval() trainer.save_model(run_id) if args.test: print("Evaluating the model...") if args.test_samples is not None: print("Using precomputed test-samples...") else: print("Generating test samples...") test_samples = args.test_samples if args.test_samples is not None else generate_test_samples( args.timesteps, args.n_agents, args.n_workers) nn_results = test_model(model, test_samples, args.timesteps, args.n_agents, device=device, normalize_samples=args.normalize_samples) nn_scores = evaluate_results(nn_results, prefix="NN_") return run_id, nn_results, nn_scores
def language_smoothing_experiment(): import pandas as pd # create boost_results pandas dataframe with colums: boost_percent, cross_entropy, dataset_name boost_results_df = pd.DataFrame( columns=["boost_percents", "cross_entropies", "dataset_name"]) # create boost_results pandas dataframe with colums: discount_percent, cross_entropy, dataset_name discount_results_df = pd.DataFrame( columns=["discount_percents", "cross_entropies", "dataset_name"]) for dataset_name in ["TEXTCZ1", "TEXTEN1"]: print(f"---Language smoothing experiment on {dataset_name} dataset---") train_data = DataLoader(f"datasets/{dataset_name}_train.txt") heldout_data = DataLoader(f"datasets/{dataset_name}_heldout.txt") test_data = DataLoader(f"datasets/{dataset_name}_test.txt") print("Fitting the training data...") model = LinearInterpolationModel(train_data.get_clean_data(), 3) print("Fitting the lambdas on the TRAINING data...") # model.fit_lambdas(train_data.get_clean_data()) print( f"Test cross-entropy: {model.cross_entropy(test_data.get_clean_data())}\n\n" ) print("Now fitting the lambdas (correctly) on the HELDOUT data...") model.fit_lambdas(heldout_data.get_clean_data()) print( f"Test cross-entropy: {model.cross_entropy(test_data.get_clean_data())}\n\n" ) boost_results = boost_lambdas_experiment(model, test_data, dataset_name) boost_results[ "dataset_name"] = "Czech" if dataset_name == "TEXTCZ1" else "English" boost_results_df = boost_results_df.append(pd.DataFrame(boost_results), ignore_index=True) discount_results = decrease_lambdas_experiment(model, test_data, dataset_name) discount_results[ "dataset_name"] = "Czech" if dataset_name == "TEXTCZ1" else "English" discount_results_df = discount_results_df.append( pd.DataFrame(discount_results), ignore_index=True) # plot with seaborn import seaborn as sns sns.set(style="darkgrid") # plot boost_results plt.figure(figsize=(9, 6)) sns.lineplot(x="boost_percents", y="cross_entropies", data=boost_results_df, hue="dataset_name", marker="o", legend="brief") plt.title("Boosting trigram-lambda experiment") plt.xlabel("Boost percentage") plt.ylabel("Cross-entropy") plt.savefig("results/boost_experiment.png") # plot discount_results plt.figure(figsize=(9, 6)) sns.lineplot(x="discount_percents", y="cross_entropies", data=discount_results_df, hue="dataset_name", marker="o", legend="brief") plt.title("Discounting trigram-lambda experiment") plt.xlabel("Discount percentage") plt.ylabel("Cross-entropy") plt.savefig("results/discount_experiment.png")
def train_style(model_prefix, alpha=0.5, size=256, batch_size=4, tv_weight=1e-4, max_epoch=1000, lr=1e-4, style_layer = ['relu1_1', 'relu2_1', 'relu3_1', 'relu4_1'], content_layer = 'relu4_1'): desc_executor, gene_executor, gram_executors = init_executor(batch_size, style_layer, content_layer) tv_grad_executor = get_tv_grad_executor(desc_executor.arg_dict['data'], mx.gpu(), tv_weight) optimizer = mx.optimizer.Adam(learning_rate=lr, wd=5e-5) optim_states = [] for i, var in enumerate(gene_executor.grad_dict): if var != 'data': optim_states.append(optimizer.create_state(i, gene_executor.arg_dict[var])) # allocate memory else: optim_states.append([]) target_grams = [mx.nd.zeros(x.outputs[0].shape, mx.gpu()) for x in gram_executors] #[64*64, 128*128, 256*256, 512*512] gram_diff = [mx.nd.empty(x.outputs[0].shape, mx.gpu()) for x in gram_executors] gram_grad = [mx.nd.empty(x.shape, mx.gpu()) for x in desc_executor.outputs[:len(style_layer)]] content_content = mx.nd.empty(desc_executor.outputs[-1].shape, mx.gpu()) style_content = mx.nd.empty(content_content.shape, mx.gpu()) content_grad = mx.nd.empty(content_content.shape, mx.gpu()) # target_style = [mx.nd.empty(desc_executor.outputs[i].shape, mx.gpu()) for i in range(len(style_layer))] style_grad = [mx.nd.empty(x.shape, mx.gpu()) for x in desc_executor.outputs[:len(style_layer)]] content_loader = DataLoader(CONTENTPATH, batch_size=batch_size) style_loader = DataLoader(STYLEPATH, batch_size=batch_size) for epoch in range(max_epoch): content_loader.next().copyto(desc_executor.arg_dict['data']) desc_executor.forward() content_content[:] = desc_executor.outputs[-1] style_loader.next().copyto(desc_executor.arg_dict['data']) desc_executor.forward() style_content[:] = desc_executor.outputs[-1] target_content = adaInstanceNorm(content_content, style_content) for j in range(len(style_layer)): desc_executor.outputs[j].copyto(gram_executors[j].arg_dict['gram_data']) gram_executors[j].forward() target_grams[j][:] = gram_executors[j].outputs[0] target_grams[j][:] /= 1 target_content.copyto(gene_executor.arg_dict['data']) gene_executor.forward(is_train=True) generate_imgs = [postprocess_img(img.asnumpy()) for img in gene_executor.outputs[0]] generate_imgs = [preprocess_img(img) for img in generate_imgs] for i in range(batch_size): desc_executor.arg_dict['data'][i:i+1] = generate_imgs[i] #copy numpy to mxnet.ndarray tv_grad_executor.forward() desc_executor.forward(is_train=True) loss = [0] * len(desc_executor.outputs) for j in range(len(style_layer)): desc_executor.outputs[j].copyto(gram_executors[j].arg_dict['gram_data']) gram_executors[j].forward(is_train=True) gram_diff[j] = gram_executors[j].outputs[0] - target_grams[j] gram_executors[j].backward(gram_diff[j]) gram_grad[j][:] = gram_executors[j].grad_dict['gram_data'] / batch_size loss[j] += np.sum(np.square(gram_diff[j].asnumpy())) / batch_size dec_gen_feat = desc_executor.outputs[-1] layer_size = np.prod(dec_gen_feat.shape) content_diff = dec_gen_feat - target_content # loss[-1] += alpha * np.sum(np.mean(np.square(content_diff.asnumpy()), axis=(2,3))) / batch_size loss[-1] += alpha * np.sum(np.square(content_diff.asnumpy()/np.sqrt(layer_size))) / batch_size content_grad = alpha * content_diff / batch_size / layer_size if epoch % 20 == 0: print epoch, 'loss', sum(loss), np.array(loss) desc_executor.backward(style_grad+[content_grad]) gene_executor.backward(desc_executor.grad_dict['data']+tv_grad_executor.outputs[0]) for i, var in enumerate(gene_executor.grad_dict): if var != 'data': optimizer.update(i, gene_executor.arg_dict[var], gene_executor.grad_dict[var], optim_states[i]) #update parameter if epoch % 500 == 499: mx.nd.save('%s_args.nd'%model_prefix, gene_executor.arg_dict) mx.nd.save('%s_auxs.nd'%model_prefix, gene_executor.aux_dict) mx.nd.save('%s_args.nd'%model_prefix, gene_executor.arg_dict) mx.nd.save('%s_auxs.nd'%model_prefix, gene_executor.aux_dict)
def main(args): # Get device device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') # Define model model = nn.DataParallel(LightSpeech()).to(device) print("Model Has Been Defined") num_param = utils.get_param_num(model) print('Number of LightSpeech Parameters:', num_param) # Get dataset dataset = LightSpeechDataset() # Optimizer optimizer = torch.optim.Adam(model.parameters(), lr=hp.learning_rate, weight_decay=hp.weight_decay) # Criterion criterion = LigthSpeechLoss() # Load checkpoint if exists try: checkpoint = torch.load( os.path.join(hp.checkpoint_path, 'checkpoint_%d.pth.tar' % args.restore_step)) model.load_state_dict(checkpoint['model']) optimizer.load_state_dict(checkpoint['optimizer']) print("\n---Model Restored at Step %d---\n" % args.restore_step) except: print("\n---Start New Training---\n") if not os.path.exists(hp.checkpoint_path): os.mkdir(hp.checkpoint_path) # Init logger if not os.path.exists(hp.logger_path): os.mkdir(hp.logger_path) # Define Some Information Time = np.array([]) Start = time.clock() # Training model = model.train() for epoch in range(hp.epochs): # Get Training Loader training_loader = DataLoader(dataset, batch_size=hp.batch_size**2, shuffle=True, collate_fn=collate_fn, drop_last=True, num_workers=0) total_step = hp.epochs * len(training_loader) * hp.batch_size for i, batchs in enumerate(training_loader): for j, data_of_batch in enumerate(batchs): start_time = time.clock() current_step = i * hp.batch_size + j + args.restore_step + \ epoch * len(training_loader)*hp.batch_size + 1 # Init optimizer.zero_grad() # Get Data character = torch.from_numpy( data_of_batch["text"]).long().to(device) # mel_gt_target = torch.from_numpy( # data_of_batch["mel_gt_target"]).float().to(device) mel_tac2_target = torch.from_numpy( data_of_batch["mel_tac2_target"]).float().to(device) D = torch.from_numpy(data_of_batch["D"]).int().to(device) cemb = torch.from_numpy( data_of_batch["cemb"]).float().to(device) input_lengths = torch.from_numpy( data_of_batch["length_text"]).long().to(device) output_lengths = torch.from_numpy( data_of_batch["length_mel"]).long().to(device) max_c_len = max(input_lengths).item() max_mel_len = max(output_lengths).item() # Forward mel, padd_predicted, cemb_out = model(character, input_lengths, output_lengths, max_c_len, max_mel_len, D) # Cal Loss mel_loss, similarity_loss, duration_loss = criterion( mel, padd_predicted, cemb_out, mel_tac2_target, D, cemb) total_loss = mel_loss + similarity_loss + duration_loss # Logger t_l = total_loss.item() m_l = mel_loss.item() s_l = similarity_loss.item() d_l = duration_loss.item() with open(os.path.join("logger", "total_loss.txt"), "a") as f_total_loss: f_total_loss.write(str(t_l) + "\n") with open(os.path.join("logger", "mel_loss.txt"), "a") as f_mel_loss: f_mel_loss.write(str(m_l) + "\n") with open(os.path.join("logger", "similarity_loss.txt"), "a") as f_s_loss: f_s_loss.write(str(s_l) + "\n") with open(os.path.join("logger", "duration_loss.txt"), "a") as f_d_loss: f_d_loss.write(str(d_l) + "\n") # Backward total_loss.backward() # Clipping gradients to avoid gradient explosion nn.utils.clip_grad_norm_(model.parameters(), 1.) # Update weights optimizer.step() # Print if current_step % hp.log_step == 0: Now = time.clock() str1 = "Epoch [{}/{}], Step [{}/{}]:".format( epoch + 1, hp.epochs, current_step, total_step) str2 = "Mel Loss: {:.4f}, Duration Loss: {:.4f}, Similarity Loss: {:.4f};".format( m_l, d_l, s_l) str3 = "Time Used: {:.3f}s, Estimated Time Remaining: {:.3f}s.".format( (Now - Start), (total_step - current_step) * np.mean(Time)) print("\n" + str1) print(str2) print(str3) with open(os.path.join("logger", "logger.txt"), "a") as f_logger: f_logger.write(str1 + "\n") f_logger.write(str2 + "\n") f_logger.write(str3 + "\n") f_logger.write("\n") if current_step % hp.save_step == 0: torch.save( { 'model': model.state_dict(), 'optimizer': optimizer.state_dict() }, os.path.join(hp.checkpoint_path, 'checkpoint_%d.pth.tar' % current_step)) print("save model at step %d ..." % current_step) end_time = time.clock() Time = np.append(Time, end_time - start_time) if len(Time) == hp.clear_Time: temp_value = np.mean(Time) Time = np.delete(Time, [i for i in range(len(Time))], axis=None) Time = np.append(Time, temp_value)
ITORS = 100 # 加载字典 vocab = Vocab(vocab_path) char_vocab = CharVocab(char_vocab_path) entity_vocab = EntityVocab(entity_vocab_path) # 创建数据集 train_data_set = DataSet(path=train_data_path, vocab=vocab, entity_vocab=entity_vocab, entity_padding_len=max_sent_length) test_data_set = DataSet(path=test_data_path, vocab=vocab, entity_vocab=entity_vocab, entity_padding_len=max_sent_length) # 创建加载器 train_data_loader = DataLoader(train_data_set, shuffle=True, batch_size=BATCH_SIZE) test_data_loader = DataLoader(test_data_set, shuffle=True, batch_size=BATCH_SIZE) # 是否用GPU if torch.cuda.is_available(): device = torch.device('cuda:0') else: device = torch.device('cpu') # 模型初始化 model = Model(vocab=vocab, char_vocab=char_vocab, entity_vocab=entity_vocab, char_embedding_dim=char_embedding_dim, word_embedding_dim=word_embedding_dim, hidden_size=hidden_size, device=device, kernel_n=kernel_n, # 卷积核长度
def sentence_weight_examine(sentence_idx): dataset = SimpleDataset(validation_percentage=validation_percentage, dataset_name=dataset_name) loader = DataLoader(data='valid', simple_dataset=dataset, dataset_name='sem-2016', padding=False) # for idx, sentence in enumerate(dataset.valid_original_sentence): # print(idx) # print(sentence) # print(dataset.valid_data[idx][0]) # print(loader[idx][1]) # f, ax = plt.subplots(figsize=(9, 6)) # flights = flights_long.pivot("month", "year", "passengers") # print(type(flights)) # exit() item = loader[sentence_idx][0] preprocessed_sentence = dataset.valid_data[sentence_idx][0] orig_sentence = dataset.valid_original_sentence[sentence_idx] translator = str.maketrans(string.punctuation, ' ' * len(string.punctuation)) orig_sentence = orig_sentence.translate(translator) orig_sentence = orig_sentence.split() weights, existence = get_sentence_weights('./topic-attention', item) topics = [] for i in range(11): topics.append(str(i + 1)) stopwords_english = set(stopwords.words('english')) attention_weights = [] words = [] for idx, weight in enumerate(weights): weight = list(weight.squeeze(0).squeeze(-1).detach().numpy()) temp = [] for word in orig_sentence: words.append(word) if word.lower() in stopwords_english: temp.append(float(0.0)) else: temp.append( float(weight[preprocessed_sentence.index(word.lower())])) attention_weights.append(np.array(temp)) attention_weights = np.array(attention_weights) attention_weights = attention_weights.transpose() fig, ax = plt.subplots(figsize=(10, 10)) sns.heatmap(attention_weights, annot=True, fmt=".2f", ax=ax, xticklabels=topics, yticklabels=orig_sentence, cmap="YlGnBu", cbar_kws={'label': 'Attention Weights'}) plt.xlabel('Topics') plt.ylabel('Sentence') plt.savefig('./attention_heatmap/valid_sentence_#' + str(sentence_idx) + '_attention_weights') topic_probs = [] for idx in range(len(existence)): prob = float(existence[idx]) topic_probs.append(prob) topic_probs = [np.array(topic_probs)] topic_probs = np.array(topic_probs).transpose() fig, ax = plt.subplots(figsize=(10, 10)) sns.heatmap(topic_probs, annot=True, fmt=".2f", ax=ax, xticklabels=['Topic Probabilities'], yticklabels=topics, cmap="YlGnBu") plt.savefig('./attention_heatmap/valid_sentence_#' + str(sentence_idx) + '_topic_probs')
import tensorflow.keras as keras import tensorflow as tf from method import generator, discriminator, CallBackFun import os from tools import copy_code config = tf.ConfigProto() config.gpu_options.allow_growth = True tf.enable_eager_execution(config) os.environ["CUDA_VISIBLE_DEVICES"] = "3" experiment_path = '/export/project/SAVE_PATH/' train_folder = 'apr28-photo2ce-128-par1' copy_code(src='/export/project/CODE_PATH/', dst=experiment_path + train_folder + '/') train_data = DataLoader(path_x='../cezanne2photo/trainB/', path_y='../cezanne2photo/trainA/', is_pair=False) test_data = DataLoader(path_x='../cezanne2photo/testB/', path_y='../cezanne2photo/testA/', is_pair=False) # Different lr for generative network and discriminate network optimizer_dis = tf.train.AdamOptimizer(1e-6) optimizer_gen = tf.train.AdamOptimizer(1e-4) loss_fn_mse = keras.losses.MeanSquaredError() loss_fn_mae = keras.losses.MeanAbsoluteError() gen_xy = generator(input_shape=[128, 128, 3]) gen_yx = generator(input_shape=[128, 128, 3]) dis = discriminator(input_shape=[128, 128, 3]) # Print Network Information
m = GBDTMulti(LIB, out_dim=meta['out'], params=p) x_train, y_train, x_test, y_test = data m.set_data((x_train, y_train), (x_test, y_test)) t = time.time() m.train(ROUND) t = time.time() - t if args.time == 1: print("Average time: {:.3f}".format(t / ROUND)) else: print("Total time: {:.3f}".format(t)) del m if __name__ == '__main__': m = DataLoader() if args.data == 'mnist_reg': data, meta = m.get('mnist_reg', True, 10000) for k in [4, 8, 16]: regression(data, meta, 7, 0.1, k, True) regression(data, meta, 7, 0.1, k, False) elif args.data == 'nus-wide': data, meta = m.get('nus-wide', True, 10000) for k in [8, 16, 32, 64]: regression(data, meta, 8, 0.1, k, True) regression(data, meta, 8, 0.1, k, False) elif args.data == 'Caltech101': data, meta = m.get('Caltech101', False, 10000) for k in [8, 16, 32, 64]: classification(data, meta, 8, 0.1, k, True) classification(data, meta, 8, 0.1, k, False)
test_acc_stdev = np.std(test_acc_list) if print_result: print("K-fold: {}, Kernel: {}".format(self.loader.get_n_splits(), self.gpc_dict[0].kernel)) print("Train Acc: {} (+/- {})".format(train_acc_avg, train_acc_stdev)) print("Test Acc: {} (+/- {})".format(test_acc_avg, test_acc_stdev)) print("=" * 10) return train_acc_list, test_acc_list if __name__ == '__main__': now_fold = 0 data_loader = DataLoader('{}.tsv'.format(FILE_NAME)) data_loader.transform_x(decomposition.PCA, n_components=80) kernel_list = [ Matern(0.5) + Matern(1.5) + Matern(nu=2.5), random() * Matern(0.5) + random() * Matern(1.5) + random() * Matern(nu=2.5), random() * Matern(0.5) + random() * Matern(1.5) + random() * Matern(nu=2.5), random() * Matern(0.5) + random() * Matern(1.5) + random() * Matern(nu=2.5), ] results = [] for now_kernel in kernel_list: try: gp_classifier = GPC( kernel=now_kernel, loader=data_loader,
def validate(test_img_files_path, test_target_files_path, category_list, split_size, batch_size, load_size, model, cell_dim, num_boxes, num_classes, device, iou_threshold_nms, iou_threshold_map, threshold, use_nms): """ Uses the test dataset to validate the performance of the model. Calculates the mean Average Precision (mAP) for object detection. Parameters: test_img_files_path (str): System path to the image directory containing the test dataset images. test_target_files_path (str): System path to the json file containing the ground-truth labels for the test dataset. category_list (list): A list containing all classes which should be detected. split_size (int): Size of the grid which is applied to the image. batch_size (int): Batch size. load_size (int): Amount of batches which are loaded in one function call. model (): The YOLOv1-model. cell_dim (int): The dimension of a single cell. num_boxes (int): Amount of bounding boxes which are being predicted by the model. num_classes (int): Amount of classes which are being predicted. device (): Device which is used for training and testing the model. iou_threshold_nms (float): Threshold for the IoU between the predicted boxes and the ground-truth boxes for non maximum suppression. iou_threshold_map (float): Threshold for the IoU between the predicted boxes and the ground-truth boxes for mean average precision. threshold (float): Threshold for the confidence score of predicted bounding boxes. use_nms (bool): Specifies if non max suppression should be applied to the bounding box predictions. """ model.eval() print("DATA IS BEING LOADED FOR VALIDATION") print("") # Initialize the DataLoader for the test dataset data = DataLoader(test_img_files_path, test_target_files_path, category_list, split_size, batch_size, load_size) data.LoadFiles() # Here will all predicted and ground-truth bounding boxes for the whole test # dataset be stored. These two lists will be finally used for evaluation. # Every element of the list will have the following form: # [image index, class prediction, confidence score, x1, y1, x2, y2] # Every element of the list represents a single bounding box. all_pred_boxes = [] all_target_boxes = [] train_idx = 0 # Tracks the sample index for each image in the test dataset # This while loop is used to fill the two lists all_pred_boxes and all_target_boxes. while len(data.img_files) > 0: print("LOADING NEW VALIDATION BATCHES") print("Remaining validation files:" + str(len(data.img_files))) print("") data.LoadData() for batch_idx, (img_data, target_data) in enumerate(data.data): img_data = img_data.to(device) target_data = target_data.to(device) with torch.no_grad(): predictions = model(img_data) print('Extracting bounding boxes') print('Batch: {}/{} ({:.0f}%)'.format(batch_idx + 1, len( data.data), (batch_idx + 1) / len(data.data) * 100.)) print('') pred_boxes = extract_boxes(predictions, num_classes, num_boxes, cell_dim, threshold) target_boxes = extract_boxes(target_data, num_classes, 1, cell_dim, threshold) for sample_idx in range(len(pred_boxes)): if use_nms: # Applies non max suppression to the bounding box predictions nms_boxes = non_max_suppression(pred_boxes[sample_idx], iou_threshold_nms) else: # Use the same list without changing anything nms_boxes = pred_boxes[sample_idx] for nms_box in nms_boxes: all_pred_boxes.append([train_idx] + nms_box) for box in target_boxes[sample_idx]: all_target_boxes.append([train_idx] + box) train_idx += 1 pred = { 0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0, 11: 0, 12: 0, 13: 0 } for prediction in all_pred_boxes: cls_idx = prediction[1] pred[cls_idx] += 1 print(pred) pred = { 0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0, 11: 0, 12: 0, 13: 0 } for prediction in all_target_boxes: cls_idx = prediction[1] pred[cls_idx] += 1 print(pred) """
from config import * import copy from model import G import torch.utils.data as data from torch import nn from train import train_model from tensorboardX import SummaryWriter from dataset import DataLoader from torch.optim.lr_scheduler import ExponentialLR import torch import torch.backends.cudnn as cudnn #training code if args.phase=='train': dataloaders = data.DataLoader(DataLoader(args), batch_size=args.batch_size, shuffle=True, num_workers=args.workers, pin_memory=True) device = torch.device("cuda:0") print("constructing model ....") model = G(args.nframes) model = nn.DataParallel(model.to(device), gpuids) if args.resume: ckpt = torch.load(args.model_path) new_ckpt = {} for key in ckpt: if not key.startswith('module'): new_key = 'module.' + key else:
model.restore(str(hp.load_weight_file)) optimizer = optim.Adam(model.parameters()) current_step = model.get_step() stft = MelSTFT(filter_length=hp.n_fft, hop_length=hp.hop_length, win_length=hp.win_length, n_mel_channels=hp.n_mels, sampling_rate=hp.sampling_rate, mel_fmin=hp.min_f, mel_fmax=hp.max_f).to(device=hp.device) train_set = DataLoader(q_size=hp.q_size, n_loading_threads=hp.n_loading_threads, stft=stft, redundancy=hp.redundancy, device=hp.device) # Iterate on training session number: for session in hp.schedule: r, max_step, batch_size = session if current_step < max_step: model.set_r(r) train_set.set_state(batch_size, r) training_steps = max_step - current_step simple_table([(f'Steps with r={r}', str(training_steps // 1000) + 'k Steps'), ('Batch Size', batch_size), ('Outputs/Step (r)', model.get_r())])
'early_stop': 25, 'one_side': True, 'verbose': False, 'hist_cache': 48, 'min_samples': 8} x_train, y_train, x_test, y_test = data m = GBDTSingle(LIB, out_dim=1, params=p) preds = np.zeros_like(y_test) m.set_data((x_train, None), (x_test, None)) for i in range(meta['out']): yy_train = np.ascontiguousarray(y_train[:, i]) yy_test = np.ascontiguousarray(y_test[:, i]) m._set_label(yy_train, True) m._set_label(yy_test, False) m.train(ROUND) _ = m.predict(x_test) preds[:, i] = _ m.reset() np.save("result/gbdt", preds) if __name__ == '__main__': m = DataLoader() data, meta = m.get(args.data, False, args.N) if args.mode == 'gbdtmo': train_gbdt_multi(data, meta) elif args.mode == 'gbdtso': train_gbdt_single(data, meta) elif args.mode == 'lightgbm': train_lightgbm(data, meta)
import argparse import numpy as np import torch import torch.nn as nn import torch.optim as optim from dataset import Dataset from dataset import DataLoader train_dataset = Dataset('data') loader = DataLoader(train_dataset) for batch_inputs, batch_outputs, masks, lengths in loader: loader.print_batch(batch_inputs) print(lengths) print(masks)
args.name = f'shot{args.shot}_trainway{args.train_way}_validway{args.valid_way}'+\ f'_{args.distance}_epochs{args.epochs}' wandb.init(config=args, project='dlcv_proto_net', name=args.name) # Image transform train_trans, valid_trans = get_transform() # Train data train_set = MiniImageNet_Dataset('../hw4_data/train/', train_trans) train_sampler = CategoriesSampler(train_set.label, n_batch=args.n_batch, n_ways=args.train_way, n_shot=args.shot + args.query) train_loader = DataLoader(train_set, batch_sampler=train_sampler, num_workers=6, worker_init_fn=worker_init_fn) # Valid data valid_set = MiniImageNet_Dataset('../hw4_data/val/', valid_trans) valid_sampler = CategoriesSampler(valid_set.label, n_batch=args.n_batch, n_ways=args.valid_way, n_shot=args.shot + args.query) valid_loader = DataLoader(valid_set, batch_sampler=valid_sampler, num_workers=6, worker_init_fn=worker_init_fn) # model model = Conv4().to(args.device)
file_handler = logging.FileHandler('%s/record.txt' % (log_dir)) file_handler.setLevel(logging.INFO) file_handler.setFormatter(formatter) logger.addHandler(file_handler) log_string('PARAMETER ...') log_string(opt) # Data Loading ################################################################################################################### log_string('Load dataset {} ...'.format(opt.dataset)) Data_path = { "ModelNet": '/eva_data/psa/datasets/PointNet/ModelNet40_pcd/', "ShapeNet": '/eva_data/psa/datasets/MSN_PointNet/ShapeNetCore.v1' } TRAIN_DATASET = DataLoader(root=Data_path[opt.dataset], dataset=opt.dataset, sparsify_mode=opt.sparsify_mode, npoint=opt.num_point, split='train') TEST_DATASET = DataLoader(root=Data_path[opt.dataset], dataset=opt.dataset, sparsify_mode=opt.sparsify_mode, npoint=opt.num_point, split='test') trainDataLoader = torch.utils.data.DataLoader(TRAIN_DATASET, batch_size=opt.batch_size, shuffle=True, num_workers=4, drop_last=True) testDataLoader = torch.utils.data.DataLoader(TEST_DATASET, batch_size=opt.batch_size, shuffle=False,
if __name__ == '__main__': args = parse_args() os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu os.environ['CUDA_LAUNCH_BLOCKING'] = "1" ## load tokenizer #tokenizer = BertTokenizer.from_pretrained(pretrained_weights)#, do_lower_case=True) tokenizer = BertJapaneseTokenizer.from_pretrained( pretrained_weights) #, do_lower_case=True) ## load dataset train_dataset = Cinnamon_Dataset(f'{args.cinnamon_data_path}/train/', tokenizer, args.delta) valid_dataset = Cinnamon_Dataset(f'{args.cinnamon_data_path}/dev/', tokenizer, args.delta) train_dataloader = DataLoader(train_dataset, batch_size=args.batch_size, num_workers=args.num_workers, collate_fn=train_dataset.collate_fn, shuffle=True) valid_dataloader = DataLoader(valid_dataset, batch_size=args.batch_size * 4, num_workers=args.num_workers, collate_fn=valid_dataset.collate_fn) ## train train(args, train_dataloader, valid_dataloader)
def main(args): # Get device # device = torch.device('cuda'if torch.cuda.is_available()else 'cpu') device = 'cuda' # Define model model = FastSpeech().to(device) print("Model Has Been Defined") num_param = utils.get_param_num(model) print('Number of FastSpeech Parameters:', num_param) current_time = time.strftime("%Y-%m-%dT%H:%M", time.localtime()) writer = SummaryWriter(log_dir='log/' + current_time) optimizer = torch.optim.Adam(model.parameters(), betas=(0.9, 0.98), eps=1e-9) # Load checkpoint if exists try: checkpoint_in = open( os.path.join(hp.checkpoint_path, 'checkpoint.txt'), 'r') args.restore_step = int(checkpoint_in.readline().strip()) checkpoint_in.close() checkpoint = torch.load( os.path.join(hp.checkpoint_path, 'checkpoint_%08d.pth' % args.restore_step)) model.load_state_dict(checkpoint['model']) optimizer.load_state_dict(checkpoint['optimizer']) print("\n---Model Restored at Step %d---\n" % args.restore_step) except: print("\n---Start New Training---\n") if not os.path.exists(hp.checkpoint_path): os.mkdir(hp.checkpoint_path) # Get dataset dataset = FastSpeechDataset() # Optimizer and loss scheduled_optim = ScheduledOptim(optimizer, hp.d_model, hp.n_warm_up_step, args.restore_step) fastspeech_loss = FastSpeechLoss().to(device) print("Defined Optimizer and Loss Function.") # Init logger if not os.path.exists(hp.logger_path): os.mkdir(hp.logger_path) # Define Some Information Time = np.array([]) Start = time.perf_counter() # Training model = model.train() t_l = 0.0 for epoch in range(hp.epochs): # Get Training Loader training_loader = DataLoader(dataset, batch_size=hp.batch_size**2, shuffle=True, collate_fn=collate_fn, drop_last=True, num_workers=0) total_step = hp.epochs * len(training_loader) * hp.batch_size for i, batchs in enumerate(training_loader): for j, data_of_batch in enumerate(batchs): start_time = time.perf_counter() current_step = i * hp.batch_size + j + args.restore_step + \ epoch * len(training_loader)*hp.batch_size + 1 # Init scheduled_optim.zero_grad() # Get Data condition1 = torch.from_numpy( data_of_batch["condition1"]).long().to(device) condition2 = torch.from_numpy( data_of_batch["condition2"]).long().to(device) mel_target = torch.from_numpy( data_of_batch["mel_target"]).long().to(device) norm_f0 = torch.from_numpy( data_of_batch["norm_f0"]).long().to(device) mel_in = torch.from_numpy( data_of_batch["mel_in"]).float().to(device) D = torch.from_numpy(data_of_batch["D"]).int().to(device) mel_pos = torch.from_numpy( data_of_batch["mel_pos"]).long().to(device) src_pos = torch.from_numpy( data_of_batch["src_pos"]).long().to(device) lens = data_of_batch["lens"] max_mel_len = data_of_batch["mel_max_len"] # print(condition1,condition2) # Forward mel_output = model(src_seq1=condition1, src_seq2=condition2, mel_in=mel_in, src_pos=src_pos, mel_pos=mel_pos, mel_max_length=max_mel_len, length_target=D) # print(mel_target.size()) # print(mel_output) # print(mel_postnet_output) # Cal Loss # mel_loss, mel_postnet_loss= fastspeech_loss(mel_output, mel_postnet_output,mel_target,) # print(mel_output.shape,mel_target.shape) Loss = torch.nn.CrossEntropyLoss() predict = mel_output.transpose(1, 2) target1 = mel_target.long().squeeze() target2 = norm_f0.long().squeeze() target = ((target1 + target2) / 2).long().squeeze() # print(predict.shape,target.shape) # print(target.float().mean()) losses = [] # print(lens,target) for index in range(predict.shape[0]): # print(predict[i,:,:lens[i]].shape,target[i,:lens[i]].shape) losses.append( Loss(predict[index, :, :lens[index]].transpose(0, 1), target[index, :lens[index]]).unsqueeze(0)) # losses.append(0.5*Loss(predict[index,:,:lens[index]].transpose(0,1),target2[index,:lens[index]]).unsqueeze(0)) total_loss = torch.cat(losses).mean() t_l += total_loss.item() # assert np.isnan(t_l)==False with open(os.path.join("logger", "total_loss.txt"), "a") as f_total_loss: f_total_loss.write(str(t_l) + "\n") # Backward if not np.isnan(t_l): total_loss.backward() else: print(condition1, condition2, D) # Clipping gradients to avoid gradient explosion nn.utils.clip_grad_norm_(model.parameters(), hp.grad_clip_thresh) # Update weights if args.frozen_learning_rate: scheduled_optim.step_and_update_lr_frozen( args.learning_rate_frozen) else: scheduled_optim.step_and_update_lr() # Print if current_step % hp.log_step == 0: Now = time.perf_counter() str1 = "Epoch[{}/{}] Step[{}/{}]:".format( epoch + 1, hp.epochs, current_step, total_step) str2 = "Loss:{:.4f} ".format(t_l / hp.log_step) str3 = "LR:{:.6f}".format( scheduled_optim.get_learning_rate()) str4 = "T: {:.1f}s ETR:{:.1f}s.".format( (Now - Start), (total_step - current_step) * np.mean(Time)) print('\r' + str1 + ' ' + str2 + ' ' + str3 + ' ' + str4, end='') writer.add_scalar('loss', t_l / hp.log_step, current_step) writer.add_scalar('lreaning rate', scheduled_optim.get_learning_rate(), current_step) if hp.gpu_log_step != -1 and current_step % hp.gpu_log_step == 0: os.system('nvidia-smi') with open(os.path.join("logger", "logger.txt"), "a") as f_logger: f_logger.write(str1 + "\n") f_logger.write(str2 + "\n") f_logger.write(str3 + "\n") f_logger.write(str4 + "\n") f_logger.write("\n") t_l = 0.0 if current_step % hp.fig_step == 0 or current_step == 20: f = plt.figure() plt.matshow(mel_output[0].cpu().detach().numpy()) plt.savefig('out_predicted.png') plt.matshow( F.softmax(predict, dim=1).transpose( 1, 2)[0].cpu().detach().numpy()) plt.savefig('out_predicted_softmax.png') writer.add_figure('predict', f, current_step) plt.cla() f = plt.figure(figsize=(8, 6)) # plt.matshow(mel_target[0].cpu().detach().numpy()) # x=np.arange(mel_target.shape[1]) # y=sample_from_discretized_mix_logistic(mel_output.transpose(1,2)).cpu().detach().numpy()[0] # plt.plot(x,y) sample = [] p = F.softmax(predict, dim=1).transpose( 1, 2)[0].detach().cpu().numpy() for index in range(p.shape[0]): sample.append(np.random.choice(200, 1, p=p[index])) sample = np.array(sample) plt.plot(np.arange(sample.shape[0]), sample, color='grey', linewidth='1') for index in range(D.shape[1]): x = np.arange(D[0][index].cpu().numpy() ) + D[0][:index].cpu().numpy().sum() y = np.arange(D[0][index].detach().cpu().numpy()) if condition2[0][index].cpu().numpy() != 0: y.fill( (condition2[0][index].cpu().numpy() - 40.0) * 5) plt.plot(x, y, color='blue') plt.plot(np.arange(target.shape[1]), target[0].squeeze().detach().cpu().numpy(), color='red', linewidth='1') plt.savefig('out_target.png', dpi=300) writer.add_figure('target', f, current_step) plt.cla() plt.close("all") if current_step % (hp.save_step) == 0: print("save model at step %d ..." % current_step, end='') torch.save( { 'model': model.state_dict(), 'optimizer': optimizer.state_dict() }, os.path.join(hp.checkpoint_path, 'checkpoint_%08d.pth' % current_step)) checkpoint_out = open( os.path.join(hp.checkpoint_path, 'checkpoint.txt'), 'w') checkpoint_out.write(str(current_step)) checkpoint_out.close() # os.system('python savefig.py') print('save completed') end_time = time.perf_counter() Time = np.append(Time, end_time - start_time) if len(Time) == hp.clear_Time: temp_value = np.mean(Time) Time = np.delete(Time, [i for i in range(len(Time))], axis=None) Time = np.append(Time, temp_value)
def inference(args): if args.target=='mnistm': args.source = 'usps' elif args.target=='usps': args.source = 'svhn' elif args.target=='svhn': args.source = 'mnistm' else: raise NotImplementedError(f"{args.target}: not implemented!") size = args.img_size t1 = transforms.Compose([ transforms.Resize(size), transforms.Grayscale(3), transforms.ToTensor(), transforms.Normalize((0.5,0.5,0.5),(0.5,0.5,0.5)) ]) valid_target_dataset = Digits_Dataset_Test(args.dataset_path, t1) valid_target_dataloader = DataLoader(valid_target_dataset, batch_size=512, num_workers=6) load = torch.load( f"./p3/result/3_2/{args.source}2{args.target}/best_model.pth", map_location='cpu') feature_extractor = FeatureExtractor() feature_extractor.load_state_dict(load['F']) feature_extractor.cuda() feature_extractor.eval() label_predictor = LabelPredictor() label_predictor.load_state_dict(load['C']) label_predictor.cuda() label_predictor.eval() out_preds = [] out_fnames = [] count=0 for i,(imgs, fnames) in enumerate(valid_target_dataloader): bsize = imgs.size(0) imgs = imgs.cuda() features = feature_extractor(imgs) class_output = label_predictor(features) _, preds = class_output.max(1) preds = preds.detach().cpu() out_preds.append(preds) out_fnames += fnames count+=bsize print(f"\t [{count}/{len(valid_target_dataloader.dataset)}]", end=" \r") out_preds = torch.cat(out_preds) out_preds = out_preds.cpu().numpy() d = {'image_name':out_fnames, 'label':out_preds} df = pd.DataFrame(data=d) df = df.sort_values('image_name') df.to_csv(args.out_csv, index=False) print(f' [Info] finish predicting {args.dataset_path}')
encoding='utf8') # convert results to submission format convert(f'{args.save_result_path}/result.csv', f'{args.save_result_path}/submission.csv') # score if args.score: s = score(args.ref_file, f'{args.save_result_path}/submission.csv') print('\t[Info] score:', s) if __name__ == '__main__': args = parse_args() os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu os.environ['CUDA_LAUNCH_BLOCKING'] = "1" tokenizer = BertJapaneseTokenizer.from_pretrained( pretrained_weights) #, do_lower_case=True) dataset = Cinnamon_Dataset_Testing( f'/media/D/ADL2020-SPRING/project/cinnamon/{args.dev_or_test}/', tokenizer, args.delta) dataloader = DataLoader(dataset, batch_size=1, collate_fn=dataset.collate_fn, shuffle=False) inference(args, tokenizer, dataloader)
def train(num_gpus, rank, group_name, output_directory, log_directory, checkpoint_path): # Get device device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') torch.manual_seed(hp.seed) torch.cuda.manual_seed(hp.seed) #=====START: ADDED FOR DISTRIBUTED====== if num_gpus > 1: init_distributed(rank, num_gpus, group_name, **dist_config) #=====END: ADDED FOR DISTRIBUTED====== criterion = WaveGlowLoss(hp.sigma) model = WaveGlow().cuda() #=====START: ADDED FOR DISTRIBUTED====== if num_gpus > 1: model = apply_gradient_allreduce(model) #=====END: ADDED FOR DISTRIBUTED====== learning_rate = hp.learning_rate optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate) if hp.fp16_run: from apex import amp model, optimizer = amp.initialize(model, optimizer, opt_level='O1') # Load checkpoint if one exists iteration = 0 if checkpoint_path: model, optimizer, iteration = load_checkpoint(checkpoint_path, model, optimizer) iteration += 1 # next iteration is iteration + 1 # Get dataset dataset = FastSpeechDataset() # Get training loader print("Get Training Loader") training_loader = DataLoader(dataset, batch_size=hp.batch_size, shuffle=True, collate_fn=collate_fn, drop_last=True, num_workers=0) if rank == 0: if not os.path.isdir(output_directory): os.makedirs(output_directory) os.chmod(output_directory, 0o775) print("output directory", output_directory) if hp.with_tensorboard and rank == 0: logger = prepare_directories_and_logger(output_directory, log_directory) model = model.train() epoch_offset = max(0, int(iteration / len(training_loader))) beta = hp.batch_size print("Total Epochs: {}".format(hp.epochs)) print("Batch Size: {}".format(hp.batch_size)) # ================ MAIN TRAINNIG LOOP! =================== for epoch in range(epoch_offset, hp.epochs): print("Epoch: {}".format(epoch)) for i, data_of_batch in enumerate(training_loader): model.zero_grad() if not hp.pre_target: # Prepare Data src_seq = data_of_batch["texts"] src_pos = data_of_batch["pos"] src_seq = torch.from_numpy(src_seq).long().to(device) src_pos = torch.from_numpy(src_pos).long().to(device) alignment_target = get_alignment(src_seq, tacotron2).float().to(device) # For Data Parallel else: # Prepare Data audio = data_of_batch["audios"] src_seq = data_of_batch["texts"] src_pos = data_of_batch["pos"] mel_tgt = data_of_batch["mels"] alignment_target = data_of_batch["alignment"] audio = torch.cat(audio).to(device) src_seq = torch.from_numpy(src_seq).long().to(device) src_pos = torch.from_numpy(src_pos).long().to(device) alignment_target = torch.from_numpy( alignment_target).float().to(device) mel_tgt = torch.from_numpy(mel_tgt).float().to(device) # For Data Parallel mel_max_length = mel_tgt.size(1) outputs = model(src_seq, src_pos, audio, mel_max_length, alignment_target) _, _, _, duration_predictor = outputs max_like, dur_loss = criterion(outputs, alignment_target) # mel_loss = criterion(outputs, alignment_target, mel_tgt) loss = max_like + dur_loss loss.backward() reduced_loss = loss.item() print('{}:\t{:.9f}'.format(iteration, reduced_loss)) #grad_norm = torch.nn.utils.clip_grad_norm_(model.parameters(), hp.grad_clip_thresh)a '''if iteration % 8 == 0: optimizer.step() model.zero_grad()''' optimizer.step() if hp.with_tensorboard and rank == 0: logger.log_training(reduced_loss, dur_loss, learning_rate, iteration) if (iteration % hp.save_step == 0): if rank == 0: # logger.log_alignment(model, mel_predict, iteration) checkpoint_path = "{}/TTSglow_{}".format( output_directory, iteration) save_checkpoint(model, optimizer, learning_rate, iteration, checkpoint_path) iteration += 1
def cnn_train(net, user_opts, is_training=True): opts = dict() opts['expDir'] = 'Exp' opts['continue'] = -1 opts['batchSize'] = 10 opts['numSubBatches'] = 1 opts['train'] = [] opts['val'] = [] opts['gpu'] = [] opts['numEpochs'] = 5 opts['learningRate'] = 0.001 opts['weightDecay'] = 0.0005 opts['momentum'] = 0.9 opts['randomSeed'] = 0 opts['folder_map'] = [] opts['test_data_idx'] = [] opts['train_data_idx'] = [] opts['plotStatistics'] = True opts['silent'] = False opts = arg_parse(opts, user_opts) if 'data_loader_train' not in opts or 'data_loader_val' not in opts: opts['data_loader_train'] = DataLoader(opts['train_data_idx'], opts['teethRegionDetector']) opts['data_loader_val'] = DataLoader(opts['test_data_idx'], opts['teethRegionDetector']) if not os.path.isdir(opts['expDir']): os.mkdir(opts['expDir']) evaluate_mode = False def model_path(ep): return os.path.join(opts['expDir'], 'net-epoch-' + str(ep), 'model.ckpt') def model_folder_path(ep): return os.path.join(opts['expDir'], 'net-epoch-' + str(ep)) model_figure_path = os.path.join(opts['expDir'], 'net-train.pdf') fig = plt.gcf() fig.show() fig.canvas.draw() sess = tf.Session(config=net.config) if opts['continue'] is not None: prev_pos_1 = max( 0, min(opts['continue'], find_last_checkpoint(opts['expDir']))) else: prev_pos_1 = max(0, find_last_checkpoint(opts['expDir'])) start_1 = prev_pos_1 + 1 if prev_pos_1 >= 1: print('Resuming by loading epoch', str(prev_pos_1)) stats, sess = load_state(model_folder_path(prev_pos_1), model_path(prev_pos_1), net.saver, sess) if sess is None or stats is None: sess.run(tf.global_variables_initializer()) stats = dict() stats['train'] = [] stats['val'] = [] print('Failed to load. Starting with epoch ', str(start_1), '\n') else: print('Continuing at epoch ', str(start_1), '\n') else: sess.run(tf.global_variables_initializer()) if net.load_pre_trained_model_func is not None: net.load_pre_trained_model_func(sess) stats = dict() stats['train'] = [] stats['val'] = [] print('Starting at epoch ', str(start_1), '\n') state = dict() opts['session'] = sess timer = Timer() for ep in range(start_1 - 1, opts['numEpochs']): # Set the random seed based on the epoch and opts.randomSeed. # This is important for reproducibility, including when training # is restarted from a checkpoint. epoch = ep + 1 print(' Epoch # ' + str(epoch)) random.seed(epoch + opts['randomSeed']) # Train for one epoch params = opts params['epoch'] = epoch if is_training: [net, state] = process_epoch(net, state, params, timer, 'train') [net, state] = process_epoch(net, state, params, timer, 'val') else: [net, state] = process_epoch(net, state, params, timer, 'val', training=False) if not evaluate_mode: save_state(model_folder_path(epoch), model_path(epoch), net, state, opts['silent']) last_stats = state['stats'] stats['train'].append(last_stats['train']) stats['val'].append(last_stats['val']) save_stats(model_folder_path(epoch), stats) if opts['plotStatistics']: plot_stats(fig, stats, model_figure_path) plt.close(fig) return net, sess, stats
def main(args): # Get device device = torch.device('cuda'if torch.cuda.is_available()else 'cpu') # Define model model = nn.DataParallel(FastSpeech()).to(device) print("Model Has Been Defined") num_param = utils.get_param_num(model) print('Number of FastSpeech Parameters:', num_param) # Get dataset dataset = FastSpeechDataset() # Optimizer and loss optimizer = torch.optim.Adam( model.parameters(), betas=(0.9, 0.98), eps=1e-9) scheduled_optim = ScheduledOptim(optimizer, hp.d_model, hp.n_warm_up_step, args.restore_step) fastspeech_loss = FastSpeechLoss().to(device) print("Defined Optimizer and Loss Function.") # Load checkpoint if exists try: checkpoint = torch.load(os.path.join( hp.checkpoint_path, 'checkpoint_%d.pth.tar' % args.restore_step)) model.load_state_dict(checkpoint['model']) optimizer.load_state_dict(checkpoint['optimizer']) print("\n---Model Restored at Step %d---\n" % args.restore_step) except: print("\n---Start New Training---\n") if not os.path.exists(hp.checkpoint_path): os.mkdir(hp.checkpoint_path) # Init logger if not os.path.exists(hp.logger_path): os.mkdir(hp.logger_path) # Define Some Information Time = np.array([]) Start = time.clock() # Training model = model.train() for epoch in range(hp.epochs): # Get Training Loader training_loader = DataLoader(dataset, batch_size=hp.batch_size**2, shuffle=True, collate_fn=collate_fn, drop_last=True, num_workers=0) total_step = hp.epochs * len(training_loader) * hp.batch_size for i, batchs in enumerate(training_loader): for j, data_of_batch in enumerate(batchs): start_time = time.clock() current_step = i * hp.batch_size + j + args.restore_step + \ epoch * len(training_loader)*hp.batch_size + 1 # Init scheduled_optim.zero_grad() # Get Data character = torch.from_numpy( data_of_batch["text"]).long().to(device) mel_target = torch.from_numpy( data_of_batch["mel_target"]).float().to(device) D = torch.from_numpy(data_of_batch["D"]).int().to(device) mel_pos = torch.from_numpy( data_of_batch["mel_pos"]).long().to(device) src_pos = torch.from_numpy( data_of_batch["src_pos"]).long().to(device) max_mel_len = data_of_batch["mel_max_len"] # Forward # 注意:此处训练时没有将标准的时长送入decoder,还是将length regular输出的时长送了进去,和论文的叙述不符 mel_output, mel_postnet_output, duration_predictor_output = model(character, src_pos, mel_pos=mel_pos, mel_max_length=max_mel_len, length_target=D) # print(mel_target.size()) # print(mel_output.size()) # Cal Loss mel_loss, mel_postnet_loss, duration_loss = fastspeech_loss(mel_output, mel_postnet_output, duration_predictor_output, mel_target, D) total_loss = mel_loss + mel_postnet_loss + duration_loss # 三种loss的比例是1:1:1. # Logger t_l = total_loss.item() m_l = mel_loss.item() m_p_l = mel_postnet_loss.item() d_l = duration_loss.item() with open(os.path.join("logger", "total_loss.txt"), "a") as f_total_loss: f_total_loss.write(str(t_l)+"\n") with open(os.path.join("logger", "mel_loss.txt"), "a") as f_mel_loss: f_mel_loss.write(str(m_l)+"\n") with open(os.path.join("logger", "mel_postnet_loss.txt"), "a") as f_mel_postnet_loss: f_mel_postnet_loss.write(str(m_p_l)+"\n") with open(os.path.join("logger", "duration_loss.txt"), "a") as f_d_loss: f_d_loss.write(str(d_l)+"\n") # Backward total_loss.backward() # Clipping gradients to avoid gradient explosion nn.utils.clip_grad_norm_( model.parameters(), hp.grad_clip_thresh) # Update weights if args.frozen_learning_rate: scheduled_optim.step_and_update_lr_frozen( args.learning_rate_frozen) else: scheduled_optim.step_and_update_lr() # Print if current_step % hp.log_step == 0: Now = time.clock() str1 = "Epoch [{}/{}], Step [{}/{}]:".format( epoch+1, hp.epochs, current_step, total_step) str2 = "Mel Loss: {:.4f}, Mel PostNet Loss: {:.4f}, Duration Loss: {:.4f};".format( m_l, m_p_l, d_l) str3 = "Current Learning Rate is {:.6f}.".format( scheduled_optim.get_learning_rate()) str4 = "Time Used: {:.3f}s, Estimated Time Remaining: {:.3f}s.".format( (Now-Start), (total_step-current_step)*np.mean(Time)) print("\n" + str1) print(str2) print(str3) print(str4) with open(os.path.join("logger", "logger.txt"), "a") as f_logger: f_logger.write(str1 + "\n") f_logger.write(str2 + "\n") f_logger.write(str3 + "\n") f_logger.write(str4 + "\n") f_logger.write("\n") if current_step % hp.save_step == 0: torch.save({'model': model.state_dict(), 'optimizer': optimizer.state_dict( )}, os.path.join(hp.checkpoint_path, 'checkpoint_%d.pth.tar' % current_step)) print("save model at step %d ..." % current_step) end_time = time.clock() Time = np.append(Time, end_time - start_time) if len(Time) == hp.clear_Time: temp_value = np.mean(Time) Time = np.delete( Time, [i for i in range(len(Time))], axis=None) Time = np.append(Time, temp_value)
x_train, y_train, x_test, y_test = data m.set_data((x_train, y_train), (x_test, y_test)) m.train(ROUND) def train_gbdt_single(data, meta): depth = cfg.Depth[args.mode][args.data] lr = cfg.Learning_rate[args.mode][args.data] p = {'max_depth': depth, 'max_leaves': int(0.75 * 2 ** depth), 'topk': 0, 'loss': b"ce_column", 'gamma': GAMMA, 'num_threads': num_threads, 'max_bins': meta['bin'], 'lr': lr, 'reg_l2': 1.0, 'early_stop': 25, 'one_side': True, 'verbose': True, 'min_samples': min_samples} x_train, y_train, x_test, y_test = data m = GBDTSingle(LIB, out_dim=meta['out'], params=p) m.set_data((x_train, y_train), (x_test, y_test)) m.train_multi(ROUND) if __name__ == '__main__': m = DataLoader() data, meta = m.get(args.data, False, args.N) if args.mode == 'gbdtmo': train_gbdt_multi(data, meta) elif args.mode == 'gbdtso': train_gbdt_single(data, meta) elif args.mode == 'lightgbm': train_lightgbm(data, meta)