-
Notifications
You must be signed in to change notification settings - Fork 167
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
#790 Add Ops\Boxes #867
#790 Add Ops\Boxes #867
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2754,7 +2754,14 @@ public static Tensor einsum(string equation, params Tensor[] tensors) | |
/// </summary> | ||
/// <param name="input">The first input tensor</param> | ||
/// <param name="other">The second input tensor</param> | ||
public static Tensor maximum(Tensor input, Tensor other) => input.maximum(other); | ||
public static Tensor max(Tensor input, Tensor other) => maximum(input, other); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think 'max' and 'maximum' are aliases of each other. https://pytorch.org/docs/stable/generated/torch.max.html?highlight=max#torch.max There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there is an overload at the bottom of the 'max‘ page torch.max(input, other, *, out=None) → Tensor |
||
|
||
/// <summary> | ||
/// Computes the element-wise maximum of input and other. | ||
/// </summary> | ||
/// <param name="input">The first input tensor</param> | ||
/// <param name="other">The second input tensor</param> | ||
static public Tensor maximum(Tensor input, Tensor other) => input.maximum(other); | ||
|
||
/// <summary> | ||
/// Returns a named tuple (values, indexes) where values is the maximum value of each row of the input tensor in the given dimension dim. | ||
|
@@ -2797,7 +2804,14 @@ public static Tensor einsum(string equation, params Tensor[] tensors) | |
/// </summary> | ||
/// <param name="input">The first input tensor</param> | ||
/// <param name="other">The second input tensor</param> | ||
public static Tensor minimum(Tensor input, Tensor other) => input.minimum(other); | ||
public static Tensor min(Tensor input, Tensor other) => minimum(input, other); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See comment on 'max' |
||
|
||
/// <summary> | ||
/// Computes the element-wise minimum of input and other. | ||
/// </summary> | ||
/// <param name="input">The first input tensor</param> | ||
/// <param name="other">The second input tensor</param> | ||
static public Tensor minimum(Tensor input, Tensor other) => input.minimum(other); | ||
|
||
/// <summary> | ||
/// Returns a named tuple (values, indexes) where values is the minimum value of each row of the input tensor in the given dimension dim. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace TorchSharp.Utils | ||
{ | ||
/// <summary> | ||
/// Converts IEnumerable to tuple. | ||
/// </summary> | ||
public static class DeconstructExtension | ||
{ | ||
public static void Deconstruct<T>(this IEnumerable<T> seq, out T first, out IEnumerable<T> rest) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doc comments on all public APIs, please. |
||
{ | ||
first = seq.FirstOrDefault(); | ||
rest = seq.Skip(1); | ||
} | ||
|
||
public static void Deconstruct<T>(this IEnumerable<T> seq, out T first, out T second, out IEnumerable<T> rest) | ||
=> (first, (second, rest)) = seq; | ||
|
||
public static void Deconstruct<T>(this IEnumerable<T> seq, out T first, out T second, out T third, out IEnumerable<T> rest) | ||
=> (first, second, (third, rest)) = seq; | ||
|
||
public static void Deconstruct<T>(this IEnumerable<T> seq, out T first, out T second, out T third, out T fourth, out IEnumerable<T> rest) | ||
=> (first, second, third, (fourth, rest)) = seq; | ||
|
||
public static void Deconstruct<T>(this IEnumerable<T> seq, out T first, out T second, out T third, out T fourth, out T fifth, out IEnumerable<T> rest) | ||
=> (first, second, third, fourth, (fifth, rest)) = seq; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. See LICENSE in the project root for license information. | ||
|
||
// A number of implementation details in this file have been translated from the Python version of torchvision, | ||
// largely located in the files found in this folder: | ||
// | ||
// https://github.com/pytorch/vision/blob/3d60f498e71ba63b428edb184c9ac38fa3737fa6/torchvision/ops/_box_convert.py | ||
// | ||
// The origin has the following copyright notice and license: | ||
// | ||
// https://github.com/pytorch/vision/blob/main/LICENSE | ||
// | ||
|
||
using System; | ||
using static TorchSharp.torch; | ||
using TorchSharp.Utils; | ||
|
||
#nullable enable | ||
namespace TorchSharp | ||
{ | ||
public static partial class torchvision | ||
{ | ||
public static partial class ops | ||
{ | ||
/// <summary> | ||
/// Converts bounding boxes from (cx, cy, w, h) format to (x1, y1, x2, y2) format. | ||
/// (cx, cy) refers to center of bounding box | ||
/// (w, h) are width and height of bounding box | ||
/// </summary> | ||
/// <param name="boxes">boxes (Tensor[N, 4]): boxes in (cx, cy, w, h) format which will be converted.</param> | ||
/// <returns>boxes (Tensor(N, 4)): boxes in (x1, y1, x2, y2) format.</returns> | ||
internal static Tensor _box_cxcywh_to_xyxy(Tensor boxes) | ||
{ | ||
//# We need to change all 4 of them so some temporary variable is needed. | ||
var (cx, cy, w, h, _) = boxes.unbind(-1); | ||
var x1 = cx - 0.5 * w; | ||
var y1 = cy - 0.5 * h; | ||
var x2 = cx + 0.5 * w; | ||
var y2 = cy + 0.5 * h; | ||
|
||
boxes = torch.stack(new Tensor[] { x1, y1, x2, y2 }, dim: -1); | ||
return boxes; | ||
} | ||
|
||
/// <summary> | ||
/// Converts bounding boxes from (x1, y1, x2, y2) format to (cx, cy, w, h) format. | ||
/// (x1, y1) refer to top left of bounding box | ||
/// (x2, y2) refer to bottom right of bounding box | ||
/// </summary> | ||
/// <param name="boxes">boxes (Tensor[N, 4]): boxes in (x1, y1, x2, y2) format which will be converted.</param> | ||
/// <returns>boxes (Tensor(N, 4)): boxes in (cx, cy, w, h) format.</returns> | ||
internal static Tensor _box_xyxy_to_cxcywh(Tensor boxes) | ||
{ | ||
var (x1, y1, x2, y2, _) = boxes.unbind(-1); | ||
var cx = (x1 + x2) / 2; | ||
var cy = (y1 + y2) / 2; | ||
var w = x2 - x1; | ||
var h = y2 - y1; | ||
|
||
boxes = torch.stack(new Tensor[] { cx, cy, w, h }, dim: -1); | ||
|
||
return boxes; | ||
} | ||
|
||
/// <summary> | ||
/// Converts bounding boxes from (x, y, w, h) format to (x1, y1, x2, y2) format. | ||
/// (x, y) refers to top left of bouding box. | ||
/// (w, h) refers to width and height of box. | ||
/// </summary> | ||
/// <param name="boxes">boxes (Tensor[N, 4]): boxes in (x, y, w, h) which will be converted.</param> | ||
/// <returns>boxes (Tensor[N, 4]): boxes in (x1, y1, x2, y2) format.</returns> | ||
internal static Tensor _box_xywh_to_xyxy(Tensor boxes) | ||
{ | ||
var (x, y, w, h, _) = boxes.unbind(-1); | ||
boxes = torch.stack(new Tensor[] { x, y, x + w, y + h }, dim: -1); | ||
return boxes; | ||
} | ||
|
||
/// <summary> | ||
/// Converts bounding boxes from (x1, y1, x2, y2) format to (x, y, w, h) format. | ||
/// (x1, y1) refer to top left of bounding box | ||
/// (x2, y2) refer to bottom right of bounding box | ||
/// </summary> | ||
/// <param name="boxes">boxes (Tensor[N, 4]): boxes in (x1, y1, x2, y2) which will be converted.</param> | ||
/// <returns>boxes (Tensor[N, 4]): boxes in (x, y, w, h) format.</returns> | ||
internal static Tensor _box_xyxy_to_xywh(Tensor boxes) | ||
{ | ||
var (x1, y1, x2, y2, _) = boxes.unbind(-1); | ||
var w = x2 - x1; | ||
var h = y2 - y1; | ||
boxes = torch.stack(new Tensor[] { x1, y1, w, h }, dim: -1); | ||
return boxes; | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This won't work on Mac, I believe. There, it's '.dylib'