Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Open sidebar
Oleh Astappiev
Near-Similar Image Recognition
Commits
04f1733a
Commit
04f1733a
authored
May 24, 2022
by
Oleh Astappiev
Browse files
chore: rename map function
parent
6f761cb4
Changes
13
Hide whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
33 additions
and
34 deletions
+33
-34
src/alexnet.py
src/alexnet.py
+1
-1
src/alexnet_cifar10.py
src/alexnet_cifar10.py
+1
-1
src/data/cifar10.py
src/data/cifar10.py
+6
-6
src/data/imagenette.py
src/data/imagenette.py
+6
-6
src/data/simple3.py
src/data/simple3.py
+5
-5
src/efficientnet.py
src/efficientnet.py
+1
-1
src/mobilenet.py
src/mobilenet.py
+5
-6
src/mobilenet_cifar10.py
src/mobilenet_cifar10.py
+2
-2
src/utils/common.py
src/utils/common.py
+1
-1
src/vgg16.py
src/vgg16.py
+1
-1
src/vgg16_cifar10.py
src/vgg16_cifar10.py
+1
-1
src/vgg16_small.py
src/vgg16_small.py
+1
-1
src/vit.py
src/vit.py
+2
-2
No files found.
src/alexnet.py
View file @
04f1733a
...
...
@@ -11,7 +11,7 @@ from src.model.siamese import SiameseModel
model_name
=
'imagenette_alexnet'
embeddings_name
=
model_name
+
'_embeddings'
train_ds
,
val_ds
,
test_ds
=
load_dataset3
(
image_size
=
TARGET_SHAPE
,
preprocess
_fn
=
AlexNetModel
.
preprocess_input
)
train_ds
,
val_ds
,
test_ds
=
load_dataset3
(
image_size
=
TARGET_SHAPE
,
map
_fn
=
AlexNetModel
.
preprocess_input
)
comb_ds
=
train_ds
.
concatenate
(
val_ds
).
concatenate
(
test_ds
)
# create model
...
...
src/alexnet_cifar10.py
View file @
04f1733a
...
...
@@ -11,7 +11,7 @@ from src.model.siamese import SiameseModel
model_name
=
'cifar10_alexnet'
embeddings_name
=
model_name
+
'_embeddings'
train_ds
,
val_ds
,
test_ds
=
load_dataset3
(
image_size
=
TARGET_SHAPE
,
preprocess
_fn
=
AlexNetModel
.
preprocess_input
)
train_ds
,
val_ds
,
test_ds
=
load_dataset3
(
image_size
=
TARGET_SHAPE
,
map
_fn
=
AlexNetModel
.
preprocess_input
)
comb_ds
=
train_ds
.
concatenate
(
val_ds
).
concatenate
(
test_ds
)
# create model
...
...
src/data/cifar10.py
View file @
04f1733a
...
...
@@ -7,7 +7,7 @@ NUM_CLASSES = 10
CLASS_NAMES
=
[
'airplane'
,
'automobile'
,
'bird'
,
'cat'
,
'deer'
,
'dog'
,
'frog'
,
'horse'
,
'ship'
,
'truck'
]
def
load_dataset
(
image_size
=
IMAGE_SIZE
,
batch_size
=
BATCH_SIZE
,
preprocess
_fn
=
None
):
def
load_dataset
(
image_size
=
IMAGE_SIZE
,
batch_size
=
BATCH_SIZE
,
map
_fn
=
None
):
train_ds
=
tf
.
keras
.
utils
.
image_dataset_from_directory
(
directory
=
'../datasets/cifar10/train/'
,
labels
=
'inferred'
,
...
...
@@ -27,15 +27,15 @@ def load_dataset(image_size=IMAGE_SIZE, batch_size=BATCH_SIZE, preprocess_fn=Non
interpolation
=
'nearest'
)
if
preprocess
_fn
is
not
None
:
train_ds
=
train_ds
.
map
(
preprocess
_fn
).
prefetch
(
tf
.
data
.
AUTOTUNE
)
test_ds
=
test_ds
.
map
(
preprocess
_fn
).
prefetch
(
tf
.
data
.
AUTOTUNE
)
if
map
_fn
is
not
None
:
train_ds
=
train_ds
.
map
(
map
_fn
).
prefetch
(
tf
.
data
.
AUTOTUNE
)
test_ds
=
test_ds
.
map
(
map
_fn
).
prefetch
(
tf
.
data
.
AUTOTUNE
)
return
train_ds
,
test_ds
def
load_dataset3
(
image_size
=
IMAGE_SIZE
,
batch_size
=
BATCH_SIZE
,
preprocess
_fn
=
None
):
train_ds
,
test_ds
=
load_dataset
(
image_size
=
image_size
,
batch_size
=
batch_size
,
preprocess_fn
=
preprocess
_fn
)
def
load_dataset3
(
image_size
=
IMAGE_SIZE
,
batch_size
=
BATCH_SIZE
,
map
_fn
=
None
):
train_ds
,
test_ds
=
load_dataset
(
image_size
=
image_size
,
batch_size
=
batch_size
,
map_fn
=
map
_fn
)
train_ds_size
=
tf
.
data
.
experimental
.
cardinality
(
train_ds
).
numpy
()
train_ds
=
train_ds
.
skip
(
train_ds_size
/
10
)
...
...
src/data/imagenette.py
View file @
04f1733a
...
...
@@ -7,7 +7,7 @@ NUM_CLASSES = 10
CLASS_NAMES
=
[
'fish'
,
'dog'
,
'player'
,
'saw'
,
'building'
,
'music'
,
'truck'
,
'gas'
,
'ball'
,
'parachute'
]
def
load_dataset
(
image_size
=
IMAGE_SIZE
,
batch_size
=
BATCH_SIZE
,
preprocess
_fn
=
None
):
def
load_dataset
(
image_size
=
IMAGE_SIZE
,
batch_size
=
BATCH_SIZE
,
map
_fn
=
None
):
train_ds
=
tf
.
keras
.
utils
.
image_dataset_from_directory
(
directory
=
'../datasets/imagenette2/train/'
,
labels
=
'inferred'
,
...
...
@@ -27,15 +27,15 @@ def load_dataset(image_size=IMAGE_SIZE, batch_size=BATCH_SIZE, preprocess_fn=Non
interpolation
=
'nearest'
)
if
preprocess
_fn
is
not
None
:
train_ds
=
train_ds
.
map
(
preprocess
_fn
).
prefetch
(
tf
.
data
.
AUTOTUNE
)
test_ds
=
test_ds
.
map
(
preprocess
_fn
).
prefetch
(
tf
.
data
.
AUTOTUNE
)
if
map
_fn
is
not
None
:
train_ds
=
train_ds
.
map
(
map
_fn
).
prefetch
(
tf
.
data
.
AUTOTUNE
)
test_ds
=
test_ds
.
map
(
map
_fn
).
prefetch
(
tf
.
data
.
AUTOTUNE
)
return
train_ds
,
test_ds
def
load_dataset3
(
image_size
=
IMAGE_SIZE
,
batch_size
=
BATCH_SIZE
,
preprocess
_fn
=
None
):
train_ds
,
test_ds
=
load_dataset
(
image_size
=
image_size
,
batch_size
=
batch_size
,
preprocess_fn
=
preprocess
_fn
)
def
load_dataset3
(
image_size
=
IMAGE_SIZE
,
batch_size
=
BATCH_SIZE
,
map
_fn
=
None
):
train_ds
,
test_ds
=
load_dataset
(
image_size
=
image_size
,
batch_size
=
batch_size
,
map_fn
=
map
_fn
)
test_ds_size
=
tf
.
data
.
experimental
.
cardinality
(
test_ds
).
numpy
()
val_ds
=
test_ds
.
take
(
test_ds_size
/
2
)
...
...
src/data/simple3.py
View file @
04f1733a
...
...
@@ -7,7 +7,7 @@ NUM_CLASSES = 3
CLASS_NAMES
=
[
'building'
,
'dog'
,
'player'
]
def
load_dataset
(
image_size
=
IMAGE_SIZE
,
batch_size
=
BATCH_SIZE
,
preprocess
_fn
=
None
):
def
load_dataset
(
image_size
=
IMAGE_SIZE
,
batch_size
=
BATCH_SIZE
,
map
_fn
=
None
):
ds
=
tf
.
keras
.
utils
.
image_dataset_from_directory
(
directory
=
'../datasets/simple3/'
,
labels
=
'inferred'
,
...
...
@@ -17,14 +17,14 @@ def load_dataset(image_size=IMAGE_SIZE, batch_size=BATCH_SIZE, preprocess_fn=Non
interpolation
=
'nearest'
)
if
preprocess
_fn
is
not
None
:
ds
=
ds
.
map
(
preprocess
_fn
).
prefetch
(
tf
.
data
.
AUTOTUNE
)
if
map
_fn
is
not
None
:
ds
=
ds
.
map
(
map
_fn
).
prefetch
(
tf
.
data
.
AUTOTUNE
)
return
ds
def
load_dataset3
(
image_size
=
IMAGE_SIZE
,
batch_size
=
BATCH_SIZE
,
preprocess
_fn
=
None
):
ds
=
load_dataset
(
image_size
=
image_size
,
batch_size
=
batch_size
,
preprocess_fn
=
preprocess
_fn
)
def
load_dataset3
(
image_size
=
IMAGE_SIZE
,
batch_size
=
BATCH_SIZE
,
map
_fn
=
None
):
ds
=
load_dataset
(
image_size
=
image_size
,
batch_size
=
batch_size
,
map_fn
=
map
_fn
)
ds_size
=
tf
.
data
.
experimental
.
cardinality
(
ds
).
numpy
()
train_ds
=
ds
.
take
(
ds_size
*
0.6
)
...
...
src/efficientnet.py
View file @
04f1733a
...
...
@@ -10,7 +10,7 @@ from src.model.siamese import SiameseModel
model_name
=
'cifar10_efficientnet'
embeddings_name
=
model_name
+
'_embeddings'
train_ds
,
val_ds
,
test_ds
=
load_dataset3
(
image_size
=
TARGET_SHAPE
,
batch_size
=
BATCH_SIZE
,
preprocess
_fn
=
EfficientNetModel
.
preprocess_input
)
train_ds
,
val_ds
,
test_ds
=
load_dataset3
(
image_size
=
TARGET_SHAPE
,
batch_size
=
BATCH_SIZE
,
map
_fn
=
EfficientNetModel
.
preprocess_input
)
comb_ds
=
train_ds
.
concatenate
(
val_ds
).
concatenate
(
test_ds
)
model
=
EfficientNetModel
()
...
...
src/mobilenet.py
View file @
04f1733a
...
...
@@ -8,24 +8,23 @@ from src.utils.common import get_modeldir
from
src.model.mobilenet
import
MobileNetModel
,
PRETRAIN_EPOCHS
,
TARGET_SHAPE
,
EMBEDDING_VECTOR_DIMENSION
from
src.model.siamese
import
SiameseModel
model_name
=
'imagenet_mobilenet'
model_name
=
'imagenet
te
_mobilenet'
embeddings_name
=
model_name
+
'_embeddings'
train_ds
,
val_ds
,
test_ds
=
load_dataset3
(
image_size
=
TARGET_SHAPE
,
preprocess
_fn
=
MobileNetModel
.
preprocess_input
)
train_ds
,
val_ds
,
test_ds
=
load_dataset3
(
image_size
=
TARGET_SHAPE
,
map
_fn
=
MobileNetModel
.
preprocess_input
)
comb_ds
=
train_ds
.
concatenate
(
val_ds
).
concatenate
(
test_ds
)
PRETRAIN_TOTAL_STEPS
=
PRETRAIN_EPOCHS
*
len
(
train_ds
)
# create model
model
=
MobileNetModel
()
model
.
compile
(
optimizer
=
tf
.
keras
.
optimizers
.
RMSprop
(
tf
.
keras
.
optimizers
.
schedules
.
CosineDecay
(
1e-3
,
PRETRAIN_TOTAL_STEPS
)))
model
.
summary
()
# load weights
model
.
load_weights
(
get_modeldir
(
model_name
+
'.h5'
))
#
model.load_weights(get_modeldir(model_name + '.h5'))
# train & save model
#
model.fit(train_ds, epochs=PRETRAIN_EPOCHS, validation_data=val_ds)
#
model.save_weights(get_modeldir(model_name + '.h5'))
model
.
fit
(
train_ds
,
epochs
=
PRETRAIN_EPOCHS
,
validation_data
=
val_ds
)
model
.
save_weights
(
get_modeldir
(
model_name
+
'.h5'
))
# evaluate
print
(
'evaluating...'
)
...
...
src/mobilenet_cifar10.py
View file @
04f1733a
...
...
@@ -11,7 +11,7 @@ from src.model.siamese import SiameseModel
model_name
=
'cifar10_mobilenet'
embeddings_name
=
model_name
+
'_embeddings'
train_ds
,
val_ds
,
test_ds
=
load_dataset3
(
image_size
=
TARGET_SHAPE
,
preprocess
_fn
=
MobileNetModel
.
preprocess_input
)
train_ds
,
val_ds
,
test_ds
=
load_dataset3
(
image_size
=
TARGET_SHAPE
,
map
_fn
=
MobileNetModel
.
preprocess_input
)
comb_ds
=
train_ds
.
concatenate
(
val_ds
).
concatenate
(
test_ds
)
PRETRAIN_TOTAL_STEPS
=
PRETRAIN_EPOCHS
*
len
(
train_ds
)
...
...
@@ -55,6 +55,6 @@ inference_model.save(get_modeldir(model_name + '_inference.tf'))
print
(
'visualization'
)
# compute
embedding
s of the images and their labels, store them in a tsv file for visualization
# compute
vector
s of the images and their labels, store them in a tsv file for visualization
siamese_vectors
,
siamese_labels
=
calc_vectors
(
comb_ds
,
inference_model
)
project_embeddings
(
siamese_vectors
,
siamese_labels
,
model_name
+
'_siamese'
)
src/utils/common.py
View file @
04f1733a
import
time
import
tensorflow
as
tf
from
os
import
path
,
curdir
import
time
def
normalize_image
(
image
):
...
...
src/vgg16.py
View file @
04f1733a
...
...
@@ -11,7 +11,7 @@ from src.model.siamese import SiameseModel
model_name
=
'imagenet_vgg16'
embeddings_name
=
model_name
+
'_embeddings'
train_ds
,
val_ds
,
test_ds
=
load_dataset3
(
image_size
=
TARGET_SHAPE
,
preprocess
_fn
=
VGG16Model
.
preprocess_input
)
train_ds
,
val_ds
,
test_ds
=
load_dataset3
(
image_size
=
TARGET_SHAPE
,
map
_fn
=
VGG16Model
.
preprocess_input
)
comb_ds
=
train_ds
.
concatenate
(
val_ds
).
concatenate
(
test_ds
)
PRETRAIN_TOTAL_STEPS
=
PRETRAIN_EPOCHS
*
len
(
train_ds
)
...
...
src/vgg16_cifar10.py
View file @
04f1733a
...
...
@@ -13,7 +13,7 @@ embeddings_name = model_name + '_embeddings'
TARGET_SHAPE
=
(
32
,
32
)
train_ds
,
val_ds
,
test_ds
=
load_dataset3
(
image_size
=
TARGET_SHAPE
,
preprocess
_fn
=
VGG16Model
.
preprocess_input
)
train_ds
,
val_ds
,
test_ds
=
load_dataset3
(
image_size
=
TARGET_SHAPE
,
map
_fn
=
VGG16Model
.
preprocess_input
)
comb_ds
=
train_ds
.
concatenate
(
val_ds
).
concatenate
(
test_ds
)
PRETRAIN_TOTAL_STEPS
=
PRETRAIN_EPOCHS
*
len
(
train_ds
)
...
...
src/vgg16_small.py
View file @
04f1733a
...
...
@@ -13,7 +13,7 @@ embeddings_name = model_name + '_embeddings'
TARGET_SHAPE
=
(
32
,
32
)
train_ds
,
val_ds
,
test_ds
=
load_dataset3
(
image_size
=
TARGET_SHAPE
,
preprocess
_fn
=
VGG16Model
.
preprocess_input
)
train_ds
,
val_ds
,
test_ds
=
load_dataset3
(
image_size
=
TARGET_SHAPE
,
map
_fn
=
VGG16Model
.
preprocess_input
)
comb_ds
=
train_ds
.
concatenate
(
val_ds
).
concatenate
(
test_ds
)
PRETRAIN_TOTAL_STEPS
=
PRETRAIN_EPOCHS
*
len
(
train_ds
)
...
...
src/vit.py
View file @
04f1733a
...
...
@@ -11,7 +11,7 @@ from src.model.siamese import SiameseModel
model_name
=
'cifar10_vit'
embeddings_name
=
model_name
+
'_embeddings'
train_ds
,
val_ds
,
test_ds
=
load_dataset3
(
image_size
=
TARGET_SHAPE
,
batch_size
=
BATCH_SIZE
,
preprocess
_fn
=
VitModel
.
preprocess_input
)
train_ds
,
val_ds
,
test_ds
=
load_dataset3
(
image_size
=
TARGET_SHAPE
,
batch_size
=
BATCH_SIZE
,
map
_fn
=
VitModel
.
preprocess_input
)
comb_ds
=
train_ds
.
concatenate
(
val_ds
).
concatenate
(
test_ds
)
model
=
VitModel
()
...
...
@@ -24,7 +24,7 @@ save_embeddings(emb_vectors, emb_labels, embeddings_name)
# emb_vectors, emb_labels = load_embeddings(embeddings_name)
# siamese is the model we train
siamese
=
SiameseModel
(
embedding_vector_dimension
=
384
,
image_vector_dimensions
=
512
)
siamese
=
SiameseModel
(
embedding_vector_dimension
=
384
,
image_vector_dimensions
=
3
)
siamese
.
compile
(
loss_margin
=
0.05
,
optimizer
=
tf
.
keras
.
optimizers
.
Adam
(
learning_rate
=
0.001
))
siamese
.
summary
()
...
...
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