参考程序:
1 import tensorflow as tf
2 import os
3 import tarfile
4 import requests
5
6 #inception模型下载地址
7 inception_pretrain_model_url = 'http://download.tensorflow.org/models/image/imagenet/inception-2015-12-05.tgz'
8
9 #模型存放地址
10 inception_pretrain_model_dir = "inception_model"
11 if not os.path.exists(inception_pretrain_model_dir):
12 os.makedirs(inception_pretrain_model_dir)
13
14 #获取文件名,以及文件路径
15 filename = inception_pretrain_model_url.split('/')[-1]
16 filepath = os.path.join(inception_pretrain_model_dir, filename)
17
18 #下载模型
19 if not os.path.exists(filepath):
20 print("download: ", filename)
21 r = requests.get(inception_pretrain_model_url, stream=True)
22 with open(filepath, 'wb') as f:
23 for chunk in r.iter_content(chunk_size=1024):
24 if chunk:
25 f.write(chunk)
26 print("finish: ", filename)
27 #解压文件
28 tarfile.open(filepath, 'r:gz').extractall(inception_pretrain_model_dir)
29
30 #模型结构存放文件
31 log_dir = 'inception_log'
32 if not os.path.exists(log_dir):
33 os.makedirs(log_dir)
34
35 #classify_image_graph_def.pb为google训练好的模型
36 inception_graph_def_file = os.path.join(inception_pretrain_model_dir, 'classify_image_graph_def.pb')
37 with tf.Session() as sess:
38 #创建一个图来存放google训练好的模型
39 with tf.gfile.FastGFile(inception_graph_def_file, 'rb') as f:
40 graph_def = tf.GraphDef()
41 graph_def.ParseFromString(f.read())
42 tf.import_graph_def(graph_def, name='')
43 #保存图的结构
44 writer = tf.summary.FileWriter(log_dir, sess.graph)
45 writer.close()
download: inception-2015-12-05.tgz
finish: inception-2015-12-05.tgz
程序运行之后,当前路径下生成了2个文件夹:
文件夹中下载的压缩包和解压之后的文件:
events文件,查看网络结构:
输入命令行:
2019-06-19 15:23:16