tensorfreitas / siamese-networks-for-one-shot-learning Goto Github PK
View Code? Open in Web Editor NEWImplementation of Siamese Neural Networks for One-shot Image Recognition
Implementation of Siamese Neural Networks for One-shot Image Recognition
Hi,
I am a beginner in this field and I am really curious about one shot learning because I am facing the lack of data problem. So can you tell me how to use the resulted model to do the prediction.
Thank you
First of all, thanks to your code, I have a deeper understanding of the paper.I met a problem in the training process, I hope the author can help me to solve it.Thank you.Here's the problem.
Traceback (most recent call last):
File "D:\PyCharm 5.0.3\helpers\pydev\pydevd.py", line 2407, in
globals = debugger.run(setup['file'], None, None, is_module)
File "D:\PyCharm 5.0.3\helpers\pydev\pydevd.py", line 1798, in run
launch(file, globals, locals) # execute the script
File "D:\PyCharm 5.0.3\helpers\pydev_pydev_imps_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "D:/software/github/Siamese-Networks-for-One-Shot-Learning/train_siamese_network.py", line 59, in
main()
File "D:/software/github/Siamese-Networks-for-One-Shot-Learning/train_siamese_network.py", line 31, in main
tensorboard_log_path=tensorboard_log_path
File "D:/software/github/Siamese-Networks-for-One-Shot-Learning\siamese_network.py", line 72, in init
l2_regularization_penalization)
File "D:/software/github/Siamese-Networks-for-One-Shot-Learning\siamese_network.py", line 142, in _construct_siamese_architecture
momentum=0.5)
File "D:/software/github/Siamese-Networks-for-One-Shot-Learning\modified_sgd.py", line 42, in init
self.lr = K.variable(lr, name='lr')
AttributeError: can't set attribute
Hi,
I tried to train the network with a server using two GPUs still it's so much time consuming.
Could you please share the weights and biases for pretrained network.
Thank you
Hi, may I request for a requirement.txt file so I know what to pip install.
And are you using python 3.5 or 3.7?
Thank you.
Hello,
I'm trying to run your training script, but cudnn does not initalize. It is due to some conflicts with my tensorflow version apparently. Can you precise me the version you used to run your scripts, please?
Herlderlord
Hey, thanks for the great work. Can you please tell me what versions of Keras and TensorFlow did you use? I am facing issues running the code on the present version. Thanks!
Traceback (most recent call last):
File "/home/user/ShadowCreative/Siamese network/Siamese-Networks-for-One-Shot-Learning/train_siamese_network.py", line 59, in
main()
File "/home/user/ShadowCreative/Siamese network/Siamese-Networks-for-One-Shot-Learning/train_siamese_network.py", line 46, in main
model_name='siamese_net_lr10e-4')
File "/home/user/ShadowCreative/Siamese network/Siamese-Networks-for-One-Shot-Learning/siamese_network.py", line 235, in train_siamese_network
train_loss, train_accuracy = self.model.train_on_batch(images, labels)
File "/home/user/anaconda3/envs/tensorflow-gpu/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1727, in train_on_batch
logs = self.train_function(iterator)
File "/home/user/anaconda3/envs/tensorflow-gpu/lib/python3.6/site-packages/tensorflow/python/eager/def_function.py", line 828, in call
result = self._call(*args, **kwds)
File "/home/user/anaconda3/envs/tensorflow-gpu/lib/python3.6/site-packages/tensorflow/python/eager/def_function.py", line 871, in _call
self._initialize(args, kwds, add_initializers_to=initializers)
File "/home/user/anaconda3/envs/tensorflow-gpu/lib/python3.6/site-packages/tensorflow/python/eager/def_function.py", line 726, in _initialize
*args, **kwds))
File "/home/user/anaconda3/envs/tensorflow-gpu/lib/python3.6/site-packages/tensorflow/python/eager/function.py", line 2969, in _get_concrete_function_internal_garbage_collected
graph_function, _ = self._maybe_define_function(args, kwargs)
File "/home/user/anaconda3/envs/tensorflow-gpu/lib/python3.6/site-packages/tensorflow/python/eager/function.py", line 3361, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "/home/user/anaconda3/envs/tensorflow-gpu/lib/python3.6/site-packages/tensorflow/python/eager/function.py", line 3206, in _create_graph_function
capture_by_value=self._capture_by_value),
File "/home/user/anaconda3/envs/tensorflow-gpu/lib/python3.6/site-packages/tensorflow/python/framework/func_graph.py", line 990, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File "/home/user/anaconda3/envs/tensorflow-gpu/lib/python3.6/site-packages/tensorflow/python/eager/def_function.py", line 634, in wrapped_fn
out = weak_wrapped_fn().wrapped(*args, **kwds)
File "/home/user/anaconda3/envs/tensorflow-gpu/lib/python3.6/site-packages/tensorflow/python/framework/func_graph.py", line 977, in wrapper
raise e.ag_error_metadata.to_exception(e)
TypeError: in user code:
/home/user/anaconda3/envs/tensorflow-gpu/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py:805 train_function *
return step_function(self, iterator)
/home/user/anaconda3/envs/tensorflow-gpu/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py:795 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
/home/user/anaconda3/envs/tensorflow-gpu/lib/python3.6/site-packages/tensorflow/python/distribute/distribute_lib.py:1259 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/home/user/anaconda3/envs/tensorflow-gpu/lib/python3.6/site-packages/tensorflow/python/distribute/distribute_lib.py:2730 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
/home/user/anaconda3/envs/tensorflow-gpu/lib/python3.6/site-packages/tensorflow/python/distribute/distribute_lib.py:3417 _call_for_each_replica
return fn(*args, **kwargs)
/home/user/anaconda3/envs/tensorflow-gpu/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py:788 run_step **
outputs = model.train_step(data)
/home/user/anaconda3/envs/tensorflow-gpu/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py:757 train_step
self.optimizer.minimize(loss, self.trainable_variables, tape=tape)
/home/user/anaconda3/envs/tensorflow-gpu/lib/python3.6/site-packages/tensorflow/python/keras/optimizer_v2/optimizer_v2.py:497 minimize
loss, var_list=var_list, grad_loss=grad_loss, tape=tape)
/home/user/anaconda3/envs/tensorflow-gpu/lib/python3.6/site-packages/tensorflow/python/keras/optimizer_v2/optimizer_v2.py:547 _compute_gradients
with ops.name_scope_v2(self._name + "/gradients"):
TypeError: unsupported operand type(s) for +: 'Modified_SGD' and 'str'
Process finished with exit code 1
@@@@@
Hi, i met some problems
Hi,
I tried to trian the network, but i can't find the tensorboard_log_path and the model weights?
Could you please share these.
Hello,
Can you please let me know where can I find author's implementation? You did mention that it slightly different. And what was the accuracy rate of your implementation? Thanks
hello,how do i run it for two different new images? thank you
Hi, I'm trying to run this with my own dataset (which only has characters and no alphabets).
I'm having trouble modifying this..
# First let's take care of the train alphabets for alphabet in os.listdir(train_path): alphabet_path = os.path.join(train_path, alphabet) current_alphabet_dictionary = {} for character in os.listdir(alphabet_path): character_path = os.path.join(alphabet_path, character) current_alphabet_dictionary[character] = os.listdir( character_path) self.train_dictionary[alphabet] = current_alphabet_dictionary
I've tried changing this to
for alphabet in os.listdir(train_path): alphabet_path = os.path.join(train_path, alphabet) current_alphabet_dictionary = {} for character in os.listdir(alphabet_path): character_path = os.path.join(alphabet_path, character) current_alphabet_dictionary[character] = character_path self.train_dictionary[alphabet] = current_alphabet_dictionary
My character path is something like this(with the number '498' being the label)
character path isdata/train/raw/498/996.png
I'm getting a type error saying that
TypeError: list indices must be integers or slices, not str
This is because the dictionary is supposed to have keys and values, but what if I don't have any key(alphabets) to work with, and only have values(characters)?
Hi, i cloned the repo and tried running it as it is but my accuracy doesn't seem to be improving. Is it because of the TensorFlow warning that I get? Is there any pre-processing that I'm supposed to do before running the train_siamese_networks
(mypython3) MacBook-Pro-3:one-shot Sabrinai$ python train_siamese_network.py
Using TensorFlow backend.
2018-08-14 18:21:16.735624: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
Iteration 1/1000000: Train loss: 4.849324, Train Accuracy: 0.531250, lr = 0.001000
Iteration 2/1000000: Train loss: 4.848513, Train Accuracy: 0.453125, lr = 0.001000
Iteration 3/1000000: Train loss: 4.848382, Train Accuracy: 0.515625, lr = 0.001000
Iteration 4/1000000: Train loss: 4.848629, Train Accuracy: 0.546875, lr = 0.001000
Iteration 5/1000000: Train loss: 4.848142, Train Accuracy: 0.578125, lr = 0.001000
Iteration 6/1000000: Train loss: 4.848841, Train Accuracy: 0.468750, lr = 0.001000
Iteration 7/1000000: Train loss: 4.846568, Train Accuracy: 0.578125, lr = 0.001000
Iteration 8/1000000: Train loss: 4.847251, Train Accuracy: 0.484375, lr = 0.001000
Iteration 9/1000000: Train loss: 4.846010, Train Accuracy: 0.578125, lr = 0.001000
Iteration 10/1000000: Train loss: 4.846083, Train Accuracy: 0.562500, lr = 0.001000
Iteration 11/1000000: Train loss: 4.846534, Train Accuracy: 0.437500, lr = 0.001000
Iteration 12/1000000: Train loss: 4.846575, Train Accuracy: 0.484375, lr = 0.001000
Iteration 13/1000000: Train loss: 4.846043, Train Accuracy: 0.468750, lr = 0.001000
Iteration 14/1000000: Train loss: 4.844590, Train Accuracy: 0.609375, lr = 0.001000
Iteration 15/1000000: Train loss: 4.844895, Train Accuracy: 0.468750, lr = 0.001000
Iteration 16/1000000: Train loss: 4.844888, Train Accuracy: 0.468750, lr = 0.001000
Iteration 17/1000000: Train loss: 4.845278, Train Accuracy: 0.484375, lr = 0.001000
Iteration 18/1000000: Train loss: 4.843062, Train Accuracy: 0.562500, lr = 0.001000
Iteration 19/1000000: Train loss: 4.843076, Train Accuracy: 0.593750, lr = 0.001000
Iteration 20/1000000: Train loss: 4.842909, Train Accuracy: 0.437500, lr = 0.001000
Iteration 21/1000000: Train loss: 4.842513, Train Accuracy: 0.531250, lr = 0.001000
Iteration 22/1000000: Train loss: 4.841990, Train Accuracy: 0.484375, lr = 0.001000
Iteration 23/1000000: Train loss: 4.841151, Train Accuracy: 0.500000, lr = 0.001000
Iteration 24/1000000: Train loss: 4.841326, Train Accuracy: 0.500000, lr = 0.001000
Iteration 25/1000000: Train loss: 4.840984, Train Accuracy: 0.437500, lr = 0.001000
Iteration 26/1000000: Train loss: 4.838634, Train Accuracy: 0.515625, lr = 0.001000
Iteration 27/1000000: Train loss: 4.838871, Train Accuracy: 0.609375, lr = 0.001000
Iteration 28/1000000: Train loss: 4.837312, Train Accuracy: 0.609375, lr = 0.001000
Iteration 29/1000000: Train loss: 4.838040, Train Accuracy: 0.578125, lr = 0.001000
Iteration 30/1000000: Train loss: 4.836654, Train Accuracy: 0.531250, lr = 0.001000
Iteration 31/1000000: Train loss: 4.835261, Train Accuracy: 0.578125, lr = 0.001000
Iteration 32/1000000: Train loss: 4.836050, Train Accuracy: 0.515625, lr = 0.001000
Iteration 33/1000000: Train loss: 4.835767, Train Accuracy: 0.515625, lr = 0.001000
Iteration 34/1000000: Train loss: 4.834557, Train Accuracy: 0.500000, lr = 0.001000
Iteration 35/1000000: Train loss: 4.833904, Train Accuracy: 0.500000, lr = 0.001000
Iteration 36/1000000: Train loss: 4.832893, Train Accuracy: 0.515625, lr = 0.001000
Iteration 37/1000000: Train loss: 4.832811, Train Accuracy: 0.453125, lr = 0.001000
Iteration 38/1000000: Train loss: 4.829530, Train Accuracy: 0.625000, lr = 0.001000
Iteration 39/1000000: Train loss: 4.830757, Train Accuracy: 0.421875, lr = 0.001000
Iteration 40/1000000: Train loss: 4.829597, Train Accuracy: 0.468750, lr = 0.001000
Iteration 41/1000000: Train loss: 4.830768, Train Accuracy: 0.531250, lr = 0.001000
Iteration 42/1000000: Train loss: 4.826791, Train Accuracy: 0.515625, lr = 0.001000
Iteration 43/1000000: Train loss: 4.825655, Train Accuracy: 0.578125, lr = 0.001000
Iteration 44/1000000: Train loss: 4.825454, Train Accuracy: 0.562500, lr = 0.001000
Iteration 45/1000000: Train loss: 4.823873, Train Accuracy: 0.578125, lr = 0.001000
Iteration 46/1000000: Train loss: 4.821697, Train Accuracy: 0.531250, lr = 0.001000
Iteration 47/1000000: Train loss: 4.821634, Train Accuracy: 0.546875, lr = 0.001000
Iteration 48/1000000: Train loss: 4.819137, Train Accuracy: 0.593750, lr = 0.001000
Iteration 49/1000000: Train loss: 4.818835, Train Accuracy: 0.531250, lr = 0.001000
Iteration 50/1000000: Train loss: 4.813269, Train Accuracy: 0.468750, lr = 0.001000
Iteration 51/1000000: Train loss: 4.813827, Train Accuracy: 0.562500, lr = 0.001000
Iteration 52/1000000: Train loss: 4.813818, Train Accuracy: 0.500000, lr = 0.001000
Iteration 53/1000000: Train loss: 4.813799, Train Accuracy: 0.437500, lr = 0.001000
Iteration 54/1000000: Train loss: 4.810773, Train Accuracy: 0.515625, lr = 0.001000
Iteration 55/1000000: Train loss: 4.808959, Train Accuracy: 0.500000, lr = 0.001000
Iteration 56/1000000: Train loss: 4.810541, Train Accuracy: 0.453125, lr = 0.001000
Iteration 57/1000000: Train loss: 4.807623, Train Accuracy: 0.453125, lr = 0.001000
Iteration 58/1000000: Train loss: 4.804411, Train Accuracy: 0.531250, lr = 0.001000
Iteration 59/1000000: Train loss: 4.800980, Train Accuracy: 0.546875, lr = 0.001000
Iteration 60/1000000: Train loss: 4.802567, Train Accuracy: 0.484375, lr = 0.001000
Iteration 61/1000000: Train loss: 4.801285, Train Accuracy: 0.468750, lr = 0.001000
Iteration 62/1000000: Train loss: 4.798449, Train Accuracy: 0.453125, lr = 0.001000
Iteration 63/1000000: Train loss: 4.795244, Train Accuracy: 0.515625, lr = 0.001000
Iteration 64/1000000: Train loss: 4.795055, Train Accuracy: 0.468750, lr = 0.001000
Iteration 65/1000000: Train loss: 4.790056, Train Accuracy: 0.500000, lr = 0.001000
Iteration 66/1000000: Train loss: 4.792620, Train Accuracy: 0.515625, lr = 0.001000
Iteration 67/1000000: Train loss: 4.791032, Train Accuracy: 0.468750, lr = 0.001000
Iteration 68/1000000: Train loss: 4.788491, Train Accuracy: 0.515625, lr = 0.001000
Iteration 69/1000000: Train loss: 4.787354, Train Accuracy: 0.500000, lr = 0.001000
Iteration 70/1000000: Train loss: 4.785830, Train Accuracy: 0.484375, lr = 0.001000
Iteration 71/1000000: Train loss: 4.783831, Train Accuracy: 0.484375, lr = 0.001000
Iteration 72/1000000: Train loss: 4.781487, Train Accuracy: 0.500000, lr = 0.001000
Iteration 73/1000000: Train loss: 4.779665, Train Accuracy: 0.546875, lr = 0.001000
Iteration 74/1000000: Train loss: 4.779678, Train Accuracy: 0.500000, lr = 0.001000
Iteration 75/1000000: Train loss: 4.777548, Train Accuracy: 0.484375, lr = 0.001000
Iteration 76/1000000: Train loss: 4.774950, Train Accuracy: 0.515625, lr = 0.001000
Iteration 77/1000000: Train loss: 4.774406, Train Accuracy: 0.437500, lr = 0.001000
Iteration 78/1000000: Train loss: 4.768193, Train Accuracy: 0.515625, lr = 0.001000
Iteration 79/1000000: Train loss: 4.769056, Train Accuracy: 0.500000, lr = 0.001000
Iteration 80/1000000: Train loss: 4.764924, Train Accuracy: 0.546875, lr = 0.001000
Iteration 81/1000000: Train loss: 4.766854, Train Accuracy: 0.500000, lr = 0.001000
Iteration 82/1000000: Train loss: 4.763232, Train Accuracy: 0.515625, lr = 0.001000
Iteration 83/1000000: Train loss: 4.761155, Train Accuracy: 0.531250, lr = 0.001000
Iteration 84/1000000: Train loss: 4.760967, Train Accuracy: 0.484375, lr = 0.001000
Iteration 85/1000000: Train loss: 4.758671, Train Accuracy: 0.562500, lr = 0.001000
Iteration 86/1000000: Train loss: 4.754441, Train Accuracy: 0.562500, lr = 0.001000
Iteration 87/1000000: Train loss: 4.756378, Train Accuracy: 0.500000, lr = 0.001000
Iteration 88/1000000: Train loss: 4.754009, Train Accuracy: 0.531250, lr = 0.001000
Iteration 89/1000000: Train loss: 4.769855, Train Accuracy: 0.515625, lr = 0.001000
Iteration 90/1000000: Train loss: 4.751012, Train Accuracy: 0.546875, lr = 0.001000
Iteration 91/1000000: Train loss: 4.749597, Train Accuracy: 0.562500, lr = 0.001000
Iteration 92/1000000: Train loss: 4.745842, Train Accuracy: 0.515625, lr = 0.001000
Iteration 93/1000000: Train loss: 4.747173, Train Accuracy: 0.484375, lr = 0.001000
Iteration 94/1000000: Train loss: 4.744622, Train Accuracy: 0.546875, lr = 0.001000
Iteration 95/1000000: Train loss: 4.743729, Train Accuracy: 0.484375, lr = 0.001000
Iteration 96/1000000: Train loss: 4.740994, Train Accuracy: 0.531250, lr = 0.001000
Iteration 97/1000000: Train loss: 4.739508, Train Accuracy: 0.531250, lr = 0.001000
Iteration 98/1000000: Train loss: 4.727015, Train Accuracy: 0.515625, lr = 0.001000
Iteration 99/1000000: Train loss: 4.733810, Train Accuracy: 0.578125, lr = 0.001000
Hello,
Can you tell me which version of tensorflow you used in this code?
Thanks for this code!
Hi, after the line 'Trained Ended!'
I ran into AttributeError: 'str' object has no attribute 'decode'.
I believe it happens when loading the weights
Thank you
Hi, there!
Thanks for your great work, it helps a lot and the application of siamese networks for one-shot learning also deserves further research.
However, I find you implement the loss function as binary_crossentropy
, in the following way:
I suppose it is the expected behavior but the paper (https://www.cs.cmu.edu/~rsalakhu/papers/oneshot1.pdf, oneshot1.pdf) says, the former part of the proposed loss function is:
which seems to be the opposite number of binary_crossentropy
and confuses me a lot. Does the author mean:
Or could you please provide some more explanation?
Thanks&Best Regards
I changed the max train iteration to 18000, and after the training process the evaluation result shows below:
Making One Shot Task on evaluation alphabets:
Atemayar_Qelisayer alphabet, accuracy: 1.0
ULOG alphabet, accuracy: 1.0
Sylheti alphabet, accuracy: 1.0
Angelic alphabet, accuracy: 1.0
Glagolitic alphabet, accuracy: 1.0
Ge_ez alphabet, accuracy: 1.0
Tengwar alphabet, accuracy: 1.0
Oriya alphabet, accuracy: 1.0
Avesta alphabet, accuracy: 1.0
Kannada alphabet, accuracy: 1.0
Aurek-Besh alphabet, accuracy: 1.0
Keble alphabet, accuracy: 1.0
Mongolian alphabet, accuracy: 1.0
Gurmukhi alphabet, accuracy: 1.0
Manipuri alphabet, accuracy: 1.0
Malayalam alphabet, accuracy: 1.0
Atlantean alphabet, accuracy: 1.0
Old_Church_Slavonic_(Cyrillic) alphabet, accuracy: 1.0
Tibetan alphabet, accuracy: 1.0
Syriac_(Serto) alphabet, accuracy: 1.0
Mean global accuracy: 1.0
Final Evaluation Accuracy = 1.0
Is that the same with you?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.