当前位置:首页 > 分类133 > 正文

keras

摘要: keraskeras1.Compile在训练模型之前,我们需要通过compile来对学习过程进行配置。c ompile接收三个参...
keras

keras

1. Compile在训练模型之前,我们需要通过compile来对学习过程进行配置。compile接收三个参数:
优化器optimizer该参数可指定为已预定义的优化器名,如rmsprop、adagrad,或一个Optimizer类的对象,详情见optimizers
损失函数loss该参数为模型试图最小化的目标函数,它可为预定义的损失函数名,如categorical_crossentropy、mse,也可以为一个损失函数。详情见losses
指标列表metrics对分类问题,我们一般将该列表设置为metrics=['accuracy']。指标可以是一个预定义指标的名字,也可以是一个用户定制的函数.指标函数应该返回单个张量,或一个完成metric_name - > metric_value映射的字典.请参考性能评估.
# 多分类问题model.compile(optimizer='rmsprop',              loss='categorical_crossentropy',              metrics=['accuracy'])# 二分类问题model.compile(optimizer='rmsprop',              loss='binary_crossentropy',              metrics=['accuracy'])# 回归问题model.compile(optimizer='rmsprop',              loss='mse')# 自定义metricsimport keras.backend as Kdef mean_pred(y_true, y_pred):    return K.mean(y_pred)model.compile(optimizer='rmsprop',              loss='binary_crossentropy',              metrics=['accuracy', mean_pred])
2. Fit
# 构建与编译模型model = Sequential()model.add(Dense(32, activation='relu', input_dim=100))model.add(Dense(1, activation='sigmoid'))model.compile(optimizer='rmsprop',              loss='binary_crossentropy',              metrics=['accuracy'])# 查出数据import numpy as npdata = np.random.random((1000, 100))labels = np.random.randint(2, size=(1000, 1))# 训练与数据拟合model.fit(data, labels, epochs=10, batch_size=32)
def generate_arrays_from_file(path):    while 1:        f = open(path)        for line in f:            x, y = process_line(line)            img = load_images(x)            yield (img, y)        f.close()model.fit_generator(generate_arrays_from_file('/my_file.txt'),        samples_per_epoch=10000, nb_epoch=10)
3. Case
from keras.models import Sequentialfrom keras.layers import Dense, Dropout, Activationfrom keras.optimizers import SGD# Generate dummy dataimport numpy as npx_train = np.random.random((1000, 20))y_train = keras.utils.to_categorical(np.random.randint(10, size=(1000, 1)), num_classes=10)x_test = np.random.random((100, 20))y_test = keras.utils.to_categorical(np.random.randint(10, size=(100, 1)), num_classes=10)model = Sequential()model.add(Dense(64, activation='relu', input_dim=20))model.add(Dropout(0.5))model.add(Dense(64, activation='relu'))model.add(Dropout(0.5))model.add(Dense(10, activation='softmax'))sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)model.compile(loss='categorical_crossentropy',              optimizer=sgd,              metrics=['accuracy'])model.fit(x_train, y_train,          epochs=20,          batch_size=128)score = model.evaluate(x_test, y_test, batch_size=128)
import numpy as npimport kerasfrom keras.models import Sequentialfrom keras.layers import Dense, Dropout, Flattenfrom keras.layers import Conv2D, MaxPooling2Dfrom keras.optimizers import SGDx_train = np.random.random((100, 100, 100, 3))y_train = keras.utils.to_categorical(np.random.randint(10, size=(100, 1)), num_classes=10)x_test = np.random.random((20, 100, 100, 3))y_test = keras.utils.to_categorical(np.random.randint(10, size=(20, 1)), num_classes=10)model = Sequential()# input: 100x100 images with 3 channels -> (100, 100, 3) tensors.# this applies 32 convolution filters of size 3x3 each.model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(100, 100, 3)))model.add(Conv2D(32, (3, 3), activation='relu'))model.add(MaxPooling2D(pool_size=(2, 2)))model.add(Dropout(0.25))model.add(Conv2D(64, (3, 3), activation='relu'))model.add(Conv2D(64, (3, 3), activation='relu'))model.add(MaxPooling2D(pool_size=(2, 2)))model.add(Dropout(0.25))model.add(Flatten())model.add(Dense(256, activation='relu'))model.add(Dropout(0.5))model.add(Dense(10, activation='softmax'))sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)model.compile(loss='categorical_crossentropy', optimizer=sgd)model.fit(x_train, y_train, batch_size=32, epochs=10)score = model.evaluate(x_test, y_test, batch_size=32)
from keras.models import Sequentialfrom keras.layers import Dense, Dropoutfrom keras.layers import Embeddingfrom keras.layers import LSTMmodel = Sequential()model.add(Embedding(max_features, output_dim=256))model.add(LSTM(128))model.add(Dropout(0.5))model.add(Dense(1, activation='sigmoid'))model.compile(loss='binary_crossentropy',              optimizer='rmsprop',              metrics=['accuracy'])model.fit(x_train, y_train, batch_size=16, epochs=10)score = model.evaluate(x_test, y_test, batch_size=16)
4. functionalKeras函数式模型接口是用户定义多输出模型、非循环有向模型或具有共享层的模型等复杂模型的途径。一句话,只要你的模型不是类似VGG一样一条路走到黑的模型,或者你的模型需要多于一个的输出,那么你总应该选择函数式模型。函数式模型是最广泛的一类模型,序贯模型(Sequential)只是它的一种特殊情况。因为序贯模型是函数式模型的一个特例,所以我们从一个简单的序贯模型开始,看看函数式模型是如何完成的。
from keras.layers import Input, Densefrom keras.models import Modelinputs = Input(shape=(784,))x = Dense(64, activation='relu')(inputs)x = Dense(64, activation='relu')(x)predictions = Dense(10, activation='softmax')(x)model = Model(inputs=inputs, outputs=predictions)model.compile(optimizer='rmsprop',              loss='categorical_crossentropy',              metrics=['accuracy'])model.fit(data, labels)
使用函数式模型的一个典型场景是搭建多输入、多输出的模型。考虑这样一个模型。
from keras.layers import Input, Embedding, LSTM, Densefrom keras.models import Model# Headline input: meant to receive sequences of 100 integers, between 1 and 10000.# Note that we can name any layer by passing it a "name" argument.main_input = Input(shape=(100,), dtype='int32', name='main_input')# This embedding layer will encode the input sequence# into a sequence of dense 512-dimensional vectors.x = Embedding(output_dim=512, input_dim=10000, input_length=100)(main_input)# A LSTM will transform the vector sequence into a single vector,# containing information about the entire sequencelstm_out = LSTM(32)(x)auxiliary_output = Dense(1, activation='sigmoid', name='aux_output')(lstm_out)auxiliary_input = Input(shape=(5,), name='aux_input')x = keras.layers.concatenate([lstm_out, auxiliary_input])# We stack a deep densely-connected network on topx = Dense(64, activation='relu')(x)x = Dense(64, activation='relu')(x)x = Dense(64, activation='relu')(x)# And finally we add the main logistic regression layermain_output = Dense(1, activation='sigmoid', name='main_output')(x)model = Model(inputs=[main_input, auxiliary_input], outputs=[main_output, auxiliary_output])model.compile(optimizer='rmsprop', loss='binary_crossentropy',              loss_weights=[1., 0.2])model.fit([headline_data, additional_data], [labels, labels],          epochs=50, batch_size=32)model.compile(optimizer='rmsprop',              loss={'main_output': 'binary_crossentropy', 'aux_output': 'binary_crossentropy'},              loss_weights={'main_output': 1., 'aux_output': 0.2})# And trained it via:model.fit({'main_input': headline_data, 'aux_input': additional_data},          {'main_output': labels, 'aux_output': labels},          epochs=50, batch_size=32)
4.1 VGG
from keras.layers import Conv2D, MaxPooling2D, Inputinput_img = Input(shape=(3, 256, 256))tower_1 = Conv2D(64, (1, 1), padding='same', activation='relu')(input_img)tower_1 = Conv2D(64, (3, 3), padding='same', activation='relu')(tower_1)tower_2 = Conv2D(64, (1, 1), padding='same', activation='relu')(input_img)tower_2 = Conv2D(64, (5, 5), padding='same', activation='relu')(tower_2)tower_3 = MaxPooling2D((3, 3), strides=(1, 1), padding='same')(input_img)tower_3 = Conv2D(64, (1, 1), padding='same', activation='relu')(tower_3)output = keras.layers.concatenate([tower_1, tower_2, tower_3], axis=1)
4.2 ResNet
from keras.layers import Conv2D, Input# input tensor for a 3-channel 256x256 imagex = Input(shape=(3, 256, 256))# 3x3 conv with 3 output channels (same as input channels)y = Conv2D(3, (3, 3), padding='same')(x)# this returns x + y.z = keras.layers.add([x, y])
5. Net5.1 MLP
from keras.datasets imp'''Trains and evaluate a simple MLPon the Reuters newswire topic classification task.'''from __future__ import print_functionimport numpy as npimport kerasort reutersfrom keras.models import Sequentialfrom keras.layers import Dense, Dropout, Activationfrom keras.preprocessing.text import Tokenizermax_words = 1000batch_size = 32epochs = 5print('Loading data...')(x_train, y_train), (x_test, y_test) = reuters.load_data(num_words=max_words,                                                         test_split=0.2)print(len(x_train), 'train sequences')print(len(x_test), 'test sequences')num_classes = np.max(y_train) + 1print(num_classes, 'classes')print('Vectorizing sequence data...')tokenizer = Tokenizer(num_words=max_words)x_train = tokenizer.sequences_to_matrix(x_train, mode='binary')x_test = tokenizer.sequences_to_matrix(x_test, mode='binary')print('x_train shape:', x_train.shape)print('x_test shape:', x_test.shape)print('Convert class vector to binary class matrix '      '(for use with categorical_crossentropy)')y_train = keras.utils.to_categorical(y_train, num_classes)y_test = keras.utils.to_categorical(y_test, num_classes)print('y_train shape:', y_train.shape)print('y_test shape:', y_test.shape)print('Building model...')model = Sequential()model.add(Dense(512, input_shape=(max_words,)))model.add(Activation('relu'))model.add(Dropout(0.5))model.add(Dense(num_classes))model.add(Activation('softmax'))model.compile(loss='categorical_crossentropy',              optimizer='adam',              metrics=['accuracy'])history = model.fit(x_train, y_train,                    batch_size=batch_size,                    epochs=epochs,                    verbose=1,                    validation_split=0.1)score = model.evaluate(x_test, y_test,                       batch_size=batch_size, verbose=1)print('Test score:', score[0])print('Test accuracy:', score[1]
5.2 vgg16
# -*- coding: utf-8 -*-'''VGG16 model for Keras.# Reference:- [Very Deep Convolutional Networks for Large-Scale Image Recognition](https://arxiv.org/abs/1409.1556)'''from __future__ import print_functionimport numpy as npimport warningsfrom keras.models import Modelfrom keras.layers import Flattenfrom keras.layers import Densefrom keras.layers import Inputfrom keras.layers import Conv2Dfrom keras.layers import MaxPooling2Dfrom keras.layers import GlobalMaxPooling2Dfrom keras.layers import GlobalAveragePooling2Dfrom keras.preprocessing import imagefrom keras.utils import layer_utilsfrom keras.utils.data_utils import get_filefrom keras import backend as Kfrom keras.applications.imagenet_utils import decode_predictionsfrom keras.applications.imagenet_utils import preprocess_inputfrom keras.applications.imagenet_utils import _obtain_input_shapefrom keras.engine.topology import get_source_inputsWEIGHTS_PATH = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_tf_dim_ordering_tf_kernels.h5'WEIGHTS_PATH_NO_TOP = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5'def VGG16(include_top=True, weights='imagenet',          input_tensor=None, input_shape=None,          pooling=None,          classes=1000):    """Instantiates the VGG16 architecture.    Optionally loads weights pre-trained    on ImageNet. Note that when using TensorFlow,    for best performance you should set    `image_data_format="channels_last"` in your Keras config    at ~/.keras/keras.json.    The model and the weights are compatible with both    TensorFlow and Theano. The data format    convention used by the model is the one    specified in your Keras config file.    # Arguments        include_top: whether to include the 3 fully-connected            layers at the top of the network.        weights: one of `None` (random initialization)            or "imagenet" (pre-training on ImageNet).        input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)            to use as image input for the model.        input_shape: optional shape tuple, only to be specified            if `include_top` is False (otherwise the input shape            has to be `(224, 224, 3)` (with `channels_last` data format)            or `(3, 224, 244)` (with `channels_first` data format).            It should have exactly 3 inputs channels,            and width and height should be no smaller than 48.            E.g. `(200, 200, 3)` would be one valid value.        pooling: Optional pooling mode for feature extraction            when `include_top` is `False`.            - `None` means that the output of the model will be                the 4D tensor output of the                last convolutional layer.            - `avg` means that global average pooling                will be applied to the output of the                last convolutional layer, and thus                the output of the model will be a 2D tensor.            - `max` means that global max pooling will                be applied.        classes: optional number of classes to classify images            into, only to be specified if `include_top` is True, and            if no `weights` argument is specified.    # Returns        A Keras model instance.    # Raises        ValueError: in case of invalid argument for `weights`,            or invalid input shape.    """    if weights not in {'imagenet', None}:        raise ValueError('The `weights` argument should be either '                         '`None` (random initialization) or `imagenet` '                         '(pre-training on ImageNet).')    if weights == 'imagenet' and include_top and classes != 1000:        raise ValueError('If using `weights` as imagenet with `include_top`'                         ' as true, `classes` should be 1000')    # Determine proper input shape    input_shape = _obtain_input_shape(input_shape,                                      default_size=224,                                      min_size=48,                                      data_format=K.image_data_format(),                                      include_top=include_top)    if input_tensor is None:        img_input = Input(shape=input_shape)    else:        if not K.is_keras_tensor(input_tensor):            img_input = Input(tensor=input_tensor, shape=input_shape)        else:            img_input = input_tensor    # Block 1    x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1')(img_input)    x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2')(x)    x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x)    # Block 2    x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1')(x)    x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv2')(x)    x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x)    # Block 3    x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1')(x)    x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv2')(x)    x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv3')(x)    x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x)    # Block 4    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1')(x)    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv2')(x)    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv3')(x)    x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x)    # Block 5    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1')(x)    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv2')(x)    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv3')(x)    x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x)    if include_top:        # Classification block        x = Flatten(name='flatten')(x)        x = Dense(4096, activation='relu', name='fc1')(x)        x = Dense(4096, activation='relu', name='fc2')(x)        x = Dense(classes, activation='softmax', name='predictions')(x)    else:        if pooling == 'avg':            x = GlobalAveragePooling2D()(x)        elif pooling == 'max':            x = GlobalMaxPooling2D()(x)    # Ensure that the model takes into account    # any potential predecessors of `input_tensor`.    if input_tensor is not None:        inputs = get_source_inputs(input_tensor)    else:        inputs = img_input    # Create model.    model = Model(inputs, x, name='vgg16')    # load weights    if weights == 'imagenet':        if include_top:            weights_path = get_file('vgg16_weights_tf_dim_ordering_tf_kernels.h5',                                    WEIGHTS_PATH,                                    cache_subdir='models')        else:            weights_path = get_file('vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5',                                    WEIGHTS_PATH_NO_TOP,                                    cache_subdir='models')        model.load_weights(weights_path)        if K.backend() == 'theano':            layer_utils.convert_all_kernels_in_model(model)        if K.image_data_format() == 'channels_first':            if include_top:                maxpool = model.get_layer(name='block5_pool')                shape = maxpool.output_shape[1:]                dense = model.get_layer(name='fc1')                layer_utils.convert_dense_weights_data_format(dense, shape, 'channels_first')            if K.backend() == 'tensorflow':                warnings.warn('You are using the TensorFlow backend, yet you '                              'are using the Theano '                              'image data format convention '                              '(`image_data_format="channels_first"`). '                              'For best performance, set '                              '`image_data_format="channels_last"` in '                              'your Keras config '                              'at ~/.keras/keras.json.')    return modelif __name__ == '__main__':    model = VGG16(include_top=True, weights='imagenet')    img_path = 'elephant.jpg'    img = image.load_img(img_path, target_size=(224, 224))    x = image.img_to_array(img)    x = np.expand_dims(x, axis=0)    x = preprocess_input(x)    print('Input image shape:', x.shape)    preds = model.predict(x)    print('Predicted:', decode_predictions(preds))
5.3 inception v3
# -*- coding: utf-8 -*-"""Inception V3 model for Keras.Note that the input image format for this model is different than forthe VGG16 and ResNet models (299x299 instead of 224x224),and that the input preprocessing function is also different (same as Xception).# Reference- [Rethinking the Inception Architecture for Computer Vision](http://arxiv.org/abs/1512.00567)"""from __future__ import print_functionfrom __future__ import absolute_importimport warningsimport numpy as npfrom keras.models import Modelfrom keras import layersfrom keras.layers import Activationfrom keras.layers import Densefrom keras.layers import Inputfrom keras.layers import BatchNormalizationfrom keras.layers import Conv2Dfrom keras.layers import MaxPooling2Dfrom keras.layers import AveragePooling2Dfrom keras.layers import GlobalAveragePooling2Dfrom keras.layers import GlobalMaxPooling2Dfrom keras.engine.topology import get_source_inputsfrom keras.utils.layer_utils import convert_all_kernels_in_modelfrom keras.utils.data_utils import get_filefrom keras import backend as Kfrom keras.applications.imagenet_utils import decode_predictionsfrom keras.applications.imagenet_utils import _obtain_input_shapefrom keras.preprocessing import imageWEIGHTS_PATH = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.5/inception_v3_weights_tf_dim_ordering_tf_kernels.h5'WEIGHTS_PATH_NO_TOP = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.5/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5'def conv2d_bn(x,              filters,              num_row,              num_col,              padding='same',              strides=(1, 1),              name=None):    """Utility function to apply conv + BN.    Arguments:        x: input tensor.        filters: filters in `Conv2D`.        num_row: height of the convolution kernel.        num_col: width of the convolution kernel.        padding: padding mode in `Conv2D`.        strides: strides in `Conv2D`.        name: name of the ops; will become `name + '_conv'`            for the convolution and `name + '_bn'` for the            batch norm layer.    Returns:        Output tensor after applying `Conv2D` and `BatchNormalization`.    """    if name is not None:        bn_name = name + '_bn'        conv_name = name + '_conv'    else:        bn_name = None        conv_name = None    if K.image_data_format() == 'channels_first':        bn_axis = 1    else:        bn_axis = 3    x = Conv2D(        filters, (num_row, num_col),        strides=strides,        padding=padding,        use_bias=False,        name=conv_name)(x)    x = BatchNormalization(axis=bn_axis, scale=False, name=bn_name)(x)    x = Activation('relu', name=name)(x)    return xdef InceptionV3(include_top=True,                weights='imagenet',                input_tensor=None,                input_shape=None,                pooling=None,                classes=1000):    """Instantiates the Inception v3 architecture.    Optionally loads weights pre-trained    on ImageNet. Note that when using TensorFlow,    for best performance you should set    `image_data_format="channels_last"` in your Keras config    at ~/.keras/keras.json.    The model and the weights are compatible with both    TensorFlow and Theano. The data format    convention used by the model is the one    specified in your Keras config file.    Note that the default input image size for this model is 299x299.    Arguments:        include_top: whether to include the fully-connected            layer at the top of the network.        weights: one of `None` (random initialization)            or "imagenet" (pre-training on ImageNet).        input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)            to use as image input for the model.        input_shape: optional shape tuple, only to be specified            if `include_top` is False (otherwise the input shape            has to be `(299, 299, 3)` (with `channels_last` data format)            or `(3, 299, 299)` (with `channels_first` data format).            It should have exactly 3 inputs channels,            and width and height should be no smaller than 139.            E.g. `(150, 150, 3)` would be one valid value.        pooling: Optional pooling mode for feature extraction            when `include_top` is `False`.            - `None` means that the output of the model will be                the 4D tensor output of the                last convolutional layer.            - `avg` means that global average pooling                will be applied to the output of the                last convolutional layer, and thus                the output of the model will be a 2D tensor.            - `max` means that global max pooling will                be applied.        classes: optional number of classes to classify images            into, only to be specified if `include_top` is True, and            if no `weights` argument is specified.    Returns:        A Keras model instance.    Raises:        ValueError: in case of invalid argument for `weights`,            or invalid input shape.    """    if weights not in {'imagenet', None}:        raise ValueError('The `weights` argument should be either '                         '`None` (random initialization) or `imagenet` '                         '(pre-training on ImageNet).')    if weights == 'imagenet' and include_top and classes != 1000:        raise ValueError('If using `weights` as imagenet with `include_top`'                         ' as true, `classes` should be 1000')    # Determine proper input shape    input_shape = _obtain_input_shape(        input_shape,        default_size=299,        min_size=139,        data_format=K.image_data_format(),        include_top=include_top)    if input_tensor is None:        img_input = Input(shape=input_shape)    else:        img_input = Input(tensor=input_tensor, shape=input_shape)    if K.image_data_format() == 'channels_first':        channel_axis = 1    else:        channel_axis = 3    x = conv2d_bn(img_input, 32, 3, 3, strides=(2, 2), padding='valid')    x = conv2d_bn(x, 32, 3, 3, padding='valid')    x = conv2d_bn(x, 64, 3, 3)    x = MaxPooling2D((3, 3), strides=(2, 2))(x)    x = conv2d_bn(x, 80, 1, 1, padding='valid')    x = conv2d_bn(x, 192, 3, 3, padding='valid')    x = MaxPooling2D((3, 3), strides=(2, 2))(x)    # mixed 0, 1, 2: 35 x 35 x 256    branch1x1 = conv2d_bn(x, 64, 1, 1)    branch5x5 = conv2d_bn(x, 48, 1, 1)    branch5x5 = conv2d_bn(branch5x5, 64, 5, 5)    branch3x3dbl = conv2d_bn(x, 64, 1, 1)    branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3)    branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3)    branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x)    branch_pool = conv2d_bn(branch_pool, 32, 1, 1)    x = layers.concatenate(        [branch1x1, branch5x5, branch3x3dbl, branch_pool],        axis=channel_axis,        name='mixed0')    # mixed 1: 35 x 35 x 256    branch1x1 = conv2d_bn(x, 64, 1, 1)    branch5x5 = conv2d_bn(x, 48, 1, 1)    branch5x5 = conv2d_bn(branch5x5, 64, 5, 5)    branch3x3dbl = conv2d_bn(x, 64, 1, 1)    branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3)    branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3)    branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x)    branch_pool = conv2d_bn(branch_pool, 64, 1, 1)    x = layers.concatenate(        [branch1x1, branch5x5, branch3x3dbl, branch_pool],        axis=channel_axis,        name='mixed1')    # mixed 2: 35 x 35 x 256    branch1x1 = conv2d_bn(x, 64, 1, 1)    branch5x5 = conv2d_bn(x, 48, 1, 1)    branch5x5 = conv2d_bn(branch5x5, 64, 5, 5)    branch3x3dbl = conv2d_bn(x, 64, 1, 1)    branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3)    branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3)    branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x)    branch_pool = conv2d_bn(branch_pool, 64, 1, 1)    x = layers.concatenate(        [branch1x1, branch5x5, branch3x3dbl, branch_pool],        axis=channel_axis,        name='mixed2')    # mixed 3: 17 x 17 x 768    branch3x3 = conv2d_bn(x, 384, 3, 3, strides=(2, 2), padding='valid')    branch3x3dbl = conv2d_bn(x, 64, 1, 1)    branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3)    branch3x3dbl = conv2d_bn(        branch3x3dbl, 96, 3, 3, strides=(2, 2), padding='valid')    branch_pool = MaxPooling2D((3, 3), strides=(2, 2))(x)    x = layers.concatenate(        [branch3x3, branch3x3dbl, branch_pool], axis=channel_axis, name='mixed3')    # mixed 4: 17 x 17 x 768    branch1x1 = conv2d_bn(x, 192, 1, 1)    branch7x7 = conv2d_bn(x, 128, 1, 1)    branch7x7 = conv2d_bn(branch7x7, 128, 1, 7)    branch7x7 = conv2d_bn(branch7x7, 192, 7, 1)    branch7x7dbl = conv2d_bn(x, 128, 1, 1)    branch7x7dbl = conv2d_bn(branch7x7dbl, 128, 7, 1)    branch7x7dbl = conv2d_bn(branch7x7dbl, 128, 1, 7)    branch7x7dbl = conv2d_bn(branch7x7dbl, 128, 7, 1)    branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7)    branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x)    branch_pool = conv2d_bn(branch_pool, 192, 1, 1)    x = layers.concatenate(        [branch1x1, branch7x7, branch7x7dbl, branch_pool],        axis=channel_axis,        name='mixed4')    # mixed 5, 6: 17 x 17 x 768    for i in range(2):        branch1x1 = conv2d_bn(x, 192, 1, 1)        branch7x7 = conv2d_bn(x, 160, 1, 1)        branch7x7 = conv2d_bn(branch7x7, 160, 1, 7)        branch7x7 = conv2d_bn(branch7x7, 192, 7, 1)        branch7x7dbl = conv2d_bn(x, 160, 1, 1)        branch7x7dbl = conv2d_bn(branch7x7dbl, 160, 7, 1)        branch7x7dbl = conv2d_bn(branch7x7dbl, 160, 1, 7)        branch7x7dbl = conv2d_bn(branch7x7dbl, 160, 7, 1)        branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7)        branch_pool = AveragePooling2D(            (3, 3), strides=(1, 1), padding='same')(x)        branch_pool = conv2d_bn(branch_pool, 192, 1, 1)        x = layers.concatenate(            [branch1x1, branch7x7, branch7x7dbl, branch_pool],            axis=channel_axis,            name='mixed' + str(5 + i))    # mixed 7: 17 x 17 x 768    branch1x1 = conv2d_bn(x, 192, 1, 1)    branch7x7 = conv2d_bn(x, 192, 1, 1)    branch7x7 = conv2d_bn(branch7x7, 192, 1, 7)    branch7x7 = conv2d_bn(branch7x7, 192, 7, 1)    branch7x7dbl = conv2d_bn(x, 192, 1, 1)    branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 7, 1)    branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7)    branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 7, 1)    branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7)    branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x)    branch_pool = conv2d_bn(branch_pool, 192, 1, 1)    x = layers.concatenate(        [branch1x1, branch7x7, branch7x7dbl, branch_pool],        axis=channel_axis,        name='mixed7')    # mixed 8: 8 x 8 x 1280    branch3x3 = conv2d_bn(x, 192, 1, 1)    branch3x3 = conv2d_bn(branch3x3, 320, 3, 3,                          strides=(2, 2), padding='valid')    branch7x7x3 = conv2d_bn(x, 192, 1, 1)    branch7x7x3 = conv2d_bn(branch7x7x3, 192, 1, 7)    branch7x7x3 = conv2d_bn(branch7x7x3, 192, 7, 1)    branch7x7x3 = conv2d_bn(        branch7x7x3, 192, 3, 3, strides=(2, 2), padding='valid')    branch_pool = MaxPooling2D((3, 3), strides=(2, 2))(x)    x = layers.concatenate(        [branch3x3, branch7x7x3, branch_pool], axis=channel_axis, name='mixed8')    # mixed 9: 8 x 8 x 2048    for i in range(2):        branch1x1 = conv2d_bn(x, 320, 1, 1)        branch3x3 = conv2d_bn(x, 384, 1, 1)        branch3x3_1 = conv2d_bn(branch3x3, 384, 1, 3)        branch3x3_2 = conv2d_bn(branch3x3, 384, 3, 1)        branch3x3 = layers.concatenate(            [branch3x3_1, branch3x3_2], axis=channel_axis, name='mixed9_' + str(i))        branch3x3dbl = conv2d_bn(x, 448, 1, 1)        branch3x3dbl = conv2d_bn(branch3x3dbl, 384, 3, 3)        branch3x3dbl_1 = conv2d_bn(branch3x3dbl, 384, 1, 3)        branch3x3dbl_2 = conv2d_bn(branch3x3dbl, 384, 3, 1)        branch3x3dbl = layers.concatenate(            [branch3x3dbl_1, branch3x3dbl_2], axis=channel_axis)        branch_pool = AveragePooling2D(            (3, 3), strides=(1, 1), padding='same')(x)        branch_pool = conv2d_bn(branch_pool, 192, 1, 1)        x = layers.concatenate(            [branch1x1, branch3x3, branch3x3dbl, branch_pool],            axis=channel_axis,            name='mixed' + str(9 + i))    if include_top:        # Classification block        x = GlobalAveragePooling2D(name='avg_pool')(x)        x = Dense(classes, activation='softmax', name='predictions')(x)    else:        if pooling == 'avg':            x = GlobalAveragePooling2D()(x)        elif pooling == 'max':            x = GlobalMaxPooling2D()(x)    # Ensure that the model takes into account    # any potential predecessors of `input_tensor`.    if input_tensor is not None:        inputs = get_source_inputs(input_tensor)    else:        inputs = img_input    # Create model.    model = Model(inputs, x, name='inception_v3')    # load weights    if weights == 'imagenet':        if K.image_data_format() == 'channels_first':            if K.backend() == 'tensorflow':                warnings.warn('You are using the TensorFlow backend, yet you '                              'are using the Theano '                              'image data format convention '                              '(`image_data_format="channels_first"`). '                              'For best performance, set '                              '`image_data_format="channels_last"` in '                              'your Keras config '                              'at ~/.keras/keras.json.')        if include_top:            weights_path = get_file(                'inception_v3_weights_tf_dim_ordering_tf_kernels.h5',                WEIGHTS_PATH,                cache_subdir='models',                md5_hash='9a0d58056eeedaa3f26cb7ebd46da564')        else:            weights_path = get_file(                'inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5',                WEIGHTS_PATH_NO_TOP,                cache_subdir='models',                md5_hash='bcbd6486424b2319ff4ef7d526e38f63')        model.load_weights(weights_path)        if K.backend() == 'theano':            convert_all_kernels_in_model(model)    return modeldef preprocess_input(x):    x /= 255.    x -= 0.5    x *= 2.    return xif __name__ == '__main__':    model = InceptionV3(include_top=True, weights='imagenet')    img_path = 'elephant.jpg'    img = image.load_img(img_path, target_size=(299, 299))    x = image.img_to_array(img)    x = np.expand_dims(x, axis=0)    x = preprocess_input(x)    preds = model.predict(x)    print('Predicted:', decode_predictions(preds))
5.4 ResNet-50
# -*- coding: utf-8 -*-'''ResNet50 model for Keras.# Reference:- [Deep Residual Learning for Image Recognition](https://arxiv.org/abs/1512.03385)Adapted from code contributed by BigMoyan.'''from __future__ import print_functionimport numpy as npimport warningsfrom keras.layers import Inputfrom keras import layersfrom keras.layers import Densefrom keras.layers import Activationfrom keras.layers import Flattenfrom keras.layers import Conv2Dfrom keras.layers import MaxPooling2Dfrom keras.layers import GlobalMaxPooling2Dfrom keras.layers import ZeroPadding2Dfrom keras.layers import AveragePooling2Dfrom keras.layers import GlobalAveragePooling2Dfrom keras.layers import BatchNormalizationfrom keras.models import Modelfrom keras.preprocessing import imageimport keras.backend as Kfrom keras.utils import layer_utilsfrom keras.utils.data_utils import get_filefrom keras.applications.imagenet_utils import decode_predictionsfrom keras.applications.imagenet_utils import preprocess_inputfrom keras.applications.imagenet_utils import _obtain_input_shapefrom keras.engine.topology import get_source_inputsWEIGHTS_PATH = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.2/resnet50_weights_tf_dim_ordering_tf_kernels.h5'WEIGHTS_PATH_NO_TOP = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.2/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'def identity_block(input_tensor, kernel_size, filters, stage, block):    """The identity block is the block that has no conv layer at shortcut.    # Arguments        input_tensor: input tensor        kernel_size: defualt 3, the kernel size of middle conv layer at main path        filters: list of integers, the filterss of 3 conv layer at main path        stage: integer, current stage label, used for generating layer names        block: 'a','b'..., current block label, used for generating layer names    # Returns        Output tensor for the block.    """    filters1, filters2, filters3 = filters    if K.image_data_format() == 'channels_last':        bn_axis = 3    else:        bn_axis = 1    conv_name_base = 'res' + str(stage) + block + '_branch'    bn_name_base = 'bn' + str(stage) + block + '_branch'    x = Conv2D(filters1, (1, 1), name=conv_name_base + '2a')(input_tensor)    x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2a')(x)    x = Activation('relu')(x)    x = Conv2D(filters2, kernel_size,               padding='same', name=conv_name_base + '2b')(x)    x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2b')(x)    x = Activation('relu')(x)    x = Conv2D(filters3, (1, 1), name=conv_name_base + '2c')(x)    x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2c')(x)    x = layers.add([x, input_tensor])    x = Activation('relu')(x)    return xdef conv_block(input_tensor, kernel_size, filters, stage, block, strides=(2, 2)):    """conv_block is the block that has a conv layer at shortcut    # Arguments        input_tensor: input tensor        kernel_size: defualt 3, the kernel size of middle conv layer at main path        filters: list of integers, the filterss of 3 conv layer at main path        stage: integer, current stage label, used for generating layer names        block: 'a','b'..., current block label, used for generating layer names    # Returns        Output tensor for the block.    Note that from stage 3, the first conv layer at main path is with strides=(2,2)    And the shortcut should have strides=(2,2) as well    """    filters1, filters2, filters3 = filters    if K.image_data_format() == 'channels_last':        bn_axis = 3    else:        bn_axis = 1    conv_name_base = 'res' + str(stage) + block + '_branch'    bn_name_base = 'bn' + str(stage) + block + '_branch'    x = Conv2D(filters1, (1, 1), strides=strides,               name=conv_name_base + '2a')(input_tensor)    x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2a')(x)    x = Activation('relu')(x)    x = Conv2D(filters2, kernel_size, padding='same',               name=conv_name_base + '2b')(x)    x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2b')(x)    x = Activation('relu')(x)    x = Conv2D(filters3, (1, 1), name=conv_name_base + '2c')(x)    x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2c')(x)    shortcut = Conv2D(filters3, (1, 1), strides=strides,                      name=conv_name_base + '1')(input_tensor)    shortcut = BatchNormalization(axis=bn_axis, name=bn_name_base + '1')(shortcut)    x = layers.add([x, shortcut])    x = Activation('relu')(x)    return xdef ResNet50(include_top=True, weights='imagenet',             input_tensor=None, input_shape=None,             pooling=None,             classes=1000):    """Instantiates the ResNet50 architecture.    Optionally loads weights pre-trained    on ImageNet. Note that when using TensorFlow,    for best performance you should set    `image_data_format="channels_last"` in your Keras config    at ~/.keras/keras.json.    The model and the weights are compatible with both    TensorFlow and Theano. The data format    convention used by the model is the one    specified in your Keras config file.    # Arguments        include_top: whether to include the fully-connected            layer at the top of the network.        weights: one of `None` (random initialization)            or "imagenet" (pre-training on ImageNet).        input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)            to use as image input for the model.        input_shape: optional shape tuple, only to be specified            if `include_top` is False (otherwise the input shape            has to be `(224, 224, 3)` (with `channels_last` data format)            or `(3, 224, 244)` (with `channels_first` data format).            It should have exactly 3 inputs channels,            and width and height should be no smaller than 197.            E.g. `(200, 200, 3)` would be one valid value.        pooling: Optional pooling mode for feature extraction            when `include_top` is `False`.            - `None` means that the output of the model will be                the 4D tensor output of the                last convolutional layer.            - `avg` means that global average pooling                will be applied to the output of the                last convolutional layer, and thus                the output of the model will be a 2D tensor.            - `max` means that global max pooling will                be applied.        classes: optional number of classes to classify images            into, only to be specified if `include_top` is True, and            if no `weights` argument is specified.    # Returns        A Keras model instance.    # Raises        ValueError: in case of invalid argument for `weights`,            or invalid input shape.    """    if weights not in {'imagenet', None}:        raise ValueError('The `weights` argument should be either '                         '`None` (random initialization) or `imagenet` '                         '(pre-training on ImageNet).')    if weights == 'imagenet' and include_top and classes != 1000:        raise ValueError('If using `weights` as imagenet with `include_top`'                         ' as true, `classes` should be 1000')    # Determine proper input shape    input_shape = _obtain_input_shape(input_shape,                                      default_size=224,                                      min_size=197,                                      data_format=K.image_data_format(),                                      include_top=include_top)    if input_tensor is None:        img_input = Input(shape=input_shape)    else:        if not K.is_keras_tensor(input_tensor):            img_input = Input(tensor=input_tensor, shape=input_shape)        else:            img_input = input_tensor    if K.image_data_format() == 'channels_last':        bn_axis = 3    else:        bn_axis = 1    x = ZeroPadding2D((3, 3))(img_input)    x = Conv2D(64, (7, 7), strides=(2, 2), name='conv1')(x)    x = BatchNormalization(axis=bn_axis, name='bn_conv1')(x)    x = Activation('relu')(x)    x = MaxPooling2D((3, 3), strides=(2, 2))(x)    x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1))    x = identity_block(x, 3, [64, 64, 256], stage=2, block='b')    x = identity_block(x, 3, [64, 64, 256], stage=2, block='c')    x = conv_block(x, 3, [128, 128, 512], stage=3, block='a')    x = identity_block(x, 3, [128, 128, 512], stage=3, block='b')    x = identity_block(x, 3, [128, 128, 512], stage=3, block='c')    x = identity_block(x, 3, [128, 128, 512], stage=3, block='d')    x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a')    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b')    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c')    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d')    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e')    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f')    x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a')    x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b')    x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c')    x = AveragePooling2D((7, 7), name='avg_pool')(x)    if include_top:        x = Flatten()(x)        x = Dense(classes, activation='softmax', name='fc1000')(x)    else:        if pooling == 'avg':            x = GlobalAveragePooling2D()(x)        elif pooling == 'max':            x = GlobalMaxPooling2D()(x)    # Ensure that the model takes into account    # any potential predecessors of `input_tensor`.    if input_tensor is not None:        inputs = get_source_inputs(input_tensor)    else:        inputs = img_input    # Create model.    model = Model(inputs, x, name='resnet50')    # load weights    if weights == 'imagenet':        if include_top:            weights_path = get_file('resnet50_weights_tf_dim_ordering_tf_kernels.h5',                                    WEIGHTS_PATH,                                    cache_subdir='models',                                    md5_hash='a7b3fe01876f51b976af0dea6bc144eb')        else:            weights_path = get_file('resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5',                                    WEIGHTS_PATH_NO_TOP,                                    cache_subdir='models',                                    md5_hash='a268eb855778b3df3c7506639542a6af')        model.load_weights(weights_path)        if K.backend() == 'theano':            layer_utils.convert_all_kernels_in_model(model)        if K.image_data_format() == 'channels_first':            if include_top:                maxpool = model.get_layer(name='avg_pool')                shape = maxpool.output_shape[1:]                dense = model.get_layer(name='fc1000')                layer_utils.convert_dense_weights_data_format(dense, shape, 'channels_first')            if K.backend() == 'tensorflow':                warnings.warn('You are using the TensorFlow backend, yet you '                              'are using the Theano '                              'image data format convention '                              '(`image_data_format="channels_first"`). '                              'For best performance, set '                              '`image_data_format="channels_last"` in '                              'your Keras config '                              'at ~/.keras/keras.json.')    return modelif __name__ == '__main__':    model = ResNet50(include_top=True, weights='imagenet')    img_path = 'elephant.jpg'    img = image.load_img(img_path, target_size=(224, 224))    x = image.img_to_array(img)    x = np.expand_dims(x, axis=0)    x = preprocess_input(x)    print('Input image shape:', x.shape)    preds = model.predict(x)    print('Predicted:', decode_predictions(preds))
5.5 LSTM
'''Example script to generate text from Nietzsche's writings.At least 20 epochs are required before the generated textstarts sounding coherent.It is recommended to run this script on GPU, as recurrentnetworks are quite computationally intensive.If you try this script on new data, make sure your corpushas at least ~100k characters. ~1M is better.'''from __future__ import print_functionfrom keras.models import Sequentialfrom keras.layers import Dense, Activationfrom keras.layers import LSTMfrom keras.optimizers import RMSpropfrom keras.utils.data_utils import get_fileimport numpy as npimport randomimport syspath = get_file('nietzsche.txt', origin='https://s3.amazonaws.com/text-datasets/nietzsche.txt')text = open(path).read().lower()print('corpus length:', len(text))chars = sorted(list(set(text)))print('total chars:', len(chars))char_indices = dict((c, i) for i, c in enumerate(chars))indices_char = dict((i, c) for i, c in enumerate(chars))# cut the text in semi-redundant sequences of maxlen charactersmaxlen = 40step = 3sentences = []next_chars = []for i in range(0, len(text) - maxlen, step):    sentences.append(text[i: i + maxlen])    next_chars.append(text[i + maxlen])print('nb sequences:', len(sentences))print('Vectorization...')X = np.zeros((len(sentences), maxlen, len(chars)), dtype=np.bool)y = np.zeros((len(sentences), len(chars)), dtype=np.bool)for i, sentence in enumerate(sentences):    for t, char in enumerate(sentence):        X[i, t, char_indices[char]] = 1    y[i, char_indices[next_chars[i]]] = 1# build the model: a single LSTMprint('Build model...')model = Sequential()model.add(LSTM(128, input_shape=(maxlen, len(chars))))model.add(Dense(len(chars)))model.add(Activation('softmax'))optimizer = RMSprop(lr=0.01)model.compile(loss='categorical_crossentropy', optimizer=optimizer)def sample(preds, temperature=1.0):    # helper function to sample an index from a probability array    preds = np.asarray(preds).astype('float64')    preds = np.log(preds) / temperature    exp_preds = np.exp(preds)    preds = exp_preds / np.sum(exp_preds)    probas = np.random.multinomial(1, preds, 1)    return np.argmax(probas)# train the model, output generated text after each iterationfor iteration in range(1, 60):    print()    print('-' * 50)    print('Iteration', iteration)    model.fit(X, y,              batch_size=128,              epochs=1)    start_index = random.randint(0, len(text) - maxlen - 1)    for diversity in [0.2, 0.5, 1.0, 1.2]:        print()        print('----- diversity:', diversity)        generated = ''        sentence = text[start_index: start_index + maxlen]        generated += sentence        print('----- Generating with seed: "' + sentence + '"')        sys.stdout.write(generated)        for i in range(400):            x = np.zeros((1, maxlen, len(chars)))            for t, char in enumerate(sentence):                x[0, t, char_indices[char]] = 1.            preds = model.predict(x, verbose=0)[0]            next_index = sample(preds, diversity)            next_char = indices_char[next_index]            generated += next_char            sentence = sentence[1:] + next_char            sys.stdout.write(next_char)            sys.stdout.flush()        print()
5.6 GAN
'''DCGAN on MNIST using KerasDependencies: tensorflow 1.0 and keras 2.0Usage: python dcgan_mnist.py'''import numpy as npimport timefrom tensorflow.examples.tutorials.mnist import input_datafrom keras.models import Sequentialfrom keras.layers import Dense, Activation, Flatten, Reshapefrom keras.layers import Conv2D, Conv2DTranspose, UpSampling2Dfrom keras.layers import LeakyReLU, Dropoutfrom keras.layers import BatchNormalizationfrom keras.optimizers import Adam, RMSpropimport matplotlib.pyplot as pltclass ElapsedTimer(object):    def __init__(self):        self.start_time = time.time()    def elapsed(self,sec):        if sec < 60:            return str(sec) + " sec"        elif sec < (60 * 60):            return str(sec / 60) + " min"        else:            return str(sec / (60 * 60)) + " hr"    def elapsed_time(self):        print("Elapsed: %s " % self.elapsed(time.time() - self.start_time) )class DCGAN(object):    def __init__(self, img_rows=28, img_cols=28, channel=1):        self.img_rows = img_rows        self.img_cols = img_cols        self.channel = channel        self.D = None   # discriminator        self.G = None   # generator        self.AM = None  # adversarial model        self.DM = None  # discriminator model    # (W?F+2P)/S+1    def discriminator(self):        if self.D:            return self.D        self.D = Sequential()        depth = 64        dropout = 0.4        # In: 28 x 28 x 1, depth = 1        # Out: 14 x 14 x 1, depth=64        input_shape = (self.img_rows, self.img_cols, self.channel)        self.D.add(Conv2D(depth*1, 5, strides=2, input_shape=input_shape,\            padding='same'))        self.D.add(LeakyReLU(alpha=0.2))        self.D.add(Dropout(dropout))        self.D.add(Conv2D(depth*2, 5, strides=2, padding='same'))        self.D.add(LeakyReLU(alpha=0.2))        self.D.add(Dropout(dropout))        self.D.add(Conv2D(depth*4, 5, strides=2, padding='same'))        self.D.add(LeakyReLU(alpha=0.2))        self.D.add(Dropout(dropout))        self.D.add(Conv2D(depth*8, 5, strides=1, padding='same'))        self.D.add(LeakyReLU(alpha=0.2))        self.D.add(Dropout(dropout))        # Out: 1-dim probability        self.D.add(Flatten())        self.D.add(Dense(1))        self.D.add(Activation('sigmoid'))        self.D.summary()        return self.D    def generator(self):        if self.G:            return self.G        self.G = Sequential()        dropout = 0.4        depth = 64+64+64+64        dim = 7        # In: 100        # Out: dim x dim x depth        self.G.add(Dense(dim*dim*depth, input_dim=100))        self.G.add(BatchNormalization(momentum=0.9))        self.G.add(Activation('relu'))        self.G.add(Reshape((dim, dim, depth)))        self.G.add(Dropout(dropout))        # In: dim x dim x depth        # Out: 2*dim x 2*dim x depth/2        self.G.add(UpSampling2D())        self.G.add(Conv2DTranspose(int(depth/2), 5, padding='same'))        self.G.add(BatchNormalization(momentum=0.9))        self.G.add(Activation('relu'))        self.G.add(UpSampling2D())        self.G.add(Conv2DTranspose(int(depth/4), 5, padding='same'))        self.G.add(BatchNormalization(momentum=0.9))        self.G.add(Activation('relu'))        self.G.add(Conv2DTranspose(int(depth/8), 5, padding='same'))        self.G.add(BatchNormalization(momentum=0.9))        self.G.add(Activation('relu'))        # Out: 28 x 28 x 1 grayscale image [0.0,1.0] per pix        self.G.add(Conv2DTranspose(1, 5, padding='same'))        self.G.add(Activation('sigmoid'))        self.G.summary()        return self.G    def discriminator_model(self):        if self.DM:            return self.DM        optimizer = RMSprop(lr=0.0002, decay=6e-8)        self.DM = Sequential()        self.DM.add(self.discriminator())        self.DM.compile(loss='binary_crossentropy', optimizer=optimizer,\            metrics=['accuracy'])        return self.DM    def adversarial_model(self):        if self.AM:            return self.AM        optimizer = RMSprop(lr=0.0001, decay=3e-8)        self.AM = Sequential()        self.AM.add(self.generator())        self.AM.add(self.discriminator())        self.AM.compile(loss='binary_crossentropy', optimizer=optimizer,\            metrics=['accuracy'])        return self.AMclass MNIST_DCGAN(object):    def __init__(self):        self.img_rows = 28        self.img_cols = 28        self.channel = 1        self.x_train = input_data.read_data_sets("mnist",\            one_hot=True).train.images        self.x_train = self.x_train.reshape(-1, self.img_rows,\            self.img_cols, 1).astype(np.float32)        self.DCGAN = DCGAN()        self.discriminator =  self.DCGAN.discriminator_model()        self.adversarial = self.DCGAN.adversarial_model()        self.generator = self.DCGAN.generator()    def train(self, train_steps=2000, batch_size=256, save_interval=0):        noise_input = None        if save_interval>0:            noise_input = np.random.uniform(-1.0, 1.0, size=[16, 100])        for i in range(train_steps):            images_train = self.x_train[np.random.randint(0,                self.x_train.shape[0], size=batch_size), :, :, :]            noise = np.random.uniform(-1.0, 1.0, size=[batch_size, 100])            images_fake = self.generator.predict(noise)            x = np.concatenate((images_train, images_fake))            y = np.ones([2*batch_size, 1])            y[batch_size:, :] = 0            d_loss = self.discriminator.train_on_batch(x, y)            y = np.ones([batch_size, 1])            noise = np.random.uniform(-1.0, 1.0, size=[batch_size, 100])            a_loss = self.adversarial.train_on_batch(noise, y)            log_mesg = "%d: [D loss: %f, acc: %f]" % (i, d_loss[0], d_loss[1])            log_mesg = "%s  [A loss: %f, acc: %f]" % (log_mesg, a_loss[0], a_loss[1])            print(log_mesg)            if save_interval>0:                if (i+1)%save_interval==0:                    self.plot_images(save2file=True, samples=noise_input.shape[0],\                        noise=noise_input, step=(i+1))    def plot_images(self, save2file=False, fake=True, samples=16, noise=None, step=0):        filename = 'mnist.png'        if fake:            if noise is None:                noise = np.random.uniform(-1.0, 1.0, size=[samples, 100])            else:                filename = "mnist_%d.png" % step            images = self.generator.predict(noise)        else:            i = np.random.randint(0, self.x_train.shape[0], samples)            images = self.x_train[i, :, :, :]        plt.figure(figsize=(10,10))        for i in range(images.shape[0]):            plt.subplot(4, 4, i+1)            image = images[i, :, :, :]            image = np.reshape(image, [self.img_rows, self.img_cols])            plt.imshow(image, cmap='gray')            plt.axis('off')        plt.tight_layout()        if save2file:            plt.savefig(filename)            plt.close('all')        else:            plt.show()if __name__ == '__main__':    mnist_dcgan = MNIST_DCGAN()    timer = ElapsedTimer()    mnist_dcgan.train(train_steps=10000, batch_size=256, save_interval=500)    timer.elapsed_time()    mnist_dcgan.plot_images(fake=True)    mnist_dcgan.plot_images(fake=False, save2file=True)
Note: https://keras-cn.readthedocs.io/en/latest/other/callbacks/

TensorFlow和Keras入门必读教程

导读:本文对TensorFlow的框架和基本示例进行简要介绍。
作者:本杰明·普朗什(Benjamin Planche)艾略特·安德烈斯(Eliot Andres)
来源:华章科技
01 TensorFlow
TensorFlow最初由Google开发,旨在让研究人员和开发人员进行机器学习研究。它最初被定义为描述机器学习算法的接口,以及执行该算法的实现。
TensorFlow的主要预期目标是简化机器学习解决方案在各种平台上的部署,如计算机CPU、计算机GPU、移动设备以及最近的浏览器中的部署。最重要的是,TensorFlow提供了许多有用的功能来创建机器学习模型并大规模运行它们。TensorFlow 2于2019年发布,它专注于易用性,并能保持良好的性能。
这个库于2015年11月开源。从那时起,它已被世界各地的用户改进和使用。它被认为是开展研究的首选平台之一。就GitHub活跃度而言,它也是最活跃的深度学习框架之一。
TensorFlow既可供初学者使用,也可供专家使用。TensorFlow API具有不同级别的复杂度,从而使初学者可以从简单的API开始,同时也可以让专家创建非常复杂的模型。我们来探索一下这些不同级别的模型。
1. TensorFlow主要架构
TensorFlow架构(见图2-1)具有多个抽象层级。我们首先介绍底层,然后逐渐通往最上层。
▲图2-1 TensorFlow架构图
大多数深度学习计算都是用C++编码的。为了在GPU上进行运算,TensorFlow使用了由NVIDIA开发的库CUDA。这就是如果想要利用GPU功能就需要安装CUDA,以及不能使用其他硬件制造商GPU的原因。
然后,Python底层API(low-level API)封装了C++源代码。当调用TensorFlow的Python方法时,通常会在后台调用C++代码。这个封装层使用户可以更快地工作,因为Python被认为更易于使用并且不需要编译。该Python封装器可以创建非常基本的运算,例如矩阵乘法和加法。
最上层是高级API(high-level API),由Keras和评估器API(estimator API)两个组件组成。Keras是TensorFlow的一个用户友好型、模块化且可扩展的封装器,评估器API包含多个预制组件,可让你轻松地构建机器学习模型。你可以将它们视为构建块或模板。
tip:在深度学习中,模型通常是指经过数据训练的神经网络。模型由架构、矩阵权重和参数组成。2. Keras介绍
Keras于2015年首次发布,它被设计为一种接口,可用于使用神经网络进行快速实验。因此,它依赖TensorFlow或Theano(另一个深度学习框架,现已弃用)来运行深度学习操作。Keras以其用户友好性著称,是初学者的首选库。
自2017年以来,TensorFlow完全集成了Keras,这意味着无须安装TensorFlow以外的任何库就可使用它。我们将依赖tf.keras而不是Keras的独立版本。这两个版本之间有一些细微的差异,例如与TensorFlow的其他模块的兼容性以及模型的保存方式。因此,读者必须确保使用正确的版本,具体方法如下:
在代码中,导入tf.keras而不是keras。浏览TensorFlow网站上的tf.keras文档,而不是keras.io文档。在使用外部Keras库时,请确保它们与tf.keras兼容。某些保存的模型在Keras版本之间可能不兼容。这两个版本在可预见的未来将继续共存,而tf.keras与TensorFlow集成将越来越密切。为了说明Keras的强大功能和简单性,我们将使用该库实现一个简单的神经网络。
02 基于Keras的简单计算机视觉模型
在深入探讨TensorFlow的核心概念之前,我们先从一个计算机视觉的经典示例开始,它使用数据集MNIST进行数字识别。
1. 准备数据
首先,导入数据。它由用于训练集的60 000幅图像和用于测试集的10 000幅图像组成:
import tensorflow as tfnum_classes = 10img_rows, img_cols = 28, 28num_channels = 1input_shape = (img_rows, img_cols, num_channels)(x_train, y_train),(x_test, y_test) = tf.keras.datasets.mnist.load_data()x_train, x_test = x_train / 255.0, x_test / 255.0
tip:常见的做法是使用别名tf来导入TensorFlow,从而加快读取和键入速度。通常用x表示输入数据,用y表示标签。tf.keras.datasets模块提供快速访问,以下载和实例化一些经典数据集。使用load_data导入数据后,请注意,我们将数组除以255.0,得到的数字范围为[0, 1]而不是[0, 255]。将数据归一化在[0, 1]范围或[-1, 1]范围是一种常见的做法。
2. 构建模型
现在,我们可以继续构建实际模型。我们将使用一个非常简单的架构,该架构由两个全连接层(也称为稠密层)组成。在详细介绍架构之前,我们来看一下代码。可以看到,Keras代码非常简洁:
model = tf.keras.models.Sequential()model.add(tf.keras.layers.Flatten())model.add(tf.keras.layers.Dense(128, activation='relu'))model.add(tf.keras.layers.Dense(num_classes, activation='softmax'))
由于模型是层的线性堆栈,因此我们首先调用Sequential函数。然后,依次添加每一层。模型由两个全连接层组成。我们逐层构建:
展平层(Flatten):它将接受表示图像像素的二维矩阵,并将其转换为一维数组。我们需要在添加全连接层之前执行此操作。28×28的图像被转换为大小为784的向量。大小为128的稠密层(Dense):它使用大小为128×784的权重矩阵和大小为128的偏置矩阵,将784个像素值转换为128个激活值。这意味着有100 480个参数。大小为10的稠密层(Dense):它将把128个激活值转变为最终预测。注意,因为概率总和为1,所以我们将使用softmax激活函数。tip:softmax函数获取某层的输出,并返回总和为1的概率。它是分类模型最后一层的选择的激活函数。请注意,使用model.summary()可以获得有关模型、输出及其权重的描述。下面是输出:
设置好架构并初始化权重后,模型现在就可以针对所选任务进行训练了。
3. 训练模型
Keras让训练变得非常简单:
model.compile(optimizer='sgd',loss='sparse_categorical_crossentropy',metrics=['accuracy'])callbacks = [tf.keras.callbacks.TensorBoard('./keras')]model.fit(x_train, y_train, epochs=25, verbose=1, validation_data=(x_test, y_test), callbacks=callbacks)
在刚刚创建的模型上调用.compile()是一个必需的步骤。必须指定几个参数:
优化器(optimizer):运行梯度下降的组件。损失(loss):优化的指标。在本例中,选择交叉熵,就像上一章一样。评估指标(metrics):在训练过程进行评估的附加评估函数,以进一步查看有关模型性能(与损失不同,它们不在优化过程中使用)。名为sparse_categorical_crossentropy的Keras损失执行与categorical_crossentropy相同的交叉熵运算,但是前者直接将真值标签作为输入,而后者则要求真值标签先变成独热(one-hot)编码。因此,使用sparse_...损失可以免于手动转换标签的麻烦。
tip:将'sgd'传递给Keras等同于传递tf.keras.optimizers.SGD()。前一个选项更易于阅读,而后一个选项则可以指定参数,如自定义学习率。传递给Keras方法的损失、评估指标和大多数参数也是如此。然后,我们调用.fit()方法。它与另一个流行的机器学习库scikit-learn中所使用的接口非常相似。我们将训练5轮,这意味着将对整个训练数据集进行5次迭代。
请注意,我们将verbose设置为1。这将让我们获得一个进度条,其中包含先前选择的指标、损失和预计完成时间(Estimated Time of Arrival,ETA)。ETA是对轮次结束之前剩余时间的估计。进度条如图2-2所示。
▲图2-2 Keras在详细模式下显示的进度条屏幕截图
4. 模型性能
如第1章中所述,你会注意到模型是过拟合的——即训练准确率大于测试准确率。如果对模型训练5轮,则最终在测试集上的准确率为97%。这比上一章(95%)高了约2个百分点。最先进的算法可达到99.79%的准确率。
我们遵循了三个主要步骤:
加载数据:在本例中,数据集已经可用。在未来的项目中,你可能需要其他的步骤来收集和清理数据。创建模型:使用Keras可以让这一步骤变得容易——按顺序添加层即可定义模型的架构。然后,选择损失、优化器和评估指标进行监控。训练模型:模型第一次运行效果很好。在更复杂的数据集上,通常需要在训练过程中微调参数。借助TensorFlow的高级API——Keras,整个过程非常简单。在这个简单API的背后,该库隐藏了很多复杂操作。
关于作者:本杰明·普朗什(Benjamin Planche),他在计算机视觉和深度学习领域的全球多个研究实验室(法国LIRIS、日本三菱电机和德国西门子)工作超过5年。他的研究重点是针对工业应用使用更少的数据开发更智能的视觉系统。他还在在线平台(例如StackOverflow)上分享自己的知识和经验,或者创建有美感的演示系统。
艾略特·安德烈斯(Eliot Andres),深度学习和计算机视觉工程师。他在该领域拥有3年以上的经验,涉及银行、医疗、社交媒体和视频流等行业。他关注的是工业化,即通过将新技术应用于商业问题来实现价值。
本文摘编自《计算机视觉实战:基于TensorFlow 2》,经出版方授权发布。
延伸阅读《计算机视觉实战:基于TensorFlow 2》
推荐语:本书从计算机视觉和深度学习基础知识开始,教你如何从头开始构建神经网络。你将掌握一些让TensorFlow成为广泛使用的Al库的特性,以及直观的Keras接口,继而高效地构建、训练和部署CNN。

发表评论