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

Tensorboard Issue #932 #936

Merged
merged 27 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
ee3722a
add_text
ChengYen-Tang Feb 22, 2023
32ad2c6
Merge branch 'main' into tensorboard
ChengYen-Tang Feb 26, 2023
352f8b5
add_image
ChengYen-Tang Feb 28, 2023
b086567
Comment by NiklasGustafsson
ChengYen-Tang Mar 1, 2023
79a0138
GifEncoder
ChengYen-Tang Mar 2, 2023
c037b1a
add_video
ChengYen-Tang Mar 3, 2023
ec228a0
Add tensorboard examples
ChengYen-Tang Mar 3, 2023
c01b8e6
Merge branch 'main' into tensorboard
NiklasGustafsson Mar 3, 2023
3ce69ec
Fix add_video will crash
ChengYen-Tang Mar 6, 2023
fb51155
Merge branch 'main' into tensorboard
ChengYen-Tang Mar 7, 2023
d30c285
Using CUDA in the example
ChengYen-Tang Mar 7, 2023
63eed94
Merge remote-tracking branch 'origin/tensorboard' into tensorboard
ChengYen-Tang Mar 7, 2023
a8cef18
Download example gif image from the Internet
ChengYen-Tang Mar 8, 2023
97fd763
Replace torch.cat() with Tensor.expand()
ChengYen-Tang Mar 9, 2023
c5b9eac
Merge branch 'main' into tensorboard
NiklasGustafsson Mar 6, 2023
4bcfe2d
Fix program crash caused by add_image
ChengYen-Tang Mar 10, 2023
785d752
Merge branch 'main' into tensorboard
ChengYen-Tang Mar 10, 2023
a89b7de
Merge branch 'main' into tensorboard
ChengYen-Tang Mar 17, 2023
02d0f57
Merge branch 'main' into tensorboard
ChengYen-Tang Mar 29, 2023
886bde5
Merge remote-tracking branch 'origin/main' into tensorboard
ChengYen-Tang Apr 11, 2023
59e7306
add_histogram (Remarks: Not yet tested)
ChengYen-Tang Apr 14, 2023
0ac598a
Test add_histogram
ChengYen-Tang Apr 19, 2023
2bc591e
Update source of code
ChengYen-Tang Apr 24, 2023
8c5f715
Update example main
ChengYen-Tang Apr 24, 2023
6aa4df8
Add third party licence
ChengYen-Tang Apr 26, 2023
0641d2d
Merge branch 'main' into tensorboard
ChengYen-Tang Apr 27, 2023
52edc57
Recommendations made by @NiklasGustafsson
ChengYen-Tang Apr 27, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 75 additions & 17 deletions THIRD-PARTY-NOTICES.TXT
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ SOFTWARE.
This software incorporates material from third parties listed below:

==================

From PyTorch

Copyright (c) 2016- Facebook, Inc (Adam Paszke)
Copyright (c) 2014- Facebook, Inc (Soumith Chintala)
Copyright (c) 2011-2014 Idiap Research Institute (Ronan Collobert)
Expand All @@ -35,54 +35,54 @@ Copyright (c) 2011-2013 NYU (Clement Farabet)
Copyright (c) 2006-2010 NEC Laboratories America (Ronan Collobert, Leon Bottou, Iain Melvin, Jason Weston)
Copyright (c) 2006 Idiap Research Institute (Samy Bengio)
Copyright (c) 2001-2004 Idiap Research Institute (Ronan Collobert, Samy Bengio, Johnny Mariethoz)

From Caffe2:

Copyright (c) 2016-present, Facebook Inc. All rights reserved.

All contributions by Facebook:
Copyright (c) 2016 Facebook Inc.

All contributions by Google:
Copyright (c) 2015 Google Inc.
All rights reserved.

All contributions by Yangqing Jia:
Copyright (c) 2015 Yangqing Jia
All rights reserved.

All contributions from Caffe:
Copyright(c) 2013, 2014, 2015, the respective contributors
All rights reserved.

All other contributions:
Copyright(c) 2015, 2016 the respective contributors
All rights reserved.

Caffe2 uses a copyright model similar to Caffe: each contributor holds
copyright over their contributions to Caffe2. The project versioning records
all such contribution and copyright details. If a contributor wants to further
mark their specific copyright on a particular contribution, they should
indicate their copyright solely in the commit message of the change when it is
committed.

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

3. Neither the names of Facebook, Deepmind Technologies, NYU, NEC Laboratories America
and IDIAP Research Institute nor the names of its contributors may be
used to endorse or promote products derived from this software without
specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Expand Down Expand Up @@ -591,4 +591,62 @@ Apache License
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

==================
Android Code(gif_encoder)
https://cs.android.com/android/platform/superproject/+/master:external/glide/LICENSE
https://cs.android.com/android/platform/superproject/+/master:external/glide/third_party/gif_encoder/LICENSE

Copyright 2014 Google, Inc. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are
permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list
of conditions and the following disclaimer in the documentation and/or other materials
provided with the distribution.

THIS SOFTWARE IS PROVIDED BY GOOGLE, INC. ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GOOGLE, INC. OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

The views and conclusions contained in the software and documentation are those of the
authors and should not be interpreted as representing official policies, either expressed
or implied, of Google, Inc.
---------------------------------------------------------------------------------------------

License for Encoder.cs and LZWEncoder.cs

No copyright asserted on the source code of this class. May be used for any
purpose, however, refer to the Unisys LZW patent for restrictions on use of
the associated LZWEncoder class. Please forward any corrections to
kweiner@fmsware.com.

-----------------------------------------------------------------------------
License for NeuQuant.cs

Copyright (c) 1994 Anthony Dekker

NEUQUANT Neural-Net quantization algorithm by Anthony Dekker, 1994. See
"Kohonen neural networks for optimal colour quantization" in "Network:
Computation in Neural Systems" Vol. 5 (1994) pp 351-367. for a discussion of
the algorithm.

Any party obtaining a copy of these files from the author, directly or
indirectly, is granted, free of charge, a full and unrestricted irrevocable,
world-wide, paid up, royalty-free, nonexclusive right and license to deal in
this software and documentation files (the "Software"), including without
limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons who
receive copies from any such party to do so, with the only requirement being
that this copyright notice remain intact.
ChengYen-Tang marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions src/Examples/Examples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Net.Http" Version="2.2.29" Condition="'$(TargetFramework)' == 'net472'" />
<PackageReference Include="SharpZipLib" Version="1.4.0" />
<PackageReference Include="SkiaSharp" Version="2.88.3" />
<PackageReference Include="System.Memory" Version="4.5.5" />
Expand Down
1 change: 1 addition & 0 deletions src/Examples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public static void Main(string[] args)
//ImageTransforms.Main(args);
//SpeechCommands.Main(args);
IOReadWrite.Main(args);
Tensorboard.Main(args).Wait();
}
}
}
114 changes: 114 additions & 0 deletions src/Examples/Tensorboard.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. See LICENSE in the project root for license information.
using System;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SkiaSharp;
using static TorchSharp.torch;

namespace TorchSharp.Examples
{
public static class Tensorboard
{
private static string imagePath = "TensorboardExample/Images";
internal static async Task Main(string[] args)
{
await DownloadExampleData("https://agirls.aottercdn.com/media/0d532b3f-0196-466a-96e1-7c6db56d0142.gif");
var device = cuda.is_available() ? CUDA : CPU;
var writer = utils.tensorboard.SummaryWriter("tensorboard");
Console.WriteLine($"Running Tensorboard on {device.type}");
AddText(writer);
AddImageUsePath(writer);
AddImageUseTensor(writer, device);
AddVideo(writer, device);
AddHistogram(writer, device);
}

private static void AddHistogram(Modules.SummaryWriter writer, Device device)
{
for (int i = 0; i < 10; i++) {
Tensor x = randn(1000, device: device);
writer.add_histogram("AddHistogram", x + i, i);
}
}

private static void AddText(Modules.SummaryWriter writer)
{
writer.add_text("AddText", "step_1", 1);
writer.add_text("AddText", "step_2", 2);
writer.add_text("AddText", "step_3", 3);
writer.add_text("AddText", "step_4", 4);
writer.add_text("AddText", "step_5", 5);
}

private static void AddImageUsePath(Modules.SummaryWriter writer)
{
var imagesPath = Directory.GetFiles(imagePath);
for (var i = 0; i < imagesPath.Length; i++) {
writer.add_img("AddImageUsePath", imagesPath[i], i);
}
}
ChengYen-Tang marked this conversation as resolved.
Show resolved Hide resolved

private static void AddImageUseTensor(Modules.SummaryWriter writer, Device device)
{
var images = Directory.GetFiles(imagePath).Select(item => SKBitmap.Decode(item)).ToArray();
using var d = NewDisposeScope();
for (var i = 0; i < images.Length; i++) {
var tensor = SKBitmapToTensor(images[i], device);
writer.add_img("AddImageUseTensor", tensor, i, dataformats: "CHW");
images[i].Dispose();
}
}

private static void AddVideo(Modules.SummaryWriter writer, Device device)
{
var images = Directory.GetFiles(imagePath).Select(item => SKBitmap.Decode(item)).ToArray();
using var d = NewDisposeScope();
var tensor = stack(images.Select(item => SKBitmapToTensor(item, device)).ToArray());
tensor = stack(new Tensor[] { tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor });
foreach (var image in images)
image.Dispose();
writer.add_video("AddVideo", tensor, 1, 10);
writer.add_video("AddVideo", tensor, 2, 10);
writer.add_video("AddVideo", tensor, 3, 10);
}

private static Tensor SKBitmapToTensor(SKBitmap skBitmap, Device device)
{
var width = skBitmap.Width;
var height = skBitmap.Height;
var hwcData = new byte[4, height, width];
for (var y = 0; y < height; y++) {
for (var x = 0; x < width; x++) {
var color = skBitmap.GetPixel(x, y);
hwcData[0, y, x] = color.Red;
hwcData[1, y, x] = color.Green;
hwcData[2, y, x] = color.Blue;
hwcData[3, y, x] = color.Alpha;
}
}

return tensor(hwcData, ScalarType.Byte, device);
}

private static async Task DownloadExampleData(string url)
{
if (Directory.Exists(imagePath))
Directory.Delete(imagePath, true);
Directory.CreateDirectory(imagePath);
using var client = new HttpClient();

using var message = await client.GetAsync(url);
using var stream = await message.Content.ReadAsStreamAsync();
using var animatedImage = Image.Load<Rgba32>(stream);
for (var i = 0; i < animatedImage.Frames.Count; i++) {
var pngFilename = Path.Combine(imagePath, $"frame_{i}.png");
using var pngImage = animatedImage.Frames.CloneFrame(i);
pngImage.SaveAsPng(pngFilename);
}
}
}
}
4 changes: 2 additions & 2 deletions src/TorchSharp/Tensor/Enums/HistogramBinSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace TorchSharp
{
public enum HistogramBinSelector
public enum HistogramBinSelector : byte
{
Doane,
Doane = 0,
Rice,
Scott,
Sqrt,
Expand Down
9 changes: 0 additions & 9 deletions src/TorchSharp/Tensor/Enums/side.cs

This file was deleted.

11 changes: 0 additions & 11 deletions src/TorchSharp/Tensor/torch.OtherOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -628,17 +628,6 @@ public static Tensor[] meshgrid(IEnumerable<Tensor> tensors, string indexing = "
/// </summary>
public static Tensor roll(Tensor input, ReadOnlySpan<long> shifts, ReadOnlySpan<long> dims = default) => input.roll(shifts, dims);

// https://pytorch.org/docs/stable/generated/torch.searchsorted
Copy link
Contributor

Choose a reason for hiding this comment

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

Why remove?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because in case of 1 this code is ambiguous with 2.

[Obsolete("not implemented", true)]
static Tensor searchsorted(
Tensor sorted_sequence,
Tensor values,
bool out_int32 = false,
bool right = false,
side side = side.left,
Tensor? sorter = null)
=> throw new NotImplementedException();

// https://pytorch.org/docs/stable/generated/torch.tensordot
/// <summary>
/// Returns a contraction of <paramref name="a"/> and <paramref name="b"/> over multiple dimensions.
Expand Down
1 change: 1 addition & 0 deletions src/TorchSharp/TorchSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<ItemGroup>
<PackageReference Include="Google.Protobuf" Version="3.21.9" />
<PackageReference Include="SharpZipLib" Version="1.4.0" />
<PackageReference Include="SkiaSharp" Version="2.88.3" />
<PackageReference Include="System.Memory" Version="4.5.5" />
</ItemGroup>

Expand Down
13 changes: 13 additions & 0 deletions src/TorchSharp/Utils/tensorboard/Enums/HistogramBinSelector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace TorchSharp.Utils.tensorboard.Enums
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this enum declared in two places?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This enum has one more tensorflow item.

{
public enum HistogramBinSelector : byte
{
Doane = 0,
Rice,
Scott,
Sqrt,
Stone,
Sturges,
Tensorflow
}
}
Loading