forked from jremillard/images-to-osm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
osmmodelconfig.py
118 lines (94 loc) · 3.56 KB
/
osmmodelconfig.py
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
109
110
111
112
113
114
115
116
117
118
import sys
sys.path.append("Mask_RCNN")
import os
import random
import math
import re
import time
import numpy as np
import cv2
import matplotlib
import random
import glob
import skimage
from config import Config
import imagestoosm.config as osmcfg
import utils
import model as modellib
import visualize
from model import log
featureNames = {
"baseball":1,
"basketball":2,
"tennis":3
# "american_football":4,
# "soccer":5,
}
class OsmModelConfig(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 = "OSM Images Baseball,Basketball,Tennis"
# Batch size is (GPUs * images/GPU).
GPU_COUNT = 1
IMAGES_PER_GPU = 2
LEARNING_RATE = 0.001
# 2 minutes
#STEPS_PER_EPOCH = 100 // IMAGES_PER_GPU
# 1 hour epoch
STEPS_PER_EPOCH = 12000 // IMAGES_PER_GPU
# Number of classes (including background)
NUM_CLASSES = 1 + len(featureNames) # background + featureType's
# Each tile is 256 pixels across, training data is 3x3 tiles
TILES=3
IMAGE_MIN_DIM = 256*TILES
IMAGE_MAX_DIM = 256*TILES
MINI_MASK_SHAPE = (128, 128)
#MASK_SHAPE = (IMAGE_MIN_DIM, IMAGE_MIN_DIM)
# 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 = 64
#DETECTION_MAX_INSTANCES = 64
VALIDATION_STEPS = 100
class OsmImagesDataset(utils.Dataset):
def __init__(self, rootDir):
utils.Dataset.__init__(self)
self.ROOT_DIR = rootDir
def load(self, imageDirs, height, width):
"""Generate the requested number of synthetic images.
count: number of images to generate.
height, width: the size of the generated images.
"""
for feature in featureNames:
self.add_class("osm", featureNames[feature],feature)
# Add images
for i in range(len(imageDirs)):
imgPath = os.path.join( self.ROOT_DIR, osmcfg.trainDir,imageDirs[i],imageDirs[i] + ".jpg")
self.add_image("osm", image_id=imageDirs[i], path=imgPath, width=width, height=height)
def load_mask(self, image_id):
"""Generate instance masks for shapes of the given image ID.
"""
info = self.image_info[image_id]
imgDir = os.path.join( self.ROOT_DIR, osmcfg.trainDir,info['id'])
wildcard = os.path.join( imgDir,"*.png")
# 00015-american_football-0.png 00015-baseball-0.png 00015-baseball-1.png 00015-baseball-2.png 00015-baseball-3.png 00015-basketball-0.png 00015-basketball-1.png 00015.jpg 00015.txt
maskCount = 0
for filePath in glob.glob(wildcard):
filename = os.path.split(filePath)[1]
parts = filename.split( "-")
if ( len(parts) == 3) and parts[1] in featureNames:
maskCount += 1
mask = np.zeros([info['height'], info['width'], maskCount], dtype=np.uint8)
class_ids = np.zeros((maskCount), np.int32)
count = 0
for filePath in glob.glob(wildcard):
filename = os.path.split(filePath)[1]
parts = filename.split( "-")
if ( len(parts) == 3) and parts[1] in featureNames:
imgPath = filePath
mask[:, :, count] = skimage.io.imread(filePath)
class_ids[count] = featureNames[parts[1]]
count += 1
return mask, class_ids