Cannot identify inplace operation

Following many messages, I have an error related to inplace operation. I have narrowed it down to a few lines of codes but I still can’t find it.

def getSingleImage(pred):
    urineVal = 0.3137255
    wallVal = 0.627451
    tumorVal = 0.94117647
    x = predToSegmentation(pred)
    for i in range(x.size(0)):
        x[i, 0] = x[i, 0] * 0
        x[i, 1] = x[i, 1] * urineVal
        x[i, 2] = x[i, 2] * wallVal
        x[i, 3] = x[i, 3] * tumorVal

    x = x.sum(dim=1, keepdim=True)
    return x

def predToSegmentation(pred):
    x = pred
    for i in range(x.size(0)):
        top2 = x[i].topk(2, dim=0)[0]
        x[i] = x[i] - top2[1]
        x[i] = x[i] / (top2[0] - top2[1])
        x[i] = x[i].clamp(0, 1)
    return x

Any idea ?

Thanks

Given that x is a Variable. Any operation that does x[some_index] = some_value with anything as some_index and some_value IS an inplace operation on x.

1 Like

Oh wow thanks. That was so obviously wrong from the start I didn’t even look at that. I guess someone else looking at your code sometimes sheds light on stupid mistakes.