实现任务:
1. 从tensorflow 官网上将 inception3 的模型下载下来,进行保存。
2. 在 tensorboard 里面将inception3 的模型结构进行可视化。
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 4 17:12:10 2019
@author: 666
"""
import tensorflow as tf
import requests
import tarfile
import os
#inception下载地址
inception_model_url = 'http://download.tensorflow.org/models/image/imagenet/inception-2015-12-05.tgz'
#模型保存地址
inception_mdel_dir = "inception_model"
if not os.path.exists(inception_mdel_dir):
os.makedirs(inception_mdel_dir)
#获取文件名,以及文件的路径
filename = inception_model_url.split('/')[-1]
filepath = os.path.join(inception_mdel_dir,filename)
#下载模型
if not os.path.exists(filepath):
print("download: " ,filename)
r = requests.get(inception_model_url,stream = True)
with open(filepath,'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
print("finsh: ",filename)
#解压文件
tarfile.open(filepath,'r:gz').extractall(inception_mdel_dir)
#模型结构存放文件
log_dir = 'inception_log'
if not os.path.exists(log_dir):
os.makedirs(log_dir)
#读训练好的模型的路径
inception_graph_file = os.path.join(inception_mdel_dir,'classify_image_graph_def.pb')
with tf.Session() as sess:
#创建一个图来存放google 训练好的模型
with tf.gfile.FastGFile(inception_graph_file,'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
tf.import_graph_def(graph_def,name='')
#保存图的结构
writer = tf.summary.FileWriter(log_dir,sess.graph)
writer.close()