Python input_data.read_data_sets() Examples

The following are 6 code examples of input_data.read_data_sets(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module input_data , or try the search function .
Example #1
Source File: main.py    From variational-autoencoder with Apache License 2.0 7 votes vote down vote up
def __init__(self):
        self.mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
        self.n_samples = self.mnist.train.num_examples

        self.n_hidden = 500
        self.n_z = 20
        self.batchsize = 100

        self.images = tf.placeholder(tf.float32, [None, 784])
        image_matrix = tf.reshape(self.images,[-1, 28, 28, 1])
        z_mean, z_stddev = self.recognition(image_matrix)
        samples = tf.random_normal([self.batchsize,self.n_z],0,1,dtype=tf.float32)
        guessed_z = z_mean + (z_stddev * samples)

        self.generated_images = self.generation(guessed_z)
        generated_flat = tf.reshape(self.generated_images, [self.batchsize, 28*28])

        self.generation_loss = -tf.reduce_sum(self.images * tf.log(1e-8 + generated_flat) + (1-self.images) * tf.log(1e-8 + 1 - generated_flat),1)

        self.latent_loss = 0.5 * tf.reduce_sum(tf.square(z_mean) + tf.square(z_stddev) - tf.log(tf.square(z_stddev)) - 1,1)
        self.cost = tf.reduce_mean(self.generation_loss + self.latent_loss)
        self.optimizer = tf.train.AdamOptimizer(0.001).minimize(self.cost)


    # encoder 
Example #2
Source File: input_data_permuted.py    From rwa with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def read_data_sets():

	basepath = '/'.join(__file__.split('/')[:-1])

	import input_data
	data = input_data.read_data_sets(basepath+'/bin', one_hot=True)

	import os
	import numpy as np
	if not os.path.isfile(basepath+'/bin/permutation.npy'):
		indices = np.random.permutation(28**2)
		os.makedirs(basepath+'/bin', exist_ok=True)
		np.save(basepath+'/bin/permutation.npy', indices)
	else:
		indices = np.load(basepath+'/bin/permutation.npy')

	data.train.images[:,:] = data.train.images[:,indices]
	data.validation.images[:,:] = data.validation.images[:,indices]
	data.test.images[:,:] = data.test.images[:,indices]

	return data 
Example #3
Source File: imif_digits.py    From openimif with GNU General Public License v2.0 6 votes vote down vote up
def train_and_save_model(self, data_location, save_location):
        # Our training data
        mnist = input_data.read_data_sets(data_location, one_hot=True)

        for i in range(20000):
            batch = mnist.train.next_batch(50)
            if i%100 == 0:
                train_accuracy = self.accuracy.eval(feed_dict={
                    self.x:batch[0], self.y_: batch[1], self.keep_prob: 1.0
                })
                print("step %d, training accuracy %g"%(i, train_accuracy))
            self.train_step.run(feed_dict={self.x: batch[0], self.y_: batch[1], self.keep_prob: 0.5})

        # Saves path
        save_path = saver.save(sess, save_location)
        print("Model saved in file: ", save_path)

    # Loads saved model 
Example #4
Source File: imid_digits.py    From openimif with GNU General Public License v2.0 6 votes vote down vote up
def train_and_save_model(self):
        # Our training data
        mnist = input_data.read_data_sets('../data/MNIST_digits', one_hot=True)

        for i in range(20000):
            batch = mnist.train.next_batch(50)
            if i%100 == 0:
                train_accuracy = self.accuracy.eval(feed_dict={
                    self.x:batch[0], self.y_: batch[1], self.keep_prob: 1.0
                })
                print("step %d, training accuracy %g"%(i, train_accuracy))
            self.train_step.run(feed_dict={self.x: batch[0], self.y_: batch[1], self.keep_prob: 0.5})

        # Saves path
        save_path = saver.save(sess, "../trained_models/mnist_digits.ckpt")
        print("Model saved in file: ", save_path)

    # Loads saved model 
Example #5
Source File: mnist_fully_connected.py    From cocob with Apache License 2.0 4 votes vote down vote up
def main(_):
  # Import data
  mnist = input_data.read_data_sets('data', one_hot=True, validation_size=0)

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

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

  W_fc1 = weight_variable([28*28, 1000])
  b_fc1 = bias_variable([1000])
  h_fc1 = tf.nn.relu(tf.matmul(x, W_fc1) + b_fc1)

  W_fc2 = weight_variable([1000, 1000])
  b_fc2 = bias_variable([1000])
  h_fc2 = tf.nn.relu(tf.matmul(h_fc1, W_fc2) + b_fc2)
  
  W_fc3 = weight_variable([1000, 10])
  b_fc3 = bias_variable([10])
  out = tf.matmul(h_fc2, W_fc3) + b_fc3
  
  cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=out))
  train_step = cocob_optimizer.COCOB().minimize(cross_entropy)
  correct_prediction = tf.equal(tf.argmax(out, 1), tf.argmax(y_, 1))
  accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

  with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for i in range(600*40):
      batch = mnist.train.next_batch(100)
      if i % 600 == 0:
        test_batch_size = 10000
        batch_num = int(mnist.train.num_examples / test_batch_size)
        train_loss = 0
    
        for j in range(batch_num):
            train_loss += cross_entropy.eval(feed_dict={x: mnist.train.images[test_batch_size*j:test_batch_size*(j+1), :],
                                              y_: mnist.train.labels[test_batch_size*j:test_batch_size*(j+1), :]})
            
        train_loss /= batch_num

        test_err = 1-accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels})

        print('epoch %d, training cost %g, test error %g ' % (i/600, train_loss, test_err))
      train_step.run(feed_dict={x: batch[0], y_: batch[1]}) 
Example #6
Source File: Digit-Recognizer.py    From Digit-Recognizer with MIT License 4 votes vote down vote up
def main():
    mnist = input_data.read_data_sets("MNIST_data/", one_hot=False)
    data = mnist.train.next_batch(8000)
    train_x = data[0]
    Y = data[1]
    train_y = (np.arange(np.max(Y) + 1) == Y[:, None]).astype(int)
    mnist = input_data.read_data_sets("MNIST_data/", one_hot=False)
    tb = mnist.train.next_batch(2000)
    Y_test = tb[1]
    X_test = tb[0]
    # 0.00002-92
    # 0.000005-92, 93 when 200000 190500

    d1 = Digit_Recognizer_LR.model(train_x.T, train_y.T, Y, X_test.T, Y_test, num_iters=1500, alpha=0.05,
                                   print_cost=True)
    w_LR = d1["w"]
    b_LR = d1["b"]

    d2 = Digit_Recognizer_NN.model_nn(train_x.T, train_y.T, Y, X_test.T, Y_test, n_h=100, num_iters=1500, alpha=0.05,
                                      print_cost=True)

    dims = [784, 100, 80, 50, 10]
    d3 = Digit_Recognizer_DL.model_DL(train_x.T, train_y.T, Y, X_test.T, Y_test, dims, alpha=0.5, num_iterations=1100,
                                      print_cost=True)

    cap = cv2.VideoCapture(0)

    while (cap.isOpened()):
        ret, img = cap.read()
        img, contours, thresh = get_img_contour_thresh(img)
        ans1 = ''
        ans2 = ''
        ans3 = ''
        if len(contours) > 0:
            contour = max(contours, key=cv2.contourArea)
            if cv2.contourArea(contour) > 2500:
                # print(predict(w_from_model,b_from_model,contour))
                x, y, w, h = cv2.boundingRect(contour)
                # newImage = thresh[y - 15:y + h + 15, x - 15:x + w +15]
                newImage = thresh[y:y + h, x:x + w]
                newImage = cv2.resize(newImage, (28, 28))
                newImage = np.array(newImage)
                newImage = newImage.flatten()
                newImage = newImage.reshape(newImage.shape[0], 1)
                ans1 = Digit_Recognizer_LR.predict(w_LR, b_LR, newImage)
                ans2 = Digit_Recognizer_NN.predict_nn(d2, newImage)
                ans3 = Digit_Recognizer_DL.predict(d3, newImage)

        x, y, w, h = 0, 0, 300, 300
        cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
        cv2.putText(img, "Logistic Regression : " + str(ans1), (10, 320),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
        cv2.putText(img, "Shallow Network :  " + str(ans2), (10, 350),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
        cv2.putText(img, "Deep Network :  " + str(ans3), (10, 380),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
        cv2.imshow("Frame", img)
        cv2.imshow("Contours", thresh)
        k = cv2.waitKey(10)
        if k == 27:
            break