Machine learning 我得到了一份名单';索引超出范围';在keras中使用model.predict时

Machine learning 我得到了一份名单';索引超出范围';在keras中使用model.predict时,machine-learning,keras,deep-learning,Machine Learning,Keras,Deep Learning,这是我的代码,我正在使用迁移学习来训练我的模型。但我得到的索引超出了误差范围。当尝试测试我的模型时,它为model.predict()函数提供了此错误。原因可能是什么 IMAGE_SIZE = [100, 100] train_path = 'input/train' valid_path = 'input/val' 在VGG前端增加预处理层 vgg = VGG16(input_shape=IMAGE_SIZE + [3], weights='imagenet', include_top=F

这是我的代码,我正在使用迁移学习来训练我的模型。但我得到的索引超出了误差范围。当尝试测试我的模型时,它为model.predict()函数提供了此错误。原因可能是什么

IMAGE_SIZE = [100, 100]

train_path = 'input/train'
valid_path = 'input/val'
在VGG前端增加预处理层

vgg = VGG16(input_shape=IMAGE_SIZE + [3], weights='imagenet', include_top=False)

# don't train existing weights
for layer in vgg.layers:
  layer.trainable = False
用于获取类的数量

folders = glob('input/train/*')
我们的层次

x = Flatten()(vgg.output)
# x = Dense(1000, activation='relu')(x)
prediction = Dense(len(folders), activation='softmax')(x)
创建模型对象

model = Model(inputs=vgg.input, outputs=prediction)
查看模型的结构

model.summary()
r = model.fit(
  training_set,
  validation_data=val_set,
  epochs=50,
  steps_per_epoch=len(training_set),
  validation_steps=len(val_set)
)
告诉模型使用什么成本和优化方法

model.compile(
  loss='categorical_crossentropy',
  optimizer='adam',
  metrics=['accuracy']
)
符合模型

model.summary()
r = model.fit(
  training_set,
  validation_data=val_set,
  epochs=50,
  steps_per_epoch=len(training_set),
  validation_steps=len(val_set)
)
这一行有错误

model.predict('/content/input/test/0/IMG_4099.JPG')

Model
predic
不接受路径作为输入。从文档中,输入样本可以是:

  • Numpy数组(或类似数组)或数组列表(如果模型有多个输入)
  • 张量流张量或张量列表(如果模型有多个输入)
  • 一个tf.data数据集
  • 生成器或keras.utils.Sequence实例。Model.fit中类似迭代器输入的解包行为部分给出了迭代器类型(数据集、生成器、序列)解包行为的更详细描述
有多种方法可以从图像路径获取
numpy数组。例如,您可以使用
Keras
预处理来读取图像,然后使用
img\u to\u array
来获得
numpy array
。如果文件夹中的图像尚未达到模型预期的大小,则必须使用
target\u size
参数来调整模型输入形状的大小

img = tf.keras.preprocessing.image.load_img(
    "/content/input/test/0/IMG_4099.JPG",
    target_size=(100,100)
)
img_nparray = tf.keras.preprocessing.image.img_to_array(img)
type(img_nparray) # numpy.ndarray
input_Batch = np.array([img_nparray])   # Convert single image to a batch.
predictions = model.predict(input_Batch)
另一种选择是使用先前声明的映像
生成器
test\u datagen
,同样不进行任何数据扩充以进行公平预测)指向包含单个(或多个)映像的文件夹

文件夹结构

├── content
│   └── input
│       └── test
│           └── 0
│               └── IMG_4099.JPG

Model
predic
不接受路径作为输入。从文档中,输入样本可以是:

  • Numpy数组(或类似数组)或数组列表(如果模型有多个输入)
  • 张量流张量或张量列表(如果模型有多个输入)
  • 一个tf.data数据集
  • 生成器或keras.utils.Sequence实例。Model.fit中类似迭代器输入的解包行为部分给出了迭代器类型(数据集、生成器、序列)解包行为的更详细描述
有多种方法可以从图像路径获取
numpy数组。例如,您可以使用
Keras
预处理来读取图像,然后使用
img\u to\u array
来获得
numpy array
。如果文件夹中的图像尚未达到模型预期的大小,则必须使用
target\u size
参数来调整模型输入形状的大小

img = tf.keras.preprocessing.image.load_img(
    "/content/input/test/0/IMG_4099.JPG",
    target_size=(100,100)
)
img_nparray = tf.keras.preprocessing.image.img_to_array(img)
type(img_nparray) # numpy.ndarray
input_Batch = np.array([img_nparray])   # Convert single image to a batch.
predictions = model.predict(input_Batch)
另一种选择是使用先前声明的映像
生成器
test\u datagen
,同样不进行任何数据扩充以进行公平预测)指向包含单个(或多个)映像的文件夹

文件夹结构

├── content
│   └── input
│       └── test
│           └── 0
│               └── IMG_4099.JPG