1
+ import numpy as np
2
+ import tensorflow as tf
3
+ from tensorflow import keras
4
+ from tensorflow .keras import layers
5
+
6
+ from tensorflow .python .platform import gfile
7
+ # GRAPH_PB_PATH = 'D:/tmp/TensorflowIssue/TensorflowIssue/model/saved_model.pb'
8
+ GRAPH_PB_PATH = 'D:/tmp/TensorFlow.NET/data/saved_model.pb'
9
+ with tf .compat .v1 .Session () as sess :
10
+ print ("load graph" )
11
+ with tf .io .gfile .GFile (GRAPH_PB_PATH ,'rb' ) as f :
12
+ graph_def = tf .compat .v1 .GraphDef ()
13
+ graph_def .ParseFromString (f .read ())
14
+ sess .graph .as_default ()
15
+ tf .import_graph_def (graph_def , name = '' )
16
+ graph_nodes = [n for n in graph_def .node ]
17
+ names = []
18
+ for t in graph_nodes :
19
+ names .append (t .name )
20
+ print (names )
21
+
22
+ inputs = keras .Input (shape = (784 ,))
23
+
24
+ dense = layers .Dense (64 , activation = "relu" )
25
+ x = dense (inputs )
26
+
27
+ dense = layers .Dense (64 , activation = "relu" )
28
+ x = dense (x )
29
+
30
+ dense = layers .Dense (10 )
31
+ outputs = dense (x )
32
+
33
+ model = keras .Model (inputs = inputs , outputs = outputs , name = "mnist_model" )
34
+ model .summary ();
35
+
36
+ (x_train , y_train ), (x_test , y_test ) = keras .datasets .mnist .load_data ()
37
+
38
+ x_train = x_train .reshape (60000 , 784 ).astype ("float32" ) / 255
39
+ x_test = x_test .reshape (10000 , 784 ).astype ("float32" ) / 255
40
+
41
+ model .compile (
42
+ loss = keras .losses .SparseCategoricalCrossentropy (from_logits = True ),
43
+ optimizer = keras .optimizers .RMSprop (),
44
+ metrics = ["accuracy" ],
45
+ )
46
+
47
+ history = model .fit (x_train , y_train , batch_size = 64 , epochs = 2 , validation_split = 0.2 )
48
+
49
+ test_scores = model .evaluate (x_test , y_test , verbose = 2 )
50
+ print ("Test loss:" , test_scores [0 ])
51
+ print ("Test accuracy:" , test_scores [1 ])
0 commit comments