常用损失函数的一些总结:
分类任务经常使用的交叉熵损失函数,
tf.losses.softmax_cross_entropy()
这里的标签必须是one_hot型的,如果类别使用的是数值表示可以用以下函数取代
tf.losses.sparse_softmax_cross_entropy()
对于图像到图像的任务常用的有MSE或者MAE等:
loss=tf.losses.mean_squared_error(labels,predictions)
loss=tf.reduce_mean(tf.square(labels-predictions))
loss=tf.reduce_mean(tf.abs(labels-predictions))
图像到图像的任务为了增加图像细节可以使用GradientLoss:
import tensorflow as tf
def gray_sobel(inputs):assert inputs.shape[3]==1filter_x=tf.constant([[-1,0,1],[-2,0,2],[-1,0,1]],tf.float32)filter_x = tf.reshape(filter_x,[3,3,1,1])filter_y = tf.transpose(filter_x,[1,0,2,3])res_x = tf.nn.conv2d(inputs,filter_x,strides=[1,1,1,1],padding='SAME')res_y = tf.nn.conv2d(inputs,filter_y,strides=[1,1,1,1],padding='SAME')res_xy = tf.sqrt(tf.square(res_x)+tf.square(res_y))return res_xydef rgb_sobel(inputs):assert inputs.shape[3]==3inputs1=inputs[:,:,:,:1]inputs2=inputs[:,:,:,1:2]inputs3=inputs[:,:,:,2:3]res_xy = tf.concat((gray_sobel(inputs1),gray_sobel(inputs2),gray_sobel(inputs3)),axis=3)return res_xy
def GradientLoss(pred_out,ground_truth):grad_loss=tf.reduce_mean(tf.square(rgb_sobel(pred_out)-rgb_sobel(ground_truth)))return grad_loss
关于perceptual loss它的实现需要用到VGG网络的预训练模型获取特征。
import os
import tensorflow as tfimport numpy as np
import time
import inspectVGG_MEAN = [103.939, 116.779, 123.68]class Vgg19:def __init__(self, vgg19_npy_path=None):if vgg19_npy_path is None:path = inspect.getfile(Vgg19)path = os.path.abspath(os.path.join(path, os.pardir))path = os.path.join(path, "vgg19.npy")vgg19_npy_path = pathprint(vgg19_npy_path)self.data_dict = np.load(vgg19_npy_path,allow_pickle=True, encoding='latin1').item()print("npy file loaded")def build(self, rgb):"""load variable from npy to build the VGG:param rgb: rgb image [batch, height, width, 3] values scaled [-1, 1]"""start_time = time.time()print("build model started")rgb_scaled = rgb * 255.0# Convert RGB to BGRred, green, blue = tf.split(axis=3, num_or_size_splits=3, value = rgb_scaled)bgr = tf.concat(axis=3, values=[blue - VGG_MEAN[0],green - VGG_MEAN[1],red - VGG_MEAN[2],])self.conv1_1 = self.conv_layer(bgr, "conv1_1")self.relu1_1 = self.relu_layer(self.conv1_1, "relu1_1")self.conv1_2 = self.conv_layer(self.relu1_1, "conv1_2")self.relu1_2 = self.relu_layer(self.conv1_2, "relu1_2")self.pool1 = self.max_pool(self.relu1_2, 'pool1')self.conv2_1 = self.conv_layer(self.pool1, "conv2_1")self.relu2_1 = self.relu_layer(self.conv2_1, "relu2_1")self.conv2_2 = self.conv_layer(self.relu2_1, "conv2_2")self.relu2_2 = self.relu_layer(self.conv2_2, "relu2_2")self.pool2 = self.max_pool(self.relu2_2, 'pool2')self.conv3_1 = self.conv_layer(self.pool2, "conv3_1")self.relu3_1 = self.relu_layer(self.conv3_1, "relu3_1")self.conv3_2 = self.conv_layer(self.relu3_1, "conv3_2")self.relu3_2 = self.relu_layer(self.conv3_2, "relu3_2")self.conv3_3 = self.conv_layer(self.relu3_2, "conv3_3")self.relu3_3 = self.relu_layer(self.conv3_3, "relu3_3")self.conv3_4 = self.conv_layer(self.relu3_3, "conv3_4")self.relu3_4 = self.relu_layer(self.conv3_4, "relu3_4")self.pool3 = self.max_pool(self.relu3_4, 'pool3')self.conv4_1 = self.conv_layer(self.pool3, "conv4_1")self.relu4_1 = self.relu_layer(self.conv4_1, "relu4_1")self.conv4_2 = self.conv_layer(self.relu4_1, "conv4_2")self.relu4_2 = self.relu_layer(self.conv4_2, "relu4_2")self.conv4_3 = self.conv_layer(self.relu4_2, "conv4_3")self.relu4_3 = self.relu_layer(self.conv4_3, "relu4_3")self.conv4_4 = self.conv_layer(self.relu4_3, "conv4_4")self.relu4_4 = self.relu_layer(self.conv4_4, "relu4_4")self.pool4 = self.max_pool(self.relu4_4, 'pool4')self.conv5_1 = self.conv_layer(self.pool4, "conv5_1")self.relu5_1 = self.relu_layer(self.conv5_1, "relu5_1")self.conv5_2 = self.conv_layer(self.relu5_1, "conv5_2")self.relu5_2 = self.relu_layer(self.conv5_2, "relu5_2")self.conv5_3 = self.conv_layer(self.relu5_2, "conv5_3")self.relu5_3 = self.relu_layer(self.conv5_3, "relu5_3")self.conv5_4 = self.conv_layer(self.relu5_3, "conv5_4")self.relu5_4 = self.relu_layer(self.conv5_4, "relu5_4")self.pool5 = self.max_pool(self.conv5_4, 'pool5')self.data_dict = Noneprint(("build model finished: %ds" % (time.time() - start_time)))def avg_pool(self, bottom, name):return tf.nn.avg_pool(bottom, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name=name)def max_pool(self, bottom, name):return tf.nn.max_pool(bottom, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name=name)def relu_layer(self, bottom, name):return tf.nn.relu(bottom, name = name)def conv_layer(self, bottom, name):with tf.variable_scope(name):filt = self.get_conv_filter(name)conv = tf.nn.conv2d(bottom, filt, [1, 1, 1, 1], padding='SAME')conv_biases = self.get_bias(name)bias = tf.nn.bias_add(conv, conv_biases)
# relu = tf.nn.relu(bias)return biasdef fc_layer(self, bottom, name):with tf.variable_scope(name):shape = bottom.get_shape().as_list()dim = 1for d in shape[1:]:dim *= dx = tf.reshape(bottom, [-1, dim])weights = self.get_fc_weight(name)biases = self.get_bias(name)# Fully connected layer. Note that the '+' operation automatically# broadcasts the biases.fc = tf.nn.bias_add(tf.matmul(x, weights), biases)return fcdef get_conv_filter(self, name):return tf.constant(self.data_dict[name][0], name="filter")def get_bias(self, name):return tf.constant(self.data_dict[name][1], name="biases")def get_fc_weight(self, name):return tf.constant(self.data_dict[name][0], name="weights")
这里放的是VGG19的代码初始化和计算perceptual loss:
def Perceptual_loss(logits,label,batch_size=1,vgg_model_path='./vgg19.npy'):vgg=Vgg19(vgg_model_path)vgg.build(tf.concat([label,logits],axis=0))conten_loss=tf.reduce_mean(tf.reduce_sum(tf.square(vgg.relu3_3[batch_size:]-vgg.relu3_3[:batch_size]),axis=3))return conten_loss
之后会更新一些其它的损失函数