-
Notifications
You must be signed in to change notification settings - Fork 163
Fix deserialization of dicts for json predict requests #76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -151,7 +151,8 @@ def _create_classification_request(self, data): | |
| def _create_feature_dict_list(self, data): | ||
| """ | ||
| Parses the input data and returns a [dict<string, iterable>] which will be used to create the tf examples. | ||
| If the input data is not a dict, a dictionary will be created with the default predict key PREDICT_INPUTS | ||
| If the input data is not a dict, a dictionary will be created with the default key PREDICT_INPUTS. | ||
| Used on the code path for creating ClassificationRequests. | ||
|
|
||
| Examples: | ||
| input => output | ||
|
|
@@ -184,43 +185,46 @@ def _raise_not_implemented_exception(self, data): | |
|
|
||
| def _create_input_map(self, data): | ||
| """ | ||
| Parses the input data and returns a dict<string, TensorProto> which will be used to create the predict request. | ||
| Parses the input data and returns a dict<string, TensorProto> which will be used to create the PredictRequest. | ||
| If the input data is not a dict, a dictionary will be created with the default predict key PREDICT_INPUTS | ||
|
|
||
| input. | ||
|
|
||
| Examples: | ||
| input => output | ||
| {'inputs': tensor_proto} => {'inputs': tensor_proto} | ||
| ------------------------------------------------- | ||
| tensor_proto => {PREDICT_INPUTS: tensor_proto} | ||
| [1,2,3] => {PREDICT_INPUTS: tensor_proto(1,2,3)} | ||
| {'custom_tensor_name': tensor_proto} => {'custom_tensor_name': TensorProto} | ||
| [1,2,3] => {PREDICT_INPUTS: TensorProto(1,2,3)} | ||
| {'custom_tensor_name': [1, 2, 3]} => {'custom_tensor_name': TensorProto(1,2,3)} | ||
| Args: | ||
| data: request data. Can be any instance of dict<string, tensor_proto>, tensor_proto or any array like data. | ||
| data: request data. Can be any of: ndarray-like, TensorProto, dict<str, TensorProto>, dict<str, ndarray-like> | ||
|
|
||
| Returns: | ||
| dict<string, tensor_proto> | ||
|
|
||
|
|
||
| """ | ||
| msg = """Unsupported request data format: {}. | ||
| Valid formats: tensor_pb2.TensorProto, dict<string, tensor_pb2.TensorProto> and predict_pb2.PredictRequest""" | ||
|
|
||
| if isinstance(data, dict): | ||
| if all(isinstance(v, tensor_pb2.TensorProto) for k, v in data.items()): | ||
| return data | ||
| raise ValueError(msg.format(data)) | ||
| return {k: self._value_to_tensor(v) for k, v in data.items()} | ||
|
|
||
| # When input data is not a dict, no tensor names are given, so use default | ||
| return {self.input_tensor_name: self._value_to_tensor(data)} | ||
|
|
||
| if isinstance(data, tensor_pb2.TensorProto): | ||
| return {self.input_tensor_name: data} | ||
| def _value_to_tensor(self, value): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add some docstring here too. |
||
| """Converts the given value to a tensor_pb2.TensorProto. Used on code path for creating PredictRequests.""" | ||
| if isinstance(value, tensor_pb2.TensorProto): | ||
| return value | ||
|
|
||
| msg = """Unable to convert value to TensorProto: {}. | ||
| Valid formats: tensor_pb2.TensorProto, list, numpy.ndarray""" | ||
| try: | ||
| # TODO: tensorflow container supports prediction requests with ONLY one tensor as input | ||
| input_type = self.input_type_map.values()[0] | ||
| ndarray = np.asarray(data) | ||
| tensor_proto = make_tensor_proto(values=ndarray, dtype=input_type, shape=ndarray.shape) | ||
| return {self.input_tensor_name: tensor_proto} | ||
| except: | ||
| raise ValueError(msg.format(data)) | ||
| ndarray = np.asarray(value) | ||
| return make_tensor_proto(values=ndarray, dtype=input_type, shape=ndarray.shape) | ||
| except Exception: | ||
| raise ValueError(msg.format(value)) | ||
|
|
||
|
|
||
| def _create_tf_example(feature_dict): | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we change the docstrings to reflect the behaviour change?