Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactors onnx import adding better support for image functions #9466

Merged
merged 12 commits into from
Oct 6, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ fun SDImage() = Namespace("Image"){
Arg(BOOL, "preserveAspectRatio") { description = "Whether to preserve the aspect ratio." +
" If this is set, then images will be resized to a size that fits in size while preserving the aspect ratio" +
" of the original image. Scales up the image if size is bigger than the current size of the image. Defaults to False."; defaultValue=false; }
Arg(BOOL, "antialis") { description = "Whether to use an anti-aliasing filter when downsampling an image"; defaultValue=false; }
Arg(BOOL, "antialias") { description = "Whether to use an anti-aliasing filter when downsampling an image"; defaultValue=false; }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@treo typo fix

Arg(ENUM, "ImageResizeMethod") { possibleValues = listOf( "ResizeBilinear", "ResizeBicubic", "ResizeNearest", "ResizeGaussian",
"ResizeLanczos5", "ResizeMitchelcubic", "ResizeArea"); description = "ResizeBilinear: Bilinear interpolation. If 'antialias' is true, becomes a hat/tent filter function with radius 1 when downsampling.\n" +
"ResizeLanczos5: Lanczos kernel with radius 5. Very-high-quality filter but may have stronger ringing.\n" +
Expand All @@ -259,4 +259,36 @@ fun SDImage() = Namespace("Image"){
""".trimIndent()
}
}

Op("resizeBiLinear") {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@treo new op for bi linear image resize.

javaPackage = "org.nd4j.linalg.api.ops.impl.image"
javaOpClass = "ResizeBilinear"
Input(NUMERIC,"input") { description = "4D image"}
Arg(INT ,"height") { description = "target height for resizing to "}
Arg(INT ,"width") { description = "target width for resizing to"}
Arg(BOOL ,"alignCorners") { description = "whether to align corners during resizing. Images are aligned to preserve corners."}
Arg(BOOL,"halfPixelCenters") { description = "When resizing, assumes pixels are centered at 0.5."}
Output(NUMERIC, "output"){ description = "Output image" }
Doc(Language.ANY, DocScope.ALL){
"""
Resize images to size using the specified method.
""".trimIndent()
}
}

Op("resizeBiCubic") {
javaPackage = "org.nd4j.linalg.api.ops.impl.image"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@treo same for bicubic

javaOpClass = "ResizeBicubic"
Input(NUMERIC,"input") { description = "4D image"}
Input(INT ,"size") { description = "the target size to resize to "}
Arg(BOOL ,"alignCorners") { description = "whether to align corners during resizing. Images are aligned to preserve corners."}
Arg(BOOL,"alignPixelCenters") { description = "When resizing, assumes pixels are centered at 0.5."}
Output(NUMERIC, "output"){ description = "Output image" }
Doc(Language.ANY, DocScope.ALL){
"""
Resize images to size using the specified method.
""".trimIndent()
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ public SDVariable hsvToRgb(String name, SDVariable input) {
* @param input 4D image [NHWC] (NUMERIC type)
* @param size new height and width (INT type)
* @param preserveAspectRatio Whether to preserve the aspect ratio. If this is set, then images will be resized to a size that fits in size while preserving the aspect ratio of the original image. Scales up the image if size is bigger than the current size of the image. Defaults to False.
* @param antialis Whether to use an anti-aliasing filter when downsampling an image
* @param antialias Whether to use an anti-aliasing filter when downsampling an image
* @param ImageResizeMethod ResizeBilinear: Bilinear interpolation. If 'antialias' is true, becomes a hat/tent filter function with radius 1 when downsampling.
* ResizeLanczos5: Lanczos kernel with radius 5. Very-high-quality filter but may have stronger ringing.
* ResizeBicubic: Cubic interpolant of Keys. Equivalent to Catmull-Rom kernel. Reasonably good quality and faster than Lanczos3Kernel, particularly when upsampling.
Expand All @@ -272,10 +272,10 @@ public SDVariable hsvToRgb(String name, SDVariable input) {
* @return output Output image (NUMERIC type)
*/
public SDVariable imageResize(SDVariable input, SDVariable size, boolean preserveAspectRatio,
boolean antialis, ImageResizeMethod ImageResizeMethod) {
boolean antialias, ImageResizeMethod ImageResizeMethod) {
SDValidation.validateNumerical("imageResize", "input", input);
SDValidation.validateInteger("imageResize", "size", size);
return new org.nd4j.linalg.api.ops.impl.image.ImageResize(sd,input, size, preserveAspectRatio, antialis, ImageResizeMethod).outputVariable();
return new org.nd4j.linalg.api.ops.impl.image.ImageResize(sd,input, size, preserveAspectRatio, antialias, ImageResizeMethod).outputVariable();
}

/**
Expand All @@ -285,7 +285,7 @@ public SDVariable imageResize(SDVariable input, SDVariable size, boolean preserv
* @param input 4D image [NHWC] (NUMERIC type)
* @param size new height and width (INT type)
* @param preserveAspectRatio Whether to preserve the aspect ratio. If this is set, then images will be resized to a size that fits in size while preserving the aspect ratio of the original image. Scales up the image if size is bigger than the current size of the image. Defaults to False.
* @param antialis Whether to use an anti-aliasing filter when downsampling an image
* @param antialias Whether to use an anti-aliasing filter when downsampling an image
* @param ImageResizeMethod ResizeBilinear: Bilinear interpolation. If 'antialias' is true, becomes a hat/tent filter function with radius 1 when downsampling.
* ResizeLanczos5: Lanczos kernel with radius 5. Very-high-quality filter but may have stronger ringing.
* ResizeBicubic: Cubic interpolant of Keys. Equivalent to Catmull-Rom kernel. Reasonably good quality and faster than Lanczos3Kernel, particularly when upsampling.
Expand All @@ -296,10 +296,10 @@ public SDVariable imageResize(SDVariable input, SDVariable size, boolean preserv
* @return output Output image (NUMERIC type)
*/
public SDVariable imageResize(String name, SDVariable input, SDVariable size,
boolean preserveAspectRatio, boolean antialis, ImageResizeMethod ImageResizeMethod) {
boolean preserveAspectRatio, boolean antialias, ImageResizeMethod ImageResizeMethod) {
SDValidation.validateNumerical("imageResize", "input", input);
SDValidation.validateInteger("imageResize", "size", size);
SDVariable out = new org.nd4j.linalg.api.ops.impl.image.ImageResize(sd,input, size, preserveAspectRatio, antialis, ImageResizeMethod).outputVariable();
SDVariable out = new org.nd4j.linalg.api.ops.impl.image.ImageResize(sd,input, size, preserveAspectRatio, antialias, ImageResizeMethod).outputVariable();
return sd.updateVariableNameAndReference(out, name);
}

Expand Down Expand Up @@ -411,6 +411,74 @@ public SDVariable randomCrop(String name, SDVariable input, SDVariable shape) {
return sd.updateVariableNameAndReference(out, name);
}

/**
* Resize images to size using the specified method.<br>
*
* @param input 4D image (NUMERIC type)
* @param size the target size to resize to (INT type)
* @param alignCorners whether to align corners during resizing. Images are aligned to preserve corners.
* @param alignPixelCenters When resizing, assumes pixels are centered at 0.5.
* @return output Output image (NUMERIC type)
*/
public SDVariable resizeBiCubic(SDVariable input, SDVariable size, boolean alignCorners,
boolean alignPixelCenters) {
SDValidation.validateNumerical("resizeBiCubic", "input", input);
SDValidation.validateInteger("resizeBiCubic", "size", size);
return new org.nd4j.linalg.api.ops.impl.image.ResizeBicubic(sd,input, size, alignCorners, alignPixelCenters).outputVariable();
}

/**
* Resize images to size using the specified method.<br>
*
* @param name name May be null. Name for the output variable
* @param input 4D image (NUMERIC type)
* @param size the target size to resize to (INT type)
* @param alignCorners whether to align corners during resizing. Images are aligned to preserve corners.
* @param alignPixelCenters When resizing, assumes pixels are centered at 0.5.
* @return output Output image (NUMERIC type)
*/
public SDVariable resizeBiCubic(String name, SDVariable input, SDVariable size,
boolean alignCorners, boolean alignPixelCenters) {
SDValidation.validateNumerical("resizeBiCubic", "input", input);
SDValidation.validateInteger("resizeBiCubic", "size", size);
SDVariable out = new org.nd4j.linalg.api.ops.impl.image.ResizeBicubic(sd,input, size, alignCorners, alignPixelCenters).outputVariable();
return sd.updateVariableNameAndReference(out, name);
}

/**
* Resize images to size using the specified method.<br>
*
* @param input 4D image (NUMERIC type)
* @param height target height for resizing to
* @param width target width for resizing to
* @param alignCorners whether to align corners during resizing. Images are aligned to preserve corners.
* @param halfPixelCenters When resizing, assumes pixels are centered at 0.5.
* @return output Output image (NUMERIC type)
*/
public SDVariable resizeBiLinear(SDVariable input, int height, int width, boolean alignCorners,
boolean halfPixelCenters) {
SDValidation.validateNumerical("resizeBiLinear", "input", input);
return new org.nd4j.linalg.api.ops.impl.image.ResizeBilinear(sd,input, height, width, alignCorners, halfPixelCenters).outputVariable();
}

/**
* Resize images to size using the specified method.<br>
*
* @param name name May be null. Name for the output variable
* @param input 4D image (NUMERIC type)
* @param height target height for resizing to
* @param width target width for resizing to
* @param alignCorners whether to align corners during resizing. Images are aligned to preserve corners.
* @param halfPixelCenters When resizing, assumes pixels are centered at 0.5.
* @return output Output image (NUMERIC type)
*/
public SDVariable resizeBiLinear(String name, SDVariable input, int height, int width,
boolean alignCorners, boolean halfPixelCenters) {
SDValidation.validateNumerical("resizeBiLinear", "input", input);
SDVariable out = new org.nd4j.linalg.api.ops.impl.image.ResizeBilinear(sd,input, height, width, alignCorners, halfPixelCenters).outputVariable();
return sd.updateVariableNameAndReference(out, name);
}

/**
* Converting array from HSV to RGB format<br>
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ public void initFromTensorFlow(NodeDef nodeDef, SameDiff initWith, Map<String, A
public List<DataType> calculateOutputDataTypes(List<DataType> inputDataTypes){
Preconditions.checkState(inputDataTypes != null && (inputDataTypes.size() == 1 || inputDataTypes.size() == 2),
"Expected 1 or 2 input datatypes for %s, got %s", getClass(), inputDataTypes);
if(inputDataTypes.get(0).isFPType())
return Collections.singletonList(inputDataTypes.get(0));
return Collections.singletonList(Nd4j.defaultFloatingPointType());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public INDArray hsvToRgb(INDArray input) {
* @param input 4D image [NHWC] (NUMERIC type)
* @param size new height and width (INT type)
* @param preserveAspectRatio Whether to preserve the aspect ratio. If this is set, then images will be resized to a size that fits in size while preserving the aspect ratio of the original image. Scales up the image if size is bigger than the current size of the image. Defaults to False.
* @param antialis Whether to use an anti-aliasing filter when downsampling an image
* @param antialias Whether to use an anti-aliasing filter when downsampling an image
* @param ImageResizeMethod ResizeBilinear: Bilinear interpolation. If 'antialias' is true, becomes a hat/tent filter function with radius 1 when downsampling.
* ResizeLanczos5: Lanczos kernel with radius 5. Very-high-quality filter but may have stronger ringing.
* ResizeBicubic: Cubic interpolant of Keys. Equivalent to Catmull-Rom kernel. Reasonably good quality and faster than Lanczos3Kernel, particularly when upsampling.
Expand All @@ -152,10 +152,10 @@ public INDArray hsvToRgb(INDArray input) {
* @return output Output image (NUMERIC type)
*/
public INDArray imageResize(INDArray input, INDArray size, boolean preserveAspectRatio,
boolean antialis, ImageResizeMethod ImageResizeMethod) {
boolean antialias, ImageResizeMethod ImageResizeMethod) {
NDValidation.validateNumerical("imageResize", "input", input);
NDValidation.validateInteger("imageResize", "size", size);
return Nd4j.exec(new org.nd4j.linalg.api.ops.impl.image.ImageResize(input, size, preserveAspectRatio, antialis, ImageResizeMethod))[0];
return Nd4j.exec(new org.nd4j.linalg.api.ops.impl.image.ImageResize(input, size, preserveAspectRatio, antialias, ImageResizeMethod))[0];
}

/**
Expand Down Expand Up @@ -208,6 +208,38 @@ public INDArray randomCrop(INDArray input, INDArray shape) {
return Nd4j.exec(new org.nd4j.linalg.api.ops.custom.RandomCrop(input, shape))[0];
}

/**
* Resize images to size using the specified method.<br>
*
* @param input 4D image (NUMERIC type)
* @param size the target size to resize to (INT type)
* @param alignCorners whether to align corners during resizing. Images are aligned to preserve corners.
* @param alignPixelCenters When resizing, assumes pixels are centered at 0.5.
* @return output Output image (NUMERIC type)
*/
public INDArray resizeBiCubic(INDArray input, INDArray size, boolean alignCorners,
boolean alignPixelCenters) {
NDValidation.validateNumerical("resizeBiCubic", "input", input);
NDValidation.validateInteger("resizeBiCubic", "size", size);
return Nd4j.exec(new org.nd4j.linalg.api.ops.impl.image.ResizeBicubic(input, size, alignCorners, alignPixelCenters))[0];
}

/**
* Resize images to size using the specified method.<br>
*
* @param input 4D image (NUMERIC type)
* @param height target height for resizing to
* @param width target width for resizing to
* @param alignCorners whether to align corners during resizing. Images are aligned to preserve corners.
* @param halfPixelCenters When resizing, assumes pixels are centered at 0.5.
* @return output Output image (NUMERIC type)
*/
public INDArray resizeBiLinear(INDArray input, int height, int width, boolean alignCorners,
boolean halfPixelCenters) {
NDValidation.validateNumerical("resizeBiLinear", "input", input);
return Nd4j.exec(new org.nd4j.linalg.api.ops.impl.image.ResizeBilinear(input, height, width, alignCorners, halfPixelCenters))[0];
}

/**
* Converting array from HSV to RGB format<br>
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,8 @@ val reshape = OnnxMappingProcess(

//TODO: ReduceSumSquare
//TODO: Resize PRIORITIZE
//for mapping indices see: https://github.com/eclipse/deeplearning4j/blob/228f6cda30e27999f0fea74badc8d98ee8fb0647/nd4j/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/enums/ImageResizeMethod.java#L29

//TODO: ReverseSequence
//TODO: RoiAlign
//TODO: SVMClassifier
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
/*
* ******************************************************************************
* *
* *
* * This program and the accompanying materials are made available under the
* * terms of the Apache License, Version 2.0 which is available at
* * https://www.apache.org/licenses/LICENSE-2.0.
* *
* * See the NOTICE file distributed with this work for additional
* * information regarding copyright ownership.
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* * License for the specific language governing permissions and limitations
* * under the License.
* *
* * SPDX-License-Identifier: Apache-2.0
* *****************************************************************************
*/
package org.nd4j.samediff.frameworkimport.onnx.definitions.implementations

import org.nd4j.autodiff.samediff.SameDiff
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
/*
* ******************************************************************************
* *
* *
* * This program and the accompanying materials are made available under the
* * terms of the Apache License, Version 2.0 which is available at
* * https://www.apache.org/licenses/LICENSE-2.0.
* *
* * See the NOTICE file distributed with this work for additional
* * information regarding copyright ownership.
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* * License for the specific language governing permissions and limitations
* * under the License.
* *
* * SPDX-License-Identifier: Apache-2.0
* *****************************************************************************
*/
package org.nd4j.samediff.frameworkimport.onnx.definitions.implementations

import org.nd4j.autodiff.samediff.SameDiff
Expand Down