我正在尝试将前一层的各个内核输出提供给新的conv过滤器,以获得下一层。为此,我尝试通过Conv2D
传递每个内核输出,通过它们的索引调用它们。我使用的功能是:
def modification(weights_path=None, classes=2):
###########
## Input ##
###########
### 224x224x3 sized RGB Input
inputs = Input(shape=(224,224,3))
#################################
## Conv2D Layer with 5 kernels ##
#################################
k = 5
x = Conv2D(k, (3,3), data_format='channels_last', padding='same', name='block1_conv1')(inputs)
y = np.empty(k, dtype=object)
for i in range(0,k):
y[i] = Conv2D(1, (3,3), data_format='channels_last', padding='same')(np.asarray([x[i]]))
y = keras.layers.concatenate([y[i] for i in range (0,k)], axis=3, name='block1_conv1_loc')
out = Activation('relu')(y)
print ('Output shape is, ' +str(out.get_shape()))
### Maxpooling(2,2) with a stride of (2,2)
out = MaxPooling2D((2,2), strides=(2,2), data_format='channels_last')(out)
############################################
## Top layer, with fully connected layers ##
############################################
out = Flatten(name='flatten')(out)
out = Dense(4096, activation='relu', name='fc1')(out)
out = Dropout(0.5)(out)
out = Dense(4096, activation='relu', name='fc2')(out)
out = Dropout(0.5)(out)
out = Dense(classes, activation='softmax', name='predictions')(out)
if weights_path:
model.load_weights(weights_path)
model = Model(inputs, out, name='modification')
return model
但这不起作用,并抛出以下错误:
Traceback (most recent call last):
File "sim-conn-edit.py", line 137, in <module>
model = modification()
File "sim-conn-edit.py", line 38, in modification
y[i] = Conv2D(1, (3,3), data_format='channels_last', padding='same')(np.asarray([x[i]]))
File "/home/yx96/anaconda2/lib/python2.7/site-packages/keras/engine/topology.py", line 511, in __call__
self.assert_input_compatibility(inputs)
File "/home/yx96/anaconda2/lib/python2.7/site-packages/keras/engine/topology.py", line 408, in assert_input_compatibil
ity
if K.ndim(x) != spec.ndim:
File "/home/yx96/anaconda2/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 437, in ndim
dims = x.get_shape()._dims
AttributeError: 'numpy.ndarray' object has no attribute 'get_shape'
我将图层x[i]
作为[ x[i] ]
提供,以满足Conv2D
图层的尺寸要求。任何帮助解决这个问题的人都会非常感激!
答案 0 :(得分:2)
在StackOverflow中发布this和this个问题以及一些个人探索之后,我想出了一个解决方案。人们可以用Lambda
层做到这一点;通过调用Lambda
图层来提取上一层的子部分。例如,如果Lambda
函数定义为,
def layer_slice(x,i):
return x[:,:,:,i:i+1]
然后,称为,
k = 5
x = Conv2D(k, (3,3), data_format='channels_last', padding='same', name='block1_conv1')(inputs)
y = np.empty(k, dtype=object)
for i in range(0,k):
y[i] = Lambda(layer_slice, arguments={'i':i})(x)
y[i] = Conv2D(1,(3,3), data_format='channels_last', padding='same')(y[i])
y = keras.layers.concatenate([y[i] for i in range (0,k)], axis=3, name='block1_conv1_loc')
out = Activation('relu')(y)
print ('Output shape is, ' +str(out.get_shape()))
它应该有效地将单个内核输出提供给新的Conv2D
层。从model.summary()
获得的层形状和相应的可训练参数数量与期望相匹配。感谢Daniel指出Lambda
图层无法获得可训练的权重。
答案 1 :(得分:2)
Prabaha。我知道你已经解决了你的问题,但现在我看到了你的答案,你可以不使用lambda层就可以做到这一点,只需将第一个Conv2D拆分为多个。具有k个滤波器的一层相当于具有一个滤波器的k层:
for i in range(0,k):
y[i] = Conv2D(1, (3,3), ... , name='block1_conv'+str(i))(inputs)
y[i] = Conv2D(1,(3,3), ...)(y[i])
y = Concatenate()([y[i] for i in range (0,k)])
out = Activation('relu')(y)
您可以计算答案中的总参数,并在此答案中进行比较。