-
Notifications
You must be signed in to change notification settings - Fork 88
/
FastaiSSD.py
84 lines (65 loc) · 3.69 KB
/
FastaiSSD.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
'''
Copyright 2018 Esri
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
'''
import os
import sys
prf_root_dir = os.path.join(os.path.dirname(__file__), os.pardir)
sys.path.append(prf_root_dir)
from Templates.TemplateBaseDetector import TemplateBaseDetector
sys.path.append(os.path.dirname(__file__))
# from util import load_model, norm, denorm, export_img, get_tile_images, get_img, \
# get_cropped_tiles, predict_, detect_objects, suppress_close_pools, overlap, predict_classf, \
# get_nms_preds
import util
from model import ConvnetBuilder, SSD_MultiHead, resnet34, k
class ChildObjectDetector(TemplateBaseDetector):
def load_model(self, model_path):
'''
Fill this method to write your own model loading python code
save it self object if you would like to reference it later.
Tips: you can access emd information through self.json_info.
'''
# Todo: fill in this method to load your model
f_model = resnet34
head_reg = SSD_MultiHead(k, -4.)
models = ConvnetBuilder(f_model, 0, 0, 0, custom_head=head_reg)
self.model = models.model
self.model = util.load_model(self.model, model_path)
self.model.eval()
def getParameterInfo(self, required_parameters):
required_parameters.extend(
[
# Todo: add your inference parameters here
# https://github.com/Esri/raster-functions/wiki/PythonRasterFunction#getparameterinfo
]
)
return required_parameters
def inference(self, batch, **scalars):
'''
Fill this method to write your own inference python code, you can refer to the model instance that is created
in the load_model method. Expected results format is described in the returns as below.
:param batch: numpy array with shape (B, H, W, D), B is batch size, H, W is specified and equal to
ImageHeight and ImageWidth in the emd file and D is the number of bands and equal to the length
of ExtractBands in the emd. If BatchInference is set to False in emd, B is constant 1.
:param scalars: inference parameters, accessed by the parameter name,
i.e. score_threshold=float(kwargs['score_threshold']). If you want to have more inference
parameters, add it to the list of the following getParameterInfo method.
:return: bounding boxes, python list representing bounding boxes whose length is equal to B, each element is
[N,4] numpy array representing [ymin, xmin, ymax, xmax] with respect to the upper left
corner of the image tile.
scores, python list representing the score of each bounding box whose length is equal to B, each element
is [N,] numpy array
classes, python list representing the class of each bounding box whose length is equal to B, each element
is [N,] numpy array and its dype is np.uint8
'''
#Todo: fill in this method to inference your model and return bounding boxes, scores and classes
return util.detect_objects_image_space(self.model, batch, self.score_threshold)