Skip to content
This repository has been archived by the owner on Apr 10, 2024. It is now read-only.

Commit

Permalink
Improved assembling of suggest_save_args report
Browse files Browse the repository at this point in the history
Also runs slow tests for coverage again.
  • Loading branch information
Ludwig Schubert committed Apr 15, 2019
1 parent 98cb953 commit 523d4dd
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 206 deletions.
52 changes: 20 additions & 32 deletions lucid/modelzoo/vision_base.py
Expand Up @@ -204,14 +204,11 @@ def get_layer(self, name):

@staticmethod
def suggest_save_args(graph_def=None):
# TODO: Check with uint8 placeholders
if graph_def is None:
graph_def = tf.get_default_graph().as_graph_def()

inferred_info = {
"input_name" : None,
"image_shape" : None,
"output_names": None,
}
inferred_info = dict.fromkeys(("input_name", "image_shape", "output_names", "image_value_range"))

nodes_of_op = lambda s: [n.name for n in graph_def.node if n.op == s]
node_by_name = lambda s: [n for n in graph_def.node if n.name == s][0]
Expand Down Expand Up @@ -245,34 +242,25 @@ def suggest_save_args(graph_def=None):
else:
warnings.warn("Could not infer output_names.")

if all(inferred_info.values()):
return inferred_info
else:
raise AttributeError("Could not infer all arguments for saving.")


# print("")
# print("# Sanity check all inferred values before using this code!")
# print("save_model(")
# print(" save_path = 'gs://save/model.pb', # TODO: replace")
report = []
report.append("# Please sanity check all inferred values before using this code!")
report.append("Model.save(")

# if inferred_info["input_name"] is not None:
# print(" input_name = %s," % repr(inferred_info["input_name"]))
# else:
# print(" input_name = , # TODO (eg. 'input' )")

# if inferred_info["output_names"] is not None:
# print(" output_names = %s," % repr(inferred_info["output_names"]) )
# else:
# print(" output_names = [ ], # TODO (eg. ['logits'] )")

# if inferred_info["image_shape"] is not None:
# print(" image_shape = %s,"% repr(inferred_info["image_shape"]) )
# else:
# print(" image_shape = , # TODO (eg. [224, 224, 3] )")

# print(" image_value_range = # TODO (eg. [0, 1], [0, 255], [-117, 138] )")
# print(" )")
suggestions = {
"input_name" : 'input',
"image_shape" : [224, 224, 3],
"output_names": ['logits'],
"image_value_range": "[-1, 1], [0, 1], [0, 255], or [-117, 138]"
}
for key, value in inferred_info.items():
if value is not None:
report.append(" {}={!r},".format(key, value))
else:
report.append(" {}=_, # TODO (eg. {!r})".format(key, suggestions[key]))
report.append(" )")

print("\n".join(report))
return inferred_info


@staticmethod
Expand Down
158 changes: 0 additions & 158 deletions lucid/scratch/export/export_with_meta.py

This file was deleted.

14 changes: 14 additions & 0 deletions tests/conftest.py
@@ -1,4 +1,18 @@
import pytest
import tensorflow as tf


@pytest.fixture
def minimodel():
def inner(input=None, shape=(16,16,3)):
"""Constructs a tiny graph containing one each of a typical input
(tf.placegholder), variable and typical output (softmax) nodes."""
if input is None:
input = tf.placeholder(tf.float32, shape=shape, name="input")
w = tf.Variable(0.1, name="variable")
logits = tf.reduce_mean(w*input, name="output", axis=(0,1))
return tf.nn.softmax(logits)
return inner

# Add support for a slow tests marker:

Expand Down
7 changes: 2 additions & 5 deletions tests/modelzoo/test_saveload.py
Expand Up @@ -7,12 +7,9 @@

shape = (16,16,3)

def test_Model_save():
def test_Model_save(minimodel):
with tf.Session().as_default() as sess:
x = tf.placeholder(tf.float32, shape=shape, name="input")
w = tf.Variable(0.1, name="variable")
logits = tf.reduce_mean(w*x, axis=(0,1))
y = tf.nn.softmax(logits, name="output")
_ = minimodel()
sess.run(tf.global_variables_initializer())
path = "./tests/fixtures/minigraph.pb"
Model.save(path, "input", ["output"], shape, [0,1])
Expand Down
16 changes: 6 additions & 10 deletions tests/modelzoo/test_vision_base.py
Expand Up @@ -2,30 +2,26 @@

from lucid.modelzoo.vision_base import Model

shape = [16,16,3]

def test_suggest_save_args(capsys):
def test_suggest_save_args(capsys, minimodel):
path = "./tests/fixtures/minigraph.pb"

with tf.Graph().as_default() as graph, tf.Session() as sess:
x = tf.placeholder(tf.float32, shape=shape, name="input")
w = tf.Variable(0.1, name="variable")
logits = tf.reduce_mean(w*x, name="output", axis=(0,1))
y = tf.nn.softmax(logits)
_ = minimodel()
sess.run(tf.global_variables_initializer())

# ask for suggested arguments
inferred = Model.suggest_save_args()

# they should be both printed...
captured = capsys.readouterr().out # captures stdout
names = ["input_name", "image_shape", "output_names"]
assert all(name in captured for name in names)
#...and returned
assert all(inferred.values())

# check that these inferred values work
Model.save(path, image_value_range=(0,1), **inferred)
inferred.update(image_value_range=(0,1))
Model.save(path, **inferred)
loaded_model = Model.load(path)
assert str(shape) in repr(loaded_model.graph_def)
assert "0.100" in repr(loaded_model.graph_def)


2 changes: 1 addition & 1 deletion tox.ini
Expand Up @@ -5,7 +5,7 @@ envlist = py{27,36}
deps =
tensorflow
.[test]
commands = coverage run --source lucid --omit lucid/scratch/*,lucid/recipes/*,lucid/misc/gl/* -m py.test {posargs}
commands = coverage run --source lucid --omit lucid/scratch/*,lucid/recipes/*,lucid/misc/gl/* -m py.test --run-slow {posargs}

[pytest]
addopts = --verbose
Expand Down

0 comments on commit 523d4dd

Please sign in to comment.