Custom Vision Models
Complete the full lesson to earn 25 points
Work through each section, then tap “Mark as Complete” on the last one.
✦ Skip the page breaks and see fewer ads — read each lesson on a single page with Pro
Lesson: Implementing Custom Vision Models in Foundry
Introduction: The Power of Visual Intelligence
In the modern enterprise landscape, the ability to process visual data—images and videos—at scale is no longer a luxury; it is a fundamental requirement for operational efficiency. While general-purpose vision models can identify common objects like chairs, cats, or trees, they often fail when confronted with domain-specific challenges. For instance, a general model might identify a "component" in a manufacturing line, but it cannot tell you if that component is improperly seated, missing a screw, or showing early signs of corrosion. This is where Custom Vision Models come into play.
Custom Vision allows you to train, deploy, and refine machine learning models tailored specifically to your unique organizational data. By using your own labeled images, you create a model that understands the nuances of your products, your environments, and your specific quality control standards. Within the Foundry ecosystem, this process is integrated into the data pipeline, allowing you to move from raw image ingestion to actionable insights without moving your data across disparate cloud environments.
Understanding how to build and implement these models is critical because it empowers your organization to automate visual inspections, streamline inventory management, and enhance safety protocols. This lesson will guide you through the entire lifecycle of a Custom Vision project, from data preparation and model training to deployment and continuous monitoring.
The Lifecycle of a Custom Vision Project
Implementing a vision model is not a linear task; it is an iterative cycle. Many beginners make the mistake of treating it like a "set it and forget it" software deployment. Instead, think of it as a living application that requires constant feedback and refinement.
1. Data Collection and Curation
The quality of your model is strictly bounded by the quality of your data. If you feed the model blurry, poorly lit, or mislabeled images, the resulting predictions will be unreliable. You must collect a diverse set of images that represent the environment where the model will actually operate. This includes capturing images at different angles, varying lighting conditions, and even "negative" samples—images that do not contain the object of interest.
2. Annotation (Labeling)
Annotation is the process of defining the ground truth for your model. For image classification, this might mean assigning a single tag to an image. For object detection, it involves drawing bounding boxes around specific items. In Foundry, this is typically handled through integrated labeling workflows that allow subject matter experts—not just data scientists—to verify the data.
3. Training and Hyperparameter Tuning
Once the data is ready, the training phase begins. During this stage, the model examines the labeled images to identify patterns and features that distinguish one class from another. You will interact with various hyperparameters, such as the learning rate or batch size, which dictate how the model learns.
4. Evaluation and Validation
Before deploying, you must test the model against a "holdout" set—data that the model has never seen before. This allows you to measure accuracy, precision, and recall. If the model performs well on the training data but fails on the holdout set, you have encountered overfitting, a common issue we will discuss later.
5. Deployment and Monitoring
Deployment makes the model available to your production pipelines. Once live, monitoring is essential. You need to keep track of "model drift," where the incoming live data begins to differ significantly from the training data, eventually degrading the model's accuracy.
Preparing Your Dataset: The Foundation of Success
Before writing a single line of code, you must organize your data. In Foundry, data is stored in datasets, which act as the source of truth for your vision models. When preparing for a vision project, ensure your images are structured in a way that the training algorithm can consume them efficiently.
Note: Always aim for a balanced dataset. If you have 1,000 images of "functioning parts" but only 10 images of "defective parts," the model will develop a strong bias toward the functioning class, leading to poor detection of defects.
Best Practices for Data Preparation:
- Consistency: Keep image dimensions uniform where possible. While modern models handle varying sizes, consistent input sizes often lead to more stable training.
- Diverse Environments: If your camera is placed in a dimly lit warehouse, do not train your model exclusively on high-definition photos taken in a bright office.
- Data Augmentation: If you have a limited number of images, use programmatic methods to flip, rotate, or slightly adjust the contrast of your images to artificially expand your dataset.
- Clear Labeling Taxonomy: Create a strict naming convention for your labels. Avoid ambiguous labels like "thing" or "bad." Use specific, descriptive labels like "missing_bolt," "cracked_casing," or "misaligned_sensor."
Implementing the Model: A Technical Walkthrough
In Foundry, you generally interact with vision services via an SDK or an API wrapper. This allows you to integrate the model directly into your data transformation pipelines. Below is a conceptual example of how you might initialize and train a custom vision model using a Python-based interface within a Foundry code repository.
Initializing the Training Job
The following code snippet demonstrates how to define a training task. We assume the existence of a dataset object that points to your labeled image repository.
from foundry_vision import VisionClient
# Initialize the client
client = VisionClient(api_key="YOUR_API_KEY")
# Define the dataset source from your Foundry project
dataset_source = client.get_dataset(dataset_id="ri.foundry.main.dataset.12345")
# Configure the training job
training_config = {
"model_type": "object_detection",
"epochs": 50,
"learning_rate": 0.001,
"augmentation": True
}
# Launch the training process
model_job = client.train_model(
name="production_line_inspector",
data=dataset_source,
config=training_config
)
print(f"Training started. Job ID: {model_job.id}")
Explanation of the Code:
- Client Initialization: We start by connecting to the Vision service. This securely authenticates your script to the Foundry environment.
- Dataset Retrieval: We point the client to a specific dataset ID. This dataset contains your images and their corresponding label files (usually in JSON or CSV format).
- Configuration: The
training_configdictionary allows you to set the behavior of the model.model_typespecifies whether you are classifying a whole image or looking for specific objects within one. - Job Execution: The
train_modelfunction sends the instructions to the backend compute cluster. This process is asynchronous, meaning your code continues to run while the training happens in the background.
Comparison: Classification vs. Object Detection
When planning your vision project, choosing the right architecture is the most important decision you will make. Use the table below to determine which approach fits your business problem.
| Feature | Image Classification | Object Detection |
|---|---|---|
| Output | A single label for the entire image | Multiple labels with bounding box coordinates |
| Use Case | Identifying if an image contains a specific product | Locating and counting multiple items in a scene |
| Complexity | Lower, easier to train | Higher, requires more precise annotation |
| Performance | Faster inference | Slower, requires more compute resources |
Callout: When to choose Classification Choose classification when you only need to know "what" is in the image, regardless of where it is located. For example, if you are sorting mail into "Domestic" and "International" bins, you don't need to know where the stamp is—you just need to classify the envelope.
Callout: When to choose Object Detection Choose object detection when the location of the object matters or when there are multiple items in the frame. If you are scanning a conveyor belt and need to count how many bolts are on a plate, you must use object detection to isolate each individual bolt.
Best Practices for Model Deployment
Once your model has achieved satisfactory performance on your validation set, you are ready to move to deployment. Deploying a model effectively requires planning for scale and reliability.
1. Versioning Models
Never overwrite your existing models. Always create a new version of the model artifact. This allows you to perform an instant rollback if you discover that a newly deployed model is underperforming in the live production environment.
2. Resource Allocation
Vision models can be resource-intensive. Ensure that the compute profile assigned to your inference service has sufficient memory and CPU (or GPU) capabilities. If you are processing video streams, you may need to optimize your inference frequency—for example, processing every 5th frame instead of every single frame—to keep up with the real-time data flow.
3. Handling Inference Failures
What happens if the model returns an error or a low-confidence score? Your application code should have a fallback mechanism. If the confidence is below a certain threshold (e.g., 70%), you might want to route the image to a human reviewer rather than making an automated decision.
4. Automated Retraining Pipelines
The world changes. Lighting in your factory might shift, or the design of your product might be updated. Establish a pipeline that periodically pulls the most recent, verified data and triggers a retraining job. This "Human-in-the-Loop" approach ensures the model remains relevant over time.
Common Pitfalls and How to Avoid Them
Even experienced teams often encounter the same hurdles when working with Custom Vision. Being aware of these traps can save you weeks of troubleshooting.
The "Overfitting" Trap
Overfitting occurs when your model learns the training data "by heart" rather than learning the general patterns. It will perform perfectly on your training set but fail miserably on any new, live data.
- How to avoid: Use a larger, more diverse dataset. Implement "early stopping" during training, which halts the process once the performance on the validation set stops improving, even if the training set performance is still increasing.
The "Data Leakage" Problem
Data leakage happens when information from your validation or test set accidentally makes its way into your training set. For example, if you take 10 photos of the exact same object from slightly different angles and put 5 in the training set and 5 in the test set, the model will simply "remember" the object rather than learning to recognize the class.
- How to avoid: Ensure that your training and validation sets are split based on distinct objects or sessions, not just by splitting the image files randomly.
Neglecting the "Background" Class
Many people focus only on the objects they want to find and ignore the background. If your model is trained to find "bolts," but it has never seen a "screwdriver" or a "wrench," it might mistakenly identify a screwdriver as a bolt.
- How to avoid: Include "negative samples" in your training data. These are images of your workspace that do not contain the target objects. This teaches the model what not to detect.
Step-by-Step: Integrating Inference into a Pipeline
Now that we have covered training and best practices, let's look at how you integrate an existing model into a data pipeline in Foundry to process incoming data.
- Identify the Trigger: Determine what should trigger the inference. Is it a new file appearing in a folder? A row being added to a database?
- Load the Model: In your transform script, load the model object using the unique model version ID.
- Pre-process the Input: Models expect input in a specific format. You might need to resize images, normalize pixel values, or convert color spaces.
- Run Inference: Pass the processed input to the model's
predict()method. - Post-process the Output: The model will return raw data (e.g., coordinates or confidence scores). You must convert this into a format that your downstream systems can use, such as a structured table in a Foundry dataset.
# Conceptual pipeline code
def process_incoming_images(image_file):
# 1. Pre-processing
processed_image = resize_and_normalize(image_file)
# 2. Inference
model = load_model_version("production_line_inspector", "v2")
results = model.predict(processed_image)
# 3. Post-processing
if results['confidence'] > 0.85:
return {
"status": "detected",
"label": results['label'],
"location": results['bounding_box']
}
else:
return {"status": "needs_review", "label": None}
Managing Model Drift
Model drift is the silent killer of AI projects. It happens when the statistical properties of the data being fed into the model change over time. Imagine you trained your model to identify parts during the day shift. If the night shift uses a different lighting setup, the images will look different to the model, and its accuracy will plummet.
Strategies for Monitoring Drift:
- Confidence Score Tracking: Store the confidence scores of your model's predictions in a dataset. If you notice the average confidence score dropping over time, it is a clear indicator that the model is struggling with the current data.
- Periodic Audits: Regularly pull a sample of the images that the model has processed and have a human verify the predictions. Compare the human labels to the model labels.
- Data Distribution Shifts: Monitor the input data. If you are suddenly seeing more gray-scale images when you trained on color images, you have a distribution shift that needs to be addressed.
Warning: Never assume a model is "finished." A model is a snapshot of the data it was trained on. As your business processes evolve, your models must evolve with them, or they will eventually become a liability rather than an asset.
Frequently Asked Questions (FAQ)
Q: Do I need to be an expert in deep learning to use Custom Vision? A: No. Foundry’s platform is designed to abstract away the complex mathematical layers of neural networks. While understanding the basics helps, you can achieve excellent results by focusing on data quality and proper labeling.
Q: Can I use pre-trained models instead of training my own? A: Yes, you can use "Transfer Learning." This involves taking a model that has already been trained on millions of images (like ImageNet) and "fine-tuning" it on your specific data. This is often faster and requires less data than training from scratch.
Q: How many images do I need to get started? A: For simple classification, you might get decent results with as few as 50–100 images per class. For complex object detection, you usually want at least 200–500 images per class. Quality always beats quantity.
Q: Can I run these models offline? A: Foundry is primarily a cloud-based environment. However, depending on your specific architectural setup, you can export certain model formats to run at the "edge" (e.g., on a local camera or gateway device) if low latency is required.
Advanced Implementation: Handling Video Streams
Processing video is essentially processing a sequence of images. However, doing this efficiently requires a different approach than static images. If you attempt to run an inference on every single frame of a 60fps video, you will quickly overwhelm your compute resources.
Key Strategies for Video:
- Frame Sampling: Only process a subset of frames. For many industrial applications, processing 1–2 frames per second is more than enough to detect a defective part moving along a belt.
- Tracking: Once an object is detected, use a tracking algorithm to follow it across multiple frames rather than re-running the heavy detection model on every frame.
- Keyframe Analysis: Only trigger the detection model when a "change" is detected in the background, which saves compute cycles during idle times.
Key Takeaways
As we conclude this lesson, remember that the technical implementation is only one piece of the puzzle. The success of a Custom Vision project relies on the marriage of domain expertise and data science. Here are the core principles to keep in mind:
- Prioritize Data Quality: Your model is only as good as the images you feed it. Invest time in cleaning, labeling, and balancing your datasets; this is where you will get the highest return on your effort.
- Iterate, Don't Build Once: Treat your vision models as dynamic software that requires updates. Monitor for model drift and establish a regular cadence for retraining.
- Choose the Right Architecture: Don't use a sledgehammer to crack a nut. Use classification for simple identification and reserve object detection for complex scenarios where location or counting is required.
- Implement Human-in-the-Loop: Always build in a mechanism for human intervention when the model is uncertain. This not only protects your operations but also provides a stream of "hard" examples that you can use to improve the model in the future.
- Plan for Deployment: Think about the end-to-end pipeline before you start training. How will the images get to the model? What will the downstream system do with the result? A model that works in a notebook but cannot be integrated into a pipeline provides zero business value.
- Guard Against Bias and Leakage: Be vigilant about how you split your data and ensure that your training examples are representative of the actual environment, including the "background noise" of your workplace.
- Document Your Taxonomy: Keep your labeling taxonomy consistent across the organization. If one team calls a defect "scratch" and another calls it "abrasion," you will struggle to build a unified model.
By following these guidelines, you will be well-equipped to implement vision solutions that provide real, measurable value within the Foundry environment. Start small, focus on a high-impact use case, and refine your models as you gain experience with your specific data.
Continue the course
Enjoying the courses?
Everything stays free. Pro shows fewer ads, doubles your daily points limit so you progress twice as fast, and lets you read each lesson on one page.
- ✓ Fewer advertisements
- ✓ 2× daily points limit
- ✓ Distraction-free lessons