Python 文本到图像GAN-自定义块和tf.decordent函数
我试图用Keras再现本文中描述的体系结构:。这是一个用于文本到图像生成的GAN。他们提出了一个层来多次融合文本嵌入和图像特征。我已经实现了该层,但它不起作用,并且在尝试开始培训时出现以下错误:ValueError:tf.function-decorded函数尝试在非第一次调用时创建变量 以下是第一个自定义图层的代码:Python 文本到图像GAN-自定义块和tf.decordent函数,python,tensorflow,keras,google-colaboratory,generative-adversarial-network,Python,Tensorflow,Keras,Google Colaboratory,Generative Adversarial Network,我试图用Keras再现本文中描述的体系结构:。这是一个用于文本到图像生成的GAN。他们提出了一个层来多次融合文本嵌入和图像特征。我已经实现了该层,但它不起作用,并且在尝试开始培训时出现以下错误:ValueError:tf.function-decorded函数尝试在非第一次调用时创建变量 以下是第一个自定义图层的代码: class affine_T(Layer): def __init__(self, text, units_out, **kwargs): super(affine_T,
class affine_T(Layer):
def __init__(self, text, units_out, **kwargs):
super(affine_T, self).__init__(**kwargs)
self.text = text
self.dense_1 = Dense(units=256)
self.dense_2 = Dense(units=units_out)
self.dense_3 = Dense(units=256)
self.dense_4 = Dense(units=units_out)
self.reshape_1 = Reshape((1, 1, units_out))
self.reshape_2 = Reshape((1, 1, units_out))
def build(self, input_shape):
self.built = True
def call(self, img):
gamma = self.dense_1(self.text)
gamma = ReLU()(gamma)
gamma = self.dense_2(gamma)
gamma = self.reshape_1(gamma)
beta = self.dense_3(self.text)
beta = ReLU()(beta)
beta = self.dense_4(beta)
beta = self.reshape_2(beta)
self.out = gamma * img + beta
return self.out
以下是使用第一个自定义图层的第二个自定义图层的代码:
class deep_F(Layer):
def __init__(self, nf_in, nf_out, text_input, **kwargs):
super(deep_F, self).__init__(**kwargs)
self.nf_in = nf_in
self.nf_out = nf_out
self.text = text_input
self.units_out = nf_in
self.affine_1 = affine_T(self.text, self.units_out)
self.affine_2 = affine_T(self.text, self.units_out)
self.affine_3 = affine_T(self.text, self.units_out)
self.affine_4 = affine_T(self.text, self.units_out)
self.conv2d_1 = Conv2D(filters=self.nf_in, kernel_size=(3, 3), strides=1, padding='same')
self.conv2d_2 = Conv2D(filters=self.nf_out, kernel_size=(3, 3), strides=1, padding='same')
def build(self, input_shape):
self.built = True
def call(self, inputs):
image_features = inputs
shortcut = image_features #preserve input information for residual block
#Fusion Block
x = self.affine_1(image_features)
x = LeakyReLU(alpha=0.2)(x)
x = self.affine_2(x)
x = LeakyReLU(alpha=0.2)(x)
x = self.conv2d_1(x)
#Fusion Block
x_1 = self.affine_3(x)
x_1 = LeakyReLU(alpha=0.2)(x_1)
x_1 = self.affine_4(x_1)
x_1 = LeakyReLU(alpha=0.2)(x_1)
x_1 = self.conv2d_2(x_1)
#check number of channels for residual operation
if self.nf_in != self.nf_out:
shortcut = Conv2D(filters=self.nf_out, kernel_size=(1, 1), strides=1, padding='valid') (shortcut) #!
return add([x_1, shortcut]) #Residual
相反,发电机是这样的:
input_z = Input(shape = (100,))
input_text = Input(shape = (256,))
#i = Dense(units=128)(input_text)
#i = LeakyReLU(alpha=0.2)(i)
concat_layer = Concatenate(axis=1)([input_z, input_text])
x = Dense(4*4*8*64)(concat_layer)
x = Reshape((4, 4, 64*8))(x)
#x = affine_T(i, 512)(x)
x = UpSampling2D(size=(2, 2))(x)
x = deep_F(512, 512, input_text)(x)
x = UpSampling2D(size=(2, 2))(x)
x = deep_F(512, 256, input_text)(x)
x = UpSampling2D(size=(2, 2))(x)
x = deep_F(256, 128, input_text)(x)
x = UpSampling2D(size=(2, 2))(x)
x = deep_F(128, 128, input_text)(x)
x = UpSampling2D(size=(2, 2))(x)
x = deep_F(128, 128, input_text)(x)
#x = UpSampling2D(size=(2, 2))(x)
#x = deep_F(512, 512, i)(x)
x = LeakyReLU(alpha=0.2)(x)
x_out = Conv2DTranspose(filters=3, kernel_size=(3, 3), strides=1, padding='same', use_bias=False, activation='tanh')(x)
- 是因为我多次使用文本输入吗
- 我是否在自定义层实现方面做错了什么
非常感谢你的帮助 因为我只使用API中的默认层,所以我通过将这些层包装到python函数中解决了这个问题。无论如何,在未来,我将研究如何更好地创建自定义层。