Keras実装中のエラー対処

はじめに

Keras実装中のエラーメモ書きしておきます

AttributeError: ‘KerasTensor’ object has no attribute ‘summary’

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import MaxPool2D
from tensorflow.keras.optimizers import Adam

import tensorflow as tf
from tensorflow.keras.models import Sequential, Model
!pip install git+https://www.github.com/keras-team/keras-contrib.git
from tensorflow.keras.layers import Input,Dense, Activation, Dropout, Flatten,LeakyReLU,UpSampling2D,Concatenate
from keras_contrib.layers.normalization.instancenormalization import InstanceNormalization
from tensorflow.keras.utils import plot_model, to_categorical
from keras.callbacks import TensorBoard

class Generator():
  def __init__(self):
    self.img_rows = 256
    self.img_cols = 256
    self.channels = 3
    self.img_shape = (self.img_rows, self.img_rows, self.channels)
    self.gf = 32

  def encode(self,layer_input,filter_size,f_size = 4, normalization = True):
    d = Conv2D(filter_size,kernel_size = f_size,strides = 2,padding = "same")(layer_input)
    d = LeakyReLU(alpha = 0.2)(d)
    if normalization:
      d = InstanceNormalization()(d)
    return d

  def decode(self,layer_input,marge_layer,filter_size,f_size = 4, normalization = True,dropout_rate=0):
    u = UpSampling2D(size = 2)(layer_input)
    u = Conv2D(filter_size,kernel_size = f_size,strides = 1,padding = "same",activation="relu")(u)
    if dropout_rate:
      u = Dropout(dropout_rate)(u)
    u = InstanceNormalization()(u)
    u = Concatenate()([u,marge_layer])
    return u
  
  def forward(self):
    #make layer
    self.d0 = Input(shape = self.img_shape)
    # down sampling
    self.d1 = self.encode(self.d0,self.gf)
    self.d2 = self.encode(self.d1,self.gf*2)
    self.d3 = self.encode(self.d2,self.gf*4)
    self.d4 = self.encode(self.d3,self.gf*8)
    # up sampling
    self.u1 = self.decode(self.d4,self.d3,self.gf *4)
    self.u2 = self.decode(self.u1,self.d2,self.gf *2)
    self.u3 = self.decode(self.u2,self.d1,self.gf)
    #output layer
    self.u4 = UpSampling2D(size = 2)(self.u3)
    self.u5 = Conv2D(self.channels,kernel_size=4,strides = 1,padding="same",activation="tanh")(self.u4)
    return self.u5

model = Generator()
a = model.forward()
a.summary()

return self.u5をreturn Model(self.d0,self.u5)にすることで解決した

コメント

タイトルとURLをコピーしました