示例#1
0
    def __init__(self, args):
        self.args = args
        self.tr_global_step = 1
        self.val_global_step = 1
        self.best_mIoU = 0
        self.num_classes = CityScapes.num_classes
        self.mode = args.mode
        self.segmentation = args.segmentation
        self.reconstruct = args.reconstruct

        self.model = get_model(args)
        self.best_model = copy.deepcopy(self.model)
        self.optimizer = get_optimizer(self.model, args)
        self.summary = TensorboardSummary(args)

        if not args.trainval:
            self.train_loader, self.val_loader = make_data_loader(
                args, 'train'), make_data_loader(args, 'val')
        else:
            self.train_loader, self.val_loader = make_data_loader(
                args, 'trainval'), make_data_loader(args, 'test')

        self.class_weights = get_class_weights(
            self.train_loader, self.num_classes,
            args.weighting_mode) if args.use_class_weights else None
        self.criterion = get_loss_function(args.loss_type, self.class_weights)
        if self.reconstruct:
            self.reconstruction_criterion = get_reconstruction_loss_function(
                args.reconstruct_loss_type)
        self.scheduler = LR_Scheduler(args.lr_policy, args.lr, args.epochs,
                                      len(self.train_loader))
        self.evaluator = Evaluator(self.num_classes)
示例#2
0
def text_clf():
    train_set = Textset()
    test_set = Textset(type="test")
    
    print(train_set.target)
    print(train_set.data.shape)

    network = nt.Network(train_set,cost_type="MSE")

    # Hiden layer
    network.append_linear_layer(16)
    network.append_activation_layer(type="ReLU")
    network.append_linear_layer(8)
    network.append_activation_layer(type="ReLU")
    network.append_linear_layer(4)

    # network.show_structure()
    network.train_repeatly(times=1000  ,print_cost=True)



    print(network.final_result)
    print(train_set.target)
    #print(network.final_result)
    ploter.plot_cost(network)

    self_evaluator = Evaluator(network,network.dataset)
    evaluator = Evaluator(network,test_set)
    self_evaluator.clf_evaluate()
    evaluator.clf_evaluate()
示例#3
0
文件: predict.py 项目: muye5/YOLOv3
def predict(args):
    image_paths = load_image(args)

    sess, input_image, output_nodes, config = load_model(args)
    evaluator = Evaluator(config=config, augment=None, params={
        'obj_thresh': 1e-2, 'nms_thresh': 0.45,
        'images': None, 'annotations': None, 'shapes': None, 'loss': None, 'output_nodes': None
    })

    output_dir = args.output_dir
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    out_lst = []
    for name in config['model']['labels']:
        f = os.path.join(output_dir, 'comp3_det_test_' + name + '.txt')
        out = open(f, 'w')
        out_lst.append(out)

    net_h, net_w = config['model']['input_size'], config['model']['input_size']

    total_image_cnt, batch_size, idx = len(image_paths), 20, 0
    images, shapes, names = [], [], []
    for image_path in tqdm(image_paths):
        idx += 1

        img = cv2.imread(image_path)
        images.append(img)
        shapes.append(img.shape)

        basename = os.path.splitext(os.path.basename(image_path))[0]
        names.append(basename)

        if idx % batch_size == 0 or idx == total_image_cnt:
            batch_output, batch_input = inference(sess, input_image, output_nodes, images, net_h, net_w)
            shapes = np.asarray(shapes)
            batch_boxes = evaluator.get_boxes(shapes, batch_input, batch_output, coco=True,
                                              nms='nms', cls='softmax')

            for k, class_boxes in enumerate(batch_boxes):
                for i, boxes in enumerate(class_boxes):
                    if len(boxes) == 0:
                        continue
                    for box in boxes:
                        left, top, right, bottom, score = box[:5]
                        out_lst[i].write('{} {:.6f} {:.6f} {:.6f} {:.6f} {:.6f}\n'.format(
                            names[k], score, left + 1, top + 1, right + 1, bottom + 1))
            images, shapes, names = [], [], []

    for out in out_lst:
        out.close()
示例#4
0
def main():
    opt = TestOptions().parse()
    opt.serial_batches = True  # no shuffle
    opt.no_flip = True  # no flip
    visualize_eval = opt.visualize_eval
    opt.process_rank = -1

    data_loader = CreateDataLoader(opt)
    dataset = data_loader.load_data()
    assert (len(dataset.dataset.all_datasets) == 1)
    test_dataset = dataset.dataset.all_datasets[0]
    evaluator = Evaluator(test_dataset.data_list, opt.model_root)

    test_res_dir = 'evaluate_results'
    ry_utils.renew_dir(test_res_dir)

    epoch = 'latest'
    evaluator.clear()
    opt.which_epoch = str(epoch)
    model = DCTModel(opt)
    model.eval()

    timer = Timer(len(dataset))
    for i, data in enumerate(dataset):
        model.set_input(data)
        model.test()
        losses = model.compute_loss()
        pred_res = model.get_pred_result()
        data_idxs = data['index'].numpy()
        evaluator.update(data_idxs, losses, pred_res)
        timer.click(i)

    evaluator.remove_redunc()

    res_pkl_file = osp.join(test_res_dir, 'estimator_{}.pkl'.format(epoch))
    evaluator.save_to_pkl(res_pkl_file)
    backup_pkl_file = osp.join(test_res_dir, 'estimator.pkl')
    shutil.copy2(res_pkl_file, backup_pkl_file)

    print("Test of epoch: {} complete".format(epoch))
    print("PVE:{}".format(evaluator.pve))
    print("MPJPE:{}".format(evaluator.mpjpe))
    print("PVE-TPose:{}".format(evaluator.pve_tpose))
    print('------------------')
    sys.stdout.flush()
示例#5
0
def mnist_clf():

    mnist_set = Mnistset(type="train",selection_range=(0,5000))

    print("Load mnist data done!")

    print(mnist_set.data.shape)
    print(mnist_set.target)
    
    
    lr = 0.0001
    network = nt.Network(mnist_set,cost_type="MSE")
    network.append_linear_layer(32,learning_rate=lr)
    network.append_activation_layer(type="Sigmoid")
    network.append_linear_layer(16,learning_rate=lr)
    network.append_activation_layer(type="Sigmoid")
    network.append_linear_layer(10,learning_rate=lr)
    network.append_activation_layer(type="Sigmoid")	
    network.train_repeatly(5000,print_cost=True)

    ploter.plot_cost(network)
    network.dataset.selection_range = (10000,20000)
    self_evaulator = Evaluator(network,network.dataset)
    self_evaulator.clf_evaluate()
示例#6
0
def sin_reg():
    sin_set = Sineset()
    sin_test = Sineset(type="test")

    network = nt.Network(sin_set, cost_type="MSE")

    # Hiden layer
    network.append_linear_layer(97)
    network.append_activation_layer(type="Tanh")
    network.append_linear_layer(1)

    # network.show_structure()
    network.train_repeatly(times=10000, print_cost=True)

    ploter.plot_cost(network)
    
    self_evaluator = Evaluator(network,network.dataset)
    evaluator = Evaluator(network,sin_test)
    self_evaluator.reg_evaluate()
    evaluator.reg_evaluate()
示例#7
0
    if len(opt.gpu_ids) > 0:
        torch.cuda.manual_seed(10)
    # train dataset
    train_dataset = create_dataset(
        opt)  # create a dataset given opt.dataset_mode and other options
    train_size = len(train_dataset)  # get the number of images in the dataset.
    print('The number of training images = %d. Trainset: %s' %
          (train_size, opt.dataroot))

    opt.print_freq = train_size // 10  # print 10 times for each epoch
    opt.save_latest_freq = train_size // opt.batch_size * opt.batch_size  # save latest model and evaluate the performance after every epoch

    # test dataset
    opt.phase = 'test'
    test_dataset = create_dataset(opt)
    evaluator = Evaluator(opt)
    test_size = len(test_dataset)
    print('The number of test images = %d. Testset: %s' %
          (test_size, opt.dataroot))
    opt.phase = 'train'

    model = create_model(opt)
    model.setup(opt)
    visualizer = Visualizer(opt)
    total_iters = 0
    best_res = 0.0

    for epoch in range(opt.epoch_count, opt.niter + opt.niter_decay + 1):
        epoch_start_time = time.time()
        iter_data_time = time.time()
        epoch_iter = 0
示例#8
0
from data import create_dataset
from models import create_model
from tqdm import tqdm
from util.evaluator import Evaluator

if __name__ == '__main__':
    opt = TestOptions().parse()
    opt.num_threads = 1
    opt.batch_size = 1
    opt.serial_batches = True
    test_dataset = create_dataset(opt)
    test_size = len(test_dataset)
    print('The number of test images = %d. Testset: %s' %
          (test_size, opt.dataroot))
    opt.num_test = test_size
    evaluator = Evaluator(opt)

    model = create_model(opt)
    model.setup(opt)

    #save_dir = os.path.join(os.getcwd(), opt.results_dir, opt.name, opt.dataroot.split('/')[-1], '%s_%s' % (opt.phase, opt.epoch))
    #if not os.path.exists(save_dir):
    #    os.makedirs(save_dir)

    model.eval()
    evaluator.reset()
    eval_start_time = time.time()
    for data in tqdm(test_dataset):
        model.set_input(data)
        preds = model.test()
        evaluator.update(preds)
示例#9
0
class Trainer(object):
    def __init__(self, args):
        self.args = args
        self.tr_global_step = 1
        self.val_global_step = 1
        self.best_mIoU = 0
        self.num_classes = CityScapes.num_classes
        self.mode = args.mode
        self.segmentation = args.segmentation
        self.reconstruct = args.reconstruct

        self.model = get_model(args)
        self.best_model = copy.deepcopy(self.model)
        self.optimizer = get_optimizer(self.model, args)
        self.summary = TensorboardSummary(args)

        if not args.trainval:
            self.train_loader, self.val_loader = make_data_loader(
                args, 'train'), make_data_loader(args, 'val')
        else:
            self.train_loader, self.val_loader = make_data_loader(
                args, 'trainval'), make_data_loader(args, 'test')

        self.class_weights = get_class_weights(
            self.train_loader, self.num_classes,
            args.weighting_mode) if args.use_class_weights else None
        self.criterion = get_loss_function(args.loss_type, self.class_weights)
        if self.reconstruct:
            self.reconstruction_criterion = get_reconstruction_loss_function(
                args.reconstruct_loss_type)
        self.scheduler = LR_Scheduler(args.lr_policy, args.lr, args.epochs,
                                      len(self.train_loader))
        self.evaluator = Evaluator(self.num_classes)

    def training(self, epoch):
        train_loss = 0.0
        if self.reconstruct:
            train_reconstruction_loss = 0.0
        self.model.train()
        tbar = tqdm(self.train_loader)
        num_img_tr = len(self.train_loader)

        for i, sample in enumerate(tbar):
            with torch.autograd.set_detect_anomaly(True):
                image = sample[0]
                target = sample[1]

                if self.args.cuda:
                    image, target = image.cuda(), target.cuda()

                reconstruction_target = None
                if self.reconstruct:
                    reconstruction_target = sample[2].cuda(
                    ) if self.args.cuda else sample[2]

                self.scheduler(self.optimizer, i, epoch, self.best_mIoU)
                self.optimizer.zero_grad()

                segmentation_output, reconstruction_output = self.model(image)

                reconstruction_output_GT, reconstruction_target_GT, segmentation_output_GT = None, None, None
                if 'sequence' in self.mode:
                    image_GT = image[:, self.args.timesteps - 1, :, :, :]

                    if self.segmentation:
                        segmentation_output_GT = segmentation_output[:,
                                                                     self.args.
                                                                     timesteps -
                                                                     1, :, :, :]

                    if self.reconstruct:
                        reconstruction_output_GT = reconstruction_output[:,
                                                                         self.
                                                                         args.
                                                                         timesteps
                                                                         -
                                                                         1, :, :, :]
                        reconstruction_target_GT = reconstruction_target[:,
                                                                         self.
                                                                         args.
                                                                         timesteps
                                                                         -
                                                                         1, :, :, :]
                else:
                    image_GT, segmentation_output_GT = image, segmentation_output
                    if self.mode == 'fbf-1234':
                        image_GT = image_GT[:, :3, :, :]

                if self.segmentation:
                    segmentation_loss = self.criterion(segmentation_output_GT,
                                                       target.long())
                    total_loss = segmentation_loss

                if self.reconstruct:
                    reconstruction_loss = self.reconstruction_criterion(
                        reconstruction_output, reconstruction_target)
                    train_reconstruction_loss += reconstruction_loss.item()
                    reconstruction_loss = self.args.reconstruct_loss_coeff * reconstruction_loss
                    total_loss = reconstruction_loss

                if self.segmentation and self.reconstruct:
                    total_loss = segmentation_loss + reconstruction_loss

                total_loss.backward()
                #plot_grad_flow(self.model)

                # Show 10 * 3 inference results each epoch
                if i % (num_img_tr // 10) == 0:
                    self.summary.visualize_image(self.tr_global_step,
                                                 image_GT,
                                                 target,
                                                 segmentation_output_GT,
                                                 reconstruction_output_GT,
                                                 reconstruction_target_GT,
                                                 split="train")
                    self.tr_global_step += 1

                if self.args.clip > 0:
                    if self.args.gpu_ids:
                        torch.nn.utils.clip_grad_norm_(
                            self.model.module().parameters(), self.args.clip)
                    else:
                        torch.nn.utils.clip_grad_norm_(self.model.parameters(),
                                                       self.args.clip)
                self.optimizer.step()

                train_loss += total_loss.item()
                tbar.set_description('Train loss: %.3f' % (train_loss /
                                                           (i + 1)))

        self.summary.add_scalar('train/total_loss_epoch', train_loss, epoch)
        if self.reconstruct:
            self.summary.add_scalar('train/total_recon_loss_epoch',
                                    train_reconstruction_loss, epoch)
            self.summary.add_scalar('train/total_seg_loss_epoch',
                                    train_loss - train_reconstruction_loss,
                                    epoch)
        print('[Epoch: %d, numImages: %5d]' %
              (epoch, i * self.args.batch_size + image.data.shape[0]))

    def validation(self, epoch):
        test_loss = 0.0
        test_reconstruction_loss = 0.0
        self.model.eval()
        vbar = tqdm(self.val_loader)
        num_img_val = len(self.val_loader)

        labels = []
        outputs = []

        for i, sample in enumerate(vbar):  # inner loop within one epoch
            image = sample[0]
            target = sample[1]

            if self.args.cuda:
                image, target = image.cuda(), target.cuda()

            reconstruction_target = None
            if self.reconstruct:
                reconstruction_target = sample[2].cuda(
                ) if self.args.cuda else sample[2]

            with torch.no_grad():
                segmentation_output, reconstruction_output = self.model(image)

            reconstruction_output_GT, reconstruction_target_GT, segmentation_output_GT = None, None, None
            if 'sequence' in self.mode:
                image_GT = image[:, self.args.timesteps - 1, :, :, :]

                if self.segmentation:
                    segmentation_output_GT = segmentation_output[:, self.args.
                                                                 timesteps -
                                                                 1, :, :, :]

                if self.reconstruct:
                    reconstruction_output_GT = reconstruction_output[:,
                                                                     self.args.
                                                                     timesteps -
                                                                     1, :, :, :]
                    reconstruction_target_GT = reconstruction_target[:,
                                                                     self.args.
                                                                     timesteps -
                                                                     1, :, :, :]
            else:
                image_GT, segmentation_output_GT = image, segmentation_output
                if self.mode == 'fbf-1234':
                    image_GT = image_GT[:, :3, :, :]

            if self.segmentation:
                segmentation_loss = self.criterion(segmentation_output_GT,
                                                   target.long())
                total_loss = segmentation_loss

            if self.reconstruct:
                reconstruction_loss = self.reconstruction_criterion(
                    reconstruction_output, reconstruction_target)
                test_reconstruction_loss += reconstruction_loss.item()
                reconstruction_loss = self.args.reconstruct_loss_coeff * reconstruction_loss
                total_loss = reconstruction_loss

            if self.segmentation and self.reconstruct:
                total_loss = reconstruction_loss + segmentation_loss

            # Show 10 * 3 inference results each epoch
            if i % (num_img_val // 10) == 0:
                self.summary.visualize_image(self.val_global_step,
                                             image_GT,
                                             target,
                                             segmentation_output_GT,
                                             reconstruction_output_GT,
                                             reconstruction_target_GT,
                                             split="val")
                self.val_global_step += 1

            test_loss += total_loss.item()
            vbar.set_description('Val loss: %.3f' % (test_loss / (i + 1)))

            if self.segmentation:
                outputs.append(
                    torch.argmax(segmentation_output_GT, dim=1).cpu().numpy())
                labels.append(target.cpu().numpy())

        if self.segmentation:
            acc, acc_cls, mIoU, IoU_class, fwavacc = self.evaluator.evaluate(
                outputs, labels)
            self.summary.add_results(epoch,
                                     mIoU,
                                     acc,
                                     acc_cls,
                                     fwavacc,
                                     test_loss,
                                     split="val")

        if self.reconstruct:
            self.summary.add_scalar('val/total_recon_loss_epoch',
                                    test_reconstruction_loss, epoch)
            self.summary.add_scalar('val/total_seg_loss_epoch',
                                    test_loss - test_reconstruction_loss,
                                    epoch)

        if self.segmentation and mIoU > self.best_mIoU:
            self.best_mIoU = mIoU
            self.best_model = copy.deepcopy(self.model)

        if self.segmentation:
            print(20 * "-")
            for i in range(len(IoU_class)):
                print("IoU for class " + CityScapes.classes[i] + " is: " +
                      str(IoU_class[i]))
            print(20 * "-")
            print("Accuray: ", acc)
            print("Class accuracy: ", acc_cls)
            print("FwIoU:", fwavacc)
            print("Mean IoU: ", mIoU)
            print("Best IoU: ", self.best_mIoU)

    def visualization(self, split):
        print(20 * "-")
        print("Started final Visualization")
        print(20 * "-")

        visualization_loss = 0.0
        visualization_reconstruction_loss = 0.0

        self.best_model.eval()
        if split == 'test' or split == 'demoVideo':
            vis_bar = tqdm(make_data_loader(self.args, split))
        else:
            vis_bar = tqdm(self.val_loader)

        labels = []
        outputs = []
        paths = []

        for i, sample in enumerate(vis_bar):  # inner loop within one epoch
            image = sample[0]
            target = sample[1]

            if self.args.cuda:
                image, target = image.cuda(), target.cuda()

            reconstruction_target = None
            if self.reconstruct:
                reconstruction_target = sample[2].cuda(
                ) if self.args.cuda else sample[2]

            with torch.no_grad():
                segmentation_output, reconstruction_output = self.best_model(
                    image)

            reconstruction_output_GT, reconstruction_target_GT = None, None
            if 'sequence' in self.mode:
                image_GT = image[:, self.args.timesteps - 1, :, :, :]
                segmentation_output_GT = segmentation_output[:, self.args.
                                                             timesteps -
                                                             1, :, :, :]
                if self.reconstruct:
                    reconstruction_output_GT = reconstruction_output[:,
                                                                     self.args.
                                                                     timesteps -
                                                                     1, :, :, :]
                    reconstruction_target_GT = reconstruction_target[:,
                                                                     self.args.
                                                                     timesteps -
                                                                     1, :, :, :]
            else:
                image_GT, segmentation_output_GT = image, segmentation_output
                if self.mode == 'fbf-1234':
                    image_GT = image_GT[:, :3, :, :]

            segmentation_loss = self.criterion(segmentation_output_GT,
                                               target.long())

            if self.reconstruct:
                reconstruction_loss = self.reconstruction_criterion(
                    reconstruction_output, reconstruction_target)
                visualization_reconstruction_loss += reconstruction_loss.item()
                reconstruction_loss = self.args.reconstruct_loss_coeff * reconstruction_loss
                total_loss = reconstruction_loss + segmentation_loss
            else:
                total_loss = segmentation_loss

            visualization_loss += total_loss.item()
            vis_bar.set_description('Visualization loss: %.3f' %
                                    (visualization_loss / (i + 1)))

            outputs.append(torch.argmax(segmentation_output_GT, dim=1).cpu())
            labels.append(target.cpu())

            if split == 'test' or split == 'demoVideo':
                paths.append(sample[2][0])

        if split not in ['test', 'demoVideo']:
            acc, acc_cls, mIoU, IoU_class, fwavacc = self.evaluator.evaluate(
                [output.numpy() for output in outputs],
                [label.numpy() for label in labels])

        outputs = torch.squeeze(torch.stack(outputs))
        labels = torch.squeeze(torch.stack(labels))
        self.summary.save_visualization_images(outputs, labels, paths)

        if split not in ['test', 'demoVideo']:
            print(20 * "-")
            for i in range(len(IoU_class)):
                print("IoU for class " + CityScapes.classes[i] + " is: " +
                      str(IoU_class[i]))
            print(20 * "-")
            print("Accuray: ", acc)
            print("Class accuracy: ", acc_cls)
            print("FwIoU:", fwavacc)
            print("Mean IoU: ", mIoU)
            print("Best IoU: ", self.best_mIoU)

    def save_network(self):
        self.summary.save_network(self.best_model)

    def load_network(self):
        self.best_model = get_model(self.args)
        self.best_model.load_state_dict(torch.load(''))
示例#10
0
        sys.stdout.flush()


if __name__ == '__main__':

    opt = TestOptions().parse()
    opt.serial_batches = True  # no shuffle
    opt.no_flip = True  # no flip
    visualize_eval = opt.visualize_eval
    opt.process_rank = -1

    data_loader = CreateDataLoader(opt)
    dataset = data_loader.load_data()
    assert (len(dataset.dataset.all_datasets) == 1)
    test_dataset = dataset.dataset.all_datasets[0]
    evaluator = Evaluator(test_dataset.data_list)

    test_res_dir = opt.eval_res_dir
    ry_utils.renew_dir(test_res_dir)

    evaluator.clear()
    model = DCTModel(opt)
    model.eval()

    res_pkl_file = osp.join(test_res_dir, 'eval_result.pkl')
    test_img_dir = osp.join(test_res_dir, 'images')
    os.makedirs(test_img_dir)

    timer = Timer(len(dataset))
    for i, data in enumerate(dataset):
        model.set_input(data)
示例#11
0
def train(server, cluster):
    is_chief = (flags.task_index == 0)
    config, train_file_path, val_file_path = get_data_path(flags)
    tf_height, tf_width, tf_channel = config['model']['tf_input_size']
    accumulate = config['train']['accumulate'] if 'accumulate' in config[
        'train'] else 8

    worker_device = '/job:worker/task:{}'.format(flags.task_index)
    with tf.device(worker_device):
        train_images, train_annotations, train_shapes = load_sample(
            flags,
            config,
            train_file_path,
            width=tf_width,
            height=tf_height,
            channel=tf_channel)
        val_images, val_annotations, val_shapes = load_sample(
            flags,
            config,
            val_file_path,
            width=tf_width,
            height=tf_height,
            channel=tf_channel,
            capacity=500,
            min_after_dequeue=10,
            num_threads=2)

    augment = Augment(config)
    with tf.device('/job:worker/task:{}/cpu:0'.format(flags.task_index)):
        train_input = tf.py_func(
            augment, [train_images, train_annotations, train_shapes, True],
            [tf.float32] * 5,
            stateful=True)
        val_input = tf.py_func(
            augment, [val_images, val_annotations, val_shapes, False],
            [tf.float32] * 5 + [tf.uint8, tf.string, tf.int64],
            stateful=False)

    with tf.device(
            tf.train.replica_device_setter(worker_device=worker_device,
                                           cluster=cluster)):
        model = create_model(config)
        if is_chief and not flags.restore:
            init_ops = model.init_weight()

    validator = Evaluator(
        config, augment, {
            'loss': model.loss,
            'data': val_input,
            'output_nodes': model.output_nodes,
            'nms_func': 'fast_nms',
            'cls_func': 'softmax'
        })
    hook_dict = create_hook(
        config, {
            'lr': model.learning_rate,
            'train_loss': model.loss,
            'validator': validator
        })
    total_ops = hook_dict['dependent_ops'] + [
        model.accu_ops, model.update_ops, model.loss, model.global_step
    ]

    with tf.train.MonitoredTrainingSession(
            master=server.target,
            hooks=hook_dict['hooks'],
            chief_only_hooks=hook_dict['chief_only_hooks'],
            is_chief=is_chief) as sess:
        if is_chief and not flags.restore:
            sess.run(init_ops)
        elif is_chief and flags.restore:
            model.restore_model(sess, hook_dict['restore_saver'])
        while not sess.should_stop():
            for _ in range(accumulate):
                if not sess.should_stop():
                    t_input = sess._tf_sess().run(train_input)
                if not sess.should_stop():
                    sess.run(total_ops,
                             feed_dict={
                                 'input_image:0': t_input[0],
                                 'true_boxes:0': t_input[1],
                                 'true_yolo_1:0': t_input[2],
                                 'true_yolo_2:0': t_input[3],
                                 'true_yolo_3:0': t_input[4],
                                 'phase:0': True,
                             })

            if not sess.should_stop():
                sess.run([model.train_op, model.global_step])

            if not sess.should_stop():
                sess._tf_sess().run([model.zero_ops, model.global_step])
示例#12
0
文件: predict.py 项目: muye5/YOLOv3
def predict_coco(args):
    image_paths = load_image(args)

    sess, input_image, output_nodes, config = load_model(args)
    evaluator = Evaluator(config=config, augment=None, params={
        'obj_thresh': 1e-2, 'nms_thresh': 0.45,
        'images': None, 'annotations': None, 'shapes': None, 'loss': None, 'output_nodes': None
    })

    output_dir = args.output_dir
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    output_path = 'detections_test-dev2017_yolov3_results.json'
    output_path = os.path.join(output_dir, output_path)
    out = open(output_path, 'w')

    cat_names = ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat',
                 'traffic light', 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat',
                 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella',
                 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat',
                 'baseball glove', 'skateboard', 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork',
                 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog',
                 'pizza', 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv',
                 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink',
                 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush']
    cat_ids = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27, 28, 31,
               32, 33, 34, 35,36, 37, 38, 39, 40, 41, 42, 43, 44, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
               58, 59, 60, 61, 62, 63, 64, 65, 67, 70, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 84, 85, 86, 87,
               88, 89, 90]
    cat_maps = dict(zip(cat_names, cat_ids))

    out_lst = []
    net_h, net_w = config['model']['input_size'], config['model']['input_size']

    total_image_cnt, batch_size, idx = len(image_paths), 20, 0
    images, shapes, names = [], [], []
    for image_path in tqdm(image_paths):
        idx += 1

        img = cv2.imread(image_path)
        images.append(img)
        shapes.append(img.shape)

        basename = os.path.splitext(os.path.basename(image_path))[0]
        img_id = int(basename.split('_')[-1])
        names.append(img_id)

        if idx % batch_size == 0 or idx == total_image_cnt:
            batch_output, batch_input = inference(sess, input_image, output_nodes, images, net_h, net_w)
            shapes = np.asarray(shapes)
            batch_boxes = evaluator.get_boxes(shapes, batch_input, batch_output, coco=True,
                                              nms='nms', cls='softmax')

            for k, class_boxes in enumerate(batch_boxes):
                for i, boxes in enumerate(class_boxes):
                    if len(boxes) == 0:
                        continue
                    category_id = cat_maps[config['model']['labels'][i]]
                    for box in boxes:
                        left, top, right, bottom, score = box[:5]
                        x = round(left, 4)
                        y = round(top, 4)
                        width = round(right - left, 4)
                        height = round(bottom - top, 4)
                        score = round(float(score), 6)
                        out_lst.append({'image_id': names[k], 'category_id': category_id,
                                        'bbox': [x, y, width, height], 'score': score})
            images, shapes, names = [], [], []

    out.write(json.dumps(out_lst))
    out.close()
示例#13
0
文件: predict.py 项目: muye5/YOLOv3
def evaluate(args):
    global MODEL_FLOPS
    image_paths = load_image(args)
    annotations = load_label(args)

    sess, input_image, output_nodes, config = load_model(args)
    evaluator = Evaluator(config=config, augment=None, params={
        'obj_thresh': 1e-2, 'nms_thresh': 0.45,
        'images': None, 'annotations': None, 'shapes': None, 'loss': None, 'output_nodes': None
    })

    net_h, net_w = config['model']['input_size'], config['model']['input_size']

    all_gts = []
    all_tps = []
    all_fps = []
    all_scores = []

    num_class = len(config['model']['labels'])
    for i in range(num_class):
        all_gts.append(0)
        all_tps.append([])
        all_fps.append([])
        all_scores.append([])

    total_image_cnt, batch_size, idx = len(image_paths), 40, 0
    images, shapes, annos = [], [], []
    for image_path in tqdm(image_paths):
        idx += 1

        basename = os.path.splitext(os.path.basename(image_path))[0]
        if basename not in annotations:
            print('no found {}'.format(basename))
            continue
        anno = np.array(annotations[basename])
        img = cv2.imread(image_path)

        images.append(img)
        shapes.append(img.shape)
        annos.append(anno)

        if idx % batch_size == 0 or idx == total_image_cnt:
            batch_output, batch_input = inference(sess, input_image, output_nodes, images, net_h, net_w)
            batch_boxes = evaluator.get_boxes(np.asarray(shapes), batch_input, batch_output, coco=False,
                                              nms='nms', cls='softmax')
            labels = evaluator.get_labels(np.asarray(annos))
            tps, fps, scores, gts, _, _ = evaluator.get_tp_fp_case(labels, batch_boxes)

            for i in range(num_class):
                all_gts[i] += gts[i]
                all_tps[i] += tps[i]
                all_fps[i] += fps[i]
                all_scores[i] += scores[i]

            images, shapes, annos = [], [], []

    all_ap = evaluator.evaluate(all_gts, all_tps, all_fps, all_scores)
    m_ap, num = 0, 0
    for i, ap in enumerate(all_ap):
        print('{:15} AP = {:.6f}'.format(config['model']['labels'][i], ap))
        if ap > -1:
            m_ap += ap
            num += 1
    if num > 0:
        m_ap /= float(num)
    print('mAP = {}'.format(m_ap))
    if MODEL_FLOPS is not None:
        print('Model FLOPS = {:.2f} Bn'.format(MODEL_FLOPS / 1e+9))