- Mastering TensorFlow 1.x
- Armando Fandango
- 419字
- 2021-06-25 22:50:59
TF Slim
TF Slim is a lightweight library built on top of TensorFlow core for defining and training models. TF Slim can be used in conjunction with other TensorFlow low level and high-level libraries such as TF Learn. The TF Slim comes as part of the TensorFlow installation in the package: tf.contrib.slim. Run the following command to check if your TF Slim installation is working:
python3 -c 'import tensorflow.contrib.slim as slim; eval = slim.evaluation.evaluate_once'
TF Slim provides several modules that can be picked and applied independently and mixed with other TensorFlow packages. For example, at the time of writing of this book TF Slim had following major modules:

The simple workflow in TF Slim is as follows:
- Create the model using slim layers.
- Provide the input to the layers to instantiate the model.
- Use the logits and labels to define the loss.
- Get the total loss using convenience function get_total_loss().
- Create an optimizer.
- Create a training function using convenience function slim.learning.create_train_op(), total_loss and optimizer.
- Run the training using the convenience function slim.learning.train() and training function defined in the previous step.
The complete code for the MNIST classification example is provided in the notebook ch-02_TF_High_Level_Libraries. The output from the TF Slim MNIST example is as follows:
INFO:tensorflow:Starting Session. INFO:tensorflow:Saving checkpoint to path ./slim_logs/model.ckpt INFO:tensorflow:global_step/sec: 0 INFO:tensorflow:Starting Queues. INFO:tensorflow:global step 100: loss = 2.2669 (0.010 sec/step) INFO:tensorflow:global step 200: loss = 2.2025 (0.010 sec/step) INFO:tensorflow:global step 300: loss = 2.1257 (0.010 sec/step) INFO:tensorflow:global step 400: loss = 2.0419 (0.009 sec/step) INFO:tensorflow:global step 500: loss = 1.9532 (0.009 sec/step) INFO:tensorflow:global step 600: loss = 1.8733 (0.010 sec/step) INFO:tensorflow:global step 700: loss = 1.8002 (0.010 sec/step) INFO:tensorflow:global step 800: loss = 1.7273 (0.010 sec/step) INFO:tensorflow:global step 900: loss = 1.6688 (0.010 sec/step) INFO:tensorflow:global step 1000: loss = 1.6132 (0.010 sec/step) INFO:tensorflow:Stopping Training. INFO:tensorflow:Finished training! Saving model to disk. final loss=1.6131552457809448
As we see from the output, the convenience function slim.learning.train() saves the output of the training in checkpoint files in the specified log directory. If you restart the training, it will first check if the checkpoint exists and will resume the training from the checkpoint by default.
The documentation page for the TF Slim was found empty at the time of this writing at the following link: https://www.tensorflow.org/api_docs/python/tf/contrib/slim. However, some of the documentation can be found in the source code at the following link: https://github.com/tensorflow/tensorflow/tree/r1.4/tensorflow/contrib/slim.
We shall use TF Slim for learning how to use pre-trained models such as VGG16 and Inception V3 in later chapters.
- 新媒體跨界交互設計
- 基于Proteus和Keil的C51程序設計項目教程(第2版):理論、仿真、實踐相融合
- 數字道路技術架構與建設指南
- 計算機組裝·維護與故障排除
- 筆記本電腦維修不是事兒(第2版)
- 電腦軟硬件維修從入門到精通
- 分布式微服務架構:原理與實戰
- BeagleBone Robotic Projects
- Neural Network Programming with Java(Second Edition)
- 單片機原理及應用:基于C51+Proteus仿真
- FL Studio Cookbook
- 觸摸屏應用技術從入門到精通
- Angular 6 by Example
- Blender for Video Production Quick Start Guide
- 電腦主板維修技術