TypeError: float() argument must be a string or a number, not 'ViolenceModel'

Hi
can anyone help me how to solve this problem please :slight_smile:

model = ViolenceModel(modelUsed,pretrained)
    svclassifier = SVC(kernel='linear')


    trainParams = []
    for params in model.parameters():
        if params.requires_grad:
            trainParams += [params]
    model.train(True)
    if(torch.cuda.is_available()):
        model.cuda()

    lossFn = nn.CrossEntropyLoss()
    optimizerFn = torch.optim.RMSprop(trainParams, lr=lr)
    optimizerFn.zero_grad()
    optimScheduler = torch.optim.lr_scheduler.StepLR(optimizerFn, stepSize, decayRate)

    minAccuracy = 50
    train_loss=[]
    val_loss=[]
    train_acc=[]
    val_acc=[]
    bestmodel=None

    for epoch in range(numEpochs):
        optimScheduler.step()
        epochLoss = 0
        numCorrTrain = 0
        iterPerEpoch = 0
        print('Epoch = {}'.format(epoch + 1))
        writer.add_scalar('lr', optimizerFn.param_groups[0]['lr'], epoch+1)
        for i, (inputs, targets) in enumerate(trainLoader):
            iterPerEpoch += 1
            optimizerFn.zero_grad()
            if(torch.cuda.is_available()):
                inputVariable1 = Variable(inputs.permute(1, 0, 2, 3, 4).cpu())
                labelVariable = Variable(targets.cpu())
            else:
                inputVariable1=Variable(inputs.permute(1,0,2,3,4))
                labelVariable=Variable(targets)
            print(inputVariable1.shape)
            svclassifier.fit(model,labelVariable)
            outputLabel = svclassifier.predict(model)
            loss = lossFn(outputLabel, labelVariable)
            loss.backward()
            optimizerFn.step()
            #outputProb = torch.nn.Softmax(dim=1)(outputLabel)
            #_, predicted = torch.max(outputProb.data, 1)
            _, predicted = outputLabel
            if(torch.cuda.is_available()):
                numCorrTrain += (predicted == targets.cuda()).sum()
            else:
                numCorrTrain+=(predicted==targets).sum()
            epochLoss += loss.item()
        avgLoss = epochLoss/iterPerEpoch
        trainAccuracy = (float(numCorrTrain) * 100)/float(numTrainInstances)
        train_loss.append(avgLoss)
        train_acc.append(trainAccuracy)
        print('Training: Loss = {} | Accuracy = {}% '.format(avgLoss, trainAccuracy))
        writer.add_scalar('train/epochLoss', avgLoss, epoch+1)
        writer.add_scalar('train/accuracy', trainAccuracy, epoch+1)
        trainLogLoss.write('Training loss after {} epoch = {}\n'.format(epoch+1, avgLoss))
        trainLogAcc.write('Training accuracy after {} epoch = {}\n'.format(epoch+1, trainAccuracy))

model:

import torch.nn as nn
from torchvision import models
from ConvLSTMCell import *
from efficientnet_pytorch import EfficientNet
from sklearn.svm import SVC


class ViolenceModel(nn.Module):
    def __init__(self,modelused,pretrained):
        super(ViolenceModel, self).__init__()
        if modelused=='alexnet':
                self.mod=models.alexnet(pretrained=pretrained)
                self.convNet = nn.Sequential(*(list(self.mod.children()))[:-1])
                self.mem_size=256
                self.conv_lstm = ConvLSTMCell(256, self.mem_size)
        
        if pretrained:
            for param in self.convNet.parameters():
                param.requires_grad = False
    
    
    def forward(self, x):
        state = None
        seqLen = x.size(0) - 1
        for t in range(0, seqLen):
            x1 = x[t] - x[t+1]
            x1 = self.convNet(x1)
            state = self.conv_lstm(x1, state)
        
        x = self.maxpool(state[0])
        x= x.view(x.size(0), -1)
        return x

How did you create numTrainInstances?
If seems that its definition is not included in your code snippet.
Anyway, there seem to be just two calls to float, i.e. in this line:

trainAccuracy = (float(numCorrTrain) * 100)/float(numTrainInstances)

Could you add a print statement right before this line and check the types?

print(type(numCorrTrain))
print(type(numTrainInstances))
trainAccuracy = (...

thank you so much for reply, the error arise in this line

svclassifier.fit(model,labelVariable)
Traceback (most recent call last):

  File "<ipython-input-18-740ff909cca9>", line 1, in <module>
    runfile('C:/Users/Windows10/Downloads/Hala3/main-run-vr.py', wdir='C:/Users/Windows10/Downloads/Hala3')

  File "C:\Users\Windows10\Anaconda3\envs\New\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
    execfile(filename, namespace)

  File "C:\Users\Windows10\Anaconda3\envs\New\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/Windows10/Downloads/Hala3/main-run-vr.py", line 541, in <module>
    __main__()

  File "C:/Users/Windows10/Downloads/Hala3/main-run-vr.py", line 539, in __main__
    evalInterval, evalMode, numWorkers, outDir,modelUsed,pretrained,train_test_split,datasetDir,crossValidation,nFolds)

  File "C:/Users/Windows10/Downloads/Hala3/main-run-vr.py", line 414, in main_run
    model,accuracy=modelTrain(modelUsed,pretrained,trainDataset,trainLabels,validationDataset,validationLabels,numEpochs,evalInterval,evalMode,outDir,numWorkers,lr, stepSize, decayRate, trainBatchSize, seqLen,True)

  File "C:/Users/Windows10/Downloads/Hala3/main-run-vr.py", line 294, in modelTrain
    svclassifier.fit(model,labelVariable)

  File "C:\Users\Windows10\Anaconda3\envs\New\lib\site-packages\sklearn\svm\base.py", line 146, in fit
    accept_large_sparse=False)

  File "C:\Users\Windows10\Anaconda3\envs\New\lib\site-packages\sklearn\utils\validation.py", line 719, in check_X_y
    estimator=estimator)

  File "C:\Users\Windows10\Anaconda3\envs\New\lib\site-packages\sklearn\utils\validation.py", line 496, in check_array
    array = np.asarray(array, dtype=dtype, order=order)

  File "C:\Users\Windows10\Anaconda3\envs\New\lib\site-packages\numpy\core\numeric.py", line 538, in asarray
    return array(a, dtype, copy=False, order=order)

TypeError: float() argument must be a string or a number, not 'ViolenceModel'

code snipped of create numTrainInstance:

    vidSeqTrain = makeDataset(trainDataset, trainLabels, spatial_transform=spatial_transform,
                                seqLen=seqLen)
    # torch iterator to give data in batches of specified size
    trainLoader = torch.utils.data.DataLoader(vidSeqTrain, batch_size=trainBatchSize,
                            shuffle=True, num_workers=numWorkers, pin_memory=True, drop_last=True)
mean, std=std)])
    
    vidSeqValid = makeDataset(validationDataset, validationLabels, seqLen=seqLen,
    spatial_transform=test_spatial_transform)

    validationLoader = torch.utils.data.DataLoader(vidSeqValid, batch_size=1,
                            shuffle=False, num_workers=int(numWorkers/2), pin_memory=True)

    numTrainInstances = vidSeqTrain.__len__()
    numValidationInstances = vidSeqValid.__len__()

<class ‘int’>

Thanks for the information.
You cannot call .fit on PyTorch models, which is a method of sklearn models.
The fit method expects data and target as numpy arrays to fit the model.

then, what should i do if i want to use features extracted by CNN model in SVM classifier?

You could store the outputs of your CNN with the corresponding targets and pass them to the SVM classifier afterwards, as you won’t be able to train them end-to-end.

how can i convert variable that require grad. to numpy array? i use var.detach().numpy() but also get the error:

RuntimeError: Can't call numpy() on Variable that requires grad. Use var.detach().numpy() instead.

This should work.
Could you post the code throwing this error?

for epoch in range(numEpochs):
        optimScheduler.step()
        epochLoss = 0
        numCorrTrain = 0
        iterPerEpoch = 0
        model.train(True)
        print('Epoch = {}'.format(epoch + 1))
        writer.add_scalar('lr', optimizerFn.param_groups[0]['lr'], epoch+1)
        for i, (inputs, targets) in enumerate(trainLoader):
            iterPerEpoch += 1
            optimizerFn.zero_grad()
            if(torch.cuda.is_available()):
                inputVariable1 = Variable(inputs.permute(1, 0, 2, 3, 4).cuda()).cpu()                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
                labelVariable = Variable(targets.cuda()).cpu()
            
            
            else:
                inputVariable1=Variable(inputs.permute(1,0,2,3,4))
                labelVariable=Variable(targets)
            #outputLabel = model(inputVariable1)
            features = model(inputVariable1)
            labelVariable=(labelVariable.data).detach().cpu().numpy()
            svclassifier.fit(features, labelVariable)
            outputLabel= svclassifier.predict(inputVariable1)
Traceback (most recent call last):

  File "<ipython-input-4-740ff909cca9>", line 1, in <module>
    runfile('C:/Users/Windows10/Downloads/Hala3/main-run-vr.py', wdir='C:/Users/Windows10/Downloads/Hala3')

  File "C:\Users\Windows10\Anaconda3\envs\New\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
    execfile(filename, namespace)

  File "C:\Users\Windows10\Anaconda3\envs\New\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/Windows10/Downloads/Hala3/main-run-vr.py", line 545, in <module>
    __main__()

  File "C:/Users/Windows10/Downloads/Hala3/main-run-vr.py", line 543, in __main__
    evalInterval, evalMode, numWorkers, outDir,modelUsed,pretrained,train_test_split,datasetDir,crossValidation,nFolds)

  File "C:/Users/Windows10/Downloads/Hala3/main-run-vr.py", line 418, in main_run
    model,accuracy=modelTrain(modelUsed,pretrained,trainDataset,trainLabels,validationDataset,validationLabels,numEpochs,evalInterval,evalMode,outDir,numWorkers,lr, stepSize, decayRate, trainBatchSize, seqLen,True)

  File "C:/Users/Windows10/Downloads/Hala3/main-run-vr.py", line 298, in modelTrain
    svclassifier.fit(features, labelVariable)

  File "C:\Users\Windows10\Anaconda3\envs\New\lib\site-packages\sklearn\svm\base.py", line 146, in fit
    accept_large_sparse=False)

  File "C:\Users\Windows10\Anaconda3\envs\New\lib\site-packages\sklearn\utils\validation.py", line 719, in check_X_y
    estimator=estimator)

  File "C:\Users\Windows10\Anaconda3\envs\New\lib\site-packages\sklearn\utils\validation.py", line 496, in check_array
    array = np.asarray(array, dtype=dtype, order=order)

  File "C:\Users\Windows10\Anaconda3\envs\New\lib\site-packages\numpy\core\numeric.py", line 538, in asarray
    return array(a, dtype, copy=False, order=order)

  File "C:\Users\Windows10\Anaconda3\envs\New\lib\site-packages\torch\tensor.py", line 460, in __array__
    return self.numpy().astype(dtype, copy=False)

RuntimeError: Can't call numpy() on Variable that requires grad. Use var.detach().numpy() instead.

features seems not to be detached, so could you call features = features.detach().cpu().numpy() before passing it to the svm?

thank you very much it’s works :slight_smile:
i convert (label variable1 and outputlabel) back from numpy to tensor in order to passing them through loss function but get error:

torch.Size([20, 16, 3, 224, 224])
Traceback (most recent call last):

  File "<ipython-input-15-740ff909cca9>", line 1, in <module>
    runfile('C:/Users/Windows10/Downloads/Hala3/main-run-vr.py', wdir='C:/Users/Windows10/Downloads/Hala3')

  File "C:\Users\Windows10\Anaconda3\envs\New\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
    execfile(filename, namespace)

  File "C:\Users\Windows10\Anaconda3\envs\New\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/Windows10/Downloads/Hala3/main-run-vr.py", line 549, in <module>
    __main__()

  File "C:/Users/Windows10/Downloads/Hala3/main-run-vr.py", line 547, in __main__
    evalInterval, evalMode, numWorkers, outDir,modelUsed,pretrained,train_test_split,datasetDir,crossValidation,nFolds)

  File "C:/Users/Windows10/Downloads/Hala3/main-run-vr.py", line 422, in main_run
    model,accuracy=modelTrain(modelUsed,pretrained,trainDataset,trainLabels,validationDataset,validationLabels,numEpochs,evalInterval,evalMode,outDir,numWorkers,lr, stepSize, decayRate, trainBatchSize, seqLen,True)

  File "C:/Users/Windows10/Downloads/Hala3/main-run-vr.py", line 304, in modelTrain
    loss = lossFn(outputLabel, labelVariable)

  File "C:\Users\Windows10\Anaconda3\envs\New\lib\site-packages\torch\nn\modules\module.py", line 493, in __call__
    result = self.forward(*input, **kwargs)

  File "C:\Users\Windows10\Anaconda3\envs\New\lib\site-packages\torch\nn\modules\loss.py", line 942, in forward
    ignore_index=self.ignore_index, reduction=self.reduction)

  File "C:\Users\Windows10\Anaconda3\envs\New\lib\site-packages\torch\nn\functional.py", line 2056, in cross_entropy
    return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)

  File "C:\Users\Windows10\Anaconda3\envs\New\lib\site-packages\torch\nn\functional.py", line 1350, in log_softmax
    ret = input.log_softmax(dim)

IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)
    for epoch in range(numEpochs):
        optimScheduler.step()
        epochLoss = 0
        numCorrTrain = 0
        iterPerEpoch = 0
        model.train(True)
        print('Epoch = {}'.format(epoch + 1))
        writer.add_scalar('lr', optimizerFn.param_groups[0]['lr'], epoch+1)
        for i, (inputs, targets) in enumerate(trainLoader):
            iterPerEpoch += 1
            optimizerFn.zero_grad()
            if(torch.cuda.is_available()):
                inputVariable1 = Variable(inputs.permute(1, 0, 2, 3, 4).cuda()).cpu()                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
                labelVariable = Variable(targets.cuda()).cpu()
            
            
            else:
                inputVariable1=Variable(inputs.permute(1,0,2,3,4))
                labelVariable=Variable(targets)
            #outputLabel = model(inputVariable1)
            features = model(inputVariable1)
            features = features.detach().cpu().numpy()
            labelVariable=(labelVariable.data).detach().cpu().numpy()
            svclassifier.fit(features, labelVariable)
            print(inputVariable1.shape)
            outputLabel= svclassifier.predict(features)
            labelVariable = torch.from_numpy(labelVariable)
            outputLabel = torch.from_numpy(outputLabel)
            loss = lossFn(outputLabel, labelVariable)
            loss.backward()
            optimizerFn.step()

How can i fix this please, and sorry for my lot questions :frowning:

You cannot train these classifiers end-to-end.
Once you leave PyTorch and use numpy tensors, Autograd cannot track the operations anymore and you would need to implement all backward methods yourself.

You could train the CNN in Pytorch first, store all outputs of it after training, and train your SVM on these outputs (completely detached from PyTorch).
Alternatively, you could use the hinge loss, which (if I’m not mistaken) some researches refer to in papers if they say “we trained on SVM on top of the CNN feature classifier”.

even if i use CNN for feature extraction only (pre-trained model)?

That would be similar to my suggestion and would be possible:

Just skip the training part :wink: