Keras&Tensorflow在构建模型时利用GPU

时间:2019-06-29 18:21:03

标签: python tensorflow keras deep-learning

创建以下Keras模型后,我得到 tensorflow警告,通常仅在训练开始时出现。当我对不同的模型使用相同的方法时,不会收到相同的警告。这是为什么?我看不到代码中应该以任何方式利用GPU的任何内容。

警告:

Using TensorFlow backend.
WARNING:tensorflow:From /home/xxx/PycharmProjects/WAVE/venv/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
2019-06-29 13:49:23.902006: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 4200000000 Hz
2019-06-29 13:49:23.902465: I tensorflow/compiler/xla/service/service.cc:150] XLA service 0x2177cd0 executing computations on platform Host. Devices:
2019-06-29 13:49:23.902480: I tensorflow/compiler/xla/service/service.cc:158]   StreamExecutor device (0): <undefined>, <undefined>
2019-06-29 13:49:24.084793: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:998] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2019-06-29 13:49:24.086719: I tensorflow/compiler/xla/service/service.cc:150] XLA service 0x20fc0a0 executing computations on platform CUDA. Devices:
2019-06-29 13:49:24.086730: I tensorflow/compiler/xla/service/service.cc:158]   StreamExecutor device (0): GeForce GTX 1070, Compute Capability 6.1
2019-06-29 13:49:24.086911: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1433] Found device 0 with properties: 
name: GeForce GTX 1070 major: 6 minor: 1 memoryClockRate(GHz): 1.835
pciBusID: 0000:01:00.0
totalMemory: 7.93GiB freeMemory: 7.16GiB
2019-06-29 13:49:24.086918: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1512] Adding visible gpu devices: 0
2019-06-29 13:49:24.088823: I tensorflow/core/common_runtime/gpu/gpu_device.cc:984] Device interconnect StreamExecutor with strength 1 edge matrix:
2019-06-29 13:49:24.088832: I tensorflow/core/common_runtime/gpu/gpu_device.cc:990]      0 
2019-06-29 13:49:24.088836: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1003] 0:   N 
2019-06-29 13:49:24.088981: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 6966 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1070, pci bus id: 0000:01:00.0, compute capability: 6.1)

下面是可重复性的完整代码。

完整代码:

from keras.models import Model
from keras.layers import Input, Activation, Dense, Flatten
from keras.layers import Conv2D, MaxPooling2D, AveragePooling2D
from keras.layers import add
from keras.layers import BatchNormalization
from keras.regularizers import l2
from keras import backend as K


def batch_norm(input, act):
    norm = BatchNormalization(axis=3)(input)
    return Activation(act)(norm)


def conv_batch_norm(filters, kernel_size, strides, padding, kernel_initializer, kernel_regularizer, act):

    def f(input):
        conv = Conv2D(filters=filters, kernel_size=kernel_size,
                      strides=strides, padding=padding,
                      kernel_initializer=kernel_initializer,
                      kernel_regularizer=kernel_regularizer)(input)
        return batch_norm(conv, act=act)

    return f


def batch_norm_conv(act='relu', **conv_params):
    filters = conv_params['filters']
    kernel_size = conv_params['kernel_size']
    strides = conv_params.setdefault('strides', (1, 1))
    kernel_initializer = conv_params.setdefault('kernel_initializer', 'he_normal')
    padding = conv_params.setdefault('padding', 'same')
    kernel_regularizer = conv_params.setdefault('kernel_regularizer', l2(1.e-4))

    def f(input):
        activation = batch_norm(input, act=act)
        return Conv2D(filters=filters, kernel_size=kernel_size,
                      strides=strides, padding=padding,
                      kernel_initializer=kernel_initializer,
                      kernel_regularizer=kernel_regularizer)(activation)

    return f


def shortcut(input, residual):
    input_shape = K.int_shape(input)
    residual_shape = K.int_shape(residual)
    stride_width = int(round(input_shape[1] / residual_shape[1]))
    stride_height = int(round(input_shape[2] / residual_shape[2]))
    equal_channels = input_shape[3] == residual_shape[3]

    shortcut = input
    if stride_width > 1 or stride_height > 1 or not equal_channels:
        shortcut = Conv2D(filters=residual_shape[3],
                          kernel_size=(1, 1),
                          strides=(stride_width, stride_height),
                          padding='valid',
                          kernel_initializer='he_normal',
                          kernel_regularizer=l2(0.0001))(input)

    return add([shortcut, residual])


def residual_block(block_function, filters, repetitions, is_first_layer=False):
    def f(input):
        for i in range(repetitions):
            init_strides = (1, 1)
            if i == 0 and not is_first_layer:
                init_strides = (2, 2)
            input = block_function(filters=filters, init_strides=init_strides,
                                   is_first_block_of_first_layer=(is_first_layer and i == 0))(input)
        return input

    return f


def basic_block(filters, init_strides=(1, 1), is_first_block_of_first_layer=False):
    def f(input):

        if is_first_block_of_first_layer:
            conv1 = Conv2D(filters=filters, kernel_size=(3, 3),
                           strides=init_strides,
                           padding='same',
                           kernel_initializer='he_normal',
                           kernel_regularizer=l2(1e-4))(input)
        else:
            conv1 = batch_norm_conv(filters=filters, kernel_size=(3, 3),
                                    strides=init_strides, act='relu')(input)

        residual = batch_norm_conv(filters=filters, kernel_size=(3, 3), act='relu')(conv1)
        return shortcut(input, residual)

    return f


def bottleneck(filters, init_strides=(1, 1), is_first_block_of_first_layer=False):
    def f(input):

        if is_first_block_of_first_layer:
            conv_1_1 = Conv2D(filters=filters, kernel_size=(1, 1),
                              strides=init_strides,
                              padding='same',
                              kernel_initializer='he_normal',
                              kernel_regularizer=l2(1e-4))(input)
        else:
            conv_1_1 = batch_norm_conv(filters=filters, kernel_size=(1, 1),
                                       strides=init_strides, act='relu')(input)

        conv_3_3 = batch_norm_conv(filters=filters, kernel_size=(3, 3), act='relu')(conv_1_1)
        residual = batch_norm_conv(filters=filters * 4, kernel_size=(1, 1), act='relu')(conv_3_3)
        return shortcut(input, residual)

    return f


def RESNET_NN(input_shape, num_outputs, block_fn, repetitions):
    kernel_initializer = 'he_normal'
    kernel_regularizer = l2(1.e-4)
    padding = 'same'

    input = Input(shape=input_shape)
    conv1 = conv_batch_norm(filters=64, kernel_size=(7, 7), strides=(2, 2), act='relu',
                            kernel_initializer=kernel_initializer, kernel_regularizer=kernel_regularizer,
                            padding=padding)(input)

    pool1 = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='same')(conv1)

    block = pool1
    filters = 64
    for i, r in enumerate(repetitions):
        block = residual_block(block_fn, filters=filters, repetitions=r, is_first_layer=(i == 0))(block)
        filters *= 2

    # Last activation
    block = batch_norm(block, act='relu')

    # Classifier block
    block_shape = K.int_shape(block)
    pool2 = AveragePooling2D(pool_size=(block_shape[1], block_shape[2]),
                             strides=(1, 1))(block)
    flatten1 = Flatten()(pool2)
    dense = Dense(units=num_outputs, kernel_initializer='he_normal',
                  activation='softmax')(flatten1)

    model = Model(inputs=input, outputs=dense)

    model.compile(loss='categorical_crossentropy',
                  optimizer='adam',
                  metrics=['acc'])

    model.summary()
    return model


model = RESNET_NN(input_shape=(13, 49, 1), num_outputs=9, block_fn=basic_block, repetitions=[2, 2, 2, 2])

0 个答案:

没有答案