简体   繁体   English

keras 列表索引超出范围,同时拟合 model

[英]keras list index out of range while fitting the model

Unable to figure the error.无法确定错误。 The test directory contains two sub folders, inside the subfolder there are images (.jpg) files.测试目录包含两个子文件夹,子文件夹内有图像(.jpg)文件。 I am trying to find the accuracy of model.我试图找到 model 的准确性。 In which format the test directory has to be read?必须以哪种格式读取测试目录? What am I doing wrong?我究竟做错了什么? In this project I am trying to do transfer learning to train a model for image recognition on the chest X-ray dataset.在这个项目中,我正在尝试进行迁移学习来训练 model 用于胸部 X 射线数据集的图像识别。

import os
from PIL import Image
import numpy as np
import tensorflow as tf
from tensorflow import keras


test_dataset = 'C:\\Users\\arjun\\Desktop\\Rashmi\\Courses\\Deep Learning\\Project 2\\chest-xray-pneumonia\\chest_xray\\chest_xray\\test'
train_dataset = 'C:\\Users\\arjun\\Desktop\\Rashmi\\Courses\\Deep Learning\\Project 2\\chest-xray-pneumonia\\chest_xray\\chest_xray\\train'
batch_size=8

tf.keras.preprocessing.image_dataset_from_directory(
train_dataset,
labels="inferred",
label_mode="int",
class_names=None,
color_mode="rgb",
batch_size=batch_size,
image_size=(256, 256),
shuffle=True,
seed=None,
validation_split=None,
subset=None,
interpolation="bilinear",
follow_links=False,
)

base_model = tf.keras.applications.VGG16(include_top=False, 
                                     weights='imagenet',
                                     input_shape=(150,150,3), 
                                     pooling='avg')

base_model.trainable = False

inputs = keras.Input(shape=(150,150,3))
x = base_model(inputs, training = False)

x = keras.layers.Flatten()(x)
x = keras.layers.Dense(512, kernel_initializer = 'he_normal', activation = 'relu')(x)

predictions = keras.layers.Dense(1, activation = 'sigmoid')(x)

transfer_model = keras.Model(inputs, predictions)

print(transfer_model.summary())

transfer_model.compile(loss='categorical_crossentropy',
                   optimizer=keras.optimizers.SGD(lr=1e-4,momentum=0.9),
                   metrics=['accuracy']) 

transfer_model.fit(train_dataset, 
               epochs = 2, 
               shuffle=True, 
               verbose=1, 
               validation_data = test_dataset)



transfer_model.fit(train_dataset, 
               epochs = 2, 
               shuffle=True, 
               verbose=1, 
               validation_data = test_dataset)
Traceback (most recent call last):

  File "<ipython-input-104-a543f53dce65>", line 5, in <module>
validation_data = test_dataset)

  File "C:\Users\arjun\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1064, in fit
steps_per_execution=self._steps_per_execution)

  File "C:\Users\arjun\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py", line 1112, in __init__
model=model)

  File "C:\Users\arjun\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py", line 650, in __init__
**kwargs)

  File "C:\Users\arjun\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py", line 273, in __init__
num_samples = set(int(i.shape[0]) for i in nest.flatten(inputs)).pop()

  File "C:\Users\arjun\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py", line 273, in <genexpr>
num_samples = set(int(i.shape[0]) for i in nest.flatten(inputs)).pop()

  File "C:\Users\arjun\Anaconda3\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 889, in __getitem__
return self._dims[key].value

IndexError: list index out of range

You have to return the image_dataset_from_directory function to train_dataset variable.您必须将image_dataset_from_directory function 返回到train_dataset变量。

train_dataset = (
train_dataset,
labels="inferred",
label_mode="int",
class_names=None,
color_mode="rgb",
batch_size=batch_size,
image_size=(256, 256),
shuffle=True,
seed=None,
validation_split=None,
subset=None,
interpolation="bilinear",
follow_links=False,
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 IndexError:列表索引超出范围,而 keras model.predict() - IndexError: list index out of range while keras model.predict() 在构建和编译keras顺序模型时“列表索引超出范围” - “list index out of range” while building and compiling keras sequential model 保存 model 时:列表索引 (0) 超出范围 - while saving model: list index (0) out of range 保存 model 时 Keras “列表索引超出范围” - Keras “list index out of range” when saving a model Keras 列表索引超出范围 - Keras List index is out of range Keras ImageGenerator:IndexError:列表索引超出范围 - Keras ImageGenerator : IndexError: list index out of range Keras模型泄漏出未知错误| (IndexError:列表索引超出范围) - Keras model spills out unknown error | (IndexError: list index out of range) 无法使用 keras.load_model 保存/加载模型 - IndexError: list index out of range - Can't save/load model using keras.load_model - IndexError: list index out of range 当我想在 keras 中拟合模型时,列出索引超出范围错误 - list index out of range error when I want fit a model in keras Keras:修复使用 model.fit 时出现的“IndexError: list index out of range”错误 - Keras: fix "IndexError: list index out of range" error when using model.fit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM