nengo / nengo-extras

Extra utilities and add-ons for Nengo
https://www.nengo.ai/nengo-extras
Other
5 stars 8 forks source link

ValueError: Shapes must be equal rank, but are 4 and 0 for 'soft_lif_7/Select_1' #65

Closed assadRasheed closed 6 years ago

assadRasheed commented 6 years ago

i am trying to run CNN_Spiking from nengo on my own dataset which is composed of jpg images. i made neccessary changes in code according to the size of my dataset images and run following code but got the follwoing error.pls suggest me the solution.

from __future__ import print_function

import os
os.environ['THEANO_FLAGS'] = 'device=gpu,floatX=float32'

import nengo
import numpy as np

import keras

from keras.models import Sequential
from keras.layers import (
    Dense, Dropout, Activation, Flatten, Convolution2D, AveragePooling2D)
from keras.layers.noise import GaussianNoise
from keras.utils import np_utils

import nengo
from nengo_extras.keras import (
    load_model_pair, save_model_pair, SequentialNetwork, SoftLIF)
from nengo_extras.gui import image_display_function

img_rows, img_cols = 227, 227

np.random.seed(5)

#X_train = X_train.reshape(X_train.shape[0], 1, img_rows, img_cols)
#X_test = X_test.reshape(X_test.shape[0], 1, img_rows, img_cols)
#(X_train, y_train), (X_test, y_test) = mnist.load_data()
#X_train = X_train.reshape(X_train.shape[0], 1, img_rows, img_cols)
#X_test = X_test.reshape(X_test.shape[0], 1, img_rows, img_cols)

#X_train = X_train.reshape(X_train.shape[0], 1, img_rows, img_cols)
#X_test = X_test.reshape(X_test.shape[0], 1, img_rows, img_cols)
(X_train, y_train), (X_test, y_test), label_names = ((X_train, iy_train),(X_test, y_test),class_names)

X_train = X_train.reshape(X_train.shape[0], 3, img_rows, img_cols)
#X_train = X_train.reshape(-1, 227, 227,3).astype('float32')
X_test = X_test.reshape(X_test.shape[0], 3, img_rows, img_cols)
X_train = X_train.astype('float32')/255 - 1
X_test = X_test.astype('float32')/255 - 1

nb_classes = len(label_names)
X_train = X_train[:, :, 16:-16, 16:-16]
X_test = X_test[:, :, 16:-16, 16:-16]

# --- Train model
nb_epoch = 25

    # number of convolutional filters to use
nb_filters = 32
    # size of pooling area for max pooling
nb_pool = 2
    # convolution kernel size
nb_conv = 3

    # convert class vectors to binary class matrices
Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)

kmodel = Sequential()
softlif_params = dict(
    sigma=0.002, amplitude=0.063, tau_rc=0.022, tau_ref=0.002)
kmodel.add(GaussianNoise(0.1, input_shape=(3, img_rows, img_cols)))
kmodel.add(Convolution2D(nb_filters, nb_conv, nb_conv, border_mode='valid'))
kmodel.add(SoftLIF(**softlif_params))
kmodel.add(Convolution2D(nb_filters, nb_conv, nb_conv))
kmodel.add(SoftLIF(**softlif_params))
kmodel.add(AveragePooling2D(pool_size=(nb_pool, nb_pool)))
kmodel.add(Dropout(0.25))

kmodel.add(Flatten())
kmodel.add(Dense(227))
kmodel.add(SoftLIF(**softlif_params))
kmodel.add(Dropout(0.5))
kmodel.add(Dense(nb_classes))
kmodel.add(Activation('softmax'))

kmodel.compile(loss='categorical_crossentropy',
                   optimizer='adadelta',
                   metrics=['accuracy'])

kmodel.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch,
               verbose=1, validation_data=(X_test, Y_test))
score = kmodel.evaluate(X_test, Y_test, verbose=0)
print('Test score:', score[0])
print('Test accuracy:', score[1])

save_model_pair(kmodel, filename, overwrite=True)

the following error appears C:\ProgramData\Anaconda34\lib\site-packages\ipykernel_launcher.py:71: UserWarning: Update your Conv2D call to the Keras 2 API: Conv2D(32, (3, 3), padding="valid")

InvalidArgumentError Traceback (most recent call last) C:\ProgramData\Anaconda34\lib\site-packages\tensorflow\python\framework\common_shapes.py in _call_cpp_shape_fn_impl(op, input_tensors_needed, input_tensors_as_shapes_needed, require_shape_fn) 685 graph_def_version, node_def_str, input_shapes, input_tensors, --> 686 input_tensors_as_shapes, status) 687 except errors.InvalidArgumentError as err:

C:\ProgramData\Anaconda34\lib\site-packages\tensorflow\python\framework\errors_impl.py in exit(self, type_arg, value_arg, traceback_arg) 472 compat.as_text(c_api.TF_Message(self.status.status)), --> 473 c_api.TF_GetCode(self.status.status)) 474 # Delete the underlying status object from memory otherwise it stays alive

InvalidArgumentError: Shapes must be equal rank, but are 4 and 0 for 'soft_lif_7/Select_1' (op: 'Select') with input shapes: [?,?,?,?], [?,32,225,1], [].

During handling of the above exception, another exception occurred:

ValueError Traceback (most recent call last)

in () 70 #kmodel.add(GaussianNoise(0.1, input_shape=(3,3, img_rows, img_cols))) 71 kmodel.add(Convolution2D(nb_filters, nb_conv, nb_conv, border_mode='valid')) ---> 72 kmodel.add(SoftLIF(**softlif_params)) 73 kmodel.add(Convolution2D(nb_filters, nb_conv, nb_conv)) 74 kmodel.add(SoftLIF(**softlif_params)) C:\ProgramData\Anaconda34\lib\site-packages\keras\models.py in add(self, layer) 490 output_shapes=[self.outputs[0]._keras_shape]) 491 else: --> 492 output_tensor = layer(self.outputs[0]) 493 if isinstance(output_tensor, list): 494 raise TypeError('All layers in a Sequential model ' C:\ProgramData\Anaconda34\lib\site-packages\keras\engine\topology.py in __call__(self, inputs, **kwargs) 615 616 # Actually call the layer, collecting output(s), mask(s), and shape(s). --> 617 output = self.call(inputs, **kwargs) 618 output_mask = self.compute_mask(inputs, previous_mask) 619 C:\ProgramData\Anaconda34\lib\site-packages\nengo_extras\keras.py in call(self, x, mask) 23 j = K.softplus(x / self.sigma) * self.sigma 24 r = self.amplitude / (self.tau_ref + self.tau_rc*K.log(1 + 1/j)) ---> 25 return K.switch(j > 0, r, 0) 26 27 def get_config(self): C:\ProgramData\Anaconda34\lib\site-packages\keras\backend\tensorflow_backend.py in switch(condition, then_expression, else_expression) 2833 tile_shape = tf.where(shape_diff > 0, expr_shape, tf.ones_like(expr_shape)) 2834 condition = tf.tile(condition, tile_shape) -> 2835 x = tf.where(condition, then_expression, else_expression) 2836 return x 2837 C:\ProgramData\Anaconda34\lib\site-packages\tensorflow\python\ops\array_ops.py in where(condition, x, y, name) 2538 return gen_array_ops.where(condition=condition, name=name) 2539 elif x is not None and y is not None: -> 2540 return gen_math_ops._select(condition=condition, x=x, y=y, name=name) 2541 else: 2542 raise ValueError("x and y must both be non-None or both be None.") C:\ProgramData\Anaconda34\lib\site-packages\tensorflow\python\ops\gen_math_ops.py in _select(condition, x, y, name) 4526 if _ctx.in_graph_mode(): 4527 _, _, _op = _op_def_lib._apply_op_helper( -> 4528 "Select", condition=condition, t=x, e=y, name=name) 4529 _result = _op.outputs[:] 4530 _inputs_flat = _op.inputs C:\ProgramData\Anaconda34\lib\site-packages\tensorflow\python\framework\op_def_library.py in _apply_op_helper(self, op_type_name, name, **keywords) 785 op = g.create_op(op_type_name, inputs, output_types, name=scope, 786 input_types=input_types, attrs=attr_protos, --> 787 op_def=op_def) 788 return output_structure, op_def.is_stateful, op 789 C:\ProgramData\Anaconda34\lib\site-packages\tensorflow\python\framework\ops.py in create_op(self, op_type, inputs, dtypes, input_types, name, attrs, op_def, compute_shapes, compute_device) 3160 op_def=op_def) 3161 self._create_op_helper(ret, compute_shapes=compute_shapes, -> 3162 compute_device=compute_device) 3163 return ret 3164 C:\ProgramData\Anaconda34\lib\site-packages\tensorflow\python\framework\ops.py in _create_op_helper(self, op, compute_shapes, compute_device) 3206 # compute_shapes argument. 3207 if op._c_op or compute_shapes: # pylint: disable=protected-access -> 3208 set_shapes_for_outputs(op) 3209 # TODO(b/XXXX): move to Operation.__init__ once _USE_C_API flag is removed. 3210 self._add_op(op) C:\ProgramData\Anaconda34\lib\site-packages\tensorflow\python\framework\ops.py in set_shapes_for_outputs(op) 2425 return _set_shapes_for_outputs_c_api(op) 2426 else: -> 2427 return _set_shapes_for_outputs(op) 2428 2429 C:\ProgramData\Anaconda34\lib\site-packages\tensorflow\python\framework\ops.py in _set_shapes_for_outputs(op) 2398 shape_func = _call_cpp_shape_fn_and_require_op 2399 -> 2400 shapes = shape_func(op) 2401 if shapes is None: 2402 raise RuntimeError( C:\ProgramData\Anaconda34\lib\site-packages\tensorflow\python\framework\ops.py in call_with_requiring(op) 2328 2329 def call_with_requiring(op): -> 2330 return call_cpp_shape_fn(op, require_shape_fn=True) 2331 2332 _call_cpp_shape_fn_and_require_op = call_with_requiring C:\ProgramData\Anaconda34\lib\site-packages\tensorflow\python\framework\common_shapes.py in call_cpp_shape_fn(op, require_shape_fn) 625 res = _call_cpp_shape_fn_impl(op, input_tensors_needed, 626 input_tensors_as_shapes_needed, --> 627 require_shape_fn) 628 if not isinstance(res, dict): 629 # Handles the case where _call_cpp_shape_fn_impl calls unknown_shape(op). C:\ProgramData\Anaconda34\lib\site-packages\tensorflow\python\framework\common_shapes.py in _call_cpp_shape_fn_impl(op, input_tensors_needed, input_tensors_as_shapes_needed, require_shape_fn) 689 missing_shape_fn = True 690 else: --> 691 raise ValueError(err.message) 692 693 if missing_shape_fn: ValueError: Shapes must be equal rank, but are 4 and 0 for 'soft_lif_7/Select_1' (op: 'Select') with input shapes: [?,?,?,?], [?,32,225,1], [].