I have to work with tensorflow 1.15 and need a custom layer. A very simplistic layer can look like this:
class Dummy(keras.layers.Layer):
def __init__(self, units=32, input_dim=32):
super(Dummy, self).__init__()
self.cnt = 1
def call(self, inputs):
self.cnt += 1
return inputs
If I use this Dummy Layer in any architecture the variable cnt was only set to two. What am I missing?
Here is a very simplistic dummy script to shwocase my issue:
import numpy as np
import keras
from keras.models import Sequential
from keras.layers import Conv2D, Activation
from tensorflow import set_random_seed
from numpy.random import seed
seed(312991)
set_random_seed(3121991)
class Dummy(keras.layers.Layer):
def __init__(self, units=32, input_dim=32):
super(Dummy, self).__init__()
self.cnt = 1
def call(self, inputs):
self.cnt += 1
return inputs
# creating the input image
input_img = np.ones(shape=(8,8,3))
#adjust range
input_img_adjusted = input_img / 255
target = input_img_adjusted[:,:,0:2]
model = Sequential()
model.add(Conv2D(2, (3, 3),input_shape=input_img.shape, padding='same'))
model.add(Dummy())
model.add(Activation('sigmoid'))
opt = keras.optimizers.Adam(0.001)
model.compile(optimizer=opt,
loss="mean_absolute_error")
hist = model.fit(np.array(2048*[input_img_adjusted]),np.array(2048*[target]),epochs=100,batch_size=32)
print("called the Dummy Layer:", model.layers[-2].cnt)
My assumption would have been that it is something like 32,32*100 or something similar.
question from:
https://stackoverflow.com/questions/65842712/why-does-my-keras-custom-layer-only-gets-called-once 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…