示例#1
0
def main():
    # parser config
    config_file = "./config.ini"
    cp = ConfigParser()
    cp.read(config_file)

    # default config
    weights_dir = cp["DEFAULT"].get("weights_dir")
    base_model_name = cp["DEFAULT"].get("base_model_name")
    chexnet_class_names = cp["DEFAULT"].get("chexnet_class_names").split(",")
    class_names = cp["DEFAULT"].get("class_names").split(",")

    image_source_dir = cp["DEFAULT"].get("image_source_dir")
    data_dir = cp["DEFAULT"].get("data_dir")

    # train config
    image_dimension = cp["TRAIN"].getint("image_dimension")

    # test config
    batch_size = cp["TEST"].getint("batch_size")
    test_steps = cp["TEST"].get("test_steps")

    # parse weights file path
    output_weights_name = cp["TRAIN"].get("output_weights_name")
    weights_path = os.path.join(weights_dir, output_weights_name)

    # get test sample count
    test_counts = get_sample_counts(data_dir, "all_data", class_names)
    # compute steps
    if test_steps == "auto":
        test_steps = int(test_counts / batch_size)
    else:
        try:
            test_steps = int(test_steps)
        except ValueError:
            raise ValueError(f"""
                test_steps: {test_steps} is invalid,
                please use 'auto' or integer.
                """)
    print(f"** test_steps: {test_steps} **")

    print("** load model **")

    model_weights_path = weights_path
    model_factory = ModelFactory()
    model = model_factory.get_model(chexnet_class_names,
                                    model_name=base_model_name,
                                    use_base_weights=False,
                                    weights_path=model_weights_path,
                                    pop_last_layer=True)
    model.summary()
    print("** load test generator **")
    test_sequence = AugmentedImageSequence(
        dataset_csv_file=os.path.join(data_dir, "all_data.csv"),
        class_names=class_names,
        source_image_dir=image_source_dir,
        batch_size=batch_size,
        target_size=(image_dimension, image_dimension),
        augmenter=None,
        steps=test_steps,
        shuffle_on_epoch_end=False,
    )

    print("** make prediction **")
    image, y = test_sequence.__getitem__(4)

    y_hat = model.predict(image)
    # y_hat = model.predict_generator(test_sequence, verbose=1)
    # y = test_sequence.get_y_true()

    print(y_hat.shape)
示例#2
0
ckpt_manager = tf.train.CheckpointManager(ckpt, checkpoint_path, max_to_keep=5)

start_epoch = 0
if ckpt_manager.latest_checkpoint and continue_from_last_ckpt:
    start_epoch = int(ckpt_manager.latest_checkpoint.split('-')[-1])
    ckpt.restore(ckpt_manager.latest_checkpoint)
    print("Restored from checkpoint: {}".format(
        ckpt_manager.latest_checkpoint))

for epoch in range(start_epoch, EPOCHS):
    start = time.time()
    total_loss = 0

    for batch in range(data_generator.steps):
        img, target, _ = data_generator.__getitem__(batch)
        # print( target.max())
        with graph_mode():
            img_tensor = chexnet.get_visual_features(img)

        print("batch: {}".format(batch))
        # img_tensor=np.random.randint(low=-1,high=1,size=(1,1024))
        # img_tensor=np.float32(img_tensor)
        batch_loss, t_loss = train_step(img_tensor, target)
        total_loss += t_loss

        if batch % 5 == 0:
            print('Epoch {} Batch {} Loss {:.4f}'.format(
                epoch + 1, batch,
                batch_loss.numpy() / int(target.shape[1])))
    # storing the epoch end loss value to plot later
示例#3
0
    fig = plt.figure(figsize=(7.20, 10.80))
    fig.add_axes((.0, .5, .9, .7))
    fig.text(.1, .3, caption, wrap=True, fontsize=20)

    plt.xticks([])
    plt.yticks([])
    plt.imshow(img)
    plt.savefig(output_images_folder + "/{}".format(img_name))
    plt.close(fig)


hypothesis = []
references = []
for batch in range(data_generator.steps):
    print("Batch: {}".format(batch))
    img, target, img_path = data_generator.__getitem__(batch)
    with graph_mode():
        img_tensor = chexnet.get_visual_features(img)
    result, attention_plot = evaluate(img_tensor)
    target_word_list = tokenizer_wrapper.get_sentence_from_tokens(target)
    references.append([target_word_list])
    hypothesis.append(result)
    target_sentence = tokenizer_wrapper.get_string_from_word_list(
        target_word_list)
    predicted_sentence = tokenizer_wrapper.get_string_from_word_list(result)
    # save_output_prediction(img_path[0], target_sentence, predicted_sentence)

print(get_bleu_scores(hypothesis, references))

# # captions on the validation set
# rid = np.random.randint(0, len(img_name_val))