最近我一直在玩用 Keras,TF 编写的 CNN。不幸的是,我在那里遇到了一个问题:
在我强烈推荐的这些精彩的教程(https://github.com/Hvass-Labs/TensorFlow-Tutorials/blob/master/vgg16.py;其余代码在这里)中,maestro 加载预训练的 vgg16.tfmodel以非常非常丑陋的方式。
def __init__(self):
# Now load the model from file. The way TensorFlow
# does this is confusing and requires several steps.
# Create a new TensorFlow computational graph.
self.graph = tf.Graph()
# Set the new graph as the default.
with self.graph.as_default():
# TensorFlow graphs are saved to disk as so-called Protocol Buffers
# aka. proto-bufs which is a file-format that works on multiple
# platforms. In this case it is saved as a binary file.
# Open the graph-def file for binary reading.
path = os.path.join(data_dir, path_graph_def)
with tf.gfile.FastGFile(path, 'rb') as file:
# The graph-def is a saved copy of a TensorFlow graph.
# First we need to create an empty graph-def.
graph_def = tf.GraphDef()
# Then we load the proto-buf file into the graph-def.
graph_def.ParseFromString(file.read())
# Finally we import the graph-def to the default TensorFlow graph.
tf.import_graph_def(graph_def, name='')
# Now self.graph holds the VGG16 model from the proto-buf file.
# Get a reference to the tensor for inputting images to the graph.
self.input = self.graph.get_tensor_by_name(self.tensor_name_input_image)
# Get references to the tensors for the commonly used layers.
self.layer_tensors = [self.graph.get_tensor_by_name(name + ":0") for name in self.layer_names]
问题是 - 我希望我自己的预训练模型以相同/相似的方式加载,所以我可以将模型放入我稍后调用的类的图中,如果可能的话,让代码的最后几行在这里工作.(意思是从图中获取想要的层的张量。)
我所有的尝试都基于从 keras 和 comp 导入的load_model 。图让我失望了。此外,我不想以完全不同的方式加载它,因为之后我将不得不更改很多代码——对于新手来说是个大问题。
好的,我希望这个问题能传达给合适的人,并且对你来说这不是太琐碎:D。
顺便说一句:我正在解决的复杂问题,为您制作图片是样式转换也在同一个 github 存储库中。(https://github.com/Hvass-Labs/TensorFlow-Tutorials/blob/master/15_Style_Transfer.ipynb)