Example #1
0
    def run(self, step_limit):
        self.train()

        with tf.Session() as sess:
            tf.global_variables_initializer().run()

            # MNIST 데이터
            dataset = mnist.read_data_sets("MNIST_data/", one_hot=True)
            train_data, train_label, test_data, test_label = dataset.train.images, dataset.train.labels, \
                                                             dataset.test.images, dataset.test.labels
            train_data = train_data.reshape(
                -1, self.time_step,
                int(28 * 28 /
                    self.time_step))  #TF의 RNN은 입력데이터의 rank가 높아야함(열이 많아야 함)
            test_data = test_data.reshape(
                -1, self.time_step,
                int(28 * 28 / self.time_step))  #mnist 데이터는 사실 적절하진 못함

            test_indices = np.arange(len(train_data))
            np.random.shuffle(test_indices)
            test_indices = test_indices[0:10000]

            path = "LSTM_mnist/" + str(step_limit) + ""
            saver = NNutils.save(path, sess)
            writer, writer_test, merged = NNutils.graph(path, sess)

            step = sess.run(self.global_step)
            while step < step_limit:
                print("step :", step)
                for start, end in zip(
                        range(0, train_data.shape[0], self.batch_size),
                        range(self.batch_size, train_data.shape[0],
                              self.batch_size)):
                    summary, \
                    _, loss, \
                    step = sess.run([merged,
                                     self.training, self.cost,
                                     self.global_step],
                                    feed_dict={self.x: train_data[start:end],
                                               self.y: train_label[start:end],
                                               self.dropout_conv: 1.0,
                                               self.dropout_normal: 0.5})

                    if step % 50 == 0:
                        writer.add_summary(summary, step)
                        print(step, datetime.now(), loss)

                summary, \
                loss, \
                accuracy = sess.run([merged, self.cost, self.accuracy],
                                    feed_dict={self.x: test_data,
                                               self.y: test_label,
                                               self.dropout_conv: 1.0,
                                               self.dropout_normal: 1.0})

                writer_test.add_summary(summary, step)
                print("test results : ", accuracy, loss)
                saver.save(sess, path + "/model.ckpt", step)
Example #2
0
    def run(self, step_limit):
        self.train()

        with tf.Session() as sess:
            tf.global_variables_initializer().run()

            dataset = mnist.read_data_sets("MNIST_data/", one_hot=True)
            train_data, train_label, test_data, test_label = dataset.train.images, dataset.train.labels, \
                                          dataset.test.images, dataset.test.labels

            # 이 데이터셋은 기본적으로 0~1사이 값으로 정규화되어있어 이미지를 볼 수 없다. => 0~255로 변경
            train_data = train_data * 255
            test_data = test_data * 255

            test_indices = np.arange(len(test_data))
            np.random.shuffle(test_indices)
            test_indices = test_indices[0:1000]

            path = "AE/" + str(step_limit)
            saver = NNutils.save(path, sess)
            writer, writer_test, merged = NNutils.graph(path, sess)

            step = sess.run(self.global_step)
            while step < step_limit:
                print("step :", step)
                for start, end in zip(
                        range(0, len(train_data), self.batch_size),
                        range(self.batch_size, len(train_data),
                              self.batch_size)):
                    summary, \
                    _, loss, \
                    _, \
                    step = sess.run([merged,
                                     self.training, self.cost,
                                     self.training_fc,
                                     self.global_step],
                                    feed_dict={self.x: train_data[start:end],
                                               self.y: train_label[start:end],
                                               self.dropout_conv: 0.8,
                                               self.dropout_normal: 0.5})

                    if step % 50 == 0:
                        writer.add_summary(summary, step)
                        print(step, datetime.now(), loss)

                summary, loss, loss_fc, im = sess.run(
                    [merged, self.cost, self.cost_fc, self.im],
                    feed_dict={
                        self.x: test_data,
                        self.y: test_label,
                        self.dropout_conv: 1.0,
                        self.dropout_normal: 1.0
                    })

                writer_test.add_summary(summary, step)
                print("test results : ", loss, loss_fc)
                saver.save(sess, path + "/model.ckpt", step)
def get_dataset(dataset_name, seed=0, test=False):
    if dataset_name == "mnist":
        data_folder = './data/mnist'
        if not os.path.exists(data_folder):
            os.makedirs(data_folder)
        if not test:
            dataset = mnist.read_data_sets("./data/mnist", seed=seed).train
        else:
            dataset = mnist.read_data_sets("./data/mnist", seed=seed).test
    elif dataset_name == "cifar-10":
        if not test:
            dataset = cifar10.read_data_sets("./data/cifar-10",
                                             seed=seed).train
        else:
            dataset = cifar10.read_data_sets("./data/cifar-10", seed=seed).test
    else:
        raise Exception("Not implemented.")
    return dataset
Example #4
0
def gen_mnist_cases(data_dir='mnist', one_hot=True):
    mnist_data = mnist.read_data_sets(data_dir, one_hot=one_hot)
    (images, labels) = (mnist_data.train.images.tolist(),
                        mnist_data.train.labels.tolist())
    dataset = [[fvec, target] for fvec, target in zip(images, labels)]
    # Return the dataset as a vector, where each row consists of images (784 elements) and labels
    # Images have been preprocessed by casting to float32 and normalizing the pixels to range [0,1]
    # Labels are one-hot vectors
    return dataset
Example #5
0
    def run(self, step_limit):
        self.train()

        with tf.Session() as sess:
            tf.global_variables_initializer().run()

            dataset = mnist.read_data_sets(one_hot=True)
            train_data, train_label, test_data, test_label = dataset.train.images, dataset.train.labels, \
                                          dataset.test.images, dataset.test.labels

            test_indices = np.arange(len(test_data))
            np.random.shuffle(test_indices)
            test_indices = test_indices[0:1000]

            name = self.info()
            path = "mnist/" + str(step_limit) + name

            saver = NNutils.save(path, sess)
            writer, writer_test, merged = NNutils.graph(path, sess)

            step = sess.run(self.global_step)
            while step < step_limit:
                print("step :", step)
                for start, end in zip(
                        range(0, len(train_data), self.batch_size),
                        range(self.batch_size, len(train_data),
                              self.batch_size)):
                    summary, \
                    _, loss, \
                    step = sess.run([merged,
                                     self.training, self.cost,
                                     self.global_step],
                                    feed_dict={self.x: train_data[start:end],
                                               self.y: train_label[start:end],
                                               self.dropout_conv: 1.0,
                                               self.dropout_normal: 1.0})

                    if step % 50 == 0:
                        writer.add_summary(summary, step)
                        print(step, datetime.now(), loss)

                summary, \
                loss, \
                accuracy = sess.run([merged, self.cost, self.accuracy],
                                    feed_dict={self.x: test_data,
                                               self.y: test_label,
                                               self.dropout_conv: 1.0,
                                               self.dropout_normal: 1.0})

                writer_test.add_summary(summary, step)
                print("test results : ", accuracy, loss)
                saver.save(
                    sess,
                    path + "/" + name + ".ckpt",
                    step,
                )
def main(unused_argv):
    #获取数据
    data_sets = mnist.read_data_sets(FLAGS.directory,
                                     dtype=tf.uint8,
                                     reshape=False,
                                     validation_size=FLAGS.validation_size)
    #将数据转换为tf.train.Example类型,并写入TFRecords文件
    convert_to(data_sets.train, 'train')
    convert_to(data_sets.validation, 'validation')
    convert_to(data_sets.test, 'test')
Example #7
0
def select_dataset(name):
    x_size, y_size, train_data, train_label, test_data, test_label = 0, 0, [], [] ,[] ,[] #초기화
    if name == 'cifar':
        dataset = cifar.CIFAR()
        train_data, train_label, test_data, test_label = dataset.getdata()

        train_data = train_data.reshape(-1, 3072)
        test_data = test_data.reshape(-1, 3072)
        x_size = 3072
        y_size = 10

    elif name == 'svhn':
        dataset = svhn.SVHN()
        train_data, train_label = dataset.get_trainset()
        test_data, test_label = dataset.get_testset()

        train_data = train_data.reshape(-1, 3072)
        test_data = test_data.reshape(-1, 3072)
        x_size = 3072
        y_size = 10

    elif name == 'mnist':
        dataset = mnist.read_data_sets(flags.MNIST_DIR, one_hot=True)
        train_data, train_label, test_data, test_label = dataset.train.images, dataset.train.labels, \
                                                         dataset.test.images, dataset.test.labels
        x_size = 784
        y_size = 10

    elif name == 'news':
        trainset = fetch_20newsgroups(data_home=flags.NEWS_DIR, subset='train')
        testset = fetch_20newsgroups(data_home=flags.NEWS_DIR, subset='test')

        vectorizer = TfidfVectorizer(analyzer='word', max_features=3072)

        vectorizer.fit(trainset.data)
        train_data = vectorizer.transform(trainset.data)
        train_data = csr_matrix.todense(train_data)
        train_label = trainset.target
        train_label = NNutils.onehot(train_label, 20, list=True)
        # print(train_label.shape)

        test_data = vectorizer.transform(testset.data)
        test_data = csr_matrix.todense(test_data)
        test_label = testset.target
        test_label = NNutils.onehot(test_label, 20, list=True)

        x_size = 3072
        y_size = 20

    return Dataset(name, x_size, y_size, train_data, train_label, test_data,
                   test_label)
Example #8
0
def load_dataset(model):
	if model.dataset_name == 'mnist':
		import mnist as ds
	elif model.dataset_name == 'fashion':
		import fashion as ds
	elif model.dataset_name == 'affmnist':
		import affmnist as ds
	elif model.dataset_name == 'cifar10':
		import cifar10 as ds
	elif model.dataset_name == 'celeba':
		import celeba as ds
	elif model.dataset_name == 'chair':
		import chair as ds
	return ds.read_data_sets(model.dataset_path, dtype=tf.uint8, reshape=False, validation_size=0).train
Example #9
0
def run_training():

    with tf.Graph().as_default() as graph:

        # Prepare training data
        mnist_data = mnist.read_data_sets(DATA_DIR,
                                          one_hot=True,
                                          local_only=LOCAL_DATA)

        # Create placeholders
        x = tf.placeholder(tf.float32, [None, 784])
        t = tf.placeholder(tf.float32, [None, 10])
        keep_prob = tf.placeholder(tf.float32, [])
        global_step = tf.Variable(
            0, trainable=False
        )  # This is a useless variable (in this code) but it's use to not brake the API

        # Add test loss and test accuracy to summary
        test_loss = tf.placeholder(tf.float32, [])
        test_accuracy = tf.placeholder(tf.float32, [])
        tf.summary.scalar('Test_loss', test_loss)
        tf.summary.scalar('Test_accuracy', test_accuracy)

        # Define a model
        p = mnist_model.get_model(x, keep_prob, training=True)
        train_step, loss, accuracy = mnist_model.get_trainer(p, t, global_step)

        init_op = tf.global_variables_initializer()
        saver = tf.train.Saver()
        summary = tf.summary.merge_all()

        # Create a supervisor
        sv = tf.train.Supervisor(is_chief=True,
                                 logdir=LOG_DIR,
                                 init_op=init_op,
                                 saver=saver,
                                 summary_op=None,
                                 global_step=global_step,
                                 save_model_secs=0)

        # Create a session and start a training loop
        with sv.managed_session() as sess:

            reports, step = 0, 0
            start_time = time.time()

            while not sv.should_stop() and step < MAX_STEPS:

                images, labels = mnist_data.train.next_batch(BATCH_SIZE)
                feed_dict = {x: images, t: labels, keep_prob: 0.5}
                _, loss_val, step = sess.run([train_step, loss, global_step],
                                             feed_dict=feed_dict)

                if step > CHECKPOINT * reports:
                    reports += 1
                    logging.info('Step: %d, Train loss: %f', step, loss_val)

                    # Evaluate the test loss and test accuracy
                    loss_vals, acc_vals = [], []
                    for _ in range(len(mnist_data.test.labels) // BATCH_SIZE):
                        images, labels = mnist_data.test.next_batch(BATCH_SIZE)
                        feed_dict = {x: images, t: labels, keep_prob: 1.0}
                        loss_val, acc_val = sess.run([loss, accuracy],
                                                     feed_dict=feed_dict)
                        loss_vals.append(loss_val)
                        acc_vals.append(acc_val)

                    loss_val, acc_val = np.sum(loss_vals), np.mean(acc_vals)

                    # Save summary
                    feed_dict = {test_loss: loss_val, test_accuracy: acc_val}
                    sv.summary_computed(sess,
                                        sess.run(summary, feed_dict=feed_dict),
                                        step)
                    sv.summary_writer.flush()

                    logging.info('Time elapsed: %d',
                                 (time.time() - start_time))
                    logging.info('Step: %d, Test loss: %f, Test accuracy: %f',
                                 step, loss_val, acc_val)

        sv.stop()
Example #10
0
import mnist
import numpy as np
import matplotlib.pyplot as plt
from numpy.lib.stride_tricks import as_strided
import classifiers 
from scipy.linalg import svd
import math

PEGASOS = 0
SGDQN = 1
ASGD = 2

data = mnist.read_data_sets("MNIST_data/", one_hot=True)

print data.train.images.shape
print data.train.labels.shape

train_image = data.train.images.copy()
train_label = data.train.labels.copy()

train_label[train_label == 0 ] = -1

test_image = data.test.images.copy()
test_label = data.test.labels.copy()

test_label[test_label == 0] = -1

classifier_type = 1

if classifier_type == SGDQN:
	reg = 1e-4
Example #11
0
import matplotlib.pyplot as plt
import numpy as np
import mnist
"""
理解本段程序参考过的article:
RNN原理: https://www.cnblogs.com/jiangxinyang/p/9362922.html
tf.contrib.rnn函数介绍: https://blog.csdn.net/MOU_IT/article/details/86103110
tf.nn.dynamic_rnn介绍: https://www.cnblogs.com/lovychen/p/9294624.html
tf.reduce_mean解释: https://blog.csdn.net/dcrmg/article/details/79797826
tf.unstack解释:https://blog.csdn.net/u012193416/article/details/77411535  

完全第一次接触神经网络可以想看下一些术语的解释:
http://www.dataguru.cn/article-12193-1.html
"""

mnist = mnist.read_data_sets('MNIST_data', one_hot=True)

#configuration variable
learn_rate = 0.001
training_times = 80000

batch_size = 128

input_vec_size = 28
time_step_size = 28
n_hidden_units = 128
n_classes = 10

#tf graph input
x = tf.placeholder(tf.float32, [None, time_step_size, input_vec_size])  #创建一个三维占位符,分别根据LSTM网络的:输入向量,时间,和
y = tf.placeholder(tf.float32, [None, n_classes])
Example #12
0
        # run the trained Generator excluding discriminator
        generated_samples = sess.run(output_Gen, feed_dict = {Z_input:Z_noise, Y_input: Y_label})

        # Plotting & saving the images
        dateTimeObj = datetime.now()
        dateNtime = dateTimeObj.strftime("%d-%b-%Y(%H-%M-%S)")
        fig = generate_plot(generated_samples)
        plt.savefig(inp + '__' + dateNtime + '.png', bbox_inches = 'tight') # Saving the figures
        
    
    create('y')
    create('e')
    create('s')


if __name__ == '__main__':
    emnist = ms.read_data_sets("emnist_letters/",one_hot = True)
    ##n = 6
    ##sample_image = emnist.train.next_batch(n)
    ##
    ##create_results(sample_image, 6)
##    init = 30
##    for i in range(15):
##        plt.figure(figsize=(1,1))
##        plt.imshow(emnist.train.images[init+i].reshape([28,28])*-1, cmap="gray")
##        plt.savefig('tete.png', bbox_inches = 'tight')
##        print (np.where(emnist.train.labels[init+i] == 1.))
##        plt.show()

    start_everything()
Example #13
0
'''
MNIST CNN
'''

import tensorflow as tf
from mnist import read_data_sets
tf.reset_default_graph()

mnist = read_data_sets("./MNIST_data", one_hot=True)

# 參數
learning_rate = 0.001
epochs = 1000
batch_size = 100
D = 0.5  # dropout %


def weight_variable(shape):
    initial_value = tf.truncated_normal(shape, stddev=0.1)
    return tf.Variable(initial_value)


def bias_variable(shape):
    initial_value = tf.constant(0.1, shape=shape)
    return tf.Variable(initial_value)


def conv2d(x, W):
    # x 輸入照片 , W 是 Kernal 或 可稱 Filter
    # 步長 stride = [1, 水平步長, 垂直步長, 1] "通常前後都寫1"
    # padding = SAME mean add padding on input data
Example #14
0
from __future__ import division, print_function, absolute_import
import tensorflow as tf
import numpy as np
from datetime import datetime
import time
import os
from six.moves import xrange  # pylint: disable=redefined-builtin
import scipy.misc

import mnist as mnist_data
mnist = mnist_data.read_data_sets("/tmp/data/", one_hot=True)

from infogan_model import INFOGAN
import infogan_flags as flags

def save_images(images, target_dir=flags.train_dir, prefix=''):
    for i in xrange(images.shape[0]):
        name = prefix + str(i) + '.png'
        img = np.squeeze(images[i,:,:,:])
        scipy.misc.toimage(img, cmin=0.0, cmax=1.0).save(os.path.join(target_dir, name))


def train():

    with tf.Graph().as_default():

        global_step = tf.Variable(0, trainable=False)
        batch_size = 64

        infogan = InfoGAN(batch_size, flags)
        train_steps_op = infogan.train_steps(global_step)
Example #15
0
    # Dashboard info
    log.info('Dashboard: http://localhost/deep-dashboard?id={}'.format(model_id))

    # Model options
    opt = {
        'num_inp': 28 * 28,
        'num_hid_enc': 100,
        'num_hid': 20,
        'num_hid_dec': 100,
        'non_linear': 'tf.nn.relu',
        'weight_decay': 5e-5
    }

    # MNIST dataset
    dataset = mnist.read_data_sets("../MNIST_data/", one_hot=True)

    # Create models
    m = get_train_model(opt)
    m_dec = get_decoder(opt, m)

    # RNG
    random = np.random.RandomState(2)

    # Start session
    sess = tf.Session()
    sess.run(tf.initialize_all_variables())

    # Train loop
    step = 0
    while step < 50000:
Example #16
0
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import sys
import argparse
import numpy as np
import matplotlib.pyplot as plt
import mnist
import functions as fun

height = 100
width = 100
num_input_pixels = height * width
num_classes = 20

data = mnist.read_data_sets("dataset", one_hot=True, num_classes=num_classes)

for i in range(data.train.images.shape[0]):
    fun.DisplayDigit(data.test.images, height, width, data.test.labels, i)
        target_tensor: the target tensor that we want to reconstruct
        epsilon:
    '''
    print('Target:' + str(target_tensor.get_shape()))
    print('Output:' + str(output_tensor.get_shape()))

    return tf.reduce_sum(-target_tensor * tf.log(output_tensor + epsilon) -
                         (1.0 - target_tensor) *
                         tf.log(1.0 - output_tensor + epsilon))


if __name__ == "__main__":
    data_directory = os.path.join(FLAGS.working_directory, "MNIST")
    if not os.path.exists(data_directory):
        os.makedirs(data_directory)
    mnist = input_data.read_data_sets(data_directory, one_hot=True)

    input_tensor = tf.placeholder(
        tf.float32, [FLAGS.batch_size, FLAGS.input_size * FLAGS.input_size])

    with pt.defaults_scope(activation_fn=tf.nn.elu,
                           batch_normalize=True,
                           learned_moments_update_rate=0.0003,
                           variance_epsilon=0.001,
                           scale_after_normalization=True):
        with pt.defaults_scope(phase=pt.Phase.train):
            with tf.variable_scope("model") as scope:
                output_tensor, mean, stddev = decoder(encoder(input_tensor))

        with pt.defaults_scope(phase=pt.Phase.test):
            with tf.variable_scope("model", reuse=True) as scope:
import numpy as np
import tflearn
import tflearn.datasets import mnist

MNIST_data = mnist.read_data_sets(one_hot=True)

data_train = MNIST_data.train
data_validation = MNIST_data.validation
data_test = MNIST_data.test

X,y = data_train._images, data_train._labels

tflearn.init_graph(num_cores=4)

net = tflearn.input_data(shape=[None,784])
net = tflearn.fully_connected(net,100, activation='relu')

net = tflearn.fully_connected(net,100, activation='relu')
net = tflearn.fully_connected(net,10, activation='softmax')

net = tflearn.regression(net, loss="categorical_crossentropy", optimizer='adam')

model = tflearn.DNN(net)
model.fit(X,y,n_epoch=1, batch_size=10, show_metric=True)
                    action="store_true",
                    help="use batch norm to the bottleneck feature or not.")
parser.add_argument("--label_smoothing",
                    action="store_true",
                    help="use label smoothing or not")
parser.add_argument("--heat_up",
                    action="store_true",
                    help="use heat up or not")
args = parser.parse_args()

os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu_id

tf.compat.v1.set_random_seed(args.seed)
np.random.seed(args.seed)

mnist = mnist_dataset.read_data_sets("./data/", one_hot=True, reshape=False)


def draw_feature_and_weights(save_file_name, feature, weights, test_class_id):
    embedding_dim = feature.shape[1]
    if embedding_dim == 3:
        fig = plt.figure(figsize=(4, 4), dpi=160)
        ax = Axes3D(fig)
    elif embedding_dim == 2:
        fig = plt.figure(figsize=(4, 4), dpi=160)
        ax = plt.subplot(111)
    else:
        print('Only support feature with dim 3 or 2.')
        return

    n_classes = np.amax(test_class_id) + 1
Example #20
0
    # Dashboard info
    log.info(
        'Dashboard: http://localhost/deep-dashboard?id={}'.format(model_id))

    # Model options
    opt = {
        'num_inp': 28 * 28,
        'num_hid_enc': 100,
        'num_hid': 20,
        'num_hid_dec': 100,
        'non_linear': 'tf.nn.relu',
        'weight_decay': 5e-5
    }

    # MNIST dataset
    dataset = mnist.read_data_sets("../MNIST_data/", one_hot=True)

    # Create models
    m = get_train_model(opt)
    m_dec = get_decoder(opt, m)

    # RNG
    random = np.random.RandomState(2)

    # Start session
    sess = tf.Session()
    sess.run(tf.initialize_all_variables())

    # Train loop
    step = 0
    while step < 50000:
def main(_):
  # Import data
  mnist = input_data.read_data_sets(DATASET_PATH, one_hot=True)

  # Create the model
  # Add the first layer.
  x = tf.placeholder(tf.float32, [None, 784])
  W = tf.Variable(tf.zeros([784, 300]))
  b = tf.Variable(tf.zeros([300]))
  y = tf.matmul(x, W) + b

  # Add the second layer.
  W2 = tf.Variable(tf.zeros([300, 62]))
  b2 = tf.Variable(tf.zeros([62]))
  y2 = tf.matmul(y, W2) + b2  

  # Define loss and optimizer
  y_ = tf.placeholder(tf.float32, [None, 62])

  # The raw formulation of cross-entropy,
  #
  #   tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(tf.nn.softmax(y)),
  #                                 reduction_indices=[1]))
  #
  # can be numerically unstable.
  #
  # So here we use tf.nn.softmax_cross_entropy_with_logits on the raw
  # outputs of 'y', and then average across the batch.
  cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y2))
  train_step = tf.train.GradientDescentOptimizer(2.0).minimize(cross_entropy)

  sess = tf.InteractiveSession()
  tf.global_variables_initializer().run()
  # Train
  number_of_batches = int(len(mnist.train.images) / BATCH_SIZE)
  for _ in range(number_of_batches):
    batch_xs, batch_ys = mnist.train.next_batch(BATCH_SIZE)
    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})

  # Test trained model
  correct_prediction = tf.equal(tf.argmax(y2, 1), tf.argmax(y_, 1))
  accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
  print(sess.run(accuracy, feed_dict={x: mnist.test.images,
                                      y_: mnist.test.labels}))
  # Test a character image.
  a = mnist.train.next_batch(1)
  np.set_printoptions(linewidth=250)
  char_image = a[0]

  # Reshape into 28x28 from 1x784
  char_image = np.array(char_image.reshape(28, 28)) * int(255)
  char_image = np.array(char_image, dtype=int)

  # Inverse Transformations
  char_image = np.flip(char_image, axis = 0) # Inverse flip at axis 0.
  char_image = np.rot90(char_image,3) # Rotate 270 counter clock-wise

  # Reshape into 28x28 from 1x784
  char_image = np.array(char_image.reshape(28, 28)) * int(255)
  char_image = np.array(char_image, dtype=int)
  
  print ("Image is " , char_image , "Correct Label is", np.argmax(a[1], axis =  1))
  print ("Predicted value for image 0 is ", np.argmax(sess.run(y2, {x: a[0]}), axis = 1))
Example #22
0
# -*- coding: utf-8 -*-
import tensorflow as tf
from mnist import read_data_sets

input_data = read_data_sets('MNIST_data', one_hot=True)

# x = tf.placeholder("float", [None, 784])
x = tf.placeholder(tf.float32, [None, 784])

W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

y = tf.nn.softmax(tf.matmul(x, W) + b)

# y_ = tf.placeholder("float", [None,10])
y_ = tf.placeholder(tf.float32, [None, 10])

# cross_entropy = -tf.reduce_sum(y_*tf.log(y))
cross_entropy = tf.reduce_mean(
    -tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))

train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)

init = tf.initialize_all_variables()
# init = tf.global_variables_initializer()

sess = tf.Session()
sess.run(init)

for i in range(1000):
    batch_xs, batch_ys = input_data.train.next_batch(100)
Example #23
0
# \x/x\x/x\x/x\x/x\x/    -- fully connected layer (softmax)      W [784, 10]     b[10]
#   · · · · · · · ·                                              Y [batch, 10]

# The model is:
#
# Y = softmax( X * W + b)
#              X: matrix for 100 grayscale images of 28x28 pixels, flattened (there are 100 images in a mini-batch)
#              W: weight matrix with 784 lines and 10 columns
#              b: bias vector with 10 dimensions
#              +: add with broadcasting: adds the vector to each line of the matrix (numpy)
#              softmax(matrix) applies softmax on each line
#              softmax(line) applies an exp to each value then divides by the norm of the resulting line
#              Y: output matrix with 100 lines and 10 columns

# Download images and labels into mnist.test (10K images+labels) and mnist.train (60K images+labels)
mnist = read_data_sets("data", one_hot=True, reshape=False, validation_size=0)

# input X: 28x28 grayscale images, the first dimension (None) will index the images in the mini-batch
X = tf.placeholder(tf.float32, [None, 28, 28, 1])
# correct answers will go here
Y_ = tf.placeholder(tf.float32, [None, 10])
# weights W[784, 10]   784=28*28
W = tf.Variable(tf.zeros([784, 10]))
# biases b[10]
b = tf.Variable(tf.zeros([10]))

# flatten the images into a single line of pixels
# -1 in the shape definition means "the only possible dimension that will preserve the number of elements"
XX = tf.reshape(X, [-1, 784])

# The model
Example #24
0
# MODEL_FILENAME = sys.argv[2]
# BS = int(sys.argv[3]) #шонч╗Г batch_size
# if BS > 10000:
#     print('Error: test_number > 10000')
#     sys.exit()

DATA_FILENAME = 'data/'
MODEL_FILENAME = 'model/'
BS = 10000


def compute_accuracy(predict, labels):
    error = [
        a - b for a, b in zip(np.argmax(predict, axis=-1),
                              np.argmax(labels, axis=-1))
    ]
    correct = list(filter(lambda x: x == 0, error))
    accuracy = len(correct) / BS
    return accuracy


if __name__ == '__main__':
    mnist_net = net.NET(input_shape=[BS, 1, 28, 28])
    mnist_net.load_model(MODEL_FILENAME)
    mni = mnist.read_data_sets(DATA_FILENAME, one_hot=True, reshape=False)
    imgages, labels = mni.test.next_batch(BS)
    imgages = imgages.reshape((BS, 1, 28, 28))
    loss, pred = mnist_net.forward(imgages, labels)
    print('test_loss', loss)
    print('test_accuracy', compute_accuracy(pred, labels))
Example #25
0
import tensorflow as tf
import os
try:
    os.environ["CUDA_VISIBLE_DEVICES"] = visable_device
except:
    print('No available gpu')
    exit()
import numpy as np
np.set_printoptions(precision=4, suppress=True, linewidth=100)
import tensorflow.contrib.layers as ly
from mnist import read_data_sets
from mnist import dense_to_one_hot

clabel = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
dataset = read_data_sets('MNIST_data',
                         one_hot=False,
                         validation_size=0,
                         clabel=clabel)

n_rdims = 19
n_classes = 20
n_features = 32
z_dim = 128
n_generator = 10

batch_size = 32
eigenThreshold = 0.0001
qrThreshold = 0.0001
residueThreshold = 0.0001
EVALS_UPPER_BOUND = 1
ILDA_DECAY = 0.998
def main(_):
    # Import data
    mnist = input_data.read_data_sets(DATASET_PATH, one_hot=True)
    print("CHECKING",
          np.array(mnist.train.images).shape,
          np.array(mnist.train.labels).shape)
    print("CHECKING",
          np.array(mnist.test.images).shape,
          np.array(mnist.test.labels).shape)

    # Create the model
    x = tf.placeholder(tf.float32, [None, 784])

    # Define loss and optimizer
    y_ = tf.placeholder(tf.float32, [None, NUM_CLASSES])

    # Build the graph for the deep net
    y_conv = deepnn(x)

    with tf.name_scope('loss'):
        cross_entropy = tf.nn.softmax_cross_entropy_with_logits(labels=y_,
                                                                logits=y_conv)
    cross_entropy = tf.reduce_mean(cross_entropy)

    with tf.name_scope('adam_optimizer'):
        train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)

    with tf.name_scope('accuracy'):
        correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
        correct_prediction = tf.cast(correct_prediction, tf.float32)
    accuracy = tf.reduce_mean(correct_prediction)

    graph_location = "graphs/"
    print('Saving graph to: %s' % graph_location)
    train_writer = tf.summary.FileWriter(graph_location)
    train_writer.add_graph(tf.get_default_graph())

    # Save the model.
    saver = tf.train.Saver()

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        epoch_num = 1

        for e in range(NUMBER_OF_EPOCHES):
            # One epoch training.
            acc_train = []
            number_of_batches = int(len(mnist.train.images) / BATCH_SIZE)

            for i in range(number_of_batches):
                batch = mnist.train.next_batch(BATCH_SIZE)
                train_step.run(feed_dict={
                    x: batch[0],
                    y_: batch[1],
                    keep_prob: 0.5
                })
                train_accuracy = accuracy.eval(feed_dict={
                    x: batch[0],
                    y_: batch[1],
                    keep_prob: 1.0
                })
                acc_train.append(train_accuracy)
                # print('step %d, training accuracy %g' % (i, train_accuracy))

            print("Epoch ", epoch_num, ", Training accuracy is ",
                  sum(acc_train) / len(acc_train), "%")

            # One epoch testing.
            acc_test = []
            number_of_test_batches = int(len(mnist.test.images) / BATCH_SIZE)

            for i in range(number_of_test_batches):
                batch = mnist.test.next_batch(BATCH_SIZE)
                acc_test.append(
                    accuracy.eval(feed_dict={
                        x: batch[0],
                        y_: batch[1],
                        keep_prob: 1.0
                    }))

            print("Epoch ", epoch_num, ", Testing accuracy is ",
                  sum(acc_test) / len(acc_test), "%")

            # Save the model.
            save_path = saver.save(sess,
                                   'checkpoints/model.ckpt',
                                   global_step=1)
            print("Checkpoint saved in file: %s" % save_path)

            epoch_num += 1
Example #27
0
# this script is to try a mnist digit number classification with MLP by using
# tensorflow
# coder: Jie An
# version: 20170322
# bug_submission: [email protected]
# ==============================================================================
import numpy as np
import tensorflow as tf
import mnist


# set up HIDDEN_NEURON_NUM = 500
HIDDEN_NEURON_NUM = 500

# data input and preprocessing
mnist_data = mnist.read_data_sets("MNIST_data/", one_hot=True)
train_x, train_y, test_x, test_y = mnist_data.train.images, mnist_data.train.labels, mnist_data.test.images, mnist_data.test.labels

# weight init
W_h = tf.Variable(tf.random_normal([784, HIDDEN_NEURON_NUM], stddev=0.01))
W_o = tf.Variable(tf.random_normal([HIDDEN_NEURON_NUM, 10], stddev=0.01))

# construct model
X = tf.placeholder("float", [None, 784])
Y = tf.placeholder("float", [None, 10])
Y_pre = tf.matmul(tf.nn.relu(tf.matmul(X, W_h)), W_o)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=Y_pre, labels=Y))
train_op = tf.train.GradientDescentOptimizer(0.05).minimize(cost)
predict_op = tf.argmax(Y_pre, 1)

# run graph
Example #28
0
import tensorflow as tf
from mnist import read_data_sets

mnist = read_data_sets("MNIST_data/",one_hot=True)
sess = tf.InteractiveSession()

in_units = 784 # 输入节点数
h1_units = 300 # 隐含层节点数

#偏置初始为0,权重初始化为截断的正态分布,标准差为0.1
# W1对应784个特征,300个输出
W1 = tf.Variable(tf.truncated_normal([in_units,h1_units],stddev=0.1))
b1 = tf.Variable(tf.zeros([h1_units]))  # 300个输出
W2 = tf.Variable(tf.zeros([h1_units,10]))
b2 = tf.Variable(tf.zeros([10]))

x = tf.placeholder(tf.float32,[None,in_units])
# dropout的比率是不一样的,通常训练时小于1,预测时等于1
keep_prob = tf.placeholder(tf.float32)

hidden1 = tf.nn.relu(tf.matmul(x,W1) + b1)
hidden1_drop = tf.nn.dropout(hidden1,keep_prob)
#第一步,定义算法公式
y = tf.nn.softmax(tf.matmul(hidden1_drop,W2) + b2)

#第二步,定义损失函数和选择优化器来优化loss,优化器选择自适应的优化器Adagrad,学习率设0.3
y_ = tf.placeholder(tf.float32,[None,10])
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y),
                               reduction_indices=[1]))
train_step = tf.train.AdagradOptimizer(0.3).minimize(cross_entropy)
Example #29
0
def run_training():
    # Get cluster and node information
    env = json.loads(os.environ.get('TF_CONFIG', '{}'))
    cluster_info = env.get('cluster')
    cluster_spec = tf.train.ClusterSpec(cluster_info)
    task_info = env.get('task')
    job_name, task_index = task_info['type'], task_info['index']

    device_fn = tf.train.replica_device_setter(
        cluster=cluster_spec,
        worker_device='/job:%s/task:%d' % (job_name, task_index))

    logging.info('Start job:%s, index:%d', job_name, task_index)

    # Create a server object
    server = tf.train.Server(cluster_spec,
                             job_name=job_name,
                             task_index=task_index)

    # Start a parameter server node
    if job_name == 'ps':
        server.join()

    # Start a master/worker node
    if job_name == 'master' or job_name == 'worker':
        is_chief = (job_name == 'master')

        with tf.Graph().as_default() as graph:
            with tf.device(device_fn):

                # Prepare training data
                mnist_data = mnist.read_data_sets(DATA_DIR,
                                                  one_hot=True,
                                                  local_only=LOCAL_DATA)

                # Create placeholders
                x = tf.placeholder(tf.float32, [None, 784])
                t = tf.placeholder(tf.float32, [None, 10])
                keep_prob = tf.placeholder(tf.float32, [])
                global_step = tf.Variable(0, trainable=False)

                # Add test loss and test accuracy to summary
                test_loss = tf.placeholder(tf.float32, [])
                test_accuracy = tf.placeholder(tf.float32, [])
                tf.summary.scalar('Test_loss', test_loss)
                tf.summary.scalar('Test_accuracy', test_accuracy)

                # Define a model
                p = mnist_model.get_model(x, keep_prob, training=True)
                train_step, loss, accuracy = mnist_model.get_trainer(
                    p, t, global_step)

                init_op = tf.global_variables_initializer()
                saver = tf.train.Saver()
                summary = tf.summary.merge_all()

                # Create a supervisor
                sv = tf.train.Supervisor(is_chief=is_chief,
                                         logdir=LOG_DIR,
                                         init_op=init_op,
                                         saver=saver,
                                         summary_op=None,
                                         global_step=global_step,
                                         save_model_secs=0)

                # Create a session and start a training loop
                with sv.managed_session(server.target) as sess:
                    reports, step = 0, 0
                    start_time = time.time()
                    while not sv.should_stop() and step < MAX_STEPS:
                        images, labels = mnist_data.train.next_batch(
                            BATCH_SIZE)
                        feed_dict = {x: images, t: labels, keep_prob: 0.5}
                        _, loss_val, step = sess.run(
                            [train_step, loss, global_step],
                            feed_dict=feed_dict)
                        if step > CHECKPOINT * reports:
                            reports += 1
                            logging.info('Step: %d, Train loss: %f', step,
                                         loss_val)
                            if is_chief:
                                # Save checkpoint
                                sv.saver.save(sess,
                                              sv.save_path,
                                              global_step=step)

                                # Evaluate the test loss and test accuracy
                                loss_vals, acc_vals = [], []
                                for _ in range(
                                        len(mnist_data.test.labels) //
                                        BATCH_SIZE):
                                    images, labels = mnist_data.test.next_batch(
                                        BATCH_SIZE)
                                    feed_dict = {
                                        x: images,
                                        t: labels,
                                        keep_prob: 1.0
                                    }
                                    loss_val, acc_val = sess.run(
                                        [loss, accuracy], feed_dict=feed_dict)
                                    loss_vals.append(loss_val)
                                    acc_vals.append(acc_val)
                                loss_val, acc_val = np.sum(loss_vals), np.mean(
                                    acc_vals)

                                # Save summary
                                feed_dict = {
                                    test_loss: loss_val,
                                    test_accuracy: acc_val
                                }
                                sv.summary_computed(
                                    sess, sess.run(summary,
                                                   feed_dict=feed_dict), step)
                                sv.summary_writer.flush()

                                logging.info('Time elapsed: %d',
                                             (time.time() - start_time))
                                logging.info(
                                    'Step: %d, Test loss: %f, Test accuracy: %f',
                                    step, loss_val, acc_val)

                    # Export the final model
                    if is_chief:
                        sv.saver.save(sess,
                                      sv.save_path,
                                      global_step=sess.run(global_step))
                        export_model(tf.train.latest_checkpoint(LOG_DIR))

                sv.stop()
Example #30
0
    return tf.Variable(initial)


def conv2d(x, W):
    return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')


def max_pooling_22(x):
    return tf.nn.max_pool(x,
                          ksize=[1, 2, 2, 1],
                          strides=[1, 2, 2, 1],
                          padding='SAME')


# import data
mnist_data = mnist.read_data_sets('MNIST_data/', one_hot=True)
train_x, train_y, test_x, test_y = mnist_data.train.images, mnist_data.train.labels, mnist_data.test.images,\
                                   mnist_data.test.labels

# create data holder
x = tf.placeholder('float', shape=[None, np.size(train_x, axis=1)])
y_ = tf.placeholder('float', shape=[None, np.size(train_y, axis=1)])
keep_prob = tf.placeholder('float')

# parameters creation
W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])
W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])
W_fc1 = weight_variable([7 * 7 * 64, 1024])
b_fc1 = bias_variable([1024])
Example #31
0
filter_size1 = 4          # Convolution filters are 5 x 5 pixels.
num_filters1 = 16         # There are 16 of these filters.

# Convolutional Layer 2.
filter_size2 = 4          # Convolution filters are 5 x 5 pixels.
num_filters2 = 32         # There are 36 of these filters.

# Fully-connected layer.
fc_size = 128             # Number of neurons in fully-connected layer.

categories = []
categoriesFilePath = "DataSet/categories.txt"
with open(categoriesFilePath) as file:
    categories = [l.strip() for l in file]

data = mnist.read_data_sets('data/MNIST/', one_hot=True, validation_size=600)
#data = sklearn.datasets.load_files('data/MNIST/', shuffle="False")
print("Size of:")
print("- Training-set:\t\t{}".format(len(data.train.labels)))
print("- Test-set:\t\t{}".format(len(data.test.labels)))
print("- Validation-set:\t{}".format(len(data.validation.labels)))

data.test.cls = np.argmax(data.test.labels, axis=1)

# We know that MNIST images are 28 pixels in each dimension.
img_size = 28

# Images are stored in one-dimensional arrays of this length.
img_size_flat = img_size * img_size

# Tuple with height and width of images used to reshape arrays.