Saturday, 9 October 2021

How to use fastai unet_learner?

Summary

  • Details Utilize fastai(v2) unet_learner function to utilize resnet34 in transfer learning.
  • Expected Results Learner that is passes building phase (.fine_tune(n)).
  • Errors IndexError: Target 20 is out of bounds.

Attempted Remedy(s)

  1. Ran the same processes as shown here without issue. The process ran smoothly, with the learner completing the fine_tuning and predictions without issue (on the camvid_tiny dataset).

  2. Checked my processed data, including 'labels', 'images', and 'codes' against those in the camvid proccess, and they are near identical (my 21 classes vs camvid's ~30, images & labels are 256x256 vs camvid's 96, 128).

  3. Confirmed label values within dls are not 0/255 (at noted here & here )

for i in range(len(lnames)):
    y = Image.open(lnames[i])
    y_array = np.array(y)
    print(np.unique(y_array))

[20]
[20]
[20]
[ 5  7  8 11 12 13 14 17 20]
[ 5 12 13 14 15 16 17 20]
...
[14 17 20]
[14 17 20]
[ 8  9 10 11 12 13 14 16 17 19 20]
[ 1  2  3  4  7  8  9 10 11 12 13 14 16 17 18 19 20]
[ 2  3  9 10 12 13 14 16 17 19 20]
[ 1  2  3  4  7  8  9 10 11 12 13 14 16 17 18 19 20]

Code

import os
import json
import numpy as np
from pathlib import Path
from fastai.vision.all import *

>>path = Path(r"D:\EuroSATDS")
>>json_file = r"D:\EuroSATDS\esri_accumulated_stats.json"
>>>with open(json_file, 'r') as f:
    data = json.load(f)
>>>classes = data['Classes']
>>>classes_list = []
>>>classes_value = []
>>>for i in classes:
    x = i['ClassName']
    y = i['ClassValue']
    classes_list.append(x)
    classes_value.append(y)
>>>classes_list[20]
'Palustrine Aquatic Bed'
>>>codes = np.asarray(classes_list, dtype='<U17')
>>>codes
array(['High Intensity De', 'Medium Intensity ', 'Low Intensity Dev',
       'Developed Open Sp', 'Cultivated', 'Pasture/Hay', 'Grassland',
       'Deciduous Forest', 'Evergreen Forest', 'Mixed Forest',
       'Scrub/Shrub', 'Palustrine Forest', 'Palustrine Scrub/',
       'Palustrine Emerge', 'Estuarine Foreste', 'Estuarine Scrub/S',
       'Estuarine Emergen', 'Unconsolidated Sh', 'Bare Land', 'Water'],
      dtype='<U17')
>>fnames = get_image_files(path/"images")
>>fnames[0]
Path('../000000000.jpg')
>>def label_func(fn): return pathB/"labels"/f"{fn.stem}_P.png"
>>dls = SegmentationDataLoaders.from_label_func(pathB, bs=8, fnames = fnames, label_func = label_func, codes = codes)
>>dls.show_batch(max_n=6)

enter image description here

>> learn = unet_learner(dls, resnet34)
>> learn.fine_tune(1)
IndexError: Target 20 is out of bounds.

IndexError                                Traceback (most recent call last)
~TEMP/ipykernel_14508/3714593663.py in <module>
      2 import time
      3 start = time.time()
----> 4 learn.fine_tune(1)
      5 end = time.time()
      6 print("The time of execution of above program is :", end-start)  



from How to use fastai unet_learner?

No comments:

Post a Comment