# Path: ssd/ssdmodel.py
# class SSDModel:
#     def __init__(self, feature_extractor, model_name, weight_decay, is_training=True):
#     def get_model(self, inputs):
#     def get_anchors_all_layers(self, dtype=np.float32):
#     def get_all_anchors(self, minmaxformat=False):
#     def match_achors(self, gt_labels, gt_bboxes, matching_threshold=0.5):
#     def decode_bboxes_layer(self, feat_localizations, anchors):
#     def decode_bboxes_all_layers(self, localizations):
#     def decode_bboxes_all_layers_tf(self, feat_localizations):
#     def bboxes_encode(self, labels, bboxes, dtype=tf.float32, scope='ssd_bboxes_encode'):
#     def _convert2layers(self, gclasses, glocalisations, gscores):
# def get_losses(logits, localisations, gclasses, glocalisations, gscores,
#                match_threshold=0, negative_ratio=2.5, alpha=1., scope=None):
# def detected_bboxes(predictions, localisations, num_classes,
#                     select_threshold=0.01, nms_threshold=0.45,
#                     clipping_bbox=None, top_k=400, keep_top_k=200):


import tensorflow as tf
import tensorflow.contrib.slim as slim
import tf_extended as tfe
from tensorflow.python.ops import math_ops
from ssd import ssdmodel
            # Apply NMS algorithm.
            rscores, rbboxes = ssdmodel.detected_bboxes(predictions, localisations, self.g_ssd.params.num_classes)

            # Compute TP and FP statistics.
            c_num_gbboxes, c_tp, c_fp, c_scores = \
                tfe.bboxes_matching_batch(rscores.keys(), rscores, rbboxes, glabels, gbboxes, gdifficults)

            aps_voc07, aps_voc12 = self._compute_AP(c_scores, c_tp, c_fp, c_num_gbboxes)
            # Mean average precision VOC07.
            # summary_name = 'AP_VOC07/mAP'
            mAP_07_op = tf.add_n(list(aps_voc07.values())) / len(aps_voc07)
            # op = tf.summary.scalar(summary_name, mAP, collections=[])
            # print_mAP_07_op = tf.Print(mAP_07, [mAP_07], summary_name)
            # tf.summary.scalar(summary_name, print_mAP_07_op)
            # tf.add_to_collection(tf.GraphKeys.SUMMARIES, op)

            # Mean average precision VOC12.
            #             summary_name = 'AP_VOC12/mAP'
            mAP_12_op = tf.add_n(list(aps_voc12.values())) / len(aps_voc12)
            # op = tf.summary.scalar(summary_name, mAP, collections=[])
            # print_mAP_12_op = tf.Print(mAP, [mAP], summary_name)
            # tf.summary.scalar(summary_name, print_mAP_12_op)

        return mAP_07_op, mAP_12_op

    def get_mAP_tf_accumulative(self, predictions, localisations, glabels, gbboxes, gdifficults):
        # Performing post-processing on CPU: loop-intensive, usually more efficient.
        with tf.device('/device:CPU:0'):
            # Detected objects from SSD output.
            localisations = self.g_ssd.decode_bboxes_all_layers_tf(localisations)

# Path: ssd/ssdmodel.py
# class SSDModel:
#     def __init__(self, feature_extractor, model_name, weight_decay, is_training=True):
#     def get_model(self, inputs):
#     def get_anchors_all_layers(self, dtype=np.float32):
#     def get_all_anchors(self, minmaxformat=False):
#     def match_achors(self, gt_labels, gt_bboxes, matching_threshold=0.5):
#     def decode_bboxes_layer(self, feat_localizations, anchors):
#     def decode_bboxes_all_layers(self, localizations):
#     def decode_bboxes_all_layers_tf(self, feat_localizations):
#     def bboxes_encode(self, labels, bboxes, dtype=tf.float32, scope='ssd_bboxes_encode'):
#     def _convert2layers(self, gclasses, glocalisations, gscores):
# def get_losses(logits, localisations, gclasses, glocalisations, gscores,
#                match_threshold=0, negative_ratio=2.5, alpha=1., scope=None):
# def detected_bboxes(predictions, localisations, num_classes,
#                     select_threshold=0.01, nms_threshold=0.45,
#                     clipping_bbox=None, top_k=400, keep_top_k=200):


# Path: trainer/postprocessingdata.py
import tensorflow as tf
import tensorflow.contrib.slim as slim
import tf_extended as tfe
from tensorflow.python.ops import math_ops
from ssd import ssdmodel

            # Apply NMS algorithm.
            rscores, rbboxes = ssdmodel.detected_bboxes(predictions, localisations, self.g_ssd.params.num_classes)

            # Compute TP and FP statistics.
            c_num_gbboxes, c_tp, c_fp, c_scores = \
                tfe.bboxes_matching_batch(rscores.keys(), rscores, rbboxes, glabels, gbboxes, gdifficults)

            aps_voc07, aps_voc12 = self._compute_AP(c_scores, c_tp, c_fp, c_num_gbboxes)
            # Mean average precision VOC07.
            # summary_name = 'AP_VOC07/mAP'
            mAP_07_op = tf.add_n(list(aps_voc07.values())) / len(aps_voc07)
            # op = tf.summary.scalar(summary_name, mAP, collections=[])
            # print_mAP_07_op = tf.Print(mAP_07, [mAP_07], summary_name)
            # tf.summary.scalar(summary_name, print_mAP_07_op)
            # tf.add_to_collection(tf.GraphKeys.SUMMARIES, op)

            # Mean average precision VOC12.
            #             summary_name = 'AP_VOC12/mAP'
            mAP_12_op = tf.add_n(list(aps_voc12.values())) / len(aps_voc12)
            # op = tf.summary.scalar(summary_name, mAP, collections=[])
            # print_mAP_12_op = tf.Print(mAP, [mAP], summary_name)
            # tf.summary.scalar(summary_name, print_mAP_12_op)

        return mAP_07_op, mAP_12_op

    def get_mAP_tf_accumulative(self, predictions, localisations, glabels, gbboxes, gdifficults):
        # Performing post-processing on CPU: loop-intensive, usually more efficient.
        with tf.device('/device:CPU:0'):
            # Detected objects from SSD output.
            localisations = self.g_ssd.decode_bboxes_all_layers_tf(localisations)

            rscores, rbboxes = ssdmodel.detected_bboxes(predictions, localisations, self.g_ssd.params.num_classes)