Exemplo n.º 1
0
def train():
    with fluid.dygraph.guard():
        epoch_num = train_parameters["num_epochs"]
        net = DenseNet("densenet", layers=121, dropout_prob=train_parameters['dropout_prob'],
                       class_dim=train_parameters['class_dim'])
        optimizer = optimizer_rms_setting(net.parameters())
        file_list = os.path.join(train_parameters['data_dir'], "train.txt")
        train_reader = paddle.batch(reader.custom_image_reader(file_list, train_parameters['data_dir'], 'train'),
                                    batch_size=train_parameters['train_batch_size'],
                                    drop_last=True)
        test_reader = paddle.batch(reader.custom_image_reader(file_list, train_parameters['data_dir'], 'val'),
                                   batch_size=train_parameters['train_batch_size'],
                                   drop_last=True)
        if train_parameters["continue_train"]:
            model, _ = fluid.dygraph.load_dygraph(train_parameters["save_persistable_dir"])
            net.load_dict(model)

        best_acc = 0
        for epoch_num in range(epoch_num):

            for batch_id, data in enumerate(train_reader()):
                dy_x_data = np.array([x[0] for x in data]).astype('float32')
                y_data = np.array([x[1] for x in data]).astype('int')
                y_data = y_data[:, np.newaxis]

                img = fluid.dygraph.to_variable(dy_x_data)
                label = fluid.dygraph.to_variable(y_data)
                label.stop_gradient = True
                t1 = time.time()
                out, acc = net(img, label)
                t2 = time.time()
                forward_time = t2 - t1
                loss = fluid.layers.cross_entropy(out, label)
                avg_loss = fluid.layers.mean(loss)
                # dy_out = avg_loss.numpy()
                t3 = time.time()
                avg_loss.backward()
                t4 = time.time()
                backward_time = t4 - t3
                optimizer.minimize(avg_loss)
                net.clear_gradients()
                # print(forward_time, backward_time)

                dy_param_value = {}
                for param in net.parameters():
                    dy_param_value[param.name] = param.numpy

                if batch_id % 40 == 0:
                    logger.info("Loss at epoch {} step {}: {}, acc: {}".format(epoch_num, batch_id, avg_loss.numpy(),
                                                                               acc.numpy()))

            net.eval()
            epoch_acc = eval_net(test_reader, net)
            net.train()
            if epoch_acc > best_acc:
                fluid.dygraph.save_dygraph(net.state_dict(), train_parameters["save_persistable_dir"])
                fluid.dygraph.save_dygraph(optimizer.state_dict(), train_parameters["save_persistable_dir"])
                best_acc = epoch_acc
                logger.info("model saved at epoch {}, best accuracy is {}".format(epoch_num, best_acc))
        logger.info("Final loss: {}".format(avg_loss.numpy()))
Exemplo n.º 2
0
    val_losses = np.zeros(epochs)
    val_accs = np.zeros(epochs)
    for epoch in range(epochs):  # loop over the dataset multiple times
        start = time.time()
        losses[epoch], accs[epoch] = __one_epoch(train_loader, 'train')
        val_losses[epoch], val_accs[epoch] = __one_epoch(val_loader, 'val')

        end = time.time()
        print(
            '[{}/{}] - Loss: {:.4f} Acc: {:.4f} Val Loss: {:.4f} Val Acc: {:.4f} Time: {:.4f} min'
            .format(epoch, epochs, losses[epoch], accs[epoch],
                    val_losses[epoch], val_accs[epoch], (end - start) / 60))
        if epoch > 10 and losses[epoch] < best_loss:
            best_loss = val_losses[epoch]
            print('Saving model at epoch {}'.format(epoch))
            torch.save(net.state_dict(), 'model.ckpt')

    print('Finished Training')
    torch.save(net.state_dict(), 'model.ckpt')

if test:
    print('Starting inference...')
    correct = 0.0
    net.load_state_dict(torch.load('model.ckpt'))
    start = time.time()
    net.eval()
    for i, (image, target) in enumerate(val_loader):
        # forward + backward + optimize
        outputs = net(image) > 0.5
        # print statistics
        correct += ((outputs[:, 1] == target.byte()).sum().item() /