Skip to content

Recreate Towhee image embedding with timm #2215

Answered by junjiejiangjjj
orion512 asked this question in Q&A
Discussion options

You must be logged in to vote

https://towhee.io/image-embedding/timm/src/branch/main/timm_image.py
If use timm:

import timm
import torch
from PIL import Image as PILImage

from timm.data.transforms_factory import create_transform
from timm.data import resolve_data_config
from timm.models.factory import create_model

model_name = 'resnet50'
model = create_model(model_name, pretrained=True, num_classes=1000)
model.eval()
config = resolve_data_config({}, model=model)
tfms = create_transform(**config)


read_image = PILImage.open(EXAMPLE_IMG)
inputs = torch.stack([tfms(read_image)])
features = model.forward_features(inputs)
global_pool = torch.nn.AdaptiveAvgPool2d(1)
features = global_pool(features)
features = features.fl…

Replies: 2 comments 7 replies

Comment options

You must be logged in to vote
1 reply
@orion512
Comment options

Answer selected by orion512
Comment options

You must be logged in to vote
6 replies
@junjiejiangjjj
Comment options

@orion512
Comment options

@junjiejiangjjj
Comment options

@orion512
Comment options

@orion512
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants