Issues in order to adapt label_image.py
Issues in order to adapt label_image.py
I'd like to modify label_image.py, my main goal is to create a GUI.
Some words about my ideas
I'm trying to modify some stuffs in the code (https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/label_image/label_image.py), but it doesn't work.
I first tried to had a filename = askopenfilename()
line, which it seems to work.
filename = askopenfilename()
Then I don't know how I can adapt the code.
For example, in order to load MY graph, which line have I to adapt?
Maybe line 26 : def load_graph(model_file) :
and putting def load_graph("c:my_path_hereowngraph.pb):
? It doesn't work either.
def load_graph(model_file) :
def load_graph("c:my_path_hereowngraph.pb):
I have the same question for label, and also for the choosen picture:
filename = askopenfilename()
--> where can I load MY picture (I tried to replace all file_name
by filename
but it fails).
filename = askopenfilename()
file_name
filename
You will find here my last tests : I tried to change code frome line 99 (cf. code in github)
@Andreas Storvik Strauman You will find here my last test ; I tried to change the arg (yes or not) test :
# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from tkinter import Tk
from tkinter.filedialog import askopenfilename
Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
filename = askopenfilename() # show an "Open" dialog box and return the path to the selected file
import argparse
import numpy as np
import tensorflow as tf
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(filename,
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 filename.endswith(".png"):
image_reader = tf.image.decode_png(
file_reader, channels=3, name="png_reader")
elif filename.endswith(".gif"):
image_reader = tf.squeeze(
tf.image.decode_gif(file_reader, name="gif_reader"))
elif filename.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__":
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 = "mul"
output_layer = "final_result"
parser = argparse.ArgumentParser()
parser.add_argument("--image", help="image to be processed")
parser.add_argument("--graph", help="graph/model to be executed")
parser.add_argument("--labels", help="name of file containing labels")
parser.add_argument("--input_height", type=int, help="input height")
parser.add_argument("--input_width", type=int, help="input width")
parser.add_argument("--input_mean", type=int, help="input mean")
parser.add_argument("--input_std", type=int, help="input std")
parser.add_argument("--input_layer", help="name of input layer")
parser.add_argument("--output_layer", help="name of output layer")
args = parser.parse_args()
model_file = c:my_pathgaz_graph2.pb
file_name = filename #I get it with filename = askopenfilename()
if args.labels:
label_file = c:my_pathgaz_labels.txt
if args.input_height:
input_height = args.input_height
if args.input_width:
input_width = args.input_width
if args.input_mean:
input_mean = args.input_mean
if args.input_std:
input_std = args.input_std
if args.input_layer:
input_layer = args.input_layer
if args.output_layer:
output_layer = args.output_layer
graph = load_graph(model_file)
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
})
results = np.squeeze(results)
top_k = results.argsort()[-5:][::-1]
labels = load_labels(label_file)
for i in top_k:
print(labels[i], results[i])
Also, assuming you're not very familiar with tensorflow, I don't think manipulating tensorflow source code is a good place to start exploring tensorflow.
– Andreas Storvik Strauman
Jun 30 at 19:25
Thanks. I used some models and it works...but I'm still a beginner.
– Pierre ROY
Jun 30 at 19:42
I also tried a GUI with an action button linked to label_image and I don't manage to make it work. I imported label_image by using : from label_image import *
– Pierre ROY
Jun 30 at 19:43
@Andreas Storvik Strauman is my post MCVE now ?
– Pierre ROY
Jul 1 at 14:31
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Hello and welcome to Stack Overflow! When you ask questions here, it's really helpful (and would probably get you an answer faster) if you provide a minimal, complete and verifiable example (MCVE): just a starting point for the people here that want to help you. This way we can just copy/paste the code and go straight into solving the problem without resorting to wild guesses about what you really are asking.
– Andreas Storvik Strauman
Jun 30 at 19:24