def main():

    batch_size = 32
    num_classes = 21
    image_size = (300, 300)
    """ load dataset """
    dataset = loaders.PascalVOCLoader('./datasets/PascalVOC').load()
    train_dataset, valid_dataset = dataset
    """ default box """
    default_box = utils.anchor_box_utils.generate_default_box(
        image_size, utils.anchor_box_utils.num_grids_ssd300,
        utils.anchor_box_utils.grid_step_ssd300,
        utils.anchor_box_utils.grid_size_ssd300,
        utils.anchor_box_utils.aspect_ratio_ssd300)
    """ processor """
    train_processor = processors.PascalVOCAnchorBoxProcessor(
        batch_size,
        num_classes=num_classes,
        default_box=default_box,
        image_size=image_size,
        enable_augmentation=True,
    )
    valid_processor = processors.PascalVOCAnchorBoxProcessor(
        batch_size,
        num_classes=num_classes,
        default_box=default_box,
        image_size=image_size,
        enable_augmentation=False,
    )
    """ iterator """
    train_iterator = iterators.MultiprocessIterator(train_dataset,
                                                    train_processor,
                                                    num_workers=1)
    valid_iterator = iterators.MultiprocessIterator(valid_dataset,
                                                    valid_processor,
                                                    num_workers=2)
    """ device """
    device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
    """ model """
    model = models.SSD300LiteVGG16(input_channels=3,
                                   num_classes=num_classes,
                                   num_bboxes=[4, 6, 6, 6, 4, 4]).to(device)
    """ loss """
    loss_function = losses.MultiBoxLoss().to('cpu')
    """ optimizer """
    optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
    scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=20)
    """ logger """
    logger = loggers.SimpleLogger()
    """ learning """
    for epoch in range(1, 50):
        print(f"-" * 64)
        print(f"[epoch {epoch:>4d}]")
        phase = 'train'
        torch.set_grad_enabled(True)
        for batch_data in tqdm.tqdm(train_iterator, desc=phase):
            optimizer.zero_grad()
            batch_image = torch.from_numpy(batch_data['image']).to(device)
            batch_gt_conf = torch.from_numpy(batch_data['conf']).to('cpu')
            batch_gt_loc = torch.from_numpy(batch_data['loc']).to('cpu')
            batch_output = model(batch_image)
            batch_pred_conf, batch_pred_loc = batch_output
            batch_pred_conf = batch_pred_conf.to('cpu')
            batch_pred_loc = batch_pred_loc.to('cpu')
            batch_loss_conf, batch_loss_loc = loss_function(
                batch_pred_conf, batch_pred_loc, batch_gt_conf, batch_gt_loc)
            batch_loss = batch_loss_conf + batch_loss_loc
            batch_loss = batch_loss.sum() / batch_loss.shape[0]
            batch_loss.backward()
            optimizer.step()
            batch_loss = (batch_loss_conf +
                          batch_loss_loc).detach().cpu().numpy()
            logger.add_batch_loss(batch_loss, phase=phase)
            #break
        loss = logger.get_loss(phase)
        print(f"loss : {loss:.4f}")
        phase = 'valid'
        torch.set_grad_enabled(False)
        i = 0
        for batch_data in tqdm.tqdm(valid_iterator, desc=phase):
            optimizer.zero_grad()
            batch_image = torch.from_numpy(batch_data['image']).to(device)
            batch_gt_conf = torch.from_numpy(batch_data['conf']).to('cpu')
            batch_gt_loc = torch.from_numpy(batch_data['loc']).to('cpu')
            batch_output = model(batch_image)
            batch_pred_conf, batch_pred_loc = batch_output
            batch_pred_conf = batch_pred_conf.to('cpu')
            batch_pred_loc = batch_pred_loc.to('cpu')
            batch_loss_conf, batch_loss_loc = loss_function(
                batch_pred_conf, batch_pred_loc, batch_gt_conf, batch_gt_loc)
            batch_loss = batch_loss_conf + batch_loss_loc
            batch_loss = batch_loss.data.cpu().numpy()
            logger.add_batch_loss(batch_loss, phase=phase)
            batch_image = batch_image.detach().cpu().numpy()
            batch_conf = F.softmax(batch_pred_conf, -1).detach().cpu().numpy()
            batch_gt_conf = batch_gt_conf.detach().cpu().numpy()
            batch_loc = batch_pred_loc.detach().cpu().numpy()
            for image, conf, loc in zip(batch_image, batch_conf, batch_loc):
                image = np.transpose(image, (1, 2, 0)).astype(np.uint8)
                image = utils.draw_bboxes.draw_voc_bboxes(
                    image, default_box, conf, loc)
                if i < 32:
                    cv2.imwrite(
                        f'./results/valid_images/{epoch:04d}_{i:08d}.jpg',
                        image)
                    i += 1
                break
        loss = logger.get_loss(phase)
        print(f"loss : {loss:.4f}")
        logger.step()

        scheduler.step()

        torch.save(model.state_dict(),
                   f"./results/model/epoch_{epoch:04d}.model")
Esempio n. 2
0
def main():

    batch_size = 16
    num_classes = 20
    image_size = (256, 256)
    """ load dataset """
    dataset = loaders.BDD100KSegmentationLoader('./datasets/BDD100K').load()
    train_dataset, valid_dataset = dataset
    """ processor """
    train_processor = processors.BDD100KSegmentationProcessor(
        batch_size,
        num_classes=num_classes,
        image_size=image_size,
        enable_augmentation=True,
    )
    valid_processor = processors.BDD100KSegmentationProcessor(
        batch_size,
        num_classes=num_classes,
        image_size=image_size,
        enable_augmentation=False,
    )
    """ iterator """
    train_iterator = iterators.MultiprocessIterator(train_dataset,
                                                    train_processor,
                                                    num_workers=1)
    valid_iterator = iterators.MultiprocessIterator(valid_dataset,
                                                    valid_processor,
                                                    num_workers=2)
    """ device """
    device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
    """ model """
    model = models.LiteUNet(input_channels=3,
                            num_classes=num_classes).to(device)
    """ optimizer """
    optimizer = torch.optim.Adam(model.parameters(), lr=0.1)
    #scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer,
    #                                                       T_max=20)
    """ logger """
    logger = loggers.SimpleLogger()
    """ learning """
    for epoch in range(1, 200):
        print(f"-" * 64)
        print(f"[epoch {epoch:>4d}]")
        phase = 'train'
        torch.set_grad_enabled(True)
        i = 0
        for batch_data in tqdm.tqdm(train_iterator, desc=phase):
            optimizer.zero_grad()
            batch_image = torch.from_numpy(batch_data['image']).to(device)
            batch_segment = torch.from_numpy(batch_data['segment']).to(device)
            batch_output = model(batch_image)
            batch_loss = -1 * batch_segment * F.log_softmax(batch_output, 1)
            batch_loss = (1 - F.softmax(batch_output, 1)).pow(2) * batch_loss
            batch_loss = batch_loss.sum(1).mean(1).mean(1)
            batch_loss.mean().backward()
            optimizer.step()
            batch_loss = batch_loss.detach().cpu().numpy()
            logger.add_batch_loss(batch_loss, phase=phase)
            if i >= 32:
                continue
            batch_image = batch_image.detach().cpu().numpy()
            batch_output = F.softmax(batch_output, 1).detach().cpu().numpy()
            batch_segment = batch_segment.detach().cpu().numpy()
            for image, output, segment in zip(batch_image, batch_output,
                                              batch_segment):
                image = np.transpose(image, (1, 2, 0)).astype(np.uint8)
                output = utils.segment_decode.voc_color_decode(output)
                segment = utils.segment_decode.voc_color_decode(segment)
                bg = np.zeros([image.shape[0] * 2, image.shape[1] * 2, 3])
                bg[:image.shape[0], :image.shape[1]] = image
                bg[:image.shape[0], image.shape[1]:] = segment
                bg[image.shape[0]:, :image.shape[1]] = (output * 0.5 +
                                                        image * 0.5).astype(
                                                            np.uint8)
                bg[image.shape[0]:, image.shape[1]:] = output
                image = bg
                cv2.imwrite(f'./results/train_images/{epoch:04d}_{i:08d}.jpg',
                            image)
                i += 1
                break
        loss = logger.get_loss(phase)
        print(f"loss : {loss:.4f}")
        phase = 'valid'
        torch.set_grad_enabled(False)
        i = 0
        for batch_data in tqdm.tqdm(valid_iterator, desc=phase):
            optimizer.zero_grad()
            batch_image = torch.from_numpy(batch_data['image']).to(device)
            batch_segment = torch.from_numpy(batch_data['segment']).to(device)
            batch_output = model(batch_image)
            batch_loss = -1 * batch_segment * F.log_softmax(batch_output, 1)
            batch_loss = (1 - F.softmax(batch_output, 1)).pow(2) * batch_loss
            batch_loss = batch_loss.sum(1).mean(1).mean(1)
            batch_loss = batch_loss.detach().cpu().numpy()
            logger.add_batch_loss(batch_loss, phase=phase)
            if i >= 32:
                continue
            batch_image = batch_image.detach().cpu().numpy()
            batch_output = F.softmax(batch_output, 1).detach().cpu().numpy()
            batch_segment = batch_segment.detach().cpu().numpy()
            for image, output, segment in zip(batch_image, batch_output,
                                              batch_segment):
                image = np.transpose(image, (1, 2, 0)).astype(np.uint8)
                output = utils.segment_decode.voc_color_decode(output)
                segment = utils.segment_decode.voc_color_decode(segment)
                bg = np.zeros([image.shape[0] * 2, image.shape[1] * 2, 3])
                bg[:image.shape[0], :image.shape[1]] = image
                bg[:image.shape[0], image.shape[1]:] = segment
                bg[image.shape[0]:, :image.shape[1]] = (output * 0.5 +
                                                        image * 0.5).astype(
                                                            np.uint8)
                bg[image.shape[0]:, image.shape[1]:] = output
                image = bg
                cv2.imwrite(f'./results/valid_images/{epoch:04d}_{i:08d}.jpg',
                            image)
                i += 1
                break
        loss = logger.get_loss(phase)
        print(f"loss : {loss:.4f}")
        logger.step()

        #scheduler.step()

        torch.save(model.state_dict(),
                   f"./results/model/epoch_{epoch:04d}.model")
Esempio n. 3
0
def main():

    batch_size = 256
    num_classes = 10
    """ load dataset """
    dataset = loaders.Cifar10Loader('./datasets/CIFAR-10').load()
    train_dataset, valid_dataset = dataset
    """ processor """
    train_processor = processors.Cifar10ClassificationProcessor(
        batch_size,
        num_classes=num_classes,
        enable_augmentation=True,
        image_size=(32, 32))
    valid_processor = processors.Cifar10ClassificationProcessor(
        batch_size,
        num_classes=num_classes,
        enable_augmentation=False,
        image_size=(32, 32))
    """ iterator """
    train_iterator = iterators.MultiprocessIterator(train_dataset,
                                                    train_processor,
                                                    num_workers=1)
    valid_iterator = iterators.MultiprocessIterator(valid_dataset,
                                                    valid_processor,
                                                    num_workers=1)
    """ device """
    device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
    """ model """
    model = models.RiriverceCifar10Net9(input_channels=3,
                                        num_classes=num_classes).to(device)
    """ loss """
    loss_function = losses.CrossEntropyLoss().to(device)
    """ optimizer """
    optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
    scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=20)
    """ logger """
    logger = loggers.SimpleLogger()
    """ learning """
    for epoch in range(100):
        print(f"-" * 64)
        print(f"[epoch {epoch:>4d}]")
        phase = 'train'
        torch.set_grad_enabled(True)
        for batch_data in tqdm.tqdm(train_iterator, desc=phase):
            optimizer.zero_grad()
            batch_image = torch.from_numpy(batch_data['image']).to(device)
            batch_target = torch.from_numpy(batch_data['target']).to(device)
            batch_output = model(batch_image)
            batch_loss = loss_function(batch_output, batch_target)
            batch_loss.sum().backward()
            optimizer.step()
            batch_loss = batch_loss.data.cpu().numpy()
            batch_label = np.argmax(batch_target.data.cpu().numpy(),
                                    axis=-1).flatten()
            batch_pred = np.argmax(batch_output.data.cpu().numpy(),
                                   axis=-1).flatten()
            logger.add_batch_loss(batch_loss, phase=phase)
            logger.add_batch_pred(batch_pred, phase=phase)
            logger.add_batch_label(batch_label, phase=phase)
        loss = logger.get_loss(phase)
        accuracy = logger.get_accuracy(phase)
        print(f"loss : {loss}")
        print(f"accuracy : {accuracy}")
        phase = 'valid'
        torch.set_grad_enabled(False)
        for batch_data in tqdm.tqdm(valid_iterator, desc=phase):
            optimizer.zero_grad()
            batch_image = torch.from_numpy(batch_data['image']).to(device)
            batch_target = torch.from_numpy(batch_data['target']).to(device)
            batch_output = model(batch_image)
            batch_loss = loss_function(batch_output, batch_target)
            batch_loss = batch_loss.data.cpu().numpy()
            batch_label = np.argmax(batch_target.data.cpu().numpy(),
                                    axis=-1).flatten()
            batch_pred = np.argmax(batch_output.data.cpu().numpy(),
                                   axis=-1).flatten()
            logger.add_batch_loss(batch_loss, phase=phase)
            logger.add_batch_pred(batch_pred, phase=phase)
            logger.add_batch_label(batch_label, phase=phase)
        loss = logger.get_loss(phase)
        accuracy = logger.get_accuracy(phase)
        print(f"loss : {loss:.4f}")
        print(f"accuracy : {accuracy:.4f}")
        logger.step()