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
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Audard Lucile
Image classification
Compare revisions
5d1fa2d1d9d7469a971e30e76cd9fd35b491ac0e to d67598549627dc061b05eb2f2e266263cfcb7f20
Compare revisions
Changes are shown as if the
source
revision was being merged into the
target
revision.
Learn more about comparing revisions.
Source
laudard/image-classification
Select target project
No results found
d67598549627dc061b05eb2f2e266263cfcb7f20
Select Git revision
Branches
main
1 result
Swap
Target
laudard/image-classification
Select target project
laudard/image-classification
1 result
5d1fa2d1d9d7469a971e30e76cd9fd35b491ac0e
Select Git revision
Branches
main
1 result
Show changes
Only incoming changes from source
Include changes to target since source was created
Compare
Commits on Source
3
Update knn.py
· 72bb7ee1
Audard Lucile
authored
Nov 7, 2023
72bb7ee1
Update read_cifar.py
· 9f6d2ace
Audard Lucile
authored
Nov 7, 2023
9f6d2ace
Create mlp.py
· d6759854
Audard Lucile
authored
Nov 7, 2023
d6759854
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
knn.py
+35
-13
35 additions, 13 deletions
knn.py
mlp.py
+5
-0
5 additions, 0 deletions
mlp.py
read_cifar.py
+1
-1
1 addition, 1 deletion
read_cifar.py
with
41 additions
and
14 deletions
knn.py
View file @
d6759854
import
numpy
as
np
from
read_cifar
import
*
import
matplotlib.pyplot
as
plt
def
distance_matrix
(
mat1
,
mat2
):
square1
=
np
.
sum
(
np
.
square
(
mat1
),
axis
=
1
)
square2
=
np
.
sum
(
np
.
square
(
mat2
),
axis
=
1
)
# A^2 and B^2
square1
=
np
.
sum
(
np
.
square
(
mat1
),
axis
=
1
,
keepdims
=
True
)
square2
=
np
.
sum
(
np
.
square
(
mat2
),
axis
=
1
,
keepdims
=
True
)
# A*B
prod
=
np
.
dot
(
mat1
,
mat2
.
T
)
dists
=
np
.
sqrt
(
square1
+
square2
-
2
*
prod
)
# A^2 + B^2 -2*A*B
dists
=
np
.
sqrt
(
square1
+
square2
.
T
-
2
*
prod
)
return
dists
def
knn_predict
(
dists
,
labels_train
,
k
):
# results matrix initiali
s
ation
# results matrix initiali
z
ation
predicted_labels
=
np
.
zeros
(
len
(
dists
))
# loop on all the test images
for
i
in
range
(
len
(
dists
)):
...
...
@@ -19,17 +24,21 @@ def knn_predict(dists, labels_train, k):
# get the matching labels_train
closest_labels
=
labels_train
[
k_sorted_dists
]
# get the most common labels_train
predicted_labels
[
i
]
=
np
.
argmax
(
closest_labels
)
uniques
,
counts
=
np
.
unique
(
closest_labels
,
return_counts
=
True
)
predicted_labels
[
i
]
=
uniques
[
np
.
argmax
(
counts
)]
return
np
.
array
(
predicted_labels
)
def
evaluate_knn
(
data_train
,
labels_train
,
data_test
,
labels_test
,
k
):
dists
=
distance_matrix
(
data_test
,
data_train
)
# Determine the number of images in data_test
tot
=
len
(
data_test
)
accurate
=
0
predicted_labels
=
knn_predict
(
dists
,
labels_train
,
k
)
# Count the number of images in data_test whose label has been estimated correctly
for
i
in
range
(
tot
):
if
predicted_labels
[
i
]
==
labels_test
[
i
]:
accurate
+=
1
# Calculate the classification rate
accuracy
=
accurate
/
tot
return
accuracy
...
...
@@ -42,14 +51,27 @@ def evaluate_knn(data_train, labels_train, data_test, labels_test, k):
if
__name__
==
"
__main__
"
:
bench_knn
()
# data, labels = read_cifar.read_cifar('image-classification/data/cifar-10-batches-py')
# X_train, X_test, y_train, y_test = read_cifar.split_dataset(data, labels, 0.9)
# print(evaluate_knn(X_train, y_train, X_test, y_test, 5))
# print(X_train.shape, X_test.shape, y_train.shape, y_test.shape)
data
,
labels
=
read_cifar
(
"
./data/cifar-10-batches-py
"
)
data_train
,
labels_train
,
data_test
,
labels_test
=
split_dataset
(
data
,
labels
,
0.9
)
k_list
=
[
k
for
k
in
range
(
1
,
21
)]
accuracy
=
[
evaluate_knn
(
data_train
,
labels_train
,
data_test
,
labels_test
,
k
)
for
k
in
range
(
1
,
21
)]
plt
.
plot
([
k
for
k
in
range
(
1
,
21
)],
accuracy
)
plt
.
title
(
"
Variation of k-nearest neighbors method accuracy for k from 1 to 20
"
)
plt
.
xlabel
(
"
k value
"
)
plt
.
ylabel
(
"
Accuracy
"
)
plt
.
grid
(
True
,
which
=
'
both
'
)
plt
.
savefig
(
"
results/knn.png
"
)
# y_test = []
# x_test = np.array([[1,2],[4,6]])
# x_labels_test = np.array([0,1])
# x_train = np.array([[2,4],[7,2],[4,6]])
# y_train = [1,2,1]
# x_labels_train = np.array([0,1,1])
# dist = distance_matrix(x_test, x_train)
# accuracy = evaluate_knn(x_train, x_labels_train, x_test, x_labels_test, 1)
# print(accuracy)
This diff is collapsed.
Click to expand it.
mlp.py
0 → 100644
View file @
d6759854
def
learn_once_mse
(
w1
,
b1
,
w2
,
b2
,
data
,
targets
,
learning_rate
):
return
w1
,
b1
,
w2
,
b2
,
loss
This diff is collapsed.
Click to expand it.
read_cifar.py
View file @
d6759854
...
...
@@ -14,7 +14,7 @@ def read_cifar_batch(path):
return
np
.
float32
(
data
),
np
.
int64
(
labels
)
def
read_cifar
(
folder_path
):
data
,
labels
=
read_cifar_batch
(
"
./data/cifar-10-
python.tar/cifar-10-batches-py~/cifar-10-
batches-py/test_batch
"
)
data
,
labels
=
read_cifar_batch
(
"
./data/cifar-10-batches-py/test_batch
"
)
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
]))
...
...
This diff is collapsed.
Click to expand it.