This repository has been archived by the owner on Feb 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(image_preprocessor): add fasterRCNN
- Loading branch information
Showing
14 changed files
with
182 additions
and
37 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
!SegmentPreprocessor | ||
parameter: | ||
model_dir: /ext_data/image_preprocessor | ||
model_name: fasterrcnn_resnet50_fpn | ||
target_img_size: 224 | ||
_use_cuda: false | ||
property: | ||
is_trained: true |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
!BaseSingletonPreprocessor | ||
parameter: | ||
doc_type: 2 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
!VanillaSlidingPreprocessor | ||
parameter: | ||
window_size: 16 | ||
stride_height: 16 | ||
stride_wide: 16 | ||
target_img_size: 224 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
!WeightedSlidingPreprocessor | ||
parameter: | ||
window_size: 16 | ||
stride_height: 16 | ||
stride_wide: 16 | ||
target_img_size: 224 |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from .base import BaseImagePreprocessor | ||
from ...proto import gnes_pb2, array2blob | ||
from PIL import Image | ||
import numpy as np | ||
import io | ||
import os | ||
|
||
|
||
class SegmentPreprocessor(BaseImagePreprocessor): | ||
|
||
def __init__(self, model_name: str, | ||
model_dir: str, | ||
target_img_size: int = 224, | ||
_use_cuda: bool = False, | ||
*args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.model_name = model_name | ||
self.model_dir = model_dir | ||
self.target_img_size = target_img_size | ||
self.model_name = model_name | ||
self._use_cuda = _use_cuda | ||
|
||
def post_init(self): | ||
import torch | ||
import torchvision.models as models | ||
|
||
os.environ['TORCH_HOME'] = self.model_dir | ||
self._model = getattr(models.detection, self.model_name)(pretrained=True) | ||
self._model = self._model.eval() | ||
if self._use_cuda: | ||
# self._model.cuda() | ||
self._device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") | ||
self._model = self._model.to(self._device) | ||
|
||
def apply(self, doc: 'gnes_pb2.Document'): | ||
super().apply(doc) | ||
if doc.raw_bytes: | ||
original_image = Image.open(io.BytesIO(doc.raw_bytes)) | ||
image_tensor = self._torch_transform(original_image) | ||
|
||
seg_output = self._model([image_tensor]) | ||
chunks = seg_output[0]['boxes'].tolist() | ||
weight = seg_output[0]['scores'].tolist() | ||
for ci, ele in enumerate(zip(chunks, weight)): | ||
c = doc.chunks.add() | ||
c.doc_id = doc.doc_id | ||
c.blob.CopyFrom(array2blob(self._crop_image_reshape(original_image, ele[0]))) | ||
c.offset_1d = ci | ||
c.weight = ele[1] | ||
else: | ||
self.logger.error('bad document: "raw_bytes" is empty!') | ||
|
||
def _crop_image_reshape(self, original_image, coordinates): | ||
return np.array(original_image.crop(coordinates).resize((self.target_img_size, | ||
self.target_img_size))) |
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
!SegmentPreprocessor | ||
parameter: | ||
model_dir: ${FASTERRCNN_MODEL} | ||
model_name: fasterrcnn_resnet50_fpn | ||
target_img_size: 224 | ||
_use_cuda: false | ||
property: | ||
is_trained: true |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
!BasePytorchEncoder | ||
parameter: | ||
model_dir: ${MOBILENET_MODEL} | ||
model_name: mobilenet_v2 | ||
layers: | ||
- features | ||
- x.mean([2, 3]) | ||
property: | ||
is_trained: true |