0

Context

I wrote a program that takes in input an image normalized by another of my programs.

The input image is:

enter image description here

The program outputs the normalized version of the image. The normalization method is shown further below.

The problem

The output wrongly consists in multicolor mosaics of pixels. Given the above input, the output is (zoomed):

enter image description here

The expected result should be the above plane.

Minimal & Executable Sources

  1. In a directories path of your choice, put the input image in a directory named of your choice.
  2. In this same directories path, create a second directory named differently. It will contain the output image created by the program.
  3. Copy&Paste the below code, replace the two constants containing the directories paths with your custom both directories paths.
  4. Execute the code
  5. Take a look at the output image: you should see the same output image than the one I have shown in this question!

In the below code:

  • You can see the denormalization method
  • The methods __plot_images_predicted_for_testing creates with Matplotlib.Pyplot a window containing the output image, from the RAM input image
  • The input image is fetched thanks to __fetch_the_normalized_image and __load_XYZ loads the input image from disk to RAM.
import numpy as np
from numpy import array
import matplotlib.pyplot as plt
import os
from skimage import data


class Launcher:
    __PATH_CONTAINING_THE_NORMALIZED_IMAGE = "/content/drive/My Drive/Informatique/Projets_Informatiques" \
                                             "/Projets_Python/bug_isolation_mosaics_of_pixels/normalized_image"
    __PATH_CONTAINING_THE_DENORMALIZED_IMAGE = "/content/drive/My Drive/Informatique/Projets_Informatiques" \
                                               "/Projets_Python/bug_isolation_mosaics_of_pixels" \
                                               "/denormalized_image"

    def __init__(self):
        pass

    def show_denormalized_image_from_normalized_image(self):
        self.__plot_images_predicted_for_testing()

    def __plot_images_predicted_for_testing(self):
        normalized_images = self.__fetch_the_normalized_image()
        normalized_image = normalized_images[[0, 0, 0]]
        denormalized_image = self.__denormalize(normalized_image)

        figure = plt.figure(figsize=(15, 5))

        plt.subplot(1, 3, 1)
        plt.imshow(denormalized_image[1], interpolation='nearest')
        plt.axis('off')

        plt.tight_layout()
        plt.savefig(self.__PATH_CONTAINING_THE_DENORMALIZED_IMAGE + '/the_denormalized_image')
        plt.close(figure)

    @staticmethod
    def __denormalize(input_data):
        input_data = (input_data + 1) * 127.5
        return input_data.astype(np.uint8)

    def __fetch_the_normalized_image(self):
        print("Loading the normalized image")
        images = []
        loaded_images = self.__load_data(
            Launcher.__PATH_CONTAINING_THE_NORMALIZED_IMAGE,
            ".jpg")
        for img in range(len(loaded_images)):
            images.append(loaded_images[img])
        images = array(images)
        print("/Loading the normalized image")
        return images

    @staticmethod
    def __load_data_from_dirs(dirs, ext):
        files = []
        file_names = []
        count = 0
        for d in dirs:
            for f in os.listdir(d):
                if f.endswith(ext):
                    image = data.imread(os.path.join(d, f))
                    if len(image.shape) > 2:
                        files.append(image)
                        file_names.append(os.path.join(d, f))
                    count = count + 1
        return files

    def __load_path(self, path):
        directories = []
        if os.path.isdir(path):
            directories.append(path)
        for elem in os.listdir(path):
            if os.path.isdir(os.path.join(path, elem)):
                directories = directories + self.__load_path(os.path.join(path, elem))
                directories.append(os.path.join(path, elem))
        return directories

    def __load_data(self, directory, ext):
        files = self.__load_data_from_dirs(self.__load_path(directory), ext)
        return files

print("Executing the program")
launcher = Launcher()
launcher.show_denormalized_image_from_normalized_image()
print("/Executing the program")

The question

Do you know why I get a multicolor mosaic of pixels using this denormalization method?

JarsOfJam-Scheduler
  • 2,809
  • 3
  • 31
  • 70
  • Reading all of this, it seems it has nothing to do with keras/tensorflow/deep-learning? Also the problem could be shown within a single piece of code, I suppose? – ImportanceOfBeingErnest Sep 05 '19 at 21:29
  • Tags are fixed. Thank you! – JarsOfJam-Scheduler Sep 05 '19 at 21:40
  • This is not only about the tags, but the whole 100 lines long story - it looks you can remove a lot of the text as well as at least half of the code, leading to a [mcve] with only the relevant information in the question. Look at how others have presented their problem [here](https://stackoverflow.com/questions/51389995/emphasize-the-blue-in-an-image) or [here](https://stackoverflow.com/questions/42044259/getting-black-plots-with-plt-imshow-after-multiplying-image-array-by-a-scalar). – ImportanceOfBeingErnest Sep 05 '19 at 21:52
  • "minimal reproducible example" doesn't mean "minimal version that encompasses your entire programs logic" --- it means "the bare minimum code to reproduce the exact problem." We don't need code that traverses directories to get at the problem with an image displaying. – alkasm Sep 05 '19 at 22:00
  • ImportanceOfBeingErnest and @alkasm : if you want to be able to see the problem by yourself, you must load from disk the images in RAM, that's why the code that traverses directories is present. Moreover: the problem could be bound to the way I save/load the images? Thus, this part of the code should be definitely present. – JarsOfJam-Scheduler Sep 06 '19 at 07:54
  • @ImportanceOfBeingErnest : concerning the 100 lines long story: I wanted to give you the context, and explain what is the problem according to me, and how I intended to solve it. I re-read my whole question and didn't find any part I should remove. However, I sure can do it if you highlight the part that should be deleted – JarsOfJam-Scheduler Sep 06 '19 at 07:56
  • Sorry, but you are unlikely to get help with the way this question is formatted. First, your code isn't indented correctly, and uses incorrect comments, so it's impossible to copy/paste this code and run it; it will syntax error. Second, you have hardcoded paths which only exist on your OS and your computer, so no one anywhere will be able to copy/paste this and get it to run even if you do fix the syntax errors. Third, this question is unlikely to help anyone in the future because it's too broad, and also too specifically coupled to your exact env. – alkasm Sep 06 '19 at 08:04
  • Stack Overflow in particular is suited for *programming issues*, less suited for *why is my machine learning model spitting out nonsense*. If it's your denormalizing function that is causing the problem, delete everything else except that function and show what it should output vs what it is outputting. For example. – alkasm Sep 06 '19 at 08:08
  • You didn't read the OP: I have explicitly detailed the way to run the program, and have written that changes to the paths must be done (replace them with the paths of your choices). OK for indentation (badly copied/pasted from my pycharm to StackOverflow). "Third, this question is unlikely to help anyone" ===> unlikely =/= never (and , moreover, I already posted a lot of question like this one, even more specific, and they were answered, as for others' questions. . . **please cite the StackOverflow's chart part of what you are complaining about**). – JarsOfJam-Scheduler Sep 06 '19 at 08:29
  • @alkasm You definitely didn't read the OP: this is a programming question, unrelated with deep-learning (that's why I've removed the tags). I explain it several times in the texts. I have isolated the parts of the code which deal only with pure-programming stuff (*i.e.:* not related to machine-learning)... By the way, the three persons who asked for holding my question work in machine-learning: you just saw this question with the (bad) deep-learning tags, and put this question in hold... However I've removed them, making my question totally relevant and correct. – JarsOfJam-Scheduler Sep 06 '19 at 08:31
  • 1
    I am a computer vision engineer and I answer a ton of CV-related questions here. I did fully and completely read the OP. I am aware your question is not machine learning related. My point was your entire context part of the post is completely irrelevant to your problem. Literally every single thing in your post that doesn't go from "image read -> image processed -> image displayed" is irrelevant. Downloading images isn't relevant. Show an example image, show the one function which processes it, and show the output you are getting vs the output you expect. – alkasm Sep 06 '19 at 08:37
  • OK, thank your for your feedback @alkasm! I will take account of your remarks and write another question today. – JarsOfJam-Scheduler Sep 06 '19 at 08:38
  • 1
    @JarsOfJam-Scheduler You don't necessarily need to write a new question, you can edit this one. If you make an edit, it will pop back up in queue for people to review it. If you edit it and not enough people are seeing it, you can post in the Python chat room (sopython.com) and ask for some reopen votes. SO's how-to-ask summarizes it the best IMO - "Pretend you're talking to a busy colleague." FWIW, it's not that I or others don't appreciate the time you put into your question, that's why we've tried to give remedies via comments. We want questions to be helpful on here :). – alkasm Sep 06 '19 at 08:45
  • @alkasm Thank you for your comments, again. I've edited the question. But I couldn't format the whole code: do you know why? :/ – JarsOfJam-Scheduler Sep 06 '19 at 19:25
  • 1
    It's a markdown thing---after the list is defined with the `-` marks, if the stuff in the next block of text is indented (i.e. your code) it is treated as belonging to the list item still. This can be remedied by either adding text after the list, or, as I edited, by putting the code in a code block with backticks instead of indenting. – alkasm Sep 06 '19 at 20:40
  • 1
    I have a few qs: Why are you using a masked array for your images? You have `from numpy.ma import array` --- the `ma` here is "masked array", but you should be using normal arrays here right? These should be `np.array` not `np.ma.array` unless I'm missing something. Also, do you know whether this may be visualization related? Like, have you tried saving the image to disk and viewing it (that is, using something other than matplotlib)? – alkasm Sep 06 '19 at 20:44
  • @alkasm Oh sorry, Pycharm imported the wrong array: you are right. I've edited the OP code. I tried to view the output using the Google Drive's images viewer: yes, the problem is present too :( – JarsOfJam-Scheduler Sep 08 '19 at 18:48
  • ** Now, I load the images from disk to RAM, I normalized my RAM-images, I don't save the normalized images on disk but keep them in RAM instead ; so I denormalize the RAM images, I show them in Matlib.Plot and........... it works....................** I can't save and/or load in RAM the normalized version of the images in a file, I need to always use the RAM. Too bad, it will decrease the calculation speed of my SRGAN but I don't have time anymore to try to debug this problem ^^' – JarsOfJam-Scheduler Sep 09 '19 at 08:40

0 Answers0