TensorFlow Filesystem — Access Tensors Differently

The motivation I had behind writing TFFS (TensorFlow File System) can be shared by anyone who has used tensorflow, including you.All I wanted was to know what the name of a specific tensor is; or what its input tensors are (ignoring operations).All of these questions can be easily answered using tensorboard.Sure, you just open the graph tab, and visually examine the graph. Really convenient, right? Well, only if you want to have a bird overview of the graph. But if you’re focused and have a specific question you want to answer, using the keyboard is the way to go.So why not load the graph inside a python shell and examine it?That’s doable, but writing these lines of code every time I want to do that task? Having to remember how to load a graph, how to look for a tensor, how to get its inputs… Sure, it’s only a couple of lines of code, but once you repeat the same task over and over again, it’s time to write a script!So why not write an interactive script?You mean a script that given the path to your model loads it for you, and provides utility functions to ease your pain of writing tensorflow code? Well, we could do that, but that’s not gonna be as awesome as what I’m gonna show you!Disclaimer: if you want a solution that makes sense, stop reading here and just use the interactive script approach. Continue reading only if you want to learn something a bit different ;)Filesystem to the Rescue!The names of tensors have slashes — a big resemblance with the UNIX filesystem..Imagine a world with a tensorflow filesystem, where directories are analogous to tensorflow scopes, and files — to tensors..For instance, we could list all available scopes and tensors by running find ~/tf — assuming ~/tf is where the tensorflow filesystem mounted..Including documentation, it’s only 345 lines of code.First we need to load a tensorflow model:Import the graph structure using tf.train.import_meta_graph.If the model was trained, load the weights using saver.restore.Mapping Tensors to FilesNext, I’ll describe the main class..Given its shape, and given we use a formatter that outputs a fixed amount of characters per entry in the result (this is where _fixed_val_length comes in), we can calculate the size.Getting Inputs and OutputsWhile tensorflow scopes have a structure that resembles a filesystem, tensor inputs and outputs don’t..So instead of using a filesystem to get inputs and outputs, we can write a script that can be executed as follows:~/tf/bin/outputs — depth 3 ~/tf/a:0The result will look like this:~/tf/a:0├── ~/tf/b:0└── ~/tf/c:0 └── ~/tf/d:0 | └── ~/tf/e:0 └── ~/tf/f:0Nice!.It means it won’t have access to the tensorflow graph, which was loaded in the main process.So how is it going to access all the inputs/outputs of a tensor?.The _populate_bin function which we saw earlier reads this python file, and replaces {{TENSOR_TO_TENSORS_PLACEHOLDER}} with the dictionary of tensor to outputs (or inputs)..We mapped a tensorflow model to a filesystem..With python, you can easily import a tensorflow model and inspect the tensors manually.. More details

Leave a Reply