Comments (4)
I can't spot any leak in your code, just some double/verbal tensor deletions. Any chance that you can share a testing model so that I can run the code? Thanks.
Also, if you have insight into your model, you can construct model with gotch and load weights from trained model in Pytorch or even train with gotch. Have a look at subpackage https://github.com/sugarme/gotch/tree/master/pickle and example at https://github.com/sugarme/gotch/tree/master/example/pickle .
from gotch.
@sugarme Thanks for taking a look! The model I'm using is https://storage.googleapis.com/freelance-models/model.pt
Ah ok, I'll take a look at that second option as well. The model is resnet32 (fine tuned using FastAI).
from gotch.
I tried to run your code with the model and can see memory consuming. It turned out the model was loaded every iteration as you passed Go struct instead of pointer.
Have a look at the code below. It runs just Okay.
package main
import (
"log"
"github.com/sugarme/gotch"
"github.com/sugarme/gotch/ts"
"github.com/sugarme/gotch/vision"
)
type Model struct {
cmodule *ts.CModule
imageNet *vision.ImageNet
}
func NewModel(modelPath string) (*Model, error) {
model, err := ts.ModuleLoadOnDevice(modelPath, gotch.CPU)
if err != nil {
return nil, err
}
imageNet := vision.NewImageNet()
return &Model{
cmodule: model,
imageNet: imageNet,
}, nil
}
func (m *Model) Predict(imageFile string) ([]float32, error) {
imageNet := vision.NewImageNet()
image, err := imageNet.LoadImageAndResize224(imageFile)
if err != nil {
log.Fatal(err)
}
input := image.MustUnsqueeze(0, true)
raw_output := input.ApplyCModule(m.cmodule)
output := raw_output.MustSoftmax(-1, gotch.Float, true)
probs := output.Vals().([]float32)
output.MustDrop()
return probs, nil
}
func main() {
modelPath := "./model.pt"
imageFile := "./image.jpg"
model, err := NewModel(modelPath)
if err != nil {
panic(err)
}
n := 1000
for i := 0; i < n; i++ {
prob, err := model.Predict(imageFile)
if err != nil {
panic(err)
}
if i%100 == 0 && i > 0 {
log.Printf("Done... %2d: %v\n", i, prob)
}
}
}
from gotch.
Ah I see, that makes sense! Thanks for your help here as it was my mistake and not a bug with gotch.
Excellent work with gotch btw, I shall be making use of it in a few of my upcoming projects so do let me know if there's anywhere I can donate to support the project!
from gotch.
Related Issues (20)
- v2.0 support HOT 3
- Possible Memory Leak From C.malloc(0) HOT 3
- how to load model pytorch_model.bin HOT 5
- Indexing documentation HOT 1
- Can't build project with gotch v0.9.0 HOT 3
- Production use for gotch just for inference HOT 3
- Concurrency issue in generating tensor name in newTensor HOT 3
- Can memory leak in tensor-generated.go because of malloc(0) ? HOT 8
- Memory Leak in JIT Model under Multi-Goroutine Environment HOT 9
- how can i convert gocv.Mat image data to Tensor ? HOT 2
- `*ts.CModule` does not implement `ts.Module` interface correctly
- Cannot Run the Application with Cgo HOT 1
- Cannot Run the Application using Libtorch 2.1 (CPU) Docker Image
- Consider using build tags instead of a bash script HOT 6
- TestOptimizer is flaky
- install steps on macos HOT 3
- Cgo Memory Leak HOT 13
- Q) Is there a function or method to clear the cached memory? HOT 1
- Float64Values() shows an error 'Unsupported Go type: []float64' HOT 2
- Releasing tensor causes segmentation fault error
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gotch.