from fastai.vision.all import *
CamVid Demo
This is a small demo showing that the functionality of misas is not at all limited to the domain of medical imaging. However, the main use case for the authors of misas is currently medical imaging, therefore this is just a proof of concept. It should give you an idea on how to work with a new dataset. It can be extended if users need it. The example builds on the camvid example in the fastai1 documentation.
Please reach out via GitHub if you have problems using misas on your own dataset.
= untar_data(URLs.CAMVID_TINY) path
= np.loadtxt(path/'codes.txt', dtype=str)
codes codes
array(['Animal', 'Archway', 'Bicyclist', 'Bridge', 'Building', 'Car',
'CartLuggagePram', 'Child', 'Column_Pole', 'Fence', 'LaneMkgsDriv',
'LaneMkgsNonDriv', 'Misc_Text', 'MotorcycleScooter', 'OtherMoving',
'ParkingBlock', 'Pedestrian', 'Road', 'RoadShoulder', 'Sidewalk',
'SignSymbol', 'Sky', 'SUVPickupTruck', 'TrafficCone',
'TrafficLight', 'Train', 'Tree', 'Truck_Bus', 'Tunnel',
'VegetationMisc', 'Void', 'Wall'], dtype='<U17')
= get_image_files(path/"images") fnames
def label_func(fn): return path/"labels"/f"{fn.stem}_P{fn.suffix}"
= fnames[0]
cam_fn = label_func(fnames[0])
mask_fn
= lambda: Image.open(cam_fn).convert("RGB")
cam_img = lambda: Image.open(mask_fn).convert("I") mask
= SegmentationDataLoaders.from_label_func(
dls =8, fnames = fnames, label_func = label_func, codes = codes
path, bs )
=6) dls.show_batch(max_n
= unet_learner(dls, resnet34) learn
Downloading: "https://download.pytorch.org/models/resnet34-b627a593.pth" to /home/markus/.cache/torch/hub/checkpoints/resnet34-b627a593.pth
6) learn.fine_tune(
epoch | train_loss | valid_loss | time |
---|---|---|---|
0 | 3.004354 | 2.548569 | 00:30 |
epoch | train_loss | valid_loss | time |
---|---|---|---|
0 | 2.048129 | 1.562803 | 00:37 |
1 | 1.704228 | 1.354810 | 00:33 |
2 | 1.483399 | 1.031199 | 00:30 |
3 | 1.315127 | 0.921900 | 00:27 |
4 | 1.164001 | 0.827559 | 00:33 |
5 | 1.051317 | 0.808568 | 00:32 |
=6, figsize=(7,8)) learn.show_results(max_n
= SegmentationInterpretation.from_learner(learn)
interp =3) interp.plot_top_losses(k
= DataBlock(blocks=(ImageBlock, MaskBlock(codes)),
camvid = get_image_files,
get_items = label_func,
get_y =RandomSplitter(),
splitter=aug_transforms(size=(120,160))) batch_tfms
= camvid.dataloaders(path/"images", path=path, bs=8) dls
=6) dls.show_batch(max_n
# learn.save('mini_train')
128,128))) plt.imshow(cam_img().resize((
<matplotlib.image.AxesImage>
from misas.core import *
from misas.core import default_cmap
= lambda item: item.resize((128,128)) learn.prepareSize
class Fastai_model:
def __init__(self, learner):
self.trainedModel = learner
self.resize128 = lambda x: x.resize ((128,128))
self.trainedModel.remove_cbs(ProgressCallback)
def prepareSize(self, item):
return self.resize128(item)
def predict(self, image):
= PILImage.create(np.array(image))
image = self.trainedModel.predict(image)
output = PILImage.create(output [0])
output = Image.fromarray(np.array(output)) #mode="I"
output return output
= Fastai_model(learn) Cam_vid
=31, vmin=0) plot_series(get_rotation_series(cam_img(), Cam_vid), vmax
=31, vmin=0, nrow=2) plot_series(get_zoom_series(cam_img(), Cam_vid), vmax
= np.unique(np.array(Cam_vid.predict(cam_img()))) found_classes
codes[found_classes]
array(['Building', 'Car', 'LaneMkgsDriv', 'Road', 'Sidewalk', 'Sky',
'Tree', 'Void', 'Wall'], dtype='<U17')
= eval_rotation_series(cam_img(),mask(),Cam_vid,components=codes) result
When plotting the evaluation series, it makes sense to only plot classes that actually occur.
"deg",codes[found_classes])]) plot_eval_series(result[np.append(