paddle paddle ssd 多图片预测
武林風灬 发布于2019-12 浏览:1383 回复:2
0
收藏

#!/usr/bin/env python
#coding=utf-8
import sys
import cv2
import signal
import io
import numpy as np
import os
import urllib
import time
import numpy as np
import argparse
import functools
import matplotlib.pyplot as plt
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import paddle
import paddle.fluid as fluid
import reader
import re
import base64
from io import BytesIO
from mobilenet_ssd import build_mobilenet_ssd
from utility import add_arguments, print_arguments, check_cuda
rf = None
f = None
global i
parser = argparse.ArgumentParser(description=__doc__)
add_arg = functools.partial(add_arguments, argparser=parser)
# yapf: disable
add_arg('dataset', str, 'pascalvoc', "coco and pascalvoc.")
add_arg('use_gpu', bool, False, "Whether use GPU.")
add_arg('image_path', str, '', "The image used to inference and visualize.")
add_arg('model_dir', str, 'model/best_model', "The model path.")
add_arg('nms_threshold', float, 0.45, "NMS threshold.")
add_arg('confs_threshold', float, 0.5, "Confidence threshold to draw bbox.")
add_arg('resize_h', int, 300, "The resized image height.")
add_arg('resize_w', int, 300, "The resized image height.")
add_arg('mean_value_B', float, 127.5, "Mean value for B channel which will be subtracted.") #123.68
add_arg('mean_value_G', float, 127.5, "Mean value for G channel which will be subtracted.") #116.78
add_arg('mean_value_R', float, 127.5, "Mean value for R channel which will be subtracted.") #103.94
# yapf: enable

class receive:
def __init__(self):
read_path = "/tmp/server_in.pipe"
write_path = "/tmp/server_out.pipe"
#image=cv2.imread("/home/ros/0.jpg")
try:
os.mkfifo(read_path)
os.mkfifo(write_path)
except OSError as e:
print("mkfifo error:", e)
global rf
global f

rf = os.open(read_path, os.O_RDONLY)
f = os.open(write_path, os.O_SYNC | os.O_CREAT | os.O_RDWR)
def load(self):
global rf
global f
s = os.read(rf, 1000000)

#print(len(s))


image=np.fromstring(s,np.uint8)

os.write(f, bytes("4asdafa",'UTF-8'))
return image
def infer(args, data_args, image_find, model_dir):

image_shape = [3, 300,300]
image_find=Image.open(image_find)
#print(image_find.size())
# if image_find.any():
#image_find=Image.fromarray(image_find) data_args.resize_w
'''
if 'coco' in data_args.dataset:
num_classes = 91

# cocoapi
from pycocotools.coco import COCO
from pycocotools.cocoeval import COCOeval
label_fpath = os.path.join(data_dir, label_file)
coco = COCO(label_fpath)
category_ids = coco.getCatIds()
label_list = {
item['id']: item['name']
for item in coco.loadCats(category_ids)
}
label_list[0] = ['background']
'''
#if 'pascalvoc' in data_args.dataset:
num_classes = 21
label_list = data_args.label_list

image = fluid.layers.data(name='image', shape=image_shape, dtype='float32')

#print(image)
locs, confs, box, box_var = build_mobilenet_ssd(image, num_classes,image_shape)
nmsed_out = fluid.layers.detection_output(locs, confs, box, box_var, 0.45)
#print(nmsed_out)
place = fluid.CUDAPlace(0) if args.use_gpu else fluid.CPUPlace()
exe = fluid.Executor(place)
# yapf: disable
if model_dir:
def if_exist(var):
return os.path.exists(os.path.join(model_dir, var.name))
fluid.io.load_vars(exe, model_dir, predicate=if_exist)
infer_reader = reader.infer(data_args, image_find)

feeder = fluid.DataFeeder(place=place, feed_list=[image])
#print(feeder)
data = infer_reader()
#print(data)

# switch network to test mode (i.e. batch norm test mode)return_numpy=False
test_program = fluid.default_main_program().clone(for_test=True)
nmsed_out_v, = exe.run(test_program,feed=feeder.feed([[data]]),fetch_list=[nmsed_out],return_numpy=False)
#nmsed_out_v=[]
nmsed_out_v = np.array(nmsed_out_v)
#image_path=Image.fromarray(image_find)
pic=draw_bounding_box_on_image(image_find, nmsed_out_v, args.confs_threshold,label_list)
image_shape=[]

#draw_bounding_box_on_image(image_path, nmsed_out_v, args.confs_threshold,label_list)
return pic


def draw_bounding_box_on_image(image_find, nms_out, confs_threshold,label_list):

#cv2.namedWindow("image", cv2.WINDOW_AUTOSIZE)
#image =Image.fromarray(image_find)
image=image_find
draw = ImageDraw.Draw(image)
im_width, im_height = image.size

for dt in nms_out:

if dt[1] < confs_threshold:
continue
category_id = dt[0]
bbox = dt[2:]
xmin, ymin, xmax, ymax = clip_bbox(dt[2:])
(left, right, top, bottom) = (xmin * im_width, xmax * im_width,
ymin * im_height, ymax * im_height)
draw.line([(left, top), (left, bottom), (right, bottom), (right, top),(left, top)],width=4,fill='red')
draw.text((left, top), label_list[int(category_id)], (255, 255, 0))
img = cv2.cvtColor(np.asarray(image), cv2.COLOR_RGB2BGR)

#plt.imshow(image)
#plt.show()
#cv2.imshow("image",img)
#cv2.waitKey(10)
return img

def clip_bbox(bbox):
xmin = max(min(bbox[0], 1.), 0.)
ymin = max(min(bbox[1], 1.), 0.)
xmax = max(min(bbox[2], 1.), 0.)
ymax = max(min(bbox[3], 1.), 0.)
return xmin, ymin, xmax, ymax
def quit(signum, frame):
print("exit")
cv2.destroyAllWindows()
os.close(rf)
os.close(f)
sys.exit()
class receive:
def __init__(self):
read_path = "/tmp/server_in.pipe"
write_path = "/tmp/server_out.pipe"
#image=cv2.imread("/home/ros/0.jpg")
try:
os.mkfifo(read_path)
os.mkfifo(write_path)
except OSError as e:
print("mkfifo error:", e)
global rf
global f

rf = os.open(read_path, os.O_RDONLY)
f = os.open(write_path, os.O_SYNC | os.O_CREAT | os.O_RDWR)
def load(self):
global rf
global f
s = os.read(rf, 1000000)
image=np.fromstring(s,np.uint8)
if len(s)<1:
#print(len(s))


image=[]
#image=Image.open(io.BytesIO(s))
os.write(f, bytes("4asdafa",'UTF-8'))
return image

def main(args):
cv2.namedWindow("image", cv2.WINDOW_AUTOSIZE)
signal.signal(signal.SIGINT, quit)
signal.signal(signal.SIGTERM, quit)
#bool signalp
#bb=receive()
global i

#while True:
for i in range(2):
if i==0:


pic1='/home/ros/ssd/data/pascalvoc/VOCdevkit/VOC2007/JPEGImages/000022.jpg'
if i==1:

pic1='/home/ros/ssd/data/pascalvoc/VOCdevkit/VOC2007/JPEGImages/000025.jpg'
args = parser.parse_args()
print_arguments(args)

check_cuda(args.use_gpu)

data_dir = 'data/pascalvoc'
label_file = 'label_list'

if not os.path.exists(args.model_dir):
raise ValueError("The model path [%s] does not exist." % (args.model_dir))

data_args = reader.Settings(
dataset=args.dataset,
data_dir=data_dir,
label_file=label_file,
resize_h=args.resize_h,
resize_w=args.resize_w,
mean_value=[args.mean_value_B, args.mean_value_G, args.mean_value_R],
apply_distort=False,
apply_expand=False,
ap_version='')
pic=infer(
args,
data_args=data_args,
image_find=pic1,
model_dir=args.model_dir)
cv2.imshow("image",pic)
cv2.waitKey(10)


if __name__ == '__main__':
#cv2.namedWindow("image", cv2.WINDOW_AUTOSIZE)
main(sys.argv)

只能执行一次,第二次到标红色的地方就报错;

报错如下

/home/ros/.local/lib/python3.6/site-packages/paddle/fluid/executor.py:779: UserWarning: The following exception is not an EOF exception.
"The following exception is not an EOF exception.")
Traceback (most recent call last):
File "b.py", line 260, in
main(sys.argv)
File "b.py", line 247, in main
model_dir=args.model_dir)
File "b.py", line 121, in infer
nmsed_out_v, = exe.run(test_program,feed=feeder.feed([[data]]),fetch_list=[nmsed_out],return_numpy=False)
File "/home/ros/.local/lib/python3.6/site-packages/paddle/fluid/executor.py", line 780, in run
six.reraise(*sys.exc_info())
File "/home/ros/.local/lib/python3.6/site-packages/six.py", line 696, in reraise
raise value
File "/home/ros/.local/lib/python3.6/site-packages/paddle/fluid/executor.py", line 775, in run
use_program_cache=use_program_cache)
File "/home/ros/.local/lib/python3.6/site-packages/paddle/fluid/executor.py", line 822, in _run_impl
use_program_cache=use_program_cache)
File "/home/ros/.local/lib/python3.6/site-packages/paddle/fluid/executor.py", line 899, in _run_program
fetch_var_name)
paddle.fluid.core_avx.EnforceNotMet:

--------------------------------------------
C++ Call Stacks (More useful to developers):
--------------------------------------------
0 std::string paddle::platform::GetTraceBackString(std::string const&, char const*, int)
1 paddle::platform::EnforceNotMet::EnforceNotMet(std::string const&, char const*, int)
2 paddle::framework::Tensor::type() const
3 paddle::operators::ConvOp::GetExpectedKernelType(paddle::framework::ExecutionContext const&) const
4 paddle::framework::OperatorWithKernel::ChooseKernel(paddle::framework::RuntimeContext const&, paddle::framework::Scope const&, paddle::platform::Place const&) const
5 paddle::framework::OperatorWithKernel::RunImpl(paddle::framework::Scope const&, paddle::platform::Place const&, paddle::framework::RuntimeContext*) const
6 paddle::framework::OperatorWithKernel::RunImpl(paddle::framework::Scope const&, paddle::platform::Place const&) const
7 paddle::framework::OperatorBase::Run(paddle::framework::Scope const&, paddle::platform::Place const&)
8 paddle::framework::Executor::RunPreparedContext(paddle::framework::ExecutorPrepareContext*, paddle::framework::Scope*, bool, bool, bool)
9 paddle::framework::Executor::Run(paddle::framework::ProgramDesc const&, paddle::framework::Scope*, int, bool, bool, std::vector > const&, bool)

------------------------------------------
Python Call Stacks (More useful to users):
------------------------------------------
File "/home/ros/.local/lib/python3.6/site-packages/paddle/fluid/framework.py", line 2488, in append_op
attrs=kwargs.get("attrs", None))
File "/home/ros/.local/lib/python3.6/site-packages/paddle/fluid/layer_helper.py", line 43, in append_op
return self.main_program.current_block().append_op(*args, **kwargs)
File "/home/ros/.local/lib/python3.6/site-packages/paddle/fluid/layers/nn.py", line 2803, in conv2d
"data_format": data_format,
File "/home/ros/ssd/mobilenet_ssd.py", line 92, in conv_bn
bias_attr=False)
File "/home/ros/ssd/mobilenet_ssd.py", line 27, in ssd_net
tmp = self.conv_bn(self.img, 3, int(32 * scale), 2, 1)
File "/home/ros/ssd/mobilenet_ssd.py", line 137, in build_mobilenet_ssd
return ssd_model.ssd_net()
File "b.py", line 102, in infer
locs, confs, box, box_var = build_mobilenet_ssd(image, num_classes,image_shape)
File "b.py", line 247, in main
model_dir=args.model_dir)
File "b.py", line 260, in
main(sys.argv)

----------------------
Error Message Summary:
----------------------
Error: Tensor not initialized yet when Tensor::type() is called.
[Hint: holder_ should not be null.] at (/paddle/paddle/fluid/framework/tensor.h:139)
[operator < conv2d > error]

 

收藏
点赞
0
个赞
共2条回复 最后由用户已被禁言回复于2022-04
#3武林風灬回复于2020-01

好的 谢谢。

 

0
#2greeny730回复于2019-12

可以在对应的帖子下提问,会有同学解答

0
TOP
切换版块