This code builds a simple SSD-like model using MobileNetV2 as a base. It predicts bounding box locations and class scores in one step. The output shape shows how many boxes and classes are predicted.
import tensorflow as tf
from tensorflow.keras.applications import MobileNetV2
from tensorflow.keras.layers import Conv2D, Reshape, Concatenate, Input
from tensorflow.keras.models import Model
import numpy as np
# Simple SSD-like model for demonstration
def create_ssd(input_shape, num_classes):
input_tensor = Input(shape=input_shape)
base_model = MobileNetV2(input_tensor=input_tensor, include_top=False, weights=None)
# Feature map from base model
feature_map = base_model.output # shape approx (None, 10, 10, 1280) for 300x300 input
# Predict locations (4 coords per box) and class scores
num_boxes = 6 # number of boxes per location (simplified)
loc_pred = Conv2D(num_boxes * 4, kernel_size=3, padding='same')(feature_map)
loc_pred = Reshape((-1, 4))(loc_pred) # flatten to (batch, total_boxes, 4)
class_pred = Conv2D(num_boxes * num_classes, kernel_size=3, padding='same')(feature_map)
class_pred = Reshape((-1, num_classes))(class_pred) # (batch, total_boxes, num_classes)
predictions = Concatenate(axis=2)([loc_pred, class_pred])
model = Model(inputs=input_tensor, outputs=predictions)
return model
# Create model
num_classes = 3 # e.g., background + 2 object types
model = create_ssd((300, 300, 3), num_classes)
# Dummy input image batch
x = np.random.random((1, 300, 300, 3)).astype(np.float32)
# Get predictions
preds = model.predict(x)
print(f"Predictions shape: {preds.shape}")