RuntimeError:May be error in input data

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
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(
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 =[index]).convert('RGB')
    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 =, batch_size=1)

                                                                                                                             num_epochs = 10

for epoch in range(num_epochs):
for images, targets in dataloader:

    for k, v in targets.items():
   targets = [{'boxes': b, 'labels': l} for b, l in zip(targets['boxes'], targets['labels'])]
    images = list( for image in images)  
    targets = [{k: 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.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

Based on the error message it seems nn.CrossEntropyLoss fails as its inputs are wrong.
In the common use case this loss function would accept model outputs in the shape [batch_size, nb_classes, *] containing raw logits and targets in the shape [batch_size, *] (note the missing class dimension) containing class indices in the range [0, nb_classes-1].
In newer PyTorch releases this loss function also accepts “soft” targets if the target is a FloatTensor.
In this case the target shape should be equal to the model output shape.

I guess your error is raised since you might be passing class indices as a FloatTensor to nn.CrossEnotrpyLoss, thus mixing both use cases.