Я реализую улучшенный билинейный пул (http://vis -www.cs.umass.edu / bcnn / docs / улучшенный_bcnn.pdf ), и я хочу добавить вычисления (например, log и корень sqrt)перед софтмакс слоем на мою модель, которая прекрасно настроена из кераса vgg16.Как я могу это сделать?
vgg_16 = keras.applications.vgg16.VGG16(weights='imagenet',include_top=False, input_shape=(224, 224, 3))
vgg_16.layers.pop()
My_Model = Sequential()
for layer in vgg_16.layers:
My_Model.add(layer)
for layer in My_Model.layers:
layer.trainable = False
# I want to add this function on top of my model then feed the result to a softmax layer
#
def BILINEAR_POOLING(bottom1, bottom2, sum_pool=True):
assert(np.all(bottom1.shape[:3] == bottom2.shape[:3]))
batch_size, height, width = bottom1.shape[:3]
output_dim = bottom1.shape[-1] * bottom2.shape[-1]
bottom1_flat = bottom1.reshape((-1, bottom1.shape[-1]))
bottom2_flat = bottom2.reshape((-1, bottom2.shape[-1]))
output = np.empty((batch_size*height*width, output_dim), np.float32)
for n in range(len(output)):
output[n, ...] = np.outer(bottom1_flat[n], bottom2_flat[n]).reshape(-1)
output = output.reshape((batch_size, height, width, output_dim))
if sum_pool:
output = np.sum(output, axis=(1, 2))
return output