GithubHelp home page GithubHelp logo

ywpkwon / siamese_tf_mnist Goto Github PK

View Code? Open in Web Editor NEW
249.0 9.0 85.0 734 KB

Implementing Siamese Network using Tensorflow with MNIST

Home Page: https://github.com/ywpkwon/siamese_tf_mnist

Python 100.00%

siamese_tf_mnist's Introduction

Implementing Siamese Network using Tensorflow with MNIST

I have been interested in Siamese network. To my understanding, it is one way of dealing with weakly supervised problems. Its beauty lies in its simple scheme. It seems Siamese networks (and Triplet network) have been popularly used in many applications such as face similarity and image matching. A web page motivates me to implement a similar Siamese network using Tensorflow.

These codes here embed hand-written digits into 2D space. A loss function controls the embedding to be closer for digits in the same class and further for digits in the different classes. I borrowed visualization part from the original source with a little modification (including colors).

I kept codes minimal for my personal experiments (e.g., different architectures or loss functions).

  • run.py : nothing but a wrapper for running.
  • inference.py : architecture and loss definition. you can modify as you want.
  • visualize.py : visualizes result.

You can simply run :

$ python run.py
...
step 34750: loss 0.179
step 34760: loss 0.113
step 34770: loss 0.078
...

This will download and extract MNIST dataset (once downloaded, it will skip downloading next time). Once training done, a result will appear as in the image above. It saves an intermediate model regularly (with names model.*) while training.

When you run run.py, if an model file exists, you will be asked if you want to load it and continue training from that status. no will start training from the scratch.

$ python run.py
We found model.ckpt file. Do you want to load it [yes/no]? yes

For your convenience, while training, embedding results are keeping updated in embed.txt. So you can see an resulting embedding anytime (by stopping training or with a separate cmd/shell while training), using

$ python visualize.py

Please let me know if there are mistakes or comments. Thanks!

Youngwook Paul Kwon

siamese_tf_mnist's People

Contributors

colemurray avatar ywpkwon 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  avatar

siamese_tf_mnist's Issues

diffrence between two types of loss

Hi, Thanks for your work.
I noticed that you have implemented two loss functions in inference.py. loss_with_spring and loss_with_step.
The default one is loss_with_spring. However, by my understanding, loss_with_step corresponds to definition of contrastive-loss, which is used by caffe example.
Besides, the default margin value is set to 5,which is much larger than other's implementation. Could you please share your insight with me.

other datasets

Hi Youngwook,

Thanks for offering this implementation. I'm curious, how would I go about using my own images with this network? For example, I anticipate using 256x256 RGB images.

Thanks in advance!
Lilly

Definition of loss seems incorrect

Negative loss should be equal to (1-label)*ecud2. Also respective change should be made in possitive loss.
For exact equation see about contrastive loss

no model.ckpt, 3 model.ckpt.* files instead

Thanks for the code! A couple of small fixes:

I bet this is due to a tf version change: tf: 1.3.0
Rather than a model.ckpt file, I see

-rw-r--r-- 1 priot staff 7421960 Aug 19 13:53 model.ckpt.data-00000-of-00001
-rw-r--r-- 1 priot staff 283 Aug 19 13:53 model.ckpt.index
-rw-r--r-- 1 priot staff 59654 Aug 19 13:53 model.ckpt.meta

So this code fails:

model_ckpt = 'model.ckpt'
if os.path.isfile(model_ckpt):

Fix: model_ckpt = 'model.ckpt.meta'

Next problem:

File "run.py", line 70, in
visualize.visualize(embed, x_test)
NameError: name 'embed' is not defined

Fix: insert the 2 'embed' lines at the end of run.py:

visualize result

embed = np.fromfile('embed.txt', dtype=np.float32)
embed = embed.reshape([-1, 2])
x_test = mnist.test.images.reshape([-1, 28, 28])
visualize.visualize(embed, x_test)

Theory behind the implementation?

In particular looking for explanations/link for loss_with_spring function (loss_with_step is not used)

And in general is there a blog post that links your implementation with the logic behind it?

In fact, there is a relative answer here: Contrastive loss: https://stackoverflow.com/a/38270293/720484
But your implementation is a little bit different and would be good to have some explanation of why the details differ from a simple contrastive loss. Thank you

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.