RuntimeError:May be error in input data

username: SARITHA_KOKKOT
cooked: <p>Lung nodule detection using faster rcnn. Model is                                                                        model = torchvision.models.detection.fasterrcnn_resnet50_fpn(weights=True)<br>
in_features = model.roi_heads.box_predictor.cls_score.in_features<br>
print(“modelout”,in_features)<br>
model.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes=1)<br>
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]])]} …<br>
class LungNoduleDataset(torch.utils.data.Dataset):<br>
def <strong>init</strong>(self, image_files, annotation_file, transform=None):<br>
self.image_files = image_files<br>
with open(annotation_file, ‘r’) as f:<br>
self.annotations = json.load(f)[‘annotations’]<br>
self.transform = transform</p>
<pre><code>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
</code></pre>
<p>dataset = LungNoduleDataset(image_files, ‘annotations2.json’, transform=transform)<br>
dataloader = torch.utils.data.DataLoader(dataset, batch_size=1)</p>
<pre><code>                                                                                                                             num_epochs = 10
</code></pre>
<p>for epoch in range(num_epochs):<br>
for images, targets in dataloader:<br>
print(“type”,targets)<br>
print(“image”,type(images))</p>
<pre><code>    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)                                                                                         
</code></pre>
<p>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:<br>
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</p>

username: ptrblck
cooked: <p>Based on the error message it seems <code>nn.CrossEntropyLoss</code> fails as its inputs are wrong.<br>
In the common use case this loss function would accept model outputs in the shape <code>[batch_size, nb_classes, *]</code> containing raw logits and targets in the shape <code>[batch_size, *]</code> (note the missing class dimension) containing class indices in the range <code>[0, nb_classes-1]</code>.<br>
In newer PyTorch releases this loss function also accepts “soft” targets if the target is a <code>FloatTensor</code>.<br>
In this case the target shape should be equal to the model output shape.</p>
<p>I guess your error is raised since you might be passing class indices as a <code>FloatTensor</code> to <code>nn.CrossEnotrpyLoss</code>, thus mixing both use cases.</p>