augment增强:

一、Padding+Resize 

import matplotlib.pyplot as plt

from matplotlib.axes import Axes
import torchvision.transforms as T
from PIL import Image


img = Image.open("cat.png")
img = img.convert('L')

def plot_figure(imgs, operation, altitude, with_origin=True):

    n_rows, n_cols = len(imgs), len(imgs[0]) + with_origin
    fig, axs = plt.subplots(n_rows, n_cols, figsize=(8, 4), squeeze=False, dpi=100)
    for i in range(n_rows):
        for j in range(n_cols):
            if with_origin and j == 0:
                axs[i, 0].set_title('Origin image',fontsize=8)
                axs[i, 0].imshow(img, cmap='gray')
            else:
                axs[i, j].set_title(f"{operation[0]} : {altitude[0][i + j-with_origin]}  {operation[1]} : {altitude[1][i + j-with_origin]}",
                                    fontsize=8)
                axs[i, j].imshow(imgs[i][j-with_origin], cmap='gray')
            axs[i, j].set(xticklabels=[],yticklabels=[],xticks=[],yticks=[])

    plt.savefig('padding_resize.svg', dpi=200, bbox_inches='tight')
    # plt.show()


operation = {'padding': [10, 30, 60], 'resize': [80, 100, 200], 'degree': [10, 80, 180]}
n = 3
imgs = [T.Pad(padding=operation['padding'][i])(img) for i in range(n)]
imgs = [T.Resize(size=operation['resize'][i])(img) for i, img in enumerate(imgs)]



imgs = [imgs]
plot_figure(imgs, list(operation.keys()), list(operation.values()))

二、Elastic+Rotation 

import matplotlib.pyplot as plt
 
from matplotlib.axes import Axes
import torchvision.transforms as T
from PIL import Image
 
 
img = Image.open("cat.png")
img = img.convert('L')
 
def plot_figure(imgs, operation, altitude, with_origin=True):
 
    n_rows, n_cols = len(imgs), len(imgs[0]) + with_origin
    fig, axs = plt.subplots(n_rows, n_cols, figsize=(8, 4), squeeze=False, dpi=100)
    for i in range(n_rows):
        for j in range(n_cols):
            if with_origin and j == 0:
                axs[i, 0].set_title('Origin image',fontsize=8)
                axs[i, 0].imshow(img, cmap='gray')
            else:
                axs[i, j].set_title(f"{operation[0]} : {altitude[0][i + j-with_origin]}  {operation[1]} : {altitude[1][i + j-with_origin]}",
                                    fontsize=8)
                axs[i, j].imshow(imgs[i][j-with_origin], cmap='gray')
            axs[i, j].set(xticklabels=[],yticklabels=[],xticks=[],yticks=[])
 
    plt.savefig('Elastic_rotation.svg', dpi=200, bbox_inches='tight')
    # plt.show()
 
 
operation = {'padding': [10, 30, 60], 'resize': [80, 100, 180], 'degree': [10, 80, 180],'alpha':[20.0,100.0,250.0]}
n = 3
imgs = [T.ElasticTransform(alpha=operation['alpha'][i])(img) for i in range(n)]
imgs = [T.RandomRotation(degrees=operation['degree'][i])(img) for i, img in enumerate(imgs)]
 
 
 
imgs = [imgs]
plot_figure(imgs, ['Elastic','rotation'], list(operation.values()))

你可能感兴趣的:(python)