Ejemplo n.º 1
0
 def batch_compute(self, preds, labels):
     # logging.info(f'preds: {len(preds)}  {preds[0].shape}  {preds[1].shape}')
     # logging.info(f'labels: {labels}')
     # logging.info(f'labels: {len(labels)}  {labels[0].shape}  {labels[1].shape}')
     losses = [loss(preds[idx], labels[idx]) for idx, loss in enumerate(self.losses)]
     if len(self.metrics) > 0:
         for idx, metric in enumerate(self.metrics):
             metric.update(labels[idx], preds[idx])
     return losses
Ejemplo n.º 2
0
 def batch_compute(self, preds, labels):
     logging.info(
         f'preds: {len(preds)}  {preds[0].shape}  {preds[0].dtype}  {preds[1].shape}  {preds[1].dtype}'
     )
     logging.info(
         f'labels: {len(labels)}  {labels[0].shape}  {labels[0].dtype}  {np.max(labels[0])}  {labels[1].shape}  {labels[1].dtype}'
     )
     losses = [
         loss(preds[idx], labels[idx] / 128)
         for idx, loss in enumerate(self.losses)
     ]
     if len(self.metrics) > 0:
         for idx, metric in enumerate(self.metrics):
             metric.update(labels[idx], preds[idx])
     return losses
     for idx, loss_func in enumerate(self.losses):
         loss = loss_func(preds[idx], labels[idx] / 128.0)
         self.metrics[idx].update()
Ejemplo n.º 3
0
    def forward(self, datas, labels=None, loss_funcs=None, metrics=None):
        print(f'[forward]  datas: {type(datas)}  {type(labels)}')
        gpu_datas = mx.gluon.utils.split_and_load(datas, self.ctxs)
        gpu_labels = mx.gluon.utils.split_and_load(labels, self.ctxs)
        losses = None
        # losses = [self.sec_loss(self.net(x), y) for x, y in zip(gpu_datas, gpu_labels)]
        for x, y in zip(gpu_datas, gpu_labels):
            # logging.info(f'[train] x: {x.shape}  y: {y.shape}  {x.context}')
            preds = self.net(x)

            if loss_funcs is None:
                continue
            losses = [loss(preds, y) for loss in loss_funcs]

            if metrics is None:
                continue
            if len(metrics) > 0:
                for metric in metrics:
                    metric.update(y, preds)
        return losses
Ejemplo n.º 4
0
def train(network, training_dataloader, batch_size, epochs):
    """
    Should take an initialized network and train that network using data from the data loader.
    
    :param network: initialized gluon network to be trained
    :type network: gluon.Block
    
    :param training_dataloader: the training DataLoader provides batches for data for every iteration
    :type training_dataloader: gluon.data.DataLoader
    
    :param batch_size: batch size for the DataLoader.
    :type batch_size: int
    
    :param epochs: number of epochs to train the DataLoader
    :type epochs: int
    
    :return: tuple of trained network and the final training accuracy
    :rtype: (gluon.Block, float)
    """
    trainer = gluon.Trainer(network.collect_params(), 'adam',
                            {'learning_rate': 0.002})
    metric = mx.metric.Accuracy()

    for epoch in range(epochs):
        train_loss = 0.
        for data, label in training_dataloader:

            #             print (data.shape)
            #             print (label.shape)
            with autograd.record():
                output = network(data)
                loss = mx.ndarray.softmax_cross_entropy(output, label)
            loss.backward()

            trainer.step(batch_size)
            train_loss += loss.mean().asscalar()
            metric.update(label, output)

        print(epoch, metric.get()[1])
        training_accuracy = metric.get()[1]
    return network, training_accuracy
Ejemplo n.º 5
0
 def update_metrics(self, preds, labels):
     if len(self.metrics) > 0:
         for idx, metric in enumerate(self.metrics):
             metric.update(labels[idx], preds[idx])