# Help debugging DenseNet model on CIFAR-10

**URL:** <https://discuss.pytorch.org/t/help-debugging-densenet-model-on-cifar-10/412>\
**Category:** vision\
**Created:** [February 9, 2017, 3:42pm UTC](https://discuss.pytorch.org/t/help-debugging-densenet-model-on-cifar-10/412 "2017-02-09T15:42:33Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![bamos](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/bamos/32/15743_2.png) [@bamos](https://discuss.pytorch.org/u/bamos)\
**Post date:** [February 9, 2017, 3:42pm UTC](https://discuss.pytorch.org/t/help-debugging-densenet-model-on-cifar-10/412/1 "2017-02-09T15:42:33Z")

</div>

Hi PyTorch community,

I strongly dislike asking for help on things like a model not converging, but I have implemented a DenseNet model in PyTorch and do not know how to further debug why it’s not working. It’s very likely that I’ve overlooked something simple, but I’m starting to think there might be something deeper going on with PyTorch. I’ve been checking gradients and my training procedure and everything I can think to check looks surprisingly good. Can somebody that’s familiar with PyTorch or training DenseNets take a quick look at my project so far and let me know if anything looks wrong or if there’s anything else I should check? I have posted it to GitHub at [https://github.com/bamos/densenet.pytorch](https://github.com/bamos/densenet.pytorch) along with some more details about what I’ve checked and instructions on running the code.

-Brandon.

---

<div class="post-metadata">

**Author:** ![apaszke](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/apaszke/32/21_2.png) [@apaszke](https://discuss.pytorch.org/u/apaszke)\
**Post date:** [February 9, 2017, 4:07pm UTC](https://discuss.pytorch.org/t/help-debugging-densenet-model-on-cifar-10/412/2 "2017-02-09T16:07:14Z")

</div>

One thing is that this:

```python
for param_group in optimizer.state_dict()['param_groups']:

```

should be replaced with that:

```python
for param_group in optimizer.param_groups:

```

I know that the first version appears in the ImageNet example, but it no longer works as expected. I can’t see anything wront at a glance, but I’ll try to look more carefully into it sometime.

---

<div class="post-metadata">

**Author:** ![bamos](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/bamos/32/15743_2.png) [@bamos](https://discuss.pytorch.org/u/bamos)\
**Post date:** [February 9, 2017, 4:25pm UTC](https://discuss.pytorch.org/t/help-debugging-densenet-model-on-cifar-10/412/3 "2017-02-09T16:25:49Z")

</div>

Thanks for quickly looking! I fixed that. (It doesn’t sound like you expected this to help the convergence – the model’s still not converging as before.)

---

<div class="post-metadata">

**Author:** ![bamos](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/bamos/32/15743_2.png) [@bamos](https://discuss.pytorch.org/u/bamos)\
**Post date:** [February 9, 2017, 4:41pm UTC](https://discuss.pytorch.org/t/help-debugging-densenet-model-on-cifar-10/412/5 "2017-02-09T16:41:19Z")

</div>

Thanks!

My training code does successfully train a known model (VGGnet) on CIFAR-10, so my logic is also that there’s something wrong with my model. However I have also exactly compared my model’s outputs and gradients to the official model.

I thought about trying to remove the passthrough connections from my DenseNet implementation to further debug this. However I haven’t tried this yet because I’ve never seen reports of such an architecture (even correctly implemented) converging on CIFAR-10. And if my implementation of this did converge, then it would indicate that there’s a problem of layers that concatenate the input and output. So to directly check if there’s a problem with this kind of operation, I used numdifftools to numerically check the gradients of a single PyTorch layer that concatenated the input to a fully-connected operation.

As another idea of breaking the DenseNet into a known architecture, I could start with a ResNet architecture that’s known to converge and then start adding DenseNet features. However these intermediate architecture are not known to converge, so if it doesn’t work, then I won’t know if it’s because of a code bug or something more fundamental.

---

<div class="post-metadata">

**Author:** ![bamos](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/bamos/32/15743_2.png) [@bamos](https://discuss.pytorch.org/u/bamos)\
**Post date:** [February 9, 2017, 7:44pm UTC](https://discuss.pytorch.org/t/help-debugging-densenet-model-on-cifar-10/412/6 "2017-02-09T19:44:07Z")

</div>

Adam’s been helping me debug this over Slack today and we’ve solved it! We found a new PyTorch bug with cudnn that comes up with DenseNet-style layers. After fixing this, my DenseNet model converges much better than before, and I’ll update my repo with the current results shortly. My understanding is that Adam will push a short patch to PyTorch master soon.

Thanks again for the help, Adam.

-Brandon.

---

<div class="post-metadata">

**Author:** ![apaszke](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/apaszke/32/21_2.png) [@apaszke](https://discuss.pytorch.org/u/apaszke)\
**Post date:** [February 9, 2017, 11:26pm UTC](https://discuss.pytorch.org/t/help-debugging-densenet-model-on-cifar-10/412/7 "2017-02-09T23:26:46Z")

</div>

The fix is already in master.

---

<div class="post-metadata">

**Author:** ![ClementPinard](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/clementpinard/32/1419_2.png) [@ClementPinard](https://discuss.pytorch.org/u/ClementPinard)\
**Post date:** [February 10, 2017, 10:24am UTC](https://discuss.pytorch.org/t/help-debugging-densenet-model-on-cifar-10/412/8 "2017-02-10T10:24:52Z")

</div>

What was the problem ? Been having some convergence issues with my network which has some layers concatenation just as yours. My problems are not necessarily related to yours, but I’m glad you could figure it out with adam !

---

<div class="post-metadata">

**Author:** ![apaszke](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/apaszke/32/21_2.png) [@apaszke](https://discuss.pytorch.org/u/apaszke)\
**Post date:** [February 10, 2017, 2:16pm UTC](https://discuss.pytorch.org/t/help-debugging-densenet-model-on-cifar-10/412/9 "2017-02-10T14:16:26Z")

</div>

The problem arised form concatenating outputs of convolution along second dimension. This lead to calling conv’s backward with non-contiguous gradients, and we were overly smart about reusing cuDNN descriptors, so the backend thought that it’s contiguous. The fix is to either disable cuDNN or rebuild pytorch.

I’m sorry if you that also affected your network 😕

---

<div class="post-metadata">

**Author:** ![fmassa](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/fmassa/32/58_2.png) [@fmassa](https://discuss.pytorch.org/u/fmassa)\
**Post date:** [March 3, 2017, 11:51am UTC](https://discuss.pytorch.org/t/help-debugging-densenet-model-on-cifar-10/412/11 "2017-03-03T11:51:21Z")

</div>

Some tensor operations, like transpose, do not perform memory copy and only change the strides of the tensor (and pixel shuffle is one of this cases)  
`tensor.contiguous()` will make sure that the data is contiguous in memory, and if not will perform a copy so that it becomes contiguous. This is necessary for some functions to work (for example, `.view` only accepts contiguous tensors).
