175 points · by mattismegevand · 2y ago · 38 comments
The conservative solution to adapt images for dark-mode consist in dimming the image, however there is a lot of images that can be inverted (graph for example). Using deep learning we can avoid heuristics and obtain a much more reliable solution.
The API uses an EfficientNet pre-trained model fine-tuned on a custom dataset (1.1k samples). EfficientNet was chosen as it was pre-trained and offered the best performance for its size.
The trained model is very small (16MB) which means you can easily run your own instance. This problem is very simple for deep learning as it's a simple binary classification.
For this project training the model wasn't the challenge as most of the time was spent on the construction of the dataset.
For the API I'm using FastAPI, Redis and ONNX Runtime to run the model. The API can be used by posting the images to the API, using URL and using SHA-1 for already processed images.
The API is free and open-sourced (http://github.com/mattismegevand/invertornot).
It's far from perfect, but it's a neat one to have in your back pocket.
We did introduce dark mode in a manner where a creator can even make it dynamic for end user to choose a different theme.
While I was loggerheads with my CTO cofounder on this - I am glad someone finds this useful in the community of users.
I am curious what kind of use case you have for retool/ alternate tools where you see dark mode as a need
CSS:
img[src^="/images/invertable"] {
filter: hue-rotate(180deg) invert(1);
}
Now all image names starting with "invertable" will be inverted.It's just that often you do have a lot of images and, more importantly, they may not be known in advance. If images are dynamic, then you obviously can't go over them manually, no matter how few of them there are.
This was the principal motivation for me for InvertOrNot.com: I am willing to manually classify hundreds or thousands of images for inversion, and I did, but what I can't do is do that for all of the many Wikipedia popups on gwern.net - not just because there's 22k+ of them, but because they are constantly being added, WP articles are constantly changing, and they are fully recursive so a reader could pop up any WP article. A fully-automated API is the only possible solution. (Doesn't have to be neural nets, that's just the most obvious & easiest approach; I expect classical approaches would work too and would be easier to get running in-browser too.)
And then if you have a high-quality fully-automated API, then you might as well drop the manual classifying. It's tedious and adds friction to writing.
https://upload.wikimedia.org/wikipedia/commons/thumb/6/69/Pr... https://upload.wikimedia.org/wikipedia/commons/thumb/f/f7/US... https://upload.wikimedia.org/wikipedia/commons/thumb/d/d4/Na... https://upload.wikimedia.org/wikipedia/commons/thumb/b/bb/Li... https://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/Pi...
Generally making emails dark mode is a non trivial problem, there is an hope source library that is a good start from Tempo: https://github.com/yourtempo/tempo-message-dark-mode
However there are plenty of edge cases that plugin doesn’t handle that we had to fix at Shortwave. Examples include legacy HTML4 attributes, better support for emails that are half dark mode, emails that have background images of #fff (looking at you google play reciepts), the list goes on. It’s honestly pretty solid now
As a side, I have a great frustration balancing dark mode with other things. I use flux on my Mac, not for color temp but strictly for the "darkroom" mode which inverts everything and makes the (inverted) brightness values into red. I only do this when coding at night, to help with my sleep... but the trouble is it's made for people who use light mode during the day. I use dark mode all day. So I have to get out of dark mode and change my vscode theme to light in order for it to work. Not a huge deal, but then I open up some web page with a black background and go blind from all the red.
But I guess with ML reinforced by human feedback, trends like this manifest without having to explicitly handle them as special cases at all...
EDIT: from another comment - actually "inverting" seems to refer to doing
hue-rotate(180deg) invert()
...which (if I read it correctly) inverts the hue of colored areas before applying the actual inversion in order to keep the colors the same (more or less - see also https://stackoverflow.com/questions/65344006/why-does-filter...) ? Not a frontend developer, so I wasn't familiar with this trick until now. TIL...You'd need to look at this project's code to see how it converts input data to the expected matrix format, though.
In practice, the accuracy, whatever it is, appears to be very high and more than adequate to justify its use.