Esempio n. 1
0
def apply_model_to_image_raw_bytes(raw):
    img = utils.decode_image_from_buf(raw)
    fig = plt.figure(figsize=(7, 7))
    plt.grid('off')
    plt.axis('off')
    plt.imshow(img)
    img = utils.crop_and_preprocess(img, (IMG_SIZE, IMG_SIZE), final_model.preprocess_for_model)
    print(' '.join(generate_caption(img)[1:-1]))
    plt.show()
Esempio n. 2
0
def apply_model_to_image_raw_bytes(raw, fname=None, do_save=False):
    img = utils.decode_image_from_buf(raw)
    fig = plt.figure(figsize=(7, 7))
    plt.grid('off')
    plt.axis('off')
    plt.imshow(img)
    img = utils.crop_and_preprocess(img, (IMG_SIZE, IMG_SIZE),
                                    final_model.preprocess_for_model)
    plt.title(' '.join(generate_caption(img)[1:-1]))
    if do_save:
        plt.savefig(fname)
    plt.show()
Esempio n. 3
0
def apply_model_to_image_raw_bytes(raw):
    img = utils.decode_image_from_buf(raw)
    plt.figure(figsize=(7, 7))
    plt.grid('off')
    plt.axis('off')
    plt.imshow(img)
    img = utils.crop_and_preprocess(img, (IMG_SIZE, IMG_SIZE),
                                    generator_model.process_for_model)
    img = np.expand_dims(img, axis=0)
    title = generate_caption(img)[1:-1]
    plt.title(' '.join(title))
    plt.show()
Esempio n. 4
0
def show_trainig_example(train_img_fns, train_captions, example_idx=0):
    """
    You can change example_idx and see different images
    """
    zf = zipfile.ZipFile("train2014_sample.zip")
    captions_by_file = dict(zip(train_img_fns, train_captions))
    all_files = set(train_img_fns)
    found_files = list(filter(lambda x: x.filename.rsplit("/")[-1] in all_files, zf.filelist))
    example = found_files[example_idx]
    img = utils.decode_image_from_buf(zf.read(example))
    plt.imshow(utils.image_center_crop(img))
    plt.title("\n".join(captions_by_file[example.filename.rsplit("/")[-1]]))
    plt.show()
Esempio n. 5
0
def show_training_example(train_img_fns, train_captions, example_idx=0):
    """
    showing images with their image captions
    """
    zf = zipfile.ZipFile("train2014_sample.zip")
    captions_by_file = dict(zip(train_img_fns, train_captions))
    all_files = set(train_img_fns)
    found_files = list(
        filter(lambda x: x.filename.rsplit("/")[-1] in all_files,
               zf.filelist))  # the last word i.e. file name
    example = found_files[example_idx]
    img = utils.decode_image_from_buf(zf.read(
        example))  # example is ZipInfo necessary for zf.read() function
    plt.imshow(utils.image_center_crop(img))
    plt.title("\n".join(captions_by_file[example.filename.rsplit("/")[-1]]))
    plt.show()
Esempio n. 6
0
def show_valid_example_series(val_img_fns, index_array):
    all_files = set(val_img_fns)
    found_files = list(
        filter(lambda x: x.filename.rsplit("/")[-1] in all_files, zf.filelist))
    #plt.figure()
    index = 1
    for row in range(index_array.shape[0]):
        for col in range(index_array.shape[1]):
            fn = found_files[index_array[row, col]]
            img = utils.decode_image_from_buf(zf.read(fn))
            subplot = plt.subplot(index_array.shape[0], index_array.shape[1],
                                  index)
            subplot.axis('off')
            subplot.grid('off')
            plt.imshow(img)
            img = utils.crop_and_preprocess(img, (IMG_SIZE, IMG_SIZE),
                                            generator_model.process_for_model)
            img_s = np.expand_dims(img, axis=0)
            title = generate_caption(img_s, sample=False)[1:-1]
            subplot.set_title(" ".join(title))
            index += 1
    plt.show()
Esempio n. 7
0
def apply_model_to_image_raw_bytes(raw):
    img = utils.decode_image_from_buf(raw)
    img = utils.crop_and_preprocess(img, (IMG_SIZE, IMG_SIZE),
                                    final_model.preprocess_for_model)
    print(' '.join(generate_caption(img)[1:-1]))
Esempio n. 8
0
def apply_model_to_image_raw_bytes_return_next_word_probs(raw, par_caption):
    img = utils.decode_image_from_buf(raw)
    img = utils.crop_and_preprocess(img, (IMG_SIZE, IMG_SIZE),
                                    final_model.preprocess_for_model)
    return generate_next_word_probs(img, par_caption)