- 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.
- 零點起飛學Xilinx FPG
- FPGA從入門到精通(實戰篇)
- Applied Unsupervised Learning with R
- 硬件產品經理手冊:手把手構建智能硬件產品
- Apple Motion 5 Cookbook
- 嵌入式系統中的模擬電路設計
- CC2530單片機技術與應用
- Practical Machine Learning with R
- Building 3D Models with modo 701
- Blender Game Engine:Beginner's Guide
- 筆記本電腦芯片級維修從入門到精通(圖解版)
- 基于網絡化教學的項目化單片機應用技術
- 單片機原理及應用
- 筆記本電腦現場維修實錄
- 從企業級開發到云原生微服務:Spring Boot實戰