GithubHelp home page GithubHelp logo

Comments (10)

yufengm avatar yufengm commented on August 21, 2024

There is randomness in the transform process during training.
You can fix this by removing
transforms.RandomCrop( args.crop_size ),
transforms.RandomHorizontalFlip(),

But it would be good to keep this. As such data augmentation can improve generalizability.
While during the testing, you can use CenterCrop to ensure the same result for the same image.

from adaptive.

KeepingItClassy avatar KeepingItClassy commented on August 21, 2024

Thank you for the quick reply! However, this is happening when I submit new images for captioning - I'm using a saved trained model. I wasn't using any random crop during training, but I did use random horizontal flip. My caption generation script doesn't apply any random transforms though, just resizing and normalization of the input image. Is the trained model doing the random flipping before it encodes the image?

Thanks again!

from adaptive.

yufengm avatar yufengm commented on August 21, 2024

Did you use my following function?

def coco_eval( model, args, epoch ):

Or you write your own code for caption generation?

from adaptive.

yufengm avatar yufengm commented on August 21, 2024

The only randomness I can think of is the dropout/batchnorm that might be incorporated in the model.
And during evaluation, you'll need to change mode to 'eval' mode!

from adaptive.

KeepingItClassy avatar KeepingItClassy commented on August 21, 2024

I wrote my own caption generation that captions one image at a time. I need the resized images to keep their aspect ratio so I wrote a custom resize_pad function using Pillow. I did already have model.eval() - without it the captions were completely wrong. Here's my caption function code.

def caption(image, model, vocab):

    transform = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize((0.485, 0.456, 0.406),
                             (0.229, 0.224, 0.225))])
    
    image = resize_pad(image)
    image = transform(image).unsqueeze(0)
    image_tensor = Variable(image, volatile=True)
    
    generated_captions, _, _ = model.sampler(image_tensor)
    captions = generated_captions.data.numpy()

    sampled_ids = captions[0]
    sampled_caption = []

    for word_id in sampled_ids:
        word = vocab.idx2word[word_id]
        if word == '<end>':
            break
        else:
            sampled_caption.append(word)

    sentence = ' '.join(sampled_caption)
    return sentence

from adaptive.

sleighsoft avatar sleighsoft commented on August 21, 2024

@KeepingItClassy How does the resize_pad function look? Do you mind sharing your code

from adaptive.

KeepingItClassy avatar KeepingItClassy commented on August 21, 2024

@sleighsoft, I'm using PIL:

from __future__ import division
from PIL import Image

def resize_pad(image):
    IMAGE_SIZE = 224
    bg_color = image.getpixel((0, 0))
    new_image = Image.new('RGB', (IMAGE_SIZE, IMAGE_SIZE), bg_color)
    old_width, old_height = image.size

    if old_width > old_height:
        ratio = old_width / old_height
        new_height = int(round(IMAGE_SIZE / ratio))
        position = int(round((IMAGE_SIZE - new_height)/2))
        resized_image = image.resize((IMAGE_SIZE, new_height), Image.HAMMING)
        new_image.paste(resized_image, (0, position))
    else:
        ratio = old_height / old_width
        new_width = int(round(IMAGE_SIZE / ratio))
        position = int(round((IMAGE_SIZE - new_width)/2))
        resized_image = image.resize((new_width, IMAGE_SIZE), Image.HAMMING)
        new_image.paste(resized_image, (position, 0))

    return new_image

I'm creating a background image with the target size and the color of upper left-hand corner pixel of the original image, but you can use any RGB tuple. Since the goal is to preserve the aspect ratio of the original image, I'm checking whether the height is greater than the width or vice versa, and resizing and positioning the image so that it's centered accordingly. Image.HAMMING is an optional sampling filter to make the resized images look smooth; you can find other filter options in the doc. Hope this helps!

from adaptive.

KeepingItClassy avatar KeepingItClassy commented on August 21, 2024

As for the original issue, I solved it by getting rid of volatile=True. Closing.

from adaptive.

CherishineNi avatar CherishineNi commented on August 21, 2024

@KeepingItClassy
Could you share your test code? Code can be achieved on the screen images and captions. Thanks very much.

from adaptive.

CherishineNi avatar CherishineNi commented on August 21, 2024

@sleighsoft
Could you share your test code? Code can be achieved on the screen images and captions. Thanks very much.

from adaptive.

Related Issues (19)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.