如何在python中保存单词数据包?

时间:2017-09-29 10:31:08

标签: python machine-learning scikit-learn text-classification

这是我的代码:

sift=cv2.xfeatures2d.SIFT_create()
descriptors_unclustered=[]
dictionarysize=800
BOW=cv2.BOWKmeansTrainer(dictionarysize)
for p in training-paths :
    kp,dsc=sift.detectAndCompute(image,None)
    BOW.add(dsc)

dictionary=BOW.cluster()
bowdiction=cv2.BOWImgDescriptorExtractor(sift, cv2.BFMatcher(cv2.NORM_L2))
bowdiction.setvocabulary(dictionary)

我想保存此弓箭数据以便以后使用。我不想每次都等待这些计算,那么如何保存这些数据呢?

2 个答案:

答案 0 :(得分:1)

pickle用于此

  1. 将BOW保存到pickle:

    import pickle
    
    sift=cv2.xfeatures2d.SIFT_create()
    descriptors_unclustered=[]
    dictionarysize=800
    BOW=cv2.BOWKmeansTrainer(dictionarysize)
    for p in training-paths :
        kp,dsc=sift.detectAndCompute(image,None)
        BOW.add(dsc)
    
    with open('bow_pickle.pickle', 'wb') as f:
    pickle.dump(f)
    
  2. 从pickle返回数据:

    import pickle    
    with open('bow_pickle.pickle', 'rb') as f:
        BOW = pickle.load(f)    
    dictionary=BOW.cluster()
    bowdiction=cv2.BOWImgDescriptorExtractor(sift,cv2.BFMatcher(cv2.NORM_L2))
    bowdiction.setvocabulary(dictionary)
    

答案 1 :(得分:-1)

我知道这是旧的。但是当我来到这里并没有看到任何答案时,我尝试了一些方法并且这奏效了:

dictionary=BOW.cluster() 是最耗时的。所以你只需要保存字典。这只是一个 ndarray:

np.savetxt('bow_dict.txt', dictionary)

loaded_dictionary = np.loadtxt('bow_dict.txt')

然后继续

bowdiction=cv2.BOWImgDescriptorExtractor(sift,cv2.BFMatcher(cv2.NORM_L2))
bowdiction.setvocabulary(loaded_dictionary)