import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input,Flatten,Dense,Dropout,GlobalAveragePooling2D
from tensorflow.keras.applications.mobilenet import MobileNet,preprocess_input
import math
TRAIN_DATA_DIR = '/kaggle/working/train'
VALIDATION_DATA_DIR = '/kaggle/working/test'
TRAIN_SAMPLES = 25000
VALIDATION_SAMPLES = 12500
NUM_CLASSES = 2
IMG_WIGTH,IMG_HEIGHT=224,224
BATCH_SIZE = 500
train_datagen = ImageDataGenerator(preprocessing_function=preprocess_input,
rotation_range=20,
width_shift_range=0.2,
height_shift_range=0.2,
zoom_range=0.2)
val_datagen = ImageDataGenerator(preprocessing_function=preprocess_input)
train_generator = train_datagen.flow_from_directory(TRAIN_DATA_DIR,
target_size=(IMG_WIGTH,
IMG_HEIGHT),
batch_size=BATCH_SIZE,
shuffle=True,
seed=12345,
class_mode='categorical')
validation_generator = val_datagen.flow_from_directory(
VALIDATION_DATA_DIR,
target_size=(IMG_WIGTH, IMG_HEIGHT),
batch_size=BATCH_SIZE,
shuffle=False,
class_mode='categorical')
def model_maker():
base_model = MobileNet(include_top=False, input_shape=(IMG_WIGTH, IMG_HEIGHT, 3))
for layer in base_model.layers[:]:
layer.trainable = False
input = Input(shape=(IMG_WIGTH, IMG_HEIGHT, 3))
custom_model = base_model(input)
custom_model = GlobalAveragePooling2D()(custom_model)
custom_model = Dense(64, activation='relu')(custom_model)
custom_model = Dropout(0.5)(custom_model)
predictions = Dense(NUM_CLASSES, activation='softmax')(custom_model)
return Model(inputs=input, outputs=predictions)
model = model_maker()
model.compile(loss='categorical_crossentropy',
optimizer=tf.keras.optimizers.Adam(0.001),
metrics=['acc'])
model.fit(
train_generator,
steps_per_epoch=math.ceil(float(TRAIN_SAMPLES) / BATCH_SIZE),
validation_data=validation_generator,
validation_steps=math.ceil(float(VALIDATION_SAMPLES) / BATCH_SIZE))
运行时提示以下错误,求解答
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/tmp/ipykernel_27/783854733.py in <module>
18 steps_per_epoch=math.ceil(float(TRAIN_SAMPLES) / BATCH_SIZE),
19 validation_data=validation_generator,
---> 20 validation_steps=math.ceil(float(VALIDATION_SAMPLES) / BATCH_SIZE))
21
22
/opt/conda/lib/python3.7/site-packages/keras/utils/traceback_utils.py in error_handler(*args, **kwargs)
68 # To get the full stack trace, call:
69 # `tf.debugging.disable_traceback_filtering()`
---> 70 raise e.with_traceback(filtered_tb) from None
71 finally:
72 del filtered_tb
/opt/conda/lib/python3.7/site-packages/keras/preprocessing/image.py in __getitem__(self, idx)
104 "Asked to retrieve element {idx}, "
105 "but the Sequence "
--> 106 "has length {length}".format(idx=idx, length=len(self))
107 )
108 if self.seed is not None:
ValueError: Asked to retrieve element 0, but the Sequence has length 0