GithubHelp home page GithubHelp logo

hlamba28 / one-shot-learning-with-siamese-networks Goto Github PK

View Code? Open in Web Editor NEW
137.0 137.0 111.0 78 KB

Implementation of One Shot Learning using Convolutional Siamese Networks on Omniglot Dataset

Home Page: https://towardsdatascience.com/one-shot-learning-with-siamese-networks-using-keras-17f34e75bb3d

Jupyter Notebook 100.00%
convolutional-neural-networks keras-tensorflow omniglot one-shot-learning siamese-network

one-shot-learning-with-siamese-networks's People

Contributors

hlamba28 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

one-shot-learning-with-siamese-networks's Issues

weight height reversed?

in the function get_batch:
this line: pairs=[np.zeros((batch_size, h, w,1)) for i in range(2)]
is the order of h and w reversed?
because in the line above it, it was:
n_classes, n_examples, w, h = X.shape

Error creating the model

When I try to exectute:

model = get_siamese_model((105, 105, 1))

I got this error:

Traceback (most recent call last):
  File "main.py", line 158, in <module>
    model = get_siamese_model((105, 105, 1))
  File "main.py", line 123, in get_siamese_model
    kernel_initializer=initialize_weights, kernel_regularizer=l2(2e-4)))
  File "/home/fabio/.local/lib/python3.6/site-packages/tensorflow/python/training/tracking/base.py", line 456, in _method_wrapper
    result = method(self, *args, **kwargs)
  File "/home/fabio/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/sequential.py", line 198, in add
    layer(x)
  File "/home/fabio/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py", line 897, in __call__
    self._maybe_build(inputs)
  File "/home/fabio/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py", line 2416, in _maybe_build
    self.build(input_shapes)  # pylint:disable=not-callable
  File "/home/fabio/.local/lib/python3.6/site-packages/tensorflow/python/keras/layers/convolutional.py", line 163, in build
    dtype=self.dtype)
  File "/home/fabio/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py", line 577, in add_weight
    caching_device=caching_device)
  File "/home/fabio/.local/lib/python3.6/site-packages/tensorflow/python/training/tracking/base.py", line 743, in _add_variable_with_custom_getter
    **kwargs_for_getter)
  File "/home/fabio/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer_utils.py", line 141, in make_variable
    shape=variable_shape if variable_shape else None)
  File "/home/fabio/.local/lib/python3.6/site-packages/tensorflow/python/ops/variables.py", line 259, in __call__
    return cls._variable_v1_call(*args, **kwargs)
  File "/home/fabio/.local/lib/python3.6/site-packages/tensorflow/python/ops/variables.py", line 220, in _variable_v1_call
    shape=shape)
  File "/home/fabio/.local/lib/python3.6/site-packages/tensorflow/python/ops/variables.py", line 198, in <lambda>
    previous_getter = lambda **kwargs: default_variable_creator(None, **kwargs)
  File "/home/fabio/.local/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py", line 2598, in default_variable_creator
    shape=shape)
  File "/home/fabio/.local/lib/python3.6/site-packages/tensorflow/python/ops/variables.py", line 263, in __call__
    return super(VariableMetaclass, cls).__call__(*args, **kwargs)
  File "/home/fabio/.local/lib/python3.6/site-packages/tensorflow/python/ops/resource_variable_ops.py", line 1434, in __init__
    distribute_strategy=distribute_strategy)
  File "/home/fabio/.local/lib/python3.6/site-packages/tensorflow/python/ops/resource_variable_ops.py", line 1567, in _init_from_args
    initial_value() if init_from_fn else initial_value,
  File "/home/fabio/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer_utils.py", line 121, in <lambda>
    init_val = lambda: initializer(shape, dtype=dtype)
TypeError: initialize_weights() got an unexpected keyword argument 'dtype'

Any idea why this happen?

Unable to Load Saved Model

I trained the model on a custom dataset using a custom generator as opposed to loading in-memory and have saved the Keras sequential model using the ModelCheckpoint callback.

However, on loading model as model = load_model('..'), I get the following error:

Traceback (most recent call last):
File "predict.py", line 25, in
model = load_model(os.path.join('models','binary_custom.hdf5'))
File "/home/ubuntu/image_matching/env/lib/python3.6/site-packages/keras/engine/saving.py", line 419, in load_model
model = _deserialize_model(f, custom_objects, compile)
File "/home/ubuntu/image_matching/env/lib/python3.6/site-packages/keras/engine/saving.py", line 225, in _deserialize_model
model = model_from_config(model_config, custom_objects=custom_objects)
File "/home/ubuntu/image_matching/env/lib/python3.6/site-packages/keras/engine/saving.py", line 458, in model_from_config
return deserialize(config, custom_objects=custom_objects)
File "/home/ubuntu/image_matching/env/lib/python3.6/site-packages/keras/layers/init.py", line 55, in deserialize
printable_module_name='layer')
File "/home/ubuntu/image_matching/env/lib/python3.6/site-packages/keras/utils/generic_utils.py", line 145, in deserialize_keras_object
list(custom_objects.items())))
File "/home/ubuntu/image_matching/env/lib/python3.6/site-packages/keras/engine/network.py", line 1022, in from_config
process_layer(layer_data)
File "/home/ubuntu/image_matching/env/lib/python3.6/site-packages/keras/engine/network.py", line 1008, in process_layer
custom_objects=custom_objects)
File "/home/ubuntu/image_matching/env/lib/python3.6/site-packages/keras/layers/init.py", line 55, in deserialize
printable_module_name='layer')
File "/home/ubuntu/image_matching/env/lib/python3.6/site-packages/keras/utils/generic_utils.py", line 145, in deserialize_keras_object
list(custom_objects.items())))
File "/home/ubuntu/image_matching/env/lib/python3.6/site-packages/keras/engine/sequential.py", line 300, in from_config
custom_objects=custom_objects)
File "/home/ubuntu/image_matching/env/lib/python3.6/site-packages/keras/layers/init.py", line 55, in deserialize
printable_module_name='layer')
File "/home/ubuntu/image_matching/env/lib/python3.6/site-packages/keras/utils/generic_utils.py", line 147, in deserialize_keras_object
return cls.from_config(config['config'])
File "/home/ubuntu/image_matching/env/lib/python3.6/site-packages/keras/engine/base_layer.py", line 1109, in from_config
return cls(**config)
File "/home/ubuntu/image_matching/env/lib/python3.6/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "/home/ubuntu/image_matching/env/lib/python3.6/site-packages/keras/layers/convolutional.py", line 490, in init
**kwargs)
File "/home/ubuntu/image_matching/env/lib/python3.6/site-packages/keras/layers/convolutional.py", line 117, in init
self.kernel_initializer = initializers.get(kernel_initializer)
File "/home/ubuntu/image_matching/env/lib/python3.6/site-packages/keras/initializers.py", line 511, in get
return deserialize(config)
File "/home/ubuntu/image_matching/env/lib/python3.6/site-packages/keras/initializers.py", line 503, in deserialize
printable_module_name='initializer')
File "/home/ubuntu/image_matching/env/lib/python3.6/site-packages/keras/utils/generic_utils.py", line 138, in deserialize_keras_object
': ' + class_name)
ValueError: Unknown initializer: initialize_weights

My python version is 3.6.7 with Keras==2.2.4 and tensorflow==1.13.1.

Any suggestions would be higly appreciated.

number of images per character

according this example:
X.shape
(964, 20, 105, 105)

"This means we have 964 characters (or letters or categories) spanning across 30 different alphabets. For each of this character, we have 20 images, and each image is a gray scale image of resolution 105x105. Hence the shape (964, 20, 105, 105)."

Does the number of images per character have to be the same? (20 in this case)

Varying sample size in Siamese network

I have an image dataset where the folder structure is as follows- there are 900 folders (each folder will be a class) and in each folder, we have a varying number of images(each image has been resized to the same pixel intensity).

Now when we construct the 4d matrix to be fed to the deep learning model it should be of the dimension-

(900,A,105,105)

900 because that's the total number of classes
105 x 105 because I have resized each image to that
now A should be the number of images in each folder.
In my case, the number of images in each folder is varying so my question is-

If I keep the original varying number of images in each folder that means in my 4d array A would be the dimension of the largest folder size which means for folders of lesser number of images it will have zero 2d matrix or matrices. So is that technically right or should I make all the folder sizes the same?

TypeError

Hi! While running this part of code:

model = get_siamese_model((105, 105, 1))
model.summary()

I got this error:

TypeError Traceback (most recent call last)
in
----> 1 model = get_siamese_model((105, 105, 1))
2 model.summary()

in get_siamese_model(input_shape)
11 model = Sequential()
12 model.add(Conv2D(64, (10,10), activation='relu', input_shape=input_shape,
---> 13 kernel_initializer=initialize_weights, kernel_regularizer=l2(2e-4)))
14 model.add(MaxPooling2D())
15 model.add(Conv2D(128, (7,7), activation='relu',

~\Anaconda3\lib\site-packages\keras\engine\sequential.py in add(self, layer)
164 # and create the node connecting the current layer
165 # to the input layer we just created.
--> 166 layer(x)
167 set_inputs = True
168 else:

~\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py in symbolic_fn_wrapper(*args, **kwargs)
73 if _SYMBOLIC_SCOPE.value:
74 with get_graph().as_default():
---> 75 return func(*args, **kwargs)
76 else:
77 return func(*args, **kwargs)

~\Anaconda3\lib\site-packages\keras\engine\base_layer.py in call(self, inputs, **kwargs)
461 'You can build it manually via: '
462 'layer.build(batch_input_shape)')
--> 463 self.build(unpack_singleton(input_shapes))
464 self.built = True
465

~\Anaconda3\lib\site-packages\keras\layers\convolutional.py in build(self, input_shape)
139 name='kernel',
140 regularizer=self.kernel_regularizer,
--> 141 constraint=self.kernel_constraint)
142 if self.use_bias:
143 self.bias = self.add_weight(shape=(self.filters,),

~\Anaconda3\lib\site-packages\keras\engine\base_layer.py in add_weight(self, name, shape, dtype, initializer, regularizer, trainable, constraint)
277 if dtype is None:
278 dtype = self.dtype
--> 279 weight = K.variable(initializer(shape, dtype=dtype),
280 dtype=dtype,
281 name=name,

TypeError: initialize_weights() got an unexpected keyword argument 'dtype'

Is it related to my tf version?
My current tf version is 2.0 gpu

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.