Giter VIP home page Giter VIP logo

image_triplet_loss's Introduction

Triplet Loss for Image Similarity using tensorflow

This repository is an implementation of following "medium" story: Image similarity using Triplet Loss

Requirements

  • Python 3
  • Pip 3
  • Tensorflow
  • Matplotlib
  • Requests

Environment Setup

Execute requirements.txt to install dependency packages

pip install -r requirements.txt

Training

  1. Download Training Dataset by executing download_dataset.py
python download_dataset.py
  1. To train
python train_triplets.py 

Prediction

Run Prediction.ipynb using Jupyter notebook to look into Prediction code.

Prediction.ipynb

image_triplet_loss's People

Contributors

sanku-lib 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

image_triplet_loss's Issues

ValueError: Convolution expects input with rank 4, got 1

Hello!
Thanks for sharing your project.
I tried to run training with my own dataset but the following error was occured:

Dataset loaded successfully.
Preprocessing Done. Summary:
Images train : (837,)
Labels train : (837,)
Images test  : (91,)
Labels test  : (91,)
Unique label : [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 24 25 26 27 28 29]
placeholder_shape [None]
WARNING:tensorflow:From train_triplets.py:25: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.

WARNING:tensorflow:From /home/user/similarity/sanku/model.py:8: The name tf.variable_scope is deprecated. Please use tf.compat.v1.variable_scope instead.

WARNING:tensorflow:
The TensorFlow contrib module will not be included in TensorFlow 2.0.
For more information, please see:
  * https://github.com/tensorflow/community/blob/master/rfcs/20180907-contrib-sunset.md
  * https://github.com/tensorflow/addons
  * https://github.com/tensorflow/io (for I/O related ops)
If you depend on functionality not listed there, please file an issue.

Traceback (most recent call last):
  File "train_triplets.py", line 30, in <module>
    anchor_output = model.conv_net(anchor_input, reuse=False)
  File "/home/user/similarity/sanku/model.py", line 11, in conv_net
    scope=scope, reuse=reuse)
  File "/home/user/.local/lib/python3.6/site-packages/tensorflow/contrib/framework/python/ops/arg_scope.py", line 182, in func_with_args
    return func(*args, **current_args)
  File "/home/user/.local/lib/python3.6/site-packages/tensorflow/contrib/layers/python/layers/layers.py", line 1159, in convolution2d
    conv_dims=2)
  File "/home/user/.local/lib/python3.6/site-packages/tensorflow/contrib/framework/python/ops/arg_scope.py", line 182, in func_with_args
    return func(*args, **current_args)
  File "/home/user/.local/lib/python3.6/site-packages/tensorflow/contrib/layers/python/layers/layers.py", line 1025, in convolution
    (conv_dims + 2, input_rank))
ValueError: Convolution expects input with rank 4, got 1

Please help. What should I improve for a launch?

My packages versions are all from requirements.txt. Besides, I tried to run the program without any env (tensorflow 1.14, numpy 1.18) but got the same error.

give one input and want output through that.

Hi ,
Can You please help me, how can I pick only one image while testing? Right now it picks random images (random images through index value). I want to give one image and get output through that.

NameError: name 'TripletLoss' is not defined

Hello,

thank you for your great project!
I'm currently trying to execute your code. However, when I try to execute the jupyter notebook, cell 6 throws the following error:

model = TripletLoss()
"NameError: name 'TripletLoss' is not defined".

Is there anything which needs to be additionally installed for this?
Best wishes

Edit: Is it possible, that "from model import TripletLossetLoss" is meant to be "from model import TripletLoss"?

Different flavor of layers

Hi,

Thanks for the wonderful algorithm. Helped me a lot.
I am trying to implement a different flavor of networks for my algortihm:

def conv_net(self, inp, reuse=False):
        vgg_model = VGG16(weights=None, include_top=False,input_shape=(32,32,3),input_tensor=inp)
        #print(" vgg_model_input {}".format(vgg_model.input))
        x = vgg_model.output
        x = GlobalAveragePooling2D()(x)
        x = Dense(4096, activation='relu')(x)
        x = Dropout(0.6)(x)
        x = Dense(4096, activation='relu')(x)
        x = Dropout(0.6)(x)
        x = Lambda(lambda  x_: K.l2_normalize(x,axis=1))(x)
        convnet_model = Model(inputs=vgg_model.input,outputs=x)
        
 
        first_conv = Conv2D(96, kernel_size=(8, 8),strides=(16,16), padding='same',name="conv_layer_1")(inp)
        first_max = MaxPool2D(pool_size=(3,3),strides = (4,4),padding='same')(first_conv)
        first_max = Flatten()(first_max)
        first_max = Lambda(lambda  x: K.l2_normalize(x,axis=1))(first_max)


        second_conv = Conv2D(96, kernel_size=(8, 8),strides=(32,32), padding='same',name="conv_layer_2")(inp)
        second_max = MaxPool2D(pool_size=(7,7),strides = (2,2),padding='same')(second_conv)
        second_max = Flatten()(second_max)
        second_max = Lambda(lambda  x: K.l2_normalize(x,axis=1))(second_max)

        merge_one = concatenate([first_max, second_max])

        merge_two = concatenate([merge_one, convnet_model.output])
        emb = Dense(512)(merge_two)
        l2_norm_final = Lambda(lambda  x: K.l2_normalize(x,axis=1))(emb)
        #emb = Flatten()(emb)
        print("Embedding {}".format(emb))
        
        return l2_norm_final

However when I train using these layers, my model only produces nonzero loss for first iteration, all further losses are 0.0.

Could you think of a reason why that might be happening ?

I am new to tensorflow to the struggles.

Thanks

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.