其他分享
首页 > 其他分享> > 如何使用Spark和Caffe对图像进行分类

如何使用Spark和Caffe对图像进行分类

作者:互联网

我正在使用Caffe进行图像分类,我可以使用MAC OS X,Pyhton.

现在我知道如何使用Caffe和Spark python对图像列表进行分类,但如果我想让它更快,我想使用Spark.

因此,我尝试在RDD的每个元素上应用图像分类,RDD是从image_path列表创建的.但是,Spark不允许我这样做.

这是我的代码:

这是图像分类的代码:

# display image name, class number, predicted label
def classify_image(image_path, transformer, net):
    image = caffe.io.load_image(image_path)
    transformed_image = transformer.preprocess('data', image)
    net.blobs['data'].data[...] = transformed_image
    output = net.forward()
    output_prob = output['prob'][0]
    pred = output_prob.argmax()

    labels_file = caffe_root + 'data/ilsvrc12/synset_words.txt'
    labels = np.loadtxt(labels_file, str, delimiter='\t')
    lb = labels[pred]

    image_name = image_path.split(images_folder_path)[1]

    result_str = 'image: '+image_name+'  prediction: '+str(pred)+'  label: '+lb
    return result_str

这个代码生成Caffe参数并在RDD的每个元素上应用classify_image方法:

def main():
    sys.path.insert(0, caffe_root + 'python')
    caffe.set_mode_cpu()
    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,
                model_weights,
                caffe.TEST)

    mu = np.load(caffe_root + 'python/caffe/imagenet/ilsvrc_2012_mean.npy')
    mu = mu.mean(1).mean(1)

    transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})

    transformer.set_transpose('data', (2,0,1))
    transformer.set_mean('data', mu)
    transformer.set_raw_scale('data', 255)
    transformer.set_channel_swap('data', (2,1,0))

    net.blobs['data'].reshape(50,
                          3,
                          227, 227)

    image_list= []
    for image_path in glob.glob(images_folder_path+'*.jpg'):
        image_list.append(image_path)

    images_rdd = sc.parallelize(image_list)
    transformer_bc = sc.broadcast(transformer)
    net_bc = sc.broadcast(net)
    image_predictions = images_rdd.map(lambda image_path: classify_image(image_path, transformer_bc, net_bc))
    print image_predictions

if __name__ == '__main__':
    main()

如您所见,这里我尝试广播caffe参数,transformer_bc = sc.broadcast(transformer),net_bc = sc.broadcast(net)
错误是:

RuntimeError: Pickling of “caffe._caffe.Net” instances is not enabled

在我做广播之前,错误是:

Driver stacktrace…. Caused by: org.apache.spark.api.python.PythonException: Traceback (most recent call last):….

所以,你知道吗,有没有什么方法可以用Caffe和Spark对图像进行分类,还可以利用Spark?

解决方法:

当您使用复杂的非本机对象时,初始化必须直接移动到worker,例如使用singleton模块:

net_builder.py:

import cafe 

net = None

def build_net(*args, **kwargs):
     ...  # Initialize net here
     return net       

def get_net(*args, **kwargs):
    global net
    if net is None:
        net = build_net(*args, **kwargs)
    return net

main.py:

import net_builder

sc.addPyFile("net_builder.py")

def classify_image(image_path, transformer, *args, **kwargs):
    net = net_builder.get_net(*args, **kwargs)

这意味着您还必须分发所有必需的文件.它可以手动或using SparkFiles机制完成.

另外,您应该看看SparkNet包装.

标签:python,apache-spark,rdd,pyspark,pycaffe
来源: https://codeday.me/bug/20190710/1428810.html