Kaggle competition: RSNA Pneumonia Detection Challenge
<SOS> metric: RSNAObjectDetectionAP Kaggle data source: rsna-pneumonia-detection-challenge<set_options>
%%writefile test.py
device = torch.device('cuda')if torch.cuda.is_available() else torch.device('cpu')
 DIR_INPUT = '.. /input/rsna-pneumonia-detection-2018/input'
DIR_TEST = f"{DIR_INPUT}/samples"
test_images = os.listdir(DIR_TEST)
 print(f"Validation instances: {len(test_images)}")
 model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True, min_size=1024)
 num_classes = 2
in_features = model.roi_heads.box_predictor.cls_score.in_features
model.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes)
 os.makedirs('.. /validation_predictions', exist_ok=True)
 model.load_state_dict(torch.load('.. /input/rsna-pytorch-hackathon-fasterrcnn-resnet-training/fasterrcnn_resnet50_fpn.pth'))
model.to(device)
 def format_prediction_string(boxes, scores):
pred_strings = []
for j in zip(scores, boxes):
pred_strings.append("{0:.4f} {1} {2} {3} {4}".format(j[0],
int(j[1][0]), int(j[1][1]),
int(j[1][2]), int(j[1][3])))
 return " ".join(pred_strings)
 detection_threshold = 0.9
img_num = 0
results = []
model.eval()
with torch.no_grad() :
for i, image in tqdm(enumerate(test_images), total=len(test_images)) :
orig_image = cv2.imread(f"{DIR_TEST}/{test_images[i]}", cv2.IMREAD_COLOR)
 image = cv2.cvtColor(orig_image, cv2.COLOR_BGR2RGB ).astype(np.float32)
 image /= 255.0
image = np.transpose(image,(2, 0, 1)).astype(np.float)
 image = torch.tensor(image, dtype=torch.float ).cuda()
image = torch.unsqueeze(image, 0)
 model.eval()
cpu_device = torch.device("cpu")
 outputs = model(image)
 outputs = [{k: v.to(cpu_device)for k, v in t.items() } for t in outputs]
if len(outputs[0]['boxes'])!= 0:
for counter in range(len(outputs[0]['boxes'])) :
boxes = outputs[0]['boxes'].data.cpu().numpy()
scores = outputs[0]['scores'].data.cpu().numpy()
boxes = boxes[scores >= detection_threshold].astype(np.int32)
 draw_boxes = boxes.copy()
boxes[:, 2] = boxes[:, 2] - boxes[:, 0]
boxes[:, 3] = boxes[:, 3] - boxes[:, 1]
for box in draw_boxes:
cv2.rectangle(orig_image, 
(int(box[0]), int(box[1])) , 
(int(box[2]), int(box[3])) , 
(0, 0, 255), 3)
 plt.imshow(cv2.cvtColor(orig_image, cv2.COLOR_BGR2RGB))
plt.axis('off')
 plt.savefig(f"{test_images[i]}")
 plt.close()
result = {
'patientId': test_images[i].split('.')[0],
'PredictionString': format_prediction_string(boxes, scores)
 }
results.append(result)
 else:
result = {
'patientId': test_images[i].split('.')[0],
'PredictionString': None
}
results.append(result)
 sub_df = pd.DataFrame(results, columns=['patientId', 'PredictionString'])
 print(sub_df.head())
 sub_df.to_csv('submission.csv', index=False )
<load_from_csv><EOS> 
!python test.py