import tensorflow as tf
import os
import tarfile
import requests
#模型下载地址
inception_pretrain_model_url='http://download.tensorflow.org/models/image/imagenet/inception-2015-12-05.tgz'
#模型存放地址
inception_pretrain_model_dir="inception_model"
if not os.path.exists(inception_pretrain_model_dir):
os.makedirs(inception_pretrain_model_dir)
#获取文件名以及文件路径
filename=inception_pretrain_model_url.split('/')[-1]
filepath=os.path.join(inception_pretrain_model_dir, filename)
#下载模型
if not os.path.exists(filepath):
print("download:", filename)
r=requests.get(inception_pretrain_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("finish: ",filename)
#解压文件
tarfile.open(filepath, 'r:gz').extractall(inception_pretrain_model_dir)
#模型结构存放文件
log_dir='inception_log'
if not os.path.exists(log_dir):
os.makedirs(log_dir)
#classify_image_graph_def.pb为google训练好的模型
inception_graph_def_file=os.path.join(inception_pretrain_model_dir, 'classify_image_graph_def.pb')
with tf.Session() as sess:
#创建一个图来保存google训练好的模型
with tf.gfile.FastGFile(inception_graph_def_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()
这里使用了requests库进行抓取并保存数据,如果要用py下载文件,都可以用这种方式进行下载;
使用tarfile库进行解压,使用tf.gfile tf.GraphDef()等进行图的存储。
目录:
- tensorflow简介、目录
- tensorflow中的图(02-1)
- tensorflow变量的使用(02-2)
- tensorflow中的Fetch、Feed(02-3)
- tensorflow版helloworld---拟合线性函数的k和b(02-4)
- tensorflow非线性回归(03-1)
- MNIST手写数字分类simple版(03-2)
- 二次代价函数、交叉熵(cross-entropy)、对数似然代价函数(log-likelihood cost)(04-1)
- 多层网络通过防止过拟合,增加模型的准确率(04-2)
- 修改优化器进一步提升准确率(04-3)
- 手写数字识别-卷积神经网络cnn(06-2)
- 循环神经网络rnn与长短时记忆神经网络简述(07-2)
- 循环神经网络lstm代码实现(07-3)
- tensorflow模型保存和使用08
- 下载inception v3 google训练好的模型并解压08-3
- 使用inception v3做各种图像分类识别08-4
- word2vec模型训练简单案例
- word2vec+textcnn文本分类简述及代码
来源:oschina
链接:https://my.oschina.net/u/4258176/blog/4316122