提交 ae65c8c0 编写于 作者: D dongdaxiang

add imagenet preprocessing example and more functions for image_reader

上级 2e03def0
...@@ -13,22 +13,24 @@ ...@@ -13,22 +13,24 @@
# limitations under the License. # limitations under the License.
import sys import sys
from image_reader import ImageReader
from paddle_serving_client import Client from paddle_serving_client import Client
from paddle_serving_app.reader import Sequential, File2Image, Resize, CenterCrop, RGB2BGR, Transpose, Div, Normalize
import time import time
client = Client() client = Client()
client.load_client_config(sys.argv[1]) client.load_client_config(sys.argv[1])
client.connect(["127.0.0.1:9393"]) client.connect(["127.0.0.1:9393"])
reader = ImageReader()
seq = Sequential([
File2Image(), Resize(256), CenterCrop(224), RGB2BGR(), Transpose((2, 0, 1)),
Div(255), Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
print(seq)
start = time.time() start = time.time()
image_file = "daisy.jpg"
for i in range(1000): for i in range(1000):
with open("./data/n01440764_10026.JPEG", "rb") as f: img = seq(image_file)
img = f.read()
img = reader.process_image(img)
fetch_map = client.predict(feed={"image": img}, fetch=["score"]) fetch_map = client.predict(feed={"image": img}, fetch=["score"])
end = time.time() end = time.time()
print(end - start) print(end - start)
#print(fetch_map["score"])
...@@ -11,4 +11,4 @@ ...@@ -11,4 +11,4 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from .image_reader import ImageReader, File2Image, URL2Image, Sequential, Normalize, CenterCrop, Resize from .image_reader import ImageReader, File2Image, URL2Image, Sequential, Normalize, CenterCrop, Resize, Transpose, Div, RGB2BGR, BGR2RGB
...@@ -16,14 +16,17 @@ import cv2 ...@@ -16,14 +16,17 @@ import cv2
import numpy as np import numpy as np
def transpose(img, transpose_target):
img = img.transpose(transpose_target)
return img
def normalize(img, mean, std): def normalize(img, mean, std):
# need to optimize here # need to optimize here
img = img.astype('float32').transpose((2, 0, 1)) / 255
img_mean = np.array(mean).reshape((3, 1, 1)) img_mean = np.array(mean).reshape((3, 1, 1))
img_std = np.array(std).reshape((3, 1, 1)) img_std = np.array(std).reshape((3, 1, 1))
img -= img_mean img -= img_mean
img /= img_std img /= img_std
img = img.transpose((1, 2, 0))
return img return img
...@@ -47,7 +50,7 @@ def resize(img, target_size, interpolation): ...@@ -47,7 +50,7 @@ def resize(img, target_size, interpolation):
resized_width = target_size[0] resized_width = target_size[0]
resized_height = target_size[1] resized_height = target_size[1]
else: else:
percent = float(target_size) / min(img.shape[1], img.shape[2]) percent = float(target_size) / min(img.shape[0], img.shape[1])
resized_width = int(round(img.shape[1] * percent)) resized_width = int(round(img.shape[1] * percent))
resized_height = int(round(img.shape[0] * percent)) resized_height = int(round(img.shape[0] * percent))
if interpolation: if interpolation:
...@@ -55,5 +58,4 @@ def resize(img, target_size, interpolation): ...@@ -55,5 +58,4 @@ def resize(img, target_size, interpolation):
img, (resized_width, resized_height), interpolation=interpolation) img, (resized_width, resized_height), interpolation=interpolation)
else: else:
resized = cv2.resize(img, (resized_width, resized_height)) resized = cv2.resize(img, (resized_width, resized_height))
print(resized.shape)
return resized return resized
...@@ -17,7 +17,7 @@ import numpy as np ...@@ -17,7 +17,7 @@ import numpy as np
import base64 import base64
import functional as F import functional as F
_cv2_interpolation_to_str = {cv2.INTER_LINEAR: "cv2.INTER_LINEAR"} _cv2_interpolation_to_str = {cv2.INTER_LINEAR: "cv2.INTER_LINEAR", None: "None"}
class Sequential(object): class Sequential(object):
...@@ -51,6 +51,28 @@ class Sequential(object): ...@@ -51,6 +51,28 @@ class Sequential(object):
return format_string_ return format_string_
class RGB2BGR(object):
def __init__(self):
pass
def __call__(self, img):
return img[:, :, ::-1]
def __repr__(self):
return self.__class__.__name__ + "()"
class BGR2RGB(object):
def __init__(self):
pass
def __call__(self, img):
return img[:, :, ::-1]
def __repr__(self):
return self.__class__.__name__ + "()"
class File2Image(object): class File2Image(object):
def __init__(self): def __init__(self):
pass pass
...@@ -81,6 +103,40 @@ class URL2Image(object): ...@@ -81,6 +103,40 @@ class URL2Image(object):
return self.__class__.__name__ + "()" return self.__class__.__name__ + "()"
class Base64ToImage(object):
def __init__(self):
pass
def __call__(self, img_base64):
img = base64.b64decode(img_base64)
return img
def __repr__(self):
return self.__class__.__name__ + "()"
class Div(object):
""" divide by some float number """
def __init__(self, value):
self.value = value
def __call__(self, img):
"""
Args:
img (numpy array): (int8 numpy array)
Returns:
img (numpy array): (float32 numpy array)
"""
img = img.astype('float32') / self.value
return img
def __repr__(self):
return self.__class__.__name__ + "({})".format(self.value)
class Normalize(object): class Normalize(object):
"""Normalize a tensor image with mean and standard deviation. """Normalize a tensor image with mean and standard deviation.
Given mean: ``(M1,...,Mn)`` and std: ``(S1,..,Sn)`` for ``n`` channels, this transform Given mean: ``(M1,...,Mn)`` and std: ``(S1,..,Sn)`` for ``n`` channels, this transform
...@@ -115,6 +171,27 @@ class Normalize(object): ...@@ -115,6 +171,27 @@ class Normalize(object):
self.std) self.std)
class Lambda(object):
"""Apply a user-defined lambda as a transform.
Very shame to just copy from
https://github.com/pytorch/vision/blob/master/torchvision/transforms/transforms.py#L301
Args:
lambd (function): Lambda/function to be used for transform.
"""
def __init__(self, lambd):
assert callable(lambd), repr(type(lambd)
.__name__) + " object is not callable"
self.lambd = lambd
def __call__(self, img):
return self.lambd(img)
def __repr__(self):
return self.__class__.__name__ + '()'
class CenterCrop(object): class CenterCrop(object):
"""Crops the given Image at the center. """Crops the given Image at the center.
...@@ -154,18 +231,32 @@ class Resize(object): ...@@ -154,18 +231,32 @@ class Resize(object):
``PIL.Image.BILINEAR`` ``PIL.Image.BILINEAR``
""" """
def __init__(self, size, interpolation=cv2.INTER_LINEAR): def __init__(self, size, interpolation=None):
self.size = size self.size = size
self.interpolation = interpolation self.interpolation = interpolation
def __call__(self, img): def __call__(self, img):
return F.resize(img, self.size, self.interpolation) return F.resize(img, self.size, self.interpolation)
def __repr__(self, img): def __repr__(self):
return self.__class__.__name__ + '(size={0}, interpolation={1})'.format( return self.__class__.__name__ + '(size={0}, interpolation={1})'.format(
self.size, _cv2_interpolation_to_str[self.interpolation]) self.size, _cv2_interpolation_to_str[self.interpolation])
class Transpose(object):
def __init__(self, transpose_target):
self.transpose_target = transpose_target
def __call__(self, img):
return F.transpose(img, self.transpose_target)
return img
def __repr__(self):
format_string = self.__class__.__name__ + \
"({})".format(self.transpose_target)
return format_string
class ImageReader(): class ImageReader():
def __init__(self, def __init__(self,
image_shape=[3, 224, 224], image_shape=[3, 224, 224],
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册