remove background using u2net produced mask

Viewed 224

I am trying to remove background from an image. For this purpose I am using U2NET. I am writing the network structure using Tensorflow by following this repository. I have changed the model architecture according to my needs. It takes 96x96 image and produces 7 masks. I am taking 1st mask (out of 7) and multiplying it against the all channels of original 96x96 image.

The code that predicts 7 masks is:

img = Image.open(os.path.join('DUTS-TE','DUTS-TE-Image', test_x_names[90]))
copied = deepcopy(img)
copied = copied.resize((96,96))
copied = np.expand_dims(copied,axis=0)
preds = model.predict(copied)
preds = np.squeeze(preds)

"preds[0]" is:

predicted mask

Multiplying the mask against the original image produces: masked image and corresponding code is ("img2" is original image):

img2 = np.asarray(img2)
immg = np.zeros((96,96,3), np.uint8)
for i in range(0,3):
  immg[:,:,i] = img2[:,:,i] * preds[0]
plt.imshow(immg)
plt.show()

If i binarize the mask and then multiply it against the original image it produces : enter image description here and corresponding code is :

frame = binarize(preds[0,:,:], threshold = 0.5)
img2 = np.asarray(img2)
immg = np.zeros((96,96,3), np.uint8)
for i in range(0,3):
  immg[:,:,i] = img2[:,:,i] * frame
plt.imshow(immg)
plt.show()

Multiplying the original image with mask or binarized mask do not segment the foreground properly from the background. So, what can be done? Am I missing something?

0 Answers
Related