oarriaga / STN.keras

Implementation of spatial transformer networks (STNs) in keras 2 with tensorflow as backend.
MIT License
278 stars 75 forks source link

Crashed when adding SpatialTransformer layer #1

Closed hoanghng closed 7 years ago

hoanghng commented 7 years ago

Thanks for your sharing. I tried to run your notebook with Tensorflow 1.0 but I got the error:

TypeError Traceback (most recent call last)

in () 3 model.add(SpatialTransformer(localization_net=locnet, 4 input_shape=input_shape, ----> 5 output_size=(30,30))) 6 7 model.add(Convolution2D(32, 3, 3, border_mode='same')) /home/deepws/anaconda2/lib/python2.7/site-packages/keras/models.pyc in add(self, layer) 297 else: 298 input_dtype = None --> 299 layer.create_input_layer(batch_input_shape, input_dtype) 300 301 if len(layer.inbound_nodes) != 1: /home/deepws/anaconda2/lib/python2.7/site-packages/keras/engine/topology.pyc in create_input_layer(self, batch_input_shape, input_dtype, name) 399 # and create the node connecting the current layer 400 # to the input layer we just created. --> 401 self(x) 402 403 def add_weight(self, shape, initializer, name=None, /home/deepws/anaconda2/lib/python2.7/site-packages/keras/engine/topology.pyc in __call__(self, x, mask) 570 if inbound_layers: 571 # This will call layer.build() if necessary. --> 572 self.add_inbound_node(inbound_layers, node_indices, tensor_indices) 573 # Outputs were already computed when calling self.add_inbound_node. 574 outputs = self.inbound_nodes[-1].output_tensors /home/deepws/anaconda2/lib/python2.7/site-packages/keras/engine/topology.pyc in add_inbound_node(self, inbound_layers, node_indices, tensor_indices) 633 # creating the node automatically updates self.inbound_nodes 634 # as well as outbound_nodes on inbound layers. --> 635 Node.create_node(self, inbound_layers, node_indices, tensor_indices) 636 637 def get_output_shape_for(self, input_shape): /home/deepws/anaconda2/lib/python2.7/site-packages/keras/engine/topology.pyc in create_node(cls, outbound_layer, inbound_layers, node_indices, tensor_indices) 164 165 if len(input_tensors) == 1: --> 166 output_tensors = to_list(outbound_layer.call(input_tensors[0], mask=input_masks[0])) 167 output_masks = to_list(outbound_layer.compute_mask(input_tensors[0], input_masks[0])) 168 # TODO: try to auto-infer shape /home/deepws/workspace/spatial_transformer_networks/src/spatial_transformer.py in call(self, X, mask) 46 def call(self, X, mask=None): 47 affine_transformation = self.locnet.call(X) ---> 48 output = self._transform(affine_transformation, X, self.output_size) 49 return output 50 /home/deepws/workspace/spatial_transformer_networks/src/spatial_transformer.py in _transform(self, affine_transformation, input_shape, output_size) 145 output_height = output_size[0] 146 output_width = output_size[1] --> 147 indices_grid = self._meshgrid(output_height, output_width) 148 indices_grid = tf.expand_dims(indices_grid, 0) 149 indices_grid = tf.reshape(indices_grid, [-1]) # flatten? /home/deepws/workspace/spatial_transformer_networks/src/spatial_transformer.py in _meshgrid(self, height, width) 127 y_coordinates = tf.reshape(y_coordinates, shape=(1, -1)) 128 ones = tf.ones_like(x_coordinates) --> 129 indices_grid = tf.concat(0, [x_coordinates, y_coordinates, ones]) 130 return indices_grid 131 /home/deepws/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/array_ops.pyc in concat(values, axis, name) 1045 ops.convert_to_tensor(axis, 1046 name="concat_dim", -> 1047 dtype=dtypes.int32).get_shape( 1048 ).assert_is_compatible_with(tensor_shape.scalar()) 1049 return identity(values[0], name=scope) /home/deepws/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc in convert_to_tensor(value, dtype, name, preferred_dtype) 649 name=name, 650 preferred_dtype=preferred_dtype, --> 651 as_ref=False) 652 653 /home/deepws/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc in internal_convert_to_tensor(value, dtype, name, as_ref, preferred_dtype) 714 715 if ret is None: --> 716 ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref) 717 718 if ret is NotImplemented: /home/deepws/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/constant_op.pyc in _constant_tensor_conversion_function(v, dtype, name, as_ref) 174 as_ref=False): 175 _ = as_ref --> 176 return constant(v, dtype=dtype, name=name) 177 178 /home/deepws/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/constant_op.pyc in constant(value, dtype, shape, name, verify_shape) 163 tensor_value = attr_value_pb2.AttrValue() 164 tensor_value.tensor.CopyFrom( --> 165 tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape, verify_shape=verify_shape)) 166 dtype_value = attr_value_pb2.AttrValue(type=tensor_value.tensor.dtype) 167 const_tensor = g.create_op( /home/deepws/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/tensor_util.pyc in make_tensor_proto(values, dtype, shape, verify_shape) 365 nparray = np.empty(shape, dtype=np_dt) 366 else: --> 367 _AssertCompatible(values, dtype) 368 nparray = np.array(values, dtype=np_dt) 369 # check to them. /home/deepws/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/tensor_util.pyc in _AssertCompatible(values, dtype) 300 else: 301 raise TypeError("Expected %s, got %s of type '%s' instead." % --> 302 (dtype.name, repr(mismatch), type(mismatch).__name__)) 303 304 TypeError: Expected int32, got list containing Tensors of type '_Message' instead.
oarriaga commented 7 years ago

Hello @hoanghng unfortunately the tensorflow version for which it was tested was 0.12.0. I have come across several blog posts that state that newer tensorflow versions are not backward compatible. However, I will gladly accept any pull requests that puts forward the current version.

hoanghng commented 7 years ago

It works based on the fix #2