[英]Error: 'ImageDataGenerator' object has no attribute 'shape'
I am new in coding and I've been trying to code this neural network for images classification, but unfortunately, I've encountered this error:我是编码新手,我一直在尝试为图像分类编写这个神经网络,但不幸的是,我遇到了这个错误:
import tensorflow as tf
import tensorflow.keras as kr
import os
train_PATH='./Data/Entrenamiento/'
valid_PATH='./Data/Validacion/'
# Parámetros
epocas=20
alt,long=100,100
batch_size=32
pasos=1000
pasos_valid=200
n_filtroConv1=32
n_filtroConv2=64
tamaño_filtro1=(3,3)
tamaño_filtro2=(2,2)
tamaño_pooling=(2,2)
clases=3
lr=0.0005
# Pre-procesamiento de imágenes
train_generador=kr.preprocessing.image.ImageDataGenerator(
rescale=1./255,
shear_range=0.3,
zoom_range=0.3,
horizontal_flip=True)
valid_generador=kr.preprocessing.image.ImageDataGenerator(
rescale=1./255)
imagen_entrenamiento=train_generador.flow_from_directory(
train_PATH,
target_size=(alt,long),
batch_size=batch_size,
class_mode='categorical')
imagen_validacion=valid_generador.flow_from_directory(
valid_PATH,
target_size=(alt,long),
batch_size=batch_size,
class_mode='categorical')
# Crear red convolucional
nn=kr.Sequential()
nn.add(kr.layers.Convolution2D(n_filtroConv1,tamaño_filtro1,padding='same',input_shape=(alt,long,3),activation='relu'))
nn.add(kr.layers.MaxPooling2D(pool_size=tamaño_pooling))
nn.add(kr.layers.Convolution2D(n_filtroConv2,tamaño_filtro2,padding='same',activation='relu'))
nn.add(kr.layers.MaxPooling2D(pool_size=tamaño_pooling))
nn.add(kr.layers.Flatten())
nn.add(kr.layers.Dense(256,activation='relu'))
nn.add(kr.layers.Dropout(0.5))
nn.add(kr.layers.Dense(clases,activation='softmax'))
nn.compile(loss='categorical_crossentropy',optimizer=tf.keras.optimizers.Adam(lr=lr),metrics=['accuracy'])
nn.fit_generator(train_generador,steps_per_epoch=pasos,epochs=epocas,validation_data=imagen_validacion,validation_steps=pasos_valid)
dir_modelo='./modelo/'
if not os.path.exists(dir_modelo):
os.mkdir(dir_modelo)
nn.save(dir_modelo+'modelo.h5')
nn.save_weights(dir_modelo+'pesos.h5')
When I execute this code on Spyder with Python 3.7 on Anaconda-Navigator, I'm facing with this error:当我在 Anaconda-Navigator 上使用 Python 3.7 在 Spyder 上执行此代码时,我遇到了以下错误:
nn.fit_generator(train_generador,steps_per_epoch=pasos,epochs=epocas,validation_data=imagen_validacion,validation_steps=pasos_valid)
File "/Applications/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training.py", line 1297, in fit_generator
steps_name='steps_per_epoch')
File "/Applications/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_generator.py", line 144, in model_iteration
shuffle=shuffle)
File "/Applications/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_generator.py", line 477, in convert_to_generator_like
num_samples = int(nest.flatten(data)[0].shape[0])
AttributeError: 'ImageDataGenerator' object has no attribute 'shape'
The generator is imagen_entrenamiento
, not train_generator
.生成器是imagen_entrenamiento
,而不是train_generator
。
Use nn.fit_generator(imagen_entrenamiento,...)
使用nn.fit_generator(imagen_entrenamiento,...)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.