How to reshape Tensor in C++ like Caffe's Blob - c++

I want to use tensors of dynamic shapes in C++. For example, I want add a new op in tensorflow, but I do not know the output's shape in the beginning. If I use Caffe, I can firstly reshape the output blob to the maximum size I will use, and reshape to it is actual size in the end.
How can do it with tensorflow's Tensor?.

If you are not sure about the shape of your Variable yet, leave one or more dimension of that tf.Variable as None. For example:
x = tf.placeholder(tf.float32, shape=[None, 1,1])
Tensorflow also has a tf.reshape() function that you can use in the same fashion as caffe. For example:
x2 = tf.reshape(x, [-1, dim]) # -1 means "all"

Related

Pass variable sized input to Linear layer in Pytorch

I have a Linear() layer in Pytorch after a few Conv() layers. All the images in my dataset are black and white. However most of the images in my test set are of a different dimension than the images in my training set. Apart from resizing the images themselves, is there any way to define the Linear() layer in such a way that it takes a variable input dimension? For example something similar to view(-1)
Well, it doesn't make sense to have a Linear() layer with a variable input size. Because in fact it's a learnable matrix of shape [n_in, n_out]. And matrix multiplication is not defined for inputs if theirs feature dimension != n_in
What you can do is to apply pooling from functional API. You'll need to specify kernel_size and stride such that resulting output will have feature dimension size = n_in.

Is there a clean way to create 3d tensors for feeding Tensorflow prediction in C++?

I've built a model in Python where each input tensor has the shape (4, 6, 13). I want to run predictions with the model in C++.
I'd like to write a function that takes as input data in a C++ three-dimensional array:
float input[4][6][13];
and produces a Tensor with shape TensorShape({4,6,13}).
It seems Eigen3 does not have support for 3d arrays. I see that Tensorflow::Input::Initializer can initialize a 3d tensor from an initializer list, but I have dynamic data, not compile-time constants.
After searching for a solution I am about to conclude that I should change my model to expect a flattened vector of length 4x6x13=312, and then reshape it as the first step, but I'd prefer a cleaner solution.

Generate new tensorflow tensor according to the element index of original tensor

I have a question about tensorflow tensor.
If I have a NeuralNet like y=xw+b as an example.
then x is placeholder([7,7] dims), w is Variable([7,1]) and b is Variable([1,1])
So, y is tensorflow tensor with [7,1] dims.
then, in this case. can I make a new tensor like
new_y = [tf.reduce_sum(y[0:3]), tf.reduce_sum(y[3:5]), tf.reduce_sum(y[5:])]
and use it for training step?
If possible, how can I make it?
You should just make your label (y) in your reduced sum format (i.e. 3 bits), and train to that label. The neural net should be smart enough to adjust the weights to imitate your reduce_sum logic.

What is the equivalent python code for this octave code

link to code and files
1.I =imread('one.jpg');
2.I = inresize(I,[20,20]);
3.I=im2double(I);
4.I=mean(I,3);
#This next line
5.a = reshape(I,[],400);
I read an image and resizzed it to 20*20 and then converted it to matrix and then find the grayscale .All this I can do in Python too....but I can't do the 5 th line of code...if I tried ,
reshape (I,1,400)...the image appears rotated...I don't know how to write the 5 the line as above in python
The problem
in the link along with the code theres is a displayData function.I saved the matrix i got using python as mat and loaded it on octave when i called displayData() on the matrix i got a rotated image.thats inclued in the link.And theres no such problem in octave.Thank you for looking into this.
For reshaping an array you can use numpy, and, following your code, you can use reshape. In your case, you are changing the size of I, from (20,20) to (1,400).
A complete example which saves the resulting reshaped array to a mat file, using OpenCV APIs for dealing with images, is:
import numpy as np
import cv2
import scipy.io
I = cv2.imread('one.jpg')
I = cv2.resize(I,(20,20))
I = cv2.normalize(I.astype('float'), None, 0.0, 1.0, cv2.NORM_MINMAX)
I = np.mean(I, axis=2)
a = np.reshape(I, (1,400), order='F')
scipy.io.savemat('a.mat', mdict={'a': a})
Note the second parameter of reshape, which is a tuple containing the new size of the array. Also, notice the third parameter order that allows to rearrange elements in column-major style (Fortran) which is the convention used by octave (see reshape in octave http://www.gnu.org/software/octave/doc/v4.0.1/Rearranging-Matrices.html#XREFreshape). This results in a correct image, non rotated, compared to the one got from octave.
However, given the fact that you want to get from a 2d array a 1d array, you can use, from numpy, ravel if you want to get a view of I (when possible), namely a modification of a changes also I; or flatten, which returns a copy of I, thus modifying a does not change I. However, note that both ravel and flatten returns a 1d array resulting in a size of (400,). The same order parameter should be used.

Python Imaging Processing (PIL) - changing the overall RGB of an image

I am trying to change the RGB for the overall image for a project. Currently I am working with a test file before I apply it to the actual Image. I want to test different values of RGB but would first like to start with the mean of all three. How would I go about doing this? I have other modules installed such as scipy, numpy, matplotlib, etc if those are needed. Thanks
from PIL import Image, ImageFilter
test = Image.open('/Users/MeganRCunninghan/Pictures/4th-of-July-Wallpaper.ppm')
test.show()
test.getrgb()
Assuming your image is stored as a numpy.ndarray (Test this with print type(test))...
Your image will be represented by an NxMx3 array. Basically this means you have a N by M image with a color depth of 3- your RGB values. Taking the mean of those 3 will leave you with an NxMx1 array, where the 1 is now the average intensity. Numpy does this very well:
test = test.mean(2)
The parameter given, 2, specifies the dimension to take the mean along. It could be either 0, 1, or 2, because your image matrix is 3 dimensional. This should return an NxM array. You basically will be left with a gray-scale, (color depth of 1) image. Try to show the value that gets returned! If you get Nx3 or Mx3, you know you have just taken the average along the wrong axis. Note that you can check the dimensions of a numpy array with:
test.shape
Shape will be a tuple describing the dimensions of your image.