記事ディレクトリ
- 1. 実験の紹介
- 2. 実験環境
-
- 1. 仮想環境を構成する
- 2. ライブラリバージョンの紹介
- 3. 実験内容
-
- 0. 必要なライブラリをインポートする
- 1. PILの基本動作
- 2~4. ランダムオクルージョン、ランダム消去、リニアブレンディング
- 5. 画像合成
-
- 5.1 原則
- 5.2 実装
- 5.3 エフェクト表示
- 6. 画像の融合
-
- 6.1 原則
- 6.2 実装
- 6.3 エフェクト表示
1. 実験の紹介
ディープ ラーニング タスクにおいて、データの強化は、モデルの一般化能力を向上させるための重要な手順の 1 つです。トレーニング セットを変換および拡張することで、データ量を効果的に増やすことができ、サンプル間の差異を導入することができ、モデルをさまざまな入力にさらに適応させることができます。
この実験では、 画像合成 (貼り付け結合)、画像融合 (2 つの画像を融合するためのガウス マスクの作成) などのカスタム画像データ拡張操作を引き続き実装します。 )
2. 実験環境
1. 仮想環境を構成する
conda create -n Image python=3.9
conda activate Image
conda install pillow numpy
2. ライブラリバージョンの紹介
ソフトウェアパッケージ | 今回の実験版は |
---|---|
しこり | 1.21.5 |
パイソン | 3.9.13 |
枕 | 9.2.0 |
3. 実験内容
0. 必要なライブラリをインポートする
import numpy as np
from PIL import Image
1. PILの基本動作
【ディープラーニング実験】画像処理(1):Python Imaging Library(PIL)ライブラリ:画像の読み込み、書き込み、コピー、ペースト、幾何変換、画像強調、画像フィルタリング
【ディープラーニング実験】画像処理(2):PILとPyTorchでの画像処理とランダム画像強調(変換)
2~4. ランダムオクルージョン、ランダム消去、リニアブレンディング
[ディープラーニング実験] 画像処理 (3): PIL - カスタム画像データ強調操作 (ランダム オクルージョン、消去、線形混合)
5. 画像合成
5.1 原則
-
入力画像:
- 画像 1 \text{画像 1}图像1
- 画像 2 \text{画像 2}图像2
- 画像 1 \text{画像 1}图像1
-
マスキングと選択:
- 画像 1 のオクルージョン領域 x x x:
- 画像 1 内の遮蔽する領域をランダムに選択します x x x (トレーニング データに変動を導入します)
- 画像 2 から対応する領域を選択します y y y:
- 画像 1 の遮蔽された領域を選択します x x x 画像 2 の対応する領域 y y そして
- 画像 1 のオクルージョン領域 x x x:
-
ペースト:
- 将 y y y を画像 1 x x x 移動:
- 画像 2 から選択する領域 y y y 画像 1 のブロックされた領域に貼り付けます x x x の位置 (画像ブレンド効果をシミュレート)
- 将 y y y を画像 1 x x x 移動:
-
出力:
- 強化された画像 1 を返します。これには貼り付けられた領域が含まれています y はい。
5.2 実装
class Combine(object):
def __init__(self,x_start, y_start, x_end, y_end):
self.x_start = x_start
self.y_start = y_start
self.x_end = x_end
self.y_end = y_end
def __call__(self, img1, img2):
# Masking out a region x of image1
img1_array = np.array(img1)
img1_array[self.y_start:self.y_end, self.x_start:self.x_end] = 0
img1_masked = Image.fromarray(img1_array.astype('uint8')).convert('RGB')
# Selecting a region y of the same as x from image2
region_y = img2.crop((self.x_start, self.y_start, self.x_end, self.y_end))
# Pasting region y on the location of x of image1
img1_masked.paste(region_y, (self.x_start, self.y_start))
return img1_masked
5.3 エフェクト表示
img1 = Image.open('3.png').convert('RGB')
img2 = Image.open('2.png').convert('RGB')
combine = Combine(628, 128, 1012, 512)
img = combine(img1,img2)
img.save('./combine_image.png')
6. 画像の融合
6.1 原則
ガウス カーネル関数を使用してマスクを作成し、2 つの画像を融合します。
- 調整サンプル x j x_j バツj (2.jpg) サンプルと一致するサイズ x i x_i バツi (1.jpg);
- 在 x i x_i バツi (或 x j x_j バツj ) ランダムな位置を選択します C C C;
- 2D 標準ガウス カーネル関数を使用してマスクを作成する G G G,确保其中心与位置 C C C 对齐,并且其大小与 x i x_i バツi マッチ;
- 使用 G G G 修改 x i x_i バツi ,并使用 1 − G 1-G 1−G 修改 x j x_j バツj ;
- 取得した変更を組み合わせて x ^ \hat x を取得します。バツ^;
- 返回 x ^ \hat x バツ^。
6.2 実装
class Gaussian(object):
def __init__(self, sigma):
# 混合参数
self.sigma = sigma
def __call__(self, img1, img2):
# Choose a random position, labeled as $C$, within $x_i$ (or $x_j$)
self.size = img1.shape[1], img1.shape[0]
print(self.size)
x = np.random.randint(0, img1.shape[1])
y = np.random.randint(0, img1.shape[0])
position_c = (x, y)
print(position_c)
# Create mask $G$ using a 2D standard Gaussian kernel function,
# ensuring its center aligns with position $C$, and the size of $G$ matches that of $x_i$
mask_g = self.gaussian_mask(position_c)
# print(mask_g.shape)
mask_g = np.expand_dims(mask_g, axis=2)
mask_g = np.repeat(mask_g, 3, axis=2)
# print(mask_g.shape)
# Use $G$ to modify $x_i$ and use $1-G$ to modify $x_j$
# Combine the resulting modifications together as $\hat x$
hat_x = img1 * mask_g + img2 * (1 - mask_g)
return hat_x
def gaussian_mask(self, center):
x, y = np.meshgrid(np.arange(0, self.size[0]), np.arange(0, self.size[1]))
d = np.sqrt((x - center[0]) ** 2 + (y - center[1]) ** 2)
gaussian_mask = np.exp(-(d ** 2 / (2.0 * self.sigma ** 2)))
return gaussian_mask
6.3 エフェクト表示
# Input two images, which are image1 (1.jpg) and image2 (2.jpg)
img1 = Image.open('2.png').convert('RGB')
img2 = Image.open('3.png').convert('RGB')
# Adjust the size of Sample $x_j$ (2.jpg) to match Sample $x_i$ (1.jpg)
img2 = img2.resize(img1.size, Image.Resampling.BICUBIC)
img1 = np.array(img1)
img2 = np.array(img2)
gaussian = Gaussian(300)
img = gaussian(img1,img2)
img = Image.fromarray(img.astype('uint8')).convert('RGB')
img.save('./gaussian_image.png')