Skip to content

Commit 00f0bed

Browse files
committed
tfmodel_save_load
1 parent 980fb2e commit 00f0bed

File tree

12 files changed

+277
-0
lines changed

12 files changed

+277
-0
lines changed

.DS_Store

0 Bytes
Binary file not shown.

Untitled.ipynb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,44 @@
7979
"source": [
8080
"t.astype(str)"
8181
]
82+
},
83+
{
84+
"cell_type": "code",
85+
"execution_count": 1,
86+
"metadata": {},
87+
"outputs": [],
88+
"source": [
89+
"import os, argparse"
90+
]
91+
},
92+
{
93+
"cell_type": "code",
94+
"execution_count": 3,
95+
"metadata": {},
96+
"outputs": [],
97+
"source": [
98+
"dir = os.path.dirname(os.path.realpath('./server'))"
99+
]
100+
},
101+
{
102+
"cell_type": "code",
103+
"execution_count": 4,
104+
"metadata": {},
105+
"outputs": [
106+
{
107+
"data": {
108+
"text/plain": [
109+
"'/Users/Alum/Documents/WorkSpace/python_learn'"
110+
]
111+
},
112+
"execution_count": 4,
113+
"metadata": {},
114+
"output_type": "execute_result"
115+
}
116+
],
117+
"source": [
118+
"dir"
119+
]
82120
}
83121
],
84122
"metadata": {

tensorflow/.DS_Store

8 KB
Binary file not shown.

tensorflow/Model/checkpoint

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
model_checkpoint_path: "data-all"
2+
all_model_checkpoint_paths: "data-all"
8 Bytes
Binary file not shown.

tensorflow/Model/data-all.index

135 Bytes
Binary file not shown.

tensorflow/Model/data-all.meta

4.27 KB
Binary file not shown.

tensorflow/Model/frozen_model.pb

100 Bytes
Binary file not shown.

tensorflow/Untitled.ipynb

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [
8+
{
9+
"name": "stderr",
10+
"output_type": "stream",
11+
"text": [
12+
"/usr/local/Cellar/python/3.6.4_3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88\n",
13+
" return f(*args, **kwds)\n"
14+
]
15+
}
16+
],
17+
"source": [
18+
"import tensorflow as tf\n",
19+
"\n",
20+
"def load_graph(frozen_graph_filename):\n",
21+
" # We load the protobuf file from the disk and parse it to retrieve the \n",
22+
" # unserialized graph_def\n",
23+
" with tf.gfile.GFile(frozen_graph_filename, \"rb\") as f:\n",
24+
" graph_def = tf.GraphDef()\n",
25+
" graph_def.ParseFromString(f.read())\n",
26+
"\n",
27+
" # Then, we import the graph_def into a new Graph and returns it \n",
28+
" with tf.Graph().as_default() as graph:\n",
29+
" # The name var will prefix every op/nodes in your graph\n",
30+
" # Since we load everything in a new graph, this is not needed\n",
31+
" tf.import_graph_def(graph_def, name=\"prefix\")\n",
32+
" return graph"
33+
]
34+
},
35+
{
36+
"cell_type": "code",
37+
"execution_count": 2,
38+
"metadata": {},
39+
"outputs": [],
40+
"source": [
41+
"graph = load_graph('./Model/frozen_model.pb')"
42+
]
43+
},
44+
{
45+
"cell_type": "code",
46+
"execution_count": 3,
47+
"metadata": {},
48+
"outputs": [
49+
{
50+
"name": "stdout",
51+
"output_type": "stream",
52+
"text": [
53+
"prefix/v1\n",
54+
"prefix/v2\n"
55+
]
56+
}
57+
],
58+
"source": [
59+
"for op in graph.get_operations():\n",
60+
" print(op.name)"
61+
]
62+
},
63+
{
64+
"cell_type": "code",
65+
"execution_count": 4,
66+
"metadata": {},
67+
"outputs": [],
68+
"source": [
69+
"x = graph.get_tensor_by_name('prefix/v1:0')\n",
70+
"y = graph.get_tensor_by_name('prefix/v2:0')"
71+
]
72+
},
73+
{
74+
"cell_type": "code",
75+
"execution_count": 8,
76+
"metadata": {},
77+
"outputs": [],
78+
"source": [
79+
"a = tf.add(x,y)"
80+
]
81+
},
82+
{
83+
"cell_type": "code",
84+
"execution_count": 9,
85+
"metadata": {},
86+
"outputs": [
87+
{
88+
"name": "stdout",
89+
"output_type": "stream",
90+
"text": [
91+
"3.0\n"
92+
]
93+
}
94+
],
95+
"source": [
96+
"with tf.Session(graph=graph) as sess:\n",
97+
" print(sess.run(a))"
98+
]
99+
}
100+
],
101+
"metadata": {
102+
"kernelspec": {
103+
"display_name": "Python 3",
104+
"language": "python",
105+
"name": "python3"
106+
},
107+
"language_info": {
108+
"codemirror_mode": {
109+
"name": "ipython",
110+
"version": 3
111+
},
112+
"file_extension": ".py",
113+
"mimetype": "text/x-python",
114+
"name": "python",
115+
"nbconvert_exporter": "python",
116+
"pygments_lexer": "ipython3",
117+
"version": "3.6.4"
118+
}
119+
},
120+
"nbformat": 4,
121+
"nbformat_minor": 2
122+
}

tensorflow/load-model.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import tensorflow as tf
2+
3+
def load_graph(frozen_graph_filename):
4+
# We load the protobuf file from the disk and parse it to retrieve the
5+
# unserialized graph_def
6+
with tf.gfile.GFile(frozen_graph_filename, "rb") as f:
7+
graph_def = tf.GraphDef()
8+
graph_def.ParseFromString(f.read())
9+
10+
# Then, we import the graph_def into a new Graph and returns it
11+
with tf.Graph().as_default() as graph:
12+
# The name var will prefix every op/nodes in your graph
13+
# Since we load everything in a new graph, this is not needed
14+
tf.import_graph_def(graph_def, name="prefix")
15+
return graph
16+
17+
if __name__=='__main__':
18+
graph = load_graph('./Model/frozen_model.pb')
19+
for op in graph.get_operations():
20+
print(op.name)
21+
x = graph.get_tensor_by_name('prefix/v1:0')
22+
y = graph.get_tensor_by_name('prefix/v2:0')
23+
a = tf.add(x,y)
24+
with tf.Session(graph=graph) as sess :
25+
print(sess.run(a))

0 commit comments

Comments
 (0)