Exemplo n.º 1
0
class NetEval:
  def __init__(self, num_classes, num_hidden1, num_hidden2):
    self.NUM_CLASSES = num_classes
    self.IMAGE_SIZE = 28
    self.IMAGE_PIXELS = self.IMAGE_SIZE * self.IMAGE_SIZE

    self.images_pl = tf.placeholder(tf.float32, shape=(None, self.IMAGE_PIXELS))
    if FLAGS.one_hot:
      self.labels_pl = tf.placeholder(tf.int32, shape=(None))
    else:
      self.labels_pl = tf.placeholder(tf.int32, shape=(None, self.NUM_CLASSES))

    # Generate network
    self.mnist = FourLayeredFFNN(self.NUM_CLASSES)
    #self.mnist = FourLayeredFFCNN(self.NUM_CLASSES)

    # Initialize network
    self.mnist.init_net(self.images_pl, self.labels_pl, 1e-4)

  def load(self, fname):
    saver = tf.train.Saver()
    saver.restore(self.mnist.sess, fname)

  def eval(self, img):
    img = cv2.resize(img, (28, 28))
    imgg = cv2.cvtColor(img, cv2.cv.CV_BGR2GRAY)
    imgg = imgg[:,:].reshape(imgg.size)
    imgg = imgg.reshape(1,imgg.size)
    imgf = imgg.astype(np.float32)
    imgf = np.multiply(imgf, 1.0 / 255.0)

    return self.mnist.test(feed_dict={self.images_pl: imgf,
                                     self.labels_pl: [[0] * self.NUM_CLASSES]}).flatten()
Exemplo n.º 2
0
  def __init__(self, num_classes, num_hidden1, num_hidden2):
    self.NUM_CLASSES = num_classes
    self.IMAGE_SIZE = 28
    self.IMAGE_PIXELS = self.IMAGE_SIZE * self.IMAGE_SIZE

    self.images_pl = tf.placeholder(tf.float32, shape=(None, self.IMAGE_PIXELS))
    if FLAGS.one_hot:
      self.labels_pl = tf.placeholder(tf.int32, shape=(None))
    else:
      self.labels_pl = tf.placeholder(tf.int32, shape=(None, self.NUM_CLASSES))

    # Generate network
    self.mnist = FourLayeredFFNN(self.NUM_CLASSES)
    #self.mnist = FourLayeredFFCNN(self.NUM_CLASSES)

    # Initialize network
    self.mnist.init_net(self.images_pl, self.labels_pl, 1e-4)
Exemplo n.º 3
0
def run_training():
  """Train MNIST for a number of steps."""
  # Get the sets of images and labels for training, validation, and
  # test on MNIST.
  data_sets = input_data.read_data_sets(FLAGS.train_dir, FLAGS.fake_data, one_hot=FLAGS.one_hot)

  # Tell TensorFlow that the model will be built into the default Graph.
  with tf.Graph().as_default():
    # Generate network
    mnist = FourLayeredFFNN()
    #mnist = FourLayeredFFCNN()

    # Generate placeholders for the images and labels.
    if FLAGS.one_hot == True:
      images_placeholder, labels_placeholder = placeholder_inputs(
          FLAGS.batch_size, mnist.IMAGE_PIXELS, mnist.NUM_CLASSES)
    else:
      images_placeholder, labels_placeholder = placeholder_inputs(
          FLAGS.batch_size, mnist.IMAGE_PIXELS)

    # Initialize network
    mnist.init_net(images_placeholder, labels_placeholder, FLAGS.learning_rate)
    mnist.init_val()

    # Build the summary operation based on the TF collection of Summaries.
    summary_op = tf.merge_all_summaries()

    # Create a saver for writing training checkpoints.
    saver = tf.train.Saver()

    # Instantiate a SummaryWriter to output summaries and the Graph.
    summary_writer = tf.train.SummaryWriter(FLAGS.train_dir,
                                            graph_def=mnist.sess.graph_def)

    # And then after everything is built, start the training loop.
    for step in xrange(FLAGS.max_steps):
      start_time = time.time()

      # Fill a feed dictionary with the actual set of images and labels
      # for this particular training step.
      feed_dict = fill_feed_dict(data_sets.train,
                                 images_placeholder,
                                 labels_placeholder)

      # Run one step of the model.  The return values are the activations
      # from the `train_op` (which is discarded) and the `loss` Op.  To
      # inspect the values of your Ops or variables, you may include them
      # in the list passed to sess.run() and the value tensors will be
      # returned in the tuple from the call.
      loss_value = mnist.train(feed_dict)

      duration = time.time() - start_time

      # Write the summaries and print an overview fairly often.
      if step % 100 == 0:
        # Print status to stdout.
        print('Step %d: loss = %.2f (%.3f sec)' % (step, loss_value, duration))
        # Update the events file.
        summary_str = mnist.run(summary_op, feed_dict=feed_dict)
        summary_writer.add_summary(summary_str, step)

      # Save a checkpoint and evaluate the model periodically.
      if (step + 1) % 1000 == 0 or (step + 1) == FLAGS.max_steps:
        saver.save(mnist.sess, FLAGS.train_dir, global_step=step)
        # Evaluate against the training set.
        print('Training Data Eval:')
        do_eval(mnist,
                images_placeholder,
                labels_placeholder,
                data_sets.train)
        # Evaluate against the validation set.
        print('Validation Data Eval:')
        do_eval(mnist,
                images_placeholder,
                labels_placeholder,
                data_sets.validation)
        # Evaluate against the test set.
        print('Test Data Eval:')
        do_eval(mnist,
                images_placeholder,
                labels_placeholder,
                data_sets.test)