I have a Net built in Theano. I am trying to convert it to Keras.
net['voxres2_out'] = ElemwiseSumLayer([net['conv1c'], net['voxres2_conv2']])
Now, I want a conversion of this statement in Theano to Keras. However, I am unsure about which layer would be a substitute of the same functionality in Keras. Initially, thought Concatenate would be the option, but I am not sure.
Below is the original code in theano
net = {}
net['input'] = InputLayer((None, 1, 110, 110, 110), input_var=input_var)
net['conv1a'] = Conv3DDNNLayer(net['input'], 32, 3, pad='same',
nonlinearity=identity)
net['bn1a'] = BatchNormLayer(net['conv1a'])
net['relu1a'] = NonlinearityLayer(net['bn1a'])
net['conv1b'] = Conv3DDNNLayer(net['relu1a'], 32, 3, pad='same',
nonlinearity=identity)
net['bn1b'] = BatchNormLayer(net['conv1b'])
net['relu1b'] = NonlinearityLayer(net['bn1b'])
net['conv1c'] = Conv3DDNNLayer(net['relu1b'], 64, 3, stride=(2, 2, 2),
pad='same', nonlinearity=identity)
# VoxRes block 2
net['voxres2_bn1'] = BatchNormLayer(net['conv1c'])
net['voxres2_relu1'] = NonlinearityLayer(net['voxres2_bn1'])
net['voxres2_conv1'] = Conv3DDNNLayer(net['voxres2_relu1'], 64, 3,
pad='same', nonlinearity=identity)
net['voxres2_bn2'] = BatchNormLayer(net['voxres2_conv1'])
net['voxres2_relu2'] = NonlinearityLayer(net['voxres2_bn2'])
net['voxres2_conv2'] = Conv3DDNNLayer(net['voxres2_relu2'], 64, 3,
pad='same', nonlinearity=identity)
net['voxres2_out'] = ElemwiseSumLayer([net['conv1c'],
net['voxres2_conv2']])
# VoxRes block 3
net['voxres3_bn1'] = BatchNormLayer(net['voxres2_out'])
Following is my conversion to Keras
classifier = Sequential()
classifier.add(Conv3D(32, (3, 3, 3), input_shape = (128, 128, 128, 1), padding = 'same', activation = 'relu'))
classifier.add(BatchNormalization())
classifier.add(Activation('relu'))
classifier.add(Conv3D(32, (3, 3, 3), input_shape = (128, 128, 128, 1), padding = 'same', activation = 'relu'))
classifier.add(BatchNormalization())
classifier.add(Activation('relu'))
classifier.add(Conv3D(64, (3, 3, 3), input_shape = (128, 128, 128, 1), padding = 'same', activation = 'relu', strides = (2, 2, 2)))
classifier_1 = Sequential()#comment
classifier_1 = classifier
classifier_1.add(BatchNormalization())
classifier_1.add(Activation('relu'))
classifier_1.add(Conv3D(64, (3, 3, 3), input_shape = (128, 128, 128, 1), padding = 'same', activation = 'relu'))
classier_added_1 = Add()([classifier.output, classifier_1.output])
classifier_2 = Sequential()
classifier_2 = classier_added_1
classifier_2.add(BatchNormalization())
classifier_2.add(Activation('relu'))
classifier_2.add(Conv3D(64, (3, 3, 3), padding = 'same', activation = 'relu'))
So I used the Add function part of Keras. However, it returns a tensor and on executing the statements associated with classifier_2 using the returned tensor, I get the following error.
AttributeError: 'Tensor' object has no attribute 'add'
I understand the error, however I don't know the workaround.
Could you help?
In order to solve the issue, I built the model independently and over there tensor addition works.
classifier_added_2 = Add()([classifier_2, classifier_3])
Where classifier_2 and classifier_3 are the input tensors and classifier_added_2 is the output tensor.
Now, these tensors can be buil using the Model function in Keras