调用模型子类时,预期的字符串或类似字节的对象

问题描述

我试图写一个生成对抗网络。下面是其中一个鉴别符的代码

class D1(Layer):
def __init__ (self,input_shape=(256,256,3),name='d1',**kwargs):
    super(D1,self).__init__(name=name,**kwargs)
    self.h1 = Conv2D(64,(3,strides=(1,1),padding='same')
    self.h2 = MaxPooling2D(pool_size=(2,2),strides=None,padding='same')
    self.h3 = LeakyReLU(alpha=0.2)
    self.h4 = Conv2D(128,padding='same')
    self.h5 = Conv2D(128,padding='same')
    self.h6 = MaxPooling2D(pool_size=(2,padding='same')
    self.h7 = LeakyReLU(alpha=0.2)
    self.h8 = Conv2D(256,padding='same')
    self.h9 = Conv2D(256,padding='same')
    self.h10 = MaxPooling2D(pool_size=(2,padding='same')
    self.h11 = LeakyReLU(alpha=0.2)
    self.h12 = Conv2D(512,padding='same')
    self.h13 = Conv2D(512,padding='same')
    self.h14 = MaxPooling2D(pool_size=(2,padding='same')
    self.h15 = Flatten()
    self.h16 = Dropout(0.4)
    self.D1R = Dense(1,activation='sigmoid')
    self.h17 = Dense(4096,activation='relu')
    self.h18 = Dense(4096,activation='relu')
    self.D1C = Dense(16,activation='sigmoid')

def call(self,inputs):
    x = self.h1(inputs)
    x = self.h2(x)
    x = self.h3(x)
    x = self.h4(x)
    x = self.h5(x)
    x = self.h6(x)
    x = self.h7(x)
    x = self.h8(x)
    x = self.h9(x)
    x = self.h10(x)
    x = self.h11(x)
    x = self.h12(x)
    x = self.h13(x)
    x = self.h14(x)
    x = self.h15(x)
    x = self.h16(x)
    d1r = self.D1R(x)
    x = self.h17(x)
    x = self.h18(x)
    d1c = self.D1C(x)
    

    return d1r,d1c'''

class discriminator1(Model):
def __init__(
    self,input_shape=(None,name='disc1',**kwargs
):  
    super(discriminator1,**kwargs)
    self.d1 = D1(input_shape=input_shape)

def call(self,inputs):
    image = inputs
    d1r,d1c = self.d1(image)
    d1_loss = d1_loss(d1r,d1c)
    self.add_loss(d1_loss)
    return out

在训练中调用它时,它将引发TypeError:预期的字符串或类似字节的对象。我不知道是什么。 有什么帮助吗?我的所有功能都不应该使用字符串

  '''def generate_latent_noise(latent_dim,n_samples):
        x_input = randn(latent_dim * n_samples)
        x_input = x_input.reshape(n_samples,latent_dim)
        return x_input'''
  '''def generate_fake_samples(g,latent_dim,n_samples,y_i,y_l):
         x_input = generate_latent_noise(latent_dim,n_samples)
         X = g.predict(x_input)
         y = zeros((n_samples,1))
         for i in range(n_samples-1):
           intent = y_i[i]
           bio = y_l[i]
        return X,y,intent,bio'''

  '''epochs = 200
     opt = SGD(learning_rate=1e-3,momentum=0.99)
     metric = Accuracy()
     yi,yl = retrieve_target_labels('/content/drive/My Drive/Project/input.xlsx')
     g = Generator(100)
     d1 = D1((256,3))
     d2 = D2((256,3))
     gen = G_Model((256,100,yi,yl)
     disc1 = discriminator1((256,100)
     disc2 = discriminator2((256,100)
     art,yc_real,yi_real,yl_real =load_real_samples('/content/drive/MyDrive/Project/TrainSA.xlsx')
     half_batch = yi.shape[0]
     n_batch = half_batch * 2
     batch_per_epoch = int(art.shape[0]/n_batch)
     for epoch in range(epochs):
         for batch in range(batch_per_epoch):
             fake,yl = generate_fake_samples(g,half_batch,yl)

             real,y_real,c_real,i_real,l_real = generate_real_samples(art,yl_real)
    
            fake_image = tf.convert_to_tensor(fake)
    
            d1r,d1c = d1(fake_image) #error!
            d1_loss_fake = d1.losses
            d1r,d1c = d1(real)
            d1_loss = d1.losses
            d2i_fake,d2l_fake = d2(fake_image)
            d2_loss_fake = d2.losses
            d2i,d2l = d2(real)
            d2_loss = d2.losses
            g_loss = gen.losses '''

要提供一个最小的工作示例有点困难,因为在调用了许多函数之后,错误出现在代码的末尾,但是我试图包括可能与错误有关的那些函数

---------------------------------------------------------------------------
  TypeError                                 Traceback (most recent call last)
 <ipython-input-95-d4bb0da7c68f> in <module>()
      23         #d1_loss_fake = d1.losses
      24         real_image = tf.convert_to_tensor(real,dtype = tf.float32)
 ---> 25         d1r,d1c = disc1(real_image)
      26         d1_loss = d1.losses
      27         d2i_fake,d2l_fake = d2(fake_image)

 3 frames
 /tensorflow-1.15.2/python3.6/tensorflow_core/python/framework/ops.py in 
name_scope(self,name)
   4126         # Scopes created in the root must match the more restrictive
   4127         # op name regex,which constrains the initial character.
-> 4128         if not _VALID_OP_NAME_REGEX.match(name):
   4129           raise ValueError("'%s' is not a valid scope name" % name)
   4130     old_stack = self._name_stack

  TypeError: expected string or bytes-like object

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)