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

username: Omar2017
cooked: <p>Hi<br>
can anyone help me how to solve this problem please <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
<pre><code class="lang-auto">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))

</code></pre>
<p>model:</p>
<pre><code class="lang-auto">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

</code></pre>

username: ptrblck
cooked: <p>How did you create <code>numTrainInstances</code>?<br>
If seems that its definition is not included in your code snippet.<br>
Anyway, there seem to be just two calls to <code>float</code>, i.e. in this line:</p>
<pre><code class="lang-python">trainAccuracy = (float(numCorrTrain) * 100)/float(numTrainInstances)
</code></pre>
<p>Could you add a print statement right before this line and check the types?</p>
<pre><code class="lang-python">print(type(numCorrTrain))
print(type(numTrainInstances))
trainAccuracy = (...
</code></pre>

username: Omar2017
cooked: <p>thank you so much for reply, the error arise in this line</p>
<pre><code class="lang-auto">svclassifier.fit(model,labelVariable)
</code></pre>
<pre><code class="lang-auto">Traceback (most recent call last):

  File "&lt;ipython-input-18-740ff909cca9&gt;", line 1, in &lt;module&gt;
    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 &lt;module&gt;
    __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></pre>

username: Omar2017
cooked: <p>code snipped of create numTrainInstance:</p>
<pre><code class="lang-auto">    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__()

</code></pre>
<p>&lt;class ‘int’&gt;</p>

username: ptrblck
cooked: <p>Thanks for the information.<br>
You cannot call <code>.fit</code> on PyTorch models, which is a method of sklearn models.<br>
The <code>fit</code> method expects data and target as numpy arrays to fit the model.</p>

username: Omar2017
cooked: <p>then, what should i do if i want to use features extracted by CNN model in SVM classifier?</p>

username: ptrblck
cooked: <p>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.</p>

username: Omar2017
cooked: <p>how can i convert variable that require grad. to numpy array? i use var.detach().numpy() but also get the error:</p>
<pre><code class="lang-auto">RuntimeError: Can't call numpy() on Variable that requires grad. Use var.detach().numpy() instead.
</code></pre>

username: ptrblck
cooked: <p>This should work.<br>
Could you post the code throwing this error?</p>

username: Omar2017
cooked: <pre><code class="lang-auto">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)
</code></pre>
<pre><code class="lang-auto">Traceback (most recent call last):

  File "&lt;ipython-input-4-740ff909cca9&gt;", line 1, in &lt;module&gt;
    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 &lt;module&gt;
    __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.
</code></pre>

username: ptrblck
cooked: <p><code>features</code> seems not to be detached, so could you call <code>features = features.detach().cpu().numpy()</code> before passing it to the svm?</p>

username: Omar2017
cooked: <p>thank you very much it’s works <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"><br>
i convert (label variable1 and outputlabel) back from numpy to tensor in order to passing them through loss function but get error:</p>
<pre><code class="lang-auto">torch.Size([20, 16, 3, 224, 224])
Traceback (most recent call last):

  File "&lt;ipython-input-15-740ff909cca9&gt;", line 1, in &lt;module&gt;
    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 &lt;module&gt;
    __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)
</code></pre>
<pre><code class="lang-auto">    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()
</code></pre>
<p>How can i fix this please, and sorry for my lot questions <img src="https://discuss.pytorch.org/images/emoji/apple/frowning.png?v=9" title=":frowning:" class="emoji" alt=":frowning:"></p>

username: ptrblck
cooked: <p>You cannot train these classifiers end-to-end.<br>
Once you leave PyTorch and use numpy tensors, Autograd cannot track the operations anymore and you would need to implement all backward methods yourself.</p>
<p>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).<br>
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”.</p>

username: Omar2017
cooked: <p>even if i use CNN for feature extraction only (pre-trained model)?</p>

username: ptrblck
cooked: <p>That would be similar to my suggestion and would be possible:</p>
<aside class="quote no-group" data-username="ptrblck" data-post="13" data-topic="49827">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>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).</p>
</blockquote>
</aside>
<p>Just skip the training part <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=12" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"></p>