Esempio n. 1
0
def main(args):
    if not os.path.exists(FLAGS.checkpoint):
        tf.logging.fatal(
            'Checkpoint %s does not exist. Have you download it? See tools/download_data.sh',
            FLAGS.checkpoint)
    g = tf.Graph()
    with g.as_default():
        input_image = PreprocessImage(FLAGS.image_path[0])

        with slim.arg_scope(inception.inception_v3_arg_scope()):
            logits, end_points = inception.inception_v3(
                input_image, num_classes=FLAGS.num_classes, is_training=False)

        bottleneck = end_points['PreLogits']
        init_op = control_flow_ops.group(
            variables.initialize_all_variables(),
            variables.initialize_local_variables(),
            data_flow_ops.initialize_all_tables())
        saver = tf_saver.Saver()
        sess = tf.Session()
        saver.restore(sess, FLAGS.checkpoint)

        # Run the evaluation on the image
        bottleneck_eval = np.squeeze(sess.run(bottleneck))

    first = True
    for val in bottleneck_eval:
        if not first:
            sys.stdout.write(",")
        first = False
        sys.stdout.write('{:.3f}'.format(val))
    sys.stdout.write('\n')
def build_inceptionv3_graph(images, endpoint, is_training, checkpoint,
                            reuse=False):
  """Builds an InceptionV3 model graph.

  Args:
    images: A 4-D float32 `Tensor` of batch images.
    endpoint: String, name of the InceptionV3 endpoint.
    is_training: Boolean, whether or not to build a training or inference graph.
    checkpoint: String, path to the pretrained model checkpoint.
    reuse: Boolean, whether or not we are reusing the embedder.
  Returns:
    inception_output: `Tensor` holding the InceptionV3 output.
    inception_variables: List of inception variables.
    init_fn: Function to initialize the weights (if not reusing, then None).
  """
  with slim.arg_scope(inception.inception_v3_arg_scope()):
    _, endpoints = inception.inception_v3(
        images, num_classes=1001, is_training=is_training)
    inception_output = endpoints[endpoint]
    inception_variables = slim.get_variables_to_restore()
    inception_variables = [
        i for i in inception_variables if 'global_step' not in i.name]
    if is_training and not reuse:
      init_saver = tf.train.Saver(inception_variables)
      def init_fn(scaffold, sess):
        del scaffold
        init_saver.restore(sess, checkpoint)
    else:
      init_fn = None
    return inception_output, inception_variables, init_fn
Esempio n. 3
0
def process_images(serialized_images):
    def decode(jpeg_str, central_fraction=0.875, image_size=299):
        decoded = tf.cast(tf.image.decode_jpeg(jpeg_str, channels=3),
                          tf.float32)
        cropped = tf.image.central_crop(decoded,
                                        central_fraction=central_fraction)
        resized = tf.squeeze(
            tf.image.resize_bilinear(tf.expand_dims(cropped, [0]),
                                     [image_size, image_size],
                                     align_corners=False), [0])
        resized.set_shape((image_size, image_size, 3))
        normalized = tf.subtract(tf.multiply(resized, 1.0 / 127.5), 1.0)

        return normalized

    def process(images, image_size=299):
        images = tf.map_fn(decode, images, dtype=tf.float32)

        return images

    images = process(serialized_images)

    with slim.arg_scope(inception.inception_v3_arg_scope()):
        logits, end_points = inception.inception_v3(images,
                                                    num_classes=num_classes,
                                                    is_training=False)

    features = tf.reshape(end_points['PreLogits'], [-1, 2048])
    class_predictions = tf.nn.sigmoid(logits)

    return features, class_predictions
Esempio n. 4
0
def prep_graph():
    global predictions
    global labelmap
    global label_dict
    global sess
    global input_image
    global food_list
    food_list = []
    with open(food_names) as f:
        for x in f:
            food_list.append(x.rstrip())
    g = tf.Graph()
    with g.as_default():
        input_image = tf.placeholder(tf.string)
        processed_image = PreprocessImage(input_image)
        with slim.arg_scope(inception.inception_v3_arg_scope()):
            logits, end_points = inception.inception_v3(processed_image,
                                                        num_classes=6012,
                                                        is_training=False)
        predictions = end_points['multi_predictions'] = tf.nn.sigmoid(
            logits, name='multi_predictions')
        init_op = control_flow_ops.group(
            variables.initialize_all_variables(),
            variables.initialize_local_variables(),
            data_flow_ops.initialize_all_tables())
        saver = tf_saver.Saver()
        sess = tf.Session()
        saver.restore(sess, checkpoint)
        labelmap, label_dict = LoadLabelMaps(6012, labelmap_file,
                                             label_dict_file)
def main(args):
  if not os.path.exists(FLAGS.checkpoint):
    tf.logging.fatal(
        'Checkpoint %s does not exist. Have you download it? See tools/download_data.sh',
        FLAGS.checkpoint)
  g = tf.Graph()
  with g.as_default():
    input_image = PreprocessImage(FLAGS.image_path[0])

    with slim.arg_scope(inception.inception_v3_arg_scope()):
      logits, end_points = inception.inception_v3(
          input_image, num_classes=FLAGS.num_classes, is_training=False)

    bottleneck = end_points['PreLogits']
    init_op = tf.group(tf.global_variables_initializer(),
                       tf.local_variables_initializer(),
                       tf.tables_initializer())
    saver = tf_saver.Saver()
    sess = tf.Session()
    saver.restore(sess, FLAGS.checkpoint)

    # Run the evaluation on the image
    bottleneck_eval = np.squeeze(sess.run(bottleneck))

  first = True
  for val in bottleneck_eval:
    if not first:
      sys.stdout.write(",")
    first = False
    sys.stdout.write('{:.3f}'.format(val))
  sys.stdout.write('\n')
Esempio n. 6
0
def main(args):
    if not os.path.exists(FLAGS.checkpoint):
        tf.logging.fatal(
            'Checkpoint %s does not exist. Have you download it? See tools/download_data.sh',
            FLAGS.checkpoint)
    g = tf.Graph()
    with g.as_default():
        input_image = tf.placeholder(tf.string)
        processed_image = PreprocessImage(input_image)

        with slim.arg_scope(inception.inception_v3_arg_scope()):
            logits, end_points = inception.inception_v3(
                processed_image, num_classes=FLAGS.num_classes, is_training=False)

        predictions = end_points['multi_predictions'] = tf.nn.sigmoid(
            logits, name='multi_predictions')

        sess = tf.Session()

        saver = tf_saver.Saver()

        logits_2 = layers.conv2d(
            end_points['PreLogits'],
            FLAGS.num_classes, [1, 1],
            activation_fn=None,
            normalizer_fn=None,
            scope='Conv2d_final_1x1')

        logits_2 = array_ops.squeeze(logits_2, [1, 2], name='SpatialSqueeze_2')

        predictions_2 = end_points['multi_predictions_2'] = tf.nn.sigmoid(logits_2, name='multi_predictions_2')

        sess.run(tf.global_variables_initializer())

        saver.restore(sess, FLAGS.checkpoint)
Esempio n. 7
0
 def load(self):
     if self.session is None:
         if len(self.labelmap) != self.num_classes:
             logging.error(
                 "{} lines while the number of classes is {}".format(
                     len(self.labelmap), self.num_classes))
         self.label_dict = {}
         for line in tf.gfile.GFile(self.dict_path).readlines():
             words = [word.strip(' "\n') for word in line.split(',', 1)]
             self.label_dict[words[0]] = words[1]
         logging.warning(
             "Loading the network {} , first apply / query will be slower".
             format(self.name))
         config = tf.ConfigProto()
         config.gpu_options.per_process_gpu_memory_fraction = self.gpu_fraction
         g = tf.Graph()
         with g.as_default():
             self.input_image = tf.placeholder(tf.string)
             processed_image = inception_preprocess(self.input_image)
             with slim.arg_scope(inception.inception_v3_arg_scope()):
                 logits, end_points = inception.inception_v3(
                     processed_image,
                     num_classes=self.num_classes,
                     is_training=False)
             self.predictions = end_points[
                 'multi_predictions'] = tf.nn.sigmoid(
                     logits, name='multi_predictions')
             saver = tf_saver.Saver()
             self.session = tf.InteractiveSession(config=config)
             saver.restore(self.session, self.network_path)
Esempio n. 8
0
    def classify(self, image=None, image_path=None):
        #image = tf.gfile.FastGFile(image_path, 'rb').read()
        image_data = tf.image.decode_jpeg(image, channels=3)
        processed_image = inception_preprocessing.preprocess_image(
            image_data,
            inception.inception_v3.default_image_size,
            inception.inception_v3.default_image_size,
            is_training=False)
        processed_images = tf.expand_dims(processed_image, 0)

        with slim.arg_scope(inception.inception_v3_arg_scope()):
            logits, end_points = inception.inception_v3(processed_images,
                                                        num_classes=1001,
                                                        is_training=False)

        # In order to get probabilities we apply softmax on the output.
        probabilities = tf.nn.softmax(logits)

        init_fn = slim.assign_from_checkpoint_fn(
            "C:\\Users\\emman\\PycharmProjects\\TensorWebApi\\models\\inception\\inception_v3.ckpt",
            slim.get_model_variables('InceptionV3'))

        init_fn(self.session)

        list = []
        for op in self.graph.get_operations():
            for i in [
                    "Conv2d_1a_3x3", "Conv2d_2a_3x3", "Conv2d_2b_3x3",
                    "MaxPool_3a_3x3", "Conv2d_3b_1x1", "Conv2d_4a_3x3",
                    "MaxPool_5a_3x3"
            ]:
                if i in str(op.name):
                    list.append(op)
                    break

        to_graph = tf.Graph()

        for i in list:
            cg.copy_op_to_graph(org_instance=i,
                                to_graph=to_graph,
                                variables="")

        self.graph = to_graph

        # for a in self.graph.get_operations():
        #    print(str(a.name))

        start_time = time.time()

        np_image, network_input, probabilities = self.session.run(
            [image_data, processed_image, probabilities])

        duration = time.time() - start_time
        print("Compute time: " + str(duration))

        probabilities = probabilities[0, 0:]

        return probabilities
def main(img_dir):
 
  if not os.path.exists(FLAGS.checkpoint):
    tf.logging.fatal(
        'Checkpoint %s does not exist. Have you download it? See tools/download_data.sh',
        FLAGS.checkpoint)
  g = tf.Graph()
  with g.as_default():
    input_image = tf.placeholder(tf.string)
    processed_image = PreprocessImage(input_image)

    with slim.arg_scope(inception.inception_v3_arg_scope()):
      logits, end_points = inception.inception_v3(processed_image, num_classes=FLAGS.num_classes, is_training=False)

    predictions = end_points['multi_predictions'] = tf.nn.sigmoid(logits, name='multi_predictions')
    saver = tf_saver.Saver()
    sess = tf.Session()
    saver.restore(sess, FLAGS.checkpoint)
    
    # img_dir = sorted(glob.glob(os.path.join(FLAGS.image_folder_path, '*.jpg')))
    # sorted(img_dir, key = lambda d: d[-7: -4])   
   
  
  # Run the evaluation on the images
  image_path = os.path.join(FLAGS.image_folder_path, img_dir)

  # for i in range(len(img_dir)):
    # image_path = img_dir[i]
  if not os.path.exists(image_path):
    tf.logging.fatal('Input image does not exist %s', image_path)
  img_data = tf.gfile.FastGFile(image_path, "rb").read()
  print(image_path)
  predictions_eval = np.squeeze(sess.run(predictions, {input_image: img_data}))

  # Print top(n) results
  labelmap, label_dict = LoadLabelMaps(FLAGS.num_classes, FLAGS.labelmap, FLAGS.dict)

  top_k = predictions_eval.argsort()[-FLAGS.n:][::-1]
  label_confidence_dic = {}
  display_label_name = []
  display_score = []
  for idx in top_k:
    mid = labelmap[idx]
    display_name = label_dict.get(mid, 'unknown')
    score = predictions_eval[idx]
    label_confidence_dic[display_name] = score

    display_label_name.append(display_name)
    display_score.append(score)

    print('{}: {} - {} (score = {:.2f})'.format(idx, mid, display_name, score))

    display_dict = dict({'name': display_label_name, 'score': display_score})
    display_df = pd.DataFrame(display_dict)
  return display_df
Esempio n. 10
0
def inception_net(images, num_classes, for_training=False, reuse=False):
    """Build Inception v3 model architecture."""

    with slim.arg_scope(inception.inception_v3_arg_scope()):
        logits, endpoints = inception.inception_v3(images,
                                                   dropout_keep_prob=0.8,
                                                   num_classes=num_classes,
                                                   is_training=for_training,
                                                   reuse=reuse,
                                                   scope='InceptionV3')

    return logits, endpoints
Esempio n. 11
0
def label(image_path,
          checkpoint="openimages_dataset/data/2016_08/model.ckpt",
          num_classes=6012,
          labelmap_path="openimages_dataset/data/2016_08/labelmap.txt",
          dict_path="openimages_dataset/dict.csv",
          threshold=0.5,
          rounding_digits=1):
    if not os.path.exists(checkpoint):
        tf.logging.fatal(
            'Checkpoint %s does not exist. Have you download it? See tools/download_data.sh',
            checkpoint)
    g = tf.Graph()
    with g.as_default():
        input_image = PreprocessImage(image_path)

        with slim.arg_scope(inception.inception_v3_arg_scope()):
            logits, end_points = inception.inception_v3(
                input_image, num_classes=num_classes, is_training=False)

        predictions = end_points['multi_predictions'] = tf.nn.sigmoid(
            logits, name='multi_predictions')
        init_op = control_flow_ops.group(
            variables.initialize_all_variables(),
            variables.initialize_local_variables(),
            data_flow_ops.initialize_all_tables())
        saver = tf_saver.Saver()
        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True
        sess = tf.Session(config=config)
        saver.restore(sess, checkpoint)

        # Run the evaluation on the image
        predictions_eval = np.squeeze(sess.run(predictions))

    # Print top(n) results
    labelmap, label_dict = LoadLabelMaps(num_classes, labelmap_path, dict_path)

    top_k = predictions_eval.argsort()[:][::-1]
    returned_labels = []
    for idx in top_k:
        mid = labelmap[idx]
        display_name = label_dict.get(mid, 'unknown')
        score = predictions_eval[idx]
        if score < threshold:
            if returned_labels:
                break
            else:
                threshold -= 0.1
                if threshold < 0.1:
                    break
        returned_labels.append((display_name, score))

    return returned_labels
Esempio n. 12
0
def main(args):
    if not os.path.exists(FLAGS.checkpoint):
        tf.logging.fatal(
            'Checkpoint %s does not exist. Have you download it? See tools/download_data.sh',
            FLAGS.checkpoint)
    g = tf.Graph()
    with g.as_default():
        input_image = tf.placeholder(tf.string)
        processed_image = PreprocessImage(input_image)

        with slim.arg_scope(inception.inception_v3_arg_scope()):
            logits, end_points = inception.inception_v3(
                processed_image,
                num_classes=FLAGS.num_classes,
                is_training=False)

        predictions = end_points['multi_predictions'] = tf.nn.sigmoid(
            logits, name='multi_predictions')
        init_op = control_flow_ops.group(
            variables.initialize_all_variables(),
            variables.initialize_local_variables(),
            data_flow_ops.initialize_all_tables())
        saver = tf_saver.Saver()
        sess = tf.Session()
        saver.restore(sess, FLAGS.checkpoint)

        # Run the evaluation on the images
        for image_path in FLAGS.image_path:
            if not os.path.exists(image_path):
                tf.logging.fatal('Input image does not exist %s',
                                 FLAGS.image_path[0])
            img_data = tf.gfile.FastGFile(image_path).read()
            print(image_path)
            predictions_eval = np.squeeze(
                sess.run(predictions, {input_image: img_data}))

            # Print top(n) results
            labelmap, label_dict = LoadLabelMaps(FLAGS.num_classes,
                                                 FLAGS.labelmap, FLAGS.dict)

            top_k = predictions_eval.argsort()[-FLAGS.n:][::-1]
            for idx in top_k:
                mid = labelmap[idx]
                display_name = label_dict.get(mid, 'unknown')
                score = predictions_eval[idx]
                print('{}: {} - {} (score = {:.2f})'.format(
                    idx, mid, display_name, score))
            print()
Esempio n. 13
0
 def load(self):
     if self.session is None:
         logging.warning("Loading the network {} , first apply / query will be slower".format(self.name))
         config = tf.ConfigProto()
         config.gpu_options.per_process_gpu_memory_fraction = self.gpu_fraction
         network_path = os.path.abspath(__file__).split('annotator.py')[0]+'data/2016_08/model.ckpt'
         g = tf.Graph()
         with g.as_default():
             self.input_image = tf.placeholder(tf.string)
             processed_image = inception_preprocess(self.input_image)
             with slim.arg_scope(inception.inception_v3_arg_scope()):
                 logits, end_points = inception.inception_v3(processed_image, num_classes=self.num_classes, is_training=False)
             self.predictions = end_points['multi_predictions'] = tf.nn.sigmoid(logits, name='multi_predictions')
             saver = tf_saver.Saver()
             self.session = tf.InteractiveSession(config=config)
             saver.restore(self.session, network_path)
Esempio n. 14
0
 def load(self):
     if self.session is None:
         logging.warning("Loading the network {} , first apply / query will be slower".format(self.name))
         config = tf.ConfigProto()
         config.gpu_options.per_process_gpu_memory_fraction = 0.15
         network_path = os.path.abspath(__file__).split('annotator.py')[0]+'data/2016_08/model.ckpt'
         g = tf.Graph()
         with g.as_default():
             self.input_image = tf.placeholder(tf.string)
             processed_image = inception_preprocess(self.input_image)
             with slim.arg_scope(inception.inception_v3_arg_scope()):
                 logits, end_points = inception.inception_v3(processed_image, num_classes=self.num_classes, is_training=False)
             self.predictions = end_points['multi_predictions'] = tf.nn.sigmoid(logits, name='multi_predictions')
             saver = tf_saver.Saver()
             self.session = tf.InteractiveSession(config=config)
             saver.restore(self.session, network_path)
Esempio n. 15
0
    def _build_model(self):
        self.x_input = tf.placeholder(tf.float32, shape=[None, 299, 299, 3])
        self.y_input = tf.placeholder(tf.int64, shape=[None])

        with slim.arg_scope(inception_v3_arg_scope()):
            logits, _ = inception_v3(self.x_input, num_classes=1001,
                                     is_training= self.mode == 'train')

        self.pre_softmax = logits
        self.y_pred = tf.argmax(self.pre_softmax, 1)

        self.y_xent = tf.nn.sparse_softmax_cross_entropy_with_logits(
            labels=self.y_input, logits=self.pre_softmax
        )
        self.xent = tf.reduce_sum(self.y_xent)

        self.correct_prediction = tf.equal(self.y_pred, self.y_input)

        self.num_correct = tf.reduce_sum(tf.cast(self.correct_prediction, tf.int64))
        self.accuracy = tf.reduce_mean(tf.cast(self.correct_prediction, tf.float32))
Esempio n. 16
0
def main(argv):

    if FLAGS.dataset == 'ifind':
        files = tf.data.Dataset.list_files(
            tf.gfile.Glob(FLAGS.data_dir + '/train/*'))
        dataset = files.interleave(tf.data.TFRecordDataset,
                                   cycle_length=4,
                                   block_length=16)
        dataset = dataset.map(_parse_function_ifind)

    elif FLAGS.dataset == 'kingscollege':
        dataset = tf.data.TFRecordDataset(FLAGS.data_dir +
                                          '/dataset_train.tfrecords')
        dataset = dataset.map(_parse_function_kingscollege)

    else:
        print('Invalid Option:', FLAGS.dataset)
        raise SystemExit

    dataset = dataset.repeat()
    dataset = dataset.shuffle(FLAGS.queue_buffer)
    dataset = dataset.batch(FLAGS.batch_size)
    image, vec, qt, AP1, AP2, AP3 = dataset.make_one_shot_iterator().get_next()

    # Network Definition

    image = tf.image.resize_images(image, size=[224, 224])
    # tf.summary.image('input',image,max_outputs=30)

    if FLAGS.loss == 'PoseNet':

        y_pred, _ = inception.inception_v3(image, num_classes=7)
        quaternion_pred, translation_pred = tf.split(y_pred, [4, 3], axis=1)

        l1 = tf.nn.l2_loss(quaternion_pred - qt, name='loss/l1')
        l2 = tf.nn.l2_loss(translation_pred - AP2, name='loss/l2')

        tf.summary.scalar('PoseNet_loss/loss_quaternion', l1)
        tf.summary.scalar('PoseNet_loss/loss_translation', l2)

        loss = l1 * 500 + l2

    elif FLAGS.loss == 'AP':

        y_pred, _ = inception.inception_v3(image, num_classes=9)
        AP1_pred, AP2_pred, AP3_pred = tf.split(y_pred, 3, axis=1)

        l1 = tf.nn.l2_loss(AP1_pred - AP1, name='loss/l1')
        l2 = tf.nn.l2_loss(AP2_pred - AP2, name='loss/l2')
        l3 = tf.nn.l2_loss(AP3_pred - AP3, name='loss/l3')

        tf.summary.scalar('AnchorPoints_loss/AP1', l1)
        tf.summary.scalar('AnchorPoints_loss/AP2', l2)
        tf.summary.scalar('AnchorPoints_loss/AP3', l3)

        loss = l1 + l2 + l3

    elif FLAGS.loss == 'SE3':

        from se3_geodesic_loss import SE3GeodesicLoss

        SE3_DIM = 6

        # SE3 Training Weights for KingsCollege Dataset:
        se3_weights = np.ones(SE3_DIM)  # Default
        # se3_weights = np.array([0.77848403, 0.6148858 , 0.12600519, 0.00018093, 0.00020279, 0.00082466]) # KingsCollege
        # se3_weights = np.array([0.06126877, 0.0811294 , 0.10352387, 0.65171058, 0.73758907, 0.10090916]) # iFIND
        loss = SE3GeodesicLoss(se3_weights)

        y_pred, _ = inception.inception_v3(image, num_classes=SE3_DIM)
        y_pred.set_shape([FLAGS.batch_size, SE3_DIM])

        y_true = tf.concat((vec, AP2), axis=1)
        y_true.set_shape([FLAGS.batch_size, SE3_DIM])

        with tf.variable_scope('SE3_loss'):
            loss = loss.geodesic_loss(y_pred, y_true)

    else:
        print('Invalid Option:', FLAGS.loss)
        raise SystemExit

    tf.summary.scalar('loss', loss)

    # Optimiser
    train_op = tf.train.AdamOptimizer(FLAGS.init_lr).minimize(loss)

    # Session Configuration
    config = tf.ConfigProto(
        # log_device_placement=True,
        # allow_soft_placement=True,
        gpu_options=tf.GPUOptions(allow_growth=True))

    saver = tf.train.Saver()
    with tf.Session(config=config) as sess:
        initializer = tf.group(tf.global_variables_initializer(),
                               tf.local_variables_initializer())

        sess.run(initializer)

        merged = tf.summary.merge_all()
        train_writer = tf.summary.FileWriter(FLAGS.model_dir + '/train',
                                             sess.graph)

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

        try:
            for step in range(FLAGS.train_iter):
                start_time = time.time()

                _, summary, loss_value = sess.run([train_op, merged, loss])

                train_writer.add_summary(summary, step)
                duration = time.time() - start_time
                if step % FLAGS.log_steps == 0:

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

                if step % FLAGS.ckpt_steps == 0:
                    tf.logging.log(tf.logging.INFO,
                                   'Saving Iteration: {}'.format(step))
                    saver.save(sess,
                               FLAGS.model_dir + '/iter_{}.ckpt'.format(step))

        except tf.errors.OutOfRangeError:
            # End of dataset
            tf.logging.log(tf.logging.INFO, 'End of Training')

        except KeyboardInterrupt:
            tf.logging.log(tf.logging.INFO, 'Keyboard Interrupt!')

        finally:
            tf.logging.log(tf.logging.INFO, 'Stopping Threads')
            coord.request_stop()
            coord.join(threads)
            tf.logging.log(tf.logging.INFO, 'Saving iter: {}'.format(step))
            saver.save(sess, FLAGS.model_dir + '/iter_{}.ckpt'.format(step))
Esempio n. 17
0
# create a list of all our filenames
filename_train = ['../dataset/KingsCollege/dataset_train.tfrecords']
#filename_test = ['../dataset/KingsCollege/dataset_test.tfrecords']

reader_train = PoseNetReader(filename_train)
#reader_eval = PoseNetReader(filename_test)

# Get Input Tensors
image, pose_q, pose_x = reader_train.read_and_decode()
#image, pose_q, pose_x = reader_eval.read_and_decode()


# Construct model and encapsulating all ops into scopes, making
# Tensorboard's Graph visualization more convenient
with tf.name_scope('Model'):
    py_x , _ = inception.inception_v3(tf.cast(image,tf.float32))

    py_x = tf.nn.relu(py_x)

    weights = {
        'h1': tf.Variable(tf.random_normal([1000, 4]),name='w_wpqr_out'),
        'h2': tf.Variable(tf.random_normal([1000, 3]),name='w_xyz_out'),
    }
    biases = {
        'b1': tf.Variable(tf.zeros([4]),name='b_wpqr_out'),
        'b2': tf.Variable(tf.zeros([3]),name='b_xyz_out'),
    }

    cls3_fc_pose_wpqr = tf.add(tf.matmul(py_x, weights['h1']), biases['b1'])
    cls3_fc_pose_xyz = tf.add(tf.matmul(py_x, weights['h2']), biases['b2'])
Esempio n. 18
0
# create a list of all our filenames
#filename_train = ['dataset/KingsCollege/dataset_train.tfrecords']
filename_test = ['../dataset/KingsCollege/dataset_test.tfrecords']

#reader_train = PoseNetReader(filename_train)
reader_eval = PoseNetReader(filename_test)

# Get Input Tensors
#image, pose_q, pose_x = reader_train.read_and_decode()
image, pose_q, pose_x = reader_eval.read_and_decode()

# Construct model and encapsulating all ops into scopes, making
# Tensorboard's Graph visualization more convenient
with tf.name_scope('Model'):
    py_x, _ = inception.inception_v3(tf.cast(image, tf.float32),
                                     is_training=False)

    py_x = tf.nn.relu(py_x)

    weights = {
        'h1': tf.Variable(tf.random_normal([1000, 4]), name='w_wpqr_out'),
        'h2': tf.Variable(tf.random_normal([1000, 3]), name='w_xyz_out'),
    }
    biases = {
        'b1': tf.Variable(tf.random_normal([4]), name='b_wpqr_out'),
        'b2': tf.Variable(tf.random_normal([3]), name='b_xyz_out'),
    }

    cls3_fc_pose_wpqr = tf.add(tf.matmul(py_x, weights['h1']), biases['b1'])
    cls3_fc_pose_xyz = tf.add(tf.matmul(py_x, weights['h2']), biases['b2'])
Esempio n. 19
0
def main(argv):

    # TF Record
    datafiles = FLAGS.data_dir + '/test/' + FLAGS.subject_id + '.tfrecord'
    dataset = tf.data.TFRecordDataset(datafiles)
    dataset = dataset.map(_parse_function_ifind)
    # dataset = dataset.repeat()
    # dataset = dataset.shuffle(FLAGS.queue_buffer)
    dataset = dataset.batch(1)
    image, vec, qt, AP1, AP2, AP3 = dataset.make_one_shot_iterator().get_next()

    # Nifti Volume
    subject_path = FLAGS.scan_dir + '/test/' + FLAGS.subject_id + '.nii.gz'
    fixed_image_sitk_tmp = sitk.ReadImage(subject_path, sitk.sitkFloat32)
    fixed_image_sitk = sitk.GetImageFromArray(
        sitk.GetArrayFromImage(fixed_image_sitk_tmp))
    fixed_image_sitk = sitk.RescaleIntensity(fixed_image_sitk, 0, 1) * 255.

    # Network Definition
    image_resized = tf.image.resize_images(image, size=[224, 224])

    # Measurements
    cc = []
    mse = []
    psnr = []
    ssim = []

    if FLAGS.loss == 'PoseNet':

        y_pred, _ = inception.inception_v3(image_resized,
                                           num_classes=7,
                                           is_training=False)
        quaternion_pred, translation_pred = tf.split(y_pred, [4, 3], axis=1)

        sess = tf.Session()

        ckpt_file = tf.train.latest_checkpoint(FLAGS.model_dir)
        tf.train.Saver().restore(sess, ckpt_file)
        print('restoring parameters from', ckpt_file)

        SO3_GROUP = SpecialOrthogonalGroup(3)

        for i in tqdm.tqdm(range(FLAGS.n_iter)):

            _image, _quaternion_true, _translation_true, _quaternion_pred, _translation_pred = \
                sess.run([image, qt, AP2, quaternion_pred, translation_pred])

            rx = SO3_GROUP.matrix_from_quaternion(_quaternion_pred)[0]
            tx = _translation_pred[0] * 60.

            image_true = np.squeeze(_image)
            image_pred = resample_sitk(fixed_image_sitk, rx, tx)

            imageio.imsave('imgdump/image_{}_true.png'.format(i),
                           np.uint8(_image[0, ...]))
            imageio.imsave('imgdump/image_{}_pred.png'.format(i),
                           np.uint8(image_pred))

            cc.append(calc_correlation(image_pred, image_true))
            mse.append(calc_mse(image_pred, image_true))
            psnr.append(calc_psnr(image_pred, image_true))
            ssim.append(calc_ssim(image_pred, image_true))

    elif FLAGS.loss == 'AP':

        y_pred, _ = inception.inception_v3(image_resized,
                                           num_classes=9,
                                           is_training=False)
        AP1_pred, AP2_pred, AP3_pred = tf.split(y_pred, 3, axis=1)

        sess = tf.Session()

        ckpt_file = tf.train.latest_checkpoint(FLAGS.model_dir)
        tf.train.Saver().restore(sess, ckpt_file)
        print('restoring parameters from', ckpt_file)

        for i in tqdm.tqdm(range(FLAGS.n_iter)):

            _image, _AP1, _AP2, _AP3, _AP1_pred, _AP2_pred, _AP3_pred = \
                sess.run([image, AP1, AP2, AP3, AP1_pred, AP2_pred, AP3_pred])

            dist_ap1 = np.linalg.norm(_AP1 - _AP1_pred)
            dist_ap2 = np.linalg.norm(_AP2 - _AP2_pred)
            dist_ap3 = np.linalg.norm(_AP3 - _AP3_pred)

            rx = matrix_from_anchor_points(_AP1_pred[0], _AP2_pred[0],
                                           _AP3_pred[0])
            tx = _AP2_pred[0] * 60.

            image_true = np.squeeze(_image)
            image_pred = resample_sitk(fixed_image_sitk, rx, tx)

            imageio.imsave('imgdump/image_{}_true.png'.format(i),
                           np.uint8(_image[0, ...]))
            imageio.imsave('imgdump/image_{}_pred.png'.format(i),
                           np.uint8(image_pred))

            cc.append(calc_correlation(image_pred, image_true))
            mse.append(calc_mse(image_pred, image_true))
            psnr.append(calc_psnr(image_pred, image_true))
            ssim.append(calc_ssim(image_pred, image_true))

    elif FLAGS.loss == 'SE3':

        y_pred, _ = inception.inception_v3(image_resized,
                                           num_classes=6,
                                           is_training=False)

        sess = tf.Session()

        ckpt_file = tf.train.latest_checkpoint(FLAGS.model_dir)
        tf.train.Saver().restore(sess, ckpt_file)
        print('restoring parameters from', ckpt_file)

        SO3_GROUP = SpecialOrthogonalGroup(3)
        SE3_GROUP = SpecialEuclideanGroup(3)
        _se3_err_i = []

        for i in tqdm.tqdm(range(FLAGS.n_iter)):

            _image, _rvec, _tvec, _y_pred = \
                sess.run([image, vec, AP2, y_pred])

            rx = SO3_GROUP.matrix_from_rotation_vector(_y_pred[0, :3])[0]
            tx = _y_pred[0, 3:] * 60.

            image_true = np.squeeze(_image)
            image_pred = resample_sitk(fixed_image_sitk, rx, tx)

            imageio.imsave('imgdump/image_{}_true.png'.format(i),
                           np.uint8(_image[0, ...]))
            imageio.imsave('imgdump/image_{}_pred.png'.format(i),
                           np.uint8(image_pred))

            cc.append(calc_correlation(image_pred, image_true))
            mse.append(calc_mse(image_pred, image_true))
            psnr.append(calc_psnr(image_pred, image_true))
            ssim.append(calc_ssim(image_pred, image_true))

            _y_true = np.concatenate((_rvec, _tvec), axis=-1)
            _se3_err_i.append(
                SE3_GROUP.compose(SE3_GROUP.inverse(_y_true), _y_pred))

        err_vec = np.vstack(_se3_err_i)
        err_weights = np.diag(np.linalg.inv(np.cov(err_vec.T)))
        err_weights = err_weights / np.linalg.norm(err_weights)
        print(err_weights)

    else:
        print('Invalid Option:', FLAGS.loss)
        raise SystemExit

    cc = np.stack(cc)
    mse = np.stack(mse)
    psnr = np.stack(psnr)
    ssim = np.stack(ssim)

    print('CC:', np.median(cc))
    print('MSE:', np.median(mse))
    print('PSNR:', np.median(psnr))
    print('SSIM:', np.median(ssim))
Esempio n. 20
0
checkpoint='data/2016_08/model.ckpt'
labelmap='data/2016_08/labelmap.txt'
dict='dict.csv'
image_size=299
num_classes=6012
n=10

g = tf.Graph()
with g.as_default():
    input_image = tf.placeholder(tf.float32,shape=[None,299,299,3])
    #processed_image = PreprocessImage(input_image,image_size)
    processed_image=input_image

    with slim.arg_scope(inception.inception_v3_arg_scope()):
      logits, end_points = inception.inception_v3(
          processed_image, num_classes=num_classes, is_training=False)

    predictions = end_points['multi_predictions'] = tf.nn.sigmoid(
        logits, name='multi_predictions')
    saver = tf_saver.Saver()
    sess = tf.Session()
    saver.restore(sess, checkpoint)


def classify(images):
#     embedding=np.empty([len(images),1000])
    with g.as_default():
        embedding=[]
        # Run the evaluation on the image

    #     print(images_x_x)
Esempio n. 21
0
def main(argv):

    # TF Record
    datafiles = FLAGS.data_dir + '/test/' + FLAGS.subject_id + '.tfrecord'
    dataset = tf.data.TFRecordDataset(datafiles)
    dataset = dataset.map(_parse_function_ifind)
    # dataset = dataset.repeat()
    # dataset = dataset.shuffle(FLAGS.queue_buffer)
    dataset = dataset.batch(1)
    image, vec, qt, AP1, AP2, AP3 = dataset.make_one_shot_iterator().get_next()

    # Nifti Volume
    subject_path = FLAGS.scan_dir + '/test/' + FLAGS.subject_id + '.nii.gz'
    fixed_image_sitk_tmp = sitk.ReadImage(subject_path, sitk.sitkFloat32)
    fixed_image_sitk = sitk.GetImageFromArray(
        sitk.GetArrayFromImage(fixed_image_sitk_tmp))
    fixed_image_sitk = sitk.RescaleIntensity(fixed_image_sitk, 0, 1)  # * 255.

    # Network Definition
    image_input = tf.placeholder(shape=[1, 224, 224, 1], dtype=tf.float32)
    image_resized = tf.image.resize_images(image, size=[224, 224])

    if FLAGS.loss == 'PoseNet':

        y_pred, _ = inception.inception_v3(image_input, num_classes=7)
        quaternion_pred, translation_pred = tf.split(y_pred, [4, 3], axis=1)

        sess = tf.Session()

        ckpt_file = tf.train.latest_checkpoint(FLAGS.model_dir)
        tf.train.Saver().restore(sess, ckpt_file)
        print('restoring parameters from', ckpt_file)

        SO3_GROUP = SpecialOrthogonalGroup(3)

        for i in range(FLAGS.n_iter):

            _image, _image_resized, _quaternion_true, _translation_true = \
                sess.run([image, image_resized, qt, AP2], )

            _quaternion_pred_sample = []
            _translation_pred_sample = []
            for j in range(FLAGS.n_samples):
                _quaternion_pred_i, _translation_pred_i = \
                    sess.run([quaternion_pred, translation_pred],
                             feed_dict={image_input: _image_resized})
                _quaternion_pred_sample.append(_quaternion_pred_i)
                _translation_pred_sample.append(_translation_pred_i)
                print(_quaternion_pred_i, _translation_pred_i)

            _quaternion_pred_sample = np.vstack(_quaternion_pred_sample)
            _rotvec_pred_sample = SO3_GROUP.rotation_vector_from_quaternion(
                _quaternion_pred_sample)
            _rotvec_pred = SO3_GROUP.left_canonical_metric.mean(
                _rotvec_pred_sample)

            _quaternion_pred = SO3_GROUP.quaternion_from_rotation_vector(
                _rotvec_pred)
            _translation_pred = np.mean(np.vstack(_translation_pred_sample),
                                        axis=0)

            # _quaternion_pred_variance = SO3_GROUP.left_canonical_metric.variance(_rotvec_pred_sample)
            _translation_pred_variance = np.var(
                np.vstack(_translation_pred_sample), axis=0)

            rx = SO3_GROUP.matrix_from_quaternion(_quaternion_pred)[0]
            tx = _translation_pred[0] * 60.

            image_true = np.squeeze(_image)
            image_pred = resample_sitk(fixed_image_sitk, rx, tx)

            imageio.imsave('imgdump/image_{}_true.png'.format(i), _image[0,
                                                                         ...])
            imageio.imsave('imgdump/image_{}_pred.png'.format(i), image_pred)

            calc_psnr(image_pred, image_true)
            calc_mse(image_pred, image_true)
            calc_ssim(image_pred, image_true)
            calc_correlation(image_pred, image_true)

    elif FLAGS.loss == 'AP':

        y_pred, _ = inception.inception_v3(image_input, num_classes=9)
        AP1_pred, AP2_pred, AP3_pred = tf.split(y_pred, 3, axis=1)

        sess = tf.Session()

        ckpt_file = tf.train.latest_checkpoint(FLAGS.model_dir)
        tf.train.Saver().restore(sess, ckpt_file)
        print('restoring parameters from', ckpt_file)

        for i in range(FLAGS.n_iter):

            _image, _image_resized, _AP1, _AP2, _AP3 = \
                sess.run([image, image_resized, AP1, AP2, AP3])

            _AP1_sample = []
            _AP2_sample = []
            _AP3_sample = []
            for j in range(FLAGS.n_samples):
                _AP1_pred_i, _AP2_pred_i, _AP3_pred_i = \
                    sess.run([AP1_pred, AP2_pred, AP3_pred],
                             feed_dict={image_input: _image_resized})
                _AP1_sample.append(_AP1_pred_i)
                _AP2_sample.append(_AP2_pred_i)
                _AP3_sample.append(_AP3_pred_i)

            _AP1_pred = np.mean(np.vstack(_AP1_sample), axis=0)
            _AP2_pred = np.mean(np.vstack(_AP2_sample), axis=0)
            _AP3_pred = np.mean(np.vstack(_AP3_sample), axis=0)

            _AP1_pred_variance = np.var(np.vstack(_AP1_sample), axis=0)
            _AP2_pred_variance = np.var(np.vstack(_AP2_sample), axis=0)
            _AP3_pred_variance = np.var(np.vstack(_AP3_sample), axis=0)

            dist_ap1 = np.linalg.norm(_AP1 - _AP1_pred)
            dist_ap2 = np.linalg.norm(_AP2 - _AP2_pred)
            dist_ap3 = np.linalg.norm(_AP3 - _AP3_pred)

            rx = matrix_from_anchor_points(_AP1_pred[0], _AP2_pred[0],
                                           _AP3_pred[0])
            tx = _AP2_pred[0] * 60.

            image_true = np.squeeze(_image)
            image_pred = resample_sitk(fixed_image_sitk, rx, tx)

            imageio.imsave('imgdump/image_{}_true.png'.format(i), _image[0,
                                                                         ...])
            imageio.imsave('imgdump/image_{}_pred.png'.format(i), image_pred)

            calc_psnr(image_pred, image_true)
            calc_mse(image_pred, image_true)
            calc_ssim(image_pred, image_true)
            calc_correlation(image_pred, image_true)

    elif FLAGS.loss == 'SE3':

        y_pred, _ = inception.inception_v3(image_input, num_classes=6)

        sess = tf.Session()

        ckpt_file = tf.train.latest_checkpoint(FLAGS.model_dir)
        tf.train.Saver().restore(sess, ckpt_file)
        print('restoring parameters from', ckpt_file)

        SO3_GROUP = SpecialOrthogonalGroup(3)
        SE3_GROUP = SpecialEuclideanGroup(3)

        for i in range(FLAGS.n_iter):

            print(i)

            _image, _image_resized, _rvec, _tvec = \
                sess.run([image, image_resized, vec, AP2])

            _y_pred_sample = []
            for j in range(FLAGS.n_samples):
                _y_pred_i = sess.run([y_pred],
                                     feed_dict={image_input: _image_resized})
                _y_pred_sample.append(_y_pred_i[0])

            _y_pred_sample = np.vstack(_y_pred_sample)
            _y_pred = SE3_GROUP.left_canonical_metric.mean(_y_pred_sample)
            _y_pred_variance = SE3_GROUP.left_canonical_metric.variance(
                _y_pred_sample)

            rx = SO3_GROUP.matrix_from_rotation_vector(_y_pred[0, :3])[0]
            tx = _y_pred[0, 3:] * 60.

            image_true = np.squeeze(_image)
            image_pred = resample_sitk(fixed_image_sitk, rx, tx)

            imageio.imsave('imgdump/image_{}_true.png'.format(i), _image[0,
                                                                         ...])
            imageio.imsave('imgdump/image_{}_pred.png'.format(i), image_pred)

            calc_psnr(image_pred, image_true)
            calc_mse(image_pred, image_true)
            calc_ssim(image_pred, image_true)
            calc_correlation(image_pred, image_true)

    else:
        print('Invalid Option:', FLAGS.loss)
        raise SystemExit
Esempio n. 22
0
from tensorflow.contrib.slim.python.slim.nets import inception
from tensorflow.python.training import saver as tf_saver
from tensorflow.python.framework import graph_util
slim = tf.contrib.slim

input_checkpoint = '/home/johnny/Documents/TF_CONFIG/finetune/resv2/model.ckpt'
output_file = 'inference_graph.pb'

g = tf.Graph()
with g.as_default():
    image = tf.placeholder(name='input',
                           dtype=tf.float32,
                           shape=[1, 299, 299, 3])
    with slim.arg_scope(inception.inception_v3_arg_scope()):
        logits, end_points = inception.inception_v3(image,
                                                    num_classes=6012,
                                                    is_training=False)
        predictions = tf.nn.sigmoid(logits, name='multi_predictions')
        saver = tf_saver.Saver()
        input_graph_def = g.as_graph_def()
        sess = tf.Session()
        saver.restore(sess, input_checkpoint)

        output_node_names = "multi_predictions"
        output_graph_def = graph_util.convert_variables_to_constants(
            sess,  # The session is used to retrieve the weights
            input_graph_def,  # The graph_def is used to retrieve the nodes
            output_node_names.split(
                ","
            )  # The output node names are used to select the usefull nodes
        )
Esempio n. 23
0
def VAE(input_shape=[None, 784],
        output_shape=[None, 784],
        n_filters=[64, 64, 64],
        filter_sizes=[4, 4, 4],
        n_hidden=32,
        n_code=2,
        activation=tf.nn.tanh,
        dropout=False,
        denoising=False,
        convolutional=False,
        variational=False,
        softmax=False,
        classifier='alexnet_v2'):
    """(Variational) (Convolutional) (Denoising) Autoencoder.

    Uses tied weights.

    Parameters
    ----------
    input_shape : list, optional
        Shape of the input to the network. e.g. for MNIST: [None, 784].
    n_filters : list, optional
        Number of filters for each layer.
        If convolutional=True, this refers to the total number of output
        filters to create for each layer, with each layer's number of output
        filters as a list.
        If convolutional=False, then this refers to the total number of neurons
        for each layer in a fully connected network.
    filter_sizes : list, optional
        Only applied when convolutional=True.  This refers to the ksize (height
        and width) of each convolutional layer.
    n_hidden : int, optional
        Only applied when variational=True.  This refers to the first fully
        connected layer prior to the variational embedding, directly after
        the encoding.  After the variational embedding, another fully connected
        layer is created with the same size prior to decoding.  Set to 0 to
        not use an additional hidden layer.
    n_code : int, optional
        Only applied when variational=True.  This refers to the number of
        latent Gaussians to sample for creating the inner most encoding.
    activation : function, optional
        Activation function to apply to each layer, e.g. tf.nn.relu
    dropout : bool, optional
        Whether or not to apply dropout.  If using dropout, you must feed a
        value for 'keep_prob', as returned in the dictionary.  1.0 means no
        dropout is used.  0.0 means every connection is dropped.  Sensible
        values are between 0.5-0.8.
    denoising : bool, optional
        Whether or not to apply denoising.  If using denoising, you must feed a
        value for 'corrupt_rec', as returned in the dictionary.  1.0 means no
        corruption is used.  0.0 means every feature is corrupted.  Sensible
        values are between 0.5-0.8.
    convolutional : bool, optional
        Whether or not to use a convolutional network or else a fully connected
        network will be created.  This effects the n_filters parameter's
        meaning.
    variational : bool, optional
        Whether or not to create a variational embedding layer.  This will
        create a fully connected layer after the encoding, if `n_hidden` is
        greater than 0, then will create a multivariate gaussian sampling
        layer, then another fully connected layer.  The size of the fully
        connected layers are determined by `n_hidden`, and the size of the
        sampling layer is determined by `n_code`.

    Returns
    -------
    model : dict
        {
            'cost': Tensor to optimize.
            'Ws': All weights of the encoder.
            'x': Input Placeholder
            'z': Inner most encoding Tensor (latent features)
            'y': Reconstruction of the Decoder
            'keep_prob': Amount to keep when using Dropout
            'corrupt_rec': Amount to corrupt when using Denoising
            'train': Set to True when training/Applies to Batch Normalization.
        }
    """
    # network input / placeholders for train (bn) and dropout
    x = tf.placeholder(tf.float32, input_shape, 'x')
    t = tf.placeholder(tf.float32, output_shape, 't')
    label = tf.placeholder(tf.int32, [None], 'label')
    phase_train = tf.placeholder(tf.bool, name='phase_train')
    keep_prob = tf.placeholder(tf.float32, name='keep_prob')
    corrupt_rec = tf.placeholder(tf.float32, name='corrupt_rec')
    corrupt_cls = tf.placeholder(tf.float32, name='corrupt_cls')

    # input of the reconstruction network
    # np.tanh(2) = 0.964
    current_input1 = utils.corrupt(x)*corrupt_rec + x*(1-corrupt_rec) \
        if (denoising and phase_train is not None) else x
    current_input1.set_shape(x.get_shape())
    # 2d -> 4d if convolution
    current_input1 = utils.to_tensor(current_input1) \
        if convolutional else current_input1

    Ws = []
    shapes = []

    # Build the encoder
    for layer_i, n_output in enumerate(n_filters):
        with tf.variable_scope('encoder/{}'.format(layer_i)):
            shapes.append(current_input1.get_shape().as_list())
            if convolutional:
                h, W = utils.conv2d(x=current_input1,
                                    n_output=n_output,
                                    k_h=filter_sizes[layer_i],
                                    k_w=filter_sizes[layer_i])
            else:
                h, W = utils.linear(x=current_input1, n_output=n_output)
            h = activation(batch_norm(h, phase_train, 'bn' + str(layer_i)))
            if dropout:
                h = tf.nn.dropout(h, keep_prob)
            Ws.append(W)
            current_input1 = h

    shapes.append(current_input1.get_shape().as_list())

    with tf.variable_scope('variational'):
        if variational:
            dims = current_input1.get_shape().as_list()
            flattened = utils.flatten(current_input1)

            if n_hidden:
                h = utils.linear(flattened, n_hidden, name='W_fc')[0]
                h = activation(batch_norm(h, phase_train, 'fc/bn'))
                if dropout:
                    h = tf.nn.dropout(h, keep_prob)
            else:
                h = flattened

            z_mu = utils.linear(h, n_code, name='mu')[0]
            z_log_sigma = 0.5 * utils.linear(h, n_code, name='log_sigma')[0]
            # modified by yidawang
            # s, u, v = tf.svd(z_log_sigma)
            # z_log_sigma = tf.matmul(
            #        tf.matmul(u, tf.diag(s)), tf.transpose(v))
            # end yidawang

            # Sample from noise distribution p(eps) ~ N(0, 1)
            epsilon = tf.random_normal(tf.stack([tf.shape(x)[0], n_code]))

            # Sample from posterior
            z = z_mu + tf.multiply(epsilon, tf.exp(z_log_sigma))

            if n_hidden:
                h = utils.linear(z, n_hidden, name='fc_t')[0]
                h = activation(batch_norm(h, phase_train, 'fc_t/bn'))
                if dropout:
                    h = tf.nn.dropout(h, keep_prob)
            else:
                h = z

            size = dims[1] * dims[2] * dims[3] if convolutional else dims[1]
            h = utils.linear(h, size, name='fc_t2')[0]
            current_input1 = activation(batch_norm(h, phase_train, 'fc_t2/bn'))
            if dropout:
                current_input1 = tf.nn.dropout(current_input1, keep_prob)

            if convolutional:
                current_input1 = tf.reshape(
                    current_input1,
                    tf.stack([
                        tf.shape(current_input1)[0], dims[1], dims[2], dims[3]
                    ]))
        else:
            z = current_input1

    shapes.reverse()
    n_filters.reverse()
    Ws.reverse()

    n_filters += [input_shape[-1]]

    # %%
    # Decoding layers
    for layer_i, n_output in enumerate(n_filters[1:]):
        with tf.variable_scope('decoder/{}'.format(layer_i)):
            shape = shapes[layer_i + 1]
            if convolutional:
                h, W = utils.deconv2d(x=current_input1,
                                      n_output_h=shape[1],
                                      n_output_w=shape[2],
                                      n_output_ch=shape[3],
                                      n_input_ch=shapes[layer_i][3],
                                      k_h=filter_sizes[layer_i],
                                      k_w=filter_sizes[layer_i])
            else:
                h, W = utils.linear(x=current_input1, n_output=n_output)
            h = activation(batch_norm(h, phase_train, 'dec/bn' + str(layer_i)))
            if dropout:
                h = tf.nn.dropout(h, keep_prob)
            current_input1 = h

    y = current_input1
    t_flat = utils.flatten(t)
    y_flat = utils.flatten(y)

    # l2 loss
    loss_x = tf.reduce_mean(
        tf.reduce_sum(tf.squared_difference(t_flat, y_flat), 1))
    loss_z = 0

    if variational:
        # Variational lower bound, kl-divergence
        loss_z = tf.reduce_mean(-0.5 * tf.reduce_sum(
            1.0 + 2.0 * z_log_sigma - tf.square(z_mu) -
            tf.exp(2.0 * z_log_sigma), 1))

        # Add l2 loss
        cost_vae = tf.reduce_mean(loss_x + loss_z)
    else:
        # Just optimize l2 loss
        cost_vae = tf.reduce_mean(loss_x)

    # Alexnet for clasification based on softmax using TensorFlow slim
    if softmax:
        axis = list(range(len(x.get_shape())))
        mean1, variance1 = tf.nn.moments(t, axis) \
            if (phase_train is True) else tf.nn.moments(x, axis)
        mean2, variance2 = tf.nn.moments(y, axis)
        var_prob = variance2 / variance1

        # Input of the classification network
        current_input2 = utils.corrupt(x)*corrupt_cls + \
            x*(1-corrupt_cls) \
            if (denoising and phase_train is True) else x
        current_input2.set_shape(x.get_shape())
        current_input2 = utils.to_tensor(current_input2) \
            if convolutional else current_input2

        y_concat = tf.concat([current_input2, y], 3)
        with tf.variable_scope('deconv/concat'):
            shape = shapes[layer_i + 1]
            if convolutional:
                # Here we set the input of classification network is
                # the twice of
                # the input of the reconstruction network
                # 112->224 for alexNet and 150->300 for inception v3 and v4
                y_concat, W = utils.deconv2d(
                    x=y_concat,
                    n_output_h=y_concat.get_shape()[1] * 2,
                    n_output_w=y_concat.get_shape()[1] * 2,
                    n_output_ch=y_concat.get_shape()[3],
                    n_input_ch=y_concat.get_shape()[3],
                    k_h=3,
                    k_w=3)
                Ws.append(W)

        # The following are optional networks for classification network
        if classifier == 'squeezenet':
            predictions, net = squeezenet.squeezenet(y_concat, num_classes=13)
        elif classifier == 'zigzagnet':
            predictions, net = squeezenet.zigzagnet(y_concat, num_classes=13)
        elif classifier == 'alexnet_v2':
            predictions, end_points = alexnet.alexnet_v2(y_concat,
                                                         num_classes=13)
        elif classifier == 'inception_v1':
            predictions, end_points = inception.inception_v1(y_concat,
                                                             num_classes=13)
        elif classifier == 'inception_v2':
            predictions, end_points = inception.inception_v2(y_concat,
                                                             num_classes=13)
        elif classifier == 'inception_v3':
            predictions, end_points = inception.inception_v3(y_concat,
                                                             num_classes=13)

        label_onehot = tf.one_hot(label, 13, axis=-1, dtype=tf.int32)
        cost_s = tf.losses.softmax_cross_entropy(label_onehot, predictions)
        cost_s = tf.reduce_mean(cost_s)
        acc = tf.nn.in_top_k(predictions, label, 1)
    else:
        predictions = tf.one_hot(label, 13, 1, 0)
        label_onehot = tf.one_hot(label, 13, 1, 0)
        cost_s = 0
        acc = 0
    # Using Summaries for Tensorboard
    tf.summary.scalar('cost_vae', cost_vae)
    tf.summary.scalar('cost_s', cost_s)
    tf.summary.scalar('loss_x', loss_x)
    tf.summary.scalar('loss_z', loss_z)
    tf.summary.scalar('corrupt_rec', corrupt_rec)
    tf.summary.scalar('corrupt_cls', corrupt_cls)
    tf.summary.scalar('var_prob', var_prob)
    merged = tf.summary.merge_all()

    return {
        'cost_vae': cost_vae,
        'cost_s': cost_s,
        'loss_x': loss_x,
        'loss_z': loss_z,
        'Ws': Ws,
        'x': x,
        't': t,
        'label': label,
        'label_onehot': label_onehot,
        'predictions': predictions,
        'z': z,
        'y': y,
        'acc': acc,
        'keep_prob': keep_prob,
        'corrupt_rec': corrupt_rec,
        'corrupt_cls': corrupt_cls,
        'var_prob': var_prob,
        'train': phase_train,
        'merged': merged
    }
Esempio n. 24
0
validation_iterator = validation_dataset.make_one_shot_iterator()

# Create a feedable iterator that use a placeholder to switch between dataset
handle = tf.placeholder(tf.string)

iterator = tf.contrib.data.Iterator.from_string_handle(
    handle, train_iterator.output_types, train_iterator.output_shapes)

image, pose_q, pose_x = iterator.get_next()

####################################################################################################
# Construct model and encapsulating all ops into scopes, making
# Tensorboard's Graph visualization more convenient

with tf.name_scope('Model'):
    py_x, _ = inception.inception_v3(image)

    py_x = tf.nn.relu(py_x)

    weights = {
        'h1': tf.Variable(tf.random_normal([1000, 4]), name='w_wpqr_out'),
        'h2': tf.Variable(tf.random_normal([1000, 3]), name='w_xyz_out'),
    }
    biases = {
        'b1': tf.Variable(tf.zeros([4]), name='b_wpqr_out'),
        'b2': tf.Variable(tf.zeros([3]), name='b_xyz_out'),
    }

    cls3_fc_pose_wpqr = tf.add(tf.matmul(py_x, weights['h1']), biases['b1'])
    cls3_fc_pose_xyz = tf.add(tf.matmul(py_x, weights['h2']), biases['b2'])
Esempio n. 25
0
def main(argv):

    # TF Record
    dataset = tf.data.TFRecordDataset(FLAGS.data_dir +
                                      '/dataset_test.tfrecords')
    dataset = dataset.map(_parse_function_kingscollege)
    # dataset = dataset.repeat()
    # dataset = dataset.shuffle(FLAGS.queue_buffer)
    dataset = dataset.batch(1)
    image, vec, pose_q, pose_x = dataset.make_one_shot_iterator().get_next()

    # Network Definition
    image_resized = tf.image.resize_images(image, size=[224, 224])

    if FLAGS.loss == 'PoseNet':

        y_pred, _ = inception.inception_v3(image_resized,
                                           num_classes=7,
                                           is_training=False)
        quaternion_pred, translation_pred = tf.split(y_pred, [4, 3], axis=1)

        sess = tf.Session()

        ckpt_file = tf.train.latest_checkpoint(FLAGS.model_dir)
        tf.train.Saver().restore(sess, ckpt_file)
        print('restoring parameters from', ckpt_file)

        i = 0

        results = []

        try:

            while True:
                _image, _quaternion_true, _translation_true, _quaternion_pred, _translation_pred = \
                    sess.run([image, pose_q, pose_x, quaternion_pred, translation_pred])

                # Compute Individual Sample Error
                q1 = _quaternion_true / np.linalg.norm(_quaternion_true)
                q2 = _quaternion_pred / np.linalg.norm(_quaternion_pred)
                d = abs(np.sum(np.multiply(q1, q2)))
                theta = 2. * np.arccos(d) * 180. / np.pi
                error_x = np.linalg.norm(_translation_true - _translation_pred)

                results.append([error_x, theta])

                print('Iteration:', i, 'Error XYZ (m):', error_x,
                      'Error Q (degrees):', theta)
                i = i + 1

        except tf.errors.OutOfRangeError:
            print('End of Test Data')

        results = np.stack(results)
        results = np.median(results, axis=0)
        print('Error XYZ (m):', results[0], 'Error Q (degrees):', results[1])

    elif FLAGS.loss == 'SE3':

        y_pred, _ = inception.inception_v3(image_resized,
                                           num_classes=6,
                                           is_training=False)

        sess = tf.Session()

        ckpt_file = tf.train.latest_checkpoint(FLAGS.model_dir)
        tf.train.Saver().restore(sess, ckpt_file)
        print('restoring parameters from', ckpt_file)

        SO3_GROUP = SpecialOrthogonalGroup(3)
        SE3_GROUP = SpecialEuclideanGroup(3)
        metric = InvariantMetric(group=SE3_GROUP,
                                 inner_product_mat_at_identity=np.eye(6),
                                 left_or_right='left')

        i = 0

        results = []
        _y_pred_i = []
        _y_true_i = []
        _se3_err_i = []

        try:

            while True:
                _image, _rvec, _qvec, _tvec, _y_pred = \
                    sess.run([image, vec, pose_q, pose_x, y_pred])

                _quaternion_true = _qvec
                _quaternion_pred = SO3_GROUP.quaternion_from_rotation_vector(
                    _y_pred[0, :3])[0]

                # Compute Individual Sample Error
                q1 = _quaternion_true / np.linalg.norm(_quaternion_true)
                q2 = _quaternion_pred / np.linalg.norm(_quaternion_pred)
                d = abs(np.sum(np.multiply(q1, q2)))
                theta = 2. * np.arccos(d) * 180. / np.pi
                error_x = np.linalg.norm(_tvec - _y_pred[0, 3:])
                results.append([error_x, theta])

                # SE3 compute
                _y_true = np.concatenate((_rvec, _tvec), axis=-1)
                se3_dist = metric.squared_dist(_y_pred, _y_true)[0]

                _y_pred_i.append(_y_pred)
                _y_true_i.append(_y_true)
                _se3_err_i.append(
                    SE3_GROUP.compose(SE3_GROUP.inverse(_y_true), _y_pred))

                print('Iteration:', i, 'Error XYZ (m):', error_x,
                      'Error Q (degrees):', theta, 'SE3 dist:', se3_dist)
                i = i + 1

        except tf.errors.OutOfRangeError:
            print('End of Test Data')

        # Calculate SE3 Error Weights
        err_vec = np.vstack(_se3_err_i)
        err_weights = np.diag(np.linalg.inv(np.cov(err_vec.T)))
        err_weights = err_weights / np.linalg.norm(err_weights)
        print(err_weights)
        results = np.stack(results)
        results = np.median(results, axis=0)
        print('Error XYZ (m):', results[0], 'Error Q (degrees):', results[1])

    else:
        print('Invalid Option:', FLAGS.loss)
        raise SystemExit
Esempio n. 26
0
# Variables

W1 = tf.Variable(tf.Variable(tf.random_normal([2048, hidden_layer])),name="W1")
b1 = tf.Variable(tf.Variable(tf.random_normal([hidden_layer])),name='b1')
W2 = tf.Variable(tf.Variable(tf.random_normal([hidden_layer,hidden_layer2])),name="W2")
b2 = tf.Variable(tf.Variable(tf.random_normal([hidden_layer2])),name='b2')
W3 = tf.Variable(tf.Variable(tf.random_normal([hidden_layer2,1])),name="W3")
b3 = tf.Variable(tf.Variable(tf.random_normal([1])),name='b3')

#pdb.set_trace()
slim = tf.contrib.slim

# Inception model
with slim.arg_scope(inception.inception_v3_arg_scope()):
    _, end_points = inception.inception_v3(x, num_classes=1001, is_training=False)

# Selecting top n number of frames
output_feature = tf.squeeze(end_points['PreLogits'],[1,2])
#feature vector shape=(batch_size,num of frames,feature vector size)
# bb = tf.split(output_feature, ind)
bb = tf.split(input_feature, ind)
out = []
for i in bb:
    aa = calculate_feature_score(i,W1,b1,W2,b2,W3,b3)  #importance score shape=(batch_size,num of frames)
    sorted_a, indices = tf.nn.top_k(tf.transpose(aa), n_input)
    indices = tf.transpose(tf.transpose(indices)[::-1])
    #shape_a = tf.shape(aa)
    #auxiliary_indices = tf.meshgrid(*[tf.range(d) for d in (tf.unstack(shape_a[:(aa.get_shape().ndims - 1)]) + [n_input])], indexing='ij')
    #sorted_b = tf.gather_nd(bb, tf.stack(auxiliary_indices[:-1] + [indices], axis=-1))# selected k number of important feature vector
    sorted_b = tf.gather(i,tf.squeeze(indices,0))[::-1]