Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DEVX-219]: Model Training #208

Merged
merged 4 commits into from
Nov 10, 2023
Merged

[DEVX-219]: Model Training #208

merged 4 commits into from
Nov 10, 2023

Conversation

sanjaychelliah
Copy link
Contributor

What

  • Integrating Model Training in SDK
  • Adding support functions for training

Features

Model Training

# Note: CLARIFAI_PAT must be set as env variable.
from clarifai.client.app import App
from clarifai.client.model import Model

"""
Create model with trainable model_type
"""
app = App(user_id="user_id", app_id="app_id")
model = app.create_model(model_id="model_id", model_type_id="visual-classifier")
               (or)
model = Model('url')

"""
List training templates for the model_type
"""
templates = model.list_training_templates()
print(templates)

"""
Get parameters for the model.
"""
params = model.get_params(template='classification_basemodel_v1', yaml_file='model_params.yaml'
)

"""
Update the model params yaml and pass it to model.train()
"""
model_version_id = model.train('model_params.yaml')

"""
Training status and saving logs
"""
status = model.training_status(version_id=model_version_id,training_logs=True)
print(status)

Model Training support functionalities

# Note: CLARIFAI_PAT must be set as env variable.
from clarifai.client.app import App
from clarifai.client.model import Model

"""
show trainable model types
"""
app = App(user_id="user_id", app_id="app_id")
print(app.list_trainable_model_types())

"""
Show individual param information
"""
params = model.get_params(template='classification_basemodel_v1', yaml_file='model_params.yaml')
print(model.get_param_info(param = 'dataset_id))

"""
Update specific params for the model
"""
model.update_params({'template': 'xxx'})
model.train()

Dataset functionalities added

  • dataset.create_version()
  • dataset.list_versions()
  • dataset.delete_version()

TODO

  • Pytest tests in the upcoming PR
  • More examples in examples repo

Copy link
Contributor

@ackizilkale ackizilkale left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good to me.

Copy link
Contributor

@sainivedh sainivedh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great Work. Added some comments

README.md Outdated Show resolved Hide resolved
clarifai/client/dataset.py Outdated Show resolved Hide resolved
clarifai/client/model.py Outdated Show resolved Hide resolved
clarifai/client/model.py Outdated Show resolved Hide resolved
clarifai/client/model.py Outdated Show resolved Hide resolved
clarifai/client/model.py Outdated Show resolved Hide resolved
clarifai/client/model.py Outdated Show resolved Hide resolved
clarifai/client/model.py Outdated Show resolved Hide resolved
Comment on lines 121 to 140
dict_response = MessageToDict(response)
for model_type in dict_response['modelTypes']:
if model_type['id'] == model_type_id:
for modeltypefield in model_type['modelTypeFields']:
if modeltypefield['path'].split('.')[-1] == param:
if param == 'template':
del modeltypefield['placeholder']
del modeltypefield['modelTypeEnumOptions']
return modeltypefield
modeltypefield['param'] = modeltypefield.pop('path').split('.')[-1]
del modeltypefield['placeholder']
return modeltypefield
if modeltypefield['path'].split('.')[-1] == "template":
for modeltypeenum in modeltypefield['modelTypeEnumOptions']:
if modeltypeenum['id'] == template:
for modeltypeenumfield in modeltypeenum['modelTypeFields']:
if modeltypeenumfield['path'].split('.')[-1] == param:
modeltypeenumfield['param'] = modeltypeenumfield.pop('path').split('.')[-1]
del modeltypeenumfield['placeholder']
return modeltypeenumfield
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comments could be helpful to maintain in future

Copy link
Contributor Author

@sanjaychelliah sanjaychelliah Nov 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. We can add comments as parsing the API response Multimodeltypes. But every loop detail, not sure, its just indexing. Thats why kept the iter variable names clearly.

from google.protobuf.struct_pb2 import Struct


def response_to_templates(response: MultiModelTypeResponse, model_type_id: str) -> List[str]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def response_to_templates(response: MultiModelTypeResponse, model_type_id: str) -> List[str]:
def _response_to_templates(response: MultiModelTypeResponse, model_type_id: str) -> List[str]:

Shall we mark all these function as internal use ? wdyt

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The functions are stored in internal utils and we are following the same structure in Workflows. So I think this is fine.

clarifai/client/model.py Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
Copy link
Contributor

@sainivedh sainivedh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

@sanjaychelliah sanjaychelliah merged commit 3bd592a into master Nov 10, 2023
4 checks passed
@sanjaychelliah sanjaychelliah deleted the DEVX-219-model_train branch November 10, 2023 13:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants