您输入的数据已耗尽。我已经尝试了几次但失败了,非常感谢有人能解决我的问题

问题描述 投票:0回答:1

我的数据集有 480 个训练集和 120 个测试集

当我运行时它显示:

警告:tensorflow:您的输入数据不足;中断训练。确保您的数据集或生成器可以生成至少

steps_per_epoch * epochs
批次(在本例中为 100 批次)。构建数据集时,您可能需要使用 Repeat() 函数。

这是代码:

import os
import random
import warnings
warnings.filterwarnings("ignore")
from ut2 import train_test_split

src = 'Dataset/corrosion/'


# create the train/test folders if it does not exists already
if not os.path.isdir(src+'train/'):
    train_test_split(src)

from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Dropout, Flatten, Dense
from keras.preprocessing.image import ImageDataGenerator

# Define hyperparameters
FILTER_SIZE = 3
NUM_FILTERS = 32
INPUT_SIZE  = 200
MAXPOOL_SIZE = 2
BATCH_SIZE = 2
STEPS_PER_EPOCH = 480//BATCH_SIZE
EPOCHS = 50
#repeat()

model = Sequential()
model.add(Conv2D(NUM_FILTERS, (FILTER_SIZE, FILTER_SIZE), input_shape = (INPUT_SIZE, INPUT_SIZE, 3), activation = 'relu'))
model.add(MaxPooling2D(pool_size = (MAXPOOL_SIZE, MAXPOOL_SIZE)))
model.add(Conv2D(NUM_FILTERS, (FILTER_SIZE, FILTER_SIZE), activation = 'relu'))
model.add(MaxPooling2D(pool_size = (MAXPOOL_SIZE, MAXPOOL_SIZE)))
model.add(Flatten())
model.add(Dense(units = 128, activation = 'relu'))
model.add(Dropout(0.5))
model.add(Dense(units = 5, activation = 'softmax'))
model.add(Flatten())
model.compile(optimizer = 'adam', loss = 'SparseCategoricalCrossentropy', metrics = ['accuracy'])

training_data_generator = ImageDataGenerator(rescale = 1./255)
testing_data_generator = ImageDataGenerator(rescale = 1./255)

training_set = training_data_generator.flow_from_directory(src+'Train/',
                                                target_size = (INPUT_SIZE, INPUT_SIZE),
                                                batch_size = BATCH_SIZE,
                                                class_mode = 'sparse')

test_set = testing_data_generator.flow_from_directory(src+'Test/',
                                             target_size = (INPUT_SIZE, INPUT_SIZE),
                                             batch_size = BATCH_SIZE,
                                            class_mode='sparse')

model.fit_generator(training_set, steps_per_epoch = STEPS_PER_EPOCH, epochs = EPOCHS, verbose=1)

score = model.evaluate_generator(test_set, steps=100)

for idx, metric in enumerate(model.metrics_names):
    print("{}: {}".format(metric, score[idx]))

我的数据集有 480 个训练集和 120 个测试集

当我运行时它显示:

警告:tensorflow:您的输入数据不足;中断训练。确保您的数据集或生成器可以生成至少

steps_per_epoch * epochs
批次(在本例中为 100 批次)。构建数据集时,您可能需要使用 Repeat() 函数。

python tensorflow dataset
1个回答
0
投票

这是最新的代码,准确性更差。

import os
import tensorflow as tf
import random
import warnings
warnings.filterwarnings("ignore")
from ut2 import train_test_split

src = 'Dataset/corrosion/'


# create the train/test folders if it does not exists already
if not os.path.isdir(src+'train/'):
    train_test_split(src)

from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Dropout, Flatten, Dense
from keras.preprocessing.image import ImageDataGenerator

# Define hyperparameters
FILTER_SIZE = 3
NUM_FILTERS = 32
INPUT_SIZE  = 200
MAXPOOL_SIZE = 2
BATCH_SIZE = 16
#STEPS_PER_EPOCH = 540//BATCH_SIZE
EPOCHS = 50
#repeat()

model = Sequential()
model.add(Conv2D(NUM_FILTERS, (FILTER_SIZE, FILTER_SIZE), input_shape = (INPUT_SIZE, INPUT_SIZE, 3), activation = 'relu'))
model.add(MaxPooling2D(pool_size = (MAXPOOL_SIZE, MAXPOOL_SIZE)))
model.add(Conv2D(NUM_FILTERS, (FILTER_SIZE, FILTER_SIZE), activation = 'relu'))
model.add(MaxPooling2D(pool_size = (MAXPOOL_SIZE, MAXPOOL_SIZE)))
model.add(Flatten())
model.add(Dense(units = 128, activation = 'relu'))
model.add(Dropout(0.5))
model.add(Dense(units = 5, activation = 'softmax'))
model.add(Flatten())
model.compile(optimizer = 'adam', loss = 'SparseCategoricalCrossentropy', metrics = ['accuracy'])

training_data_generator = ImageDataGenerator(rescale = 1./255)
testing_data_generator = ImageDataGenerator(rescale = 1./255)

training_set = tf.keras.preprocessing.image_dataset_from_directory(src+'Train/',
                                                labels='inferred',
                                                image_size = (INPUT_SIZE, INPUT_SIZE),
                                                batch_size = BATCH_SIZE,
                                                label_mode='int')

test_set = tf.keras.preprocessing.image_dataset_from_directory(src+'Test/',
                                                               labels='inferred',
                                                               image_size=(INPUT_SIZE, INPUT_SIZE),
                                                               batch_size=BATCH_SIZE,
                                                               label_mode='int')

model.fit(training_set, epochs = EPOCHS, verbose=1)

score = model.evaluate_generator(test_set, steps=100)

for idx, metric in enumerate(model.metrics_names):
    print("{}: {}".format(metric, score[idx]))

警告:tensorflow:您的输入数据不足;中断训练。确保您的数据集或生成器可以生成至少

steps_per_epoch * epochs
批次(在本例中为 100 批次)。构建数据集时,您可能需要使用 Repeat() 函数。 损失:12.894475936889648 准确度:0.20000000298023224

进程已完成,退出代码为 0

© www.soinside.com 2019 - 2024. All rights reserved.