akankshakshurai/cuda-error-resolution-analysis huggingface dataset:
What is the efficient way to reduce the dimension of 3d convolution filters?

username: Pritesh_Gohil
cooked: <p>I have 2 questions with the dimension of the tensor.</p>
<ol>
<li>
<p>How do I convert my model output torch.Size([5, 1024, 7, 2, 4]) to torch.Size([5,1024,7,1,1]) ? I thought of slicing, but is that good idea? or there is another good way to do it?</p>
</li>
<li>
<p>I have two torch.Size([5,1024,7,1,1]) output from the network, I would like to concatenate these two outputs and feed to two-layer Fully Connected layer. (layer 1 output :1024, layer 2 output: 1). What would be the best way to do it?</p>
</li>
</ol>
<p>Thank you.</p>

username: ptrblck
cooked: <ol>
<li>
<p>It looks like you are working with some volumetric data based on the shape. You could apply some pooling, or another conv layer over the last two dimensions. I don’t think there is a “best” way and it will most likely depend on your model and use case.</p>
</li>
<li>
<p>Usually you would flatten the tensors to have the shape <code>[batch_size, in_features]</code>. However, <code>nn.Linear</code> layers also take arbitrary dimensions for inputs such as <code>[batch_size, *, in_features]</code> and apply the linear transformation on each sample in the asterisk dimensions.<br>
Would you like to apply the linear layers separately on each of the inputs or would you like to concatenate the inputs in the feature dimension?</p>
</li>
</ol>

username: Pritesh_Gohil
cooked: <ol>
<li>Since I don’t want to modify the network, I used torch.max to get the shape of [5, 1024, 7] from [5, 1024, 7, 2, 4].</li>
</ol>
<pre><code class="lang-auto">out = torch.max(x, dim=3)[0]
out = torch.max(out, dim=3)[0]
</code></pre>
<p>I hope it will not lose any important feature from the final layer.</p>
<ol start="2">
<li>I would like to concatenate the input. Yes flattening helped me to achieve this.</li>
</ol>
<pre><code class="lang-auto">    def __init__(self, spatial_model, temporal_model):
        super(FusionNet, self).__init__()
        self.spatial_model = spatial_model
        self.temporal_model = temporal_model
        self.fc = nn.Sequential(nn.Linear(1024*7*2, 1024, bias=True), nn.ReLU(inplace=True), nn.Dropout(p = 0.5),
                                nn.Linear(1024, 1))

    def forward(self, spatial_input, temporal_input):
        spatial_output = self.spatial_model(spatial_input)
        temporal_output = self.temporal_model(temporal_input)
        fused = torch.cat((spatial_output, temporal_output), dim = 1)
        out = fused.view(fused.size(0), -1)
        out = self.fc(out)
        return out
</code></pre>
<p>Thanks again</p>
Torchvision functional transforms with Compose

username: gokul_uf
cooked: <p>Hi Everyone, I am working with the ImageNet dataset. As a part of the experiments, I want to shift the input images right by 10 pixels (among other affine transforms).</p>
<p>I can’t use <code>torchvision.transforms.RandomAffine</code> because I want deterministic transformations. So I’m left with <code>torchvision.transforms.functional.affine</code>.</p>
<p>Any idea how I could plug it in as a part of a pipeline made with <code>transforms.Compose</code>?</p>
<p>From what I see in the codebase, it seems easy enough by wrapping <code>affine</code> inside a class and call it inside <code>__call__</code>. Is this the right way to go about this or are there better ways to do this?</p>

username: ptrblck
cooked: <p>Your suggestion should work. Alternatively you could use <code>transforms.Lambda</code> to warp your functional transformation.</p>
Getting error while ploting ROC for multiclass classification

username: mchowdhu
cooked: <p>I am trying to plot ROC Curve for multiclass classification.I followed <a href="https://scikit-learn.org/stable/auto_examples/model_selection/plot_roc.html" rel="nofollow noopener">https://scikit-learn.org/stable/auto_examples/model_selection/plot_roc.html</a>.<br>
I used the below code to calculate y_test,and y_score</p>
<pre><code class="lang-auto">def test_epoch(net,test_loader):
 y_test =[]
 y_score =[]
 with torch.no_grad():
  for batch in test_loader:
    images, labels = batch['image'], batch['grade']
    images =Variable(images)
    labels= Variable(labels)
    outputs = net(images)
    _, predicted = torch.max(outputs.data, 1)
    c = (predicted == labels).squeeze().numpy()
    y_score.append(outputs.numpy())
    y_test.append(labels.numpy())
    return y_test,y_score    `  

</code></pre>
<p>I seen that my y_test is array like below <code>[array([0, 4, 0, 2, 1, 0, 0, 3, 0, 1, 1, 0, 3, 2, 0, 2, 2, 2, 1, 1, 1, 0,0, 0, 0, 3, 0, 0, 0, 4, 2, 1, 2, 0, 2, 1, 0, 4, 0, 0, 0, 1, 2, 2 0, 1, 2, 2, 0, 2, 0, 2, 2, 3, 2, 3, 3, 1, 1, 1, 0, 2, 0, 0, 2, 1,3, 0, 2, 0, 3, 2, 1, 0, 2, 2, 1, 0, 0, 0, 0, 2, 1, 2, 0, 3, 0, 1, 3, 0, 3, 2, 0, 3, 1, 0, 1, 2, 2, 0])]</code><br>
And y_score is like<code>[array([[ 0.30480504, -0.12213976, 0.09632117, -0.16465648, -0.44081157],[ 0.21797988, -0.09650452, 0.07616544, -0.12001953, -0.34972644],[ 0.3230184 , -0.13098559, 0.10277118, -0.17656785, -0.45888817],[ 0.38143447, -0.15880316, 0.12123139, -0.21719441, -0.5281661 ],[ 0.3427343 , -0.13945231, 0.11076729, -0.19657779, -0.4913683 ],</code></p>
<p>Whenever I called the function for plotting ROC</p>
<pre><code>fpr = dict()
tpr = dict()
roc_auc = dict()
for i in range(N_classes):
    fpr[i], tpr[i], _ = roc_curve(truth_val[:, i],preds_val[:, i])
    roc_auc[i] = auc(fpr[i], tpr[i])
# Compute micro-average ROC curve and ROC area
fpr["micro"], tpr["micro"], _ = roc_curve(truth_val.ravel(), preds_val.ravel())
roc_auc["micro"] = auc(fpr["micro"], tpr["micro"]
</code></pre>
<p>I am getting this error massages,<code>Traceback (most recent call last): File "/home/Downloads/demo 3.py", line 435, in &lt;module&gt; plot_roc(y_test, y_score, 5) fpr[i], tpr[i], _ = roc_curve(truth_val[:, i],preds_val[:, i]) TypeError: list indices must be integers or slices, not tuple</code></p>
<p>I could not understand how I will solve this problem.<br>
I am highly appreciate any kind of help regarding this issue.</p>

username: ilyes
cooked: <p>Based on the error I think <code>truth_val[:, i] or preds_val[:, i]</code> is a python list ?</p>
How do I design models for multiple labels?

username: swp
cooked: <p>train data’ shape is (3, 224, 224)</p>
<p>label is total 5</p>
<p>label 1 = (0~5)<br>
label 2 = (0~7)<br>
label 3 = (0~3)<br>
label 4 = (0~2)<br>
label 5 = (0~8)</p>
<p>so i mapped</p>
<p>example))<br>
image &lt;-&gt; [1, 7, 3, 1, 4]</p>
<p>and</p>
<p>model = models.resnet50(pretrained=False)<br>
in_features = model.fc.in_features<br>
model.fc = nn.Linear(in_features=in_features, out_features=5)<br>
model.to(device)<br>
print(model)</p>
<p>Is it right to design this?</p>
<p>I have no idea how to set the loss and model if there are multiple such labels using Resnet50.</p>

username: ilyes
cooked: <p>One idea is to work with hot vectors instead. So basically, your last fc layer will be connected to 5 different outputs such that :</p>
<ul>
<li>output 1 is a 6 element  vector that represents the first label. If your label is 3 for example then you vector would be [0, 0, 0, 1, 0, 0]</li>
<li>output 2 is 8 element vector.<br>
.<br>
. and so on</li>
</ul>
<p>Once you have these representations, you apply <code>CrossEntropyLoss</code> on each one of your outputs. And your final loss is the sum of all these 5 losses.<br>
You can also weight your loss labels if you think one label is more important than the other.</p>
Loading weights of specific modules in the model

username: kl_divergence
cooked: <p>I’ve two models (based on faster_rcnn from torchvision). I want to replace the weights of roi_head in model 2 with that of model 1’s.</p>
<p>My dict keys from model 1 has these keys:</p>
<pre><code class="lang-auto"> 'roi_heads.box_head.fc6.weight'
 'roi_heads.box_head.fc6.bias'
 'roi_heads.box_head.fc7.weight'
 'roi_heads.box_head.fc7.bias'
 'roi_heads.box_predictor.cls_score.weight'
 'roi_heads.box_predictor.cls_score.bias'
 'roi_heads.box_predictor.bbox_pred.weight' 
 'roi_heads.box_predictor.bbox_pred.bias'
</code></pre>
<p>Here’s my model 1</p>
<pre><code class="lang-auto">ckpt = torch.load('saved_models/model1.pth')
model = get_model(15)
model.load_state_dict(ckpt['model'])
</code></pre>
<p>Model 2 is identical but roi_heads is different.How can I load weights of roi_heads (from model 1) to my model 2’s roi_head ?</p>

username: kl_divergence
cooked: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> Can you please provide some snippet?</p>

username: ptrblck
cooked: <p>How would you like to replace the <code>roi_head</code> parameters, if both <code>roi_heads</code> are different?<br>
Are they just using different names or also different shapes of the parameters?</p>

username: kl_divergence
cooked: <p>Both roi_head have same key names, it’s just the weights that are different. Even the shapes of in between layers is same. I just want to load the weights of roi_head with that of another model.</p>
<p>I want to just replace the weights of these :<br>
<code>'roi_heads.box_head.fc6.weight' 'roi_heads.box_head.fc6.bias'</code> with that of another identical roi_head which will be used for taking predictions.</p>

username: ptrblck
cooked: <p>In that case something like this should work:</p>
<pre><code class="lang-python">modelA.roi_head.load_state_dict(modelB.roi_head.state_dict())
</code></pre>
<p>Alternatively, remove the <code>roi_heads</code> strings from the checkpoint keys and try:</p>
<pre><code class="lang-python">checkpoint = chpt['model']
# Remove 'roi_heads'
checkpoint_cleaned = ...
model.roi_head.load_state_dict(checkpoint_cleaned)
</code></pre>