0

I have one image as input and two masks, I used multi label Unet, the training process works without any problem but when I'm trying to get the prediction Id encountered the error (KeyError: ) for test generator I used the pice of cod

def testGenerator(test_path= "data/membrane/test/image",num_image = 1584,target_size = (224,224),flag_multi_class = False,as_gray = False):
for i in range(num_image):
    img = io.imread(os.path.join(test_path,"%d.jpg"%i),as_gray = as_gray)
    img = img / 255.
    img = trans.resize(img,target_size)
    img = np.reshape(img,img.shape) if (not flag_multi_class) else img
    img = np.reshape(img,(1,)+img.shape)
    yield img

and for Visualization I used

def labelVisualize(num_class,color_dict,img):
     img = img[:,:,0] if len(img.shape) == 3 else img
     img_out = np.zeros(img.shape + (3,))
     for i in range(num_class):
     img_out[img == i,:] = color_dict[i]
     return img_out / 255


def saveResult(save_path,npyfile,flag_multi_class = False,num_class = 2):
    for i,item in enumerate(npyfile):
         img = labelVisualize(num_class,COLOR_DICT,item) if flag_multi_class else item[:,:,0]
         io.imsave(os.path.join(save_path,"%d_predict.tif"%(i)), os.path.join(save_path,"%d_predict.tif"%(i)),skimage.img_as_ubyte(img))

the traceback as shown:

KeyError                       Traceback (most recent call last)
<ipython-input-29-60fe459f67b9> in <module>
      4 results = model.predict_generator(testGene,10,verbose=1)
      5 #saveResult("data/membrane/test/results",results)
----> 6 saveResult("data/membrane/test/results/road",results)
      7 saveResult("data/membrane/test/results/cl",results)

<ipython-input-26-6c6016bc75cc> in saveResult(save_path, npyfile, flag_multi_class, num_class)
      26     for i,item in enumerate(npyfile):
      27         img = labelVisualize(num_class,COLOR_DICT,item) if  flag_multi_class else item[:,:,0]
 ---> 28         io.imsave(os.path.join(save_path,"%d_predict.tif"%    (i)), os.path.join(save_path,"%d_predict.tif"%  (i)),skimage.img_as_ubyte(img))

/anaconda3/lib/python3.6/site-packages/skimage/io/_io.py in imsave(fname, arr, plugin, **plugin_args)
     137         if fname.lower().endswith(('.tiff', '.tif')):
     138             plugin = 'tifffile'
 --> 139     if is_low_contrast(arr):
     140         warn('%s is a low contrast image' % fname)
     141     if arr.dtype == bool:

  /anaconda3/lib/python3.6/site-packages/skimage/exposure/exposure.py in is_low_contrast(image, fraction_threshold, lower_percentile, upper_percentile, method)
     501         image = rgb2gray(image)
     502 
 --> 503     dlimits = dtype_limits(image, clip_negative=False)
     504     limits = np.percentile(image, [lower_percentile, upper_percentile])
     505     ratio = (limits[1] - limits[0]) / (dlimits[1] - dlimits[0])

 /anaconda3/lib/python3.6/site-packages/skimage/util/dtype.py in dtype_limits(image, clip_negative)
      55         warn('The default of `clip_negative` in `skimage.util.dtype_limits` '
      56              'will change to `False` in version 0.15.')
 ---> 57     imin, imax = dtype_range[image.dtype.type]
      58     if clip_negative:
      59         imin = 0

KeyError: <class 'numpy.str_'>

I need to get two predicted masks for each image in the test dataset and save it into separated folders, any idea to solve this problem will be appreciate it, thank you in advance

tamara
  • 1
  • 3
  • Post the traceback and show us which line of your code fails. – John Zwinck Apr 12 '20 at 02:48
  • @JohnZwinck, thank you for your reply, i edited the question and added the traceback , I think my error probaply formed from this line (io.imsave(os.path.join(save_path,"%d_predict.tif"%(i)), os.path.join(save_path,"%d_predict.tif"%(i)),skimage.img_as_ubyte(img))), i didn't know how to separate the two predicted masks and save it into two different folders, the predicted masks supposed to have the same number of the input image, ex: image_0, predicted_mask_0, predicted_mask_0 , thank you in advance – tamara Apr 12 '20 at 15:46

1 Answers1

0

You are passing the same argument os.path.join(save_path,"%d_predict.tif"%(i)) twice to imsave(), but it should be passed only once. This is why nothing works, because when imsave() checks what type of data your array contains, the array argument is actually the filename a second time, and that doesn't make sense.

Pass the file path just once to imsave().

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • thank you for your reply , when I passed the imsave argument once I've got (ValueError: Invalid number of channels in image array.) , when id tried to solve it in different way I've got the problem mentioned here(https://stackoverflow.com/questions/60861264/wrong-predicted-images-size-from-predict-generator-for-multi-label-unet), I'm sorry I'm not good in coding but the reason Id passed the imsave with two paths BC I want to save two predicted masks from same image into two different folders and by far i dont know how to do that, thank you in advance – tamara Apr 13 '20 at 16:22
  • If you want to save two files, just call `imsave()` twice. I'm glad to see this solved your first problem, perhaps you'd consider accepting this answer by clicking the checkmark on the left. – John Zwinck Apr 13 '20 at 23:52
  • when i called imsave twice I've got the same problem here as i mentioned before (https://stackoverflow.com/questions/60861264/wrong-predicted-images-size-from-predict-generator-for-multi-label-unet), unfortunately non of my problems solved yet all these scenarios is just to find a way around to solve it – tamara Apr 14 '20 at 00:43