I am currently working on pascal voc dataset. I used Unets for getting segmentation masks from the given inputs. Here is github gist of my notebook
As we can see from the notebook for given input image I am getting correct segmentation map. In this notebook I am using fastai’s load()
to load my pretrained model. I have used data.export()
to export all configurations of my data in file called as export.pkl
.
Now I want to deploy this as a web-app. I created flask-app for this deployment. I placed export.pkl
file in my flask-app and pretrained model stage-1_224
in models folder. Here, since I need to make predictions on a single image, so I am creating an empty databunch using ImageDataBunch.load_empty()
and passing fname=export1.pkl
(export1.pkl is alias to export.pkl). Here is code snippet of my model in flask-app:
from fastai import *
from fastai.vision import *
# from utils import SegmentationProcessor,SegmentationLabelList,SegmentationItemList
import pathlib
import matplotlib.pyplot as plt
from PIL import Image
class UnetSeg():
def __init__(self):
self.data = ImageDataBunch.load_empty(pathlib.PosixPath('.'),fname='export1.pkl')
self.learn = unet_learner(self.data,models.resnet34,metrics=self.custom_acc,loss_func=self.custom_loss).load('stage_1_224',strict=False)
# self.learn.load('stage-1_224_opt_f',strict=False)
def custom_loss(self,y_hat,y):
y = y.squeeze(1)
loss = nn.CrossEntropyLoss(ignore_index=255)
return loss(y_hat,y)
# data.c = 1
def custom_acc(self,input,targs):
targs = targs.squeeze(dim=1)
input = input.argmax(dim=1)
return (input.flatten()==targs.flatten()).float().mean()
def get_seg(self,img_path):
img = open_image(img_path)
pred = self.learn.predict(img)
return pred
u1 = UnetSeg()
print("Empty databunch initialization done....\n\n",u1.data)
print("Enter the path of input image = \n")
path = input()
y = u1.get_seg(path)
print("Plotting input image = \n\n")
# plt1 = plt.figure()
# plt.imshow(Image.open(path))
# plt.show()
print("Plotting predicted seg_mask = \n\n")
temp = np.array(y[0].data)
temp = temp.squeeze()
plt.imshow(temp)
plt.show()
After running this script, I get inconsistent predictions i.e. predictions from notebook doesn’t match with predictions from the above script.
Output of above script:
Why is there is mismatch in predictions? Are my pretrained weights not loading properly?