Commit aa738388 authored by Oleh Astappiev's avatar Oleh Astappiev
Browse files

feat: use classes for daasets

parent 195641ef
import sys import sys
sys.path.append("..") sys.path.append("..")
import tensorflow as tf from src.model.alexnet import AlexNetModel, TARGET_SHAPE
from src.data.imagenette import load_dataset3, NUM_CLASSES from src.data.imagenette import Imagenette
from src.utils.embeddings import save_embeddings, load_embeddings, project_embeddings, calc_vectors from src.data.cifar10 import Cifar10
from src.utils.common import get_modeldir from src.utils.embeddings import project_embeddings, load_weights_of, get_embeddings_of, save_embeddings
from src.model.alexnet import AlexNetModel, TARGET_SHAPE, EMBEDDING_VECTOR_DIMENSION
from src.model.siamese import SiameseModel from src.model.siamese import SiameseModel
model_name = 'imagenette_alexnet' dataset = Imagenette(image_size=TARGET_SHAPE, map_fn=AlexNetModel.preprocess_input)
embeddings_name = model_name + '_embeddings' # dataset = Cifar10(image_size=TARGET_SHAPE, map_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
model = AlexNetModel() model = AlexNetModel()
model.compile() model.compile()
model.summary() load_weights_of(model, dataset)
# load weights
# model.load_weights(get_modeldir(model_name + '.h5'))
# train & save model
model.fit(train_ds, validation_data=val_ds)
model.save_weights(get_modeldir(model_name + '.h5'))
# evaluate
print('evaluating...')
model.evaluate(test_ds)
print('calculating embeddings...')
embedding_model = model.get_embedding_model()
embedding_model.summary()
emb_vectors, emb_labels = calc_vectors(comb_ds, embedding_model) emb_vectors, emb_labels = get_embeddings_of(model.get_embedding_model(), dataset)
save_embeddings(emb_vectors, emb_labels, embeddings_name) emb_ds = SiameseModel.prepare_dataset(emb_vectors, emb_labels)
# emb_vectors, emb_labels = load_embeddings(embeddings_name) siamese = SiameseModel(embedding_model=model.get_embedding_model(), image_vector_dimensions=512)
# siamese is the model we train
siamese = SiameseModel(embedding_vector_dimension=EMBEDDING_VECTOR_DIMENSION, image_vector_dimensions=3)
siamese.compile(loss_margin=0.05) siamese.compile(loss_margin=0.05)
siamese.summary() siamese.fit(emb_ds, num_classes=dataset.num_classes)
ds = SiameseModel.prepare_dataset(emb_vectors, emb_labels)
history = siamese.fit(ds, class_weight={0: 1 / NUM_CLASSES, 1: (NUM_CLASSES - 1) / NUM_CLASSES})
# Build full inference model (from image to image vector):
inference_model = siamese.get_inference_model(embedding_model)
inference_model.save(get_modeldir(model_name + '_inference.tf'))
# inference_model = tf.keras.models.load_model(get_modeldir(model_name + '_inference.tf'), compile=False)
print('visualization') projection_vectors = siamese.projection_model.predict(emb_vectors)
# compute vectors of the images and their labels, store them in a tsv file for visualization save_embeddings(projection_vectors, emb_labels, dataset.name + '_' + siamese.name + '_vectors')
projection_vectors = siamese.get_projection_model().predict(emb_vectors) project_embeddings(projection_vectors, emb_labels, siamese.name + '_' + dataset.name)
project_embeddings(projection_vectors, emb_labels, model_name + '_siamese') print('Done!')
import sys
sys.path.append("..")
import tensorflow as tf
from src.data.cifar10 import load_dataset3, NUM_CLASSES
from src.utils.embeddings import save_embeddings, load_embeddings, project_embeddings, calc_vectors
from src.utils.common import get_modeldir
from src.model.alexnet import AlexNetModel, TARGET_SHAPE, EMBEDDING_VECTOR_DIMENSION
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, map_fn=AlexNetModel.preprocess_input)
comb_ds = train_ds.concatenate(val_ds).concatenate(test_ds)
# create model
model = AlexNetModel()
model.compile()
model.summary()
# load weights
# model.load_weights(get_modeldir(model_name + '.h5'))
# train & save model
model.fit(train_ds, validation_data=val_ds)
model.save_weights(get_modeldir(model_name + '.h5'))
# evaluate
print('evaluating...')
model.evaluate(test_ds)
print('calculating embeddings...')
embedding_model = model.get_embedding_model()
embedding_model.summary()
emb_vectors, emb_labels = calc_vectors(comb_ds, embedding_model)
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=EMBEDDING_VECTOR_DIMENSION, image_vector_dimensions=3)
siamese.compile(loss_margin=0.05)
siamese.summary()
ds = SiameseModel.prepare_dataset(emb_vectors, emb_labels)
history = siamese.fit(ds, class_weight={0: 1 / NUM_CLASSES, 1: (NUM_CLASSES - 1) / NUM_CLASSES})
# Build full inference model (from image to image vector):
inference_model = siamese.get_inference_model(embedding_model)
inference_model.save(get_modeldir(model_name + '_inference.tf'))
# inference_model = tf.keras.models.load_model(get_modeldir(model_name + '_inference.tf'), compile=False)
print('visualization')
# compute vectors 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')
projection_vectors = siamese.get_projection_model().predict(emb_vectors)
project_embeddings(projection_vectors, emb_labels, model_name + '_siamese2')
from abc import ABC, abstractmethod
from typing import Tuple, Callable, List
PRINT_SIZE = True
DEFAULT_BATCH_SIZE = 32
class BaseDataset(ABC):
def __init__(self, name: str, classes: List[str], image_size: Tuple[int, int], batch_size: int = DEFAULT_BATCH_SIZE, map_fn: Callable = None):
self.name = name
self.classes = classes
self.num_classes = len(classes)
self._image_size = image_size
self._batch_size = batch_size
self._map_fn = map_fn
self._train_ds = None
self._val_ds = None
self._test_ds = None
def get_classes(self):
return self.classes
def get_num_classes(self):
return len(self.classes)
def get_train(self):
if self._train_ds is None:
self.__load()
return self._train_ds
def get_val(self):
if self._val_ds is None:
self.__load()
return self._val_ds
def get_test(self):
if self._test_ds is None:
self.__load()
return self._test_ds
def get_combined(self):
return self.get_train().concatenate(self.get_val()).concatenate(self.get_test())
def __load(self):
args = self._load_dataset(self._image_size, self._batch_size, self._map_fn)
train_ds, val_ds, test_ds = self._split_dataset(*args)
self._train_ds = train_ds
self._val_ds = val_ds
self._test_ds = test_ds
if PRINT_SIZE:
print(self.name, "dataset loaded")
print("Training size:", train_ds.cardinality().numpy())
print("Validation size:", val_ds.cardinality().numpy())
print("Evaluation size:", test_ds.cardinality().numpy())
@abstractmethod
def _load_dataset(self, image_size, batch_size, map_fn):
pass
@abstractmethod
def _split_dataset(self, *args):
pass
import tensorflow as tf import tensorflow as tf
from src.data.base import BaseDataset, DEFAULT_BATCH_SIZE
BATCH_SIZE = 32 DEFAULT_IMAGE_SIZE = (32, 32)
IMAGE_SIZE = (32, 32)
NUM_CLASSES = 10
CLASS_NAMES = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck'] CLASS_NAMES = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
class Cifar10(BaseDataset):
def load_dataset(image_size=IMAGE_SIZE, batch_size=BATCH_SIZE, map_fn=None): def __init__(self, image_size=DEFAULT_IMAGE_SIZE, batch_size=DEFAULT_BATCH_SIZE, map_fn=None):
train_ds = tf.keras.utils.image_dataset_from_directory( super(Cifar10, self).__init__(name='cifar10', classes=CLASS_NAMES, image_size=image_size, batch_size=batch_size, map_fn=map_fn)
directory='../datasets/cifar10/train/',
labels='inferred', def _load_dataset(self, image_size, batch_size, map_fn):
label_mode='int', train_ds = tf.keras.utils.image_dataset_from_directory(
batch_size=batch_size, directory='../datasets/cifar10/train/',
image_size=image_size, labels='inferred',
interpolation='nearest' label_mode='int',
) batch_size=batch_size,
image_size=image_size,
test_ds = tf.keras.utils.image_dataset_from_directory( interpolation='nearest'
directory='../datasets/cifar10/test/', )
labels='inferred',
label_mode='int', test_ds = tf.keras.utils.image_dataset_from_directory(
batch_size=batch_size, directory='../datasets/cifar10/test/',
image_size=image_size, labels='inferred',
shuffle=False, label_mode='int',
interpolation='nearest' batch_size=batch_size,
) image_size=image_size,
shuffle=False,
if map_fn is not None: interpolation='nearest'
train_ds = train_ds.map(map_fn).prefetch(tf.data.AUTOTUNE) )
test_ds = test_ds.map(map_fn).prefetch(tf.data.AUTOTUNE)
if map_fn is not None:
return train_ds, test_ds train_ds = train_ds.map(map_fn).prefetch(tf.data.AUTOTUNE)
test_ds = test_ds.map(map_fn).prefetch(tf.data.AUTOTUNE)
def load_dataset3(image_size=IMAGE_SIZE, batch_size=BATCH_SIZE, map_fn=None): return train_ds, test_ds
train_ds, test_ds = load_dataset(image_size=image_size, batch_size=batch_size, map_fn=map_fn)
def _split_dataset(self, train_ds, test_ds):
train_ds_size = tf.data.experimental.cardinality(train_ds).numpy() train_ds_size = train_ds.cardinality().numpy()
train_ds = train_ds.skip(train_ds_size / 10) train_ds = train_ds.skip(train_ds_size / 10)
val_ds = train_ds.take(train_ds_size / 10) val_ds = train_ds.take(train_ds_size / 10)
return train_ds, val_ds, test_ds
if True: \ No newline at end of file
print("CIFAR10 dataset loaded")
print("Training data size:", tf.data.experimental.cardinality(train_ds).numpy())
print("Validation data size:", tf.data.experimental.cardinality(val_ds).numpy())
print("Evaluation data size:", tf.data.experimental.cardinality(test_ds).numpy())
return train_ds, val_ds, test_ds
import tensorflow as tf import tensorflow as tf
from src.data.base import BaseDataset, DEFAULT_BATCH_SIZE
BATCH_SIZE = 32 DEFAULT_IMAGE_SIZE = (400, 320)
IMAGE_SIZE = (224, 224)
NUM_CLASSES = 10
CLASS_NAMES = ['fish', 'dog', 'player', 'saw', 'building', 'music', 'truck', 'gas', 'ball', 'parachute'] CLASS_NAMES = ['fish', 'dog', 'player', 'saw', 'building', 'music', 'truck', 'gas', 'ball', 'parachute']
class Imagenette(BaseDataset):
def load_dataset(image_size=IMAGE_SIZE, batch_size=BATCH_SIZE, map_fn=None): def __init__(self, image_size=DEFAULT_IMAGE_SIZE, batch_size=DEFAULT_BATCH_SIZE, map_fn=None):
train_ds = tf.keras.utils.image_dataset_from_directory( super(Imagenette, self).__init__(name='imagenette', classes=CLASS_NAMES, image_size=image_size, batch_size=batch_size, map_fn=map_fn)
directory='../datasets/imagenette2/train/',
labels='inferred', def _load_dataset(self, image_size, batch_size, map_fn):
label_mode='int', train_ds = tf.keras.utils.image_dataset_from_directory(
batch_size=batch_size, directory='../datasets/imagenette2/train/',
image_size=image_size, labels='inferred',
interpolation='nearest' label_mode='int',
) batch_size=batch_size,
image_size=image_size,
test_ds = tf.keras.utils.image_dataset_from_directory( interpolation='nearest'
directory='../datasets/imagenette2/val/', )
labels='inferred',
label_mode='int', test_ds = tf.keras.utils.image_dataset_from_directory(
batch_size=batch_size, directory='../datasets/imagenette2/val/',
image_size=image_size, labels='inferred',
shuffle=False, label_mode='int',
interpolation='nearest' batch_size=batch_size,
) image_size=image_size,
shuffle=False,
if map_fn is not None: interpolation='nearest'
train_ds = train_ds.map(map_fn).prefetch(tf.data.AUTOTUNE) )
test_ds = test_ds.map(map_fn).prefetch(tf.data.AUTOTUNE)
if map_fn is not None:
return train_ds, test_ds train_ds = train_ds.map(map_fn).prefetch(tf.data.AUTOTUNE)
test_ds = test_ds.map(map_fn).prefetch(tf.data.AUTOTUNE)
def load_dataset3(image_size=IMAGE_SIZE, batch_size=BATCH_SIZE, map_fn=None): return train_ds, test_ds
train_ds, test_ds = load_dataset(image_size=image_size, batch_size=batch_size, map_fn=map_fn)
def _split_dataset(self, train_ds, test_ds):
test_ds_size = tf.data.experimental.cardinality(test_ds).numpy() test_ds_size = test_ds.cardinality().numpy()
val_ds = test_ds.take(test_ds_size / 2) val_ds = test_ds.take(test_ds_size / 2)
test_ds = test_ds.skip(test_ds_size / 2) test_ds = test_ds.skip(test_ds_size / 2)
return train_ds, val_ds, test_ds
if True:
print("Imagenette dataset loaded")
print("Training data size:", tf.data.experimental.cardinality(train_ds).numpy())
print("Validation data size:", tf.data.experimental.cardinality(val_ds).numpy())
print("Evaluation data size:", tf.data.experimental.cardinality(test_ds).numpy())
return train_ds, val_ds, test_ds
import tensorflow as tf import tensorflow as tf
from src.data.base import BaseDataset
BATCH_SIZE = 6 DEFAULT_BATCH_SIZE = 6
IMAGE_SIZE = (400, 320) DEFAULT_IMAGE_SIZE = (400, 320)
NUM_CLASSES = 3
CLASS_NAMES = ['building', 'dog', 'player'] CLASS_NAMES = ['building', 'dog', 'player']
class Simple3(BaseDataset):
def load_dataset(image_size=IMAGE_SIZE, batch_size=BATCH_SIZE, map_fn=None): def __init__(self, image_size=DEFAULT_IMAGE_SIZE, batch_size=DEFAULT_BATCH_SIZE, map_fn=None):
ds = tf.keras.utils.image_dataset_from_directory( super(Simple3, self).__init__(name='simple3', classes=CLASS_NAMES, image_size=image_size, batch_size=batch_size, map_fn=map_fn)
directory='../datasets/simple3/',
labels='inferred', def _load_dataset(self, image_size, batch_size, map_fn):
label_mode='int', ds = tf.keras.utils.image_dataset_from_directory(
batch_size=batch_size, directory='../datasets/simple3/',
image_size=image_size, labels='inferred',
interpolation='nearest' label_mode='int',
) batch_size=batch_size,
image_size=image_size,
if map_fn is not None: interpolation='nearest'
ds = ds.map(map_fn).prefetch(tf.data.AUTOTUNE) )
return ds if map_fn is not None:
ds = ds.map(map_fn).prefetch(tf.data.AUTOTUNE)
def load_dataset3(image_size=IMAGE_SIZE, batch_size=BATCH_SIZE, map_fn=None): return ds
ds = load_dataset(image_size=image_size, batch_size=batch_size, map_fn=map_fn)
def _split_dataset(self, ds):
ds_size = tf.data.experimental.cardinality(ds).numpy() ds_size = ds.cardinality().numpy()
train_ds = ds.take(ds_size * 0.6) train_ds = ds.take(ds_size * 0.6)
val_ds = ds.skip(ds_size * 0.6).take(ds_size * 0.2) val_ds = ds.skip(ds_size * 0.6).take(ds_size * 0.2)
test_ds = ds.skip(ds_size * 0.6).skip(ds_size * 0.2) test_ds = ds.skip(ds_size * 0.6).skip(ds_size * 0.2)
return train_ds, val_ds, test_ds
if True:
print("Simple 3 dataset loaded")
print("Total dataset size:", ds_size)
print("Training data size:", tf.data.experimental.cardinality(train_ds).numpy())
print("Validation data size:", tf.data.experimental.cardinality(val_ds).numpy())
print("Evaluation data size:", tf.data.experimental.cardinality(test_ds).numpy())
return train_ds, val_ds, test_ds
import sys import sys
sys.path.append("..") sys.path.append("..")
from src.data.cifar10 import load_dataset3, NUM_CLASSES
from src.utils.embeddings import project_embeddings, calc_vectors, save_embeddings
from src.utils.common import get_modeldir, get_datadir
from src.model.efficientnet import EfficientNetModel, TARGET_SHAPE, BATCH_SIZE from src.model.efficientnet import EfficientNetModel, TARGET_SHAPE, BATCH_SIZE
from src.data.imagenette import Imagenette
from src.data.cifar10 import Cifar10
from src.utils.embeddings import project_embeddings, load_weights_of, get_embeddings_of, save_embeddings
from src.model.siamese import SiameseModel from src.model.siamese import SiameseModel
model_name = 'cifar10_efficientnet' dataset = Imagenette(image_size=TARGET_SHAPE, batch_size=BATCH_SIZE, map_fn=EfficientNetModel.preprocess_input)
embeddings_name = model_name + '_embeddings' # dataset = Cifar10(image_size=TARGET_SHAPE, batch_size=BATCH_SIZE, map_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() model = EfficientNetModel()
model.summary() model.compile()
load_weights_of(model, dataset)
print('calculating embeddings...')
emb_vectors, emb_labels = calc_vectors(comb_ds, model)
save_embeddings(emb_vectors, emb_labels, embeddings_name)
# emb_vectors, emb_labels = load_embeddings(embeddings_name) emb_vectors, emb_labels = get_embeddings_of(model, dataset)
emb_ds = SiameseModel.prepare_dataset(emb_vectors, emb_labels)
# siamese is the model we train siamese = SiameseModel(embedding_model=model, image_vector_dimensions=512)
siamese = SiameseModel(embedding_vector_dimension=1280, image_vector_dimensions=128)
siamese.compile(loss_margin=0.05) siamese.compile(loss_margin=0.05)
siamese.summary() siamese.fit(emb_ds, num_classes=dataset.num_classes)
ds = SiameseModel.prepare_dataset(emb_vectors, emb_labels)
history = siamese.fit(ds, class_weight={0: 1 / NUM_CLASSES, 1: (NUM_CLASSES - 1) / NUM_CLASSES})
# Build full inference model (from image to image vector):
inference_model = siamese.get_inference_model(model)
inference_model.save(get_modeldir(model_name + '_inference.tf'))
# inference_model = tf.keras.models.load_model(get_modeldir(model_name + '_inference.tf'), compile=False)
print('visualization') projection_vectors = siamese.projection_model.predict(emb_vectors)
# compute vectors of the images and their labels, store them in a tsv file for visualization save_embeddings(projection_vectors, emb_labels, dataset.name + '_' + siamese.name + '_vectors')
projection_vectors = siamese.get_projection_model().predict(emb_vectors) project_embeddings(projection_vectors, emb_labels, siamese.name + '_' + dataset.name)
project_embeddings(projection_vectors, emb_labels, model_name + '_siamese') print('Done!')
import sys import sys
sys.path.append("..") sys.path.append("..")
import csv
from src.utils.hsv import * from src.utils.hsv import *
from src.utils.sift import * from src.utils.sift import *
from src.utils.embeddings import * from src.data.cifar10 import Cifar10
from src.data.cifar10 import *
train_ds, test_ds = load_dataset() train_ds, test_ds = Cifar10()
cifar10_vds = train_ds.concatenate(test_ds) cifar10_vds = train_ds.concatenate(test_ds)
...@@ -41,11 +41,6 @@ def export_sift(nfeatures=8): ...@@ -41,11 +41,6 @@ def export_sift(nfeatures=8):
writer.writerow([i, label_str, value_str]) writer.writerow([i, label_str, value_str])
def export_embeddings():
embeddings, embeddings_labels = load_embeddings(name='cifar10_alexnet_embeddings')
export_embeddings(embeddings, embeddings_labels, 'alexnet')
# HSV # HSV
# export_hsv(170, 171, 171) # 512 # export_hsv(170, 171, 171) # 512
# export_hsv(340, 342, 342) # 1024 # export_hsv(340, 342, 342) # 1024
...@@ -59,5 +54,4 @@ def export_embeddings(): ...@@ -59,5 +54,4 @@ def export_embeddings():
# export_sift(32) # export_sift(32)
# Siamese Embeddings # Siamese Embeddings
export_embeddings() print('Done!')
print('done')
...@@ -2,59 +2,28 @@ import sys ...@@ -2,59 +2,28 @@ import sys
sys.path.append("..") sys.path.append("..")
import tensorflow as tf import tensorflow as tf
from src.data.imagenette import load_dataset3, NUM_CLASSES from src.model.mobilenet import MobileNetModel, PRETRAIN_EPOCHS, TARGET_SHAPE
from src.utils.embeddings import save_embeddings, project_embeddings, calc_vectors from src.data.imagenette import Imagenette
from src.utils.common import get_modeldir from src.data.cifar10 import Cifar10
from src.model.mobilenet import MobileNetModel, PRETRAIN_EPOCHS, TARGET_SHAPE, EMBEDDING_VECTOR_DIMENSION from src.utils.embeddings import project_embeddings, load_weights_of, get_embeddings_of, save_embeddings
from src.model.siamese import SiameseModel from src.model.siamese import SiameseModel
model_name = 'imagenette_mobilenet' dataset = Imagenette(image_size=TARGET_SHAPE, map_fn=MobileNetModel.preprocess_input)
embeddings_name = model_name + '_embeddings' # dataset = Cifar10(image_size=TARGET_SHAPE, map_fn=MobileNetModel.preprocess_input)
PRETRAIN_TOTAL_STEPS = PRETRAIN_EPOCHS * len(dataset.get_train())
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)
model = MobileNetModel() model = MobileNetModel()
model.compile(optimizer=tf.keras.optimizers.RMSprop(tf.keras.optimizers.schedules.CosineDecay(1e-3, PRETRAIN_TOTAL_STEPS))) model.compile(optimizer=tf.keras.optimizers.RMSprop(tf.keras.optimizers.schedules.CosineDecay(1e-3, PRETRAIN_TOTAL_STEPS)))
model.summary() load_weights_of(model, dataset)
# load weights
# 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'))
# evaluate
print('evaluating...')
model.evaluate(test_ds)