Модель DETR (преобразователь обнаружения) не работает для моего набора данных [закрыто]Python

Программы на Python
Ответить Пред. темаСлед. тема
Anonymous
 Модель DETR (преобразователь обнаружения) не работает для моего набора данных [закрыто]

Сообщение Anonymous »

Я пытаюсь обучить свой набор данных DETR (преобразователю обнаружения), чтобы иметь возможность обнаруживать дефекты на бетонных мостах. у меня много ошибок, я пытался их исправить, но через некоторое время те же ошибки появляются снова.
Вот мой код, учитывая, что я имею дело с прямоугольными ограничивающими рамками.
Вот мой код, учитывая, что я имею дело с прямоугольными ограничивающими рамками.
р>

Код: Выделить всё

import os
import json
from PIL import Image
from torch.utils.data import Dataset, DataLoader
import torch
from torchvision import transforms
from transformers import DetrImageProcessor, DetrForObjectDetection
import torch.optim as optim
import cv2  # Ensure you have OpenCV installed

# Set your directories
train_images_dir = r'D:\Civil Engineering\PHD\Thesis\ImageProcessing\work\dataset\defect_detection\dacl10k-DatasetNinja\train\img_sized'
val_images_dir = r'D:\Civil Engineering\PHD\Thesis\ImageProcessing\work\dataset\defect_detection\dacl10k-DatasetNinja\val\img_sized'
test_images_dir = r'D:\Civil Engineering\PHD\Thesis\ImageProcessing\work\dataset\defect_detection\dacl10k-DatasetNinja\test\img_sized'

train_annotations_dir = r'D:\Civil Engineering\PHD\Thesis\ImageProcessing\work\dataset\defect_detection\dacl10k-DatasetNinja\train\ann_sized'
val_annotations_dir = r'D:\Civil Engineering\PHD\Thesis\ImageProcessing\work\dataset\defect_detection\dacl10k-DatasetNinja\val\ann_sized'
test_annotations_dir = None  # No masks for test dataset

# Function to load annotations from JSON file
def load_annotations(json_file):
with open(json_file, 'r') as f:
data = json.load(f)

annotations = []
for obj in data['objects']:
class_id = obj['classId']
points = obj['points']['exterior']  # polygon points
annotations.append({'class_id': class_id, 'points': points})

return annotations

def create_mask_from_polygons(image_shape, annotations):
""" Create a binary mask from polygon annotations.  """
mask = torch.zeros(image_shape, dtype=torch.float32)  # Assuming image_shape is (height, width)

for ann in annotations:
if ann and 'points' in ann:
# Convert polygon points to a format compatible with drawing
points = torch.tensor(ann['points'], dtype=torch.int32)
# Fill the mask using polygon points
cv2.fillPoly(mask.numpy(), [points.numpy()], color=(1.0))  # Fill the polygon with 1

return mask

# Custom dataset class
class DefectDataset(Dataset):
def __init__(self, images_dir, annotations_dir):
self.images_dir = images_dir
self.annotations_dir = annotations_dir
self.image_files = os.listdir(images_dir)
self.transform = transforms.Compose([
transforms.Pad(padding=(10, 10, 10, 10), fill=0),  # Pad image by 10 pixels on all sides
transforms.Resize((224, 224)),  # Resize to 224x224
transforms.ToTensor()  # Convert image to tensor
])

def __len__(self):
return len(self.image_files)

def __getitem__(self, idx):
image_file = self.image_files[idx]
image_path = os.path.join(self.images_dir, image_file)

try:
image = Image.open(image_path).convert("RGB")
image = self.transform(image)  # Convert image to tensor

# Load corresponding JSON file
json_file = os.path.join(self.annotations_dir, f"{os.path.splitext(image_file)[0]}.json")
annotations = load_annotations(json_file)

return image, annotations
except Exception as e:
print(f"Error loading {image_file}: {e}")
return None, None  # Skip or handle as needed

# Custom collate function to handle variable-sized images and annotations
def custom_collate_fn(batch):
images, annotations = zip(*batch)

# Filter out any None items
images = [img for img in images if img is not None]
annotations = [ann for ann in annotations if ann is not None]

# Stack images into a single tensor (this assumes images are of the same size)
images = torch.stack(images, dim=0)  # Ensure all images have same size

return images, annotations

# Create datasets and dataloaders
train_dataset = DefectDataset(train_images_dir, train_annotations_dir)
val_dataset = DefectDataset(val_images_dir, val_annotations_dir)

train_loader = DataLoader(train_dataset, batch_size=5, shuffle=True, collate_fn=custom_collate_fn)
val_loader = DataLoader(val_dataset, batch_size=5, collate_fn=custom_collate_fn)

# Load Detr model
image_processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50")

# Define optimizer
optimizer = optim.Adam(model.parameters(), lr=1e-5)

# Example placeholder loss function for object detection
# Updated compute_loss function
def compute_loss(outputs, annotations):
"""
Computes the loss for object detection using DETR outputs.

Args:
outputs: The model's output.
annotations: A list of annotations for each image in the batch, containing class labels and bounding boxes.

Returns:
The computed loss tensor.
"""

# Extract predicted class logits and bounding boxes
pred_logits = outputs.logits  # Shape: [batch_size, num_queries, num_classes + 1]
pred_boxes = outputs.pred_boxes  # Shape: [batch_size, num_queries, 4]

target_classes = []
target_boxes = []

for ann in annotations:
# Collect class IDs and bounding boxes from annotations
classes = [obj['class_id'] for obj in ann]
boxes = [obj['bbox'] for obj in ann]  # [x_min, y_min, x_max, y_max]

target_classes.append(torch.tensor(classes, dtype=torch.long))
target_boxes.append(torch.tensor(boxes, dtype=torch.float32))

# Convert to tensors
target_classes = torch.stack(target_classes)  # Shape: [batch_size, num_objects]
target_boxes = torch.stack(target_boxes)  # Shape:  [batch_size, num_objects, 4]

# Calculate classification and bounding box loss (DETR uses Hungarian matching)
loss_class = torch.nn.CrossEntropyLoss()(pred_logits.view(-1, pred_logits.shape[-1]), target_classes.view(-1))
loss_bbox = torch.nn.L1Loss()(pred_boxes.view(-1, 4), target_boxes.view(-1, 4))

# Total loss (you might want to weight these losses)
total_loss = loss_class + loss_bbox

return total_loss

# Training loop with loss computation and saving model weights
for epoch in range(5):  # Number of epochs
model.train()
total_loss = 0
for batch in train_loader:
images, annotations = batch

# Check if batch is empty or invalid (skip invalid images)
if len(images) == 0 or any([ann is None for ann in annotations]):
continue  # Skip this batch if there are no valid images or annotations

try:
# Forward pass
outputs = model(images)

# Compute loss (with image shape passed)
loss = compute_loss(outputs, annotations)  # Only pass two arguments

# If loss is NaN or invalid, skip this batch
if not torch.isfinite(loss):
print(f"Invalid loss for batch, skipping.")
continue

# Backward pass and optimization
optimizer.zero_grad()
loss.backward()
optimizer.step()

# Accumulate total loss
total_loss += loss.item()

except Exception as e:
# Handle any exceptions in processing a batch
print(f"Error processing batch: {e}")
continue  # Skip this batch on error

print(f"Epoch {epoch + 1} completed. Total Loss: {total_loss:.4f}")

# Save the trained model
torch.save(model.state_dict(), "detr_trained_weights.pth")
print("Model saved as detr_trained_weights.pth")

Я пытался исправить ошибки, я исправил некоторые, но другие ошибки продолжают появляться.
Я ожидаю, что процесс обучения может быть завершен и файл весов будет создан.

Подробнее здесь: https://stackoverflow.com/questions/790 ... my-dataset
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Python»