Example #1
0
def _generate_image_and_label_batch(image, label, min_queue_examples,
                                    batch_size, shuffle):
    """ generate a batch of images and labels.

    Args:
        image: the trained image.
        label: label correspond to the image.
        min_queue_examples: the least examples int the example's queue.
        batch_size: the size of a batch.
        shuffle: whether or not to shuffle the examples.

    Returns:
        A batch of examples including images and the corresponding label.
    """
    num_preprocess_threads = 16
    if shuffle:
        images, label_batch = tf.train.shuffle_batch(
            [image, label],
            batch_size=batch_size,
            num_threads=num_preprocess_threads,
            capacity=min_queue_examples + 3 * batch_size,
            min_after_dequeue=min_queue_examples)
    else:
        images, label_batch = tf.train.batch(
            [image, label],
            batch_size=batch_size,
            num_threads=num_preprocess_threads,
            capacity=min_queue_examples + 3 * batch_size)

    # Display the training images in the visualizer.
    tf.image_summary('images', images)
    return images, label_batch
Example #2
0
def _generate_image_and_label_batch(image, label, min_queue_examples,
                                    batch_size, shuffle):
  """Construct a queued batch of images and labels.
  Args:
    image: 3-D Tensor of [height, width, 3] of type.float32.
    label: 1-D Tensor of type.int32
    min_queue_examples: int32, minimum number of samples to retain
      in the queue that provides of batches of examples.
    batch_size: Number of images per batch.
    shuffle: boolean indicating whether to use a shuffling queue.
  Returns:
    images: Images. 4D tensor of [batch_size, height, width, 3] size.
    labels: Labels. 1D tensor of [batch_size] size.
  """
  # Create a queue that shuffles the examples, and then
  # read 'batch_size' images + labels from the example queue.
  num_preprocess_threads = 16
  if shuffle:
    images, label_batch = tf.train.shuffle_batch(
        [image, label],
        batch_size=batch_size,
        num_threads=num_preprocess_threads,
        capacity=min_queue_examples + 3 * batch_size,
        min_after_dequeue=min_queue_examples)
  else:
    images, label_batch = tf.train.batch(
        [image, label],
        batch_size=batch_size,
        num_threads=num_preprocess_threads,
        capacity=min_queue_examples + 3 * batch_size)

  # Display the training images in the visualizer.
  tf.image_summary('images', images)

  return images, tf.reshape(label_batch, [batch_size])
def nerve_inputs(batch_size):
  """ Construct nerve input net.
  Args:
    batch_size: Number of images per batch.
  Returns:
    images: Images. 4D tensor. Possible of size [batch_size, 84x84x4].
    mask: Images. 4D tensor. Possible of size [batch_size, 84x84x4].
  """

  shape = (420,580)

  tfrecord_filename = glb('../data/tfrecords/*') 
  print(tfrecord_filename)
  
  filename_queue = tf.train.string_input_producer(tfrecord_filename) 

  image, mask = read_data(filename_queue, shape)

  images, masks = _generate_image_label_batch(image, mask, batch_size)
 
  # display in tf summary page 
  tf.image_summary('images', images)
  tf.image_summary('mask', masks)

  return images, masks 
Example #4
0
def inputs(files, distort=False):
    fqueue = tf.train.string_input_producer(files)
    reader = tf.TFRecordReader()
    key, value = reader.read(fqueue)
    features = tf.parse_single_example(value, features={
        'label': tf.FixedLenFeature([], tf.int64),
        'image_raw': tf.FixedLenFeature([], tf.string),
    })
    image = tf.image.decode_jpeg(features['image_raw'], channels=3)
    image = tf.cast(image, tf.float32)

    if distort:
        cropsize = random.randint(INPUT_SIZE, IMAGE_SIZE)
        image = tf.image.random_crop(image, [cropsize, cropsize])
        image = tf.image.random_flip_left_right(image)
        image = tf.image.random_brightness(image, max_delta=0.63)
        image = tf.image.random_contrast(image, lower=0.8, upper=1.2)
        image = tf.image.random_hue(image, max_delta=0.02)
        image = tf.image.random_saturation(image, lower=0.8, upper=1.2)
    else:
        image = tf.image.random_crop(image, [IMAGE_SIZE, IMAGE_SIZE])
        image = tf.image.resize_image_with_crop_or_pad(image, INPUT_SIZE, INPUT_SIZE)

    min_fraction_of_examples_in_queue = 0.4
    min_queue_examples = int(FLAGS.num_examples_per_epoch_for_train * min_fraction_of_examples_in_queue)
    images, labels = tf.train.shuffle_batch(
        [tf.image.per_image_whitening(image), tf.cast(features['label'], tf.int32)],
        batch_size=BATCH_SIZE,
        capacity=min_queue_examples + 3 * BATCH_SIZE,
        min_after_dequeue=min_queue_examples
    )
    images = tf.image.resize_images(images, INPUT_SIZE, INPUT_SIZE)
    tf.image_summary('images', images)
    return images, labels
Example #5
0
def _multichannel_image_summary(name, images, perm=[0, 3, 1, 2], max_summary_images=16):
    _min = tf.reduce_min(images)
    _max = tf.reduce_max(images)
    _ = tf.mul(tf.div(tf.add(images, _min), tf.sub(_max, _min)), 255.0)
    _ = tf.transpose(_, perm=perm)
    shape = _.get_shape().as_list()
    tf.image_summary(name, tf.reshape(tf.transpose(_, perm=perm), [reduce(lambda x,y:x*y, shape)/(shape[3]*shape[2]), shape[2], shape[3], 1]), max_images=max_summary_images)
Example #6
0
 def preprocess(self):
     with tf.name_scope('input'):
         x = tf.placeholder(tf.float32, [None, 784], name='x-input')
         image_shaped_input = tf.reshape(x, [-1, 28, 28, 1])
         tf.image_summary('input', image_shaped_input, max_images=100)
         y_ = tf.placeholder(tf.float32, [None, 10], name='y-input')
         return (x, y_)
Example #7
0
def _conv(inpOp, kH, kW, nOut, dH=1, dW=1, relu=True):
    global conv_counter
    global parameters
    name = 'conv' + str(conv_counter)
    conv_counter += 1
    with tf.name_scope(name) as scope:
        nIn = int(inpOp.get_shape()[-1])
        stddev = 5e-3
        kernel = tf.Variable(tf.truncated_normal([kH, kW, nIn, nOut],
                                                 dtype=tf.float32,
                                                 stddev=(kH*kW*nIn)**0.5*stddev), name='weights')
        
        conv = tf.nn.conv2d(inpOp, kernel, [1, 1, 1, 1],
                         padding="SAME")

        biases = tf.Variable(tf.constant(0.0, shape=[nOut], dtype=tf.float32),
                             trainable=True, name='biases')
        bias = tf.reshape(tf.nn.bias_add(conv, biases), conv.get_shape())
        if relu:
          bias = tf.nn.relu(bias, name=scope)
        #parameters += [kernel, biases]
        #bias = tf.Print(bias, [tf.sqrt(tf.reduce_mean(tf.square(inpOp - tf.reduce_mean(inpOp))))], message=kernel.name)
        tf.histogram_summary(scope+"/output", bias)
        tf.image_summary(scope+"/output", bias[:,:,:,0:3])
        tf.image_summary(scope+"/kernel_weight", tf.expand_dims(kernel[:,:,0:3,0], 0))
        # tf.image_summary(scope+"/point_weight", pointwise_filter)
        
        return bias
Example #8
0
def _generate_image_and_label_batch(image, label, filename, min_queue_examples,
                                    batch_size, shuffle):

    # Create a queue that shuffles the examples, and then
    # read 'batch_size' images + labels from the example queue.
    num_preprocess_threads = 16
    capacity = min_queue_examples + 3 * batch_size

    if shuffle:
        images, label_batch, filename = tf.train.shuffle_batch(
            [image, label, filename],
            batch_size=batch_size,
            num_threads=num_preprocess_threads,
            capacity=capacity,
            min_after_dequeue=min_queue_examples)
    else:
        images, label_batch, filename = tf.train.batch(
            [image, label, filename],
            batch_size=batch_size,
            num_threads=num_preprocess_threads,
            capacity=min_queue_examples + 3 * batch_size)

    # Display the training images in the visualizer.
    tf.image_summary('image', images, max_images = 100)

    labels = tf.reshape(label_batch, [batch_size, NUM_CLASS])
    return images, labels, filename
Example #9
0
def _generate_image_and_label_batch(image, label, min_queue_examples):
  """Construct a queued batch of images and labels.

  Args:
    image: 3-D Tensor of [IMAGE_SIZE, IMAGE_SIZE, 3] of type.float32.
    label: 1-D Tensor of type.int32
    min_queue_examples: int32, minimum number of samples to retain
      in the queue that provides of batches of examples.

  Returns:
    images: Images. 4D tensor of [batch_size, IMAGE_SIZE, IMAGE_SIZE, 3] size.
    labels: Labels. 1D tensor of [batch_size] size.
  """
  # Create a queue that shuffles the examples, and then
  # read 'FLAGS.batch_size' images + labels from the example queue.
  num_preprocess_threads = 16
  images, label_batch = tf.train.shuffle_batch(
      [image, label],
      batch_size=FLAGS.batch_size,
      num_threads=num_preprocess_threads,
      capacity=min_queue_examples + 3 * FLAGS.batch_size,
      min_after_dequeue=min_queue_examples)

  # Display the training images in the visualizer.
  tf.image_summary('images', images)

  return images, tf.reshape(label_batch, [FLAGS.batch_size])
Example #10
0
def read_image_data():
    dirname, filename = os.path.split(os.path.abspath(__file__))
    #Create a list of filenames
    #path = '/home/david/datasets/fs_ready/Aaron_Eckhart/'
    jpeg_files = glob.glob(os.path.join(path, '*.jpg'))
    path = '/home/david/datasets/fs_ready/Zooey_Deschanel/'
    #Create a queue that produces the filenames to read
    filename_queue = tf.train.string_input_producer(jpeg_files)
    #Create a reader for the filequeue
    reader = tf.WholeFileReader()
    #Read in the files
    key, value = reader.read(filename_queue)
    #Convert the Tensor(of type string) to representing the Tensor of type uint8
    # and shape [height, width, channels] representing the images
    images = tf.image.decode_jpeg(value, channels=3)
    #convert images to floats and attach image summary
    float_images = tf.expand_dims(tf.cast(images, tf.float32),0)
    tf.image_summary('images', float_images)
    
    #Create session
    sess = tf.Session()
    summary_op = tf.merge_all_summaries()
    tf.initialize_all_variables()
    #Write summary
    summary_writer = tf.train.SummaryWriter(dirname+'/log/', graph_def=sess.graph_def)
    tf.train.start_queue_runners(sess=sess)
    for i in xrange(10):
        summary_str, float_image = sess.run([summary_op, float_images])
        print (float_image.shape)
        summary_writer.add_summary(summary_str)
    #Close session
    sess.close()
Example #11
0
def conv2d(x, num_filters, name, filter_size=(3, 3), stride=(1, 1), pad="SAME", dtype=tf.float32, collections=None,
           summary_tag=None):
    with tf.variable_scope(name):
        stride_shape = [1, stride[0], stride[1], 1]
        filter_shape = [filter_size[0], filter_size[1], int(x.get_shape()[3]), num_filters]

        # there are "num input feature maps * filter height * filter width"
        # inputs to each hidden unit
        fan_in = intprod(filter_shape[:3])
        # each unit in the lower layer receives a gradient from:
        # "num output feature maps * filter height * filter width" /
        #   pooling size
        fan_out = intprod(filter_shape[:2]) * num_filters
        # initialize weights with random weights
        w_bound = np.sqrt(6. / (fan_in + fan_out))

        w = tf.get_variable("W", filter_shape, dtype, tf.random_uniform_initializer(-w_bound, w_bound),
                            collections=collections)
        b = tf.get_variable("b", [1, 1, 1, num_filters], initializer=tf.zeros_initializer,
                            collections=collections)

        if summary_tag is not None:
            tf.image_summary(summary_tag,
                             tf.transpose(tf.reshape(w, [filter_size[0], filter_size[1], -1, 1]),
                                          [2, 0, 1, 3]),
                             max_images=10)

        return tf.nn.conv2d(x, w, stride_shape, pad) + b
    def model(self):
        """
        Define the model
        """
        # Reshape the input for batchSize, dims_in[0] X dims_in[1] image, dims_in[2] channels
        x_image = tf.reshape(self.input, [-1, self.dims_in[0], self.dims_in[1], self.dims_in[2]],
                             name='x_input_reshaped')

        # Apply image resize
        x_image_upscale = tf.image.resize_bilinear(x_image, np.array([self.dims_out[0],
                                          self.dims_out[1]]), align_corners=None, name='x_input_upscale')

        self.x_input_upscale = x_image_upscale
        # Dump input image out
        tf.image_summary('x_upscale', x_image_upscale)

        # Model convolutions
        conv_1 = ops.conv2d(x_image_upscale, output_dim=8, k_h=5, k_w=5, d_h=1, d_w=1, name="conv_1")
        relu_1 = tf.nn.relu(conv_1)

        conv_2 = ops.conv2d(relu_1, output_dim=4, k_h=3, k_w=3, d_h=1, d_w=1, name="conv_2")
        relu_2 = tf.nn.relu(conv_2)

        conv_3 = ops.conv2d(relu_2, output_dim=1, k_h=1, k_w=1, d_h=1, d_w=1, name="conv_3")
        relu_3 = tf.nn.relu(conv_3)

        conv_4 = ops.conv2d(relu_3, output_dim=1, k_h=3, k_w=3, d_h=1, d_w=1, name="conv_4")

        predict = tf.reshape(conv_4, [-1, self.dims_out[0], self.dims_out[1], self.dims_out[2]], name='predict')

        # Dump prediction out
        tf.image_summary('predict', predict)
        return predict
Example #13
0
def _deconv(inpOp, kH, kW, nOut, dH=1, dW=1, relu=True, name=None):
    global deconv_counter
    global parameters
    if not name:
      name = 'deconv' + str(deconv_counter)
    deconv_counter += 1
    with tf.variable_scope(name) as scope:
        nIn = int(inpOp.get_shape()[-1])
        in_shape = inpOp.get_shape()
        stddev = 1e-3
        kernel = tf.get_variable('weights',[kH, kW, nOut, nIn], initializer=tf.random_normal_initializer(stddev=(kH*kW*nIn)**0.5*stddev))
        
        conv = tf.nn.deconv2d(inpOp, kernel, [int(in_shape[0]),int(in_shape[1]),int(in_shape[2]),nOut], [1, 1, 1, 1],
                         padding="SAME")
                         
        biases = tf.get_variable('biases', [nOut], initializer=tf.constant_initializer(value=0.0))
        bias = tf.reshape(tf.nn.bias_add(conv, biases), conv.get_shape())
        if relu:
          bias = tf.nn.relu(bias, name='relu')
        #parameters += [kernel, biases]
        #bias = tf.Print(bias, [tf.sqrt(tf.reduce_mean(tf.square(inpOp - tf.reduce_mean(inpOp))))], message=kernel.name)
        tf.histogram_summary(bias.name+"/output", bias)
        tf.image_summary(bias.name+"/output", bias[:,:,:,0:3])
        #tf.image_summary(scope+"/depth_weight", depthwise_filter)
        # tf.image_summary(scope+"/point_weight", pointwise_filter)
        
        return bias
Example #14
0
def inputs(eval_data, data_dir, batch_size):
  filename = os.path.join(data_dir, TEST_FILE)
  filename_queue = tf.train.string_input_producer([filename])
  image, label = read_and_decode(filename_queue)
  height = IMAGE_SIZE
  width = IMAGE_SIZE
  print ("THIS",image.get_shape)
  
  resized_image = tf.image.resize_images(image, height, width)
  print (resized_image.get_shape)

  # Subtract off the mean and divide by the variance of the pixels.
  float_image = tf.image.per_image_whitening(resized_image)

  # Ensure that the random shuffling has good mixing properties.
  min_fraction_of_examples_in_queue = 0.4
  min_queue_examples = int(NUM_EXAMPLES_PER_EPOCH_FOR_EVAL *
                           min_fraction_of_examples_in_queue)

  images, label_batch = tf.train.batch(
      [image, label],
      batch_size=batch_size,
      num_threads=1,
      capacity=min_queue_examples + 3 * batch_size)

  tf.image_summary('images', images)
  return images, tf.reshape(label_batch, [batch_size])
Example #15
0
def distorted_inputs (tfrecord_file_paths=[]):
    fqueue = tf.train.string_input_producer(tfrecord_file_paths)
    reader = tf.TFRecordReader()
    key, serialized_example = reader.read(fqueue)
    features = tf.parse_single_example(serialized_example, features={
        'label': tf.FixedLenFeature([], tf.int64),
        'image': tf.FixedLenFeature([], tf.string)
    })
    image = tf.image.decode_jpeg(features['image'], channels=size['depth'])
    image = tf.cast(image, tf.float32)
    image.set_shape([size['width'], size['height'], size['depth']])

    min_fraction_of_examples_in_queue = 0.4
    min_queue_examples = int(cifar10.NUM_EXAMPLES_PER_EPOCH_FOR_EVAL * min_fraction_of_examples_in_queue)

    images, labels = tf.train.shuffle_batch(
        [tf.image.per_image_whitening(image), tf.cast(features['label'], tf.int32)],
        batch_size=BATCH_SIZE,
        capacity=min_queue_examples + 3 * BATCH_SIZE,
        min_after_dequeue=min_queue_examples
    )

    images = tf.image.resize_images(images, size['input_width'], size['input_height'])
    tf.image_summary('images', images)
    return images, labels
def distorted_inputs(data_dir, batch_size):
  """Construct distorted input for CIFAR training using the Reader ops.
  Args:
    data_dir: Path to the CIFAR-10 data directory.
    batch_size: Number of images per batch.
  Returns:
    images: Images. 4D tensor of [batch_size, IMAGE_SIZE, IMAGE_SIZE, 3] size.
    labels: Labels. 1D tensor of [batch_size] size.
  """
  filenames = [os.path.join(data_dir, 'data_batch_%d.bin' % i)
               for i in xrange(1, 6)]
  for f in filenames:
    if not tf.gfile.Exists(f):
      raise ValueError('Failed to find file: ' + f)

  # Create a queue that produces the filenames to read.
  filename_queue = tf.train.string_input_producer(filenames)

  # Read examples from files in the filename queue.
  read_input = read_cifar10(filename_queue)
  reshaped_image = tf.cast(read_input.uint8image, tf.float32)

  height = IMAGE_SIZE
  width = IMAGE_SIZE

  # Image processing for training the network. Note the many random
  # distortions applied to the image.

  # Randomly crop a [height, width] section of the image.
  distorted_image = tf.random_crop(reshaped_image, [height, width, 3])

  # Randomly flip the image horizontally.
  distorted_image = tf.image.random_flip_left_right(distorted_image)

  # Because these operations are not commutative, consider randomizing
  # randomize the order their operation.
  distorted_image = tf.image.random_brightness(distorted_image,
                                               max_delta=63)
  distorted_image = tf.image.random_contrast(distorted_image,
                                             lower=0.2, upper=1.8)

  # Subtract off the mean and divide by the variance of the pixels.
  float_image = tf.image.per_image_whitening(distorted_image)

  # Ensure that the random shuffling has good mixing properties.
  min_fraction_of_examples_in_queue = 0.4
  min_queue_examples = int(NUM_EXAMPLES_PER_EPOCH_FOR_TRAIN *
                           min_fraction_of_examples_in_queue)
  print ('Filling queue with %d CIFAR images before starting to train. '
         'This will take a few minutes.' % min_queue_examples)

  # Generate a batch of images and labels by building up a queue of examples.

  images, labels = _generate_image_and_label_batch(float_image, read_input.label,
                                                   min_queue_examples, batch_size)

  tf.image_summary('distorted_images', images)

  return images, labels
    def model(data, train=False):
        """The Model definition."""
        # 2D convolution, with 'SAME' padding (i.e. the output feature map has
        # the same size as the input). Note that {strides} is a 4D array whose
        # shape matches the data layout: [image index, y, x, depth].
        conv = tf.nn.conv2d(data,
                            conv1_weights,
                            strides=[1, 1, 1, 1],
                            padding='SAME')
        # Bias and rectified linear non-linearity.
        relu = tf.nn.relu(tf.nn.bias_add(conv, conv1_biases))
        # Max pooling. The kernel size spec {ksize} also follows the layout of
        # the data. Here we have a pooling window of 2, and a stride of 2.
        pool = tf.nn.max_pool(relu,
                              ksize=[1, 2, 2, 1],
                              strides=[1, 2, 2, 1],
                              padding='SAME')

        conv2 = tf.nn.conv2d(pool,
                            conv2_weights,
                            strides=[1, 1, 1, 1],
                            padding='SAME')
        relu2 = tf.nn.relu(tf.nn.bias_add(conv2, conv2_biases))
        pool2 = tf.nn.max_pool(relu2,
                              ksize=[1, 2, 2, 1],
                              strides=[1, 2, 2, 1],
                              padding='SAME')



        # Reshape the feature map cuboid into a 2D matrix to feed it to the
        # fully connected layers.
        pool_shape = pool2.get_shape().as_list()
        reshape = tf.reshape(
            pool2,
            [pool_shape[0], pool_shape[1] * pool_shape[2] * pool_shape[3]])
        # Fully connected layer. Note that the '+' operation automatically
        # broadcasts the biases.
        hidden = tf.nn.relu(tf.matmul(reshape, fc1_weights) + fc1_biases)
        # Add a 50% dropout during training only. Dropout also scales
        # activations such that no rescaling is needed at evaluation time.
        #if train:
        #    hidden = tf.nn.dropout(hidden, 0.5, seed=SEED)
        out = tf.matmul(hidden, fc2_weights) + fc2_biases

        if train == True:
            summary_id = '_0'
            s_data = get_image_summary(data)
            filter_summary0 = tf.image_summary('summary_data' + summary_id, s_data)
            s_conv = get_image_summary(conv)
            filter_summary2 = tf.image_summary('summary_conv' + summary_id, s_conv)
            s_pool = get_image_summary(pool)
            filter_summary3 = tf.image_summary('summary_pool' + summary_id, s_pool)
            s_conv2 = get_image_summary(conv2)
            filter_summary4 = tf.image_summary('summary_conv2' + summary_id, s_conv2)
            s_pool2 = get_image_summary(pool2)
            filter_summary5 = tf.image_summary('summary_pool2' + summary_id, s_pool2)

        return out
def main(argv=None):
    print "Reading MNIST data..."
    data = mnist.input_data.read_data_sets("MNIST_data", one_hot=True)
    images = tf.placeholder(tf.float32, [None, IMAGE_SIZE * IMAGE_SIZE])
    tf.image_summary("Input", tf.reshape(images, [-1, IMAGE_SIZE, IMAGE_SIZE, 1]), max_images=1)
    print "Setting up inference..."
    encoded, output_image = inference_fc(images)
    tf.image_summary("Output", tf.reshape(output_image, [-1, IMAGE_SIZE, IMAGE_SIZE, 1]), max_images=1)

    print "Loss setup..."
    loss1 = tf.nn.l2_loss(tf.sub(output_image, images)) / (IMAGE_SIZE * IMAGE_SIZE)
    loss2 = tf.add_n(tf.get_collection("losses"))
    loss = loss1 + FLAGS.regularization * loss2
    tf.scalar_summary("Loss", loss)
    tf.scalar_summary("Encoder_loss", loss1)
    tf.scalar_summary("Reg_loss", loss2)

    print "Setting up optimizer..."
    train_op = tf.train.AdamOptimizer(LEARNING_RATE).minimize(loss)

    print 'Setting up graph summary...'
    summary_op = tf.merge_all_summaries()

    # print "Creating matplot fig"
    # fig = plt.figure()
    # ax = fig.add_subplot(111, projection='3d')

    with tf.Session() as sess:
        summary_writer = tf.train.SummaryWriter(FLAGS.logs_dir, sess.graph_def)
        print "Creating saver..."
        saver = tf.train.Saver()
        sess.run(tf.initialize_all_variables())
        ckpt = tf.train.get_checkpoint_state(FLAGS.logs_dir)
        if ckpt and ckpt.model_checkpoint_path:
            saver.restore(sess, ckpt.model_checkpoint_path)
            print "Model restored..."

        for step in xrange(MAX_ITERATIONS):
            batch_image, batch_label = data.train.next_batch(FLAGS.batch_size)
            feed_dict = {images: batch_image}
            if step % 100 == 0:
                summary_str, loss_val = sess.run([summary_op, loss], feed_dict=feed_dict)
                print "Step %d Train loss %f" % (step, loss_val)
                summary_writer.add_summary(summary_str, global_step=step)

            if step % 1000 == 0:
                saver.save(sess, FLAGS.logs_dir + "model.ckpt", global_step=step)
                test_compression = sess.run(encoded, feed_dict={images: data.test.images})
                labels = np.argmax(data.test.labels, axis=1).reshape((-1, 1))
                write_file = os.path.join(FLAGS.logs_dir, "checkpoint%d.txt" % step)
                write_arr = np.hstack((test_compression, np.argmax(data.test.labels, axis=1).reshape((-1, 1))))
                np.savetxt(write_file, write_arr)
                # ax.clear()
                # ax.scatter(test_compression[:, 0], test_compression[:, 1], test_compression[:, 2], s=10,
                #            c=COLORS[labels], marker='o')
                # plt.show()
            sess.run(train_op, feed_dict=feed_dict)
Example #19
0
    def _get_cost(self, input_vars, is_training):
        image, label = input_vars
        keep_prob = tf.constant(0.5 if is_training else 1.0)

        if is_training:
            image, label = tf.train.shuffle_batch(
                [image, label], BATCH_SIZE, CAPACITY, MIN_AFTER_DEQUEUE,
                num_threads=6, enqueue_many=True)
            tf.image_summary("train_image", image, 10)

        image = image / 4.0     # just to make range smaller
        l = Conv2D('conv1.1', image, out_channel=64, kernel_shape=3)
        l = Conv2D('conv1.2', l, out_channel=64, kernel_shape=3, nl=tf.identity)
        l = BatchNorm('bn1', l, is_training)
        l = tf.nn.relu(l)
        l = MaxPooling('pool1', l, 3, stride=2, padding='SAME')

        l = Conv2D('conv2.1', l, out_channel=128, kernel_shape=3)
        l = Conv2D('conv2.2', l, out_channel=128, kernel_shape=3, nl=tf.identity)
        l = BatchNorm('bn2', l, is_training)
        l = tf.nn.relu(l)
        l = MaxPooling('pool2', l, 3, stride=2, padding='SAME')

        l = Conv2D('conv3.1', l, out_channel=128, kernel_shape=3, padding='VALID')
        l = Conv2D('conv3.2', l, out_channel=128, kernel_shape=3, padding='VALID', nl=tf.identity)
        l = BatchNorm('bn3', l, is_training)
        l = tf.nn.relu(l)
        l = FullyConnected('fc0', l, 1024 + 512,
                           b_init=tf.constant_initializer(0.1))
        l = tf.nn.dropout(l, keep_prob)
        l = FullyConnected('fc1', l, out_dim=512,
                           b_init=tf.constant_initializer(0.1))
        # fc will have activation summary by default. disable for the output layer
        logits = FullyConnected('linear', l, out_dim=10, summary_activation=False,
                                nl=tf.identity)
        prob = tf.nn.softmax(logits, name='output')

        y = one_hot(label, 10)
        cost = tf.nn.softmax_cross_entropy_with_logits(logits, y)
        cost = tf.reduce_mean(cost, name='cross_entropy_loss')
        tf.add_to_collection(MOVING_SUMMARY_VARS_KEY, cost)

        # compute the number of failed samples, for ValidationError to use at test time
        wrong = prediction_incorrect(logits, label)
        nr_wrong = tf.reduce_sum(wrong, name='wrong')
        # monitor training error
        tf.add_to_collection(
            MOVING_SUMMARY_VARS_KEY, tf.reduce_mean(wrong, name='train_error'))

        # weight decay on all W of fc layers
        wd_cost = tf.mul(0.004,
                         regularize_cost('fc.*/W', tf.nn.l2_loss),
                         name='regularize_loss')
        tf.add_to_collection(MOVING_SUMMARY_VARS_KEY, wd_cost)

        add_param_summary([('.*/W', ['histogram', 'sparsity'])])   # monitor W
        return tf.add_n([cost, wd_cost], name='cost')
Example #20
0
    def set_model(self, model):
        self.model = model
        self.sess = K.get_session()
        if self.histogram_freq and self.merged is None:
            for layer in self.model.layers:

                for weight in layer.weights:
                    if hasattr(tf, 'histogram_summary'):
                        tf.histogram_summary(weight.name, weight)
                    else:
                        tf.summary.histogram(weight.name, weight)

                    if self.write_images:
                        w_img = tf.squeeze(weight)

                        shape = w_img.get_shape()
                        if len(shape) > 1 and shape[0] > shape[1]:
                            w_img = tf.transpose(w_img)

                        if len(shape) == 1:
                            w_img = tf.expand_dims(w_img, 0)

                        w_img = tf.expand_dims(tf.expand_dims(w_img, 0), -1)

                        if hasattr(tf, 'image_summary'):
                            tf.image_summary(weight.name, w_img)
                        else:
                            tf.summary.image(weight.name, w_img)

                if hasattr(layer, 'output'):
                    if hasattr(tf, 'histogram_summary'):
                        tf.histogram_summary('{}_out'.format(layer.name),
                                             layer.output)
                    else:
                        tf.summary.histogram('{}_out'.format(layer.name),
                                             layer.output)

        if hasattr(tf, 'merge_all_summaries'):
            self.merged = tf.merge_all_summaries()
        else:
            self.merged = tf.summary.merge_all()

        if self.write_graph:
            if hasattr(tf, 'summary') and hasattr(tf.summary, 'FileWriter'):
                self.writer = tf.summary.FileWriter(self.log_dir,
                                                    self.sess.graph)
            elif parse_version(tf.__version__) >= parse_version('0.8.0'):
                self.writer = tf.train.SummaryWriter(self.log_dir,
                                                     self.sess.graph)
            else:
                self.writer = tf.train.SummaryWriter(self.log_dir,
                                                     self.sess.graph_def)
        else:
            if hasattr(tf, 'summary') and hasattr(tf.summary, 'FileWriter'):
                self.writer = tf.summary.FileWriter(self.log_dir)
            else:
                self.writer = tf.train.SummaryWriter(self.log_dir)
def collectImage(name,tensor,num=5):
    """
    :param name: 用于显示的名字前缀
    :param tensor:输入图片batch
    :param num:需要显示的图片数量
    :return:
    """
    temp = tf.reduce_mean(tensor,axis=3,keep_dims=True)
    tf.image_summary(tag=name,tensor=temp,max_images=num)
Example #22
0
def main():
  with tf.Graph().as_default():
    if not dataset_dir:
      raise ValueError('You must supply the dataset directory with --dataset_dir')

    deploy_config = model_deploy.DeploymentConfig(
        num_clones=num_clones,
        clone_on_cpu=clone_on_cpu,
        replica_id=task,
        num_replicas=worker_replicas,
        num_ps_tasks=num_ps_tasks)

    dataset = dataset_factory.get_dataset(
        dataset_name, dataset_split_name, dataset_dir)

    image_preprocessing_fn = preprocessing_factory.get_preprocessing(
        preprocessing_name,
        is_training=True)

    with tf.device(deploy_config.inputs_device()):
      with tf.name_scope('inputs'):
        provider = slim.dataset_data_provider.DatasetDataProvider(
            dataset,
            num_readers=num_readers,
            common_queue_capacity=20 * batch_size,
            common_queue_min=10 * batch_size)
        [image, label, fp] = provider.get(['image', 'label', 'filepath'])
        label -= labels_offset

    train_image_size = 224

    image = image_preprocessing_fn(image, train_image_size,
                                   train_image_size)

    images, labels, fps = tf.train.batch(
        [image, label, fp],
        batch_size=batch_size,
        num_threads=num_preprocessing_threads,
        capacity=5 * batch_size)
    tf.image_summary('image', images, max_images=5)
    labels = slim.one_hot_encoding(
        labels, dataset.num_classes - labels_offset)
    batch_queue = slim.prefetch_queue.prefetch_queue(
        [images, labels, fps], capacity=2 * deploy_config.num_clones)

    images, labels, fps = batch_queue.dequeue()

    sess = tf.Session()
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess, coord)

    image_data, label_data, fp_data = sess.run([images, labels, fps])

    coord.request_stop()
    coord.join(threads)
    sess.close()
    return image_data, label_data, fp_data
Example #23
0
    def set_activation_summary(self):
        '''Log each layers activations and sparsity.'''
        tf.image_summary("input images", self.input_layer.output, max_images=100)

        for var in tf.trainable_variables():
            tf.histogram_summary(var.op.name, var)

        for layer in self.hidden_layers:
            tf.histogram_summary(layer.name + '/activations', layer.output)
            tf.scalar_summary(layer.name + '/sparsity', tf.nn.zero_fraction(layer.output))
def _filter_summary(x):
    '''
    filterの可視化サマリ
    '''
    x_input = tf.get_default_graph().as_graph_element(x)
    x_viz = tf.transpose(x_input, perm=[3, 0, 1, 2])
    #[:, :, :, : 0]
    #pool5_flat = tf.reshape(pool5, [FLAGS.batch_size, dim])
    tensor_name = re.sub('%s_[0-9]*/' % TOWER_NAME, '', x.op.name)
    tf.image_summary(tensor_name + '/filters', x_viz, max_images=24)
Example #25
0
 def masked_mpse(predictions, labels, weight):
   """Masked mpse assuming we have a depth to create a mask from."""
   assert labels.shape.as_list()[-1] == 4
   mask = tf.to_float(tf.less(labels[:, :, :, 3:4], 0.99))
   mask = tf.tile(mask, [1, 1, 1, 4])
   predictions *= mask
   labels *= mask
   tf.image_summary('masked_pred', predictions)
   tf.image_summary('masked_label', labels)
   return tf.contrib.losses.mean_pairwise_squared_error(
       predictions, labels, weight)
Example #26
0
def ImageInput(input_pattern, num_threads, shape, using_ctc, reader=None):
  """Creates an input image tensor from the input_pattern filenames.

  TODO(rays) Expand for 2-d labels, 0-d labels, and logistic targets.
  Args:
    input_pattern:  Filenames of the dataset(s) to read.
    num_threads:    Number of preprocessing threads.
    shape:          ImageShape with the desired shape of the input.
    using_ctc:      Take the unpadded_class labels instead of padded.
    reader:         Function that returns an actual reader to read Examples from
      input files. If None, uses tf.TFRecordReader().
  Returns:
    images:   Float Tensor containing the input image scaled to [-1.28, 1.27].
    heights:  Tensor int64 containing the heights of the images.
    widths:   Tensor int64 containing the widths of the images.
    labels:   Serialized SparseTensor containing the int64 labels.
    sparse_labels:   Serialized SparseTensor containing the int64 labels.
    truths:   Tensor string of the utf8 truth texts.
  Raises:
    ValueError: if the optimizer type is unrecognized.
  """
  data_files = tf.gfile.Glob(input_pattern)
  assert data_files, 'no files found for dataset ' + input_pattern
  queue_capacity = shape.batch_size * num_threads * 2
  filename_queue = tf.train.string_input_producer(
      data_files, capacity=queue_capacity)

  # Create a subgraph with its own reader (but sharing the
  # filename_queue) for each preprocessing thread.
  images_and_label_lists = []
  for _ in range(num_threads):
    image, height, width, labels, text = _ReadExamples(filename_queue, shape,
                                                       using_ctc, reader)
    images_and_label_lists.append([image, height, width, labels, text])
  # Create a queue that produces the examples in batches.
  images, heights, widths, labels, truths = tf.train.batch_join(
      images_and_label_lists,
      batch_size=shape.batch_size,
      capacity=16 * shape.batch_size,
      dynamic_pad=True)
  # Deserialize back to sparse, because the batcher doesn't do sparse.
  labels = tf.deserialize_many_sparse(labels, tf.int64)
  sparse_labels = tf.cast(labels, tf.int32)
  labels = tf.sparse_tensor_to_dense(labels)
  labels = tf.reshape(labels, [shape.batch_size, -1], name='Labels')
  # Crush the other shapes to just the batch dimension.
  heights = tf.reshape(heights, [-1], name='Heights')
  widths = tf.reshape(widths, [-1], name='Widths')
  truths = tf.reshape(truths, [-1], name='Truths')
  # Give the images a nice name as well.
  images = tf.identity(images, name='Images')

  tf.image_summary('Images', images)
  return images, heights, widths, labels, sparse_labels, truths
Example #27
0
def _generate_image_and_label_for_test(image, label):
	images, label_batch = tf.train.batch(
		[image, label],
		batch_size=1, # number of test images
		num_threads=16,
		capacity=1)

  	# Display the training images in the visualizer.
  	tf.image_summary('images', images)

  	return images, tf.reshape(label_batch, [1])
Example #28
0
def inference(graph):
    #Stich togheter different layers
    stiched = stichMutipleLayers(getLastLayersFromGraph(graph))

    #Predict and upscale
    pred_region = findRegions(stiched)
    old_shape = pred_region.get_shape().as_list()
    softmaxed = tf.reshape(tf.nn.softmax(tf.reshape(pred_region, (-1, 2))), [-1] + old_shape[1:])
    tf.histogram_summary('pred_region', pred_region)
    tf.image_summary('predicted regions', tf.expand_dims(softmaxed[:, :, :, 0], 3))
    return softmaxed
Example #29
0
def alphago(_X, _weights, _biases, _dropout):
    # Reshape input picture
    _X = tf.reshape(_X, shape=[-1, 15, 15, 1])

    # Convolution Layer
    conv1 = conv2d('conv1', _X, _weights['wc1'], _biases['bc1'])
    print conv1
    # Max Pooling (down-sampling)
    pool1 = max_pool('pool1', conv1, k=2)
    print pool1
    # Apply Normalization
    norm1 = norm('norm1', pool1, lsize=4)
    print norm1
    # Apply Dropout
    norm1 = tf.nn.dropout(norm1, _dropout)
    #conv1 image show
    tf.image_summary("conv1", conv1)
    
    # Convolution Layer
    
    conv2 = conv2d('conv2', norm1, _weights['wc2'], _biases['bc2'])
    print conv2
    # Max Pooling (down-sampling)
    pool2 = max_pool('pool2', conv2, k=2)
    print pool2
    # Apply Normalization
    norm2 = norm('norm2', pool2, lsize=4)
    print norm2
    # Apply Dropout
    norm2 = tf.nn.dropout(norm2, _dropout)

    # Convolution Layer
    conv3 = conv2d('conv3', norm2, _weights['wc3'], _biases['bc3'])
    print conv3
    # Max Pooling (down-sampling)
    pool3 = max_pool('pool3', conv3, k=2)
    print pool3
    # Apply Normalization
    norm3 = norm('norm3', pool3, lsize=4)
    
    # Apply Dropout
    norm3 = tf.nn.dropout(norm3, _dropout)
   
    # Fully connected layer
    print norm3
    print _weights['wd1'].get_shape().as_list()[0]
    dense1 = tf.reshape(norm3, [-1, _weights['wd1'].get_shape().as_list()[0]]) # Reshape conv3 output to fit dense layer input
    dense1 = tf.nn.relu(tf.matmul(dense1, _weights['wd1']) + _biases['bd1'], name='fc1') # Relu activation

    dense2 = tf.nn.relu(tf.matmul(dense1, _weights['wd2']) + _biases['bd2'], name='fc2') # Relu activation
    print dense2
    # Output, class prediction
    out = tf.matmul(dense2, _weights['out']) + _biases['out']
    return out
def inference(images, keep_prob):
    def weight_variable(shape):
        initial = tf.truncated_normal(shape, stddev=0.1)
        return tf.Variable(initial)

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

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

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

    x_image = tf.reshape(images, [-1, IMAGE_SIZE, IMAGE_SIZE, IMAGE_COLORS])
    tf.image_summary('images', x_image)

    with tf.name_scope('conv1'):
        W_conv1 = weight_variable([5, 5, IMAGE_COLORS, 32])
        b_conv1 = bias_variable([32])
        h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
        tf.histogram_summary('W_conv1', W_conv1)
        tf.histogram_summary('b_conv1', b_conv1)
        tf.histogram_summary('h_conv1', h_conv1)

    with tf.name_scope('pool1'):
        h_pool1 = max_pool_2x2(h_conv1)

    with tf.name_scope('conv2'):
        W_conv2 = weight_variable([5, 5, 32, 64])
        b_conv2 = bias_variable([64])
        h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)

    with tf.name_scope('pool2'):
        h_pool2 = max_pool_2x2(h_conv2)

    with tf.name_scope('fc1'):
        W_fc1 = weight_variable([7 * 7 * 64, 1024])
        b_fc1 = bias_variable([1024])
        h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64])
        h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)

    with tf.name_scope('drop'):
        h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)

    with tf.name_scope('fc2'):
        W_fc2 = weight_variable([1024, NUM_CLASSES])
        b_fc2 = bias_variable([NUM_CLASSES])
        y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
        tf.histogram_summary('W_fc2', W_fc2)
        tf.histogram_summary('b_fc2', b_fc2)

    return y_conv
Example #31
0
def train(total_loss, global_step):

    # Generate moving averages of all losses and associated summaries.
    loss_averages_op = _add_loss_summaries(total_loss)

    # Compute gradients.
    with tf.control_dependencies([loss_averages_op]):
        opt = tf.train.AdamOptimizer()
        grads = opt.compute_gradients(total_loss)

        # Apply gradients.
        apply_gradient_op = opt.apply_gradients(grads, global_step=global_step)

    # Add histograms for trainable variables.
    for var in tf.trainable_variables():
        tf.histogram_summary(var.op.name, var)

    # Add histograms for gradients.
    for grad, var in grads:
        if grad is not None:
            tf.histogram_summary(var.op.name + '/gradients', grad)

    # Add conv1 filter images
    with tf.variable_scope("conv1"):
        tf.get_variable_scope().reuse_variables()
        weights = tf.get_variable("weights")
        grid = put_kernels_on_grid(weights)
        tf.image_summary("conv1/features", grid, max_images=1)
        w = tf.transpose(weights, [3, 0, 1, 2])
        tf.image_summary("conv1/features_isolated", w, max_images=64)

    # Add conv2 filter images
    with tf.variable_scope("conv2"):
        tf.get_variable_scope().reuse_variables()
        weights = tf.get_variable("weights")
        grid = put_kernels_on_grid(weights[:, :, :1, :])
        tf.image_summary("conv2/features", grid, max_images=1)
        #tf.image_summary("conv2/features_isolated", weights, max_images = 5)

    # Track the moving averages of all trainable variables.
    variable_averages = tf.train.ExponentialMovingAverage(
        MOVING_AVERAGE_DECAY, global_step)
    variables_averages_op = variable_averages.apply(tf.trainable_variables())

    with tf.control_dependencies([apply_gradient_op, variables_averages_op]):
        train_op = tf.no_op(name='train')

    return train_op
Example #32
0
  def build_model(self,batch_size,images,labels,numclasses,is_training,restore):

    self.z = tf.random_uniform([batch_size,self.z_dim], -1, 1, dtype=tf.float32)
    
    tf.histogram_summary('z', self.z)
    
    self.G = self.generator(y=labels, y_dim=numclasses, is_training=is_training, restore=restore, scope='g')
    self.D = self.discriminator(images, labels, reuse=False, is_training=is_training, restore=restore, scope='d')

    self.D_ = self.discriminator(self.G, labels, reuse=True, is_training=is_training, restore=restore, scope='d')

    self.G_sum = tf.image_summary("G", self.G, name='g/image')

    self.d_loss_real = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(self.D, tf.ones_like(self.D)))
    self.d_loss_fake = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(self.D_, tf.zeros_like(self.D_)))
    self.g_loss_d = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(self.D_, tf.ones_like(self.D_)))
    self.g_loss_h = self.perception_loss(self.G, labels, numclasses, False, True, 'h')
    
    self.g_loss = self.g_loss_d + 10*self.g_loss_h
    
#    self.g_regularization_losses = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES, scope='g')
#    self.g_total_loss = tf.add_n([self.g_loss] + self.g_regularization_losses, name='g_total_loss')
      
    self.d_loss_real_sum = tf.scalar_summary("d_loss_real", self.d_loss_real, name='d/loss_real')
    self.d_loss_fake_sum = tf.scalar_summary("d_loss_fake", self.d_loss_fake, name='d/loss_fake')
                                                
    self.d_loss = 0.5*self.d_loss_real + 0.5*self.d_loss_fake
    
#    self.d_regularization_losses = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES, scope='d')
#    self.d_total_loss = tf.add_n([self.d_loss] + self.d_regularization_losses, name='d_total_loss')

    self.g_loss_sum = tf.scalar_summary("g_loss", self.g_loss, name='g/loss')
    self.d_loss_sum = tf.scalar_summary("d_loss", self.d_loss, name='d/loss')

    t_vars = tf.trainable_variables()
    

    self.d_vars = [var for var in t_vars if 'd/' in var.name]
    self.g_vars = [var for var in t_vars if 'g/' in var.name]   
Example #33
0
    def build_model(self):
        self.inputs = tf.placeholder(tf.float32, [self.batch_size, self.input_size, self.input_size, 3], name='real_images')
        # self.inputs = tf.placeholder(tf.float32, [None, self.input_size, self.input_size, 3], name='real_images')

        try:
            self.up_inputs = tf.image.resize_images(self.inputs, self.image_shape[0], self.image_shape[1], tf.image.ResizeMethod.NEAREST_NEIGHBOR)
        except ValueError:
            # newer versions of tensorflow
            self.up_inputs = tf.image.resize_images(self.inputs, [self.image_shape[0], self.image_shape[1]], tf.image.ResizeMethod.NEAREST_NEIGHBOR)

        self.images = tf.placeholder(tf.float32, [self.batch_size] + self.image_shape, name='real_images')
        # self.images = tf.placeholder(tf.float32, [None] + self.image_shape, name='real_images')
        self.sample_images= tf.placeholder(tf.float32, [self.sample_size] + self.image_shape, name='sample_images')
        # self.sample_images = tf.placeholder(tf.float32, [None] + self.image_shape, name='sample_images')

        self.G = self.generator(self.inputs)
        self.G_sum = tf.image_summary("G", self.G)
        self.g_loss = tf.reduce_mean(tf.square(self.images-self.G))
        self.g_loss_sum = tf.scalar_summary("g_loss", self.g_loss)
        t_vars = tf.trainable_variables()
        self.g_vars = [var for var in t_vars if 'g_' in var.name]
        self.saver = tf.train.Saver()
def conv_max_pool_2x2(x, conv_width, conv_height, in_depth, out_depth, name="conv"):

    with tf.name_scope(name) as scope:
        W_conv = weight_variable([conv_width, conv_height, in_depth, out_depth])
        b_conv = bias_variable([out_depth])
        h_conv = tf.nn.relu(conv2d(x, W_conv) + b_conv)
        h_pool = max_pool_2x2(h_conv)

    with tf.name_scope("summaries") as scope:

        # TIPS: to display the 32 convolution filters, re-arrange the
        # weigths to look like 32 images with a transposition.
        a = tf.reshape(W_conv, [conv_width * conv_height * in_depth, out_depth])
        b = tf.transpose(a)
        c = tf.reshape(b, [out_depth, conv_width, conv_height * in_depth, 1])
        conv_image = tf.image_summary(name + " filter", c, out_depth)

        # TIPS: by looking at the weights histogram, we can see the the
        # weigths are explosing or vanishing.
        W_conv_hist = tf.histogram_summary(name + " weights", W_conv)
        b_conv_hist = tf.histogram_summary(name + " biases", b_conv)
    
    return h_pool
Example #35
0
    def testImageSummary(self):
        np.random.seed(7)
        with self.test_session() as sess:
            for depth in 1, 3, 4:
                shape = (4, 5, 7) + (depth, )
                bad_color = [255, 0, 0, 255][:depth]
                for positive in False, True:
                    # Build a mostly random image with one nan
                    const = np.random.randn(*shape).astype(np.float32)
                    const[0, 1, 2] = 0  # Make the nan entry not the max
                    if positive:
                        const = 1 + np.maximum(const, 0)
                        scale = 255 / const.reshape(4, -1).max(axis=1)
                        offset = 0
                    else:
                        scale = 127 / np.abs(const.reshape(4, -1)).max(axis=1)
                        offset = 128
                    adjusted = np.floor(scale[:, None, None, None] * const +
                                        offset)
                    const[0, 1, 2, depth // 2] = np.nan

                    # Summarize
                    summ = tf.image_summary("img", const)
                    value = sess.run(summ)
                    self.assertEqual([], summ.get_shape())
                    image_summ = self._AsSummary(value)

                    # Decode the first image and check consistency
                    image = image_ops.decode_png(
                        image_summ.value[0].image.encoded_image_string).eval()
                    self.assertAllEqual(image[1, 2], bad_color)
                    image[1, 2] = adjusted[0, 1, 2]
                    self.assertAllClose(image, adjusted[0])

                    # Check the rest of the proto
                    self._CheckProto(image_summ, shape)
Example #36
0
  def run(self):
    """Run training."""
    # Create logging directory if not exists.
    if not os.path.isdir(self._train_log_dir):
      os.makedirs(self._train_log_dir)

    # Load data and compute loss function
    self._initialize()

    # Visualize input images in Tensorboard.
    self._summary_ops.append(tf.image_summary("Image_Train", self._observations, max_images=5))

    # Initialize optimizer.
    optimizer = tf.train.AdadeltaOptimizer(self._config.learning_rate)
    train_op = slim.learning.create_train_op(self._loss, optimizer)

    # Use `slim.learning.train` to manage training.
    slim.learning.train(train_op=train_op,
                        logdir=self._train_log_dir,
                        graph=self._graph,
                        number_of_steps=self._config.train_steps,
                        summary_op=tf.merge_summary(self._summary_ops),
                        save_summaries_secs=self._config.save_summaries_secs,
                        save_interval_secs=self._config.save_interval_secs)
Example #37
0
def main():
    # Load the data
    provider = data_provider.ICT3DFE()
    images, normals, mask = provider.get('normals/mask')

    # Define the network
    with tf.variable_scope('net'):
        with slim.arg_scope([slim.batch_norm, slim.layers.dropout],
                            is_training=False):
            predictions, _ = resnet_model.multiscale_nrm_net(images, scales=(1, 2, 4))

    tf.image_summary('images', images)
    tf.image_summary('normals', normals)
    tf.image_summary('predictions', predictions)
    
    # Choose the metrics to compute:
    names_to_values, names_to_updates = slim.metrics.aggregate_metric_map({
        'cosine': tf.contrib.metrics.streaming_mean_cosine_distance(
            predictions, normals, 3, weights=mask)
    })

    # Create the summary ops such that they also print out to std output:
    summary_ops = []
    for metric_name, metric_value in names_to_values.items():
        op = tf.scalar_summary(metric_name, metric_value)
        op = tf.Print(op, [metric_value], metric_name)
        summary_ops.append(op)

    num_examples = provider.num_samples()
    batch_size = 1
    num_batches = math.ceil(num_examples / batch_size)

    # Setup the global step.
    slim.get_or_create_global_step()

    slim.evaluation.evaluation_loop('',
                                    FLAGS.checkpoint_dir,
                                    FLAGS.log_dir,
                                    num_evals=num_batches,
                                    eval_op=list(names_to_updates.values()),
                                    summary_op=tf.merge_summary(summary_ops),
                                    eval_interval_secs=FLAGS.eval_interval_secs)
def _generate_image_and_label_batch(image, label, min_queue_examples, batch_size, shuffle):
  """Construct a queued batch of images and labels.

  Args:
    image: 4-D Tensor of [frame, height, width, NUM_CHANNELS] of type.float32.
    label: 1-D Tensor of type.int32
    min_queue_examples: int32, minimum number of samples to retain
      in the queue that provides of batches of examples.
    batch_size: Number of images per batch.
    shuffle: boolean indicating whether to use a shuffling queue.

  Returns:
    images: Images. 5D tensor of [batch_size, frame, height, width, NUM_CHANNELS] size.
    labels: Labels. 1D tensor of [batch_size] size.
  """
  # Create a queue that shuffles the examples, and then
  # read 'batch_size' images + labels from the example queue.

  # Set it to 1 because there is racing... WTF?!
  num_preprocess_threads = 1
  if shuffle:
    images, label_batch = tf.train.shuffle_batch(
        [image, label],
        batch_size=batch_size,
        num_threads=num_preprocess_threads,
        capacity=min_queue_examples + 3 * batch_size,
        min_after_dequeue=min_queue_examples)
  else:
    images, label_batch = tf.train.batch(
        [image, label],
        batch_size=batch_size,
        num_threads=num_preprocess_threads,
        capacity=min_queue_examples + 3 * batch_size,
        allow_smaller_final_batch=True)

  # Display the training images in the visualizer.
  tf.image_summary('images0', tf.unpack(images, axis=1)[0])
  tf.image_summary('images1', tf.unpack(images, axis=1)[1])
  tf.image_summary('images2', tf.unpack(images, axis=1)[2])

  return images, tf.reshape(label_batch, [batch_size])
def main(argv=None):
    utils.maybe_download_and_extract(FLAGS.data_dir, DATA_URL, is_tarfile=True)
    print "Setting up model..."
    global_step = tf.Variable(0,trainable=False)
    gray, color = inputs()
    pred = 255 * inference(gray) + 128
    tf.image_summary("Gray", gray, max_images=1)
    tf.image_summary("Ground_truth", color, max_images=1)
    tf.image_summary("Prediction", pred, max_images=1)

    image_loss = loss(pred, color)
    train_op = train(image_loss, global_step)

    summary_op = tf.merge_all_summaries()
    with tf.Session() as sess:
        print "Setting up summary writer, queue, saver..."
        sess.run(tf.initialize_all_variables())
        
        summary_writer = tf.train.SummaryWriter(FLAGS.logs_dir, sess.graph)
        saver = tf.train.Saver()

        ckpt = tf.train.get_checkpoint_state(FLAGS.logs_dir)
        if ckpt and ckpt.model_checkpoint_path:
            print "Restoring model from checkpoint..."
            saver.restore(sess, ckpt.model_checkpoint_path)
        tf.train.start_queue_runners(sess)
        for step in xrange(MAX_ITERATIONS):
            if step % 400 == 0:
                loss_val, summary_str = sess.run([image_loss, summary_op])
                print "Step %d, Loss: %g" % (step, loss_val)
                summary_writer.add_summary(summary_str, global_step=step)

            if step % 1000 == 0:
                saver.save(sess, FLAGS.logs_dir + "model.ckpt", global_step=step)
                print "%s" % datetime.now()

            sess.run(train_op)
Example #40
0
    def __init__(self, env, task, visualise):
        """
An implementation of the A3C algorithm that is reasonably well-tuned for the VNC environments.
Below, we will have a modest amount of complexity due to the way TensorFlow handles data parallelism.
But overall, we'll define the model, specify its inputs, and describe how the policy gradients step
should be computed.
"""

        self.env = env
        self.task = task
        ob_space = self.env.observation_space
        ac_space = self.env.action_space
        worker_device = "/job:worker/task:{}/cpu:0".format(task)
        with tf.device(
                tf.train.replica_device_setter(1,
                                               worker_device=worker_device)):
            with tf.variable_scope("global"):
                self.network = CnnPolicy(ob_space, ac_space)
                self.global_step = tf.get_variable(
                    "global_step", [],
                    tf.int32,
                    initializer=tf.constant_initializer(0, dtype=tf.int32),
                    trainable=False)

        with tf.device(worker_device):
            with tf.variable_scope("local"):
                self.local_network = pi = CnnPolicy(ob_space, ac_space)
                pi.global_step = self.global_step

            self.ac = tf.placeholder(tf.float32, [None, env.action_space.n],
                                     name="ac")
            self.adv = tf.placeholder(tf.float32, [None], name="adv")
            self.r = tf.placeholder(tf.float32, [None], name="r")

            log_prob_tf = tf.nn.log_softmax(pi.logits)
            prob_tf = tf.nn.softmax(pi.logits)

            # the "policy gradients" loss:  its derivative is precisely the policy gradient
            # notice that self.ac is a placeholder that is provided externally.
            # adv will contain the advantages, as calculated in process_rollout
            pi_loss = -tf.reduce_sum(
                tf.reduce_sum(log_prob_tf * self.ac, [1]) * self.adv)

            # loss of value function
            vf_loss = 0.5 * tf.reduce_sum(tf.square(pi.vf - self.r))
            entropy = -tf.reduce_sum(prob_tf * log_prob_tf)

            bs = tf.to_float(tf.shape(pi.x)[0])
            self.loss = pi_loss + 0.5 * vf_loss - entropy * 0.01

            # 20 represents the number of "local steps":  the number of timesteps
            # we run the policy before we update the parameters.
            # The larger local steps is, the lower is the variance in our policy gradients estimate
            # on the one hand;  but on the other hand, we get less frequent parameter updates, which
            # slows down learning.  In this code, we found that making local steps be much
            # smaller than 20 makes the algorithm more difficult to tune and to get to work.
            self.runner = RunnerThread(env, pi, 20, visualise)

            grads = tf.gradients(self.loss, pi.var_list)

            if use_tf12_api:
                tf.summary.scalar("model/policy_loss", pi_loss / bs)
                tf.summary.scalar("model/value_loss", vf_loss / bs)
                tf.summary.scalar("model/entropy", entropy / bs)
                tf.summary.image("model/state", pi.x)
                tf.summary.scalar("model/grad_global_norm",
                                  tf.global_norm(grads))
                tf.summary.scalar("model/var_global_norm",
                                  tf.global_norm(pi.var_list))
                self.summary_op = tf.summary.merge_all()

            else:
                tf.scalar_summary("model/policy_loss", pi_loss / bs)
                tf.scalar_summary("model/value_loss", vf_loss / bs)
                tf.scalar_summary("model/entropy", entropy / bs)
                tf.image_summary("model/state", pi.x)
                tf.scalar_summary("model/grad_global_norm",
                                  tf.global_norm(grads))
                tf.scalar_summary("model/var_global_norm",
                                  tf.global_norm(pi.var_list))
                self.summary_op = tf.merge_all_summaries()

            grads, _ = tf.clip_by_global_norm(grads, 40.0)

            # copy weights from the parameter server to the local model
            self.sync = tf.group(*[
                v1.assign(v2)
                for v1, v2 in zip(pi.var_list, self.network.var_list)
            ])

            grads_and_vars = list(zip(grads, self.network.var_list))
            inc_step = self.global_step.assign_add(tf.shape(pi.x)[0])

            # each worker has a different set of adam optimizer parameters
            opt = tf.train.AdamOptimizer(7e-4)
            self.train_op = tf.group(opt.apply_gradients(grads_and_vars),
                                     inc_step)
            self.summary_writer = None
            self.local_steps = 0
Example #41
0
                W_conv1_img1 = tf.concat(0, W_conv1_imgs[0:6])
                W_conv1_img2 = tf.concat(0, W_conv1_imgs[6:12])
                W_conv1_img3 = tf.concat(0, W_conv1_imgs[12:18])
                W_conv1_img4 = tf.concat(0, W_conv1_imgs[18:24])
                W_conv1_img5 = tf.concat(0, W_conv1_imgs[24:30])
                W_conv1_img6 = tf.concat(0, W_conv1_imgs[30:36])

                W_conv1_img = tf.concat(1, [
                    W_conv1_img1, W_conv1_img2, W_conv1_img3, W_conv1_img4,
                    W_conv1_img5, W_conv1_img6
                ])
                W_conv1_img = tf.reshape(W_conv1_img, [1, 42, 42, 1])

                W_conv1_sum = tf.image_summary("W_conv1_Visualize" + str(i),
                                               W_conv1_img,
                                               max_images=10)
                W_conv1_sum_str = sess.run(W_conv1_sum,
                                           feed_dict={
                                               x: batch[0],
                                               y_: batch[1],
                                               keep_prob: 1.0
                                           })
                train_writer.add_summary(W_conv1_sum_str, i)

            train_step.run(feed_dict={
                x: batch[0],
                y_: batch[1],
                keep_prob: 0.5
            })
Example #42
0
bc1_hist = tf.histogram_summary("biases",biases['bc1'])


#reconstructed image
output = conv_net(x, weights, biases)
recon_image_vec = output['out']
recon_image = tf.reshape(recon_image_vec,shape=[-1,28,28,1])


#features
feature_vec = output['features']
feature_hist = tf.histogram_summary("features",feature_vec)


#summary of the images
recon_image_summary = tf.image_summary("image_recon",recon_image)
#original_image = tf.image_summary("image_actual",x_image)


#ls cost
cost = tf.reduce_mean(tf.square(recon_image_vec - x))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)


#summary of cost
cost_summary = tf.scalar_summary("cost",cost)


#merge all summaries
merged = tf.merge_all_summaries()
def train(dataset_frame1, dataset_frame2, dataset_frame3):
    """Trains a model."""
    with tf.Graph().as_default():
        # Create input and target placeholder.
        input_placeholder = tf.placeholder(tf.float32,
                                           shape=(None, 256, 256, 6))
        target_placeholder = tf.placeholder(tf.float32,
                                            shape=(None, 256, 256, 3))

        # input_resized = tf.image.resize_area(input_placeholder, [128, 128])
        # target_resized = tf.image.resize_area(target_placeholder,[128, 128])

        # Prepare model.
        model = Voxel_flow_model()
        prediction = model.inference(input_placeholder)
        # reproduction_loss, prior_loss = model.loss(prediction, target_placeholder)
        reproduction_loss = model.loss(prediction, target_placeholder)
        # total_loss = reproduction_loss + prior_loss
        total_loss = reproduction_loss

        # Perform learning rate scheduling.
        learning_rate = FLAGS.initial_learning_rate

        # Create an optimizer that performs gradient descent.
        opt = tf.train.AdamOptimizer(learning_rate)
        grads = opt.compute_gradients(total_loss)
        update_op = opt.apply_gradients(grads)

        # Create summaries
        summaries = tf.get_collection(tf.GraphKeys.SUMMARIES)
        summaries.append(tf.scalar_summary('total_loss', total_loss))
        summaries.append(
            tf.scalar_summary('reproduction_loss', reproduction_loss))
        # summaries.append(tf.scalar_summary('prior_loss', prior_loss))
        summaries.append(tf.image_summary('Input Image', input_placeholder, 3))
        summaries.append(tf.image_summary('Output Image', prediction, 3))
        summaries.append(
            tf.image_summary('Target Image', target_placeholder, 3))

        # Create a saver.
        saver = tf.train.Saver(tf.all_variables())

        # Build the summary operation from the last tower summaries.
        summary_op = tf.merge_all_summaries()

        # Build an initialization operation to run below.
        init = tf.initialize_all_variables()
        sess = tf.Session()
        sess.run(init)

        # Summary Writter
        summary_writer = tf.train.SummaryWriter(FLAGS.train_dir,
                                                graph=sess.graph)

        # Training loop using feed dict method.
        data_list_frame1 = dataset_frame1.read_data_list_file()
        random.seed(1)
        shuffle(data_list_frame1)

        data_list_frame2 = dataset_frame2.read_data_list_file()
        random.seed(1)
        shuffle(data_list_frame2)

        data_list_frame3 = dataset_frame3.read_data_list_file()
        random.seed(1)
        shuffle(data_list_frame3)

        data_size = len(data_list_frame1)
        epoch_num = int(data_size / FLAGS.batch_size)

        # num_workers = 1

        # load_fn_frame1 = partial(dataset_frame1.process_func)
        # p_queue_frame1 = PrefetchQueue(load_fn_frame1, data_list_frame1, FLAGS.batch_size, shuffle=False, num_workers=num_workers)

        # load_fn_frame2 = partial(dataset_frame2.process_func)
        # p_queue_frame2 = PrefetchQueue(load_fn_frame2, data_list_frame2, FLAGS.batch_size, shuffle=False, num_workers=num_workers)

        # load_fn_frame3 = partial(dataset_frame3.process_func)
        # p_queue_frame3 = PrefetchQueue(load_fn_frame3, data_list_frame3, FLAGS.batch_size, shuffle=False, num_workers=num_workers)

        for step in xrange(0, FLAGS.max_steps):
            batch_idx = step % epoch_num

            batch_data_list_frame1 = data_list_frame1[
                int(batch_idx * FLAGS.batch_size):int((batch_idx + 1) *
                                                      FLAGS.batch_size)]
            batch_data_list_frame2 = data_list_frame2[
                int(batch_idx * FLAGS.batch_size):int((batch_idx + 1) *
                                                      FLAGS.batch_size)]
            batch_data_list_frame3 = data_list_frame3[
                int(batch_idx * FLAGS.batch_size):int((batch_idx + 1) *
                                                      FLAGS.batch_size)]

            # Load batch data.
            batch_data_frame1 = np.array([
                dataset_frame1.process_func(line)
                for line in batch_data_list_frame1
            ])
            batch_data_frame2 = np.array([
                dataset_frame2.process_func(line)
                for line in batch_data_list_frame2
            ])
            batch_data_frame3 = np.array([
                dataset_frame3.process_func(line)
                for line in batch_data_list_frame3
            ])

            # batch_data_frame1 = p_queue_frame1.get_batch()
            # batch_data_frame2 = p_queue_frame2.get_batch()
            # batch_data_frame3 = p_queue_frame3.get_batch()

            feed_dict = {
                input_placeholder:
                np.concatenate((batch_data_frame1, batch_data_frame3), 3),
                target_placeholder:
                batch_data_frame2
            }

            # Run single step update.
            _, loss_value = sess.run([update_op, total_loss],
                                     feed_dict=feed_dict)

            if batch_idx == 0:
                # Shuffle data at each epoch.
                random.seed(1)
                shuffle(data_list_frame1)
                random.seed(1)
                shuffle(data_list_frame2)
                random.seed(1)
                shuffle(data_list_frame3)
                print('Epoch Number: %d' % int(step / epoch_num))

            # Output Summary
            if step % 10 == 0:
                # summary_str = sess.run(summary_op, feed_dict = feed_dict)
                # summary_writer.add_summary(summary_str, step)
                print("Loss at step %d: %f" % (step, loss_value))

            if step % 500 == 0:
                # Run a batch of images
                prediction_np, target_np = sess.run(
                    [prediction, target_placeholder], feed_dict=feed_dict)
                for i in range(0, prediction_np.shape[0]):
                    file_name = FLAGS.train_image_dir + str(i) + '_out.png'
                    file_name_label = FLAGS.train_image_dir + str(
                        i) + '_gt.png'
                    imwrite(file_name, prediction_np[i, :, :, :])
                    imwrite(file_name_label, target_np[i, :, :, :])

            # Save checkpoint
            if step % 5000 == 0 or (step + 1) == FLAGS.max_steps:
                checkpoint_path = os.path.join(FLAGS.train_dir, 'model.ckpt')
                saver.save(sess, checkpoint_path, global_step=step)
Example #44
0
 def make_image_summaries(self, imgs1, dec_im1, dec_ed1, imgs2, dec_im2,
                          dec_ed2):
     tf.image_summary('imgs/imgs1',
                      montage_tf(imgs1, 1, self.im_per_smry),
                      max_images=1)
     tf.image_summary('imgs/dec_im1',
                      montage_tf(dec_im1, 1, self.im_per_smry),
                      max_images=1)
     tf.image_summary('imgs/dec_ed1',
                      montage_tf(dec_ed1, 1, self.im_per_smry),
                      max_images=1)
     tf.image_summary('imgs/imgs2',
                      montage_tf(imgs2, 1, self.im_per_smry),
                      max_images=1)
     tf.image_summary('imgs/dec_im2',
                      montage_tf(dec_im2, 1, self.im_per_smry),
                      max_images=1)
     tf.image_summary('imgs/dec_ed2',
                      montage_tf(dec_ed2, 1, self.im_per_smry),
                      max_images=1)
Example #45
0
    tanh = tf.nn.tanh(conv)
    s6_conv3 = tf.nn.dropout(tanh, keep_prob)

##############################
# Section 7

# Convolution down to one channel
layer_name = "s7_conv1"
with tf.name_scope(layer_name):
    W = utils.weight_variable([k_size, k_size, ch[5], 1])
    b = utils.bias_variable([1])
    conv = utils.conv2d(s6_conv3, W, b, 1)

    s7_conv1 = conv

##############################

# Form logits and a prediction
y = tf.squeeze(s7_conv1, squeeze_dims=[3])
prediction = tf.cast(tf.round(tf.sigmoid(y)), tf.bool)

# Image summarries
tf.image_summary("input", x, max_images=2)
tf.image_summary("label",
                 tf.expand_dims(tf.cast(y_, tf.float32), dim=3),
                 max_images=2)
tf.image_summary("prediction",
                 tf.expand_dims(tf.cast(prediction, tf.float32), dim=3),
                 max_images=2)
def train():
    # Import data
    mnist = input_data.read_data_sets(
        "mnist_train/",  #FLAGS.data_dir,
        one_hot=True,
        fake_data=FLAGS.fake_data)

    sess = tf.InteractiveSession()

    # Create a multilayer model.

    # Input placeholders
    with tf.name_scope('input'):
        x = tf.placeholder(tf.float32, [None, 784], name='x-input')
        y_ = tf.placeholder(tf.float32, [None, 10], name='y-input')

    with tf.name_scope('input_reshape'):
        image_shaped_input = tf.reshape(x, [-1, 28, 28, 1])
        tf.image_summary('input', image_shaped_input, 10)

    # We can't initialize these variables to 0 - the network will get stuck.
    def weight_variable(shape):
        """Create a weight variable with appropriate initialization."""
        initial = tf.truncated_normal(shape, stddev=0.1)
        return tf.Variable(initial)

    def bias_variable(shape):
        """Create a bias variable with appropriate initialization."""
        initial = tf.constant(0.1, shape=shape)
        return tf.Variable(initial)

    def variable_summaries(var, name):
        """Attach a lot of summaries to a Tensor."""
        with tf.name_scope('summaries'):
            mean = tf.reduce_mean(var)
            tf.scalar_summary('mean/' + name, mean)
            with tf.name_scope('stddev'):
                stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean)))
            tf.scalar_summary('sttdev/' + name, stddev)
            tf.scalar_summary('max/' + name, tf.reduce_max(var))
            tf.scalar_summary('min/' + name, tf.reduce_min(var))
            tf.histogram_summary(name, var)

    def nn_layer(input_tensor,
                 input_dim,
                 output_dim,
                 layer_name,
                 act=tf.nn.relu):
        """Reusable code for making a simple neural net layer.
    It does a matrix multiply, bias add, and then uses relu to nonlinearize.
    It also sets up name scoping so that the resultant graph is easy to read,
    and adds a number of summary ops.
    """
        # Adding a name scope ensures logical grouping of the layers in the graph.
        with tf.name_scope(layer_name):
            # This Variable will hold the state of the weights for the layer
            with tf.name_scope('weights'):
                weights = weight_variable([input_dim, output_dim])
                variable_summaries(weights, layer_name + '/weights')
            with tf.name_scope('biases'):
                biases = bias_variable([output_dim])
                variable_summaries(biases, layer_name + '/biases')
            with tf.name_scope('Wx_plus_b'):
                preactivate = tf.matmul(input_tensor, weights) + biases
                tf.histogram_summary(layer_name + '/pre_activations',
                                     preactivate)
            activations = act(preactivate, 'activation')
            tf.histogram_summary(layer_name + '/activations', activations)
            return activations

    hidden1 = nn_layer(x, 784, 500, 'layer1')

    with tf.name_scope('dropout'):
        keep_prob = tf.placeholder(tf.float32)
        tf.scalar_summary('dropout_keep_probability', keep_prob)
        dropped = tf.nn.dropout(hidden1, keep_prob)

    y = nn_layer(dropped, 500, 10, 'layer2', act=tf.nn.softmax)

    with tf.name_scope('cross_entropy'):
        diff = y_ * tf.log(y)
        with tf.name_scope('total'):
            cross_entropy = -tf.reduce_mean(diff)
        tf.scalar_summary('cross entropy', cross_entropy)

    with tf.name_scope('train'):
        train_step = tf.train.AdamOptimizer(
            FLAGS.learning_rate).minimize(cross_entropy)

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

    # Merge all the summaries and write them out to /tmp/mnist_logs (by default)
    merged = tf.merge_all_summaries()
    train_writer = tf.train.SummaryWriter(FLAGS.summaries_dir + '/train',
                                          sess.graph)
    test_writer = tf.train.SummaryWriter(FLAGS.summaries_dir + '/test')
    tf.initialize_all_variables().run()

    # Train the model, and also write summaries.
    # Every 10th step, measure test-set accuracy, and write test summaries
    # All other steps, run train_step on training data, & add training summaries

    def feed_dict(train):
        """Make a TensorFlow feed_dict: maps data onto Tensor placeholders."""
        if train or FLAGS.fake_data:
            xs, ys = mnist.train.next_batch(100, fake_data=FLAGS.fake_data)
            k = FLAGS.dropout
        else:
            xs, ys = mnist.test.images, mnist.test.labels
            k = 1.0
        return {x: xs, y_: ys, keep_prob: k}

    for i in range(FLAGS.max_steps):
        if i % 10 == 0:  # Record summaries and test-set accuracy
            summary, acc = sess.run([merged, accuracy],
                                    feed_dict=feed_dict(False))
            test_writer.add_summary(summary, i)
            print('Accuracy at step %s: %s' % (i, acc))
        else:  # Record train set summaries, and train
            if i % 100 == 99:  # Record execution stats
                run_options = tf.RunOptions(
                    trace_level=tf.RunOptions.FULL_TRACE)
                run_metadata = tf.RunMetadata()
                summary, _ = sess.run([merged, train_step],
                                      feed_dict=feed_dict(True),
                                      options=run_options,
                                      run_metadata=run_metadata)
                train_writer.add_run_metadata(run_metadata, 'step%03d' % i)
                train_writer.add_summary(summary, i)
                print('Adding run metadata for', i)
            else:  # Record a summary
                summary, _ = sess.run([merged, train_step],
                                      feed_dict=feed_dict(True))
                train_writer.add_summary(summary, i)
    train_writer.close()
    test_writer.close()
Example #47
0
                    'the content of an image and the style of another.',
        formatter_class=argparse.ArgumentDefaultsHelpFormatter
    )
    parser.add_argument('--content', default=data_dir + '/overfit/COCO_val2014_000000000074.jpg', type=str, help='Content image.')
    parser.add_argument('--style', default=data_dir + '/paintings/edvard_munch-the_scream.jpg', type=str, help='Style image.')
    parser.add_argument('--image_size', default=256, type=int, help='Input image size.')
    parser.add_argument('--ratio', default=4, type=int, help='Ratio between encoding and decoding')
    parser.add_argument('--nb_res_layer', default=5, type=int, help='Number of residual layer.')
    args = parser.parse_args()

    dir = os.path.dirname(os.path.realpath(__file__))
    results_dir = dir + '/data/st'

    input_shape = [None, args.image_size, args.image_size, 3]
    fst = fast_style_transfer(input_shape, ratio=args.ratio, nb_res_layer=args.nb_res_layer)
    tf.image_summary('input_img', fst['input'], 2)
    tf.image_summary('output_img', fst['output'], 2)

    vgg = vgg19.Vgg19()


    style_loss = 1
    content_loss = 1
    tv_loss = 1
    tf.scalar_summary('style_loss', style_loss)
    tf.scalar_summary('content_loss', content_loss)
    tf.scalar_summary('tv_loss', tv_loss)
    
    adam = tf.AdamOptimizer(1e-3)
    train_op = adam.minimize(total_loss)
def train():
    # Import data and create session
    mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
    sess = tf.InteractiveSession()

    # Create a multilayer model.

    # Input placeholders
    with tf.name_scope('input'):
        x = tf.placeholder(tf.float32, [None, 784], name='x-input')
        image_shaped_input = tf.reshape(x, [-1, 28, 28, 1])
        tf.image_summary('input', image_shaped_input, 10)
        y_ = tf.placeholder(tf.float32, [None, 10], name='y-input')
        keep_prob = tf.placeholder(tf.float32)
        tf.scalar_summary('dropout_keep_probability', keep_prob)

    # We can't initialize these variables to 0 - the network will get stuck.
    def weight_variable(shape):
        """Create a weight variable with appropriate initialization."""
        initial = tf.truncated_normal(shape, stddev=0.1)
        return tf.Variable(initial)

    def bias_variable(shape):
        """Create a bias variable with appropriate initialization."""
        initial = tf.constant(0.1, shape=shape)
        return tf.Variable(initial)

    #Strides of 1 and use 2*2 blocks
    def conv2d(x, W):
        return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')

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

    def variable_summaries(var, name):
        """Attach a lot of summaries to a Tensor."""
        with tf.name_scope('summaries'):
            mean = tf.reduce_mean(var)
            tf.scalar_summary('mean/' + name, mean)
            with tf.name_scope('stddev'):
                stddev = tf.sqrt(tf.reduce_sum(tf.square(var - mean)))
            tf.scalar_summary('sttdev/' + name, stddev)
            tf.scalar_summary('max/' + name, tf.reduce_max(var))
            tf.scalar_summary('min/' + name, tf.reduce_min(var))
            tf.histogram_summary(name, var)

    def nn_layer(input_tensor,
                 input_dim,
                 output_dim,
                 layer_name,
                 act=tf.nn.relu):
        """Reusable code for making a simple neural net layer.
        It does a matrix multiply, bias add, and then uses relu to nonlinearize.
        It also sets up name scoping so that the resultant graph is easy to read, and
        adds a number of summary ops.
        """
        # Adding a name scope ensures logical grouping of the layers in the graph.
        with tf.name_scope(layer_name):
            # This Variable will hold the state of the weights for the layer
            with tf.name_scope('weights'):
                weights = weight_variable([input_dim, output_dim])
                variable_summaries(weights, layer_name + '/weights')
            with tf.name_scope('biases'):
                biases = bias_variable([output_dim])
                variable_summaries(biases, layer_name + '/biases')
            with tf.name_scope('Wx_plus_b'):
                preactivate = tf.matmul(input_tensor, weights) + biases
                tf.histogram_summary(layer_name + '/pre_activations',
                                     preactivate)
            activations = act(preactivate, 'activation')
            tf.histogram_summary(layer_name + '/activations', activations)
            return activations

    def conv_layer(input_tensor, dim, output_dim, layer_name, act=tf.nn.relu):
        with tf.name_scope(layer_name):
            # This Variable will hold the state of the weights for the layer
            with tf.name_scope('weights'):
                weights = weight_variable(dim)
                variable_summaries(weights, layer_name + '/weights')
            with tf.name_scope('biases'):
                biases = bias_variable([output_dim])
                variable_summaries(biases, layer_name + '/biases')
            with tf.name_scope('Wx_plus_b'):
                h_conv1 = tf.nn.relu(conv2d(input_tensor, weights) + biases)
                preactivate = max_pool_2x2(h_conv1)
                tf.histogram_summary(layer_name + '/pre_activations',
                                     preactivate)
            activations = act(preactivate, 'activation')
            tf.histogram_summary(layer_name + '/activations', activations)
            return activations

    #input
    x_image = tf.reshape(x, [-1, 28, 28, 1])

    #layer 1
    layer1 = conv_layer(x_image, [10, 10, 1, 8], 8, 'layer1')

    #second layer
    layer2 = conv_layer(layer1, [10, 10, 8, 16], 16, 'layer2')

    #third layer
    layer3 = conv_layer(layer2, [7, 7, 16, 32], 32, 'layer3')

    #Densely connected layer with all 1024 neurons
    reshaping = tf.reshape(layer3, [-1, 4 * 4 * 32])
    denselayer = nn_layer(reshaping, 4 * 4 * 32, 1024, 'dense')
    #W_fc1 = weight_variable([12 * 12 * 32, 1024])
    #b_fc1 = bias_variable([1024])

    #h_pool2_flat = tf.reshape(layer2, [-1, 7*7*64])
    #h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)

    #Implement Dropout
    h_fc1_drop = tf.nn.dropout(denselayer, keep_prob)

    #Softmax Readout layer
    y = nn_layer(h_fc1_drop, 1024, 10, 'output', act=tf.nn.softmax)

    with tf.name_scope('cross_entropy'):
        diff = y_ * tf.log(y)
        with tf.name_scope('total'):
            cross_entropy = -tf.reduce_mean(diff)
        tf.scalar_summary('cross entropy', cross_entropy)

    with tf.name_scope('train'):
        train_step = tf.train.AdamOptimizer(
            FLAGS.learning_rate).minimize(cross_entropy)

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

    # Merge all the summaries and write them out to /tmp/mnist_logs (by default)
    merged = tf.merge_all_summaries()
    train_writer = tf.train.SummaryWriter(FLAGS.summaries_dir + '/train',
                                          sess.graph)
    test_writer = tf.train.SummaryWriter(FLAGS.summaries_dir + '/test')
    tf.initialize_all_variables().run()

    # Train the model, and also write summaries.
    # Every 10th step, measure test-set accuracy, and write test summaries
    # All other steps, run train_step on training data, & add training summaries
    def feed_dict(train):
        """Make a TensorFlow feed_dict: maps data onto Tensor placeholders."""
        if train:
            xs, ys = mnist.train.next_batch(100)
            k = FLAGS.dropout
        else:
            xs, ys = mnist.test.images, mnist.test.labels
            k = 1.0
        return {x: xs, y_: ys, keep_prob: k}

    #num steps=2000
    for i in range(2001):
        # Record summaries and test-set accuracy every 100 steps
        if i % 100 == 0:
            summary, acc = sess.run([merged, accuracy],
                                    feed_dict=feed_dict(False))
            test_writer.add_summary(summary, i)
            print('Accuracy at step %s: %s' % (i, acc))
        else:  # Record train set summarieis, and train
            summary, _ = sess.run([merged, train_step],
                                  feed_dict=feed_dict(True))
            train_writer.add_summary(summary, i)
Example #49
0
    def __init__(self, env, task, visualise):
        """
An implementation of the A3C algorithm that is reasonably well-tuned for the VNC environments.
Below, we will have a modest amount of complexity due to the way TensorFlow handles data parallelism.
But overall, we'll define the model, specify its inputs, and describe how the policy gradients step
should be computed.
parameters:
    1. env: environment
    2. task: taks id
    3. network: LSTMPolicy
    4. global_step: variable that tracks global steps
    5. local_network: copy of LSTMPolicy
    6. ac: acter critic
    7. adv: advantage place holder, single value
    8. r: reward
    9. loss: the loss value
    10. runner
"""

        self.env = env
        self.task = task    #task id specifying which worker is working
        worker_device = "/job:worker/task:{}/cpu:0".format(task)  #create single worker
        #while  
        with tf.device(tf.train.replica_device_setter(1, worker_device=worker_device)):
            with tf.variable_scope("global"):
                # the input shpae is 128 x 200 x 1, size of the action is 
                self.network = LSTMPolicy(env.observation_space.shape, env.action_space.n)
                self.global_step = tf.get_variable("global_step", [], tf.int32, initializer=tf.constant_initializer(0, dtype=tf.int32),
                                                   trainable=False)

        with tf.device(worker_device):
            with tf.variable_scope("local"):
                #space dimension 128 x 200, action space size 7, if using neon race
                self.local_network = pi = LSTMPolicy(env.observation_space.shape, env.action_space.n)
                #this syncronize the worker with global step
                pi.global_step = self.global_step
            
            #placeholder for variables
            self.ac = tf.placeholder(tf.float32, [None, env.action_space.n], name="ac")
            self.adv = tf.placeholder(tf.float32, [None], name="adv")
            self.r = tf.placeholder(tf.float32, [None], name="r")
            
            # probability of actions
            log_prob_tf = tf.nn.log_softmax(pi.logits)
            prob_tf = tf.nn.softmax(pi.logits)

            # the "policy gradients" loss:  its derivative is precisely the policy gradient
            # notice that self.ac is a placeholder that is provided externally.
            # adv will contain the advantages, as calculated in process_rollout
            pi_loss = - tf.reduce_sum(tf.reduce_sum(log_prob_tf * self.ac, [1]) * self.adv)

            # loss of value function
            vf_loss = 0.5 * tf.reduce_sum(tf.square(pi.vf - self.r))   # sum of squared error which is defined as state_value - actual_reward
            entropy = - tf.reduce_sum(prob_tf * log_prob_tf)

            bs = tf.to_float(tf.shape(pi.x)[0])
            # this is the total loss function
            self.loss = pi_loss + 0.5 * vf_loss - entropy * 0.01

            # 20 represents the number of "local steps":  the number of timesteps
            # we run the policy before we update the parameters.
            # The larger local steps is, the lower is the variance in our policy gradients estimate
            # on the one hand;  but on the other hand, we get less frequent parameter updates, which
            # slows down learning.  In this code, we found that making local steps be much
            # smaller than 20 makes the algorithm more difficult to tune and to get to work.
            #t_max = 20, looking a head of 20 steps
            self.runner = RunnerThread(env, pi, 20, visualise)


            grads = tf.gradients(self.loss, pi.var_list)

            #save summary
            if use_tf12_api:
                tf.summary.scalar("model/policy_loss", pi_loss / bs)
                tf.summary.scalar("model/value_loss", vf_loss / bs)
                tf.summary.scalar("model/entropy", entropy / bs)
                tf.summary.image("model/state", pi.x)
                tf.summary.scalar("model/grad_global_norm", tf.global_norm(grads))
                tf.summary.scalar("model/var_global_norm", tf.global_norm(pi.var_list))
                self.summary_op = tf.summary.merge_all()

            else:
                tf.scalar_summary("model/policy_loss", pi_loss / bs)
                tf.scalar_summary("model/value_loss", vf_loss / bs)
                tf.scalar_summary("model/entropy", entropy / bs)
                tf.image_summary("model/state", pi.x)
                tf.scalar_summary("model/grad_global_norm", tf.global_norm(grads))
                tf.scalar_summary("model/var_global_norm", tf.global_norm(pi.var_list))
                self.summary_op = tf.merge_all_summaries()

            # perform gradient clipping
            # https://hackernoon.com/gradient-clipping-57f04f0adae
            grads, _ = tf.clip_by_global_norm(grads, 40.0)

            # copy weights from the parameter server to the local model
            self.sync = tf.group(*[v1.assign(v2) for v1, v2 in zip(pi.var_list, self.network.var_list)])

            # this will generate a tuple, where each of elements is in the form of (gradient, variable_name)
            grads_and_vars = list(zip(grads, self.network.var_list))
            inc_step = self.global_step.assign_add(tf.shape(pi.x)[0])

            # each worker has a different set of adam optimizer parameters
            opt = tf.train.AdamOptimizer(1e-4)
            self.train_op = tf.group(opt.apply_gradients(grads_and_vars), inc_step)
            self.summary_writer = None
            self.local_steps = 0
Example #50
0
def inference(images):
    """Build the CIFAR-10 model.

  Args:
    images: Images returned from distorted_inputs() or inputs().

  Returns:
    Logits.
  """
    # We instantiate all variables using tf.get_variable() instead of
    # tf.Variable() in order to share variables across multiple GPU training runs.
    # If we only ran this model on a single GPU, we could simplify this function
    # by replacing all instances of tf.get_variable() with tf.Variable().
    #
    # conv1
    with tf.variable_scope('conv1') as scope:
        kernel = _variable_with_weight_decay('weights',
                                             shape=[5, 5, 3, 64],
                                             stddev=5e-2,
                                             wd=0.0)
        conv = tf.nn.conv2d(images, kernel, [1, 1, 1, 1], padding='SAME')
        biases = _variable_on_cpu('biases', [64], tf.constant_initializer(0.0))
        bias = tf.nn.bias_add(conv, biases)
        conv1 = tf.nn.relu(bias, name=scope.name)
        _activation_summary(conv1)

    with tf.variable_scope('visualization'):
        # scale weights to [0 1], type is still float
        x_min = tf.reduce_min(kernel)
        x_max = tf.reduce_max(kernel)
        kernel_0_to_1 = (kernel - x_min) / (x_max - x_min)

        # to tf.image_summary format [batch_size, height, width, channels]
        kernel_transposed = tf.transpose(kernel_0_to_1, [3, 0, 1, 2])

        # this will display random 3 filters from the 64 in conv1
        tf.image_summary('conv1/filters', kernel_transposed, max_images=3)

    # pool1
    pool1 = tf.nn.max_pool(conv1,
                           ksize=[1, 3, 3, 1],
                           strides=[1, 2, 2, 1],
                           padding='SAME',
                           name='pool1')
    # norm1
    norm1 = tf.nn.lrn(pool1,
                      4,
                      bias=1.0,
                      alpha=0.001 / 9.0,
                      beta=0.75,
                      name='norm1')

    # conv2
    with tf.variable_scope('conv2') as scope:
        kernel = _variable_with_weight_decay('weights',
                                             shape=[5, 5, 64, 64],
                                             stddev=5e-2,
                                             wd=0.0)
        conv = tf.nn.conv2d(norm1, kernel, [1, 1, 1, 1], padding='SAME')
        biases = _variable_on_cpu('biases', [64], tf.constant_initializer(0.1))
        bias = tf.nn.bias_add(conv, biases)
        conv2 = tf.nn.relu(bias, name=scope.name)
        _activation_summary(conv2)

    # norm2
    norm2 = tf.nn.lrn(conv2,
                      4,
                      bias=1.0,
                      alpha=0.001 / 9.0,
                      beta=0.75,
                      name='norm2')
    # pool2
    pool2 = tf.nn.max_pool(norm2,
                           ksize=[1, 3, 3, 1],
                           strides=[1, 2, 2, 1],
                           padding='SAME',
                           name='pool2')

    # local3
    with tf.variable_scope('local3') as scope:
        # Move everything into depth so we can perform a single matrix multiply.
        reshape = tf.reshape(pool2, [FLAGS.batch_size, -1])
        dim = reshape.get_shape()[1].value
        weights = _variable_with_weight_decay('weights',
                                              shape=[dim, 384],
                                              stddev=0.04,
                                              wd=0.004)
        biases = _variable_on_cpu('biases', [384],
                                  tf.constant_initializer(0.1))
        local3 = tf.nn.relu(tf.matmul(reshape, weights) + biases,
                            name=scope.name)
        _activation_summary(local3)

    # local4
    with tf.variable_scope('local4') as scope:
        weights = _variable_with_weight_decay('weights',
                                              shape=[384, 192],
                                              stddev=0.04,
                                              wd=0.004)
        biases = _variable_on_cpu('biases', [192],
                                  tf.constant_initializer(0.1))
        local4 = tf.nn.relu(tf.matmul(local3, weights) + biases,
                            name=scope.name)
        _activation_summary(local4)

    # softmax, i.e. softmax(WX + b)
    with tf.variable_scope('softmax_linear') as scope:
        weights = _variable_with_weight_decay('weights', [192, NUM_CLASSES],
                                              stddev=1 / 192.0,
                                              wd=0.0)
        biases = _variable_on_cpu('biases', [NUM_CLASSES],
                                  tf.constant_initializer(0.0))
        softmax_linear = tf.add(tf.matmul(local4, weights),
                                biases,
                                name=scope.name)
        _activation_summary(softmax_linear)

    return softmax_linear
Example #51
0
def build(H, q):
    '''
    Build full model for training, including forward / backward passes,
    optimizers, and summary statistics.
    '''
    arch = H
    solver = H["solver"]

    os.environ['CUDA_VISIBLE_DEVICES'] = str(solver.get('gpu', ''))

    #gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.8)
    gpu_options = tf.GPUOptions()
    config = tf.ConfigProto(gpu_options=gpu_options)

    learning_rate = tf.placeholder(tf.float32)
    if solver['opt'] == 'RMS':
        opt = tf.train.RMSPropOptimizer(learning_rate=learning_rate,
                                        decay=0.9,
                                        epsilon=solver['epsilon'])
    elif solver['opt'] == 'Adam':
        opt = tf.train.AdamOptimizer(learning_rate=learning_rate,
                                     epsilon=solver['epsilon'])
    elif solver['opt'] == 'SGD':
        opt = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
    else:
        raise ValueError('Unrecognized opt type')
    loss, accuracy, confidences_loss, boxes_loss = {}, {}, {}, {}
    for phase in ['train', 'test']:
        # generate predictions and losses from forward pass
        x, p1_x, p2_x, p3_x, p4_x, p5_x, p6_x, p7_x, p8_x, f_x, confidences, boxes = q[
            phase].dequeue_many(arch['batch_size'])
        flags = tf.argmax(confidences, 3)

        grid_size = H['grid_width'] * H['grid_height']

        (pred_boxes, pred_confidences, loss[phase], confidences_loss[phase],
         boxes_loss[phase]) = build_forward_backward(H, x, p1_x, p2_x, p3_x,
                                                     p4_x, p5_x, p6_x, p7_x,
                                                     p8_x, f_x, phase, boxes,
                                                     flags)
        pred_confidences_r = tf.reshape(
            pred_confidences,
            [H['batch_size'], grid_size, H['rnn_len'], arch['num_classes']])
        pred_boxes_r = tf.reshape(
            pred_boxes, [H['batch_size'], grid_size, H['rnn_len'], 4])

        # Set up summary operations for tensorboard
        a = tf.equal(tf.argmax(confidences[:, :, 0, :], 2),
                     tf.argmax(pred_confidences_r[:, :, 0, :], 2))
        accuracy[phase] = tf.reduce_mean(tf.cast(a, 'float32'),
                                         name=phase + '/accuracy')

        if phase == 'train':
            global_step = tf.Variable(0, trainable=False)

            tvars = tf.trainable_variables()
            if H['clip_norm'] <= 0:
                grads = tf.gradients(loss['train'], tvars)
            else:
                grads, norm = tf.clip_by_global_norm(
                    tf.gradients(loss['train'], tvars), H['clip_norm'])
            train_op = opt.apply_gradients(zip(grads, tvars),
                                           global_step=global_step)
        elif phase == 'test':
            moving_avg = tf.train.ExponentialMovingAverage(0.95)
            smooth_op = moving_avg.apply([
                accuracy['train'],
                accuracy['test'],
                confidences_loss['train'],
                boxes_loss['train'],
                confidences_loss['test'],
                boxes_loss['test'],
            ])

            for p in ['train', 'test']:
                tf.scalar_summary('%s/accuracy' % p, accuracy[p])
                tf.scalar_summary('%s/accuracy/smooth' % p,
                                  moving_avg.average(accuracy[p]))
                tf.scalar_summary("%s/confidences_loss" % p,
                                  confidences_loss[p])
                tf.scalar_summary("%s/confidences_loss/smooth" % p,
                                  moving_avg.average(confidences_loss[p]))
                tf.scalar_summary("%s/regression_loss" % p, boxes_loss[p])
                tf.scalar_summary("%s/regression_loss/smooth" % p,
                                  moving_avg.average(boxes_loss[p]))

        if phase == 'test':
            test_image = x
            # show ground truth to verify labels are correct
            test_true_confidences = confidences[0, :, :, :]
            test_true_boxes = boxes[0, :, :, :]

            # show predictions to visualize training progress
            test_pred_confidences = pred_confidences_r[0, :, :, :]
            test_pred_boxes = pred_boxes_r[0, :, :, :]

            def log_image(np_img, np_confidences, np_boxes, np_global_step,
                          pred_or_true):

                merged = train_utils.add_rectangles(H,
                                                    np_img,
                                                    np_confidences,
                                                    np_boxes,
                                                    use_stitching=True,
                                                    rnn_len=H['rnn_len'])[0]

                num_images = 10
                img_path = os.path.join(
                    H['save_dir'], '%s_%s.jpg' %
                    ((np_global_step / H['logging']['display_iter']) %
                     num_images, pred_or_true))
                misc.imsave(img_path, merged)
                return merged

            pred_log_img = tf.py_func(log_image, [
                test_image, test_pred_confidences, test_pred_boxes,
                global_step, 'pred'
            ], [tf.float32])
            true_log_img = tf.py_func(log_image, [
                test_image, test_true_confidences, test_true_boxes,
                global_step, 'true'
            ], [tf.float32])
            tf.image_summary(phase + '/pred_boxes',
                             tf.pack(pred_log_img),
                             max_images=10)
            tf.image_summary(phase + '/true_boxes',
                             tf.pack(true_log_img),
                             max_images=10)

    summary_op = tf.merge_all_summaries()

    return (config, loss, accuracy, summary_op, train_op, smooth_op,
            global_step, learning_rate)
Example #52
0
    def _build_graph(self, input_vars):
        xys = np.array([(y,x,1) for y in range(WARP_TARGET_SIZE)
                        for x in range(WARP_TARGET_SIZE)], dtype='float32')
        xys = tf.constant(xys, dtype=tf.float32, name='xys')    # p x 3

        image, label = input_vars

        image = image / 255.0 - 0.5 # bhw2

        def get_stn(image):
            stn = (LinearWrap(image)
                .AvgPooling('downsample', 2)
                .Conv2D('conv0', 20, 5, padding='VALID')
                .MaxPooling('pool0', 2)
                .Conv2D('conv1', 20, 5, padding='VALID')
                .FullyConnected('fc1', out_dim=32)
                .FullyConnected('fct', out_dim=6, nl=tf.identity,
                    W_init=tf.constant_initializer(),
                    b_init=tf.constant_initializer([1, 0, HALF_DIFF, 0, 1, HALF_DIFF]))())
            # output 6 parameters for affine transformation
            stn = tf.reshape(stn, [-1, 2, 3], name='affine') # bx2x3
            stn = tf.reshape(tf.transpose(stn, [2, 0, 1]), [3, -1]) # 3 x (bx2)
            coor = tf.reshape(tf.matmul(xys, stn),
                    [WARP_TARGET_SIZE, WARP_TARGET_SIZE, -1, 2])
            coor = tf.transpose(coor, [2, 0, 1, 3], 'sampled_coords') # b h w 2
            sampled = ImageSample('warp', [image, coor], borderMode='constant')
            return sampled

        with argscope([Conv2D, FullyConnected], nl=tf.nn.relu):
            with tf.variable_scope('STN1'):
                sampled1 = get_stn(image)
            with tf.variable_scope('STN2'):
                sampled2 = get_stn(image)

        # For visualization in tensorboard
        padded1 = tf.pad(sampled1, [[0,0],[HALF_DIFF,HALF_DIFF],[HALF_DIFF,HALF_DIFF],[0,0]])
        padded2 = tf.pad(sampled2, [[0,0],[HALF_DIFF,HALF_DIFF],[HALF_DIFF,HALF_DIFF],[0,0]])
        img_orig = tf.concat(1, [image[:,:,:,0], image[:,:,:,1]])   #b x 2h  x w
        transform1 = tf.concat(1, [padded1[:,:,:,0], padded1[:,:,:,1]])
        transform2 = tf.concat(1, [padded2[:,:,:,0], padded2[:,:,:,1]])
        stacked = tf.concat(2, [img_orig, transform1, transform2], 'viz')
        tf.image_summary('visualize',
                tf.expand_dims(stacked, -1), max_images=30)

        sampled = tf.concat(3, [sampled1, sampled2], 'sampled_concat')
        logits = (LinearWrap(sampled)
                .apply(symbf.batch_flatten)
                .FullyConnected('fc1', out_dim=256, nl=tf.nn.relu)
                .FullyConnected('fc2', out_dim=128, nl=tf.nn.relu)
                .FullyConnected('fct', out_dim=19, nl=tf.identity)())
        prob = tf.nn.softmax(logits, name='prob')

        cost = tf.nn.sparse_softmax_cross_entropy_with_logits(logits, label)
        cost = tf.reduce_mean(cost, name='cross_entropy_loss')

        wrong = symbolic_functions.prediction_incorrect(logits, label)
        summary.add_moving_summary(tf.reduce_mean(wrong, name='train_error'))

        wd_cost = tf.mul(1e-5, regularize_cost('fc.*/W', tf.nn.l2_loss),
                         name='regularize_loss')
        summary.add_moving_summary(cost, wd_cost)
        self.cost = tf.add_n([wd_cost, cost], name='cost')
Example #53
0
def batch_inputs(dataset,
                 batch_size,
                 train,
                 num_preprocess_threads=None,
                 num_readers=1):
    """Contruct batches of training or evaluation examples from the image dataset.

  Args:
    dataset: instance of Dataset class specifying the dataset.
      See dataset.py for details.
    batch_size: integer
    train: boolean
    num_preprocess_threads: integer, total number of preprocessing threads
    num_readers: integer, number of parallel readers

  Returns:
    images: 4-D float Tensor of a batch of images
    labels: 1-D integer Tensor of [batch_size].

  Raises:
    ValueError: if data is not found
  """
    with tf.name_scope('batch_processing'):
        data_files = dataset.data_files()
        if data_files is None:
            raise ValueError('No data files found for this dataset')

        # Create filename_queue
        if train:
            filename_queue = tf.train.string_input_producer(data_files,
                                                            shuffle=True,
                                                            capacity=16)
        else:
            filename_queue = tf.train.string_input_producer(data_files,
                                                            shuffle=False,
                                                            capacity=1)
        if num_preprocess_threads is None:
            num_preprocess_threads = FLAGS.num_preprocess_threads

        if num_preprocess_threads % 4:
            raise ValueError(
                'Please make num_preprocess_threads a multiple '
                'of 4 (%d % 4 != 0).', num_preprocess_threads)

        if num_readers is None:
            num_readers = FLAGS.num_readers

        if num_readers < 1:
            raise ValueError('Please make num_readers at least 1')

        # Approximate number of examples per shard.
        examples_per_shard = 1024
        # Size the random shuffle queue to balance between good global
        # mixing (more examples) and memory use (fewer examples).
        # 1 image uses 299*299*3*4 bytes = 1MB
        # The default input_queue_memory_factor is 16 implying a shuffling queue
        # size: examples_per_shard * 16 * 1MB = 17.6GB
        min_queue_examples = examples_per_shard * FLAGS.input_queue_memory_factor
        if train:
            examples_queue = tf.RandomShuffleQueue(
                capacity=min_queue_examples + 3 * batch_size,
                min_after_dequeue=min_queue_examples,
                dtypes=[tf.string])
        else:
            examples_queue = tf.FIFOQueue(capacity=examples_per_shard +
                                          3 * batch_size,
                                          dtypes=[tf.string])

        reader = tf.TFRecordReader()
        _, example_serialized = reader.read(filename_queue)
        filename, label_index, bbox, label_text = parse_example_proto(
            example_serialized)

        fn = FLAGS.data_dir + '/' + label_text + '/' + filename

        examples_qr = tf.train.queue_runner.QueueRunner(
            examples_queue, [examples_queue.enqueue([fn])])
        tf.train.queue_runner.add_queue_runner(examples_qr)

        images_and_labels = []
        for thread_id in range(num_preprocess_threads):
            # Parse a serialized Example proto to extract the image and metadata.

            whole_file_reader = tf.WholeFileReader()
            _, image_buffer = whole_file_reader.read(examples_queue)

            image = image_preprocessing(image_buffer, bbox, train, thread_id)
            images_and_labels.append([image, label_index])

        images, label_index_batch = tf.train.batch_join(
            images_and_labels,
            batch_size=batch_size,
            capacity=2 * num_preprocess_threads * batch_size)

        # Reshape images into these desired dimensions.
        height = FLAGS.image_size
        width = FLAGS.image_size
        depth = 3

        images = tf.cast(images, tf.float32)
        images = tf.reshape(images, shape=[batch_size, height, width, depth])

        # Display the training images in the visualizer.
        tf.image_summary('images', images)

        return images, tf.reshape(label_index_batch, [batch_size])
Example #54
0
def train(is_training, logits, images, labels):
    global_step = tf.get_variable('global_step', [],
                                  initializer=tf.constant_initializer(0),
                                  trainable=False)
    val_step = tf.get_variable('val_step', [],
                               initializer=tf.constant_initializer(0),
                               trainable=False)

    loss_ = loss(logits, labels)
    predictions = tf.nn.softmax(logits)

    top1_error = top_k_error(predictions, labels, 1)

    # loss_avg
    ema = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step)
    tf.add_to_collection(UPDATE_OPS_COLLECTION, ema.apply([loss_]))
    tf.summary.scalar('loss_avg', ema.average(loss_))

    # validation stats
    ema = tf.train.ExponentialMovingAverage(0.9, val_step)
    val_op = tf.group(val_step.assign_add(1), ema.apply([top1_error]))
    top1_error_avg = ema.average(top1_error)
    tf.summary.scalar('val_top1_error_avg', top1_error_avg)

    tf.summary.scalar('learning_rate', FLAGS.learning_rate)

    opt = tf.train.MomentumOptimizer(FLAGS.learning_rate, MOMENTUM)
    grads = opt.compute_gradients(loss_)
    for grad, var in grads:
        if grad is not None and not FLAGS.minimal_summaries:
            tf.histogram_summary(var.op.name + '/gradients', grad)
    apply_gradient_op = opt.apply_gradients(grads, global_step=global_step)

    if not FLAGS.minimal_summaries:
        # Display the training images in the visualizer.
        tf.image_summary('images', images)

        for var in tf.trainable_variables():
            tf.histogram_summary(var.op.name, var)

    batchnorm_updates = tf.get_collection(UPDATE_OPS_COLLECTION)
    batchnorm_updates_op = tf.group(*batchnorm_updates)
    train_op = tf.group(apply_gradient_op, batchnorm_updates_op)

    saver = tf.train.Saver(tf.all_variables())

    summary_op = tf.summary.merge_all()

    init = tf.initialize_all_variables()

    sess = tf.Session(config=tf.ConfigProto(log_device_placement=False))
    sess.run(init)
    tf.train.start_queue_runners(sess=sess)

    summary_writer = tf.summary.FileWriter(FLAGS.train_dir, sess.graph)

    if FLAGS.resume:
        latest = tf.train.latest_checkpoint(FLAGS.train_dir)
        if not latest:
            print("No checkpoint to continue from in %s" % FLAGS.train_dir)
            sys.exit(1)
        print("resume %s" % latest)
        saver.restore(sess, latest)

    for x in xrange(FLAGS.max_steps + 1):
        start_time = time.time()

        step = sess.run(global_step)
        i = [train_op, loss_]

        write_summary = step % 100 and step > 1
        if write_summary:
            i.append(summary_op)

        o = sess.run(i, {is_training: True})

        loss_value = o[1]

        duration = time.time() - start_time

        assert not np.isnan(loss_value), 'Model diverged with loss = NaN'

        if step % 5 == 0:
            examples_per_sec = FLAGS.batch_size / float(duration)
            format_str = ('step %d, loss = %.2f (%.1f examples/sec; %.3f '
                          'sec/batch)')
            print(format_str % (step, loss_value, examples_per_sec, duration))

        if write_summary:
            summary_str = o[2]
            summary_writer.add_summary(summary_str, step)

        # Save the model checkpoint periodically.
        if step > 1 and step % 100 == 0:
            checkpoint_path = os.path.join(FLAGS.train_dir, 'model.ckpt')
            saver.save(sess, checkpoint_path, global_step=global_step)

        # Run validation periodically
        if step > 1 and step % 100 == 0:
            _, top1_error_value = sess.run([val_op, top1_error],
                                           {is_training: False})
            print('Validation top1 error %.2f' % top1_error_value)
def preprocess_for_train(image, height, width, bbox,
                         fast_mode=True,
                         scope=None):
  """Distort one image for training a network.

  Distorting images provides a useful technique for augmenting the data
  set during training in order to make the network invariant to aspects
  of the image that do not effect the label.

  Additionally it would create image_summaries to display the different
  transformations applied to the image.

  Args:
    image: 3-D Tensor of image. If dtype is tf.float32 then the range should be
      [0, 1], otherwise it would converted to tf.float32 assuming that the range
      is [0, MAX], where MAX is largest positive representable number for
      int(8/16/32) data type (see `tf.image.convert_image_dtype` for details).
    height: integer
    width: integer
    bbox: 3-D float Tensor of bounding boxes arranged [1, num_boxes, coords]
      where each coordinate is [0, 1) and the coordinates are arranged
      as [ymin, xmin, ymax, xmax].
    fast_mode: Optional boolean, if True avoids slower transformations (i.e.
      bi-cubic resizing, random_hue or random_contrast).
    scope: Optional scope for name_scope.
  Returns:
    3-D float Tensor of distorted image used for training with range [-1, 1].
  """
  with tf.name_scope(scope, 'distort_image', [image, height, width, bbox]):
    if bbox is None:
      bbox = tf.constant([0.0, 0.0, 1.0, 1.0],
                         dtype=tf.float32,
                         shape=[1, 1, 4])
    if image.dtype != tf.float32:
      image = tf.image.convert_image_dtype(image, dtype=tf.float32)
    # Each bounding box has shape [1, num_boxes, box coords] and
    # the coordinates are ordered [ymin, xmin, ymax, xmax].
    image_with_box = tf.image.draw_bounding_boxes(tf.expand_dims(image, 0),
                                                  bbox)
    tf.image_summary('image_with_bounding_boxes', image_with_box)

    distorted_image, distorted_bbox = distorted_bounding_box_crop(image, bbox)
    # Restore the shape since the dynamic slice based upon the bbox_size loses
    # the third dimension.
    distorted_image.set_shape([None, None, 3])
    image_with_distorted_box = tf.image.draw_bounding_boxes(
        tf.expand_dims(image, 0), distorted_bbox)
    tf.image_summary('images_with_distorted_bounding_box',
                     image_with_distorted_box)

    # This resizing operation may distort the images because the aspect
    # ratio is not respected. We select a resize method in a round robin
    # fashion based on the thread number.
    # Note that ResizeMethod contains 4 enumerated resizing methods.

    # We select only 1 case for fast_mode bilinear.
    num_resize_cases = 1 if fast_mode else 4
    distorted_image = apply_with_random_selector(
        distorted_image,
        lambda x, method: tf.image.resize_images(x, [height, width], method=method),
        num_cases=num_resize_cases)

    tf.image_summary('cropped_resized_image',
                     tf.expand_dims(distorted_image, 0))

    # Randomly flip the image horizontally.
    distorted_image = tf.image.random_flip_left_right(distorted_image)

    # Randomly distort the colors. There are 4 ways to do it.
    distorted_image = apply_with_random_selector(
        distorted_image,
        lambda x, ordering: distort_color(x, ordering, fast_mode),
        num_cases=4)

    tf.image_summary('final_distorted_image',
                     tf.expand_dims(distorted_image, 0))
    distorted_image = tf.sub(distorted_image, 0.5)
    distorted_image = tf.mul(distorted_image, 2.0)
    return distorted_image
def train_a_font(input_filters_dict,output_feature_list, nEpochs=5000):
 
    ds = ocr_utils.read_data(input_filters_dict = input_filters_dict, 
                                output_feature_list=output_feature_list,
                                test_size = .1,
                                engine_type='tensorflow',
                                dtype=dtype)

        
    """# ==============================================================================
    
    Start TensorFlow Interactive Session
    
    """# ==============================================================================

    sess = tf.InteractiveSession()
    
    """# ==============================================================================
    
    Placeholders
    
    Compute the size of various layers 
    
    Create a tensorflow Placeholder for each feature of data returned from the
    dataset
    
    """# ==============================================================================

    

    lst = []
    extra_features_width = 0 # width of extra features
    
    for i,nm in enumerate(output_feature_list):
        
        # features[0], is always the target. For instance it may be m_label_one_hot 
        # the second features[1] is the 'image' that is passed to the convolution layers 
        # Any additional features bypass the convolution layers and go directly 
        # into the fully connected layer.  
        
        # The width of the extra features is calculated in order to allocate 
        # the correct widths of weights,  # and inputs 
        # names are assigned to make the look pretty on the tensorboard graph.
        
        if i == 0:
            nm = 'y_'+nm
        else:
            nm = 'x_'+nm
        if i>1:
            extra_features_width += ds.train.feature_width[i]
        lst.append(tf.placeholder(dtype, shape=[None, ds.train.feature_width[i]], name=nm))
        
    # ph is a named tuple with key names like 'image', 'm_label', and values that
    # are tensors.  The display name on the Chrome graph are 'y_m_label', 'x_image, 
    # x_upper_case etc.
    Place_Holders = namedtuple('Place_Holders', output_feature_list)   
    ph = Place_Holders(*lst) # unpack placeholders into named Tuple
        
    nRows = ds.train.num_rows #image height
    nCols = ds.train.num_columns #image width    
    nFc0 = 2048      # size of fully connected layer
    nFc1 = 2048      # size of fully connected layer        
    nFc2 = 2048      # size of fully connected layer    
    nConv1 = 32     # size of first convolution layer
    nConv2 = 64     # size of second convolution layer
    nTarget = ds.train.feature_width[0]  # the number of one_hot features in the target, 'm_label'    
    n_h_pool2_outputs = int(nRows/4) * int(nCols/4) * nConv2 # second pooling layer 
    n_h_pool2_outputsx = n_h_pool2_outputs + extra_features_width # fully connected
        
    """# ==============================================================================
    
    Build a Multilayer Convolutional Network
    
    Weight Initialization
    
    """# ==============================================================================
    
    def weight_variable(shape, dtype):
        initial = tf.truncated_normal(shape, stddev=0.1,dtype=dtype)
        return tf.Variable(initial)
    
    def bias_variable(shape, dtype):
        initial = tf.constant(0.1, shape=shape, dtype=dtype)
        return tf.Variable(initial)   
    
    """# ==============================================================================
    
    Convolution and Pooling
    
    keep our code cleaner, let's also abstract those operations into functions.
    
    """# ==============================================================================
    
    def conv2d(x, W):
        return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
    
    def max_pool_2x2(x):
        return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
                            strides=[1, 2, 2, 1], padding='SAME')
    
    """# ==============================================================================
    
    First Convolutional Layer
    
    """# ==============================================================================
    with tf.name_scope("w_conv1") as scope:
        W_conv1 = weight_variable([5, 5, 1, nConv1],dtype)
        b_conv1 = bias_variable([nConv1],dtype)    
    
    with tf.name_scope("reshape_x_image") as scope:
        x_image = tf.reshape(ph.image, [-1,nCols,nRows,1])
    
    image_summ = tf.image_summary("x_image", x_image)
    
    """# ==============================================================================
    
    We then convolve x_image with the weight tensor, add the bias, apply the ReLU function,
     and finally max pool.
    
    """# ==============================================================================
    
    with tf.name_scope("convolve_1") as scope:
        h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
        
    with tf.name_scope("pool_1") as scope:    
        h_pool1 = max_pool_2x2(h_conv1)
    
    """# ==============================================================================
    
    Second Convolutional Layer
    
    In order to build a deep network, we stack several layers of this type. The second 
    layer will have 64 features for each 5x5 patch.
    
    """# ==============================================================================
    
    with tf.name_scope("convolve_2") as scope:
        W_conv2 = weight_variable([5, 5, nConv1, nConv2],dtype)
        b_conv2 = bias_variable([64],dtype)
        h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)  
         
    with tf.name_scope("pool_2") as scope:
        h_pool2 = max_pool_2x2(h_conv2)
    
    """# ==============================================================================
    
    Densely Connected Layer 0
    
    Now that the image size has been reduced to 7x7, we add a fully-connected layer 
    with neurons to allow processing on the entire image. We reshape the tensor 
    from the pooling layer into a batch of vectors, multiply by a weight matrix, add 
    a bias, and apply a ReLU.
    
    """# ==============================================================================
    
    with tf.name_scope("W_fc0_b") as scope:
        W_fc0 = weight_variable([n_h_pool2_outputsx, nFc0],dtype)
        b_fc0 = bias_variable([nFc0],dtype)
            
        h_pool2_flat = tf.reshape(h_pool2, [-1, n_h_pool2_outputs])
        
        # append the features, the 2nd on, that go directly to the fully connected layer
        for i in range(2,ds.train.num_features ):
            h_pool2_flat = tf.concat(1, [h_pool2_flat, ph[i]])  
        h_fc0 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc0) + b_fc0)
    """# ==============================================================================
    
    Densely Connected Layer 1
    
    We add a fully-connected layer 
    with neurons to allow processing on the entire image. We reshape the tensor 
    from the pooling layer into a batch of vectors, multiply by a weight matrix, add 
    a bias, and apply a ReLU.
    
    """# ==============================================================================  
          
    with tf.name_scope("W_fc1_b") as scope:
        W_fc1 = weight_variable([nFc0, nFc1],dtype)
        b_fc1 = bias_variable([nFc1],dtype)
        
        h_fc1 = tf.nn.relu(tf.matmul(h_fc0, W_fc1) + b_fc1)    
    
    """# ==============================================================================
    
    Densely Connected Layer 2
    
    We add a fully-connected layer 
    with neurons to allow processing on the entire image. We reshape the tensor 
    from the pooling layer into a batch of vectors, multiply by a weight matrix, add 
    a bias, and apply a ReLU.
    
    """# ==============================================================================
    
    with tf.name_scope("W_fc2_b") as scope:
        W_fc2 = weight_variable([nFc1, nFc2],dtype)
        b_fc2 = bias_variable([nFc2],dtype)
        
        h_fc2 = tf.nn.relu(tf.matmul(h_fc1, W_fc2) + b_fc2)
        
    """# ==============================================================================        
    Dropout
    
    """# ==============================================================================
    keep_prob = tf.placeholder(dtype,name='keep_prob')
    
    with tf.name_scope("drop") as scope:
        h_fc2_drop = tf.nn.dropout(h_fc2, keep_prob)
    
    """# ==============================================================================
    
    Readout Layer
    
    """# ==============================================================================
    with tf.name_scope("softmax") as scope:
        W_fc3 = weight_variable([nFc2, nTarget],dtype)
        b_fc3 = bias_variable([nTarget],dtype)    
        y_conv=tf.nn.softmax(tf.matmul(h_fc2_drop, W_fc3) + b_fc3)
    
    """# ==============================================================================
    
    Train and Evaluate the Model
    
    """# ==============================================================================
    
    with tf.name_scope("xent") as scope:
        # 1e-8 added to eliminate the crash of training when taking log of 0
        cross_entropy = -tf.reduce_sum(ph[0]*tf.log(y_conv+1e-8))
        ce_summ = tf.scalar_summary("cross entropy", cross_entropy)
            
    with tf.name_scope("train") as scope:
        train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
        
    with tf.name_scope("test") as scope:        
        correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(ph[0],1))
    
        accuracy = tf.reduce_mean(tf.cast(correct_prediction,dtype))
        accuracy_summary = tf.scalar_summary("accuracy", accuracy)    
    
    merged = tf.merge_all_summaries()
    tm = ""
    tp = datetime.datetime.now().timetuple()
    for i in range(4):
        tm += str(tp[i])+'-'
    tm += str(tp[4])    
    writer = tf.train.SummaryWriter("/tmp/ds_logs/"+ tm, sess.graph)
    
    # To see the results in Chrome, 
    # Run the following in terminal to activate server.
    # tensorboard --logdir '/tmp/ds_logs/'
    # See results on localhost:6006 
    
    sess.run(tf.initialize_all_variables())
    
    perfect_count=10
    for i in range(nEpochs):
    
        batch = ds.train.next_batch(100)
        # assign feature data to each placeholder
        # the batch list is returned in the same order as the features requested
        feed = {keep_prob: 0.5}
        for j in range(ds.train.num_features):
            feed[ph[j]] = batch[j]  
            
        if i%100 == 0:
            # sh=h_pool2_flat.get_shape()
            feed[keep_prob] = 1.0
            result = sess.run([merged, accuracy ], feed_dict=feed)    
            summary_str = result[0]
            #acc = result[1]       
            writer.add_summary(summary_str, i)
            train_accuracy = accuracy.eval(feed)    
            if train_accuracy != 1:
                perfect_count=10;
            else:
                perfect_count -= 1
                if perfect_count==0:
                    break;  
                
            print ("step %d, training accuracy %g"%(i, train_accuracy),flush=True)
        train_step.run(feed_dict=feed)
    
    def computeSize(s,tens):
        sumC = 1
        tShape = tens.get_shape()
        nDims = len(tShape)
        for i in range(nDims):
            sumC *= tShape[i].value
        print ('\t{}\t{}'.format(s,sumC),flush=True)
        return sumC
            
    print ('network size:',flush=True)
    total = computeSize("W_fc0",W_fc0)+ \
    computeSize ("b_fc0",b_fc0) + \
    computeSize ("W_conv1",W_conv1) + \
    computeSize ("b_conv1",b_conv1) + \
    computeSize ("W_conv2",W_conv2) + \
    computeSize ("b_conv2",b_conv2) + \
    computeSize ("W_fc0",W_fc0) + \
    computeSize ("b_fc0",b_fc0) + \
    computeSize ("W_fc1",W_fc1) + \
    computeSize ("b_fc1",b_fc1) + \
    computeSize ("W_fc2",W_fc2) + \
    computeSize ("b_fc2",b_fc2)   

    
    print('\ttotal\t{}'.format(total),flush=True)
    
    feed={keep_prob: 1.0}
    # assign feature data to each placeholder
    error_images = np.empty((0,nRows,nCols))
        
    test_accuracy=0
    m=0
    for n in range(0,ds.test.features[0].shape[0],100 ):   
        for i in range(ds.train.num_features ):  
            feed[ph[i]] = ds.test.features[i] [n:n+100]
        result = sess.run([accuracy, x_image, W_conv1, correct_prediction], feed_dict=feed)    
        test_accuracy += result[0]
        error_images = np.append(error_images, result[1][:,:,:,0][result[3]==False],axis=0)
        m += 1
    try:        
        print ("test accuracy {} for font: {}".format(test_accuracy/m, input_filters_dict['font']),flush=True)       
        ocr_utils.montage(error_images,title='TensorFlow {} Error Images'.format(input_filters_dict['font']))  
    except:                            
        print ("test accuracy {}".format(test_accuracy/m),flush=True)       
        ocr_utils.montage(error_images,title='TensorFlow Error Images')    
    
    tf.reset_default_graph() # only necessary when iterating through fonts
    sess.close()
Example #57
0
                                                  [1, 100, 100, 1])
                    h_pool1_imgs[i] = h_pool1_part_img
                h_pool1_img1 = tf.concat(1, h_pool1_imgs[0:7])
                h_pool1_img2 = tf.concat(1, h_pool1_imgs[7:14])
                h_pool1_img3 = tf.concat(1, h_pool1_imgs[14:21])
                h_pool1_img4 = tf.concat(1, h_pool1_imgs[21:28])
                h_pool1_img5 = tf.concat(1, h_pool1_imgs[28:35])
                h_pool1_img6 = tf.concat(1, h_pool1_imgs[35:42])
                h_pool1_img7 = tf.concat(1, h_pool1_imgs[42:49])
                h_pool1_img = tf.concat(2, [
                    h_pool1_img1, h_pool1_img2, h_pool1_img3, h_pool1_img4,
                    h_pool1_img5, h_pool1_img6, h_pool1_img7
                ])

                h_pool1_sum = tf.image_summary("h_pool1_Visualize" + str(i),
                                               h_pool1_img,
                                               max_images=10)
                h_pool1_sum_str = sess.run(h_pool1_sum,
                                           feed_dict={
                                               x: batch[0],
                                               y_: batch[1],
                                               keep_prob: 1.0
                                           })
                train_writer.add_summary(h_pool1_sum_str, i)

            train_step.run(feed_dict={
                x: batch[0],
                y_: batch[1],
                keep_prob: 0.5
            })
Example #58
0
def build_graph(train):
    with tf.device("/cpu:0"):
        with tf.name_scope('X'):
            x = tf.placeholder(tf.float32, [None, 125, 160, 1], name='x')
            mlp = x

        if train:
            with tf.name_scope('RANDOM-CROP-FLIP'):
                crop_x = tf.map_fn(
                    lambda img: tf.random_crop(img, [112, 144, 1]), mlp)
                crop_x = tf.map_fn(
                    lambda img: tf.image.random_flip_up_down(img), crop_x)
                mlp = crop_x
        else:
            with tf.name_scope('CENTER-CROP'):
                crop_x = tf.map_fn(
                    lambda img: tf.image.resize_image_with_crop_or_pad(
                        img, 112, 144), mlp)
                mlp = crop_x

    with tf.name_scope('CONV-1'):
        c1 = 16
        res = tf.pad(mlp, [[0, 0], [0, 0], [0, 0], [0, c1 - 1]])
        mlp = conv(mlp, weight([3, 3, 1, c1], name='w11')) + positive_bias(
            [c1], name='b11')
        mlp = tf.nn.relu(mlp, name='conv1')
        mlp = conv(mlp, weight([3, 3, c1, c1], name='w12')) + positive_bias(
            [c1], name='b12')
        mlp = tf.nn.relu(mlp, name='conv2')
        mlp = conv(mlp, weight([3, 3, c1, c1], name='w13')) + positive_bias(
            [c1], name='b13')
        mlp = tf.nn.relu(mlp, name='conv3')
        # mlp = conv(mlp, weight([3, 3, c1, c1], name='w14')) + positive_bias([c1], name='b14')
        # mlp = tf.nn.relu(mlp, name='conv4')
        mlp = tf.add(mlp, res, name='res')
        mlp = pool(mlp, name='pool')

    with tf.name_scope('CONV-2'):
        c2 = 32
        res = tf.pad(mlp, [[0, 0], [0, 0], [0, 0], [0, c2 - c1]])
        mlp = conv(mlp, weight([3, 3, c1, c2], name='w21')) + positive_bias(
            [c2], name='b21')
        mlp = tf.nn.relu(mlp, name='conv1')
        mlp = conv(mlp, weight([3, 3, c2, c2], name='w22')) + positive_bias(
            [c2], name='b22')
        mlp = tf.nn.relu(mlp, name='conv2')
        mlp = conv(mlp, weight([3, 3, c2, c2], name='w23')) + positive_bias(
            [c2], name='b23')
        mlp = tf.nn.relu(mlp, name='conv3')
        # mlp = conv(mlp, weight([3, 3, c2, c2], name='w24')) + positive_bias([c2], name='b24')
        # mlp = tf.nn.relu(mlp, name='conv4')
        mlp = tf.add(mlp, res, name='res')
        mlp = pool(mlp, name='pool')

    with tf.name_scope('CONV-3'):
        c3 = 64
        res = tf.pad(mlp, [[0, 0], [0, 0], [0, 0], [0, c3 - c2]])
        mlp = conv(mlp, weight([3, 3, c2, c3], name='w31')) + positive_bias(
            [c3], name='b31')
        mlp = tf.nn.relu(mlp, name='conv1')
        mlp = conv(mlp, weight([3, 3, c3, c3], name='w32')) + positive_bias(
            [c3], name='b32')
        mlp = tf.nn.relu(mlp, name='conv2')
        # mlp = conv(mlp, weight([3, 3, c3, c3], name='w33')) + positive_bias([c3], name='b33')
        # mlp = tf.nn.relu(mlp, name='conv3')
        # mlp = conv(mlp, weight([3, 3, c3, c3], name='w34')) + positive_bias([c3], name='b34')
        # mlp = tf.nn.relu(mlp, name='conv4')
        mlp = tf.add(mlp, res, name='res')
        mlp = pool(mlp, name='pool')

    with tf.name_scope('CONV-4'):
        c4 = 128
        res = tf.pad(mlp, [[0, 0], [0, 0], [0, 0], [0, c4 - c3]])
        mlp = conv(mlp, weight([3, 3, c3, c4], name='w41')) + positive_bias(
            [c4], name='b41')
        mlp = tf.nn.relu(mlp, name='conv1')
        mlp = conv(mlp, weight([3, 3, c4, c4], name='w42')) + positive_bias(
            [c4], name='b42')
        mlp = tf.nn.relu(mlp, name='conv2')
        # mlp = conv(mlp, weight([3, 3, c4, c4], name='w43')) + positive_bias([c4], name='b43')
        # mlp = tf.nn.relu(mlp, name='conv3')
        # mlp = conv(mlp, weight([3, 3, c4, c4], name='w44')) + positive_bias([c4], name='b44')
        # mlp = tf.nn.relu(mlp, name='conv4')
        mlp = tf.add(mlp, res, name='res')
        mlp = pool(mlp, name='pool')

    with tf.name_scope('MASK'):
        ca = 66
        mask = tf.reshape(mlp, [-1, c4])
        mask = tf.nn.xw_plus_b(mask, weight([c4, ca], 'w5'),
                               zero_bias([ca], 'b5'))
        mask = tf.tanh(mask)
        mask = tf.nn.xw_plus_b(mask, weight([ca, 1], 'w6'), zero_bias([1],
                                                                      'b6'))
        mask = tf.reshape(mask, [-1, 7, 9])
        mask = (mask + tf.reverse(mask, [False, True, False])) / 2
        mask = tf.reshape(mask, [-1, 7 * 9])
        mask = tf.nn.softmax(mask)
        mask = tf.reshape(mask, [-1, 7, 9, 1])

        mlp = tf.mul(mlp, mask)
        mlp = tf.reduce_sum(mlp, [1, 2], True)

    if train:
        with tf.name_scope('DROPOUT'):
            mlp = tf.nn.dropout(mlp,
                                0.5,
                                noise_shape=tf.shape(mlp) * [1, 0, 0, 1] +
                                [0, 1, 1, 0],
                                name='dropout')  # dropout by map

    with tf.name_scope('FLAT'):
        mlp = tf.reshape(mlp, [-1, c4], name='flat')
    '''
    if train:
        with tf.name_scope('DROPOUT'):
            mlp = tf.nn.dropout(mlp, 0.5, name='dropout')
    '''

    # 1FC
    with tf.name_scope('FC'):
        mlp = tf.nn.xw_plus_b(mlp,
                              weight([c4, 7], name='w7'),
                              zero_bias([7], name='b7'),
                              name='fc')

    with tf.name_scope('Y'):
        y = tf.placeholder(tf.float32, [None, 7], name='y')

    with tf.name_scope('SOFTMAX-WITH-LOSS'):
        loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(mlp, y),
                              name='loss')

    with tf.name_scope('SOFTMAX'):
        prob = tf.nn.softmax(mlp, name='prob')

    with tf.name_scope('ACC'):
        acc = tf.equal(tf.argmax(prob, 1), tf.argmax(y, 1), name='correct')
        acc = tf.reduce_mean(tf.cast(acc, tf.float32), name='acc')

    if train:
        with tf.name_scope('OPT'):
            opt = tf.train.AdamOptimizer(name='opt')
            train_op = opt.minimize(loss, name='train_op')
    else:
        train_op = None

    # 创建summary
    with tf.name_scope('SUM'):
        # mask 为方便展示仅尺度变化
        # mask_m = tf.reduce_min(mask, [1, 2], True)
        mask_M = tf.reduce_max(mask, [1, 2], True)
        mask_visual = mask / mask_M * 255.0  # mask_visual = (mask-mask_m) / (mask_M-mask_m) * 255.0

        # prj_mask 为方便展示仅尺度变化
        prj_mask = mask
        prj_mask = tf.nn.conv2d(prj_mask,
                                tf.ones([3, 3, 1, 1]) / 9.0,
                                strides=[1, 1, 1, 1],
                                padding='SAME')
        prj_mask = tf.nn.conv2d(prj_mask,
                                tf.ones([3, 3, 1, 1]) / 9.0,
                                strides=[1, 1, 1, 1],
                                padding='SAME')
        prj_mask = tf.image.resize_nearest_neighbor(
            prj_mask,
            tf.shape(prj_mask)[1:3] * 2)
        prj_mask = tf.nn.conv2d(prj_mask,
                                tf.ones([3, 3, 1, 1]) / 9.0,
                                strides=[1, 1, 1, 1],
                                padding='SAME')
        prj_mask = tf.nn.conv2d(prj_mask,
                                tf.ones([3, 3, 1, 1]) / 9.0,
                                strides=[1, 1, 1, 1],
                                padding='SAME')
        prj_mask = tf.image.resize_nearest_neighbor(
            prj_mask,
            tf.shape(prj_mask)[1:3] * 2)
        prj_mask = tf.nn.conv2d(prj_mask,
                                tf.ones([3, 3, 1, 1]) / 9.0,
                                strides=[1, 1, 1, 1],
                                padding='SAME')
        prj_mask = tf.nn.conv2d(prj_mask,
                                tf.ones([3, 3, 1, 1]) / 9.0,
                                strides=[1, 1, 1, 1],
                                padding='SAME')
        prj_mask = tf.nn.conv2d(prj_mask,
                                tf.ones([3, 3, 1, 1]) / 9.0,
                                strides=[1, 1, 1, 1],
                                padding='SAME')
        prj_mask = tf.image.resize_nearest_neighbor(
            prj_mask,
            tf.shape(prj_mask)[1:3] * 2)
        prj_mask = tf.nn.conv2d(prj_mask,
                                tf.ones([3, 3, 1, 1]) / 9.0,
                                strides=[1, 1, 1, 1],
                                padding='SAME')
        prj_mask = tf.nn.conv2d(prj_mask,
                                tf.ones([3, 3, 1, 1]) / 9.0,
                                strides=[1, 1, 1, 1],
                                padding='SAME')
        prj_mask = tf.nn.conv2d(prj_mask,
                                tf.ones([3, 3, 1, 1]) / 9.0,
                                strides=[1, 1, 1, 1],
                                padding='SAME')
        prj_mask = tf.image.resize_nearest_neighbor(
            prj_mask,
            tf.shape(prj_mask)[1:3] * 2)
        # prj_mask_m = tf.reduce_min(prj_mask, [1, 2], True)
        prj_mask_M = tf.reduce_max(prj_mask, [1, 2], True)
        prj_mask_visual = prj_mask / prj_mask_M * 255.0  # prj_mask_visual = (prj_mask-prj_mask_m) / (prj_mask_M-prj_mask_m) * 255.0

        # mask_crop_x 为方便展示动态范围变化
        mask_crop_x = prj_mask * crop_x
        mask_crop_x_m = tf.reduce_min(mask_crop_x, [1, 2], True)
        mask_crop_x_M = tf.reduce_max(mask_crop_x, [1, 2], True)
        mask_crop_x_visual = (mask_crop_x - mask_crop_x_m) / (
            mask_crop_x_M - mask_crop_x_m) * 255.0

        # y
        y_visual = tf.reshape(y, [-1, 1, 7, 1]) * 255.0

        # prob
        prob_visual = tf.reshape(prob, [-1, 1, 7, 1]) * 255.0

    if train:
        summary = tf.merge_summary([
            tf.image_summary('train mask', mask_visual),  # 1 7 9 1
            tf.image_summary('train prj_mask', prj_mask_visual),  # 1 112 144 1
            tf.image_summary('train crop_x', crop_x),  # 1 112 144 1
            tf.image_summary('train mask_crop_x',
                             mask_crop_x_visual),  # 1 112 144 1
            tf.image_summary('train y', y_visual),  # 1 1 7 1
            tf.image_summary('train prob', prob_visual),  # 1 1 7 1
            tf.scalar_summary('train loss', loss),
            tf.scalar_summary('train acc', acc),
        ])
    else:
        summary = tf.merge_summary([
            tf.image_summary('val mask', mask_visual),  # 1 7 9 1
            tf.image_summary('val prj_mask', prj_mask_visual),  # 1 112 144 1
            tf.image_summary('val crop_x', crop_x),  # 1 112 144 1
            tf.image_summary('val mask_crop_x',
                             mask_crop_x_visual),  # 1 112 144 1
            tf.image_summary('val y', y_visual),  # 1 1 7 1
            tf.image_summary('val prob', prob_visual),  # 1 1 7 1
            tf.scalar_summary('val loss', loss),
            tf.scalar_summary('val acc', acc),
        ])

    return [x, y, loss, prob, acc, train_op, summary, crop_x, mask]
Example #59
0
def get_summary(images):
    #Construct the summary object that sends the images tensor to Tensorboard for display (displays a maximum of $max_images images)
    return tf.image_summary("data", images, max_images=FLAGS.num_iterations)
Example #60
0
 def init_logging(self, sess):
     w_summary_t = tf.image_summary('w1', w)
     self.w_summary_t = w_summary_t
     return summary_op