[英]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.