def load_data():
    """
        this function loads MNIST data (downloads it if necessary)
    """
    train_x_data, train_y_data, test_x_data, test_y_data = mnist.load_data(
        one_hot=True)
    return train_x_data, train_y_data, test_x_data, test_y_data
Exemple #2
0
    def __init__(self, learning_rate=0.01, momentum=0.8):
        self.X, self.Y, self.test_x, self.test_y = mnist.load_data(
            one_hot=True)

        self.X = self.X.reshape([-1, 28, 28, 1])
        self.test_x = self.test_x.reshape([-1, 28, 28, 1])

        conv_net = input_data(shape=[None, 28, 28, 1], name='input')
        conv_net = conv_2d(conv_net,
                           20,
                           5,
                           strides=1,
                           padding='valid',
                           activation='relu')
        conv_net = max_pool_2d(conv_net, 2, strides=2, padding='valid')

        conv_net = conv_2d(conv_net,
                           50,
                           5,
                           strides=1,
                           padding='valid',
                           activation='relu')
        conv_net = max_pool_2d(conv_net, 2, strides=2, padding='valid')

        conv_net = fully_connected(conv_net, 500, activation='relu')
        #convnet = dropout(convnet, 0.8)

        conv_net = fully_connected(conv_net, 10, activation='softmax')
        momentum = tflearn.optimizers.Momentum(learning_rate=learning_rate,
                                               momentum=momentum)
        self.network = regression(conv_net,
                                  optimizer=momentum,
                                  loss='categorical_crossentropy',
                                  name='targets')
        self.model = None
Exemple #3
0
    def test_vbs1(self):

        # Temp skip test
        return

        with tf.Graph().as_default():
            # Data loading and preprocessing
            import tflearn.datasets.mnist as mnist
            X, Y, testX, testY = mnist.load_data(one_hot=True)
            X = X.reshape([-1, 28, 28, 1])
            testX = testX.reshape([-1, 28, 28, 1])
            X = X[:20, :, :, :]
            Y = Y[:20, :]
            testX = testX[:10, :, :, :]
            testY = testY[:10, :]

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

            # Training
            model = tflearn.DNN(network, tensorboard_verbose=3)
            model.fit({'input': X}, {'target': Y},
                      n_epoch=1,
                      batch_size=10,
                      validation_set=({
                          'input': testX
                      }, {
                          'target': testY
                      }),
                      validation_batch_size=5,
                      snapshot_step=10,
                      show_metric=True,
                      run_id='convnet_mnist_vbs')

            self.assertEqual(model.train_ops[0].validation_batch_size, 5)
            self.assertEqual(model.train_ops[0].batch_size, 10)
Exemple #4
0
def MnistCNN():
    X, Y, X_test, Y_test = mnist.load_data(one_hot=True)
    # print (X)
    X = X.reshape([-1, 28, 28, 1])
    # print (X)
    X_test = X_test.reshape([-1, 28, 28, 1])

    # Building the Network
    CNN = input_data(shape=[None, 28, 28, 1], name='input')
    CNN = conv_2d(CNN, 32, 5, activation='relu', regularizer="L2")
    CNN = max_pool_2d(CNN, 2)
    CNN = local_response_normalization(CNN)
    CNN = conv_2d(CNN, 64, 5, activation='relu', regularizer="L2")
    CNN = max_pool_2d(CNN, 2)
    CNN = local_response_normalization(CNN)
    CNN = fully_connected(CNN, 1024, activation=None)
    CNN = dropout(CNN, 0.5)
    CNN = fully_connected(CNN, 10, activation='softmax')
    CNN = regression(CNN, optimizer='adam', learning_rate=0.0001, loss='categorical_crossentropy', name='target')

    # train
    model = tflearn.DNN(CNN,
                        tensorboard_verbose=0,
                        tensorboard_dir='MINST_tflearn_board/',
                        checkpoint_path='MINST_tflearn_checkpoints/checkpoints')
    model.fit({'input': X}, {'target': Y},
        n_epoch=3,
        validation_set=({'input': X_test},{'target':Y_test}),
        show_metric=True,
        snapshot_step=1000,
        run_id='convnet_mnist')
Exemple #5
0
def get_DNN():
    X, Y, testX, testY = mnist.load_data(one_hot=True)

    # Building deep neural network
    input_layer = tflearn.input_data(shape=[None, 784])
    dense1 = tflearn.fully_connected(input_layer,
                                     64,
                                     activation='tanh',
                                     regularizer='L2',
                                     weight_decay=0.001)
    dropout1 = tflearn.dropout(dense1, 0.8)
    dense2 = tflearn.fully_connected(dropout1,
                                     64,
                                     activation='tanh',
                                     regularizer='L2',
                                     weight_decay=0.001)
    dropout2 = tflearn.dropout(dense2, 0.8)
    softmax = tflearn.fully_connected(dropout2, 10, activation='softmax')

    # Regression using SGD with learning rate decay and Top-3 accuracy
    sgd = tflearn.SGD(learning_rate=0.1, lr_decay=0.96, decay_step=1000)
    top_k = tflearn.metrics.Top_k(3)
    net = tflearn.regression(softmax,
                             optimizer=sgd,
                             metric=top_k,
                             loss='categorical_crossentropy')

    # Training
    model = tflearn.DNN(net, tensorboard_verbose=0)
    model.fit(X,
              Y,
              n_epoch=20,
              validation_set=(testX, testY),
              show_metric=True,
              run_id="dense_model")
Exemple #6
0
def cnn():
    X, Y, testX, testY = mnist.load_data(one_hot=True)
    X = X.reshape([-1, 28, 28, 1])
    testX = testX.reshape([-1, 28, 28, 1])

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

    # Training
    model = tflearn.DNN(network, tensorboard_verbose=0)
    model.fit({'input': X}, {'target': Y}, n_epoch=20,
               validation_set=({'input': testX}, {'target': testY}),
               snapshot_step=100, show_metric=True, run_id='cnn_demo')
def learn():
    import tflearn.datasets.mnist as mnist
    X, Y, testX, testY = mnist.load_data(one_hot=True)

    d = AutoEncoder(784, [784 * 2], 64)
    d.learn(X, testX)
    d.save()
Exemple #8
0
 def __init__(self):
     mnist_data = mnist.load_data(one_hot = True)
     self.training_images  = self.create_dataset (mnist_data[:2])
     self.testing_images   = self.create_dataset (mnist_data[2:])
     
     self.image_size     = 28 * 28 #--- size of input (mnist images are 28x28)
     self.num_categories = 10      #--- size of output (images can be either 0, 1, 2, 3, 4, 5, 6, 7, 8 9)
     
     return
Exemple #9
0
def run_mnist():
    X, Y, testX, testY = mnist.load_data(one_hot=True)
    g = tflearn.input_data(shape=[None, 784], name='input')
    g = tflearn.fully_connected(g, 128, name='dense1')
    g = tflearn.fully_connected(g, 256, name='dense2')
    g = tflearn.fully_connected(g, 10, activation='softmax', name='softmax')
    g = tflearn.regression(g,
                           optimizer='adam',
                           learning_rate=0.001,
                           loss='categorical_crossentropy')

    if not os.path.isdir('models'):
        os.mkdir('models')
    m = tflearn.DNN(g, checkpoint_path='models/model.tfl.ckpt')
    m.fit(
        X,
        Y,
        n_epoch=1,
        validation_set=(testX, testY),
        show_metric=True,
        # Snapshot (save & evaluate) model every epoch.
        snapshot_epoch=True,
        # Snapshot (save & evalaute) model every 500 steps.
        snapshot_step=500,
        run_id='model_and_weights')
    m.save('models/mnist.tfl')

    # # load from file or ckpt and continue training
    # m.load('models/mnist.tfl')
    # # m.load('models/mnist.tfl.ckpt-500')
    # m.fit(X, Y, n_epoch=1,
    #       validation_set=(testX, testY),
    #       show_metric=True,
    #       # Snapshot (save & evaluate) model every epoch.
    #       snapshot_epoch=True,
    #       # Snapshot (save & evalaute) model every 500 steps.
    #       snapshot_step=500,
    #       run_id='model_and_weights')

    # retrieve layer by name, print weights
    dense1_vars = tflearn.variables.get_layer_variables_by_name('dense1')
    print('Dense1 layer weights:')
    print(m.get_weights(dense1_vars[0]))
    # or using generic tflearn function
    print('Dense1 layer biases:')
    with m.session.as_default():
        print(tflearn.variables.get_value(dense1_vars[1]))

    # or can even retrieve using attr `W` or `b`!
    print('Dense2 layer weights:')
    dense2 = tflearn.get_layer_by_name('dense2')
    print(dense2)
    print(m.get_weights(dense2.W))
    print('Dense2 layer biases:')
    with m.session.as_default():
        print(tflearn.variables.get_value(dense2.b))
Exemple #10
0
def test_with_mnist():
    import tflearn.datasets.mnist as mnist

    X, Y, testX, testY = mnist.load_data(one_hot=True)
    X = X.reshape([-1, 28, 28, 1])
    testX = testX.reshape([-1, 28, 28, 1])

    model = get_cnn_model(width=28, height=28, depth=1)
    model.fit({'input': X}, {'target': Y}, n_epoch=20,
                validation_set=({'input': testX}, {'target': testY}),
                snapshot_step=100, show_metric=True, run_id='cnn_mnist')
Exemple #11
0
def train_rec():
    X, Y, X_test, Y_test = mnist.load_data(one_hot=True)
    X = X.reshape([-1, 28, 28, 1])
    X_test = X_test.reshape([-1, 28, 28, 1])
    #X, Y = shuffle(X, Y)
    #Y = to_categorical(Y,10)
    #Y_test = to_categorical(Y_test,10)

    # Real-time data preprocessing
    img_prep = ImagePreprocessing()
    img_prep.add_featurewise_zero_center()
    img_prep.add_featurewise_stdnorm()

    # Real-time data augmentation
    img_aug = ImageAugmentation()
    #img_aug.add_random_flip_leftright()
    img_aug.add_random_rotation(max_angle=25.)

    # Convolutional network building
    inputs = input_data(
        shape=[None, 28, 28, 1],
        data_preprocessing=img_prep,
        data_augmentation=img_aug,
        name="inputs")
    network = conv_2d(inputs, 32, 3, activation='relu')
    network = max_pool_2d(network, 2)
    network = conv_2d(network, 64, 3, activation='relu')
    network = conv_2d(network, 64, 3, activation='relu')
    network = max_pool_2d(network, 2)

    network = fully_connected(network, 128, activation='relu')
    network = fully_connected(network, 256, activation='relu')
    network = dropout(network, 0.6)
    network = fully_connected(network, 10, activation='relu')
    network = regression(
        network,
        optimizer='adam',
        loss='categorical_crossentropy',
        learning_rate=0.001)

    # Train using classifier
    model = tflearn.DNN(network, tensorboard_verbose=3)
    global rec_input, rec_network
    rec_input, rec_network = inputs, network
    model.fit(
        X,
        Y,
        n_epoch=20,
        shuffle=True,
        validation_set=(X_test, Y_test),
        show_metric=True,
        batch_size=128,
        run_id='mnist')
    return model
    def load_data(self, ds):
        _ds = None
        if ds['name'] == 'mnist':
            from tflearn.datasets import mnist as _ds
            self._X, self._Y, self._test_X, self._test_Y = _ds.load_data(
                one_hot=ds.get('one_hot', False))

        if ds['name'] == 'cifar10':
            from tflearn.datasets import cifar10 as _ds
            (self._X, self._Y), (self._test_X, self._test_Y) = _ds.load_data(
                one_hot=ds.get('one_hot', False))
        from tflearn.data_utils import shuffle, to_categorical
        del _ds  # discard
        if 'reshape' in ds: self.reshape(ds['reshape'])
        if ds.get('shuffle', False):
            self._X, self._Y = shuffle(self._X, self._Y)

        if ds.get('to_categorical', False):
            self._Y = to_categorical(self._Y, None)
            self._test_Y = to_categorical(self._test_Y, None)
        return self
def run_mnist():
    X, Y, testX, testY = mnist.load_data(one_hot=True)
    g = tflearn.input_data(shape=[None, 784], name='input')
    g = tflearn.fully_connected(g, 128, name='dense1')
    g = tflearn.fully_connected(g, 256, name='dense2')
    g = tflearn.fully_connected(g, 10, activation='softmax', name='softmax')
    g = tflearn.regression(
        g, optimizer='adam',
        learning_rate=0.001,
        loss='categorical_crossentropy')

    if not os.path.isdir('models'):
        os.mkdir('models')
    m = tflearn.DNN(g, checkpoint_path='models/model.tfl.ckpt')
    m.fit(X, Y, n_epoch=1,
          validation_set=(testX, testY),
          show_metric=True,
          # Snapshot (save & evaluate) model every epoch.
          snapshot_epoch=True,
          # Snapshot (save & evalaute) model every 500 steps.
          snapshot_step=500,
          run_id='model_and_weights')
    m.save('models/mnist.tfl')

    # # load from file or ckpt and continue training
    # m.load('models/mnist.tfl')
    # # m.load('models/mnist.tfl.ckpt-500')
    # m.fit(X, Y, n_epoch=1,
    #       validation_set=(testX, testY),
    #       show_metric=True,
    #       # Snapshot (save & evaluate) model every epoch.
    #       snapshot_epoch=True,
    #       # Snapshot (save & evalaute) model every 500 steps.
    #       snapshot_step=500,
    #       run_id='model_and_weights')

    # retrieve layer by name, print weights
    dense1_vars = tflearn.variables.get_layer_variables_by_name('dense1')
    print('Dense1 layer weights:')
    print(m.get_weights(dense1_vars[0]))
    # or using generic tflearn function
    print('Dense1 layer biases:')
    with m.session.as_default():
        print(tflearn.variables.get_value(dense1_vars[1]))

    # or can even retrieve using attr `W` or `b`!
    print('Dense2 layer weights:')
    dense2 = tflearn.get_layer_by_name('dense2')
    print(dense2)
    print(m.get_weights(dense2.W))
    print('Dense2 layer biases:')
    with m.session.as_default():
        print(tflearn.variables.get_value(dense2.b))
def main():
    trainX, trainY, testX, testY = mnist.load_data(one_hot=True)

    nn = build_nn(trainX.shape[1])

    error, iteration = nn.train(trainX, trainY, 2)
    print('Epoch: {}, Lost: {}'.format(iteration, np.mean(error[-4:])))

    preds = np.array(nn.predict(testX)).argmax(axis=1).flatten()
    actual = testY.argmax(axis=1)
    test_accuracy = np.mean(preds == actual, axis=0)

    print("Test accuracy: {}".format(test_accuracy))
Exemple #15
0
def get_data():
    x, y, test_x, test_y = mnist.load_data(one_hot=True)
    x, y = shuffle(x, y)
    test_x, test_y = shuffle(test_x, test_y)
    train_x = x[0:50000]
    train_y = y[0:50000]
    valid_x = x[50000:]
    valid_y = y[50000:]
    # make sure you reshape the training and testing
    # data as follows.
    train_x = train_x.reshape([-1, 28, 28, 1])
    test_x = test_x.reshape([-1, 28, 28, 1])
    return train_x, train_y, test_x, test_y, valid_x, valid_y
Exemple #16
0
def init():
    learning_rate = 0.01
    classes = 10  # digits

    # Fetch inputs
    X, Y,testX, testY = mnist.load_data(one_hot=True)
    testX = testX.reshape([-1, 28, 28, 1])

    # Instantiante model for testing
    model = models.create_model(learning_rate, [None, 28, 28, 1], 10, dir)

    model.load(dir + "/checkpoints/step-17200")
    evaluate_model_accuracy(model, testX, testY)
Exemple #17
0
def main():
    """Main Function.

    Display data prediction from tensorflow model

    """
    # Initialize key variables
    epochs_to_try = 3
    start = int(time.time())

    # Define the image width and height of images
    width = 28
    height = 28

    # Get the data from mnist
    vectors, classes, test_x, test_y = mnist.load_data(one_hot=True)

    # Print useful information
    print('Vectors Shape / Type', vectors.shape, type(vectors))
    print('Classes Shape / Type', classes.shape, type(classes))
    print('First Vector', vectors[0])
    print('First Class', classes[0])

    # Reshape the test and live vectors
    vectors = vectors.reshape([-1, width, height, 1])
    test_x = test_x.reshape([-1, width, height, 1])

    # Print useful information
    print('Reshaped Vector Shape', vectors.shape)
    # print('First Reshaped Vector', vectors[0])
    # sys.exit()

    model = tflearn.DNN(convolutional_neural_network())
    model.fit(
        {'input': vectors},
        {'targets': classes},
        n_epoch=epochs_to_try,
        validation_set=({'input': test_x}, {'targets': test_y}),
        snapshot_step=500,
        show_metric=True,
        run_id='mnist')

    print(
        np.round(model.predict([test_x[1]])),
        model.predict([test_x[1]])
    )
    print(test_y[1])

    #
    print('Duration:', int(time.time() - start))
Exemple #18
0
def main():
    """Main Function.

    Display data prediction from tensorflow model

    """
    # Initialize key variables
    epochs_to_try = 3
    start = int(time.time())

    # Define the image width and height of images
    width = 28
    height = 28

    # Get the data from mnist
    vectors, classes, test_x, test_y = mnist.load_data(one_hot=True)

    # Print useful information
    print('Vectors Shape / Type', vectors.shape, type(vectors))
    print('Classes Shape / Type', classes.shape, type(classes))
    print('First Vector', vectors[0])
    print('First Class', classes[0])

    # Reshape the test and live vectors
    vectors = vectors.reshape([-1, width, height, 1])
    test_x = test_x.reshape([-1, width, height, 1])

    # Print useful information
    print('Reshaped Vector Shape', vectors.shape)
    # print('First Reshaped Vector', vectors[0])
    # sys.exit()

    model = tflearn.DNN(convolutional_neural_network())
    model.fit({'input': vectors}, {'targets': classes},
              n_epoch=epochs_to_try,
              validation_set=({
                  'input': test_x
              }, {
                  'targets': test_y
              }),
              snapshot_step=500,
              show_metric=True,
              run_id='mnist')

    print(np.round(model.predict([test_x[1]])), model.predict([test_x[1]]))
    print(test_y[1])

    #
    print('Duration:', int(time.time() - start))
Exemple #19
0
def init():
    dropout = 0.8
    learning_rate = 0.01
    run_id = 'mnist_cnn_' + str(int(time.time()))

    X, Y, testX, testY = mnist.load_data(one_hot=True)
    X = X.reshape([-1, 28, 28, 1])
    testX = testX.reshape([-1, 28, 28, 1])

    # Instantiante model for training
    model = models.create_model(learning_rate, [None, 28, 28, 1], 10, dir, drop=dropout)

    # evaluate_model_accuracy(model, testX, testY)
    model.fit({'input': X}, {'target': Y}, n_epoch=20,
              validation_set=({'input': testX}, {'target': testY}), show_metric=True, run_id=run_id)
    model.save(dir + "/saveMnist_trained")
Exemple #20
0
def main(unused_argv):
    #global rec_model, old_graph, new_graph
    rec_model = train_rec()
    exit()

    X, Y, X_test, Y_test = mnist.load_data(one_hot=True)

    img_prep = ImagePreprocessing()
    img_aug = ImageAugmentation()

    inputs = input_data(
        shape=[None, 28, 28, 1],
        data_preprocessing=img_prep,
        data_augmentation=img_aug)
    # Classifier
    classifier_fc1 = fully_connected(
        inputs, 128, activation='relu', name="clssfr_fc1")
    classifier_fc2 = fully_connected(
        inputs, 128, activation='relu', name="clssfr_fc2")
    classifier_fc3 = fully_connected(
        inputs, 10, activation='relu', name="clssfr_fc3")
    classifier_out = regression(
        classifier_fc3, optimizer='adam', loss=new_cost, learning_rate=0.001)
    print(classifier_out)
    exit()

    # Confuser
    confuser_fc1 = fully_connected(inputs, 128, activation='relu')
    confuser_fc2 = fully_connected(confuser_fc1, 128, activation='relu')
    confuser_fc3 = fully_connected(confuser_fc2, 28 * 28, activation='relu')
    confuser_out = regression(
        confuser_fc3, optimizer='adam', loss=new_cost, learning_rate=0.001)

    # TODO: GAN
    new_model = tflearn.DNN(new_network, tensorboard_verbose=0)
    new_model.fit(
        X,
        Y,
        n_epoch=5,
        shuffle=False,
        validation_set=(X_test, Y_test),
        show_metric=True,
        batch_size=1,
        run_id='mnist_counter')

    pass
Exemple #21
0
def demo():
    import tflearn.datasets.mnist as mnist
    x, y, test_x, test_y = mnist.load_data(one_hot='True')
    print(x.shape)

    x = x.reshape([-1, 28, 28, 1])
    test_x = test_x.reshape([-1, 28, 28, 1])

    # 按功能划分的零中心将每个样本的中心置零,并指定平均值。如果未指定,则对所有样品评估平均值。
    # Returns : A numpy array with same shape as input. Or a tuple (array, mean) if no mean value was specified.
    x, mean = du.featurewise_zero_center(x)
    test_x = du.featurewise_zero_center(test_x, mean)

    net = tflearn.input_data(shape=[None, 28, 28, 1])
    net = tflearn.conv_2d(net, 64, 3, activation='relu', bias=False)

    net = tflearn.residual_bottleneck(net, 3, 16, 64)
    net = tflearn.residual_bottleneck(net, 1, 32, 128, downsample=True)
    net = tflearn.residual_bottleneck(net, 2, 32, 128)
    net = tflearn.residual_bottleneck(net, 1, 64, 256, downsample=True)
    net = tflearn.residual_bottleneck(net, 2, 64, 256)
    net = tflearn.batch_normalization(net)
    net = tflearn.activation(net, 'relu')
    net = tflearn.global_avg_pool(net)
    # Regression
    net = tflearn.fully_connected(net, 10, activation='softmax')
    net = tflearn.regression(net,
                             optimizer='momentum',
                             loss='categorical_crossentropy',
                             learning_rate=0.1)
    # Training
    model = tflearn.DNN(net,
                        checkpoint_path='model_resnet_mnist',
                        max_checkpoints=10,
                        tensorboard_verbose=0)
    model.fit(x,
              y,
              n_epoch=100,
              validation_set=(test_x, test_y),
              show_metric=True,
              batch_size=256,
              run_id='resnet_mnist')
Exemple #22
0
def load_mnist_dataset(data_dir=None):
    import tflearn.datasets.mnist as mnist

    HEIGHT = 28
    WIDTH = 28
    CHANNELS = 1
    CLASSES = 10

    X, Y, Xv, Yv = mnist.load_data(data_dir=data_dir, one_hot=True)
    X, Y = shuffle(X, Y)
    Xv, Yv = shuffle(Xv, Yv)
    X = X.reshape([-1, 28, 28, 1])
    Xv = Xv.reshape([-1, 28, 28, 1])

    Xt = Xv[2000:]
    Yt = Yv[2000:]

    Xv = Xv[:2000]
    Yv = Yv[:2000]

    return CLASSES, X, Y, HEIGHT, WIDTH, CHANNELS, Xv, Yv, Xt, Yt
    def test_vbs1(self):

        with tf.Graph().as_default():
            # Data loading and preprocessing
            import tflearn.datasets.mnist as mnist
            X, Y, testX, testY = mnist.load_data(one_hot=True)
            X = X.reshape([-1, 28, 28, 1])
            testX = testX.reshape([-1, 28, 28, 1])
            X = X[:20, :, :, :]
            Y = Y[:20, :]
            testX = testX[:10, :, :, :]
            testY = testY[:10, :]
            
            # Building convolutional network
            network = input_data(shape=[None, 28, 28, 1], name='input')
            network = conv_2d(network, 32, 3, activation='relu', regularizer="L2")
            network = max_pool_2d(network, 2)
            network = local_response_normalization(network)
            network = conv_2d(network, 64, 3, activation='relu', regularizer="L2")
            network = max_pool_2d(network, 2)
            network = local_response_normalization(network)
            network = fully_connected(network, 128, activation='tanh')
            network = dropout(network, 0.8)
            network = fully_connected(network, 256, activation='tanh')
            network = dropout(network, 0.8)
            network = fully_connected(network, 10, activation='softmax')
            network = regression(network, optimizer='adam', learning_rate=0.01,
                                 loss='categorical_crossentropy', name='target')
            
            # Training
            model = tflearn.DNN(network, tensorboard_verbose=3)
            model.fit({'input': X}, {'target': Y}, n_epoch=1,
                      batch_size=10,
                      validation_set=({'input': testX}, {'target': testY}),
                      validation_batch_size=5,
                      snapshot_step=10, show_metric=True, run_id='convnet_mnist_vbs')
    
            self.assertEqual(model.train_ops[0].validation_batch_size, 5)
            self.assertEqual(model.train_ops[0].batch_size, 10)
      learning applied to document recognition." Proceedings of the IEEE,
      86(11):2278-2324, November 1998.
Links:
    - [Deep Residual Network](http://arxiv.org/pdf/1512.03385.pdf)
    - [MNIST Dataset](http://yann.lecun.com/exdb/mnist/)
"""

from __future__ import division, print_function, absolute_import

import tensorflow as tf
import tflearn
import tflearn.data_utils as du

# Data loading and preprocessing
import tflearn.datasets.mnist as mnist
X, Y, testX, testY = mnist.load_data(dirname='/home/jiawei/dataset/mnist/', one_hot=True)
X = X.reshape([-1, 28, 28, 1])
testX = testX.reshape([-1, 28, 28, 1])
X, mean = du.featurewise_zero_center(X)
testX = du.featurewise_zero_center(testX, mean)

# Building Residual Network
net = tflearn.input_data(shape=[None, 28, 28, 1])
net = tflearn.conv_2d(net, 64, 3, activation='relu', bias=False)
# Residual blocks
net = tflearn.residual_bottleneck(net, 3, 16, 64)
net = tflearn.residual_bottleneck(net, 1, 32, 128, downsample=True)
net = tflearn.residual_bottleneck(net, 2, 32, 128)
net = tflearn.residual_bottleneck(net, 1, 64, 256, downsample=True)
net = tflearn.residual_bottleneck(net, 2, 64, 256)
net = tflearn.batch_normalization(net)
Exemple #25
0
import tflearn
import torch.nn as nn
import torch.optim as optim
import matplotlib.pyplot as plt
from torch.autograd import Variable
import torch.nn.functional as F
from torch.utils import data
from tensorboardX import SummaryWriter
from progressbar import ProgressBar
from datetime import datetime
import os
from time import time

# import MNIST dataset
import tflearn.datasets.mnist as mnist
X, Y, testX, testY = mnist.load_data(one_hot=False)
print(X.shape, testX.shape)

# instantiate the tensorboard summary writers
now = datetime.now()
name = 'DDM_FC_{}/{}/{}_{}:{}'.format(now.month, now.day, now.year, now.hour, now.minute)
writer = SummaryWriter(os.path.join('runs', name))
n_classes = 10
test_batch_size = 32

# just take two classes for now
X, testX = X[Y<n_classes, :], testX[testY<n_classes, ...]
Y, testY = Y[Y<n_classes], testY[testY<n_classes]

# standardize each feature
X_mean, X_std = np.mean(X, 0), np.std(X, 0)
Exemple #26
0
# -*- coding:utf-8 -*-
from __future__ import division, print_function, absolute_import

import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
import tflearn

# Data loading and preprocessing
import tflearn.datasets.mnist as mnist
X, Y, testX, testY = mnist.load_data()

image_dim = 784  # 28*28 pixels
z_dim = 200  # Noise data points
total_samples = len(X)


# Generator
def generator(x, reuse=False):
    with tf.variable_scope('Generator', reuse=reuse):
        x = tflearn.fully_connected(x, 256, activation='relu')
        x = tflearn.fully_connected(x, image_dim, activation='sigmoid')
        return x


# Discriminator
def discriminator(x, reuse=False):
    with tf.variable_scope('Discriminator', reuse=reuse):
        x = tflearn.fully_connected(x, 256, activation='relu')
        x = tflearn.fully_connected(x, 1, activation='sigmoid')
        return x
Exemple #27
0
Links:
    - [DCGAN Paper](https://arxiv.org/abs/1511.06434).

"""

from __future__ import division, print_function, absolute_import

import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
import tflearn

# Data loading and preprocessing
import tflearn.datasets.mnist as mnist
X, Y, testX, testY = mnist.load_data()
X = np.reshape(X, newshape=[-1, 28, 28, 1])

z_dim = 200 # Noise data points
total_samples = len(X)


# Generator
def generator(x, reuse=False):
    with tf.variable_scope('Generator', reuse=reuse):
        x = tflearn.fully_connected(x, n_units=7 * 7 * 128)
        x = tflearn.batch_normalization(x)
        x = tf.nn.tanh(x)
        x = tf.reshape(x, shape=[-1, 7, 7, 128])
        x = tflearn.upsample_2d(x, 2)
        x = tflearn.conv_2d(x, 64, 5, activation='tanh')
Exemple #28
0
import tflearn
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.estimator import regression
import tflearn.datasets.mnist as mnist

#tflearn.reset_default_graph()

X, Y, test_x, test_y = mnist.load_data(one_hot=True)

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

convnet = input_data(shape=[None, 28, 28, 1], name='input')

convnet = conv_2d(convnet, 32, 2, activation='relu')
convnet = max_pool_2d(convnet, 2)

convnet = conv_2d(convnet, 64, 2, activation='relu')
convnet = max_pool_2d(convnet, 2)

convnet = fully_connected(convnet, 1024, activation='relu')
convnet = dropout(convnet, 0.8)

convnet = fully_connected(convnet, 10, activation='softmax')
convnet = regression(convnet, optimizer='adam', learning_rate=0.01, loss='categorical_crossentropy', name='targets')

model = tflearn.DNN(convnet)
'''
model.fit({'input': X}, {'targets': Y}, n_epoch=10, validation_set=({'input': test_x}, {'targets': test_y}), snapshot_step=500, show_metric=True, run_id='mnist')
Exemple #29
0
import tflearn
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.estimator import regression
import tflearn.datasets.mnist as mnist

X, Y, testX, testY = mnist.load_data(one_hot=True)

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

convNet = input_data(shape=[None, 28, 28, 1], name='input')
convNet = conv_2d(convNet, 32, 2, activation='relu')
convNet = max_pool_2d(convNet, 2)

convNet = conv_2d(convNet, 64, 2, activation='relu')
convNet = max_pool_2d(convNet, 2)

convNet = fully_connected(convNet, 1024, activation='relu')
convNet = dropout(convNet, 0.8)

convNet = fully_connected(convNet, 10, activation='softmax')
convNet = regression(convNet,
                     optimizer='adam',
                     learning_rate=0.01,
                     loss='categorical_crossentropy',
                     name='targets')

model = tflearn.DNN(convNet)

model.fit({'input': X}, {'targets': Y},
    def test_vm1(self):

        with tf.Graph().as_default():
            # Data loading and preprocessing
            import tflearn.datasets.mnist as mnist
            X, Y, testX, testY = mnist.load_data(one_hot=True)
            X = X.reshape([-1, 28, 28, 1])
            testX = testX.reshape([-1, 28, 28, 1])
            X = X[:10, :, :, :]
            Y = Y[:10, :]
            
            # Building convolutional network
            network = input_data(shape=[None, 28, 28, 1], name='input')
            network = conv_2d(network, 32, 3, activation='relu', regularizer="L2")
            network = max_pool_2d(network, 2)
            network = local_response_normalization(network)
            network = conv_2d(network, 64, 3, activation='relu', regularizer="L2")
            network = max_pool_2d(network, 2)
            network = local_response_normalization(network)
            network = fully_connected(network, 128, activation='tanh')
            network = dropout(network, 0.8)
            network = fully_connected(network, 256, activation='tanh')
            network = dropout(network, 0.8)
    
            # construct two varaibles to add as additional "valiation monitors"
            # these varaibles are evaluated each time validation happens (eg at a snapshot)
            # and the results are summarized and output to the tensorboard events file,
            # together with the accuracy and loss plots.
            #
            # Here, we generate a dummy variable given by the sum over the current
            # network tensor, and a constant variable.  In practice, the validation
            # monitor may present useful information, like confusion matrix
            # entries, or an AUC metric.
            with tf.name_scope('CustomMonitor'):
                test_var = tf.reduce_sum(tf.cast(network, tf.float32), name="test_var")
                test_const = tf.constant(32.0, name="custom_constant")
    
            print ("network=%s, test_var=%s" % (network, test_var))
            network = fully_connected(network, 10, activation='softmax')
            network = regression(network, optimizer='adam', learning_rate=0.01,
                                 loss='categorical_crossentropy', name='target', validation_monitors=[test_var, test_const])
            
            # Training
            model = tflearn.DNN(network, tensorboard_verbose=3)
            model.fit({'input': X}, {'target': Y}, n_epoch=1,
                       validation_set=({'input': testX}, {'target': testY}),
                       snapshot_step=10, show_metric=True, run_id='convnet_mnist')
            
            # check for validation monitor variables
            ats = tf.get_collection("Adam_testing_summaries")
            print ("ats=%s" % ats)
            self.assertTrue(len(ats)==4)	# should be four variables being summarized: [loss, test_var, test_const, accuracy]
            
            session = model.session
            print ("session=%s" % session)
            trainer = model.trainer
            print ("train_ops = %s" % trainer.train_ops)
            top = trainer.train_ops[0]
            vmtset = top.validation_monitors_T
            print ("validation_monitors_T = %s" % vmtset)
            with model.session.as_default():
                ats_var_val = tflearn.variables.get_value(vmtset[0])
                ats_const_val = tflearn.variables.get_value(vmtset[1])
            print ("summary values: var=%s, const=%s" % (ats_var_val, ats_const_val))
            self.assertTrue(ats_const_val==32)	# test to make sure the constant made it through
import tflearn
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.estimator import regression

import tflearn.datasets.mnist as mnist
# pip install tflearn

trainX, trainY, testX, testY = mnist.load_data(
    data_dir="../datasets/MNIST_data", one_hot=True)
# 将图像数据resize成卷积卷积神经网络输入的格式。
trainX = trainX.reshape([-1, 28, 28, 1])
testX = testX.reshape([-1, 28, 28, 1])

# 构建神经网络。
net = input_data(shape=[None, 28, 28, 1], name='input')
net = conv_2d(net, 32, 5, activation='relu')
net = max_pool_2d(net, 2)
net = conv_2d(net, 64, 5, activation='relu')
net = max_pool_2d(net, 2)
net = fully_connected(net, 500, activation='relu')
net = fully_connected(net, 10, activation='softmax')

# 定义学习任务。指定优化器为sgd,学习率为0.01,损失函数为交叉熵
net = regression(net,
                 optimizer='sgd',
                 learning_rate=0.01,
                 loss='categorical_crossentropy')

model = tflearn.DNN(net, tensorboard_verbose=0)
model.fit(trainX,
Exemple #32
0
def load_the_data(which = 'notmnist'):

	if which == 'mnist':
		import tflearn.datasets.mnist as mnist
		train_dataset, train_labels, test_dataset, test_labels = mnist.load_data(one_hot=True)
		print('Training set', train_dataset.shape, train_labels.shape)
		print('Test set', test_dataset.shape, test_labels.shape)
		return train_dataset, train_labels, test_dataset, test_labels

	elif which == 'kaggle':
		num_labels = 2
		print "Loading data..."
		feature_names = ['L3_S38_F3960', 'L3_S33_F3865', 'L3_S38_F3956', 'L3_S33_F3857',
	       'L3_S29_F3321', 'L1_S24_F1846', 'L3_S32_F3850', 'L3_S29_F3354',
	       'L3_S29_F3324', 'L3_S35_F3889', 'L0_S1_F28', 'L1_S24_F1844',
	       'L3_S29_F3376', 'L0_S0_F22', 'L3_S33_F3859', 'L3_S38_F3952', 
	       'L3_S30_F3754', 'L2_S26_F3113', 'L3_S30_F3759', 'L0_S5_F114']

		numeric_cols = pd.read_csv("../input/train_numeric.csv", nrows = 1).columns.values
		imp_idxs = [np.argwhere(feature_name == numeric_cols)[0][0] for feature_name in feature_names]
		train = pd.read_csv("../input/train_numeric.csv", 
		                index_col = 0, header = 0, usecols = [0, len(numeric_cols) - 1] + imp_idxs)

		numeric_cols_test = pd.read_csv("../input/test_numeric.csv", nrows = 1).columns.values
		imp_idxs = [np.argwhere(feature_name == numeric_cols_test)[0][0] for feature_name in feature_names]
		test = pd.read_csv("../input/test_numeric.csv", 
		                index_col = 0, header = 0, usecols = [0, len(numeric_cols_test) - 1] + imp_idxs)

		print "Using imputer"
		imputer = Imputer()

		total_train_dataset = train[feature_names].values[:50000].astype(np.float32)
		raw_total_train_labels = train['Response'].values[:50000].astype(np.float32)
		test_kaggle_dataset = test[feature_names].values[:50000].astype(np.float32)
		print ("...Data loaded !")
		print ("...Split train / test")

		print "split train test"
		sss = StratifiedShuffleSplit(raw_total_train_labels, 2, test_size=0.2, random_state=0)
		for train_index, test_index in sss:
		    train_dataset, test_dataset = total_train_dataset[train_index], total_train_dataset[test_index]
		    raw_train_labels, raw_test_labels = raw_total_train_labels[train_index], raw_total_train_labels[test_index]

		def reformat(labels):
		  # Map 2 to [0.0, 1.0, 0.0 ...], 3 to [0.0, 0.0, 1.0 ...]
		  labels = (np.arange(num_labels) == labels[:,None]).astype(np.float32)
		  return labels

		train_labels = reformat(raw_train_labels)
		test_labels = reformat(raw_test_labels)


		print ("Train dataset shape %i %i" % (train_dataset.shape[0], train_dataset.shape[1]))
		print ("Test dataset shape %i %i" % (test_dataset.shape[0], test_dataset.shape[1]))
		print ("Train labels shape %i %i" % (train_labels.shape[0], train_labels.shape[1]))
		print ("Test labels shape %i %i" % (test_labels.shape[0], test_labels.shape[1]))
		return train_dataset, train_labels, test_dataset, test_labels


	elif which == 'notmnist':
		num_labels = 10
		print ("loading data...")

		pickle_file = 'notMNIST.pickle'

		with open(pickle_file, 'rb') as f:
		  save = pickle.load(f)
		  train_dataset = save['train_dataset']
		  train_labels = save['train_labels']
		  valid_dataset = save['valid_dataset']
		  valid_labels = save['valid_labels']
		  test_dataset = save['test_dataset']
		  test_labels = save['test_labels']
		  del save  # hint to help gc free up memory
		  print('Training set', train_dataset.shape, train_labels.shape)
		  print('Validation set', valid_dataset.shape, valid_labels.shape)
		  print('Test set', test_dataset.shape, test_labels.shape)


		def reformat(dataset, labels):
		  dataset = dataset.reshape((-1, 784)).astype(np.float32)
		  # Map 2 to [0.0, 1.0, 0.0 ...], 3 to [0.0, 0.0, 1.0 ...]
		  labels = (np.arange(num_labels) == labels[:,None]).astype(np.float32)
		  return dataset, labels
		train_dataset, train_labels = reformat(train_dataset, train_labels)
		valid_dataset, valid_labels = reformat(valid_dataset, valid_labels)
		test_dataset, test_labels = reformat(test_dataset, test_labels)
		print('Training set', train_dataset.shape, train_labels.shape)
		print('Validation set', valid_dataset.shape, valid_labels.shape)
		print('Test set', test_dataset.shape, test_labels.shape)
		return train_dataset, train_labels, test_dataset, test_labels
Exemple #33
0
noise distribution.
References:
    - Generative adversarial nets. I Goodfellow, J Pouget-Abadie, M Mirza,
    B Xu, D Warde-Farley, S Ozair, Y. Bengio. Advances in neural information
    processing systems, 2672-2680.
Links:
    - [GAN Paper](https://arxiv.org/pdf/1406.2661.pdf).
"""

import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
import tflearn

import tflearn.datasets.mnist as mnist
X, Y, testX, testY = mnist.load_data('data/')

image_dim = 784  # 28 X 28 pixels
z_dim = 200  # Noise data points
total_samples = len(X)


# Generator
def generator(x, reuse=False):
    with tf.variable_scope('Generator', reuse=reuse):
        x = tflearn.fully_connected(x, 256, activation='relu')
        x = tflearn.fully_connected(x, image_dim, activation='sigmoid')
        return x


# Discriminator
# Parameters
n_features = 784
n_classes = 10
learning_rate = 0.5
training_epochs = 2
batch_size = 100
logdir = '/tmp/mnist/11'

# Step 1: Preprocess the  Data
import tflearn
from tflearn.layers.core import input_data, fully_connected
from tflearn.layers.estimator import regression

import tflearn.datasets.mnist as mnist

X_train, y_train, X_test, y_test = mnist.load_data(one_hot=True)

# Step 2: Build the  Network
L1 = 200
L2 = 100
L3 = 60
L4 = 30
network = input_data(shape=[None, n_features])
network = fully_connected(network, L1, activation='relu')
network = fully_connected(network, L2, activation='relu')
network = fully_connected(network, L3, activation='relu')
network = fully_connected(network, L4, activation='relu')
network = fully_connected(network, n_classes, activation='softmax')
network = regression(network, optimizer='adam', loss='binary_crossentropy')

# Step 3: Training
Exemple #35
0
import deepneuralnet as net
import random
import tflearn.datasets.mnist as mnist
from skimage import io


model = net.model
path_to_model = 'final-model.tflearn'

_,_,testX, _ = mnist.load_data(one_hot=True)
model.load(path_to_model)

rand_index = random.randint(0,len(testX) -1)

x = testX[rand_index].reshape((28,28,1))


result = model.predict([x])[0]

prediction = result.tolist().index(max(result))

print("Predicition",prediction)

io.imsave('testimage.jpg', x.reshape(28,28))
        self.model = tflearn.DNN(network, tensorboard_verbose=0)

    def load_from_two(self, m1fn, m2fn):
        self.model.load(m1fn, scope_for_restore="scope1", weights_only=True)
        self.model.load(m2fn, scope_for_restore="scope2", weights_only=True, create_new_session=False)

    def train(self, X, Y, testX, testY, n_epoch=1, snapshot_step=1000):
        # Training
        self.model.fit(X, Y, n_epoch=n_epoch, validation_set=(testX, testY),
                       snapshot_step=snapshot_step,
                       show_metric=True, run_id="model12")

#-----------------------------------------------------------------------------

X, Y, testX, testY = mnist.load_data(one_hot=True)

def prepare_model1_weights_file():
    tf.reset_default_graph()
    m1 = Model1()
    m1.train(X, Y, testX, testY, 2)
    m1.model.save("model1.tfl")

def prepare_model1_weights_file_in_scopeQ():
    tf.reset_default_graph()
    with tf.variable_scope("scopeQ") as scope:
        m1 = Model1()
    m1.model.fit({"scopeQ/input": X}, {"scopeQ/target": Y}, n_epoch=1, validation_set=0.1, show_metric=True, run_id="model1_scopeQ")
    m1.model.save("model1_scopeQ.tfl")

def prepare_model2_weights_file():
Exemple #37
0
""" Random Forest example. """

from __future__ import division, print_function, absolute_import

import tflearn
from tflearn.estimators import RandomForestClassifier

# Data loading and pre-processing
import tflearn.datasets.mnist as mnist
X, Y, testX, testY = mnist.load_data(one_hot=False)

m = RandomForestClassifier(n_estimators=100, max_nodes=1000)
m.fit(X, Y, batch_size=10000, display_step=10)

print("Compute the accuracy on train set:")
print(m.evaluate(X, Y, tflearn.accuracy_op))

print("Compute the accuracy on test set:")
print(m.evaluate(testX, testY, tflearn.accuracy_op))

print("Digits for test images id 0 to 5:")
print(m.predict(testX[:5]))
print("True digits:")
print(testY[:5])
Exemple #38
0
# no arguments
# Import Numpy, TensorFlow, TFLearn, and MNIST data
import numpy as np
import tensorflow as tf

# first version
import tflearn
import tflearn.datasets.mnist as mnist  ##

#
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.callbacks import EarlyStopping, TensorBoard

# Retrieve the training and test data
trainX, trainY, testX, testY = mnist.load_data(one_hot=True)

print(trainX.shape)
print(trainX)

# Visualizing the data
import matplotlib.pyplot as plt

#%matplotlib inline # no more jupyter


# Function for displaying a training image by it's index in the MNIST set
def show_digit(index):
    label = trainY[index].argmax(axis=0)
    # Reshape 784 array into 28x28 image
    image = trainX[index].reshape([28, 28])
        v for v in tflearn.get_all_trainable_variable()
        if scope + '/' in v.name
    ]


def get_input_tensor_by_name(name):
    return tf.get_collection(tf.GraphKeys.INPUTS, scope=name)[0]


# メイン部
if __name__ == "__main__":

    print("%s: start" % SCRIPT_NAME)

    # 目的: MNIST文字生成
    X, Y, testX, testY = mnist.load_data(data_dir=MNIST_DIR)

    # ランダムで100次元
    Z = np.random.uniform(-1., 1., SAMPLE_NUM * Z_SIZE)

    # データを処理
    sample_X = X[:SAMPLE_NUM]
    sample_testX = testX[:SAMPLE_NUM]
    sample_X = np.reshape(sample_X, (SAMPLE_NUM, X_SIZE))
    sample_Z = np.reshape(Z, (SAMPLE_NUM, Z_SIZE))

    # Train
    train_gan(sample_X, sample_Z)

    print("%s: done" % SCRIPT_NAME)
Exemple #40
0
import tflearn
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.estimator import regression
import tflearn.datasets.mnist as mnist

X, Y, x_test, y_test = mnist.load_data(one_hot=True)

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

convnet = input_data(shape=[None, 28, 28, 1], name='input')

convnet = conv_2d(convnet, 32, 2, activation='relu')
convnet = max_pool_2d(convnet, 2)

convnet = conv_2d(convnet, 64, 2, activation='relu')
convnet = max_pool_2d(convnet, 2)

convent = fully_connected(convnet, 1024, activation='relu')
convnet = dropout(convnet, 0.8)

convnet = fully_connected(convnet, 10, activation='softmax')
convnet = regression(convnet,
                     optimizer='adam',
                     learning_rate=0.01,
                     loss='categorical_crossentropy',
                     name='targets')

model = tflearn.DNN(convnet)
model.fit({'input': X}, {'targets': Y},
Exemple #41
0
# predict.py

import deepneuralnet as net
import random
import tflearn.datasets.mnist as mnist
from skimage import io
model = net.model
path_to_model = 'final-model.tflearn'
_, _, testX, _ = mnist.load_data(one_hot=True)
model.load(path_to_model)

# Randomly take an image from the test set
rand_index = random.randint(0, len(testX) - 1)
x = testX[rand_index].reshape((28, 28, 1))
result = model.predict([x])[0]  # Predict
prediction = result.tolist().index(
    max(result))  # The index represents the number predicted in this case

print("Prediction", prediction)

io.imsave('testimage.jpg',
          x.reshape(28,
                    28))  # This shows the image in the computer for you to see
import tflearn  # before "from", i have to import there father.
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.estimator import regression
import tflearn.datasets.mnist as mnist
#from internet
#import numpy as np

X, Y, test_x, test_y = mnist.load_data(one_hot=True)

X = X.reshape([-1, 28, 28, 1])
test_x = test_x.reshape([-1, 28, 28, 1])  #forgot #!!!
#X = np.array(X)
#Y = np.array(Y)
#test_x = np.array(test_x)
#test_y = np.array(test_y)

convnet = input_data(shape=[None, 28, 28, 1], name='input')

convnet = conv_2d(convnet, 32, 2, activation='relu')
convnet = max_pool_2d(convnet, 2)

convnet = conv_2d(convnet, 64, 2, activation='relu')
convnet = max_pool_2d(convnet, 2)

convnet = fully_connected(convnet, 1024, activation='relu')
convnet = dropout(convnet, 0.8)

convnet = fully_connected(convnet, 10, activation='softmax')
convnet = regression(convnet,
                     optimizer='adam',
Exemple #43
0
"""
This tutorial will introduce how to combine TFLearn and Tensorflow, using
TFLearn wrappers regular Tensorflow expressions.
"""

import tensorflow as tf
import tflearn

# ----------------------------
# Utils: Using TFLearn Trainer
# ----------------------------

# Loading MNIST complete dataset
import tflearn.datasets.mnist as mnist
trainX, trainY, testX, testY = mnist.load_data(one_hot=True)

# Define a dnn using Tensorflow
with tf.Graph().as_default():

    # Model variables
    X = tf.placeholder("float", [None, 784])
    Y = tf.placeholder("float", [None, 10])

    W1 = tf.Variable(tf.random_normal([784, 256]))
    W2 = tf.Variable(tf.random_normal([256, 256]))
    W3 = tf.Variable(tf.random_normal([256, 10]))
    b1 = tf.Variable(tf.random_normal([256]))
    b2 = tf.Variable(tf.random_normal([256]))
    b3 = tf.Variable(tf.random_normal([10]))

    # Multilayer perceptron