def draw_predictions(dataset, pred_bboxes, pred_labels, pred_scores, labels, batch_size): for batch_id, image_data in enumerate(dataset): imgs, _, _ = image_data img_size = imgs.shape[1] start = batch_id * batch_size end = start + batch_size batch_bboxes, batch_labels, batch_scores = pred_bboxes[ start:end], pred_labels[start:end], pred_scores[start:end] for i, img in enumerate(imgs): denormalized_bboxes = bbox_utils.denormalize_bboxes( batch_bboxes[i], img_size, img_size) draw_bboxes_with_labels(img, denormalized_bboxes, batch_labels[i], batch_scores[i], labels)
def patch(img, gt_boxes): """Generating random patch and adjusting image and ground truth objects to this patch. After this operation some of the ground truth boxes / objects could be removed from the image. However, these objects are not excluded from the output, only the coordinates are changed as zero. inputs: img = (height, width, depth) gt_boxes = (ground_truth_object_count, [y1, x1, y2, x2]) in normalized form [0, 1] outputs: modified_img = (final_height, final_width, depth) modified_gt_boxes = (ground_truth_object_count, [y1, x1, y2, x2]) in normalized form [0, 1] """ img_shape = tf.shape(img) height, width = tf.cast(img_shape[0], tf.float32), tf.cast(img_shape[1], tf.float32) # Denormalizing bounding boxes for further operations denormalized_gt_boxes = bbox_utils.denormalize_bboxes( gt_boxes, height, width) # Randomly expand image and adjust bounding boxes img, denormalized_gt_boxes, height, width = randomly_apply_operation( expand_image, img, denormalized_gt_boxes, height, width) # Generate random patches patches = generate_random_patches(height, width) # Calculate jaccard/iou value for each bounding box iou_map = bbox_utils.generate_iou_map(patches, denormalized_gt_boxes, transpose_perm=[1, 0]) # Check each ground truth box center in the generated patch and return a boolean condition list center_in_patch_condition = get_center_in_patch_condition( patches, denormalized_gt_boxes) # Get random minimum overlap value min_overlap = get_random_min_overlap() # Check and merge center condition and minimum intersection condition valid_patch_condition = tf.logical_and(center_in_patch_condition, tf.greater(iou_map, min_overlap)) # Check at least one valid patch then apply patch img, denormalized_gt_boxes, height, width = tf.cond( tf.reduce_any(valid_patch_condition), lambda: select_and_apply_patch( img, denormalized_gt_boxes, patches, valid_patch_condition, center_in_patch_condition), lambda: (img, denormalized_gt_boxes, height, width)) # Finally normalized ground truth boxes gt_boxes = bbox_utils.normalize_bboxes(denormalized_gt_boxes, height, width) gt_boxes = tf.clip_by_value(gt_boxes, 0, 1) # return img, gt_boxes
total_landmarks = hyper_params["total_landmarks"] landmark_variances = total_landmarks * variances[0:2] variances += landmark_variances for image_data in test_data: img, _, _ = image_data pred_deltas, pred_scores = model.predict_on_batch(img) pred_deltas *= variances # pred_bboxes_and_landmarks = bbox_utils.get_bboxes_and_landmarks_from_deltas( prior_boxes, pred_deltas) pred_bboxes_and_landmarks = tf.clip_by_value(pred_bboxes_and_landmarks, 0, 1) # pred_scores = tf.cast(pred_scores, tf.float32) # weighted_suppressed_data = bbox_utils.weighted_suppression( pred_scores[0], pred_bboxes_and_landmarks[0]) # weighted_bboxes = weighted_suppressed_data[..., 0:4] weighted_landmarks = weighted_suppressed_data[..., 4:] # denormalized_bboxes = bbox_utils.denormalize_bboxes( weighted_bboxes, img_size, img_size) weighted_landmarks = tf.reshape(weighted_landmarks, (-1, total_landmarks, 2)) denormalized_landmarks = landmark_utils.denormalize_landmarks( weighted_landmarks, img_size, img_size) drawing_utils.draw_bboxes_with_landmarks(img[0], denormalized_bboxes, denormalized_landmarks)
for image_data in test_data: img, _, _ = image_data pred_bbox_deltas, pred_labels = ssd_model.predict_on_batch(img) # pred_bbox_deltas *= hyper_params["variances"] pred_bboxes = bbox_utils.get_bboxes_from_deltas(prior_boxes, pred_bbox_deltas) pred_bboxes = tf.clip_by_value(pred_bboxes, 0, 1) # pred_labels = tf.cast(pred_labels, tf.float32) reshaped_pred_bboxes = tf.reshape( pred_bboxes, (batch_size, pred_bbox_deltas.shape[1], 1, 4)) # Remove background predictions pred_labels_map = tf.argmax(pred_labels, 2, output_type=tf.int32) valid_cond = tf.not_equal(pred_labels_map, bg_id) # valid_bboxes = tf.expand_dims(reshaped_pred_bboxes[valid_cond], 0) valid_labels = tf.expand_dims(pred_labels[valid_cond], 0) # nms_bboxes, nmsed_scores, nmsed_classes, valid_detections = bbox_utils.non_max_suppression( valid_bboxes, valid_labels, max_output_size_per_class=10, max_total_size=200, score_threshold=0.5) denormalized_bboxes = bbox_utils.denormalize_bboxes( nms_bboxes[0], img_size, img_size) drawing_utils.draw_bboxes_with_labels(img[0], denormalized_bboxes, nmsed_classes[0], nmsed_scores[0], labels)