def __init__(self, obs_space, action_space, num_outputs, model_config, name): TorchModelV2.__init__(self, obs_space, action_space, num_outputs, model_config, name) nn.Module.__init__(self) custom_config = model_config["custom_options"] latent_size = custom_config['latent_size'] self.main = Encoder(latent_size=latent_size) if custom_config['encoder_path'] is not None: # saved checkpoints could contain extra weights such as linear_logsigma weights = torch.load(custom_config['encoder_path'], map_location=torch.device('cpu')) for k in list(weights.keys()): if k not in self.main.state_dict().keys(): del weights[k] self.main.load_state_dict(weights) print("Loaded Weights") else: print("No Load Weights") self.critic = nn.Sequential(nn.Linear(latent_size, 400), nn.ReLU(), nn.Linear(400, 300), nn.ReLU(), nn.Linear(300, 1)) self.actor = nn.Sequential(nn.Linear(latent_size, 400), nn.ReLU(), nn.Linear(400, 300), nn.ReLU()) self.alpha_head = nn.Sequential(nn.Linear(300, 3), nn.Softplus()) self.beta_head = nn.Sequential(nn.Linear(300, 3), nn.Softplus()) self._cur_value = None self.train_encoder = custom_config['train_encoder'] print("Train Encoder: ", self.train_encoder)
def __init__(self, driving, target, time_step, split, lr): self.dataset = DataSet(driving, target, time_step, split) f = open('dataset_obj.txt', 'wb') pickle.dump(self.dataset, f) f.close() print('save model finish!!!!!!!!!!!!!!!!!!') # f = open('dataset_obj.txt','rb') # self.dataset = pickle.load(f) # f.close() self.encoder = Encoder(input_size=self.dataset.get_num_features(), hidden_size=ENCODER_HIDDEN_SIZE, T=time_step) self.decoder = Decoder(encoder_hidden_size=ENCODER_HIDDEN_SIZE, decoder_hidden_size=DECODER_HIDDEN_SIZE, T=time_step) if torch.cuda.is_available(): # print('tocuda') self.encoder = self.encoder.cuda() self.decoder = self.decoder.cuda() self.encoder_optim = optim.Adam(self.encoder.parameters(), lr) self.decoder_optim = optim.Adam(self.decoder.parameters(), lr) self.loss_func = nn.CrossEntropyLoss() self.train_size, self.validation_size, self.test_size = self.dataset.get_size( ) self.best_dev_acc = 0.0
def __init__(self, obs_space, action_space, num_outputs, model_config, name): TorchModelV2.__init__(self, obs_space, action_space, num_outputs, model_config, name) nn.Module.__init__(self) custom_config = model_config['custom_options'] self.main = Encoder() if custom_config['encoder_path'] is not None: print("Load Trained Encoder") # saved checkpoints could contain extra weights such as linear_logsigma weights = torch.load(custom_config['encoder_path'], map_location={'cuda:0': 'cpu'}) for k in list(weights.keys()): if k not in self.main.state_dict().keys(): del weights[k] self.main.load_state_dict(weights) self.critic = nn.Sequential(nn.Linear(32, 1024), nn.ReLU(), nn.Linear(1024, 256), nn.ReLU(), nn.Linear(256, 1)) self.actor = nn.Sequential(nn.Linear(32, 1024), nn.ReLU(), nn.Linear(1024, 256), nn.ReLU(), nn.Linear(256, 3), nn.Sigmoid()) self.actor_logstd = nn.Parameter(torch.zeros(3), requires_grad=True) self._cur_value = None print("Train Encoder:", custom_config['train_encoder']) self.train_encoder = custom_config['train_encoder']
def train(dct_size, embed_size=256, hidden_size=512, epochs=10, num_layers=1, save_step=1000, lr=0.001, model_save='model/'): encoder = Encoder(embed_size=embed_size).to(device) decoder = Decoder(embed_size=embed_size, hidden_size=hidden_size, dct_size=len(dct), num_layers=num_layers).to(device) criterion = nn.CrossEntropyLoss() params = list(decoder.parameters()) + list(encoder.linear.parameters()) + list(encoder.bn.parameters()) optimizer = torch.optim.Adam(params, lr=lr) for epoch in range(epochs): print(f'epoch {epoch+1}/{epochs}: ') for i, (images, captions, lengths) in enumerate(tqdm(data_loader)): # Set mini-batch dataset images = images.to(device) captions = captions.to(device) targets = pack_padded_sequence(captions, lengths, batch_first=True)[0] # Forward, backward and optimize features = encoder(images) outputs = decoder(features, captions, lengths) loss = criterion(outputs, targets) decoder.zero_grad() encoder.zero_grad() loss.backward() optimizer.step() if (i+1) % save_step == 0: torch.save(decoder.state_dict(), os.path.join( model_save, 'decoder-{}-{}.ckpt'.format(epoch+1, i+1))) torch.save(encoder.state_dict(), os.path.join( model_save, 'encoder-{}-{}.ckpt'.format(epoch+1, i+1)))
def build_models(self): self.G = Generator(self.args.latent_dim).to(self.device) self.E = Encoder(self.args.latent_dim).to(self.device) self.D = Discriminator(self.args.latent_dim).to(self.device) self.G.apply(weights_init_normal) self.E.apply(weights_init_normal) self.D.apply(weights_init_normal)
def main(args): train_doc,train_summ,test_doc, \ test_summ,val_doc,val_summ=read_from_file(args.data_file) vocab = read_vocab(args.path_to_vocab) embeddings = Embeddings(args.embed_size, args.vocab_size).cuda() encoder = Encoder(args.embed_size, args.hidden_size).cuda() decoder = Decoder(args.embed_size, args.hidden_size, args.vocab_size).cuda() generator = BeamSearch(vocab, args.max_decode_len, args.min_decode_len, args.beam_size) trainloader = DataLoader(train_doc, train_summ, vocab, args.batch_size, args.max_doc_len, args.max_summ_len) testloader = Test_loader(test_doc, test_summ, vocab, args.max_doc_len, args.max_summ_test_len) valloader = Test_loader(val_doc, val_summ, vocab, args.max_doc_len, args.max_summ_test_len) if args.use_pretrained: params = torch.load(args.pretrained_model) embeddings.load_state_dict(params['embed_params']) encoder.load_state_dict(params['encoder_params']) decoder.load_state_dict(params['decoder_params']) test(embeddings, encoder, decoder, testloader, generator, args.lambda_) train(embeddings, encoder, decoder, generator, trainloader, valloader, args.iterations, args.lambda_, args.lr, args.max_grad_norm, args.initial_accum_val, args.threshold) test(embeddings, encoder, decoder, testloader, generator, args.lambda_)
def main(): parser = argparse.ArgumentParser(description='Test learned model') parser.add_argument('dir', type=str, help='log directory to load learned model') parser.add_argument('--render', action='store_true') parser.add_argument('--domain-name', type=str, default='cheetah') parser.add_argument('--task-name', type=str, default='run') parser.add_argument('-R', '--action-repeat', type=int, default=2) parser.add_argument('--episodes', type=int, default=1) args = parser.parse_args() # define environment and apply wrapper env = suite.load(args.domain_name, args.task_name) env = pixels.Wrapper(env, render_kwargs={ 'height': 64, 'width': 64, 'camera_id': 0 }) env = GymWrapper(env) env = RepeatAction(env, skip=args.action_repeat) # define models with open(os.path.join(args.dir, 'args.json'), 'r') as f: train_args = json.load(f) device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') encoder = Encoder().to(device) rssm = RecurrentStateSpaceModel(train_args['state_dim'], env.action_space.shape[0], train_args['rnn_hidden_dim']).to(device) action_model = ActionModel(train_args['state_dim'], train_args['rnn_hidden_dim'], env.action_space.shape[0]).to(device) # load learned parameters encoder.load_state_dict(torch.load(os.path.join(args.dir, 'encoder.pth'))) rssm.load_state_dict(torch.load(os.path.join(args.dir, 'rssm.pth'))) action_model.load_state_dict( torch.load(os.path.join(args.dir, 'action_model.pth'))) # define agent policy = Agent(encoder, rssm, action_model) # test learnged model in the environment for episode in range(args.episodes): policy.reset() obs = env.reset() done = False total_reward = 0 while not done: action = policy(obs) obs, reward, done, _ = env.step(action) total_reward += reward if args.render: env.render(height=256, width=256, camera_id=0) print('Total test reward at episode [%4d/%4d] is %f' % (episode + 1, args.episodes, total_reward))
def __init__(self, seq_len=20, learning_rate=3e-4): device = torch.device( "cuda: 0" if torch.cuda.is_available() else "cpu") self.device = device self.seq_len = seq_len time_stamp = time.strftime("%m-%d-%Y_%H:%M:%S", time.localtime()) print("run on device", device, ",current time:", time_stamp) self.writer = SummaryWriter('runs/emb_graph' + time_stamp) # define layers self.categ_embedding = CategoricalEmbedding().to(device) self.r2s_embedding = Route2Stop(vertex_feature=105, edge_feature=112).to(device) self.encoder = Encoder(input_size=100, seq_len=seq_len).to(device) self.fcn = FCN(input_size=100).to(device) self.similarity = Similarity(input_size=30, device=device).to(device) # define training parameters self.criterion = nn.BCELoss() self.optimizer = optim.Adam( [{ 'params': self.categ_embedding.parameters() }, { 'params': self.r2s_embedding.parameters() }, { 'params': self.encoder.parameters() }, { 'params': self.fcn.parameters() }, { 'params': self.similarity.parameters() }], lr=learning_rate)
def __init__(self, static_size, dynamic_size, hidden_size, num_layers=1, n_process_blocks=3, dropout=0.2): super(realCritic, self).__init__() self.static_encoder = Encoder(static_size, hidden_size) self.dynamic_encoder = Encoder(dynamic_size, hidden_size) self.decoder = Encoder(static_size, hidden_size) self.gru = nn.GRU(hidden_size, hidden_size, num_layers, batch_first=True) self.attention = criticAttention(hidden_size) self.n_process_blocks = n_process_blocks self.fc = nn.Sequential(nn.Linear(hidden_size, hidden_size), nn.ReLU(), nn.Linear(hidden_size, 1)) self.num_layers = num_layers self.drop_hh = nn.Dropout(p=dropout)
def transform(sentence, l1, l2, op_lang): if op_lang == "hi": dir = "training_checkpoints" with open("tensors1.pkl", 'rb') as f: example_input_batch = pickle.load(f) example_target_batch = pickle.load(f) elif op_lang == "nep": dir = "training_nepali" with open("tensors_nep.pkl", 'rb') as f: example_input_batch = pickle.load(f) example_target_batch = pickle.load(f) elif op_lang == "pun": dir = "training_punjabi" with open("tensors_pun.pkl", 'rb') as f: example_input_batch = pickle.load(f) example_target_batch = pickle.load(f) print(example_input_batch) global input_lang, output_lang input_lang = l1 output_lang = l2 global BATCH_SIZE, units, embedding_dim BATCH_SIZE = 64 units = 512 if op_lang == "pun": units = 1024 embedding_dim = 256 vocab_inp_size = len(input_lang.word2index) + 1 vocab_tar_size = len(output_lang.word2index) + 1 global encoder, decoder, optimizer optimizer = tf.keras.optimizers.Adam() loss_object = tf.keras.losses.SparseCategoricalCrossentropy( from_logits=True, reduction='none') encoder = Encoder(vocab_inp_size, embedding_dim, units, BATCH_SIZE) sample_hidden = encoder.initialize_hidden_state() sample_output, sample_hidden = encoder(example_input_batch, sample_hidden) attention_layer = BahdanauAttention(10) attention_result, attention_weights = attention_layer( sample_hidden, sample_output) decoder = Decoder(vocab_tar_size, embedding_dim, units, BATCH_SIZE) sample_decoder_output, _, _ = decoder(tf.random.uniform((BATCH_SIZE, 1)), sample_hidden, sample_output) checkpoint = tf.train.Checkpoint(optimizer=optimizer, encoder=encoder, decoder=decoder) curr = pathlib.Path(__file__) print(encoder.summary()) temp = os.path.join(curr.parent, "available_models") checkpoint_dir = os.path.join(temp, dir) print(checkpoint_dir) checkpoint_prefix = os.path.join(checkpoint_dir, "ckpt") print(checkpoint_dir) print(checkpoint.restore(tf.train.latest_checkpoint(checkpoint_dir))) # print(checkpoint.restore(checkpoint_dir)) print(encoder.summary()) return translate(sentence)
def da_rnn(train_data: TrainData, n_targs: int, encoder_hidden_size=64, decoder_hidden_size=64, T=10, learning_rate=0.01, batch_size=128): train_cfg = TrainConfig(T, int(train_data.feats.shape[0] * 0.7), batch_size, nn.MSELoss()) logger.info(f"Training size: {train_cfg.train_size:d}.") enc_kwargs = {"input_size": train_data.feats.shape[1], "hidden_size": encoder_hidden_size, "T": T} encoder = Encoder(**enc_kwargs).to(device) with open(os.path.join("data", "enc_kwargs.json"), "w") as f: json.dump(enc_kwargs, f, indent=4) dec_kwargs = {"encoder_hidden_size": encoder_hidden_size, "decoder_hidden_size": decoder_hidden_size, "T": T, "out_feats": n_targs} decoder = Decoder(**dec_kwargs).to(device) with open(os.path.join("data", "dec_kwargs.json"), "w") as f: json.dump(dec_kwargs, f, indent=4) encoder_optimizer = optim.Adam( params=[p for p in encoder.parameters() if p.requires_grad], lr=learning_rate) decoder_optimizer = optim.Adam( params=[p for p in decoder.parameters() if p.requires_grad], lr=learning_rate) da_rnn_net = DaRnnNet(encoder, decoder, encoder_optimizer, decoder_optimizer) return train_cfg, da_rnn_net
def instantiate_model(config, tokenizer): configure_devices(config) model = Model(config) optimizer = transformers.AdamW(model.parameters(), lr=config.learning_rate, weight_decay=0) metrics = None if config.continue_training: state_dict = torch.load(config.continue_training, map_location='cpu') model.load_state_dict(state_dict['model']) if 'optimizer_state_dict' in state_dict: optimizer.load_state_dict(state_dict['optimizer_state_dict']) for g in optimizer.param_groups: g['lr'] = config.learning_rate try: print(f"Loaded model:\nEpochs: {state_dict['epoch']}\nLoss: {state_dict['loss']}\n", f"Recall: {state_dict['rec']}\nMRR: {state_dict['mrr']}") except: pass if config.use_cuda: model = model.cuda() optimizer_to(optimizer, config.device) model = torch.nn.DataParallel(model, device_ids=config.devices) return model, optimizer, metrics
def __init__(self, state_dict): super().__init__() config = { "ndf": 128, "num_channels": 12, "n_rkhs": 1024, "n_depth": 3, "encoder_size": 128, "use_bn": 0, } dummy_batch = torch.zeros( (2, config['num_channels'], config['encoder_size'], config['encoder_size'])) self.encoder = Encoder(dummy_batch, **config) state_dict = {k: v for k, v in state_dict.items() if 'encoder.' in k} state_dict = { k.replace('encoder.', '').replace('module.', ''): v for k, v in state_dict.items() } self.encoder.load_state_dict(state_dict) self.transform = BENTransformValid()
def train(model_path=None): dataloader = DataLoader(Augmentation()) encoder = Encoder() dict_len = len(dataloader.data.dictionary) decoder = DecoderWithAttention(dict_len) if cuda: encoder = encoder.cuda() decoder = decoder.cuda() # if model_path: # text_generator.load_state_dict(torch.load(model_path)) train_iter = 1 encoder_optimizer = torch.optim.Adam(encoder.parameters(), lr=cfg.encoder_learning_rate) decoder_optimizer = torch.optim.Adam(decoder.parameters(), lr=cfg.decoder_learning_rate) val_bleu = list() losses = list() while True: batch_image, batch_label = dataloader.get_next_batch() batch_image = torch.from_numpy(batch_image).type(torch.FloatTensor) batch_label = torch.from_numpy(batch_label).type(torch.LongTensor) if cuda: batch_image = batch_image.cuda() batch_label = batch_label.cuda() # print(batch_image.size()) # print(batch_label.size()) print('Training') output = encoder(batch_image) # print('encoder output:', output.size()) predictions, alphas = decoder(output, batch_label) loss = cal_loss(predictions, batch_label, alphas, 1) decoder_optimizer.zero_grad() encoder_optimizer.zero_grad() loss.backward() decoder_optimizer.step() encoder_optimizer.step() print('Iter', train_iter, '| loss:', loss.cpu().data.numpy(), '| batch size:', cfg.batch_size, '| encoder learning rate:', cfg.encoder_learning_rate, '| decoder learning rate:', cfg.decoder_learning_rate) losses.append(loss.cpu().data.numpy()) if train_iter % cfg.save_model_iter == 0: val_bleu.append(val_eval(encoder, decoder, dataloader)) torch.save( encoder.state_dict(), './models/train/encoder_' + cfg.pre_train_model + '_' + str(train_iter) + '.pkl') torch.save(decoder.state_dict(), './models/train/decoder_' + str(train_iter) + '.pkl') np.save('./result/train_bleu4.npy', val_bleu) np.save('./result/losses.npy', losses) if train_iter == cfg.train_iter: break train_iter += 1
def build_model(self): hps = self.hps ns = self.hps.ns emb_size = self.hps.emb_size self.Encoder = Encoder(ns=ns, dp=hps.enc_dp) self.Decoder = Decoder(ns=ns, c_a=hps.n_speakers, emb_size=emb_size) self.Generator = Decoder(ns=ns, c_a=hps.n_speakers, emb_size=emb_size) self.LatentDiscriminator = LatentDiscriminator(ns=ns, dp=hps.dis_dp) self.PatchDiscriminator = PatchDiscriminator(ns=ns, n_class=hps.n_speakers) if torch.cuda.is_available(): self.Encoder.cuda() self.Decoder.cuda() self.Generator.cuda() self.LatentDiscriminator.cuda() self.PatchDiscriminator.cuda() betas = (0.5, 0.9) params = list(self.Encoder.parameters()) + list( self.Decoder.parameters()) self.ae_opt = optim.Adam(params, lr=self.hps.lr, betas=betas) self.gen_opt = optim.Adam(self.Generator.parameters(), lr=self.hps.lr, betas=betas) self.lat_opt = optim.Adam(self.LatentDiscriminator.parameters(), lr=self.hps.lr, betas=betas) self.patch_opt = optim.Adam(self.PatchDiscriminator.parameters(), lr=self.hps.lr, betas=betas)
class Model: def __init__(self, chpt_enc_path, chpt_dec_path, chpt_stat_path): historyLength = 10 encoder_dim = hiddenDimension lstm_input_dim = historyLength + 1 decoder_dim = hiddenDimension attention_dim = hiddenDimension output_dim = 1 self.decodeLength = 20 self.encoder = Encoder() self.decoder = DecoderWithAttention(encoder_dim, lstm_input_dim, decoder_dim, attention_dim, output_dim) self.encoder.load_state_dict(torch.load(chpt_enc_path)) self.decoder.load_state_dict(torch.load(chpt_dec_path)) self.encoder = self.encoder.to(device) self.decoder = self.decoder.to(device) self.encoder.eval() self.decoder.eval() with open(chpt_stat_path, 'rb') as f: chpt_stat = pickle.load(f) self.cMean = chpt_stat['cMean_tr'] self.cStd = chpt_stat['cStd_tr'] self.vMean = chpt_stat['vMean_tr'] self.vStd = chpt_stat['vStd_tr'] self.aMean = chpt_stat['aMean_tr'] self.aStd = chpt_stat['aStd_tr'] self.mean = torch.Tensor([self.vMean, self.aMean]).to(device) self.std = torch.Tensor([self.vStd, self.aStd]).to(device) def predict(self, curvatures, currentSpeed, histSpeeds, currentAccelX, histAccelXs): curvatures = torch.FloatTensor(curvatures).to(device) currentSpeed = torch.FloatTensor([currentSpeed]).to(device) histSpeeds = torch.FloatTensor(histSpeeds).to(device) currentAccelX = torch.FloatTensor([currentAccelX]).to(device) histAccelXs = torch.FloatTensor(histAccelXs).to(device) curvatures = (curvatures - self.cMean) / self.cStd currentSpeed = (currentSpeed - self.vMean) / self.vStd histSpeeds = (histSpeeds - self.vMean) / self.vStd currentAccelX = (currentAccelX - self.aMean) / self.aStd histAccelXs = (histAccelXs - self.aMean) / self.aStd curvatures = self.encoder(curvatures.unsqueeze(dim=0).unsqueeze(dim=0)) predictions, alphas, alphas_target = self.decoder(curvatures, currentSpeed, histSpeeds.unsqueeze(dim=0), currentAccelX, histAccelXs.unsqueeze(dim=0), self.decodeLength, self.vMean, self.vStd, self.aMean, self.aStd) return (predictions.squeeze()*self.aStd + self.aMean).cpu().detach().numpy(), alphas.squeeze().cpu().detach().numpy()
def main(): args = parse_arguments() n_vocab = params.n_vocab n_layer = params.n_layer n_hidden = params.n_hidden n_embed = params.n_embed n_batch = args.n_batch temperature = params.temperature train_path = params.train_path assert torch.cuda.is_available() print("loading_data...") # 训练时加载处理好的词典(如果有的话) if os.path.exists("vocab.json"): vocab = Vocabulary() with open('vocab.json', 'r') as fp: vocab.stoi = json.load(fp) for key, value in vocab.stoi.items(): vocab.itos.append(key) else: vocab = build_vocab(train_path, n_vocab) # save vocab with open('vocab.json', 'w') as fp: json.dump(vocab.stoi, fp) train_X, train_y, train_K = load_data(train_path, vocab) train_loader = get_data_loader(train_X, train_y, train_K, n_batch) print("successfully loaded") encoder = Encoder(n_vocab, n_embed, n_hidden, n_layer, vocab).cuda() Kencoder = KnowledgeEncoder(n_vocab, n_embed, n_hidden, n_layer, vocab).cuda() manager = Manager(n_hidden, n_vocab, temperature).cuda() decoder = Decoder(n_vocab, n_embed, n_hidden, n_layer, vocab).cuda() if args.restore: encoder = init_model(encoder, restore=params.encoder_restore) Kencoder = init_model(Kencoder, restore=params.Kencoder_restore) manager = init_model(manager, restore=params.manager_restore) decoder = init_model(decoder, restore=params.decoder_restore) # ToDo:目前的代码所有的embedding都是独立的,可以参考transformer源码使用直接赋值的方法共享参数: #if emb_src_trg_weight_sharing: # self.encoder.src_word_emb.weight = self.decoder.trg_word_emb.weight model = [encoder, Kencoder, manager, decoder] parameters = list(encoder.parameters()) + list(Kencoder.parameters()) + \ list(manager.parameters()) + list(decoder.parameters()) optimizer = optim.Adam(parameters, lr=args.lr) # pre_train knowledge manager print("start pre-training") pre_train(model, optimizer, train_loader, args) print("start training") train(model, optimizer, train_loader, args) # save final model save_models(model, params.all_restore)
def main(args): torch.multiprocessing.set_start_method('spawn') torch.distributed.init_process_group(backend="nccl") with open(args.config_path, 'r') as file: config = AttrDict(json.load(file)) set_seed(config.seed + torch.distributed.get_rank()) train_data_csv, test_data_csv = train_test_split( config.train_data_csv_path, config.n_test_experiments) train_image_ids, train_labels = get_data(train_data_csv, is_train=True) train_transform = TrainTransform(config.crop_size) train_dataset = CellsDataset(config.train_images_dir, train_image_ids, train_labels, train_transform) test_image_ids, test_labels = get_data(test_data_csv, is_train=True) test_dataset = CellsDataset(config.train_images_dir, test_image_ids, test_labels) if torch.distributed.get_rank() == 0: print( f'Train size: {len(train_dataset)}, test_size: {len(test_dataset)}' ) encoder = Encoder(config.n_image_channels, config.n_emedding_channels, config.n_classes, config.encoder_model, config.encoder_pretrained, config.encoder_dropout, config.encoder_scale) if config.restore_checkpoint_path is not None: state_dict = torch.load(config.restore_checkpoint_path, map_location='cpu') encoder.load_state_dict(state_dict, strict=False) decoder = Decoder(config.n_emedding_channels, config.n_image_channels, config.n_classes, config.decoder_n_channels) trainer = Trainer(encoder=encoder, decoder=decoder, optimizer_params={ 'lr': config.lr, 'weight_decay': config.weight_decay, 'warmap': config.warmap, 'amsgrad': config.amsgrad }, amp_params={ 'opt_level': config.opt_level, 'loss_scale': config.loss_scale }, rank=args.local_rank, n_jobs=config.n_jobs) trainer.train(train_data=train_dataset, n_epochs=config.n_epochs, batch_size=config.batch_size, test_data=test_dataset, best_checkpoint_path=config.best_checkpoint_path)
def build_model(self): hps = self.hps self.SpeakerClassifier = SpeakerClassifier(ns=hps.ns, dp=hps.dp, n_class=hps.n_speakers) self.Encoder = Encoder(ns=hps.ns) if torch.cuda.is_available(): self.SpeakerClassifier.cuda() self.Encoder.cuda() betas = (0.5, 0.9) self.opt = optim.Adam(self.SpeakerClassifier.parameters(), lr=self.hps.lr, betas=betas)
def train(): with open(vocab_path, 'rb') as f: vocab = pickle.load(f) vocab_size = len(vocab) print('vocab_size:', vocab_size) dataloader = get_loader(image_dir, caption_path, vocab, batch_size, crop_size, shuffle=True, num_workers=num_workers) encoder = Encoder(embedding_size).to(device) decoder = Decoder(vocab_size, embedding_size, lstm_size).to(device) if os.path.exists(encoder_path): encoder.load_state_dict(torch.load(encoder_path)) if os.path.exists(decoder_path): decoder.load_state_dict(torch.load(decoder_path)) loss_fn = torch.nn.CrossEntropyLoss() parameters = list(encoder.fc.parameters()) + list( encoder.bn.parameters()) + list(decoder.parameters()) optimizer = torch.optim.Adam(parameters, lr=learning_rate, betas=(0.9, 0.99)) num_steps = len(dataloader) for epoch in range(num_epochs): for index, (imgs, captions, lengths) in enumerate(dataloader): imgs = imgs.to(device) captions = captions.to(device) targets = pack_padded_sequence( captions, lengths, batch_first=True)[0] # the tailing [0] is necessary features = encoder(imgs) y_predicted = decoder(features, captions, lengths) loss = loss_fn(y_predicted, targets) optimizer.zero_grad() loss.backward() optimizer.step() if index % log_every == 0: print( 'Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}, Perplexity: {:5.4f}' .format(epoch, num_epochs, index, num_steps, loss.item(), np.exp(loss.item()))) if index % save_every == 0 and index != 0: print('Start saving encoder') torch.save(encoder.state_dict(), encoder_path) print('Start saving decoder') torch.save(decoder.state_dict(), decoder_path)
def __init__(self): super(KeplerModel, self).__init__() #Initialize Model Parameters Using Config Properties self.model = Encoder(config['seq_length'], config['hidden_size'], config['output_dim'], config['n_layers']) #Initialize a Cross Entropy Loss Criterion for Training self.criterion = torch.nn.CrossEntropyLoss()
def main(): infer_img_path = "C:/Users/Crypto/PycharmProjects/segmented_style_transfer/data" infer_img_name = 'two_boys.jpg' # Read and Process Raw data data = CaptioningData() # Finding image files as data data.set_all_images(cfg.images_path) captions_dict = data.get_captions(cfg.token_file) caption_maxlen = data.get_caption_maxlen() vocab = load_vocab(vocab_path=cfg.data_path, vocab_name=cfg.vocab_name) # print(vocab.word2idx) inception_encoding = Encoder() # Decoder model decoder = Decoder(vocab_size=len(vocab), embedding_size=300, input_shape=2048, caption_max_len=caption_maxlen) decoder_model = decoder.get_model() decoder_model.load_weights('model/best_weights.97-0.95.hdf5') img_ids = data.get_val_images(cfg.val_image_files) img_name = img_ids[19] enc_img = inception_encoding.encode_single_img(file_path=cfg.images_path, img_name=img_name) # enc_img = inception_encoding.encode_single_img(file_path=infer_img_path, img_name=infer_img_name) caption = ["<start>"] while True: par_caps = [vocab(i) for i in caption] par_caps = sequence.pad_sequences([par_caps], maxlen=40, padding='post') preds = decoder_model.predict( [np.array([enc_img]), np.array(par_caps)]) word_pred = vocab.idx2word[np.argmax(preds[0])] caption.append(word_pred) if word_pred == "<end>" or len(caption) > 40: break full_img_path = os.path.join(cfg.images_path, img_name) print(captions_dict[img_name]) print(full_img_path) print(' '.join(caption[1:-1])) for beam_size in [3, 5, 7]: caption = beam_search_predictions(vocab, enc_img, decoder_model, caption_maxlen, beam_index=beam_size) print(beam_size, caption)
def build_models(self): super(SolverNMsgMultipleDecodersDeepSteg, self).build_models() self.enc_c = Encoder(block_type=self.block_type, n_layers=self.config.enc_n_layers) self.dec_c = CarrierDecoder(conv_dim=self.dec_c_conv_dim, block_type=self.block_type, n_layers=self.config.dec_c_n_layers) self.dec_m = [MsgDecoder(conv_dim=self.dec_m_conv_dim, block_type=self.block_type) for _ in range(self.n_messages)]
class AMDIMEncoder(nn.Module): def __init__(self, state_dict): super().__init__() config = { "ndf": 128, "num_channels": 12, "n_rkhs": 1024, "n_depth": 3, "encoder_size": 128, "use_bn": 0, } dummy_batch = torch.zeros( (2, config['num_channels'], config['encoder_size'], config['encoder_size'])) self.encoder = Encoder(dummy_batch, **config) state_dict = {k: v for k, v in state_dict.items() if 'encoder.' in k} state_dict = { k.replace('encoder.', '').replace('module.', ''): v for k, v in state_dict.items() } self.encoder.load_state_dict(state_dict) self.transform = BENTransformValid() def forward(self, x): assert len(x.shape) == 4, "Input must be (batch_size, 12, 128, 128)" assert x.shape[1] == 12, "Input must be (batch_size, 12, 128, 128)" assert x.shape[2] == 128, "Input must be (batch_size, 12, 128, 128)" assert x.shape[3] == 128, "Input must be (batch_size, 12, 128, 128)" # -- # Preprocessing device = x.device x = x.cpu() tmp = [xx.numpy().transpose(1, 2, 0) for xx in x] tmp = [self.transform(xx) for xx in tmp] x = torch.stack(tmp) x = x.to(device) # -- # Forward acts = self.encoder._forward_acts(x) out = self.encoder.rkhs_block_1(acts[self.encoder.dim2layer[1]]) out = out[:, :, 0, 0] return out
class MyModel(TorchModelV2, nn.Module): def __init__(self, obs_space, action_space, num_outputs, model_config, name): TorchModelV2.__init__(self, obs_space, action_space, num_outputs, model_config, name) nn.Module.__init__(self) custom_config = model_config["custom_options"] latent_size = custom_config['latent_size'] self.main = Encoder(latent_size=latent_size) if custom_config['encoder_path'] is not None: # saved checkpoints could contain extra weights such as linear_logsigma weights = torch.load(custom_config['encoder_path'], map_location=torch.device('cpu')) for k in list(weights.keys()): if k not in self.main.state_dict().keys(): del weights[k] self.main.load_state_dict(weights) print("Loaded Weights") else: print("No Load Weights") self.critic = nn.Sequential(nn.Linear(latent_size, 400), nn.ReLU(), nn.Linear(400, 300), nn.ReLU(), nn.Linear(300, 1)) self.actor = nn.Sequential(nn.Linear(latent_size, 400), nn.ReLU(), nn.Linear(400, 300), nn.ReLU()) self.alpha_head = nn.Sequential(nn.Linear(300, 3), nn.Softplus()) self.beta_head = nn.Sequential(nn.Linear(300, 3), nn.Softplus()) self._cur_value = None self.train_encoder = custom_config['train_encoder'] print("Train Encoder: ", self.train_encoder) @override(TorchModelV2) def forward(self, input_dict, state, seq_lens): features = self.main(input_dict['obs'].float()) if not self.train_encoder: features = features.detach() # not train the encoder actor_features = self.actor(features) alpha = self.alpha_head(actor_features) + 1 beta = self.beta_head(actor_features) + 1 logits = torch.cat([alpha, beta], dim=1) self._cur_value = self.critic(features).squeeze(1) return logits, state @override(TorchModelV2) def value_function(self): assert self._cur_value is not None, 'Must call forward() first' return self._cur_value
def encode_dataset(cfg): out_dir = Path(utils.to_absolute_path(cfg.out_dir)) out_dir.mkdir(exist_ok=True, parents=True) root_path = Path(utils.to_absolute_path("datasets")) / cfg.dataset.path with open(root_path / "test.json") as file: metadata = json.load(file) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") encoder = Encoder(**cfg.model.encoder) encoder.to(device) print("Load checkpoint from: {}:".format(cfg.checkpoint)) checkpoint_path = utils.to_absolute_path(cfg.checkpoint) checkpoint = torch.load(checkpoint_path, map_location=lambda storage, loc: storage) encoder.load_state_dict(checkpoint["encoder"]) encoder.eval() if cfg.save_auxiliary: auxiliary = [] def hook(module, input, output): auxiliary.append(output.clone()) encoder.encoder[-1].register_forward_hook(hook) for _, _, _, path in tqdm(metadata): path = root_path.parent / path mel = torch.from_numpy(np.load( path.with_suffix(".mel.npy"))).unsqueeze(0).to(device) with torch.no_grad(): z, c, indices = encoder.encode(mel) z = z.squeeze().cpu().numpy() out_path = out_dir / path.stem with open(out_path.with_suffix(".txt"), "w") as file: np.savetxt(file, z, fmt="%.16f") if cfg.save_auxiliary: aux_path = out_dir.parent / "auxiliary_embedding1" aux_path.mkdir(exist_ok=True, parents=True) out_path = aux_path / path.stem c = c.squeeze().cpu().numpy() with open(out_path.with_suffix(".txt"), "w") as file: np.savetxt(file, c, fmt="%.16f") aux_path = out_dir.parent / "auxiliary_embedding2" aux_path.mkdir(exist_ok=True, parents=True) out_path = aux_path / path.stem aux = auxiliary.pop().squeeze().cpu().numpy() with open(out_path.with_suffix(".txt"), "w") as file: np.savetxt(file, aux, fmt="%.16f")
def __init__(self): parser = argparse.ArgumentParser(description='Image Captioning') parser.add_argument('--root', default='../../../cocodataset/', type=str) parser.add_argument('--crop_size', default=224, type=int) parser.add_argument('--epochs', default=100, type=int) parser.add_argument('--lr', default=1e-4, type=float) parser.add_argument('--batch_size', default=128, help='') parser.add_argument('--num_workers', default=4, type=int) parser.add_argument('--embed_dim', default=256, type=int) parser.add_argument('--hidden_size', default=512, type=int) parser.add_argument('--num_layers', default=1, type=int) parser.add_argument('--model_path', default='./model/', type=str) parser.add_argument('--vocab_path', default='./vocab/', type=str) parser.add_argument('--save_step', default=1000, type=int) self.args = parser.parse_args() self.Multi_GPU = False # if torch.cuda.device_count() > 1: # print('Multi GPU Activate!') # print('Using GPU :', int(torch.cuda.device_count())) # self.Multi_GPU = True os.makedirs(self.args.model_path, exist_ok=True) transform = transforms.Compose([ transforms.RandomCrop(self.args.crop_size), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)) ]) with open(self.args.vocab_path + 'vocab.pickle', 'rb') as f: data = pickle.load(f) self.vocab = data self.DataLoader = get_dataloader(root=self.args.root, transform=transform, shuffle=True, batch_size=self.args.batch_size, num_workers=self.args.num_workers, vocab=self.vocab) self.Encoder = Encoder(embed_dim=self.args.embed_dim) self.Decoder = Decoder(embed_dim=self.args.embed_dim, hidden_size=self.args.hidden_size, vocab_size=len(self.vocab), num_layers=self.args.num_layers)
def convert(cfg): dataset_path = Path(utils.to_absolute_path("datasets")) / cfg.dataset.path with open(dataset_path / "speakers.json") as file: speakers = sorted(json.load(file)) synthesis_list_path = Path(utils.to_absolute_path(cfg.synthesis_list)) with open(synthesis_list_path) as file: synthesis_list = json.load(file) in_dir = Path(utils.to_absolute_path(cfg.in_dir)) out_dir = Path(utils.to_absolute_path(cfg.out_dir)) out_dir.mkdir(exist_ok=True, parents=True) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") encoder = Encoder(**cfg.model.encoder) decoder = Decoder(**cfg.model.decoder) encoder.to(device) decoder.to(device) print("Load checkpoint from: {}:".format(cfg.checkpoint)) checkpoint_path = utils.to_absolute_path(cfg.checkpoint) checkpoint = torch.load(checkpoint_path, map_location=lambda storage, loc: storage) encoder.load_state_dict(checkpoint["encoder"]) decoder.load_state_dict(checkpoint["decoder"]) encoder.eval() decoder.eval() for wav_path, speaker_id, out_filename in tqdm(synthesis_list): wav_path = in_dir / wav_path wav, _ = librosa.load( wav_path.with_suffix(".wav"), sr=cfg.preprocessing.sr) wav = wav / np.abs(wav).max() * 0.999 mel = librosa.feature.melspectrogram( preemphasis(wav, cfg.preprocessing.preemph), sr=cfg.preprocessing.sr, n_fft=cfg.preprocessing.n_fft, n_mels=cfg.preprocessing.n_mels, hop_length=cfg.preprocessing.hop_length, win_length=cfg.preprocessing.win_length, fmin=cfg.preprocessing.fmin, power=1) logmel = librosa.amplitude_to_db(mel, top_db=cfg.preprocessing.top_db) logmel = logmel / cfg.preprocessing.top_db + 1 mel = torch.FloatTensor(logmel).unsqueeze(0).to(device) speaker = torch.LongTensor([speakers.index(speaker_id)]).to(device) with torch.no_grad(): z, _ = encoder.encode(mel) output = decoder.generate(z, speaker) path = out_dir / out_filename librosa.output.write_wav(path.with_suffix(".wav"), output.astype(np.float32), sr=cfg.preprocessing.sr)
def __init__(self, state_size, action_size, config): """Initialize an Agent object. Params ====== state_size (int): dimension of each state action_size (int): dimension of each action seed (int): random seed """ self.state_size = state_size self.seed = config["seed"] torch.manual_seed(self.seed) np.random.seed(seed=self.seed) random.seed(self.seed) env = gym.make(config["env_name"]) self.env = FrameStack(env, config) self.env.action_space.seed(self.seed) self.action_size = action_size self.seed = int(config["seed"]) self.lr = config['lr'] self.batch_size = config['batch_size'] self.device = config['device'] self.gamma = config['gamma'] self.tau = config['tau'] self.train_freq = config['train_freq'] self.total_frames = int(config['total_frames']) self.start_timesteps = int(config['start_timesteps']) self.eval = config["eval"] obs_shape = (config["history_length"], config["size"], config["size"]) self.replay_buffer = ReplayBuffer(obs_shape, (1, ), int(config["buffer_size"]), self.seed, config["image_pad"], config['device']) self.qnetwork_local = QNetwork(state_size, action_size, self.seed).to(self.device) self.qnetwork_target = QNetwork(state_size, action_size, self.seed).to(self.device) self.optimizer = optim.Adam(self.qnetwork_local.parameters(), lr=self.lr) self.encoder = Encoder(config).to(self.device) self.encoder_optimizer = torch.optim.Adam(self.encoder.parameters(), self.lr) self.t_step = 0 self.entropy = 0.03 self.alpha_m = 0.9 self.clip_log = -1 self.eps_decay = config["eps_decay"] self.eps_end = config["eps_min"] self.all_actions = [] now = datetime.now() self.vid_path = "vid" dt_string = now.strftime("%d_%m_%Y_%H:%M:%S") pathname = dt_string + "seed_" + str(config['seed']) tensorboard_name = 'runs/' + pathname self.writer = SummaryWriter(tensorboard_name) for a in range(self.action_size): action = torch.Tensor([1 for i in range(self.batch_size)]).type(torch.long) * 0 + a self.all_actions.append(action.to(self.device))
def __init__(self, static_size, dynamic_size, hidden_size): super(StateCritic, self).__init__() self.static_encoder = Encoder(static_size, hidden_size) self.dynamic_encoder = Encoder(dynamic_size, hidden_size) # Define the encoder & decoder models self.fc1 = nn.Conv1d(hidden_size * 2, 20, kernel_size=1) self.fc2 = nn.Conv1d(20, 20, kernel_size=1) self.fc3 = nn.Conv1d(20, 1, kernel_size=1) for p in self.parameters(): if len(p.shape) > 1: nn.init.xavier_uniform_(p)