-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Add op read_file and decode_jpeg #32564
Add op read_file and decode_jpeg #32564
Conversation
Thanks for your contribution! |
"of the JPEG image. It is a tensor with rank 1."); | ||
AddOutput("Out", "The output tensor of DecodeJpeg op"); | ||
AddComment(R"DOC( | ||
This operator decode a JPEG image into a 3 dimensional RGB Tensor. |
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.
下面的注释是支持灰度图的吧,这里需要正确的comments
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.
done, thanks
OP_INOUT_CHECK(ctx->HasOutput("Out"), "Output", "Out", "DecodeJpeg"); | ||
|
||
auto out_dims = std::vector<int>(1, -1); | ||
ctx->SetOutputDim("Out", framework::make_ddim(out_dims)); |
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.
out_dims的rank是3吧,这里的shape不正确, 依据mode, 是 {1, -1, -1} 或 {3,-1, -1}
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.
done, thanks
|
||
static nvjpegHandle_t nvjpeg_handle = nullptr; | ||
|
||
void init_nvjpegImage(nvjpegImage_t* img) { |
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.
InitNvjpegImage
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.
done, thanks
int height = heights[0]; | ||
|
||
nvjpegOutputFormat_t outputFormat; | ||
int outputComponents; |
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.
outputFormat -> output_format
outputComponents -> output_components
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.
done, thanks
"The provided mode is not supported for JPEG files on GPU")); | ||
} | ||
|
||
nvjpegImage_t outImage; |
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.
outImage -> out_image
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.
done, thanks
|
||
nvjpegStatus_t decode_status = platform::dynload::nvjpegDecode( | ||
nvjpeg_handle, nvjpeg_state, x_data, x->numel(), outputFormat, | ||
&outImage, stream); |
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.
Maybe need another cuda stream
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.
done, thanks
out->Resize(framework::make_ddim(out_shape)); | ||
|
||
uint8_t* data = out->mutable_data<T>(ctx.GetPlace()); | ||
std::memcpy(data, reinterpret_cast<uint8_t*>(image_data.data()), file_size); |
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.
看下能否直接读取到out里,去掉拷贝
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.
done, thanks
platform::errors::InvalidArgument( | ||
"Output(Out) of ReadFileOp is null.")); | ||
|
||
auto out_dims = std::vector<int>(1, -1); |
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.
rank是1的话,这里的out_dims是 {-1}, (1,-1)代表的rank是2
@@ -0,0 +1,67 @@ | |||
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. |
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.
21
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.
done, thanks
python/paddle/vision/ops.py
Outdated
|
||
def decode_jpeg(x, mode='unchanged', name=None): | ||
""" | ||
Decodes a JPEG image into a 3 dimensional RGB Tensor. |
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.
Decodes a JPEG image into a 3 dimensional RGB Tensor or 1 dimensional Gray Tensor
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.
done, thanks
int sz = widths[0] * heights[0]; | ||
|
||
auto* out = ctx.Output<framework::LoDTensor>("Out"); | ||
std::vector<int64_t> out_shape = {output_components, height, width}; |
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.
int64_t -> uint64_t?
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.
function make_dims only accept type int64_t
input.seekg(0, std::ios::beg); | ||
|
||
auto* out = ctx.Output<framework::LoDTensor>("Out"); | ||
std::vector<int64_t> out_shape = {file_size}; |
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.
as mentioned above
|
||
nvjpegStatus_t decode_status = platform::dynload::nvjpegDecode( | ||
nvjpeg_handle, nvjpeg_state, x_data, x->numel(), output_format, | ||
&out_image, nvjpeg_stream); |
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.
需要调用nvjpegJpegStateDestroy释放nvjpeg_state
namespace operators { | ||
|
||
static cudaStream_t nvjpeg_stream = nullptr; | ||
static nvjpegHandle_t nvjpeg_handle = nullptr; |
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.
后续需要考虑如何管理这种static变量
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.
好的
* add op read_file and decode_jpeg
PR types
New features
PR changes
APIs
Describe
Add op read_file and decode_jpeg
file_path -> gpu tensor的时间比较(单位:s/1000次)