Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
bob
bob.learn.pytorch
Commits
889ced71
Commit
889ced71
authored
Jan 30, 2019
by
Anjith GEORGE
Committed by
Olegs NIKISINS
Feb 01, 2019
Browse files
Modified the data_folder to be more generic
parent
85834fef
Changes
1
Hide whitespace changes
Inline
Side-by-side
bob/learn/pytorch/datasets/data_folder.py
View file @
889ced71
...
...
@@ -12,9 +12,8 @@ import torch.utils.data as data
import
os
import
random
random
.
seed
(
a
=
7
)
import
PIL
random
.
seed
(
a
=
7
)
import
numpy
as
np
...
...
@@ -102,7 +101,7 @@ class DataFolder(data.Dataset):
A directory containing the training data.
transform : object
A function/transform that takes in a
PIL
image, and returns a
A function/transform that takes in a
numpy.ndarray
image, and returns a
transformed version. E.g, ``transforms.RandomCrop``. Default: None.
extension : str
...
...
@@ -155,7 +154,7 @@ class DataFolder(data.Dataset):
A directory containing the training data.
transform : object
A function/transform
that takes in a PIL
image, and returns a
A
custom
function/transform
(torchvision.transforms.Compose) that takes in a numpy.ndarray
image
, and returns a
transformed version. E.g, ``transforms.RandomCrop``. Default: None.
extension : str
...
...
@@ -238,9 +237,9 @@ class DataFolder(data.Dataset):
Returns
-------
p
il
_img : Tensor
or PIL I
mage
If ``self.transform`` is defined the output is the torch.Tensor,
otherwise the
output is an instance of the PIL.Image.Image class
.
n
p_img : Tensor
i
mage
If ``self.transform`` is defined
as a custom function,
the output is the torch.Tensor,
otherwise the
last transform in the transforms.Compose should be transforms.ToTensor()
.
target : int
Index of the class.
...
...
@@ -254,20 +253,21 @@ class DataFolder(data.Dataset):
if
isinstance
(
self
.
transform
,
transforms
.
Compose
):
# if an instance of torchvision composed transformation
if
len
(
img_array
.
shape
)
==
3
:
# for color images
if
len
(
img_array
.
shape
)
==
3
:
# for color
or multi-channel
images
img_array_tr
=
np
.
swapaxes
(
img_array
,
1
,
2
)
img_array_tr
=
np
.
swapaxes
(
img_array_tr
,
0
,
2
)
p
il
_img
=
PIL
.
Image
.
fromarray
(
img_array_tr
)
# convert to PIL from array of size (H x W x 3)
n
p_img
=
img_array_tr
.
copy
()
# np_img is numpy.ndarray of shape HxWxC
else
:
# for gray-scale images
pil_img
=
PIL
.
Image
.
fromarray
(
img_array
,
'L'
)
# convert to PIL from array of size (H x W)
np_img
=
np
.
expand_dims
(
img_array_tr
,
2
)
# np_img is numpy.ndarray of size HxWx1
if
self
.
transform
is
not
None
:
p
il
_img
=
self
.
transform
(
p
il
_img
)
n
p_img
=
self
.
transform
(
n
p_img
)
# after this transformation np_img should be a tensor
else
:
# if custom transformation function is given
...
...
@@ -275,7 +275,7 @@ class DataFolder(data.Dataset):
return
torch
.
Tensor
(
img_array_transformed
).
unsqueeze
(
0
),
target
# convert array to Tensor, also return target
return
p
il
_img
,
target
return
n
p_img
,
target
#==========================================================================
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment