readme: ---
  tags:
  - fastai
  ---
  
  # Model card
  
  ## Model description
  Fastai `unet` created with `unet_learner` using `resnet34`
  
  ## Intended uses & limitations
  This is only used for demonstration of fine tuning capabilities with fastai. It may be useful for further research. This model should **not** be used for gastrointestinal polyp diagnosis.
  
  ## Training and evaluation data
  The model was trained on [Kvasir SEG dataset](https://datasets.simula.no/kvasir-seg/). Kvasir SEG is an open-access dataset of gastrointestinal polyp images and corresponding segmentation masks, manually annotated and verified by an experienced gastroenterologist.  
  20% of the data set were used as validation set and 80% as training set.  
    
  ### Model training details:
  
  #### Data pre-processing
  Masks were converted to 1 bit images: 0 for background and 1 for mask using
  ```python
  Path('/notebooks/Kvasir-SEG/masks1b-binary').mkdir(parents=True, exist_ok=True)
  for img_path in tqdm(get_image_files(path/'masks')):
      img = Image.open(img_path)
      thresh = 127
      fn = lambda x : 1 if x > thresh else 0
      img1b = img.convert('L').point(fn)
      img1b.save(path/'masks1b-binary'/f'{img_path.stem}.png')
  ```
  #### Data loaders
  `SegmentationDataloaders` were used to create fastai data loaders
  ```python
  def label_func(fn): return path/'masks1b-binary'/f'{fn.stem}.png'
  dls = SegmentationDataLoaders.from_label_func(
      path, bs=24, fnames = get_image_files(path/'images'),
      label_func = label_func,
      codes = list(range(2)),
      item_tfms=Resize(320),
      batch_tfms=aug_transforms(size=224, flip_vert=True)
  )
  ```
  An sample of training images:
  ![show_batch](show_batch.png)
  
  #### Learner
  Create learner with Dice and JaccardCoeff metrics
  ```python
  learn = unet_learner(dls, resnet34, metrics=[Dice, JaccardCoeff]).to_fp16()
  ```
  
  #### Learning rate
  Learning rate finder
  ![lr_find](lr_find.png)
  
  #### Fine tuning
  Fine tuning for 12 epochs  
  `learn.fine_tune(12, 1e-4)`
  ```
  epoch 	train_loss 	valid_loss 	dice 	jaccard_coeff 	time
  0 	0.582160 	0.433768 	0.593044 	0.421508 	00:38
  epoch 	train_loss 	valid_loss 	dice 	jaccard_coeff 	time
  0 	0.307588 	0.261374 	0.712569 	0.553481 	00:38
  1 	0.261775 	0.232007 	0.714458 	0.555764 	00:38
  2 	0.246054 	0.227708 	0.781048 	0.640754 	00:38
  3 	0.224612 	0.185920 	0.796701 	0.662097 	00:39
  4 	0.208768 	0.179064 	0.821945 	0.697714 	00:39
  5 	0.192531 	0.171336 	0.816464 	0.689851 	00:39
  6 	0.177166 	0.167357 	0.820771 	0.696023 	00:39
  7 	0.168222 	0.158182 	0.838388 	0.721745 	00:39
  8 	0.155157 	0.161950 	0.829525 	0.708709 	00:39
  9 	0.148792 	0.164533 	0.828383 	0.707043 	00:38
  10 	0.143541 	0.158669 	0.833519 	0.714559 	00:39
  11 	0.140083 	0.159437 	0.832745 	0.713422 	00:38
  ```
  ![loss_graph](loss_graph.png)
  
  #### Results
  Visualization of results  
  Target/Prediction
  ![show_results](show_results.png)
  
  Top losses
  ![top_losses](top_losses.png)
  
  #### Libraries used:
  `huggingface_hub.__version__`  
  `'0.8.1'`  
  
  `fastai.__version__`  
  `'2.6.3'`  