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

Add op read_file and decode_jpeg #32564

Merged
merged 5 commits into from
Apr 29, 2021

Conversation

LielinJiang
Copy link
Contributor

@LielinJiang LielinJiang commented Apr 26, 2021

PR types

New features

PR changes

APIs

Describe

Add op read_file and decode_jpeg

file_path -> gpu tensor的时间比较(单位:s/1000次)
image

@paddle-bot-old
Copy link

Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@LielinJiang LielinJiang reopened this Apr 26, 2021
@PaddlePaddle PaddlePaddle locked and limited conversation to collaborators Apr 26, 2021
@PaddlePaddle PaddlePaddle unlocked this conversation Apr 26, 2021
@PaddlePaddle PaddlePaddle locked and limited conversation to collaborators Apr 27, 2021
@PaddlePaddle PaddlePaddle unlocked this conversation Apr 27, 2021
"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.
Copy link
Contributor

Choose a reason for hiding this comment

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

下面的注释是支持灰度图的吧,这里需要正确的comments

Copy link
Contributor Author

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));
Copy link
Contributor

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}

Copy link
Contributor Author

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) {
Copy link
Contributor

Choose a reason for hiding this comment

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

InitNvjpegImage

Copy link
Contributor Author

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;
Copy link
Contributor

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

Copy link
Contributor Author

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;
Copy link
Contributor

Choose a reason for hiding this comment

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

outImage -> out_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.

done, thanks


nvjpegStatus_t decode_status = platform::dynload::nvjpegDecode(
nvjpeg_handle, nvjpeg_state, x_data, x->numel(), outputFormat,
&outImage, stream);
Copy link
Contributor

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

Copy link
Contributor Author

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);
Copy link
Contributor

Choose a reason for hiding this comment

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

看下能否直接读取到out里,去掉拷贝

Copy link
Contributor Author

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);
Copy link
Contributor

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.
Copy link
Contributor

Choose a reason for hiding this comment

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

21

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done, thanks


def decode_jpeg(x, mode='unchanged', name=None):
"""
Decodes a JPEG image into a 3 dimensional RGB Tensor.
Copy link
Contributor

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

Copy link
Contributor Author

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};
Copy link
Contributor

Choose a reason for hiding this comment

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

int64_t -> uint64_t?

Copy link
Contributor Author

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};
Copy link
Contributor

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);
Copy link
Contributor

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;
Copy link
Contributor

Choose a reason for hiding this comment

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

后续需要考虑如何管理这种static变量

Copy link
Contributor Author

Choose a reason for hiding this comment

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

好的

@LielinJiang LielinJiang merged commit b22f6d6 into PaddlePaddle:develop Apr 29, 2021
LielinJiang added a commit to LielinJiang/Paddle that referenced this pull request Apr 29, 2021
* add op read_file and decode_jpeg
XiaoguangHu01 pushed a commit that referenced this pull request Apr 30, 2021
* add op read_file and decode_jpeg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants