ozanciga / self-supervised-histopathology

Pretrained model for self supervised histopathology
MIT License
110 stars 12 forks source link

request for a script to get feature embeddings of patches using the cpkt #8

Closed monajalal closed 2 years ago

monajalal commented 2 years ago

Thanks for the great paper. For using the ResNet18 from img2vec git repo, I have been using the following script. Could you please provide a similar script for use on your saved model named tenpercent_resnet18.ckpt

from img2vec_pytorch import Img2Vec
from PIL import Image
import torch
import glob
import os

# # Initialize Img2Vec with GPU
img2vec = Img2Vec(cuda=True)
counter = 0 
for dirpath, dirname, filename in os.walk('resnet18_patches'):
    if dirpath.endswith('20.0'):
        images_full_path = dirpath + '/*.jpeg'
        for img in glob.glob(images_full_path):
            rgb_img = Image.open(img)
            vec = img2vec.get_vec(rgb_img, tensor=True)
            vec_name = os.path.basename(img)[:-5] + '.pt'
            dir_name = os.path.dirname(img) 
            torch.save(vec, dir_name + '/' + vec_name)
        counter += 1
        print('finished {} directories'.format(counter))
ozanciga commented 2 years ago

Set RETURN_PREACTIVATION = True in the starter code. You need to modify the images variable to accept a single image after preprocessing it to be a tensor. For the record, unless you are only interested in a few images, not using batches would be really slow compared to batch forward calls.

transform = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))
    ])
rgb_img = Image.open(img)
images = transform(rgb_img)
images = images.unsqueeze(0)  # or einops.rearrange('c h w -> () c h w')