GithubHelp home page GithubHelp logo

ardiya / siamesenetwork-tensorflow Goto Github PK

View Code? Open in Web Editor NEW
280.0 8.0 83.0 33.51 MB

Using siamese network to do dimensionality reduction and similar image retrieval

License: MIT License

Python 35.97% Shell 0.18% Jupyter Notebook 63.85%
tensorflow siamese-network dimensionality-reduction image-retrieval

siamesenetwork-tensorflow's Introduction

Siamese Network Tensorflow

Siamese network is a neural network that contain two or more identical subnetwork. The objective of this network is to find the similarity or comparing the relationship between two comparable things. Unlike classification task that uses cross entropy as the loss function, siamese network usually uses contrastive loss or triplet loss.

Siamese network has a lot of function, this repository is trying to use Siamese network to do a dimensionality reduction and image retrieval.

This project follows Hadsell-et-al.'06 [1] by computing the Euclidean distance on the output of the shared network and by optimizing the contrastive loss (see paper for more details). The contastive loss is defined as follows

-contrastive loss function-

The -D formula- is the distance of between the output of the network N with the input Xleft and the input Xright.

The similarity function is defined as -sim function-. This function will be activated when the Label Y equal to 1 and deactivated when Y is equal to 0. The goal of this function is to minimize the distance of the pairs.

The dissimilarity function is defined as -dissim function-. This function will be activated when the Label Y is equal to 0 and deactivated when Y is equal to 1. The goal of this function is to give a penalty of the pairs when the distance is lower than margin m.

[1] "Dimensionality Reduction by Learning an Invariant Mapping" http://yann.lecun.com/exdb/publis/pdf/hadsell-chopra-lecun-06.pdf

Model

The input of these will be image_left, image_right and . Our model uses 5 layer of convolutional layer and pooling followed. We do not use fully convolutonal net because convolution operation is faster on GPU(especially using CUDNN). See http://cs231n.github.io/convolutional-networks/#convert for more information on converting FC layer to Conv layer.

Run

Train the model

git clone https://github.com/ardiya/siamesenetwork-tensorflow
python train.py

Tensorboard Visualization(After training)

tensorboard --logdir=train.log

Updates

  • Update the API to 1.0
  • Cleanup the old code

Dimensionality reduction

The images below shows the final Result on MNIST test dataset. By only using 2 features, we can easily separate the input images.

The gif below shows some animation until it somehow converges.

Image retrieval

Image retrieval uses the trained model to extract the features and get the most similar image using cosine similarity. See here

Retrieving similar test image from trainset

  • Select id 865 in test image

  • Retrieved top n similar image from train data with ids of [53144 47864 11074 51561 41350 34215 48182] from train data

siamesenetwork-tensorflow's People

Contributors

akshitbhalla avatar ardiya avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

siamesenetwork-tensorflow's Issues

Using another dataset

I want to train this model for a set of objects, and not MNIST. Any suggestions on how I could do that ?

Why an extra axis is added in dataset.py for class MNISTDataset

Hi ,
i have a question regarding the dataset,py. I am trying to create my own dataset to test the model and in the dataset.py in

class MNISTDataset(Dataset):
    def __init__(self):
	print("===Loading MNIST Dataset===")
	(self.images_train, self.labels_train), (self.images_test, self.labels_test) = mnist.load_data()
	self.images_train = np.expand_dims(self.images_train, axis=3) / 255.0
	self.images_test = np.expand_dims(self.images_test, axis=3) / 255.0

Can anyone explain why we are adding a new axis? I am trying to create a dataset witg RGB channels. And i dont know if the MNIST dataset is in grayscale?

inference

hello, how to test the trained model??

Running on non-MNIST greyscale images

Hi ardiya!

Thanks for putting together this repository. Could you tell me what would need to be done to run this on non-MNIST greyscale images?

Thanks so much in advance!
Lilly

Error use_nesterov

I got this error while trying to train.

File "train.py", line 40, in
train_step = tf.train.MomentumOptimizer(0.01, 0.99, use_nesterov=True).minimize(loss, global_step=global_step)
TypeError: init() got an unexpected keyword argument 'use_nesterov'

Creating batches

I noticed that you pass FLAGS.batch_size to the gen.next_batch function but in the next_batch function, you don't actually use the batch_size variable at all. Can you please explain how you make them into batches?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.