def train(self):
    trn_losses_ = []
    val_losses_ = []
    val_acc_ = []
    trn_acc_ = []
    for epoch in range(self.args.start_epoch, (self.args.start_epoch + self.args.epochs)):
        print(('=' * 20))
        self.logger.info('Starting epoch: {0} '.format(epoch))
        dataset = self.training_queue.get()
        data_loader = DataLoader(dataset=dataset, batch_size=self.args.minibatch_size, shuffle=True, pin_memory=self.use_cuda)
        train_embedding_features_buffer = []
        train_images_buffer = []
        train_labels = []
        correct = 0
        for _ in range(0, 1):
            losses = []
            for minibatch in data_loader:
                if self.use_cuda:
                    anchor_frames = minibatch[0].cuda()
                    anchor_quats = minibatch[1].cuda()
                (loss, a_pred) = self.loss_fn(self.model, anchor_frames, anchor_quats)
                losses.append(loss.data.cpu().numpy())
                correct += (torch.norm((a_pred - anchor_quats), 2) < 1).data.cpu().numpy().sum()
                self.optimizer.zero_grad()
                loss.backward()
                self.optimizer.step()
                train_labels.append(anchor_quats)
                train_embedding_features_buffer.append(anchor_quats)
                train_images_buffer.append(anchor_frames)
        print('logging to {}'.format(self.logdir))
        self.writer.add_scalar('data/train_loss', np.mean(losses), self.itr)
        self.writer.add_scalar('data/train_correct', (correct / len(data_loader)), self.itr)
        self.itr += 1
        trn_losses_.append(np.mean(losses))
        self.logger.info('train loss: ', np.mean(losses))
        self.logger.info('Training score correct  {correct}/{total}'.format(correct=correct, total=len(data_loader)))
        trn_acc_.append(correct)
        self.writer.add_image('frame_1', minibatch[0][0], self.itr)
        self.writer.add_image('frame_2', minibatch[0][1], self.itr)
        self.writer.add_image('frame_3', minibatch[0][2], self.itr)
        self.writer.add_image('frame_4', minibatch[0][3], self.itr)
        features = torch.cat(train_embedding_features_buffer[:30]).squeeze_()
        labels = torch.cat(train_labels[:30]).squeeze_()
        images = torch.cat(train_images_buffer[:30]).squeeze_()
        self.writer.add_embedding(features, metadata=labels, label_img=images, global_step=epoch)
        if ((epoch % 1) == 0):
            (loss, correct) = self.validate()
            self.learning_rate_scheduler.step(loss)
            val_losses_.append(loss)
            val_acc_.append(correct)
        if (((epoch % self.args.save_every) == 0) and (epoch != 0)):
            self.logger.info('Saving model.')
            self.save_model(self.model, self.model_filename(self.args.model_name, epoch), join(self.model_folder, 'weight_files'))
            print('logging to {}'.format(self.logdir))
        plot_mean(trn_losses_, self.model_folder, 'train_loss')
        plot_mean(val_losses_, self.model_folder, 'validation_loss')
        plot_mean(trn_acc_, self.model_folder, 'train_acc')
        plot_mean(val_acc_, self.model_folder, 'validation_accuracy')
def train(self):
    trn_losses_ = []
    val_losses_ = []
    val_acc_ = []
    trn_acc_ = []
    for epoch in range(self.args.start_epoch, (self.args.start_epoch + self.args.epochs)):
        print(('=' * 20))
        self.logger.info('Starting epoch: {0} '.format(epoch))
        dataset = self.training_queue.get()
        data_loader = DataLoader(dataset=dataset, batch_size=self.args.minibatch_size, shuffle=True, pin_memory=self.use_cuda)
        train_embedding_features_buffer = []
        train_images_buffer = []
        train_labels = []
        correct = 0
        for _ in range(0, 1):
            losses = []
            for minibatch in data_loader:
                if self.use_cuda:
                    anchor_frames = minibatch[0].cuda()
                    anchor_quats = minibatch[1].cuda()
                (loss, a_pred) = self.loss_fn(self.model, anchor_frames, anchor_quats)
                losses.append(loss.data.cpu().numpy())
                correct += (torch.norm((a_pred - anchor_quats), 2) < 1).data.cpu().numpy().sum()
                self.optimizer.zero_grad()
                loss.backward()
                self.optimizer.step()
                train_labels.append(anchor_quats)
                train_embedding_features_buffer.append(anchor_quats)
                train_images_buffer.append(anchor_frames)
        print('logging to {}'.format(self.logdir))
        self.writer.add_scalar('data/train_loss', np.mean(losses), self.itr)
        self.writer.add_scalar('data/train_correct', (correct / len(data_loader)), self.itr)
        self.itr += 1
        trn_losses_.append(np.mean(losses))
        self.logger.info('train loss: ', np.mean(losses))
        self.logger.info('Training score correct  {correct}/{total}'.format(correct=correct, total=len(data_loader)))
        trn_acc_.append(correct)
        self.writer.add_image('frame_1', minibatch[0][0], self.itr)
        self.writer.add_image('frame_2', minibatch[0][1], self.itr)
        self.writer.add_image('frame_3', minibatch[0][2], self.itr)
        self.writer.add_image('frame_4', minibatch[0][3], self.itr)
        features = torch.cat(train_embedding_features_buffer[:30]).squeeze_()
        labels = torch.cat(train_labels[:30]).squeeze_()
        images = torch.cat(train_images_buffer[:30]).squeeze_()
        self.writer.add_embedding(features, metadata=labels, label_img=images, global_step=epoch)
        if ((epoch % 1) == 0):
            (loss, correct) = self.validate()
            self.learning_rate_scheduler.step(loss)
            val_losses_.append(loss)
            val_acc_.append(correct)
        if (((epoch % self.args.save_every) == 0) and (epoch != 0)):
            self.logger.info('Saving model.')
            self.save_model(self.model, self.model_filename(self.args.model_name, epoch), join(self.model_folder, 'weight_files'))
            print('logging to {}'.format(self.logdir))
        plot_mean(trn_losses_, self.model_folder, 'train_loss')
        plot_mean(val_losses_, self.model_folder, 'validation_loss')
        plot_mean(trn_acc_, self.model_folder, 'train_acc')
        plot_mean(val_acc_, self.model_folder, 'validation_accuracy')