Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
PySegCNN
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
earth_observation_public
PySegCNN
Commits
f8e77f34
Commit
f8e77f34
authored
4 years ago
by
Frisinghelli Daniel
Browse files
Options
Downloads
Patches
Plain Diff
Implemented plot function for class distributions.
parent
8199ac71
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
pysegcnn/core/graphics.py
+88
-3
88 additions, 3 deletions
pysegcnn/core/graphics.py
with
88 additions
and
3 deletions
pysegcnn/core/graphics.py
+
88
−
3
View file @
f8e77f34
...
...
@@ -35,9 +35,9 @@ from pysegcnn.core.utils import accuracy_function, check_filename_length
from
pysegcnn.main.train_config
import
HERE
# plot font size configuration
SMALL
=
1
0
MEDIUM
=
1
2
BIG
=
1
4
SMALL
=
1
2
MEDIUM
=
1
4
BIG
=
1
6
# controls default font size
plt
.
rc
(
'
font
'
,
size
=
MEDIUM
)
...
...
@@ -562,6 +562,91 @@ def plot_loss(state_file, figsize=(10, 10), step=5,
return
fig
def
plot_class_distribution
(
ds
,
figsize
=
(
16
,
9
),
alpha
=
0.5
):
"""
Plot the spectral distribution of the different classes in ``ds``.
Parameters
----------
ds : :py:class:`pysegcnn.core.dataset.ImageDataset`
An instance of the dataset to plot the class distribution over the
different spectral bands. Make sure to initialize ``ds`` with the
parameter ``tile_size=None``, which conserves the original size of
each image in the dataset.
Returns
-------
cls_df : :py:class:`pandas.DataFrame`
The class distribution DataFrame.
"""
# compute class distribution
cls_ds
=
ds
.
class_distribution
()
# drop classes which are not represented in the dataset
cls_ds
=
{
k
:
v
for
k
,
v
in
cls_ds
.
items
()
if
np
.
any
(
v
)}
# labels for the different classes
labels
=
[
ds
.
labels
[
cls_id
][
'
label
'
]
for
cls_id
in
cls_ds
.
keys
()]
# number of spectral bands in the dataset
nbands
=
len
(
ds
.
use_bands
)
# create a figure based on the number of spectral bands in the dataset
fig
,
axes
=
plt
.
subplots
(
min
(
3
,
nbands
),
int
(
np
.
ceil
(
max
(
1
,
nbands
/
3
))),
figsize
=
figsize
,
sharex
=
True
,
sharey
=
True
)
axes
=
axes
.
flatten
()
# iterate over the different bands
for
band
in
range
(
nbands
):
# current axis
ax
=
axes
[
band
]
# get the spectral data for each class
data
=
[
x
[:,
band
]
for
x
in
cls_ds
.
values
()]
# plot spectral distribution of the classes in the current band
bplot
=
ax
.
boxplot
(
data
,
labels
=
labels
,
patch_artist
=
True
,
whis
=
[
5
,
95
],
showfliers
=
False
)
# set axis y-limits: physical limits are (0, 1) for reflectance data
ax
.
set_ylim
(
0
,
1
)
# set colors of the boxes for the classes
for
k
,
artists
in
bplot
.
items
():
# the artists to color
if
k
in
[
'
boxes
'
,
'
medians
'
]:
# iterate over the artists
for
c
,
art
in
enumerate
(
artists
):
# line artists
if
isinstance
(
art
,
matplotlib
.
lines
.
Line2D
):
# set the colors of the lines in the boxplot
art
.
set_color
(
ds
.
labels
[
c
][
'
color
'
])
# patch artists
elif
isinstance
(
art
,
matplotlib
.
patches
.
Patch
):
# set the colors of the patches
art
.
set_facecolor
(
ds
.
labels
[
c
][
'
color
'
])
art
.
set_alpha
(
alpha
)
# add name of the spectral band to the plot
ax
.
text
(
x
=
0.6
,
y
=
0.95
,
s
=
'
({})
'
.
format
(
ds
.
use_bands
[
band
]),
ha
=
'
left
'
,
va
=
'
top
'
,
weight
=
'
bold
'
)
# adjust space between subplots
fig
.
subplots_adjust
(
hspace
=
0.075
,
wspace
=
0.025
)
# remove empty axes
for
ax
in
axes
:
if
not
ax
.
lines
:
fig
.
delaxes
(
ax
)
return
fig
class
Animate
(
object
):
"""
Easily create animations with :py:mod:`matplotlib`.
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment