首页 > 解决方案 > 如何在 keras CNN 中使用 sample_weight?

问题描述

在将 keras 用于 CNN 图像分类器时,我在使用 sample_weight 时遇到问题。我有以下模型:

input_shape = (256, 256, 3)
model = Sequential()
model.add(keras.Input(shape=input_shape))
model.add(Conv2D(64,3,padding="same", activation="relu"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(BatchNormalization())

model.add(Conv2D(128, 3, padding="same", activation="relu"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(BatchNormalization())

model.add(Conv2D(256, 3, padding="same", activation="relu"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(BatchNormalization())

model.add(Conv2D(512, 3, padding="same", activation="relu"))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.4))

model.add(Flatten())
model.add(Dense(512,activation="relu"))
model.add(BatchNormalization())
model.add(Dense(1024,activation="relu"))
model.add(BatchNormalization())
model.add(Dense(29, activation="softmax"))
model.build()
model.summary()

我发现样本重量如下:

from sklearn.utils.class_weight import compute_class_weight
from tensorflow.keras.utils import get_custom_objects

labelsForWeight = pd.read_csv(data_dir_labels_t)

def get_sample_weights(labels):
  # paramts: y is class labels as integers
  label = labels.astype(int) 
  label = label.values
  class_weight = compute_class_weight('balanced', np.unique(label), np.ravel(label))

  print("Real class weights are {}".format(class_weight),np.unique(label))
  print("value_counts",np.unique(label,return_counts=True))
  sample_weights = label.copy().astype(float)

  for i in range(29):
    sample_weights = np.where(sample_weights == i, class_weight[int(i)],class_weight)
  
  sampleWeights = sample_weights[0, :]
  return sampleWeights

sampleWeights = get_sample_weights(labelsForWeight)

其中 sampleWeight 是一个带有标签长度的数组。

NEXT 我正在使用增强数据训练模型:

aug = ImageDataGenerator(
        rotation_range=20,
        zoom_range=0.15,
        width_shift_range=0.2,
        height_shift_range=0.2,
        shear_range=0.15,
        horizontal_flip=True,
    vertical_flip=True,
        fill_mode="nearest")
model.compile(loss=keras.losses.categorical_crossentropy,
              optimizer=keras.optimizers.Adam(),
              metrics=['accuracy'])

K.set_value(model.optimizer.learning_rate, 0.0001)

history = model.fit(aug.flow(trainImg, trainLabels, batch_size=32), epochs=25,  validation_data=(valImg, valLabels), sample_weight=sampleWeights)

我可以轻松地训练模型,但是当使用 sample_weight 我得到:

ValueError: `sample_weight` argument is not supported when using `keras.utils.Sequence` as input. 

该怎么办?

标签: pythontensorflowkerasconv-neural-network

解决方案


推荐阅读