Shweta Shweta - 6 months ago 26
Python Question

Issues in save - load apollocaffe models

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.

Answer

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()