Below is the code for objection detection using yolo
import os
import xml.etree.ElementTree as ET
import tensorflow as tf
import keras_cv
import requests
import zipfile
import tensorflow as tf
import sys
print(sys.path)
sys.path.append(r'C:UsersDELLDownloadskeras-yolo3-master')
from ultralytics import YOLO
from tqdm.auto import tqdm
from tensorflow import keras
from keras_cv import bounding_box
from keras_cv import visualization
import tensorflow as tf
SPLIT_RATIO = 0.2
BATCH_SIZE = 8
LEARNING_RATE = 0.0001
EPOCH = 30
GLOBAL_CLIPNORM = 10.0
IMG_SIZE = (640,640)
class_ids = [
"motorcycle",
"aeroplane",
"face",
]
class_mapping = dict(zip(range(len(class_ids)), class_ids))
import os
# Example paths using raw string literal
path_annot = r"dataset15/annotations"
path_images = r"dataset15/documents"
# List of class names (assuming these are the names of your 13 classes)
# Get all XML file paths in path_annot and sort them
xml_files = sorted(
[
os.path.join(path_annot, class_name, file_name)
for class_name in class_ids
for file_name in os.listdir(os.path.join(path_annot, class_name))
if file_name.endswith(".xml")
]
)
# Get all JPEG image file paths in path_images and sort them
jpg_files = sorted(
[
os.path.join(path_images, class_name, file_name)
for class_name in class_ids
for file_name in os.listdir(os.path.join(path_images, class_name))
if file_name.endswith(".jpg")
]
)
print("XML Files:", xml_files)
print("JPEG Files:", jpg_files)
import os
import xml.etree.ElementTree as ET
def parse_annotation(xml_file):
tree = ET.parse(xml_file)
root = tree.getroot()
image_name = root.find("filename").text
# Example if images are organized in class subdirectories
class_name = root.find("folder").text # or however the class name is stored in the XML
image_path = os.path.join(path_images, class_name, image_name)
boxes = []
classes = []
for obj in root.iter("object"):
cls = obj.find("name").text
classes.append(cls)
bbox = obj.find("bndbox")
xmin = float(bbox.find("xmin").text)
ymin = float(bbox.find("ymin").text)
xmax = float(bbox.find("xmax").text)
ymax = float(bbox.find("ymax").text)
boxes.append([xmin, ymin, xmax, ymax])
class_ids = [
list(class_mapping.keys())[list(class_mapping.values()).index(cls)]
for cls in classes
]
return image_path, boxes, class_ids
image_paths = []
bbox = []
classes = []
for xml_file in tqdm(xml_files):
image_path, boxes, class_ids = parse_annotation(xml_file)
image_paths.append(image_path)
bbox.append(boxes)
classes.append(class_ids)
bbox = tf.ragged.constant(bbox)
classes = tf.ragged.constant(classes)
image_paths = tf.ragged.constant(image_paths)
data = tf.data.Dataset.from_tensor_slices((image_paths, classes, bbox))
num_val = int(len(xml_files) * SPLIT_RATIO)
# Split the dataset into train and validation sets
val_data = data.take(num_val)
train_data = data.skip(num_val)
def load_image(image_path):
image = tf.io.read_file(image_path)
image = tf.image.decode_jpeg(image, channels=3)
return image
def load_dataset(image_path, classes, bbox):
# Read Image
image = load_image(image_path)
bounding_boxes = {
"classes": tf.cast(classes, dtype=tf.float32),
"boxes": bbox,
}
return {"images": tf.cast(image, tf.float32), "bounding_boxes": bounding_boxes}
augmenter = keras.Sequential(
layers=[
keras_cv.layers.RandomFlip(mode="horizontal", bounding_box_format="xyxy"),
keras_cv.layers.RandomShear(
x_factor=0.2, y_factor=0.2, bounding_box_format="xyxy"
),
keras_cv.layers.JitteredResize(
target_size=(640, 640), scale_factor=(1, 1), bounding_box_format="xyxy"
),
]
)
train_ds = train_data.map(load_dataset, num_parallel_calls=tf.data.AUTOTUNE)
train_ds = train_ds.shuffle(BATCH_SIZE * 4)
train_ds = train_ds.ragged_batch(BATCH_SIZE, drop_remainder=True)
train_ds = train_ds.map(augmenter, num_parallel_calls=tf.data.AUTOTUNE)
resizing = keras_cv.layers.JitteredResize(
target_size=(640, 640),
scale_factor=(1, 1),
bounding_box_format="xyxy",
)
val_ds = val_data.map(load_dataset, num_parallel_calls=tf.data.AUTOTUNE)
val_ds = val_ds.shuffle(BATCH_SIZE * 4)
val_ds = val_ds.ragged_batch(BATCH_SIZE, drop_remainder=True)
val_ds = val_ds.map(resizing, num_parallel_calls=tf.data.AUTOTUNE)
def dict_to_tuple(inputs):
return inputs["images"], inputs["bounding_boxes"]
train_ds = train_ds.map(dict_to_tuple, num_parallel_calls=tf.data.AUTOTUNE)
train_ds = train_ds.prefetch(tf.data.AUTOTUNE)
val_ds = val_ds.map(dict_to_tuple, num_parallel_calls=tf.data.AUTOTUNE)
val_ds = val_ds.prefetch(tf.data.AUTOTUNE)
backbone = keras_cv.models.YOLOV8Backbone.from_preset(
"yolo_v8_l_backbone_coco" # We will use yolov8 small backbone with coco weights
)
backbone = backbone
yolo = keras_cv.models.YOLOV8Detector(
num_classes=len(class_mapping),
bounding_box_format="xyxy",
backbone=backbone,
fpn_depth=3,
)
yolo.summary()
optimizer = tf.keras.optimizers.Adam(
learning_rate=LEARNING_RATE,
global_clipnorm=GLOBAL_CLIPNORM,
)
yolo.compile(
optimizer=optimizer, classification_loss="binary_crossentropy", box_loss="ciou"
)
import pycocotools
print("pycocotools is installed correctly.")
class EvaluateCOCOMetricsCallback(keras.callbacks.Callback):
def __init__(self, data, save_path):
super().__init__()
self.data = data
self.metrics = keras_cv.metrics.BoxCOCOMetrics(
bounding_box_format="xyxy",
evaluate_freq=1e9,
)
self.save_path = save_path
self.best_map = -1.0
def on_epoch_end(self, epoch, logs):
self.metrics.reset_state()
for batch in self.data:
images, y_true = batch[0], batch[1]
y_pred = self.model.predict(images, verbose=0)
self.metrics.update_state(y_true, y_pred)
metrics = self.metrics.result(force=True)
logs.update(metrics)
current_map = metrics["MaP"]
if current_map > self.best_map:
self.best_map = current_map
self.model.save(self.save_path) # Save the model when mAP improves
return logs
from tensorflow.keras.models import save_model
yolo.fit(
train_ds,
validation_data=val_ds,
epochs=3,
callbacks=[EvaluateCOCOMetricsCallback(val_ds, "F:GPU6model.h5")],
)
This first code works properly but in this … but in this training and validation images are cropped this code works on 640*640 and my original image size is 2000 * 880 so i modified this code to following 2nd code in which traning and validation images are not cropped
import os
import xml.etree.ElementTree as ET
import tensorflow as tf
import keras_cv
import requests
import zipfile
import tensorflow as tf
import sys
print(sys.path)
#sys.path.append(r'C:UsersDELLDownloadskeras-yolo3-master')
from ultralytics import YOLO
from tqdm.auto import tqdm
from tensorflow import keras
from keras_cv import bounding_box
from keras_cv import visualization
import tensorflow as tf
import os
import xml.etree.ElementTree as ET
import tensorflow as tf
from ultralytics import YOLO
from tqdm.auto import tqdm
from tensorflow import keras
import matplotlib.pyplot as plt
import matplotlib.patches as patches
SPLIT_RATIO = 0.2
BATCH_SIZE = 8
LEARNING_RATE = 0.001
EPOCH = 3
GLOBAL_CLIPNORM = 10.0
IMG_SIZE = (640, 640)
class_mapping = dict(zip(range(len(class_ids)), class_ids))
path_annot = r"dataset15/annotations"
path_images = r"dataset15/documents"
import os
import xml.etree.ElementTree as ET
import tensorflow as tf
import keras_cv
import requests
import zipfile
import tensorflow as tf
import sys
print(sys.path)
#sys.path.append(r'C:UsersDELLDownloadskeras-yolo3-master')
from ultralytics import YOLO
from tqdm.auto import tqdm
from tensorflow import keras
from keras_cv import bounding_box
from keras_cv import visualization
import tensorflow as tf
import os
import xml.etree.ElementTree as ET
import tensorflow as tf
from ultralytics import YOLO
from tqdm.auto import tqdm
from tensorflow import keras
import matplotlib.pyplot as plt
import matplotlib.patches as patches
SPLIT_RATIO = 0.2
BATCH_SIZE = 8
LEARNING_RATE = 0.001
EPOCH = 30
GLOBAL_CLIPNORM = 10.0
IMG_SIZE = (640, 640)
class_ids = [
"aeroplane",
"motorcycle",
"face",
]
class_mapping = dict(zip(range(len(class_ids)), class_ids))
path_annot = r"dataset15/annotations"
path_images = r"dataset15/documents"
xml_files = sorted(
[
os.path.join(path_annot, class_name, file_name)
for class_name in class_ids
for file_name in os.listdir(os.path.join(path_annot, class_name))
if file_name.endswith(".xml")
]
)
jpg_files = sorted(
[
os.path.join(path_images, class_name, file_name)
for class_name in class_ids
for file_name in os.listdir(os.path.join(path_images, class_name))
if file_name.endswith(".jpg")
]
)
print("XML Files:", xml_files)
print("JPEG Files:", jpg_files)
def parse_annotation(xml_file):
tree = ET.parse(xml_file)
root = tree.getroot()
image_name = root.find("filename").text
class_name = root.find("folder").text
image_path = os.path.join(path_images, class_name, image_name)
boxes = []
classes = []
for obj in root.iter("object"):
cls = obj.find("name").text
classes.append(cls)
bbox = obj.find("bndbox")
xmin = float(bbox.find("xmin").text)
ymin = float(bbox.find("ymin").text)
xmax = float(bbox.find("xmax").text)
ymax = float(bbox.find("ymax").text)
boxes.append([xmin, ymin, xmax, ymax])
class_ids = [
list(class_mapping.keys())[list(class_mapping.values()).index(cls)]
for cls in classes
]
return image_path, boxes, class_ids
image_paths = []
bbox = []
classes = []
for xml_file in tqdm(xml_files):
image_path, boxes, class_ids = parse_annotation(xml_file)
image_paths.append(image_path)
bbox.append(boxes)
classes.append(class_ids)
bbox = tf.ragged.constant(bbox)
classes = tf.ragged.constant(classes)
image_paths = tf.ragged.constant(image_paths)
data = tf.data.Dataset.from_tensor_slices((image_paths, classes, bbox))
num_val = int(len(xml_files) * SPLIT_RATIO)
val_data = data.take(num_val)
train_data = data.skip(num_val)
def load_image(image_path):
image = tf.io.read_file(image_path)
image = tf.image.decode_jpeg(image, channels=3)
return image
def resize_and_crop_image(image, target_height, target_width):
original_shape = tf.shape(image)
image = tf.image.resize(image, (target_height, target_width))
image = tf.image.resize_with_crop_or_pad(image, target_height, target_width)
return image, original_shape
def resize_and_crop_bboxes(bboxes, original_shape, target_height, target_width):
height, width = original_shape[0], original_shape[1]
scale_y = target_height / height
scale_x = target_width / width
bboxes = bboxes * [scale_x, scale_y, scale_x, scale_y]
return bboxes
def load_dataset(image_path, classes, bbox):
image = load_image(image_path)
image, original_shape = resize_and_crop_image(image, IMG_SIZE[0], IMG_SIZE[1])
bbox = resize_and_crop_bboxes(bbox, original_shape, IMG_SIZE[0], IMG_SIZE[1])
bounding_boxes = {
"classes": tf.cast(classes, dtype=tf.float32),
"boxes": bbox,
}
return {"images": tf.cast(image, tf.float32), "bounding_boxes": bounding_boxes}
train_ds = train_data.map(load_dataset, num_parallel_calls=tf.data.AUTOTUNE)
train_ds = train_ds.shuffle(BATCH_SIZE * 4)
train_ds = train_ds.ragged_batch(BATCH_SIZE, drop_remainder=True)
val_ds = val_data.map(load_dataset, num_parallel_calls=tf.data.AUTOTUNE)
val_ds = val_ds.shuffle(BATCH_SIZE * 4)
val_ds = val_ds.ragged_batch(BATCH_SIZE, drop_remainder=True)
def dict_to_tuple(inputs):
return inputs["images"], inputs["bounding_boxes"]
train_ds = train_ds.map(dict_to_tuple, num_parallel_calls=tf.data.AUTOTUNE)
train_ds = train_ds.prefetch(tf.data.AUTOTUNE)
val_ds = val_ds.map(dict_to_tuple, num_parallel_calls=tf.data.AUTOTUNE)
val_ds = val_ds.prefetch(tf.data.AUTOTUNE)
for element in train_ds.take(1):
print(element)
def plot_image_with_bboxes(image, bboxes, classes):
fig, ax = plt.subplots(1)
ax.imshow(image.numpy().astype("uint8"))
for bbox, cls in zip(bboxes, classes):
xmin, ymin, xmax, ymax = bbox
width = xmax - xmin
height = ymax - ymin
rect = patches.Rectangle((xmin, ymin), width, height, linewidth=2, edgecolor='r', facecolor='none')
ax.add_patch(rect)
ax.text(xmin, ymin, class_mapping[cls], bbox=dict(facecolor='yellow', alpha=0.5))
plt.show()
def visualize_dataset(dataset, num_samples=5):
for images, bounding_boxes in dataset.take(1):
for i in range(num_samples):
image = images[i]
bboxes = bounding_boxes['boxes'][i].numpy()
classes = bounding_boxes['classes'][i].numpy()
plot_image_with_bboxes(image, bboxes, classes)
print("Training dataset samples:")
visualize_dataset(train_ds)
print("Validation dataset samples:")
visualize_dataset(val_ds)
for images, bounding_boxes in train_ds.take(1):
print("Train batch shape:", images.shape)
print("Bounding boxes shape:", bounding_boxes['boxes'].shape)
print("Classes shape:", bounding_boxes['classes'].shape)
for images, bounding_boxes in val_ds.take(1):
print("Validation batch shape:", images.shape)
print("Bounding boxes shape:", bounding_boxes['boxes'].shape)
print("Classes shape:", bounding_boxes['classes'].shape)
backbone = keras_cv.models.YOLOV8Backbone.from_preset(
"yolo_v8_l_backbone_coco" # We will use yolov8 small backbone with coco weights
)
for images, labels in train_ds.take(1):
print("Image batch shape:", images.shape)
print("Labels keys:", labels.keys())
for key, value in labels.items():
print(f"Key: {key}, Value shape: {value.shape}")
backbone = backbone
yolo = keras_cv.models.YOLOV8Detector(
num_classes=len(class_mapping),
bounding_box_format="xyxy",
backbone=backbone,
fpn_depth=3,
)
yolo.summary()
optimizer = tf.keras.optimizers.Adam(
learning_rate=LEARNING_RATE,
global_clipnorm=GLOBAL_CLIPNORM,
)
yolo.compile(
optimizer=optimizer, classification_loss="binary_crossentropy", box_loss="ciou"
)
import pycocotools
print("pycocotools is installed correctly.")
class EvaluateCOCOMetricsCallback(keras.callbacks.Callback):
def __init__(self, data, save_path):
super().__init__()
self.data = data
self.metrics = keras_cv.metrics.BoxCOCOMetrics(
bounding_box_format="xyxy",
evaluate_freq=1e9,
)
self.save_path = save_path
self.best_map = -1.0
def on_epoch_end(self, epoch, logs):
self.metrics.reset_state()
for batch in self.data:
images, y_true = batch[0], batch[1]
y_pred = self.model.predict(images, verbose=0)
self.metrics.update_state(y_true, y_pred)
metrics = self.metrics.result(force=True)
logs.update(metrics)
current_map = metrics["MaP"]
if current_map > self.best_map:
self.best_map = current_map
self.model.save(self.save_path) # Save the model when mAP improves
return logs
from tensorflow.keras.models import save_model
yolo.fit(
train_ds,
validation_data=val_ds,
epochs=3,
callbacks=[EvaluateCOCOMetricsCallback(val_ds, "model.h5")],
)
inthis code training and validation images are properly visua;ize but this code gives following error: InvalidArgumentError Traceback (most recent call last)
Cell In[2], line 328
323 return logs
325 from tensorflow.keras.models import save_model
–> 328 yolo.fit(
329 train_ds,
330 validation_data=val_ds,
331 epochs=3,
332 callbacks=[EvaluateCOCOMetricsCallback(val_ds, “model.h5”)],
333 )
File ~/anaconda3/envs/cvlab/lib/python3.11/site-packages/keras/src/utils/traceback_utils.py:70, in filter_traceback..error_handler(*args, **kwargs)
67 filtered_tb = _process_traceback_frames(e.traceback)
68 # To get the full stack trace, call:
69 # tf.debugging.disable_traceback_filtering()
—> 70 raise e.with_traceback(filtered_tb) from None
71 finally:
72 del filtered_tb
File ~/anaconda3/envs/cvlab/lib/python3.11/site-packages/tensorflow/python/eager/execute.py:53, in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
51 try:
52 ctx.ensure_initialized()
—> 53 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
54 inputs, attrs, num_outputs)
55 except core._NotOkStatusException as e:
56 if name is not None:
InvalidArgumentError: Graph execution error:
assertion failed: [Last row partition does not match flat_values.] [Condition x == y did not hold element-wise:] [x (strided_slice_4:0) = ] [4] [y (strided_slice_5:0) = ] [8]
[[{{node assert_equal_1/Assert/AssertGuard/Assert}}]]
[[IteratorGetNext]] [Op:__inference_train_function_66844]