Documentation ¶
Overview ¶
Package terf implements a reader/writer for TensorFlow TFRecords files
Index ¶
- func BytesFeature(val []byte) *protobuf.Feature
- func ExampleFeatureBytes(example *protobuf.Example, key string) []byte
- func ExampleFeatureBytesList(example *protobuf.Example, key string) [][]byte
- func ExampleFeatureFloat(example *protobuf.Example, key string) float64
- func ExampleFeatureFloatList(example *protobuf.Example, key string) []float32
- func ExampleFeatureInt64(example *protobuf.Example, key string) int64
- func ExampleFeatureInt64List(example *protobuf.Example, key string) []int64
- func FloatFeature(val float32) *protobuf.Feature
- func Int64Feature(val int64) *protobuf.Feature
- type Image
- func (i *Image) MarshalCSV(baseDir string) []string
- func (i *Image) MarshalExample() (*protobuf.Example, error)
- func (i *Image) Name() string
- func (i *Image) Read(r io.Reader) error
- func (i *Image) Save(file string) error
- func (i *Image) ToJPEG() error
- func (i *Image) UnmarshalCSV(row []string) error
- func (i *Image) UnmarshalExample(example *protobuf.Example) error
- func (i *Image) Write(w io.Writer) error
- type Reader
- type Writer
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BytesFeature ¶
BytesFeature is a helper function for encoding TensorFlow Example proto Bytes features
func ExampleFeatureBytes ¶
ExampleFeatureBytes is a helper function for decoding proto Bytes feature from a TensorFlow Example. If key is not found it returns default value
func ExampleFeatureBytesList ¶ added in v0.0.4
ExampleFeatureBytesList is a helper function for decoding proto Bytes feature from a TensorFlow Example. If key is not found it returns default value
func ExampleFeatureFloat ¶
ExampleFeatureFloat is a helper function for decoding proto Float feature from a TensorFlow Example. If key is not found it returns default value
func ExampleFeatureFloatList ¶ added in v0.0.4
ExampleFeatureFloatList is a helper function for decoding proto Float feature from a TensorFlow Example. If key is not found it returns default value
func ExampleFeatureInt64 ¶
ExampleFeatureInt64 is a helper function for decoding proto Int64 feature from a TensorFlow Example. If key is not found it returns default value
func ExampleFeatureInt64List ¶ added in v0.0.4
ExampleFeatureInt64List is a helper function for decoding proto Int64 feature from a TensorFlow Example. If key is not found it returns default value
func FloatFeature ¶
FloatFeature is a helper function for encoding TensorFlow Example proto Float features
func Int64Feature ¶
Int64Feature is a helper function for encoding TensorFlow Example proto Int64 features
Types ¶
type Image ¶
type Image struct { // Unique ID for the image ID int // Width in pixels of the image Width int // Height in pixels of the image Height int // Integer ID for the normalized label (class) LabelID int // Integer ID for the raw label LabelRaw int // The human-readable version of the normalized label LabelText string // Integer ID for the source of the image. This is typically the // organization or owner that created the image SourceID int // Base filename of the original image Filename string // Image format (JPEG, PNG) Format string // Image colorpace (RGB, Gray) Colorspace string // Raw image data Raw []byte }
Image is an Example image for training/validating in TensorFlow
func NewImage ¶
func NewImage(r io.Reader, id, labelID, labelRaw int, labelText, filename string, sourceID int) (*Image, error)
NewImage returns a new Image. r is the io.Reader for the raw image data, id is the unique identifier for the image, labelID is the integer identifier of the normalized label, labelRaw is the integer identifier for the raw label, labelText is the normalized label, filename is the base name of the file, and sourceID is the source that produced the image
func (*Image) MarshalCSV ¶
MarshalCSV encodes Image i into a CSV record. This is the inverse of UnmarshalCSV. The image_path will be generated based on the id of the image and the provided baseDir.
func (*Image) MarshalExample ¶
MarshalExample converts the Image to a TensorFlow Example proto. The Example proto schema is as follows:
image/height: integer, image height in pixels image/width: integer, image width in pixels image/colorspace: string, specifying the colorspace image/channels: integer, specifying the number of channels, always 3 image/class/label: integer, specifying the index in a normalized classification layer image/class/raw: integer, specifying the index in the raw (original) classification layer image/class/source: integer, specifying the index of the source (creator of the image) image/class/text: string, specifying the human-readable version of the normalized label image/format: string, specifying the format image/filename: string containing the basename of the image file image/id: integer, specifying the unique id for the image image/encoded: string, containing the raw encoded image
func (*Image) Read ¶ added in v0.0.2
Reads raw image data from r, parses image config and sets Format, Colorspace, Width and Height
func (*Image) UnmarshalCSV ¶
UnmarshalCSV decodes data from a single CSV record row into Image i. The CSV record row is expected to be in the following format:
image_path,image_id,label_id,label_text,label_raw,source
func (*Image) UnmarshalExample ¶
UnmarshalExample decodes data from a TensorFlow example proto into Image i. This is the inverse of MarshalExample.
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Reader implements a reader for TFRecords with Example protos
Example ¶
package main import ( "fmt" "io" "log" "os" "github.com/markdicksonjr/terf" ) func main() { // Open TFRecord file in, err := os.Open("train-000") if err != nil { log.Fatal(err) } defer in.Close() r := terf.NewReader(in) count := 0 for { // example will be a TensorFlow Example proto example, err := r.Next() if err == io.EOF { break } else if err != nil { log.Fatal(err) } // Do something with example id := terf.ExampleFeatureInt64(example, "image/id") labelID := terf.ExampleFeatureInt64(example, "image/class/label") labelText := string(terf.ExampleFeatureBytes(example, "image/class/text")) fmt.Printf("Image: %d Label: %s (%d)\n", id, labelText, labelID) count++ } fmt.Printf("Total records: %d\n", count) }
Output:
Example (Compressed) ¶
package main import ( "compress/zlib" "fmt" "io" "log" "os" "github.com/markdicksonjr/terf" ) func main() { // Open TFRecord file in, err := os.Open("train-000") if err != nil { log.Fatal(err) } defer in.Close() // Create new zlib Reader zin, err := zlib.NewReader(in) if err != nil { log.Fatal(err) } defer zin.Close() r := terf.NewReader(zin) count := 0 for { // example will be a TensorFlow Example proto example, err := r.Next() if err == io.EOF { break } else if err != nil { log.Fatal(err) } // Do something with example id := terf.ExampleFeatureInt64(example, "image/id") labelID := terf.ExampleFeatureInt64(example, "image/class/label") labelText := string(terf.ExampleFeatureBytes(example, "image/class/text")) fmt.Printf("Image: %d Label: %s (%d)\n", id, labelText, labelID) count++ } fmt.Printf("Total records: %d\n", count) }
Output:
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
Writer implements a writer for TFRecords with Example protos
Example ¶
package main import ( "log" "os" "github.com/markdicksonjr/terf" ) func main() { // Open output file out, err := os.Create("train-001") if err != nil { log.Fatal(err) } defer out.Close() // Create new terf Writer w := terf.NewWriter(out) // Read in image data from file reader, err := os.Open("image.jpg") if err != nil { log.Fatal(err) } defer reader.Close() // Create new terf Image with labels and source img, err := terf.NewImage(reader, 1, 12, 104, "Crystal", "image.jpg", 10) if err != nil { log.Fatal(err) } // Marshal image to Example proto example, err := img.MarshalExample() if err != nil { log.Fatal(err) } // Write Example proto err = w.Write(example) if err != nil { log.Fatal(err) } // Write any buffered data to the underlying writer w.Flush() if err := w.Error(); err != nil { log.Fatal(err) } }
Output:
Example (Compressed) ¶
package main import ( "compress/zlib" "log" "os" "github.com/markdicksonjr/terf" ) func main() { // Open output file out, err := os.Create("train-001") if err != nil { log.Fatal(err) } defer out.Close() // Create zlib writer zout := zlib.NewWriter(out) defer zout.Close() // Create new terf Writer w := terf.NewWriter(zout) // Read in image data from file reader, err := os.Open("image.jpg") if err != nil { log.Fatal(err) } defer reader.Close() // Create new terf Image with labels and source img, err := terf.NewImage(reader, 1, 12, 104, "Crystal", "image.jpg", 10) if err != nil { log.Fatal(err) } // Marshal image to Example proto example, err := img.MarshalExample() if err != nil { log.Fatal(err) } // Write Example proto err = w.Write(example) if err != nil { log.Fatal(err) } // Write any buffered data to the underlying writer w.Flush() if err := w.Error(); err != nil { log.Fatal(err) } }
Output:
func (*Writer) Error ¶ added in v0.0.2
Error reports any error that has occurred during a previous Write or Flush.