I am taking in a batch of images, and every image I am then splitting into patches using tf.image.extract_patches. I then want to pass those patches through a model to get embeddings/feats. The problem is that I get this error:
File "/home/fingerprint_firstpart.py", line 152, in <module>
main(args)
File "/home/fingerprint_firstpart.py", line 117, in main
patches_embs=get_patches_and_embs(image,fprinter)
File "/home/fingerprint_firstpart.py", line 55, in get_patches_and_embs
feat = fprinter(patches)
File "/home/fingerprinter.py", line 165, in __call__
x = x.to(self.device)
File "/home/kar/anaconda3/envs/styleanalysis/lib/python3.9/site-packages/tensorflow/python/framework/ops.py", line 401, in __getattr__
self.__getattribute__(name)
AttributeError: 'tensorflow.python.framework.ops.EagerTensor' object has no attribute 'to'
Here's the code in question:
def get_patches_and_embs(images, fprinter):
patches = tf.image.extract_patches(
images=images,
sizes=[1,384, 384,1],
strides= [1, 384, 384,1],
rates=[1, 1, 1, 1],
padding="VALID",
)
patches = tf.reshape(patches, [-1, 384, 384, 3])
patches = tf.transpose(patches, perm=[0, 3, 1, 2]) #at this point this is a tensor of size batch x 3 x 384 x 384, as the model needs as input
feat = fprinter(patches)
return feat
Now, if I take the patches tensorflow tensor and convert it to numpy, and then to pytorch the prgram works just fine. However, I'd like to avoid that if at all possible, so is there any way to do .to() on a tensorflow tensor?
Related
I have several RaggedTensors that I want to concatenate; I am using Keras. Vanilla Tensorflow will happily concatenate them, so I tried the code:
card_feature = layers.concatenate([ragged1, ragged2, ragged3])
but it gave the error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/timeroot/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py", line 925, in __call__
return self._functional_construction_call(inputs, args, kwargs,
File "/home/timeroot/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py", line 1084, in _functional_construction_call
base_layer_utils.create_keras_history(inputs)
File "/home/timeroot/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer_utils.py", line 191, in create_keras_history
_, created_layers = _create_keras_history_helper(tensors, set(), [])
File "/home/timeroot/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer_utils.py", line 222, in _create_keras_history_helper
raise ValueError('Tensorflow ops that generate ragged or sparse tensor '
ValueError: Tensorflow ops that generate ragged or sparse tensor outputs are currently not supported by Keras automatic op wrapping. Please wrap these ops in a Lambda layer:
```
weights_mult = lambda x: tf.sparse.sparse_dense_matmul(x, weights)
output = tf.keras.layers.Lambda(weights_mult)(input)
```
so then I tried:
concat_lambda = lambda xs: tf.concat(xs, axis=2)
card_feature = layers.Lambda(concat_lambda)([ragged1, ragged2, ragged3])
but it gave the exact same error, even though I had wrapped it. Is this a bug / is there a workaround?
Code to concatenate 3 Ragged Tensors is shown below:
import tensorflow as tf
print(tf.__version__)
Ragged_Tensor1 = tf.ragged.constant([[3, 1, 4, 1], [], [5, 9, 2], [6], []])
Ragged_Tensor2 = tf.ragged.constant([[5, 3]])
Ragged_Tensor3 = tf.ragged.constant([[6,7,8], [9,10]])
print(tf.concat([Ragged_Tensor1, Ragged_Tensor2, Ragged_Tensor3], axis=0))
Output is shown below:
2.3.0
<tf.RaggedTensor [[3, 1, 4, 1], [], [5, 9, 2], [6], [], [5, 3], [6, 7, 8], [9, 10]]>
But it looks like you are trying to concatenate Ragged Tensor Operations. Please share your complete code so that we can try to help you.
I'm trying to implement the following custom loss function from this SO post; however, I've had to make some minor changes to suit my model. For some context, I'm using multi labels with 5 classes (below is an example of how they're encoded).
0 => [1, 0, 0, 0, 0]
1 => [1, 1, 0, 0, 0]
2 => [1, 1, 1, 0, 0]
3 => [1, 1, 1, 1, 0]
4 => [1, 1, 1, 1, 1]
My custom loss function
def _cohen_kappa(y_true, y_pred, num_classes=5, weights=None, metrics_collections=None, updates_collections=None, name=None):
kappa, update_op = tf.contrib.metrics.cohen_kappa(y_true, y_pred, num_classes, weights, metrics_collections, updates_collections, name)
kappa = K.cast(kappa, 'float32')
K.get_session().run(tf.local_variables_initializer())
with tf.control_dependencies([update_op]):
kappa = tf.identity(kappa)
return kappa
def cohen_kappa_loss(num_classes=5, weights=None, metrics_collections=None, updates_collections=None, name=None):
def cohen_kappa(y_true, y_pred):
y_true = K.cast(y_true, 'int32')
y_pred = K.cast(y_pred + 0.5, 'int32')
y_true = tf.subtract(K.sum(y_true, axis=1), tf.constant(1))
y_pred = tf.subtract(K.sum(y_pred, axis=1), tf.constant(1))
return -_cohen_kappa(y_true, y_pred, num_classes, weights, metrics_collections, updates_collections, name)
return cohen_kappa
This is how I'm attempting to use my loss function:
model_cohen_kappa = cohen_kappa_loss(num_classes=5)
model.compile(loss=model_cohen_kappa,
optimizer=optimizers.SGD(lr=0.0001, momentum=0.9),
metrics=['accuracy'])
Unfortunately, I get the following error, which is confusing since my loss function doesn't contain K.argmax, K.round, K.eval., which are mentioned in the error message as operations that are non-differentiable. Is there another non-differentiable operation in my custom loss function that I'm not noticing that is giving me this error?
Traceback (most recent call last):
File "small_test.py", line 106, in <module>
main()
File "small_test.py", line 101, in main
max_queue_size=2
File "C:\Users\Anaconda3\envs\tensor\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "C:\Users\Anaconda3\envs\tensor\lib\site-packages\keras\engine\training.py", line 1418, in fit_generator
initial_epoch=initial_epoch)
File "C:\Users\Anaconda3\envs\tensor\lib\site-packages\keras\engine\training_generator.py", line 40, in fit_generator
model._make_train_function()
File "C:\Users\Anaconda3\envs\tensor\lib\site-packages\keras\engine\training.py", line 509, in _make_train_function
loss=self.total_loss)
File "C:\Users\Anaconda3\envs\tensor\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "C:\Users\Anaconda3\envs\tensor\lib\site-packages\keras\optimizers.py", line 184, in get_updates
grads = self.get_gradients(loss, params)
File "C:\Users\Anaconda3\envs\tensor\lib\site-packages\keras\optimizers.py", line 91, in get_gradients
raise ValueError('An operation has `None` for gradient. '
ValueError: An operation has `None` for gradient. Please make sure that all of your ops have a gradient defined (i.e. are differentiable). Common ops without gradient: K.argmax, K.round, K.eval.
While I suspect K.cast is non-differentiable, removing the below snippet from my loss function results in the following error:
kappa = K.cast(kappa, 'float32')
Error
Traceback (most recent call last):
File "small_test.py", line 106, in <module>
main()
File "small_test.py", line 91, in main
metrics=['accuracy'])
File "C:\Users\Anaconda3\envs\tensor\lib\site-packages\keras\engine\training.py", line 342, in compile
sample_weight, mask)
File "C:\Users\Anaconda3\envs\tensor\lib\site-packages\keras\engine\training_utils.py", line 421, in weighted
score_array *= weights
File "C:\Users\Anaconda3\envs\tensor\lib\site-packages\tensorflow\python\ops\math_ops.py", line 884, in binary_op_wrapper
return func(x, y, name=name)
File "C:\Users\Anaconda3\envs\tensor\lib\site-packages\tensorflow\python\ops\math_ops.py", line 1180, in _mul_dispatch
return gen_math_ops.mul(x, y, name=name)
File "C:\Users\Anaconda3\envs\tensor\lib\site-packages\tensorflow\python\ops\gen_math_ops.py", line 6879, in mul
"Mul", x=x, y=y, name=name)
File "C:\Users\Anaconda3\envs\tensor\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 563, in _apply_op_helper
inferred_from[input_arg.type_attr]))
TypeError: Input 'y' of 'Mul' Op has type float32 that does not match type float64 of argument 'x'.
I am implementing OCR with RNN on top of CNN features. And I can not understand mechanics of tf.space_to_batch_nd() and tf.batch_to_space_nd().
I need to apply fully connected layer on each time (1-st dim) slice of input layer to reduce tensor rank from 4 to 3
I have working implementation of this via tf.reshape().
Input tensor shape is [1, 84, 7, 128], output should be [1, 84, 128]
My current implementation is:
with tf.variable_scope('dim_redux') as scope:
conv_out_shape = tf.shape(net)
print("Conv out:", str(net))
w_fc1 = weight_variable([7 * 128, 128])
b_fc1 = bias_variable([128])
conv_layer_flat = tf.reshape(net, [-1, 7 * 127])
features = tf.matmul(conv_layer_flat, w_fc1) + b_fc1
features = lrelu(h_bn3)
features = tf.reshape(features, [batch_size, int(84), CONV_FC_OUTPUT])
And with tf.space_to_batch_nd() and tf.batch_to_space_nd():
with tf.variable_scope('dim_redux') as scope:
net = tf.space_to_batch_nd(net, block_shape=[84, 1], paddings=[[0, 0], [0, 0]])
print(net)
net = tf.contrib.layers.flatten(net)
net = tf.contrib.layers.fully_connected(net, CONV_FC_OUTPUT, biases_initializer=tf.zeros_initializer())
net = lrelu(net)
print(net)
net = tf.batch_to_space_nd(net, block_shape=[84, 128], crops=[[0, 0], [0, 0]])
features = net
It seems that block shapes should be [1, 7], but only with this values [84, 1] tf.space_to_batch_nd() return tensor with correct shapes [84, 1, 7, 128].
With current params i hame following error:
File "/Users/akislinskiy/tag_price_ocr/ocr.py", line 336, in convolutional_layers
net = tf.batch_to_space_nd(net, block_shape=[84, 128], crops=[[0, 0], [0, 0]])
File "/usr/local/lib/python3.6/site-packages/tensorflow/python/ops/gen_array_ops.py", line 318, in batch_to_space_nd
name=name)
File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op
op_def=op_def)
File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 2632, in create_op
set_shapes_for_outputs(ret)
File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1911, in set_shapes_for_outputs
shapes = shape_func(op)
File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1861, in call_with_requiring
return call_cpp_shape_fn(op, require_shape_fn=True)
File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/common_shapes.py", line 595, in call_cpp_shape_fn
require_shape_fn)
File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/common_shapes.py", line 659, in _call_cpp_shape_fn_impl
raise ValueError(err.message)
ValueError: Shape must be at least rank 3 but is rank 2 for 'convolutions/dim_redux/BatchToSpaceND' (op: 'BatchToSpaceND') with input shapes: [84,128], [2], [2,2].
This is the sequence I intend to input:
sequence = [[[113, 162, 159], [3, 163, 417], [393, 77, 333], [420, 214, 382], [308, 441, 175], [152, 80, 477], [184, 101, 54], [417, 277, 487], [494, 329, 315], [413, 386, 319]],
[425, 132, 407],
[405]]
However, I am unable to determine what shape of placeholder to use for it.
x = tf.placeholder(tf.float32, shape=[None, None, 3], name='probable_solutions')
sess = tf.Session()
init_op = tf.global_variables_initializer()
sess.run(init_op)
sess.run(x, feed_dict={x: [sequence[0], sequence[1], sequence[2]]})
This gives me the following error:
ValueError: setting an array element with a sequence.
Here's the full code-
https://pastebin.com/cq44wcir
(I've also marked a few questions in the pastebin code - you can find them by searching for '#~~#', no quotes, in the text)
Collected from :
first of all before sending it to feed_dict, sequence should be a numpy array.
of course, you can convert it to numpy array easily but that's not the solution.
It's clear that you are trying to create an array from a list which isn't shaped like a multi-dimensional array.
Any array which isn't "Generalized" cannot be used as feed_dict
I am running the tutorial code A Guide to TF Layers: Building a Convolutional Neural Network on API r.1.3
https://www.tensorflow.org/tutorials/layers
My code is here.
https://gist.github.com/Po-Hsuan-Huang/91e31d59fd3aa07f40272b75fe2a924d
The error shows:
runfile('/Users/pohsuanhuang/Documents/workspace/tensorflow_models/NMIST/cnn_mnist.py', wdir='/Users/pohsuanhuang/Documents/workspace/tensorflow_models/NMIST')
Extracting MNIST-data/train-images-idx3-ubyte.gz
Extracting MNIST-data/train-labels-idx1-ubyte.gz
Extracting MNIST-data/t10k-images-idx3-ubyte.gz
Extracting MNIST-data/t10k-labels-idx1-ubyte.gz
INFO:tensorflow:Using default config.
INFO:tensorflow:Using config: {'_save_checkpoints_secs': 600, '_session_config': None, '_keep_checkpoint_max': 5, '_tf_random_seed': 1, '_keep_checkpoint_every_n_hours': 10000, '_log_step_count_steps': 100, '_save_checkpoints_steps': None, '_model_dir': '/tmp/mnist_convnet_model', '_save_summary_steps': 100}
Traceback (most recent call last):
File "<ipython-input-1-c9b70e26f791>", line 1, in <module>
runfile('/Users/pohsuanhuang/Documents/workspace/tensorflow_models/NMIST/cnn_mnist.py', wdir='/Users/pohsuanhuang/Documents/workspace/tensorflow_models/NMIST')
File "/Users/pohsuanhuang/miniconda/envs/tensorflow/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 866, in runfile
execfile(filename, namespace)
File "/Users/pohsuanhuang/miniconda/envs/tensorflow/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 94, in execfile
builtins.execfile(filename, *where)
File "/Users/pohsuanhuang/Documents/workspace/tensorflow_models/NMIST/cnn_mnist.py", line 129, in <module>
main(None)
File "/Users/pohsuanhuang/Documents/workspace/tensorflow_models/NMIST/cnn_mnist.py", line 117, in main
hooks=[logging_hook])
File "/Users/pohsuanhuang/miniconda/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/estimator/estimator.py", line 241, in train
loss = self._train_model(input_fn=input_fn, hooks=hooks)
File "/Users/pohsuanhuang/miniconda/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/estimator/estimator.py", line 630, in _train_model
model_fn_lib.ModeKeys.TRAIN)
File "/Users/pohsuanhuang/miniconda/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/estimator/estimator.py", line 615, in _call_model_fn
model_fn_results = self._model_fn(features=features, **kwargs)
File "/Users/pohsuanhuang/Documents/workspace/tensorflow_models/NMIST/cnn_mnist.py", line 24, in cnn_model_fn
input_layer = tf.reshape(features, [-1, 28, 28, 1])
File "/Users/pohsuanhuang/miniconda/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/gen_array_ops.py", line 2619, in reshape
name=name)
File "/Users/pohsuanhuang/miniconda/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 493, in apply_op
raise err
TypeError: Failed to convert object of type <type 'dict'> to Tensor. Contents: {'x': <tf.Tensor 'random_shuffle_queue_DequeueMany:1' shape=(100, 784) dtype=float32>}. Consider casting elements to a supported type.
I traced down a little bit, and found the function estimator._call_input_fn() does not use parameter 'mode' at all, thus not able to create a tuple comprises features and labels. Is it the tutorial that needs to be modified, or there is some problem with this function. I don't understand why mode is unused here.
Thanks !
def _call_input_fn(self, input_fn, mode):
"""Calls the input function.
Args:
input_fn: The input function.
mode: ModeKeys
Returns:
Either features or (features, labels) where features and labels are:
features - `Tensor` or dictionary of string feature name to `Tensor`.
labels - `Tensor` or dictionary of `Tensor` with labels.
Raises:
ValueError: if input_fn takes invalid arguments.
"""
del mode # unused
input_fn_args = util.fn_args(input_fn)
kwargs = {}
if 'params' in input_fn_args:
kwargs['params'] = self.params
if 'config' in input_fn_args:
kwargs['config'] = self.config
with ops.device('/cpu:0'):
return input_fn(**kwargs)
Your gist doesn't actually contain any of your code... Either way, from your error message I think you have just mistranscribed a bit of code from the tutorial.
Your error log indicates you have
"/Users/pohsuanhuang/Documents/workspace/tensorflow_models/NMIST/cnn_mnist.py", line 24, in cnn_model_fn
input_layer = tf.reshape(features, [-1, 28, 28, 1])
Whereas the tutorial has:
input_layer = tf.reshape(features["x"], [-1, 28, 28, 1])