Created
May 16, 2021 04:41
-
-
Save NickosKal/91ed9239c3753cec086aae7df525df44 to your computer and use it in GitHub Desktop.
Help with an error
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hello everyone i run the following code: | |
.......................................... | |
from os import listdir | |
from xml.etree import ElementTree | |
from numpy import zeros | |
from numpy import asarray | |
from numpy import expand_dims | |
from numpy import mean | |
from mrcnn.config import Config | |
from mrcnn.model import MaskRCNN | |
from mrcnn.utils import Dataset | |
from mrcnn.utils import compute_ap | |
from mrcnn.model import load_image_gt | |
from mrcnn.model import mold_image | |
class BSTLD(Dataset): | |
def load_dataset(self, dataset_dir, is_train=True): | |
self.add_class("dataset", 1, "dataset1") | |
image_dir = dataset_dir + "/train/" | |
annotation_dir = dataset_dir + "/save/" | |
img_cnt = 0 | |
for image in listdir(image_dir): | |
img_cnt += 1 | |
image_id = image[:-4] | |
if is_train and img_cnt < 41: | |
continue | |
if not is_train and img_cnt > 41: | |
continue | |
img_path = image_dir + image | |
ann_path = annotation_dir + image_id + ".xml" | |
self.add_image('dataset', image_id=image_id, path=img_path, annotation=ann_path) | |
# extract bboxes | |
def extract_boxes(self, filename): | |
tree = ElementTree.parse(filename) | |
root = tree.getroot() | |
boxes = list() | |
for box in root.findall('.//bndbox'): | |
xmin = int(float(box.find('xmin').text)) | |
ymin = int(float(box.find('ymin').text)) | |
xmax = int(float(box.find('xmax').text)) | |
ymax = int(float(box.find('ymax').text)) | |
coors = [xmin, ymin, xmax, ymax] | |
boxes.append(coors) | |
width = int(root.find('.//size/width').text) | |
height = int(root.find('.//size/height').text) | |
return boxes, width, height | |
def load_mask(self, image_id): | |
info = self.image_info[image_id] | |
path = info['annotation'] | |
boxes, w, h = self.extract_boxes(path) | |
masks = zeros([h, w, len(boxes)], dtype='uint8') | |
class_ids = list() | |
for i in range(len(boxes)): | |
box = boxes[i] | |
row_s, row_e = box[1], box[3] | |
col_s, col_e = box[0], box[2] | |
masks[row_s:row_e, col_s:col_e, i] = 1 | |
class_ids.append(self.class_names.index('dataset1')) | |
return masks, asarray(class_ids, dtype='int32') | |
def image_reference(self, image_id): | |
info = self.image_info[image_id] | |
return info['path'] | |
class PredictionConfig(Config): | |
NAME = "BSTLD_cfg" | |
NUM_CLASSES = 1 + 1 | |
STEPS_PER_EPOCH = 174 | |
GPU_COUNT = 1 | |
IMAGES_PER_GPU = 1 | |
def evaluate_model(dataset, model, cfg): | |
APs = list() | |
for image_id in dataset.image_ids: | |
# load image, bboxes and masks | |
image, image_meta, gt_class_id, gt_box, gt_mask = load_image_gt(dataset, cfg, image_id, use_mini_mask=False) | |
# convert pixel values | |
scaled_image = mold_image(image, cfg) | |
# convert image into one sample | |
sample = expand_dims(scaled_image, 0) | |
# make prediction | |
yhat = model.detect(sample, verbose=0) | |
# extract results for first sample | |
r = yhat[0] | |
# calculate statistics, including AP | |
AP, _, _, _ = compute_ap(gt_box, gt_class_id, gt_mask, r["rois"], r["class_ids"], r["scores"], r["masks"]) | |
# store | |
APs.append(AP) | |
# calculate the mean AP across all images | |
mAP = mean(APs) | |
return mAP | |
# load the train dataset | |
train_set = BSTLD() | |
train_set.load_dataset('dataset1', is_train=True) | |
train_set.prepare() | |
print('Train: %d' % len(train_set.image_ids)) | |
# load test dataset | |
test_set = BSTLD() | |
test_set.load_dataset('dataset1', is_train=False) | |
test_set.prepare() | |
print('Test: %d' % len(test_set.image_ids)) | |
# create config | |
cfg = PredictionConfig() | |
# define the model | |
model = MaskRCNN(mode='inference', model_dir='./', config=cfg) | |
# load model weights | |
model.load_weights(r'C:\Users\Nickos_Kal\PycharmProjects\Traffic_Lights\bstld_cfg20210516T0208\mask_rcnn_bstld_cfg_0005.h5', by_name=True) | |
# evaluate model on training dataset | |
train_mAP = evaluate_model(train_set, model, cfg) | |
print("Train mAP: %.3f" % train_mAP) | |
# evaluate model on test dataset | |
test_mAP = evaluate_model(test_set, model, cfg) | |
print("Test mAP: %.3f" % test_mAP) | |
.................................... | |
And I get the following error: | |
.................................... | |
Traceback (most recent call last): | |
File "C:/Users/Nickos_Kal/PycharmProjects/Traffic_Lights/Evaluation.py", line 117, in <module> | |
train_mAP = evaluate_model(train_set, model, cfg) | |
File "C:/Users/Nickos_Kal/PycharmProjects/Traffic_Lights/Evaluation.py", line 88, in evaluate_model | |
AP, _, _, _ = compute_ap(gt_box, gt_class_id, gt_mask, r["rois"], r["class_ids"], r["scores"], r["masks"]) | |
File "C:\Users\Nickos_Kal\anaconda3\envs\tensorflow_env\lib\site-packages\mrcnn\utils.py", line 739, in compute_ap | |
iou_threshold) | |
File "C:\Users\Nickos_Kal\anaconda3\envs\tensorflow_env\lib\site-packages\mrcnn\utils.py", line 691, in compute_matches | |
overlaps = compute_overlaps_masks(pred_masks, gt_masks) | |
File "C:\Users\Nickos_Kal\anaconda3\envs\tensorflow_env\lib\site-packages\mrcnn\utils.py", line 108, in compute_overlaps_masks | |
masks2 = np.reshape(masks2 > .5, (-1, masks2.shape[-1])).astype(np.float32) | |
File "C:\Users\Nickos_Kal\anaconda3\envs\tensorflow_env\lib\site-packages\numpy\core\fromnumeric.py", line 292, in reshape | |
return _wrapfunc(a, 'reshape', newshape, order=order) | |
File "C:\Users\Nickos_Kal\anaconda3\envs\tensorflow_env\lib\site-packages\numpy\core\fromnumeric.py", line 56, in _wrapfunc | |
return getattr(obj, method)(*args, **kwds) | |
ValueError: cannot reshape array of size 0 into shape (0) | |
............................... | |
Any help would be appreciated thanks in advance |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment