[newbie] save() produces artifacts (clipping?)

Hi folks, first post here. Please forgive my lack of very basic python knowledge, but you have to start somewhere, I guess.

Fast.ai is really helping me understand the possibilities and ways of working with AI. I’m currently experimenting with lesson7-superres. I’ve trained a model with my own dataset and results are pretty decent for a first attempt.

Problem is, I want to save some of the predictions as images.

In a cell, with slightly changed code from the course, this shows me the blurred image, the deblurred version and the ground truth:

img = open_image(fn); img.shape
p,img_hr,b = learn.predict(img)
show_image(img, figsize=(5,5), interpolation=‘nearest’);
Image(img_hr).show(figsize=(5,5))
show_image(img2, figsize=(5,5), interpolation=‘nearest’);

And it works like a charm.

Then I manage to get the save command to work, like this:

Image(img_hr).save(ccc) #ccc is the path and file name string

But when I look at the results they have artifacts every time.

00021820

Is this clipping? Results usually appear in the whitest and darkest parts of the image.

How could I solve this?
Sorry if I can’t recall any part of the course where this is mentioned.

Also, what would be the way to save an image img as created by the code above?
Image(img).save(path) doesn’t work.

Again, pardon my newbie questions. And my poor English. Greetings from Spain.

Yes, this appears to be a clipping problem.
At least I could solve it with the following save function:

def save_img(img, fn):
  x = np.minimum(np.maximum(image2np(img.data*255), 0), 255).astype(np.uint8)
  PIL.Image.fromarray(x).save(fn)
2 Likes

Thanks for that. Had the same issue -also with superres btw- and that did the trick!