如何将 sample_weights 与 3D 医疗数据一起使用,而不会 model.fit(x=tf.data.Dataset) 导致错误,例如无法挤压最后的暗淡
How to use sample_weights with 3D medical data, without model.fit(x=tf.data.Dataset) causing an error like can't squeeze the last dim
环境:
python = 3.8.12
tensorflow = 2.6.0.
keras = 2.6.0
所以问题是我正在尝试训练高度不平衡的数据,所以我尝试使用 sample_weights
作为 model.fit()
的一部分,但我总是得到同样的错误:
ValueError: Can not squeeze dim[4], expected a dimension of 1, got 4 for '{{node categorical_crossentropy/weighted_loss/Squeeze}} = Squeeze[T=DT_FLOAT, squeeze_dims=[-1]](Cast)' with input shapes: [?,48,48,80,4].
所以这是数据的形状,其中 y_s
使用 tf.keras.utils.to_categorical
转换,其中 num_classes = 4
:
x_train (54, 48, 48, 80)
y_train (54, 48, 48, 80, 4)
x_test (18, 48, 48, 80)
y_test (18, 48, 48, 80, 4)
x_val (18, 48, 48, 80)
y_val (18, 48, 48, 80, 4)
架构是U-NET
:
inputs = Input((number_of_layers, height, width, 1))
c1 = Conv3D(filters=16, kernel_size=3, activation='relu', kernel_initializer='he_normal', padding='same')(inputs)
c1 = Dropout(0.1)(c1)
c1 = Conv3D(16, kernel_size=3, activation='relu', kernel_initializer='he_normal', padding='same')(c1)
p1 = MaxPooling3D(pool_size=2)(c1)
...............
...............
...............
outputs = Conv3D(num_classes, kernel_size=1, activation='softmax')(u9)
model = Model(inputs=[inputs], outputs=[outputs])
关于compile
部分,是这样的:
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'], sample_weight_mode="temporal")
注意:我没有使用 metrics=[‘accuracy’]
进行评估,我使用了一些 IOU
问题来了,我在使用的时候:
from sklearn.utils.class_weight import compute_sample_weight
weights = compute_sample_weight(class_weight='balanced', y=y_train.flatten())
weights = weights.reshape(y_train.shape)
weights.shape # => (54, 48, 48, 80, 4) (same as y_train)
所以直到这里它都在工作,没有任何错误,但是当我将 weights
添加到以下数据集时:
tf_ds = tf.data.Dataset.from_tensor_slices((x_train, y_train, weights)).batch(4)
然后我尝试 运行 model.fit
:
model.fit(x=tf_ds, verbose=1, epochs=5, validation_data=(x_val, y_val))
我收到以下错误:
ValueError: Can not squeeze dim[4], expected a dimension of 1, got 4 for '{{node categorical_crossentropy/weighted_loss/Squeeze}} = Squeeze[T=DT_FLOAT, squeeze_dims=[-1]](Cast)' with input shapes: [?,48,48,80,4].
任何想法,如何解决这个问题?
我假设您的标签肯定是热编码的,这就是您使用 categorical_crossentropy
的原因?如果不是,那么您可以 sparse_categorical_crossentropy
试一试。
环境:
python = 3.8.12
tensorflow = 2.6.0.
keras = 2.6.0
所以问题是我正在尝试训练高度不平衡的数据,所以我尝试使用 sample_weights
作为 model.fit()
的一部分,但我总是得到同样的错误:
ValueError: Can not squeeze dim[4], expected a dimension of 1, got 4 for '{{node categorical_crossentropy/weighted_loss/Squeeze}} = Squeeze[T=DT_FLOAT, squeeze_dims=[-1]](Cast)' with input shapes: [?,48,48,80,4].
所以这是数据的形状,其中 y_s
使用 tf.keras.utils.to_categorical
转换,其中 num_classes = 4
:
x_train (54, 48, 48, 80)
y_train (54, 48, 48, 80, 4)
x_test (18, 48, 48, 80)
y_test (18, 48, 48, 80, 4)
x_val (18, 48, 48, 80)
y_val (18, 48, 48, 80, 4)
架构是U-NET
:
inputs = Input((number_of_layers, height, width, 1))
c1 = Conv3D(filters=16, kernel_size=3, activation='relu', kernel_initializer='he_normal', padding='same')(inputs)
c1 = Dropout(0.1)(c1)
c1 = Conv3D(16, kernel_size=3, activation='relu', kernel_initializer='he_normal', padding='same')(c1)
p1 = MaxPooling3D(pool_size=2)(c1)
...............
...............
...............
outputs = Conv3D(num_classes, kernel_size=1, activation='softmax')(u9)
model = Model(inputs=[inputs], outputs=[outputs])
关于compile
部分,是这样的:
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'], sample_weight_mode="temporal")
注意:我没有使用 metrics=[‘accuracy’]
进行评估,我使用了一些 IOU
问题来了,我在使用的时候:
from sklearn.utils.class_weight import compute_sample_weight
weights = compute_sample_weight(class_weight='balanced', y=y_train.flatten())
weights = weights.reshape(y_train.shape)
weights.shape # => (54, 48, 48, 80, 4) (same as y_train)
所以直到这里它都在工作,没有任何错误,但是当我将 weights
添加到以下数据集时:
tf_ds = tf.data.Dataset.from_tensor_slices((x_train, y_train, weights)).batch(4)
然后我尝试 运行 model.fit
:
model.fit(x=tf_ds, verbose=1, epochs=5, validation_data=(x_val, y_val))
我收到以下错误:
ValueError: Can not squeeze dim[4], expected a dimension of 1, got 4 for '{{node categorical_crossentropy/weighted_loss/Squeeze}} = Squeeze[T=DT_FLOAT, squeeze_dims=[-1]](Cast)' with input shapes: [?,48,48,80,4].
任何想法,如何解决这个问题?
我假设您的标签肯定是热编码的,这就是您使用 categorical_crossentropy
的原因?如果不是,那么您可以 sparse_categorical_crossentropy
试一试。