def test_all(self): self.model.eval() torch.set_grad_enabled(False) criterion_hm = detect_loss.BCELoss2d() # criterion_hm = detect_loss.BCEWithLogitsLoss2d(pos_weight=torch.tensor(2.0).to(self.device)) criterion_of = detect_loss.MSELoss2d() test_loss_all, test_acc_all, base_acc_all = [], [], [] for i in range(self.test_dataloader.num_batch): sample = self.test_dataloader.get_next_batch(i) im, dp, fl, hm, of = self.to_tensor(sample) pred_hm, pred_of = self.model(im, dp, fl) loss = self.get_loss(pred_hm, pred_of, hm, of, criterion_hm, criterion_of) acc, base_acc = self.get_accuracy(pred_hm, hm) test_loss_all.append(loss) test_acc_all.append(acc) base_acc_all.append(base_acc) logging.info('batch %d, test loss: %.4f', i, loss) logging.info('batch %d, test accuracy: %.4f', i, acc) logging.info('batch %d, base accuracy: %.4f', i, base_acc) test_loss = np.mean(np.array(test_loss_all)) test_acc = np.mean(np.array(test_acc_all)) base_acc = np.mean(np.array(base_acc_all)) logging.info('overall average test loss: %.4f', test_loss) logging.info('overall average test accuracy: %.4f', test_acc) logging.info('overall average base accuracy: %.4f', base_acc)
def predict(self, image, depth, flow, label_map, offset): self.model.eval() torch.set_grad_enabled(False) im = [torch.from_numpy(i_s).float().to(self.device) for i_s in image] dp = [torch.from_numpy(d_s).float().to(self.device) for d_s in depth] fl = [torch.from_numpy(f_s).float().to(self.device) for f_s in flow] lb = [ torch.from_numpy(l_s).float().to(self.device) for l_s in label_map ] of = [torch.from_numpy(o_s).float().to(self.device) for o_s in offset] pred, pred_of = self.model(im, dp, fl) criterion_conf = detect_loss.BCELoss2d() criterion_loc = detect_loss.MSELoss2d() loss = 0 for i in range(len(lb)): loss = loss + criterion_conf(pred[i], lb[i]) loss = loss * 10 for i in range(len(lb)): mask = lb[i] > 0 mask = mask.float() loss = loss + criterion_loc(pred_of[i], of[i], mask) for i in range(len(pred)): pred[i] = torch.sigmoid(pred[i]) pred[i] = pred[i].cpu().numpy() pred_of[i] = pred_of[i].cpu().numpy() return pred, pred_of, loss
def train(self): self.model.train() torch.set_grad_enabled(True) writer = SummaryWriter(self.tensorboard_path) criterion_hm = detect_loss.BCELoss2d() # criterion_hm = detect_loss.BCEWithLogitsLoss2d(pos_weight=torch.tensor(2.0).to(self.device)) criterion_of = detect_loss.MSELoss2d() optimizer = optim.Adam(self.model.parameters(), lr=self.learning_rate) train_loss_all, train_acc_all, base_acc_all = [], [], [] for epoch in range(self.train_epoch): self.train_dataloader.shuffle() for i in range(self.train_dataloader.num_batch): sample = self.train_dataloader.get_next_batch(i) im, dp, fl, hm, of = self.to_tensor(sample) optimizer.zero_grad() pred_hm, pred_of = self.model(im, dp, fl) loss = self.get_loss(pred_hm, pred_of, hm, of, criterion_hm, criterion_of) loss.backward() optimizer.step() acc, base_acc = self.get_accuracy(pred_hm, hm) it = epoch * self.train_dataloader.num_batch + i writer.add_scalar('train_loss', loss, it) train_loss_all.append(loss) if len(train_loss_all) > 100: train_loss_all.pop(0) ave_train_loss = sum(train_loss_all) / float(len(train_loss_all)) logging.info('epoch %d, batch %d, train loss: %.4f, average train loss: %.4f', epoch, i, loss, ave_train_loss) writer.add_scalar('train_acc', acc, it) train_acc_all.append(acc) if len(train_acc_all) > 100: train_acc_all.pop(0) ave_train_acc = sum(train_acc_all) / float(len(train_acc_all)) logging.info('epoch %d, batch %d, train acc: %.4f, average train acc: %.4f', epoch, i, acc, ave_train_acc) base_acc_all.append(base_acc) if len(base_acc_all) > 100: base_acc_all.pop(0) ave_base_acc = sum(base_acc_all) / float(len(base_acc_all)) logging.info('epoch %d, batch %d, base acc: %.4f, average base acc: %.4f', epoch, i, base_acc, ave_base_acc) if (it + 1) % self.save_interval == 0: logging.info('epoch %d, batch %d, saving model', epoch, it) with open(self.save_model_path, 'w') as handle: torch.save(self.model.state_dict(), handle) if (it + 1) % self.test_interval == 0: logging.info('epoch %d, batch %d, testing', epoch, it) test_loss, test_acc = self.validate() writer.add_scalar('test_loss', test_loss, it) writer.add_scalar('test_acc', test_acc, it) self.model.train() torch.set_grad_enabled(True) with open(self.save_model_path, 'w') as handle: torch.save(self.model.state_dict(), handle) logging.info('finish epoch %d', epoch) logging.info('model save to %s', self.save_model_path) writer.close()
def predict(self, im, dp, fl, hm, of): self.model.eval() torch.set_grad_enabled(False) pred_hm, pred_of = self.model(im, dp, fl) criterion_hm = detect_loss.BCELoss2d() # criterion_hm = detect_loss.BCEWithLogitsLoss2d(pos_weight=torch.tensor(2.0).to(self.device)) criterion_of = detect_loss.MSELoss2d() loss = self.get_loss(pred_hm, pred_of, hm, of, criterion_hm, criterion_of) acc, _ = self.get_accuracy(pred_hm, hm) for i in range(len(pred_hm)): pred_hm[i] = torch.sigmoid(pred_hm[i]) pred_hm[i] = pred_hm[i].cpu().numpy().transpose((0, 2, 3, 1)) pred_of[i] = pred_of[i].cpu().numpy().transpose((0, 2, 3, 1)) return pred_hm, pred_of, loss, acc
def test(self): self.model.eval() torch.set_grad_enabled(False) criterion_conf = detect_loss.BCELoss2d() criterion_loc = detect_loss.MSELoss2d() test_loss_all, test_acc_all, base_acc_all = [], [], [] index, cnt = np.random.permutation(len(self.data.test_anno['img'])), 0 for it in range(self.test_iter): im, orig_im, dp, orig_dp, fl, orig_fl, box, lb, of, cnt, _ = \ self.data.get_next_batch('test', cnt, index) im = [torch.from_numpy(i_s).float().to(self.device) for i_s in im] dp = [torch.from_numpy(d_s).float().to(self.device) for d_s in dp] fl = [torch.from_numpy(f_s).float().to(self.device) for f_s in fl] lb = [torch.from_numpy(l_s).float().to(self.device) for l_s in lb] of = [torch.from_numpy(o_s).float().to(self.device) for o_s in of] pred, pred_of = self.model(im, dp, fl) loss = 0 for i in range(len(lb)): loss = loss + criterion_conf(pred[i], lb[i]) loss = loss * 10 for i in range(len(lb)): mask = lb[i] > 0 mask = mask.float() loss = loss + criterion_loc(pred_of[i], of[i], mask) acc, base_acc = self.get_accuracy(pred, lb) test_loss_all.append(loss) if len(test_loss_all) > 100: test_loss_all.pop(0) test_acc_all.append(acc) if len(test_acc_all) > 100: test_acc_all.pop(0) base_acc_all.append(base_acc) if len(base_acc_all) > 100: base_acc_all.pop(0) test_loss = np.mean(np.array(test_loss_all)) test_acc = np.mean(np.array(test_acc_all)) base_acc = np.mean(np.array(base_acc_all)) logging.info('average test loss: %.4f', test_loss) logging.info('average test accuracy: %.4f', test_acc) logging.info('average base accuracy: %.4f', base_acc) return test_loss, test_acc
def test_all(self): self.model.eval() torch.set_grad_enabled(False) criterion_conf = detect_loss.BCELoss2d() criterion_loc = detect_loss.MSELoss2d() test_loss_all, test_acc_all, base_acc_all = [], [], [] cnt = 0 while True: im, orig_im, dp, orig_dp, fl, orig_fl, box, lb, of, cnt, restart = \ self.data.get_next_batch('test', cnt) if restart: break im = [torch.from_numpy(i_s).float().to(self.device) for i_s in im] dp = [torch.from_numpy(d_s).float().to(self.device) for d_s in dp] fl = [torch.from_numpy(f_s).float().to(self.device) for f_s in fl] lb = [torch.from_numpy(l_s).float().to(self.device) for l_s in lb] of = [torch.from_numpy(o_s).float().to(self.device) for o_s in of] pred, pred_of = self.model(im, dp, fl) loss = 0 for i in range(len(lb)): loss = loss + criterion_conf(pred[i], lb[i]) loss = loss * 10 for i in range(len(lb)): mask = lb[i] > 0 mask = mask.float() loss = loss + criterion_loc(pred_of[i], of[i], mask) acc, base_acc = self.get_accuracy(pred, lb) test_loss_all.append(loss) test_acc_all.append(acc) base_acc_all.append(base_acc) logging.info('at image %d, test loss: %.4f', cnt, loss) logging.info('at image %d, test accuracy: %.4f', cnt, acc) logging.info('at image %d, base accuracy: %.4f', cnt, base_acc) test_loss = np.mean(np.array(test_loss_all)) test_acc = np.mean(np.array(test_acc_all)) base_acc = np.mean(np.array(base_acc_all)) logging.info('overall average test loss: %.4f', test_loss) logging.info('overall average test accuracy: %.4f', test_acc) logging.info('overall average base accuracy: %.4f', base_acc)
def train(self): self.model.train() torch.set_grad_enabled(True) writer = SummaryWriter(self.tensorboard_path) criterion_conf = detect_loss.BCELoss2d() criterion_loc = detect_loss.MSELoss2d() optimizer = optim.Adam(self.model.parameters(), lr=self.learning_rate) train_loss_all, train_acc_all, base_acc_all = [], [], [] index, cnt = np.random.permutation(len(self.data.train_anno['img'])), 0 for it in range(self.train_iter): im, orig_im, dp, orig_dp, fl, orig_fl, box, lb, of, cnt, restart = \ self.data.get_next_batch('train', cnt, index) im = [torch.from_numpy(i_s).float().to(self.device) for i_s in im] dp = [torch.from_numpy(d_s).float().to(self.device) for d_s in dp] fl = [torch.from_numpy(f_s).float().to(self.device) for f_s in fl] lb = [torch.from_numpy(l_s).float().to(self.device) for l_s in lb] of = [torch.from_numpy(o_s).float().to(self.device) for o_s in of] optimizer.zero_grad() pred, pred_of = self.model(im, dp, fl) loss = 0 for i in range(len(lb)): loss = loss + criterion_conf(pred[i], lb[i]) loss = loss * 10 for i in range(len(lb)): mask = lb[i] > 0 mask = mask.float() loss = loss + criterion_loc(pred_of[i], of[i], mask) loss.backward() optimizer.step() acc, base_acc = self.get_accuracy(pred, lb) writer.add_scalar('train_loss', loss, it) train_loss_all.append(loss) if len(train_loss_all) > 100: train_loss_all.pop(0) ave_train_loss = sum(train_loss_all) / float(len(train_loss_all)) logging.info( 'iteration %d, train loss: %.4f, average train loss: %.4f', it, loss, ave_train_loss) writer.add_scalar('train_acc', acc, it) train_acc_all.append(acc) if len(train_acc_all) > 100: train_acc_all.pop(0) ave_train_acc = sum(train_acc_all) / float(len(train_acc_all)) logging.info( 'iteration %d, train accuracy: %.4f, average train accuracy: %.4f', it, acc, ave_train_acc) base_acc_all.append(base_acc) if len(base_acc_all) > 100: base_acc_all.pop(0) ave_base_acc = sum(base_acc_all) / float(len(base_acc_all)) logging.info( 'iteration %d, base accuracy: %.4f, average base accuracy: %.4f', it, base_acc, ave_base_acc) if (it + 1) % self.save_interval == 0: logging.info('iteration %d, saving model', it) with open(self.save_model_path, 'w') as handle: torch.save(self.model.state_dict(), handle) if (it + 1) % self.test_interval == 0: logging.info('iteration %d, testing', it) test_loss, test_acc = self.validate() writer.add_scalar('test_loss', test_loss, it) writer.add_scalar('test_acc', test_acc, it) self.model.train() torch.set_grad_enabled(True) if restart: index, cnt = np.random.permutation( len(self.data.train_anno['img'])), 0 writer.close()