Pytorch extension for OpenML python
Pytorch extension for openml-python API. 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.
Installation Instructions:
pip install openml-pytorch
PyPi link https://pypi.org/project/openml-pytorch/
Set the API key for OpenML from the command line:
openml configure apikey <your API key>
Usage
Load Data from OpenML and Train a Model
# Import libraries
import openml
import torch
import numpy as np
from sklearn.model_selection import train_test_split
from typing import Any
from tqdm import tqdm
from openml_pytorch import GenericDataset
# Get dataset by ID and split into train and test
dataset = openml.datasets.get_dataset(20)
X, y, _, _ = dataset.get_data(target=dataset.default_target_attribute)
X = X.to_numpy(dtype=np.float32)
y = y.to_numpy(dtype=np.int64)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1, stratify=y)
# Dataloaders
ds_train = GenericDataset(X_train, y_train)
ds_test = GenericDataset(X_test, y_test)
dataloader_train = torch.utils.data.DataLoader(ds_train, batch_size=64, shuffle=True)
dataloader_test = torch.utils.data.DataLoader(ds_test, batch_size=64, shuffle=False)
# Model Definition
class TabularClassificationModel(torch.nn.Module):
def __init__(self, input_size, output_size):
super(TabularClassificationModel, self).__init__()
self.fc1 = torch.nn.Linear(input_size, 128)
self.fc2 = torch.nn.Linear(128, 64)
self.fc3 = torch.nn.Linear(64, output_size)
self.relu = torch.nn.ReLU()
self.softmax = torch.nn.Softmax(dim=1)
def forward(self, x):
x = self.fc1(x)
x = self.relu(x)
x = self.fc2(x)
x = self.relu(x)
x = self.fc3(x)
x = self.softmax(x)
return x
# Train the model
trainer = BasicTrainer(
model = TabularClassificationModel(X_train.shape[1], len(np.unique(y_train))),
loss_fn = torch.nn.CrossEntropyLoss(),
opt = torch.optim.Adam,
dataloader_train = dataloader_train,
dataloader_test = dataloader_test,
device= torch.device("mps")
)
trainer.fit(10)
More Complex Image Classification Example
Import openML libraries
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
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
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
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 for more information.
Additionally, if you want to publish the run with onnx file, then you must call openml_pytorch.add_experiment_info_to_run() immediately before run.publish().
run = openml_pytorch.add_experiment_info_to_run(run=run, trainer=trainer)
run.publish()
print('URL for run: %s/run/%d' % (openml.config.server, run.run_id))
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file openml_pytorch-0.1.2.tar.gz.
File metadata
- Download URL: openml_pytorch-0.1.2.tar.gz
- Upload date:
- Size: 33.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.1.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3c40f18973498b221cb7f6340d09d577038a86c95fd2bb89899f2b9932011284
|
|
| MD5 |
fcc8d06b3dd7915f58c20b88a8cbb193
|
|
| BLAKE2b-256 |
81f0e45949c01e3947ab4bbd5a2465aec688d78bfc466fd0b30ca99c6d5fbe7e
|
File details
Details for the file openml_pytorch-0.1.2-py3-none-any.whl.
File metadata
- Download URL: openml_pytorch-0.1.2-py3-none-any.whl
- Upload date:
- Size: 29.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.1.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0d57cddde2d641963888ba4ed29c242b105e61fd4a3efb28f99a69af81e4bab1
|
|
| MD5 |
25d11ae51f253c32aa3b7e76779e9fac
|
|
| BLAKE2b-256 |
1c6ae8e8c6d0783e1f047b681c614529911c1be92a66f665719301148d1ad914
|