0

I have a problem when I want to loop my DataFrame created before so I can pass it into my classifier, but I can't neither loop my DataFrame nor pass a filename to the classifier. What can I do?

dataset=pd.DataFrame({
                      'filename':train,
                      'category':categories
                    })
images=dataset.iloc[:,0]
labels=dataset.iloc[:,-1]

from torch import nn,optim
class Classifier(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1=nn.Linear(49152,256)
        self.fc2=nn.Linear(256,128)
        self.fc3=nn.Linear(128,64)
        self.fc4=nn.Linear(64,2)
        # add the dropout
        self.dropout=nn.Dropout(p=0.2)
    def forward(self,x):
        #flatten the input
        x=x.view(x.shape[0],-1)
        x=self.dropout(F.relu(self.fc1(x)))
        x=self.dropout(F.relu(self.fc2(x)))
        x=self.dropout(F.relu(self.fc3(x)))
        x=F.log_softmax(self.fc4(x),dim=1)
        return x
model=Classifier()
criterion=nn.NLLLoss()
optimizer=optim.Adam(model.parameters(),lr=0.005)
epochs=20
steps=0
for epoch in range(epochs):
    running_loss=0
    for filename,label in dataset:
        image=cv2.imread(filename)
        image=cv2.resize(image,dsize=(128,128),interpolation=cv2.INTER_CUBIC)
        output=model.forward(image)
        loss=criterion(output,label)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        running_loss+=loss.item()
    print(running_loss)

1 Answer 1

0

Maybe you can try wrapping up your dataframe in PyTorch's dataloader. This will help you generate batches of train/validation dataset. This answer by Allen will give you some direction. Once you have a dataloader in place, you can easily iterate over it during training or validation.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.