I trained a learner to predict the masks of some images, and now I would like to deploy it. I am currently predicting one image at a time using
learn = load_learner(path_learner, 'export.pkl')
for i in range(curr):
img = open_image(f'{test_dir}/{i}.png')
img.resize(128)
mask_pred = learn.predict(img);
mask_pred[0].save(f'{save_mask_dir}/{i}_mask.png')
But it becomes quite slow when the number of images increases, so I would ideally like to do batch predictions via
learn = load_learner(path_learner, 'export.pkl', test = SegmentationItemList.from_folder($TEST_DIR))
preds = learn.get_preds(ds_type=DatasetType.Test)
However I am getting
Exception: Attempting to apply transforms to an empty label. This usually means you are
trying to apply transforms on your xs and ys on an inference problem, which will give you wrong
predictions. Pass `tfms=None, tfm_y=False` when creating your test set.
I’ve looked into this thread for solutions, but it sounds like the only solution is to create a new databunch
But the problem is if I’m deploying this on another computer, I would have to transfer all the data folders + the weights.pth instead of just an export.pkl
You can pass tfm_y=False to load_learner to avoid the error, but I don’t know what your transforms are so the predictions (which will be done on transformed images) might not correspond to your inputs. You should export your Learner after changing its DataBunch with a version that doesn’t have transforms to be safe (note that this requires having all your images at inference having the same size).
for i in range(curr):
img = open_image(f'{save_dir}/{i}.png')
img.resize(128)
mask_pred = learn.predict(img);
mask_pred[0].save(f'{save_mask_dir}/{i}_mask.png')
The first image (mask_0) comes out to be
Maybe it has something to do with the transformations? Since I am scaling the input images to be 128x128
Edit: upon further digging, it looks like the data is being shuffled when passing in the test dataset. My data is ordered such that 0.png is followed by 1.png etc. Is there a way to keep the order when doing batch predictions?
Can’t help with the first one. But on the second turn it into a numpy array and then you can create a PILImage out of it. IE: im = mask[0].cpu().numpy() and then
plt.imshow(im)
plt.save('mask.png)
Let me know if this doesn’t and what error it gives
There appears to be a mismatch between the 2 methods
If I use mask.save('mask.png') the image comes out to
With matplotlib.image.imsave('mask.png', mask) The image comes out to
Which messes up some steps further down the pipeline. I would like to have the mask image in the same format as the 1st image, particularly for using the fastai mask.resize() method
Facing the same issue. Predictions are correct but in random order.
Tries ordered = True but it woks only for the text models.
I found several other threads with the same issue.