As stated in Python's docs:
Floating point numbers are usually implemented using
doublein C
double in C is normally a 64-bit number (double-precision, as opposed to the single-precision 32-bit float type), and it's also known as float64. So, what we call float in plain Python is usually a 64-bit float, or torch.float64.
PyTorch itself, however, uses float32 by default. AFAIK, this is done to save memory: you'll likely be training models with quite a lot of parameters, and using 32-bit floats will take two times less memory than Python's 64-bit float.
python - How to convert torch tensor to float? - Stack Overflow
Why PyTorch tensors gives preference to float32 element datatype
Python float to torch.float32
How to check if a tensor is a float/double?
Why does it appear that PyTorch tensors give preference to using default element datatype of float32 instead of float64?
For eg., the default element datatype for torch.tensor()is float32. This is the opposite with numpy arrays where the default element datatype for numpy.array()is float64. Why don’t PyTorch make it consistent with numpy arrays and make the default element datatype as float64?
(Ps. I know I can change the element datatypes in the tensor but it would be more convenient if the default was float64)
I am going round in circles with this error. Could anyone offer a pointer? I'm new to python and pytorch.
import os
import cv2
import numpy as np
import skops.io as sio # to load the saved model
from PIL import Image
from skimage.filters import laplace, sobel, roberts
from torchvision import transforms
# Load the saved SVM model
svm_model = sio.load("svm_model_trained.skops", trusted=True)
# Define the path to the directory containing the test images
test_images_dir = '/Users/test/project_dataset/archive'
# Define the label names for the predicted classes
label_names = ['Sharp', 'Defocussed', 'Motion_blurred']
# Define the transformation to be applied to each image
transform = transforms.Compose({
transforms.PILToTensor(),
transforms.Resize((224, 224)),
transforms.Normalize(mean=(0, 0, 0), std=(1, 1, 1)),
transforms.ToTensor(),
})
# Loop over each image in the test directory and predict its class
for img_name in os.listdir(test_images_dir):
if not img_name.startswith('.'):
img_path = os.path.join(test_images_dir, img_name)
img = Image.open(img_path)
img = transform(img)
img = np.array([img.numpy()])
features = []
image_gray = cv2.cvtColor(img[0], cv2.COLOR_RGB2GRAY)
lap_feat = laplace(image_gray)
sob_feat = sobel(image_gray)
rob_feat = roberts(image_gray)
features.extend([lap_feat.mean(), lap_feat.var(), np.amax(lap_feat),
sob_feat.mean(), sob_feat.var(), np.max(sob_feat),
rob_feat.mean(), rob_feat.var(), np.max(rob_feat)])
features = np.array([features])
pred = svm_model.predict(features)
print(f"Image {img_name} is classified as {label_names[pred[0]]}")