caffe-tensorflow
caffe-tensorflow copied to clipboard
ImportError: No module named kaffe.tensorflow
With this command:
convert.py --caffemodel ./VGG_ILSVRC_19_layers.caffemodel --data-output-path ./TF_conv/dataOutput.npz --code-output-path ./TF_conv/codeOutput.py ./VGG_ILSVRC_19_layers_deploy.prototxt
I get this code output file:
from kaffe.tensorflow import Network
class VGG_ILSVRC_19_layers(Network):
def setup(self):
(self.feed('data')
.conv(3, 3, 64, 1, 1, name='conv1_1')
.conv(3, 3, 64, 1, 1, name='conv1_2')
.max_pool(2, 2, 2, 2, name='pool1')
.conv(3, 3, 128, 1, 1, name='conv2_1')
.conv(3, 3, 128, 1, 1, name='conv2_2')
.max_pool(2, 2, 2, 2, name='pool2')
.conv(3, 3, 256, 1, 1, name='conv3_1')
.conv(3, 3, 256, 1, 1, name='conv3_2')
.conv(3, 3, 256, 1, 1, name='conv3_3')
.conv(3, 3, 256, 1, 1, name='conv3_4')
.max_pool(2, 2, 2, 2, name='pool3')
.conv(3, 3, 512, 1, 1, name='conv4_1')
.conv(3, 3, 512, 1, 1, name='conv4_2')
.conv(3, 3, 512, 1, 1, name='conv4_3')
.conv(3, 3, 512, 1, 1, name='conv4_4')
.max_pool(2, 2, 2, 2, name='pool4')
.conv(3, 3, 512, 1, 1, name='conv5_1')
.conv(3, 3, 512, 1, 1, name='conv5_2')
.conv(3, 3, 512, 1, 1, name='conv5_3')
.conv(3, 3, 512, 1, 1, name='conv5_4')
.max_pool(2, 2, 2, 2, name='pool5')
.fc(4096, name='fc6')
.fc(4096, name='fc7')
.fc(1000, relu=False, name='fc8')
.softmax(name='prob'))
With: python ./codeOutput.py
I get this error:
Traceback (most recent call last):
File "./codeOutput.py", line 1, in <module>
from kaffe.tensorflow import Network
ImportError: No module named kaffe.tensorflow
Do I have to put the codeOutput.py file in the directory where the kaffe.tensorflow module is?
Do I have to copy the kaffe.tensorflow module where my file is?
Can I connect the two in some way?
If you want to use your converted model.py in any other location you have to copy the network.py or adapt your python path.
I usually copy the network.py (from kaffe/tensorflow) to the project directory and adapt the import line in the model.py
@kratzert - I copied the network.py file in the directory where codeOutput.py is, but I still get the same error.
Does your model.py correspond to my codeOutput.py?
What do you mean with "adapt the import line in the model.py"? Should its path be specified in the file?
@pietrom16 yes my model.py looks the same.
Let's say you want to use your converted model and weights in any other project located and the folder exampleDir, then you could copy the model.py and the weights.npy and the network.py to this folder, change the import line to from network import Network and thats it.
If you then want to load in a script, lets say example_script.py, it could look the following:
from model import MODELNAME #class name of the model here, for you VGG_ILSVRC_19_layers
import tensorflow as tf
from skimage.io import imread
batch_size = 1
weight_file = 'weights.npy'
img = imread(/path/to/img)
x = tf.placeholder(tf.float32, [batch_size, 224, 224, 3])
model = MODELNAME({'data': x})
with tf.Session() as sess:
model.load(weights_file, sess)
batch = img.reshape((1,224,224,3))
output = sess.run(model.get_output(), feed_dict={x: batch})
@kratzert I am not sure why, your method didn't work for me. What worked for me instead is this
import sys
sys.path.append(path_to_root_of_this_repo)
import kaffe.tensorflow
Equivalently, one can add the path to root of the repo to bashrc file, and then use import kaffe.tensorflow