nn/unet.py
def make_loss(self, label, loss_type='xent', **kwargs):
        """
        Make loss to optimize for the network
        U-Net's output is smaller than input, thus ground truth need to be cropped
        :param label: input labels, can be generated by tf.data.Dataset
        :param loss_type:
            xent: cross entropy loss
        :return:
        """
        with tf.variable_scope('loss'):
            pred_flat = tf.reshape(self.pred, [-1, self.class_num])
            _, w, h, _ = label.get_shape().as_list()
            y = tf.image.resize_image_with_crop_or_pad(label, w - self.get_overlap(), h - self.get_overlap())
            y_flat = tf.reshape(tf.squeeze(y, axis=[3]), [-1, ])
            indices = tf.squeeze(tf.where(tf.less_equal(y_flat, self.class_num - 1)), 1)
            gt = tf.gather(y_flat, indices)
            prediction = tf.gather(pred_flat, indices)

            pred = tf.argmax(prediction, axis=-1, output_type=tf.int32)
            if self.class_num == 2:
                self.loss_iou = self.create_resetable_metric_single_iou(scope=tf.get_variable_scope().name,
                    labels=gt, predictions=pred, num_classes=self.class_num, name='loss_iou')
            else:
                self.loss_iou = self.create_resetable_metric(tf.metrics.mean_iou, var_name='loss_iou',
                                                             scope=tf.get_variable_scope().name,
                                                             labels=gt, predictions=pred, num_classes=self.class_num,
                                                             name='loss_iou')

            if loss_type == 'xent':
                if 'pos_weight' in kwargs:
                    self.loss = tf.reduce_mean(tf.nn.weighted_cross_entropy_with_logits(
                        logits=prediction, labels=gt, pos_weight=kwargs['pos_weight']))
                else:
                    self.loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(
                        logits=prediction, labels=gt))
                self.loss_xent = self.create_resetable_metric(tf.metrics.mean, var_name='loss_xent',
                                                              scope=tf.get_variable_scope().name,
                                                              values=self.loss, name='loss_xent')
            else:
                # TODO focal loss:
                # https://github.com/ailias/Focal-Loss-implement-on-Tensorflow/blob/master/focal_loss.py
                self.loss = None

        Make loss to optimize for the network
        U-Net's output is smaller than input, thus ground truth need to be cropped
        :param label: input labels, can be generated by tf.data.Dataset
        :param loss_type:
            xent: cross entropy loss
        :return:
        
Make loss to optimize for the network
U-Net's output is smaller than input, thus ground truth need to be cropped
Make loss to optimize for the network U - Net ' s output is smaller than input thus ground truth need to be cropped
def make_loss(self, label, loss_type='xent', **kwargs):
        with tf.variable_scope('loss'):
            pred_flat = tf.reshape(self.pred, [-1, self.class_num])
            _, w, h, _ = label.get_shape().as_list()
            y = tf.image.resize_image_with_crop_or_pad(label, w - self.get_overlap(), h - self.get_overlap())
            y_flat = tf.reshape(tf.squeeze(y, axis=[3]), [-1, ])
            indices = tf.squeeze(tf.where(tf.less_equal(y_flat, self.class_num - 1)), 1)
            gt = tf.gather(y_flat, indices)
            prediction = tf.gather(pred_flat, indices)
            pred = tf.argmax(prediction, axis=-1, output_type=tf.int32)
            if self.class_num == 2:
                self.loss_iou = self.create_resetable_metric_single_iou(scope=tf.get_variable_scope().name,
                    labels=gt, predictions=pred, num_classes=self.class_num, name='loss_iou')
            else:
                self.loss_iou = self.create_resetable_metric(tf.metrics.mean_iou, var_name='loss_iou',
                                                             scope=tf.get_variable_scope().name,
                                                             labels=gt, predictions=pred, num_classes=self.class_num,
                                                             name='loss_iou')
            if loss_type == 'xent':
                if 'pos_weight' in kwargs:
                    self.loss = tf.reduce_mean(tf.nn.weighted_cross_entropy_with_logits(
                        logits=prediction, labels=gt, pos_weight=kwargs['pos_weight']))
                else:
                    self.loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(
                        logits=prediction, labels=gt))
                self.loss_xent = self.create_resetable_metric(tf.metrics.mean, var_name='loss_xent',
                                                              scope=tf.get_variable_scope().name,
                                                              values=self.loss, name='loss_xent')
            else:
                self.loss = None
def make_loss ( self , label , loss_type = 'xent' , ** kwargs ) : with tf . variable_scope ( 'loss' ) : pred_flat = tf . reshape ( self . pred , [ - 1 , self . class_num ] ) _ , w , h , _ = label . get_shape ( ) . as_list ( ) y = tf . image . resize_image_with_crop_or_pad ( label , w - self . get_overlap ( ) , h - self . get_overlap ( ) ) y_flat = tf . reshape ( tf . squeeze ( y , axis = [ 3 ] ) , [ - 1 , ] ) indices = tf . squeeze ( tf . where ( tf . less_equal ( y_flat , self . class_num - 1 ) ) , 1 ) gt = tf . gather ( y_flat , indices ) prediction = tf . gather ( pred_flat , indices ) pred = tf . argmax ( prediction , axis = - 1 , output_type = tf . int32 ) if self . class_num == 2 : self . loss_iou = self . create_resetable_metric_single_iou ( scope = tf . get_variable_scope ( ) . name , labels = gt , predictions = pred , num_classes = self . class_num , name = 'loss_iou' ) else : self . loss_iou = self . create_resetable_metric ( tf . metrics . mean_iou , var_name = 'loss_iou' , scope = tf . get_variable_scope ( ) . name , labels = gt , predictions = pred , num_classes = self . class_num , name = 'loss_iou' ) if loss_type == 'xent' : if 'pos_weight' in kwargs : self . loss = tf . reduce_mean ( tf . nn . weighted_cross_entropy_with_logits ( logits = prediction , labels = gt , pos_weight = kwargs [ 'pos_weight' ] ) ) else : self . loss = tf . reduce_mean ( tf . nn . sparse_softmax_cross_entropy_with_logits ( logits = prediction , labels = gt ) ) self . loss_xent = self . create_resetable_metric ( tf . metrics . mean , var_name = 'loss_xent' , scope = tf . get_variable_scope ( ) . name , values = self . loss , name = 'loss_xent' ) else : self . loss = None
Make loss to optimize for the network
U-Net's output is smaller than input, thus ground truth need to be cropped
Make loss to optimize for the network U - Net ' s output is smaller than input thus ground truth need to be cropped
"""
        Make loss to optimize for the network
        U-Net's output is smaller than input, thus ground truth need to be cropped
        :param label: input labels, can be generated by tf.data.Dataset
        :param loss_type:
            xent: cross entropy loss
        :return:
        """ # TODO focal loss: # https://github.com/ailias/Focal-Loss-implement-on-Tensorflow/blob/master/focal_loss.py
None
self
label
input labels, can be generated by tf.data.Dataset
input labels can be generated by tf . data . Dataset
loss_type
cross entropy loss
cross entropy loss