【发布时间】:2020-11-03 22:08:57
【问题描述】:
我创建了以下模型:
def create_model(input_shape = (7, 7, 1280)):
input_img = Input(shape=input_shape)
backbone = Flatten(input_shape(7, 7, 1280)) (input_img)
branches = []
for i in range(8):
branches.append(backbone)
branches[i] = Dense(16000, activation = "relu", name="branch_"+str(i)+"_Dense_16000")(branches[i])
branches[i] = Dense(128, activation = "relu", name="branch_"+str(i)+"_Dense_128")(branches[i])
branches[i] = Dense(36, activation = "softmax", name="branch_"+str(i)+"_output")(branches[i])
output = Concatenate(axis=1)(branches)
output = Reshape((8, 36))(output)
model = Model(input_img, output)
return model
model = create_model()
我现在想将此模型应用于由Generator 批量生成的数据。因此,我使用了来自 mujjiga 的建议 DataGenerator,并在 def __getiteam__ 中稍作修改:
class DataGenerator(Sequence):
def __init__(self, X, y, batch_size):
self.X = X
self.y = y
self.batch_size = batch_size
self.indexes = np.arange(len(self.X))
def __len__(self):
return int(np.floor(len(self.X) / self.batch_size))
def __getitem__(self, index):
idx = self.indexes[index*self.batch_size:(index+1)*self.batch_size]
idx = int(idx)
batch_X, batch_y = self.X[idx], self.y[idx]
batch_X = np.array(batch_X)
batch_y = np.array(batch_y)
return batch_X, batch_y
在此之后,我将 DataGenerator 应用到 X_after_efn_train、y_train、X_after_efn_val 和 y_val。 X_after_efn_train 和 X_after_efn_val 的形状为 (n, 7, 7, 1280) 和在它们上运行预训练的 EfficientNet 模型后的 X 值 (https://keras.io/api/applications/efficientnet/#efficientnetb0-function)。
training_generator_after_efn = DataGenerator(X_after_efn_train, y_train, batch_size=32)
validation_generator_after_efn = DataGenerator(X_after_efn_val, y_val, batch_size=32)
我用这段代码编译了模型:
model.compile(optimizer="rmsprop", loss="categorical_crossentropy", metrics=["accuracy"])
现在,我尝试使用model.fit_generator:
model.fit_generator(generator=training_generator_after_efn,
validation_data=validation_generator_after_efn,
steps_per_epoch = num_train_samples // 32,
validation_steps = num_val_samples // 32,
epochs = 10, workers=6, use_multiprocessing=True)
得到了这个错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-120-682a3512f3ac> in <module>()
3 steps_per_epoch = num_train_samples // 32,
4 validation_steps = num_val_samples // 32,
----> 5 epochs = 10, workers=6, use_multiprocessing=True)
8 frames
<ipython-input-117-49b8453acf0a> in __getitem__(self, index)
11 def __getitem__(self, index):
12 idx = self.indexes[index*self.batch_size:(index+1)*self.batch_size]
---> 13 idx = int(idx)
14 batch_X, batch_y = self.X[idx], self.y[idx]
15 batch_X = np.array(batch_X)
TypeError: only size-1 arrays can be converted to Python scalars
非常感谢!
【问题讨论】:
-
让我们首先尝试重现该问题。请随时更新协作笔记本以重现问题colab.research.google.com/drive/…
-
y_train 和 y_val 是列表。它们包含一个热编码标签。我将它们的类型更改为数组,但得到了同样的错误。但我会尝试更新 Colab 笔记本。非常感谢您的帮助。我不明白的是:
y_val = np.random.randint(0, 2, (10, 8, 36))。(0, 2, (...))是什么意思?
标签: python function tensorflow keras conv-neural-network