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