Keras 模型输出是 float32 而不是 uint8...尽管数据标签是 uint8
Keras Model Output is float32 instead of uint8... despite data labels being uint8
我正在训练一个模型来预测医学图像中的分割。在训练数据中,输入数据的类型为:numpy.float64,ground truth 标签的类型为:numpy.uint8。问题是出于某种原因,我的模型正在生成 numpy.float32 的输出类型。
图片显示:
example of data types
# Defining the model
segmenter = Model(input_img, segmenter(input_img))
# Training the model (type of train_ground is numpy.uint8)
segmenter_train = segmenter.fit(train_X, train_ground, batch_size=batch_size,epochs=epochs,verbose=1,validation_data=(valid_X, valid_ground))
模型定义:
def segmenter(input_img):
#encoder
#input = 28 x 28 x 1 (wide and thin)
conv1 = Conv2D(32, (3, 3), activation='relu', padding='same')(input_img) #28 x 28 x 32
conv1 = BatchNormalization()(conv1)
conv1 = Conv2D(32, (3, 3), activation='relu', padding='same')(conv1)
conv1 = BatchNormalization()(conv1)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1) #14 x 14 x 32
conv2 = Conv2D(64, (3, 3), activation='relu', padding='same')(pool1) #14 x 14 x 64
conv2 = BatchNormalization()(conv2)
conv2 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv2)
conv2 = BatchNormalization()(conv2)
pool2 = MaxPooling2D(pool_size=(2, 2))(conv2) #7 x 7 x 64
conv3 = Conv2D(128, (3, 3), activation='relu', padding='same')(pool2) #7 x 7 x 128 (small and thick)
conv3 = BatchNormalization()(conv3)
conv3 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv3)
conv3 = BatchNormalization()(conv3)
#decoder
conv4 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv3) #7 x 7 x 128
conv4 = BatchNormalization()(conv4)
conv4 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv4)
conv4 = BatchNormalization()(conv4)
up1 = UpSampling2D((2,2))(conv4) # 14 x 14 x 128
conv5 = Conv2D(32, (3, 3), activation='relu', padding='same')(up1) # 14 x 14 x 64
conv5 = BatchNormalization()(conv5)
conv5 = Conv2D(32, (3, 3), activation='relu', padding='same')(conv5)
conv5 = BatchNormalization()(conv5)
up2 = UpSampling2D((2,2))(conv5) # 28 x 28 x 64
conv6 = Conv2D(64, (3, 3), activation='relu', padding='same')(up2) #7 x 7 x 128
conv6 = BatchNormalization()(conv6)
conv6 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv6)
conv6 = BatchNormalization()(conv6)
up3 = UpSampling2D((2,2))(conv6) # 14 x 14 x 128
conv7 = Conv2D(64, (3, 3), activation='relu', padding='same')(up3) #7 x 7 x 128
conv7 = BatchNormalization()(conv7)
conv7 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv7)
conv7 = BatchNormalization()(conv7)
up4 = UpSampling2D((2,2))(conv7) # 14 x 14 x 128
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(up4) # 28 x 28 x 1
return decoded
在此先感谢您的帮助:)
Sigmoid returns 实数
最后一层正好是sigmoid激活函数。它returns 0 和 1 之间的实数,而不是整数。
此外,重要的是错误度量,即正确答案与计算值之间的差异,是连续的而不是离散的,因为它是可微的并且允许通过反向传播正确学习神经网络权重。
只需转换和舍入
为了训练网络,只需将真值标签转换为浮点值。
一旦你训练了网络并想使用它的输出,只需将它们四舍五入以将它们转换为整数——S 形激活非常适合这种情况。
我正在训练一个模型来预测医学图像中的分割。在训练数据中,输入数据的类型为:numpy.float64,ground truth 标签的类型为:numpy.uint8。问题是出于某种原因,我的模型正在生成 numpy.float32 的输出类型。
图片显示: example of data types
# Defining the model
segmenter = Model(input_img, segmenter(input_img))
# Training the model (type of train_ground is numpy.uint8)
segmenter_train = segmenter.fit(train_X, train_ground, batch_size=batch_size,epochs=epochs,verbose=1,validation_data=(valid_X, valid_ground))
模型定义:
def segmenter(input_img):
#encoder
#input = 28 x 28 x 1 (wide and thin)
conv1 = Conv2D(32, (3, 3), activation='relu', padding='same')(input_img) #28 x 28 x 32
conv1 = BatchNormalization()(conv1)
conv1 = Conv2D(32, (3, 3), activation='relu', padding='same')(conv1)
conv1 = BatchNormalization()(conv1)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1) #14 x 14 x 32
conv2 = Conv2D(64, (3, 3), activation='relu', padding='same')(pool1) #14 x 14 x 64
conv2 = BatchNormalization()(conv2)
conv2 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv2)
conv2 = BatchNormalization()(conv2)
pool2 = MaxPooling2D(pool_size=(2, 2))(conv2) #7 x 7 x 64
conv3 = Conv2D(128, (3, 3), activation='relu', padding='same')(pool2) #7 x 7 x 128 (small and thick)
conv3 = BatchNormalization()(conv3)
conv3 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv3)
conv3 = BatchNormalization()(conv3)
#decoder
conv4 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv3) #7 x 7 x 128
conv4 = BatchNormalization()(conv4)
conv4 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv4)
conv4 = BatchNormalization()(conv4)
up1 = UpSampling2D((2,2))(conv4) # 14 x 14 x 128
conv5 = Conv2D(32, (3, 3), activation='relu', padding='same')(up1) # 14 x 14 x 64
conv5 = BatchNormalization()(conv5)
conv5 = Conv2D(32, (3, 3), activation='relu', padding='same')(conv5)
conv5 = BatchNormalization()(conv5)
up2 = UpSampling2D((2,2))(conv5) # 28 x 28 x 64
conv6 = Conv2D(64, (3, 3), activation='relu', padding='same')(up2) #7 x 7 x 128
conv6 = BatchNormalization()(conv6)
conv6 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv6)
conv6 = BatchNormalization()(conv6)
up3 = UpSampling2D((2,2))(conv6) # 14 x 14 x 128
conv7 = Conv2D(64, (3, 3), activation='relu', padding='same')(up3) #7 x 7 x 128
conv7 = BatchNormalization()(conv7)
conv7 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv7)
conv7 = BatchNormalization()(conv7)
up4 = UpSampling2D((2,2))(conv7) # 14 x 14 x 128
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(up4) # 28 x 28 x 1
return decoded
在此先感谢您的帮助:)
Sigmoid returns 实数
最后一层正好是sigmoid激活函数。它returns 0 和 1 之间的实数,而不是整数。
此外,重要的是错误度量,即正确答案与计算值之间的差异,是连续的而不是离散的,因为它是可微的并且允许通过反向传播正确学习神经网络权重。
只需转换和舍入
为了训练网络,只需将真值标签转换为浮点值。
一旦你训练了网络并想使用它的输出,只需将它们四舍五入以将它们转换为整数——S 形激活非常适合这种情况。