Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
Image classification
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
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
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Audard Lucile
Image classification
Commits
df8a6a82
Commit
df8a6a82
authored
1 year ago
by
Audard Lucile
Browse files
Options
Downloads
Patches
Plain Diff
Update read_cifar.py
parent
6c0a1bf6
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
read_cifar.py
+31
-4
31 additions, 4 deletions
read_cifar.py
with
31 additions
and
4 deletions
read_cifar.py
+
31
−
4
View file @
df8a6a82
import
pickle
import
numpy
as
np
from
sklearn.model_selection
import
train_test_split
import
random
def
unpickle
(
file
):
...
...
@@ -7,29 +9,54 @@ def unpickle(file):
batch
=
pickle
.
load
(
fo
,
encoding
=
'
bytes
'
)
return
batch
def
read_cifar_batch
(
path
):
batch
=
unpickle
(
path
)
data
=
batch
[
b
'
data
'
]
labels
=
batch
[
b
'
labels
'
]
return
np
.
float32
(
data
),
np
.
int64
(
labels
)
def
read_cifar
(
folder_path
):
# Get the test batch
data
,
labels
=
read_cifar_batch
(
"
./data/cifar-10-batches-py/test_batch
"
)
# Concatenate with the 5 data batches
for
i
in
range
(
1
,
5
):
data
=
np
.
concatenate
((
data
,
read_cifar_batch
(
folder_path
+
"
/data_batch_
"
+
str
(
i
))[
0
]))
labels
=
np
.
concatenate
((
labels
,
read_cifar_batch
(
folder_path
+
"
/data_batch_
"
+
str
(
i
))[
1
]))
np
.
append
(
data
,
read_cifar_batch
(
folder_path
+
"
/data_batch_
"
+
str
(
i
))[
0
])
np
.
append
(
labels
,
read_cifar_batch
(
folder_path
+
"
/data_batch_
"
+
str
(
i
))[
1
])
return
data
,
labels
def
split_dataset
(
data
,
labels
,
split
):
# Determination of an index to split the data
index
=
int
(
split
*
len
(
data
))
data_train
,
data_test
=
np
.
split
(
data
,
index
)
labels_train
,
labels_test
=
np
.
split
(
labels
,
index
)
# Split the data on the index
tableau_combine
=
list
(
zip
(
data
,
labels
))
random
.
shuffle
(
tableau_combine
)
data_train
,
data_test
,
labels_train
,
labels_test
=
train_test_split
(
data
,
labels
,
test_size
=
1
-
split
,
random_state
=
1
)
# data_train, data_test = np.split(data, [index])
# labels_train, labels_test = np.split(labels, [index])
return
data_train
,
labels_train
,
data_test
,
labels_test
if
__name__
==
"
__main__
"
:
# Extraction of the data from Cifar database
data
,
labels
=
read_cifar
(
"
./data/cifar-10-batches-py
"
)
print
(
data
)
print
(
labels
)
# Formatting the data into training and testing sets
split
=
0.21
data_train
,
labels_train
,
data_test
,
labels_test
=
split_dataset
(
data
,
labels
,
split
)
print
(
data_train
)
print
(
labels_train
)
print
(
data_test
)
print
(
labels_test
)
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