ComputeLibrary icon indicating copy to clipboard operation
ComputeLibrary copied to clipboard

Problem with PPMLoader?

Open jialiang19 opened this issue 9 years ago • 11 comments

Dear ARM supporters:

Thank you so much for your help thus far.

Currently, I have run into a problem with PPMLoader: when I have a ppm image, It seems that I can only load one channel of the images instead of the three channels (the color images have RGB three channels).

In your documentary about the PPMLoader, It seems that issue_162_1 with RGBA8888 flag, I will be able to load all three channels.

so with my code like the following: issue_162_2

The output of z should be 3, since there should be three channels. Right?

However, this the output: issue_162_3

Is there something wrong with my understanding or my codes?

P.S. The image I used for argv[1] is generated by a jpg image converted and croppted into ppm image with the convert function in linux. I am trying to load the ppm image, but the github seems does not support the ppm image files.

The original image is cat Then I concerted it into ppm image with the command: convert cat.jpg -crop 227x227 cat.ppm

jialiang19 avatar Jul 03 '17 15:07 jialiang19

The PPM loader doesn't do any processing on the image, therefore you end up with a single image (z=1) with RGB data in it (i.e 3x U8 channels interleaved) There isn't currently any easy way to convert an RGB image into a planar tensor, you might be able to do so by using channel extract + sub tensors (One per plane) but I haven't actually tested.

AnthonyBarbier avatar Jul 03 '17 15:07 AnthonyBarbier

Dear @AnthonyARM, Thank you for the quick reply.

After looking at the definition of the channel extract, I am still a little confused. Here is the definition

issue162_4

Since the src_ptr is __global uchar, I should first change data from CLTensor into __global uchar, right? what does stride_x, and step_x mean here? and what should I do with dst_ptr? (changing it back to CLTensor? what format would it be then later?)

Also, I notice the note that : issue_162_5

but I do not see anywhere I can pass a pre-processor argument.

I am sorry that I am really confused. Can you give me more information?

jialiang19 avatar Jul 03 '17 16:07 jialiang19

Use the function: CLChannelExtract

AnthonyBarbier avatar Jul 03 '17 16:07 AnthonyBarbier

Hi @AnthonyARM:

I have just given CLChannelExtract and CLSubTensor a try, but I encountered a problem. and this is my code:

This is part is configure and run.

PPMLoader ppm;
        ppm.open(argv[1]);
        CLImage catjpg;
        ppm.init_image(catjpg, Format::RGB888);

        CLTensor Total_src;

        const TensorShape Total_src_shape(227,227,3);
        const TensorShape R_src_shape(227,227,1);
        const TensorShape G_src_shape(227,227,1);
        const TensorShape B_src_shape(227,227,1);

        Total_src.allocator()->init(TensorInfo(Total_src_shape, Format::U8));

        CLSubTensor R_src(&Total_src, R_src_shape, Coordinates());
        CLSubTensor G_src(&Total_src, G_src_shape, Coordinates(0,0,1));
        CLSubTensor B_src(&Total_src, B_src_shape, Coordinates(0,0,2));

        CLChannelExtract R_extract;
        CLChannelExtract G_extract;
        CLChannelExtract B_extract;

        R_extract.configure(&catjpg, Channel::R, &R_src);
        G_extract.configure(&catjpg, Channel::G, &G_src);
        B_extract.configure(&catjpg, Channel::B, &B_src);

        Total_src.allocator()->allocate();

        R_extract.run();
        G_extract.run();
        B_extract.run();

This part is used to print out the Tensor

int Total_src_x = Total_src.info()->dimension(0);
        int Total_src_y = Total_src.info()->dimension(1);
        int Total_src_z = Total_src.info()->dimension(2);

        float *result_data = new float[Total_src_x * Total_src_y * Total_src_z];


        Total_src.map();

        Window output_window;
        output_window.use_tensor_dimensions(Total_src.info(),Window::DimY);
        Iterator output_it(&Total_src, output_window);


        execute_window_loop(output_window, [&](const Coordinates & id)
        {
        memcpy(result_data + id.z() * Total_src_y* Total_src_x + id.y() * Total_src_x, output_it.ptr(), Total_src_x * sizeof(float));
        },
        output_it);

       for(int z = 0; z < Total_src_z; z++){
                for(int y = 0; y < Total_src_y; y++){
                        for(int x = 0; x < Total_src_x; x++){
                                cout << result_data[z*Total_src_x*Total_src_y+y*Total_src_x+x];
                        }
                cout << endl;
                }
        cout << endl;
        }

The error is issues_162_10

and the detailed error by gdb is : issue_162_11

and after a few testing, and I found that error started at line

Total_src.map();

Is there a reason for this? I use Total_src.map() to print out the tensors.

Thanks.

jialiang19 avatar Jul 03 '17 22:07 jialiang19

Hello @jialiang19 , Can you allocate the catjpg like: catjpg.allocator()->allocate(); ? You will need the following sequence:

/* Read file and set input shape and format */
ppm.open(argv[1]);
ppm.init_image(catjpg, Format::U8);

/* Configure your layer etc. */

/* Allocate and fill */
catjpg.allocator()->allocate();
ppm.fill_image(catjpg);

GeorgeARM avatar Jul 04 '17 12:07 GeorgeARM

Hellow @GeorgeARM:

I give your suggestions a try. Everything starts to run, but some values seem to decay. I cropped out a 20x20 pixels jpg images, then I try to print their RGB value by two workflows. workflow 1: I load the images into Python, and print the RGB pixel value for every pixel. workflow 2: I convert the 20x20 pixels jpg images into a ppm images. Then use my code with Compute Library to print RGB values.

Most values match, but there seems to be that 10% values are decayed. In addition, every time the position of the decayed value changed a little bit.

Here is the image when I did vimdiff: The left side is by the python work flow, and the right side is by the convert in linux and my code with Compute Library:

issue_162_15

This is what happened when tried again, the positions of the decayed values changed: issue_162_16

Can you test it yourself in your end? Is there something wrong with my code?

Here is my testing code:

 PPMLoader ppm;
        ppm.open(argv[1]);
        CLImage catjpg;
        ppm.init_image(catjpg, Format::RGB888);

        CLTensor Total_src;

        const TensorShape Total_src_shape(SIZE,SIZE,3); /* SIZE = 20 */
        const TensorShape R_src_shape(SIZE,SIZE,1);
        const TensorShape G_src_shape(SIZE,SIZE,1);
        const TensorShape B_src_shape(SIZE,SIZE,1);

        Total_src.allocator()->init(TensorInfo(Total_src_shape, Format::U8));

        CLSubTensor R_src(&Total_src, R_src_shape, Coordinates());
        CLSubTensor G_src(&Total_src, G_src_shape, Coordinates(0,0,1));
        CLSubTensor B_src(&Total_src, B_src_shape, Coordinates(0,0,2));

        CLChannelExtract R_extract;
        CLChannelExtract G_extract;
        CLChannelExtract B_extract;

        R_extract.configure(&catjpg, Channel::R, &R_src);
        G_extract.configure(&catjpg, Channel::G, &G_src);
        B_extract.configure(&catjpg, Channel::B, &B_src);

        Total_src.allocator()->allocate();
        catjpg.allocator()->allocate();
        ppm.fill_image(catjpg);

        R_extract.run();
        G_extract.run();
        B_extract.run();

Print out the values here!

cout << "(" << catjpg.info()->dimension(0) << ", " << catjpg.info()->dimension(1) << ")" << endl;

        unsigned int Total_src_x = Total_src.info()->dimension(0);
        unsigned int Total_src_y = Total_src.info()->dimension(1);
        unsigned int Total_src_z = Total_src.info()->dimension(2);

        char *result_data = new char[Total_src_x * Total_src_y * Total_src_z];

        Total_src.map();

        Window output_window;
        output_window.use_tensor_dimensions(Total_src.info(),Window::DimY);
        Iterator output_it(&Total_src, output_window);


        execute_window_loop(output_window, [&](const Coordinates & id)
        {
        memcpy(result_data + id.z() * Total_src_y* Total_src_x + id.y() * Total_src_x, output_it.ptr(), Total_src_x * sizeof(char));
        },
        output_it);

                for(int y = 0; y < Total_src_y; y++){
                        for(int x = 0; x < Total_src_x; x++){
                                for(int z = 0; z < Total_src_z; z++){
                                        if(z == 0) cout << "(";
                                        else if (z == 1) cout <<", ";
                                        else if (z == 2) cout <<", ";
                                        cout << (int) result_data[z*Total_src_x*Total_src_y+y*Total_src_x+x];
                                        if(z == 2) cout << ")" << endl;
                                }
                        }
                }





jialiang19 avatar Jul 04 '17 17:07 jialiang19

Does anyone have same issues? or How do you guys load standard jpg images into tensors? Thanks!

jialiang19 avatar Jul 17 '17 14:07 jialiang19

Are you sure you have ARM_COMPUTE_CL defined ? (This is required for the image to be automatically mapped / unmapped inside fill_image.

Also, you need to map / unmap the image before you access its content too (That's almost certainly why you have corrupted bits of data: the CPU caches haven't been cleaned)

AnthonyBarbier avatar Jul 18 '17 10:07 AnthonyBarbier

I think it would be very useful to provide a JPGLoader utility.

psyhtest avatar Jul 24 '17 10:07 psyhtest

@jialiang19 Hello, is it resolved yet?

kzos avatar Aug 31 '17 09:08 kzos

Hello @jialiang19,

Has this issue been resolved?

GeorgeARM avatar Oct 02 '17 10:10 GeorgeARM