0

我从 Keras 开始,我需要实现某种类型:具有 1 个通道输入和 2 个通道作为输出的 CNN 都是回归?

4

1 回答 1

0

我的解决方案

dataset = "./dataset/img/"
dataset_file = "./dataset/gt.txt"

def model_network(dense_y, dense_x):

    main_input = Input(shape=shape_img[1:], name="main_input")
    flow = Convolution2D(32, 3, 3, border_mode='same')(main_input)
    flow = Activation('relu')(flow)
    flow = Convolution2D(32, 3, 3)(flow)
    flow = Activation('relu')(flow)
    flow = MaxPooling2D(pool_size=(2, 2))(flow)
    flow = Dropout(0.25)(flow)

    flow = Convolution2D(64, 3, 3, border_mode='same')(flow)
    flow = Activation('relu')(flow)
    flow = Convolution2D(64, 3, 3)(flow)
    flow = Activation('relu')(flow)
    flow = MaxPooling2D(pool_size=(2, 2))(flow)
    flow = Dropout(0.25)(flow)

    flow = Convolution2D(512, 3, 3, border_mode='same')(flow)
    flow = Activation('relu')(flow)
    flow = Convolution2D(512, 3, 3)(flow)
    flow = Activation('relu')(flow)
    flow = MaxPooling2D(pool_size=(2, 2))(flow)
    flow = Dropout(0.25)(flow)

    flow = Flatten()(flow)
    cod_x = Dense(100)(flow)
    output_x = Dense(dense_x, activation='sigmoid', name="output_x")(cod_x)

    cod_y = Dense(100)(flow)
    output_y = Dense(dense_y, activation='sigmoid', name="output_y")(cod_y)
    model = Model(input=[main_input], output=[output_x, output_y])

    return model


def train_model(model, path):

    # let's train the model using SGD + momentum (how original).
    sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)

    print("... compiling")
    model.compile(optimizer=sgd,
                  loss_weights=[0.5, 0.5],
                  loss='categorical_crossentropy')

    print("... training")
    model.fit_generator(generate_array_files(path), samples_per_epoch=500, nb_val_samples=60, nb_epoch=10)
    save_model(model)


if __name__ == '__main__':
    path = "dataset_names.csv"

    model_tracking = model_network(length_y, length_x)

    train_model(model_tracking, path=path)
于 2016-11-21T11:05:34.733 回答