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

Feature 1057 - Add test and train transforms for image API in the backend #1146

Open
wants to merge 5 commits into
base: nextjs
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 95 additions & 1 deletion training/training/routes/image/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,27 @@
from training.core.trainer import ClassificationTrainer
from training.routes.image.schemas import ImageParams
from training.core.authenticator import FirebaseAuth
import torchvision.transforms as transforms
Copy link
Contributor

Choose a reason for hiding this comment

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

🚫 [pyright] reported by reviewdog 🐶
Import "torchvision.transforms" could not be resolved (reportMissingImports)

import json

router = Router()


@router.post("", auth=FirebaseAuth())
def imageTrain(request: HttpRequest, imageParams: ImageParams):
transform = {}
if (imageParams.transforms != ""):
transform = json.loads(imageParams.transforms)
train_transorms = transformParser(transform["train_transforms"]) if "train_transforms" in transform else transforms.ToTensor()
test_transforms = transformParser(transform["test_transforms"]) if "test_transforms" in transform else transforms.ToTensor()

if imageParams.default:
dataCreator = ImageDefaultDatasetCreator.fromDefault(imageParams.default)
train_loader = dataCreator.createTrainDataset()
test_loader = dataCreator.createTestDataset()
model = DLModel.fromLayerParamsList(imageParams.user_arch)
optimizer = getOptimizer(model, imageParams.optimizer_name, 0.05)
criterionHandler = getCriterionHandler(imageParams.criterion)
criterionHandler = getCriterionHandler(imageParams.criterion)
if imageParams.problem_type == "CLASSIFICATION":
trainer = ClassificationTrainer(
train_loader,
Expand All @@ -38,3 +46,89 @@ def imageTrain(request: HttpRequest, imageParams: ImageParams):
print(trainer.generate_confusion_matrix())
print(trainer.generate_AUC_ROC_CURVE())
return trainer.generate_AUC_ROC_CURVE()

def transformParser(transformArray):
transformsToReturn = transforms.ToTensor()
for x in transformArray:
if (x["type"] == "CenterCrop"):
transformsToReturn = transforms.Compose([
transformsToReturn,
transforms.CenterCrop(x["parameters"]["size"])]
)
elif (x["type"] == "ColorJitter"):
transformsToReturn = transforms.Compose([
transformsToReturn,
transforms.ColorJitter(x["parameters"]["brightness"], x["parameters"]["contrast"], x["parameters"]["saturation"], x["parameters"]["hue"])]
)
elif (x["type"] == "FiveCrop"):
transformsToReturn = transforms.Compose([
transformsToReturn,
transforms.FiveCrop(x["parameters"]["size"])]
)
elif (x["type"] == "Grayscale"):
transformsToReturn = transforms.Compose([
transformsToReturn,
transforms.Grayscale(x["parameters"]["num_output_channels"])]
)
elif (x["type"] == "Pad"):
transformsToReturn = transforms.Compose([
transformsToReturn,
transforms.Pad(x["parameters"]["padding"])]
)
elif (x["type"] == "RandomAffine"):
transformsToReturn = transforms.Compose([
transformsToReturn,
transforms.RandomAffine(x["parameters"]["degrees"], x["parameters"]["translate"], x["parameters"]["scale"], x["parameters"]["shear"])]
)
elif (x["type"] == "RandomCrop"):
transformsToReturn = transforms.Compose([
transformsToReturn,
transforms.RandomCrop(x["parameters"]["size"], x["parameters"]["padding"])]
)
elif (x["type"] == "RandomGrayscale"):
transformsToReturn = transforms.Compose([
transformsToReturn,
transforms.RandomGrayscale(x["parameters"]["p"])]
)
elif (x["type"] == "RandomHorizontalFlip"):
transformsToReturn = transforms.Compose([
transformsToReturn,
transforms.RandomHorizontalFlip(x["parameters"]["p"])]
)
elif (x["type"] == "Resize"):
transformsToReturn = transforms.Compose([
transformsToReturn,
transforms.Resize(x["parameters"]["size"])]
)
elif (x["type"] == "RandomPerspective"):
transformsToReturn = transforms.Compose([
transformsToReturn,
transforms.RandomPerspective(x["parameters"]["distortion_scale"], x["parameters"]["p"])]
)
elif (x["type"] == "RandomResizedCrop"):
transformsToReturn = transforms.Compose([
transformsToReturn,
#What do we want to do for other non-single value params
transforms.RandomResizedCrop(x["parameters"]["size"])]
)
elif (x["type"] == "RandomVerticalFlip"):
transformsToReturn = transforms.Compose([
transformsToReturn,
transforms.RandomVerticalFlip(x["parameters"]["p"])]
)
elif (x["type"] == "RandomRotation"):
transformsToReturn = transforms.Compose([
transformsToReturn,
transforms.RandomRotation(x["parameters"]["degrees"])]
)
elif (x["type"] == "TenCrop"):
transformsToReturn = transforms.Compose([
transformsToReturn,
transforms.TenCrop(x["parameters"]["size"])]
)
elif (x["type"] == "GaussianBlur"):
transformsToReturn = transforms.Compose([
transformsToReturn,
transforms.GaussianBlur(x["parameters"]["kernel_size"])]
)
return transformsToReturn
1 change: 1 addition & 0 deletions training/training/routes/image/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ class ImageParams(Schema):
test_size: float
batch_size: int
user_arch: list[LayerParams]
transforms: str = ""
Loading