4

I am saving my trained model using following command:

net.save(model_name)

And then loading it using following command:

net.load(model_name)

But after loading the model, when I am trying to print blobs using net.blobs(), it gives me an empty dictionary. It looks like that model is either not saved properly or not loading properly.

Kindly help me in this regard. Thanks in advance.

Shweta
  • 1,111
  • 3
  • 15
  • 30
  • do you have a log output for `net.load(model_name)`? how do you define the net structure to your model? is it possible you need to call `net.load` after you constructed the net (either by loading a `prototxt` or using `net.f`)? – Shai May 30 '16 at 09:04
  • Thanks for the reply. I am using simple.py from http://apollocaffe.com/#Tutorial. I am using net.save("model.caffemodel") in the end to save the model. And for loading trying both net = apollocaffe.ApolloNet("model.caffemodel") and net.load("model.caffemodel"). But in both the cases net.loss gives me 0.0. I understand that I need to load something about the architecture of net. But I am not sure what exactly needs to be loaded. I tried few things, but nothing working. It would be great if you can share your thoughts on this. – Shweta May 30 '16 at 10:31
  • I am not familiar with the "apollocaffe" workflow. In regular caffe, you have two files describing your model: `model.caffemodel` - a binary file that stores the numerical values of the trained weights *and* `deploy.prototxt` a text file that describe the net architecture: what layers are used and how these layers are connected to each other. It seems like you are missing the second part: the net structure information. check out if you can save/load this structure using apollocaffe interface. – Shai May 30 '16 at 10:52

2 Answers2

2

There are two elements to your question.

Saving:

Inorder to load the model, you must save it in HDF5 format. This can be done using SnapshotLogger class as shown:

from apollocaffe.loggers import SnapshotLogger

SnapshotLogger(snapshot_interval, snapshot_prefix='PATH_TO_YOUR_MODEL',
        log_file="PATH_TO_log.txt")

Loading:

Loading a saved model can be done as shown:

net = apollocaffe.ApolloNet()
model_path = "../model_name.h5"
net.load(model_path)
Nickil Maveli
  • 29,155
  • 8
  • 82
  • 85
0

I looked more into the details of this problem and looks like I was saving-loading it correctly. The problem is that after loading the model net.blobs will not have any value. But if you use the loaded model with any test example, it works fine (as expected). I am attaching an example code, inspired from https://github.com/Russell91/apollocaffe/blob/master/examples/apollocaffe/simple.py here which works fine:

import apollocaffe
from apollocaffe.layers import NumpyData, Convolution, EuclideanLoss
import numpy as np

def save():
    net = apollocaffe.ApolloNet()
    for i in range(1000):
        example = np.array(np.random.random()).reshape((1, 1, 1, 1)) 
        net.clear_forward()
        net.f(NumpyData('data', example))
        net.f(NumpyData('label', example*3))
        net.f(Convolution('conv', (1,1), 1, bottoms=['data']))
        net.f(EuclideanLoss('loss', bottoms=['conv', 'label']))
        net.backward()
        net.update(lr=0.1)
        if i % 100 == 0:
            print net.loss
        net.save("model.h5")


def load():
    print "LOAD"
    net = apollocaffe.ApolloNet()
    net.load("model.h5")
    #example = np.array(np.random.random()).reshape((1, 1, 1, 1))
    example = np.asarray([[[[ 0.92890837]]]])
    net.clear_forward()
    net.f(NumpyData('data', example))
    net.f(NumpyData('label', example*3))
    net.f(Convolution('conv', (1,1), 1, bottoms=['data']))
    net.f(EuclideanLoss('loss', bottoms=['conv', 'label']))
    net.backward()
    net.update(lr=0.1)
    print net.loss

save()
load()
Shweta
  • 1,111
  • 3
  • 15
  • 30
  • it seems like you are confusing between the learned parameters of the net (stored in `net.layers`) and the net's response (i.e., filters' output) to a specific input (stored in `net.blobs`). Please see [this answer](http://stackoverflow.com/a/31847179/1714410) for more details. – Shai Jun 06 '16 at 11:15
  • @Shai: How do you save the solverstate using pycaffe? I noticed net.save only saves the caffemodel? any idea? Thanks – Hossein Mar 28 '18 at 15:30
  • 1
    @Breeze you need to `save` a `solver` object and not a `net` object. The `net` does not know the solverstate. – Shai Mar 28 '18 at 15:33
  • @Shai: Thanks alot, so you mean to use solver.snapshot() ? If so, is there a way that i can only create the solverstate? since I'm saving the caffemodel with a specific name and I would like to save the corrosponding solverstate as well. – Hossein Mar 28 '18 at 16:11
  • 1
    @Breeze I am not very familiar with pycaffe solver interface. Why not ask a new question? these comments are NOT the proper place for this thread – Shai Mar 28 '18 at 16:14
  • Yeah you are right. I'll make a new question for that. Thanks alot by the way – Hossein Mar 28 '18 at 16:21