keras fit_generator的参数数量出错

时间:2018-05-08 21:13:22

标签: python tensorflow keras

我无法调试,运行此训练模型,然后保存权重。

代码:

#Part 1 - Building the CNN

#Importing the Keras libraries and packages

from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
#Initialising the CNN

classifier = Sequential()
#Step 1 - Convolution

#classifier.add(Conv2D(32, (3, 3), input_shape = (64, 64, 3), activation = 'relu'))

classifier.add(Conv2D(32,3,3,input_shape = (64, 64, 3),activation = 'relu'))
#Step 2 - Pooling

classifier.add(MaxPooling2D(pool_size = (2, 2)))
#Adding a second convolutional layer

classifier.add(Conv2D(32, 3, 3, activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))
#Step 3 - Flattening

classifier.add(Flatten())
#Step 4 - Full connection

classifier.add(Dense(128, activation = 'relu'))
classifier.add(Dense(1, activation = 'sigmoid'))
#Compiling the CNN

classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy',      metrics = ['accuracy'])
#Part 2 - Fitting the CNN to the images

from keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(rescale = 1./255,
                           shear_range = 0.2,
                           zoom_range = 0.2,
                           horizontal_flip = True)

test_datagen = ImageDataGenerator(rescale = 1./255)

training_set = train_datagen.flow_from_directory('training_set',
                                         target_size = (64, 64),
                                         batch_size = 32,
                                         class_mode = 'binary')

test_set = test_datagen.flow_from_directory('test_set',
                                    target_size = (64, 64),
                                    batch_size = 32,
                                    class_mode = 'binary')

classifier.fit_generator(training_set,
                 validation_data = test_set, 
                 validation_steps = 2000,
                 steps_per_epoch = 8000, 
                 epochs = 25)


classifier.save("weights.h5")

问题#1 :我收到以下错误:

Found 8005 images belonging to 2 classes.
Found 2023 images belonging to 2 classes.
-----------------------------------------------------------------------TypeError Traceback (most recent call last) in ()
58 validation_steps = 2000,
59 steps_per_epoch = 8000,
---> 60 epochs = 25)
61
62
TypeError: fit_generator() takes at least 4 arguments (3 given)

问题#2 :我想保存经过训练的砝码,以便我不需要一遍又一遍地运行它。
我单独运行了classifier.save("weights.h5"),并使用以下消息创建了一个空文件(因为它无法训练)。如何保存模型权重?

Error! /Users/xx/xx/xx/cnn_050518/model_weights.h5 is not UTF-8 encoded Saving disabled. See Console for more details.
versions of tools used (by entering print tool.version) keras: 1.1.1 tensorflow: 0.11.orc2 python:2.7 Macbook version 10.13.2

0 个答案:

没有答案