2

I am attempting to use AlexNet as a feature extractor for my 3 channel image inputs derived from .wav file data. I have the input to the feature extractor of shape (593, 3, 227, 227). However, when using the AlexNet model, I am getting the error

Traceback (most recent call last):
  File "MainUI.py", line 1625, in <module>
    main(False)
  File "MainUI.py", line 1604, in main
    accuracy_measurement(oversample)
  File "MainUI.py", line 1463, in accuracy_measurement
    features = model.extract_features(features.double())
  File "/Users/sruthikurada/opt/anaconda3/lib/python3.7/site-packages/alexnet_pytorch/model.py", line 77, in extract_features
    x = self.features(inputs)
  File "/Users/sruthikurada/opt/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py", line 550, in __call__
    result = self.forward(*input, **kwargs)
  File "/Users/sruthikurada/opt/anaconda3/lib/python3.7/site-packages/torch/nn/modules/container.py", line 100, in forward
    input = module(input)
  File "/Users/sruthikurada/opt/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py", line 550, in __call__
    result = self.forward(*input, **kwargs)
  File "/Users/sruthikurada/opt/anaconda3/lib/python3.7/site-packages/torch/nn/modules/conv.py", line 353, in forward
    return self._conv_forward(input, self.weight)
  File "/Users/sruthikurada/opt/anaconda3/lib/python3.7/site-packages/torch/nn/modules/conv.py", line 350, in _conv_forward
    self.padding, self.dilation, self.groups)
RuntimeError: Expected object of scalar type Double but got scalar type Float for argument #3 'mat1' in call to _th_addmm_

My code that results in this error:

features, labels = extract_features(train_files)

print(features.shape) # (593, 3, 227, 227)

import torch
from alexnet_pytorch import AlexNet
model = AlexNet.from_pretrained('alexnet')

features = torch.from_numpy(features).type('torch.DoubleTensor')

features = model.extract_features(features.double()) # <-- This is where the error occurs
print(features.shape)

As you can see, I used the double() command, but that didn't help. Could you kindly provide some assistance?

  • Does this answer your question? [How to fix RuntimeError "Expected object of scalar type Float but got scalar type Double for argument"?](https://stackoverflow.com/questions/56741087/how-to-fix-runtimeerror-expected-object-of-scalar-type-float-but-got-scalar-typ) – kHarshit Jul 05 '20 at 08:26
  • 1
    I tried what the answer suggested, using the .double() command, but it didn't help the features are still torch.float64 – Sruthi Kurada Jul 05 '20 at 14:36
  • Try features = model.extract_features(features.float()) – Yan Zhao Nov 29 '20 at 16:45

1 Answers1

0

When I have a tensor of dtype torch.int64 as input of nn.Conv2d(), I got a similar error:

RuntimeError: Expected object of scalar type Long but got scalar type Float for argument #3 'mat1' in call to th_addmm

import torch.nn as nn
import torch.nn.functional as F

simple_model = nn.Sequential(
    nn.Conv2d(3, 8, kernel_size=3, stride=1, padding=1),
    nn.MaxPool2d(2, 2)
)

print(images.dtype)
for images, labels in train_dl:
    print('images.shape:', images.shape)
    out = simple_model(images)
    print('out.shape:', out.shape)
    break

When I change the images to images.float(), that fixed the issue.

print(images.dtype)
for images, labels in train_dl:
    print('images.shape:', images.shape)
    out = simple_model(images.float()) #must be float instead of double
    print('out.shape:', out.shape)
    break
Yan Zhao
  • 359
  • 4
  • 15