RuntimeError: Input type (long int) and bias type (float) should be the same

Niraja: <p>I get this error while applying data augmentation on UTKFace dataset. This is my program code.<br>
‘’’<br>
class UTKFaceDataset(Dataset):<br>
def <strong>init</strong>(self, root_dir, transform=None):<br>
self.root_dir = root_dir<br>
self.transform = transform<br>
self.image_files = sorted(os.listdir(‘/content/drive/MyDrive/UTKFace’))</p>
<p>def <strong>len</strong>(self):<br>
return len(self.image_files)</p>
<p>def <strong>getitem</strong>(self, idx):<br>
image_path  = os.path.join(self.root_dir, self.image_files[idx])<br>
image = Image.open(image_path).convert(‘RGB’)<br>
age = int(self.image_files[idx].split(‘<em>‘)[0])<br>
gender = int(self.image_files[idx].split(’</em>’)[1])</p>
<pre><code>if self.transform:
  image = self.transform(image)
return image, age, gender
</code></pre>
<p>‘’’</p>
<p>‘’’</p>
<h1>
<a name="define-the-cnn-architecture-1" class="anchor" href="#define-the-cnn-architecture-1"></a>Define the CNN architecture</h1>
<p>class CNN(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(CNN, self).<strong>init</strong>()<br>
self.conv1 = nn.Conv2d(3, 32, kernel_size=3, stride=1, padding=1)<br>
self.conv2 = nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1)<br>
self.conv3 = nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1)<br>
self.pool = nn.MaxPool2d(kernel_size=2, stride=2)<br>
self.fc1 = nn.Linear(128 * 16 * 16, 512)<br>
self.fc2 = nn.Linear(512, 2)  # gender out<br>
self.fc3 = nn.Linear(512, 1)  # age out</p>
<p>def forward(self, x):<br>
x = self.pool(nn.functional.relu(self.conv1(x)))<br>
x = self.pool(nn.functional.relu(self.conv2(x)))<br>
x = self.pool(nn.functional.relu(self.conv3(x)))<br>
x = x.view(-1, 128 * 16 * 16)<br>
age = self.fc3(nn.functional.relu(self.fc1(x)))<br>
gender = self.fc2(nn.functional.relu(self.fc1(x)))</p>
<pre><code>return {'label1': age, 'label2': gender}
</code></pre>
<p>‘’’</p>
<p>‘’’</p>
<h1>
<a name="define-the-data-augmentation-transforms-2" class="anchor" href="#define-the-data-augmentation-transforms-2"></a>Define the data augmentation transforms</h1>
<p>transform_train = transforms.Compose([<br>
transforms.RandomCrop(128, padding=4),<br>
transforms.RandomHorizontalFlip(),<br>
transforms.RandomRotation(10),<br>
transforms.ToTensor(),<br>
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))<br>
])</p>
<p>transform_test = transforms.Compose([<br>
transforms.Resize(128),<br>
transforms.ToTensor(),<br>
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))<br>
])<br>
‘’’</p>
<p>‘’’</p>
<h1>
<a name="initialize-the-dataset-and-dataloaders-3" class="anchor" href="#initialize-the-dataset-and-dataloaders-3"></a>Initialize the dataset and dataloaders</h1>
<p>train_dataset = UTKFaceDataset(‘/content/drive/MyDrive/UTKFace’, transform=transform_train)</p>
<p>test_dataset = UTKFaceDataset(‘/content/drive/MyDrive/UTKFace’, transform=transform_test)</p>
<p>train_dataloader = DataLoader(train_dataset, batch_size=64, shuffle=True, num_workers=4)</p>
<p>test_dataloader = DataLoader(test_dataset, batch_size=64, shuffle=False, num_workers=4)<br>
‘’’<br>
‘’’</p>
<h1>
<a name="define-the-loss-function-and-optimizer-4" class="anchor" href="#define-the-loss-function-and-optimizer-4"></a>Define the loss function and optimizer</h1>
<p>criterion_age = nn.MSELoss()<br>
criterion_gender = nn.CrossEntropyLoss()<br>
optimizer = optim.Adam(cnn.parameters(), lr=0.001)</p>
<p>training_loss_list = []<br>
iter_list=[]</p>
<p>def train(model, criterion1, criterion2, train_dataloader, optimizer, epoch):</p>
<h1>
<a name="set-the-model-to-training-mode-5" class="anchor" href="#set-the-model-to-training-mode-5"></a>Set the model to training mode</h1>
<p>model.train()<br>
train_loss = 0<br>
print(“Epoch:”, epoch)</p>
<h1>
<a name="process-the-images-in-batches-6" class="anchor" href="#process-the-images-in-batches-6"></a>Process the images in batches</h1>
<p>for batch_idx, (data, target1, target2) in enumerate(train_dataloader):<br>
data, target1, target2 = data.to(device), target1.to(device), target2.to(device)</p>
<pre><code># Reset the optimizer
optimizer.zero_grad()

#Push the data forward through the model layer
output = model(data.long())

label1_hat = output['label1']
label2_hat = output['label2']

# Get the loss
loss1 = criterion1(label1_hat, target1.long())
loss2 = criterion2(label2_hat, target2.long())
loss = loss1 + loss2

# Backpropagate
loss.backward()
optimizer.step()

train_loss = train_loss + ((1 / (batch_idx + 1)) * (loss.data - train_loss))
if batch_idx % 50 == 0:
  print('Epoch %d, Batch %d loss: %.6f' % (epoch, batch_idx + 1, train_loss))
</code></pre>
<h1>
<a name="return-average-loss-for-the-epoch-7" class="anchor" href="#return-average-loss-for-the-epoch-7"></a>return average loss for the epoch</h1>
<p><span class="hashtag">#avg_loss</span> = train_loss / (batch_idx + 1)<br>
iter_list.append(epoch)<br>
training_loss_list.append(train_loss)<br>
print(‘Training set: Average loss: {:.6f}’.format(train_loss))<br>
return train_loss</p>
<p>age_accuracy_list = []<br>
gender_accuracy_list = []<br>
validation_loss_list=[]</p>
<p>def test(model, criterion1, criterion2, test_dataloader, optimizer, epoch):</p>
<h1>
<a name="switch-the-model-to-evaluation-mode-8" class="anchor" href="#switch-the-model-to-evaluation-mode-8"></a>Switch the model to evaluation mode</h1>
<p>model.eval()<br>
correct_1 = 0<br>
correct_2 = 0<br>
total_1 = 0<br>
total_2 = 0<br>
valid_loss = 0</p>
<p>with torch.no_grad():</p>
<pre><code>for batch_idx, (data, target1, target2) in enumerate(test_dataloader):
  data, target1, target2 = data.to(device), target1.to(device), target2.to(device)
  
  data = data.requires_grad_()  # Load images(for accuracy)
  output = model(data.long())

  _, predicted1 = torch.max(output['label1'], 1)
  _, predicted2 = torch.max(output['label2'], 1)

  label1_hat = output['label1']
  label2_hat = output['label2']

  total_1 += target1.size(0)
  total_2 += target2.size(0)
  
  correct_1 += torch.sum(predicted1 == target1).item()
  correct_2 += torch.sum(predicted2 == target2).item()
  
  age_accuracy = 100 * correct_1 // total_1
  gender_accuracy = 100 * correct_2 // total_2
  Total_Accuracy = age_accuracy + gender_accuracy

  # calculate loss
  loss1 = criterion1(label1_hat, target1.long())
  loss2 = criterion2(label2_hat, target2.long())
  loss = loss1+loss2
  valid_loss = valid_loss + ((1 / (batch_idx + 1)) * (loss.data - valid_loss))

print('Epoch: {} \tTraining Loss: {:.6f} \tValidation Loss: {:.6f} \tAge_Accuracy: {} \tGender_Accuracy: {} \tTotal_Accuracy: {}'.format(
        epoch, train_loss, valid_loss, age_accuracy, gender_accuracy, Total_Accuracy))
age_accuracy_list.append(age_accuracy)
gender_accuracy_list.append(gender_accuracy)
validation_loss_list.append(valid_loss)
return valid_loss, age_accuracy, gender_accuracy
</code></pre>
<p>epochs = 20<br>
print(‘Training on’, device)</p>
<p>for epoch in range(1, epochs + 1):<br>
train_loss = train(cnn, criterion_age, criterion_gender, train_dataloader, optimizer, epoch)<br>
valid_loss = test(cnn, criterion_age, criterion_gender, test_dataloader, optimizer, epoch)</p>
<p>‘’’<br>
I’m not understanding how to correct my code. Thank you in advance.</p>

ptrblck: <p>You are explicitly casting the <code>input</code> tensor to a <code>LongTensor</code> in:</p>
<pre><code class="lang-python">output = model(data.long())
</code></pre>
<p>which will raise the error since the first layer of your model (<code>self.conv1</code>) expects a floating point input.<br>
Could you explain why you are casting the <code>input</code> to <code>long</code>?</p>