0
$\begingroup$

I'm quite new to pytorch so I want check is there something wrong
I got final submission code score around 10%

here is my code

train_transform = transforms.Compose([
    transforms.Resize(224),
    transforms.RandomHorizontalFlip(p=.40),
    transforms.RandomRotation(30),
    transforms.ToTensor(),
    transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])

test_transform = transforms.Compose([
    transforms.Resize(224),
    transforms.ToTensor(),
    transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])

traindata = torchvision.datasets.CIFAR10(root='/mnt/3CE35B99003D727B/input/datasets/', train=True,download=False, transform=train_transform)
trainset,valset = random_split(traindata,[42000,8000])
trainloader = torch.utils.data.DataLoader(trainset, batch_size=batch_size,shuffle=True)
valloader = torch.utils.data.DataLoader(valset, batch_size=batch_size,shuffle=False)

testset = torchvision.datasets.CIFAR10(root='/mnt/3CE35B99003D727B/input/datasets/', train=False,download=False, transform=test_transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=batch_size,shuffle=False)

device = 'cuda' if torch.cuda.is_available() else 'cpu'

torch.manual_seed(777)
if device =='cuda':
    torch.cuda.manual_seed_all(777)
device

class Model(nn.Module):
    def __init__(self):
        super().__init__()
        base = models.resnet18(pretrained=True)
        self.base = nn.Sequential(*list(base.children())[:-1])
        in_features = base.fc.in_features
        self.drop = nn.Dropout()
        self.final = nn.Linear(in_features,10)

    def forward(self,x):
        x = self.base(x)
        x = self.drop(x.view(-1,self.final.in_features))
        return self.final(x)

model = Model().cuda()
[x for x,y in model.named_children()]


criterion = nn.CrossEntropyLoss()
param_groups = [
    {'params':model.base.parameters(),'lr':.0001},
    {'params':model.final.parameters(),'lr':.001}
]
optimizer = Adam(param_groups)
lr_scheduler = lr_scheduler.StepLR(optimizer, step_size=1, gamma=0.1)
states = {}

%%time
best_val_acc = -1000
best_val_model = None
print("start")
for epoch in range(10):  
    model.train(True)
    running_loss = 0.0
    running_acc = 0


    for i, data in enumerate(trainloader, 0):
        inputs, labels = data
        inputs, labels = inputs.cuda(),labels.cuda()

        optimizer.zero_grad()
        outputs = model(inputs)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()

        # print statistics
        running_loss += loss.item() * inputs.size(0)
        out = torch.argmax(outputs.detach(),dim=1)
        assert out.shape==labels.shape
        running_acc += (labels==out).sum().item()
    print(f"Train loss {epoch+1}: {running_loss/len(trainset)},Train Acc:{running_acc*100/len(trainset)}%")

    correct = 0
    model.train(False)
    with torch.no_grad():
        for inputs,labels in valloader:
            out = model(inputs.cuda()).cpu()
            out = torch.argmax(out,dim=1)
            acc = (out==labels).sum().item()
            correct += acc
    print(f"Val accuracy:{correct*100/len(valset)}%")
    if correct>best_val_acc:
        best_val_acc = correct
        best_val_model = deepcopy(model.state_dict())
    lr_scheduler.step()

    torch.save(model.state_dict(), PATH)

print('Finished Training')

from torch.autograd import Variable
results = []
print("start")
i = 0
with torch.no_grad():
    model.eval()
    for num, data in enumerate(test_loader):
        imgs, label = data
        imgs,labels = imgs.to(device), label.to(device)
        output = model(imgs).cpu()
        _, preds_tensor = torch.max(output,dim=1)
        results += preds_tensor.tolist()
print('finish')

ans then my making submission code

https://www.kaggle.com/c/cifar-10/submit

$\endgroup$
  • $\begingroup$ Hey there, this isn't a code inspection website :) Share the error with us and we might help! $\endgroup$ – Aditya Dec 10 '19 at 7:15
  • $\begingroup$ there is not error on the code. just after train my validation score is 90% and my submission score is 10%. pytorch has cuda and cpu() or detach(). so many options. so i just wonder I missed something. $\endgroup$ – slowmonk Dec 10 '19 at 9:00
  • $\begingroup$ github.com/SlowMonk/Pytorch/blob/master/… here is my code $\endgroup$ – slowmonk Dec 10 '19 at 9:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.