머신러닝과 기술적 분석

[Tensorflow] Checkpoint file에 저장되어있는 Variable Tensor를 알아보는 방법 본문

Tensorflow

[Tensorflow] Checkpoint file에 저장되어있는 Variable Tensor를 알아보는 방법

BetterToday 2017. 8. 19. 16:13
728x90

TensorFlow는 학습한 모델을 2개의 file로 나누어서 저장한다. meta file 에는 code 로 구현한 graph가 저장이 되고 (.meta file) ckeckpoint file 에는 training 과정에서 결정된 Variable Tensor의 value가 저장된다. (.ckpt file) 따라서 두 개의 file이 있으면 학습한 network model을 load 할수 가 있다.


그런데 TensorFlow Model Zoo 처럼 ckeckpoint file 만을 제공하는 경우도 있고, ckeckpoint 에 저장되어 있는 variable tensor의 scope/name을 알고 싶을 때가 있다.


이럴 때는 tf.contrib.framework.list_variables() 를 사용하면 쉽게 variable tensor의 scope/name을 알 수가 있다.

아래의 code 처럼 ckeckpoint에 저장되어있는 모든 variable tensor의 name과 shape 을 볼 수 가 있다.

variables = tf.contrib.framework.list_variables('ckpts/vgg_16.ckpt')   # Tensorflow Model Zoo 에서 제공하는 ckeckpoint file

for i, v in enumerate(variables):
    print("{}. name : {} \n    shape : {}".format(i, v[0], v[1]))

다음은 code 를 실행한 결과이다.

  • global_step을 제외한 모든 variable이 vgg_16 이라는 최상위 scope 밑에 layer 별로 나뉘어져 있다.
  • 최하위에는 weight / biases 라는 suffix가 붙어있는 모습이다.
0. name : global_step 
    shape : []
1. name : vgg_16/conv1/conv1_1/biases 
    shape : [64]
2. name : vgg_16/conv1/conv1_1/weights 
    shape : [3, 3, 3, 64]
3. name : vgg_16/conv1/conv1_2/biases 
    shape : [64]
4. name : vgg_16/conv1/conv1_2/weights 
    shape : [3, 3, 64, 64]
5. name : vgg_16/conv2/conv2_1/biases 
    shape : [128]
6. name : vgg_16/conv2/conv2_1/weights 
    shape : [3, 3, 64, 128]
7. name : vgg_16/conv2/conv2_2/biases 
    shape : [128]
8. name : vgg_16/conv2/conv2_2/weights 
    shape : [3, 3, 128, 128]
9. name : vgg_16/conv3/conv3_1/biases 
    shape : [256]
10. name : vgg_16/conv3/conv3_1/weights 
    shape : [3, 3, 128, 256]
11. name : vgg_16/conv3/conv3_2/biases 
    shape : [256]
12. name : vgg_16/conv3/conv3_2/weights 
    shape : [3, 3, 256, 256]
13. name : vgg_16/conv3/conv3_3/biases 
    shape : [256]
14. name : vgg_16/conv3/conv3_3/weights 
    shape : [3, 3, 256, 256]
15. name : vgg_16/conv4/conv4_1/biases 
    shape : [512]
16. name : vgg_16/conv4/conv4_1/weights 
    shape : [3, 3, 256, 512]
17. name : vgg_16/conv4/conv4_2/biases 
    shape : [512]
18. name : vgg_16/conv4/conv4_2/weights 
    shape : [3, 3, 512, 512]
19. name : vgg_16/conv4/conv4_3/biases 
    shape : [512]
20. name : vgg_16/conv4/conv4_3/weights 
    shape : [3, 3, 512, 512]
21. name : vgg_16/conv5/conv5_1/biases 
    shape : [512]
22. name : vgg_16/conv5/conv5_1/weights 
    shape : [3, 3, 512, 512]
23. name : vgg_16/conv5/conv5_2/biases 
    shape : [512]
24. name : vgg_16/conv5/conv5_2/weights 
    shape : [3, 3, 512, 512]
25. name : vgg_16/conv5/conv5_3/biases 
    shape : [512]
26. name : vgg_16/conv5/conv5_3/weights 
    shape : [3, 3, 512, 512]
27. name : vgg_16/fc6/biases 
    shape : [4096]
28. name : vgg_16/fc6/weights 
    shape : [7, 7, 512, 4096]
29. name : vgg_16/fc7/biases 
    shape : [4096]
30. name : vgg_16/fc7/weights 
    shape : [1, 1, 4096, 4096]
31. name : vgg_16/fc8/biases 
    shape : [1000]
32. name : vgg_16/fc8/weights 
    shape : [1, 1, 4096, 1000]
33. name : vgg_16/mean_rgb 
    shape : [3]
  • 실행 환경
    • python 3.5 + tensorflow 1.0.1


728x90
반응형
Comments