Code Monkey home page Code Monkey logo

Comments (4)

galeone avatar galeone commented on August 20, 2024 2
package main

import (
        tg "github.com/galeone/tfgo"
        "github.com/galeone/tfgo/image"
        tf "github.com/tensorflow/tensorflow/tensorflow/go"
       "github.com/tensorflow/tensorflow/tensorflow/go/op"
)
func main() {
        root := tg.NewRoot()
        img := image.Read(root, "path_of_your_image.extension", 3) //shape 59, 199, 3
        img = img.RGBToGrayscale() // shape 59,199,1
        // resize using bilinear interpolation
        img = img.ResizeArea(image.Size{Height: 32, Width: 100})
        imageTensor := img.Value()
        // the tfgo.image package doesn't offer the reshape op, but tensorflow.op package does
        shape := tg.NewTensor(root, tg.Const(root, [3]int32{100, 32, 1}))
        imageTensor = op.Reshape(root, imageTensor, shape)
        // converto to float -> use the tfgo.image package
        newImage := image.NewImage(root, imageTensor)
        floatImage := newImage.ConvertDType(tf.float32, true)
        // extract the tensorflow tensor from the floatImage and you have the input to use in your model
        inputimage := floatImage.Value()
        // [.. other code]
}

I haven't tested it (I'm away from my computer right now) but at least it should give you an idea on how to do what you need.

Here some reference you might found useful:

Let me know if this help you

from tfgo.

leondgarse avatar leondgarse commented on August 20, 2024

I'm also trying to load an image and feed it to a model, but met some issues:

  1. First I use img.Value() as an input, but met error cannot use img.Value() <github.com/tensorflow/tensorflow/tensorflow/go.Output> as type <*github.com/tensorflow/tensorflow/tensorflow/go.Tensor> in map value
root := tg.NewRoot()
img := image.Read(root, "path/to/image.jpg", 3)
results := model.Exec([]tf.Output{model.Op("the_output", 0)},
        map[tf.Output]*tf.Tensor{model.Op("the_input", 0): img.Value()})
  1. Then I use img.Tensor as an input, but another error cannot use img.Tensor <*github.com/galeone/tfgo.Tensor> as type <*github.com/tensorflow/tensorflow/tensorflow/go.Tensor> in map value
root := tg.NewRoot()
img := image.Read(root, "path/to/image.jpg", 3)
results := model.Exec([]tf.Output{model.Op("the_output", 0)},
        map[tf.Output]*tf.Tensor{model.Op("the_input", 0): img.Tensor})
  1. The only way I found success is converting it to a tf.Tensor first, but looks ugly...
root := tg.NewRoot()
img := image.Read(root, "path/to/image.jpg", 3)
results := tg.Exec(root, []tf.Output{img.Value()}, nil, &tf.SessionOptions{})
A, _ := tf.NewTensor(results[0].Value())

results := model.Exec([]tf.Output{model.Op("the_output", 0)},
        map[tf.Output]*tf.Tensor{model.Op("the_input", 0): A})

So is this the right way?

from tfgo.

galeone avatar galeone commented on August 20, 2024

Yes, although ugly this is a correct way. Currently, I don't remember if this is the only way for doing this, but I'm pretty sure I used the very same way before (and it works)

from tfgo.

leondgarse avatar leondgarse commented on August 20, 2024

Thanks, and it helps. :)

from tfgo.

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.