-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat(JAQPOT-386): onnx_sequence #52
Merged
+100
−58
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d27cd4e
predict_sequence_function
johnsaveus 1ac3a53
Merge branch 'main' into onnx_sequence
johnsaveus cf9178f
.
johnsaveus 5f3ee34
torch_utils_folder
johnsaveus d85af6a
.
johnsaveus 72252e7
Merge branch 'main' into onnx_sequence
alarv 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 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 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 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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import base64 | ||
import onnxruntime | ||
import torch | ||
import io | ||
import numpy as np | ||
import torch.nn.functional as f | ||
from src.helpers.torch_utils import to_numpy, generate_prediction_response | ||
from jaqpotpy.descriptors.tokenizer import SmilesVectorizer | ||
from jaqpotpy.api.openapi import ModelType, PredictionRequest, PredictionResponse | ||
from jaqpotpy.descriptors.graph.graph_featurizer import SmilesGraphFeaturizer | ||
|
||
|
||
def torch_sequence_post_handler(request: PredictionRequest) -> PredictionResponse: | ||
feat_config = request.model.torch_config | ||
featurizer = _load_featurizer(feat_config) | ||
target_name = request.model.dependent_features[0].name | ||
model_task = request.model.task | ||
user_input = request.dataset.input | ||
raw_model = request.model.raw_model | ||
predictions = [] | ||
for inp in user_input: | ||
model_output = onnx_post_handler( | ||
raw_model, featurizer.transform(featurizer.transform([inp["SMILES"]])) | ||
) | ||
predictions.append( | ||
generate_prediction_response(model_task, target_name, model_output, inp) | ||
) | ||
return PredictionResponse(predictions=predictions) | ||
|
||
|
||
def onnx_post_handler(raw_model, data): | ||
onnx_model = base64.b64decode(raw_model) | ||
ort_session = onnxruntime.InferenceSession(onnx_model) | ||
ort_inputs = {ort_session.get_inputs()[0].name: to_numpy(data)} | ||
ort_outs = torch.tensor(np.array(ort_session.run(None, ort_inputs))) | ||
return ort_outs | ||
|
||
|
||
def _load_featurizer(config): | ||
featurizer = SmilesVectorizer() | ||
featurizer.load_dict(config) | ||
return featurizer |
This file contains 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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import torch.nn.functional as f | ||
from jaqpotpy.api.openapi.models.model_task import ModelTask | ||
|
||
|
||
def to_numpy(tensor): | ||
return ( | ||
tensor.detach().cpu().numpy() if tensor.requires_grad else tensor.cpu().numpy() | ||
) | ||
|
||
|
||
def torch_binary_classification(target_name, output, inp): | ||
proba = f.sigmoid(output).squeeze().tolist() | ||
pred = int(proba > 0.5) | ||
# UI Results | ||
results = { | ||
"jaqpotMetadata": { | ||
"probabilities": [round((1 - proba), 3), round(proba, 3)], | ||
"jaqpotRowId": inp["jaqpotRowId"], | ||
} | ||
} | ||
if "jaqpotRowLabel" in inp: | ||
results["jaqpotMetadata"]["jaqpotRowLabel"] = inp["jaqpotRowLabel"] | ||
results[target_name] = pred | ||
return results | ||
|
||
|
||
def torch_regression(target_name, output, inp): | ||
pred = [output.squeeze().tolist()] | ||
results = {"jaqpotMetadata": {"jaqpotRowId": inp["jaqpotRowId"]}} | ||
if "jaqpotRowLabel" in inp: | ||
results["jaqpotMetadata"]["jaqpotRowLabel"] = inp["jaqpotRowLabel"] | ||
results[target_name] = pred | ||
return results | ||
|
||
|
||
def generate_prediction_response(model_task, target_name, out, row_id): | ||
if model_task == ModelTask.BINARY_CLASSIFICATION: | ||
return torch_binary_classification(target_name, out, row_id) | ||
elif model_task == ModelTask.REGRESSION: | ||
return torch_regression(target_name, out, row_id) | ||
else: | ||
raise ValueError( | ||
"Only BINARY_CLASSIFICATION and REGRESSION tasks are supported" | ||
) |
Oops, something went wrong.
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.
Is it hard to allow multiple y target names instead of always hardcoding the 1 target_name by getting the dependent_features[0] here?