Convert FC layers to Fully Conv layers

def to_fully_conv(model):
    new_model = Sequential()
    input_layer = InputLayer(input_shape=(None, None, 3), name="input_new")
    new_model.add(input_layer)
    index = 0
    for layer in model.layers:
        layer._name = layer._name + str(index)
        index += 1
        if "InputLayer" in str(layer):
            continue
        elif "Flatten" in str(layer):
            flattened_ipt = True
            f_dim = layer.input_shape
        elif "Dense" in str(layer):
            input_shape = layer.input_shape
            output_dim =  layer.get_weights()[1].shape[0]
            W,b = layer.get_weights()
            if flattened_ipt:
                shape = (f_dim[1],f_dim[2],f_dim[3],output_dim)
                new_W = W.reshape(shape)
                new_layer = Convolution2D(output_dim,
                                          (f_dim[1],f_dim[2]),
                                          strides=(1,1),
                                          activation=layer.activation,
                                          padding='valid',
                                          weights=[new_W,b])
                flattened_ipt = False
            else:
                shape = (1,1,input_shape[1],output_dim)
                new_W = W.reshape(shape)
                new_layer = Convolution2D(output_dim,
                                          (1,1),
                                          strides=(1,1),
                                          activation=layer.activation,
                                          padding='valid',
                                          weights=[new_W,b])
        else:
            new_layer = layer
        new_model.add(new_layer)
    return new_model

Post a Comment

0 Comments