How to train Pytorch model on custom data
I am very rookie in transferring my code from Keras/Tensorflow to Pytorch and I am trying to retrain my TF model in Pytorch, however, my dataset has some particularities which make it difficult to me to make it run in Pytorch.
To understand my issues, recall that I have a custom dataset initialized this way:
class MyDataSet(torch.utils.data.Dataset):
    def __init__(self, x, y, transform=None):
        super(MyDataSet, self).__init__()
        # store the raw tensors
        self._x = np.load(x)
        self._y = np.load(y)
    
        self._x=np.swapaxes(self._x,3,2)
        self._x=np.swapaxes(self._x,2,1)
        self.transform = transform
    
    def __len__(self):
        # a DataSet must know it size
        return self._x.shape[0]

    def __getitem__(self, index):
        x = self._x[index, :]
        y = self._y[index]
    
        return x, y

The shape of _self._x is (12000, 3, 224, 224) and the shape of self._y is (12000,)
I am fine-tuning a pre-trained RESNET-50 in this data, and the training happens the following way:
import torch
import torchvision
import torchvision.transforms as transforms
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.autograd import Variable
import numpy as np
from torch.utils.data import Dataset, DataLoader
from torchvision.models import resnet50
import time
import copy

def set_parameter_requires_grad(model, feature_extracting):
    if feature_extracting:
        for param in model.parameters():
            param.requires_grad = False
        
#Transform dataset 
print(&quot;Loading Data&quot;)
transform = transforms.Compose([transforms.ToTensor()])
dataset = MyDataSet(&quot;me/train1-features.npy&quot;,&quot;/me/train1-classes.npy&quot;,transform=transform)
dataloader = DataLoader(dataset, batch_size=4)

print(&quot;Configuring network&quot;)
feature_extract = True
num_epochs = 15
num_classes=12

model_ft = resnet50(pretrained=True)
set_parameter_requires_grad(model_ft, feature_extract)
num_ftrs = model_ft.fc.in_features
model_ft.fc = nn.Linear(num_ftrs, num_classes)

if torch.cuda.is_available():
    model_ft.cuda()

params_to_update = model_ft.parameters()

print(&quot;Params to learn:&quot;)
if feature_extract:
    params_to_update = []
    for name,param in model_ft.named_parameters():
        if param.requires_grad == True:
            params_to_update.append(param)
            print(&quot;\t&quot;,name)

else:
    for name,param in model_ft.named_parameters():
        if param.requires_grad == True:
            print(&quot;\t&quot;,name)

# Observe that all parameters are being optimized
optimizer_ft = optim.SGD(params_to_update, lr=0.001, momentum=0.9)

# Setup the loss fxn
criterion = nn.CrossEntropyLoss()

#Train (how to validate?)

for epoch in range(num_epochs):  # loop over the dataset multiple times

    running_loss = 0.0
    for i, data in enumerate(dataloader, 0):
              
        # get the inputs; data is a list of [inputs, labels]
        inputs, labels = data
        
        #transfer labels and inputs to cuda()
        inputs,labels=inputs.cuda(), labels.cuda()
        
        # zero the parameter gradients
        optimizer_ft.zero_grad()

        # forward + backward + optimize
        outputs = model_ft(inputs)
        loss = loss_func(outputs, labels)
        loss.backward()
        optimizer.step()

        # print statistics
        running_loss += loss.item()
        if i % 2000 == 1999:    # print every 2000 mini-batches
            print(f'[{epoch + 1}, {i + 1:5d}] loss: {running_loss / 2000:.3f}')
            running_loss = 0.0

However, whenever I run this code, I receive the following error
    Traceback (most recent call last):
  File &quot;train_my_data_example.py&quot;, line 114, in &lt;module&gt;
    outputs = model_ft(inputs)
  File &quot;/usr/local/lib/python3.8/dist-packages/torch/nn/modules/module.py&quot;, line 889, in _call_impl
    result = self.forward(*input, **kwargs)
  File &quot;/usr/local/lib/python3.8/dist-packages/torchvision/models/resnet.py&quot;, line 249, in forward
    return self._forward_impl(x)
  File &quot;/usr/local/lib/python3.8/dist-packages/torchvision/models/resnet.py&quot;, line 232, in _forward_impl
    x = self.conv1(x)
  File &quot;/usr/local/lib/python3.8/dist-packages/torch/nn/modules/module.py&quot;, line 889, in _call_impl
    result = self.forward(*input, **kwargs)
  File &quot;/usr/local/lib/python3.8/dist-packages/torch/nn/modules/conv.py&quot;, line 399, in forward
    return self._conv_forward(input, self.weight, self.bias)
  File &quot;/usr/local/lib/python3.8/dist-packages/torch/nn/modules/conv.py&quot;, line 395, in _conv_forward
    return F.conv2d(input, weight, bias, self.stride,
RuntimeError: Input type (torch.cuda.ByteTensor) and weight type (torch.cuda.FloatTensor) should be the same

I also can do the train and validation procedures normally on TF/Keras, but I don't know how to do that in my custom Dataset with Pytorch.
How can I solve my problem and also run train/val loop with Pytorch in my custom data?

It seems that np.load is loading binary data to X so ToTensor() is trying to preserve the dtype by coercing it to a ByteTensor. You can fix this by making a small change in __getitem__:
def __getitem__(self, index):
    x = self._x[index, :]
    y = self._y[index]

    return x.astype(np.float32), y

