Siameser is a module to facilitate training a feature extractor neural network using triplet loss, distance between anchor and positive < distancec between anchor and negative
.
- clone the repo
git clone https://github.com/aielawady/Siameser.git
- Import the libraries
import Siameser.core as core
import Siameser.utils as utils
- Create the triplets.
train_siamese = utils.tripler(np.arange(len(train_x)), train_y, classnames=set(train_y))
Please note that
utils.tripler
currently takes the ID of the training examples, e.g. file name or index, not the data itself.
- Load the data using the IDs of the
train_siamese
.
# m: number of examples, H,W,C: dimension of the examples
X_loaded = [np.zeros((m,H,W,C), dtype='float32') for i in range(3)]
for i, ID in enumerate(train_siamese.T):
for j in range(3):
X_loaded[j][i,:,:,:] = train_x[ID[j]]
- Buld the Siamese Model, compile and train.
siamese_model = core.siamese_modeller(feature_extractor,input_shape=(H,W,C))
siamese_model.compile(...)
siamese_model.fit(X_loaded, np.zeros((len(X_loaded[0]),)), ...)
Please note that there's no need for the labels as the dataset is formed in this order
anchor, positive, negative