错误: ‘ImageDataGenerator’ 对象没有 ‘shape’ 属性。

huangapple go评论75阅读模式
英文:

Error: 'ImageDataGenerator' object has no attribute 'shape'

问题

我是编程新手,一直在尝试编写用于图像分类的神经网络代码,但不幸的是,我遇到了这个错误:

```python
import tensorflow as tf
import tensorflow.keras as kr
import os

train_PATH='./Data/Entrenamiento/'
valid_PATH='./Data/Validacion/'

# 参数

epocas=20
alt,long=100,100
batch_size=32
pasos=1000
pasos_valid=200
n_filtroConv1=32
n_filtroConv2=64
大小_filtro1=(3,3)
大小_filtro2=(2,2)
大小_pooling=(2,2)
clases=3
lr=0.0005

# 图像预处理

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')

# 创建卷积神经网络

nn=kr.Sequential()

nn.add(kr.layers.Convolution2D(n_filtroConv1,大小_filtro1,padding='same',input_shape=(alt,long,3),activation='relu'))
nn.add(kr.layers.MaxPooling2D(pool_size=大小_pooling))
nn.add(kr.layers.Convolution2D(n_filtroConv2,大小_filtro2,padding='same',activation='relu'))
nn.add(kr.layers.MaxPooling2D(pool_size=大小_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')

当我在Anaconda-Navigator上的Python 3.7中使用Spyder执行此代码时,我遇到了这个错误:

```python
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'
英文:

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:

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'

答案1

得分: 1

生成器是 imagen_entrenamiento,而不是 train_generator

使用 nn.fit_generator(imagen_entrenamiento,...)

英文:

The generator is imagen_entrenamiento, not train_generator.

Use nn.fit_generator(imagen_entrenamiento,...)

huangapple
  • 本文由 发表于 2020年1月6日 21:44:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613222.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定