inceptionv3

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 1, 2023 License: Apache-2.0 Imports: 13 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Inception V3 Model

This library creates the model architecture and optionally loads the pre-trained weights from Google.

Reference: - Rethinking the Inception Architecture for Computer Vision (CVPR 2016)

Based on Keras implementation:

Documentation

Overview

Package inceptionv3 provides a pre-trained InceptionV3 model, or simply it's structure.

This library creates the model architecture and optionally loads the pre-trained weights from Google. It can be used with or without the top-layer.

Reference: - Rethinking the Inception Architecture for Computer Vision (CVPR 2016), http://arxiv.org/abs/1512.00567

Based on Keras implementation:

- Source: github.com/keras-team/keras/keras/applications/inception_v3.py(https://github.com/keras-team/keras/blob/v2.12.0/keras/applications/inception_v3.py) - Documentation: https://keras.io/api/applications/inceptionv3/

To use it, start with BuildGraph. If using the pre-trained weights, call once DownloadAndUnpackWeights -- it is a no-op if weights have already been downloaded and unpacked.

Example:

var flagDataDir = flag.String("data", "~/work/inceptionv3", "Directory where to save and load model data.")

func ModelGraph(ctx *context.Context, img *Node) *Node {
	if img.Rank() == 3 {
		// Prepend the batch dimension.
		Img = ExpandDims(img, 0)
	}
	img = inceptionv3.PreprocessImage(img, 255.0)
	embeddings := inceptionv3.BuildGraph(ctx.In("inceptionV3"), img).\
		PreTrained(*flagDataDir).SetPooling(inceptionv3.MeanPooling).Done()
	return embeddings
}

func main() {
	…
	err := inceptionv3.DownloadAndUnpackWeights(*flagDataDir)
	AssertNoError(err)
	…
	ctx := context.NewContext(manager)
	modelExec := context.NewExec(manager, ctx, ModelGraph)
	output := MustNoError(modelExec.Call(imageTensor))[0].Local()
}

Index

Constants

View Source
const (
	// WeightsURL is the URL for the whole model, including the top layer, a 1000-classes linear layer on top.
	WeightsURL = "https://storage.googleapis.com/tensorflow/keras-applications/inception_v3/inception_v3_weights_tf_dim_ordering_tf_kernels.h5"

	// WeightsH5Checksum is the SHA256 checksum of the weights file.
	WeightsH5Checksum = "00c9ea4e4762f716ac4d300d6d9c2935639cc5e4d139b5790d765dcbeea539d0"

	// WeightsH5Name is the name of the local ".h5" file with the weights.
	WeightsH5Name = "weights.h5"

	// UnpackedWeightsName is the name of the subdirectory that will hold the unpacked weights.
	UnpackedWeightsName = "gomlx_weights"
)
View Source
const ClassificationImageSize = 299

ClassificationImageSize if using the Inception V3's model for classification. The image should be 299 x 299.

Variables

This section is empty.

Functions

func DownloadAndUnpackWeights

func DownloadAndUnpackWeights(baseDir string) (err error)

DownloadAndUnpackWeights to the given baseDir. It only does the work if the files are not there yet (downloaded and unpacked).

It is verbose and uses a progressbar if downloading/unpacking. It is quiet if there is nothing to do, that is, if the files are already there.

func PathToTensor

func PathToTensor(baseDir, tensorName string) string

PathToTensor returns the path to tensorName (name within the h5 file).

func PreprocessImage

func PreprocessImage(image *Node, maxValue float64) *Node

PreprocessImage takes `image` with values from 0 to `maxValue`, and it scales it -1.0 to 1.0, the values used in the training of the InceptionV3 model. It works for batches of images also.

Types

type Config

type Config struct {
	// contains filtered or unexported fields
}

Config for instantiating an InceptionV3 model. After the configuration is set, call Done, and it will build the InceptionV3 graph with the loaded variables.

See Build to construct a Config object and a usage example.

func BuildGraph

func BuildGraph(ctx *context.Context, image *Node) *Config

BuildGraph for InceptionV3 model.

For a model with pre-trained weights, call Config.PreTrained.

It returns a Config object that can be further configured. Once the configuration is finished, call `Done` and it will return the embedding (or classification) of the given image.

See example in the package inceptionv3 documentation.

Parameters:

  • ctx: context.Context where variables are created and loaded. Variables will be re-used if they were already created before in the current scope. That means one can call BuildGraph more than once, and have the same model be used for more than one input -- for instance, for 2-tower models. To instantiate more than one model with different weights, just use the context in a different scope.
  • Image: image tensor (`*Node`) on which to apply the model. There must be 3 channels, and they must be scaled from -1.0 to 1.0 -- see PreprocessImage to scale image accordingly if needed. If using ClassificationTop(true), the images must be of size 299x299 (defined as a constant `ClassificationImageSize`).

The original model has weights in `shapes.F32`. (TODO: If the image has a different `DType`, it will try to convert the weights and work the model fully on the image's `DType`. This hasn't been extensively tested, so no guarantees of quality.)

The implementation follows closely the definition in https://github.com/keras-team/keras/blob/v2.12.0/keras/applications/inception_v3.py

func (*Config) ChannelsAxis

func (cfg *Config) ChannelsAxis(channelsAxisConfig timage.ChannelsAxisConfig) *Config

ChannelsAxis configures the axis for the channels (aka. "depth" or "features") dimension. The default is `timage.ChannelsLast`, meaning the "channels" dimension comes last.

Note: `timage` refers to package `github.com/gomlx/gomlx/types/tensor/image`.

It returns the modified Config object, so calls can be cascaded.

func (*Config) ClassificationTop

func (cfg *Config) ClassificationTop(useTop bool) *Config

ClassificationTop configures whether to use the very top classification layer at the top of the model.

Typically, if using only the embeddings, set this to false. If actually classifying Inception images, you can set this to true, and it will include a last linear layer, and it will return the logits layer for each of the Inception 1000 classes.

This is only useful if PreTrained weights are configured.

It returns the modified Config object, so calls can be cascaded.

func (*Config) Done

func (cfg *Config) Done() (output *Node)

Done builds the graph based on the configuration set.

func (*Config) PreTrained

func (cfg *Config) PreTrained(baseDir string) *Config

PreTrained configures the graph to load the pre-trained weights. It takes as an argument `baseDir`, the directory where the weights have been downloaded with DownloadAndUnpackWeights -- use the same value used there.

The default is not to use the pre-trained weights, which will build an untrained InceptionV3 graph.

It returns the modified Config object, so calls can be cascaded.

func (*Config) SetPooling

func (cfg *Config) SetPooling(pooling Pooling) *Config

SetPooling configures whether to use a MaxPool at the very top of the model.

If set to NoPooling, the default, it returns a 4D tensor, with 2048 channels (see ChannelsAxis for order of axis). If set to MaxPooling or MeanPooling, it will pool the last spatial dimensions, either using Max or Mean.

This is only used if not using ClassificationTop.

It returns the modified Config object, so calls can be cascaded.

func (*Config) Trainable

func (cfg *Config) Trainable(trainable bool) *Config

Trainable configures whether the variables created will be set as trainable or not -- see `context.Variable`.

If using pre-trained weights as frozen values, set this to false -- and considering using `StopGradient()` on the value returned by Done, to prevent any gradients from even propagating. It's an error to configure this to false if not using pre-trained weights (see PreTrained).

The default is true, which allows for fine-tuning of the InceptionV3 model.

It returns the modified Config object, so calls can be cascaded.

type Pooling

type Pooling int

Pooling to be used at the top of the model

const (
	NoPooling Pooling = iota
	MaxPooling
	MeanPooling
)

func (Pooling) String

func (i Pooling) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL