在学习期间如何更改cnn的层?

时间:2019-01-23 00:04:15

标签: python tensorflow keras

我有一个自动编码器,我想向编码器输出添加随机图像,然后将其发送到解码器部分。我将代码放在这里:

from keras.layers import Input, Concatenate, GaussianNoise
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
import matplotlib.pyplot as plt

#-----------------------encoder------------------------------------------------
#------------------------------------------------------------------------------

def make_encoder():
    image = Input((28, 28, 1))
    x = Conv2D(8, (5, 5), activation='relu', padding='same')(image)
    x = Conv2D(4, (3, 3), activation='relu', padding='same')(x)
    x = Conv2D(2, (3, 3), activation='relu', padding='same')(x)
    encoded =  Conv2D(1, (3, 3), activation='relu', padding='same')(x)

    return Model(inputs=image, outputs=encoded)
encoder = make_encoder()
encoder.summary()

#-----------------------decoder------------------------------------------------
#------------------------------------------------------------------------------

def make_decoder():
    encoded_merged = Input((28, 28, 2))
    x = Conv2D(2, (5, 5), activation='relu', padding='same')(encoded_merged)
    x = Conv2D(4, (3, 3), activation='relu', padding='same')(x)
    x = Conv2D(8, (3, 3), activation='relu',padding='same')(x)
    decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same', name='decoder_output')(x) 

    return Model(inputs=encoded_merged, outputs=decoded)
decoder = make_decoder()
decoder.summary()

#-------------------w-predictor------------------------------------------------
#------------------------------------------------------------------------------

def make_w_predictor():
    decoded_noise = Input((28, 28, 1))
    x = Conv2D(8, (5, 5), activation='relu', padding='same')(decoded_noise)
    x = Conv2D(4, (3, 3), activation='relu', padding='same')(x)
    x = Conv2D(2, (3, 3), activation='relu', padding='same')(x)
    pred_w = Conv2D(1, (3, 3), activation='relu', padding='same', name='extracted_watermark')(x)  
    # reconsider activation (is W positive?)
    # should be filter=1 to match W
    return Model(inputs=decoded_noise, outputs=pred_w)

w_predictor = make_w_predictor()
w_predictor.summary()

#-------------------------put together-----------------------------------------
#------------------------------------------------------------------------------

def put_together(encoder, decoder):
    image = Input((28, 28, 1))
    w = Input((28, 28, 1))
    encoded = encoder(image)

#    encoded_merged = Concatenate(axis=3)([encoded, w])
    encoded_add = encoded+ w
#    decoded = decoder(encoded_merged)
    decoded = decoder(encoded_add)

#    decoded_noise = GaussianNoise(0.5)(decoded)
#    pred_w = w_predictor(decoded_noise)

    return Model(inputs=[image, w], outputs=[decoded])

model = put_together(encoder, decoder)
model.summary()

#----------------------training the model--------------------------------------
#------------------------------------------------------------------------------
import numpy as np
# dummy data

(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))
#images = np.random.random((1000, 28, 28, 1))
#w = np.random.randint(2,size=(1,4, 4, 1))
#in the folloing line we want to produce a watermark that the corner of image has random values 0 or 1 and 
#other part are 0
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)
wt=np.repeat(w_expand,49999,0)
wv = np.repeat(w_expand,9999,0)
wte = np.repeat(w_expand,10000,0)

# is accuracy sensible metric for this model?
model.compile(optimizer='adadelta', loss=['mse'], metrics=['mae'])
model.fit([x_train, wt], [x_train],
          epochs=5,
          batch_size=64, 
          validation_data=([x_validation,wv], [x_validation]),
          callbacks=[TensorBoard(log_dir='C:/tmp/autoencoder', histogram_freq=0, write_graph=False)])

但是会产生此错误:

  

ValueError:输入通道数与相应的不匹配   过滤器的尺寸,1!= 2

我不知道哪个部分会产生此错误!我还想可视化图层的输出,但是由于它们是作为函数实现的,所以我不知道该怎么做?请帮助我解决这些问题。

0 个答案:

没有答案