Complete the code to load a pre-trained Faster R-CNN model from torchvision.
import torchvision.models.detection as detection model = detection.[1](pretrained=True)
The function fasterrcnn_resnet50_fpn loads the Faster R-CNN model with a ResNet-50 backbone and Feature Pyramid Network, pretrained on COCO dataset.
Complete the code to put the Faster R-CNN model in evaluation mode before inference.
model.[1]()Calling eval() sets the model to evaluation mode, disabling dropout and batch normalization updates, which is needed before inference.
Fix the error in the code to move the input image tensor to the same device as the model.
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model.to(device) image = image.[1](device)
The correct method to move a tensor to a device is to(device). Using cuda() only works for CUDA devices and device() or move() are invalid.
Fill both blanks to prepare the input image tensor as a list and normalize pixel values between 0 and 1.
image = image.[1](dtype=torch.float32) / 255.0 inputs = [image.[2](0)]
First, convert the image to float32 with to(dtype=torch.float32) and scale by 255. Then add a batch dimension with unsqueeze(0) to make it a list of tensors for the model.
Fill all three blanks to run inference and extract predicted boxes and labels from the output.
with torch.no_grad(): outputs = model([1]) boxes = outputs[0][[2]].cpu().numpy() labels = outputs[0][[3]].cpu().numpy()
Pass the input list inputs to the model. The output is a list of dicts. Extract 'boxes' and 'labels' keys from the first output dict, then move to CPU and convert to numpy arrays.