def load_mnist_convnet_5(path):
    ## your code here
    input = tflearn.input_data(shape=[None, 28, 28, 1])
    cov1 = tflearn.conv_2d(input, nb_filter=120, filter_size=10, activation='sigmoid', name='conv_layer_1')
    max_pool1 = tflearn.max_pool_2d(cov1, 2, name='pool1')
    lrn = tflearn.local_response_normalization(max_pool1)
    cov2 = tflearn.conv_2d(lrn, nb_filter=80, filter_size=10, activation='sigmoid', name='conv_layer_2')
    max_pool2 = tflearn.max_pool_2d(cov2, 2, name='pool2')
    lrn2 = tflearn.local_response_normalization(max_pool2)
    cov3 = tflearn.conv_2d(lrn2, nb_filter=40, filter_size=10, activation='sigmoid', name='conv_layer_3')
    max_pool3 = tflearn.max_pool_2d(cov3, 2, name='pool3')
    fc1 = tflearn.fully_connected(max_pool3, 30, activation='sigmoid')
    fc2 = tflearn.fully_connected(fc1, 10, activation='softmax')
    model =  tflearn.DNN(fc2)
예제 #2
0
def conv_net(input_shape, **kwargs):
    net = input_data(shape=input_shape)
    net = conv_2d(net, 32, 3, activation='relu',
                  regularizer="L2")  # filter_size=32, nb_filter=3
    net = max_pool_2d(net, 2)
    net = local_response_normalization(net)
    net = conv_2d(net, 64, 3, activation='relu', regularizer="L2")
    net = max_pool_2d(net, 2)
    net = local_response_normalization(net)
    net = fully_connected(net, 128, activation='tanh')
    net = dropout(net, 0.8)
    net = fully_connected(net, 256, activation='tanh')
    net = dropout(net, 0.8)
    net = fully_connected(net, 10, activation='softmax')
    return net
예제 #3
0
    def LoadModel(self):
        self.window_height = 16
        self.window_width = 8
        self.threshold = 0.03
        self.ground_height = 2
        symbol_count = 13

        network = input_data(
            shape=[None, self.window_height, self.window_width, symbol_count])
        network = conv_2d(network, 16, 2, activation='leaky_relu')
        network = max_pool_2d(network, 2)
        network = local_response_normalization(network)
        network = fully_connected(network,
                                  self.window_height * symbol_count,
                                  activation='relu')
        network = tf.reshape(network, [-1, self.window_height, symbol_count])
        network = regression(network,
                             optimizer='adagrad',
                             learning_rate=0.005,
                             loss='mean_square',
                             name='target',
                             batch_size=64)

        self.model = tflearn.DNN(network)
        self.model.load('./CNNmodel/model.tfl')
예제 #4
0
def conv(X_train, y_train, X_test, y_test):

    # reshape data
    X_train = X_train.reshape([-1, 28, 28, 1])
    X_test = X_test.reshape([-1, 28, 28, 1])

    # build network
    network = tflearn.input_data(shape=[None, 28, 28, 1], name='input')
    network = tflearn.conv_2d(network,
                              32,
                              3,
                              activation='relu',
                              regularizer="L2")
    network = tflearn.max_pool_2d(network, 2)
    network = tflearn.local_response_normalization(network)
    network = tflearn.conv_2d(network,
                              64,
                              3,
                              activation='relu',
                              regularizer="L2")
    network = tflearn.max_pool_2d(network, 2)
    network = tflearn.local_response_normalization(network)
    network = tflearn.fully_connected(network, 128, activation='tanh')
    network = tflearn.dropout(network, 0.8)
    network = tflearn.fully_connected(network, 256, activation='tanh')
    network = tflearn.dropout(network, 0.8)
    network = tflearn.fully_connected(network, 10, activation='softmax')
    network = tflearn.regression(network,
                                 optimizer='adam',
                                 learning_rate=0.01,
                                 loss='categorical_crossentropy',
                                 name='target')

    # train conv net
    model = tflearn.DNN(network, tensorboard_verbose=0)
    model.fit({'input': X_train}, {'target': y_train},
              n_epoch=20,
              validation_set=({
                  'input': X_test
              }, {
                  'target': y_test
              }),
              snapshot_step=100,
              show_metric=True)
예제 #5
0
 def create_actor_network(self):
     inputs = tflearn.input_data(shape=[None, 75, 100, 1], name='input')
     net = tflearn.conv_2d(inputs, 32, 3, activation='relu', regularizer="L2")
     net = tflearn.max_pool_2d(net, 2)
     net = tflearn.local_response_normalization(net)
     net = tflearn.conv_2d(net, 64, 3, activation='relu', regularizer="L2")
     net = tflearn.max_pool_2d(net, 2)
     net = tflearn.local_response_normalization(net)
     net = tflearn.conv_2d(net, 128, 2, activation='relu', regularizer="L2")
     net = tflearn.fully_connected(net, 256, activation='tanh')
     net = tflearn.dropout(net, 0.8)
     # Final layer weights are init to Uniform[-3e-3, 3e-3]
     w_init = tflearn.initializations.uniform(minval=-0.01, maxval=0.01)
     #w_init = tflearn.initializations.uniform(minval=-0.003, maxval=0.003)
     out = tflearn.fully_connected(
         net, self.a_dim, activation='tanh', weights_init=w_init)
     # Scale output to -action_bound to action_bound
     scaled_out = tf.multiply(out, self.action_bound)
     return inputs, out, scaled_out
def load_mnist_convnet_3(path):
    ## your code here
    input = tflearn.input_data(shape=[None, 28, 28, 1])
    cov1 = tflearn.conv_2d(input, nb_filter=32, filter_size=5, activation='relu', name='conv_layer_1')
    max_pool1 = tflearn.max_pool_2d(cov1, 2, name='pool1')
    lrn = tflearn.local_response_normalization(max_pool1)
    cov2 = tflearn.conv_2d(lrn, nb_filter=32, filter_size=5, activation='relu', name='conv_layer_2')
    max_pool2 = tflearn.max_pool_2d(cov2, 2, name='pool2')
    fc1 = tflearn.fully_connected(max_pool2, 100, activation='relu')
    fc2 = tflearn.fully_connected(fc1, 10, activation='softmax')
    model =  tflearn.DNN(fc2)
예제 #7
0
def build_tflearn_convnet_1(): 
    # your code here
    input = tflearn.input_data(shape=[None, 28, 28, 1])
    cov1 = tflearn.conv_2d(input, nb_filter=64, filter_size=5, activation='sigmoid', name='conv_layer_1')
    max_pool1 = tflearn.max_pool_2d(cov1, 2, name='pool1')
    lrn = tflearn.local_response_normalization(max_pool1)
    cov2 = tflearn.conv_2d(lrn, nb_filter=32, filter_size=5, activation='sigmoid', name='conv_layer_2')
    max_pool2 = tflearn.max_pool_2d(cov2, 2, name='pool2')
    fc1 = tflearn.fully_connected(max_pool2, 100, activation='sigmoid')
    fc2 = tflearn.fully_connected(fc1, 10, activation='softmax')
    reg = tflearn.regression(fc2, optimizer='sgd', loss='categorical_crossentropy', learning_rate=0.1)
    return tflearn.DNN(reg)
    def conv_neural_network_model(self):
        conv1_filters = 10
        conv2_filters = 6
        pool2_flat_dense_size = 100

        network = tflearn.input_data(shape=[None, self.n_frames * self.n_mfcc],
                                     name='input')
        network = tflearn.reshape(
            network, new_shape=[-1, self.n_frames, self.n_mfcc, 1])
        network = tflearn.conv_2d(network,
                                  conv1_filters,
                                  5,
                                  activation='relu',
                                  regularizer="L2")
        network = tflearn.max_pool_2d(network, 2)
        network = tflearn.local_response_normalization(network)
        network = tflearn.conv_2d(network,
                                  conv2_filters,
                                  5,
                                  activation='relu',
                                  regularizer="L2")
        network = tflearn.max_pool_2d(network, 2)
        network = tflearn.local_response_normalization(network)
        network = tflearn.fully_connected(network,
                                          pool2_flat_dense_size,
                                          activation='tanh',
                                          regularizer="L2")
        #network = tflearn.dropout(network, 0.1)
        network = tflearn.fully_connected(network,
                                          self.n_classes,
                                          activation='softmax')
        network = tflearn.regression(network,
                                     optimizer='adam',
                                     learning_rate=self.learning_rate,
                                     loss='categorical_crossentropy',
                                     name='target')

        self.model = tflearn.DNN(network, tensorboard_verbose=0)

        return self.model
def convnet():
    model = tf.input_data(shape=[None, 5, 19, 5], name='input')

    model = tf.conv_2d(model, 32, 3, activation='relu', regularizer="L2")
    model = tf.max_pool_2d(model, 2)
    model = tf.local_response_normalization(model)
    model = tf.conv_2d(model, 64, 3, activation='relu', regularizer="L2")
    model = tf.max_pool_2d(model, 2)
    model = tf.local_response_normalization(model)
    model = tf.fully_connected(model, 128, activation='tanh')
    model = tf.dropout(model, 0.8)
    model = tf.fully_connected(model, 256, activation='tanh')
    model = tf.dropout(model, 0.8)
    model = tf.fully_connected(model, 11, activation='sigmoid')
    model = tf.regression(model,
                          optimizer='adam',
                          learning_rate=0.01,
                          loss='categorical_crossentropy',
                          name='target')

    model = tf.DNN(model, tensorboard_verbose=0)
    return model
예제 #10
0
def build_cnn_bird(dim_s, dim_a):
    inputs = tf.placeholder(tf.float32, shape=[None] + dim_s)

    net = tf.image.resize_images(inputs, size=(80, 80))

    net = tflearn.conv_2d(net, 32, 8, strides=4, activation='relu')
    net = tflearn.max_pool_2d(net, 2)
    net = tflearn.local_response_normalization(net)

    net = tflearn.conv_2d(net, 64, 4, strides=2, activation='relu')
    net = tflearn.max_pool_2d(net, 2)
    net = tflearn.local_response_normalization(net)

    net = tflearn.fully_connected(net, 512, activation='relu')
    net = tflearn.dropout(net, 0.8)

    net = tflearn.fully_connected(net, 512, activation='relu')
    net = tflearn.dropout(net, 0.8)

    q_values = tflearn.fully_connected(net, dim_a)

    return inputs, q_values
예제 #11
0
def vgg16basic(input_data, num_class):
    conv1 = tflearn.conv_2d(input_data, 96, 11, strides=4, activation='relu')
    pool1 = tflearn.max_pool_2d(conv1, 3, strides=2)
    network = tflearn.local_response_normalization(pool1)

    conv2 = tflearn.conv_2d(network, 256, 5, activation='relu')
    pool2 = tflearn.max_pool_2d(conv2, 3, strides=2)
    network = tflearn.local_response_normalization(pool2)

    conv3 = tflearn.conv_2d(network, 384, 3, activation='relu')
    conv4 = tflearn.conv_2d(conv3, 384, 3, activation='relu')
    conv5 = tflearn.conv_2d(conv4, 256, 3, activation='relu')
    pool3 = tflearn.max_pool_2d(conv5, 3, strides=2)
    network = tflearn.local_response_normalization(pool3)

    fc1 = tflearn.fully_connected(network, 4096, activation='tanh')
    dropout1 = tflearn.dropout(fc1, 0.5)

    fc2 = tflearn.fully_connected(dropout1, 2048, activation='tanh')
    dropout2 = tflearn.dropout(fc2, 0.5)

    fc3 = tflearn.fully_connected(dropout2, 2, activation='softmax')
    return fc3
예제 #12
0
 def create_actor_network(self):
     inputs = tflearn.input_data(shape=[None, self.s_dim])
     net = tflearn.fully_connected(inputs, 400, activation=lambda x: tflearn.activations.leaky_relu(x, alpha=0.2))
     #net = tflearn.layers.normalization.batch_normalization(net)
     net = tflearn.local_response_normalization(net)
     #net = tflearn.fully_connected(net, 600,activation='relu', regularizer='L1', decay=0.001)
     net = tflearn.fully_connected(net, 600,activation=lambda x: tflearn.activations.leaky_relu(x, alpha=0.2), regularizer='L1', decay=0.001)
     #net = tflearn.dropout(net, 0.7)
     net = tflearn.fully_connected(net, 300,activation=lambda x: tflearn.activations.leaky_relu(x, alpha=0.2),  regularizer='L1', decay=0.001)
     #net = tflearn.local_response_normalization(net)
     # Final layer weights are init to Uniform[-3e-3, 3e-3]
     w_init = tflearn.initializations.uniform(minval=-0.003, maxval=0.003)
     out = tflearn.fully_connected(
         net, self.a_dim, activation='tanh', weights_init=w_init)
     # Scale output to -action_bound to action_bound
     scaled_out = tf.multiply(out, self.action_bound)
     return inputs, out, scaled_out
# Load HDF5 dataset
import h5py
h5f = h5py.File('your file path.h5', 'r')
X = h5f['X']
Y = h5f['Y']

network = tflearn.input_data(shape=[None, 250, 250, 3])
network = tflearn.conv_2d(network,
                          64,
                          10,
                          4,
                          activation='relu',
                          regularizer="L1")
network = tflearn.max_pool_2d(network, 2)
network = tflearn.local_response_normalization(network)
network = tflearn.conv_2d(network,
                          128,
                          11,
                          activation='relu',
                          regularizer="L1")
network = tflearn.max_pool_2d(network, 2)
network = tflearn.local_response_normalization(network)
network = tflearn.fully_connected(network, 128, activation='relu')
network = tflearn.dropout(network, 0.8)
network = tflearn.fully_connected(network, 32, activation='relu')
network = tflearn.fully_connected(network, 2, activation='sigmoid')

#top1 = tflearn.metrics.Top_k(k=1)

network = tflearn.regression(network,
예제 #14
0
파일: layers.py 프로젝트: Averroes/tflearn
# Using MNIST Dataset
import tflearn.datasets.mnist as mnist
mnist_data = mnist.read_data_sets(one_hot=True)

# User defined placeholders
with tf.Graph().as_default():
    # Placeholders for data and labels
    X = tf.placeholder(shape=(None, 784), dtype=tf.float32)
    Y = tf.placeholder(shape=(None, 10), dtype=tf.float32)

    net = tf.reshape(X, [-1, 28, 28, 1])

    # Using TFLearn wrappers for network building
    net = tflearn.conv_2d(net, 32, 3, activation='relu')
    net = tflearn.max_pool_2d(net, 2)
    net = tflearn.local_response_normalization(net)
    net = tflearn.dropout(net, 0.8)
    net = tflearn.conv_2d(net, 64, 3, activation='relu')
    net = tflearn.max_pool_2d(net, 2)
    net = tflearn.local_response_normalization(net)
    net = tflearn.dropout(net, 0.8)
    net = tflearn.fully_connected(net, 128, activation='tanh')
    net = tflearn.dropout(net, 0.8)
    net = tflearn.fully_connected(net, 256, activation='tanh')
    net = tflearn.dropout(net, 0.8)
    net = tflearn.fully_connected(net, 10, activation='softmax')

    # Defining other ops using Tensorflow
    loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(net, Y))
    optimizer = tf.train.AdamOptimizer(learning_rate=0.01).minimize(loss)
예제 #15
0
def inception(input_layer, num_class):

    conv1_7_7 = tflearn.conv_2d(input_layer,
                                64,
                                7,
                                strides=2,
                                activation='relu',
                                name='conv1_7_7_s2')
    pool1_3_3 = tflearn.max_pool_2d(conv1_7_7, 3, strides=2)
    pool1_3_3 = tflearn.local_response_normalization(pool1_3_3)
    conv2_3_3_reduce = tflearn.conv_2d(pool1_3_3,
                                       64,
                                       1,
                                       activation='relu',
                                       name='conv2_3_3_reduce')
    conv2_3_3 = tflearn.conv_2d(conv2_3_3_reduce,
                                192,
                                3,
                                activation='relu',
                                name='conv2_3_3')
    conv2_3_3 = tflearn.local_response_normalization(conv2_3_3)
    pool2_3_3 = tflearn.max_pool_2d(conv2_3_3,
                                    kernel_size=3,
                                    strides=2,
                                    name='pool2_3_3_s2')
    inception_3a_1_1 = tflearn.conv_2d(pool2_3_3,
                                       64,
                                       1,
                                       activation='relu',
                                       name='inception_3a_1_1')
    inception_3a_3_3_reduce = tflearn.conv_2d(pool2_3_3,
                                              96,
                                              1,
                                              activation='relu',
                                              name='inception_3a_3_3_reduce')
    inception_3a_3_3 = tflearn.conv_2d(inception_3a_3_3_reduce,
                                       128,
                                       filter_size=3,
                                       activation='relu',
                                       name='inception_3a_3_3')
    inception_3a_5_5_reduce = tflearn.conv_2d(pool2_3_3,
                                              16,
                                              filter_size=1,
                                              activation='relu',
                                              name='inception_3a_5_5_reduce')
    inception_3a_5_5 = tflearn.conv_2d(inception_3a_5_5_reduce,
                                       32,
                                       filter_size=5,
                                       activation='relu',
                                       name='inception_3a_5_5')
    inception_3a_pool = tflearn.max_pool_2d(
        pool2_3_3,
        kernel_size=3,
        strides=1,
    )
    inception_3a_pool_1_1 = tflearn.conv_2d(inception_3a_pool,
                                            32,
                                            filter_size=1,
                                            activation='relu',
                                            name='inception_3a_pool_1_1')

    # merge the inception_3a__
    inception_3a_output = tflearn.merge([
        inception_3a_1_1, inception_3a_3_3, inception_3a_5_5,
        inception_3a_pool_1_1
    ],
                                        mode='concat',
                                        axis=3)

    inception_3b_1_1 = tflearn.conv_2d(inception_3a_output,
                                       128,
                                       filter_size=1,
                                       activation='relu',
                                       name='inception_3b_1_1')
    inception_3b_3_3_reduce = tflearn.conv_2d(inception_3a_output,
                                              128,
                                              filter_size=1,
                                              activation='relu',
                                              name='inception_3b_3_3_reduce')
    inception_3b_3_3 = tflearn.conv_2d(inception_3b_3_3_reduce,
                                       192,
                                       filter_size=3,
                                       activation='relu',
                                       name='inception_3b_3_3')
    inception_3b_5_5_reduce = tflearn.conv_2d(inception_3a_output,
                                              32,
                                              filter_size=1,
                                              activation='relu',
                                              name='inception_3b_5_5_reduce')
    inception_3b_5_5 = tflearn.conv_2d(inception_3b_5_5_reduce,
                                       96,
                                       filter_size=5,
                                       name='inception_3b_5_5')
    inception_3b_pool = tflearn.max_pool_2d(inception_3a_output,
                                            kernel_size=3,
                                            strides=1,
                                            name='inception_3b_pool')
    inception_3b_pool_1_1 = tflearn.conv_2d(inception_3b_pool,
                                            64,
                                            filter_size=1,
                                            activation='relu',
                                            name='inception_3b_pool_1_1')

    # merge the inception_3b_*
    inception_3b_output = tflearn.merge([
        inception_3b_1_1, inception_3b_3_3, inception_3b_5_5,
        inception_3b_pool_1_1
    ],
                                        mode='concat',
                                        axis=3,
                                        name='inception_3b_output')

    pool3_3_3 = tflearn.max_pool_2d(inception_3b_output,
                                    kernel_size=3,
                                    strides=2,
                                    name='pool3_3_3')
    inception_4a_1_1 = tflearn.conv_2d(pool3_3_3,
                                       192,
                                       filter_size=1,
                                       activation='relu',
                                       name='inception_4a_1_1')
    inception_4a_3_3_reduce = tflearn.conv_2d(pool3_3_3,
                                              96,
                                              filter_size=1,
                                              activation='relu',
                                              name='inception_4a_3_3_reduce')
    inception_4a_3_3 = tflearn.conv_2d(inception_4a_3_3_reduce,
                                       208,
                                       filter_size=3,
                                       activation='relu',
                                       name='inception_4a_3_3')
    inception_4a_5_5_reduce = tflearn.conv_2d(pool3_3_3,
                                              16,
                                              filter_size=1,
                                              activation='relu',
                                              name='inception_4a_5_5_reduce')
    inception_4a_5_5 = tflearn.conv_2d(inception_4a_5_5_reduce,
                                       48,
                                       filter_size=5,
                                       activation='relu',
                                       name='inception_4a_5_5')
    inception_4a_pool = tflearn.max_pool_2d(pool3_3_3,
                                            kernel_size=3,
                                            strides=1,
                                            name='inception_4a_pool')
    inception_4a_pool_1_1 = tflearn.conv_2d(inception_4a_pool,
                                            64,
                                            filter_size=1,
                                            activation='relu',
                                            name='inception_4a_pool_1_1')

    inception_4a_output = tflearn.merge([
        inception_4a_1_1, inception_4a_3_3, inception_4a_5_5,
        inception_4a_pool_1_1
    ],
                                        mode='concat',
                                        axis=3,
                                        name='inception_4a_output')

    inception_4b_1_1 = tflearn.conv_2d(inception_4a_output,
                                       160,
                                       filter_size=1,
                                       activation='relu',
                                       name='inception_4a_1_1')
    inception_4b_3_3_reduce = tflearn.conv_2d(inception_4a_output,
                                              112,
                                              filter_size=1,
                                              activation='relu',
                                              name='inception_4b_3_3_reduce')
    inception_4b_3_3 = tflearn.conv_2d(inception_4b_3_3_reduce,
                                       224,
                                       filter_size=3,
                                       activation='relu',
                                       name='inception_4b_3_3')
    inception_4b_5_5_reduce = tflearn.conv_2d(inception_4a_output,
                                              24,
                                              filter_size=1,
                                              activation='relu',
                                              name='inception_4b_5_5_reduce')
    inception_4b_5_5 = tflearn.conv_2d(inception_4b_5_5_reduce,
                                       64,
                                       filter_size=5,
                                       activation='relu',
                                       name='inception_4b_5_5')

    inception_4b_pool = tflearn.max_pool_2d(inception_4a_output,
                                            kernel_size=3,
                                            strides=1,
                                            name='inception_4b_pool')
    inception_4b_pool_1_1 = tflearn.conv_2d(inception_4b_pool,
                                            64,
                                            filter_size=1,
                                            activation='relu',
                                            name='inception_4b_pool_1_1')

    inception_4b_output = tflearn.merge([
        inception_4b_1_1, inception_4b_3_3, inception_4b_5_5,
        inception_4b_pool_1_1
    ],
                                        mode='concat',
                                        axis=3,
                                        name='inception_4b_output')

    inception_4c_1_1 = tflearn.conv_2d(inception_4b_output,
                                       128,
                                       filter_size=1,
                                       activation='relu',
                                       name='inception_4c_1_1')
    inception_4c_3_3_reduce = tflearn.conv_2d(inception_4b_output,
                                              128,
                                              filter_size=1,
                                              activation='relu',
                                              name='inception_4c_3_3_reduce')
    inception_4c_3_3 = tflearn.conv_2d(inception_4c_3_3_reduce,
                                       256,
                                       filter_size=3,
                                       activation='relu',
                                       name='inception_4c_3_3')
    inception_4c_5_5_reduce = tflearn.conv_2d(inception_4b_output,
                                              24,
                                              filter_size=1,
                                              activation='relu',
                                              name='inception_4c_5_5_reduce')
    inception_4c_5_5 = tflearn.conv_2d(inception_4c_5_5_reduce,
                                       64,
                                       filter_size=5,
                                       activation='relu',
                                       name='inception_4c_5_5')

    inception_4c_pool = tflearn.max_pool_2d(inception_4b_output,
                                            kernel_size=3,
                                            strides=1)
    inception_4c_pool_1_1 = tflearn.conv_2d(inception_4c_pool,
                                            64,
                                            filter_size=1,
                                            activation='relu',
                                            name='inception_4c_pool_1_1')

    inception_4c_output = tflearn.merge([
        inception_4c_1_1, inception_4c_3_3, inception_4c_5_5,
        inception_4c_pool_1_1
    ],
                                        mode='concat',
                                        axis=3,
                                        name='inception_4c_output')

    inception_4d_1_1 = tflearn.conv_2d(inception_4c_output,
                                       112,
                                       filter_size=1,
                                       activation='relu',
                                       name='inception_4d_1_1')
    inception_4d_3_3_reduce = tflearn.conv_2d(inception_4c_output,
                                              144,
                                              filter_size=1,
                                              activation='relu',
                                              name='inception_4d_3_3_reduce')
    inception_4d_3_3 = tflearn.conv_2d(inception_4d_3_3_reduce,
                                       288,
                                       filter_size=3,
                                       activation='relu',
                                       name='inception_4d_3_3')
    inception_4d_5_5_reduce = tflearn.conv_2d(inception_4c_output,
                                              32,
                                              filter_size=1,
                                              activation='relu',
                                              name='inception_4d_5_5_reduce')
    inception_4d_5_5 = tflearn.conv_2d(inception_4d_5_5_reduce,
                                       64,
                                       filter_size=5,
                                       activation='relu',
                                       name='inception_4d_5_5')
    inception_4d_pool = tflearn.max_pool_2d(inception_4c_output,
                                            kernel_size=3,
                                            strides=1,
                                            name='inception_4d_pool')
    inception_4d_pool_1_1 = tflearn.conv_2d(inception_4d_pool,
                                            64,
                                            filter_size=1,
                                            activation='relu',
                                            name='inception_4d_pool_1_1')

    inception_4d_output = tflearn.merge([
        inception_4d_1_1, inception_4d_3_3, inception_4d_5_5,
        inception_4d_pool_1_1
    ],
                                        mode='concat',
                                        axis=3,
                                        name='inception_4d_output')

    inception_4e_1_1 = tflearn.conv_2d(inception_4d_output,
                                       256,
                                       filter_size=1,
                                       activation='relu',
                                       name='inception_4e_1_1')
    inception_4e_3_3_reduce = tflearn.conv_2d(inception_4d_output,
                                              160,
                                              filter_size=1,
                                              activation='relu',
                                              name='inception_4e_3_3_reduce')
    inception_4e_3_3 = tflearn.conv_2d(inception_4e_3_3_reduce,
                                       320,
                                       filter_size=3,
                                       activation='relu',
                                       name='inception_4e_3_3')
    inception_4e_5_5_reduce = tflearn.conv_2d(inception_4d_output,
                                              32,
                                              filter_size=1,
                                              activation='relu',
                                              name='inception_4e_5_5_reduce')
    inception_4e_5_5 = tflearn.conv_2d(inception_4e_5_5_reduce,
                                       128,
                                       filter_size=5,
                                       activation='relu',
                                       name='inception_4e_5_5')
    inception_4e_pool = tflearn.max_pool_2d(inception_4d_output,
                                            kernel_size=3,
                                            strides=1,
                                            name='inception_4e_pool')
    inception_4e_pool_1_1 = tflearn.conv_2d(inception_4e_pool,
                                            128,
                                            filter_size=1,
                                            activation='relu',
                                            name='inception_4e_pool_1_1')

    inception_4e_output = tflearn.merge([
        inception_4e_1_1, inception_4e_3_3, inception_4e_5_5,
        inception_4e_pool_1_1
    ],
                                        axis=3,
                                        mode='concat')

    pool4_3_3 = tflearn.max_pool_2d(inception_4e_output,
                                    kernel_size=3,
                                    strides=2,
                                    name='pool_3_3')

    inception_5a_1_1 = tflearn.conv_2d(pool4_3_3,
                                       256,
                                       filter_size=1,
                                       activation='relu',
                                       name='inception_5a_1_1')
    inception_5a_3_3_reduce = tflearn.conv_2d(pool4_3_3,
                                              160,
                                              filter_size=1,
                                              activation='relu',
                                              name='inception_5a_3_3_reduce')
    inception_5a_3_3 = tflearn.conv_2d(inception_5a_3_3_reduce,
                                       320,
                                       filter_size=3,
                                       activation='relu',
                                       name='inception_5a_3_3')
    inception_5a_5_5_reduce = tflearn.conv_2d(pool4_3_3,
                                              32,
                                              filter_size=1,
                                              activation='relu',
                                              name='inception_5a_5_5_reduce')
    inception_5a_5_5 = tflearn.conv_2d(inception_5a_5_5_reduce,
                                       128,
                                       filter_size=5,
                                       activation='relu',
                                       name='inception_5a_5_5')
    inception_5a_pool = tflearn.max_pool_2d(pool4_3_3,
                                            kernel_size=3,
                                            strides=1,
                                            name='inception_5a_pool')
    inception_5a_pool_1_1 = tflearn.conv_2d(inception_5a_pool,
                                            128,
                                            filter_size=1,
                                            activation='relu',
                                            name='inception_5a_pool_1_1')

    inception_5a_output_ = tflearn.merge([
        inception_5a_1_1, inception_5a_3_3, inception_5a_5_5,
        inception_5a_pool_1_1
    ],
                                         axis=3,
                                         mode='concat')
    inception_5a_output = tflearn.dropout(inception_5a_output_, 0.45)
    inception_5b_1_1 = tflearn.conv_2d(inception_5a_output,
                                       384,
                                       filter_size=1,
                                       activation='relu',
                                       name='inception_5b_1_1')
    inception_5b_3_3_reduce = tflearn.conv_2d(inception_5a_output,
                                              192,
                                              filter_size=1,
                                              activation='relu',
                                              name='inception_5b_3_3_reduce')
    inception_5b_3_3 = tflearn.conv_2d(inception_5b_3_3_reduce,
                                       384,
                                       filter_size=3,
                                       activation='relu',
                                       name='inception_5b_3_3')
    inception_5b_5_5_reduce = tflearn.conv_2d(inception_5a_output,
                                              48,
                                              filter_size=1,
                                              activation='relu',
                                              name='inception_5b_5_5_reduce')
    inception_5b_5_5 = tflearn.conv_2d(inception_5b_5_5_reduce,
                                       128,
                                       filter_size=5,
                                       activation='relu',
                                       name='inception_5b_5_5')
    inception_5b_pool = tflearn.max_pool_2d(inception_5a_output,
                                            kernel_size=3,
                                            strides=1,
                                            name='inception_5b_pool')
    inception_5b_pool_1_1 = tflearn.conv_2d(inception_5b_pool,
                                            128,
                                            filter_size=1,
                                            activation='relu',
                                            name='inception_5b_pool_1_1')
    inception_5b_output = tflearn.merge([
        inception_5b_1_1, inception_5b_3_3, inception_5b_5_5,
        inception_5b_pool_1_1
    ],
                                        axis=3,
                                        mode='concat')

    pool5_7_7 = tflearn.avg_pool_2d(inception_5b_output,
                                    kernel_size=7,
                                    strides=1)
    pool5_7_7 = tflearn.dropout(pool5_7_7, 0.65)
    loss = tflearn.fully_connected(pool5_7_7, num_class, activation='softmax')

    return loss
def run():
    X = tf.placeholder(shape=(None, 784), dtype=tf.float32)
    Y = tf.placeholder(shape=(None, 10), dtype=tf.float32)

    net = tf.reshape(X, [-1, 28, 28, 1])  # batch, height, width, chnl

    # 32 filters, each of size 3(x3)
    net = tflearn.conv_2d(net, 32, 3, activation='relu')
    # pool kernel size 2, stride size default kernel soze
    net = tflearn.max_pool_2d(net, 2)
    # for "encourage some kind of inhibition and boost the neurons with
    # relatively larger activations"
    net = tflearn.local_response_normalization(net)
    # The dropout method is introduced to prevent overfitting. At each training stage, individual nodes are either "dropped out" of the net with probability {\displaystyle 1-p} 1-p or kept with probability {\displaystyle p} p, so that a reduced network is left
    # keep_prob=0.8
    net = tflearn.dropout(net, 0.8)

    # 64 filters
    net = tflearn.conv_2d(net, 64, 3, activation='relu')
    net = tflearn.max_pool_2d(net, 2)
    net = tflearn.local_response_normalization(net)
    net = tflearn.dropout(net, 0.8)

    # FC
    net = tflearn.fully_connected(net, 128, activation='tanh')
    net = tflearn.dropout(net, 0.8)
    net = tflearn.fully_connected(net, 256, activation='tanh')
    net = tflearn.dropout(net, 0.8)
    net = tflearn.fully_connected(net, 10, activation='softmax')

    # --------------------------------------
    # really manual tf way
    # # Defining other ops using Tensorflow
    # loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(net, Y))
    # optimizer = tf.train.AdamOptimizer(learning_rate=0.01)
    # optimizer_minop = optimizer.minimize(loss)

    # # start
    # init = tf.initialize_all_variables()

    # with tf.Session() as sess:
    #     sess.run(init)
    #     batch_size = 128
    #     for epoch in range(2):
    #         avg_cost = 0.
    #         total_batch = int(mnist_data.train.num_examples/batch_size)
    #         for i in range(total_batch):
    #             batch_xs, batch_ys = mnist_data.train.next_batch(batch_size)
    #             sess.run(optimizer_minop, feed_dict={X: batch_xs, Y: batch_ys})
    #             cost = sess.run(loss, feed_dict={X: batch_xs, Y: batch_ys})
    #             avg_cost += cost/total_batch
    #             if i % 20 == 0:
    #                 print("Epoch:", '%03d' % (epoch+1), "Step:", '%03d' % i,
    #                       "Loss:", str(cost))

    # --------------------------------------
    # use trainer class
    loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(net, Y))
    optimizer = tf.train.AdamOptimizer(learning_rate=0.01)
    accuracy = tf.reduce_mean(tf.cast(
        tf.equal(tf.argmax(net, 1), tf.argmax(Y, 1)),
        tf.float32), name='acc')

    trainop = tflearn.TrainOp(loss=loss, optimizer=optimizer,
                              metric=accuracy, batch_size=128)

    trainer = tflearn.Trainer(train_ops=trainop, tensorboard_verbose=0)
    trainer.fit({X: trainX, Y: trainY}, val_feed_dicts={
                X: testX, Y: testY}, n_epoch=2, show_metric=True)
    trainer.save('models/mnist_cnn.tfl')
def run():
    X = tf.placeholder(shape=(None, 784), dtype=tf.float32)
    Y = tf.placeholder(shape=(None, 10), dtype=tf.float32)

    net = tf.reshape(X, [-1, 28, 28, 1])  # batch, height, width, chnl

    # 32 filters, each of size 3(x3)
    net = tflearn.conv_2d(net, 32, 3, activation='relu')
    # pool kernel size 2, stride size default kernel soze
    net = tflearn.max_pool_2d(net, 2)
    # for "encourage some kind of inhibition and boost the neurons with
    # relatively larger activations"
    net = tflearn.local_response_normalization(net)
    # The dropout method is introduced to prevent overfitting. At each training stage, individual nodes are either "dropped out" of the net with probability {\displaystyle 1-p} 1-p or kept with probability {\displaystyle p} p, so that a reduced network is left
    # keep_prob=0.8
    net = tflearn.dropout(net, 0.8)

    # 64 filters
    net = tflearn.conv_2d(net, 64, 3, activation='relu')
    net = tflearn.max_pool_2d(net, 2)
    net = tflearn.local_response_normalization(net)
    net = tflearn.dropout(net, 0.8)

    # FC
    net = tflearn.fully_connected(net, 128, activation='tanh')
    net = tflearn.dropout(net, 0.8)
    net = tflearn.fully_connected(net, 256, activation='tanh')
    net = tflearn.dropout(net, 0.8)
    net = tflearn.fully_connected(net, 10, activation='softmax')

    # --------------------------------------
    # really manual tf way
    # # Defining other ops using Tensorflow
    # loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(net, Y))
    # optimizer = tf.train.AdamOptimizer(learning_rate=0.01)
    # optimizer_minop = optimizer.minimize(loss)

    # # start
    # init = tf.initialize_all_variables()

    # with tf.Session() as sess:
    #     sess.run(init)
    #     batch_size = 128
    #     for epoch in range(2):
    #         avg_cost = 0.
    #         total_batch = int(mnist_data.train.num_examples/batch_size)
    #         for i in range(total_batch):
    #             batch_xs, batch_ys = mnist_data.train.next_batch(batch_size)
    #             sess.run(optimizer_minop, feed_dict={X: batch_xs, Y: batch_ys})
    #             cost = sess.run(loss, feed_dict={X: batch_xs, Y: batch_ys})
    #             avg_cost += cost/total_batch
    #             if i % 20 == 0:
    #                 print("Epoch:", '%03d' % (epoch+1), "Step:", '%03d' % i,
    #                       "Loss:", str(cost))

    # --------------------------------------
    # use trainer class
    loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(net, Y))
    optimizer = tf.train.AdamOptimizer(learning_rate=0.01)
    accuracy = tf.reduce_mean(tf.cast(
        tf.equal(tf.argmax(net, 1), tf.argmax(Y, 1)), tf.float32),
                              name='acc')

    trainop = tflearn.TrainOp(loss=loss,
                              optimizer=optimizer,
                              metric=accuracy,
                              batch_size=128)

    trainer = tflearn.Trainer(train_ops=trainop, tensorboard_verbose=0)
    trainer.fit({
        X: trainX,
        Y: trainY
    },
                val_feed_dicts={
                    X: testX,
                    Y: testY
                },
                n_epoch=2,
                show_metric=True)
    trainer.save('models/mnist_cnn.tfl')
예제 #18
0
    reduce1 = tflearn.conv_2d(input, n1, 1, activation='relu')
    reduce2 = tflearn.conv_2d(input, n2, filter_size=1, activation='relu')
    pool = tflearn.max_pool_2d(input, kernel_size=3, strides=1)
    conv1 = tflearn.conv_2d(input, n3, 1, activation='relu')
    conv2 = tflearn.conv_2d(reduce1, n4, filter_size=3, activation='relu')
    conv3 = tflearn.conv_2d(reduce2, n5, filter_size=5, activation='relu')
    conv4 = tflearn.conv_2d(pool, n6, filter_size=1, activation='relu')
    output = tflearn.merge([conv1, conv2, conv3, conv4], mode='concat', axis=3)
    return output


input = tflearn.input_data(shape=[None, 227, 227, 3],
                           data_augmentation=img_aug)
conv1 = tflearn.conv_2d(input, 64, 7, strides=2, activation='relu')
m1 = tflearn.max_pool_2d(conv1, 3, strides=2)
l1 = tflearn.local_response_normalization(m1)
conv2 = tflearn.conv_2d(l1, 64, 1, activation='relu')
conv3 = tflearn.conv_2d(conv2, 192, 3, activation='relu')
l2 = tflearn.local_response_normalization(conv3)
m2 = tflearn.max_pool_2d(l2, kernel_size=3, strides=2)
i1 = inception(m2, 96, 16, 64, 128, 32, 32)
i2 = inception(i1, 128, 32, 128, 192, 96, 64)
m3 = tflearn.max_pool_2d(i2, kernel_size=3, strides=2)
i3 = inception(m3, 96, 16, 192, 208, 48, 64)
i4 = inception(i3, 112, 24, 160, 224, 64, 64)
i5 = inception(i4, 128, 24, 128, 256, 64, 64)
i6 = inception(i5, 144, 32, 112, 288, 64, 64)
i7 = inception(i6, 160, 32, 256, 320, 128, 128)
m4 = tflearn.max_pool_2d(i7, kernel_size=3, strides=2)
i8 = inception(m4, 160, 32, 256, 320, 128, 128)
i9 = inception(i8, 192, 48, 384, 384, 128, 128)
예제 #19
0
# Using MNIST Dataset
import tflearn.datasets.mnist as mnist
mnist_data = mnist.read_data_sets(one_hot=True)

# User defined placeholders
with tf.Graph().as_default():
    # Placeholders for data and labels
    X = tf.placeholder(shape=(None, 784), dtype=tf.float32)
    Y = tf.placeholder(shape=(None, 10), dtype=tf.float32)

    net = tf.reshape(X, [-1, 28, 28, 1])

    # Using TFLearn wrappers for network building
    net = tflearn.conv_2d(net, 32, 3, activation='relu')
    net = tflearn.max_pool_2d(net, 2)
    net = tflearn.local_response_normalization(net)
    net = tflearn.dropout(net, 0.8)
    net = tflearn.conv_2d(net, 64, 3, activation='relu')
    net = tflearn.max_pool_2d(net, 2)
    net = tflearn.local_response_normalization(net)
    net = tflearn.dropout(net, 0.8)
    net = tflearn.fully_connected(net, 128, activation='tanh')
    net = tflearn.dropout(net, 0.8)
    net = tflearn.fully_connected(net, 256, activation='tanh')
    net = tflearn.dropout(net, 0.8)
    net = tflearn.fully_connected(net, 10, activation='linear')

    # Defining other ops using Tensorflow
    loss = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits_v2(logits=net, labels=Y))
    optimizer = tf.train.AdamOptimizer(learning_rate=0.01).minimize(loss)
예제 #20
0
 def conv_encode(self, input_data):
     conv1 = tflearn.conv_2d(input_data, 32, 5, activation='elu')
     pool1 = tflearn.max_pool_2d(conv1, 2)
     norm1 = tflearn.local_response_normalization(pool1)
     return tf.reshape(norm1, [self.curr_batch_size, 6272])
예제 #21
0
def main():
    '''
    Establishes architecture for CNN-fully-connected-RNN neural net.
    '''
    # Create the input layer: None: batch size, 120: frames, 80: height, 80: width, 3: RGB
    network = tflearn.input_data(shape=[None, 120, 80, 80, 3], name='input')

    # Convolutional network
    network = tflearn.conv_3d(
        network, 32, (3, 3, 3), activation='relu'
    )  # 32 conv layers of 3x3x3 (3x3x3 convolves for each 3 frames, 3px height, and 3px width)
    network = tflearn.max_pool_3d(
        network, (1, 2, 2),
        strides=(1, 2, 2))  # Pools results of the conv_3d layer
    network = tflearn.conv_3d(network, 64, (3, 3, 3),
                              activation='relu')  # 64 layers of 3x3x3
    network = tflearn.max_pool_3d(network, (1, 2, 2), strides=(1, 2, 2))
    network = tflearn.conv_3d(network, 128, (3, 3, 3),
                              activation='relu')  # 128 layers of 3x3x3
    network = tflearn.conv_3d(network, 128, (3, 3, 3),
                              activation='relu')  # another one?
    network = tflearn.max_pool_3d(network, (1, 2, 2), strides=(1, 2, 2))
    network = tflearn.conv_3d(network, 256, (2, 2, 2),
                              activation='relu')  # 256 layers of 2x2x2
    network = tflearn.conv_3d(network, 256, (2, 2, 2), activation='relu')
    network = tflearn.max_pool_3d(network, (1, 2, 2), strides=(1, 2, 2))
    network = tflearn.conv_2d(network,
                              64,
                              4,
                              activation='relu',
                              regularizer="L2")  # 64 layers of 4x4
    network = tflearn.max_pool_2d(network, 2)  # and then max pool

    # Normalize activations of the previous layer at each batch.
    network = tflearn.local_response_normalization(network)

    # And now the fully-connected neural net (128 & 256 neurons + dropout)
    network = tflearn.fully_connected(network, 128, activation='tanh')
    network = tflearn.dropout(network, 0.8)
    network = tflearn.fully_connected(network, 256, activation='tanh')
    network = tflearn.dropout(network, 0.8)
    network = tflearn.reshape(network, [-1, 1, 256])  # Why 256?

    # LSTM layers
    network = tflearn.lstm(network, 128, return_seq=True)  # LSTM of 128 units
    network = tflearn.lstm(network, 128)
    network = tflearn.fully_connected(
        network, 4, activation='softmax')  # Just four neurons... okay?
    network = tflearn.regression(network,
                                 optimizer='adam',
                                 loss='categorical_crossentropy',
                                 name='target')

    model = tflearn.DNN(network, tensorboard_verbose=0)
    X, Y = getXY()
    model.fit(X,
              Y,
              n_epoch=1,
              validation_set=0.1,
              show_metric=True,
              snapshot_step=100)
예제 #22
0
import tensorflow as tf
import tflearn
import tflearn.datasets.mnist as mnist
mnist_data = mnist.read_data_sets(one_hot=True)
with tf.Graph().as_default():
    # placeholders for data and labels
    X = tf.placeholder(shape=(None, 784), dtype=tf.float32)
    Y = tf.placeholder(shape=(None, 10), dtype=tf.float32)
    net = tf.reshape(X, [-1, 28, 28, 1])
    net = tflearn.conv_2d(net, 32, 3, activation='relu')
    net = tflearn.max_pool_2d(net, 2)
    net = tflearn.local_response_normalization(net)
    net = tflearn.dropout(net, 0.8)
    net = tflearn.conv_2d(net, 64, 3, activation='relu')
    net = tflearn.max_pool_2d(net, 2)
    net = tflearn.local_response_normalization(net)
    net = tflearn.dropout(net, 0.8)
    net = tflearn.fully_connected(net, 128, activation='tanh')
    net = tflearn.dropout(net, 0.8)
    net = tflearn.fully_connected(net, 256, activation='tanh')
    net = tflearn.dropout(net, 0.8)
    net = tflearn.fully_connected(net, 10, activation='linear')
    loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(net, Y))
    optimizer = tf.train.AdamOptimizer(learning_rate=0.01).minimize(loss)
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        batch_size = 128
        for epoch in range(2):
            avg_cost = 0
            total_batch = int(mnist_data.train.num_examples / batch_size)