# Python expansion array (grayscale image is expanded to 3-channel color image, array is expanded to color image)

created at 11-22-2021 views: 5

Let the image size be n * n

The problem is: change the n*n array to n*n*3

## 1. numpy implementation¶

temp = np.expand_dims(img,axis=2).repeat(3,axis=2)


## 2.Pytorch implementation¶

>>> import torch
>>>
>>> # Define a 33x55 tensor
>>> a = torch.randn(33, 55)
>>> a.size()
torch.Size([33, 55])
>>>
>>> # Let's try the effect of the repeat function with different parameters
>>> a.repeat(1,1).size() # Original value: torch.Size([33, 55])
torch.Size([33, 55])
>>>
>>> a.repeat(2,1).size() # Original value: torch.Size([33, 55])
torch.Size([66, 55])
>>>
>>> a.repeat(1,2).size() # Original value: torch.Size([33, 55])
torch.Size([33, 110])
>>>
>>> a.repeat(1,1,1).size() # Original value: torch.Size([33, 55])
torch.Size([1, 33, 55])
>>>
>>> a.repeat(2,1,1).size() # Original value: torch.Size([33, 55])
torch.Size([2, 33, 55])
>>>
>>> a.repeat(1,2,1).size() # Original value: torch.Size([33, 55])
torch.Size([1, 66, 55])
>>>
>>> a.repeat(1,1,2).size() # Original value: torch.Size([33, 55])
torch.Size([1, 33, 110])
>>>
>>> a.repeat(1,1,1,1).size() # Original value: torch.Size([33, 55])
torch.Size([1, 1, 33, 55])
>>>
>>> # ------------------ Cut ------------------
>>> # The number of parameters of repeat() cannot be less than the number of dimensions of the tensor being operated on,
>>> # Here are some examples of errors
>>> a.repeat(2).size() # 1D <2D, error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: Number of dimensions of repeat dims can not be smaller than number of dimensions of tensor
>>>
>>> # Define a 3-dimensional tensor, and then show the error mentioned earlier
>>> b = torch.randn(5,6,7)
>>> b.size() # 3D
torch.Size([5, 6, 7])
>>>
>>> b.repeat(2).size() # 1D <3D, error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: Number of dimensions of repeat dims can not be smaller than number of dimensions of tensor
>>>
>>> b.repeat(2,1).size() # 2D <3D, error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: Number of dimensions of repeat dims can not be smaller than number of dimensions of tensor
>>>
>>> b.repeat(2,1,1).size() # 3D = 3D, okay
torch.Size([10, 6, 7])
>>>


## 3 from einops import rearrange, repeat¶

result_patch = repeat(result_patch, 'h w -> h w c', c=3)

created at:11-22-2021