Toggle navigation
Toggle navigation
This project
Loading...
Sign in
Hyunji
/
A-Performance-Evaluation-of-CNN-for-Brain-Age-Prediction-Using-Structural-MRI-Data
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
Hyunji
2021-12-20 04:22:15 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
9eadf55c5269d655faa65bf6701d97b2d275178c
9eadf55c
1 parent
19386c0f
util
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
75 additions
and
0 deletions
2DCNN/lib/data/util.py
2DCNN/lib/data/util.py
0 → 100644
View file @
9eadf55
""" general utility function for data: mostly transformations """
import
logging
import
os
import
random
import
numpy
import
numpy
as
np
from
PIL
import
ImageFilter
logger
=
logging
.
getLogger
()
DATA_FOLDER
=
os
.
getenv
(
"DATA"
)
if
os
.
getenv
(
"DATA"
)
else
"data"
def
uniform_label_noise
(
p
,
labels
,
seed
=
None
):
if
seed
is
not
None
:
rng_state
=
numpy
.
random
.
get_state
()
numpy
.
random
.
seed
(
seed
)
labels
=
numpy
.
array
(
labels
.
tolist
())
N
=
len
(
labels
)
lst
=
numpy
.
unique
(
labels
)
# generate random labels
rnd_labels
=
numpy
.
random
.
choice
(
lst
,
size
=
N
)
flip
=
numpy
.
random
.
rand
(
N
)
<=
p
labels
=
labels
*
(
1
-
flip
)
+
rnd_labels
*
flip
if
seed
is
not
None
:
numpy
.
random
.
set_state
(
rng_state
)
return
labels
class
GaussianBlur
(
object
):
"""Gaussian blur augmentation in SimCLR https://arxiv.org/abs/2002.05709"""
def
__init__
(
self
,
sigma
=
None
):
if
sigma
is
None
:
sigma
=
[
0.1
,
2.0
]
self
.
sigma
=
sigma
def
__call__
(
self
,
x
):
sigma
=
random
.
uniform
(
self
.
sigma
[
0
],
self
.
sigma
[
1
])
x
=
x
.
filter
(
ImageFilter
.
GaussianBlur
(
radius
=
sigma
))
return
x
def
lines_to_np_array
(
lines
):
return
np
.
array
([[
int
(
i
)
for
i
in
line
.
split
()]
for
line
in
lines
])
def
load_binary_mnist
():
with
open
(
os
.
path
.
join
(
DATA_FOLDER
,
"binary-mnist"
,
"binarized_mnist_train.amat"
))
as
f
:
lines
=
f
.
readlines
()
train_data
=
lines_to_np_array
(
lines
)
.
astype
(
"float32"
)
with
open
(
os
.
path
.
join
(
DATA_FOLDER
,
"binary-mnist"
,
"binarized_mnist_valid.amat"
))
as
f
:
lines
=
f
.
readlines
()
validation_data
=
lines_to_np_array
(
lines
)
.
astype
(
"float32"
)
with
open
(
os
.
path
.
join
(
DATA_FOLDER
,
"binary-mnist"
,
"binarized_mnist_test.amat"
))
as
f
:
lines
=
f
.
readlines
()
test_data
=
lines_to_np_array
(
lines
)
.
astype
(
"float32"
)
return
{
"train"
:
train_data
,
"valid"
:
validation_data
,
"test"
:
test_data
}
def
load_mnist
():
import
gzip
import
_pickle
train
,
valid
,
test
=
_pickle
.
load
(
gzip
.
open
(
os
.
path
.
join
(
DATA_FOLDER
,
"mnist"
,
"mnist.pkl.gz"
)),
encoding
=
"latin1"
,
)
return
{
"train"
:
train
,
"valid"
:
valid
,
"test"
:
test
}
Please
register
or
login
to post a comment