Example #1
0
    def __init__(self,
                 FLAGS,
                 is_training: bool,
                 dataset_type: str = "converted_coco"):
        self.tiny = FLAGS.tiny
        self.strides, self.anchors, NUM_CLASS, XYSCALE = utils.load_config(
            FLAGS)
        self.dataset_type = dataset_type

        self.annot_path = (cfg.TRAIN.ANNOT_PATH
                           if is_training else cfg.TEST.ANNOT_PATH)
        self.input_sizes = (cfg.TRAIN.INPUT_SIZE
                            if is_training else cfg.TEST.INPUT_SIZE)
        self.batch_size = (cfg.TRAIN.BATCH_SIZE
                           if is_training else cfg.TEST.BATCH_SIZE)
        self.data_aug = cfg.TRAIN.DATA_AUG if is_training else cfg.TEST.DATA_AUG

        self.train_input_sizes = cfg.TRAIN.INPUT_SIZE
        self.classes = utils.read_class_names(cfg.YOLO.CLASSES)
        self.num_classes = len(self.classes)
        self.anchor_per_scale = cfg.YOLO.ANCHOR_PER_SCALE
        self.max_bbox_per_scale = 150

        self.annotations = self.load_annotations()
        self.num_samples = len(self.annotations)
        self.num_batchs = int(np.ceil(self.num_samples / self.batch_size))
        self.batch_count = 0
Example #2
0
    def __init__(self):
        self.classes = utils.read_class_names(cfg.YOLO.CLASSES)
        self.moving_ave_decay = cfg.YOLO.MOVING_AVE_DECAY
        self.weights_path = './checkpoint/'
        ckpt = tf.train.get_checkpoint_state(self.weights_path)
        self.input_size = cfg.TRAIN.INPUT_SIZE
        self.model_size = cfg.YOLO.MODEL_SIZE

        with tf.name_scope('input'):
            self.input = tf.placeholder(
                dtype=tf.float32,
                shape=[1, self.input_size, self.input_size, 3],
                name='input')
            self.training = tf.placeholder(dtype=bool, name='trainable')

        model = YoloV3(inputs=self.input, training=self.training)
        self.pred_bbox = model.pred_bbox

        with tf.name_scope('ema'):
            ema_obj = tf.train.ExponentialMovingAverage(self.moving_ave_decay)

        self.sess = tf.Session(config=tf.ConfigProto(
            allow_soft_placement=True))
        self.sess.run(tf.global_variables_initializer())
        self.saver = tf.train.Saver(ema_obj.variables_to_restore())
        self.saver.restore(self.sess, ckpt.model_checkpoint_path)
def compute_loss(pred, conv, label, bboxes, i=0, CLASSES=YOLO_COCO_CLASSES):
    NUM_CLASS = len(read_class_names(CLASSES))
    conv_shape = tf.shape(conv)
    batch_size = conv_shape[0]
    output_size = conv_shape[1]
    input_size = STRIDES[i] * output_size
    conv = tf.reshape(conv,
                      (batch_size, output_size, output_size, 3, 5 + NUM_CLASS))

    conv_raw_conf = conv[:, :, :, :, 4:5]
    conv_raw_prob = conv[:, :, :, :, 5:]

    pred_xywh = pred[:, :, :, :, 0:4]
    pred_conf = pred[:, :, :, :, 4:5]

    label_xywh = label[:, :, :, :, 0:4]
    respond_bbox = label[:, :, :, :, 4:5]
    label_prob = label[:, :, :, :, 5:]

    giou = tf.expand_dims(bbox_giou(pred_xywh, label_xywh), axis=-1)
    input_size = tf.cast(input_size, tf.float32)

    bbox_loss_scale = 2.0 - 1.0 * label_xywh[:, :, :, :,
                                             2:3] * label_xywh[:, :, :, :,
                                                               3:4] / (
                                                                   input_size**
                                                                   2)
    giou_loss = respond_bbox * bbox_loss_scale * (1 - giou)

    iou = bbox_iou(pred_xywh[:, :, :, :, np.newaxis, :],
                   bboxes[:, np.newaxis, np.newaxis, np.newaxis, :, :])
    # Find the value of IoU with the real box The largest prediction box
    max_iou = tf.expand_dims(tf.reduce_max(iou, axis=-1), axis=-1)

    # If the largest iou is less than the threshold, it is considered that the prediction box contains no objects, then the background box
    respond_bgd = (1.0 - respond_bbox) * tf.cast(
        max_iou < YOLO_IOU_LOSS_THRESH, tf.float32)

    conf_focal = tf.pow(respond_bbox - pred_conf, 2)

    # Calculate the loss of confidence
    # we hope that if the grid contains objects, then the network output prediction box has a confidence of 1 and 0 when there is no object.
    conf_loss = conf_focal * (
        respond_bbox * tf.nn.sigmoid_cross_entropy_with_logits(
            labels=respond_bbox, logits=conv_raw_conf) +
        respond_bgd * tf.nn.sigmoid_cross_entropy_with_logits(
            labels=respond_bbox, logits=conv_raw_conf))

    prob_loss = respond_bbox * tf.nn.sigmoid_cross_entropy_with_logits(
        labels=label_prob, logits=conv_raw_prob)

    giou_loss = tf.reduce_mean(tf.reduce_sum(giou_loss, axis=[1, 2, 3, 4]))
    conf_loss = tf.reduce_mean(tf.reduce_sum(conf_loss, axis=[1, 2, 3, 4]))
    prob_loss = tf.reduce_mean(tf.reduce_sum(prob_loss, axis=[1, 2, 3, 4]))

    return giou_loss, conf_loss, prob_loss
Example #4
0
def main(_argv):
    config = ConfigProto()
    # config.gpu_options.allow_growth = True
    session = InteractiveSession(config=config)
    
    # model_module = sh_digit
    # if FLAGS.model == 'sh_digit':
    #     model_module = sh_digit

    # STRIDES = model_module.STRIDES
    # ANCHORS = model_module.ANCHORS
    # NUM_CLASS = model_module.NUM_CLASS
    # XYSCALE = model_module.XYSCALE

    original_image = cv2.imread(FLAGS.image_path)
    original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB)

    image_data = cv2.resize(original_image, tuple(FLAGS.image_size_wh))
    image_data = image_data / 255.

    images_data = []
    for i in range(1):
        images_data.append(image_data)
    images_data = np.asarray(images_data).astype(np.float32)

    saved_model_loaded = tf.saved_model.load(FLAGS.weights, tags=[tag_constants.SERVING])
    infer = saved_model_loaded.signatures['serving_default']
    batch_data = tf.constant(images_data)
    pred_bbox = infer(batch_data)

    for key, value in pred_bbox.items():
        boxes = value[:, :, 0:4]
        pred_conf = value[:, :, 4:]

    print(boxes)

    boxes, scores, classes, valid_detections = tf.image.combined_non_max_suppression(
        boxes=tf.reshape(boxes, (tf.shape(boxes)[0], -1, 1, 4)),
        scores=tf.reshape(
            pred_conf, (tf.shape(pred_conf)[0], -1, tf.shape(pred_conf)[-1])),
        max_output_size_per_class=50,
        max_total_size=50,
        iou_threshold=FLAGS.iou,
        score_threshold=FLAGS.score
    )

    print(boxes.numpy())
    pred_bbox = [boxes.numpy(), scores.numpy(), classes.numpy(), valid_detections.numpy()]

    image = utils.draw_bbox(original_image, pred_bbox,
        classes=utils.read_class_names('cfg/digit/digit.names'))

    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    cv2.imshow('result', image)
    cv2.waitKey()
def complete_yolov3(input_size=416,
                    channels=3,
                    training=False,
                    CLASSES=YOLO_COCO_CLASSES):
    NUM_CLASS = len(read_class_names(CLASSES))
    input_layer = tf.keras.layers.Input([input_size, input_size, channels])

    conv_tensors = YOLOv3(input_layer, NUM_CLASS)

    output_tensors = []
    for i, conv_tensor in enumerate(conv_tensors):
        pred_tensor = decode(conv_tensor, NUM_CLASS, i)
        if training: output_tensors.append(conv_tensor)
        output_tensors.append(pred_tensor)  # len = 3
    #print(output_tensors[0].shape)
    YoloV3 = tf.keras.Model(input_layer, output_tensors)
    return YoloV3
 def __init__(self,
              model_path,
              names_path,
              input_size=416,
              treshold=0.2,
              iou=0.4):
     self.model_path = model_path
     self.names_path = names_path
     self.input_size = input_size
     self.treshold = treshold
     self.iou = iou
     self.classes = utils.read_class_names(self.names_path)
     self.num_class = len(self.classes)
     self.startup_session()
     print(
         f'TensorFlowLiteModel initialization success! model id: {id(self)}; PID: {os.getpid()}'
     )
Example #7
0
def Create_Yolov3(input_size=416, channels=3, training=False, CLASSES=YOLO_COCO_CLASSES):
    NUM_CLASS = len(read_class_names(CLASSES))
    input_layer  = Input([input_size, input_size, channels])

    if TRAIN_YOLO_TINY:
        conv_tensors = YOLOv3_tiny(input_layer, NUM_CLASS)
    else:
        conv_tensors = YOLOv3(input_layer, NUM_CLASS)

    output_tensors = []
    for i, conv_tensor in enumerate(conv_tensors):
        pred_tensor = decode(conv_tensor, NUM_CLASS, i)
        if training: output_tensors.append(conv_tensor)
        output_tensors.append(pred_tensor)

    YoloV3 = tf.keras.Model(input_layer, output_tensors)
    return YoloV3
Example #8
0
    def __init__(self, dataset_type, TEST_INPUT_SIZE=TEST_INPUT_SIZE):
        self.annot_path = TRAIN_ANNOT_PATH if dataset_type == 'train' else TEST_ANNOT_PATH
        self.input_sizes = TRAIN_INPUT_SIZE if dataset_type == 'train' else TEST_INPUT_SIZE
        self.batch_size = TRAIN_BATCH_SIZE if dataset_type == 'train' else TEST_BATCH_SIZE
        self.data_aug = TRAIN_DATA_AUG if dataset_type == 'train' else TEST_DATA_AUG

        self.train_input_sizes = TRAIN_INPUT_SIZE
        self.strides = np.array(YOLO_STRIDES)
        self.classes = read_class_names(TRAIN_CLASSES)
        self.num_classes = len(self.classes)
        self.anchors = (np.array(YOLO_ANCHORS).T / self.strides).T
        self.anchor_per_scale = YOLO_ANCHOR_PER_SCALE
        self.max_bbox_per_scale = YOLO_MAX_BBOX_PER_SCALE

        self.annotations = self.load_annotations(dataset_type)
        self.num_samples = len(self.annotations)
        self.num_batchs = int(np.ceil(self.num_samples / self.batch_size))
        self.batch_count = 0
def get_yolov4(image):
    # inintial config
    ANCHORS = utils.get_anchors('./data/yolov4_anchors.txt', False)
    NUM_CLASS = len(utils.read_class_names('data/coco.names'))
    XYSCALE = [1.2, 1.1, 1.05]
    STRIDES = np.array([8, 16, 32])
    input_size = 608

    original_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    original_image_size = original_image.shape[:2]

    image_data = image_preporcess(np.copy(original_image),
                                  [input_size, input_size])
    image_data = image_data[np.newaxis, ...].astype(np.float32)
    # send grpc request
    request.inputs['input_1'].CopyFrom(
        tf.make_tensor_proto(image_data, dtype=types_pb2.DT_FLOAT))

    result_final = []
    result_future = stub.Predict(request, 10)
    result_1 = result_future.outputs['tf_op_layer_concat_10']
    result_final.append(np.reshape(np.array(result_1.ListFields()[2][1]), \
                                   (1, 76, 76, 3, 6)
                                   ))
    result_2 = result_future.outputs['tf_op_layer_concat_11']
    result_final.append(np.reshape(np.array(result_2.ListFields()[2][1]), \
                                   (1, 38, 38, 3, 6)
                                   ))
    result_3 = result_future.outputs['tf_op_layer_concat_12']
    result_final.append(np.reshape(np.array(result_3.ListFields()[2][1]), \
                                   (1, 19, 19, 3, 6)
                                   ))

    pred_bbox = utils.postprocess_bbbox(result_final, ANCHORS, STRIDES,
                                        XYSCALE)
    bboxes = utils.postprocess_boxes(pred_bbox, original_image_size,
                                     input_size, 0.45)
    bboxes = utils.nms(bboxes, 0.213, method='nms')

    #image = utils.draw_bbox(original_image, bboxes)
    #image = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2RGB)
    #cv2.imwrite('result.jpg', image)

    return bboxes
Example #10
0
def _do_python_eval(res_prefix,
                    imagesetfile,
                    classesfile,
                    output_dir='output'):

    filename = res_prefix + '{:s}.txt'

    cachedir = os.path.join(output_dir, 'annotations_cache')
    aps = []
    # The PASCAL VOC metric changed in 2010
    use_07_metric = False
    # print ('VOC07 metric? ' + ('Yes' if use_07_metric else 'No'))
    if not os.path.isdir(output_dir):
        os.mkdir(output_dir)

    _classes = read_class_names(classesfile)
    total = 0
    for i, cls in enumerate(_classes):
        if cls == '__background__':
            continue

        rec, prec, ap, noccur = my_eval(filename,
                                        imagesetfile,
                                        cls,
                                        cachedir,
                                        ovthresh=0.5,
                                        use_07_metric=use_07_metric)
        aps += [ap]
        total += noccur
        print('AP for {:<10s} = {:.4f} with {:4d} views'.format(
            cls, ap, noccur))
        with open(os.path.join(output_dir, cls + '_pr.pkl'), 'wb') as f:
            cPickle.dump({'rec': rec, 'prec': prec, 'ap': ap}, f)

    print('Mean AP = {:.4f} with total {:4d} views'.format(
        np.mean(aps), total))

    print(' ' * 10, 'Results:')
    print('-' * 30)
    for i, ap in enumerate(aps):
        print('{:<10s}\t{:.3f}'.format(_classes[i], ap))
    print('=' * 30)
    print('{:^10s}\t{:.3f}'.format('Average', np.mean(aps)))
    return [res_prefix, np.mean(aps)]
Example #11
0
    def __init__(self,dataset_type):
        self.annot_path = cfg['train_annot_path'] if  dataset_type == 'train' else cfg['test_annot_path']
        self.input_size = cfg['train_input_size'] if dataset_type == 'train' else cfg['test_input_size']
        self.batch_size = cfg['train_batch_size'] if dataset_type == 'train' else cfg['test_batch_size']
        self.data_aug = cfg['train_data_augement'] if dataset_type == 'train' else cfg['test_data_augement']

        self.train_input_sizes = cfg['train_input_size']
        self.train_input_size = random.choice(self.train_input_sizes)
        self.strides = np.array(cfg['yolo_strides'])
        self.classes = utils.read_class_names(cfg['yolo_classes_names'])
        self.num_classes = len(self.classes)
        self.anchors = np.array(utils.get_anchors(cfg['yolo_anchors']))
        self.anchor_per_scale = cfg['yolo_anchor_per_scale']
        self.max_bbox_per_scale = 150

        self.annotations = self.load_annotation(dataset_type)
        self.num_samples = len(self.annotations)
        self.num_batchs = int(np.ceil(self.num_samples / self.batch_size))
        self.batch_count = 0
Example #12
0
    def __init__(self, opt, datatype="train"):
        self.label_path = utils.read_label_path(opt.data, datatype)
        self.image_path = utils.read_image_path(opt.data, datatype)
        self.classes = utils.read_class_names(opt.data)
        self.input_size = opt.img_size
        self.strides = np.array([8, 16, 32])
        self.output_sizes = self.input_size // self.strides
        self.batch_size = opt.batch_size
        self.num_classes = len(self.classes)

        self.max_bbox_per_scale = 150
        # 读取anchors
        self.anchors = np.array(utils.get_anchors(opt.data))
        self.anchor_per_scale = 3

        self.num_samples = len(self.label_path)
        # np.ceil起到向上取整的作用
        self.num_batchs = int(np.ceil(self.num_samples / self.batch_size))
        self.batch_count = 0
Example #13
0
    def __init__(self, dataset_type):
        self.annot_path = cfg.TRAIN.ANNOT_PATH if dataset_type == 'train' else cfg.TRAIN.ANNOT_PATH
        self.input_sizes = cfg.TRAIN.INPUT_SIZE if dataset_type == 'train' else cfg.TEST.INPUT_SIZE
        self.batch_size = cfg.TRAIN.BATCH_SIZE if dataset_type == 'train' else cfg.TEST.BATCH_SIZE
        self.data_aug = cfg.TRAIN.DATA_AUG if dataset_type == 'train' else cfg.TEST.DATA_AUG

        self.train_input_sizes = cfg.TRAIN.INPUT_SIZE
        self.strides = np.array(cfg.YOLO.STRIDES)
        self.classes = utils.read_class_names(cfg.YOLO.CLASSES)
        self.num_classes = len(self.classes)
        self.anchors = np.array(utils.get_anchors())
        self.anchor_per_scale = cfg.YOLO.ANCHOR_PER_SCALE
        self.max_bbox_per_scale = 150
        self.img_id_list = self.load_car_person_list('train') if dataset_type == 'train' \
            else self.load_car_person_list('test')
        # print("self.img_id_list: ", self.img_id_list)
        self.train_input_size = 416

        self.annotations = self.load_annotations()
        self.num_samples = len(self.img_id_list)
        self.num_batchs = int(np.ceil(self.num_samples / self.batch_size))
        self.batch_count = 0
Example #14
0
    def __init__(self):
        self.input_size = 416
        self.anchor_per_scale = 3

        self.path1 = r"./data/classes/antenna.names"  #修改

        self.classes = utils.read_class_names(self.path1)
        self.num_classes = len(self.classes)

        self.score_threshold = 0.4
        self.iou_threshold = 0.5

        self.path3 = "./data/dataset/antenna_train.txt"

        self.annotation_path = self.path3

        self.write_image = True

        self.path5 = r"./data/detection1/"

        self.write_image_path = self.path5  # 是否将图片的预测结果保存
        self.show_label = True
Example #15
0
def get_mAP(Yolo,
            dataset,
            score_threshold=0.25,
            iou_threshold=0.50,
            TEST_INPUT_SIZE=TEST_INPUT_SIZE):
    MINOVERLAP = 0.5  # default value (defined in the PASCAL VOC2012 challenge)
    NUM_CLASS = read_class_names(TRAIN_CLASSES)

    ground_truth_dir_path = 'mAP/ground-truth'
    if os.path.exists(ground_truth_dir_path):
        shutil.rmtree(ground_truth_dir_path)

    if not os.path.exists('mAP'): os.mkdir('mAP')
    os.mkdir(ground_truth_dir_path)

    print(f'\ncalculating mAP{int(iou_threshold*100)}...\n')

    gt_counter_per_class = {}
    for index in range(dataset.num_samples):
        ann_dataset = dataset.annotations[index]

        original_image, bbox_data_gt = dataset.parse_annotation(
            ann_dataset, True)

        if len(bbox_data_gt) == 0:
            bboxes_gt = []
            classes_gt = []
        else:
            bboxes_gt, classes_gt = bbox_data_gt[:, :4], bbox_data_gt[:, 4]
        ground_truth_path = os.path.join(ground_truth_dir_path,
                                         str(index) + '.txt')
        num_bbox_gt = len(bboxes_gt)

        bounding_boxes = []
        for i in range(num_bbox_gt):
            class_name = NUM_CLASS[classes_gt[i]]
            xmin, ymin, xmax, ymax = list(map(str, bboxes_gt[i]))
            bbox = xmin + " " + ymin + " " + xmax + " " + ymax
            bounding_boxes.append({
                "class_name": class_name,
                "bbox": bbox,
                "used": False
            })

            # count that object
            if class_name in gt_counter_per_class:
                gt_counter_per_class[class_name] += 1
            else:
                # if class didn't exist yet
                gt_counter_per_class[class_name] = 1
            bbox_mess = ' '.join([class_name, xmin, ymin, xmax, ymax]) + '\n'
        with open(f'{ground_truth_dir_path}/{str(index)}_ground_truth.json',
                  'w') as outfile:
            json.dump(bounding_boxes, outfile)

    gt_classes = list(gt_counter_per_class.keys())
    # sort the classes alphabetically
    gt_classes = sorted(gt_classes)
    n_classes = len(gt_classes)

    times = []
    json_pred = [[] for i in range(n_classes)]
    for index in range(dataset.num_samples):
        ann_dataset = dataset.annotations[index]

        image_name = ann_dataset[0].split('/')[-1]
        original_image, bbox_data_gt = dataset.parse_annotation(
            ann_dataset, True)

        image = image_preprocess(np.copy(original_image),
                                 [TEST_INPUT_SIZE, TEST_INPUT_SIZE])
        image_data = image[np.newaxis, ...].astype(np.float32)

        t1 = time.time()
        if YOLO_FRAMEWORK == "tf":
            pred_bbox = Yolo.predict(image_data)
        elif YOLO_FRAMEWORK == "trt":
            batched_input = tf.constant(image_data)
            result = Yolo(batched_input)
            pred_bbox = []
            for key, value in result.items():
                value = value.numpy()
                pred_bbox.append(value)

        t2 = time.time()

        times.append(t2 - t1)

        pred_bbox = [tf.reshape(x, (-1, tf.shape(x)[-1])) for x in pred_bbox]
        pred_bbox = tf.concat(pred_bbox, axis=0)

        bboxes = postprocess_boxes(pred_bbox, original_image, TEST_INPUT_SIZE,
                                   score_threshold)
        bboxes = nms(bboxes, iou_threshold, method='nms')

        for bbox in bboxes:
            coor = np.array(bbox[:4], dtype=np.int32)
            score = bbox[4]
            class_ind = int(bbox[5])
            class_name = NUM_CLASS[class_ind]
            score = '%.4f' % score
            xmin, ymin, xmax, ymax = list(map(str, coor))
            bbox = xmin + " " + ymin + " " + xmax + " " + ymax
            json_pred[gt_classes.index(class_name)].append({
                "confidence":
                str(score),
                "file_id":
                str(index),
                "bbox":
                str(bbox)
            })

    ms = sum(times) / len(times) * 1000
    fps = 1000 / ms

    for class_name in gt_classes:
        json_pred[gt_classes.index(class_name)].sort(
            key=lambda x: float(x['confidence']), reverse=True)
        with open(f'{ground_truth_dir_path}/{class_name}_predictions.json',
                  'w') as outfile:
            json.dump(json_pred[gt_classes.index(class_name)], outfile)

    # Calculate the AP for each class
    sum_AP = 0.0
    ap_dictionary = {}
    # open file to store the results
    with open("mAP/results.txt", 'w') as results_file:
        results_file.write("# AP and precision/recall per class\n")
        count_true_positives = {}
        for class_index, class_name in enumerate(gt_classes):
            count_true_positives[class_name] = 0
            # Load predictions of that class
            predictions_file = f'{ground_truth_dir_path}/{class_name}_predictions.json'
            predictions_data = json.load(open(predictions_file))

            # Assign predictions to ground truth objects
            nd = len(predictions_data)
            tp = [0] * nd  # creates an array of zeros of size nd
            fp = [0] * nd
            for idx, prediction in enumerate(predictions_data):
                file_id = prediction["file_id"]
                # assign prediction to ground truth object if any
                #   open ground-truth with that file_id
                gt_file = f'{ground_truth_dir_path}/{str(file_id)}_ground_truth.json'
                ground_truth_data = json.load(open(gt_file))
                ovmax = -1
                gt_match = -1
                # load prediction bounding-box
                bb = [float(x) for x in prediction["bbox"].split()
                      ]  # bounding box of prediction
                for obj in ground_truth_data:
                    # look for a class_name match
                    if obj["class_name"] == class_name:
                        bbgt = [float(x) for x in obj["bbox"].split()
                                ]  # bounding box of ground truth
                        bi = [
                            max(bb[0], bbgt[0]),
                            max(bb[1], bbgt[1]),
                            min(bb[2], bbgt[2]),
                            min(bb[3], bbgt[3])
                        ]
                        iw = bi[2] - bi[0] + 1
                        ih = bi[3] - bi[1] + 1
                        if iw > 0 and ih > 0:
                            # compute overlap (IoU) = area of intersection / area of union
                            ua = (bb[2] - bb[0] + 1) * (bb[3] - bb[1] + 1) + (
                                bbgt[2] - bbgt[0] + 1) * (bbgt[3] - bbgt[1] +
                                                          1) - iw * ih
                            ov = iw * ih / ua
                            if ov > ovmax:
                                ovmax = ov
                                gt_match = obj

                # assign prediction as true positive/don't care/false positive
                if ovmax >= MINOVERLAP:  # if ovmax > minimum overlap
                    if not bool(gt_match["used"]):
                        # true positive
                        tp[idx] = 1
                        gt_match["used"] = True
                        count_true_positives[class_name] += 1
                        # update the ".json" file
                        with open(gt_file, 'w') as f:
                            f.write(json.dumps(ground_truth_data))
                    else:
                        # false positive (multiple detection)
                        fp[idx] = 1
                else:
                    # false positive
                    fp[idx] = 1

            # compute precision/recall
            cumsum = 0
            for idx, val in enumerate(fp):
                fp[idx] += cumsum
                cumsum += val
            cumsum = 0
            for idx, val in enumerate(tp):
                tp[idx] += cumsum
                cumsum += val
            #print(tp)
            rec = tp[:]
            for idx, val in enumerate(tp):
                rec[idx] = float(tp[idx]) / gt_counter_per_class[class_name]
            #print(rec)
            prec = tp[:]
            for idx, val in enumerate(tp):
                prec[idx] = float(tp[idx]) / (fp[idx] + tp[idx])
            #print(prec)

            ap, mrec, mprec = voc_ap(rec, prec)
            sum_AP += ap
            text = "{0:.3f}%".format(
                ap * 100
            ) + " = " + class_name + " AP  "  #class_name + " AP = {0:.2f}%".format(ap*100)

            rounded_prec = ['%.3f' % elem for elem in prec]
            rounded_rec = ['%.3f' % elem for elem in rec]
            # Write to results.txt
            results_file.write(text + "\n Precision: " + str(rounded_prec) +
                               "\n Recall   :" + str(rounded_rec) + "\n\n")

            print(text)
            ap_dictionary[class_name] = ap

        results_file.write("\n# mAP of all classes\n")
        mAP = sum_AP / n_classes

        text = "mAP = {:.3f}%, {:.2f} FPS".format(mAP * 100, fps)
        results_file.write(text + "\n")
        print(text)

        return mAP * 100
if not os.path.isfile(output_directory + 'filter_influence' +
                      infl_version_text + '.npy'):
    # Use when there is no info about Influence
    influence_df = pd.DataFrame(data=[filter_num]).T
    influence_df.columns = ['Filter']
    influence_txt = '_without_influence'
else:
    # Use when there is info about Influence
    influence_txt = '_with_celltype_influence'
    influence = np.load(output_directory + 'filter_influence' +
                        infl_version_text + '.npy')
    influence_cellwise = np.load(output_directory +
                                 'filter_cellwise_influence' +
                                 infl_celltype_version_text +
                                 infl_version_text + '.npy')
    cell_names = utils.read_class_names(
        '../data/' + species + '_Data/cell_type_names.txt', species)
    influence_df = pd.DataFrame(data=influence_cellwise, columns=cell_names)
    influence_df.insert(loc=0,
                        column='Influence',
                        value=pd.to_numeric(influence))
    influence_df.insert(loc=0, column='Filter', value=filter_num)

# Activation stats for OCRs
num_seqs = np.loadtxt(output_directory + 'nseqs_per_filters.txt')
influence_df['num_seqs'] = num_seqs

activated_OCRs = np.load(output_directory + 'activated_OCRs.npy')
Ave_OCR_activity = np.mean(activated_OCRs, axis=1)
influence_df['Ave_OCR_activity'] = Ave_OCR_activity

Num_Act_OCRs = np.load(output_directory + 'n_activated_OCRs.npy')
def post_processor(bbox_q, cameras, out_q, frames, times, image_q = None):
    classes = utils.read_class_names("./config/coco.names")
    
    start_time = time.time()
    start_time = time.strftime('%Y-%m-%d %H-%M-%S', time.localtime(start_time))
    
    f_directory = cameras[0]["output"].split("/")[:-1]
    f_directory.append("{}_output_frames".format(start_time))
    f_directory = "/".join(f_directory)                                                
    os.mkdir(f_directory)     

    frames_processed = np.zeros(len(cameras))
    
    try:
        while True:
            
            if not bbox_q.empty():
                box_ind = bbox_q.get()
                ped_bboxes = box_ind[0]
                veh_bboxes = box_ind[1]
                i = int(box_ind[2])
                frame = box_ind[3]
                
                # first, try and show frame
                #frame = frame.transpose(1, 2, 0)
                # cv2.imshow("test",frame)
                # cv2.waitKey(0)
                
                camera = cameras[i]
                filename = camera["output"].format(start_time)
                cam_name = camera["name"]
                pix_real = camera["im-gps"]
                frame_save = camera["save_frames"]
                dt = times[i]
                
                #find ft pts and convert to real_world
                ped_pts = utils.get_ftpts(ped_bboxes)
                realpts = tform.transform_pt_array(ped_pts, pix_real)
                
                # verifies there is more than one point in the list (each point has size 2)]
                if realpts.size > 2:
                    mytree = scipy.spatial.cKDTree(realpts)
                    errors = utils.compliance_count(mytree, realpts)
                    
                    #FIXME can probably do these both in 1 function
                    avg_dist = utils.find_dist(mytree, realpts)
                    avg_min_dist = utils.find_min_dist(mytree, realpts)
                else:
                    errors = 0
                    avg_min_dist = None
                    avg_dist = None
                occupants = len(ped_bboxes)
                #output info to csv file  
                with open(filename, 'a', newline='') as base_f:
                    writer = csv.writer(base_f)
                    utils.video_write_info(writer, realpts, str(dt), errors, occupants, avg_dist, avg_min_dist,cam_name)
                        
                stats = [i, errors, occupants, avg_min_dist]
                
                #put outpt data into queue so it is accessible by the analyzer
                if out_q.full():
                    temp = out_q.get()
                out_q.put(stats)
                
                if frame_save:
                    result = prep_frame(ped_pts, frame, camera, errors, occupants, ped_bboxes,veh_bboxes,classes)
                
                
      
                #save frames
                if frame_save:
                    frame_name = "{}/{}_{}_processed.jpg".format(f_directory,cam_name,int(frames_processed[i]))
                    cv2.imwrite(frame_name,result*255)
                        
                # if frame_show:
                #     if image_q.full():
                #         image_q.get()
                #     image_q.put(result)
                    
                # # FIXME - just for debugging, show frame on screen
                # show_frame(result, i)  
                # if cv2.waitKey(1) & 0xFF == ord('q'): break
                  
                frames_processed[i] += 1
                    
    except:
        print("Unexpected postprocessing error:", sys.exc_info()[0])
        cv2.destroyAllWindows()
    return
def main(type="image"):
    model = Model()
    names = utils.read_class_names("./data/classes.names")

    end = time.time()

    print(f"Execution time {end - start}")

    if type == "video" or type == "camera":
        if type == "video":
            vid = cv2.VideoCapture(input_video_file)
        elif type == "camera":
            vid = cv2.VideoCapture(0)

        frame_width = int(vid.get(cv2.CAP_PROP_FRAME_WIDTH))
        frame_height = int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT))
        frame_per_second = int(vid.get(cv2.CAP_PROP_FPS))
        codec = cv2.VideoWriter_fourcc(*'XVID')
        out = cv2.VideoWriter(output_video_file, codec, frame_per_second,
                              (frame_width, frame_height))

        while True:
            _, image = vid.read()
            if image is None:
                logging.warning("Empty Frame")
                time.sleep(0.1)
                continue

            image_size = image.shape[:2]

            image_input = tf.expand_dims(image, 0)
            image_input = utils.transform_images(image_input, 416)
            predicted_bounding_box = model.predict(image_input)
            predicted_bounding_box = [
                tf.reshape(x, (-1, tf.shape(x)[-1]))
                for x in predicted_bounding_box
            ]
            predicted_bounding_box = tf.concat(predicted_bounding_box, axis=0)

            final_detected_boxes, class_names, scores = utils.box_detector(
                predicted_bounding_box)
            image = utils.get_human_box_detection(final_detected_boxes,
                                                  class_names, scores, names,
                                                  image)

            if output_video_file:
                out.write(image)
            image = cv2.resize(image, (1200, 700))
            cv2.imshow('output', image)

            if cv2.waitKey(1) == ord('q'):
                break

        cv2.destroyAllWindows()

    if type == "image":

        for i, image_file in enumerate(input_image_files):
            image = cv2.imread(image_file)
            image_input = tf.expand_dims(image, 0)

            image_input = utils.transform_images(image_input, 416)
            predicted_bounding_boxes = model.predict(image_input)

            predicted_bounding_box = [
                tf.reshape(x, (-1, tf.shape(x)[-1]))
                for x in predicted_bounding_boxes
            ]

            predicted_bounding_box = tf.concat(predicted_bounding_box, axis=0)
            final_detected_boxes, class_names, scores = utils.box_detector(
                predicted_bounding_box)
            image = utils.get_human_box_detection(final_detected_boxes,
                                                  class_names, scores, names,
                                                  image)
            image = cv2.resize(image, (1200, 700))
            cv2.imwrite("output_%d.jpg" % i, image)
            cv2.imshow('output7', image)

            if cv2.waitKey(0) == ord('q'):
                cv2.destroyAllWindows()
Example #19
0
def proc_video(ind, i_lock, frames, times, bbox_q, cameras, gpu):
    classes = utils.read_class_names("./config/coco.names")
   
    worker = Worker(gpu)
    while(True):
        if worker.avail:
            worker.mark_unavail()
            # save current index so it doesn't change while processing
            #lock ensures that multiple processes aren't working on same frame
            try:
                with i_lock:
                    i = ind.value
                    ind.value = ind.value + 1
                    ind.value = ind.value % len(frames)
            except:
                worker.mark_avail()
                continue
            
            #loop through frames, find people, record occupants and infractions 
            camera = cameras[i]
                
            try:
                worker.set_frame(np.asarray(frames[i]))
            except ValueError:
                worker.mark_avail()
                torch.cuda.empty_cache()
                continue
                
            ped_bboxes,veh_bboxes,blur = worker.get_bboxes()

            # denormalize
            im = F.normalize(worker.gpu_frame[0],mean = [-0.485/0.229, -0.456/0.224, -0.406/0.225],
                                       std = [1/0.229, 1/0.224, 1/0.225])
            im = F.to_pil_image(im.cpu())
            open_cv_image = np.array(im)
            im = open_cv_image.copy()/255.0
            #im = im[:,:,::-1]
            
            # blur all peds regardless of confidence
            for ped in blur:
                im = utils.find_blur_face(ped.int(),im)
            
            
            # parse metrics and write output frame
            filename = camera["output"]
            cam_name = camera["name"]
            pix_real = camera["im-gps"]
            frame_save = camera["save_frames"]
            dt = times[i]
            
            #find ft pts and convert to real_world
            ped_pts = utils.get_ftpts(ped_bboxes)
            realpts = tform.transform_pt_array(ped_pts, pix_real)
            
            # also convert veh points to realworld
            real_veh_pts = tform.transform_pt_array(utils.get_ftpts(veh_bboxes),pix_real)
            
            # verifies there is more than one point in the list (each point has size 2)]
            if realpts.size > 2:
                mytree = scipy.spatial.cKDTree(realpts)
                errors = utils.compliance_count(mytree, realpts)
                
                #FIXME can probably do these both in 1 function
                avg_dist = utils.find_dist(mytree, realpts)
                avg_min_dist = utils.find_min_dist(mytree, realpts)
            else:
                errors = 0
                avg_min_dist = None
                avg_dist = None
            occupants = len(ped_bboxes)
            
            #save frames with occupants
            if frame_save and (occupants > 12 or errors > 5):
                result = prep_frame(ped_pts, im, camera, errors, occupants, ped_bboxes,veh_bboxes,classes)
                dt_fixed = str(dt).replace(":","-").split(".")[0]
                frame_name = "{}/{}.jpg".format(camera["frame_dir"],dt_fixed)
                cv2.imwrite(frame_name,result*255)
            
            #combine so bounding boxes remain associated with camera
            output = [realpts,real_veh_pts,dt,errors,occupants,avg_dist,avg_min_dist,cam_name,i,worker.id]
            bbox_q.put(output)            
            
            
            worker.count += 1
            worker.mark_avail()
                

    return
def main(errs, ocpts, dists, updated, frames, times, avgs, avg_lock, i_lock,
         ind, out_q, bbox_q, image_q, config, ctx):

    # file containing camera information
    # transform_f = 'C:/Users/Nikki/Documents/work/inputs-outputs/transforms.csv'
    # transform_f = 'C:/Users/Nikki/Documents/work/inputs-outputs/test.csv'
    # transform_f = 'C:/Users/Nikki/Documents/work/inputs-outputs/test_all.csv'
    # transform_f = './config/LAMBDA_TEST.config'

    #create VidObjs to store information about each camera
    cameras = initialize_cams(config)

    num_cams = len(cameras)
    #length of queues, kinda arbitrary - this is the number that will be used for moving avg analytics
    buf_num = 3

    #need to fix these references
    # errs = var_list[0]
    # ocpts = var_list[1]
    # dists = var_list[2]
    # updated = var_list[3]
    # frames = var_list[4]
    # times = var_list[5]
    # avgs = var_list[6]
    # avg_lock = var_list[7]
    # i_lock = var_list[8]
    # ind = var_list[9]
    # bbox_q = var_list[10]
    # ind = var_list[11]

    #uncomment if running from this file

    # manager = mp.Manager()
    # print('MP manager created')
    # #  create manager to handle shared variables across processes
    # updated = manager.Value(c_bool, False)
    # frames = manager.list([None]* num_cams)
    # times = manager.list([None]* num_cams)
    # avgs = manager.list([None] * 5)
    # avg_lock = manager.Lock()
    # i_lock = manager.Lock()
    # out_q = manager.Queue(num_cams*2)
    # bbox_q = manager.Queue()
    # ind = manager.Value(int, 0)
    # image_q = manager.Queue(num_cams*2)

    # # sample = manager.list([None] * 5)

    # errs = manager.list()
    # ocpts = manager.list()
    # dists = manager.list()
    # s_lock = manager.Lock()
    # # for _ in range(num_cams):
    # #     ocpts.append( [None]* buf_num)

    # for i in range(num_cams):
    #     errs.append(manager.list([None]))
    #     ocpts.append(manager.list([None]))
    #     dists.append(manager.list([None]))

    classes = utils.read_class_names("./config/coco.names")

    #stores frame data that has been transfered to GPU
    GPU_LIST = [i for i in range(torch.cuda.device_count())]
    # workers = setup_gpus(vids, gpu_list = GPU_LIST)
    #start model
    # model = detector.start_model()
    streamers = []

    worker = Worker(2)
    frame = cv2.imread("/home/worklab/Desktop/test1.png")
    worker.set_frame(frame)
    ped, veh = worker.get_bboxes()
    cameras[0]["frame_size"] = (worker.gpu_frame[0].shape[:2])

    im = F.normalize(worker.gpu_frame[0],
                     mean=[-0.485 / 0.229, -0.456 / 0.224, -0.406 / 0.225],
                     std=[1 / 0.229, 1 / 0.224, 1 / 0.225])
    im = F.to_pil_image(im.cpu())
    open_cv_image = np.array(im)
    im = open_cv_image.copy() / 255.0
    im = im[:, :, ::-1]
    #combine so bounding boxes remain associated with camera
    i = 0
    box_ind = (ped, veh, i, im)

    ped_bboxes = box_ind[0]
    veh_bboxes = box_ind[1]
    i = box_ind[2]
    frame = box_ind[3]

    # first, try and show frame
    #frame = frame.transpose(1, 2, 0)
    # cv2.imshow("test",frame)
    # cv2.waitKey(0)

    camera = cameras[i]
    filename = camera["address"]
    pix_real = camera["im-gps"]
    frame_show = camera["save_frames"]
    dt = times[i]

    #find ft pts and convert to real_world
    ped_pts = utils.get_ftpts(ped_bboxes)
    realpts = tform.transform_pt_array(ped_pts, pix_real)

    # verifies there is more than one point in the list (each point has size 2)]
    if realpts.size > 2:
        mytree = scipy.spatial.cKDTree(realpts)
        errors = utils.compliance_count(mytree, realpts)

        #FIXME can probably do these both in 1 function
        avg_dist = utils.find_dist(mytree, realpts)
        avg_min_dist = utils.find_min_dist(mytree, realpts)
    else:
        errors = 0
        avg_min_dist = None
        avg_dist = None
    occupants = len(ped_bboxes)
    #output info to csv file
    with open(filename, 'a', newline='') as base_f:
        writer = csv.writer(base_f)
        utils.video_write_info(writer, realpts, str(dt), errors, occupants,
                               avg_dist, avg_min_dist)

    stats = [i, errors, occupants, avg_min_dist]

    #put outpt data into queue so it is accessible by the analyzer
    if out_q.full():
        out_q.get()
    out_q.put(stats)

    result = prep_frame(ped_pts, frame, camera, errors, occupants, ped_bboxes,
                        veh_bboxes, classes)

    cv2.imshow("frame", result)
    cv2.waitKey(0)
Example #21
0
def valid_NB(datafile, cfgfile, weightfile, epoch, prefix='result'):
    model = Darknet(cfgfile)
    options = read_data_file(datafile)
    data_root = options['valid']
    class_root = options['names']
    names = read_class_names(class_root)

    device = 'cuda' if torch.cuda.is_available() else 'cpu'

    with open(data_root, 'r') as f:
        lines = f.readlines()
        valid_files = [item.strip() for item in lines]

    model.load_weights(weightfile)
    model = model.to(device)
    model.eval()

    NB_model = joblib.load('NB models/epoch_{}.pkl'.format(int(epoch)))

    data = YoloDataset(data_root,
                       shape=(model.width, model.height),
                       transform=transforms.Compose([transforms.ToTensor()]),
                       train=False)
    batch_size = 2
    kwargs = {'num_workers': 4, 'pin_memory': True}
    data_loader = DataLoader(data,
                             batch_size=batch_size,
                             shuffle=False,
                             **kwargs)

    fs = [None] * model.num_classes
    if not os.path.exists(prefix):
        os.makedirs(prefix)
    for i in range(model.num_classes):
        filename = prefix + '/' + str(names[i]) + '.txt'
        fs[i] = open(filename, 'w')
    net_shape = (model.width, model.height)

    conf_thresh = 0.005
    nms_thresh = 0.45

    fileIndex = 0
    for index, (imgs, labels, org_w, org_h) in enumerate(data_loader):
        imgs = imgs.to(device)
        output = model(imgs)

        batch_boxes = get_all_boxes(output,
                                    net_shape,
                                    conf_thresh,
                                    model.num_classes,
                                    device,
                                    validation=True)

        for i in range(len(batch_boxes)):
            fileId = os.path.basename(valid_files[fileIndex]).split('.')[
                0]  # gei naive image name without suffix
            w, h = float(org_w[i]), float(org_h[i])
            # print(valid_files[fileIndex], '{}/{}'.format(fileIndex+1, len(data_loader) * batch_size))
            fileIndex += 1
            boxes = batch_boxes[i]
            correct_yolo_boxes(boxes, w, h, model.width, model.height)
            boxes = auto_thresh_nms(boxes, NB_model)
            for box in boxes:
                x1 = (box[0] - box[2] / 2.0) * w
                y1 = (box[1] - box[3] / 2.0) * h
                x2 = (box[0] + box[2] / 2.0) * w
                y2 = (box[1] + box[3] / 2.0) * h

                # 包含物体的概率,乘以每一类的概率
                det_conf = box[4]
                for j in range((len(box) - 5) // 2):
                    cls_conf = box[5 + 2 * j]
                    cls_id = int(box[5 + 2 * j + 1])
                    prob = det_conf * cls_conf
                    fs[cls_id].write('{:s} {:f} {:f} {:f} {:f} {:f}\n'.format(
                        fileId, prob, x1, y1, x2, y2))

    for i in range(len(fs)):
        fs[i].close()
Example #22
0
    def __init__(self):
        print("initialize values...")
        self.anchor_per_scale = cfg.YOLO.ANCHOR_PER_SCALE
        self.classes = utils.read_class_names(cfg.YOLO.CLASSES)
        self.num_classes = len(self.classes)

        self.learn_rate_init = cfg.TRAIN.LEARN_RATE_INIT
        self.learn_rate_end = cfg.TRAIN.LEARN_RATE_END
        self.first_stage_epochs = cfg.TRAIN.FISRT_STAGE_EPOCHS
        self.second_stage_epochs = cfg.TRAIN.SECOND_STAGE_EPOCHS
        self.epoch = cfg.TRAIN.EPOCH
        self.warmup_periods = cfg.TRAIN.WARMUP_EPOCHS


        # self.initial_weight = cfg.TRAIN.INITIAL_WEIGHT
        self.time = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time()))
        self.moving_ave_decay = cfg.YOLO.MOVING_AVE_DECAY
        self.max_bbox_per_scale = cfg.YOLO.MAX_OUTPUT_SIZE

        self.train_logdir = "./train_log/"
        self.trainset = Dataset('train')
        self.testset = Dataset('test')
        self.steps_per_period = len(self.trainset)

        config = tf.ConfigProto(allow_soft_placement=True)
        config.gpu_options.allow_growth = True
        self.sess = tf.Session(config=config)

        with tf.name_scope('define_input'):
            print("defining inpuuts...")
            self.input_data = tf.placeholder(dtype=tf.float32, shape=[cfg.TRAIN.BATCH_SIZE, self.trainset.train_input_size,
                                                                      self.trainset.train_input_size, 3],
                                             name='input_data')
            self.label_sbbox = tf.placeholder(dtype=tf.float32, name='label_sbbox')
            self.label_mbbox = tf.placeholder(dtype=tf.float32, name='label_mbbox')
            self.label_lbbox = tf.placeholder(dtype=tf.float32, name='label_lbbox')
            self.true_sbbox = tf.placeholder(dtype=tf.float32, name='true_sbbox')
            self.true_mbbox = tf.placeholder(dtype=tf.float32, name='true_mbbox')
            self.true_lbbox = tf.placeholder(dtype=tf.float32, name='true_lbbox')
            self.trainable = tf.placeholder(dtype=tf.bool, name='trainig')

        with tf.name_scope('define_loss'):
            print("defining loss...")
            self.model = YoloV3(inputs=self.input_data, training=self.trainable)
            self.network_para = tf.global_variables()
            self.giou_loss, self.conf_loss, self.prob_loss, self.debug = \
                self.model.compute_loss(self.label_sbbox, self.label_mbbox, self.label_lbbox, self.true_sbbox,
                                        self.true_mbbox, self.true_lbbox)
            # print("self.giou_loss: ", self.giou_loss, " self.conf_loss: ", self.conf_loss, " self.prob_loss: ", self.prob_loss)

            # self.loss = self.giou_loss + self.conf_loss + self.prob_loss
            new_giou_loss = self.debug[4] + self.debug[9] + self.debug[14]
            self.loss = new_giou_loss + self.conf_loss + self.prob_loss

        # TODO: change this part if training doesnt do good
        with tf.name_scope('define_learning_rate'):
            print("defining learning rate...")
            self.learning_rate = cfg.TRAIN.LEARN_RATE_INIT

        with tf.name_scope('learn_rate'):
            print("learn_rate")
            self.global_step = tf.Variable(1.0, dtype=tf.float64, trainable=False, name='global_step')
            warmup_steps = tf.constant(self.warmup_periods * 1,
                                       dtype=tf.float64, name='warmup_steps')
            train_steps = tf.constant(self.epoch * self.steps_per_period,
                                      dtype=tf.float64, name='train_steps')
            self.learn_rate = tf.cond(
                pred=self.global_step < warmup_steps,
                true_fn=lambda: self.global_step / warmup_steps * self.learn_rate_init,
                false_fn=lambda: self.learn_rate_end + 0.5 * (self.learn_rate_init - self.learn_rate_end) *
                                 (1 + tf.cos(
                                     (self.global_step - warmup_steps) / (train_steps - warmup_steps) * np.pi))
            )
            global_step_update = tf.assign_add(self.global_step, 1.0)

        # TODO: change this part if training doesnt do good
        with tf.name_scope("define_weight_decay"):
            print("defining weight decay...")
            moving_ave = tf.train.ExponentialMovingAverage(self.moving_ave_decay).apply(tf.trainable_variables())

        with tf.name_scope('summary'):
            print("summary...")
            tf.summary.scalar("giou_loss", self.giou_loss)
            tf.summary.scalar("conf_loss", self.conf_loss)
            tf.summary.scalar("prob_loss", self.prob_loss)
            tf.summary.scalar("total_loss", self.loss)

            if os.path.exists(self.train_logdir):
                print('pass')
            else:
                os.mkdir(self.train_logdir)

            self.write_op = tf.summary.merge_all()
            self.summary_writer = tf.summary.FileWriter(self.train_logdir, graph=self.sess.graph)

        with tf.name_scope('define_train'):
            print("defining train...")
            trainable_var_list = tf.trainable_variables()
            optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate).minimize(self.loss,
                                                                                          var_list=trainable_var_list)  # the training step

            # optimizer = tf.train.GradientDescentOptimizer(learning_rate=self.learning_rate).minimize(self.loss,
            #                                                                               var_list=trainable_var_list)
            # optimizer = tf.train.MomentumOptimizer(learning_rate=self.learn_rate, momentum=0.9).minimize(self.loss,
            #                                                                                var_list=trainable_var_list)

            with tf.control_dependencies(
                    tf.get_collection(tf.GraphKeys.UPDATE_OPS)):  # normalize batch from the last round
                with tf.control_dependencies([optimizer, global_step_update]):
                    with tf.control_dependencies([moving_ave]):  # decay
                        self.train_op_with_all_variables = tf.no_op()

        with tf.name_scope('loader_and_saver'):
            print("defining loader and saver...")
            self.loader = tf.train.Saver(self.network_para)
            self.saver = tf.train.Saver(tf.global_variables(), max_to_keep=10)
Example #23
0
    def __init__(self, inputs, training, data_format=None):
        """Creates the model.
        Args:
            n_classes: Number of class labels.
            model_size: The input size of the model.
            max_output_size: Max number of boxes to be selected for each class.
            iou_threshold: Threshold for the IOU.
            confidence_threshold: Threshold for the confidence score.
            data_format: The input format.
        Returns:
            None.
        """
        self.iou_loss_thresh = 0.5
        self.num_class = 2
        self.anchor_per_scale = 3
        self.classes = utils.read_class_names(cfg.YOLO.CLASSES)
        self.n_classes = len(self.classes)
        self.model_size = cfg.YOLO.MODEL_SIZE
        self.max_output_size = cfg.YOLO.MAX_OUTPUT_SIZE
        self.iou_threshold = cfg.YOLO.IOU_THRESHOLD
        self.data_format = data_format
        self.strides = np.array(cfg.YOLO.STRIDES)
        self._giou_loss_backup = None

        if self.data_format == 'channels_first':
            inputs = tf.transpose(inputs, [0, 3, 1, 2])

        with tf.variable_scope('yolo_v3_model'):
            self.anchors = utils.get_anchors()

            route1, route2, inputs = darknet53(inputs=inputs, training=training, data_format=self.data_format)
            route, inputs = yolo_conv_block(inputs=inputs, filters=512, training=training, data_format=self.data_format,
                                            name='yolo_conv_block_1')
            self.conv_lbbox = yolo_detection_layer(inputs, n_classes=self.n_classes,
                                                                    anchors=self.anchors[2],
                                                                    img_size=self.model_size,
                                                                    data_format=self.data_format,
                                                                    name='con_lbbox_layer',
                                                                    strides=self.strides[2])
            inputs = con2d_fixed_padding(route, filters=256, kernel_size=1, data_format=self.data_format, name='conv_lbbox')
            inputs = batch_norm(inputs, training=training, data_format=self.data_format)
            inputs = tf.nn.leaky_relu(inputs, alpha=cfg.YOLO.LEAKY_RELU)

            upsample_output_size = route2.get_shape().as_list()
            inputs = upsample(inputs, out_shape=upsample_output_size, data_format=self.data_format)

            # get the channel axis
            axis = 1 if self.data_format == 'channels_first' else 3
            inputs = tf.concat((inputs, route2), axis=axis)

            route, inputs = yolo_conv_block(inputs, filters=256, training=training, data_format=self.data_format,
                                            name='yolo_conv_block_2')
            self.conv_mbbox = yolo_detection_layer(inputs, n_classes=self.n_classes,
                                                                    anchors=self.anchors[1],
                                                                    img_size=self.model_size,
                                                                    data_format=self.data_format,
                                                                    name='con_mbbox_layer',
                                                                    strides=self.strides[1])
            inputs = con2d_fixed_padding(route, filters=128, kernel_size=1, data_format=self.data_format, name='conv_mbbox')
            inputs = batch_norm(inputs, training=training, data_format=self.data_format)
            inputs = tf.nn.leaky_relu(inputs, alpha=cfg.YOLO.LEAKY_RELU)
            upsample_output_size = route1.get_shape().as_list()
            inputs = upsample(inputs, out_shape=upsample_output_size, data_format=self.data_format)
            inputs = tf.concat((inputs, route1), axis=axis)

            route, inputs = yolo_conv_block(inputs, filters=128, training=training, data_format=self.data_format,
                                            name='yolo_conv_block_3')
            self.conv_sbbox = yolo_detection_layer(inputs, n_classes=self.n_classes,
                                                                    anchors=self.anchors[0],
                                                                    img_size=self.model_size,
                                                                    data_format=self.data_format,
                                                                    name='conv_sbbox',
                                                                    strides=self.strides[0])

            with tf.variable_scope('pred_sbbox'):
                self.pred_sbbox = self.decode(self.conv_sbbox, self.anchors[0], self.strides[0])
                print("self.pred_sbbox: ", self.pred_sbbox.shape)

            with tf.variable_scope('pred_mbbox'):
                self.pred_mbbox = self.decode(self.conv_mbbox, self.anchors[1], self.strides[1])

            with tf.variable_scope('pred_lbbox'):
                self.pred_lbbox = self.decode(self.conv_lbbox, self.anchors[2], self.strides[2])
def post_processor(bbox_q, cameras, out_q, frames, times, image_q=None):
    classes = utils.read_class_names("./config/coco.names")

    try:
        while True:
            if not bbox_q.empty():
                box_ind = bbox_q.get()
                ped_bboxes = box_ind[0]
                veh_bboxes = box_ind[1]
                i = box_ind[2]
                frame = box_ind[3]

                # first, try and show frame
                #frame = frame.transpose(1, 2, 0)
                # cv2.imshow("test",frame)
                # cv2.waitKey(0)

                camera = cameras[i]
                filename = camera["address"]
                pix_real = camera["im-gps"]
                frame_show = camera["save_frames"]
                dt = times[i]

                #find ft pts and convert to real_world
                ped_pts = utils.get_ftpts(ped_bboxes)
                realpts = tform.transform_pt_array(ped_pts, pix_real)

                # verifies there is more than one point in the list (each point has size 2)]
                if realpts.size > 2:
                    mytree = scipy.spatial.cKDTree(realpts)
                    errors = utils.compliance_count(mytree, realpts)

                    #FIXME can probably do these both in 1 function
                    avg_dist = utils.find_dist(mytree, realpts)
                    avg_min_dist = utils.find_min_dist(mytree, realpts)
                else:
                    errors = 0
                    avg_min_dist = None
                    avg_dist = None
                occupants = len(ped_bboxes)
                #output info to csv file
                with open(filename, 'a', newline='') as base_f:
                    writer = csv.writer(base_f)
                    utils.video_write_info(writer, realpts, str(dt), errors,
                                           occupants, avg_dist, avg_min_dist)

                stats = [i, errors, occupants, avg_min_dist]

                #put outpt data into queue so it is accessible by the analyzer
                if out_q.full():
                    out_q.get()
                out_q.put(stats)

                result = prep_frame(ped_pts, frame, camera, errors, occupants,
                                    ped_bboxes, veh_bboxes, classes)

                cv2.imshow("frame", result)
                cv2.waitKey(0)

                # if frame_save or frame_show:
                #     result = prep_frame(ftpts, frame, vid, errors, occupants, bboxes)

                ### UNCOMMENT AND USE
                # frame_save = True
                # #save frames
                # if frame_save:
                #     outpt_frame(result, vid)

                # if frame_show:
                #     if image_q.full():
                #         image_q.get()
                #     image_q.put(result)

                # # FIXME - just for debugging, show frame on screen
                show_frame(result, i)
                if cv2.waitKey(1) & 0xFF == ord('q'): break
    except KeyboardInterrupt:
        print("Unexpected postprocessing error:", sys.exc_info()[0])
        cv2.destroyAllWindows()
    return
Example #25
0
import tensorflow as tf
import numpy as np
from config import cfg
from utils import read_class_names, get_anchors

NUM_CLASS = len(read_class_names(cfg.YOLO.CLASSES))
ANCHORS = get_anchors(cfg.YOLO.ANCHORS)
STRIDES = np.array(cfg.YOLO.STRIDES)


def decode(conv_output, i=0):
    """
    :return [batch_size, output_size, output_size, anchor_per_scale, 5 + num_classes]
    box format: (x, y, w, h, score, probability)
    """

    conv_shape = tf.shape(conv_output)
    batch_size = conv_shape[0]
    output_size = conv_shape[1]

    conv_output = tf.reshape(
        conv_output, (batch_size, output_size, output_size, 3, 5 + NUM_CLASS))

    conv_raw_dxdy = conv_output[:, :, :, :, 0:2]
    conv_raw_dwdh = conv_output[:, :, :, :, 2:4]
    conv_raw_conf = conv_output[:, :, :, :, 4:5]
    conv_raw_prob = conv_output[:, :, :, :, 5:]

    y = tf.tile(
        tf.range(output_size, dtype=tf.int32)[:, tf.newaxis], [1, output_size])
    x = tf.tile(
Example #26
0
def run_processing(video_stream: cv2.VideoCapture):

    return_elements = [
        "input/input_data:0", "pred_sbbox/concat_2:0", "pred_mbbox/concat_2:0",
        "pred_lbbox/concat_2:0"
    ]
    pb_file = "./model/model.pb"
    num_classes = 80
    input_size = 416
    graph = tf.Graph()
    return_tensors = utils.read_pb_return_tensors(graph, pb_file,
                                                  return_elements)

    counter = 0
    classes = utils.read_class_names()
    with tf.Session(graph=graph) as sess:
        while True:
            return_value, frame = video_stream.read()
            if return_value:
                frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
                counter = 0
            else:
                counter += 1
                if counter > 5:
                    return
                else:
                    continue
            preproc_time_start = time.time()
            frame_size = frame.shape[:2]
            image_data = utils.image_preporcess(np.copy(frame),
                                                [input_size, input_size])
            image_data = image_data[np.newaxis, ...]
            prev_time = time.time()
            preproc_time = prev_time - preproc_time_start
            info = "preprocessing time: %.2f ms" % (1000 * preproc_time)
            print(info)

            model_time = time.time()
            pred_sbbox, pred_mbbox, pred_lbbox = sess.run(
                [return_tensors[1], return_tensors[2], return_tensors[3]],
                feed_dict={return_tensors[0]: image_data})
            end_model_time = time.time()
            proc_model_time = end_model_time - model_time
            info = "preprocessing model time: %.2f ms" % (1000 *
                                                          proc_model_time)
            print(info)

            pred_bbox = np.concatenate([
                np.reshape(pred_sbbox, (-1, 5 + num_classes)),
                np.reshape(pred_mbbox, (-1, 5 + num_classes)),
                np.reshape(pred_lbbox, (-1, 5 + num_classes))
            ],
                                       axis=0)

            bboxes = utils.postprocess_boxes(pred_bbox, frame_size, input_size,
                                             0.7)
            bboxes = utils.nms(bboxes, 0.45, method='nms')
            result_image = utils.draw_bbox(frame, bboxes, classes)

            curr_time = time.time()
            exec_time = curr_time - prev_time
            info = "time: %.2f ms" % (1000 * exec_time)
            print(info)
            cv2.namedWindow("result", cv2.WINDOW_AUTOSIZE)
            result = cv2.cvtColor(result_image, cv2.COLOR_RGB2BGR)
            cv2.imshow("result", result)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
Example #27
0
# File name   : yolo3.py
# Description : Define yolo3 model,loss function,etc.
#
#=====================================================

import numpy as np
import tensorflow as tf
import utils
import common
import backbone
import getConfig

cfg = {}
cfg = getConfig.getConfig()

NUM_CLASS = len(utils.read_class_names(cfg['yolo_classes_names']))
ANCHORS = utils.get_anchors(cfg['yolo_anchors'])
STRIDES = np.array(cfg['yolo_strides'])
IOU_LOSS_THRESHOLD = cfg['yolo_iou_loss_threshold']

def yolo3(input_layer):
    branch_1,branch_2,conv = backbone.backbone(input_layer)

    conv = common.conv(conv,(1,512))
    conv = common.conv(conv,(3,1024))
    conv = common.conv(conv,(1,512))
    conv = common.conv(conv,(3,1024))
    conv = common.conv(conv,(1,512))

    conv_l_branch = common.conv(conv,(3,1024))
    l_output = common.conv(conv_l_branch,(1,3 * (NUM_CLASS + 5)),activation = False,bn = False)