numpy/pytorch vs. einops¶
- transpose : transpose vs. rearrange
- reshape : reshape/view vs. rearrange
- upsize : repeat/upsample vs. repeat
- downsize : interpolate vs. reduce
- split : split vs. rearrange
- permute : permute vs. rearrange
rearrange함수로 앵간한 건 다 다루는 듯
In [8]:
import torch
import torch.nn.functional as F
import matplotlib.pyplot as plt
import numpy as np
from torch import nn
from torch import Tensor
from PIL import Image
from torchvision.transforms import Compose, Resize, ToTensor
from einops import rearrange, reduce, repeat
from einops.layers.torch import Rearrange, Reduce
from torchsummary import summary
In [14]:
x = Image.open('./data/ac2.jpg')
x
Out[14]:
In [15]:
x = x.resize((224,224))
tf_toTensor = ToTensor()
x = tf_toTensor(x)
x = rearrange(x,'c h w -> h w c')
x.shape
Out[15]:
torch.Size([224, 224, 3])
flatten¶
In [18]:
rearrange(x, 'h w c -> (h w c)').shape
"""
input tensor : x
input shape : height, width, channel로 명시함
output shape : fuse height, width, channel by multiplying them together
()연산이 product operation
"""
Out[18]:
torch.Size([150528])
* flatten in numpy¶
In [20]:
np.reshape(x, (-1,)).shape
Out[20]:
torch.Size([150528])
Images to patches ***¶
split an image into 2x2 patches
In [23]:
rearrange(x, "(p1 h) (p2 w) c -> (p1 p2) h w c", p1=2, p2=2).shape
"""
input shape: (p1 h) : split height into 2, (p2 w) : split width into 2
output shape : (p1 p2) : an array of 4 image patches
"""
Out[23]:
torch.Size([4, 112, 112, 3])
Mixing 2 images¶
each image is a mix of input images
In [168]:
def preprocess(x):
x = x.resize((224,224))
tf_toTensor = ToTensor()
x = tf_toTensor(x)
x = rearrange(x,'c h w -> h w c')
return x
In [169]:
x = Image.open('./data/ac2.jpg')
y = Image.open('./data/ac4.jpg')
x = (preprocess(x)).numpy()
y= (preprocess(y)).numpy()
imgs = np.array([x,y])
In [170]:
imgs = rearrange(imgs, 'b (k h) w c -> k b h w c', k=2)
In [171]:
imgs.shape
Out[171]:
(2, 2, 112, 224, 3)
In [138]:
imgs[::2].shape
Out[138]:
(1, 2, 112, 224, 3)
In [172]:
imgs=np.concatenate([imgs[::2], imgs[1::2,::-1]],axis=0)
#imgs[::2] => upper image
#imgs[1::2,::-1] ==> lower image reverse
imgs = rearrange(imgs, "i j h w c -> j (i h) w c")
In [173]:
r1=(imgs[0]*256).astype(np.uint8)
Image.fromarray(r1)
Out[173]:
In [174]:
r2=(imgs[1]*256).astype(np.uint8)
Image.fromarray(r2)
Out[174]:
RGB to grayscale¶
In [190]:
x = Image.open('./data/ac2.jpg')
x = x.resize((224,224))
tf_toTensor = ToTensor()
x = tf_toTensor(x)
x = rearrange(x,'c h w -> h w c')
In [191]:
img = reduce(x, "h w c -> h w", 'mean')
upsampling¶
In [192]:
img = repeat(x, "h w c -> (h 2) (w 2) c")
downsampling¶
In [194]:
img = reduce(img, "(h 2) (w 2) c-> h w c", 'mean')
In [ ]:
In [ ]: