My code is for image clasification and i try to running this code but i got an error, i don't know how to solve it. i try to search it and solve it but there will always be errors in this part. Please help, thank you
import torch.utils.data as data
from torch.autograd import Variable
import numpy as np
print("Number of train samples: ", len(train_ds))
print("Number of test samples: ", len(test_ds))
print("Detected Classes are: ", train_ds.class_to_idx)
train_loader = data.DataLoader(train_ds, batch_size=BATCH_SIZE, shuffle=True, num_workers=4)
test_loader = data.DataLoader(test_ds, batch_size=BATCH_SIZE, shuffle=True, num_workers=4)
# Train the model
for epoch in range(EPOCHS):
for step, (x, y) in enumerate(train_loader):
# Change input array into list with each batch being one element
x = np.split(np.squeeze(np.array(x)), BATCH_SIZE)
# Remove unecessary dimension
for index, array in enumerate(x):
x[index] = np.squeeze(array)
# Apply feature extractor, stack back into 1 tensor and then convert to tensor
x = torch.tensor(np.stack(feature_extractor(x)['pixel_values'], axis=0))
# Send to GPU if available
x, y = x.to(device), y.to(device)
b_x = Variable(x) # batch x (image)
b_y = Variable(y) # batch y (target)
# Feed through model
output, loss = model(b_x, None)
# Calculate loss
if loss is None:
loss = loss_func(output, b_y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if step % 50 == 0:
# Get the next batch for testing purposes
test = next(iter(test_loader))
test_x = test[0]
# Reshape and get feature matrices as needed
test_x = np.split(np.squeeze(np.array(test_x)), BATCH_SIZE)
for index, array in enumerate(test_x):
test_x[index] = np.squeeze(array)
test_x = torch.tensor(np.stack(feature_extractor(test_x)['pixel_values'], axis=0))
# Send to appropirate computing device
test_x = test_x.to(device)
test_y = test[1].to(device)
# Get output (+ respective class) and compare to target
test_output, loss = model(test_x, test_y)
test_output = test_output.argmax(1)
# Calculate Accuracy
accuracy = (test_output == test_y).sum().item() / BATCH_SIZE
print('Epoch: ', epoch, '| train loss: %.4f' % loss, '| test accuracy: %.2f' % accuracy)
this is an error that i got from the code above
Number of train samples: 101
Number of test samples: 624
Detected Classes are: {'crack': 0, 'flaking': 1, 'putus': 2, 'spalling': 3, 'squat': 4}
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-11-a8be4ecb95e7> in <cell line: 13>()
25 b_y = Variable(y) # batch y (target)
26 # Feed through model
---> 27 output, loss = model(b_x, None)
28 # Calculate loss
29 if loss is None:
10 frames
/usr/local/lib/python3.9/dist-packages/torch/nn/modules/conv.py in _conv_forward(self, input, weight, bias)
457 weight, bias, self.stride,
458 _pair(0), self.dilation, self.groups)
--> 459 return F.conv2d(input, weight, bias, self.stride,
460 self.padding, self.dilation, self.groups)
461
RuntimeError: cuDNN error: CUDNN_STATUS_MAPPING_ERROR
how to solved it
thank you for the answer