@@ -151,7 +151,8 @@ def _create_classification_request(self, data):
151
151
def _create_feature_dict_list (self , data ):
152
152
"""
153
153
Parses the input data and returns a [dict<string, iterable>] which will be used to create the tf examples.
154
- If the input data is not a dict, a dictionary will be created with the default predict key PREDICT_INPUTS
154
+ If the input data is not a dict, a dictionary will be created with the default key PREDICT_INPUTS.
155
+ Used on the code path for creating ClassificationRequests.
155
156
156
157
Examples:
157
158
input => output
@@ -184,43 +185,46 @@ def _raise_not_implemented_exception(self, data):
184
185
185
186
def _create_input_map (self , data ):
186
187
"""
187
- Parses the input data and returns a dict<string, TensorProto> which will be used to create the predict request .
188
+ Parses the input data and returns a dict<string, TensorProto> which will be used to create the PredictRequest .
188
189
If the input data is not a dict, a dictionary will be created with the default predict key PREDICT_INPUTS
189
190
190
191
input.
191
192
192
193
Examples:
193
194
input => output
194
- {'inputs': tensor_proto} => {'inputs': tensor_proto}
195
+ -------------------------------------------------
195
196
tensor_proto => {PREDICT_INPUTS: tensor_proto}
196
- [1,2,3] => {PREDICT_INPUTS: tensor_proto(1,2,3)}
197
+ {'custom_tensor_name': tensor_proto} => {'custom_tensor_name': TensorProto}
198
+ [1,2,3] => {PREDICT_INPUTS: TensorProto(1,2,3)}
199
+ {'custom_tensor_name': [1, 2, 3]} => {'custom_tensor_name': TensorProto(1,2,3)}
197
200
Args:
198
- data: request data. Can be any instance of dict<string, tensor_proto >, tensor_proto or any array like data.
201
+ data: request data. Can be any of: ndarray-like, TensorProto, dict<str, TensorProto >, dict<str, ndarray- like>
199
202
200
203
Returns:
201
204
dict<string, tensor_proto>
202
205
203
206
204
207
"""
205
- msg = """Unsupported request data format: {}.
206
- Valid formats: tensor_pb2.TensorProto, dict<string, tensor_pb2.TensorProto> and predict_pb2.PredictRequest"""
207
-
208
208
if isinstance (data , dict ):
209
- if all (isinstance (v , tensor_pb2 .TensorProto ) for k , v in data .items ()):
210
- return data
211
- raise ValueError (msg .format (data ))
209
+ return {k : self ._value_to_tensor (v ) for k , v in data .items ()}
210
+
211
+ # When input data is not a dict, no tensor names are given, so use default
212
+ return {self .input_tensor_name : self ._value_to_tensor (data )}
212
213
213
- if isinstance (data , tensor_pb2 .TensorProto ):
214
- return {self .input_tensor_name : data }
214
+ def _value_to_tensor (self , value ):
215
+ """Converts the given value to a tensor_pb2.TensorProto. Used on code path for creating PredictRequests."""
216
+ if isinstance (value , tensor_pb2 .TensorProto ):
217
+ return value
215
218
219
+ msg = """Unable to convert value to TensorProto: {}.
220
+ Valid formats: tensor_pb2.TensorProto, list, numpy.ndarray"""
216
221
try :
217
222
# TODO: tensorflow container supports prediction requests with ONLY one tensor as input
218
223
input_type = self .input_type_map .values ()[0 ]
219
- ndarray = np .asarray (data )
220
- tensor_proto = make_tensor_proto (values = ndarray , dtype = input_type , shape = ndarray .shape )
221
- return {self .input_tensor_name : tensor_proto }
222
- except :
223
- raise ValueError (msg .format (data ))
224
+ ndarray = np .asarray (value )
225
+ return make_tensor_proto (values = ndarray , dtype = input_type , shape = ndarray .shape )
226
+ except Exception :
227
+ raise ValueError (msg .format (value ))
224
228
225
229
226
230
def _create_tf_example (feature_dict ):
0 commit comments