예제 #1
0
def build_elu_cnn(input_shape, output_size):
    """Build a variation of the CNN implemented in the ELU paper.

    https://arxiv.org/abs/1511.07289
    """
    def layers(n, channels, kernel):
        return sum(([
            Convolution2D(channels, kernel_size=kernel, padding="same"),
            ELU()
        ] for i in range(n)), [])

    model = Sequential(
        [
            Convolution2D(
                384, kernel_size=3, padding="same", input_shape=input_shape)
        ] + layers(1, 384, 3) + [MaxPooling2D(pool_size=(2, 2))] +
        layers(1, 384, 1) + layers(1, 384, 2) + layers(2, 640, 2) +
        [MaxPooling2D(pool_size=(2, 2))] + layers(1, 640, 1) +
        layers(3, 768, 2) + [MaxPooling2D(pool_size=(2, 2))] +
        layers(1, 768, 1) + layers(2, 896, 2) +
        [MaxPooling2D(pool_size=(2, 2))] + layers(1, 896, 3) +
        layers(2, 1024, 2) + [
            MaxPooling2D(pool_size=(2, 2)),
            Convolution2D(output_size, kernel_size=1, padding="same"),
            GlobalAveragePooling2D(),
            Activation("softmax")
        ])

    model.compile(optimizer=SGD(momentum=0.9),
                  loss="categorical_crossentropy",
                  metrics=["accuracy"])

    return model
예제 #2
0
def build_small_cnn(input_shape, output_size):
    model = Sequential([
        # conv1_*
        Convolution2D(32,
                      kernel_size=3,
                      padding="same",
                      input_shape=input_shape),
        Activation("relu"),
        Convolution2D(32, kernel_size=3, padding="same"),
        Activation("relu"),
        MaxPooling2D(pool_size=(2, 2)),

        # conv2_*
        Convolution2D(64, kernel_size=3, padding="same"),
        Activation("relu"),
        Convolution2D(64, kernel_size=3, padding="same"),
        Activation("relu"),
        MaxPooling2D(pool_size=(2, 2)),

        # Fully connected
        Flatten(),
        Dense(512),
        Activation("relu"),
        Dense(512),
        Activation("relu"),
        Dense(output_size),
        Activation("softmax")
    ])

    model.compile(loss="categorical_crossentropy",
                  optimizer="adam",
                  metrics=["accuracy"])

    return model
예제 #3
0
def build_network(num_actions, agent_history_length, resized_width,
                  resized_height):
    with tf.device("/gpu:0"):
        state = tf.placeholder(
            "float",
            [None, agent_history_length, resized_width, resized_height])
        inputs = Input(shape=(
            agent_history_length,
            resized_width,
            resized_height,
        ))
        model = Convolution2D(filters=16,
                              kernel_size=(8, 8),
                              strides=(4, 4),
                              activation='relu',
                              padding='same')(inputs)
        model = Convolution2D(filters=32,
                              kernel_size=(4, 4),
                              strides=(2, 2),
                              activation='relu',
                              padding='same')(model)
        model = Flatten()(model)
        model = Dense(256, activation='relu')(model)
        q_values = Dense(num_actions, activation='linear')(model)
        m = Model(inputs, outputs=q_values)
    return state, m
예제 #4
0
 def inner(x):
     x = LayerNormalization()(x)
     x = Activation("relu")(x)
     x = Convolution2D(channels, 3, strides=strides, **params)(x)
     x = Dropout(drop_rate)(x) if drop_rate > 0 else x
     x = LayerNormalization()(x)
     x = Activation("relu")(x)
     x = Convolution2D(channels, 3, **params)(x)
     return x
예제 #5
0
 def resize(x, shape):
     if K.int_shape(x) == shape:
         return x
     channels = shape[3 if K.image_data_format() ==
                      "channels_last" else 1]
     strides = K.int_shape(x)[2] // shape[2]
     return Convolution2D(channels,
                          1,
                          padding="same",
                          use_bias=False,
                          strides=strides)(x)
예제 #6
0
def build_all_conv_nn(input_shape, output_size):
    """Build a small variation of the best performing network from
    'Springenberg, Jost Tobias, et al. "Striving for simplicity: The all
     convolutional net." arXiv preprint arXiv:1412.6806 (2014)' which should
     achieve approximately 91% in CIFAR-10.
    """
    kwargs = {"activation": "relu", "border_mode": "same"}
    model = Sequential([
        # conv1
        Convolution2D(96, 3, 3, input_shape=input_shape, **kwargs),
        BatchRenormalization(),
        Convolution2D(96, 3, 3, **kwargs),
        BatchRenormalization(),
        Convolution2D(96, 3, 3, subsample=(2, 2), **kwargs),
        BatchRenormalization(),
        Dropout(0.25),

        # conv2
        Convolution2D(192, 3, 3, **kwargs),
        BatchRenormalization(),
        Convolution2D(192, 3, 3, **kwargs),
        BatchRenormalization(),
        Convolution2D(192, 3, 3, subsample=(2, 2), **kwargs),
        BatchRenormalization(),
        Dropout(0.25),

        # conv3
        Convolution2D(192, 1, 1, **kwargs),
        BatchRenormalization(),
        Dropout(0.25),
        Convolution2D(output_size, 1, 1, **kwargs),
        GlobalAveragePooling2D(),
        Activation("softmax")
    ])

    model.compile(loss="categorical_crossentropy",
                  optimizer=SGD(momentum=0.9),
                  metrics=["accuracy"])

    return model
예제 #7
0
def build_cnn(input_shape, output_size):
    kwargs = {"kernel_size": 3, "activation": "relu", "padding": "same"}
    model = Sequential([
        # conv1_*
        Convolution2D(64, input_shape=input_shape, **kwargs),
        BatchRenormalization(),
        Convolution2D(64, **kwargs),
        BatchRenormalization(),
        MaxPooling2D(pool_size=(2, 2)),
        Dropout(0.25),

        # conv2_*
        Convolution2D(128, **kwargs),
        BatchRenormalization(),
        Convolution2D(128, **kwargs),
        BatchRenormalization(),
        MaxPooling2D(pool_size=(2, 2)),
        Dropout(0.25),

        # conv3_*
        Convolution2D(256, **kwargs),
        BatchRenormalization(),
        Convolution2D(256, **kwargs),
        BatchRenormalization(),
        MaxPooling2D(pool_size=(2, 2)),
        Dropout(0.25),

        # Fully connected
        Flatten(),
        Dense(1024),
        Activation("relu"),
        Dropout(0.5),
        Dense(512),
        Activation("relu"),
        Dropout(0.5),
        Dense(output_size),
        Activation("softmax")
    ])

    model.compile(loss="categorical_crossentropy",
                  optimizer="adam",
                  metrics=["accuracy"])

    return model
예제 #8
0
    def wide_resnet_impl(input_shape, output_size):
        def conv(channels,
                 strides,
                 params=dict(padding="same",
                             use_bias=False,
                             kernel_regularizer=l2(5e-4))):
            def inner(x):
                x = LayerNormalization()(x)
                x = Activation("relu")(x)
                x = Convolution2D(channels, 3, strides=strides, **params)(x)
                x = Dropout(drop_rate)(x) if drop_rate > 0 else x
                x = LayerNormalization()(x)
                x = Activation("relu")(x)
                x = Convolution2D(channels, 3, **params)(x)
                return x

            return inner

        def resize(x, shape):
            if K.int_shape(x) == shape:
                return x
            channels = shape[3 if K.image_data_format() ==
                             "channels_last" else 1]
            strides = K.int_shape(x)[2] // shape[2]
            return Convolution2D(channels,
                                 1,
                                 padding="same",
                                 use_bias=False,
                                 strides=strides)(x)

        def block(channels, k, n, strides):
            def inner(x):
                for i in range(n):
                    x2 = conv(channels * k, strides if i == 0 else 1)(x)
                    x = add([resize(x, K.int_shape(x2)), x2])
                return x

            return inner

        # According to the paper L = 6*n+4
        n = int((L - 4) / 6)

        group0 = Convolution2D(16,
                               3,
                               padding="same",
                               use_bias=False,
                               kernel_regularizer=l2(5e-4))
        group1 = block(16, k, n, 1)
        group2 = block(32, k, n, 2)
        group3 = block(64, k, n, 2)

        x_in = x = Input(shape=input_shape)
        x = group0(x)
        x = group1(x)
        x = group2(x)
        x = group3(x)

        x = LayerNormalization()(x)
        x = Activation("relu")(x)
        x = GlobalAveragePooling2D()(x)
        x = Dense(output_size, kernel_regularizer=l2(5e-4))(x)
        y = Activation("softmax")(x)

        model = Model(inputs=x_in, outputs=y)
        model.compile(loss="categorical_crossentropy",
                      optimizer="adam",
                      metrics=["accuracy"])

        return model
예제 #9
0
 def layers(n, channels, kernel):
     return sum(([
         Convolution2D(channels, kernel_size=kernel, padding="same"),
         ELU()
     ] for i in range(n)), [])