我正在尝试将jpeg文件转换为'lmdb'格式。但我得到了这个错误:
>>> import lmdb
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "lmdb.py", line 25, in <module>
write_images_to_lmdb('/home/anaca/ferjpg' , 'fer2013')
File "lmdb.py", line 13, in write_images_to_lmdb
env = lmdb.Environment(db_name, map_size=map_size)
AttributeError: 'module' object has no attribute 'Environment'
我尝试的事情:
pip install lmdb
sudo apt-get install liblmdv-dev
以下是代码:
import caffe
import lmdb
import os
import numpy as np
import matplotlib.pyplot as plt
from caffe.proto import caffe_pb2
from caffe.io import datum_to_array, array_to_datum
def write_images_to_lmdb(img_dir, db_name):
for root, dirs, files in os.walk(img_dir, topdown = False):
if root != img_dir:
continue
map_size = 64*64*3*2*len(files)
env = lmdb.Environment(db_name, map_size=map_size)
txn = env.begin(write=True,buffers=True)
for idx, name in enumerate(files):
X = mp.imread(os.path.join(root, name))
y = 1
datum = array_to_datum(X,y)
str_id = '{:08}'.format(idx)
txn.put(str_id.encode('ascii'), datum.SerializeToString())
txn.commit()
env.close()
print " ".join(["Writing to", db_name, "done!"])
write_images_to_lmdb('/home/anaca/ferjpg' , 'fer2013')
答案 0 :(得分:1)
从您的回溯中,您尝试运行的脚本似乎名为lmdb.py
。将其名称更改为其他名称。
否则,当Python看到import lmdb
时,它假定您的脚本而不是您已安装的lmdb
模块。