judyzha
judyzha

Reputation: 377

caffe python API for finetuning

I know the command to fine tuning caffe model is like this:

caffe train -solver examples/finetuning_on_flickr_style/solver.prototxt -weights models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel

Because of some environment reasons(caffe install in root, python env install in user, when I using caffe command will always using root python env), I cannot run caffe like this success, will get error like this:

 File "/home/projec/test.py", line 8, in <module>
    import caffe
  File "/opt/caffe_gpu/python/caffe/__init__.py", line 1, in <module>
    from .pycaffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, RMSPropSolver, AdaDeltaSolver, AdamSolver, NCCL, Timer
  File "/opt/caffe_gpu/python/caffe/pycaffe.py", line 11, in <module>
    import numpy as np
  File "/home/anaconda3/envs/python27/lib/python2.7/site-packages/numpy/__init__.py", line 142, in <module>
    from . import add_newdocs
  File "/home/anaconda3/envs/python27/lib/python2.7/site-packages/numpy/add_newdocs.py", line 13, in <module>
    from numpy.lib import add_newdoc
  File "/home/anaconda3/envs/python27/lib/python2.7/site-packages/numpy/lib/__init__.py", line 8, in <module>
    from .type_check import *
  File "/home/anaconda3/envs/python27/lib/python2.7/site-packages/numpy/lib/type_check.py", line 11, in <module>
    import numpy.core.numeric as _nx
  File "/home/anaconda3/envs/python27/lib/python2.7/site-packages/numpy/core/__init__.py", line 74, in <module>
    from numpy.testing.nosetester import _numpy_tester
  File "/home/anaconda3/envs/python27/lib/python2.7/site-packages/numpy/testing/__init__.py", line 10, in <module>
    from unittest import TestCase
  File "/home/anaconda3/envs/python27/lib/python2.7/unittest/__init__.py", line 64, in <module>
    from .main import TestProgram, main
  File "/home/anaconda3/envs/python27/lib/python2.7/unittest/main.py", line 7, in <module>
    from . import loader, runner
  File "/home/anaconda3/envs/python27/lib/python2.7/unittest/runner.py", line 7, in <module>
    from .signals import registerResult
  File "/home/anaconda3/envs/python27/lib/python2.7/unittest/signals.py", line 2, in <module>
    import weakref
  File "/home/anaconda3/envs/python27/lib/python2.7/weakref.py", line 13, in <module>
    from _weakref import _remove_dead_weakref

Actually, When I type “from _weakref import _remove_dead_weakref” will not have error in python2.7 env:

Python 2.7.14 |Anaconda, Inc.| (default, Nov 20 2017, 18:04:19) 
[GCC 7.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from _weakref import _remove_dead_weakref
>>> 

So, I using caffe in *.py file can train success,but I don't know what's the python API for "-weight" in the fine tuning command?The *.py file like this:

import caffe
import numpy as np

caffe.set_mode_gpu()
caffe.set_device(0)
solver= caffe.get_solver("/home/project/test.prototxt")
solver.solve()

Can someone tell me how to set "weight" parameter in *.py file?

Upvotes: 0

Views: 248

Answers (1)

Ilya Ovodov
Ilya Ovodov

Reputation: 383

import caffe
import numpy as np

caffe.set_mode_gpu()
caffe.set_device(0)
solver= caffe.get_solver("/home/project/test.prototxt")

weights = "/home/project/test.caffemodel"
solver.net.copy_from(weights);
for net in solver.test_nets:
    net.copy_from(weights)

solver.solve()

Upvotes: 2

Related Questions