My question is how can I reshape the tensor!
Here is one syntax tf.reshape(tensor, shape, name=None)
i do not know , where am i going wrong but i am not able to reshape
how can i do that?
thanks
axis=0
# image is your tensor
tf.expand_dims(your_image, axis)
You can use tf.expand_dims:
output = tf.expand_dims(input, 0)
Try the following code:
#assuming `img` contains the data which is in the format (256,256,3)
output = tf.reshape(img, [1, 256, 256, 3]) # 1 is batch size
Related
I have a tensor with this size
torch.Size([128, 64])
how do I add one "dummy" dimension such as
torch.Size([1, 128, 64])
There are several ways:
torch.unsqueeze:
torch.unsqueeze(x, 0)
Using None (or np.newaxis):
x[None, ...]
# or
x[np.newaxis, ...]
reshape or view:
x.reshape(1, *x.shape)
# or
x.view(1, *x.shape)
I am trying to load weights and for that to work i need to perform the following:
dummy_input = tf.random.uniform(input_shape) # create a tensor of input shape
dummy_label = tf.random.uniform(label_shape) # create a tensor of label shape
hist = model.fit(dummy_input, dummy_label)
I am new to this and can't figure out what these shapes should be.
Some information about my model:
Im feeding the model images with shape (224,224,3).
In batches of 16.
I have 423 different classes and use sparse_categorical_crossentropy.
I tried this
dummy_input = tf.random.uniform([16, 224, 224, 3]) # create a tensor of input shape
dummy_label = tf.random.uniform([16, 1, 423]) # create a tensor of label shape
hist = model.fit(dummy_input, dummy_label, epochs=epochs,
steps_per_epoch=len_train // batch_size,
validation_steps=len_test // batch_size)
There may be many errors here but the one i am getting right now is
ValueError: Shape mismatch: The shape of labels (received (423,))
should equal the shape of logits except for the last dimension (received (1, 423)).
This was the solution
dummy_input = tf.random.uniform([32, 224, 224, 3]) # create a tensor of input shape
dummy_label = tf.random.uniform([32,]) # create a tensor of label shape
hist = model.fit(dummy_input, dummy_label)
Here I have one tensor with (1084, 1625, 3) shape.
And I need to reshape it to (none,none,none ,3).
how can i do that?
I used this code but it does not work.
image = tf.cast(img, tf.float32)
image = (image / 127.5) - 1
I don't think you can do that. I think what you're trying to do is turn a 3D tensor into a 4D tensor. I'm guessing this is the origin of your problem. You can do this to add a 4th dimension, because Tensorflow needs it:
import tensorflow as tf
tensor = tf.random.uniform((100, 100, 3), 0, 256, dtype=tf.int32)
new = tf.expand_dims(tensor, axis=0)
print(new.shape)
Out[14]: TensorShape([1, 100, 100, 3])
But then I could be wrong. If this is the case you can provide your error traceback and code.
My Input is like a (3,3,2) array and a (3,3) array:
img = np.array([[[1,1],[2,2],[3,3]],
[[4,4],[5,5],[6,6]],
[[7,7],[8,8],[9,9]]])
idx = np.array([[1,0,0],
[0,0,1],
[1,1,0]])
My ideal output should be:
[[1 1]
[6 6]
[7 7]
[8 8]]
I want to do this by a custom layer:
make a layer:
def extract_layer(data, idx):
idx = tf.where(idx)
data = tf.gather_nd(data,idx)
data = tf.reshape(data,[-1,2])
return data
make into model:
input_data = kl.Input(shape=(3,3,2))
input_idxs = kl.Input(shape=(3,3))
extraction = kl.Lambda(lambda x:extract_layer(*x),name='extraction')([input_data,input_idxs])
I can build the model , and i can see the keras summary of the model,
the output is
model = Model(inputs=([input_data,input_idxs]), outputs=extraction)
model.summary()
...
input_1 (InputLayer) (None, 3, 3, 2)
input_2 (InputLayer) (None, 3, 3)
extraction (Lambda) (None, 2)
Total params: 0
...
but when i start to predict like :
'i have already made the two inputs into (1,3,3,2) and (1,3,3) shape'
result = model.predict(x=([img,idx]))
it gets error:
'ValueError: could not broadcast input array from shape (4,2) into shape (1,2)'
i think the tensor of shape(4,2) is the value i want
but i don't know why keras broadcast it to (1,2)
is there anyone who can help me ??
thanks very much !
In your extract_layer() function, the data is two dims tensor. But model.predict is supposed to return results with an extra batch dim. Just expand the dim when return data in extract_layer() can fix this error.
def extract_layer(data, idx):
idx = tf.where(idx)
data = tf.gather_nd(data,idx)
data = tf.reshape(data,[-1,2])
return tf.expand_dims(data, axis=0)
Note: Since the results returned by tf.gather_nd might have different length, I think the batch size would only be 1. Please correct me if I'm wrong.
There is a 25*15 image, and i want to identify what it is by using CNN.
When training my CNN, I input a numpy named 'img' as datasets which shape is (200, 375):
sess.run(train, feed_dict={X: imgs, Y: labels}
This numpy contains 200 sample ,each of them have 375 features.
But when i reshape this numpy to a (-1, 25, 15, 1) Tensor:
X = tf.placeholder(tf.float32, [None, 375])
X = tf.reshape(X,[-1,25,15,1])
Something wrong happened:
Cannot feed value of shape (200, 375) for Tensor 'Reshape:0', which has shape '(?, 25, 15, 1)'
I don't know why it can't work, 25*15 is indeed 375.
Thank you!
You don't seem to reshape the dict variable you are feeding to the placeholder. You have to reshape your img variable as well into shape [-1, 25, 15, 1]