Code
try: import gradio as gr
except ModuleNotFoundError:
!pip install -Uq gradio
Satyabrata pal
Why do we need to test our machine learning model? Didn’t we test it during the training time using the validation set? Well! we did. Kind of, but that validation set was created by us from the data that we had created. Even when you do test your model on a test set, it might still have the same biases as the validation set and training set. To make sure that your model behaves correctly, we will have to test the trained model on a variety of data taken from the wild.
I will have to re-create the datablock and the model code here as well. Let’s do that. We will use the functions from my library fastaibreadcrumbs and some fucntions that we used in chapter 2.
I will use a picture taken from my mobile phone for this. The reason is that the target users may use a deblurring app to deblurify the pictures they take on their mobile phones. One reason for this assumption is that people would want to deblurify images which are blurred, right and most of the times we take images with our mobile deives.
for your test you can click any picture from your phone and put in the above “test” path or replace the “test” path in the above code with your path.
Just like chapter2, we will recreate our datalaoders and learner.
def get_dls(sz:int,bs:int, src):
item_tfms = Resize(sz)
batch_tfms = [Normalize.from_stats(*imagenet_stats)]
get_y = lambda x: x.relative_to(config['path_crappy'])
files_crappy = get_image_files(src)
dls= get_unet_dls(bs, source = files_crappy, get_y = get_y,
splitter = RandomSplitter(), item_tfms = item_tfms,
batch_tfms = batch_tfms)
return dls
def get_inf_model(dls, model:str):
unet_learn = unet_learner(dls, models.resnet34, loss_func=F.l1_loss,
blur=True, norm_type=NormType.Weight).load(model)
return unet_learn
def save_pred(path:str, dest:str,learner):
path = Path(path)
dest = Path(dest)
preds = learner.predict(path)
arr = preds[0].numpy().transpose(1,2,0).astype(np.uint8)
dest.mkdir(parents=True, exist_ok=True)
Image.fromarray(arr).save(dest/path.name)
return dest/path.name
Like last time let’s also create the fucntion to save our predictions.
We will create a gradio Ui to visually test our results. This is the same UI as chapter 2.
This is a typical example of model performing well on training data but not performing good on real world data.
I trained the model on data where the motion blur was simulated only in a single direction but in real world camera shake/motion blur in an image can be in any direction (horizontal, vertical etc.). So while training the model, this thing should be kept in mind.
Secondly, my training data only contained high quality images and thus there was no sample which would tell the model what todo in case noise is available in the image (this is something that I can look into in later iterations.).
The model that I am using is trained on 256*256 px images. I think I should train it on a bit higher size images as well.
I will go back to my training and then train it with these corrections in training data and I will take a look into some new architectures which can predict the noise and motion blurs better then the present architecture.