[PYTHON] I wrote Gray Scale in Pytorch

Background When I tried to Glay Scale with Pytorch, there was no Function, so I converted it to PIL once, Gray Scaled it, and then converted it back to Torch Tensor, so I thought it was a waste of calculation cost. Please support me as much as Gray Scale. .. ..

I'm sorry if there is

Code

By the way, it seems that there are various types of Gray Scale, so you can use your favorite parameters.

    def getGrayImage(self,rgbImg):
        gray = 0.114*rgbImg[:,0,:,:] + 0.587*rgbImg[:,1,:,:] + 0.299*rgbImg[:,2,:,:]
        gray = torch.unsqueeze(gray,1)
        return gray

Conclusion Please let me know if there is a GrayScale Function in Pytorch!

References Grayscale image prank https://qiita.com/yoya/items/96c36b069e74398796f3

Recommended Posts

I wrote Gray Scale in Pytorch
I wrote python in Japanese
I wrote Fizz Buzz in Python
I wrote the queue in Python
I wrote the stack in Python
I wrote the selection sort in C
I wrote Project Euler 1 in one liner.
I wrote the sliding wing in creation.
I wrote matplotlib
A memo that I wrote a quicksort in Python
I wrote a class in Python3 and Java
I wrote "Introduction to Effect Verification" in Python
I wrote a design pattern in kotlin Prototype
I wrote the hexagonal architecture in go language
[PyTorch] I was a little lost in torch.max ()
I wrote a Japanese parser in Japanese using pyparsing.
I wrote a design pattern in kotlin Factory edition
I wrote a design pattern in kotlin Builder edition
I wrote a design pattern in kotlin Adapter edition
I wrote a design pattern in kotlin, Iterator edition
I wrote a design pattern in kotlin Template edition
I wrote the basic operation of Seaborn in Jupyter Lab
I wrote an empty directory automatic creation script in Python
I wrote it in Go to understand the SOLID principle
I wrote a script to get a popular site in Japan
I wrote the basic operation of Numpy in Jupyter Lab.
[Introduction to Pytorch] I want to generate sentences in news articles
I wrote a script that splits the image in two
I wrote GP with numpy
Implement Style Transfer in Pytorch
Fold Pytorch Dataset in layers
I understand Python in Japanese!
I made Word2Vec with Pytorch
What I learned in Python
I participated in AtCoder (ABC158)
I wrote a function to load a Git extension script in Python
I wrote a script to extract a web page link in Python