1# Copyright 2016 The TensorFlow Authors. All Rights Reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# ============================================================================== 15"""Utils for managing different mode strings used by Keras and Estimator models. 16""" 17 18from __future__ import absolute_import 19from __future__ import division 20from __future__ import print_function 21 22from tensorflow.python.util.compat import collections_abc 23 24 25class KerasModeKeys(object): 26 """Standard names for model modes. 27 28 The following standard keys are defined: 29 30 * `TRAIN`: training/fitting mode. 31 * `TEST`: testing/evaluation mode. 32 * `PREDICT`: prediction/inference mode. 33 """ 34 35 TRAIN = 'train' 36 TEST = 'test' 37 PREDICT = 'predict' 38 39 40# TODO(kathywu): Remove copy in Estimator after nightlies 41class EstimatorModeKeys(object): 42 """Standard names for Estimator model modes. 43 44 The following standard keys are defined: 45 46 * `TRAIN`: training/fitting mode. 47 * `EVAL`: testing/evaluation mode. 48 * `PREDICT`: predication/inference mode. 49 """ 50 51 TRAIN = 'train' 52 EVAL = 'eval' 53 PREDICT = 'infer' 54 55 56def is_predict(mode): 57 return mode in [KerasModeKeys.PREDICT, EstimatorModeKeys.PREDICT] 58 59 60def is_eval(mode): 61 return mode in [KerasModeKeys.TEST, EstimatorModeKeys.EVAL] 62 63 64def is_train(mode): 65 return mode in [KerasModeKeys.TRAIN, EstimatorModeKeys.TRAIN] 66 67 68class ModeKeyMap(collections_abc.Mapping): 69 """Map using ModeKeys as keys. 70 71 This class creates an immutable mapping from modes to values. For example, 72 SavedModel export of Keras and Estimator models use this to map modes to their 73 corresponding MetaGraph tags/SignatureDef keys. 74 75 Since this class uses modes, rather than strings, as keys, both "predict" 76 (Keras's PREDICT ModeKey) and "infer" (Estimator's PREDICT ModeKey) map to the 77 same value. 78 """ 79 80 def __init__(self, **kwargs): 81 self._internal_dict = {} 82 self._keys = [] 83 for key in kwargs: 84 self._keys.append(key) 85 dict_key = self._get_internal_key(key) 86 if dict_key in self._internal_dict: 87 raise ValueError( 88 'Error creating ModeKeyMap. Multiple keys/values found for {} mode.' 89 .format(dict_key)) 90 self._internal_dict[dict_key] = kwargs[key] 91 92 def _get_internal_key(self, key): 93 """Return keys used for the internal dictionary.""" 94 if is_train(key): 95 return KerasModeKeys.TRAIN 96 if is_eval(key): 97 return KerasModeKeys.TEST 98 if is_predict(key): 99 return KerasModeKeys.PREDICT 100 raise ValueError('Invalid mode key: {}.'.format(key)) 101 102 def __getitem__(self, key): 103 return self._internal_dict[self._get_internal_key(key)] 104 105 def __iter__(self): 106 return iter(self._keys) 107 108 def __len__(self): 109 return len(self._keys) 110