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
6afa2221
"src/components/Dashboard/Dashboard.tsx" did not exist on "b331727c43ce5f0bbf9b2d66091807298db74584"
Commit
6afa2221
authored
4 years ago
by
Frisinghelli Daniel
Browse files
Options
Downloads
Patches
Plain Diff
Moved utility functions of ImageDataset class to utils module
parent
a1de5111
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
pytorch/utils.py
+134
-0
134 additions, 0 deletions
pytorch/utils.py
with
134 additions
and
0 deletions
pytorch/utils.py
+
134
−
0
View file @
6afa2221
...
...
@@ -8,6 +8,140 @@ Created on Tue Jul 14 15:02:23 2020
import
re
import
datetime
# externals
import
gdal
import
numpy
as
np
# the following functions are utility functions for common image
# manipulation operations
# this function reads an image to a numpy array
def
img2np
(
path
,
tile_size
=
None
,
tile
=
None
):
# open the tif file
if
path
is
None
:
print
(
'
Path is of NoneType, returning.
'
)
return
img
=
gdal
.
Open
(
path
)
# check whether to read the image in tiles
if
tile_size
is
None
:
# create empty numpy array to store whole image
image
=
np
.
empty
(
shape
=
(
img
.
RasterCount
,
img
.
RasterYSize
,
img
.
RasterXSize
))
# iterate over the bands of the image
for
b
in
range
(
img
.
RasterCount
):
# read the data of band b
band
=
img
.
GetRasterBand
(
b
+
1
)
data
=
band
.
ReadAsArray
()
# append band b to numpy image array
image
[
b
,
:,
:]
=
data
else
:
# check whether the image is evenly divisible in square tiles
# of size (tile_size x tile_size)
ntiles
=
is_divisible
((
img
.
RasterXSize
,
img
.
RasterYSize
),
tile_size
)
# get the indices of the top left corner for each tile
topleft
=
tile_offsets
((
img
.
RasterYSize
,
img
.
RasterXSize
),
tile_size
)
# check whether to read all tiles or a single tile
if
tile
is
None
:
# create empty numpy array to store all tiles
image
=
np
.
empty
(
shape
=
(
ntiles
,
img
.
RasterCount
,
tile_size
,
tile_size
))
# iterate over the tiles
for
k
,
v
in
topleft
.
items
():
# iterate over the bands of the image
for
b
in
range
(
img
.
RasterCount
):
# read the data of band b
band
=
img
.
GetRasterBand
(
b
+
1
)
data
=
band
.
ReadAsArray
(
v
[
1
],
v
[
0
],
tile_size
,
tile_size
)
# append band b to numpy image array
image
[
k
,
b
,
:,
:]
=
data
else
:
# create empty numpy array to store a single tile
image
=
np
.
empty
(
shape
=
(
img
.
RasterCount
,
tile_size
,
tile_size
))
# the tile of interest
tile
=
topleft
[
tile
]
# iterate over the bands of the image
for
b
in
range
(
img
.
RasterCount
):
# read the data of band b
band
=
img
.
GetRasterBand
(
b
+
1
)
data
=
band
.
ReadAsArray
(
tile
[
1
],
tile
[
0
],
tile_size
,
tile_size
)
# append band b to numpy image array
image
[
b
,
:,
:]
=
data
# check if there are more than 1 band
if
not
img
.
RasterCount
>
1
:
image
=
image
.
squeeze
()
# close tif file
del
img
# return the image
return
image
# this function checks whether an image is evenly divisible
# in square tiles of defined size tile_size
def
is_divisible
(
img_size
,
tile_size
):
# calculate number of pixels per tile
pixels_per_tile
=
tile_size
**
2
# check whether the image is evenly divisible in square tiles of size
# (tile_size x tile_size)
ntiles
=
((
img_size
[
0
]
*
img_size
[
1
])
/
pixels_per_tile
)
assert
ntiles
.
is_integer
(),
(
'
Image not evenly divisible in
'
'
{} x {} tiles.
'
).
format
(
tile_size
,
tile_size
)
return
int
(
ntiles
)
# this function returns the top-left corners for each tile
# if the image is evenly divisible in square tiles of
# defined size tile_size
def
tile_offsets
(
img_size
,
tile_size
):
# check if divisible
_
=
is_divisible
(
img_size
,
tile_size
)
# number of tiles along the width (columns) of the image
ntiles_columns
=
int
(
img_size
[
1
]
/
tile_size
)
# number of tiles along the height (rows) of the image
ntiles_rows
=
int
(
img_size
[
0
]
/
tile_size
)
# get the indices of the top left corner for each tile
indices
=
{}
k
=
0
for
i
in
range
(
ntiles_rows
):
for
j
in
range
(
ntiles_columns
):
indices
[
k
]
=
(
i
*
tile_size
,
j
*
tile_size
)
k
+=
1
return
indices
def
parse_landsat8_date
(
scene
):
...
...
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