def __data_generation(self, img_ids_temp): "Generates data containing batch_size samples" # Initialization X = np.empty((self.batch_size, *self.dim, self.n_channels)) y = np.empty((self.batch_size), dtype=int) # Generate data for i, ID in enumerate(img_ids_temp): image = load_dicom_image(ID, to_RGB=self.to_RGB, rescale=self.rescale) try: image = Image.fromarray(image) except: print( "Pil.Image can't read image. Possible 12 or 16 bit image. Try rescale=True to scale to 8 bit." ) image = image.resize((self.dim[0], self.dim[1])) X[i, ] = image ann = self.imgs_anns_dict[ID][0] y[i] = self.dataset.classes_dict[ann["labelId"]]["class_id"] return X, to_categorical(y, num_classes=self.n_classes)
def test_visualize(p): labels_dict = { "L_egJRyg": 1, # bounding box "L_MgevP2": 2, # polygon "L_D21YL2": 3, # freeform "L_lg7klg": 4, # line "L_eg69RZ": 5, # location "L_GQoaJg": 6, # global_image "L_JQVWjZ": 7, # global_series "L_3QEOpg": 8, # global_exam } p.set_labels_dict(labels_dict) ct_dataset = p.get_dataset_by_id("D_qGQdpN") ct_dataset.prepare() # image with multiple annotations image_id = ct_dataset.get_image_ids()[7] grey_image = visualize.load_dicom_image(image_id) rgb_image = visualize.load_dicom_image(image_id, to_RGB=True) scaled_image_1 = visualize.load_dicom_image(image_id, rescale=True) assert np.amax(grey_image) == 701 assert np.amax(scaled_image_1) == 255 assert grey_image.shape == (256, 256) assert rgb_image.shape == (256, 256, 3) scaled_image_2, gt_class_id, gt_bbox, gt_mask = visualize.get_image_ground_truth( image_id, ct_dataset ) assert len(gt_class_id) == len(gt_class_id) assert gt_mask.shape == (256, 256, 5)
def __data_generation(self, img_ids_temp): "Generates data containing batch_size samples" # Initialization X = np.empty((self.batch_size, *self.dim, self.n_channels)) y = np.empty((self.batch_size), dtype=int) # Generate data for i, ID in enumerate(img_ids_temp): image = load_dicom_image(ID, to_RGB=True) image = Image.fromarray(image) image = image.resize((self.dim[0], self.dim[1])) # image = resize(image, (self.dim[0], self.dim[1])) X[i, ] = image ann = self.imgs_anns_dict[ID][0] y[i] = self.dataset.classes_dict[ann["labelId"]]["class_id"] return X, to_categorical(y, num_classes=self.n_classes)
def create_tf_bbox_example(annotations, image_id, classes_dict): image = visualize.load_dicom_image(image_id) width = int(image.shape[1]) height = int(image.shape[0]) # TODO: I think object detection API decoder needs jpeg images... raw_img = visualize.load_dicom_image(image_id, to_RGB=True) img = Image.fromarray(raw_img) img_buffer = io.BytesIO() img.save(img_buffer, format="jpeg") encoded_jpg = Image.open(img_buffer) if encoded_jpg.format != "JPEG": raise ValueError("Image format not JPEG") key = hashlib.sha256(img_buffer.getvalue()).hexdigest() xmins = [ ] # List of normalized left x coordinates in bounding box (1 per box) xmaxs = [ ] # List of normalized right x coordinates in bounding box (1 per box) ymins = [ ] # List of normalized top y coordinates in bounding box (1 per box) ymaxs = [ ] # List of normalized bottom y coordinates in bounding box (1 per box) classes_text = [] # List of string class name of bounding box (1 per box) classes = [] # List of integer class id of bounding box (1 per box) # per annotation for a in annotations: w = int(a["data"]["width"]) h = int(a["data"]["height"]) x_min = int(a["data"]["x"]) y_min = int(a["data"]["y"]) x_max = x_min + w y_max = y_min + h # WARN: these are normalized xmins.append(float(x_min / width)) xmaxs.append(float(x_max / width)) ymins.append(float(y_min / height)) ymaxs.append(float(y_max / height)) classes_text.append(a["labelId"].encode("utf8")) classes.append(classes_dict[a["labelId"]]["class_id"]) # print(classes) tf_example = tf.train.Example(features=tf.train.Features( feature={ "image/height": dataset_util.int64_feature(height), "image/width": dataset_util.int64_feature(width), "image/filename": dataset_util.bytes_feature(image_id.encode("utf8")), "image/source_id": dataset_util.bytes_feature(image_id.encode("utf8")), "image/key/sha256": dataset_util.bytes_feature(key.encode("utf8")), "image/encoded": dataset_util.bytes_feature(img_buffer.getvalue()), "image/format": dataset_util.bytes_feature("jpg".encode("utf8")), "image/object/bbox/xmin": dataset_util.float_list_feature(xmins), "image/object/bbox/xmax": dataset_util.float_list_feature(xmaxs), "image/object/bbox/ymin": dataset_util.float_list_feature(ymins), "image/object/bbox/ymax": dataset_util.float_list_feature(ymaxs), "image/object/class/text": dataset_util.bytes_list_feature(classes_text), "image/object/class/label": dataset_util.int64_list_feature(classes), })) return tf_example