Commit a464e8ba authored by kiegeland's avatar kiegeland
Browse files

added label_image_modified

parent 907f8094
Loading
Loading
Loading
Loading
+139 −0
Original line number Diff line number Diff line
# Copyright 2018 Oh Caption my Caption. All Rights Reserved.
# Marinco Holzinger, Samuel Kiegeland 
# ==============================================================================

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import argparse

import numpy as np
import tensorflow as tf

from pycocotools.coco import COCO
import numpy as np
import skimage.io as io
import matplotlib.pyplot as plt
import pylab
import pickle


def load_graph(model_file):
  graph = tf.Graph()
  graph_def = tf.GraphDef()

  with open(model_file, "rb") as f:
    graph_def.ParseFromString(f.read())
  with graph.as_default():
    tf.import_graph_def(graph_def)

  return graph


def read_tensor_from_image_file(file_name,
                                input_height=299,
                                input_width=299,
                                input_mean=0,
                                input_std=255):
  input_name = "file_reader"
  output_name = "normalized"
  file_reader = tf.read_file(file_name, input_name)
  if file_name.endswith(".png"):
    image_reader = tf.image.decode_png(
        file_reader, channels=3, name="png_reader")
  elif file_name.endswith(".gif"):
    image_reader = tf.squeeze(
        tf.image.decode_gif(file_reader, name="gif_reader"))
  elif file_name.endswith(".bmp"):
    image_reader = tf.image.decode_bmp(file_reader, name="bmp_reader")
  else:
    image_reader = tf.image.decode_jpeg(
        file_reader, channels=3, name="jpeg_reader")
  float_caster = tf.cast(image_reader, tf.float32)
  dims_expander = tf.expand_dims(float_caster, 0)
  resized = tf.image.resize_bilinear(dims_expander, [input_height, input_width])
  normalized = tf.divide(tf.subtract(resized, [input_mean]), [input_std])
  sess = tf.Session()
  result = sess.run(normalized)

  return result


def load_labels(label_file):
  label = []
  proto_as_ascii_lines = tf.gfile.GFile(label_file).readlines()
  for l in proto_as_ascii_lines:
    label.append(l.rstrip())
  return label


if __name__ == "__main__":
  # coco stuff
  path = '/softpro/ss18/caption/cocoapi/images/train/'
  coco = COCO('/softpro/ss18/caption/cocoapi/annotations/train/instances_train2017.json')
  coco_caps = COCO('/softpro/ss18/caption/cocoapi/annotations/train/captions_train2017.json')
  cats = coco.loadCats(coco.getCatIds())
  category_names = [cat['name'] for cat in cats]

  # tensorflow stuff
  file_name = "tensorflow/examples/label_image/data/grace_hopper.jpg"
  model_file = \
    "tensorflow/examples/label_image/data/inception_v3_2016_08_28_frozen.pb"
  label_file = "tensorflow/examples/label_image/data/imagenet_slim_labels.txt"
  input_height = 299
  input_width = 299
  input_mean = 0
  input_std = 255
  input_layer = "input"
  output_layer = "InceptionV3/Predictions/Reshape_1"


  label_file = 'output_labels.txt'
  model_file = 'output_graph.pb'
  input_layer = 'Placeholder'
  output_layer= 'module_apply_default/hub_output/feature_vector/SpatialSqueeze'

  graph = load_graph(model_file)

  interface = []

  for category in category_names:
  	counter = 0
    print(category)
    catIds = coco.getCatIds(catNms=[category])
    imgIds = coco.getImgIds(catIds=catIds)
    for pic in imgIds:
      counter += 1
      if counter % 1000 == 0: 
        print(counter, " pictures finished, wooo!!")	
      img = coco.loadImgs(pic)[0]
      annIds = coco_caps.getAnnIds(imgIds=img['id'])
      anns = coco_caps.loadAnns(annIds)
      file_name = path + img['file_name']
      img['captions'] = anns
  
      t = read_tensor_from_image_file(
        file_name,
        input_height=input_height,
        input_width=input_width,
        input_mean=input_mean,
        input_std=input_std)

      input_name = "import/" + input_layer
      output_name = "import/" + output_layer
      input_operation = graph.get_operation_by_name(input_name)
      output_operation = graph.get_operation_by_name(output_name)

      with tf.Session(graph=graph) as sess:
        results = sess.run(output_operation.outputs[0], {
            input_operation.outputs[0]: t
        })

      #picture_dict[vector] = np.squeeze(results)
      img['vector'] = np.squeeze(results)
      interface.append(img)


  with open('/softpro/ss18/caption/caption-lib/lstm/Input_Data/interface_train.pickle', 'wb') as handle:
    pickle.dump(interface, handle, protocol=pickle.HIGHEST_PROTOCOL)