def get_test_data(img_frontal_path, img_lateral_path, config):
    image_frontal = np.zeros([1, config.image_size, config.image_size, 3])
    image_frontal[0] = image_utils.getImages(img_frontal_path,
                                             config.image_size)
    image_lateral = np.zeros([1, config.image_size, config.image_size, 3])
    image_lateral[0] = image_utils.getImages(img_lateral_path,
                                             config.image_size)

    sentence = np.zeros(
        [1, config.max_sentence_num * config.max_sentence_length])
    mask = np.zeros([1, config.max_sentence_num * config.max_sentence_length])

    return image_frontal, image_lateral, sentence, mask
def get_inputs(imgs_path,
               captions_path,
               split_list_path,
               vocabulary_path,
               image_size=224,
               max_caption_length=20):
    with open(vocabulary_path, 'r') as file:
        vocabulary = json.load(file)
    word2id = {}
    for i in range(vocabulary.__len__()):
        word2id[vocabulary[i]] = i
    with open(captions_path, 'r') as file:
        caption_dict = json.load(file)
    with open(split_list_path, 'r') as file:
        split_id_list = json.load(file)

    image_list, sentence_list, mask_list, image_id_list = [], [], [], []
    for image_id in split_id_list:
        filename = str('%012d' % image_id) + '.jpg'
        img_file_path = os.path.join(imgs_path, filename)
        image = image_utils.getImages(img_file_path, image_size)
        image_list.append(image)

        caption = caption_dict[str(image_id)][0]
        raw_word_list = nltk.word_tokenize(caption)
        word_list = []
        for word in raw_word_list:  # filt out the word not contains in vocabulary
            if vocabulary.__contains__(word):
                word_list.append(word)
        if word_list.__len__() < max_caption_length:
            word_list.append('</S>')
            for _ in range(max_caption_length - word_list.__len__()):
                word_list.append('<EOS>')
        else:
            word_list = word_list[:max_caption_length]
        sentence = np.zeros(shape=[max_caption_length])
        mask = np.ones(shape=[max_caption_length])
        for i in range(max_caption_length):
            sentence[i] = word2id[word_list[i]]
            if word_list[i] == '<EOS>':
                mask[i] = 0
        sentence_list.append(sentence)
        mask_list.append(mask)

        image_id_list.append([image_id])

    return image_list, sentence_list, mask_list, image_id_list
def get_train_tfrecord(imgs_path, data_entry_path, split_list_path, vocabulary_path, tfrecord_path, config):
    with open(vocabulary_path, 'r') as f:
        vocabulary = json.load(f)
    word2id = {}
    for i in range(vocabulary.__len__()):
        word2id[vocabulary[i]] = i

    filenames = os.listdir(imgs_path)
    with open(data_entry_path, 'r') as f:
        data_dict = json.load(f)
    with open(split_list_path, 'r') as f:
        split_id_list = json.load(f)

    writer = tf.python_io.TFRecordWriter(tfrecord_path)

    for id in split_id_list:
        two_name = []
        for filename in filenames:
            if id == filename.split('_')[0]:
                two_name.append(filename)

        frontal_image_name, lateral_image_name = two_name[0], two_name[1]
        if two_name[0] > two_name[1]:
            frontal_image_name, lateral_image_name = two_name[1], two_name[0]

        image_frontal = image_utils.getImages(os.path.join(imgs_path, frontal_image_name), config.image_size)
        image_frontal = image_frontal.reshape([config.image_size*config.image_size*3])
        image_lateral = image_utils.getImages(os.path.join(imgs_path, lateral_image_name), config.image_size)
        image_lateral = image_lateral.reshape([config.image_size*config.image_size*3])

        sent_list = data_dict[id]
        if sent_list.__len__() > config.max_sentence_num:
            sent_list = sent_list[:config.max_sentence_num]

        word_list = []
        for sent in sent_list:
            words = nltk.word_tokenize(sent)
            if words.__len__() >= config.max_sentence_length:
                for i in range(config.max_sentence_length - 1):
                    word_list.append(words[i])
                word_list.append('</S>')
            else:
                for i in range(words.__len__()):
                    word_list.append(words[i])
                word_list.append('</S>')
                for _ in range(config.max_sentence_length - words.__len__() - 1):
                    word_list.append('<EOS>')
        for _ in range(config.max_sentence_num - sent_list.__len__()):
            word_list.append('</S>')
            for _ in range(config.max_sentence_length-1):
                word_list.append('<EOS>')
        # print(word_list.__len__())

        sentence = np.zeros(shape=[config.max_sentence_num * config.max_sentence_length], dtype=np.int64)
        mask = np.ones(shape=[config.max_sentence_num * config.max_sentence_length], dtype=np.int64)
        for i in range(config.max_sentence_num*config.max_sentence_length):
            sentence[i] = word2id[word_list[i]]
            if word_list[i] == '<EOS>':
                mask[i] = 0

        image_id = int(id[3:])
        example = tf.train.Example(
            features=tf.train.Features(
                feature={
                    'image_frontal_pixels': tf.train.Feature(float_list=tf.train.FloatList(value=image_frontal)),
                    'image_lateral_pixels': tf.train.Feature(float_list=tf.train.FloatList(value=image_lateral)),
                    'sentence': tf.train.Feature(int64_list=tf.train.Int64List(value=sentence)),
                    'mask': tf.train.Feature(int64_list=tf.train.Int64List(value=mask)),
                    'image_id': tf.train.Feature(int64_list=tf.train.Int64List(value=[image_id]))
                }
            )
        )
        serialized = example.SerializeToString()
        writer.write(serialized)

    print('%s write to tfrecord success!' % tfrecord_path)
Exemple #4
0
def get_train_tfrecord(imgs_path,
                       data_entry_path,
                       data_label_path,
                       split_list_path,
                       image_size=224,
                       label_num=15,
                       mode='train',
                       D=40):
    with open(data_entry_path, 'r') as file:
        data_dict = json.load(file)
    with open(data_label_path, 'r') as file:
        data_label_dict = json.load(file)
        finding_labels = data_label_dict.keys()
    with open(split_list_path, 'r') as file:
        lines = file.readlines()
        split_id_list = [line.strip() for line in lines]

    Q = 112120  # all image num
    Qn = 60361  # all no disease num
    for i in range(D):
        subsets_num = split_id_list.__len__() / D + 1
        sub_split_id_list = split_id_list[i * subsets_num:(i + 1) *
                                          subsets_num]

        tfrecord_name = 'data/tfrecord/' + mode + '-%02d.tfrecord' % i
        writer = tf.python_io.TFRecordWriter(tfrecord_name)

        for image_index in sub_split_id_list:
            img_file_path = os.path.join(imgs_path, image_index)
            image = image_utils.getImages(img_file_path, image_size)
            image_pixels = image.reshape([image_size * image_size * 3])

            label_one_hot = np.zeros(shape=[label_num], dtype=np.int64)
            labels = data_dict[image_index]
            mask_beta = np.zeros(shape=[label_num], dtype=np.float32)
            mask_lambda = np.zeros(shape=[label_num], dtype=np.float32)
            for label in labels:
                for j in range(label_num):
                    mask_lambda[j] = (0.0 + Q -
                                      data_label_dict[finding_labels[j]]) / Q
                    if label == finding_labels[j]:
                        label_one_hot[j] = 1
                        mask_beta[j] = (0.0 + Qn) / Q
                    else:
                        mask_beta[j] = (0.0 + Q - Qn) / Q

            example = tf.train.Example(features=tf.train.Features(
                feature={
                    'image_pixels':
                    tf.train.Feature(float_list=tf.train.FloatList(
                        value=image_pixels)),
                    'label_one_hot':
                    tf.train.Feature(int64_list=tf.train.Int64List(
                        value=label_one_hot)),
                    'mask_beta':
                    tf.train.Feature(float_list=tf.train.FloatList(
                        value=mask_beta)),
                    'mask_lambda':
                    tf.train.Feature(float_list=tf.train.FloatList(
                        value=mask_lambda))
                }))
            serialized = example.SerializeToString()
            writer.write(serialized)

        print('%s write to tfrecord success!' % tfrecord_name)
image_path = 'data/CXR3_IM-1384-1001.png'

FLAGS = tf.app.flags.FLAGS
tf.flags.DEFINE_string('img', 'data/examples/CXR3_IM-1384-1001.png', 'The test image path')

model_path = 'data/model/my-test-68000'
data_label_path = 'data/data_label.json'

md = Model(is_training=False, batch_size=1)

with open(data_label_path, 'r') as file:
    labels = json.load(file).keys()

saver = tf.train.Saver()
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    saver.restore(sess, model_path)

    images = np.zeros([md.batch_size, md.image_size, md.image_size, 3])
    images[0] = image_utils.getImages(FLAGS.img, md.image_size)
    paddings = np.zeros([md.batch_size, md.label_num])
    feed_dict = {md.images: images,
                 md.labels: paddings,
                 md.mask_beta: paddings,
                 md.mask_lambda: paddings}
    predictions = sess.run(md.predictions, feed_dict=feed_dict)

    for j in range(md.label_num):
        print('disease \'%s\', prob = %s' % (labels[j], round(predictions[0][j], 5)))

def get_train_tfrecord(imgs_path,
                       captions_path,
                       split_list_path,
                       vocabulary_path,
                       image_size=224,
                       max_caption_length=20,
                       mode='train',
                       D=40):
    with open(vocabulary_path, 'r') as file:
        vocabulary = json.load(file)
    word2id = {}
    for i in range(vocabulary.__len__()):
        word2id[vocabulary[i]] = i
    with open(captions_path, 'r') as file:
        caption_dict = json.load(file)
    with open(split_list_path, 'r') as file:
        split_id_list = json.load(file)

    for i in range(D):
        subsets_num = split_id_list.__len__() / D + 1
        sub_split_id_list = split_id_list[i * subsets_num:(i + 1) *
                                          subsets_num]

        tfrecord_name = 'data/tfrecord/' + mode + '-%02d.tfrecord' % i
        writer = tf.python_io.TFRecordWriter(tfrecord_name)

        for image_id in sub_split_id_list:
            filename = str('%012d' % image_id) + '.jpg'
            img_file_path = os.path.join(imgs_path, filename)
            image = image_utils.getImages(img_file_path, image_size)
            image = image.reshape([image_size * image_size * 3])

            caption = caption_dict[str(image_id)][0]
            raw_word_list = nltk.word_tokenize(caption)
            word_list = []
            for word in raw_word_list:  # filt out the word not contains in vocabulary
                if vocabulary.__contains__(word):
                    word_list.append(word)
            if word_list.__len__() < max_caption_length:
                word_list.append('</S>')
                for _ in range(max_caption_length - word_list.__len__()):
                    word_list.append('<EOS>')
            else:
                word_list = word_list[:max_caption_length]
            sentence = np.zeros(shape=[max_caption_length], dtype=np.int64)

            mask = np.ones(shape=[max_caption_length], dtype=np.int64)

            for i in range(max_caption_length):
                sentence[i] = word2id[word_list[i]]
                if word_list[i] == '<EOS>':
                    mask[i] = 0

            # image_raw = image.tostring()
            example = tf.train.Example(features=tf.train.Features(
                feature={
                    'image_pixels':
                    tf.train.Feature(float_list=tf.train.FloatList(
                        value=image)),
                    'sentence':
                    tf.train.Feature(int64_list=tf.train.Int64List(
                        value=sentence)),
                    'mask':
                    tf.train.Feature(int64_list=tf.train.Int64List(
                        value=mask)),
                    'image_id':
                    tf.train.Feature(int64_list=tf.train.Int64List(
                        value=[image_id]))
                }))
            serialized = example.SerializeToString()
            writer.write(serialized)
            # print('%s' % filename)
        print('%s write to tfrecord success!' % tfrecord_name)