예제 #1
0
    def _testConfMatrixOnTensors(self, tf_dtype, np_dtype):
        with self.test_session() as sess:
            m_neg = array_ops.placeholder(dtype=dtypes.float32)
            m_pos = array_ops.placeholder(dtype=dtypes.float32)
            s = array_ops.placeholder(dtype=dtypes.float32)

            neg = random_ops.random_normal([20], mean=m_neg, stddev=s, dtype=dtypes.float32)
            pos = random_ops.random_normal([20], mean=m_pos, stddev=s, dtype=dtypes.float32)

            data = array_ops.concat([neg, pos], 0)
            data = math_ops.cast(math_ops.round(data), tf_dtype)
            data = math_ops.minimum(math_ops.maximum(data, 0), 1)
            lab = array_ops.concat([array_ops.zeros([20], dtype=tf_dtype), array_ops.ones([20], dtype=tf_dtype)], 0)

            cm = confusion_matrix.confusion_matrix(lab, data, dtype=tf_dtype, num_classes=2)

            d, l, cm_out = sess.run([data, lab, cm], {m_neg: 0.0, m_pos: 1.0, s: 1.0})

            truth = np.zeros([2, 2], dtype=np_dtype)
            try:
                range_builder = xrange
            except NameError:  # In Python 3.
                range_builder = range
            for i in range_builder(len(d)):
                truth[l[i], d[i]] += 1

            self.assertEqual(cm_out.dtype, np_dtype)
            self.assertAllClose(cm_out, truth, atol=1e-10)
예제 #2
0
  def update_state(self, y_true, y_pred, sample_weight=None):
    """Accumulates the confusion matrix statistics.
    Args:
      y_true: The ground truth values.
      y_pred: The predicted values.
      sample_weight: Optional weighting of each example. Defaults to 1. Can be a
        `Tensor` whose rank is either 0, or the same rank as `y_true`, and must
        be broadcastable to `y_true`.
    Returns:
      Update op.
    """

    y_true = math_ops.cast(y_true, self._dtype)
    y_pred = math_ops.cast(y_pred, self._dtype)

    # Flatten the input if its rank > 1.
    if y_pred.shape.ndims > 1:
      y_pred = array_ops.reshape(y_pred, [-1])

    if y_true.shape.ndims > 1:
      y_true = array_ops.reshape(y_true, [-1])

    if sample_weight is not None:
      sample_weight = math_ops.cast(sample_weight, self._dtype)
      if sample_weight.shape.ndims > 1:
        sample_weight = array_ops.reshape(sample_weight, [-1])

    # Accumulate the prediction to current confusion matrix.
    current_cm = confusion_matrix.confusion_matrix(
        y_true,
        y_pred,
        self.num_classes,
        weights=sample_weight,
        dtype=dtypes.float64)
    return self.total_cm.assign_add(current_cm)
예제 #3
0
  def _testConfMatrixOnTensors(self, tf_dtype, np_dtype):
    with self.cached_session() as sess:
      m_neg = array_ops.placeholder(dtype=dtypes.float32)
      m_pos = array_ops.placeholder(dtype=dtypes.float32)
      s = array_ops.placeholder(dtype=dtypes.float32)

      neg = random_ops.random_normal(
          [20], mean=m_neg, stddev=s, dtype=dtypes.float32)
      pos = random_ops.random_normal(
          [20], mean=m_pos, stddev=s, dtype=dtypes.float32)

      data = array_ops.concat([neg, pos], 0)
      data = math_ops.cast(math_ops.round(data), tf_dtype)
      data = math_ops.minimum(math_ops.maximum(data, 0), 1)
      lab = array_ops.concat(
          [
              array_ops.zeros(
                  [20], dtype=tf_dtype), array_ops.ones(
                      [20], dtype=tf_dtype)
          ],
          0)

      cm = confusion_matrix.confusion_matrix(
          lab, data, dtype=tf_dtype, num_classes=2)

      d, l, cm_out = sess.run([data, lab, cm], {m_neg: 0.0, m_pos: 1.0, s: 1.0})

      truth = np.zeros([2, 2], dtype=np_dtype)
      for i in xrange(len(d)):
        truth[l[i], d[i]] += 1

      self.assertEqual(cm_out.dtype, np_dtype)
      self.assertAllClose(cm_out, truth, atol=1e-10)
예제 #4
0
파일: metrics.py 프로젝트: UofM-DSP/WebDI
def streaming_confusion_matrix_with_current_cm(labels,
                                               predictions,
                                               num_classes,
                                               weights=None):
    total_cm = metric_variable([num_classes, num_classes],
                               dtypes.float32,
                               name='total_confusion_matrix')

    # Cast the type to int64 required by confusion_matrix_ops.
    predictions = math_ops.to_int64(predictions)
    labels = math_ops.to_int64(labels)
    num_classes = math_ops.to_int64(num_classes)

    # Flatten the input if its rank > 1.
    if predictions.get_shape().ndims > 1:
        predictions = array_ops.reshape(predictions, [-1])

    if labels.get_shape().ndims > 1:
        labels = array_ops.reshape(labels, [-1])

    if (weights is not None) and (weights.get_shape().ndims > 1):
        weights = array_ops.reshape(weights, [-1])

    # Accumulate the prediction to current confusion matrix.
    current_cm = confusion_matrix.confusion_matrix(labels,
                                                   predictions,
                                                   num_classes,
                                                   weights=weights,
                                                   dtype=dtypes.float32)
    update_op = state_ops.assign_add(total_cm, current_cm)
    return (total_cm, update_op, current_cm)
예제 #5
0
 def testOutputIsInt64(self):
     labels = np.arange(2)
     predictions = np.arange(2)
     with self.test_session():
         cm = confusion_matrix.confusion_matrix(labels, predictions, dtype=dtypes.int64)
         tf_cm = cm.eval()
     self.assertEqual(tf_cm.dtype, np.int64)
예제 #6
0
 def testExample(self):
     """This is a test of the example provided in pydoc."""
     with self.test_session():
         self.assertAllEqual(
             [[0, 0, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 1]],
             confusion_matrix.confusion_matrix(labels=[1, 2, 4], predictions=[2, 2, 4]).eval(),
         )
예제 #7
0
def streaming_confusion_matrix(labels,
                               predictions,
                               num_classes,
                               weights=None,
                               output_dir="./output/results_no_train"):
    """Calculate a streaming confusion matrix.
    Calculates a confusion matrix. For estimation over a stream of data,
    the function creates an  `update_op` operation.
    Args:
        labels: A `Tensor` of ground truth labels with shape [batch size] and of
        type `int32` or `int64`. The tensor will be flattened if its rank > 1.
        predictions: A `Tensor` of prediction results for semantic labels, whose
        shape is [batch size] and type `int32` or `int64`. The tensor will be
        flattened if its rank > 1.
        num_classes: The possible number of labels the prediction task can
        have. This value must be provided, since a confusion matrix of
        dimension = [num_classes, num_classes] will be allocated.
        weights: Optional `Tensor` whose rank is either 0, or the same rank as
        `labels`, and must be broadcastable to `labels` (i.e., all dimensions must
        be either `1`, or the same as the corresponding `labels` dimension).
    Returns:
        total_cm: A `Tensor` representing the confusion matrix.
        update_op: An operation that increments the confusion matrix.
    """
    # Local variable to accumulate the predictions in the confusion matrix.
    total_cm = metric_variable([num_classes, num_classes],
                               dtypes.float64,
                               name='total_confusion_matrix')

    # Cast the type to int64 required by confusion_matrix_ops.
    predictions = math_ops.to_int64(predictions)
    labels = math_ops.to_int64(labels)
    num_classes = math_ops.to_int64(num_classes)

    debug_file = os.path.join(output_dir, "debugging.txt")
    with open(debug_file, "a+") as wf:
        wf.write("\n\n***** Debugging *****\n\n")
        wf.write("Predictions: {} \n".format(predictions))
        wf.write("Labels: {} \n".format(labels))
        wf.write("Num of Classes: {} \n".format(num_classes))

    # Flatten the input if its rank > 1.
    if predictions.get_shape().ndims > 1:
        predictions = array_ops.reshape(predictions, [-1])

    if labels.get_shape().ndims > 1:
        labels = array_ops.reshape(labels, [-1])

    if (weights is not None) and (weights.get_shape().ndims > 1):
        weights = array_ops.reshape(weights, [-1])

    # Accumulate the prediction to current confusion matrix.
    current_cm = confusion_matrix.confusion_matrix(labels,
                                                   predictions,
                                                   num_classes,
                                                   weights=weights,
                                                   dtype=dtypes.float64)
    update_op = state_ops.assign_add(total_cm, current_cm)
    return (total_cm, update_op)
예제 #8
0
 def calculate_confusion_matrix(self, x_test, y_test):
     """Calculate the probabilities required for the confusion matrix and create a dataframe"""
     y_pred = self.model.predict_classes(x_test)
     y_test = argmax(y_test, axis=1)
     con_mat = confusion_matrix(labels=y_test, predictions=y_pred).numpy()
     con_mat_norm = np.around(con_mat.astype('float') / con_mat.sum(axis=1)[:, np.newaxis], decimals=2)
     classes = self.le.inverse_transform(list(range(0, self.le.encoded_labels.shape[1])))
     return pd.DataFrame(con_mat_norm, index=classes, columns=classes)
 def _testConfMatrix(self, predictions, labels, truth, weights=None):
   with self.test_session():
     dtype = predictions.dtype
     ans = confusion_matrix.confusion_matrix(
         labels, predictions, dtype=dtype, weights=weights)
     tf_ans = ans.eval()
     self.assertAllClose(tf_ans, truth, atol=1e-10)
     self.assertEqual(tf_ans.dtype, dtype)
예제 #10
0
 def testOutputIsInt32(self):
   labels = np.arange(2)
   predictions = np.arange(2)
   with self.cached_session():
     cm = confusion_matrix.confusion_matrix(
         labels, predictions, dtype=dtypes.int32)
     tf_cm = self.evaluate(cm)
   self.assertEqual(tf_cm.dtype, np.int32)
예제 #11
0
 def testOutputIsInt32(self):
   labels = np.arange(2)
   predictions = np.arange(2)
   with self.test_session():
     cm = confusion_matrix.confusion_matrix(
         labels, predictions, dtype=dtypes.int32)
     tf_cm = cm.eval()
   self.assertEqual(tf_cm.dtype, np.int32)
예제 #12
0
 def testOutputIsInt64(self):
   labels = np.arange(2)
   predictions = np.arange(2)
   with self.cached_session():
     cm = confusion_matrix.confusion_matrix(
         labels, predictions, dtype=dtypes.int64)
     tf_cm = self.evaluate(cm)
   self.assertEqual(tf_cm.dtype, np.int64)
예제 #13
0
def calculate_confusion_matrix(model, le, x_test, y_test):
    y_pred = model.predict_classes(x_test)
    y_test = argmax(y_test, axis=1)
    con_mat = confusion_matrix(labels=y_test, predictions=y_pred).numpy()
    con_mat_norm = np.around(con_mat.astype('float') /
                             con_mat.sum(axis=1)[:, np.newaxis],
                             decimals=2)
    classes = le.inverse_transform([0, 1, 2, 3, 4])
    return pd.DataFrame(con_mat_norm, index=classes, columns=classes)
예제 #14
0
 def _testConfMatrix(self, labels, predictions, truth, weights=None,
                     num_classes=None):
   with self.cached_session():
     dtype = predictions.dtype
     ans = confusion_matrix.confusion_matrix(
         labels, predictions, dtype=dtype, weights=weights,
         num_classes=num_classes).eval()
     self.assertAllClose(truth, ans, atol=1e-10)
     self.assertEqual(ans.dtype, dtype)
예제 #15
0
 def _testConfMatrix(self, labels, predictions, truth, weights=None,
                     num_classes=None):
   with self.cached_session():
     dtype = predictions.dtype
     ans = confusion_matrix.confusion_matrix(
         labels, predictions, dtype=dtype, weights=weights,
         num_classes=num_classes).eval()
     self.assertAllClose(truth, ans, atol=1e-10)
     self.assertEqual(ans.dtype, dtype)
예제 #16
0
 def testExample(self):
     """This is a test of the example provided in pydoc."""
     with self.cached_session():
         self.assertAllEqual(
             [[0, 0, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 1, 0, 0],
              [0, 0, 0, 0, 0], [0, 0, 0, 0, 1]],
             self.evaluate(
                 confusion_matrix.confusion_matrix(labels=[1, 2, 4],
                                                   predictions=[2, 2, 4])))
예제 #17
0
 def _testConfMatrix(self, predictions, labels, truth, weights=None):
     with self.test_session():
         dtype = predictions.dtype
         ans = confusion_matrix.confusion_matrix(labels,
                                                 predictions,
                                                 dtype=dtype,
                                                 weights=weights)
         tf_ans = ans.eval()
         self.assertAllClose(tf_ans, truth, atol=1e-10)
         self.assertEqual(tf_ans.dtype, dtype)
예제 #18
0
    def cal_loss(self):
        one_hot_annotations = tf.one_hot(self.annotations,
                                         depth=self.conf.class_num,
                                         axis=self.channel_axis,
                                         name='annotations/one_hot')
        losses = tf.losses.softmax_cross_entropy(one_hot_annotations,
                                                 self.predictions,
                                                 scope='loss/losses')
        self.loss_op = tf.reduce_mean(losses, name='loss/loss_op')
        self.decoded_predictions = tf.argmax(self.predictions,
                                             self.channel_axis,
                                             name='accuracy/decode_pred')
        correct_prediction = tf.equal(self.annotations,
                                      self.decoded_predictions,
                                      name='accuracy/correct_pred')
        self.accuracy_op = tf.reduce_mean(tf.cast(correct_prediction,
                                                  tf.float32,
                                                  name='accuracy/cast'),
                                          name='accuracy/accuracy_op')
        weights = tf.cast(tf.greater(self.decoded_predictions,
                                     0,
                                     name='m_iou/greater'),
                          tf.int32,
                          name='m_iou/weights')
        self.m_iou, self.miou_op = tf.metrics.mean_iou(
            self.annotations,
            self.decoded_predictions,
            self.conf.class_num,
            weights,
            name='m_iou/m_ious')

        # Flatten the input if its rank > 1.
        predictions = self.decoded_predictions
        if predictions.get_shape().ndims > 1:
            predictions = array_ops.reshape(predictions, [-1])

        labels = self.annotations
        if labels.get_shape().ndims > 1:
            labels = array_ops.reshape(labels, [-1])

        weights_conf = weights
        if (weights_conf is not None) and (weights_conf.get_shape().ndims > 1):
            weights_conf = array_ops.reshape(weights_conf, [-1])

        # Cast the type to int64 required by confusion_matrix_ops.
        predictions = math_ops.to_int64(predictions)
        labels = math_ops.to_int64(labels)

        self.confusion_matrix = confusion_matrix.confusion_matrix(
            labels,
            predictions,
            self.conf.class_num,
            weights=weights_conf,
            dtype=tf.int32,
            name='confu_matrix/confu_matrix_op')
예제 #19
0
파일: metrics.py 프로젝트: zhengduoru/tf2
    def streaming_confusion_matrix_single_label(self,
                                                labels,
                                                logits,
                                                num_label_classes,
                                                weights=None):
        """Calculate a streaming confusion matrix.
    
        Calculates a confusion matrix. For estimation over a stream of data,
        the function creates an  `update_op` operation.
    
        Args:
          labels: A `Tensor` of ground truth labels with shape [batch size] and of
            type `int32` or `int64`. The tensor will be flattened if its rank > 1.
          logits: A `Tensor` of prediction results for semantic labels, whose
            shape is [batch size] and type `int32` or `int64`. The tensor will be
            flattened if its rank > 1.
          num_label_classes: The possible number of labels the prediction task can
            have. This value must be provided, since a confusion matrix of
            dimension = [num_label_classes, num_label_classes] will be allocated.
          weights: Optional `Tensor` whose rank is either 0, or the same rank as
            `labels`, and must be broadcastable to `labels` (i.e., all dimensions must
            be either `1`, or the same as the corresponding `labels` dimension).
    
        Returns:
          total_cm: A `Tensor` representing the confusion matrix.
          update_op: An operation that increments the confusion matrix.
        """
        # Local variable to accumulate the logits in the confusion matrix.
        total_cm = _create_local('total_confusion_matrix',
                                 shape=[num_label_classes, num_label_classes],
                                 dtype=dtypes.float64)

        # Cast the type to int64 required by confusion_matrix_ops.
        logits = math_ops.to_int64(logits)
        labels = math_ops.to_int64(labels)
        num_label_classes = math_ops.to_int64(num_label_classes)

        # Flatten the input if its rank > 1.
        if logits.get_shape().ndims > 1:
            logits = array_ops.reshape(logits, [-1])

        if labels.get_shape().ndims > 1:
            labels = array_ops.reshape(labels, [-1])

        if (weights is not None) and (weights.get_shape().ndims > 1):
            weights = array_ops.reshape(weights, [-1])

        # Accumulate the prediction to current confusion matrix.
        current_cm = confusion_matrix.confusion_matrix(labels,
                                                       logits,
                                                       num_label_classes,
                                                       weights=weights,
                                                       dtype=dtypes.float64)
        update_op = state_ops.assign_add(total_cm, current_cm)
        return total_cm, update_op
예제 #20
0
def _streaming_confusion_matrix(labels, predictions, num_classes, weights=None,
                                metrics_collections=None,
                                updates_collections=None,
                                name=None):
    """Calculate a streaming confusion matrix.

    Calculates a confusion matrix. For estimation over a stream of data,
    the function creates an  `update_op` operation.

    Args:
      labels: A `Tensor` of ground truth labels with shape [batch size] and of
        type `int32` or `int64`. The tensor will be flattened if its rank > 1.
      predictions: A `Tensor` of prediction results for semantic labels, whose
        shape is [batch size] and type `int32` or `int64`. The tensor will be
        flattened if its rank > 1.
      num_classes: The possible number of labels the prediction task can
        have. This value must be provided, since a confusion matrix of
        dimension = [num_classes, num_classes] will be allocated.
      weights: Optional `Tensor` whose rank is either 0, or the same rank as
        `labels`, and must be broadcastable to `labels` (i.e., all dimensions must
        be either `1`, or the same as the corresponding `labels` dimension).

    Returns:
      total_cm: A `Tensor` representing the confusion matrix.
      update_op: An operation that increments the confusion matrix.
    """
    # Local variable to accumulate the predictions in the confusion matrix.
    with tf.variable_scope(name, 'confusion_matrix'):
        total_cm = metric_variable(
            [num_classes, num_classes], tf.float64, name='total_confusion_matrix')

        # Cast the type to int64 required by confusion_matrix_ops.
        predictions = tf.to_int64(predictions)
        labels = tf.to_int64(labels)
        num_classes = tf.to_int64(num_classes)

        # Flatten the input if its rank > 1.
        if predictions.get_shape().ndims > 1:
            predictions = tf.reshape(predictions, [-1])

        if labels.get_shape().ndims > 1:
            labels = tf.reshape(labels, [-1])

        if (weights is not None) and (weights.get_shape().ndims > 1):
            weights = tf.reshape(weights, [-1])

        # Accumulate the prediction to current confusion matrix.
        current_cm = confusion_matrix.confusion_matrix(
            labels, predictions, num_classes, weights=weights, dtype=tf.float64)
        update_op = tf.assign_add(total_cm, current_cm)
        if metrics_collections:
            tf.add_to_collection(metrics_collections, total_cm)
        if updates_collections:
            tf.add_to_collection(updates_collections, update_op)
        return total_cm, update_op
예제 #21
0
def compute_m_iou_accu(labels,
                       predictions,
                       num_classes,
                       weights=None,
                       name=None):
    """Calculate per-step mean Intersection-Over-Union (mIOU).
    """
    # Check if shape is compatible.
    predictions.get_shape().assert_is_compatible_with(labels.get_shape())

    predictions = math_ops.to_int64(predictions)
    labels = math_ops.to_int64(labels)
    num_classes = math_ops.to_int64(num_classes)
    current_cm = confusion_matrix.confusion_matrix(labels,
                                                   predictions,
                                                   num_classes,
                                                   weights=weights,
                                                   dtype=dtypes.float64)

    sum_over_row = math_ops.to_float(math_ops.reduce_sum(current_cm, 0))
    sum_over_col = math_ops.to_float(math_ops.reduce_sum(current_cm, 1))
    cm_diag = math_ops.to_float(array_ops.diag_part(current_cm))
    denominator = sum_over_row + sum_over_col - cm_diag

    # The mean is only computed over classes that appear in the
    # label or prediction tensor. If the denominator is 0, we need to
    # ignore the class.
    num_valid_entries = math_ops.reduce_sum(
        math_ops.cast(math_ops.not_equal(denominator, 0),
                      dtype=dtypes.float32))

    # If the value of the denominator is 0, set it to 1 to avoid
    # zero division.
    denominator = array_ops.where(math_ops.greater(denominator,
                                                   0), denominator,
                                  array_ops.ones_like(denominator))
    iou = math_ops.div(cm_diag, denominator)

    # If the number of valid entries is 0 (no classes) we return 0.
    m_iou = array_ops.where(math_ops.greater(num_valid_entries, 0),
                            math_ops.reduce_sum(iou) / num_valid_entries, 0)

    class_count = array_ops.where(math_ops.greater(sum_over_col, 0),
                                  sum_over_col,
                                  array_ops.ones_like(sum_over_col))

    accu_per_class = math_ops.div(cm_diag, class_count)
    m_accu = array_ops.where(
        math_ops.greater(num_valid_entries, 0),
        math_ops.reduce_sum(accu_per_class) / num_valid_entries, 0)

    return m_iou, m_accu
예제 #22
0
def confusion_matrix(labels,
                     predictions,
                     num_classes=None,
                     dtype=dtypes.int32,
                     name=None,
                     weights=None):
    """Deprecated. Use tf.math.confusion_matrix instead."""
    return cm.confusion_matrix(labels=labels,
                               predictions=predictions,
                               num_classes=num_classes,
                               dtype=dtype,
                               name=name,
                               weights=weights)
예제 #23
0
    def update_state(self, labels, predicts, weights=None):
        predicts = engine.int64(predicts)
        labels = engine.int64(labels)
        if labels.get_shape().ndims > 1:
            labels = array_ops.reshape(labels, [-1])
        if predicts.get_shape().ndims > 1:
            predicts = array_ops.reshape(predicts, [-1])
        if weights is not None and weights.get_shape().ndims > 1:
            weights = array_ops.reshape(weights, [-1])

        add_value = confusion_matrix(labels,
                                     predicts,
                                     num_classes=self.num_classes,
                                     weights=weights,
                                     dtype=dtypes.float64)
        with fops.control_dependencies([self.total_value]):
            self.update_ops.append(
                state_ops.assign_add(self.total_value, add_value))

        sum_alone_row = engine.float32(math_ops.reduce_sum(
            self.total_value, 0))
        sum_alone_col = engine.float32(math_ops.reduce_sum(
            self.total_value, 1))

        diag = engine.float32(array_ops.diag_part(self.total_value))
        denominator = sum_alone_row + sum_alone_col - diag
        valid_entries = math_ops.reduce_sum(
            engine.float32(math_ops.not_equal(denominator, 0)))

        denominator = array_ops.where(math_ops.greater(denominator, 0),
                                      denominator,
                                      array_ops.ones_like(denominator))

        iou = math_ops.div(diag, denominator)
        self._mean_iou = array_ops.where(math_ops.greater(valid_entries, 0),
                                         math_ops.reduce_sum(iou) /
                                         valid_entries,
                                         0,
                                         name='mean_iou')
        self._classes_iou = array_ops.where(math_ops.not_equal(denominator, 0),
                                            iou,
                                            array_ops.zeros_like(denominator),
                                            name='classes_iou')
예제 #24
0
    def update_state(self, y_true, y_pred, sample_weight=None):
        """Accumulated the confusion matrix statistics with one hot truth and prediction data.
        
        Parameters
        ----------
        y_true: Tensor or numpy array. 
            One hot ground truth vectors.
        y_pred: Tensor or numpy array.
            One hot predicted vectors.
        sample_weight: Tensor.
            Optional weighting of each example. Defaults to 1. Can be a
            `Tensor` whose rank is either 0, or the same rank as `y_true`, and must
            be broadcastable to `y_true`.
            
        Returns
        -------
        Update operator.
            Operator
        """
        # Convert one hot vectors and labels.
        y_pred = K.argmax(y_pred)

        y_true = math_ops.cast(y_true, self._dtype)
        y_pred = math_ops.cast(y_pred, self._dtype)

        # Flatten the input if its rank > 1.
        if y_pred.shape.ndims > 1:
            y_pred = array_ops.reshape(y_pred, [-1])

        if y_true.shape.ndims > 1:
            y_true = array_ops.reshape(y_true, [-1])

        if sample_weight is not None and sample_weight.shape.ndims > 1:
            sample_weight = array_ops.reshape(sample_weight, [-1])

        # Accumulate the prediction to current confusion matrix.
        current_cm = confusion_matrix.confusion_matrix(y_true,
                                                       y_pred,
                                                       self.num_classes,
                                                       weights=sample_weight,
                                                       dtype=dtypes.float64)
        return self.total_cm.assign_add(current_cm) if self.accum_enable \
            else self.total_cm.assign(current_cm)
def meanIOU(true_mask, pred_mask):
    pred_mask = tf.argmax(pred_mask, axis=-1)
    pred_mask = tf.reshape(tf.squeeze(pred_mask), [-1])
    true_mask = tf.reshape(tf.squeeze(true_mask), [-1])

    confusion_mat = confusion_matrix.confusion_matrix(true_mask,
                                                      pred_mask,
                                                      14,
                                                      weights=None)

    sum_row = tf.reduce_sum(confusion_mat, 0)
    sum_col = tf.reduce_sum(confusion_mat, 1)

    true_pos = tf.matrix_diag_part(confusion_mat)
    denominator = tf.cast(sum_row + sum_col - true_pos, tf.float32)
    num_valid_entries = tf.reduce_sum(
        tf.cast(tf.math.not_equal(denominator, 0), 'float32'))

    iou = tf.math.divide_no_nan(tf.cast(true_pos, tf.float32), denominator)
    mIOU = tf.math.divide_no_nan(tf.reduce_sum(iou, name='mean_iou'),
                                 num_valid_entries)
    return mIOU
    def train(self, hands, poses, show_training_images=False):
        x = {'train': [], 'val': [], 'test': []}
        y = {'train': [], 'val': [], 'test': []}
        enc = OneHotEncoder(handle_unknown='ignore', sparse=False)
        if self.gesture_image_files is None:
            self.gesture_image_files = self.scan_image_files()

        for hand in hands:
            for pose in poses:
                for t in ['train', 'val', 'test']:
                    for file in self.gesture_image_files[hand][pose][
                            f'{t}_filenames']:
                        img = keras.preprocessing.image.load_img(
                            file, color_mode='rgb')
                        img_array = keras.preprocessing.image.img_to_array(img)
                        img_array = keras.preprocessing.image.smart_resize(
                            img_array, (40, 40), interpolation='bilinear')
                        x[t].append(img_array)
                        y[t].append('{}_{}'.format(hand, pose))

        train_datagen = ImageDataGenerator(
            #preprocessing_function=keras.applications.vgg16.preprocess_input,
            width_shift_range=0.1,
            height_shift_range=0.1,
            fill_mode='nearest',
            rescale=1. / 255)

        val_datagen = ImageDataGenerator(
            #preprocessing_function=keras.applications.vgg16.preprocess_input,
            width_shift_range=0.1,
            height_shift_range=0.1,
            fill_mode='nearest',
            rescale=1. / 255)

        test_datagen = ImageDataGenerator(
            #preprocessing_function=keras.applications.vgg16.preprocess_input,
            rescale=1. / 255)

        for t in ['train', 'val', 'test']:
            x[t] = np.array(x[t])
            y[t] = np.array(y[t]).reshape(-1, 1)
            y[t] = enc.fit_transform(y[t])  # Encode Y using OneHot

        train_datagen.fit(x['train'], augment=True)
        val_datagen.fit(x['val'], augment=True)
        test_datagen.fit(x['test'])

        if show_training_images:
            for x_batch, y_batch in train_datagen.flow(x['train'],
                                                       y['train'],
                                                       batch_size=100):
                for i in range(0, len(x_batch)):
                    subplot = pyplot.subplot(10, 10, i + 1)
                    subplot.set_title(
                        enc.inverse_transform(y_batch[i].reshape(1, -1))[0][0])
                    pyplot.imshow(x_batch[i])
                pyplot.subplots_adjust(left=0,
                                       right=1.0,
                                       bottom=0.025,
                                       top=0.975,
                                       wspace=0.155,
                                       hspace=0.470)
                pyplot.get_current_fig_manager().window.showMaximized()
                pyplot.show()
                break

        print('About to train with {} training images'.format(len(x['train'])))

        learning_rate = 0.001
        epocs = 90
        batch_size = 14
        model_name = 'hand-pose-right-{}-{}.h5'.format(datetime.date.today(),
                                                       epocs)

        train_gen = train_datagen.flow(x['train'],
                                       y['train'],
                                       batch_size=batch_size,
                                       shuffle=True)
        val_gen = val_datagen.flow(x['val'],
                                   y['val'],
                                   batch_size=batch_size,
                                   shuffle=True)
        test_gen = test_datagen.flow(x['test'],
                                     y['test'],
                                     batch_size=batch_size)

        model = Sequential()
        model.add(
            Conv2D(filters=32,
                   kernel_size=(3, 3),
                   activation='relu',
                   input_shape=x['train'][0].shape))
        model.add(Conv2D(filters=64, kernel_size=(3, 3), activation='relu'))
        model.add(MaxPooling2D(pool_size=(2, 2)))
        model.add(Dropout(rate=0.25))
        model.add(Flatten())
        model.add(Dense(units=128, activation='relu'))
        model.add(Dropout(rate=0.5))
        model.add(Dense(units=len(enc.categories_[0]), activation='softmax'))
        model.compile(
            loss=keras.losses.categorical_crossentropy,
            optimizer=keras.optimizers.Adam(learning_rate=learning_rate),
            metrics=['accuracy'])

        print("Training on {} training images, {} validation images model {}".
              format(train_gen.n, val_gen.n, model_name))

        model.fit(train_gen,
                  epochs=epocs,
                  steps_per_epoch=train_gen.n // train_gen.batch_size,
                  validation_data=val_gen,
                  verbose=2)

        # Save the model and the one-hot encoding so we can decode later
        model.save(model_name)
        np.save(f'{model_name}', enc.categories_[0])

        predictions = model.predict(x=test_gen.x, verbose=2)
        y_pred = np.argmax(predictions, axis=1)
        cm_labels = np.argmax(test_gen.y, axis=1)
        cm = confusion_matrix(labels=cm_labels, predictions=y_pred).numpy()
        cm_norm = np.around(cm.astype('float') / cm.sum(axis=1)[:, np.newaxis],
                            decimals=2)
        cm_df = pd.DataFrame(cm_norm,
                             index=enc.categories_[0],
                             columns=enc.categories_[0])
        figure = plt.figure(figsize=(8, 8))
        sns.heatmap(cm_df, annot=True, cmap=plt.cm.Blues)
        plt.tight_layout()
        plt.ylabel('True label')
        plt.xlabel('Predicted label')
        plt.show()
예제 #27
0
    model.fit(X_train_array,
              y_train_array,
              validation_data=(X_test_array, y_test_array),
              epochs=5,
              batch_size=64)
    model.save("myModel.h5")

    predications = model.predict(X_test_array)
    predications = predications >= 0.5

    model.summary()
    scores = model.evaluate(X_test_array, y_test_array, verbose=0)
    print("Accuracy: %.2f%%" % (scores[1] * 100))
    print("Accuracy:", metrics.accuracy_score(y_test_array, predications))
    y_pred2 = model.predict_classes(X_test_array)
    print(confusion_matrix(y_test_array, y_pred2))

    # emb_model.fit(x=X_train_seq_trunc, y=y_train, batch_size=128, epochs=10, validation_data=(X_test_seq_trunc, y_test))
    doc = ["COVID does not exist"]

    tk.fit_on_texts(doc)
    test_text = tk.texts_to_sequences(doc)
    print(test_text)
    test_seq = pad_sequences(test_text, maxlen=100)
    predications = model.predict(test_seq)
    print(test_seq)
    print(predications)

    # print(predications)
    predications = (predications < 0.5)
    print(predications)
예제 #28
0
def confusion_matrix(labels, predictions, num_classes=None, dtype=dtypes.int32,
                     name=None, weights=None):
  """Deprecated. Use tf.confusion_matrix instead."""
  return cm.confusion_matrix(labels=labels, predictions=predictions,
                             num_classes=num_classes, dtype=dtype, name=name,
                             weights=weights)