To use your own neural network architecture, wrap your PyTorch model in a CustomModel object. Your model must be a subclass of torch.nn.Module and implement a forward method that returns a tensor of shape (batch_size, features).
You must provide:
name: A string identifier for the model.model: The PyTorch model instance.transform: A function (e.g., torchvision.transforms.Compose) that converts a PIL.Image into a PyTorch tensor compatible with your model's preprocessing requirements.
Note: name and transform do not have to be attributes of the model class itself; they can be passed separately to the CustomModel constructor.
from imagededup.methods import CNN
from imagededup.utils import CustomModel
import torch
from torchvision.transforms import transforms
# Define your custom model
class MyModel(torch.nn.Module):
transform = transforms.Compose(
[
transforms.ToTensor()
]
)
name = 'my_custom_model'
def __init__(self):
super().__init__()
# Define the layers of the model here
def forward(self, x):
# Do something with x
return x
# Wrap in CustomModel
custom_config = CustomModel(name=MyModel.name,
model=MyModel(),
transform=MyModel.transform)
# Initialize CNN with your custom model
cnn = CNN(model_config=custom_config)
# Use the model as usual
# ...