# Use focal loss or not
focal = False

# Use centernet-like Blazeface
center = False


# # set up person only VOC dataset

# In[3]:


# load files
vocpath = os.path.join("..", "VOCdevkit", "VOC2007")
train_img_list, train_anno_list, val_img_list, val_anno_list = make_datapath_list(vocpath, cls="person")

# extend with VOC2012
vocpath = "../VOCdevkit/VOC2012"
train_img_list2, train_anno_list2, _, _ = make_datapath_list(vocpath, cls="person", VOC2012=True)

train_img_list.extend(train_img_list2)
train_anno_list.extend(train_anno_list2)

# make Dataset
voc_classes = ['person']
color_mean = (104, 117, 123)  # (BGR)の色の平均値

print("trainlist: ", len(train_img_list))
print("vallist: ", len(val_img_list))
Beispiel #2
0
def main():
    args = parser()

    ### setup configs ###
    configfile = args.configfile

    with open(configfile) as f:
        configs = yaml.load(f)

    ## path process (path definition, make directories)
    now = datetime.now().isoformat()
    log_dir = Path(configs['log_dir']) / now
    paths = Paths(log_dir=log_dir)

    ### setup logs and summary writer ###
    setup_logger(logfile=paths.logfile)

    writer = SummaryWriter(str(paths.summary_dir))

    ### setup GPU or CPU ###
    if configs['n_gpus'] > 0 and torch.cuda.is_available():
        logger.info('CUDA is available! using GPU...\n')
        device = torch.device('cuda')
    else:
        logger.info('using CPU...\n')
        device = torch.device('cpu')

    ### Dataset ###
    logger.info('preparing dataset...')
    dataset_name = configs['dataset']
    logger.info(f'==> dataset: {dataset_name}\n')

    if configs['dataset'] == 'cifar10':
        transform = transforms.Compose([
            transforms.Resize(configs['img_size'], configs['img_size']),
            transforms.ToTensor(),
            transforms.Normalize(configs['color_mean'], configs['color_std']),
        ])
        train_dataset = datasets.CIFAR10(root=configs['data_root'],
                                         train=True,
                                         transform=transform,
                                         download=True)
        test_dataset = datasets.CIFAR10(root=configs['data_root'],
                                        train=False,
                                        transform=transform,
                                        download=True)
    elif configs['dataset'] == 'custom':
        train_transform = DataTransforms(img_size=configs['img_size'],
                                         color_mean=configs['color_mean'],
                                         color_std=configs['color_std'],
                                         phase='train')
        test_transform = DataTransforms(img_size=configs['img_size'],
                                        color_mean=configs['color_mean'],
                                        color_std=configs['color_std'],
                                        phase='test')
        train_img_list, train_lbl_list, test_img_list, test_lbl_list = make_datapath_list(
            root=configs['data_root'])
        train_dataset = Dataset(train_img_list,
                                train_lbl_list,
                                transform=train_transform)
        test_dataset = Dataset(test_img_list,
                               test_lbl_list,
                               transform=test_transform)
    else:
        logger.debug('dataset is not supported')
        raise ValueError('dataset is not supported')

    train_loader = torch.utils.data.DataLoader(
        train_dataset,
        batch_size=configs['batch_size'],
        shuffle=True,
        num_workers=8)
    test_loader = torch.utils.data.DataLoader(test_dataset,
                                              batch_size=configs['batch_size'],
                                              shuffle=False,
                                              num_workers=8)

    ### Network ###
    logger.info('preparing network...')

    network = CNNAutoEncoder(in_channels=configs['n_channels'],
                             n_classes=configs['n_classes'])

    network = network.to(device)
    cnn_criterion = nn.CrossEntropyLoss()
    ae_criterion = nn.MSELoss()
    optimizer = optim.Adam(network.parameters(), lr=configs['lr'])

    if configs['resume']:
        # Load checkpoint
        logger.info('==> Resuming from checkpoint...\n')
        if not Path(configs['resume']).exists():
            logger.info('No checkpoint found !')
            raise ValueError('No checkpoint found !')

        ckpt = torch.load(configs['resume'])
        network.load_state_dict(ckpt['model_state_dict'])
        optimizer.load_state_dict(ckpt['optimizer_state_dict'])
        start_epoch = ckpt['epoch']
        loss = ckpt['loss']
    else:
        logger.info('==> Building model...\n')
        start_epoch = 0

    logger.info('model summary: ')
    summary(network,
            input_size=(configs['n_channels'], configs['img_size'],
                        configs['img_size']))

    if configs["n_gpus"] > 1:
        network = nn.DataParallel(network)

    ### Metrics ###
    metrics = Metrics(n_classes=configs['n_classes'],
                      classes=configs['classes'],
                      writer=writer,
                      metrics_dir=paths.metrics_dir,
                      plot_confusion_matrix=plot_confusion_matrix)

    ### Train or Test ###
    kwargs = {
        'device': device,
        'network': network,
        'optimizer': optimizer,
        'criterions': (cnn_criterion, ae_criterion),
        'classification_loss_weight': configs['classification_loss_weight'],
        'autoencoder_loss_weight': configs['autoencoder_loss_weight'],
        'data_loaders': (train_loader, test_loader),
        'metrics': metrics,
        'writer': writer,
        'n_classses': configs['n_classes'],
        'save_ckpt_interval': configs['save_ckpt_interval'],
        'ckpt_dir': paths.ckpt_dir,
    }

    cnn_classifier = CNNClassifier(**kwargs)

    if args.inference:
        if not configs['resume']:
            logger.info('No checkpoint found for inference!')
        logger.info('mode: inference\n')
        cnn_classifier.test(epoch=start_epoch, inference=True)
    else:
        logger.info('mode: train\n')
        cnn_classifier.train(n_epochs=configs['n_epochs'],
                             start_epoch=start_epoch)
Beispiel #3
0
# select from efficientnet backbone or resnet backbone
backbone = "efficientnet-b0"
scale = 1
useBiFPN = True
# scale==1: resolution 300
# scale==2: resolution 600

# ## make data.Dataset for training

# In[4]:

# load files
# set your VOCdevkit path here.
vocpath = "../VOCdevkit/VOC2007"
train_img_list, train_anno_list, val_img_list, val_anno_list = make_datapath_list(
    vocpath)

vocpath = "../VOCdevkit/VOC2012"
train_img_list2, train_anno_list2, _, _ = make_datapath_list(vocpath)

train_img_list.extend(train_img_list2)
train_anno_list.extend(train_anno_list2)

print("trainlist: ", len(train_img_list))
print("vallist: ", len(val_img_list))

# make Dataset
voc_classes = [
    'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat',
    'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person',
    'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor'
Beispiel #4
0
import torch.nn as nn
import torch.nn.init as init
import torch.nn.functional as F
from torch.autograd import Function
import pandas as pd

# import dataset
from utils.dataset import VOCDataset, DatasetTransform, make_datapath_list, Anno_xml2list, od_collate_fn

# # set up person only VOC dataset

# In[2]:

# load files
vocpath = os.path.join("..", "VOCdevkit", "VOC2007")
train_img_list, train_anno_list, val_img_list, val_anno_list = make_datapath_list(
    vocpath, cls="person")

# make Dataset
voc_classes = ['person']
color_mean = (104, 117, 123)  # (BGR)の色の平均値
input_size = 128  # 画像のinputサイズを300×300にする

## DatasetTransformを適応
transform = DatasetTransform(input_size, color_mean)
transform_anno = Anno_xml2list(voc_classes)

train_dataset = VOCDataset(train_img_list,
                           train_anno_list,
                           phase="train",
                           transform=transform,
                           transform_anno=transform_anno)
Beispiel #5
0
def main():
    args = parser()

    ### setup configs ###
    configfile = args.configfile

    with open(configfile) as f:
        configs = yaml.load(f)

    ## path process (path definition, make directories)
    now = datetime.now().isoformat()
    log_dir = Path(configs['log_dir']) / now
    paths = Paths(log_dir=log_dir)

    ### setup logs and summary writer ###
    setup_logger(logfile=paths.logfile)

    writer = SummaryWriter(str(paths.summary_dir))

    ### setup GPU or CPU ###
    if configs['n_gpus'] > 0 and torch.cuda.is_available():
        logger.info('CUDA is available! using GPU...\n')
        device = torch.device('cuda')
    else:
        logger.info('using CPU...\n')
        device = torch.device('cpu')

    ### Dataset ###
    logger.info('preparing dataset...')

    train_transform = DataTransforms(img_size=configs['img_size'],
                                     color_mean=configs['color_mean'],
                                     color_std=configs['color_std'],
                                     phase='train')
    test_transform = DataTransforms(img_size=configs['img_size'],
                                    color_mean=configs['color_mean'],
                                    color_std=configs['color_std'],
                                    phase='test')
    train_img_list, test_img_list = make_datapath_list(
        root=configs['data_root'])
    train_dataset = Dataset(train_img_list, transform=train_transform)
    test_dataset = Dataset(test_img_list, transform=test_transform)

    train_loader = torch.utils.data.DataLoader(
        train_dataset,
        batch_size=configs['batch_size'],
        shuffle=True,
        num_workers=8)
    test_loader = torch.utils.data.DataLoader(test_dataset,
                                              batch_size=configs['batch_size'],
                                              shuffle=False,
                                              num_workers=8)

    ### Network ###
    logger.info('preparing network...')

    network = VAE(in_channels=configs['n_channels'],
                  h_dim=1024,
                  z_dim=32,
                  device=device)

    network = network.to(device)
    criterion = nn.MSELoss()
    optimizer = optim.Adam(network.parameters(), lr=configs['lr'])

    if configs['resume']:
        # Load checkpoint
        logger.info('==> Resuming from checkpoint...\n')
        if not Path(configs['resume']).exists():
            logger.info('No checkpoint found !')
            raise ValueError('No checkpoint found !')

        ckpt = torch.load(configs['resume'])
        network.load_state_dict(ckpt['model_state_dict'])
        optimizer.load_state_dict(ckpt['optimizer_state_dict'])
        start_epoch = ckpt['epoch']
        loss = ckpt['loss']
    else:
        logger.info('==> Building model...\n')
        start_epoch = 0

    logger.info('model summary: ')
    summary(network,
            input_size=(configs['n_channels'], configs['img_size'],
                        configs['img_size']))

    if configs["n_gpus"] > 1:
        network = nn.DataParallel(network)

    ### Metrics ###
    metrics = Metrics(writer=writer, metrics_dir=paths.metrics_dir)

    ### Train or Test ###
    kwargs = {
        'device': device,
        'network': network,
        'optimizer': optimizer,
        'criterion': criterion,
        'data_loaders': (train_loader, test_loader),
        'metrics': metrics,
        'writer': writer,
        'save_ckpt_interval': configs['save_ckpt_interval'],
        'ckpt_dir': paths.ckpt_dir,
        'img_outdir': paths.img_outdir,
    }

    generalizer = Generalizer(**kwargs)

    if args.inference:
        if not configs['resume']:
            logger.info('No checkpoint found for inference!')
        logger.info('mode: inference\n')
        generalizer.test(epoch=start_epoch, inference=True)
    else:
        logger.info('mode: train\n')
        generalizer.train(n_epochs=configs['n_epochs'],
                          start_epoch=start_epoch)
Beispiel #6
0
from utils.dataset import make_datapath_list, VOCDataset, DataTransform, Anno_xml2list
from utils.dataloader import od_collate_fn
import torch.nn as nn
import torch.nn.init as init
import torch
import pandas as pd
import torch.optim as optim
import time
import random
import numpy as np

torch.manual_seed(42)
np.random.seed(42)
random.seed(42)
rootpath = "./data/VOCdevkit/VOC2012/"
train_img_list, train_anno_list, val_img_list, val_anno_list = make_datapath_list(rootpath)

voc_classes = ['aeroplane', 'bicycle', 'bird', 'boat',
               'bottle', 'bus', 'car', 'cat', 'chair',
               'cow', 'diningtable', 'dog', 'horse',
               'motorbike', 'person', 'pottedplant',
               'sheep', 'sofa', 'train', 'tvmonitor']

color_mean = (104, 117, 123)
input_size = 300

train_dataset = VOCDataset(train_img_list, train_anno_list, phase='train',
                            transform=DataTransform(input_size, color_mean),
                            transform_anno=Anno_xml2list(voc_classes))

val_dataset = VOCDataset(val_img_list, val_anno_list, phase='val',