Lung nodule detection using faster rcnn. Model is model = torchvision.models.detection.fasterrcnn_resnet50_fpn(weights=True)
in_features = model.roi_heads.box_predictor.cls_score.in_features
print(“modelout”,in_features)
model.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes=1)
print(“modelout”,model.roi_heads.box_predictor ) targets is a dictionary consist of 99 boxes and labels in the format {‘boxes’: [tensor([[104, 341, 116, 353]])], ‘labels’: [tensor([[1]])]} …
class LungNoduleDataset(torch.utils.data.Dataset):
def init(self, image_files, annotation_file, transform=None):
self.image_files = image_files
with open(annotation_file, ‘r’) as f:
self.annotations = json.load(f)[‘annotations’]
self.transform = transform
def __len__(self):
return len(self.image_files)
def __getitem__(self, index):
# load image and annotations
img = Image.open(self.image_files[index]).convert('RGB')
img=transform(img)
ann = self.annotations[index]
# extract bounding box coordinates and label
bboxes = ann['bbox']
bbox = torch.stack([torch.tensor(bb) for bb in bboxes])
label = ann['label']
label = torch.tensor([int(label == 'nodules')
# return image, bounding box coordinates, and label
return img, {'boxes': [bbox], 'labels': [label]}
#create the dataset and dataloader
dataset = LungNoduleDataset(image_files, ‘annotations2.json’, transform=transform)
dataloader = torch.utils.data.DataLoader(dataset, batch_size=1)
num_epochs = 10
for epoch in range(num_epochs):
for images, targets in dataloader:
print(“type”,targets)
print(“image”,type(images))
for k, v in targets.items():
print(k,type(v))
targets = [{'boxes': b, 'labels': l} for b, l in zip(targets['boxes'], targets['labels'])]
images = list(image.to(device) for image in images)
targets = [{k: v.to(device) for k, v in t.items()} for t in targets]
loss_dict = model(images,targets) # here occured error
losses = sum(loss for loss in loss_dict.values())
optimizer.zero_grad()
losses.backward()
optimizer.step() errors shows detections, detector_losses = self.roi_heads(features, proposals, images.image_sizes, targets) loss_classifier, loss_box_reg = fastrcnn_loss(class_logits, box_regression, labels, regression_targets)
classification_loss = F.cross_entropy(class_logits, labels) in cross_entropy(input, target, weight, size_average, ignore_index, reduce, reduction, label_smoothing) if size_average is not None or reduce is not None:
reduction = _Reduction.legacy_get_string(size_average, reduce)return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index, label_smoothing) RuntimeError: Expected floating point type for target with class probabilities, got Long