-
Notifications
You must be signed in to change notification settings - Fork 7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
164 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/bin/bash | ||
set -ex | ||
|
||
script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" | ||
. "$script_dir/pkg_helpers.bash" | ||
|
||
export BUILD_TYPE=wheel | ||
setup_env 0.5.0 | ||
setup_wheel_python | ||
pip_install numpy pyyaml future ninja | ||
# TODO remove after https://github.com/pytorch/pytorch/pull/27282 gets merged | ||
pip_install six | ||
setup_pip_pytorch_version | ||
python setup.py clean | ||
IS_WHEEL=1 python setup.py bdist_wheel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import os | ||
import unittest | ||
|
||
import torch | ||
from PIL import Image | ||
from torchvision.io.image import read_png, decode_png | ||
import numpy as np | ||
|
||
IMAGE_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "assets", "fakedata", "imagefolder") | ||
|
||
|
||
def get_png_images(directory): | ||
assert os.path.isdir(directory) | ||
for root, dir, files in os.walk(directory): | ||
for fl in files: | ||
_, ext = os.path.splitext(fl) | ||
if ext == ".png": | ||
yield os.path.join(root, fl) | ||
|
||
|
||
class ImageTester(unittest.TestCase): | ||
def test_read_png(self): | ||
for img_path in get_png_images(IMAGE_DIR): | ||
img_pil = torch.from_numpy(np.array(Image.open(img_path))) | ||
img_lpng = read_png(img_path) | ||
self.assertTrue(torch.all(img_lpng == img_pil)) | ||
|
||
def test_decode_png(self): | ||
for img_path in get_png_images(IMAGE_DIR): | ||
img_pil = torch.from_numpy(np.array(Image.open(img_path))) | ||
size = os.path.getsize(img_path) | ||
img_lpng = decode_png(torch.from_file(img_path, dtype=torch.uint8, size=size)) | ||
self.assertTrue(torch.all(img_lpng == img_pil)) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#pragma once | ||
|
||
#include <torch/torch.h> | ||
#include <string> | ||
|
||
torch::Tensor decodePNG(const torch::Tensor& data); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#pragma once | ||
|
||
#include "cpu/image/readpng_cpu.h" |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import torch | ||
from torch import nn, Tensor | ||
import os | ||
|
||
|
||
def decode_png(input): | ||
# type: (Tensor) -> Tensor | ||
""" | ||
Decodes a PNG image into a 3 dimensional RGB Tensor. | ||
The values of the output tensor are uint8 between 0 and 255. | ||
Arguments: | ||
input (Tensor[1]): a one dimensional int8 tensor containing | ||
the raw bytes of the PNG image. | ||
Returns: | ||
output (Tensor[image_width, image_height, 3]) | ||
""" | ||
if not isinstance(input, torch.Tensor) or len(input) == 0: | ||
raise ValueError("Expected a non empty 1-dimensional tensor.") | ||
|
||
if not input.dtype == torch.uint8: | ||
raise ValueError("Expected a torch.uint8 tensor.") | ||
output = torch.ops.torchvision.decode_png(input) | ||
return output | ||
|
||
|
||
def read_png(path): | ||
# type: (str) -> Tensor | ||
""" | ||
Reads a PNG image into a 3 dimensional RGB Tensor. | ||
The values of the output tensor are uint8 between 0 and 255. | ||
Arguments: | ||
path (str): path of the PNG image. | ||
Returns: | ||
output (Tensor[image_width, image_height, 3]) | ||
""" | ||
if not os.path.isfile(path): | ||
raise ValueError("Excepted a valid file path.") | ||
|
||
size = os.path.getsize(path) | ||
if size == 0: | ||
raise ValueError("Excepted a non empty file.") | ||
data = torch.from_file(path, dtype=torch.uint8, size=size) | ||
return decode_png(data) |