-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodeLookup.py
130 lines (98 loc) · 3.82 KB
/
nodeLookup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
from __future__ import absolute_import, division, print_function
import os.path
import re
import sys
import tarfile
import glob
import json
import psutil
from collections import defaultdict
import numpy as np
from six.moves import urllib
import tensorflow as tf
FLAGS = tf.compat.v1.app.flags.FLAGS
# classify_image_graph_def.pb:
# Binary representation of the GraphDef protocol buffer.
# imagenet_synset_to_human_label_map.txt:
# Map from synset ID to a human readable string.
# imagenet_2012_challenge_label_map_proto.pbtxt:
# Text representation of a protocol buffer mapping a label to synset ID.
class NodeLookup():
"""Converts integer node ID's to human readable labels."""
label_lookup_path=""
uid_lookup_path=""
node_lookup = None
graph_def = ""
def __init__(self,
label_lookup_path=None,
uid_lookup_path=None):
#print("juhuu " + FLAGS.model_dir)
if not label_lookup_path:
self.label_lookup_path = os.path.join(
FLAGS.model_dir, 'imagenet_2012_challenge_label_map_proto.pbtxt')
if not uid_lookup_path:
self.uid_lookup_path = os.path.join(
FLAGS.model_dir, 'imagenet_synset_to_human_label_map.txt')
self.node_lookup = self.load()
def load(self):
"""Loads a human readable English name for each softmax node.
Args:
label_lookup_path: string UID to integer node ID.
uid_lookup_path: string UID to human-readable string.
Returns:
dict from integer node ID to human-readable string.
"""
#print("juhuu4 " + self.uid_lookup_path + " " + self.label_lookup_path)
if not tf.gfile.Exists(self.uid_lookup_path):
print("uid_lookup_path not exist")
tf.logging.fatal('File does not exist %s', self.uid_lookup_path)
if not tf.gfile.Exists(self.label_lookup_path):
print("label_lookup_path not exist")
tf.logging.fatal('File does not exist %s', self.label_lookup_path)
#print("juhuu4 " + self.uid_lookup_path)
# Loads mapping from string UID to human-readable string
proto_as_ascii_lines = tf.gfile.GFile(self.uid_lookup_path).readlines()
uid_to_human = {}
p = re.compile(r'[n\d]*[ \S,]*')
for line in proto_as_ascii_lines:
parsed_items = p.findall(line)
uid = parsed_items[0]
human_string = parsed_items[2]
uid_to_human[uid] = human_string
#print(uid + " ::: " + uid_to_human[uid] + " ::: " + human_string)
# Loads mapping from string UID to integer node ID.
node_id_to_uid = {}
proto_as_ascii = tf.gfile.GFile(self.label_lookup_path).readlines()
for line in proto_as_ascii:
if line.startswith(' target_class:'):
target_class = int(line.split(': ')[1])
if line.startswith(' target_class_string:'):
target_class_string = line.split(': ')[1]
node_id_to_uid[target_class] = target_class_string[1:-2]
#print(str(target_class) + " " + node_id_to_uid[target_class])
#print ("Length : %d" % len(node_id_to_uid))
# Loads the final mapping of integer node ID to human-readable string
#for i in node_id_to_uid:
# print("aaaa "+ str(i) + node_id_to_uid[i] )
node_id_to_name = {}
#print("juhuu4" )
for i in node_id_to_uid:
key=i
val=node_id_to_uid[i]
#print(val)
#print(uid_to_human[val])
if val not in uid_to_human:
print("feil under oppslag")
tf.logging.fatal('Failed to locate: %s', val)
#print("juhuu5")
#print(uid_to_human[val])
name = uid_to_human[val]
#print(name)
node_id_to_name[key] = name
#for i in node_id_to_name:
#print("aaaa "+ str(i) + node_id_to_name[i] )
return node_id_to_name
def id_to_string(self, node_id):
if node_id not in self.node_lookup:
return ''
return self.node_lookup[node_id]