Metadata-Version: 2.1
Name: openml-pytorch
Version: 0.1.0
Summary: 
Author: SubhadityaMukherjee
Author-email: msubhaditya@gmail.com
Requires-Python: >=3.10,<4.0
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Requires-Dist: matplotlib (>=3.9.2,<4.0.0)
Requires-Dist: mkdocs-autorefs (>=1.2.0,<2.0.0)
Requires-Dist: mkdocs-awesome-pages-plugin (>=2.9.3,<3.0.0)
Requires-Dist: mkdocs-jupyter (>=0.25.0,<0.26.0)
Requires-Dist: mkdocs-material (>=9.5.39,<10.0.0)
Requires-Dist: mkdocs-redirects (>=1.2.1,<2.0.0)
Requires-Dist: mkdocstrings (>=0.26.1,<0.27.0)
Requires-Dist: mkdocstrings-python (>=1.11.1,<2.0.0)
Requires-Dist: mknotebooks (>=0.8.0,<0.9.0)
Requires-Dist: numpy (<2.0.0)
Requires-Dist: onnx (>=1.16.2,<2.0.0)
Requires-Dist: openml (>=0.14.2,<0.15.0)
Requires-Dist: tab-transformer-pytorch (>=0.3.0,<0.4.0)
Requires-Dist: tensorboard (>=2.17.1,<3.0.0)
Requires-Dist: torchvision (>=0.19.0,<0.20.0)
Requires-Dist: transformers (>=4.44.2,<5.0.0)
Description-Content-Type: text/markdown

# Pytorch extension for OpenML python

Pytorch extension for [openml-python API](https://github.com/openml/openml-python). This library provides a simple way to run your Pytorch models on OpenML tasks. 

For a more native experience, PyTorch itself provides OpenML integrations for some tasks. You can find more information [here](<Integrations of OpenML in PyTorch.md>).

#### Installation Instructions:

`pip install openml-pytorch`

PyPi link https://pypi.org/project/openml-pytorch/

Set the API key for OpenML from the command line:
```bash
openml configure apikey <your API key>
```

#### Usage
Import openML libraries
```python
import torch.nn
import torch.optim

import openml_pytorch.config
import openml
import logging

from openml_pytorch.trainer import OpenMLTrainerModule
from openml_pytorch.trainer import OpenMLDataModule
from torchvision.transforms import Compose, Resize, ToPILImage, ToTensor, Lambda
import torchvision
from openml_pytorch.trainer import convert_to_rgb

```
Create a pytorch model and get a task from openML
```python
model = torchvision.models.efficientnet_b0(num_classes=200)
# Download the OpenML task for tiniest imagenet
task = openml.tasks.get_task(362128)
```
Download the task from openML and define Data and Trainer configuration
```python
transform = Compose(
    [
        ToPILImage(),  # Convert tensor to PIL Image to ensure PIL Image operations can be applied.
        Lambda(
            convert_to_rgb
        ),  # Convert PIL Image to RGB if it's not already.
        Resize(
            (64, 64)
        ),  # Resize the image.
        ToTensor(),  # Convert the PIL Image back to a tensor.
    ]
)
data_module = OpenMLDataModule(
    type_of_data="image",
    file_dir="datasets",
    filename_col="image_path",
    target_mode="categorical",
    target_column="label",
    batch_size = 64,
    transform=transform
)
trainer = OpenMLTrainerModule(
    data_module=data_module,
    verbose = True,
    epoch_count = 1,
)
openml_pytorch.config.trainer = trainer
```
Run the model on the task
```python
run = openml.runs.run_model_on_task(model, task, avoid_duplicate_runs=False)
run.publish()
print('URL for run: %s/run/%d' % (openml.config.server, run.run_id))
```
Note: The input layer of the network should be compatible with OpenML data output shape. Please check [examples](/examples/) for more information.

Additionally, if you want to publish the run with onnx file, then you must call ```openml_pytorch.add_onnx_to_run()``` immediately before ```run.publish()```. 

```python
run = openml_pytorch.add_onnx_to_run(run)
```

