TrainValSplits
Introduction
TrainValSplits
widget in Supervisely is a tool that helps with the creation of training and validation datasets. The widget allows for easy splitting of the original dataset into training and validation sets based on customizable parameters such as percentage split or based on datasets or specific tag. TrainValSplits
helps improve the performance of machine learning models by ensuring that they are trained on diverse and representative data.
Function signature
TrainValSplits(
project_id=None,
project_fs=None,
random_splits=True,
tags_splits=True,
datasets_splits=True,
widget_id=None,
)

Parameters
project_id
int
Input Project
ID
project_fs
str
Path to input Project
on local host
random_splits
bool
Shuffle data and split with defined probability
tags_splits
bool
Images should have assigned train or val tag
datasets_splits
bool
Select one or several datasets for every split
widget_id
str
ID of the widget
project_id
Determine input Project
ID.
type: int
default value: None
splits = TrainValSplits(project_id=project_id)
project_fs
Determine path to input Project
on local host.
type: str
default value: None
project_dir = os.path.join(sly.app.get_data_dir(), project_info.name)
sly.Project.download(api, project_id, project_dir)
project_fs = sly.Project(project_dir, sly.OpenMode.READ)
splits = TrainValSplits(project_fs=project_fs)

random_splits
Shuffle data and split with defined probability.
type: bool
default value: true
splits = TrainValSplits(
project_id=project_id,
random_splits=False,
)

tags_splits
Images should have assigned train or val tag.
type: bool
default value: true
splits = TrainValSplits(
project_id=project_id,
tags_splits=False,
)

datasets_splits
Select one or several datasets for every split.
type: bool
default value: true
splits = TrainValSplits(
project_id=project_id,
datasets_splits=False,
)

widget_id
ID of the widget.
type: str
default value: None
Methods and attributes
get_splits()
Return result train/val split.
disable()
Disable widget.
enable()
Enable widget.
Mini App Example
You can find this example in our Github repository:
supervisely-ecosystem/ui-widgets-demos/controls/006_train_val_splits/src/main.py
Import libraries
import os
import supervisely as sly
from dotenv import load_dotenv
from supervisely.app.widgets import Card, Container, TrainValSplits, Button, Text
Init API client
First, we load environment variables with credentials and init API for communicating with Supervisely Instance:
load_dotenv("local.env")
load_dotenv(os.path.expanduser("~/supervisely.env"))
api = sly.Api()
Prepare project_id, download project and create project_fs
project_id = sly.env.project_id()
project_info = api.project.get_info_by_id(project_id)
project_dir = os.path.join(sly.app.get_data_dir(), project_info.name)
sly.fs.remove_dir(project_dir)
sly.Project.download(api, project_id, project_dir)
project_fs = sly.Project(project_dir, sly.OpenMode.READ)
Initialize TrainValSplits
widget
TrainValSplits
widgetsplits = TrainValSplits(project_fs=project_fs)
Initialize Button
and Text
widget we will use
Button
and Text
widget we will usebutton = Button("Get splits")
text = Text("")
text.hide()
Create app layout
Prepare a layout for app using Card
widget with the content
parameter and place widget that we've just created in the Container
widget.
card = Card(
title="Train Val Splits",
content=Container([splits, button, text], gap=5),
)
layout = Container(widgets=[card])
Create app using layout
Create an app object with layout parameter.
app = sly.Application(layout=layout)
Add function to control widgets from python code
@button.click
def get_splits():
train_set, val_set = splits.get_splits()
text.text = f"Train split: {len(train_set)} images, Val split: {len(val_set)} images"
text.show()

Last updated
Was this helpful?