Ronny Efronny
Ronny Efronny

Reputation: 1528

Externally adding another layer to convolution output

I have a network in which the data comes from a stack of images and a vector of numbers. I begin with two "branches": The images go through several convolutions and produce an output of shape (50, 50, 64). In the other branch I import the number and go:

x = Input(shape = (13)) # data vector is of length 13
x = Dense(50*50)(x)
x = Reshape((50,50))(x)

I now have 2 outputs from the branches - one is of shape (50, 50, 64) and the other of shape (50, 50, 1). How can I "stick" them together to get a collective (50, 50, 65) which I'd then Deconv2D?

Upvotes: 1

Views: 77

Answers (2)

Ahmad Moussa
Ahmad Moussa

Reputation: 864

You could use the keras Concatenate() layer as in follows:

import numpy as np
from keras import backend as K
from keras import layers

# create some dummy tensors with numpy and the keras backend
arr1 = K.constant(np.zeros((50, 50, 1)))
arr2 = K.constant(np.zeros((50, 50, 64)))

# and this is how you call the concatenate layer
combined = layers.Concatenate()([arr1, arr2])

# it should print this: 
# Tensor("concatenate_1/concat:0", shape=(50, 50, 65), dtype=float32)
print(combined)

Upvotes: 1

abhilb
abhilb

Reputation: 5757

you can use numpy function : np.c_

try:

>>> x.shape
(50, 50, 64)
>>> y.shape
(50, 50, 1)
>>> z = np.c_[x,y]
>>> z.shape
(50, 50, 65)

Upvotes: 0

Related Questions