- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在研究医学图像分割。我有两个类。 0 级作为背景,1 级作为病变。由于数据集高度不平衡,我使用损失函数作为(1 - 加权骰子系数)和度量函数作为骰子系数。我已将数据集从 0-255 标准化为 0-1。我正在使用带有 tensorflow 后端的 keras 来训练模型。在训练 UNet++ 模型时,我的损失函数随着每个时期而减少,但我的指标保持不变。我无法理解为什么随着损失按预期减少,指标是恒定的?另外,我无法理解,为什么当骰子系数返回 0 到 1 之间的值时损失大于 1?
这是我的损失函数:
def dice_loss(y_true, y_pred):
smooth = 1.
w1 = 0.3
w2 = 0.7
y_true_f = K.flatten(y_true[...,0])
y_pred_f = K.flatten(y_pred[...,0])
intersect = K.abs(K.sum(y_true_f * y_pred_f, axis = -1))
denom = K.abs(K.sum(y_true_f, axis = -1)) + K.abs(K.sum(y_pred_f, axis = -1))
coef1 = (2 * intersect + smooth) / (denom + smooth)
y_true_f1 = K.flatten(y_true[...,1])
y_pred_f1 = K.flatten(y_pred[...,1])
intersect1 = K.abs(K.sum(y_true_f1 * y_pred_f1, axis = -1))
denom1 = K.abs(K.sum(y_true_f1, axis = -1)) + K.abs(K.sum(y_pred_f1, axis = -1))
coef2 = (2 * intersect1 + smooth) / (denom1 + smooth)
weighted_dice_coef = w1 * coef1 + w2 * coef2
return (1 - weighted_dice_coef)
def dsc(y_true, y_pred):
"""
DSC = (|X and Y|)/ (|X| + |Y|)
"""
smooth = 1.
y_true_f = K.flatten(y_true[...,1])
y_pred_f = K.flatten(y_pred[...,1])
intersect = K.abs(K.sum(y_true_f * y_pred_f, axis = -1))
denom = K.abs(K.sum(y_true_f, axis = -1)) + K.abs(K.sum(y_pred_f, axis = -1))
coef = (2 * intersect + smooth) / (denom + smooth)
return coef
def standard_unit(input_tensor, stage, nb_filter, kernel_size = 3):
x = Conv2D(nb_filter, kernel_size, padding = 'same', activation = act, kernel_initializer = 'he_normal', kernel_regularizer=l2(1e-4), name = 'conv' + stage + '_1')(input_tensor)
x = Dropout(dropout_rate, name = 'dp' + stage + '_1')(x)
x = Conv2D(nb_filter, kernel_size, padding = 'same', activation = act, kernel_initializer = 'he_normal', kernel_regularizer=l2(1e-4), name = 'conv' + stage + '_2')(x)
x = Dropout(dropout_rate, name = 'dp' + stage + '_2')(x)
return x
dropout_rate = 0.5
act = "relu"
def Nest_UNet(input_size = (None, None, 1), num_class = 2, deep_supervision = False):
#class 0: Background
#class 1: Lesions
nb_filter = [32,64,128,256,512]
#Handle Dimension Ordering for different backends
global bn_axis
if K.image_dim_ordering() == 'tf':
bn_axis = 3
else:
bn_axis = 1
img_input = Input(input_size, name = 'main_input')
conv1_1 = standard_unit(img_input, stage = '11', nb_filter = nb_filter[0])
pool1 = MaxPooling2D(2, strides=2, name='pool1')(conv1_1)
#pool1 = dilatedConv(conv1_1, stage = '11', nb_filter = nb_filter[0])
conv2_1 = standard_unit(pool1, stage='21', nb_filter=nb_filter[1])
pool2 = MaxPooling2D(2, strides=2, name='pool2')(conv2_1)
#pool2 = dilatedConv(conv2_1, stage = '21', nb_filter = nb_filter[1])
up1_2 = Conv2DTranspose(nb_filter[0], 2, strides=2, padding='same', activation = act, kernel_initializer = 'he_normal', kernel_regularizer=l2(1e-4), name='up12')(conv2_1)
conv1_2 = concatenate([up1_2, conv1_1], name='merge12', axis=bn_axis)
conv1_2 = standard_unit(conv1_2, stage='12', nb_filter=nb_filter[0])
conv3_1 = standard_unit(pool2, stage='31', nb_filter=nb_filter[2])
pool3 = MaxPooling2D(2, strides=2, name='pool3')(conv3_1)
#pool3 = dilatedConv(conv3_1, stage = '31', nb_filter = nb_filter[2])
up2_2 = Conv2DTranspose(nb_filter[1], 2, strides=2, padding='same', activation = act, kernel_initializer = 'he_normal', kernel_regularizer=l2(1e-4), name='up22')(conv3_1)
conv2_2 = concatenate([up2_2, conv2_1], name='merge22', axis=bn_axis)
conv2_2 = standard_unit(conv2_2, stage='22', nb_filter=nb_filter[1])
up1_3 = Conv2DTranspose(nb_filter[0], 2, strides=2, padding='same', activation = act, kernel_initializer = 'he_normal', kernel_regularizer=l2(1e-4), name='up13')(conv2_2)
conv1_3 = concatenate([up1_3, conv1_1, conv1_2], name='merge13', axis=bn_axis)
conv1_3 = standard_unit(conv1_3, stage='13', nb_filter=nb_filter[0])
conv4_1 = standard_unit(pool3, stage='41', nb_filter=nb_filter[3])
pool4 = MaxPooling2D(2, strides=2, name='pool4')(conv4_1)
#pool4 = dilatedConv(conv4_1, stage = '41', nb_filter = nb_filter[3])
up3_2 = Conv2DTranspose(nb_filter[2], 2, strides=2, padding='same', activation = act, kernel_initializer = 'he_normal', kernel_regularizer=l2(1e-4), name='up32')(conv4_1)
conv3_2 = concatenate([up3_2, conv3_1], name='merge32', axis=bn_axis)
conv3_2 = standard_unit(conv3_2, stage='32', nb_filter=nb_filter[2])
up2_3 = Conv2DTranspose(nb_filter[1], 2, strides=2, padding='same', activation = act, kernel_initializer = 'he_normal', kernel_regularizer=l2(1e-4), name='up23')(conv3_2)
conv2_3 = concatenate([up2_3, conv2_1, conv2_2], name='merge23', axis=bn_axis)
conv2_3 = standard_unit(conv2_3, stage='23', nb_filter=nb_filter[1])
up1_4 = Conv2DTranspose(nb_filter[0], 2, strides=2, padding='same', activation = act, kernel_initializer = 'he_normal', kernel_regularizer=l2(1e-4), name='up14')(conv2_3)
conv1_4 = concatenate([up1_4, conv1_1, conv1_2, conv1_3], name='merge14', axis=bn_axis)
conv1_4 = standard_unit(conv1_4, stage='14', nb_filter=nb_filter[0])
conv5_1 = standard_unit(pool4, stage='51', nb_filter=nb_filter[4])
up4_2 = Conv2DTranspose(nb_filter[3], 2, strides=2, padding='same', activation = act, kernel_initializer = 'he_normal', kernel_regularizer=l2(1e-4), name='up42')(conv5_1)
conv4_2 = concatenate([up4_2, conv4_1], name='merge42', axis=bn_axis)
conv4_2 = standard_unit(conv4_2, stage='42', nb_filter=nb_filter[3])
up3_3 = Conv2DTranspose(nb_filter[2], 2, strides=2, padding='same', activation = act, kernel_initializer = 'he_normal', kernel_regularizer=l2(1e-4), name='up33')(conv4_2)
conv3_3 = concatenate([up3_3, conv3_1, conv3_2], name='merge33', axis=bn_axis)
conv3_3 = standard_unit(conv3_3, stage='33', nb_filter=nb_filter[2])
up2_4 = Conv2DTranspose(nb_filter[1], 2, strides=2, padding='same', activation = act, kernel_initializer = 'he_normal', kernel_regularizer=l2(1e-4), name='up24')(conv3_3)
conv2_4 = concatenate([up2_4, conv2_1, conv2_2, conv2_3], name='merge24', axis=bn_axis)
conv2_4 = standard_unit(conv2_4, stage='24', nb_filter=nb_filter[1])
up1_5 = Conv2DTranspose(nb_filter[0], 2, strides=2, padding='same', activation = act, kernel_initializer = 'he_normal', kernel_regularizer=l2(1e-4), name='up15')(conv2_4)
conv1_5 = concatenate([up1_5, conv1_1, conv1_2, conv1_3, conv1_4], name='merge15', axis=bn_axis)
conv1_5 = standard_unit(conv1_5, stage='15', nb_filter=nb_filter[0])
nestnet_output_1 = Conv2D(num_class, 1, activation='softmax', name='output_1', kernel_initializer = 'he_normal', padding='same', kernel_regularizer=l2(1e-4))(conv1_2)
nestnet_output_2 = Conv2D(num_class, 1, activation='softmax', name='output_2', kernel_initializer = 'he_normal', padding='same', kernel_regularizer=l2(1e-4))(conv1_3)
nestnet_output_3 = Conv2D(num_class, 1, activation='softmax', name='output_3', kernel_initializer = 'he_normal', padding='same', kernel_regularizer=l2(1e-4))(conv1_4)
nestnet_output_4 = Conv2D(num_class, 1, activation='softmax', name='output_4', kernel_initializer = 'he_normal', padding='same', kernel_regularizer=l2(1e-4))(conv1_5)
nestnet_output_5 = concatenate([nestnet_output_4, nestnet_output_3, nestnet_output_2, nestnet_output_1], name = "mergeAll", axis = bn_axis)
nestnet_output_5 = Conv2D(num_class, 1, activation='softmax', name='output_5', kernel_initializer = 'he_normal', padding='same', kernel_regularizer=l2(1e-4))(nestnet_output_5)
if deep_supervision:
model = Model(input=img_input, output = nestnet_output_5)
else:
model = Model(input=img_input, output = nestnet_output_4)
return model
with tf.device("/cpu:0"):
#initialize the model
model = Nest_UNet(deep_supervision = False)
#make the model parallel
model = multi_gpu_model(model, gpus = Gpu)
#initialize the optimizer and model
optimizer = Adam(lr = init_lr, beta_1 = beta1, beta_2 = beta2)
model.compile(loss = dice_loss, optimizer = optimizer, metrics = [dsc])
callbacks = [LearningRateScheduler(poly_decay)]
#train the network
aug = ImageDataGenerator(rotation_range = 10, width_shift_range = 0.1, height_shift_range = 0.1, horizontal_flip = True, fill_mode = "nearest")
aug.fit(trainX)
train = model.fit_generator(aug.flow(x = trainX, y = trainY, batch_size = batch_size * Gpu), steps_per_epoch = len(trainX) // (batch_size * Gpu),
epochs = n_epoch, verbose = 2, callbacks = callbacks, validation_data = (validX, validY), shuffle = True)
最佳答案
看起来您已经获取了 model code 并保持其基本完整。你从 sigmoid 到 softmax 的转换有点可疑。您是否将来自网络的单热编码 y_pred 与不是单热编码的 y_true 进行比较?也许您可以打印输出层的形状并将其与 y_true 的形状进行比较。
我在我的语义分割解决方案中使用了 Tversky Index,因为它是 Intersection-over-Union 和 Sørensen-Dice Coefficient 计算的概括,让我们比加权 Dice 系数方法更优雅地强调假阳性或假阴性必须使用 axis=-1
,我认为这是您问题的根源。对于损失,我简单地反转了 Tversky 指数指标。
def tversky_index(y_true, y_pred):
# generalization of dice coefficient algorithm
# alpha corresponds to emphasis on False Positives
# beta corresponds to emphasis on False Negatives (our focus)
# if alpha = beta = 0.5, then same as dice
# if alpha = beta = 1.0, then same as IoU/Jaccard
alpha = 0.5
beta = 0.5
y_true_f = K.flatten(y_true)
y_pred_f = K.flatten(y_pred)
intersection = K.sum(y_true_f * y_pred_f)
return (intersection) / (intersection + alpha * (K.sum(y_pred_f*(1. - y_true_f))) + beta * (K.sum((1-y_pred_f)*y_true_f)))
def tversky_index_loss(y_true, y_pred):
return -tversky_index(y_true, y_pred)
learning_rate = 5e-5 # also try 5e-4, 5e-3, depending on your network
optimizer = Adam(lr=learning_rate)
unet_model.compile(optimizer=optimizer, loss=tversky_index_loss, metrics=['accuracy','sparse_categorical_accuracy',tversky_index])
关于python - 损失函数正在减少但度量函数保持不变?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54248628/
我是 Bison 解析的新手,我无法理解它是如何工作的。我有以下语法,其中我保留了最低限度的语法来突出问题。 %left '~' %left '+' %token T_VARIABLE %% star
我链接了 2 个映射器和 1 个缩减器。是否可以将中间输出(链中每个映射器的 o/p)写入 HDFS?我尝试为每个设置 OutputPath,但它似乎不起作用。现在,我不确定是否可以完成。有什么建议吗
我正在编写一些代码来管理自定义磁盘文件结构并将其同步到未连接的系统。我的要求之一是能够在实际生成同步内容之前估计同步的大小。作为一个简单的解决方案,我整理了一个包含完整路径文件名的 map ,作为高效
我来自一个 SQL 世界,其中查找由多个对象属性(published = TRUE 或 user_id = X)完成,并且有 任何地方都没有加入 (因为 1:1 缓存层)。文档数据库似乎很适合我的数据
在 R 中,我有一个整数向量。从这个向量中,我想随机减少每个整数元素的值,以获得向量的总和,即初始总和的百分比。 在这个例子中,我想将向量“x”减少到向量“y”,其中每个元素都被随机减少以获得等于初始
我发现自己遇到过几次我有一个 reducer /组合 fn 的情况,如下所示: def combiner(a: String, b: String): Either[String, String]
Ubuntu 12.04 nginx 1.2.4 avconv版本 avconv version 0.8.10-4:0.8.10-0ubuntu0.12.04.1, Copyright (c) 200
我是 R 编程语言的新手。我有一个包含 2 列(ID 和 Num)的数据集,如下所示: ID Num 3 8 3 12 4 15 4 18 4
我正在使用高阶函数将函数应用于向量中的每个元素并将结果作为标量值返回。 假设我有: v = c(0, 1, 2, 3, 4, 5, 6, 7, 8) 我想计算以左边 5 个整数为中心的所有这些整数的总
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
这个问题在这里已经有了答案: How to write the dataframes in a list to a single csv file (2 个回答) 5年前关闭。 我正在尝试使用 Red
刚开始学习CUDA编程,对归约有些迷茫。 我知道与共享内存相比,全局内存有很多访问延迟,但我可以使用全局内存来(至少)模拟类似于共享内存的行为吗? 例如,我想对长度恰好为 BLOCK_SIZE * T
我经常使用OptiPNG或pngcrush减小PNG图像的文件大小。 我希望能够从.NET应用程序中以编程方式执行此类操作。我正在动态生成要发送到移动设备的PNG,因此我想减小文件大小。 图像质量很重
减少和减少让您在序列上累积状态。 序列中的每个元素都会修改累积的状态,直到 到达序列的末尾。 在无限列表上调用reduce 或reductions 有什么含义? (def c (cycle [0]))
这与R: use the newly generated data in the previous row有关 我意识到我面临的实际问题比我在上面的线程中给出的示例要复杂一些 - 似乎我必须将 3 个
有什么办法可以减少.ttf字体的大小?即如果我们要删除一些我们不使用的glyps。 最佳答案 使用Google Web Fonts,您可以限制字符集,例如: //fonts.googleapis.co
我需要在iOS中制作一个应用程序,在她的工作过程中发出类似“哔”的声音。 我已经使用MPMusicPlayerController实现了与背景ipod的交互。 问题: 由于来自ipod的音乐音量很大,
我有一个嵌套 map m,如下所示: m = Map("电子邮件"-> "a@b.com", "背景"-> Map("语言"-> "英语")) 我有一个数组arr = Array("backgroun
有什么原因为什么不应该转发map / reduce函数中收到的可写内容? 我的意思是-每个map / reduce函数都有一个可写的键/值,并可能发出一个键/值对。如果我想执行一些过滤,我应该只发出接
假设我有一个数据列表 val data = listOf("F 1", "D 2", "U 1", "D 3", "F 10") 我想执行每个元素的给定逻辑。 我必须在外部添加 var acc2 =
我是一名优秀的程序员,十分优秀!