Skip to content

Commit 0081095

Browse files
committed
Corrected number of classes
1 parent 4f588d1 commit 0081095

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

load_test_maskrcnn.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from models import get_model_instance_segmentation
1010
from misc_utils import get_dataloaders, sample_images
1111
import torch
12+
from draw_image import instance_segmentation_api
1213
# from PIL import Image
1314
import argparse
1415

@@ -19,26 +20,33 @@
1920
opt = parser.parse_args()
2021

2122
opt.model_name='maskrcnn_250.pth'
22-
opt.path2model= 'C:/MyPrograms/t-HPC-results/mask_rcnn-Mar-30-at-3-18/'
23+
#opt.path2model= 'C:/MyPrograms/t-HPC-results/mask_rcnn-Mar-30-at-3-18/'
24+
# opt.path2model ='C:/MyPrograms/t-HPC-results/mrcn_no_bkgrnd/'
25+
opt.path2model ='C:/MyPrograms/t-HPC-results/mrcnn_prsn_detect/'
2326
opt.HPC_run = 0
24-
opt.remove_background = 1
25-
opt.person_detection = 0
27+
opt.remove_background = False
28+
opt.person_detection = True
2629
opt.train_percentage = 0.5
2730
opt.batch_size = 1
28-
opt.train_shuffle = 0
31+
opt.train_shuffle = False
2932
opt.n_cpu=0
3033
opt.cuda = False # this will definetly work on the cpu if it is false
3134

35+
folder_name = '/my'
36+
image_name = 'myimage'
37+
38+
image_path = folder_name+image_name
39+
40+
3241
device = torch.device('cuda' if opt.cuda else 'cpu')
33-
model = get_model_instance_segmentation( number_of_classes(opt.dataset_name) )
42+
43+
model = get_model_instance_segmentation( number_of_classes(opt) )
3444
print("loading model", opt.model_name )
3545
model.load_state_dict(torch.load(opt.path2model+opt.model_name, map_location=device ))
3646
model.eval()
37-
data_loader, data_loader_test = get_dataloaders(opt)
47+
instance_segmentation_api(model, folder_name+image_name)
48+
3849

39-
for i in range(3): # range( len(data_loader_test)): # let's just check a couple of images
40-
images, targets = next(iter(data_loader_test)) # get image(s)
41-
sample_images(images, targets, model, device)
4250

4351

4452

misc_utils.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,28 @@
1313
import utils
1414

1515

16-
def sample_images(images, targets, model, device):
16+
def sample_images(images, targets, model, device, number_of_classes):
1717
# Image.fromarray(255*targets[0]['masks'].squeeze(0).numpy()).show()
1818
images = list(image.to(device) for image in images)
1919
model.eval() # setting model to evaluation mode
2020
with torch.no_grad():
2121
predictions = model(images) # Returns predictions
2222
masks = predictions[0]['masks'].cpu().squeeze(1)
2323
labels = predictions[0]['labels'].cpu()
24+
scores = predictions[0]['scores'].cpu() # scores are already sorted
2425
model.train() # putting back the model into train status/mode
2526

2627

2728
print(labels)
2829
print(targets[0]['labels'])
2930
print('-------------####-----------')
3031

31-
# for i in range(len(labels)): # we have one label for each mask
32-
# Image.fromarray( 255*masks[i].numpy().round() ).show()
33-
# print(labels[i])
34-
# to_pil = transforms.ToPILImage()
35-
# to_pil(images[0]).show()
32+
for i in range(number_of_classes-1): # we have one label for each mask
33+
Image.fromarray( 255*masks[i].numpy().round() ).show()
34+
print('label:', labels[i], ', score:', scores[i])
35+
to_pil = transforms.ToPILImage()
36+
to_pil(images[0].cpu()).show()
37+
3638

3739
def get_dataloaders(opt):
3840
# Configure dataloaders

0 commit comments

Comments
 (0)