How to create your own Transform

Reference page: Transform summary that can be used with Pytorch – torchvision

In transforms used for preprocessing of image data, user-defined Transform can be created by passing Lambda function.

from torchvision import transforms
import  cv2
import matplotlib.pyplot as plt

img = cv2.imread("sample.jpeg ")

plt.imshow(img)

ダウンロード.png

def gray(img):
    """
Convert to RGB and grayscale
    """
    
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    
    return img

transform = transforms.Lambda(gray)

img_transformed = transform(img)
plt.imshow(img_transformed)

ダウンロード (1).png

If you connect this process with Compose, you can incorporate it in the pipeline of pytorch's transform.

Recommended Posts

How to create your own Transform
How to install your own (root) CA
Create your own exception
How to define your own target in Sage
[Introduction to pytorch-lightning] How to use torchvision.transforms and how to freely create your own dataset ♬
Create your own Django middleware
Memo to create your own Box with Pepper's Python
[For recording] Keras image system Part 1: How to create your own data set?
[Ansible] How to call variables when creating your own module
How to create a Conda package
How to use pyenv and pyenv-virtualenv in your own way
How to create an email user
How to create a virtual bridge
How to create / delete symbolic links
(Note) How to pass the path of your own module
Create your own name resolution service
[Django] Create your own 403, 404, 500 error pages
How to create a Dockerfile (basic)
Bridge ROS to your own protocol
Try HeloWorld in your own language (with How to & code)
How to create a config file
Make the theme of Pythonista 3 like Monokai (how to make your own theme)
How to make your own domain site with heroku (free plan)
How to build my own Linux server
How to create a clone from Github
Add your own content view to mitmproxy
Create your own Linux commands in Python
How to create a git clone folder
[LLDB] Create your own command in Python
How to create * .spec files for pyinstaller.
Migrate your own CMS data to WordPress
Create your own DNS server with Twisted
To import your own module with jupyter
How to create a repository from media
[python] How to add RDF triples to your own Fuseki server using rdflib
How to access data with object ['key'] for your own Python class
Try to make your own AWS-SDK with bash
How to create sample CSV data with hypothesis
How to create large files at high speed
How to create a Python virtual environment (venv)
How to create a function object from a string
How to create a JSON file in Python
How to create an OCF compliant resource agent
How to create data to put in CNN (Chainer)
How to create a shortcut command for LINUX
Annotate your own data to train Mask R-CNN
[Note] How to create a Ruby development environment
How to create a Kivy 1-line input box
How to create a multi-platform app with kivy
How to create a Rest Api in Django
Create a wheel of your own OpenCV module
[Note] How to create a Mac development environment
How to create random numbers with NumPy's random module
Steps to install your own library with pip
Read the Python-Markdown source: How to create a parser
How to use xml.etree.ElementTree
How to create an article from the command line
How to use virtualenv
Scraping 2 How to scrape
How to use Seaboan
Create your own Big Data in Python for validation