Giter VIP home page Giter VIP logo

Comments (4)

sugarme avatar sugarme commented on June 7, 2024

@jbloxsome ,

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.

jbloxsome avatar jbloxsome commented on June 7, 2024

@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.

sugarme avatar sugarme commented on June 7, 2024

@jbloxsome ,

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.

jbloxsome avatar jbloxsome commented on June 7, 2024

@sugarme

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.