Python config.device() Examples
The following are 30
code examples of config.device().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
config
, or try the search function
.
Example #1
Source File: memory.py From Reinforcement-Learning-Pytorch-Cartpole with MIT License | 6 votes |
def sample(self, batch_size, net, target_net, beta): probability_sum = sum(self.memory_probabiliy) p = [probability / probability_sum for probability in self.memory_probabiliy] indexes = np.random.choice(np.arange(len(self.memory)), batch_size, p=p) transitions = [self.memory[idx] for idx in indexes] transitions_p = [p[idx] for idx in indexes] batch = Transition(*zip(*transitions)) weights = [pow(self.capacity * p_j, -beta) for p_j in transitions_p] weights = torch.Tensor(weights).to(device) weights = weights / weights.max() td_error = QNet.get_loss(net, target_net, batch.state, batch.next_state, batch.action, batch.reward, batch.mask) td_error = td_error.detach() td_error_idx = 0 for idx in indexes: self.memory_probabiliy[idx] = pow(abs(td_error[td_error_idx]) + small_epsilon, alpha).item() td_error_idx += 1 return batch, weights
Example #2
Source File: instructor.py From TextGAN-PyTorch with MIT License | 6 votes |
def init_model(self): if cfg.oracle_pretrain: if not os.path.exists(cfg.oracle_state_dict_path): create_oracle() self.oracle.load_state_dict(torch.load(cfg.oracle_state_dict_path)) if cfg.dis_pretrain: self.log.info( 'Load pretrained discriminator: {}'.format(cfg.pretrained_dis_path)) self.dis.load_state_dict(torch.load(cfg.pretrained_dis_path)) if cfg.gen_pretrain: self.log.info('Load MLE pretrained generator gen: {}'.format(cfg.pretrained_gen_path)) self.gen.load_state_dict(torch.load(cfg.pretrained_gen_path, map_location='cuda:{}'.format(cfg.device))) if cfg.CUDA: self.oracle = self.oracle.cuda() self.gen = self.gen.cuda() self.dis = self.dis.cuda()
Example #3
Source File: sentigan_instructor.py From TextGAN-PyTorch with MIT License | 6 votes |
def init_model(self): if cfg.dis_pretrain: self.log.info( 'Load pretrained discriminator: {}'.format(cfg.pretrained_dis_path)) self.dis.load_state_dict(torch.load(cfg.pretrained_dis_path)) if cfg.gen_pretrain: for i in range(cfg.k_label): self.log.info('Load MLE pretrained generator gen: {}'.format(cfg.pretrained_gen_path + '%d' % i)) self.gen_list[i].load_state_dict(torch.load(cfg.pretrained_gen_path + '%d' % i)) if cfg.clas_pretrain: self.log.info('Load pretrained classifier: {}'.format(cfg.pretrained_clas_path)) self.clas.load_state_dict(torch.load(cfg.pretrained_clas_path, map_location='cuda:%d' % cfg.device)) if cfg.CUDA: for i in range(cfg.k_label): self.gen_list[i] = self.gen_list[i].cuda() self.dis = self.dis.cuda() self.clas = self.clas.cuda()
Example #4
Source File: memory.py From Reinforcement-Learning-Pytorch-Cartpole with MIT License | 6 votes |
def sample(self, batch_size, net, target_net, beta): probability_sum = sum(self.memory_probabiliy) p = [probability / probability_sum for probability in self.memory_probabiliy] indexes = np.random.choice(np.arange(len(self.memory)), batch_size, p=p) transitions = [self.memory[idx] for idx in indexes] transitions_p = [p[idx] for idx in indexes] batch = Transition(*zip(*transitions)) weights = [pow(self.capacity * p_j, -beta) for p_j in transitions_p] weights = torch.Tensor(weights).to(device) weights = weights / weights.max() td_error = QNet.get_td_error(net, target_net, batch.state, batch.next_state, batch.action, batch.reward, batch.mask) td_error = td_error.detach() td_error_idx = 0 for idx in indexes: self.memory_probabiliy[idx] = pow(abs(td_error[td_error_idx]) + small_epsilon, alpha).item() # print(pow(abs(td_error[td_error_idx]) + small_epsilon, alpha).item()) td_error_idx += 1 return batch, weights
Example #5
Source File: memory.py From Reinforcement-Learning-Pytorch-Cartpole with MIT License | 6 votes |
def sample(self, batch_size, net, target_net, beta): probability_sum = sum(self.memory_probabiliy) p = [probability / probability_sum for probability in self.memory_probabiliy] # print(len(self.memory_probabiliy)) indexes = np.random.choice(np.arange(len(self.memory)), batch_size, p=p) transitions = [self.memory[idx] for idx in indexes] transitions_p = [p[idx] for idx in indexes] batch = Transition(*zip(*transitions)) weights = [pow(self.capacity * p_j, -beta) for p_j in transitions_p] weights = torch.Tensor(weights).to(device) # print(weights) weights = weights / weights.max() # print(weights) td_error = QNet.get_td_error(net, target_net, batch.state, batch.next_state, batch.action, batch.reward, batch.mask) td_error_idx = 0 for idx in indexes: self.memory_probabiliy[idx] = pow(abs(td_error[td_error_idx]) + small_epsilon, alpha).item() # print(pow(abs(td_error[td_error_idx]) + small_epsilon, alpha).item()) td_error_idx += 1 return batch, weights
Example #6
Source File: models.py From CCF-BDCI2019-Multi-person-Face-Recognition-Competition-Baseline with MIT License | 6 votes |
def forward(self, input, label): x = F.normalize(input) W = F.normalize(self.weight) cosine = F.linear(x, W) sine = torch.sqrt(1.0 - torch.pow(cosine, 2)) phi = cosine * self.cos_m - sine * self.sin_m # cos(theta + m) if self.easy_margin: phi = torch.where(cosine > 0, phi, cosine) else: phi = torch.where(cosine > self.th, phi, cosine - self.mm) one_hot = torch.zeros(cosine.size(), device=device) one_hot.scatter_(1, label.view(-1, 1).long(), 1) output = (one_hot * phi) + ((1.0 - one_hot) * cosine) output *= self.s return output
Example #7
Source File: train.py From Speech-Transformer with MIT License | 6 votes |
def valid(valid_loader, model, logger): model.eval() losses = AverageMeter() # Batches for data in tqdm(valid_loader): # Move to GPU, if available padded_input, padded_target, input_lengths = data padded_input = padded_input.to(device) padded_target = padded_target.to(device) input_lengths = input_lengths.to(device) with torch.no_grad(): # Forward prop. pred, gold = model(padded_input, input_lengths, padded_target) loss, n_correct = cal_performance(pred, gold, smoothing=args.label_smoothing) # Keep track of metrics losses.update(loss.item()) # Print status logger.info('\nValidation Loss {loss.val:.5f} ({loss.avg:.5f})\n'.format(loss=losses)) return losses.avg
Example #8
Source File: train.py From Performance-RNN-PyTorch with MIT License | 6 votes |
def load_session(): global sess_path, model_config, device, learning_rate, reset_optimizer try: sess = torch.load(sess_path) if 'model_config' in sess and sess['model_config'] != model_config: model_config = sess['model_config'] print('Use session config instead:') print(utils.dict2params(model_config)) model_state = sess['model_state'] optimizer_state = sess['model_optimizer_state'] print('Session is loaded from', sess_path) sess_loaded = True except: print('New session') sess_loaded = False model = PerformanceRNN(**model_config).to(device) optimizer = optim.Adam(model.parameters(), lr=learning_rate) if sess_loaded: model.load_state_dict(model_state) if not reset_optimizer: optimizer.load_state_dict(optimizer_state) return model, optimizer
Example #9
Source File: model.py From Performance-RNN-PyTorch with MIT License | 6 votes |
def forward(self, event, control=None, hidden=None): # One step forward assert len(event.shape) == 2 assert event.shape[0] == 1 batch_size = event.shape[1] event = self.event_embedding(event) if control is None: default = torch.ones(1, batch_size, 1).to(device) control = torch.zeros(1, batch_size, self.control_dim).to(device) else: default = torch.zeros(1, batch_size, 1).to(device) assert control.shape == (1, batch_size, self.control_dim) concat = torch.cat([event, default, control], -1) input = self.concat_input_fc(concat) input = self.concat_input_fc_activation(input) _, hidden = self.gru(input, hidden) output = hidden.permute(1, 0, 2).contiguous() output = output.view(batch_size, -1).unsqueeze(0) output = self.output_fc(output) return output, hidden
Example #10
Source File: trainer.py From neural-question-generation with MIT License | 5 votes |
def step(self, train_data): src_seq, ext_src_seq, trg_seq, ext_trg_seq, tag_seq, _ = train_data enc_mask = torch.sign(src_seq) src_len = torch.sum(enc_mask, 1) if config.use_gpu: src_seq = src_seq.to(config.device) ext_src_seq = ext_src_seq.to(config.device) src_len = src_len.to(config.device) trg_seq = trg_seq.to(config.device) ext_trg_seq = ext_trg_seq.to(config.device) enc_mask = enc_mask.to(config.device) tag_seq = tag_seq.to(config.device) eos_trg = trg_seq[:, 1:] if config.use_pointer: eos_trg = ext_trg_seq[:, 1:] logits = self.model(src_seq, tag_seq, ext_src_seq, trg_seq) batch_size, nsteps, _ = logits.size() preds = logits.view(batch_size * nsteps, -1) targets = eos_trg.contiguous().view(-1) loss = self.criterion(preds, targets) return loss
Example #11
Source File: train.py From Speech-Transformer with MIT License | 5 votes |
def train(train_loader, model, optimizer, epoch, logger): model.train() # train mode (dropout and batchnorm is used) losses = AverageMeter() # Batches for i, (data) in enumerate(train_loader): # Move to GPU, if available padded_input, padded_target, input_lengths = data padded_input = padded_input.to(device) padded_target = padded_target.to(device) input_lengths = input_lengths.to(device) # Forward prop. pred, gold = model(padded_input, input_lengths, padded_target) loss, n_correct = cal_performance(pred, gold, smoothing=args.label_smoothing) # Back prop. optimizer.zero_grad() loss.backward() # Update weights optimizer.step() # Keep track of metrics losses.update(loss.item()) # Print status if i % print_freq == 0: logger.info('Epoch: [{0}][{1}/{2}]\t' 'Loss {loss.val:.5f} ({loss.avg:.5f})'.format(epoch, i, len(train_loader), loss=losses)) return losses.avg
Example #12
Source File: model.py From neural-question-generation with MIT License | 5 votes |
def decode(self, y, ext_x, prev_states, prev_context, encoder_features, encoder_mask): # forward one step lstm # y : [b] embedded = self.embedding(y.unsqueeze(1)) lstm_inputs = self.reduce_layer(torch.cat([embedded, prev_context], 2)) output, states = self.lstm(lstm_inputs, prev_states) context, energy = self.attention(output, encoder_features, encoder_mask) concat_input = torch.cat((output, context), 2).squeeze(1) logit_input = torch.tanh(self.concat_layer(concat_input)) logit = self.logit_layer(logit_input) # [b, |V|] if config.use_pointer: batch_size = y.size(0) num_oov = max(torch.max(ext_x - self.vocab_size + 1), 0) zeros = torch.zeros((batch_size, num_oov), device=config.device) extended_logit = torch.cat([logit, zeros], dim=1) out = torch.zeros_like(extended_logit) - INF out, _ = scatter_max(energy, ext_x, out=out) out = out.masked_fill(out == -INF, 0) logit = extended_logit + out logit = logit.masked_fill(logit == -INF, 0) # forcing UNK prob 0 logit[:, UNK_ID] = -INF return logit, states, context
Example #13
Source File: model.py From Performance-RNN-PyTorch with MIT License | 5 votes |
def get_primary_event(self, batch_size): return torch.LongTensor([[self.primary_event] * batch_size]).to(device)
Example #14
Source File: infenrence.py From neural-question-generation with MIT License | 5 votes |
def __init__(self, model_path, output_dir): with open(config.word2idx_file, "rb") as f: word2idx = pickle.load(f) self.output_dir = output_dir self.test_data = open(config.test_trg_file, "r").readlines() self.data_loader = get_loader(config.test_src_file, config.test_trg_file, word2idx, batch_size=1, use_tag=True, shuffle=False) self.tok2idx = word2idx self.idx2tok = {idx: tok for tok, idx in self.tok2idx.items()} self.model = Seq2seq() state_dict = torch.load(model_path) self.model.load_state_dict(state_dict) self.model.eval() self.moddel = self.model.to(config.device) self.pred_dir = os.path.join(output_dir, "/generated.txt") self.golden_dir = os.path.join(output_dir, "/golden.txt") self.src_file = os.path.join(output_dir, "src.txt") if not os.path.exists(output_dir): os.makedirs(output_dir) # dummy file for evaluation with open(self.src_file, "w") as f: for i in range(len(self.data_loader)): f.write(i+"\n")
Example #15
Source File: main.py From QANet-pytorch- with MIT License | 5 votes |
def train(model, optimizer, scheduler, dataset, dev_dataset, dev_eval_file, start, ema): model.train() losses = [] print(f'Training epoch {start}') for i, (Cwid, Ccid, Qwid, Qcid, y1, y2, ids) in enumerate(dataset): optimizer.zero_grad() Cwid, Ccid, Qwid, Qcid = Cwid.to(device), Ccid.to(device), Qwid.to(device), Qcid.to(device) p1, p2 = model(Cwid, Ccid, Qwid, Qcid) y1, y2 = y1.to(device), y2.to(device) p1 = F.log_softmax(p1, dim=1) p2 = F.log_softmax(p2, dim=1) loss1 = F.nll_loss(p1, y1) loss2 = F.nll_loss(p2, y2) loss = (loss1 + loss2) writer.add_scalar('data/loss', loss.item(), i+start*len(dataset)) losses.append(loss.item()) loss.backward() torch.nn.utils.clip_grad_value_(model.parameters(), config.grad_clip) optimizer.step() ema(model, i+start*len(dataset)) scheduler.step() if (i+1) % config.checkpoint == 0 and (i+1) < config.checkpoint*(len(dataset)//config.checkpoint): ema.assign(model) metrics = test(model, dev_dataset, dev_eval_file, i+start*len(dataset)) ema.resume(model) model.train() for param_group in optimizer.param_groups: #print("Learning:", param_group['lr']) writer.add_scalar('data/lr', param_group['lr'], i+start*len(dataset)) print("\rSTEP {:8d}/{} loss {:8f}".format(i + 1, len(dataset), loss.item()), end='') loss_avg = np.mean(losses) print("STEP {:8d} Avg_loss {:8f}\n".format(start, loss_avg))
Example #16
Source File: train.py From Reinforcement-Learning-Pytorch-Cartpole with MIT License | 5 votes |
def main(): env = gym.make(env_name) env.seed(500) torch.manual_seed(500) num_inputs = env.observation_space.shape[0] num_actions = env.action_space.n print('state size:', num_inputs) print('action size:', num_actions) online_net = QNet(num_inputs, num_actions) target_net = QNet(num_inputs, num_actions) target_net.load_state_dict(online_net.state_dict()) online_net.share_memory() target_net.share_memory() optimizer = SharedAdam(online_net.parameters(), lr=lr) global_ep, global_ep_r, res_queue = mp.Value('i', 0), mp.Value('d', 0.), mp.Queue() writer = SummaryWriter('logs') online_net.to(device) target_net.to(device) online_net.train() target_net.train() workers = [Worker(online_net, target_net, optimizer, global_ep, global_ep_r, res_queue, i) for i in range(mp.cpu_count())] [w.start() for w in workers] res = [] while True: r = res_queue.get() if r is not None: res.append(r) [ep, ep_r, loss] = r writer.add_scalar('log/score', float(ep_r), ep) writer.add_scalar('log/loss', float(loss), ep) else: break [w.join() for w in workers]
Example #17
Source File: lfw_eval.py From InsightFace-v2 with Apache License 2.0 | 5 votes |
def get_image(samples, transformer, file): filtered = [sample for sample in samples if file in sample['full_path'].replace('\\', '/')] assert (len(filtered) == 1), 'len(filtered): {} file:{}'.format(len(filtered), file) sample = filtered[0] full_path = sample['full_path'] landmarks = sample['landmarks'] img = align_face(full_path, landmarks) # BGR # img = blur_and_grayscale(img) img = img[..., ::-1] # RGB img = Image.fromarray(img, 'RGB') # RGB img = transformer(img) img = img.to(device) return img
Example #18
Source File: megaface.py From InsightFace-v2 with Apache License 2.0 | 5 votes |
def get_image(img, transformer): img = img[..., ::-1] # RGB img = Image.fromarray(img, 'RGB') # RGB img = transformer(img) return img.to(device)
Example #19
Source File: megaface.py From InsightFace-v2 with Apache License 2.0 | 5 votes |
def gen_feature(path): print('gen features {}...'.format(path)) # Preprocess the total files count files = [] for filepath in walkdir(path, '.jpg'): files.append(filepath) file_count = len(files) batch_size = 128 with torch.no_grad(): for start_idx in tqdm(range(0, file_count, batch_size)): end_idx = min(file_count, start_idx + batch_size) length = end_idx - start_idx imgs = torch.zeros([length, 3, 112, 112], dtype=torch.float) for idx in range(0, length): i = start_idx + idx filepath = files[i] imgs[idx] = get_image(cv.imread(filepath, True), transformer) features = model(imgs.to(device)).cpu().numpy() for idx in range(0, length): i = start_idx + idx filepath = files[i] tarfile = filepath + '_0.bin' feature = features[idx] write_feature(tarfile, feature / np.linalg.norm(feature))
Example #20
Source File: models.py From InsightFace-v2 with Apache License 2.0 | 5 votes |
def forward(self, input, label): x = F.normalize(input) W = F.normalize(self.weight) cosine = F.linear(x, W) sine = torch.sqrt(1.0 - torch.pow(cosine, 2)) phi = cosine * self.cos_m - sine * self.sin_m # cos(theta + m) if self.easy_margin: phi = torch.where(cosine > 0, phi, cosine) else: phi = torch.where(cosine > self.th, phi, cosine - self.mm) one_hot = torch.zeros(cosine.size(), device=device) one_hot.scatter_(1, label.view(-1, 1).long(), 1) output = (one_hot * phi) + ((1.0 - one_hot) * cosine) output *= self.s return output
Example #21
Source File: jsdgan_instructor.py From TextGAN-PyTorch with MIT License | 5 votes |
def init_model(self): if cfg.oracle_pretrain: if not os.path.exists(cfg.oracle_state_dict_path): create_oracle() self.oracle.load_state_dict(torch.load(cfg.oracle_state_dict_path)) if cfg.gen_pretrain: self.log.info('Load MLE pretrained generator gen: {}'.format(cfg.pretrained_gen_path)) self.gen.load_state_dict(torch.load(cfg.pretrained_gen_path, map_location='cuda:{}'.format(cfg.device))) if cfg.CUDA: self.oracle = self.oracle.cuda() self.gen = self.gen.cuda()
Example #22
Source File: train.py From Deep-Image-Matting-PyTorch with MIT License | 5 votes |
def valid(valid_loader, model, logger): model.eval() # eval mode (dropout and batchnorm is NOT used) losses = AverageMeter() # Batches for img, alpha_label in valid_loader: # Move to GPU, if available img = img.type(torch.FloatTensor).to(device) # [N, 3, 320, 320] alpha_label = alpha_label.type(torch.FloatTensor).to(device) # [N, 320, 320] alpha_label = alpha_label.reshape((-1, 2, im_size * im_size)) # [N, 320*320] # Forward prop. alpha_out = model(img) # [N, 320, 320] alpha_out = alpha_out.reshape((-1, 1, im_size * im_size)) # [N, 320*320] # Calculate loss # loss = criterion(alpha_out, alpha_label) loss = alpha_prediction_loss(alpha_out, alpha_label) # Keep track of metrics losses.update(loss.item()) # Print status status = 'Validation: Loss {loss.avg:.4f}\n'.format(loss=losses) logger.info(status) return losses.avg
Example #23
Source File: train.py From Deep-Image-Matting-PyTorch with MIT License | 5 votes |
def train(train_loader, model, optimizer, epoch, logger): model.train() # train mode (dropout and batchnorm is used) losses = AverageMeter() # Batches for i, (img, alpha_label) in enumerate(train_loader): # Move to GPU, if available img = img.type(torch.FloatTensor).to(device) # [N, 4, 320, 320] alpha_label = alpha_label.type(torch.FloatTensor).to(device) # [N, 320, 320] alpha_label = alpha_label.reshape((-1, 2, im_size * im_size)) # [N, 320*320] # Forward prop. alpha_out = model(img) # [N, 3, 320, 320] alpha_out = alpha_out.reshape((-1, 1, im_size * im_size)) # [N, 320*320] # Calculate loss # loss = criterion(alpha_out, alpha_label) loss = alpha_prediction_loss(alpha_out, alpha_label) # Back prop. optimizer.zero_grad() loss.backward() # Clip gradients clip_gradient(optimizer, grad_clip) # Update weights optimizer.step() # Keep track of metrics losses.update(loss.item()) # Print status if i % print_freq == 0: status = 'Epoch: [{0}][{1}/{2}]\t' \ 'Loss {loss.val:.4f} ({loss.avg:.4f})\t'.format(epoch, i, len(train_loader), loss=losses) logger.info(status) return losses.avg
Example #24
Source File: jsdgan_instructor.py From TextGAN-PyTorch with MIT License | 5 votes |
def init_model(self): if cfg.gen_pretrain: self.log.info('Load MLE pretrained generator gen: {}'.format(cfg.pretrained_gen_path)) self.gen.load_state_dict(torch.load(cfg.pretrained_gen_path, map_location='cuda:{}'.format(cfg.device))) if cfg.CUDA: self.gen = self.gen.cuda()
Example #25
Source File: train.py From Mobile-Image-Matting with MIT License | 5 votes |
def valid(valid_loader, model, logger): model.eval() # eval mode (dropout and batchnorm is NOT used) losses = AverageMeter() # Batches for img, alpha_label in tqdm(valid_loader): # Move to GPU, if available img = img.type(torch.FloatTensor).to(device) # [N, 4, 320, 320] alpha_label = alpha_label.type(torch.FloatTensor).to(device) # [N, 2, 320, 320] alpha_label = alpha_label.reshape((-1, 2, im_size * im_size)) # [N, 2, 320*320] # Forward prop. alpha_out = model(img) # [N, 320, 320] alpha_out = alpha_out.reshape((-1, 1, im_size * im_size)) # [N, 320*320] # Calculate loss # loss = criterion(alpha_out, alpha_label) loss = alpha_prediction_loss(alpha_out, alpha_label) # Keep track of metrics losses.update(loss.item()) # Print status status = 'Validation: Loss {loss.avg:.4f}\n'.format(loss=losses) logger.info(status) return losses.avg
Example #26
Source File: megaface_utils.py From InsightFace-PyTorch with Apache License 2.0 | 5 votes |
def get_image(transformer, filepath, flip=False): img = Image.open(filepath) if flip: img = ImageOps.flip(img) img = transformer(img) return img.to(device)
Example #27
Source File: train.py From Mobile-Image-Matting with MIT License | 5 votes |
def train(train_loader, model, optimizer, epoch, logger): model.train() # train mode (dropout and batchnorm is used) losses = AverageMeter() # Batches for i, (img, alpha_label) in enumerate(train_loader): # Move to GPU, if available img = img.type(torch.FloatTensor).to(device) # [N, 4, 320, 320] alpha_label = alpha_label.type(torch.FloatTensor).to(device) # [N, 2, 320, 320] alpha_label = alpha_label.reshape((-1, 2, im_size * im_size)) # [N, 2, 320*320] # Forward prop. alpha_out = model(img) # [N, 320, 320] alpha_out = alpha_out.reshape((-1, 1, im_size * im_size)) # [N, 320*320] # Calculate loss # loss = criterion(alpha_out, alpha_label) loss = alpha_prediction_loss(alpha_out, alpha_label) # Back prop. optimizer.zero_grad() loss.backward() # Clip gradients clip_gradient(optimizer, grad_clip) # Update weights optimizer.step() # Keep track of metrics losses.update(loss.item()) # Print status if i % print_freq == 0: status = 'Epoch: [{0}][{1}/{2}]\t' \ 'Loss {loss.val:.4f} ({loss.avg:.4f})\t'.format(epoch, i, len(train_loader), loss=losses) logger.info(status) return losses.avg
Example #28
Source File: models.py From InsightFace-PyTorch with Apache License 2.0 | 5 votes |
def forward(self, input, label): x = F.normalize(input) W = F.normalize(self.weight) cosine = F.linear(x, W) sine = torch.sqrt(1.0 - torch.pow(cosine, 2)) phi = cosine * self.cos_m - sine * self.sin_m # cos(theta + m) if self.easy_margin: phi = torch.where(cosine > 0, phi, cosine) else: phi = torch.where(cosine > self.th, phi, cosine - self.mm) one_hot = torch.zeros(cosine.size(), device=device) one_hot.scatter_(1, label.view(-1, 1).long(), 1) output = (one_hot * phi) + ((1.0 - one_hot) * cosine) output *= self.s return output
Example #29
Source File: megaface_utils.py From CCF-BDCI2019-Multi-person-Face-Recognition-Competition-Baseline with MIT License | 5 votes |
def get_image(filepath, transformer, flip=False): img = Image.open(filepath).convert('RGB') if flip: img = img.transpose(Image.FLIP_LEFT_RIGHT) img = transformer(img) return img.to(device)
Example #30
Source File: main.py From QANet-pytorch- with MIT License | 4 votes |
def train_entry(config): from models import QANet with open(config.word_emb_file, "rb") as fh: word_mat = np.array(json.load(fh), dtype=np.float32) with open(config.char_emb_file, "rb") as fh: char_mat = np.array(json.load(fh), dtype=np.float32) with open(config.dev_eval_file, "r") as fh: dev_eval_file = json.load(fh) print("Building model...") train_dataset = get_loader(config.train_record_file, config.batch_size) dev_dataset = get_loader(config.dev_record_file, config.batch_size) lr = config.learning_rate base_lr = 1 lr_warm_up_num = config.lr_warm_up_num model = QANet(word_mat, char_mat).to(device) ema = EMA(config.decay) for name, param in model.named_parameters(): if param.requires_grad: ema.register(name, param.data) parameters = filter(lambda param: param.requires_grad, model.parameters()) optimizer = optim.Adam(lr=base_lr, betas=(0.9, 0.999), eps=1e-7, weight_decay=5e-8, params=parameters) cr = lr / math.log2(lr_warm_up_num) scheduler = optim.lr_scheduler.LambdaLR( optimizer, lr_lambda=lambda ee: cr * math.log2(ee + 1) if ee < lr_warm_up_num else lr) best_f1 = 0 best_em = 0 patience = 0 unused = False for iter in range(config.num_epoch): train(model, optimizer, scheduler, train_dataset, dev_dataset, dev_eval_file, iter, ema) ema.assign(model) metrics = test(model, dev_dataset, dev_eval_file, (iter+1)*len(train_dataset)) dev_f1 = metrics["f1"] dev_em = metrics["exact_match"] if dev_f1 < best_f1 and dev_em < best_em: patience += 1 if patience > config.early_stop: break else: patience = 0 best_f1 = max(best_f1, dev_f1) best_em = max(best_em, dev_em) fn = os.path.join(config.save_dir, "model.pt") torch.save(model, fn) ema.resume(model)