shape Detection - TensorFlow
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
For simple geometric shape detection in TensorFlow 1, the most practical approach is usually image classification rather than general object detection. If each image contains one main shape such as a circle, square, or triangle, a small convolutional neural network can classify the image accurately without the complexity of a full detection pipeline.
Frame the Problem Correctly
The title says “shape detection”, but there are really two different problems:
- classify the whole image as one shape
- locate shapes inside a larger scene
TensorFlow 1 can do either, but they are very different workloads. For a first implementation, start with classification unless you genuinely need bounding boxes.
Build a Simple TensorFlow 1 Classifier
The example below uses placeholders and sessions in classic TensorFlow 1 style:
Here the three output classes might be circle, square, and triangle.
Preprocess the Images Consistently
For simple shapes, preprocessing matters almost as much as model size:
- convert images to grayscale
- resize to a fixed size such as
64 x 64 - normalize pixel values to
[0, 1] - keep labels consistent
If the training set mixes different sizes, backgrounds, and stroke widths without any consistency, the model may learn the wrong patterns.
A minimal NumPy preprocessing step might look like:
That is enough for many small synthetic shape datasets.
Train with a Session
TensorFlow 1 training still revolves around sessions:
The code assumes you already prepared x_batch and y_batch. In a real project, those would come from your dataset pipeline.
If You Need Actual Detection
If your images contain multiple shapes or shapes at unknown locations, classification is no longer enough. Then you need:
- bounding-box labels
- a detection model
- post-processing for detected regions
That is a significantly bigger project. Many developers jump straight to detection when a simple classifier would have solved their real use case with far less effort.
For classroom or prototype projects, shape datasets are often synthetic. That is fine, but remember that perfectly clean generated shapes do not represent the messiness of real camera images. If deployment images contain shadows, rotation, perspective, or cluttered backgrounds, include those conditions in training and validation instead of assuming a toy dataset proves general robustness.
Common Pitfalls
- Treating a classification problem as object detection before confirming the requirements.
- Training on inconsistent image sizes, backgrounds, or labels.
- Expecting TensorFlow 1 placeholder code to behave like modern eager-execution TensorFlow.
- Using too little data and then blaming the architecture for poor accuracy.
- Forgetting that the model learns from training examples, not from geometric definitions of circles or squares.
Summary
- For simple shape recognition, start with image classification rather than full detection.
- In TensorFlow 1, use placeholders, layers, and sessions in the classic graph-execution style.
- Normalize images consistently before training.
- A small CNN is often enough for circles, squares, and triangles on clean datasets.
- Move to real detection only when you actually need localization, not just class labels.
Related reading
- Should I normalize my features before throwing them into RNN?
- Should \`RNN\` attention weights over variable length sequences be re-normalized to mask the effects of zero-padding?
- Should `RNN` attention weights over variable length sequences be re-normalized to mask the effects of zero-padding?
- Should the custom loss function in Keras return a single loss value for the batch or an arrary of losses for every sample in the training batch?
- Should I include negative examples for Tensorflow object detection API?
- Should I use tf.function for all functions?
- Show more images in Tensorboard - Tensorflow object detection
- Shuffling the training dataset with Tensorflow object detection api
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.