繁体   English   中英

在 Lambda 层以及 Dense 和 keras.backend.clear_session() 中使用 VGG preprocess_input 时出现 KERAS 错误

[英]KERAS error when using VGG preprocess_input in Lambda layers together with Dense and keras.backend.clear_session()

我需要在一个循环中创建几个模型(所以我need to clean the environment with keras.backend.clear_session() )但是,如果 model 包含一个Lambda ,则在第二个时间为vgg16.preprocess_input 。我创建 model 我得到ValueError: Tensor("PREPROCESS/Const:0", shape=(3,), dtype=float32) must be from the same graph as Tensor("PREPROCESS_1/strided_slice:0", shape=(?, 3), dtype=float32).

重现错误的代码:

# making the model
from keras.layers import Dense, Reshape, Lambda
from keras import Sequential
f = keras.applications.vgg16.preprocess_input
d_l = Dense(3, activation='linear', input_shape=(3,), name='MYDENSE')

p_l = Lambda(f,name='PREPROCESS')

model_mod = Sequential()
model_mod.add(d_l)
model_mod.add(p_l)
model_mod.summary()
model_mod.build()
# clean the environment
keras.backend.clear_session()
# making again the same model
f = keras.applications.vgg16.preprocess_input
d_l = Dense(3, activation='linear', input_shape=(3,), name='MYDENSE')
p_l = Lambda(f,name='PREPROCESS')
model_mod = Sequential()
model_mod.add(d_l)
model_mod.add(p_l)

keras 版本:'2.2.4'

以下代码适用于 Tensorflow

# making the model
import tensorflow as tf
from keras.layers import Dense, Reshape, Lambda
from keras import Sequential
f = tf.keras.applications.vgg16.preprocess_input
d_l = Dense(3, activation='linear', input_shape=(3,), name='MYDENSE')

p_l = Lambda(f,name='PREPROCESS')

model_mod = Sequential()
model_mod.add(d_l)
model_mod.add(p_l)
model_mod.summary()
model_mod.build()
# clean the environment
tf.keras.backend.clear_session()
# making again the same model
f = tf.keras.applications.vgg16.preprocess_input
d_l = Dense(3, activation='linear', input_shape=(3,), name='MYDENSE')
p_l = Lambda(f,name='PREPROCESS')
model_mod = Sequential()
model_mod.add(d_l)
model_mod.add(p_l)

Output

Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
MYDENSE (Dense)              (None, 3)                 12        
_________________________________________________________________
PREPROCESS (Lambda)          (None, 3)                 0         
=================================================================
Total params: 12
Trainable params: 12
Non-trainable params: 0

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM