我有一个简单的卷积网络(自动编码器),我想将模型分为编码器和解码器两部分。在编码器和解码器之间,我将一个随机图像添加到编码器的输出,然后将结果发送到解码器部分,但是当我想从编码器到解码器建立模型时,会产生以下错误:
ValueError:图形已断开:无法获得张量的值 层上的Tensor(“ input_2:0”,shape =(?, 28,28,1),dtype = float32) “ input_2”。可以顺利访问以下先前的层: []
我想创建解码器模型时产生了错误。我不明白为什么会产生此错误。请帮助我解决这个错误。
from keras.layers import Input, Concatenate, GaussianNoise,Dropout
from keras.layers import Conv2D
from keras.models import Model
from keras.datasets import mnist
from keras.callbacks import TensorBoard
from keras import backend as K
from keras import layers
import matplotlib.pyplot as plt
import tensorflow as tf
import keras as Kr
import numpy as np
import pylab as pl
import matplotlib.cm as cm
#-----------------building w train---------------------------------------------
w_main = np.random.randint(2,size=(1,4,4,1))
w_main=w_main.astype(np.float32)
w_expand=np.zeros((1,28,28,1),dtype='float32')
w_expand[:,0:4,0:4]=w_main
w_expand.reshape(1,28,28,1)
w_expand=np.repeat(w_expand,49999,0)
#-----------------building w validation---------------------------------------------
w_valid = np.random.randint(2,size=(1,4,4,1))
w_valid=w_valid.astype(np.float32)
wv_expand=np.zeros((1,28,28,1),dtype='float32')
wv_expand[:,0:4,0:4]=w_valid
wv_expand.reshape(1,28,28,1)
wv_expand=np.repeat(wv_expand,9999,0)
#-----------------building w test---------------------------------------------
w_test = np.random.randint(2,size=(1,4,4,1))
w_test=w_test.astype(np.float32)
wt_expand=np.zeros((1,28,28,1),dtype='float32')
wt_expand[:,0:4,0:4]=w_test
wt_expand.reshape(1,28,28,1)
wt_expand=np.repeat(wt_expand,10000,0)
#-----------------------encoder------------------------------------------------
#------------------------------------------------------------------------------
wtm=Input((28,28,1))
image = Input((28, 28, 1))
conv1 = Conv2D(16, (3, 3), activation='relu', padding='same', name='convl1e')(image)
conv2 = Conv2D(32, (3, 3), activation='relu', padding='same', name='convl2e')(conv1)
conv3 = Conv2D(8, (3, 3), activation='relu', padding='same', name='convl3e')(conv2)
DrO1=Dropout(0.25)(conv3)
encoded = Conv2D(1, (3, 3), activation='relu', padding='same',name='reconstructed_I')(DrO1)
#-----------------------adding w---------------------------------------
#add_const = Kr.layers.Lambda(lambda x: x + Kr.backend.constant(w_expand))
#encoded_merged=Kr.layers.Add()([encoded,wtm])
add_const = Kr.layers.Lambda(lambda x: x + wtm)
encoded_merged = add_const(encoded)
encoder=Model(inputs=image, outputs=encoded_merged)
encoder.summary()
#-----------------------decoder------------------------------------------------
#------------------------------------------------------------------------------
#encoded_merged = Input((28, 28, 2))
deconv1 = Conv2D(16, (3, 3), activation='relu', padding='same', name='convl1d')(encoded_merged)
deconv2 = Conv2D(32, (3, 3), activation='relu', padding='same', name='convl2d')(deconv1)
deconv3 = Conv2D(8, (3, 3), activation='relu',padding='same', name='convl3d')(deconv2)
DrO2=Dropout(0.25)(deconv3)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same', name='decoder_output')(DrO2)
decoder=Model(inputs=encoded_merged, outputs=decoded)
#decoder.summary()
新代码:
from keras.layers import Input, Concatenate, GaussianNoise,Dropout
from keras.layers import Conv2D
from keras.models import Model
from keras.datasets import mnist
from keras.callbacks import TensorBoard
from keras import backend as K
from keras import layers
import matplotlib.pyplot as plt
import tensorflow as tf
import keras as Kr
import numpy as np
import pylab as pl
import matplotlib.cm as cm
import keract
from keras import optimizers
from keras import regularizers
from keras.callbacks import EarlyStopping
from tensorflow.python.keras.layers import Lambda;
#-----------------building w train---------------------------------------------
w_main = np.random.randint(2,size=(1,14,14,1))
w_main=w_main.astype(np.float32)
w_expand=np.zeros((1,28,28,1),dtype='float32')
w_expand[:,0:14,0:14]=w_main
w_expand.reshape(1,28,28,1)
w_expand=np.repeat(w_expand,49999,0)
#-----------------building w validation---------------------------------------------
w_valid = np.random.randint(2,size=(1,14,14,1))
w_valid=w_valid.astype(np.float32)
wv_expand=np.zeros((1,28,28,1),dtype='float32')
wv_expand[:,0:14,0:14]=w_valid
wv_expand.reshape(1,28,28,1)
wv_expand=np.repeat(wv_expand,9999,0)
#-----------------building w test---------------------------------------------
w_test = np.random.randint(2,size=(1,14,14,1))
w_test=w_test.astype(np.float32)
wt_expand=np.zeros((1,28,28,1),dtype='float32')
wt_expand[:,0:14,0:14]=w_test
wt_expand.reshape(1,28,28,1)
#wt_expand=np.repeat(wt_expand,10000,0)
#-----------------------encoder------------------------------------------------
#------------------------------------------------------------------------------
wtm=Input((28,28,1))
image = Input((28, 28, 1))
conv1 = Conv2D(16, (3, 3), activation='relu', padding='same', name='convl1e')(image)
conv2 = Conv2D(32, (3, 3), activation='relu', padding='same', name='convl2e')(conv1)
conv3 = Conv2D(8, (3, 3), activation='relu', padding='same', name='convl3e')(conv2)
#conv3 = Conv2D(8, (3, 3), activation='relu', padding='same', name='convl3e', kernel_initializer='Orthogonal',bias_initializer='glorot_uniform')(conv2)
DrO1=Dropout(0.25)(conv3)
encoded = Conv2D(1, (3, 3), activation='relu', padding='same',name='reconstructed_I')(DrO1)
#-----------------------adding watermark---------------------------------------
#add_const = Kr.layers.Lambda(lambda x: x + Kr.backend.constant(w_expand))
#encoded_merged=Kr.layers.Add()([encoded,wtm])
add_const = Kr.layers.Lambda(lambda x: x[0] + x[1])
encoded_merged = add_const([encoded,wtm])
encoder=Model(inputs=[image,wtm], outputs= encoded_merged)
encoder.summary()
#-----------------------decoder------------------------------------------------
#------------------------------------------------------------------------------
deconv_input=Input((28,28,1))
#encoded_merged = Input((28, 28, 2))
deconv1 = Conv2D(16, (3, 3), activation='relu', padding='same', name='convl1d',kernel_regularizer=regularizers.l2(0.001), kernel_initializer='Orthogonal')(deconv_input)
deconv2 = Conv2D(32, (3, 3), activation='relu', padding='same', name='convl2d')(deconv1)
deconv3 = Conv2D(8, (3, 3), activation='relu',padding='same', name='convl3d')(deconv2)
DrO2=Dropout(0.25)(deconv3)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same', name='decoder_output')(DrO2)
decoder=Model(inputs=deconv_input, outputs=decoded)
#decoder.summary()
encoded_merged = encoder([image,wtm])
decoded = decoder(encoded_merged)
model=Model(inputs=[image,wtm],outputs=decoded)
#----------------------w extraction------------------------------------
convw1 = Conv2D(16, (3,3), activation='relu', padding='same', name='conl1w',kernel_regularizer=regularizers.l2(0.001), kernel_initializer='Orthogonal')(decoded)
convw2 = Conv2D(32, (3, 3), activation='relu', padding='same', name='convl2w')(convw1)
convw3 = Conv2D(8, (3, 3), activation='relu', padding='same', name='conl3w')(convw2)
DrO3=Dropout(0.25)(convw3)
pred_w = Conv2D(1, (1, 1), activation='sigmoid', padding='same', name='reconstructed_W')(DrO3)
# reconsider activation (is W positive?)
# should be filter=1 to match W
watermark_extraction=Model(inputs=[image,wtm],outputs=[decoded,pred_w])
#----------------------training the model--------------------------------------
#------------------------------------------------------------------------------
#----------------------Data preparesion----------------------------------------
(x_train, _), (x_test, _) = mnist.load_data()
x_validation=x_train[1:10000,:,:]
x_train=x_train[10001:60000,:,:]
#
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
x_validation = x_validation.astype('float32') / 255.
x_train = np.reshape(x_train, (len(x_train), 28, 28, 1)) # adapt this if using `channels_first` image data format
x_test = np.reshape(x_test, (len(x_test), 28, 28, 1)) # adapt this if using `channels_first` image data format
x_validation = np.reshape(x_validation, (len(x_validation), 28, 28, 1))
#---------------------compile and train the model------------------------------
# is accuracy sensible metric for this model?
adadelta=optimizers.Adadelta(lr=1.0,decay=1/1000)
watermark_extraction.compile(optimizer=adadelta, loss={'decoder_output':'mse','reconstructed_W':'mse'}, metrics=['mae'])
watermark_extraction.fit([x_train,w_expand], [x_train,w_expand],
epochs=10,
batch_size=32,
validation_data=([x_validation,wv_expand], [x_validation,wv_expand]),
callbacks=[TensorBoard(log_dir='E:/tmp/AutewithW200', histogram_freq=0, write_graph=False),EarlyStopping(monitor='val_loss', patience=10,min_delta=0)])
model.summary()
新错误:
ValueError:丢失字典中的未知条目:“ decoder_output”。只要 需要以下键:['model_14','reconstructed_W']
答案 0 :(得分:0)
当您在公式中放置张量而不是将张量传递到该图层时,<section class="about">
<div class="col-1">
<h3>About me</h3>
<p>smh
</p>
</div>
<div class="col-3 float-next">
<h3>My work</h3>
<p>smh
</p>
</div>
<div class="col-3 float-next">
<h3>Ambitions</h3>
<p>smh
</p>
</div>
<div class="col-3 float-next">
<h3>Accomplishments</h3>
<p>smh
</p>
</div>
</section>
.about{
background-color:$color-gray;
color:$color-main;
}
.col-1 {
transition: 0.5s;
text-align: center;
padding: 2em 20em 2em 20em;
p{
font-size: 18px;
line-height: 200%;
}
}
.col-1:hover{
transition: 0.5s;
background-color: $color-black;
}
.float-next {
float: left;
width: 33.33%;
text-align: center;
padding: 10px 0 1em;
background-color: $color-gray;
font-size: 18px;
line-height: 200%;
transition: 0.5s;
}
.float-next:hover{
transition: 0.5s;
background-color:$color-black;
}
.col-3{
padding: 5em ;
}
.row:after {
content: "";
display: table;
clear: both;
}
层正在破坏系统。
Lambda
或者简单地:
add_const = Kr.layers.Lambda(lambda x: x[0] + x[1])
encoded_merged = add_const([encoded,wtm])
您必须将encoded_merged = Add()([encoded,wtm])
作为模型的输入:
wtm
模型应该从输入张量开始,而不是从图形中间的张量开始:
encoder = Model(inputs=[image,wtm], outputs = encoded_merged)
然后您可以创建自动编码器:
deconv_inputs = Input(shape_of_encoded_merged)
deconv1 = Conv2D(16, (3, 3), activation='relu', padding='same', name='convl1d')(deconv_inputs)
....
decoder = Model(inputs=deconv_inputs, outputs=decoded)