I have 10 class folders consisting of around 2-3 thousands satellite pictures (of size 100 K) in each folder to classify using deep learning like fastai library package in python. Names of these class folders are 'AnnualCrop', 'Forest', 'HerbaceousVegetation', 'Highway', 'Industrial', 'Pasture', 'PermanentCrop', 'Residential', 'River', 'SeaLake' respectively. In first phase I want to see the classes size by displaying bar chart of each class. In fact I can display the bar charts only issue is to proper index names on x-axis key values against each class which is displaying the full path of that folder rather only class names. Like for each bar it should be AnnualCrop, Forest and so on like that but It is displaying the full path for each class (e.g,'C:\Users\oaamer\Desktop\sample\EuroSATallBands\train\AnnualCrop')

I tried to split with different ways but could not do it. Here classes_num.keys() displays each whole string path for each class against each bar which does not fit suitable in the bar chart. How can i break this string and displays only last separated part of it as class type for each bar.

    import matplotlib.pyplot as plt

Path where 10 class folders are placed namely 'AnnualCrop', 'Forest', 'HerbaceousVegetation', 'Highway', 'Industrial', 'Pasture', 'PermanentCrop', 'Residential', 'River', 'SeaLake'.

    PATH=Path('C:/Users/oaamer/Desktop/sample/EuroSATallBands/train/')

Splitting of these folders has been done by this code.

    classes=[str(f).split('/')[-1] for f in list(PATH.iterdir())]

{'C:\Users\oaamer\Desktop\sample\EuroSATallBands\train\AnnualCrop': 3000, 'C:\Users\oaamer\Desktop\sample\EuroSATallBands\train\Forest': 3000, 'C:\Users\oaamer\Desktop\sample\EuroSATallBands\train\HerbaceousVegetation': 3000, 'C:\Users\oaamer\Desktop\sample\EuroSATallBands\train\Highway': 2500, 'C:\Users\oaamer\Desktop\sample\EuroSATallBands\train\Industrial': 2500, 'C:\Users\oaamer\Desktop\sample\EuroSATallBands\train\Pasture': 2000, 'C:\Users\oaamer\Desktop\sample\EuroSATallBands\train\PermanentCrop': 2500, 'C:\Users\oaamer\Desktop\sample\EuroSATallBands\train\Residential': 3000, 'C:\Users\oaamer\Desktop\sample\EuroSATallBands\train\River': 2500, 'C:\Users\oaamer\Desktop\sample\EuroSATallBands\train\SeaLake': 3000}


#Creating dictionary for each folder class

    classes_num = {}
    for i in classes:
        folders = train_path/i
        classes_num[i] = len(list(folders.iterdir()))
        print(classes_num)

# For displaying Bar charts
    plt.bar(classes_num.keys(), classes_num.values(), color='green')
    plt.tight_layout()

1 Answers

0
match On

you probably want to use the os.path builtin library:

from os import path

subdir = path.basename('C:/Users/oaamer/Desktop/sample/EuroSATallBands/train/'.strip('/'))
print(subdir)

train