<jupyter_start><jupyter_text>Testing Code<jupyter_code>import torchvision.models.detection as models
# how I call the model inside my train script
model_auto = models.__dict__['fasterrcnn_resnet50_fpn'](pretrained=False)
model_auto.to('cuda')<jupyter_output><empty_output><jupyter_text>Use a real loader<jupyter_code>coco_root = os.path.join('..','..','external_data','coco')
#coco_detect = torchvision.datasets.CocoDetection(root=os.path.join(coco_root, 'train2017'), 
#                               annFile=os.path.join(coco_root, 'annotations', 'instances_train2017.json'),
#                              transform = torchvision.transforms.ToTensor())
# from https://github.com/pytorch/vision/blob/master/references/detection/transforms.py
class Compose(object):
    def __init__(self, transforms):
        self.transforms = transforms

    def __call__(self, image, target):
        for t in self.transforms:
            image, target = t(image, target)
        return image, target
# processes coco labels for detection task deployed as transform
# adapted from https://github.com/pytorch/vision/blob/master/references/detection/coco_utils.py
class CocoDetectProcessor(object):
    def __call__(self, image, target):
        
        w, h = image.size
        
        image_id = target["image_id"]
        image_id = torch.tensor([image_id])

        anno = target["annotations"]
        
        # strip crowd scenes
        anno = [obj for obj in anno if obj['iscrowd'] == 0]
        
        boxes = [obj["bbox"] for obj in anno]
        # guard against no boxes via resizing
        boxes = torch.as_tensor(boxes, dtype=torch.float32).reshape(-1, 4)
        boxes[:, 2:] += boxes[:, :2]
        boxes[:, 0::2].clamp_(min=0, max=w)
        boxes[:, 1::2].clamp_(min=0, max=h)

        classes = [obj["category_id"] for obj in anno]
        classes = torch.tensor(classes, dtype=torch.int64)
        
        target = {}
        target["boxes"] = boxes
        target["labels"] = classes
        target["image_id"] = image_id
        
        return image, target
class CocoDetection(torchvision.datasets.CocoDetection):
    def __init__(self, img_folder, ann_file, transforms):
        super(CocoDetection, self).__init__(img_folder, ann_file)
        self._transforms = transforms

    def __getitem__(self, idx):
        img, target = super(CocoDetection, self).__getitem__(idx)
        image_id = self.ids[idx]
        target = dict(image_id=image_id, annotations=target)
        if self._transforms is not None:
            img, target = self._transforms(img, target)
        return img, target
# need a custom one to to return the target too
from torchvision.transforms import functional as F
class ToTensor(object):
    def __call__(self, image, target):
        image = F.to_tensor(image)
        return image, target
class RandomHorizontalFlip(object):
    def __init__(self, prob):
        self.prob = prob

    def __call__(self, image, target):
        if random.random() < self.prob:
            height, width = image.shape[-2:]
            image = image.flip(-1)
            bbox = target["boxes"]
            bbox[:, [0, 2]] = width - bbox[:, [2, 0]]
            target["boxes"] = bbox
        return image, target
train_transforms = Compose([CocoDetectProcessor(), ToTensor(), RandomHorizontalFlip(0.5)])
val_transforms = Compose([CocoDetectProcessor(), ToTensor()])
train_set = CocoDetection(os.path.join(coco_root, 'train2017'), os.path.join(coco_root, 'annotations', 'instances_train2017.json'), train_transforms)
val_set = CocoDetection(os.path.join(coco_root, 'val2017'), os.path.join(coco_root, 'annotations', 'instances_val2017.json'), val_transforms)
# samplers from the dataset
train_sampler = torch.utils.data.RandomSampler(train_set)
test_sampler = torch.utils.data.SequentialSampler(val_set)
train_batch_sampler = torch.utils.data.BatchSampler(
            train_sampler, 3, drop_last=True)
def collate_fn(batch):
    return tuple(zip(*batch))
data_loader = torch.utils.data.DataLoader(
        train_set, batch_sampler=train_batch_sampler, num_workers=1,
        collate_fn=collate_fn)

data_loader_test = torch.utils.data.DataLoader(
        val_set, batch_size=1,
        sampler=test_sampler, num_workers=1,
        collate_fn=collate_fn)<jupyter_output><empty_output><jupyter_text>Logger Function Train Loop<jupyter_code># based on https://github.com/pytorch/vision/blob/master/references/detection/utils.py
# but we don't need the full one yet
class Logger(object):
    def __init__(self):
        self.filler = 1
    
    def log(self, iterable):
        for obj in iterable:
            yield obj<jupyter_output><empty_output><jupyter_text>Simple Train Loop<jupyter_code>model_auto.train()<jupyter_output><empty_output><jupyter_text>I think we need to look at the number of categories that the model was initialised to train<jupyter_code>import random
device = 'cuda'
logger = Logger() 
for images, targets in logger.log(iterable=data_loader):
    # transforms the tuples back to list (why did we need tuple?)
    images = list(image.to(device) for image in images)
    targets = [{k: v.to(device) for k, v in t.items()} for t in targets]
    losses = model_auto(images, targets)
    break
losses<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11572756592676911, 'fraction_numerical': 0.010244735344336939, 'mean_word_length': 3.4715860899067006, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 21, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '631837', 'license': 'Apache-2.0', 'n_tokens_mistral': 1700, 'n_tokens_neox': 1658, 'n_words': 376, 'path': 'jupyter_notebooks/Faster RCNN Testing.ipynb', 'repo_name': 'Data-drone/cv_experiments', 'size': '5271'}