Прогнозирование объекта по предварительно обученной модели не работаетPython

Программы на Python
Anonymous
Прогнозирование объекта по предварительно обученной модели не работает

Сообщение Anonymous »

Я хочу предсказать объект, используя в качестве входных данных изображение, и хочу, чтобы моя модель могла предсказывать метку. Я обучил модель с использованием тензорного потока на основе аннотированной базы данных, где целевой объект для прогнозирования был добавлен к предварительно обученной модели. Я использую следующий код, в котором я устанавливаю изображение целевого объекта в качестве входных данных и хочу получить результат прогнозирования:

Код: Выделить всё

class MultiObjectDetection():

def __init__(self, classes_name):

self._classes_name = classes_name
self._num_classes = len(classes_name)

self._common_params = {'image_size': 448, 'num_classes': self._num_classes,
'batch_size':1}
self._net_params = {'cell_size': 7, 'boxes_per_cell':2, 'weight_decay': 0.0005}
self._net = YoloTinyNet(self._common_params, self._net_params, test=True)

def predict_object(self, image):
predicts = self._net.inference(image)
return predicts

def process_predicts(self, resized_img, predicts, thresh=0.2):
"""
process the predicts of object detection with one image input.

Args:
resized_img: resized source image.
predicts: output of the model.
thresh: thresh of bounding box confidence.
Return:
predicts_dict: {"stick": [[x1, y1, x2, y2, scores1], [...]]}.
"""
cls_num = self._num_classes
bbx_per_cell = self._net_params["boxes_per_cell"]
cell_size = self._net_params["cell_size"]
img_size = self._common_params["image_size"]
p_classes = predicts[0, :, :, 0:cls_num]
C = predicts[0, :, :, cls_num:cls_num+bbx_per_cell] # two bounding boxes in one cell.
coordinate = predicts[0, :, :, cls_num+bbx_per_cell:] # all bounding boxes position.

p_classes = np.reshape(p_classes, (cell_size, cell_size, 1, cls_num))
C = np.reshape(C, (cell_size, cell_size, bbx_per_cell, 1))

P = C * p_classes # confidencefor all classes of all bounding boxes (cell_size, cell_size, bounding_box_num, class_num) = (7, 7, 2, 1).

predicts_dict = {}
for i in range(cell_size):
for j in range(cell_size):
temp_data = np.zeros_like(P, np.float32)
temp_data[i, j, :, :] = P[i, j, :, :]
position = np.argmax(temp_data) # refer to the class num (with maximum confidence) for every bounding box.
index = np.unravel_index(position, P.shape)

if P[index] > thresh:
class_num = index[-1]
coordinate = np.reshape(coordinate, (cell_size, cell_size, bbx_per_cell, 4)) # (cell_size, cell_size, bbox_num_per_cell, coordinate)[xmin, ymin, xmax, ymax]
max_coordinate = coordinate[index[0], index[1], index[2], :]

xcenter = max_coordinate[0]
ycenter = max_coordinate[1]
w = max_coordinate[2]
h = max_coordinate[3]

xcenter = (index[1] + xcenter) * (1.0*img_size /cell_size)
ycenter = (index[0] + ycenter) * (1.0*img_size /cell_size)

w = w * img_size
h = h * img_size
xmin = 0 if (xcenter - w/2.0 < 0) else (xcenter - w/2.0)
ymin = 0 if (xcenter - w/2.0 < 0) else (ycenter - h/2.0)
xmax = resized_img.shape[0] if (xmin + w) > resized_img.shape[0] else (xmin + w)
ymax = resized_img.shape[1] if (ymin + h) >  resized_img.shape[1] else (ymin + h)

class_name = self._classes_name[class_num]
predicts_dict.setdefault(class_name, [])
predicts_dict[class_name].append([int(xmin), int(ymin), int(xmax), int(ymax), P[index]])

return predicts_dict

def non_max_suppress(self, predicts_dict, threshold=0.5):
"""
implement non-maximum supression on predict bounding boxes.
Args:
predicts_dict: {"stick": [[x1, y1, x2, y2, scores1], [...]]}.
threshhold: iou threshold
Return:
predicts_dict processed by non-maximum suppression
"""
for object_name, bbox in predicts_dict.items():
bbox_array = np.array(bbox, dtype=np.float)
x1, y1, x2, y2, scores = bbox_array[:,0], bbox_array[:,1], bbox_array[:,2], bbox_array[:,3], bbox_array[:,4]
areas = (x2-x1+1) * (y2-y1+1)
order = scores.argsort()[::-1]
keep = []
while order.size >  0:
i = order[0]
keep.append(i)
xx1 = np.maximum(x1[i], x1[order[1:]])
yy1 = np.maximum(y1[i], y1[order[1:]])
xx2 = np.minimum(x2[i], x2[order[1:]])
yy2 = np.minimum(y2[i], y2[order[1:]])
inter = np.maximum(0.0, xx2-xx1+1) * np.maximum(0.0, yy2-yy1+1)
iou = inter/(areas[i]+areas[order[1:]]-inter)
indexs = np.where(iou

Подробнее здесь: [url]https://stackoverflow.com/questions/78809007/predicting-an-object-over-an-pretrained-model-is-not-working[/url]

Вернуться в «Python»