-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Write model to temporary directory (#2)
- Loading branch information
Showing
6 changed files
with
96 additions
and
24 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,15 @@ go get github.com/robherley/guesslang-go | |
|
||
See example usage in [`examples/main.go`](/example/main.go) | ||
|
||
## Caveats | ||
|
||
To work around some of the limitations of the Go TensorFlow bindings (and the wrapper library)[^1], the [SavedModel](https://www.tensorflow.org/guide/saved_model) is embeded in the binary and | ||
when a [`Guesser`](https://pkg.go.dev/github.com/robherley/guesslang-go/pkg/guesser#Guesser) is initialized, it temporarily writes the model to a directory (and removes it after). | ||
|
||
So, in order to use this package, you must at least have a writeable temporary directory that aligns with Go's [`os.TempDir()`](https://pkg.go.dev/[email protected]#TempDir). | ||
|
||
[^1]: https://github.com/galeone/tfgo/issues/44#issuecomment-841806254 | ||
|
||
## Acknowledgements | ||
|
||
Powered by: | ||
|
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
File renamed without changes.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package guesser | ||
|
||
import ( | ||
"embed" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
var ( | ||
//go:embed model/* | ||
savedModel embed.FS | ||
) | ||
|
||
// as far as I know, there is no way to load a SavedModel/graph (with variables) from memory with the go tf library | ||
// https://github.com/galeone/tfgo/issues/44#issuecomment-841806254 | ||
func writeModelToTempDir() (string, error) { | ||
modelPath, err := os.MkdirTemp("", "guesslang-go") | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
savedModelFile, err := savedModel.Open("model/saved_model.pb") | ||
if err != nil { | ||
return "", err | ||
} | ||
defer savedModelFile.Close() | ||
|
||
savedModelFileDisk, err := os.Create(modelPath + "/saved_model.pb") | ||
if err != nil { | ||
return "", err | ||
} | ||
defer savedModelFileDisk.Close() | ||
|
||
if _, err := io.Copy(savedModelFileDisk, savedModelFile); err != nil { | ||
return "", err | ||
} | ||
|
||
err = os.MkdirAll(filepath.Join(modelPath, "variables"), 0755) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
variablesFiles, err := savedModel.ReadDir("model/variables") | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
for _, file := range variablesFiles { | ||
variablesFile, err := savedModel.Open("model/variables/" + file.Name()) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer variablesFile.Close() | ||
|
||
variablesFileDisk, err := os.Create(filepath.Join(modelPath, "variables", file.Name())) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer variablesFileDisk.Close() | ||
|
||
if _, err := io.Copy(variablesFileDisk, variablesFile); err != nil { | ||
return "", err | ||
} | ||
} | ||
|
||
return modelPath, nil | ||
} |
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