Comments (4)
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:
- tfgo.image test: https://github.com/galeone/tfgo/blob/master/image/image_test.go
- godoc of tensorflow.op https://godoc.org/github.com/tensorflow/tensorflow/tensorflow/go/op
Let me know if this help you
from tfgo.
I'm also trying to load an image and feed it to a model, but met some issues:
- First I use
img.Value()
as an input, but met errorcannot 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()})
- Then I use
img.Tensor
as an input, but another errorcannot 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})
- 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.
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.
Thanks, and it helps. :)
from tfgo.
Related Issues (20)
- loading model HOT 2
- cc1.exe: sorry, unimplemented: 64-bit mode not compiled in HOT 1
- Error: W tensorflow/core/framework/op_kernel.cc:1745] OP_REQUIRES failed at lookup_table_op.cc:929 : FAILED_PRECONDITION: Table not initialized. HOT 1
- SparseTensor support HOT 2
- Failed to go get tfgo HOT 3
- tensor example support HOT 6
- Potential memory leak on reloading model HOT 8
- libtensorflow.so: .dynsym local symbol at index 3 (>= sh_info of 3) HOT 2
- Matching between output tensor name and prediction HOT 7
- Thread safety HOT 2
- Panic vs Error HOT 2
- Reading jpg image from disk and passing as input to a pretrained model loaded from disk HOT 4
- Error building "Getting Started" example: rebuild TensorFlow with the appropriate compiler flags HOT 5
- Can I use tfgo for tensorflow 2.11.x? HOT 2
- Convert opencv's `mat` to tfgo's `tensor` HOT 7
- Dark Souls level frustration with compilation project with this library HOT 1
- Discrepancy between python and go outputs HOT 2
- tfgo Object detection
- Choose output layer of model . HOT 1
- Panic when loading a keras LSTM Model HOT 2
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 tfgo.