ipython并行推送类对象(TypeError:无法腌制_thread.RLock对象)

时间:2019-02-01 15:50:01

标签: parallel-processing ipython

使用tensorflow对象检测API尝试在批量图像中检测(推断)对象,因此尝试使用ipython ipcluster并行处理。

但是无法将自定义类对象推送到所有引擎并出现酸洗错误。

TypeError:无法腌制_thread.RLock对象 要么 TypeError:无法腌制SwigPyObject对象

class ObjectDetector(object):

  def __init__(self):
    from utils import label_map_util 
    PATH_TO_LABELS = '/tensorflow/models/label_map.pbtxt'
    self.detection_graph = self._build_graph()
    self.sess = tensorflow.Session(graph=self.detection_graph)

    label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
    categories = label_map_util.convert_label_map_to_categories(
        label_map, max_num_classes=1, use_display_name=True)
    self.category_index = label_map_util.create_category_index(categories)

  def _build_graph(self):
    PATH_TO_CKPT = '/tensorflow/models/frozen_inference_graph.pb'
    detection_graph = tensorflow.Graph()
    with detection_graph.as_default():
      od_graph_def = tensorflow.GraphDef()
      with tensorflow.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
        serialized_graph = fid.read()
        od_graph_def.ParseFromString(serialized_graph)
        tensorflow.import_graph_def(od_graph_def, name='')

    return detection_graph

  def _load_image_into_numpy_array(self, image):
    (im_width, im_height) = image.size
    return numpy.array(image.getdata()).reshape(
        (im_height, im_width, 3)).astype(numpy.uint8)

  def detect(self, image):
    image_np = self._load_image_into_numpy_array(image)
    image_np_expanded = numpy.expand_dims(image_np, axis=0)

    graph = self.detection_graph
        image_tensor = graph.get_tensor_by_name('image_tensor:0')
        boxes = graph.get_tensor_by_name('detection_boxes:0')
        scores = graph.get_tensor_by_name('detection_scores:0')
        classes = graph.get_tensor_by_name('detection_classes:0')
        num_detections = graph.get_tensor_by_name('num_detections:0')


        (boxes, scores, classes, num_detections) = self.sess.run(
            [boxes, scores, classes, num_detections],
            feed_dict={image_tensor: image_np_expanded})

        boxes, scores, classes, num_detections = map(
            numpy.squeeze, [boxes, scores, classes, num_detections])

        return boxes, scores, classes.astype(int), num_detections



    all_engines[ObjectDetector] = ObjectDetector
    client = ObjectDetector()
    all_engines['client'] = client

errrorTypeError:无法腌制_thread.RLock对象

是否有可能将tensorflow代码作为类推入所有引擎。 如果是,则提供任何示例或指导

强文本

0 个答案:

没有答案