Beispiel #1
0
def get_loaders(config):
    """
    update config.class_number
    return loaders
    """
    dataset_config = edict()
    dataset_config.with_edge = False
    dataset_config.resize_shape = config.input_shape
    dataset_config = get_dataset_generalize_config(dataset_config,
                                                   config.semantic_dataset)

    config.class_number = len(dataset_config.foreground_class_ids) + 1
    normer = image_normalizations(ways='-1,1')
    batch_size = config.batch_size

    dataset_loaders = {}
    for split in ['train', 'val']:
        augmentations = Augmentations() if split == 'train' else None
        batch_size = config.batch_size if split == 'train' else 1
        drop_last = True if split == 'train' else False

        xxx_dataset = dataset_generalize(dataset_config,
                                         split=split,
                                         augmentations=augmentations,
                                         normalizations=normer)
        xxx_loader = TD.DataLoader(dataset=xxx_dataset,
                                   batch_size=batch_size,
                                   shuffle=True,
                                   drop_last=drop_last,
                                   num_workers=2)
        dataset_loaders[split] = xxx_loader

    return dataset_loaders, config
Beispiel #2
0
def test_ignore_index():
    config = edict()
    config.root_path = '/media/sdb/CVDataset/ObjectSegmentation/archives/Cityscapes_archives'
    config.cityscapes_split = random.choice(['test', 'val', 'train'])
    config.print_path = True
    config.resize_shape = (224, 224)
    config.ignore_index = 255
    config.with_edge = False
    config = get_dataset_generalize_config(config, 'Cityscapes')

    fg_ids = [i for i in range(19)]

    augmentations = None
    dataset = dataset_generalize(config,
                                 split='train',
                                 augmentations=augmentations)
    loader = TD.DataLoader(dataset=dataset,
                           batch_size=2,
                           shuffle=True,
                           drop_last=False)
    for i, data in enumerate(loader):
        imgs, labels = data
        #        print(i,imgs.shape,labels.shape)
        labels_ids = np.unique(labels)
        print(labels_ids)
        for id in labels_ids:
            assert id == config.ignore_index or id in fg_ids, 'bad id=%d' % id


#        plt.imshow(imgs[0,0])
#        plt.show()
#        plt.imshow(labels[0])
#        plt.show()
        if i >= 10:
            break
Beispiel #3
0
 def get_default_config():
     config = SS.get_default_config()
     config.model.activation = 'softmax'
     config.training.log_dir = os.path.expanduser('~/tmp/logs/keras')
     config.dataset.resize_shape = (32, 32)
     config.model.input_shape = (32, 32)
     config.dataset.batch_size = 2
     config.dataset = get_dataset_generalize_config(config.dataset,
                                                    'Cityscapes')
     return config
    def __init__(self,
                 root=None,
                 split='train',
                 mode=None,
                 transform=None,
                 config=edict(),
                 name='Cityscapes'):

        self.config = get_dataset_generalize_config(config, name)
        self.NUM_CLASS = len(self.config.foreground_class_ids)
        if root is None:
            root = self.config.root_path
        super().__init__(root, split, mode, transform)

        splits = ['train', 'val', 'test', 'train_extra']
        assert self.split in splits, 'unexcepted split %s for dataset, must be one of %s' % (
            self.split, str(splits))

        if hasattr(self.config, 'txt_note'):
            self.split = self.config.txt_note + '_' + self.split

        if hasattr(self.config, 'txt_path'):
            txt_file = os.path.join(self.config.txt_path, self.split + '.txt')
            self.image_files, self.annotation_files = self.get_files_from_txt(
                txt_file, self.config.root_path)
            assert len(
                self.image_files) > 0, 'No files found in %s with %s' % (
                    self.config.root_path, txt_file)
            assert len(
                self.annotation_files) > 0, 'No files found in %s with %s' % (
                    self.config.root_path, txt_file)
        else:
            assert hasattr(
                self.config, 'image_txt_path'
            ), 'image_txt_path and annotation_txt_path needed when txt_path not offered!'
            assert hasattr(
                self.config, 'annotation_txt_path'
            ), 'image_txt_path and annotation_txt_path needed when txt_path not offered!'
            image_txt_file = os.path.join(self.config.image_txt_path,
                                          self.split + '.txt')
            annotation_txt_file = os.path.join(self.config.annotation_txt_path,
                                               self.split + '.txt')
            self.image_files = self.get_files_from_txt(image_txt_file,
                                                       self.config.root_path)
            self.annotation_files = self.get_files_from_txt(
                annotation_txt_file, self.config.root_path)
            assert len(
                self.image_files) > 0, 'No files found in %s with %s' % (
                    self.config.root_path, image_txt_file)
            assert len(
                self.annotation_files) > 0, 'No files found in %s with %s' % (
                    self.config.root_path, annotation_txt_file)

        self.images = [os.path.join(root, p) for p in self.image_files]
        self.masks = [os.path.join(root, p) for p in self.annotation_files]
def test_network():
    class_number=19
    ignore_index=255
    net=simple_net(19)
    loss_fn=torch.nn.CrossEntropyLoss(ignore_index=ignore_index)
    optimizer = torch.optim.Adam([p for p in net.parameters() if p.requires_grad], lr = 0.0001)
    
    config=edict()
    config=get_dataset_generalize_config(config,'Cityscapes')
    config.resize_shape=(224,224)
    
    augmentations=None
    dataset=dataset_generalize(config,split='train',augmentations=augmentations)
    loader=TD.DataLoader(dataset=dataset,batch_size=2, shuffle=True,drop_last=False)
    
    device=torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    net.to(device)
    net.train()
    for i, (images, labels) in enumerate(loader):
        images = torch.autograd.Variable(images.to(device).float())
        labels = torch.autograd.Variable(labels.to(device).long())
        
        optimizer.zero_grad()
        outputs = net.forward(images)
#        print('images shape',images.shape)
#        print('output shape',outputs.shape)
#        print('labels shape',labels.shape)
        assert outputs.size()[2:] == labels.size()[1:]
        assert outputs.size()[1] == class_number
        loss = loss_fn(input=outputs, target=labels)
#                    loss=torch.nn.functional.cross_entropy(input=outputs,target=labels,ignore_index=255)

        loss.backward()
        optimizer.step()
        
        print('epoch=%s, loss=%.4f'%(i,loss.data))
Beispiel #6
0
import random
from dataset.dataset_generalize import dataset_generalize, get_dataset_generalize_config
from easydict import EasyDict as edict
import matplotlib.pyplot as plt
import os
import cv2

config = edict()
config.root_path = '/media/sdb/CVDataset/ObjectSegmentation/archives/Cityscapes_archives'
#config.cityscapes_split=random.choice(['test','val','train'])
config.resize_shape = (224, 224)
config.print_path = False
config.with_path = False
config.with_edge = False
#config=get_dataset_generalize_config(config,'Cityscapes')
config = get_dataset_generalize_config(config, 'VOC2012')

split = 'test'
dataset = dataset_generalize(config, split=split)
loader = TD.DataLoader(dataset=dataset,
                       batch_size=2,
                       shuffle=False,
                       drop_last=False)

save_root = '/media/sdc/yzbx/benchmark_output/cityscapes'
os.makedirs(save_root, exist_ok=True)

for i, data in enumerate(loader):
    imgs = data['image']
    paths = data['filename']
    if split == 'test':
Beispiel #7
0
    config.model.align_corners = True

    if args.input_shape == 0:
        if args.midnet_name == 'psp':
            count_size = max(
                config.model.midnet_pool_sizes) * config.model.midnet_scale
            input_shape = (count_size, count_size)
        else:
            input_shape = (72 * 8, 72 * 8)
    else:
        input_shape = (args.input_shape, args.input_shape)

    config.model.input_shape = input_shape

    config.dataset = edict()
    config.dataset = get_dataset_generalize_config(config.dataset,
                                                   args.dataset_name)
    if config.dataset.ignore_index == 0:
        config.model.class_number = len(
            config.dataset.foreground_class_ids) + 1
    else:
        config.model.class_number = len(config.dataset.foreground_class_ids)
    config.dataset.resize_shape = input_shape
    config.dataset.name = args.dataset_name.lower()
    config.dataset.norm = True

    config.args = edict()
    config.args.n_epoch = args.n_epoch
    config.args.log_dir = os.path.expanduser('~/tmp/logs/pytorch')

    if args.note is None:
        config.args.note = '_'.join([
Beispiel #8
0
def get_config(args=None):
    # for test some function
    if args is None:
        return get_default_config()
    
    config = edict()
    config.model = edict()
    config.model.upsample_type = args.upsample_type
    config.model.auxnet_type = args.auxnet_type
    config.model.upsample_layer = args.upsample_layer
    config.model.use_none_layer = args.use_none_layer
    if config.model.upsample_layer > 3:
        config.model.use_none_layer = args.use_none_layer = True
    os.environ['use_none_layer'] = str(config.model.use_none_layer)
    
    config.model.deconv_layer=args.deconv_layer
    config.model.auxnet_layer = args.auxnet_layer
    config.model.cross_merge_times=args.cross_merge_times
    
    config.model.backbone_pretrained = args.backbone_pretrained
    config.model.use_bn=args.use_bn
    # use_bn,use_dropout will change the variable in local_bn,local_dropout
    # use_bias will affect the bias in upsample
    os.environ['torchseg_use_bn']=str(args.use_bn)
    config.model.use_dropout=args.use_dropout
    os.environ['torchseg_use_dropout']=str(args.use_dropout)
    config.model.use_bias=args.use_bias
    os.environ['torchseg_use_bias']=str(args.use_bias)
    # when use resnet and use_none_layer=True
    config.model.modify_resnet_head=args.modify_resnet_head
    os.environ['modify_resnet_head']=str(args.modify_resnet_head)
    
    config.model.eps = 1e-5
    config.model.momentum = args.momentum
    config.model.learning_rate = args.learning_rate
    config.model.optimizer = args.optimizer
    config.model.scheduler = args.scheduler
    config.model.lr_weight_decay=args.lr_weight_decay
    config.model.lr_momentum=args.lr_momentum
    config.model.use_lr_mult = args.use_lr_mult
    config.model.pre_lr_mult = args.pre_lr_mult
    config.model.changed_lr_mult=args.changed_lr_mult
    config.model.new_lr_mult=args.new_lr_mult
    config.model.use_reg = args.use_reg
    
    config.model.use_class_weight=args.use_class_weight
    config.model.class_weight_alpha=args.class_weight_alpha
    config.model.focal_loss_gamma=args.focal_loss_gamma
    config.model.focal_loss_alpha=args.focal_loss_alpha
    config.model.focal_loss_grad=args.focal_loss_grad
#    config.model.l1_reg=args.l1_reg
    config.model.l2_reg=args.l2_reg
    config.model.backbone_name = args.backbone_name
    config.model.backbone_freeze = args.backbone_freeze
    config.model.freeze_layer = args.freeze_layer
    config.model.freeze_ratio = args.freeze_ratio
    config.model.layer_preference = 'first'
    config.model.edge_seg_order=args.edge_seg_order

    config.model.midnet_pool_sizes = [6, 3, 2, 1]
    config.model.midnet_scale = args.midnet_scale
    config.model.midnet_name = args.midnet_name
    
    config.model.edge_bg_weight=args.edge_bg_weight
    config.model.edge_base_weight=args.edge_base_weight
    config.model.edge_power=args.edge_power
    config.model.aux_base_weight=args.aux_base_weight

    config.dataset = edict()
    config.dataset.edge_class_num=args.edge_class_num
    config.dataset.edge_width=args.edge_width
    config.dataset.edge_with_gray=args.edge_with_gray
    config.dataset.with_edge=False
    
#    if args.dataset_name in ['VOC2012','Cityscapes']:
#        config.dataset.norm_ways = args.dataset_name.lower()
#    else:
#        config.dataset.norm_ways = 'pytorch'
#    config.dataset.norm_ways = 'pytorch'
    config.dataset.norm_ways = args.norm_ways
    
    if args.input_shape == 0:
        if args.net_name == 'motionnet':
            upsample_ratio=3
            count_size = max(config.model.midnet_pool_sizes) * \
                config.model.midnet_scale*2**upsample_ratio
            input_shape = (count_size, count_size)
        elif args.net_name == 'motion_panet':
            input_shape=(448,448)
        elif args.midnet_name == 'psp':
            upsample_ratio=args.upsample_layer
            if args.use_none_layer and args.upsample_layer>=3:
                upsample_ratio=3
            count_size = max(config.model.midnet_pool_sizes) * \
                config.model.midnet_scale*2**upsample_ratio
            input_shape = (count_size, count_size)
        else:
            input_shape = (72*8, 72*8)
    else:
        input_shape = (args.input_shape, args.input_shape)
    
    print('convert input shape is',input_shape,'*'*30)
    
    config.model.input_shape = input_shape
    config.model.midnet_out_channels = 512
    config.model.subclass_sigmoid=args.subclass_sigmoid
    config.dataset = get_dataset_generalize_config(
        config.dataset, args.dataset_name)
    if config.dataset.ignore_index == 0:
        config.model.class_number = len(config.dataset.foreground_class_ids)+1
    else:
        config.model.class_number = len(config.dataset.foreground_class_ids)
    config.dataset.resize_shape = input_shape
    config.dataset.name = args.dataset_name.lower()
    config.dataset.augmentations_blur = args.augmentations_blur
    config.dataset.dataset_use_part=args.dataset_use_part

    config.args = edict()
    config.args.n_epoch = args.n_epoch
    # for hyperopt use ~/tmp/logs/hyperopt
    config.args.log_dir = args.log_dir
    config.args.summary_image=args.summary_image
    config.args.save_model=args.save_model
    config.args.iou_save_threshold=args.iou_save_threshold
    config.args.batch_size = args.batch_size
    config.args.augmentation = args.augmentation
    config.args.augmentations_rotate=args.augmentations_rotate
    config.args.net_name=args.net_name
    config.model.net_name=args.net_name
    config.args.checkpoint_path=args.checkpoint_path
    config.args.center_loss=args.center_loss
    config.args.center_loss_weight=args.center_loss_weight
    if args.net_name in ['psp_edge','merge_seg','cross_merge','psp_hed']:
        config.dataset.with_edge = True
        
    if args.note is None:
        config.args.note = '_'.join([args.test,
                                     'bs'+str(args.batch_size),
                                     'aug', str(args.augmentation)[0],
                                     ])
    else:
        config.args.note=args.note
    
    default_aug_config=get_default_augmentor_config()
    config.aug=edict()
    config.aug=default_aug_config.aug
    config.aug.use_rotate=config.args.augmentations_rotate
    config.aug.use_imgaug=True
    config.aug.keep_crop_ratio=args.keep_crop_ratio
    config.aug.crop_size_step=args.crop_size_step
    config.aug.min_crop_size=args.min_crop_size
    config.aug.max_crop_size=args.max_crop_size
    config.aug.pad_for_crop=args.pad_for_crop
    
    # image size != network input size != crop size
    if config.aug.keep_crop_ratio is False:
        if args.min_crop_size is None:
            config.aug.min_crop_size=[2*i for i in config.model.input_shape]
        if args.max_crop_size is None:
            config.aug.max_crop_size=config.aug.min_crop_size
        
        if not isinstance(config.aug.min_crop_size,(tuple,list)):
            assert config.aug.min_crop_size>0
        else:
            assert min(config.aug.min_crop_size)>0
        if not isinstance(config.aug.max_crop_size,(tuple,list)):
            assert config.aug.max_crop_size>0
        else:
            assert min(config.aug.max_crop_size)>0
        
        print('min_crop_size is',config.aug.min_crop_size)
        print('max_crop_size is',config.aug.max_crop_size)
        print('crop_size_step is',config.aug.crop_size_step)
            
    return config
Beispiel #9
0
        outputs = KL.Conv2D(filters=self.class_number,
                            kernel_size=(3, 3),
                            activation=self.config.model.activation,
                            padding='same',
                            data_format=data_format)(x)
        self.model = keras.models.Model(inputs=base_model.inputs,
                                        outputs=[outputs])


if __name__ == '__main__':
    config = edict()
    config.model = edict()
    config.dataset = edict()
    config.training = edict()

    config.dataset = get_dataset_generalize_config(config.dataset,
                                                   'Cityscapes')
    config.dataset.with_edge = False
    config.dataset.dataset_use_part = 0

    config.model.class_number = len(config.dataset.foreground_class_ids) + 1
    config.dataset.ignore_index = 0
    config.model.upsample_type = 'bilinear'
    config.model.upsample_layer = 3
    config.model.midnet_pool_sizes = [6, 3, 2, 1]
    config.model.midnet_scale = 15
    config.model.trainable_ratio = 1.0
    config.model.load_imagenet_weights = True
    config.model.backbone_type = 'standard'
    config.model.layer_preference = 'first'
    config.model.data_format = 'channels_last'
    config.model.backbone = 'vgg19'
                #                print(outputs.shape,labels.shape)

                loss = loss_fn(input=outputs, target=labels)

                loss.backward()
                optimizer.step()

                if (i + 1) % 20 == 0:
                    print("Epoch [%d/%d] Loss: %.4f lr: %.4f" %
                          (epoch + 1, args.n_epoch, loss.data,
                           optimizer.param_groups[-1]['lr']))
                    print('param_groups', len(optimizer.param_groups))


if __name__ == '__main__':
    config = edict()
    config.root_path = '/media/sdb/CVDataset/ObjectSegmentation/archives/Cityscapes_archives'
    config.split = random.choice(['test', 'val', 'train'])
    config.resize_shape = (224, 224)
    config = get_dataset_generalize_config(config, 'Cityscapes')
    dataset = dataset_generalize(config, split=config.split)
    loader = TD.DataLoader(dataset=dataset,
                           batch_size=2,
                           shuffle=True,
                           drop_last=False)
    args = edict()
    args.n_epoch = 3
    net = simple_net()
    net.train(args, loader)