def create_miml_model(base_model, L, K, name="miml"):
"""
Arguments:
base_model (Sequential):
A Neural Network in keras form (e.g. VGG, GoogLeNet)
L (int):
number of labels
K (int):
number of sub categories
"""
model = Sequential(layers=base_model.layers, name=name)
# input: feature_map.shape = (n_bags, C, H, W)
_, C, H, W = model.layers[-1].output_shape
print("Creating miml... input feature_map.shape={},{},{}".format(C, H, W))
n_instances = H * W
# shape -> (n_bags, (L * K), n_instances, 1)
model.add(Convolution2D(L * K, 1, 1, name=MIML_FIRST_LAYER_NAME))
# shape -> (n_bags, L, K, n_instances)
model.add(Reshape((L, K, n_instances), name=MIML_CUBE_LAYER_NAME))
# shape -> (n_bags, L, 1, n_instances)
model.add(MaxPooling2D((K, 1), strides=(1, 1)))
# softmax
model.add(Reshape((L, n_instances)))
model.add(Permute((2, 1)))
model.add(Activation("softmax"))
model.add(Permute((2, 1)))
model.add(Reshape((L, 1, n_instances), name=MIML_TABLE_LAYER_NAME))
# shape -> (n_bags, L, 1, 1)
model.add(MaxPooling2D((1, n_instances), strides=(1, 1)))
# shape -> (n_bags, L)
model.add(Reshape((L,), name=MIML_OUTPUT_LAYER_NAME))
return model
评论列表
文章目录