I used the following code on a training dataset of 40 images of flowers but the CNN classifier fails to classify it.]
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import MaxPooling2D
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Dense
from tensorflow.keras.preprocessing.image import ImageDataGenerator
model = Sequential()
model.add(Conv2D(16, (3, 3), input_shape = (32, 32, 3), activation = 'relu'))
model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(Flatten())
model.add(Dense(units = 128, activation = 'relu'))
model.add(Dense(units = 4, activation = 'softmax'))
model.summary()
model.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])
train_datagen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
val_datagen = ImageDataGenerator(rescale = 1./255)
training_set =
train_datagen.flow_from_directory('C:\\Users\\vinay\\flowerclassification\\dataset\\train',
target_size = (32, 32),
batch_size = 8
)
val_set =
val_datagen.flow_from_directory('C:\\Users\\vinay\\flowerclassification\\dataset\\val',
target_size = (32, 32),
batch_size = 8)
model.fit(training_set,
steps_per_epoch = 10,
epochs = 25,
validation_data = val_set,
validation_steps = 4)
model_json = model.to_json()
with open("model.json", "w") as json_file:
json_file.write(model_json)
model.save_weights("model.h5")
print("Saved model to disk")
Model.fit_generator
is deprecated and will be removed in a future version. Please use Model.fit
, which supports generators. warnings.warn(' Model.fit_generator
is deprecated and ' ValueError Traceback (most recent call last) in () ----> 1 model.fit_generator(training_set,steps_per_epoch = 10,epochs = 25,validation_data = val_set,validation_steps = 2)
7 frames /usr/local/lib/python3.7/dist-packages/keras_preprocessing/image/iterator.py in getitem (self, idx) 55 'but the Sequence ' 56 'has length {length}'.format(idx=idx, ---> 57 length=len(self))) 58 if self.seed is not None: 59 np.random.seed(self.seed + self.total_batches_seen)
ValueError: Asked to retrieve element 0, but the Sequence has length 0
also this message is printed in the previous step: Found 0 images belonging to 0 classes. Found 0 images belonging to 0 classes.
Please try again executing the same above code by removing steps_per_epoch
and validation_steps
from model.fit
:
model.fit(training_set,
#steps_per_epoch = 10,
epochs = 25,
validation_data = val_set,
# validation_steps = 4
)
Steps_per_epoch
and validation_steps
are not correct. Removing these from model will itself count the steps_per_epoch
as per given images_count
and batch_size
. Check this similar answer for reference.
As from warning, it shows Model.fit_generator
is deprecated. You can use model.fit
instead to remove the warning. Please let us know if the issue still persists.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.