question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Image embedder finetuning example

See original GitHub issue

Hello i try to reproduce the finetuning image embedder example https://lightning-flash.readthedocs.io/en/latest/reference/image_embedder.html#finetuning

import flash
from flash import download_data
from flash.vision import ImageClassificationData, ImageEmbedder

# 1. Download the data
download_data("https://pl-flash-data.s3.amazonaws.com/hymenoptera_data.zip", "data/")

# 2. Load the data
datamodule = ImageClassificationData.from_folders(
    train_folder="data/hymenoptera_data/train/",
    valid_folder="data/hymenoptera_data/val/",
    test_folder="data/hymenoptera_data/test/",
)

# 3. Build the model
embedder = ImageEmbedder(backbone="resnet18", embedding_dim=128)

# 4. Create the trainer. Run once on data
trainer = flash.Trainer(max_epochs=1)

# 5. Train the model
trainer.finetune(embedder, datamodule=datamodule, strategy="freeze_unfreeze")

# 6. Test the model
trainer.test()

# 7. Save it!
trainer.save_checkpoint("image_embedder_model.pt")

After that i use the saved checkpoint :

model = ImageEmbedder.load_from_checkpoint("image_embedder_model.pt")
model.predict("data/hymenoptera_data/predict/")

But the result is a class predictions instead of a list of vectors : [[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]]

lightning-flash 0.2.1

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
psardincommented, May 4, 2021

I have changed the backbone to swav-imagenet and the embeddings are much better for my use case. Im closing the issue.

1reaction
edgarribacommented, May 3, 2021

@psardin image embeddings can be trained in many ways. In this example it’s shown how to train them based on an image classification loss - that’s the reason of using ImageClassificationData and why the return value as the one hot encoding of the predicted classes. As @aniketmaurya suggested, if you could be more specific about your application we could suggest an alternative.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fine-Tune ViT for Image Classification with 🤗 Transformers
This paper explored how you can tokenize images, just as you would tokenize sentences, so that they can be passed to transformer models...
Read more >
Fine tuning model using Triplet loss for Image search
Learn how to fine tune a model to compute image similarity using Triplet loss.
Read more >
Transfer learning and fine-tuning | TensorFlow Core
When you don't have a large image dataset, it's a good practice to artificially introduce sample diversity by applying random, yet realistic, transformations...
Read more >
How to Fine-tune Stable Diffusion using Textual Inversion
This tutorial focuses on how to fine-tune the embedding to create personalized images based on custom styles or objects. Instead of re-training the...
Read more >
Fine-tuning with Keras and Deep Learning
In this tutorial, you will learn how to perform fine-tuning using Keras and Deep Learning for image classification.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found