sorenbouma / keras-oneshot Goto Github PK
View Code? Open in Web Editor NEWkoch et al, Siamese Networks for one-shot learning, (mostly) reimplimented in keras
License: MIT License
koch et al, Siamese Networks for one-shot learning, (mostly) reimplimented in keras
License: MIT License
Can you please code up a quick example of this with CIFAR10?
in SiameseNet.ipynb def test_nn_accuracy(N_ways,n_trials,loader):
pairs,targets = der.make_oneshot_task(N_ways,"val")
shoud be loader.make_oneshot_task
??
TypeError Traceback (most recent call last)
in
29 convnet = Sequential()
30 convnet.add(Conv2D(64,(10,10),activation='relu',input_shape=input_shape,
---> 31 kernel_initializer=W_init,kernel_regularizer=l2(2e-4)))
32 convnet.add(MaxPooling2D())
33 convnet.add(Conv2D(128,(7,7),activation='relu',
~/anaconda3/lib/python3.7/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/python3.7/site-packages/keras/engine/base_layer.py in call(self, inputs, **kwargs)
423 'You can build it manually via: '
424 'layer.build(batch_input_shape)
')
--> 425 self.build(unpack_singleton(input_shapes))
426 self.built = True
427
~/anaconda3/lib/python3.7/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/python3.7/site-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
89 warnings.warn('Update your ' + object_name + '
call to the ' +
90 'Keras 2 API: ' + signature, stacklevel=2)
---> 91 return func(*args, **kwargs)
92 wrapper._original_function = func
93 return wrapper
~/anaconda3/lib/python3.7/site-packages/keras/engine/base_layer.py in add_weight(self, name, shape, dtype, initializer, regularizer, trainable, constraint)
241 if dtype is None:
242 dtype = self.dtype
--> 243 weight = K.variable(initializer(shape, dtype=dtype),
244 dtype=dtype,
245 name=name,
TypeError: W_init() got an unexpected keyword argument 'dtype'
How can I solve this error?
I think the L2_distances in the function nearest_neighbour_correct should be computed as follows:
dis = pairs[0][i] - pairs[1][i]
dis = dis.reshape(dis.size)
L2_distances[i] = np.sqrt(dis.dot(dis))
instead of
L2_distances[i] = np.sum(np.sqrt(pairs[0][i]**2 - pairs[1][i]**2))
Hello!
How can I set the 'momentum' and 'decay lr with epoch' like orginal paper?
from keras.models import load_model model = load_model('weights')
ValueError: Unknown initializer: W_init
Hello, I want to reproduce the original experiment on mnist.
But there is no detailed network structure in the paper, because the input size of mnist data is 35 * 35, so do you know the meaning of "reduce version" in the paper?
thank you!
Another problem:
for i in range(0,1000):
print (i)
(inputs,targets)=loader.get_batch(batch_size)
loss=siamese_net.train_on_batch(inputs,targets)
if i % evaluate_every == 0:
val_acc = loader.test_oneshot(siamese_net,N_way,n_val,verbose=True)
if val_acc >= best: #<-- best is used before it is initialized
print("saving")
siamese_net.save('./weights')
best=val_acc
Did you really mean to disable training in the code? The following loop never executes because the range is empty.
for i in range(1,0): #<-- this is an empty range
(inputs,targets)=loader.get_batch(batch_size)
loss=siamese_net.train_on_batch(inputs,targets)
if i % evaluate_every == 0:
val_acc = loader.test_oneshot(siamese_net,N_way,n_val,verbose=True)
if val_acc >= best:
print("saving")
siamese_net.save('/home/soren/keras-oneshot/weights')
best=val_acc
I have no issue working on load_data.py under omniglot dataset, and was able to run SiameseNet.ipynb.
However, Its only when I switch to my own dataset, I noticed you used np.stack, which errors out when my number of images in each folder is different from the next folder. Is this a requirement for Siamese to work properly? How can I get pass this?
File "load_data.py", line 64, in <module>
X,y,c=loadimgs(train_folder)
File "load_data.py", line 61, in loadimgs
X = np.stack(X)
File "C:\Users\Noel Tam\AppData\Local\conda\conda\envs\py36\lib\site-packages\numpy\core\shape_base.py", line 353, in stack
raise ValueError('all input arrays must have the same shape')
ValueError: all input arrays must have the same shape
When I was training data, I changed the value of evaluate_every to 100, and the original value was 500. However, there are the following errors. Can you help me solve this problem again? Thank you very much for your help.
UnboundLocalError Traceback (most recent call last)
in ()
11
12 if i % evaluate_every == 0:
---> 13 val_acc = loader.test_oneshot(siamese_net,N_way,n_val,verbose=True)
14 print('val_acc的值为:' + val_acc)
15 if val_acc >= best:
in test_oneshot(self, model, N, k, s, verbose)
67 print("Evaluating model on {} unique {} way one-shot learning tasks ...".format(k,N))
68 for i in range(k):
---> 69 inputs, targets = self.make_oneshot_task(N,s)
70 probs = model.predict(inputs)
71 if np.argmax(probs) == np.argmax(targets):
in make_oneshot_task(self, N, s, language)
51 ex1, ex2 = rng.choice(n_examples,replace=False,size=(2,))
52 test_image = np.asarray([X[true_category,ex1,:,:]]*N).reshape(N,self.w,self.h,1)
---> 53 support_set = X[categories,indices,:,:]
54 support_set[0,:,:] = X[true_category,ex2]
55 support_set = support_set.reshape(N,self.w,self.h,1)
UnboundLocalError: local variable 'indices' referenced before assignment
Thanks for your implementation!!
When I load the saved model, it has always been wrong. It always says:Unknown initializer: W_init.....
Could you please help me to solve it? Thanks very much!
File "run.py", line 73, in
siamese_net = load_model('siamese.h5')
File "/home/qian/.local/lib/python2.7/site-packages/keras/models.py", line 240, in load_model
model = model_from_config(model_config, custom_objects=custom_objects)
File "/home/qian/.local/lib/python2.7/site-packages/keras/models.py", line 301, in model_from_config
return layer_module.deserialize(config, custom_objects=custom_objects)
File "/home/qian/.local/lib/python2.7/site-packages/keras/layers/init.py", line 46, in deserialize
printable_module_name='layer')
File "/home/qian/.local/lib/python2.7/site-packages/keras/utils/generic_utils.py", line 140, in deserialize_keras_object
list(custom_objects.items())))
File "/home/qian/.local/lib/python2.7/site-packages/keras/engine/topology.py", line 2400, in from_config
process_layer(layer_data)
File "/home/qian/.local/lib/python2.7/site-packages/keras/engine/topology.py", line 2369, in process_layer
custom_objects=custom_objects)
File "/home/qian/.local/lib/python2.7/site-packages/keras/layers/init.py", line 46, in deserialize
printable_module_name='layer')
File "/home/qian/.local/lib/python2.7/site-packages/keras/utils/generic_utils.py", line 140, in deserialize_keras_object
list(custom_objects.items())))
File "/home/qian/.local/lib/python2.7/site-packages/keras/models.py", line 1199, in from_config
layer = layer_module.deserialize(conf, custom_objects=custom_objects)
File "/home/qian/.local/lib/python2.7/site-packages/keras/layers/init.py", line 46, in deserialize
printable_module_name='layer')
File "/home/qian/.local/lib/python2.7/site-packages/keras/utils/generic_utils.py", line 141, in deserialize_keras_object
return cls.from_config(config['config'])
File "/home/qian/.local/lib/python2.7/site-packages/keras/engine/topology.py", line 1231, in from_config
return cls(**config)
File "/home/qian/.local/lib/python2.7/site-packages/keras/legacy/interfaces.py", line 88, in wrapper
return func(*args, **kwargs)
File "/home/qian/.local/lib/python2.7/site-packages/keras/layers/convolutional.py", line 455, in init
**kwargs)
File "/home/qian/.local/lib/python2.7/site-packages/keras/layers/convolutional.py", line 110, in init
self.kernel_initializer = initializers.get(kernel_initializer)
File "/home/qian/.local/lib/python2.7/site-packages/keras/initializers.py", line 466, in get
return deserialize(config)
File "/home/qian/.local/lib/python2.7/site-packages/keras/initializers.py", line 458, in deserialize
printable_module_name='initializer')
File "/home/qian/.local/lib/python2.7/site-packages/keras/utils/generic_utils.py", line 133, in deserialize_keras_object
': ' + class_name)
ValueError:Unknown initializer: W_init
Please see the last line in the code below. starts_idx
and end_idx
are not even used. Shouldn't the last line be commented out?
if language is not None:
low,high = self.categories[s][language]
try:
categories = rng.choice(range(low,high),size=(N,),replace=False)
except ValueError:
print("this language doesn't have enough characters for that one-shot task")
indices = rng.randint(0,self.n_examples,size=(N,))
else:
categories = rng.choice(range(n_classes),size=(N,),replace=False)
start_idx, end_idx =self.categories[s][language] # <--- language is None here and causes an error
Hi,
I follow the post and installed the stuff that is needed for this example, however when I run it at jupyter it has the following error:
ImportError Traceback (most recent call last)
in ()
----> 1 from keras.layers import Input, Conv2D, Lambda, merge, Dense, Flatten,MaxPooling2D
2 from keras.models import Model, Sequential
3 from keras.regularizers import l2
4 from keras import backend as K
5 from keras.optimizers import SGD,Adam
/usr/local/lib/python2.7/site-packages/keras/init.py in ()
1 from future import absolute_import
2
----> 3 from . import utils
4 from . import activations
5 from . import applications
/usr/local/lib/python2.7/site-packages/keras/utils/init.py in ()
4 from . import data_utils
5 from . import io_utils
----> 6 from . import conv_utils
7
8 # Globally-importable utils.
/usr/local/lib/python2.7/site-packages/keras/utils/conv_utils.py in ()
7 from six.moves import range
8 import numpy as np
----> 9 from .. import backend as K
10
11
/usr/local/lib/python2.7/site-packages/keras/backend/init.py in ()
82 elif _BACKEND == 'tensorflow':
83 sys.stderr.write('Using TensorFlow backend.\n')
---> 84 from .tensorflow_backend import *
85 else:
86 raise ValueError('Unknown backend: ' + str(_BACKEND))
/usr/local/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py in ()
3 from future import print_function
4
----> 5 import tensorflow as tf
6 from tensorflow.python.training import moving_averages
7 from tensorflow.python.ops import tensor_array_ops
/usr/local/lib/python2.7/site-packages/tensorflow/init.py in ()
21 from future import print_function
22
---> 23 from tensorflow.python import *
24
25
/usr/local/lib/python2.7/site-packages/tensorflow/python/init.py in ()
47 _default_dlopen_flags = sys.getdlopenflags()
48 sys.setdlopenflags(_default_dlopen_flags | ctypes.RTLD_GLOBAL)
---> 49 from tensorflow.python import pywrap_tensorflow
50 sys.setdlopenflags(_default_dlopen_flags)
51
/usr/local/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py in ()
26 fp.close()
27 return _mod
---> 28 _pywrap_tensorflow = swig_import_helper()
29 del swig_import_helper
30 else:
/usr/local/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py in swig_import_helper()
22 if fp is not None:
23 try:
---> 24 _mod = imp.load_module('_pywrap_tensorflow', fp, pathname, description)
25 finally:
26 fp.close()
ImportError: /usr/local/lib/python2.7/site-packages/tensorflow/python/_pywrap_tensorflow.so: undefined symbol: PyUnicodeUCS4_AsUTF8String
can someone do enlighten me? I just started learning using SiameseNet and jupyter so am not sure whats the error and how to slove it.
Thanks
Hi,
first of all thanks for the implementation, very useful to understand the paper and compare with own implementation !
Here is a small bug with make_oneshot_task
:
if language is not None:
low, high = self.categories[s][language]
if N > high - low:
raise ValueError("This language ({}) has less than {} letters".format(language, N))
categories = rng.choice(range(low,high),size=(N,),replace=False)
indices = rng.randint(0,self.n_examples,size=(N,))
else:#if no language specified just pick a bunch of random letters
categories = rng.choice(range(n_classes),size=(N,),replace=False)
array of indices is not defined in the else case.
Probably, this fixes the problem
else:#if no language specified just pick a bunch of random letters
categories = rng.choice(range(n_classes),size=(N,),replace=False)
indices = rng.randint(0,self.n_examples,size=(N,))
HTH
Hi,
first of all thanks for the implementation, very useful to understand the paper and compare with own implementation !
I have the following error in the training data. Can you help me to solve it? Thanks!
FailedPreconditionError: Attempting to use uninitialized value conv2d_1/kernel
[[Node: conv2d_1/kernel/read = IdentityT=DT_FLOAT, _class=["loc:@conv2d_1/kernel"], _device="/job:localhost/replica:0/task:0/cpu:0"]]
Caused by op 'conv2d_1/kernel/read', defined at:
File "C:\ProgramData\Anaconda3\lib\runpy.py", line 193, in _run_module_as_main
"main", mod_spec)
File "C:\ProgramData\Anaconda3\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\ProgramData\Anaconda3\lib\site-packages\ipykernel_launcher.py", line 16, in
app.launch_new_instance()
File "C:\ProgramData\Anaconda3\lib\site-packages\traitlets\config\application.py", line 658, in launch_instance
app.start()
File "C:\ProgramData\Anaconda3\lib\site-packages\ipykernel\kernelapp.py", line 477, in start
ioloop.IOLoop.instance().start()
File "C:\ProgramData\Anaconda3\lib\site-packages\zmq\eventloop\ioloop.py", line 177, in start
super(ZMQIOLoop, self).start()
File "C:\ProgramData\Anaconda3\lib\site-packages\tornado\ioloop.py", line 888, in start
handler_func(fd_obj, events)
File "C:\ProgramData\Anaconda3\lib\site-packages\tornado\stack_context.py", line 277, in null_wrapper
return fn(*args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\zmq\eventloop\zmqstream.py", line 440, in _handle_events
self._handle_recv()
File "C:\ProgramData\Anaconda3\lib\site-packages\zmq\eventloop\zmqstream.py", line 472, in _handle_recv
self._run_callback(callback, msg)
File "C:\ProgramData\Anaconda3\lib\site-packages\zmq\eventloop\zmqstream.py", line 414, in _run_callback
callback(*args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\tornado\stack_context.py", line 277, in null_wrapper
return fn(*args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 283, in dispatcher
return self.dispatch_shell(stream, msg)
File "C:\ProgramData\Anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 235, in dispatch_shell
handler(stream, idents, msg)
File "C:\ProgramData\Anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 399, in execute_request
user_expressions, allow_stdin)
File "C:\ProgramData\Anaconda3\lib\site-packages\ipykernel\ipkernel.py", line 196, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "C:\ProgramData\Anaconda3\lib\site-packages\ipykernel\zmqshell.py", line 533, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2717, in run_cell
interactivity=interactivity, compiler=compiler, result=result)
File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2821, in run_ast_nodes
if self.run_code(code, result):
File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2881, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "", line 29, in
convnet.add(Conv2D(64,(10,10),activation='relu',input_shape=input_shape,kernel_initializer=W_init,kernel_regularizer=l2(2e-4)))
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\models.py", line 442, in add
layer(x)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\topology.py", line 575, in call
self.build(input_shapes[0])
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\layers\convolutional.py", line 134, in build
constraint=self.kernel_constraint)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\legacy\interfaces.py", line 87, in wrapper
return func(*args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\topology.py", line 399, in add_weight
constraint=constraint)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py", line 316, in variable
v = tf.Variable(value, dtype=_convert_string_dtype(dtype), name=name)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\ops\variables.py", line 200, in init
expected_shape=expected_shape)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\ops\variables.py", line 319, in _init_from_args
self._snapshot = array_ops.identity(self._variable, name="read")
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 1303, in identity
result = _op_def_lib.apply_op("Identity", input=input, name=name)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 767, in apply_op
op_def=op_def)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 2506, in create_op
original_op=self._default_original_op, op_def=op_def)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1269, in init
self._traceback = _extract_stack()
FailedPreconditionError (see above for traceback): Attempting to use uninitialized value conv2d_1/kernel
[[Node: conv2d_1/kernel/read = IdentityT=DT_FLOAT, _class=["loc:@conv2d_1/kernel"], _device="/job:localhost/replica:0/task:0/cpu:0"]]
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.