TypeError:遍历keras模型时,'Sequential'对象不支持索引

时间:2019-08-19 17:28:48

标签: python tensorflow keras

我正在尝试通过投票系统将预先训练的模型整合为一个模型。我指的是同样的blog

这是代码

for i in os.listdir(model_root): //loading all the models
    print(i)
    filename = model_root + "/" + i
    # load model
    model = load_model(filename, custom_objects={'KerasLayer': hub.KerasLayer})
    models.append(model)
print(len(models))

  #To fit the loaded models to the data and saving it to an array fit_models

steps_per_epoch = image_data.samples // image_data.batch_size
batch_stats = CollectBatchStats()
validation_steps = image_data_val.samples / image_data_val.batch_size
for i in range(len(models)):
    model[i].fit_generator((item for item in image_data), epochs=2,
              steps_per_epoch=steps_per_epoch, #callbacks=[batch_stats],
              validation_data=(item for item in image_data_val), validation_steps=validation_steps, verbose=2)
    fit_models.append(model[i])

这是该错误的回溯:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Program Files\JetBrains\PyCharm 2019.2\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm 2019.2\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/Pawandeep/Desktop/Python projects/ensemble_image.py", line 90, in <module>
    model[i].fit_generator((item for item in image_data), epochs=2,
TypeError: 'Sequential' object does not support indexing

UPDATE ---------------------

遵循注释中的解决方案之后,我不再是原始错误,而是一个新错误。

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Program Files\JetBrains\PyCharm 2019.2\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm 2019.2\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/Pawandeep/Desktop/Python projects/ensemble_image.py", line 89, in <module>
    validation_data=(item for item in image_data_val), validation_steps=validation_steps, verbose=2)
  File "C:\Python\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1433, in fit_generator
    steps_name='steps_per_epoch')
  File "C:\Python\lib\site-packages\tensorflow\python\keras\engine\training_generator.py", line 264, in model_iteration
    batch_outs = batch_function(*batch_data)
  File "C:\Python\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1153, in train_on_batch
    extract_tensors_from_dataset=True)
  File "C:\Python\lib\site-packages\tensorflow\python\keras\engine\training.py", line 2692, in _standardize_user_data
    y, self._feed_loss_fns, feed_output_shapes)
  File "C:\Python\lib\site-packages\tensorflow\python\keras\engine\training_utils.py", line 549, in check_loss_and_target_compatibility
    ' while using as loss `' + loss_name + '`. '
ValueError: A target array with shape (32, 3) was passed for an output of shape (None, 2) while using as loss `binary_crossentropy`. This loss expects targets to have the same shape as the output.

我的数据如下:

Image batch shape:  (32, 224, 224, 3)
Label batch shape:  (32, 3)

0 个答案:

没有答案