Using .Pth Files

Using .pth files

As described in the documentation, PTH files are only processed if they are in the site-packages directory. (More precisely, they are processed if they are in a "site directory", but "site directory" itself is a setting global to the Python installation and does not depend on the current directory or the directory where the script resides.)

If the directory containing your script is on sys.path, you could create a sitecustomize.py in that directory. This will be loaded when Python starts up. Inside sitecustomize.py, you can do:

import site
site.addsitedir('/some/dir/you/want/on/the/path')

This will not only add that directory, but will add it as a "site directory", causing PTH files there to be processed. This is handy if you want to create your own personal site-packages-like-directory.

If you only need to add one or two directories to the path, you could do so more simply. Just create a tiny Python library that manipulates sys.path, and then import that library from your script. Something like:

# makepath.py
import sys
sys.path.append('/whatever/dir/you/want')

# script.py
import makepath

Edit: Again, according to the documentation, there is the possibility of a site-specific directory in %APPDATA%\Python\PythonXY\site-packages (on Windows). You could try that, if in fact you have write access to that (and not just to your script directory).

how to use pretrained model .pth in pytorch?

You can load the parameters inside from a.pt/h into a model like this:

# initialize a model with the same architecture as the model which parameters you saved into the .pt/h file
model = Model()

# load the parameters into the model
model.load_state_dict(torch.load("parameters.pth"))

What is the difference between .pt, .pth and .pwf extentions in PyTorch?

There are no differences between the extensions that were listed: .pt, .pth, .pwf. One can use whatever extension (s)he wants. So, if you're using torch.save() for saving models, then it by default uses python pickle (pickle_module=pickle) to save the objects and some metadata. Thus, you have the liberty to choose the extension you want, as long as it doesn't cause collisions with any other standardized extensions.

Having said that, it is however not recommended to use .pth extension when checkpointing models because it collides with Python path (.pth) configuration files. Because of this, I myself use .pth.tar or .pt but not .pth, or any other extensions.


The standard way of checkpointing models in PyTorch is not finalized yet. Here is an open issue, as of this writing: Recommend a different file extension for models (.PTH is a special extension for Python) - issues/14864

It's been suggested by @soumith to use:

  • .pt for checkpointing models in pickle format
  • .ptc for checkpointing models in pytorch compiled (for JIT)

Viewing Pytorch weights from a *.pth file

import torch

model = torch.load('path')
print(model)

(Verify and confirm)



Related Topics



Leave a reply



Submit