I am glad to say that I have managed to train a U-Net model. However, I get an error when trying to predict the masks. Please have a look at the code below. This relates to this question where the training part could be found.
Code(Predicting Part):
#define augmentations
inference_transform = A.Compose([
A.Resize(256, 256, always_apply=True),
A.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),
ToTensorV2()
])
ā
#define function for predictions
def predict(model, img, device):
model.eval()
with torch.no_grad():
images = img.to(device)
output = model(images)
predicted_masks = (output.squeeze() >= 0.5).float().cpu().numpy()
return(predicted_masks)
ā
#define function to load image and output mask
def get_mask(img_path):
image = cv2.imread(img_path)
#assert image is not None
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
original_height, original_width = tuple(image.shape[:2])
image_trans = inference_transform(image = image)
image_trans = image_trans["image"]
image_trans = image_trans.unsqueeze(0)
image_mask = predict(unet, image_trans, device)
#image_mask = image_mask.astype(np.int16)
image_mask = cv2.resize(image_mask, original_height, original_width,
interpolation=cv2.INTER_NEAREST)
return(image_mask)
############################
#image example
example_path = "../input/test-image/10078.tiff"
image = cv2.imread(example_path)
#assert image is not None
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
mask = get_mask(example_path)
masked_img = image*np.expand_dims(mask, 2).astype("uint8")
#plot the image, mask and multiplied together
fig, (ax1, ax2, ax3) = plt.subplots(3)
ax1.imshow(image)
ax2.imshow(mask)
ax3.imshow(masked_img)
Output:
---------------------------------------------------------------------------
error Traceback (most recent call last)
/tmp/ipykernel_18/2666594668.py in <module>
5 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
6
----> 7 mask = get_mask(example_path)
8
9 masked_img = image*np.expand_dims(mask, 2).astype("uint8")
/tmp/ipykernel_18/1641483929.py in get_mask(img_path)
30 image_mask = image_mask.astype(np.int16)
31 image_mask = cv2.resize(image_mask, original_height, original_width,
---> 32 interpolation=cv2.INTER_NEAREST)
33
34 return(image_mask)
error: OpenCV(4.5.4) :-1: error: (-5:Bad argument) in function 'resize'
> Overload resolution failed:
> - Can't parse 'dsize'. Input argument doesn't provide sequence protocol
> - Can't parse 'dsize'. Input argument doesn't provide sequence protocol
Would anyone be able to help me in this regards please?
Thanks & Best Regards Schroter Michael