示例#1
0
def model():
    with tf.name_scope('MnistCNN'):
        seq = tg.Sequential()
        seq.add(Conv2D(num_filters=32, kernel_size=(3, 3), stride=(1, 1), padding='SAME'))
        seq.add(BatchNormalization())
        seq.add(RELU())

        seq.add(MaxPooling(poolsize=(2, 2), stride=(2,2), padding='SAME'))
        seq.add(LRN())

        seq.add(Conv2D(num_filters=64, kernel_size=(3, 3), stride=(1, 1), padding='SAME'))
        seq.add(BatchNormalization())
        seq.add(RELU())

        seq.add(MaxPooling(poolsize=(2, 2), stride=(2,2), padding='SAME'))
        seq.add(LRN())
        seq.add(Flatten())
        seq.add(Linear(128))
        seq.add(BatchNormalization())
        seq.add(Tanh())
        seq.add(Dropout(0.8))
        seq.add(Linear(256))
        seq.add(BatchNormalization())
        seq.add(Tanh())
        seq.add(Dropout(0.8))
        seq.add(Linear(10))
        seq.add(Softmax())
    return seq
示例#2
0
def test_UNet():
    seq = tg.Sequential()
    seq.add(UNet(input_shape=(h, w)))
    seq.add(MaxPooling(poolsize=(3,3), stride=(1,1), padding='VALID'))
    seq.add(Flatten())
    seq.add(Linear(this_dim=nclass))
    seq.add(Softmax())
    train(seq)
示例#3
0
def test_DenseNet():
    seq = tg.Sequential()
    seq.add(DenseNet(ndense=1, growth_rate=1, nlayer1blk=1))
    seq.add(MaxPooling(poolsize=(3,3), stride=(1,1), padding='VALID'))
    seq.add(Flatten())
    seq.add(Linear(this_dim=nclass))
    seq.add(Softmax())
    train(seq)
示例#4
0
def test_ResNetBase():
    seq = tg.Sequential()
    seq.add(ResNetBase(config=[1,1,1,1]))
    seq.add(MaxPooling(poolsize=(1,1), stride=(1,1), padding='VALID'))
    seq.add(Flatten())
    seq.add(Linear(this_dim=nclass))
    seq.add(Softmax())
    train(seq)
示例#5
0
def model():
    with tf.name_scope('MnistCNN'):
        seq = tg.Sequential()
        seq.add(
            Conv2D(input_channels=1,
                   num_filters=32,
                   kernel_size=(3, 3),
                   stride=(1, 1),
                   padding='SAME'))
        h, w = same(in_height=28,
                    in_width=28,
                    stride=(1, 1),
                    kernel_size=(3, 3))
        seq.add(BatchNormalization(input_shape=[h, w, 32]))
        seq.add(RELU())

        seq.add(MaxPooling(poolsize=(2, 2), stride=(2, 2), padding='SAME'))
        h, w = same(in_height=h, in_width=w, stride=(2, 2), kernel_size=(2, 2))
        seq.add(LRN())

        seq.add(
            Conv2D(input_channels=32,
                   num_filters=64,
                   kernel_size=(3, 3),
                   stride=(1, 1),
                   padding='SAME'))
        h, w = same(in_height=h, in_width=w, stride=(1, 1), kernel_size=(3, 3))
        seq.add(BatchNormalization(input_shape=[h, w, 64]))
        seq.add(RELU())

        seq.add(MaxPooling(poolsize=(2, 2), stride=(2, 2), padding='SAME'))
        h, w = same(in_height=h, in_width=w, stride=(2, 2), kernel_size=(2, 2))
        seq.add(LRN())
        seq.add(Flatten())
        seq.add(Linear(int(h * w * 64), 128))
        seq.add(BatchNormalization(input_shape=[128]))
        seq.add(Tanh())
        seq.add(Dropout(0.8))
        seq.add(Linear(128, 256))
        seq.add(BatchNormalization(input_shape=[256]))
        seq.add(Tanh())
        seq.add(Dropout(0.8))
        seq.add(Linear(256, 10))
        seq.add(Softmax())
    return seq
示例#6
0
def test_DenseNet():
    seq = tg.Sequential()
    model = DenseNet(input_channels=c, input_shape=(h, w), ndense=1, growth_rate=1, nlayer1blk=1)
    print('output channels:', model.output_channels)
    print('output shape:', model.output_shape)
    seq.add(model)
    seq.add(MaxPooling(poolsize=tuple(model.output_shape), stride=(1,1), padding='VALID'))
    seq.add(Flatten())
    seq.add(Linear(model.output_channels, nclass))
    seq.add(Softmax())
    train(seq)
示例#7
0
def test_UNet():
    seq = tg.Sequential()
    model = UNet(input_channels=c, input_shape=(h, w))
    print('output channels:', model.output_channels)
    print('output shape:', model.output_shape)
    out_dim = np.prod(model.output_shape) * model.output_channels
    seq.add(model)
    seq.add(MaxPooling(poolsize=tuple(model.output_shape), stride=(1,1), padding='VALID'))
    seq.add(Flatten())
    seq.add(Linear(model.output_channels, nclass))
    seq.add(Softmax())
    train(seq)
示例#8
0
def test_ResNetBase():
    seq = tg.Sequential()
    model = ResNetBase(input_channels=c, input_shape=(h, w), config=[1,1,1,1])
    print('output channels:', model.output_channels)
    print('output shape:', model.output_shape)
    seq.add(model)
    seq.add(MaxPooling(poolsize=tuple(model.output_shape), stride=(1,1), padding='VALID'))
    outshape = valid_nd(model.output_shape, kernel_size=model.output_shape, stride=(1,1))
    print(outshape)
    out_dim = model.output_channels
    seq.add(Flatten())
    seq.add(Linear(int(out_dim), nclass))
    seq.add(Softmax())
    train(seq)
示例#9
0
 def __init__(self, nclass, h, w, c):
     layers = []
     model = UNet(input_channels=c, input_shape=(h, w))
     layers.append(model)
     layers.append(
         MaxPooling(poolsize=tuple(model.output_shape),
                    stride=(1, 1),
                    padding='VALID'))
     layers.append(Flatten())
     layers.append(Linear(model.output_channels, nclass))
     layers.append(Softmax())
     self.startnode = tg.StartNode(input_vars=[None])
     model_hn = tg.HiddenNode(prev=[self.startnode], layers=layers)
     self.endnode = tg.EndNode(prev=[model_hn])
示例#10
0
def classifier(X_ph, X_gen_ph, h, w):
    with tf.variable_scope('Classifier'):
        X_sn = tg.StartNode(input_vars=[X_ph])
        X_gen_sn = tg.StartNode(input_vars=[X_gen_ph])
        h1, w1 = same(in_height=h,
                      in_width=w,
                      stride=(1, 1),
                      kernel_size=(3, 3))
        h2, w2 = same(in_height=h1,
                      in_width=w1,
                      stride=(2, 2),
                      kernel_size=(2, 2))
        h3, w3 = same(in_height=h2,
                      in_width=w2,
                      stride=(1, 1),
                      kernel_size=(3, 3))
        h4, w4 = same(in_height=h3,
                      in_width=w3,
                      stride=(2, 2),
                      kernel_size=(2, 2))

        print('---', h, w)
        X_hn = tg.HiddenNode(prev=[X_sn],
                             layers=[
                                 Conv2D(input_channels=1,
                                        num_filters=32,
                                        kernel_size=(3, 3),
                                        stride=(1, 1),
                                        padding='SAME'),
                                 BatchNormalization(input_shape=[h1, w1, 32]),
                                 RELU(),
                                 MaxPooling(poolsize=(2, 2),
                                            stride=(2, 2),
                                            padding='SAME'),
                                 LRN(),
                                 Conv2D(input_channels=32,
                                        num_filters=64,
                                        kernel_size=(3, 3),
                                        stride=(1, 1),
                                        padding='SAME'),
                                 BatchNormalization(input_shape=[h3, w3, 64]),
                                 RELU(),
                                 MaxPooling(poolsize=(2, 2),
                                            stride=(2, 2),
                                            padding='SAME'),
                                 Flatten(),
                             ])

        X_gen_hn = tg.HiddenNode(
            prev=[X_gen_sn],
            layers=[
                Conv2D(input_channels=1,
                       num_filters=32,
                       kernel_size=(3, 3),
                       stride=(1, 1),
                       padding='SAME'),
                BatchNormalization(input_shape=[h1, w1, 32]),
                RELU(),
                MaxPooling(poolsize=(2, 2), stride=(2, 2), padding='SAME'),
                LRN(),
                Conv2D(input_channels=32,
                       num_filters=64,
                       kernel_size=(3, 3),
                       stride=(1, 1),
                       padding='SAME'),
                BatchNormalization(input_shape=[h3, w3, 64]),
                RELU(),
                MaxPooling(poolsize=(2, 2), stride=(2, 2), padding='SAME'),
                Flatten(),
            ])

        print('===', h4 * w4 * 64 * 2)

        merge_hn = tg.HiddenNode(prev=[X_hn, X_gen_hn],
                                 input_merge_mode=Concat(),
                                 layers=[
                                     Linear(h4 * w4 * 64 * 2, 100),
                                     RELU(),
                                     BatchNormalization(input_shape=[100]),
                                     Linear(100, 1),
                                     Sigmoid()
                                 ])

        en = tg.EndNode(prev=[merge_hn])

        graph = tg.Graph(start=[X_sn, X_gen_sn], end=[en])
        y_train, = graph.train_fprop()
        y_test, = graph.test_fprop()
    return y_train, y_test