Loading and saving torchvision transforms

username: JMunz
cooked: <p>Is it possible to save and load a transform for an image? I would like to have a folder of transformations and load them into different models if possible.</p>

username: Matias_Vasquez
cooked: <p>You can make a .py file that contains your custom transformations and then import it</p>
<pre><code class="lang-python"># my_transforms.py
import torchvision.transforms as T

der my_cstm_trnsfrm_1(img):
	preprocess = T.Compose([
  		T.Resize(256),
  		T.CenterCrop(224),
  		T.ToTensor(),
  		T.Normalize(
      			mean=[0.485, 0.456, 0.406],
      			std=[0.229, 0.224, 0.225]
  		)
	])
	return preprocess	
</code></pre>
<p>Then you can import it to where you define your dataset you can use something like <a href="https://pytorch.org/vision/stable/generated/torchvision.datasets.ImageFolder.html#torchvision.datasets.ImageFolder" rel="noopener nofollow ugc">Image Folder</a> and pass your transform as parameter.</p>

username: JMunz
cooked: <p>How about using scriptable transformations with torch.nn.Sequential? I tried this out on my image and got a different transformation with the same resize settings</p>

username: Matias_Vasquez
cooked: <p>Maybe you can post your code so it will be easier.</p>

username: JMunz
cooked: <p>My goal is still to output that transformation to a file to read it in elsewhere where the original python file may not have access to. I guess pickle might be the only way since it seems you can only output transformations made to the model itself and they are not exactly the same as using Compose</p>

username: Matias_Vasquez
cooked: <p>Hmmm, sorry I don`t think that I am understanding your goal.</p>
<p>If I understand correctly, what you want is a custom module to use inside of your models/architectures (NOT some transform preprocessing for the dataset).</p>
<p>If that is the case you can create your custom <a><code>nn.Module</code></a> that you can then use it in your models either alone or inside a <a href="https://pytorch.org/docs/stable/generated/torch.nn.Sequential.html" rel="noopener nofollow ugc"><code>nn.Sequential</code></a></p>
<p>Still, I think it would be helpful to see the code of what you are trying to do.</p>

username: Matias_Vasquez
cooked: <p>Maybe you mean something like this</p>
<pre><code class="lang-python"># File to declare your nn.Module that you want to load somewhere else
class MyCell(torch.nn.Module):
    def __init__(self):
        super(MyCell, self).__init__()

    def forward(self, x, h):
        return x + h

cell = torch.jit.script(MyCell())
print(cell.code)
cell.save('my_cell.pt')
</code></pre>
<pre><code class="lang-python"># Other file where you load that module and use it
class MyOtherCell(torch.nn.Module):
    def __init__(self):
        super(MyOtherCell, self).__init__()
        self.loaded = torch.jit.load('my_cell.pt')
        
    def forward(self, x, h):
        return self.loaded(self.loaded(x, h), h)

x = torch.tensor([3, 4])
h = torch.tensor([1, 2])

module = MyOtherCell()
print(module(x, h))
</code></pre>
<p>If so, you might want to look into <a href="https://pytorch.org/tutorials/beginner/Intro_to_TorchScript_tutorial.html" rel="noopener nofollow ugc">torchscript</a>.</p>

username: hleborgne
cooked: <ol>
<li>Save the <em>name</em> of the transform in YAML</li>
<li>Load it and reconstruct the transform</li>
</ol>
<pre><code class="lang-auto">import yaml

data_transforms_name =  "ResNet50_Weights.IMAGENET1K_V2"


with open("config.yaml", "w") as f:
    yaml.dump(data_transforms_name, f)
</code></pre>
<pre><code class="lang-auto">import yaml
from torchvision.models import ResNet50_Weights # need to import all possible transforms

with open("config.yaml", "r") as f:
    data_transforms_name = yaml.safe_load(f)


weights_enum = eval(data_transforms_name)
transform = weights_enum.transforms()

</code></pre>
<p>for custom transforms, name and import them</p>