-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathdemo_synthia.py
More file actions
108 lines (86 loc) · 3.49 KB
/
demo_synthia.py
File metadata and controls
108 lines (86 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import os
import sys
import random
import math
import numpy as np
import skimage.io
import matplotlib
import matplotlib.pyplot as plt
import coco
import utils
import model as modellib
import visualize
import torch
from config import Config
# Root directory of the project
ROOT_DIR = os.getcwd()
# Directory to save logs and trained model
MODEL_DIR = os.path.join(ROOT_DIR, "logs")
device=torch.device("cuda")
# Path to trained weights file
# Download this file and place in the root of your
# project (See README file for details)
#COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.pth")
# Directory of images to run detection on
IMAGE_DIR = os.path.join(ROOT_DIR, "images")
class synthiaConfig(Config):
"""Configuration for training on the toy shapes dataset.
Derives from the base Config class and overrides values specific
to the toy shapes dataset.
"""
# Give the configuration a recognizable name
NAME = "synthia"
# Train on 1 GPU and 8 images per GPU. We can put multiple images on each
# GPU because the images are small. Batch size is 8 (GPUs * images/GPU).
GPU_COUNT = 1
IMAGES_PER_GPU = 10
# Number of classes (including background)
NUM_CLASSES = 1 + 22 # background + 22 shapes
# Use small images for faster training. Set the limits of the small side
# the large side, and that determines the image shape.
#IMAGE_MIN_DIM = 512
#IMAGE_MAX_DIM = 768
IMAGE_MIN_DIM = 760
IMAGE_MAX_DIM = 1280
#MEAN_PIXEL = np.array([123.7, 116.8, 103.9,123.7, 116.8, 103.9])
# MEAN_PIXEL = np.array([123.7, 116.8, 103.9,1000])
# Use smaller anchors because our image and objects are small
# RPN_ANCHOR_SCALES = (8, 16, 32, 64, 128) # anchor side in pixels
# Reduce training ROIs per image because the images are small and have
# few objects. Aim to allow ROI sampling to pick 33% positive ROIs.
#TRAIN_ROIS_PER_IMAGE =
# Use a small epoch since the data is simple
STEPS_PER_EPOCH = 100
# use small validation steps since the epoch is small
VALIDATION_STEPS = 20
class InferenceConfig(synthiaConfig):
# Set batch size to 1 since we'll be running inference on
# one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU
GPU_COUNT = 1
IMAGES_PER_GPU = 1
#DETECTION_MIN_CONFIDENCE = 0
config = InferenceConfig()
config.display()
# Create model object.
model = modellib.MaskRCNN(model_dir=MODEL_DIR, config=config)
if config.GPU_COUNT:
model = model.cuda()
# Load weights trained on MS-COCO
#model.load_state_dict(torch.load(COCO_MODEL_PATH))
model_path = "/mnt/backup/jianyuan/pytorch-mask-rcnn/logs/synthia20180907T2148/mask_rcnn_synthia_0002.pth"
#model.find_last()[1]
model.load_weights(model_path)
# COCO Class names
# Index of the class in the list is its ID. For example, to get ID of
# the teddy bear class, use: class_names.index('teddy bear')
class_names = ["BG", "sky","Building","Road", "Sidewalk","Fence", "Vegetation","Pole", "Car","Traffic sign","Pedestrian","Bicycle","Motorcycle","Parking-slot" ,"Road-work","Traffic light","Terrain","Rider","Truck", "Bus", "Train", "Wall","Lanemarking"]
# Load a random image from the images folder
file_names = next(os.walk(IMAGE_DIR))[2]
image = skimage.io.imread(os.path.join(IMAGE_DIR, random.choice(file_names)))
# Run detection
results = model.detect([image],device)
# Visualize results
r = results[0]
visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'],
class_names, r['scores'])
plt.show()