user3867261
user3867261

Reputation: 109

How to save feature values of all batch data from pretrained torch networks?

Now I'm using fb torch library from github fb torch resnet

It's my first time to use torch and lua, so Im encountering some problems.

My goal is to save the feature vector of specific layer (last avg pooling of resnet) into a one file with the class of the input image. All input images are from cifar-10 db.

The file format that i want to get is like belows

image1.txt := class index of image and feature vector of image 1 of cifar-10
image2.txt := class index of image and feature vector of image 2 of cifar-10
 // and so on  through all images of cifar-10

Now I have seen some sample code of that github extract-features.lua

Because it's my first time for lua, I feel so hard to understand this code and to modify to the way i want. And i don't want my data to save into t7 file format.

  1. How can i access only one specific layer from network in torch via lua? (last average pooling)
  2. How can i access values of the layer and classification result index?
  3. How can read all each images from cifar-10 db file(t7 batch)?

Sorry for too many questions. But im feeling hard using torch because of pool amouns of community threads and posting of torch.. please understand me.

Upvotes: 0

Views: 234

Answers (1)

Manuel Lagunas
Manuel Lagunas

Reputation: 2751

How can i access only one specific layer from network in torch via lua? (last average pooling)

To access each layer you just have to load the model and get it using an integer number. If you do print model you will be able to see in which position the last average pooling is.

model = torch.load(path_to_model):cuda()
avg_pooling_layer = model:get(position_of_the_avg_pooling_layer)

How can i access values of the layer and classification result index?

I do not quite understand what you mean by this. If you want to see the output or the weights from a specific layer. (following the code above) You need to get these elements from the layer table. Again, to see which ones are the possible elements to get use print avg_pooling_layer

weights = avg_pooling_layer.weight -- get the weights of the layer
output = avg_pooling_layer.output  -- get the output of the layer

How can read all each images from cifar-10 db file(t7 batch)?

To read the images from a t7 file use the torch function torch.load. (used before to load the model).

cifar_10 = torch.load("path_to_cifar-10.t7")

Once loaded you could have the training and test set in subtables or functions. Again, print the table and visualize which values are the ones you need to get.

Hope this helps!

Upvotes: 1

Related Questions