jackson-banks jackson-banks -4 years ago 225
Python Question

Why Caffe refrense should go after sys in python?

I use Caffe framework and i use python language and have this code :

import numpy as np
import os
import matplotlib
import sys

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

caffe_root = '/home/jackson/caffe/' # this file should be run from {caffe_root}/examples (otherwise change this line)
sys.path.insert(0, caffe_root + 'python')

import caffe

model_def = caffe_root + 'models/bvlc_reference_caffenet/deploy.prototxt'
model_weights = caffe_root + 'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel'

net = caffe.Net(model_def, # defines the structure of the model
model_weights, # contains the trained weights
caffe.TEST) # use test mode (e.g., don't perform dropout)


the problem is, when i move import caffe before sys.path.insert(0, caffe_root + 'python') i get this error :


F0612 09:44:35.476567 5443 upgrade_proto.cpp:928] Check failed:
ReadProtoFromTextFile(param_file, param) Failed to parse NetParameter
file:
/home/jackson/caffe/models/bvlc_reference_caffenet/deploy.prototxt
* Check failure stack trace: * Aborted (core dumped)


Do Caffe and sys library have some sort of conflict ? Why did this happen? or it is because i change the module directory with sys.path ? (I should mention , without sys.path.insert the program would not run)

Answer Source
caffe_root = '/home/jackson/caffe/'  # this file should be run from {caffe_root}/examples (otherwise change this line)
sys.path.insert(0, caffe_root + 'python')

This code put your /home/jackson/caffe/ folder at the beginning of your python import path,when you move it after import caffe ,python looks for caffe in other folders in your PYTHONPATH to see if caffe is installed,from what i see,you might have another caffe installed somewhere that is not compatible with the deploy.prototxt file you are using

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download