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 image with raw pixel data #20

Closed
Mirocos opened this issue Apr 11, 2022 · 9 comments
Closed

add image with raw pixel data #20

Mirocos opened this issue Apr 11, 2022 · 9 comments

Comments

@Mirocos
Copy link

Mirocos commented Apr 11, 2022

Hi, I read all codes in the test file that I actually didn't find an example to save images with its raw pixel data.
Does it already supported or not?

@RustingSword
Copy link
Owner

Not yet, it has to be encoded into one of the supported formats first.

@Mirocos
Copy link
Author

Mirocos commented Apr 20, 2022

Hi, I was trying to encode raw pixels data to png format and send it to add_image.
My code looks like below:

            int out_len = 0;
            int width = t.size(0), height = t.size(1), channel = t.size(2);
            auto encoded_bytes = stbi_write_png_to_mem((const unsigned char*)v.data(), 0, height, width, channel, &out_len);
            std::string png(encoded_bytes, encoded_bytes+out_len);
            logger.add_image("Tensor raw data plugin", 1, png, height, width, channel, "imrod");
            std::ofstream file("./demo/imord.png", std::ios::binary);
            file << png;

where stbi_write_png_to_mem is a function from stbi_image project,.

I found that the imord.png is correct which ouput by the binary stream. But the file output by the logger, say imrod.pb seems to be wrong. Because tensorboard tell that no dashboard active data in it.

@RustingSword
Copy link
Owner

Thanks for the feedback, I will try to look into this as soon as possible. Maybe you can do a quick inspection to do a round trip by stbi_load(some_valid_png_file) then stbi_write_png_to_mem to see what's wrong. If this works, then the diff comes from the raw pixel data; if this doesn't work, maybe stbi encodes png file differently?

@RustingSword
Copy link
Owner

I just tried this, and it worked:

diff --git a/CMakeLists.txt b/CMakeLists.txt
index b99ba57..7b885d0 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -23,3 +23,7 @@ target_link_libraries(tensorboard_logger PUBLIC ${Protobuf_LIBRARIES})

 add_executable(tensorboard_logger_test tests/test_tensorboard_logger.cc)
 target_link_libraries(tensorboard_logger_test tensorboard_logger)
+target_include_directories(tensorboard_logger_test PUBLIC
+    ../stb/deprecated/
+    ../stb/
+)
diff --git a/tests/test_tensorboard_logger.cc b/tests/test_tensorboard_logger.cc
index d9f1f3e..bde9d07 100644
--- a/tests/test_tensorboard_logger.cc
+++ b/tests/test_tensorboard_logger.cc
@@ -5,6 +5,14 @@
 #include <vector>

 #include "tensorboard_logger.h"
+#define STB_IMAGE_WRITE_IMPLEMENTATION
+#include "stb_image_write.h"
+
+#define STB_IMAGE_IMPLEMENTATION
+#include "stb_image.h"
+
+#define STB_DEFINE
+#include "stb.h"

 using namespace std;

@@ -57,6 +65,21 @@ int test_log_image(TensorBoardLogger& logger) {
     logger.add_image("TensorBoard Audo Plugin", 1, image2, 1766, 814, 3,
                      "TensorBoard", "Audio");

+    // add image from memory
+    int x, y, c, size;
+    const char* test_file = "tux.png";
+    auto image3 = stbi_load(test_file, &x, &y, &c, 0);
+    auto encoded = stbi_write_png_to_mem(image3, 0, x, y, c, &size);
+    cout << test_file << " x=" << x << " y=" << y << " c=" << c <<
+        " re-encoded size=" << size << endl;
+    string png(encoded, encoded + size);
+    logger.add_image("image from memory", 1, png, x, y, c, "Tux");
+    ofstream file("reencoded_tux.png", ios::binary);
+    file << png;
+    file.close();
+    stbi_image_free(image3);
+    STBIW_FREE(encoded);
+
     // add multiple images
     // FIXME This seems doesn't work anymore.
     // logger.add_images(

image

Test file is this one https://cdn.freebiesupply.com/logos/large/2x/tux-logo-png-transparent.png

@Mirocos
Copy link
Author

Mirocos commented Apr 23, 2022

Hi, thanks for that check. I use the reference picture tux.png and load it first than write it to encoded memory. I don't what happened that I open the target imrod.pb in hex format and all I saw is like below snap.
image

@Mirocos
Copy link
Author

Mirocos commented Apr 23, 2022

I know what happened why tensorboard didn't detect the event file. From this issue #13, I found that my target ouput filename did not have a substring like tfevents that is what tensorboard seach for. Sorry to waste your time, my apologies

@RustingSword
Copy link
Owner

🤦 I'm planning to add a warning message if the file name is not valid.

@Mirocos
Copy link
Author

Mirocos commented Apr 23, 2022

What about assert if the filename does not have substring "tfevents"?

@RustingSword
Copy link
Owner

Yes, on second thought I think throwing a runtime_error is more appropriate than a warning message. Just pushed the code, thanks for the suggestion.

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

No branches or pull requests

2 participants