export AugmentOption and correct ColorJitter output dtype

This commit is contained in:
sugarme 2021-07-20 13:58:45 +10:00
parent c89e4b3ba1
commit 4fd5c5059d
11 changed files with 67 additions and 48 deletions

View File

@ -21,10 +21,16 @@ before_install:
- export LIBRARY_PATH=${LIBTORCH}/lib
- export CPATH=${LIBTORCH}/lib:${LIBTORCH}/include:${LIBTORCH}/include/torch/csrc:${LIBTORCH}/include/torch/csrc/api/include
- export LD_LIBRARY_PATH=${LIBTORCH}/lib
- export CUDA_VER=cpu &&
wget https://raw.githubusercontent.com/sugarme/gotch/master/setup-libtorch.sh
chmod +x setup-libtorch.sh
# Precompiled libtorch
- sudo rm -rf $LIBTORCH
- sudo mkdir -p $LIBTORCH
- bash setup-libtorch.sh
- wget -O /tmp/libtorch-cxx11-abi-shared-with-deps-${LIBTORCH_VERSION}+cpu.zip https://download.pytorch.org/libtorch/cpu/libtorch-cxx11-abi-shared-with-deps-${LIBTORCH_VERSION}%2Bcpu.zip
- sudo unzip /tmp/libtorch-cxx11-abi-shared-with-deps-${LIBTORCH_VERSION}+cpu.zip -d ${GOTCH}/libtch
- sudo rm ${GOTCH}/libtch/lib.go

View File

@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Changed `dutil.DataLoader.Reset()` to reshuffle when resetting DataLoader if flag is true
- Changed `dutil.DataLoader.Next()`. Deleted case batch size == 1 to make consistency by always returning items in a slice `[]element dtype` even with batchsize = 1.
- Added `nn.CrossEntropyLoss` and `nn.BCELoss`
- Fixed `tensor.ForwardIs` return `Tuple` and `TensorList` instead of always returning `TensorList`
- Changed exporting augment options and make ColorJitter forward output dtype `uint8` for chaining with other augment options.
## [Nofix]
- ctype `long` caused compiling error in MacOS as noted on [#44]. Not working on linux box.

View File

@ -528,11 +528,19 @@ func IValueFromC(cval *CIValue) (*IValue, error) {
vals = append(vals, Tensor{v.Value().(lib.Ctensor)})
}
return &IValue{
value: vals,
kind: TensorListVal,
name: "TensorList",
}, nil
if len == 2 {
return &IValue{
value: vals,
kind: TensorListVal,
name: "Tuple",
}, nil
} else {
return &IValue{
value: vals,
kind: TensorListVal,
name: "TensorList",
}, nil
}
case "IntList":
var vals []int64
for _, civalue := range civalues {

View File

@ -110,7 +110,7 @@ func (ra *RandomAffine) Forward(x *ts.Tensor) *ts.Tensor {
return bx
}
func newRandomAffine(opts ...affineOption) *RandomAffine {
func newRandomAffine(opts ...AffineOption) *RandomAffine {
p := defaultAffineOptions()
for _, o := range opts {
o(p)
@ -135,7 +135,7 @@ type affineOptions struct {
fillValue []float64
}
type affineOption func(*affineOptions)
type AffineOption func(*affineOptions)
func defaultAffineOptions() *affineOptions {
return &affineOptions{
@ -148,43 +148,43 @@ func defaultAffineOptions() *affineOptions {
}
}
func WithAffineDegree(degree []int64) affineOption {
func WithAffineDegree(degree []int64) AffineOption {
return func(o *affineOptions) {
o.degree = degree
}
}
func WithAffineTranslate(translate []float64) affineOption {
func WithAffineTranslate(translate []float64) AffineOption {
return func(o *affineOptions) {
o.translate = translate
}
}
func WithAffineScale(scale []float64) affineOption {
func WithAffineScale(scale []float64) AffineOption {
return func(o *affineOptions) {
o.scale = scale
}
}
func WithAffineShear(shear []float64) affineOption {
func WithAffineShear(shear []float64) AffineOption {
return func(o *affineOptions) {
o.shear = shear
}
}
func WithAffineMode(mode string) affineOption {
func WithAffineMode(mode string) AffineOption {
return func(o *affineOptions) {
o.interpolationMode = mode
}
}
func WithAffineFillValue(fillValue []float64) affineOption {
func WithAffineFillValue(fillValue []float64) AffineOption {
return func(o *affineOptions) {
o.fillValue = fillValue
}
}
func WithRandomAffine(opts ...affineOption) Option {
func WithRandomAffine(opts ...AffineOption) Option {
ra := newRandomAffine(opts...)
return func(o *Options) {
o.randomAffine = ra

View File

@ -222,5 +222,8 @@ func (c *ColorJitter) Forward(x *ts.Tensor) *ts.Tensor {
sOut.MustDrop()
}
return hOut
bx := Float2ByteImage(hOut)
hOut.MustDrop()
return bx
}

View File

@ -35,7 +35,7 @@ type cutoutOptions struct {
rgbVal []int64 // RGB value
}
type cutoutOption func(o *cutoutOptions)
type CutoutOption func(o *cutoutOptions)
func defaultCutoutOptions() *cutoutOptions {
return &cutoutOptions{
@ -55,7 +55,7 @@ func newRandomCutout(pvalue float64, scale, ratio []float64, rgbVal []int64) *Ra
}
}
func WithCutoutPvalue(p float64) cutoutOption {
func WithCutoutPvalue(p float64) CutoutOption {
if p < 0 || p > 1 {
log.Fatalf("Cutout p-value must be in range from 0 to 1. Got %v\n", p)
}
@ -64,7 +64,7 @@ func WithCutoutPvalue(p float64) cutoutOption {
}
}
func WithCutoutScale(scale []float64) cutoutOption {
func WithCutoutScale(scale []float64) CutoutOption {
if len(scale) != 2 {
log.Fatalf("Cutout scale should be in a range of 2 elments. Got %v elements\n", len(scale))
}
@ -73,7 +73,7 @@ func WithCutoutScale(scale []float64) cutoutOption {
}
}
func WithCutoutRatio(ratio []float64) cutoutOption {
func WithCutoutRatio(ratio []float64) CutoutOption {
if len(ratio) != 2 {
log.Fatalf("Cutout ratio should be in a range of 2 elments. Got %v elements\n", len(ratio))
}
@ -82,7 +82,7 @@ func WithCutoutRatio(ratio []float64) cutoutOption {
}
}
func WithCutoutValue(rgb []int64) cutoutOption {
func WithCutoutValue(rgb []int64) CutoutOption {
var rgbVal []int64
switch len(rgb) {
case 1:
@ -169,7 +169,7 @@ func (rc *RandomCutout) Forward(img *ts.Tensor) *ts.Tensor {
return bx
}
func WithRandomCutout(opts ...cutoutOption) Option {
func WithRandomCutout(opts ...CutoutOption) Option {
params := defaultCutoutOptions()
for _, o := range opts {
o(params)

View File

@ -24,7 +24,7 @@ type normalizeOptions struct {
std []float64
}
type normalizeOption func(*normalizeOptions)
type NormalizeOption func(*normalizeOptions)
// Mean and SD can be calculated for specific dataset as follow:
/*
@ -54,19 +54,19 @@ func defaultNormalizeOptions() *normalizeOptions {
}
}
func WithNormalizeStd(std []float64) normalizeOption {
func WithNormalizeStd(std []float64) NormalizeOption {
return func(o *normalizeOptions) {
o.std = std
}
}
func WithNormalizeMean(mean []float64) normalizeOption {
func WithNormalizeMean(mean []float64) NormalizeOption {
return func(o *normalizeOptions) {
o.mean = mean
}
}
func newNormalize(opts ...normalizeOption) *Normalize {
func newNormalize(opts ...NormalizeOption) *Normalize {
p := defaultNormalizeOptions()
for _, o := range opts {
o(p)
@ -90,7 +90,7 @@ func (n *Normalize) Forward(x *ts.Tensor) *ts.Tensor {
return bx
}
func WithNormalize(opts ...normalizeOption) Option {
func WithNormalize(opts ...NormalizeOption) Option {
n := newNormalize(opts...)
return func(o *Options) {
o.normalize = n

View File

@ -43,33 +43,33 @@ func defaultPerspectiveOptions() *perspectiveOptions {
}
}
type perspectiveOption func(*perspectiveOptions)
type PerspectiveOption func(*perspectiveOptions)
func WithPerspectivePvalue(p float64) perspectiveOption {
func WithPerspectivePvalue(p float64) PerspectiveOption {
return func(o *perspectiveOptions) {
o.pvalue = p
}
}
func WithPerspectiveScale(s float64) perspectiveOption {
func WithPerspectiveScale(s float64) PerspectiveOption {
return func(o *perspectiveOptions) {
o.distortionScale = s
}
}
func WithPerspectiveMode(m string) perspectiveOption {
func WithPerspectiveMode(m string) PerspectiveOption {
return func(o *perspectiveOptions) {
o.interpolationMode = m
}
}
func WithPerspectiveValue(v []float64) perspectiveOption {
func WithPerspectiveValue(v []float64) PerspectiveOption {
return func(o *perspectiveOptions) {
o.fillValue = v
}
}
func newRandomPerspective(opts ...perspectiveOption) *RandomPerspective {
func newRandomPerspective(opts ...PerspectiveOption) *RandomPerspective {
params := defaultPerspectiveOptions()
for _, opt := range opts {
opt(params)
@ -189,7 +189,7 @@ func (rp *RandomPerspective) Forward(x *ts.Tensor) *ts.Tensor {
return bx
}
func WithRandomPerspective(opts ...perspectiveOption) Option {
func WithRandomPerspective(opts ...PerspectiveOption) Option {
rp := newRandomPerspective(opts...)
return func(o *Options) {
o.randomPerspective = rp

View File

@ -21,7 +21,7 @@ type posterizeOptions struct {
bits uint8
}
type posterizeOption func(*posterizeOptions)
type PosterizeOption func(*posterizeOptions)
func defaultPosterizeOptions() *posterizeOptions {
return &posterizeOptions{
@ -30,19 +30,19 @@ func defaultPosterizeOptions() *posterizeOptions {
}
}
func WithPosterizePvalue(p float64) posterizeOption {
func WithPosterizePvalue(p float64) PosterizeOption {
return func(o *posterizeOptions) {
o.pvalue = p
}
}
func WithPosterizeBits(bits uint8) posterizeOption {
func WithPosterizeBits(bits uint8) PosterizeOption {
return func(o *posterizeOptions) {
o.bits = bits
}
}
func newRandomPosterize(opts ...posterizeOption) *RandomPosterize {
func newRandomPosterize(opts ...PosterizeOption) *RandomPosterize {
p := defaultPosterizeOptions()
for _, o := range opts {
o(p)
@ -68,7 +68,7 @@ func (rp *RandomPosterize) Forward(x *ts.Tensor) *ts.Tensor {
return out
}
func WithRandomPosterize(opts ...posterizeOption) Option {
func WithRandomPosterize(opts ...PosterizeOption) Option {
rp := newRandomPosterize(opts...)
return func(o *Options) {

View File

@ -21,7 +21,7 @@ type sharpnessOptions struct {
pvalue float64
}
type sharpnessOption func(*sharpnessOptions)
type SharpnessOption func(*sharpnessOptions)
func defaultSharpnessOptions() *sharpnessOptions {
return &sharpnessOptions{
@ -30,19 +30,19 @@ func defaultSharpnessOptions() *sharpnessOptions {
}
}
func WithSharpnessPvalue(p float64) sharpnessOption {
func WithSharpnessPvalue(p float64) SharpnessOption {
return func(o *sharpnessOptions) {
o.pvalue = p
}
}
func WithSharpnessFactor(f float64) sharpnessOption {
func WithSharpnessFactor(f float64) SharpnessOption {
return func(o *sharpnessOptions) {
o.sharpnessFactor = f
}
}
func newRandomAdjustSharpness(opts ...sharpnessOption) *RandomAdjustSharpness {
func newRandomAdjustSharpness(opts ...SharpnessOption) *RandomAdjustSharpness {
p := defaultSharpnessOptions()
for _, o := range opts {
o(p)
@ -67,7 +67,7 @@ func (ras *RandomAdjustSharpness) Forward(x *ts.Tensor) *ts.Tensor {
return out
}
func WithRandomAdjustSharpness(opts ...sharpnessOption) Option {
func WithRandomAdjustSharpness(opts ...SharpnessOption) Option {
ras := newRandomAdjustSharpness(opts...)
return func(o *Options) {
o.randomAdjustSharpness = ras

View File

@ -22,7 +22,7 @@ type solarizeOptions struct {
pvalue float64
}
type solarizeOption func(*solarizeOptions)
type SolarizeOption func(*solarizeOptions)
func defaultSolarizeOptions() *solarizeOptions {
return &solarizeOptions{
@ -31,19 +31,19 @@ func defaultSolarizeOptions() *solarizeOptions {
}
}
func WithSolarizePvalue(p float64) solarizeOption {
func WithSolarizePvalue(p float64) SolarizeOption {
return func(o *solarizeOptions) {
o.pvalue = p
}
}
func WithSolarizeThreshold(th float64) solarizeOption {
func WithSolarizeThreshold(th float64) SolarizeOption {
return func(o *solarizeOptions) {
o.threshold = th
}
}
func newRandomSolarize(opts ...solarizeOption) *RandomSolarize {
func newRandomSolarize(opts ...SolarizeOption) *RandomSolarize {
params := defaultSolarizeOptions()
for _, o := range opts {
@ -75,7 +75,7 @@ func (rs *RandomSolarize) Forward(x *ts.Tensor) *ts.Tensor {
return bx
}
func WithRandomSolarize(opts ...solarizeOption) Option {
func WithRandomSolarize(opts ...SolarizeOption) Option {
rs := newRandomSolarize(opts...)
return func(o *Options) {