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
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
def get_loader(config): if config.dataset.norm_ways is None: normalizations = None else: normalizations = image_normalizations(config.dataset.norm_ways) if config.args.augmentation: augmentations = Augmentations(config) else: augmentations = None # must change batch size here!!! batch_size = config.args.batch_size train_dataset = dataset_generalize( config.dataset, split='train', augmentations=augmentations, normalizations=normalizations) train_loader = TD.DataLoader( dataset=train_dataset, batch_size=batch_size, shuffle=True, drop_last=True, num_workers=4) val_dataset = dataset_generalize(config.dataset, split='val', augmentations=None, normalizations=normalizations) val_loader = TD.DataLoader( dataset=val_dataset, batch_size=batch_size, shuffle=True, drop_last=False, num_workers=2) return train_loader, val_loader
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))
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': print(paths[0]) print(imgs[0].shape) else:
else: config.args.note = '_'.join([ args.note, args.backbone_name, 'bn%d' % args.batch_size, 'aug' + str(args.augmentation)[0] ]) if args.train_split == 'train_extra': config.args.note += '_train_extra' if args.augmentation: augmentations = Augmentations(p=0.25) else: augmentations = None train_dataset = dataset_generalize(config.dataset, split=args.train_split, augmentations=augmentations) train_loader = TD.DataLoader(dataset=train_dataset, batch_size=args.batch_size, shuffle=True, drop_last=True, num_workers=8) val_dataset = dataset_generalize(config.dataset, split='val', augmentations=augmentations) val_loader = TD.DataLoader(dataset=val_dataset, batch_size=args.batch_size, shuffle=True, drop_last=False, num_workers=8)
if config.dataset.norm_ways is None: normalizations = None else: normalizations = image_normalizations(config.dataset.norm_ways) if config.args.augmentation: augmentations = Augmentations(config) else: augmentations = None # must change batch size here!!! batch_size = args.batch_size train_dataset = dataset_generalize(config.dataset, split='train', augmentations=augmentations, normalizations=normalizations) train_loader = TD.DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True, drop_last=True, num_workers=4) val_dataset = dataset_generalize(config.dataset, split='val', augmentations=None, normalizations=normalizations) val_loader = TD.DataLoader(dataset=val_dataset, batch_size=batch_size, shuffle=True, drop_last=False,
def train(self, args=None, train_loader=None, val_loader=None): if args is None: args = self.config.training self.model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['acc']) if train_loader is None and val_loader is None: train_dataset = dataset_generalize(config.dataset, split='train', bchw=False) train_loader = TD.DataLoader( dataset=train_dataset, batch_size=self.config.dataset.batch_size, shuffle=True, drop_last=False) val_dataset = dataset_generalize(config.dataset, split='val', bchw=False) val_loader = TD.DataLoader( dataset=val_dataset, batch_size=self.config.dataset.batch_size, shuffle=True, drop_last=False) running_metrics = runningScore(self.class_number) time_str = time.strftime("%Y-%m-%d___%H-%M-%S", time.localtime()) log_dir = os.path.join(args.log_dir, self.name, self.dataset_name, args.note, time_str) checkpoint_path = os.path.join( log_dir, "{}_{}_best_model.pkl".format(self.name, self.dataset_name)) os.makedirs(log_dir, exist_ok=True) writer = SummaryWriter(log_dir=log_dir) best_iou = 0.6 loaders = [train_loader, val_loader] loader_names = ['train', 'val'] for epoch in range(args.n_epoch): for loader, loader_name in zip(loaders, loader_names): if loader is None: continue if loader_name == 'val': if epoch % 10 != 0: continue print(loader_name + '.' * 50) n_step = len(loader) losses = [] for i, (images, labels) in enumerate(loader): x = images.data.numpy() trues = labels.data.numpy() y = to_categorical(trues, self.class_number) if loader_name == 'train': outputs = self.model.train_on_batch(x, y) else: outputs = self.model.test_on_batch(x, y) predict_outputs = self.model.predict_on_batch(x) predicts = np.argmax(predict_outputs, axis=-1) losses.append(outputs[0]) if i % 5 == 0: print( "%s Epoch [%d/%d] Step [%d/%d]" % (loader_name, epoch + 1, args.n_epoch, i, n_step)) for name, value in zip(self.model.metrics_names, outputs): print(name, value) running_metrics.update(trues, predicts) score, class_iou = running_metrics.get_scores() for k, v in score.items(): print(k, v) writer.add_scalar('%s/loss' % loader_name, np.mean(losses), epoch) writer.add_scalar('%s/acc' % loader_name, score['Overall Acc: \t'], epoch) writer.add_scalar('%s/iou' % loader_name, score['Mean IoU : \t'], epoch) running_metrics.reset() if loader_name == 'val': if score['Mean IoU : \t'] >= best_iou: best_iou = score['Mean IoU : \t'] self.model.save(checkpoint_path) if epoch % (1 + args.n_epoch // 10) == 0: print('write image to tensorboard' + '.' * 50) idx = np.random.choice(predicts.shape[0]) writer.add_image('val/images', x[idx, :, :, :], epoch) writer.add_image('val/predicts', torch.from_numpy(predicts[idx, :, :]), epoch) writer.add_image('val/trues', torch.from_numpy(trues[idx, :, :]), epoch) diff_img = ( predicts[idx, :, :] == trues[idx, :, :]).astype( np.uint8) writer.add_image('val/difference', torch.from_numpy(diff_img), epoch) writer.close()
def do_train_or_val(net, args=None, train_loader=None, val_loader=None): gpu_config = tf.ConfigProto() # config.gpu_options.per_process_gpu_memory_fraction = 0.3 gpu_config.gpu_options.allow_growth = True session = tf.Session(config=gpu_config) KTF.set_session(session) session.run(tf.global_variables_initializer()) if args is None: args = net.config.training metrics = net.get_metrics(net.class_number) opt = net.get_optimizer(args) net.model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=metrics) if train_loader is None and val_loader is None: train_dataset = dataset_generalize(net.config.dataset, split='train', bchw=False) train_loader = TD.DataLoader(dataset=train_dataset, batch_size=net.config.dataset.batch_size, shuffle=True, drop_last=False) val_dataset = dataset_generalize(net.config.dataset, split='val', bchw=False) val_loader = TD.DataLoader(dataset=val_dataset, batch_size=net.config.dataset.batch_size, shuffle=True, drop_last=False) running_metrics = runningScore(net.class_number) time_str = time.strftime("%Y-%m-%d___%H-%M-%S", time.localtime()) log_dir = os.path.join(args.log_dir, net.name, args.dataset_name, args.note, time_str) checkpoint_path = os.path.join( log_dir, "{}_{}_best_model.pkl".format(net.name, args.dataset_name)) os.makedirs(log_dir, exist_ok=True) writer = SummaryWriter(log_dir=log_dir) config = net.config config_str = json.dumps(config, indent=2, sort_keys=True).replace( '\n', '\n\n').replace(' ', '\t') writer.add_text(tag='config', text_string=config_str) # write config to config.txt config_path = os.path.join(log_dir, 'config.txt') config_file = open(config_path, 'w') json.dump(config, config_file, sort_keys=True) config_file.close() best_iou = 0.6 loaders = [train_loader, val_loader] loader_names = ['train', 'val'] for epoch in range(args.n_epoch): for loader, loader_name in zip(loaders, loader_names): if loader is None: continue if loader_name == 'val': if epoch % 5 != 0: continue print(loader_name + '.' * 50) n_step = len(loader) losses = [] for i, (images, labels) in enumerate(loader): x = images.data.numpy() trues = labels.data.numpy() y = to_categorical(trues, net.class_number) if loader_name == 'train': outputs = net.model.train_on_batch(x, y) else: outputs = net.model.test_on_batch(x, y) predict_outputs = net.model.predict_on_batch(x) predicts = np.argmax(predict_outputs, axis=-1) losses.append(outputs[0]) if epoch % 5 == 0: print('keras metrics as follow:', '*' * 30) print("%s Epoch [%d/%d] Step [%d/%d]" % (loader_name, epoch + 1, args.n_epoch, i, n_step)) for name, value in zip(net.model.metrics_names, outputs): print(name, value) print('running metrics as follow:', '*' * 30) running_metrics.update(trues, predicts) score, class_iou = running_metrics.get_scores() for k, v in score.items(): print(k, v) if epoch % 5 == 0: writer.add_scalar('%s/loss' % loader_name, np.mean(losses), epoch) writer.add_scalar('%s/acc' % loader_name, score['Overall Acc: \t'], epoch) writer.add_scalar('%s/iou' % loader_name, score['Mean IoU : \t'], epoch) running_metrics.reset() if loader_name == 'val': if score['Mean IoU : \t'] >= best_iou: best_iou = score['Mean IoU : \t'] net.model.save(checkpoint_path) # if epoch % (1+args.n_epoch//10) == 0: # print('write image to tensorboard'+'.'*50) # idx=np.random.choice(predicts.shape[0]) # writer.add_image('val/images',x[idx,:,:,:],epoch) # writer.add_image('val/predicts', torch.from_numpy(predicts[idx,:,:]), epoch) # writer.add_image('val/trues', torch.from_numpy(trues[idx,:,:]), epoch) # diff_img=(predicts[idx,:,:]==trues[idx,:,:]).astype(np.uint8) # writer.add_image('val/difference', torch.from_numpy(diff_img), epoch) print('best iou is', best_iou) writer.close()