在keras2中保存模型时,“ NoneType”对象没有属性“ update”

时间:2019-03-14 07:55:39

标签: keras python-3.5

背景: keras2 + tensorflow1.9.0 + python3.5

我有一个这样的模型构建:

def Build_SiameseNet():
input_shape = (7, 128, 128, 1)
model = Sequential()
model.add(Conv3D(8, (2, 7, 7), input_shape=input_shape, name='Conv_1', padding='same', kernel_initializer=W_init,
                 bias_initializer=b_init, strides=(1, 1, 1)))  # 9 13 17
model.add(BatchNormalization(axis=1))
model.add(Activation('relu', name='Act_1'))
# model.add(MaxPooling3D(pool_size=(1,3,3),strides=(1,2,2)))

model.add(Conv3D(32, (2, 5, 5), name='Conv_2', strides=(1, 1, 1), kernel_initializer=W_init,
                 bias_initializer=b_init))
model.add(BatchNormalization(axis=1))
model.add(Activation('relu', name='Act_2'))
model.add(AveragePooling3D(pool_size=(1, 2, 2)))

model.add(Conv3D(128, (2, 3, 3), name='Conv_3', strides=(1, 1, 1), kernel_initializer=W_init,
                 bias_initializer=b_init))
model.add(BatchNormalization(axis=1))
model.add(Activation('relu', name='Act_3'))

model.add(AveragePooling3D(pool_size=(1, 2, 2)))

model.add(Conv3D(256, (2, 3, 3), name='Conv_4', strides=(1, 1, 1), kernel_initializer=W_init,
                 bias_initializer=b_init))
model.add(BatchNormalization(axis=1))
model.add(Activation('relu', name='Act_4'))

model.add(AveragePooling3D(pool_size=(1, 2, 2)))

model.add(Conv3D(512, (2, 3, 3), name='Conv_5', strides=(1, 1, 1), kernel_initializer=W_init,
                 bias_initializer=b_init))
model.add(BatchNormalization(axis=1))
model.add(Activation('relu', name='Act_5'))

# model.add(LocallyConnected2D())
model.add(AveragePooling3D(pool_size=(3, 4, 4)))
model.add(Flatten(name='Flatten'))

# model.add(Dense(24,name='FC_1',kernel_regularizer=regularizers.l1_l2(0.001,0.01)))

model.add(Dropout(0.5, name='Dropout'))
model.add(Activation('relu', name='Act_6'))
# model.add(Dense(1000, activation='relu'))
model.summary()

# Copy the net
left_input = Input(input_shape)
right_input = Input(input_shape)
encoded_l = model(left_input)
encoded_r = model(right_input)

# merge outputs of two net
L1_layer = Lambda(lambda tensors: K.abs(tensors[0] - tensors[1]))
L1_distance = L1_layer([encoded_l, encoded_r])

prediction = Dense(1, activation='sigmoid')(L1_distance)

siamese_net = Model(inputs=[left_input, right_input], outputs=prediction)

optimizer = optimizers.Adam(lr=3e-2, decay=1e-4)
siamese_net.compile(loss="binary_crossentropy", optimizer=optimizer, metrics=['accuracy'])

return siamese_net

我想在fit_generator拟合后保存模型,然后查看文档。 There is a Q&A named "How can I save a Keras model?" in FAQ of Documentation .说model.save()会满足我的要求。比我这样尝试:

his = model.fit_generator(self.generate(batch_size)...)
model.save('asd0314.h5')

错误信息:

Traceback (most recent call last):
File "/usr/local/pycharm-community-2017.2.4/helpers/pydev/pydevd.py", line 1599, in <module>
globals = debugger.run(setup['file'], None, None, is_module)
File "/usr/local/pycharm-community-2017.2.4/helpers/pydev/pydevd.py", line 1026, in run
pydev_imports.execfile(file, globals, locals)  # execute the script
File "/usr/local/pycharm-community-2017.2.4/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "/home/mashuyang/PycharmProjects/SiameseNet/copy0307LightSiamese0.98/SiameseNet_lighNet.py", line 65, in <module>
loss_line, acc_line, model_trained = loader.train(model, batch_size, epoch)
File "/home/mashuyang/PycharmProjects/SiameseNet/copy0307LightSiamese0.98/Siamese_Loader.py", line 148, in train
model.save('asd0314.h5')
File "/usr/local/lib/python3.5/dist-packages/keras/engine/topology.py", line 2580, in save
save_model(self, filepath, overwrite, include_optimizer)
File "/usr/local/lib/python3.5/dist-packages/keras/models.py", line 111, in save_model
'config': model.get_config()
File "/usr/local/lib/python3.5/dist-packages/keras/engine/topology.py", line 2421, in get_config
return copy.deepcopy(config)
File "/usr/lib/python3.5/copy.py", line 155, in deepcopy
y = copier(x, memo)
File "/usr/lib/python3.5/copy.py", line 243, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "/usr/lib/python3.5/copy.py", line 155, in deepcopy
y = copier(x, memo)
File "/usr/lib/python3.5/copy.py", line 218, in _deepcopy_list
y.append(deepcopy(a, memo))
File "/usr/lib/python3.5/copy.py", line 155, in deepcopy
y = copier(x, memo)
File "/usr/lib/python3.5/copy.py", line 243, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "/usr/lib/python3.5/copy.py", line 155, in deepcopy
y = copier(x, memo)
File "/usr/lib/python3.5/copy.py", line 243, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "/usr/lib/python3.5/copy.py", line 155, in deepcopy
y = copier(x, memo)
File "/usr/lib/python3.5/copy.py", line 223, in _deepcopy_tuple
y = [deepcopy(a, memo) for a in x]
File "/usr/lib/python3.5/copy.py", line 223, in <listcomp>
y = [deepcopy(a, memo) for a in x]
File "/usr/lib/python3.5/copy.py", line 155, in deepcopy
y = copier(x, memo)
File "/usr/lib/python3.5/copy.py", line 223, in _deepcopy_tuple
y = [deepcopy(a, memo) for a in x]
File "/usr/lib/python3.5/copy.py", line 223, in <listcomp>
y = [deepcopy(a, memo) for a in x]
File "/usr/lib/python3.5/copy.py", line 182, in deepcopy
y = _reconstruct(x, rv, 1, memo)
File "/usr/lib/python3.5/copy.py", line 297, in _reconstruct
state = deepcopy(state, memo)
File "/usr/lib/python3.5/copy.py", line 155, in deepcopy
y = copier(x, memo)
File "/usr/lib/python3.5/copy.py", line 243, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "/usr/lib/python3.5/copy.py", line 182, in deepcopy
y = _reconstruct(x, rv, 1, memo)
File "/usr/lib/python3.5/copy.py", line 297, in _reconstruct
state = deepcopy(state, memo)
File "/usr/lib/python3.5/copy.py", line 155, in deepcopy
y = copier(x, memo)
File "/usr/lib/python3.5/copy.py", line 243, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "/usr/lib/python3.5/copy.py", line 182, in deepcopy
y = _reconstruct(x, rv, 1, memo)
File "/usr/lib/python3.5/copy.py", line 306, in _reconstruct
y.__dict__.update(state)

AttributeError: 'NoneType' object has no attribute 'update'

谁可以帮助我〜我只想保存模型。我曾经尝试过使用his.history.save_model和save_modle(his.history.model),但两者都不起作用。

0 个答案:

没有答案