Skip to content
Permalink
Browse files

refactor: fixing the imports of all base module

  • Loading branch information...
hanxiao committed Aug 21, 2019
1 parent 6092f12 commit 5f1ca000555020249e59ad8cda4349f7c1351b02
@@ -176,7 +176,7 @@ def _post_init_wrapper(self):
_id = str(uuid.uuid4()).split('-')[0]
_name = '%s-%s' % (self.__class__.__name__, _id)
self.logger.warning(
'this object is not named ("- gnes_config: - name" is not found in YAML config), '
'this object is not named ("name" is not found under "gnes_config" in YAML config), '
'i will call it "%s". '
'naming the object is important as it provides an unique identifier when '
'serializing/deserializing this object.' % _name)
@@ -0,0 +1,35 @@
from .encoder import base as encoder_base
from .indexer import base as indexer_base
from .preprocessor import base as prep_base
from .router import base as router_base

# Encoder
BaseEncoder = encoder_base.BaseEncoder
BaseTextEncoder = encoder_base.BaseTextEncoder
BaseAudioEncoder = encoder_base.BaseAudioEncoder
BaseImageEncoder = encoder_base.BaseImageEncoder
BaseVideoEncoder = encoder_base.BaseVideoEncoder
BaseBinaryEncoder = encoder_base.BaseBinaryEncoder
BaseNumericEncoder = encoder_base.BaseNumericEncoder
PipelineEncoder = encoder_base.PipelineEncoder

# Indexer
BaseVectorIndexer = indexer_base.BaseVectorIndexer
BaseIndexer = indexer_base.BaseIndexer
BaseTextIndexer = indexer_base.BaseTextIndexer
BaseKeyIndexer = indexer_base.BaseKeyIndexer
JointIndexer = indexer_base.JointIndexer

# Preprocessor
BasePreprocessor = prep_base.BasePreprocessor
BaseImagePreprocessor = prep_base.BaseImagePreprocessor
BaseTextPreprocessor = prep_base.BaseTextPreprocessor
BaseAudioPreprocessor = prep_base.BaseAudioPreprocessor
BaseVideoPreprocessor = prep_base.BaseVideoPreprocessor
PipelinePreprocessor = prep_base.PipelinePreprocessor
UnaryPreprocessor = prep_base.UnaryPreprocessor

# Router
BaseReduceRouter = router_base.BaseReduceRouter
BaseRouter = router_base.BaseRouter
BaseMapRouter = router_base.BaseMapRouter
@@ -30,7 +30,7 @@ def _create_flask_app(self):
except ImportError:
raise ImportError('Flask or its dependencies are not fully installed, '
'they are required for serving HTTP requests.'
'Please use "pip install gnes[http]" to install it.')
'Please use "pip install gnes[flask]" to install it.')

app = Flask(__name__)
args = set_composer_parser().parse_args([])
@@ -37,7 +37,7 @@
'BaseAudioEncoder': 'base',
'PipelineEncoder': 'base',
'HashEncoder': 'numeric.hash',
'BasePytorchEncoder': 'image.base',
'TorchvisionEncoder': 'image.torchvision',
'TFInceptionEncoder': 'image.inception',
'CVAEEncoder': 'image.cvae',
'IncepMixtureEncoder': 'video.incep_mixture',
@@ -22,7 +22,7 @@
from ...helper import batching


class BasePytorchEncoder(BaseImageEncoder):
class TorchvisionEncoder(BaseImageEncoder):
batch_size = 64

def __init__(self, model_name: str,
@@ -19,7 +19,7 @@
import tensorflow.contrib.slim as slim


class NetFV():
class NetFV:
def __init__(self, feature_size,
cluster_size,
vocab_size,
@@ -20,21 +20,20 @@
_cls2file_map = {
'BasePreprocessor': 'base',
'PipelinePreprocessor': 'base',
'TextPreprocessor': 'text.simple',
'BaseImagePreprocessor': 'image.base',
'BaseTextPreprocessor': 'text.base',
'BaseSlidingPreprocessor': 'image.sliding_window',
'PunctSplitPreprocessor': 'text.split_punct',
'BaseImagePreprocessor': 'base',
'BaseTextPreprocessor': 'base',
'VanillaSlidingPreprocessor': 'image.sliding_window',
'WeightedSlidingPreprocessor': 'image.sliding_window',
'SegmentPreprocessor': 'image.segmentation',
'UnaryPreprocessor': 'base',
'ResizeChunkPreprocessor': 'image.resize',
'BaseVideoPreprocessor': 'video.base',
'BaseVideoPreprocessor': 'base',
'FFmpegPreprocessor': 'video.ffmpeg',
'FFmpegVideoSegmentor': 'video.ffmpeg',
'ShotDetectPreprocessor': 'video.shotdetect',
'AudioVanilla': 'audio.audio_vanilla',
'BaseAudioPreprocessor': 'audio.base'
'BaseAudioPreprocessor': 'base'
}

register_all_class(_cls2file_map, 'preprocessor')
@@ -15,7 +15,7 @@

import numpy as np

from .base import BaseAudioPreprocessor
from ..base import BaseAudioPreprocessor
from ..helper import get_video_length_from_raw, get_audio
from ...proto import array2blob

This file was deleted.

@@ -38,6 +38,22 @@ def apply(self, doc: 'gnes_pb2.Document') -> None:
doc.weight = 1.0


class BaseTextPreprocessor(BasePreprocessor):
doc_type = gnes_pb2.Document.TEXT


class BaseAudioPreprocessor(BasePreprocessor):
doc_type = gnes_pb2.Document.AUDIO


class BaseImagePreprocessor(BasePreprocessor):
doc_type = gnes_pb2.Document.IMAGE


class BaseVideoPreprocessor(BasePreprocessor):
doc_type = gnes_pb2.Document.VIDEO


class PipelinePreprocessor(CompositionalTrainableBase):
def apply(self, doc: 'gnes_pb2.Document') -> None:
if not self.components:
@@ -19,6 +19,7 @@
import os
import subprocess as sp
from datetime import timedelta
from itertools import product
from typing import List, Callable

import cv2
@@ -281,3 +282,18 @@ def compare_descriptor(descriptor1: 'np.ndarray',
}

return cv2.compareHist(descriptor1, descriptor2, dist_metric[metric])


def torch_transform(img):
import torchvision.transforms as transforms
return transforms.Compose([transforms.ToTensor(),
transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))])(img)


def get_all_subarea(img):
x_list = [0, img.size[0] / 3, 2 * img.size[0] / 3, img.size[0]]
y_list = [0, img.size[1] / 3, 2 * img.size[1] / 3, img.size[1]]

index = [[x, y, x + 1, y + 1] for [x, y] in product(range(len(x_list) - 1), range(len(y_list) - 1))]
all_subareas = [[x_list[idx[0]], y_list[idx[1]], x_list[idx[2]], y_list[idx[3]]] for idx in index]
return all_subareas, index

This file was deleted.

@@ -16,12 +16,11 @@
import numpy as np
from PIL import Image

from .base import BaseImagePreprocessor
from ..base import BaseImagePreprocessor
from ...proto import gnes_pb2, blob2array, array2blob


class ResizeChunkPreprocessor(BaseImagePreprocessor):

class SizedPreprocessor(BaseImagePreprocessor):
def __init__(self,
target_width: int = 224,
target_height: int = 224,
@@ -30,6 +29,9 @@ def __init__(self,
self.target_width = target_width
self.target_height = target_height


class ResizeChunkPreprocessor(SizedPreprocessor):

def apply(self, doc: 'gnes_pb2.Document') -> None:
super().apply(doc)
for c in doc.chunks:
@@ -20,11 +20,12 @@
import numpy as np
from PIL import Image

from .base import BaseImagePreprocessor
from .resize import SizedPreprocessor
from ..helper import torch_transform, get_all_subarea
from ...proto import array2blob


class SegmentPreprocessor(BaseImagePreprocessor):
class SegmentPreprocessor(SizedPreprocessor):

def __init__(self, model_name: str,
model_dir: str,
@@ -51,8 +52,8 @@ def apply(self, doc: 'gnes_pb2.Document'):
super().apply(doc)
if doc.raw_bytes:
original_image = Image.open(io.BytesIO(doc.raw_bytes))
all_subareas, index = self._get_all_subarea(original_image)
image_tensor = self._torch_transform(original_image)
all_subareas, index = get_all_subarea(original_image)
image_tensor = torch_transform(original_image)
if self._use_cuda:
image_tensor = image_tensor.cuda()

@@ -73,8 +74,8 @@ def apply(self, doc: 'gnes_pb2.Document'):

c = doc.chunks.add()
c.doc_id = doc.doc_id
c.blob.CopyFrom(array2blob(np.array(original_image.resize((self.target_img_size,
self.target_img_size)))))
c.blob.CopyFrom(array2blob(np.array(original_image.resize((self.target_width,
self.target_height)))))
c.offset_1d = len(chunks)
c.offset_nd.x.extend([100, 100])
c.weight = 1.
@@ -19,12 +19,13 @@
import numpy as np
from PIL import Image

from .base import BaseImagePreprocessor
from .resize import SizedPreprocessor
from ..helper import get_all_subarea, torch_transform
from ..video.ffmpeg import FFmpegPreprocessor
from ...proto import gnes_pb2, array2blob


class BaseSlidingPreprocessor(BaseImagePreprocessor):
class _SlidingPreprocessor(SizedPreprocessor):

def __init__(self, window_size: int = 64,
stride_height: int = 64,
@@ -39,13 +40,13 @@ def apply(self, doc: 'gnes_pb2.Document'):
super().apply(doc)
if doc.raw_bytes:
original_image = Image.open(io.BytesIO(doc.raw_bytes))
all_subareas, index = self._get_all_subarea(original_image)
all_subareas, index = get_all_subarea(original_image)
image_set, center_point_list = self._get_all_sliding_window(np.array(original_image))
normalizaed_image_set = [np.array(self._torch_transform(img)).transpose(1, 2, 0)
for img in image_set]
weight = self._get_all_chunks_weight(normalizaed_image_set)
normalized_img_set = [np.array(torch_transform(img)).transpose(1, 2, 0)
for img in image_set]
weight = self._get_all_chunks_weight(normalized_img_set)

for ci, ele in enumerate(zip(normalizaed_image_set, weight)):
for ci, ele in enumerate(zip(normalized_img_set, weight)):
c = doc.chunks.add()
c.doc_id = doc.doc_id
c.blob.CopyFrom(array2blob(ele[0]))
@@ -87,7 +88,7 @@ def _get_all_sliding_window(self, img: 'np.ndarray'):
for y in range(expanded_input.shape[1])]

expanded_input = expanded_input.reshape((-1, self.window_size, self.window_size, 3))
return [np.array(Image.fromarray(img).resize((self.target_img_size, self.target_img_size))) for img in
return [np.array(Image.fromarray(img).resize((self.target_width, self.target_height))) for img in
expanded_input], center_point_list

def _get_slid_offset_nd(self, all_subareas: List[List[int]], index: List[List[int]], center_point: List[float]) -> \
@@ -117,14 +118,17 @@ def _get_location(all_subareas: List[List[int]], center_point: List[float]) -> L
location_list[-1] = True
return location_list

def _get_all_chunks_weight(self, normalizaed_image_set):
raise NotImplementedError

class VanillaSlidingPreprocessor(BaseSlidingPreprocessor):

class VanillaSlidingPreprocessor(_SlidingPreprocessor):

def _get_all_chunks_weight(self, image_set: List['np.ndarray']) -> List[float]:
return [1 / len(image_set) for _ in range(len(image_set))]


class WeightedSlidingPreprocessor(BaseSlidingPreprocessor):
class WeightedSlidingPreprocessor(_SlidingPreprocessor):

def _get_all_chunks_weight(self, image_set: List['np.ndarray']) -> List[float]:
return FFmpegPreprocessor.pic_weight(image_set)

This file was deleted.

0 comments on commit 5f1ca00

Please sign in to comment.
You can’t perform that action at this time.