What dataset are used for tests in Caffe? - caffe

When I run,
caffe test or caffe time
the tests proceed even for the newly imported network architectures. What data is used for those tests?
Update:
Here is a snippet from the data layer from the Caffenet, where there is no reference to source. It comes from:
https://github.com/BVLC/caffe/blob/master/models/bvlc_reference_caffenet/deploy.prototxt
name: "CaffeNet"
layer {
name: "data"
type: "Input"
top: "data"
input_param { shape: { dim: 10 dim: 3 dim: 227 dim: 227 } }
}
Update 2:
But still the following command works and runs the test, despite the fact we are using deploy.prototxt:
caffe test -model=models/bvlc_reference_caffenet/deploy.prototxt -weights=models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel

As #Shai suggested, you need to look at the input layer, as defined in the prototxt.
Since you don't give any details about your models, I will use the interfaces tutorial as an example:
http://caffe.berkeleyvision.org/tutorial/interfaces.html
which has the following:
caffe test -model examples/mnist/lenet_train_test.prototxt -weights examples/mnist/lenet_iter_10000.caffemodel -gpu 0 -iterations 100
You can find this prototxt here:
For example:
https://github.com/BVLC/caffe/blob/master/examples/mnist/lenet_train_test.prototxt
The data source (that is included for testing) is given as:
source: "examples/mnist/mnist_test_lmdb"
This is the data that is used.
You can read about how to download this data in the examples/mnist directory:
https://github.com/BVLC/caffe/tree/master/examples/mnist
Update: if the Data layer doesn't have a source parameter, then it is likely just used for "deployment". In this setup the data can be passed to the model live without the ground truth data. caffe test should not be called on this type of prototxt. Look for the training/testing prototxt instead (most models come with both).

Related

Pytorch 1.0: what does net.to(device) do in nn.DataParallel?

The following code from the tutorial to pytorch data paraleelism reads strange to me:
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model = Model(input_size, output_size)
if torch.cuda.device_count() > 1:
print("Let's use", torch.cuda.device_count(), "GPUs!")
# dim = 0 [30, xxx] -> [10, ...], [10, ...], [10, ...] on 3 GPUs
model = nn.DataParallel(model)
model.to(device)
According to my best knowledge, mode.to(device) copy the data to GPU.
DataParallel splits your data automatically and sends job orders to multiple models on several GPUs. After each model finishes their job, DataParallel collects and merges the results before returning it to you.
If the DataParallel does the job of copying, what does the to(device) do here?
They add few lines in the tutorial to explain nn.DataParallel.
DataParallel splits your data automatically, and send job orders to multiple models on different GPUs using the data. After each model finishes their job, DataParallel collects and merges the results for you.
The above quote can be understood that nn.DataParallel is just a wrapper class to inform model.cuda() should make a multiple copies to GPUs.
In my case, I don't have any GPU on my laptop. I still call nn.DataParallel() without any problem.
import torch
import torchvision
model = torchvision.models.alexnet()
model = torch.nn.DataParallel(model)
# No error appears if I don't move the model to `cuda`

what is wrong when training an autoencoder on mnist dataset with caffe?

I want to use mnist dataset to train a simple autoencoder in caffe and with nvidia-digits.
I have:
caffe: 0.16.4
DIGITS: 5.1
python 2.7
I use the structure provided here:
https://github.com/BVLC/caffe/blob/master/examples/mnist/mnist_autoencoder.prototxt
Then I face 2 problems:
When I use the provided structure I get this error:
Traceback (most recent call last):
File "digits/scheduler.py", line 512, in run_task
task.run(resources)
File "digits/task.py", line 189, in run
self.before_run()
File "digits/model/tasks/caffe_train.py", line 220, in before_run
self.save_files_generic()
File "digits/model/tasks/caffe_train.py", line 665, in save_files_generic
'cannot specify two val image data layers'
AssertionError: cannot specify two val image data layers
when I remove the layer for ''test-on-test'', I get a bad result like this:
https://screenshots.firefox.com/8hwLmSmEP2CeiyQP/localhost
What is the problem??
The first problem occurs because the .prototxt has two layers with name data and TEST phase. The first layer that uses data, i.e. flatdata, does not know which data to use (the test-to-train or test-to-test). That's why when you remove one of the data layers with TEST phase, the error does not happen. Edit: I've checked the solver file and it has a test_stage parameter that should switch between the test files, but it's clearly not working in your case.
The second problem is a little more difficult to solve. My knowledge in autoencoders is limited. It seems your euclidean loss changes very little during your iterations; I would check the base learning rate in your solver.prototxt and decrease it. Check how the losses fluctuate.
Besides that, for the epochs/iterations that achieved a low error, have you checked the output data/images? Do they make sense?

Caffe: Print the softmax score

In the given example of MNIST in the Caffe installation.
For any given test image, how to get the softmax scores for each category and do some processing on them? Say compute the mean and variance of them.
I am newbie so a detail would help me a lot. I am able to train the model and use the testing feature to get the prediction but I am not sure which files are to be edited in order to get the above results.
You can use python interface
import caffe
net = caffe.Net('/path/to/deploy.prototxt', '/path/to/weights.caffemodel', caffe.TEST)
in_ = read_data(...) # this is up to you to read a sample and convert it to numpy array
out_ = net.forward(data=in_) # assuming your net expects "data" in blob
Now you have the output of your net in a dictionary out (keys are names of output blobs). You can run it in a loop on several examples etc.
I can try to answer your question. Assuming in your deploying net, the softmax layer is like below:
layer {
name: "prob"
type : "Softmax"
bottom: "fc6"
top: "prob"
}
In your python code that processes data, combining with the code #Shai provided, you can get the probability of each category by adding code based on #Shai's code:
predicted_prob = net.blobs['prob'].data
predicted_prob will be returned an array that contains the probabilities with all categories.
For example, if you only have two categories, predicted_prob[0][0] will be the probability that this testing data belongs to one category and predicted_prob[0][1] will be the probability of the other one.
PS:
If you don't want to write any additional python script, according to https://github.com/BVLC/caffe/tree/master/examples/mnist
it says this example will automatically do the testing every 500 iterations. "500" is defined in solver, such as https://github.com/BVLC/caffe/blob/master/examples/mnist/lenet_solver.prototxt
So you need to trace back the caffe source code that processes the solver file. I guess it should be https://github.com/BVLC/caffe/blob/master/src/caffe/solver.cpp
I am not sure solver.cpp is the correct file you need to look at. But in this file, you can see it has functions of testing and calculation of some values. I hope it can give you some ideas if no one else can answer your question.

Check failed: registry.count(type) == 0 (1 vs. 0) Layer type Split already registered

I have created a python layer for data augmentation which worked well with digits but when I train the network using terminal command on ubuntu 14.04, I get this error:
I1130 16:29:56.155732 18230 layer_factory.hpp:77] Creating layer aug_data
F1130 16:29:56.220578 18230 layer_factory.hpp:69] Check failed: registry.count(type) == 0 (1 vs. 0) Layer type Split already registered.
where aug_data is the custom python layer. I have made changes in configuration file to accept the python layer but I think there is something wrong with linking the layers that I could not fix. I cannot use DIGITS as my data is hyperspectral while the DIGITS accept either grayscale or RGB images.
Any help would be appreciated.
According to your prototxt file, you should be able to run "from digits_python_layers import AugmentationLayer". Does this work (from any directory)?
Old answer:
Your new layer should return something other than "Split" for its layer type (via its type() function).

Fusing different input channels in caffe?

I'd like to process different types of data seperately first and then fuse them in a common layer. Is this possible in Caffe and if yes what would be the best way to do it?
I've read that one can define several data layers in the same prototxt file. But how to fuse them?
Can I just create a InnerProduct layer and specify several bottom layers? Or do I have to concatenate the individual layers first using a Concat layer?
For any small code example I would be very thankful!
As discussed in the comments above, InnerProduct works with a single input. The fusion (concatenation) can then be done in a specific Concat layer with a configuration like this:
layer {
name: "concat"
bottom: "in1"
bottom: "in2"
top: "out"
type: "Concat"
concat_param {
axis: 1
}
}
The official documentation has more details about that layer: http://caffe.berkeleyvision.org/tutorial/layers.html