10
10
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11
11
# ANY KIND, either express or implied. See the License for the specific
12
12
# language governing permissions and limitations under the License.
13
- from __future__ import absolute_import
14
- from __future__ import division
15
- from __future__ import print_function
16
-
17
- import os
13
+ from __future__ import absolute_import , division , print_function
18
14
19
15
import tensorflow as tf
20
- from tensorflow .python .keras .layers import InputLayer , Conv2D , Activation , MaxPooling2D , Dropout , Flatten , Dense
16
+ from tensorflow .python .keras .layers import Activation , Conv2D , Dense , Dropout , Flatten , MaxPooling2D
21
17
from tensorflow .python .keras .models import Sequential
22
- from tensorflow .python .keras .optimizers import RMSprop
23
- from tensorflow .python .saved_model .signature_constants import PREDICT_INPUTS
18
+ from tensorflow .python .training .rmsprop import RMSPropOptimizer
24
19
25
20
HEIGHT = 32
26
21
WIDTH = 32
29
24
NUM_DATA_BATCHES = 5
30
25
NUM_EXAMPLES_PER_EPOCH_FOR_TRAIN = 10000 * NUM_DATA_BATCHES
31
26
BATCH_SIZE = 128
32
- INPUT_TENSOR_NAME = PREDICT_INPUTS
27
+ INPUT_TENSOR_NAME = 'inputs_input' # needs to match the name of the first layer + "_input"
33
28
34
29
35
30
def keras_model_fn (hyperparameters ):
@@ -43,10 +38,7 @@ def keras_model_fn(hyperparameters):
43
38
"""
44
39
model = Sequential ()
45
40
46
- # TensorFlow Serving default prediction input tensor name is PREDICT_INPUTS.
47
- # We must conform to this naming scheme.
48
- model .add (InputLayer (input_shape = (HEIGHT , WIDTH , DEPTH ), name = PREDICT_INPUTS ))
49
- model .add (Conv2D (32 , (3 , 3 ), padding = 'same' ))
41
+ model .add (Conv2D (32 , (3 , 3 ), padding = 'same' , name = 'inputs' , input_shape = (HEIGHT , WIDTH , DEPTH )))
50
42
model .add (Activation ('relu' ))
51
43
model .add (Conv2D (32 , (3 , 3 )))
52
44
model .add (Activation ('relu' ))
@@ -67,19 +59,17 @@ def keras_model_fn(hyperparameters):
67
59
model .add (Dense (NUM_CLASSES ))
68
60
model .add (Activation ('softmax' ))
69
61
70
- _model = tf .keras .Model (inputs = model .input , outputs = model .output )
71
-
72
- opt = RMSprop (lr = hyperparameters ['learning_rate' ], decay = hyperparameters ['decay' ])
62
+ opt = RMSPropOptimizer (learning_rate = hyperparameters ['learning_rate' ], decay = hyperparameters ['decay' ])
73
63
74
- _model .compile (loss = 'categorical_crossentropy' ,
75
- optimizer = opt ,
76
- metrics = ['accuracy' ])
64
+ model .compile (loss = 'categorical_crossentropy' ,
65
+ optimizer = opt ,
66
+ metrics = ['accuracy' ])
77
67
78
- return _model
68
+ return model
79
69
80
70
81
71
def serving_input_fn (hyperpameters ):
82
- inputs = {PREDICT_INPUTS : tf .placeholder (tf .float32 , [None , 32 , 32 , 3 ])}
72
+ inputs = {INPUT_TENSOR_NAME : tf .placeholder (tf .float32 , [None , 32 , 32 , 3 ])}
83
73
return tf .estimator .export .ServingInputReceiver (inputs , inputs )
84
74
85
75
0 commit comments