File size: 1,778 Bytes
f972ca0
cbe2bf8
f972ca0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5de5c7b
 
 
f972ca0
 
 
 
 
ab3067a
f972ca0
 
ab3067a
f972ca0
 
 
 
 
 
 
 
 
64b5666
f972ca0
 
 
 
64b5666
5de5c7b
5b87c67
f972ca0
8cb3ab3
5b87c67
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import tensorflow as tf
import numpy as np
import datasets


_DESCRIPTION = (
    "This dataset consists 90k images of Chest-X-Ray from the Mimic-CXR dataset."
    "For each image, we have a consise report obtain from de PRO-CXR dataset"
    "All images have a size of 512x512 pixels."
)


_BASE_URL = "https://drive.google.com/file/d/1u27GCgIIRqDz8a5-VTcMJ1pEFQbGv_QB/view?usp=sharing"

FEATURE_DESCRIPTION_TFRECORD = {
  'report': tf.io.FixedLenFeature([], tf.string),
  'image': tf.io.FixedLenFeature([], tf.string),
}

def _parse_image_function(example_proto):
  # Parse the input tf.train.Example proto using the dictionary above.
  return tf.io.parse_single_example(example_proto, FEATURE_DESCRIPTION_TFRECORD)

class ReportsCXR(datasets.GeneratorBasedBuilder):
  def _info(self):
    return datasets.DatasetInfo(
        description = _DESCRIPTION,
        features = datasets.Features({
            'image': datasets.Image(),
            'report': datasets.Value(dtype='string')
        })
    )
  def _get_drive_url(self, url):
        base_url = 'https://drive.google.com/uc?id='
        split_url = url.split('/')
        return base_url + split_url[5]

  def _split_generators(self, dl_manager):
    archive_path = dl_manager.download(self._get_drive_url(_BASE_URL))
    return [
        datasets.SplitGenerator(name='full', gen_kwargs={'filepath': archive_path})
    ]


  def _generate_examples(self, filepath):
    raw_image_dataset = tf.data.TFRecordDataset(filepath)
    parsed_image_dataset = raw_image_dataset.map(_parse_image_function)
    for i, image_features in enumerate(parsed_image_dataset):
      image_raw = image_features['image'].numpy()
      str_report = image_features['report'].numpy()
      yield i, {'image': image_raw, 'report': str_report}