def loss_func(self): with tf.name_scope('Loss'): self.y_prob = tf.nn.softmax(self.logits, axis=-1) y_one_hot = tf.one_hot(self.labels_pl, depth=self.conf.num_cls, axis=3, name='y_one_hot') if self.conf.weighted_loss: loss = weighted_cross_entropy(y_one_hot, self.logits, self.conf.num_cls, data=self.conf.data) else: if self.conf.loss_type == 'cross-entropy': with tf.name_scope('cross_entropy'): loss = cross_entropy(y_one_hot, self.logits, self.conf.num_cls) elif self.conf.loss_type == 'dice': with tf.name_scope('dice_coefficient'): loss = dice_coeff(y_one_hot, self.logits) with tf.name_scope('total'): if self.conf.use_reg: with tf.name_scope('L2_loss'): l2_loss = tf.reduce_sum(self.conf.lmbda * tf.stack([ tf.nn.l2_loss(v) for v in tf.get_collection('weights') ])) self.total_loss = loss + l2_loss else: self.total_loss = loss self.mean_loss, self.mean_loss_op = tf.metrics.mean( self.total_loss)
def loss_func(self): with tf.name_scope('Loss'): self.y_prob = tf.nn.softmax(self.logits, axis=-1) with tf.name_scope('cross_entropy'): loss = cross_entropy(self.labels_pl, self.logits) with tf.name_scope('total'): if self.conf.use_reg: with tf.name_scope('L2_loss'): l2_loss = tf.reduce_sum(self.conf.lmbda * tf.stack([ tf.nn.l2_loss(v) for v in tf.get_collection('weights') ])) self.total_loss = loss + l2_loss else: self.total_loss = loss self.mean_loss, self.mean_loss_op = tf.metrics.mean( self.total_loss)