Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code upgrade for TensorFlow 2 #138

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Overview
#"TensorFlow for poets 2" for TensorFlow 2.0

This repo contains code for the "TensorFlow for poets 2" series of codelabs.

Expand Down
2 changes: 1 addition & 1 deletion scripts/count_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
def load_graph(file_name):
with open(file_name,'rb') as f:
content = f.read()
graph_def = tf.GraphDef()
graph_def = tf.compat.v1.GraphDef()
graph_def.ParseFromString(content)
with tf.Graph().as_default() as graph:
tf.import_graph_def(graph_def, name='')
Expand Down
8 changes: 4 additions & 4 deletions scripts/evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@

def evaluate_graph(graph_file_name):
with load_graph(graph_file_name).as_default() as graph:
ground_truth_input = tf.placeholder(
ground_truth_input = tf.compat.v1.placeholder(
tf.float32, [None, 5], name='GroundTruthInput')

image_buffer_input = graph.get_tensor_by_name('input:0')
final_tensor = graph.get_tensor_by_name('final_result:0')
accuracy, _ = retrain.add_evaluation_step(final_tensor, ground_truth_input)

logits = graph.get_tensor_by_name("final_training_ops/Wx_plus_b/add:0")
xent = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
labels = ground_truth_input,
xent = tf.reduce_mean(input_tensor=tf.nn.softmax_cross_entropy_with_logits(
labels = tf.stop_gradient( ground_truth_input),
logits = logits))

image_dir = 'tf_files/flower_photos'
Expand Down Expand Up @@ -68,7 +68,7 @@ def evaluate_graph(graph_file_name):

accuracies = []
xents = []
with tf.Session(graph=graph) as sess:
with tf.compat.v1.Session(graph=graph) as sess:
for filename, ground_truth in zip(filenames, ground_truths):
image = Image.open(filename).resize((224,224),Image.ANTIALIAS)
image = np.array(image, dtype=np.float32)[None,...]
Expand Down
6 changes: 3 additions & 3 deletions scripts/graph_pb2tb.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@
def load_graph(graph_pb_path):
with open(graph_pb_path,'rb') as f:
content = f.read()
graph_def = tf.GraphDef()
graph_def = tf.compat.v1.GraphDef()
graph_def.ParseFromString(content)
with tf.Graph().as_default() as graph:
tf.import_graph_def(graph_def, name='')
return graph


def graph_to_tensorboard(graph, out_dir):
with tf.Session():
train_writer = tf.summary.FileWriter(out_dir)
with tf.compat.v1.Session():
train_writer = tf.compat.v1.summary.FileWriter(out_dir)
train_writer.add_graph(graph)


Expand Down
19 changes: 10 additions & 9 deletions scripts/label_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@

import numpy as np
import tensorflow as tf
tf.compat.v1.disable_eager_execution()

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

with open(model_file, "rb") as f:
graph_def.ParseFromString(f.read())
Expand All @@ -39,7 +40,7 @@ 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)
file_reader = tf.io.read_file(file_name, input_name)
if file_name.endswith(".png"):
image_reader = tf.image.decode_png(file_reader, channels = 3,
name='png_reader')
Expand All @@ -53,16 +54,16 @@ def read_tensor_from_image_file(file_name, input_height=299, input_width=299,
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])
resized = tf.image.resize(dims_expander, [input_height, input_width], method=tf.image.ResizeMethod.BILINEAR)
normalized = tf.divide(tf.subtract(resized, [input_mean]), [input_std])
sess = tf.Session()
sess = tf.compat.v1.Session()
result = sess.run(normalized)

return result

def load_labels(label_file):
label = []
proto_as_ascii_lines = tf.gfile.GFile(label_file).readlines()
proto_as_ascii_lines = tf.io.gfile.GFile(label_file).readlines()
for l in proto_as_ascii_lines:
label.append(l.rstrip())
return label
Expand All @@ -71,11 +72,11 @@ def load_labels(label_file):
file_name = "tf_files/flower_photos/daisy/3475870145_685a19116d.jpg"
model_file = "tf_files/retrained_graph.pb"
label_file = "tf_files/retrained_labels.txt"
input_height = 224
input_width = 224
input_height = 299
input_width = 299
input_mean = 128
input_std = 128
input_layer = "input"
input_layer = "Mul"
output_layer = "final_result"

parser = argparse.ArgumentParser()
Expand Down Expand Up @@ -121,7 +122,7 @@ def load_labels(label_file):
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:
with tf.compat.v1.Session(graph=graph) as sess:
start = time.time()
results = sess.run(output_operation.outputs[0],
{input_operation.outputs[0]: t})
Expand Down
9 changes: 5 additions & 4 deletions scripts/quantize_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import collections
import re
import numpy as np
import tensorflow as tf

from tensorflow.core.framework import attr_value_pb2
from tensorflow.core.framework import graph_pb2
Expand Down Expand Up @@ -584,7 +585,7 @@ def eightbitize_nodes_recursively(self, current_node):
quantize_input = False
if current_node.op in ("MatMul", "Conv2D", "BiasAdd", "MaxPool",
"AvgPool", "Relu", "Relu6",
"BatchNormWithGlobalNormalization"):
tf.nn.batch_normalization()):
quantize_input = True
elif current_node.op == "Concat" and i > 0:
quantize_input = (
Expand Down Expand Up @@ -616,7 +617,7 @@ def eightbitize_nodes_recursively(self, current_node):
elif (current_node.op == "Concat" and
dtypes.as_dtype(current_node.attr["T"].type) == dtypes.float32):
self.eightbitize_concat_node(current_node)
elif current_node.op == "BatchNormWithGlobalNormalization":
elif current_node.op == tf.nn.batch_normalization():
self.eightbitize_batch_norm_node(current_node)
elif (current_node.op == "Reshape" and
dtypes.as_dtype(current_node.attr["T"].type) == dtypes.float32):
Expand Down Expand Up @@ -1044,7 +1045,7 @@ def eightbitize_batch_norm_node(self, original_node):
self.eightbitize_input_to_node(namespace_prefix, original_gamma_name,
reshape_dims_name, reduction_dims_name))
quantized_batch_norm_node = create_node(
"QuantizedBatchNormWithGlobalNormalization", quantized_batch_norm_name,
tf.nn.batch_normalization(), quantized_batch_norm_name,
[
quantize_input_name, min_input_name, max_input_name,
quantize_mean_name, min_mean_name, max_mean_name,
Expand Down Expand Up @@ -1187,7 +1188,7 @@ def apply_final_node_renames(self):
def remove_dead_nodes(self, output_names):
"""Removes nodes that are no longer needed for inference from the graph."""
old_output_graph = self.output_graph
self.output_graph = graph_util.extract_sub_graph(old_output_graph,
self.output_graph = tf.compat.v1.graph_util.extract_sub_graph(old_output_graph,
output_names)

def quantize_weights(self, input_graph, quantization_mode):
Expand Down
Loading