gpt4 book ai didi

python - 识别猫狗: Error in Flattening in Keras Tensorflow

转载 作者:行者123 更新时间:2023-12-01 01:39:39 25 4
gpt4 key购买 nike

我正在努力进一步加深对 Keras 的理解,但遇到了一个令人困惑的错误。我正在通过 Anaconda 使用最新的软件包。该错误似乎与 model.add(Flatten()) 有关,但我尝试的所有操作似乎都失败了。如果您能阐明如何思考这一点或超越谷歌的方向,我们将不胜感激。

数据文件:https://www.kaggle.com/c/dogs-vs-cats/data

数据结构:

data/
train/
dogs/
dog001.jpg
dog002.jpg
...
cats/
cat001.jpg
cat002.jpg
...
validation/
dogs/
dog001.jpg
dog002.jpg
...
cats/
cat001.jpg
cat002.jpg
...

代码https://gist.github.com/fchollet/0830affa1f7f19fd47b06d4cf89ed44d/47d3e33764c902ed33a64f35f5f68d911de05d8d :

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

print(keras.__version__)


# dimensions of our images.
img_width, img_height = 150, 150

train_data_dir = 'data\\train\\'
validation_data_dir = 'data\\validation\\'
nb_train_samples = 2000
nb_validation_samples = 800
epochs = 50
batch_size = 16


if K.image_data_format() == 'channels_first':
input_shape = (3, img_height, img_width)
else:
input_shape = (img_height, img_width, 3)

model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape = input_shape))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('sigmoid'))

model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])

# this is the augmentation configuration we will use for training
train_datagen = ImageDataGenerator(
rescale = 1. / 255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)

# this is the augmentation configuration we will use for testing:
# only rescaling
test_datagen = ImageDataGenerator(rescale = 1. / 255)

train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size = (img_height, img_width),
batch_size = batch_size,
class_mode = 'binary')

validation_generator = test_datagen.flow_from_directory(
validation_data_dir,
target_size = (img_height, img_width),
batch_size = batch_size,
class_mode = 'binary')

model.fit_generator(
train_generator,
steps_per_epoch = nb_train_samples // batch_size,
epochs = epochs,
validation_data = validation_generator,
validation_steps = nb_validation_samples // batch_size)

model.save_weights('first_try.h5')

错误:

InvalidArgumentError: Reshape cannot infer the missing input size for an empty tensor unless all specified input sizes are non-zero
[[Node: flatten_3/Reshape = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _class=["loc:@training_2/RMSprop/gradients/flatten_3/Reshape_grad/Reshape"], _device="/job:localhost/replica:0/task:0/device:GPU:0"](max_pooling2d_9/MaxPool-3-0-TransposeNCHWToNHWC-LayoutOptimizer, flatten_3/stack)]]
[[Node: metrics_2/acc/Mean_1/_263 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_741_metrics_2/acc/Mean_1", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

最佳答案

OP's comment 中所述,通过重新安装Conda/Keras/Tensorflow解决了问题。

注意:此答案作为社区 wiki 发布,如 accepted answer 中的建议。的"Question with no answers, but issue solved in the comments (or extended in chat)" .

关于python - 识别猫狗: Error in Flattening in Keras Tensorflow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52023609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com