-
Notifications
You must be signed in to change notification settings - Fork 733
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
[WIP]Add_pytorch_model_to_inference_model_in_scala #2897
Closed
+67
−11
Closed
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
eb962d2
add pytorch model to inference model in scala
ManfeiBai cfb7761
Modify space file for scala test
ManfeiBai 175fdc3
git pull from manfeibai and push to manfei bai to keep version
ManfeiBai 2bf1864
delete repeat
ManfeiBai 46e9768
add TorchModel inference
ManfeiBai fd721cc
change other function to todo type
ManfeiBai af9721d
Merge branch 'master' of https://github.com/intel-analytics/analytics…
ManfeiBai 6a2ab11
add todo comment
ManfeiBai 3caac92
add two more todo comment
ManfeiBai 8e82a50
add todo again
ManfeiBai 19d91d6
add todo comment in java file
ManfeiBai a8e9dab
debug in TorchModelSpec.scala
ManfeiBai 9e002fe
add new model initialization
ManfeiBai af71fa2
correct function name
ManfeiBai d4bbe66
use lenet in ut
ManfeiBai dc91015
clear todo comment
ManfeiBai 08ba17d
use apply for modelBytes
ManfeiBai 6fccbe2
correct Chinese to English
ManfeiBai c99a08c
different byte load function
ManfeiBai 55d60cd
different function
ManfeiBai adf0168
correct spec logic
ManfeiBai 99e9e3c
add AbstructModel and FloatModel
ManfeiBai 6edf1e9
correct the logic of function
ManfeiBai 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
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 |
---|---|---|
|
@@ -19,10 +19,12 @@ import com.intel.analytics.bigdl.tensor.Tensor | |
import com.intel.analytics.zoo.common.{PythonInterpreter, PythonInterpreterTest} | ||
import com.intel.analytics.zoo.core.TFNetNative | ||
import com.intel.analytics.zoo.pipeline.api.keras.ZooSpecHelper | ||
import com.intel.analytics.zoo.pipeline.inference.{InferenceModel, AbstractModel, FloatModel} | ||
import jep.NDArray | ||
import org.apache.log4j.{Level, Logger} | ||
import org.apache.spark.{SparkConf, SparkContext} | ||
|
||
|
||
@PythonInterpreterTest | ||
class TorchModelSpec extends ZooSpecHelper{ | ||
|
||
|
@@ -232,4 +234,60 @@ class TorchModelSpec extends ZooSpecHelper{ | |
PythonInterpreter.exec(genInputCode) | ||
model.forward(Tensor[Float]()) | ||
} | ||
|
||
"doLoadPyTorch" should "do load PyTorch Model without error" in { | ||
ifskipTest() | ||
val tmpname = createTmpFile().getAbsolutePath() | ||
val code = lenet + | ||
s""" | ||
|model = LeNet() | ||
|torch.save(model, "$tmpname", pickle_module=zoo_pickle_module) | ||
|""".stripMargin | ||
PythonInterpreter.exec(code) | ||
val model = new InferenceModel() | ||
model.doLoadPyTorch(tmpname) | ||
|
||
val genInputCode = | ||
s""" | ||
|import numpy as np | ||
|import torch | ||
|input = torch.tensor(np.random.rand(4, 1, 28, 28), dtype=torch.float32) | ||
|target = torch.tensor(np.ones([4]), dtype=torch.long) | ||
|_data = (input, target) | ||
|""".stripMargin | ||
PythonInterpreter.exec(genInputCode) | ||
val result = model.doPredict(Tensor[Float]()) | ||
result should not be (Tensor[Float](4, 10).fill(-2.3025851f)) | ||
} | ||
|
||
"doLoadPyTorch" should "also load PyTorch by modelBytes" in { | ||
ifskipTest() | ||
val code = lenet + | ||
s""" | ||
|model = LeNet() | ||
|criterion = nn.CrossEntropyLoss() | ||
|from pyspark.serializers import CloudPickleSerializer | ||
|byc = CloudPickleSerializer.dumps(CloudPickleSerializer, criterion) | ||
|bys = io.BytesIO() | ||
|torch.save(model, bys, pickle_module=zoo_pickle_module) | ||
|bym = bys.getvalue() | ||
|""".stripMargin | ||
PythonInterpreter.exec(code) | ||
|
||
val bys = PythonInterpreter.getValue[Array[Byte]]("bym") | ||
val model = new InferenceModel() | ||
model.doLoadPyTorchBytes(bys) | ||
|
||
val genInputCode = | ||
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. same 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. Thank! qiuxin, I would try it soon~ |
||
s""" | ||
|import numpy as np | ||
|import torch | ||
|input = torch.tensor(np.random.rand(4, 1, 28, 28), dtype=torch.float32) | ||
|target = torch.tensor(np.ones([4]), dtype=torch.long) | ||
|_data = (input, target) | ||
|""".stripMargin | ||
PythonInterpreter.exec(genInputCode) | ||
val result = model.doPredict(Tensor[Float]()) | ||
result should not be (Tensor[Float](4, 10).fill(-2.3025851f)) | ||
} | ||
} |
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.
I think you should pass the input in scala.
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.
Thanks! qiuxin, I would try it soon~