Exemplo n.º 1
0
def run():
    statistical_losses = StatisticalValue()
    statistical_mae_errors = StatisticalValue()
    sub_dir = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime())
    print('Test start.')

    with torch.no_grad():

        for idx, (images, labels, coordinates,
                  names) in enumerate(trainloader):
            images, coordinates = images.to(DEVICE), coordinates.to(DEVICE)

            _, locs = model(images)

            loss = criterion([locs], [coordinates]) / ITERATION_SIZE

            # mae_error = mae(input=outputs, target=labels)
            # statistical_mae_errors.update(mae_error.item(), names)
            statistical_losses.update(loss.item())

            iteration_writer.add_scalar(
                'Loss/Test',
                statistical_losses.val[0],
                idx + 1,
            )

            if idx % 1 == 0:
                print(
                    'mae Error: mean: {errors.avg}, mid: {errors.mid}, worst: {errors.max[0]}, best: {errors.min[0]}'
                    .format(errors=statistical_losses))

        return statistical_losses
Exemplo n.º 2
0
def run(epoch):
    statistical_losses = StatisticalValue()
    statistical_mae_errors = StatisticalValue()
    statistical_mae_errors1 = StatisticalValue()
    statistical_mae_errors2 = StatisticalValue()

    optimizer.zero_grad()

    for idx, (images, labels, names) in enumerate(trainloader):
        break
        images, labels = images.to(DEVICE), labels.to(DEVICE)

        outputs, _1, _2, crops = model(images)
        outputs_list = [outputs]

        loss = saliency_loss(outputs_list, labels)
        loss.backward()

        mae_error = mae(input=outputs, target=labels)
        statistical_mae_errors.update(mae_error.item(), names)
        statistical_mae_errors1.update(
            mae(input=_1, target=labels).item(), names)
        statistical_mae_errors2.update(
            mae(input=_2, target=labels).item(), names)

        statistical_losses.update(loss.item())

        iteration_writer.add_scalar('Loss/Iteration',
                                    statistical_losses.val[0],
                                    (epoch - 1) * len(trainloader) + idx + 1)

        if (idx + 1) % ITERATION_SIZE == 0:
            optimizer.step()
            optimizer.zero_grad()

            print_training_status(epoch, idx + 1, len(trainloader),
                                  statistical_losses.val[0],
                                  statistical_losses.avg)
            print(
                'Final MAE Error: mean: {errors.avg}, worst: {errors.max[0]}, best: {errors.min[0]}'
                .format(errors=statistical_mae_errors))
            print(
                '1 MAE Error: mean: {errors.avg}, worst: {errors.max[0]}, best: {errors.min[0]}'
                .format(errors=statistical_mae_errors1))
            print(
                '2 MAE Error: mean: {errors.avg}, worst: {errors.max[0]}, best: {errors.min[0]}'
                .format(errors=statistical_mae_errors2))

    # scheduler.step()
    test()
    return statistical_losses
Exemplo n.º 3
0
def run():
    statistical_losses = StatisticalValue()
    statistical_mae_errors = StatisticalValue()
    sub_dir = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime())
    print('Test start.')

    with torch.no_grad():

        for idx, (images, labels, names) in enumerate(trainloader):
            images, labels = images.to(DEVICE), labels.to(DEVICE)

            outputs = model(images)

            loss = criterion(outputs, labels) / ITERATION_SIZE

            mae_error = mae(input=outputs, target=labels)
            statistical_mae_errors.update(mae_error.item(), names)
            statistical_losses.update(loss.item())

            iteration_writer.add_scalar(
                'Loss/Test',
                statistical_losses.val[0],
                idx + 1,
            )

            if idx % 1 == 0:
                view_data = torch.zeros((3, *images.shape[1:]))
                view_data[0, :, :, :] = images[0]
                view_data[1, :, :, :] = outputs[0]
                view_data[2, :, :, :] = labels[0]

                if not os.path.isdir(os.path.join(TMP_ROOT, 'test', sub_dir)):
                    os.makedirs(os.path.join(TMP_ROOT, 'test', sub_dir))

                torchvision.utils.save_image(
                    view_data,
                    os.path.join(TMP_ROOT, 'test/%s/%s' % (sub_dir, names[0]))
                )

            print(
                'mae Error: mean: {errors.avg}, mid: {errors.mid}, worst: {errors.max[0]}, best: {errors.min[0]}'.format(
                    errors=statistical_mae_errors))

        return statistical_losses
Exemplo n.º 4
0
def run(epoch):
    statistical_losses = StatisticalValue()
    statistical_angular_errors = StatisticalValue()

    optimizer.zero_grad()

    for idx, (images, labels, names) in enumerate(trainloader):
        images, labels = images.to(DEVICE), labels.to(DEVICE)

        predictions = model(images)

        loss = torch.zeros(1).to(DEVICE)

        for p in predictions:
            loss += criterion(p, labels) / ITERATION_SIZE

        loss.backward()

        angular_error = multi_angular_loss(predictions[-1], labels)
        statistical_angular_errors.update(angular_error.item(), names)
        statistical_losses.update(loss.item())

        iteration_writer.add_scalar(
            'Loss/Iteration',
            statistical_losses.val[0],
            (epoch - 1) * len(trainloader) + idx + 1
        )

        if (idx + 1) % ITERATION_SIZE == 0:
            optimizer.step()
            optimizer.zero_grad()

            print_training_status(epoch, idx + 1, len(trainloader),
                                  statistical_losses.val[0], statistical_losses.avg)
            print(
                'Angular Error: mean: {errors.avg}, worst: {errors.max[0]}, best: {errors.min[0]}'.format(
                    errors=statistical_angular_errors))

    scheduler.step()

    return statistical_losses
Exemplo n.º 5
0
def run(epoch):
    statistical_losses = StatisticalValue()
    statistical_mae_errors = StatisticalValue()

    optimizer.zero_grad()

    for idx, (images, labels, names) in enumerate(trainloader):
        images, labels = images.to(DEVICE), labels.to(DEVICE)

        pred_masks, ca_act_regs = model(images)

        loss = (criterion(pred_masks, labels) + ca_act_regs) / ITERATION_SIZE

        loss.backward()

        mae_error = mae(input=pred_masks, target=labels)
        statistical_mae_errors.update(mae_error.item(), names)
        statistical_losses.update(loss.item())

        iteration_writer.add_scalar('Loss/Iteration',
                                    statistical_losses.val[0],
                                    (epoch - 1) * len(trainloader) + idx + 1)

        if (idx + 1) % ITERATION_SIZE == 0:
            optimizer.step()
            optimizer.zero_grad()

            print_training_status(epoch, idx + 1, len(trainloader),
                                  statistical_losses.val[0],
                                  statistical_losses.avg)
            print(
                'MAE Error: mean: {errors.avg}, worst: {errors.max[0]}, best: {errors.min[0]}'
                .format(errors=statistical_mae_errors))

    # scheduler.step()

    return statistical_losses
Exemplo n.º 6
0
def run(epoch):
    statistical_losses = StatisticalValue()
    statistical_mae_errors = StatisticalValue()

    optimizer.zero_grad()

    for idx, (images, _, coordinates, names) in enumerate(trainloader):
        images, coordinates = images.to(DEVICE), coordinates.to(DEVICE)

        locs = model(images)
        print(locs.shape, coordinates.shape)

        loss = criterion([locs], [coordinates])
        loss.backward()

        # mae_error = mae(input=outputs, target=labels)
        # statistical_mae_errors.update(mae_error.item(), names)
        statistical_losses.update(loss.item())

        iteration_writer.add_scalar('Loss/Iteration',
                                    statistical_losses.val[0],
                                    (epoch - 1) * len(trainloader) + idx + 1)

        if (idx + 1) % ITERATION_SIZE == 0:
            optimizer.step()
            optimizer.zero_grad()

            print_training_status(epoch, idx + 1, len(trainloader),
                                  statistical_losses.val[0],
                                  statistical_losses.avg)
            # print(
            #     'MAE Error: mean: {errors.avg}, worst: {errors.max[0]}, best: {errors.min[0]}'.format(
            #         errors=statistical_mae_errors))

    # scheduler.step()
    # exit()
    return statistical_losses
Exemplo n.º 7
0
def run(epoch):
    statistical_losses = StatisticalValue()
    statistical_mae_errors = StatisticalValue()

    optimizer.zero_grad()

    for idx, (images, labels, names) in enumerate(trainloader):
        images, labels = images.to(DEVICE), labels.to(DEVICE)

        outputs, crops = model(images)

        # xmin, ymin, xmax, ymax = crops[0]['box']
        # view_data = torch.zeros((4, *images.shape[1:]))
        # view_data[0, :, :, :] = images[0]
        # view_data[1, :, :, :] = labels[0]
        # view_data[2, 0:3, ymin: ymax, xmin: xmax] = 1
        # view_data[3, :, :, :] = outputs[0]

        # if not os.path.isdir(os.path.join(TMP_ROOT, 'test')):
        #     os.makedirs(os.path.join(TMP_ROOT, 'test'))

        # torchvision.utils.save_image(
        #     view_data,
        #     os.path.join(TMP_ROOT, 'test/%s' % (names[0]))
        # )

        loss = torch.tensor(0.0).to(DEVICE)

        for i, (output) in enumerate(outputs):
            xmin, ymin, xmax, ymax = crops[i]['box']
            output_list = [output[0]]
            reshaped_label = labels[i][:, ymin:ymax, xmin:xmax]
            total = torch.numel(reshaped_label)
            postive = torch.count_nonzero(reshaped_label)
            negative = total - postive

            # if postive > negative:
            #     loss += saliency_loss(output_list, reshaped_label, weight_0=postive * 2 / total, weight_1=1.0)
            # else:
            #     loss += saliency_loss(output_list, reshaped_label, weight_0=1.0, weight_1=negative * 2 / total)
            # print(output[0].unsqueeze(0).shape, reshaped_label.unsqueeze(0).shape)
            if postive > negative:
                loss += criterion(output[0].unsqueeze(0),
                                  reshaped_label.unsqueeze(0),
                                  weight_0=postive * 2 / total,
                                  weight_1=1.0)
            else:
                loss += criterion(output[0].unsqueeze(0),
                                  reshaped_label.unsqueeze(0),
                                  weight_0=1.0,
                                  weight_1=negative * 2 / total)

        loss /= len(outputs)
        loss.backward()

        mae_error = torch.tensor(0.0).to(DEVICE)

        for i, (output) in enumerate(outputs):
            xmin, ymin, xmax, ymax = crops[i]['box']
            mae_error += mae(input=output[0],
                             target=labels[i][:, ymin:ymax, xmin:xmax])

        mae_error /= len(outputs)

        statistical_mae_errors.update(mae_error.item(), names)
        statistical_losses.update(loss.item())

        iteration_writer.add_scalar('Loss/Iteration',
                                    statistical_losses.val[0],
                                    (epoch - 1) * len(trainloader) + idx + 1)

        if (idx + 1) % ITERATION_SIZE == 0:
            optimizer.step()
            optimizer.zero_grad()

            print_training_status(epoch, idx + 1, len(trainloader),
                                  statistical_losses.val[0],
                                  statistical_losses.avg)
            print(
                'MAE Error: mean: {errors.avg}, worst: {errors.max[0]}, best: {errors.min[0]}'
                .format(errors=statistical_mae_errors))

    # scheduler.step()
    return statistical_losses