def test_classification(backbone, fields, tmpdir): train_data_frame = TEST_DF_1.copy() val_data_frame = TEST_DF_1.copy() test_data_frame = TEST_DF_1.copy() data = TabularClassificationData.from_data_frame( **fields, target_fields="label", train_data_frame=train_data_frame, val_data_frame=val_data_frame, test_data_frame=test_data_frame, num_workers=0, batch_size=2, ) model = TabularClassifier.from_data(datamodule=data, backbone=backbone) trainer = pl.Trainer(fast_dev_run=True, default_root_dir=tmpdir) trainer.fit(model, data)
def test_serve(): train_data = { "num_col": [1.4, 2.5], "cat_col": ["positive", "negative"], "target": [1, 2] } datamodule = TabularData.from_data_frame( "cat_col", "num_col", "target", pd.DataFrame.from_dict(train_data), ) model = TabularClassifier.from_data(datamodule) # TODO: Currently only servable once a preprocess has been attached model._preprocess = datamodule.preprocess model.eval() model.serve()
# 1. Download the data download_data("https://pl-flash-data.s3.amazonaws.com/titanic.zip", "data/") # 2. Load the data datamodule = TabularData.from_csv( ["Sex", "Age", "SibSp", "Parch", "Ticket", "Cabin", "Embarked"], ["Fare"], target_field="Survived", train_file="./data/titanic/titanic.csv", test_file="./data/titanic/test.csv", val_split=0.25, ) # 3. Build the model model = TabularClassifier.from_data( datamodule, metrics=[Accuracy(), Precision(), Recall()]) # 4. Create the trainer trainer = flash.Trainer(fast_dev_run=True) # 5. Train the model trainer.fit(model, datamodule=datamodule) # 6. Test model trainer.test(model) # 7. Save it! trainer.save_checkpoint("tabular_classification_model.pt")
import torch import flash from flash.core.data.utils import download_data from flash.tabular import TabularClassificationData, TabularClassifier # 1. Create the DataModule download_data("https://pl-flash-data.s3.amazonaws.com/titanic.zip", "./data") datamodule = TabularClassificationData.from_csv( ["Sex", "Age", "SibSp", "Parch", "Ticket", "Cabin", "Embarked"], "Fare", target_fields="Survived", train_file="data/titanic/titanic.csv", val_split=0.1, ) # 2. Build the task model = TabularClassifier.from_data(datamodule) # 3. Create the trainer and train the model trainer = flash.Trainer(max_epochs=3, gpus=torch.cuda.device_count()) trainer.fit(model, datamodule=datamodule) # 4. Generate predictions from a CSV predictions = model.predict("data/titanic/titanic.csv") print(predictions) # 5. Save the model! trainer.save_checkpoint("tabular_classification_model.pt")
# 1. Create the DataModule download_data("https://pl-flash-data.s3.amazonaws.com/titanic.zip", "./data") datamodule = TabularClassificationData.from_csv( categorical_fields=[ "Sex", "Age", "SibSp", "Parch", "Ticket", "Cabin", "Embarked" ], numerical_fields="Fare", target_fields="Survived", train_file="data/titanic/titanic.csv", val_split=0.1, batch_size=8, ) # 2. Build the task model = TabularClassifier.from_data(datamodule, backbone="fttransformer") # 3. Create the trainer and train the model trainer = flash.Trainer(max_epochs=3, gpus=torch.cuda.device_count()) trainer.fit(model, datamodule=datamodule) # 4. Generate predictions from a CSV datamodule = TabularClassificationData.from_csv( predict_file="data/titanic/titanic.csv", parameters=datamodule.parameters, batch_size=8, ) predictions = trainer.predict(model, datamodule=datamodule, output="classes") print(predictions) # 5. Save the model!