renom_img.api.utility ¶
renom_img.api.utility.box ¶
-
rescale
( box , before_size , after_size ) ¶ -
ボックスが特定のサイズになるように座標とサイズを変換します。
パラメータ: - box ( list ) -- このリストは上の座標で表現される4つの要素からなります。
- before_size ( float ) -- スケールを変換する前のボックスのサイズ
- after_size ( float ) -- スケールを変換する前のボックスのサイズ
-
transform2xywh
( box ) ¶ -
この関数はボックスの座標のフォーマットを(x1, y1, x2, y2)から(x, y, w, h)に変換します。
(x1, y1) は左上の角を表します。(x2, y2) は右下の角を表します。
(x, y)はバウンディングボックスの中心を表します。 (w, h)はバウンディングボックスの幅と高さを表します。
引数のボックスは以下の例のようなフォーマットでなければなりません。
[x1(float), y1(float), x2(float), y2(float)]
パラメータ: box ( list ) -- このリストは上の座標で表現される4つの要素からなります。 戻り値: フォームが変更されたバウンディングボックスを返します。 戻り値の型: (list)
-
transform2xy12
( box ) ¶ -
この関数はボックスの座標のフォーマットを(x, y, w, h)から(x1, y1, x2, y2)に変換します。
(x, y)はバウンディングボックスの中心を表します。 (w, h)はバウンディングボックスの幅と高さを表します。
(x1, y1) は左上の角を表します。(x2, y2) は右下の角を表します。
引数のボックスは以下の例のようなフォーマットでなければなりません。
[x(float), y(float), w(float), h(float)]
パラメータ: box ( list ) -- このリストは上の座標で表現される4つの要素からなります。 戻り値: フォームが変更されたバウンディングボックスを返します。 戻り値の型: (list)
-
calc_iou_xyxy
( box1 , box2 ) ¶ -
この関数は座標のフォーマット(x, y, w, h)の下でIOUを計算します。
(x, y)はバウンディングボックスの中心を表します。 (w, h)はバウンディングボックスの幅と高さを表します。
引数のボックスは以下の例のようなフォーマットでなければなりません。
[x(float), y(float), w(float), h(float)]
パラメータ: - box1 ( list ) -- このリストは上の座標で表現される4つの要素からなります。
- box2 ( list ) -- このリストは上の座標で表現される4つの要素からなります。
戻り値: IOUの値を返却します。
戻り値の型: (float)
-
calc_iou_xywh
( box1 , box2 ) ¶ -
この関数は座標のフォーマット(x1, y1, x2, y2)の下でIOUを計算します。
(x1, y1) は左上の角を表します。(x2, y2) は右下の角を表します。
引数のボックスは以下の例のようなフォーマットでなければなりません。
[x1(float), y1(float), x2(float), y2(float)]
パラメータ: - box1 ( list ) -- このリストは上の座標で表現される4つの要素からなります。
- box2 ( list ) -- このリストは上の座標で表現される4つの要素からなります。
戻り値: IOUの値を返却します。
戻り値の型: (float)
renom_img.api.utility.load ¶
-
parse_xml_detection
( xml_path_list , num_thread=8 ) ¶ -
XMLのフォーマットはPascal VOCフォーマットである必要があります。
パラメータ: - xml_path_list ( list ) -- xmlファイルのパスのリスト
- num_thread ( int ) -- Number of thread for parsing xml files.
戻り値: アノテーションのリストを返却します。各アノテーションはキーとしてボックスと名前を含むような辞書のリストからなります。構造は以下に示す通りです。
戻り値の型: (list)
# An example of returned list. [ [ # Objects of 1st image. {'box': [x(float), y, w, h], 'name': class_name(string), 'class': id(int)}, {'box': [x(float), y, w, h], 'name': class_name(string), 'class': id(int)}, ... ], [ # Objects of 2nd image. {'box': [x(float), y, w, h], 'name': class_name(string), 'class': id(int)}, {'box': [x(float), y, w, h], 'name': class_name(string), 'class': id(int)}, ... ] ]
renom_img.api.utility.nms ¶
-
nms
( ) ¶ -
Non-Maximum Suppression
パラメータ: - preds ( list ) -- 予測されたボックスのリスト。フォーマットは以下の通りになります。
- threshold ( float , optional ) -- デフォルト値は0.5です。これはボックス間の重なりの割合を表します。
"preds" 引数の例 ¶[ [ # Objects of 1st image. {'box': [x(float), y, w, h], 'class': class_id(int), 'score': score}, {'box': [x(float), y, w, h], 'class': class_id(int), 'score': score}, ... ], [ # Objects of 2nd image. {'box': [x(float), y, w, h], 'class': class_id(int), 'score': score}, {'box': [x(float), y, w, h], 'class': class_id(int), 'score': score}, ... ] ]
戻り値: フォームが変更されたバウンディングボックスを返します。 戻り値の型: (list) 戻り値の例 ¶[ [ # Objects of 1st image. {'box': [x(float), y, w, h], 'class': class_id(int), 'score': score}, {'box': [x(float), y, w, h], 'class': class_id(int), 'score': score}, ... ], [ # Objects of 2nd image. {'box': [x(float), y, w, h], 'class': class_id(int), 'score': score}, {'box': [x(float), y, w, h], 'class': class_id(int), 'score': score}, ... ] ]
-
soft_nms
( ) ¶ -
Soft Non-Maximum Suppression
パラメータ: - preds ( list ) -- 予測されたボックスのリスト。フォーマットは以下の通りになります。
- threshold ( float , optional ) -- デフォルト値は0.5です。これはボックス間の重なりの割合を表します。
"preds" 引数の例 ¶[ [ # Objects of 1st image. {'box': [x(float), y, w, h], 'class': class_id(int), 'score': score}, {'box': [x(float), y, w, h], 'class': class_id(int), 'score': score}, ... ], [ # Objects of 2nd image. {'box': [x(float), y, w, h], 'class': class_id(int), 'score': score}, {'box': [x(float), y, w, h], 'class': class_id(int), 'score': score}, ... ] ]
戻り値: フォームが変更されたバウンディングボックスを返します。 戻り値の型: (list) [ [ # Objects of 1st image. {'box': [x(float), y, w, h], 'class': class_id(int), 'score': score}, {'box': [x(float), y, w, h], 'class': class_id(int), 'score': score}, ... ], [ # Objects of 2nd image. {'box': [x(float), y, w, h], 'class': class_id(int), 'score': score}, {'box': [x(float), y, w, h], 'class': class_id(int), 'score': score}, ... ] ]
引用
Navaneeth Bodla, Bharat Singh, Rama Chellappa, Larry S. Davis,Soft-NMS -- Improving Object Detection With One Line of Code
renom_img.api.utility.target ¶
-
class
DataBuilderClassification
( class_map , imsize ) ¶ -
ベースクラス:
renom_img.api.utility.target.DataBuilderBase
分類タスク用のデータビルダー
パラメータ: - class_map ( array ) -- クラス名の配列
- imsize ( int or tuple ) -- 入力画像サイズ
-
build
( img_path_list , annotation_list , augmentation=None , **kwargs ) ¶ -
画像とそれに対するラベルを構築します。
パラメータ: - img_path_list ( list ) -- 入力画像のパスのリスト
- annotation_list ( list ) -- クラスIDのリスト[1, 4, 6(int)]
- augmentation ( Augmentation ) -- Augmentationクラスのインスタンス
戻り値: 画像のバッチ y(ndarray): バッチ内の各画像のOne-hotラベル
戻り値の型: (tuple)
-
class
DataBuilderDetection
( class_map , imsize ) ¶ -
ベースクラス:
renom_img.api.utility.target.DataBuilderBase
物体検出タスクのデータビルダー
パラメータ: - class_map ( array ) -- クラス名の配列
- imsize ( int or tuple ) -- 入力画像サイズ
-
build
( img_path_list , annotation_list , augmentation=None , **kwargs ) ¶ -
パラメータ: - img_path_list ( list ) -- 入力画像のパスのリスト
- annotation_list ( list ) -- アノテーションのリストのフォーマットは以下の通りとなります。
- augmentation ( Augmentation ) -- Augmentationクラスのインスタンス
戻り値: 画像のバッチとshapeが ** (画像の枚数, 画像一枚あたりにおける最大の物体数 * (4(座標) + 1(確信度)**のndarray
戻り値の型: (tuple)
-
resize_img
( img_list , annotation_list ) ¶
-
class
DataBuilderSegmentation
( class_map , imsize ) ¶ -
ベースクラス:
renom_img.api.utility.target.DataBuilderBase
セマンティックセグメンテーションタスク用のデータビルダー
パラメータ: - class_map ( array ) -- クラス名の配列
- imsize ( int or tuple ) -- 入力画像サイズ
-
build
( img_path_list , annotation_list , augmentation=None , **kwargs ) ¶ -
パラメータ: - img_path_list ( list ) -- 入力画像のパスのリスト
- annotation_list ( list ) -- アノテーションのリストのフォーマットは以下の通りとなります。
- augmentation ( Augmentation ) -- Augmentationクラスのインスタンス
戻り値: 画像のバッチとshapeが (バッチサイズ, クラス数, 幅, 高さ)
戻り値の型: (tuple)
-
crop_to_square
( image ) ¶
-
load_annotation
( path ) ¶ -
アノテーションデータのロード
パラメータ: path -- アノテーションパスのリスト 戻り値: アノテーションデータ(numpy.array)、元画像の幅に対する与えられた幅の割合 戻り値の型: (tuple)
-
load_img
( path ) ¶ -
Loads an image
パラメータ: path ( str ) -- 入力画像のパスのリスト 戻り値: アノテーションデータ(numpy.array)、元画像の幅に対する与えられた幅の割合 戻り値の型: (tuple)
-
resize
( img_list , label_list ) ¶
renom_img.api.utility.augmentation ¶
-
class
Augmentation
( process_list ) ¶ -
ベースクラス:
object
このクラスは画像に前処理を適用します。AugmentationのインスタンスはImageDistributorモジュールに渡されます、そして学習プロセスが実行されている場合のみに呼び出されます。Processモジュールから前処理用のメソッドを呼び出せます。パラメータ: process_list ( list of Process modules ) -- Processモジュールのリスト。Flip, Shift, RotateそしてWhiteNoiseから選択できます。 例
>>> from renom_img.api.utility.augmentation import Augmentation >>> from renom_img.api.utility.augmentation.process import Flip, Shift, Rotate, WhiteNoise >>> from renom_img.api.utility.distributor.distributor import ImageDistributor >>> aug = Augmentation([ ... Shift(40, 40), ... Rotate(), ... Flip(), ... WhiteNoise() ... ]) >>> distributor = ImageDistributor( ... img_path_list, ... label_list, ... builder, ... aug, ... num_worker ... )
-
transform
( x , y=None , mode='classification' ) ¶ -
この関数はImageDistributorに前処理を適用します。
パラメータ: - x ( list of str ) -- 入力画像のパスのリスト
- y ( list of annotations ) -- アノテーションパスのリスト
戻り値: 変換された画像のリストとxのアノテーションのリスト
[ x (list of numpy.ndarray), # List of transformed images. y (list of annotations) # List of annotations for x. ]
戻り値の型: tuple
-
-
flip
( x , y=None , mode='classification' ) ¶ -
画像をランダムに反転させます。
パラメータ: - x ( list of str ) -- 入力画像のパスのリスト
- y ( list of annotation ) -- アノテーションパスのリスト
戻り値: 変換された画像のリストとxのアノテーションのリスト
戻り値の型: tuple
[ x (list of numpy.ndarray), # List of transformed images. y (list of annotation) # List of annotations for x. ]
例
>>> from renom_img.api.utility.augmentation.process import flip >>> from PIL import Image >>> import numpy as np >>> >>> img1 = Image.open(img1_path).convert('RGB') >>> img1 = np.asarray(img1).transpose(2,0,1).astype(np.float32) >>> >>> img2 = Image.open(img2_path).convert('RGB') >>> img2 = np.asarray(img2).transpose(2,0,1).astype(np.float32) >>> >>> img_list = [img1, img2] >>> flipped_img = flip(img_list)
-
horizontalflip
( x , y=None , prob=True , mode='classification' ) ¶ -
画像をランダムに水平反転させます。
パラメータ: - x ( list of str ) -- 入力画像のパスのリスト
- y ( list of annotations ) -- アノテーションパスのリスト
戻り値: 変換された画像のリストとxのアノテーションのリスト
戻り値の型: tuple
[ x (list of numpy.ndarray), # List of transformed images. y (list of annotation) # List of annotations for x. ]
例
>>> from renom_img.api.utility.augmentation.process import horizontalflip >>> from PIL import Image >>> import numpy as np >>> >>> img1 = Image.open(img1_path).convert('RGB') >>> img1 = np.asarray(img1).transpose(2,0,1).astype(np.float32) >>> >>> img2 = Image.open(img2_path).convert('RGB') >>> img2 = np.asarray(img2).transpose(2,0,1).astype(np.float32) >>> >>> img_list = [img1, img2] >>> flipped_img = horizontalflip(img_list)
-
verticalflip
( x , y=None , prob=True , mode='classification' ) ¶ -
画像をランダムに垂直反転させます。
パラメータ: - x ( list of str ) -- 入力画像のパスのリスト
- y ( list of annotations ) -- アノテーションパスのリスト
戻り値: 変換された画像のリストとxのアノテーションのリスト
戻り値の型: tuple
[ x (list of numpy.ndarray), # List of transformed images. y (list of annotations) # List of annotations for x. ]
例
>>> from renom_img.api.utility.augmentation.process import verticalflip >>> from PIL import Image >>> import numpy as np >>> >>> img1 = Image.open(img1_path).convert('RGB') >>> img1 = np.asarray(img1).transpose(2,0,1).astype(np.float32) >>> >>> img2 = Image.open(img2_path).convert('RGB') >>> img2 = np.asarray(img2).transpose(2,0,1).astype(np.float32) >>> >>> img_list = [img1, img2] >>> flipped_img = verticalflip(img_list)
-
random_crop
( x , y=None , padding=4 , mode='classification' ) ¶ -
画像をランダムにクロップさせます。
パラメータ: - x ( list of str ) -- 入力画像のパスのリスト
- y ( list of annotations ) -- アノテーションパスのリスト
戻り値: 変換された画像のリストとxのアノテーションのリスト
戻り値の型: tuple
[ x (list of numpy.ndarray), # List of transformed images. y (list of annotations) # List of annotations for x. ]
例
>>> from renom_img.api.utility.augmentation.process import random_crop >>> from PIL import Image >>> import numpy as np >>> >>> img1 = Image.open(img1_path).convert('RGB') >>> img1 = np.asarray(img1).transpose(2,0,1).astype(np.float32) >>> >>> img2 = Image.open(img2_path).convert('RGB') >>> img2 = np.asarray(img2).transpose(2,0,1).astype(np.float32) >>> >>> img_list = [img1, img2] >>> cropped_img = random_crop(img_list)
-
center_crop
( x , y , mode='classification' ) ¶ -
画像のセンターをクロップさせます。
パラメータ: - x ( list of str ) -- 入力画像のパスのリスト
- y ( list of annotations ) -- アノテーションパスのリスト
戻り値: 変換された画像のリストとxのアノテーションのリスト
戻り値の型: tuple
[ x (list of numpy.ndarray), # List of transformed images. y (list of annotations) # List of annotations for x. ]
例
>>> from renom_img.api.utility.augmentation.process import center_crop >>> from PIL import Image >>> import numpy as np >>> >>> img1 = Image.open(img1_path).convert('RGB') >>> img1 = np.asarray(img1).transpose(2,0,1).astype(np.float32) >>> >>> img2 = Image.open(img2_path).convert('RGB') >>> img2 = np.asarray(img2).transpose(2,0,1).astype(np.float32) >>> >>> img_list = [img1, img2] >>> cropped_img = center_crop(img_list)
-
shift
( x , y=None , horizontal=10 , vertivcal=10 , mode='classification' ) ¶ -
与えられたパラメータを元に画像をランダムにシフトさせます。
パラメータ: - x ( list of str ) -- 入力画像のパスのリスト
- y ( list of annotations ) -- アノテーションパスのリスト
戻り値: 変換された画像のリストとxのアノテーションのリスト
戻り値の型: tuple
[ x (list of numpy.ndarray), # List of transformed images. y (list of annotations) # List of annotations for x. ]
例
>>> from renom_img.api.utility.augmentation.process import shift >>> from PIL import Image >>> import numpy as np >>> >>> img1 = Image.open(img1_path).convert('RGB') >>> img1 = np.asarray(img1).transpose(2,0,1).astype(np.float32) >>> >>> img2 = Image.open(img2_path).convert('RGB') >>> img2 = np.asarray(img2).transpose(2,0,1).astype(np.float32) >>> >>> img_list = [img1, img2] >>> shifted_img = shift(img_list)
-
rotate
( x , y=None , mode='classification' ) ¶ -
0度、90度、180度、270度からランダムに画像を回転させます。
パラメータ: - x ( list of str ) -- 入力画像のパスのリスト
- y ( list of annotations ) -- アノテーションパスのリスト
戻り値: 変換された画像のリストとxのアノテーションのリスト
戻り値の型: tuple
[ x (list of numpy.ndarray), # List of transformed images. y (list of annotations) # List of annotations for x. ]
例
>>> from renom_img.api.utility.augmentation.process import rotate >>> from PIL import Image >>> import numpy as np >>> >>> img1 = Image.open(img1_path).convert('RGB') >>> img1 = np.asarray(img1).transpose(2,0,1).astype(np.float32) >>> >>> img2 = Image.open(img2_path).convert('RGB') >>> img2 = np.asarray(img2).transpose(2,0,1).astype(np.float32) >>> >>> img_list = [img1, img2] >>> rotated_img = rotate(img_list)
-
white_noise
( x , y=None , std=0.01 , mode='classification' ) ¶ -
画像にホワイトノイズを加えます。
パラメータ: - x ( list of str ) -- 入力画像のパスのリスト
- y ( list of annotations ) -- アノテーションパスのリスト
戻り値: 変換された画像のリストとxのアノテーションのリスト
[ x (list of numpy.ndarray), # List of transformed images. y (list of annotations) # list of annotations for x. ]
戻り値の型: tuple
例
>>> from renom_img.api.utility.augmentation.process import white_noise >>> from PIL import Image >>> import numpy as np >>> >>> img1 = Image.open(img1_path).convert('RGB') >>> img1 = np.asarray(img1).transpose(2,0,1).astype(np.float32) >>> >>> img2 = Image.open(img2_path).convert('RGB') >>> img2 = np.asarray(img2).transpose(2,0,1).astype(np.float32) >>> >>> img_list = [img1, img2] >>> white_noise_img = white_noise(img_list)
-
distortion
( x , y , mode='classification' ) ¶ -
画像に歪みを加えます。
パラメータ: - x ( list of str ) -- 入力画像のパスのリスト
- y ( list of annotations ) -- アノテーションパスのリスト
戻り値: 変換された画像のリストとxのアノテーションのリスト
[ x (list of numpy.ndarray), # List of transformed images. y (list of annotations) # list of annotations for x. ]
戻り値の型: tuple
例
>>> from renom_img.api.utility.augmentation.process import distortion >>> from PIL import Image >>> import numpy as np >>> >>> img1 = Image.open(img1_path).convert('RGB') >>> img1 = np.asarray(img1).transpose(2,0,1).astype(np.float32) >>> >>> img2 = Image.open(img2_path).convert('RGB') >>> img2 = np.asarray(img2).transpose(2,0,1).astype(np.float32) >>> >>> img_list = [img1, img2] >>> distorted_img = distortion(img_list)
-
color_jitter
( x , y=None , h=0.1 , s=0.1 , v=0.1 , mode='classification' ) ¶ -
HSV空間においてランダムにジッターを加えます。
パラメータ: - x ( list of str ) -- 入力画像のパスのリスト
- y ( list of annotations ) -- アノテーションパスのリスト
- h ( float , or tuple/list of two floats ) -- Hueジッターのスケールファクター。List型、tuple型、もしくはfloat型を受け付けます。
- s ( float , or tuple/list of two floats ) -- Saturationジッターのスケールファクター。List型、tuple型、もしくはfloat型を受け付けます。
- v ( float , or tuple/list of two floats ) -- Brightnessジッターのスケールファクター。List型、tuple型、もしくはfloat型を受け付けます。
戻り値: 変換された画像のリストとxのアノテーションのリスト
戻り値の型: tuple
[ x (list of numpy.ndarray), # List of transformed images. y (list of annotations) # list of annotations for x. ]
例
>>> from renom_img.api.utility.augmentation.process import color_jitter >>> from PIL import Image >>> import numpy as np >>> >>> img = Image.open(img_path) >>> img.convert('RGB') >>> img = np.array(img).transpose(2, 0, 1).astype(np.float) >>> x = np.array([img]) >>> new_x, new_y = color_jitter(x, h=0.1, s=0.1, v=0.2)
-
contrast_norm
( x , y=None , alpha=0.5 , per_channel=False , mode='classification' ) ¶ -
コントラスト正規化
パラメータ: - x ( list of str ) -- 入力画像のパスのリスト
- y ( list of annotations ) -- アノテーションパスのリスト
- alpha ( float or list of two floats ) -- コントラストを変動させる範囲のパラメーターです。List型かfloat型を受け付けます。
- per_channel ( Bool ) -- チェンネルごとに変動させるかどうかを指定します。alphaにリスト型が引き渡される場合は、チャンネルごとにコントラストの変動をさせます。
戻り値: 変換された画像のリストとxのアノテーションのリスト
戻り値の型: tuple
[ x (list of numpy.ndarray), # List of transformed images. y (list of annotations) # List of annotations for x. ]
例
>>> from renom_img.api.utility.augmentation.process import contrast_norm >>> from PIL import Image >>> import numpy as np >>> >>> img = Image.open(img_path) >>> img.convert('RGB') >>> img = np.array(img).transpose(2, 0, 1).astype(np.float) >>> x = np.array([img]) >>> new_x, new_y = contrast_norm(x, alpha=0.4)
-
random_brightness
( x , y=None , delta=32 , mode='classification' ) ¶ -
画像の明度をランダムに変動させます。
パラメータ: - x ( list of str ) -- 入力画像のパスのリスト
- y ( list of annotations ) -- アノテーションパスのリスト
- delta ( int ) -- 明度を調整する範囲
戻り値: 変換された画像のリストとxのアノテーションのリスト
戻り値の型: tuple
[ x (list of numpy.ndarray), # List of transformed images. y (list of annotations) # List of annotations for x. ]
例
>>> from renom_img.api.utility.augmentation.process import random_brightness >>> from PIL import Image >>> import numpy as np >>> >>> img = Image.open(img_path) >>> img.convert('RGB') >>> img = np.array(img).transpose(2, 0, 1).astype(np.float) >>> x = np.array([img]) >>> new_x, new_y = random_brightness(x, delta=16)
-
random_hue
( x , y=None , mode='classification' ) ¶ -
画像の色調をランダムに変動させます。
パラメータ: - x ( list of str ) -- 入力画像のパスのリスト
- y ( list of annotations ) -- アノテーションパスのリスト
- max_delta ( float ) -- 色調変動のパラメーター。指定可能範囲は[0, 0.5]。
戻り値: 変換された画像のリストとxのアノテーションのリスト
戻り値の型: tuple
[ x (list of numpy.ndarray), # List of transformed images. y (list of annotations) # List of annotations for x. ]
例
>>> from renom_img.api.utility.augmentation.process import random_hue >>> from PIL import Image >>> import numpy as np >>> >>> img = Image.open(img_path) >>> img.convert('RGB') >>> img = np.array(img).transpose(2, 0, 1).astype(np.float) >>> x = np.array([img]) >>> new_x, new_y = random_hue(x, max_delta=0.2)
-
random_saturation
( x , y=None , mode='classification' ) ¶ -
画像の彩度をランダムに変動させます。
パラメータ: - x ( list of str ) -- 入力画像のパスのリスト
- y ( list of annotations ) -- アノテーションパスのリスト
- ratio ( float ) -- 彩度変動のパラメーター。指定可能範囲は[0,1]。
戻り値: 変換された画像のリストとxのアノテーションのリスト
戻り値の型: tuple
[ x (list of numpy.ndarray), # List of transformed images. y (list of annotations) # List of annotations for x. ]
例
>>> from renom_img.api.utility.augmentation.process import random_saturation >>> from PIL import Image >>> import numpy as np >>> >>> img = Image.open(img_path) >>> img.convert('RGB') >>> img = np.array(img).transpose(2, 0, 1).astype(np.float) >>> x = np.array([img]) >>> new_x, new_y = random_saturation(x, ratio=0.2)
-
random_lighting
( x , y=None , mode='classification' ) ¶ -
画像の明暗をランダムに変動させます。
パラメータ: - x ( list of str ) -- 入力画像のパスのリスト
- y ( list of annotations ) -- アノテーションパスのリスト
戻り値: 変換された画像のリストとxのアノテーションのリスト
戻り値の型: tuple
[ x (list of numpy.ndarray), # List of transformed images. y (list of annotations) # List of annotations for x. ]
例
>>> from renom_img.api.utility.augmentation.process import random_lighting >>> from PIL import Image >>> import numpy as np >>> >>> img = Image.open(img_path) >>> img.convert('RGB') >>> img = np.array(img).transpose(2, 0, 1).astype(np.float) >>> x = np.array([img]) >>> new_x, new_y = random_lighting(x)
-
random_expand
( x , y=None , mode='classification' ) ¶ -
画像をランダムに拡大させます。
パラメータ: - x ( list of str ) -- 入力画像のパスのリスト
- y ( list of annotations ) -- アノテーションパスのリスト
戻り値: 変換された画像のリストとxのアノテーションのリスト
戻り値の型: tuple
[ x (list of numpy.ndarray), # List of transformed images. y (list of annotations) # List of annotations for x. ]
例
>>> from renom_img.api.utility.augmentation.process import random_expand >>> from PIL import Image >>> import numpy as np >>> >>> img = Image.open(img_path) >>> img.convert('RGB') >>> img = np.array(img).transpose(2, 0, 1).astype(np.float) >>> x = np.array([img]) >>> new_x, new_y = random_expand(x)
-
shear
( x , y=None , mode='classification' ) ¶ -
画像の剪断変形をランダムに実施します。
パラメータ: - x ( list of str ) -- 入力画像のパスのリスト
- y ( list of annotations ) -- アノテーションパスのリスト
- max_shear_factor ( int ) -- 剪断変形のアングルパラメーター範囲
戻り値: 変換された画像のリストとxのアノテーションのリスト
戻り値の型: tuple
[ x (list of numpy.ndarray), # List of transformed images. y (list of annotations) # List of annotations for x. ]
例
>>> from renom_img.api.utility.augmentation.process import shear >>> from PIL import Image >>> import numpy as np >>> >>> img = Image.open(img_path) >>> img.convert('RGB') >>> img = np.array(img).transpose(2, 0, 1).astype(np.float) >>> x = np.array([img]) >>> new_x, new_y = shear(x, max_shear_factor=8)
renom_img.api.utility.evaluate ¶
-
class
EvaluatorClassification
( prediction , target ) ¶ -
ベースクラス:
renom_img.api.utility.evaluate.EvaluatorBase
クラス分類タスクのための評価器
パラメータ: - prediction ( list ) -- 予測されたクラスのリスト
- target ( list ) -- ターゲットクラスのリスト。フォーマットは以下の通りとなります。
"prediction" and "target"の引数の例 ¶[ class_id1(int), class_id2(int), class_id3(int), ]
例
>>> evaluator = EvaluatorClassification(prediction, target) >>> evaluator.precision() >>> evaluator.recall()
-
accuracy
( ) ¶ -
全体正解率を返します。
戻り値: 全体正解率 戻り値の型: (float)
-
f1
( ) ¶ -
各クラスのf1スコアとその平均を返します。
戻り値: 2つの値が返されます。一つは各クラスのF1スコアでもう一つはその平均です。フォーマットは以下の通りとなります。 戻り値の型: (tuple) 出力の例 ¶({ class_id1(int): f1 score(float), class_id2(int): f1_score(float) }, mean_f1_score(float))
-
precision
( ) ¶ -
各クラスの適合率とその平均を返します。
戻り値: 2つの値を返します。一つは各クラスのprecision(辞書型)、もう一つはその平均(float)です。フォーマットは以下の通りとなります。 戻り値の型: (tuple) 出力の例 ¶({ class_id1(int): precision(float), class_id2(int): precision(float), }, mean_precision(float))
-
recall
( ) ¶ -
各クラスの再現率とその平均を返します。
戻り値: 2つの値を返します。一つは各クラスの再現率(dictionary)、もう一つはその平均です。フォーマットは以下の通りとなります。 戻り値の型: (tuple) 出力の例 ¶){ class_id1(int): recall(float), class_id2(int): recall(float), }, mean_recall(float))
-
report
( round_off=3 ) ¶ -
各クラスの適合率、再現率、F1値、正解したクラス数と教師データのクラス数をデーブルとして出力します。
パラメータ: round_off ( int ) -- 出力値の小数の数 戻り値: 適合率 再現率 F1値 #pred/#target class_id1: 0.800 0.308 0.444 4/13 class_id2: 0.949 0.909 0.929 150/165 .... Average 0.364 0.500 0.421 742/1256
-
class
EvaluatorDetection
( prediction , target , num_class=None ) ¶ -
ベースクラス:
renom_img.api.utility.evaluate.EvaluatorBase
物体検出のための評価器
パラメータ: - prediction ( list ) -- 予測結果のリスト。フォーマットは以下の通りとなります。
- target ( list ) -- 教師データのボックスとクラスのリスト
- num_class ( int ) -- クラスの数
"prediction" and "target"の引数の例 ¶[ [ # Objects of 1st image. {'box': [x(float), y, w, h], 'class': class_id(int), 'score': score}, {'box': [x(float), y, w, h], 'class': class_id(int), 'score': score}, ... ], [ # Objects of 2nd image. {'box': [x(float), y, w, h], 'class': class_id(int), 'score': score}, {'box': [x(float), y, w, h], 'clas': class_id(int), 'score': score}, ... ] ]
例
>>> evaluator = EvaluatorDetection(pred, gt) >>> evaluator.mAP() >>> evaluator.mean_iou()
-
AP
( iou_thresh=0.5 , round_off=3 ) ¶ -
各クラスのAPを返します。
\(AP = 1/11 \sum_{r \in \{0.0,..1.0\}} AP_{r}\)
パラメータ: - iou_thresh -- IoU閾値。デフォルト値は0.5。
- round_off ( int ) -- 出力値の小数の数
戻り値: 各クラスのAP。 フォーマットは以下のとおりです。
戻り値の型: (dictionary)
{ class_id1(int): AP1 (float), class_id2(int): AP2 (float), class_id3(int): AP3 (float), }
-
iou
( iou_thresh=0.5 , round_off=3 ) ¶ -
各クラスのIoUを返します。
パラメータ: - iou_thresh ( float ) -- IoU閾値。デフォルト値は0.5。
- round_off ( int ) -- 出力値の小数の数
戻り値: 各クラスのIoUを返します。フォーマットは以下のとおりです。
{ class_id1(int): iou1 (float), class_id2(int): iou2 (float), class_id3(int): iou3 (float), }
戻り値の型: (dictionary)
-
mAP
( iou_thresh=0.5 , round_off=3 ) ¶ -
mAPを返します。
パラメータ: - iou_thresh ( float ) -- IoU閾値。デフォルト値は0.5。
- round_off ( int ) -- 出力値の小数の数
戻り値: mAP(mean Average Precision).
戻り値の型: (float)
-
mean_iou
( iou_thresh=0.5 , round_off=3 ) ¶ -
平均のIoUを返します。
パラメータ: - iou_thresh -- IoU閾値。デフォルト値は0.5。
- round_off ( int ) -- 出力値の小数の数
戻り値: Mean IoU
戻り値の型: (float)
-
plot_pr_curve
( iou_thresh=0.5 , class_names=None ) ¶ -
precision-recall 曲線をプロットします。
パラメータ: - iou_thresh ( float ) -- IoU閾値。デフォルト値は0.5。
- class_names ( list ) -- 予測リストのキー、もしくはある特定のクラスのみのprecision-recall曲線を出力させる場合はキーのうちの一つを文字列として渡すことでprecision-recall曲線が出力されます。
-
prec_rec
( iou_thresh=0.5 ) ¶ -
各クラスの適合率と再現率を返します。
パラメータ: iou_thresh ( float ) -- IoU閾値。デフォルト値は0.5 戻り値: 2つの値が返されます。一つは各クラスの適合率(辞書型)、もう一つはその平均の適合率(float)を返します。 戻り値の型: (tuple) 出力の例 ¶({ class_id1(int): [precision1(float), precision2(float), ..], class_id2(int): [precision3(float), precision4(float), ..], }, { class_id1(int): [recall1(float), recall2(float), ..] class_id2(int): [recall3(float), recall4(float), ..] })
-
report
( iou_thresh=0.5 , round_off=3 ) ¶ -
各クラスのAP、IoU、予測されたインスタンスの数、教師データのインスタンの数をテーブルとして表示
パラメータ: - iou_thresh ( flaot ) -- IoU閾値。デフォルト値は0.5。
- round_off ( int ) -- 出力値の小数の数
戻り値: AP IoU #pred/#target class_name1: 0.091 0.561 1/13 class_name2: 0.369 0.824 6/15 .... mAP / mean IoU 0.317 0.698 266/686
-
class
EvaluatorSegmentation
( prediction , target , num_class , ignore_class=0 ) ¶ -
ベースクラス:
renom_img.api.utility.evaluate.EvaluatorBase
セマンティックセグメンテーションタスクのための評価器
パラメータ: - prediction ( list ) -- 予測されたクラス配列のリスト
- target ( list ) -- 正解ラベルのクラス配列のリスト
- num_class ( int ) -- データセットにあるクラス数
- ignore_class ( int ) -- 評価指標計算に含めないクラスインデックス。デフォルトは0でバックグラウンドクラスを指します。バックグラウンドクラスを含めるには、データセットのクラス数より大きい数字を指定してください。
例
>>> evaluator = EvaluatorSegmentation(prediction, target, num_class=5) >>> evaluator.iou() >>> evaluator.precision()
-
f1
( round_off=3 ) ¶ -
各クラスのF1スコアとその平均を返します。
パラメータ: round_off ( int ) -- 出力値の小数の数 戻り値: 2つの値が返されます。1つは各クラスのF1スコア(dictionary)、もう一方はその平均値(float)です。 戻り値の型: (tuple)
-
iou
( round_off=3 ) ¶ -
各クラスのIoUを返します。
パラメータ: round_off ( int ) -- 出力値の小数の数 戻り値: 2つの値が返されます。1つは各クラスのIoU(dictionary)、もう一方はその平均値(float)です。 戻り値の型: (tuple)
-
precision
( round_off=3 ) ¶ -
各クラスの適合率を返します。
パラメータ: round_off ( int ) -- 出力値の小数の数 戻り値: 2つの値が返されます。一つは各クラスの適合率(dictionary)、もう一方はその平均値(float)です。 戻り値の型: (tuple)
-
recall
( round_off=3 ) ¶ -
各クラスの再現率とその平均を返します。
パラメータ: round_off ( int ) -- 出力値の小数の数 戻り値: 2つの値が返されます。一つは各クラスの再現率(dictionary)、もう一方はその平均値(float)です。 戻り値の型: (tuple)
-
report
( round_off=3 ) ¶ -
各クラスの適合率、再現率、F1値、true positiveなピクセル数と教師データのピクセル数をデーブルとして出力します。
パラメータ: round_off ( int ) -- 出力値の小数の数 戻り値: IoU 適合率 再現率 F1値 #pred/#target class_id1: 0.178 0.226 0.457 0.303 26094/571520 class_id2: 0.058 0.106 0.114 0.110 25590/224398 .... Average 0.317 0.698 0.404 0.259 5553608/18351769
renom_img.api.utility.distributor ¶
-
class
ImageDistributor
( img_path_list , label_list=None , target_builder=None , augmentation=None , imsize=None , num_worker=3 ) ¶ -
ベースクラス:
renom_img.api.utility.distributor.distributor.ImageDistributorBase
-
batch
( batch_size , target_builder=None , shuffle=True ) ¶ -
パラメータ: - batch_size ( int ) -- バッチサイズ
- target_builder ( ImageDistributor ) -- ターゲットビルダー
- shuffle ( bool ) -- データを分割する際にシャッフルするかどうか
Yields: (画像のパス(list), ラベルのパス(list))
-
split
( ratio , shuffle=True ) ¶ -
画像とラベルを分割します。
パラメータ: - ratio ( float ) -- トレーニングセットとバリデーションセットの割合
- shuffle ( bool ) -- データを分割する際にシャッフルするかどうか
-
renom_img.api.utility.visualize.grad_cam ¶
-
class
GuidedGradCam
( model_cam ) ¶ -
ベースクラス:
object
画像分類畳み込み層モデルの特徴の重視領域を可視化するツール
パラメータ: model_cam ( ReNom model instance ) -- Guided Grad-CAM可視化の対象となる画像分類CNNモデル。モデルはReNomで作成されたVGG、ResNet、ResNeXtまたはrenom.Sequentialのインスタンスである必要があります。また、事前にモデルをGrad-CAM可視化の対象となるデータセットで学習させる必要があります。モデルは活性化関数にReLuを利用する必要があります。 戻り値: 3つの配列が返されます:Guided backpropagationの配列、Grad-CAM(++)顕著性マップの配列及びGuided Grad-CAM(++)の配列です。 戻り値の型: (numpy.ndarray) 例
>>> #This sample uses matplotlib to display files, so it is recommended to run this inside a Jupyter Notebook >>> >>> import renom as rm >>> import numpy as np >>> from PIL import Image >>> import matplotlib.pyplot as plt >>> from matplotlib.pyplot import cm >>> from renom_img.api.classification.vgg import VGG16 >>> from renom_img.api.utility.visualize.grad_cam import GuidedGradCam >>> from renom_img.api.utility.visualize.tools import load_img, preprocess_img, visualize_grad_cam >>> >>> model = VGG16() >>> >>> #Provide pre-trained model weights for same dataset you are producing Grad-CAM visualizations on >>> model.load("my_pretrained_weights.h5") >>> >>> #Create Grad-CAM instance based on pre-trained model (VGG, ResNet, ResNeXt, or rm.Sequential) >>> grad_cam = GuidedGradCam(model) >>> >>> #Provide path to image file for producing Grad-CAM visualizations >>> img_path = '/home/username/path/to/images/cat_dog.jpg' >>> >>> #Load and pre-process image (must be same pre-processing as used during training) >>> img = Image.open(img_path) >>> size=(224,224) >>> img = load_img(img_path, size) >>> x = preprocess_img(img) >>> >>> #Select class_id (index of array in model's final output) to produce visualizations for. Must be consistent with class ID in trained model. >>> class_id = 243 >>> >>> #Generate Grad-CAM maps >>> input_map, L, result = grad_cam(x, size, class_id=class_id, mode='normal') >>> >>> #Overlay Grad-CAM saliency map on image using matplotlib >>> plt.imshow(img) >>> plt.imshow(L, cmap = cm.jet, alpha = 0.6) >>> plt.axis("off") >>> plt.savefig("grad_cam_sample.png", bbox_inches='tight', pad_inches=0) >>> >>> #Visualize Guided Grad-CAM (original image, guided backpropagation, Grad-CAM saliency map, Guided Grad-CAM visualization) >>> visualize_grad_cam(img, input_map, L, result) >>> >>> #Generate Grad-CAM++ maps >>> input_map, L, result = grad_cam(x, size, class_id=class_id, mode='plus') >>> #Visualize results (original image, guided backpropagation, Grad-CAM++ saliency map, Guided Grad-CAM++ visualization) >>> visualize_grad_cam(img, input_map, L, result)
引用
Ramprasaath R. Selvaraju, Michael Cogswell, Abhishek Das, Ramakrishna Vedantam, Devi Parikh, Dhruv BatraGrad-CAM: Visual Explanations from Deep Networks via Gradient-based Localization
Aditya Chattopadhyay, Anirban Sarkar, Prantik Howlader, Vineeth N BalasubramanianGrad-CAM++: Improved Visual Explanations for Deep Convolutional Networks
-
check_for_relus
( model ) ¶ -
モデルにはReLuが存在するかどうかを確認します。
パラメータ: model_cam ( ReNom model instance ) -- Guided Grad-CAM可視化の対象となる画像分類CNNモデル。モデルはReNomで作成されたVGG、ResNet、ResNeXtまたはrenom.Sequentialのインスタンスである必要があります。また、事前にモデルをGrad-CAM可視化の対象となるデータセットで学習させる必要があります。モデルは活性化関数にReLuを利用する必要があります。 戻り値: assertの結果です。 戻り値の型: (bool)
-
forward_cam
( x , class_id , mode , node ) ¶ -
Grad-CAMのフォワードパスを計算します。
パラメータ: - x ( renom.Variable ) -- 前処理後のモデル入力データです。
- class_id ( int ) -- 可視化対象となるクラス番号です。
- mode ( string ) -- Grad-CAMまたはGrad-CAM++を切り替えるフラグです。
- node ( int ) -- モデルの最終畳み込み層を表すインデックス(rm.Sequentialモデルの場合のみ)です。
戻り値: 最終層の出力及び最終畳み込み層の出力です。
戻り値の型: (renom.Variable)
-
forward_gb
( x_gb , class_id , mode ) ¶ -
Guided backpropagation用のフォワードパスを計算します。
パラメータ: - x_gb ( renom.Variable ) -- 前処理後のモデル入力データです。
- class_id ( int ) -- 可視化対象となるクラス番号です。
- mode ( string ) -- Grad-CAMまたはGrad-CAM++を切り替えるフラグです。
戻り値: 最終層の出力です。
戻り値の型: (renom.Variable)
-
generate_map
( y_c , final_conv , gb_map , mode , size ) ¶ -
Guided Grad-CAMマップ及び顕著性マップをnumpy配列として生成します。
パラメータ: - y_c ( renom.Variable ) -- フォワードパス時のモデル最終層の出力です。
- final_conv ( renom.Variable ) -- フォワードパス時のモデル最終畳み込み層の出力です。
- gb_map ( numpy.ndarray ) -- 正規化されたguided backpropagation出力を表すnumpy配列です。
- mode ( string ) -- Grad-CAM ('normal'、デフォルト値)またはGrad-CAM++ ('plus')を切り替えるフラグです。
戻り値: Grad-CAM顕著性マップ及びGuided Grad-CAMマップのnumpy配列です。
戻り値の型: (numpy.ndarray)
-
get_model_type
( model_cam ) ¶ -
Grad-CAMインスタンスに引き渡されたモデルの種類を取得します。
パラメータ: model_cam ( ReNom model instance ) -- Guided Grad-CAM可視化の対象となる画像分類CNNモデル。モデルはReNomで作成されたVGG、ResNet、ResNeXtまたはrenom.Sequentialのインスタンスである必要があります。また、事前にモデルをGrad-CAM可視化の対象となるデータセットで学習させる必要があります。モデルは活性化関数にReLuを利用する必要があります。 戻り値: クラス名の配列 戻り値の型: (string)
-
get_predicted_class
( x ) ¶ -
入力データに対するモデルの予測クラスを返します。
パラメータ: - x ( renom.Variable ) -- 前処理後のモデル入力データです。
- class_id ( int ) -- 可視化対象となるクラス番号です。
- mode ( string ) -- Grad-CAM ('normal'、デフォルト値)またはGrad-CAM++ ('plus')を切り替えるフラグです。
戻り値: モデル最終出力のnp.argmaxインデックスです。
戻り値の型: (int)
-
get_scaling_factor
( size , L ) ¶ -
Grad-CAMマップと入力画像のサイズを揃えるためのスケーリングを計算します。
パラメータ: - size ( tuple ) -- 入力画像サイズを表す整数のタプルです。
- L ( ndarray ) -- Grad-CAMの顕著性マップです。
戻り値: 出力配列のサイズを揃えるためのスケーリング要素です。
戻り値の型: float, float
-
guided_backprop
( x_gb , y_gb ) ¶ -
Guided backpropagationのバックワードパスを計算します。
パラメータ: - x_gb ( renom.Variable ) -- 前処理後のモデル入力データです。
- y_gb ( renom.Variable ) -- x_gbに対するGuided backpropagationフォワードパスの出力です。
戻り値: Guided backpropagationの出力です。
戻り値の型: (numpy.ndarray)
-
renom_img.api.utility.misc ¶
-
draw_box
( img , prediction , show_size=None , font_path=None , color_list=None ) ¶ -
入力画像に対するバウンディングボックスとクラス名とスコアを描く関数
パラメータ: - img ( string , ndarray ) -- 画像と画像の配列のパス
- prediction ( list ) -- アノテーションのリストを返却します。各アノテーションはキーとしてボックスと名前を含むような辞書のリストからなります。構造は以下に示す通りです。
[ {'box': [x(float), y, w, h], 'name': class name(string), 'score': score(float)}, {'box': [x(float), y, w, h], 'name': class name(string), 'score': score(float)}, ... ] font_path(string): Path to font file for showing object's name. If None is given, default font will be used. color_list(list): A list of color for rendering bounding boxes. If None is given, default color list will be used.
戻り値: フォームが変更されたバウンディングボックスを返却します。 戻り値の型: (PIL.Image) 例
>>> from PIL import Image >>> from renom_img.api.utility.load import * >>> prediction = parse_xml_detection(prediction_xml_path_list)[0] >>> bbox_image = draw_bbox(img_path, prediction)
注釈
boxの値は相対座標なので、[0.0 ~ 1.0]の範囲を取ります。ndarrayとしてimgを引数として渡す場合には、(channel, height, width)のフォーマットになります。例えば、サイズが(100, 10)のRGB画像の場合は、行列の型はは(3, 10 100)となります。
-
draw_segment
( img , prediction , color_list=None , show_background=True ) ¶ -
prediction引数に対応するセグメントを記述する関数
パラメータ: - img ( string , ndarray ) -- 画像と画像の配列のパス
- prediction ( ndarray ) -- 予測されたアノテーションのリスト。これはimageに等しいサイズの行列でなければなりません。
- color_list ( list ) -- バウンディングボックスのレンダリングに利用する色のリスト。Noneが与えられた場合、デファルトのろのリストが利用されます。
- show_background ( bool ) -- Falseの場合、id 0のクラスの背景色は描写されません。
戻り値: 予測結果が描写された画像を返します。
戻り値の型: (PIL.Image)
例
>>> from PIL import Image >>> prediction = Image.open(predicticted_result) >>> image = Image.open(img_path) >>> bbox_image = draw_segment(img_path, prediction)
注釈
引数imgをndarrayとして渡す場合、フォーマットは(chanel, height, width)でなければなりません同様に、引数predictionは(channel, height, width)の行列でなければなりません。例えば、RGBでサイズが(100, 10)の画像の場合、行列の型は(3, 10, 100)となります。
-
pil2array
( img ) ¶ -
PIL画像をnumpy配列に変換する関数
例
>>> from renom_img.api.utility.misc.display import pil2array >>> from PIL import Image >>> img = Image.open(img_path) >>> converted_img = pil2array(img)
パラメータ: img ( PIL.Image ) -- PIL画像 戻り値: numpy配列オブジェクトを返します。 戻り値の型: (numpy.ndarray)