How to reshape the res5c layer of ResNet (3D to 2D)? - python

I extract the features of an image with ResNet of the 'res5c' layer, resulting of a numpy array of shape (2048, 14, 14)
I have trouble manipulating these dimensions. I understand there is 14*14 features of size 2048. I would like to iterate over to access every feature at a time.
Therefore, how I can reshape this to an array of (14*14, 2048) without mistakes and then easily iterate over it with a for loop?

You can read the features after net.forward():
feat = net.blobs['res5c'].data.cop() # copy to be on the safe side.
As you describe, feat is an np.array with shape = (2048, 14, 14).
You can reshape it:
feat.reshape((2048,-1)) # fix the first dimension to 2048, -1 set the number of features to match that of `feat`.
Now you can iterate over features:
for fi in xrange(feat.shape[1]):
f = feat[:,fi] # get the fi-th feature
# do somethinf to the feature f

Related

Transform 2D embeddings into 3D embeddings

I want to fuse node embeddings from a GNN shaped {[N, 128], where N can vary, with hidden state from an LSTM shaped [2, 32, 128]. To do so, I wanted to add both feature tensors, but they need to be the same shape.
I have something like:
N = 75
t = torch.rand(N, 128)
t.unsqueeze_(0)
t = t.expand(2,N, 128)
I do not seem to figure out how to make dimension 1 equal to 32. I am looking for some combination of transforms and learnable layers that can help me make this shape conversion. Thanks in advance for any help you can give.

Python numpy concatenate 4D

I'm trying to concatenate 2 numpy arrays of features predicted by the convolution layers in a vgg16 model.
Basically i have used the bottom layers of a vgg16 model to predict the features for my full dataset and now I want to load the parts of dataset dynamically based on some settings, to train some models with it.
So, I have 2 array of shape:
(724, 512, 6, 8) and (3376, 512, 6, 8)
Basically the first one contains features predicted from 724 image files (each prediction has shape (512, 6, 8)).
I want to concatenate these 2 arrays into one of shape (4100, 512, 6, 8)
I have tried using:
np.array([np.concatenate(arr, axis=0) for arr in false_train_list])
where false_train_list is the list containing the 2 arrays with the above shapes.
Also tried with np.stack, tf.stack...
All of these result in an array with shape (2,)
Can someone explain why ? I haven't found any good resources to understand how exactly np.concatenate() works..
Thank you!
I think you simply need this instead:
np.concatenate(false_train_list, axis=0)
https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.concatenate.html

Giving input of different sizes to LSTM model in keras

I have a multi-class(4-class) classification model in keras which looks like 1
While training, the model expects the input shape to be (None,None,300). That is, If there are 'n' different input sequences, then the input shape should be (n,None,300). In my case, the size of each input sequence is different.
Say, the input sequences are of shapes (1000,300), (1500,300), (1200,300) and (2000,300). Now I need to put them together to (4,None,300). I tried using numpy array, but numpy array won't give shape of (4,None,300),instead it will be (4L,).
Now I want to know how to train my model? Is it possible to do with numpy arrays or any different data structures are available?
Since your sequences are of different duration, you may consider padding them with zeros (adjusting the loss/labels accordingly) and then
max_duration = 2000
in_ = np.zeros((4, max_duration, 300), dtype='f4')
for i in xrange(4):
# fit sequence
in_[i,:len(seq[i]),:] = seq[i]

Using Sparse Tensors to feed a placeholder for a softmax layer in TensorFlow

Has anyone tried using Sparse Tensors for Text Analysis with TensorFlow with success? Everything is ready and I manage to feed feed_dict in tf.Session for a Softmax layer with numpy arrays, but I am unable to feed the dictionary with SparseTensorValues.
I have not found either documentation about using sparse matrices to train a model ( softmax for example ) with Tensor Flow, which is strange, as classes SparseTensor and SparseTensorValues or TensorFlow.sparse_to_dense methods are ready for it, but there is no documentation about how to feed the feed_dict dictionary of values in the session.run(fetches,feed_dict=None) method.
Thanks a lot,
I have found a way of putting sparse images into tensorflow including batch processing if that is of any help.
I create a 4-d sparse matrix in a dictionary where the dimensions are batchSize, xLen, ylen, zLen (where zLen is 3 for colour for example). The following pseudo code is for a batch of 50 32x96 pixel 3-color images. Values are the intensity of each pixel. In the snippet below I show the first 2 pixels of the first batch being initialised...
shape = [50, 32, 96, 3]
indices = [[0, 20, 31, 0],[0, 22, 33, 1], etc...]
values = [12, 24, etc...]
batch = {"indices": indices, "values": values, "shape": shape}
When setting up the computational graph I create a sparse-placeholder of the correct dimensions
images = tf.sparse_placeholder(tf.float32, shape=[None, 32, 96, 3])
'None' is used so I can vary the batch size.
When I first want to use the images, e.g. to feed into a batch convolution, I convert them back to a dense tensor:
images = tf.sparse_tensor_to_dense(batch)
Then when I am ready to run a session, e.g. for training, I pass the 3 components of the batch into the dictionary so that they will be picked up by the sparse_placeholder:
train_dict = {images: (batch['indices'], batch['values'], batch['shape']), etc...}
sess.run(train_step, feed_dict=train_dict)
If you are not needing to do batch processing just leave off the first dimension and remove 'none' from the placeholder shape.
I couldn't find any way of passing the images across in batch as an array of sparse matrices. It only worked if I created the 4th dimension. I'd be interested to know of alternatives.
Whilst this doesn't give an exact answer to your question I hope it is of use as I have been struggling with similar issues.

Get output from Lasagne (python deep neural network framework)

I loaded the mnist_conv.py example from official github of Lasagne.
At the and, I would like to predict my own example. I saw that "lasagne.layers.get_output()" should handle numpy arrays from official documentation, but it doesn't work and I cannot figure out how can I do that.
Here's my code:
if __name__ == '__main__':
output_layer = main() #the output layer from the net
exampleChar = np.zeros((28,28)) #the example I would predict
outputValue = lasagne.layers.get_output(output_layer, exampleChar)
print(outputValue.eval())
but it gives me:
TypeError: ConvOp (make_node) requires input be a 4D tensor; received "TensorConstant{(28, 28) of 0.0}" (2 dims)
I understand that it expects a 4D tensor, but I don't have any idea how to correct it.
Can you help me? Thanks
First you try pass a single "image" into your network, which so it has the dimension (256,256).
But it need a list of 3 dimensional data i.e. images, which in theano is implemented as 4D tensor.
I don't see your full code, how you intended to use lasagne's interface, but if your code is written properly, from what I saw so far, I think you should convert your (256,256) data first to a one single channel image like (1,256,256), then make a list from either use more (1,256,256) data passed in a list e.g. [(1,256,256), (1,256,256), (1,256,256)], or make a list from this single example like [(1,256,256)].
Former you get and then pass a (3,1,256,256), latter a (1,1,256,256) 4D tensor, which will be accepted by lasagne interface.
As written in your error message, the input is expected to be a 4D tensor, of shape (n_samples, n_channel, width, height). In the MNIST case, n_channels is 1, and width and height are 28.
But you are inputting a 2D tensor, of shape (28, 28). You need to add new axes, which you can do with exampleChar = exampleChar[None, None, :, :]
exampleChar = np.zeros(28, 28)
print exampleChar.shape
exampleChar = exampleChar[None, None, :, :]
print exampleChar.shape
outputs
(28, 28)
(1, 1, 28, 28)
Note: I think you can use np.newaxis instead of None to add an axis. And exampleChar = exampleChar[None, None] should work too.

Categories

Resources