Coder Social home page Coder Social logo

img2vec's Introduction

Image 2 Vec with PyTorch

Medium post on building the first version from scratch: https://becominghuman.ai/extract-a-feature-vector-for-any-image-with-pytorch-9717561d1d4c

Applications of image embeddings:

  • Ranking for recommender systems
  • Clustering images to different categories
  • Classification tasks
  • Image compression

Available models

Model name Return vector length
Resnet-18 512
Alexnet 4096
Vgg-11 4096
Densenet 1024
efficientnet_b0 1280
efficientnet_b1 1280
efficientnet_b2 1408
efficientnet_b3 1536
efficientnet_b4 1792
efficientnet_b5 2048
efficientnet_b6 2304
efficientnet_b7 2560

Installation

Tested on Python 3.6 and torchvision 0.11.0 (nightly, 2021-09-25)

Requires Pytorch: http://pytorch.org/

conda install -c pytorch-nightly torchvision

pip install img2vec_pytorch

Run test

python -m img2vec_pytorch.test_img_to_vec

Using img2vec as a library

from img2vec_pytorch import Img2Vec
from PIL import Image

# Initialize Img2Vec with GPU
img2vec = Img2Vec(cuda=True)

# Read in an image (rgb format)
img = Image.open('test.jpg')
# Get a vector from img2vec, returned as a torch FloatTensor
vec = img2vec.get_vec(img, tensor=True)
# Or submit a list
vectors = img2vec.get_vec(list_of_PIL_images)
For running the example, you will additionally need:
  • Pillow: pip install Pillow
  • Sklearn pip install scikit-learn

Running the example

git clone https://github.com/christiansafka/img2vec.git

cd img2vec/example

python test_img_similarity.py

Expected output

Which filename would you like similarities for?
cat.jpg
0.72832 cat2.jpg
0.641478 catdog.jpg
0.575845 face.jpg
0.516689 face2.jpg

Which filename would you like similarities for?
face2.jpg
0.668525 face.jpg
0.516689 cat.jpg
0.50084 cat2.jpg
0.484863 catdog.jpg

Try adding your own photos!

Img2Vec Params

cuda = (True, False)   # Run on GPU?     default: False
model = ('resnet-18', 'efficientnet_b0', etc.)   # Which model to use?     default: 'resnet-18'

Advanced users


Read only file systems

If you use this library from the app running in read only environment (for example, docker container), specify writable directory where app can store pre-trained models.

export TORCH_HOME=/tmp/torch

Additional Parameters

layer = 'layer_name' or int   # For advanced users, which layer of the model to extract the output from.   default: 'avgpool'
layer_output_size = int   # Size of the output of your selected layer
gpu = (0, 1, etc.)   # Which GPU to use?     default: 0

Defaults: (layer = 'avgpool', layer_output_size = 512)
Layer parameter must be an string representing the name of a layer below

conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False)
bn1 = nn.BatchNorm2d(64)
relu = nn.ReLU(inplace=True)
maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
layer1 = self._make_layer(block, 64, layers[0])
layer2 = self._make_layer(block, 128, layers[1], stride=2)
layer3 = self._make_layer(block, 256, layers[2], stride=2)
layer4 = self._make_layer(block, 512, layers[3], stride=2)
avgpool = nn.AvgPool2d(7)
fc = nn.Linear(512 * block.expansion, num_classes)

Defaults: (layer = 2, layer_output_size = 4096)
Layer parameter must be an integer representing one of the layers below

alexnet.classifier = nn.Sequential(
            7. nn.Dropout(),                  < - output_size = 9216
            6. nn.Linear(256 * 6 * 6, 4096),  < - output_size = 4096
            5. nn.ReLU(inplace=True),         < - output_size = 4096
            4. nn.Dropout(),		      < - output_size = 4096
            3. nn.Linear(4096, 4096),	      < - output_size = 4096
            2. nn.ReLU(inplace=True),         < - output_size = 4096
            1. nn.Linear(4096, num_classes),  < - output_size = 4096
        )

Defaults: (layer = 2, layer_output_size = 4096)

vgg.classifier = nn.Sequential(
            nn.Linear(512 * 7 * 7, 4096),
            nn.ReLU(True),
            nn.Dropout(),
            nn.Linear(4096, 4096),
            nn.ReLU(True),
            nn.Dropout(),
            nn.Linear(4096, num_classes),
        )

Defaults: (layer = 1 from features, layer_output_size = 1024)

densenet.features = nn.Sequential(OrderedDict([
	('conv0', nn.Conv2d(3, num_init_features, kernel_size=7, stride=2,
						padding=3, bias=False)),
	('norm0', nn.BatchNorm2d(num_init_features)),
	('relu0', nn.ReLU(inplace=True)),
	('pool0', nn.MaxPool2d(kernel_size=3, stride=2, padding=1)),
]))

Defaults: (layer = 1 from features, layer_output_size = 1280 for efficientnet_b0 model)

To-do

  • Benchmark speed and accuracy
  • Add ability to fine-tune on input data
  • Export documentation to a normal place

img2vec's People

Contributors

christiansafka avatar cobanov avatar kapily avatar keherri avatar mdkozlowski avatar noibar avatar yuiseki avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

img2vec's Issues

Speeding up a script that uses Img2Vec

Is there a way to run this across 4 GPUs or speed it up any other way? The script takes a really long time. Also, I have 4 GPUs each with 11G of GPU memory. However, this code only takes ~1G of one of the GPUs.

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

# # Initialize Img2Vec with GPU
img2vec = Img2Vec(cuda=True)
for dirpath, dirname, filename in os.walk('/SeaExp/ABC/'):
    if dirpath.endswith('XYZ'):
        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)

KeyError: 'Model vgg was not found'

Hello,

When I type the following for instance:

img2vec = Img2Vec(model='vgg')

I get the following error:

KeyError: 'Model vgg was not found'

Is VGG not supported in your code anymore?

Thanks.

RGBA images convert

Hi,
I used your library and got a problem with my images because the PIL channels were 'RGBA' instead of 'RGB'. It is not a big deal since it is possible to convert using the img.convert('RGB') function.
I just thought you could add a comment somewhere that the model needs RGB image, or you could try to convert the input image yourself in the get_vec function ?

Have a good day !
Pierrot

RuntimeError: cuDNN error: "CUDNN_STATUS_NOT_INITIALIZED" when using torch 1.8

Hi,

I was trying out the new PyTorch 1.8 release, with torchvision 0.9, and I got this error when trying to do a forward pass using your package (using CUDA):

---> 18     vectors = model.get_vec(imgs[0])
     19 
     20 embs = get_embeddings(img2vec, '../data/image_cache')

~/python/fit-the-size/.venv/lib/python3.8/site-packages/img2vec_pytorch/img_to_vec.py in get_vec(self, img, tensor)
     70 
     71             h = self.extraction_layer.register_forward_hook(copy_data)
---> 72             h_x = self.model(image)
     73             h.remove()
     74 

~/python/fit-the-size/.venv/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    887             result = self._slow_forward(*input, **kwargs)
    888         else:
--> 889             result = self.forward(*input, **kwargs)
    890         for hook in itertools.chain(
    891                 _global_forward_hooks.values(),

~/python/fit-the-size/.venv/lib/python3.8/site-packages/torchvision/models/resnet.py in forward(self, x)
    247 
    248     def forward(self, x: Tensor) -> Tensor:
--> 249         return self._forward_impl(x)
    250 
    251 

~/python/fit-the-size/.venv/lib/python3.8/site-packages/torchvision/models/resnet.py in _forward_impl(self, x)
    230     def _forward_impl(self, x: Tensor) -> Tensor:
    231         # See note [TorchScript super()]
--> 232         x = self.conv1(x)
    233         x = self.bn1(x)
    234         x = self.relu(x)

~/python/fit-the-size/.venv/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    887             result = self._slow_forward(*input, **kwargs)
    888         else:
--> 889             result = self.forward(*input, **kwargs)
    890         for hook in itertools.chain(
    891                 _global_forward_hooks.values(),

~/python/fit-the-size/.venv/lib/python3.8/site-packages/torch/nn/modules/conv.py in forward(self, input)
    397 
    398     def forward(self, input: Tensor) -> Tensor:
--> 399         return self._conv_forward(input, self.weight, self.bias)
    400 
    401 class Conv3d(_ConvNd):

~/python/fit-the-size/.venv/lib/python3.8/site-packages/torch/nn/modules/conv.py in _conv_forward(self, input, weight, bias)
    393                             weight, bias, self.stride,
    394                             _pair(0), self.dilation, self.groups)
--> 395         return F.conv2d(input, weight, bias, self.stride,
    396                         self.padding, self.dilation, self.groups)
    397 

RuntimeError: cuDNN error: CUDNN_STATUS_NOT_INITIALIZED

when I revert to torchvision 0.8 and torch 1.7.0 everything works as expected. Not exactly sure on how to fix this though. any thoughts?
thanks for the useful module by the way, very convenient!

How can I change the code to work with ResNet50?

I added these lines:

        elif model_name == 'resnet-50':
            model = models.resnet50(pretrained=True)
            if layer == 'default':
                layer = model._modules.get('avgpool')
                self.layer_output_size = 512
            else:
                layer = model._modules.get(layer)

But I get the following error:

[jalal@goku example]$ python img_to_vec_nearest_neighbors.py 
/scratch/sjn-p3/anaconda/anaconda3/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88
  return f(*args, **kwds)
/scratch/sjn-p3/anaconda/anaconda3/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88
  return f(*args, **kwds)
/scratch/sjn-p3/anaconda/anaconda3/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88
  return f(*args, **kwds)
/scratch/sjn-p3/anaconda/anaconda3/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88
  return f(*args, **kwds)
Traceback (most recent call last):
  File "img_to_vec_nearest_neighbors.py", line 11, in <module>
    img2vec = Img2Vec()
  File "../img_to_vec.py", line 18, in __init__
    self.model, self.extraction_layer = self._get_model_and_layer(model, layer)
TypeError: 'NoneType' object is not iterable

How can I create image embeddings of size 256?

Thanks for your great work. I noticed that smallest image embedding pertains to ResNet18 which is 512. Do you have any suggestion for a proper model that could lead to feature size of 256?

using img2vec for a resnet18 trained on specific images that has a pytorch lightning ckpt

Hi Christian,

I am wondering if there would be a chance to use the following script but use the ckpt from https://github.com/ozanciga/self-supervised-histopathology/releases/tag/tenpercent ( 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/self-supervised-histopathology#8

RuntimeError: The size of tensor a (4) must match the size of tensor b (3) at non-singleton dimension 0

When I tried to run the test code, I found that I met an error
RuntimeError: The size of tensor a (4) must match the size of tensor b (3) at non-singleton dimension 0
Then I just added .convert('RGB'), bug fixed
ofc,the Scale method is too old, so I changed it
here is the full code
scaler=transforms.Resize((224,224)) normalize=transforms.Normalize(mean=[0.485,0.456,0.406],std=[0.229,0.224,0.225]) to_tensor=transforms.ToTensor() def get_vector(image_name): img=Image.open(image_name).convert('RGB') t_img=Variable(normalize(to_tensor(scaler(img))).unsqueeze(0)) embeddings=torch.zeros(512) def copy(m,i,o): embeddings.copy_(o.data) h=layer.register_forward_hook(copy) model(t_img) h.remove() return embeddings

output with shape [512] doesn't match the broadcast shape [1, 512, 1, 512]

Hi I was using your get vector function to extract features of MSCOCO images and I am getting the above error .
Can you please help me I dont understand why is this happening.

RuntimeError Traceback (most recent call last)
in
1 image=[]
2 for path in new_df['filename']:
----> 3 img=get_vector(path)
4 image.append(img)

in get_vector(image_name)
13 h = layer.register_forward_hook(copy_data)
14 # 6. Run the model on our transformed image
---> 15 model(t_img)
16 # 7. Detach our copy function from the layer
17 h.remove()

/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in call(self, *input, **kwargs)
548 result = self._slow_forward(*input, **kwargs)
549 else:
--> 550 result = self.forward(*input, **kwargs)
551 for hook in self._forward_hooks.values():
552 hook_result = hook(self, input, result)

/opt/conda/lib/python3.7/site-packages/torchvision/models/resnet.py in forward(self, x)
218
219 def forward(self, x):
--> 220 return self._forward_impl(x)
221
222

/opt/conda/lib/python3.7/site-packages/torchvision/models/resnet.py in _forward_impl(self, x)
211 x = self.layer4(x)
212
--> 213 x = self.avgpool(x)
214 x = torch.flatten(x, 1)
215 x = self.fc(x)

/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in call(self, *input, **kwargs)
550 result = self.forward(*input, **kwargs)
551 for hook in self._forward_hooks.values():
--> 552 hook_result = hook(self, input, result)
553 if hook_result is not None:
554 result = hook_result

in copy_data(m, i, o)
9 # 4. Define a function that will copy the output of a layer
10 def copy_data(m, i, o):
---> 11 my_embedding.copy_(o.data)
12 # 5. Attach that function to our selected layer
13 h = layer.register_forward_hook(copy_data)

RuntimeError: output with shape [512] doesn't match the broadcast shape [1, 512, 1, 512]

Cuda Performance Question

Hello,

First off, thank you for this library! It's awesome!

This is a general question and not an issue.

I'm implementing this library like so to test the timing.

tic = time.perf_counter()

toc = time.perf_counter()
print(f"Step Zero {toc - tic:0.4f} seconds")
img2vec = Img2Vec(cuda=False, model="resnet18", layer_output_size=512)

toc = time.perf_counter()
print(f"Step One {toc - tic:0.4f} seconds")

img = Image.open(requests.get(request.url, stream=True).raw).convert("RGB")
toc = time.perf_counter()
print(f"Step Two {toc - tic:0.4f} seconds")

vector = img2vec.get_vec(img, tensor=False)
print(f"Step Three {toc - tic:0.4f} seconds")

This outputs.

Step Zero 0.0000 seconds
Step One 0.2044 seconds
Step Two 0.5028 seconds
Step Three 0.5361 seconds

If I were to run this with cuda=True would I see the performance improvement at Step One? If so, what could I general expect for a performance increase?

Thanks again.

Storing the Downloaded Models

Is there a way I can store the ResNet50 image that is download somewhere so that it's not re-downloaded when I initialize img2Vec via img2vec = Img2Vec(cuda=False, model="resnet50", layer_output_size=2048)

I want to create an Docker image that uses this library which will run through AWS lambda and I'd like to store the Model so that every time the lambda function kicks-off it's not re downloading the model when img2vec = Img2Vec(cuda=False, model="resnet50", layer_output_size=2048) is called.

torchvision's `pretrained` deprecation warning

I'm on torchvision==0.16.2 and I'm getting the following deprecation warning:

/usr/local/lib/python3.9/site-packages/torchvision/models/_utils.py:208: UserWarning: The parameter 'pretrained' is deprecated since 0.13 and may be removed in the future, please use 'weights' instead.
  warnings.warn(
/usr/local/lib/python3.9/site-packages/torchvision/models/_utils.py:223: UserWarning: Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and may be removed in the future. The current behavior is equivalent to passing `weights=ResNet18_Weights.IMAGENET1K_V1`. You can also use `weights=ResNet18_Weights.DEFAULT` to get the most up-to-date weights.

Question: Should I resize my images?

I'm just wondering if I should be resizing the provided image to the Resnet expected size?

For example Resnet50 is expecting 224x224 image sizes. Or is that taken care of?

Best Model for High Accuracy When Performing CosineSimilarity Search

Hello,

I'm wonder which model is the best to use when trying to find the most accurate images, regardless of performance, when performing a CosineSimilarity Search? My goal is to find the same image, but with very slight differences, i.e. a smudge or slight glare.

Thank you.

Reduce dimension

hello,

is there any way i can use other model and layer to output vectors with 128 dimensions instead of 512, which is the smallest one in img2vec. or maybe there is a way to reduce it to 128 dimensions?

AssertionError: Torch not compiled with CUDA enabled

system specification:
Windows 11
PyTorch version: 2.2.2
Package: Pip
compute platform: CUDA 12.1
python version: 3.12

proof that torch is installed:

C:\Users\User>python
Python 3.12.0 (tags/v3.12.0:0fb18b0, Oct  2 2023, 13:03:39) [MSC v.1935 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> torch.cuda.is_available()
True

proof that cuda is installed:

nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2023 NVIDIA Corporation
Built on Wed_Feb__8_05:53:42_Coordinated_Universal_Time_2023
Cuda compilation tools, release 12.1, V12.1.66
Build cuda_12.1.r12.1/compiler.32415258_0

although both torch and cuda are installed, I am getting this error:

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
Cell In[131], [line 19](vscode-notebook-cell:?execution_count=131&line=19)
      [1](vscode-notebook-cell:?execution_count=131&line=1) # print(X_train.shape)
      [2](vscode-notebook-cell:?execution_count=131&line=2) # print(type(list(X_train[0][0][0])))
      [3](vscode-notebook-cell:?execution_count=131&line=3) 
   (...)
     [16](vscode-notebook-cell:?execution_count=131&line=16) 
     [17](vscode-notebook-cell:?execution_count=131&line=17) # convert each image into a vector of size 512 using pretrained Resnet-18 
     [18](vscode-notebook-cell:?execution_count=131&line=18) X_train_features = []
---> [19](vscode-notebook-cell:?execution_count=131&line=19) img2vec = Img2Vec(cuda=True)
     [20](vscode-notebook-cell:?execution_count=131&line=20) for i in range(0, len(X_train_images)):
     [21](vscode-notebook-cell:?execution_count=131&line=21)     temp_PIL_img = Image.fromarray(np.uint8(X_train_images[i])).convert('RGB')

File [~\AppData\Roaming\Python\Python312\site-packages\img2vec_pytorch\img_to_vec.py:40](https://file+.vscode-resource.vscode-cdn.net/d%3A/codes/PROJECTS/fourth%20year/second%20semester/satellite%20imaging/lab3/~/AppData/Roaming/Python/Python312/site-packages/img2vec_pytorch/img_to_vec.py:40), in Img2Vec.__init__(self, cuda, model, layer, layer_output_size)
     [36](https://file+.vscode-resource.vscode-cdn.net/d%3A/codes/PROJECTS/fourth%20year/second%20semester/satellite%20imaging/lab3/~/AppData/Roaming/Python/Python312/site-packages/img2vec_pytorch/img_to_vec.py:36) self.model_name = model
     [38](https://file+.vscode-resource.vscode-cdn.net/d%3A/codes/PROJECTS/fourth%20year/second%20semester/satellite%20imaging/lab3/~/AppData/Roaming/Python/Python312/site-packages/img2vec_pytorch/img_to_vec.py:38) self.model, self.extraction_layer = self._get_model_and_layer(model, layer)
---> [40](https://file+.vscode-resource.vscode-cdn.net/d%3A/codes/PROJECTS/fourth%20year/second%20semester/satellite%20imaging/lab3/~/AppData/Roaming/Python/Python312/site-packages/img2vec_pytorch/img_to_vec.py:40) self.model = self.model.to(self.device)
     [42](https://file+.vscode-resource.vscode-cdn.net/d%3A/codes/PROJECTS/fourth%20year/second%20semester/satellite%20imaging/lab3/~/AppData/Roaming/Python/Python312/site-packages/img2vec_pytorch/img_to_vec.py:42) self.model.eval()
     [44](https://file+.vscode-resource.vscode-cdn.net/d%3A/codes/PROJECTS/fourth%20year/second%20semester/satellite%20imaging/lab3/~/AppData/Roaming/Python/Python312/site-packages/img2vec_pytorch/img_to_vec.py:44) self.scaler = transforms.Resize((224, 224))

File [~\AppData\Roaming\Python\Python312\site-packages\torch\nn\modules\module.py:1152](https://file+.vscode-resource.vscode-cdn.net/d%3A/codes/PROJECTS/fourth%20year/second%20semester/satellite%20imaging/lab3/~/AppData/Roaming/Python/Python312/site-packages/torch/nn/modules/module.py:1152), in Module.to(self, *args, **kwargs)
   [1148](https://file+.vscode-resource.vscode-cdn.net/d%3A/codes/PROJECTS/fourth%20year/second%20semester/satellite%20imaging/lab3/~/AppData/Roaming/Python/Python312/site-packages/torch/nn/modules/module.py:1148)         return t.to(device, dtype if t.is_floating_point() or t.is_complex() else None,
   [1149](https://file+.vscode-resource.vscode-cdn.net/d%3A/codes/PROJECTS/fourth%20year/second%20semester/satellite%20imaging/lab3/~/AppData/Roaming/Python/Python312/site-packages/torch/nn/modules/module.py:1149)                     non_blocking, memory_format=convert_to_format)
   [1150](https://file+.vscode-resource.vscode-cdn.net/d%3A/codes/PROJECTS/fourth%20year/second%20semester/satellite%20imaging/lab3/~/AppData/Roaming/Python/Python312/site-packages/torch/nn/modules/module.py:1150)     return t.to(device, dtype if t.is_floating_point() or t.is_complex() else None, non_blocking)
-> [1152](https://file+.vscode-resource.vscode-cdn.net/d%3A/codes/PROJECTS/fourth%20year/second%20semester/satellite%20imaging/lab3/~/AppData/Roaming/Python/Python312/site-packages/torch/nn/modules/module.py:1152) return self._apply(convert)

File [~\AppData\Roaming\Python\Python312\site-packages\torch\nn\modules\module.py:802](https://file+.vscode-resource.vscode-cdn.net/d%3A/codes/PROJECTS/fourth%20year/second%20semester/satellite%20imaging/lab3/~/AppData/Roaming/Python/Python312/site-packages/torch/nn/modules/module.py:802), in Module._apply(self, fn, recurse)
    [800](https://file+.vscode-resource.vscode-cdn.net/d%3A/codes/PROJECTS/fourth%20year/second%20semester/satellite%20imaging/lab3/~/AppData/Roaming/Python/Python312/site-packages/torch/nn/modules/module.py:800) if recurse:
    [801](https://file+.vscode-resource.vscode-cdn.net/d%3A/codes/PROJECTS/fourth%20year/second%20semester/satellite%20imaging/lab3/~/AppData/Roaming/Python/Python312/site-packages/torch/nn/modules/module.py:801)     for module in self.children():
--> [802](https://file+.vscode-resource.vscode-cdn.net/d%3A/codes/PROJECTS/fourth%20year/second%20semester/satellite%20imaging/lab3/~/AppData/Roaming/Python/Python312/site-packages/torch/nn/modules/module.py:802)         module._apply(fn)
    [804](https://file+.vscode-resource.vscode-cdn.net/d%3A/codes/PROJECTS/fourth%20year/second%20semester/satellite%20imaging/lab3/~/AppData/Roaming/Python/Python312/site-packages/torch/nn/modules/module.py:804) def compute_should_use_set_data(tensor, tensor_applied):
    [805](https://file+.vscode-resource.vscode-cdn.net/d%3A/codes/PROJECTS/fourth%20year/second%20semester/satellite%20imaging/lab3/~/AppData/Roaming/Python/Python312/site-packages/torch/nn/modules/module.py:805)     if torch._has_compatible_shallow_copy_type(tensor, tensor_applied):
    [806](https://file+.vscode-resource.vscode-cdn.net/d%3A/codes/PROJECTS/fourth%20year/second%20semester/satellite%20imaging/lab3/~/AppData/Roaming/Python/Python312/site-packages/torch/nn/modules/module.py:806)         # If the new tensor has compatible tensor type as the existing tensor,
    [807](https://file+.vscode-resource.vscode-cdn.net/d%3A/codes/PROJECTS/fourth%20year/second%20semester/satellite%20imaging/lab3/~/AppData/Roaming/Python/Python312/site-packages/torch/nn/modules/module.py:807)         # the current behavior is to change the tensor in-place using `.data =`,
   (...)
    [812](https://file+.vscode-resource.vscode-cdn.net/d%3A/codes/PROJECTS/fourth%20year/second%20semester/satellite%20imaging/lab3/~/AppData/Roaming/Python/Python312/site-packages/torch/nn/modules/module.py:812)         # global flag to let the user control whether they want the future
    [813](https://file+.vscode-resource.vscode-cdn.net/d%3A/codes/PROJECTS/fourth%20year/second%20semester/satellite%20imaging/lab3/~/AppData/Roaming/Python/Python312/site-packages/torch/nn/modules/module.py:813)         # behavior of overwriting the existing tensor or not.

File [~\AppData\Roaming\Python\Python312\site-packages\torch\nn\modules\module.py:825](https://file+.vscode-resource.vscode-cdn.net/d%3A/codes/PROJECTS/fourth%20year/second%20semester/satellite%20imaging/lab3/~/AppData/Roaming/Python/Python312/site-packages/torch/nn/modules/module.py:825), in Module._apply(self, fn, recurse)
    [821](https://file+.vscode-resource.vscode-cdn.net/d%3A/codes/PROJECTS/fourth%20year/second%20semester/satellite%20imaging/lab3/~/AppData/Roaming/Python/Python312/site-packages/torch/nn/modules/module.py:821) # Tensors stored in modules are graph leaves, and we don't want to
    [822](https://file+.vscode-resource.vscode-cdn.net/d%3A/codes/PROJECTS/fourth%20year/second%20semester/satellite%20imaging/lab3/~/AppData/Roaming/Python/Python312/site-packages/torch/nn/modules/module.py:822) # track autograd history of `param_applied`, so we have to use
    [823](https://file+.vscode-resource.vscode-cdn.net/d%3A/codes/PROJECTS/fourth%20year/second%20semester/satellite%20imaging/lab3/~/AppData/Roaming/Python/Python312/site-packages/torch/nn/modules/module.py:823) # `with torch.no_grad():`
    [824](https://file+.vscode-resource.vscode-cdn.net/d%3A/codes/PROJECTS/fourth%20year/second%20semester/satellite%20imaging/lab3/~/AppData/Roaming/Python/Python312/site-packages/torch/nn/modules/module.py:824) with torch.no_grad():
--> [825](https://file+.vscode-resource.vscode-cdn.net/d%3A/codes/PROJECTS/fourth%20year/second%20semester/satellite%20imaging/lab3/~/AppData/Roaming/Python/Python312/site-packages/torch/nn/modules/module.py:825)     param_applied = fn(param)
    [826](https://file+.vscode-resource.vscode-cdn.net/d%3A/codes/PROJECTS/fourth%20year/second%20semester/satellite%20imaging/lab3/~/AppData/Roaming/Python/Python312/site-packages/torch/nn/modules/module.py:826) should_use_set_data = compute_should_use_set_data(param, param_applied)
    [827](https://file+.vscode-resource.vscode-cdn.net/d%3A/codes/PROJECTS/fourth%20year/second%20semester/satellite%20imaging/lab3/~/AppData/Roaming/Python/Python312/site-packages/torch/nn/modules/module.py:827) if should_use_set_data:

File [~\AppData\Roaming\Python\Python312\site-packages\torch\nn\modules\module.py:1150](https://file+.vscode-resource.vscode-cdn.net/d%3A/codes/PROJECTS/fourth%20year/second%20semester/satellite%20imaging/lab3/~/AppData/Roaming/Python/Python312/site-packages/torch/nn/modules/module.py:1150), in Module.to.<locals>.convert(t)
   [1147](https://file+.vscode-resource.vscode-cdn.net/d%3A/codes/PROJECTS/fourth%20year/second%20semester/satellite%20imaging/lab3/~/AppData/Roaming/Python/Python312/site-packages/torch/nn/modules/module.py:1147) if convert_to_format is not None and t.dim() in (4, 5):
   [1148](https://file+.vscode-resource.vscode-cdn.net/d%3A/codes/PROJECTS/fourth%20year/second%20semester/satellite%20imaging/lab3/~/AppData/Roaming/Python/Python312/site-packages/torch/nn/modules/module.py:1148)     return t.to(device, dtype if t.is_floating_point() or t.is_complex() else None,
   [1149](https://file+.vscode-resource.vscode-cdn.net/d%3A/codes/PROJECTS/fourth%20year/second%20semester/satellite%20imaging/lab3/~/AppData/Roaming/Python/Python312/site-packages/torch/nn/modules/module.py:1149)                 non_blocking, memory_format=convert_to_format)
-> [1150](https://file+.vscode-resource.vscode-cdn.net/d%3A/codes/PROJECTS/fourth%20year/second%20semester/satellite%20imaging/lab3/~/AppData/Roaming/Python/Python312/site-packages/torch/nn/modules/module.py:1150) return t.to(device, dtype if t.is_floating_point() or t.is_complex() else None, non_blocking)

File [~\AppData\Roaming\Python\Python312\site-packages\torch\cuda\__init__.py:293](https://file+.vscode-resource.vscode-cdn.net/d%3A/codes/PROJECTS/fourth%20year/second%20semester/satellite%20imaging/lab3/~/AppData/Roaming/Python/Python312/site-packages/torch/cuda/__init__.py:293), in _lazy_init()
    [288](https://file+.vscode-resource.vscode-cdn.net/d%3A/codes/PROJECTS/fourth%20year/second%20semester/satellite%20imaging/lab3/~/AppData/Roaming/Python/Python312/site-packages/torch/cuda/__init__.py:288)     raise RuntimeError(
    [289](https://file+.vscode-resource.vscode-cdn.net/d%3A/codes/PROJECTS/fourth%20year/second%20semester/satellite%20imaging/lab3/~/AppData/Roaming/Python/Python312/site-packages/torch/cuda/__init__.py:289)         "Cannot re-initialize CUDA in forked subprocess. To use CUDA with "
    [290](https://file+.vscode-resource.vscode-cdn.net/d%3A/codes/PROJECTS/fourth%20year/second%20semester/satellite%20imaging/lab3/~/AppData/Roaming/Python/Python312/site-packages/torch/cuda/__init__.py:290)         "multiprocessing, you must use the 'spawn' start method"
    [291](https://file+.vscode-resource.vscode-cdn.net/d%3A/codes/PROJECTS/fourth%20year/second%20semester/satellite%20imaging/lab3/~/AppData/Roaming/Python/Python312/site-packages/torch/cuda/__init__.py:291)     )
    [292](https://file+.vscode-resource.vscode-cdn.net/d%3A/codes/PROJECTS/fourth%20year/second%20semester/satellite%20imaging/lab3/~/AppData/Roaming/Python/Python312/site-packages/torch/cuda/__init__.py:292) if not hasattr(torch._C, "_cuda_getDeviceCount"):
--> [293](https://file+.vscode-resource.vscode-cdn.net/d%3A/codes/PROJECTS/fourth%20year/second%20semester/satellite%20imaging/lab3/~/AppData/Roaming/Python/Python312/site-packages/torch/cuda/__init__.py:293)     raise AssertionError("Torch not compiled with CUDA enabled")
    [294](https://file+.vscode-resource.vscode-cdn.net/d%3A/codes/PROJECTS/fourth%20year/second%20semester/satellite%20imaging/lab3/~/AppData/Roaming/Python/Python312/site-packages/torch/cuda/__init__.py:294) if _cudart is None:
    [295](https://file+.vscode-resource.vscode-cdn.net/d%3A/codes/PROJECTS/fourth%20year/second%20semester/satellite%20imaging/lab3/~/AppData/Roaming/Python/Python312/site-packages/torch/cuda/__init__.py:295)     raise AssertionError(
    [296](https://file+.vscode-resource.vscode-cdn.net/d%3A/codes/PROJECTS/fourth%20year/second%20semester/satellite%20imaging/lab3/~/AppData/Roaming/Python/Python312/site-packages/torch/cuda/__init__.py:296)         "libcudart functions unavailable. It looks like you have a broken build?"
    [297](https://file+.vscode-resource.vscode-cdn.net/d%3A/codes/PROJECTS/fourth%20year/second%20semester/satellite%20imaging/lab3/~/AppData/Roaming/Python/Python312/site-packages/torch/cuda/__init__.py:297)     )

AssertionError: Torch not compiled with CUDA enabled

Runtime error

Hi,

I get this error on this piece of code:
def copy_data(m, i, o): my_embedding.copy_(o.data)

RuntimeError Message=expand(torch.FloatTensor{[1, 512, 1, 1]}, size=[512]): the number of sizes provided (1) must be greater or equal to the number of dimensions in the tensor (4)

I am running pytorch-cpu 0.4.0-py36_cpuhe774522_1

torchvision 0.9.0 waring

transforms.py:286: UserWarning: The use of the transforms.Scale transform is deprecated, please use transforms.Resize instead.
"please use transforms.Resize instead.")

change the layer_output_size of `resnet18`

I want to change the output size of Resnet-18 which default size is 512. It seems to large for us to storage in faiss. So I want to change it to 128 . It failed when I tried.

alexnet tensor size error

Current, using alexnet throws the error The expanded size of the tensor (1) must match the existing size (4096) at non-singleton dimension 3 resulting from the previous update my_embedding = torch.zeros(1, self.layer_output_size, 1, 1)

Patch for using alexnet:

  1. Change my_embedding = torch.zeros(1, self.layer_output_size, 1, 1) to my_embedding = torch.zeros(1, self.layer_output_size) if using alexnet
  2. Change tensor = my_embedding.numpy()[0, :, 1, 1] to tensor = my_embedding.numpy()[0, :]

Thanks for making this. Look forward to pip installable version.

OrderedDict mutated during iteration

I occured with an error :

 img = Image.open('xxx.jpg')
 embddeing = img2vec.get_vec(img=img)

I got the error OrderedDict mutated during iteration. Not all the time, it's seldom. I read the source code and I don't find any answer about this error.

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.