1010# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
1111# ANY KIND, either express or implied. See the License for the specific
1212# 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
1814
1915import 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
2117from 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
2419
2520HEIGHT = 32
2621WIDTH = 32
2924NUM_DATA_BATCHES = 5
3025NUM_EXAMPLES_PER_EPOCH_FOR_TRAIN = 10000 * NUM_DATA_BATCHES
3126BATCH_SIZE = 128
32- INPUT_TENSOR_NAME = PREDICT_INPUTS
27+ INPUT_TENSOR_NAME = 'inputs_input' # needs to match the name of the first layer + "_input"
3328
3429
3530def keras_model_fn (hyperparameters ):
@@ -43,10 +38,7 @@ def keras_model_fn(hyperparameters):
4338 """
4439 model = Sequential ()
4540
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 )))
5042 model .add (Activation ('relu' ))
5143 model .add (Conv2D (32 , (3 , 3 )))
5244 model .add (Activation ('relu' ))
@@ -67,19 +59,17 @@ def keras_model_fn(hyperparameters):
6759 model .add (Dense (NUM_CLASSES ))
6860 model .add (Activation ('softmax' ))
6961
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' ])
7363
74- _model .compile (loss = 'categorical_crossentropy' ,
75- optimizer = opt ,
76- metrics = ['accuracy' ])
64+ model .compile (loss = 'categorical_crossentropy' ,
65+ optimizer = opt ,
66+ metrics = ['accuracy' ])
7767
78- return _model
68+ return model
7969
8070
8171def 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 ])}
8373 return tf .estimator .export .ServingInputReceiver (inputs , inputs )
8474
8575
0 commit comments