I am working on dog breed identification problem. While training I am getting zero accuracy though my loss is decreasing after each epoch. I can’t get where I am wrong so please help. I have used a dataset of images which is being loaded in the following class using root directory. The label passed to the following class is a pandas dataframe where the first column denotes id, i.e., the name of the file using which we can load the image. The second one is the breed_name( total 120 breeds ) and third is the labels or targets from 0 to 119.
class DogsDataset(Dataset):
def __init__(self, labels, root_dir, transform=None):
self.labels = labels
self.root_dir = root_dir
self.transform = transform
def __len__(self):
return self.labels.shape[0]
def __getitem__(self, idx):
img_name = '{}.jpg'.format(self.labels.iloc[idx, 0])
fullname = self.root_dir+ img_name
image = Image.open(fullname)
label = torch.tensor(labels.iloc[idx,2])
if self.transform:
image = self.transform(image)
return [image, label]
Used SGD as optimizer and cross entrpy loss as loss function
# Creating a model
use_gpu = torch.cuda.is_available()
model = models.resnet50(pretrained=True)
if use_gpu:
model = model.cuda()
for param in model.parameters():
param.requires_grad = False
if use_gpu:
model.fc = (nn.Linear(2048, num_classes)).cuda()
else:
model.fc = (nn.Linear(2048, num_classes))
def train(model, criterion, validaton_data, train_data, optimizer, epochs = 30):
use_gpu = torch.cuda.is_available()
train_loss_history = []
valid_loss_history = []
train_loader = {
'train' : train_data,
'validation' : validation_data
}
best_acc = 0
best_model = None
for epoch in tqdm(range(epochs), desc = "Loading", position = 0):
for dataset in ['train', 'validation']:
running_loss = 0
running_corrects = 0
for x, y in tqdm(train_loader[dataset]):
optimizer.zero_grad()
if use_gpu:
x, y = Variable(x.cuda()), Variable(y.cuda())
else:
x, y = Variable(x), Variable(y)
yhat = model(x)
_, preds = yhat.max(1)
loss = criterion(yhat, y)
loss.backward()
optimizer.step()
#loss_history.append(loss.item())
running_loss += loss.item()*x.size(0)
running_corrects += (torch.sum(preds == y))
if dataset == 'train':
train_loss = running_loss / 8151
train_acc = float(running_corrects / 8151)
print
print("train_loss: ", train_loss)
print("train_accuracy: ", train_acc)
train_loss_history.append(train_loss)
else:
valid_loss = running_loss / 2071
valid_acc = float(running_corrects / 2071)
print("validation_ loss: " , valid_loss)
print("validation_accuracy: " , valid_acc)
valid_loss_history.append(valid_loss)
if valid_acc > best_acc:
best_acc = valid_acc
best_model = model
print()
print("best accuracy : ", best_acc)
return best_model
The transforms that I have used
normalize = transforms.Normalize(
mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]
)
train_transform = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(200),
transforms.ColorJitter(brightness=[0.5, 1], contrast=[0.5, 1], saturation=[0.5, 1]),
transforms.RandomHorizontalFlip(p=0.5),
transforms.ToTensor(),
normalize
])