Example #1
0
def add_n_bottleneck_blocks(in_layer, n_outfmaps, n_repeat, is_first_layer):
    assert n_outfmaps % 4 == 0
    a0 = in_layer
    for i1 in xrange(n_repeat):
        if i1 == 0:
            if is_first_layer is True:
                strides = (1, 1)
                shortcut = a0
            else:
                strides = (2, 2)
                shortcut = Convolution2D(n_outfmaps=n_outfmaps,
                                         n_row=1,
                                         n_col=1,
                                         act='linear',
                                         border_mode='valid',
                                         strides=strides)(a0)
        else:
            strides = (1, 1)
            shortcut = a0

        a1 = BN(axes=(0, 2, 3))(a0)
        a2 = Activation('relu')(a1)
        a3 = Convolution2D(n_outfmaps=n_outfmaps,
                           n_row=1,
                           n_col=1,
                           act='linear',
                           border_mode='valid',
                           strides=strides)(a2)
        a4 = BN(axes=(0, 2, 3))(a3)
        a5 = Activation('relu')(a4)
        a6 = Convolution2D(n_outfmaps=n_outfmaps / 4,
                           n_row=3,
                           n_col=3,
                           act='linear',
                           border_mode=(1, 1),
                           strides=(1, 1))(a5)
        a7 = BN(axes=(0, 2, 3))(a6)
        a8 = Activation('relu')(a7)
        a9 = Convolution2D(n_outfmaps=n_outfmaps,
                           n_row=1,
                           n_col=1,
                           act='linear',
                           border_mode='valid',
                           strides=(1, 1))(a8)

        a10 = Lambda(add_layers)([shortcut, a9])

        a0 = a10

    return a0
Example #2
0
def add_n_blocks(in_layer, n_outfmaps, n_repeat, is_first_layer):
    a0 = in_layer
    for i1 in range(n_repeat):
        if i1 == 0:
            if is_first_layer is True:
                strides = (1, 1)
                shortcut = a0
            else:
                strides = (2, 2)
                shortcut = Convolution2D(n_outfmaps=n_outfmaps,
                                         n_row=1,
                                         n_col=1,
                                         act='linear',
                                         border_mode='valid',
                                         strides=strides)(a0)
        else:
            strides = (1, 1)
            shortcut = a0

        a1 = Convolution2D(n_outfmaps=n_outfmaps,
                           n_row=3,
                           n_col=3,
                           act='linear',
                           border_mode=(1, 1),
                           strides=strides)(a0)
        a2 = BN(axes=(0, 2, 3))(a1)
        a3 = Activation('relu')(a2)
        a4 = Convolution2D(n_outfmaps=n_outfmaps,
                           n_row=3,
                           n_col=3,
                           act='linear',
                           border_mode=(1, 1),
                           strides=(1, 1))(a3)
        a5 = BN(axes=(0, 2, 3))(a4)
        a6 = Activation('relu')(a5)

        a7 = Lambda(add_layers)([shortcut, a6])

        a0 = a7

    return a0
Example #3
0
def add_n_blocks(seq, n_outfmaps, n_repeat, is_first_layer):
    for i1 in range(n_repeat):
        if i1 == 0:
            if is_first_layer is True: strides = (1, 1)
            else: strides = (2, 2)
        else:
            strides = (1, 1)
        seq.add(
            Convolution2D(n_outfmaps=n_outfmaps,
                          n_row=3,
                          n_col=3,
                          act='linear',
                          border_mode=(1, 1),
                          strides=strides))
        seq.add(BN(axes=(0, 2, 3)))
        seq.add(Activation('relu'))

    return seq
Example #4
0
# init params
n_in = 784
n_hid = 500
n_out = 10

# sparse label to 1 of K categorical label
tr_y = sparse_to_categorical(tr_y, n_out)
va_y = sparse_to_categorical(va_y, n_out)
te_y = sparse_to_categorical(te_y, n_out)

### Build model
act = 'relu'
seq = Sequential()
seq.add(InputLayer(in_shape=(1, 28, 28)))
seq.add(Convolution2D(n_outfmaps=32, n_row=3, n_col=3, act='relu'))
seq.add(MaxPool2D(pool_size=(2, 2)))
seq.add(Convolution2D(n_outfmaps=32, n_row=3, n_col=3, act='relu'))
seq.add(MaxPool2D(pool_size=(2, 2)))
seq.add(Dropout(0.2))
seq.add(Flatten())
seq.add(Dense(n_hid, act='relu'))
seq.add(Dropout(0.5))
seq.add(Dense(n_hid, act='relu'))
seq.add(Dense(n_out, act='softmax'))
md = seq.combine()

# print summary info of model
md.summary()

# optimization method
Example #5
0
n_out = 10

# sparse label to 1-of-K categorical label
tr_y = sparse_to_categorical(tr_y, n_out)
te_y = sparse_to_categorical(te_y, n_out)
print tr_X.shape
print tr_y.shape

### Build model
seq = Sequential()
seq.add(InputLayer(in_shape=(3, 32, 32)))

seq.add(
    Convolution2D(n_outfmaps=64,
                  n_row=3,
                  n_col=3,
                  act='linear',
                  border_mode=(1, 1)))
seq.add(BN(axes=(0, 2, 3)))
seq.add(Activation('relu'))
seq.add(
    Convolution2D(n_outfmaps=64,
                  n_row=3,
                  n_col=3,
                  act='linear',
                  border_mode=(1, 1)))
seq.add(BN(axes=(0, 2, 3)))
seq.add(Activation('relu'))
seq.add(MaxPool2D(pool_size=(2, 2)))

seq.add(
Example #6
0
                           border_mode=(1, 1),
                           strides=(1, 1))(a3)
        a5 = BN(axes=(0, 2, 3))(a4)
        a6 = Activation('relu')(a5)

        a7 = Lambda(add_layers)([shortcut, a6])

        a0 = a7

    return a0


x0 = InputLayer(in_shape=(3, 32, 32))
x1 = Convolution2D(n_outfmaps=64,
                   n_row=3,
                   n_col=3,
                   act='relu',
                   border_mode=(1, 1))(x0)
x2 = add_n_blocks(x1, n_outfmaps=64, n_repeat=3, is_first_layer=True)
x3 = add_n_blocks(x2, n_outfmaps=128, n_repeat=4, is_first_layer=False)
x4 = add_n_blocks(x3, n_outfmaps=256, n_repeat=6, is_first_layer=False)
x5 = add_n_blocks(x4, n_outfmaps=512, n_repeat=3, is_first_layer=False)

y1 = Lambda(mean_pool)(x5)
y2 = Flatten()(y1)
y3 = Dense(n_out, act='softmax')(y2)
md = Model([x0], [y3])

# print summary info of model
md.summary()