Commit 5eda2136 authored by holzinger's avatar holzinger
Browse files

adding part of the demo and requirements

parent c1371d2d
Loading
Loading
Loading
Loading

Demo/CNN.ipynb

0 → 100644
+266 −0
Original line number Diff line number Diff line
%% Cell type:code id: tags:

``` python
# !git clone https://github.com/cocodataset/cocoapi.git
# !python cocoapi/PythonAPI/setup.py install
# !pip install -r requirements.txt

from __future__ import print_function
from ipywidgets import interact, interactive, fixed, interact_manual
import ipywidgets as widgets
import os.path
```

%% Cell type:code id: tags:

``` python
%matplotlib inline
import pickle
from pycocotools.coco import COCO
import skimage.io as io
import matplotlib.pyplot as plt
```

%% Cell type:code id: tags:

``` python
coco = COCO('/softpro/ss18/caption/cocoapi/annotations/val/instances_val2017.json')
coco_caps = COCO('/softpro/ss18/caption/cocoapi/annotations/val/captions_val2017.json')
imgIds = coco.getImgIds()
```

%% Output

    loading annotations into memory...
    Done (t=0.76s)
    creating index...
    index created!
    loading annotations into memory...
    Done (t=0.05s)
    creating index...
    index created!

%% Cell type:code id: tags:

``` python
from enum import Enum
class Pictures(Enum):
    bear = 205776
    cat = 89271
    child = 235836
    rockstar = 297353
    random = imgIds[np.random.randint(0, len(imgIds))]
```

%% Cell type:code id: tags:

``` python
images = [Pictures.bear, Pictures.cat, Pictures.child, Pictures.rockstar, Pictures.random]
```

%% Cell type:code id: tags:

``` python
from IPython.display import display
```

%% Cell type:code id: tags:

``` python
def browse_images(images):
    def view_image(i):
        img = coco.loadImgs(i.value)[0]
        annIds = coco_caps.getAnnIds(imgIds=img['id'])
        anns = coco_caps.loadAnns(annIds)
        I = io.imread(img['coco_url'])
        coco_caps.showAnns(anns)
        plt.title('Title: %s' % i.name)
        plt.imshow(I)
        plt.axis('off')
        plt.show()
    view_image(images)
```

%% Cell type:code id: tags:

``` python
w = interactive(browse_images, images=images)
```

%% Cell type:code id: tags:

``` python
w
```

%% Output


%% Cell type:code id: tags:

``` python
img = coco.loadImgs(w.kwargs['images'].value)[0]
annIds = coco_caps.getAnnIds(imgIds=img['id'])
anns = coco_caps.loadAnns(annIds)
img['captions'] = anns
print(img)
```

%% Output

    {'license': 6, 'file_name': '000000089271.jpg', 'coco_url': 'http://images.cocodataset.org/val2017/000000089271.jpg', 'height': 524, 'width': 640, 'date_captured': '2013-11-17 03:19:00', 'flickr_url': 'http://farm5.staticflickr.com/4122/4873790839_1f8aa7d6b2_z.jpg', 'id': 89271, 'captions': [{'image_id': 89271, 'id': 715892, 'caption': "A cat wearing a hat while resting it's paws on top of a chair."}, {'image_id': 89271, 'id': 723659, 'caption': 'Cat wearing a baseball cap with ears sticking out. '}, {'image_id': 89271, 'id': 723749, 'caption': 'Tabby cat with green eyes wearing a hat'}, {'image_id': 89271, 'id': 724229, 'caption': 'A cat peeks over a chair while wearing a hat'}, {'image_id': 89271, 'id': 730640, 'caption': 'A cat is wearing an orange and brown hat.'}]}

%% Cell type:code id: tags:

``` python
import numpy as np
import tensorflow as tf
```

%% Cell type:code id: tags:

``` python
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
```

%% Cell type:code id: tags:

``` python
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
```

%% Cell type:code id: tags:

``` python
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
```

%% Cell type:code id: tags:

``` python
path = '/softpro/ss18/caption/cnn_exercise/good_models/fourth_model_all_objects_different_hyperparameters/'
file_name = '/softpro/ss18/caption/cocoapi/val/' + img['file_name']
label_file = path + 'output_labels.txt'
model_file = path + 'output_graph.pb'
input_layer = 'Placeholder'
output_layer= 'module_apply_default/hub_output/feature_vector/SpatialSqueeze'

input_name = "import/" + input_layer
output_name = "import/" + output_layer

graph = load_graph(model_file)
input_operation = graph.get_operation_by_name(input_name)
output_operation = graph.get_operation_by_name(output_name)
print(file_name)
```

%% Output

    /softpro/ss18/caption/cocoapi/val/000000089271.jpg

%% Cell type:code id: tags:

``` python
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)
with tf.Session(graph=graph) as sess:
        results = sess.run(output_operation.outputs[0], {
            input_operation.outputs[0]: t
        })
img['vector'] = np.squeeze(results)
```

%% Cell type:code id: tags:

``` python
import pprint
pprint.pprint(img, indent=4)
```

%% Output

    {   'captions': [   {   'caption': "A cat wearing a hat while resting it's "
                                       'paws on top of a chair.',
                            'id': 715892,
                            'image_id': 89271},
                        {   'caption': 'Cat wearing a baseball cap with ears '
                                       'sticking out. ',
                            'id': 723659,
                            'image_id': 89271},
                        {   'caption': 'Tabby cat with green eyes wearing a hat',
                            'id': 723749,
                            'image_id': 89271},
                        {   'caption': 'A cat peeks over a chair while wearing a '
                                       'hat',
                            'id': 724229,
                            'image_id': 89271},
                        {   'caption': 'A cat is wearing an orange and brown hat.',
                            'id': 730640,
                            'image_id': 89271}],
        'coco_url': 'http://images.cocodataset.org/val2017/000000089271.jpg',
        'date_captured': '2013-11-17 03:19:00',
        'file_name': '000000089271.jpg',
        'flickr_url': 'http://farm5.staticflickr.com/4122/4873790839_1f8aa7d6b2_z.jpg',
        'height': 524,
        'id': 89271,
        'license': 6,
        'vector': array([0.2896427 , 0.24722002, 0.06824438, ..., 0.66454995, 0.46067858,
           0.28921682], dtype=float32),
        'width': 640}

%% Cell type:code id: tags:

``` python
with open('example.pickle', 'wb') as handle:
    pickle.dump(img, handle, protocol=pickle.HIGHEST_PROTOCOL)
```

%% Cell type:code id: tags:

``` python
```

requirements.txt

0 → 100644
+4 −0
Original line number Diff line number Diff line
tensorflow==1.8.0
pycocotools==2.0
scikit-image==0.13.1
matplotlib==2.1.1