Как добавить вычисления (например, log и sqrt root) в keras vgg16 перед слоем softmax - PullRequest
0 голосов
/ 23 апреля 2019

Я реализую улучшенный билинейный пул (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

1 Ответ

0 голосов
/ 02 мая 2019

Решением было только добавление слоя лямбда-кераса, как следует

My_Model.add(Lambda(BILINEAR_POOLING,output_shape=[512,512]))

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...