def on_epoch_end(self, epoch, logs={}): logs = logs or {} ds = self.config.ds_factor if self.config.mode == 'heatmap': for i, (image, y, name, spacing) in enumerate( zip(self.data.x_val, self.data.y_val, self.data.names_val, self.data.spacings_val)): height = image.shape[0] if i > self.validation_steps: break slice_thickness = spacing[2] pred_y, prob, pred_map, img = predict_slice(self.model, image, ds=ds) pred_map = np.expand_dims(zoom(np.squeeze(pred_map), ds), 2) img = img[:, :height, :, :] pred_map = pred_map[:height, :] img = place_line_on_img(img[0], y, pred_y, r=1) img = to256(img) # imageio.imwrite(os.path.join(out_path,str(i)+'_test.jpg'),img) print(pred_map.shape) if pred_map.shape[1] == 1: # case that the output is 1D pred_map = np.expand_dims( np.concatenate([pred_map] * img.shape[1], axis=1), 2) img = overlay_heatmap_on_image(img, pred_map) imageio.imwrite( os.path.join( self.output_dir, str(i) + '_' + name + '_map_e' + str(epoch) + '.jpg'), np.clip(img, 0, 255).astype(np.uint8)) # img = place_line_on_img(np.hstack([X[:, :, np.newaxis], X_s[:, :, np.newaxis]]), y, m, r=1) # imageio.imwrite(os.path.join(out_path, str(i) + '_' + str(int(max_pred * 100)) + '_otest.jpg'), img) else: for i, (image, y, name, spacing) in enumerate( zip(self.data.x_train, self.data.y_train, self.data.names_train, self.data.spacings_train)): if i > self.validation_steps: break height = image.shape[0] img = image.copy() pred_y, prob = predict_reg(self.model, image, y) img = place_line_on_img(gray2rgb(img), y, pred_y, r=1) img = to256(img) imageio.imwrite( os.path.join( self.output_dir, str(i) + '_' + name + '_map_e' + str(epoch) + '.jpg'), np.clip(img, 0, 255).astype(np.uint8)) return
def process_file(image_path, filename, prob_threshold=0.1): global segmentation_model global slice_detection_model print(image_path) pred_id = uuid.uuid4().hex results = {"success": False, "prediction": {'id': pred_id}} try: sitk_image, _ = load_image(image_path) except: return 1 image2d, image2d_preview = preprocess_sitk_image_for_slice_detection( sitk_image) image3d = sitk.GetArrayFromImage(sitk_image) spacing = sitk_image.GetSpacing() size = list(sitk_image.GetSize()) with graph.as_default(): set_session(sess) preds = slice_detection_model.predict(image2d) pred_z, prob = decode_slice_detection_prediction(preds) slice_z = adjust_detected_position_spacing(pred_z, spacing) slice_detected = prob > prob_threshold if slice_detected: results["prediction"]["slice_z"] = slice_z slice_image = image3d[slice_z, :, :] with graph.as_default(): set_session(sess) seg_image = segmentation_model.predict( preprocess_test_image(slice_image[np.newaxis, :, :, np.newaxis])) seg_image = seg_image[0] out_seg_image = np.flipud( blend2d(np.squeeze(preprocess_test_image(slice_image)), np.squeeze(seg_image > 0.5), 0.5)) image2d_preview = place_line_on_img(image2d_preview, pred_z, pred_z, r=1) cv2.imwrite(f'{settings.UPLOAD_FOLDER}/{filename}_slice-{pred_id}.jpg', to256(np.squeeze(preprocess_test_image(slice_image)))) cv2.imwrite( f'{settings.UPLOAD_FOLDER}/{filename}_frontal-{pred_id}.jpg', to256(np.squeeze(image2d_preview))) cv2.imwrite(f'{settings.UPLOAD_FOLDER}/{filename}_seg-{pred_id}.jpg', to256(np.squeeze(out_seg_image))) results["prediction"]["muscle_attenuation"] = '{0:.2f} HU'.format( compute_muscle_attenuation(slice_image, np.squeeze(seg_image > 0.5))) results["prediction"]["muscle_area"] = '{0:.2f}'.format( compute_muscle_area(np.squeeze(seg_image > 0.5), spacing, units='cm')) results["prediction"]["slice_prob"] = '{0:.2f}%'.format(100 * prob) results["success"] = True if results["success"]: result_str = 'Slice detected at position {0} of {1} with {2:.2f}% confidence '.format( slice_z, size[2], 100 * prob) else: result_str = 'Slice not detected' results["prediction"]["str"] = result_str return results
pred_z, spacing) slice_detected = prob > prob_threshold #print(slice_z) if slice_detected: results["prediction"]["slice_z"] = slice_z results["prediction"][ "slice_prob"] = '{0:.2f}%'.format(100 * prob) results["success"] = True #creating the (2) jpeg imageS of slice position and L3 slice ####################################### image = image3d slice_image = image[slice_z, :, :] image2d = place_line_on_img(image2d[0], pred_z, pred_z, r=1) cv2.imwrite( str(pred_id) + '_YES_' + image_name + '_SL_' + str(slice_z) + '_PROB_' + str(prob) + '.jpg', to256(slice_image)) cv2.imwrite( str(pred_id) + '_YES_' + image_name + '_FRL_' + str(slice_z) + '_PROB_' + str(prob) + '.jpg', to256(image2d)) output = [ image_path, folder, sub_folder, sub_sub_folder, 'YES', slice_z, prob, str(pred_id) + '_YES_' + image_name + '_SL_' +
numpy.savetxt(image_path+".csv", preds_reshaped, delimiter=",") print("Pred_Z") print(pred_z) #print("Slize_Z") #print(slice_z); #slice_detected = prob > prob_threshold_U #slice_not_detected = prob < prob_threshold_L #print(slice_z) if (prob > prob_threshold_U): #creating the (2) jpeg imageS of slice position and L3 slice ####################################### image = image3d slice_image = image[new_slice_z, :, :] image2dA = place_line_on_img(image2d[0], pred_z, pred_z, r=1) image2dB = place_line_on_img(image2d[0], new_pred_z, new_pred_z, r=1) #with graph.as_default(): # set_session(sess) # seg_image = segmentation_model.predict(preprocess_test_image(slice_image[np.newaxis, :, :, np.newaxis])) # seg_image = seg_image[0] #out_seg_image = np.flipud(blend2d(np.squeeze(slice_image), # np.squeeze(seg_image > 0.5), 0.5)) cv2.imwrite(str(pred_id)+'_YES_'+image_name+'_SL_'+str(new_slice_z)+'_PROB_'+str(prob)+'.jpg', to256(slice_image)) #cv2.imwrite(str(pred_id)+'_YES_'+image_name+'_FR_'+str(slice_z)+'_PROB_'+str(prob)+'.jpg', to256(image2dA))