解决python在import caffe时出现的no module name _caffe问题

来源:互联网 发布:广电网络梅江电话 编辑:程序博客网 时间:2024/06/18 10:57

在python文件的最上面放上下面内容就可以了

import sys

sys.path.append("/home/zhangqi/Desktop/caffe-master/python")

sys.path.append("/home/zhangqi/Desktop/caffe-master/python/caffe")


#############################################################################################

记得修改caffe_forward.py文件的路径

import syssys.path.append("/home/zhangqi/Desktop/caffe-master/python")sys.path.append("/home/zhangqi/Desktop/caffe-master/python/caffe")import caffeimport cv2 as cvimport matplotlib.pyplot as pltimport numpy as npmodel_defination = '/usr/xhh/model/general_prediction/cnn/forward_network.prototxt'weights = '/usr/xhh/model/general_prediction/cnn/train_iter_146000.caffemodel'data_path='/usr/xhh/model/general_prediction/cnn/current_position.txt'def load_net(list):    caffe.set_mode_cpu()    # net=caffe.Net(weights)    net=caffe.Net(model_defination,weights,caffe.TEST)    transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})    # transformer.set_transpose('data', (2, 0, 1))    # transformer.set_mean('data', ())     # Load the mean file    # transformer.set_raw_scale('data', 1)    # transformer.set_channel_swap('data', (2, 1, 0))                             # Convert RGB to GBR    net.blobs['data'].reshape(1, 3, 19, 19)    data=get_img_datum(data_path)    # net.blobs['data'].data[...] = transformer.preprocess('data', data)    net.blobs['data'].data[...] = data.reshape(1, 3, 19, 19)    res = net.forward()    res = np.asarray(res['loss'])    res=res[0]    if list[0]!='':        for i in list:            res[int(i)]=0    res=res.tolist()    a=sorted(res,reverse=True)    index=res.index(a[0])    #res = res.reshape((19, 19))    #plt.imshow(res, cmap= plt.cm.jet)    print index    for i in range(50):        print res.index(a[i])def get_img_datum(data_path):    file=open(data_path)    for line in file:        str=line.split()    img=np.zeros((3,19,19))    img1=np.zeros((19,19))    img2=np.zeros((19,19))    img3=np.zeros((19,19))    i=0    j=0    for s1 in str[0]:        img1[i,j]=int(s1)        i=i+1        if i>18:            i=0            j=j+1    i=0    j=0    for s2 in str[1]:        img2[i,j]=int(s2)        i=i+1        if i>18:            i=0            j=j+1    i=0    j=0    for s3 in str[2]:        img3[i,j]=int(s3)        i=i+1        if i>18:            i=0            j=j+1    img[0,:,:]=img1    img[1,:,:]=img2    img[2,:,:]=img3    return imgif __name__ == '__main__':    index=""    list=[]    jList=sys.argv[1:]    jList_str=""    for i in jList:        jList_str+=i    jList_str=jList_str[1:len(jList_str)-1]    list=jList_str.split(',')    load_net(list)



0 0
原创粉丝点击