从Keras迁移到Tensorflow:“GlobalAveragePooling2D()”和“std bad:alloc”

时间:2017-12-30 19:28:24

标签: tensorflow machine-learning keras

我对tensorflow很新,并尝试将此keras cnn迁移到tensorflow。

 inputs = Input(shape=(1, BANDS, 500))

        x = Conv2D(100, kernel_size=(BANDS, 50), kernel_initializer='he_uniform')(inputs)
        x = BatchNormalization(axis=1)(x)
        x = LeakyReLU()(x)
        x = Dropout(0.25)(x)

        x = Conv2D(100, kernel_size=(1, 1), kernel_initializer='he_uniform')(x)
        x = BatchNormalization(axis=1)(x)
        x = LeakyReLU()(x)
        x = Dropout(0.25)(x)

        x = Conv2D(15, kernel_size=(1, 1), kernel_initializer='he_uniform')(x)
        x = Lambda(softmax, arguments={'axis': 1}, name='softmax')(x)

        x = GlobalAveragePooling2D()(x)

        model = Model(inputs=inputs, outputs=x)
        model.compile(loss='categorical_crossentropy', optimizer=keras.optimizers.Adam(lr=0.001),
                      metrics=['accuracy'])

这是Tf代码

def sai_net( ):

    network = input_data(shape=[None, NUM_OF_BANDS, 500, 1 ], name='features')
    network = conv_2d(network, 100 , [NUM_OF_BANDS,50 ], activation='relu')
    network = local_response_normalization(network)
    network = dropout(network, 0.25)
    network = conv_2d(network, 100 , [1,1], activation='relu')
    network = local_response_normalization(network)
    network = dropout(network, 0.25)
    network = conv_2d(network, 15, [1,1], activation='relu')
    network = fully_connected(network, 15, activation='softmax')
    network = regression(network, optimizer='momentum', loss='categorical_crossentropy', 
                         learning_rate=LEARNING_RATE, name='labels')

    model = tflearn.DNN(network, checkpoint_path=MODEL_PATH + MODEL_NAME, 
                        tensorboard_dir=TRAIN_PATH, tensorboard_verbose=3, max_checkpoints=1)
    return model

首先,我在如何在tf中使用GlobalAveragePooling时遇到问题,当我尝试使用完全连接的层时,它会给出一个错误:可能由于内存过多而导致分配。

基本上输入是Bands * 500的矩阵,其中band == 200,在下列情况下,输出是15个场景标签之一。 三个问题:

1)如何在此网络中应用GlobalAveragePooling图层

2)std:由于完全连接的层而导致的不良分配(8演出Ram,256演出的ssd)

3)如何在此网络中激活之前应用规范化

1 个答案:

答案 0 :(得分:0)

因为没有人回答这个问题:所以我想出了自己。坏:alloc确实是由于大量的内存开销。使用tflearn库的这个model的tf中的正确网络实现如下:

def sai_net( ):

    network = input_data(shape=[None, NUM_OF_BANDS, 500, 1 ], name='features')
    print(network.shape) 
    network = conv_2d(network, 100 , [NUM_OF_BANDS,50 ] , strides = [NUM_OF_BANDS,1] )
    print(network.shape) 
    network = tflearn.layers.batch_normalization(network)
    print(network.shape ) 
    network = tflearn.activations.relu(network)
    print(network.shape ) 
    network = dropout(network, 0.25)
    print(network.shape ) 
    network = conv_2d(network, 100 , [1,1])
    print(network.shape ) 
    network = tflearn.layers.batch_normalization(network)
    network = tflearn.activations.relu(network)
    network = dropout(network, 0.25)
    network = conv_2d(network, 15, [1,1], activation='softmax')
    print(network.shape ) 
    network = tflearn.layers.conv.global_avg_pool (network, name='GlobalAvgPool')
    print(network.shape)
    network = regression(network, optimizer='momentum', loss='categorical_crossentropy', 
                         learning_rate=LEARNING_RATE, name='labels')

    model = tflearn.DNN( network,tensorboard_dir=TRAIN_PATH, tensorboard_verbose=3 )
    return model