Anurag1734/cuda-error-resolution-analysis
07
1[2 {3 "post_stream": {4 "posts": [5 {6 "id": 275630,7 "name": "Nikola Vetnic",8 "username": "NikolaVetnic",9 "avatar_template": "/user_avatar/discuss.pytorch.org/nikolavetnic/{size}/36807_2.png",10 "created_at": "2021-04-07T06:40:01.297Z",11 "cooked": "<p>Hello there,</p>\n<p>I have problems figuring out how to do the following: I have a pre-trained ResNet model with custom final layers at the end - let’s call these parts A (all the architecture up to the final layers) and B (final layers themselves). Now, I have copied that model and cut off part B so that A now ends with convolution layer’s output, I do something with that model and then, at a later point, I would like to take B part from the first model (the final layers that is), copy them and re-attach them to A part of the second model.</p>\n<p>How would one go about doing this? I’d appreciate any ideas, thanks!</p>",12 "post_number": 1,13 "post_type": 1,14 "posts_count": 6,15 "updated_at": "2021-04-07T06:42:57.235Z",16 "reply_count": 0,17 "reply_to_post_number": null,18 "quote_count": 0,19 "incoming_link_count": 1013,20 "reads": 15,21 "readers_count": 14,22 "score": 5053.0,23 "yours": false,24 "topic_id": 117325,25 "topic_slug": "connecting-another-models-final-layers-with-a-pre-trained-model",26 "display_username": "Nikola Vetnic",27 "primary_group_name": null,28 "flair_name": null,29 "flair_url": null,30 "flair_bg_color": null,31 "flair_color": null,32 "flair_group_id": null,33 "badges_granted": [],34 "version": 1,35 "can_edit": false,36 "can_delete": false,37 "can_recover": false,38 "can_see_hidden_post": false,39 "can_wiki": false,40 "read": true,41 "user_title": null,42 "bookmarked": false,43 "actions_summary": [],44 "moderator": false,45 "admin": false,46 "staff": false,47 "user_id": 43991,48 "hidden": false,49 "trust_level": 0,50 "deleted_at": null,51 "user_deleted": false,52 "edit_reason": null,53 "can_view_edit_history": true,54 "wiki": false,55 "post_url": "/t/connecting-another-models-final-layers-with-a-pre-trained-model/117325/1",56 "can_accept_answer": false,57 "can_unaccept_answer": false,58 "accepted_answer": false,59 "topic_accepted_answer": true,60 "can_vote": false61 },62 {63 "id": 275642,64 "name": "timothy",65 "username": "tlim",66 "avatar_template": "/user_avatar/discuss.pytorch.org/tlim/{size}/32364_2.png",67 "created_at": "2021-04-07T07:47:01.373Z",68 "cooked": "<p>My suggestion is to <code>print(model)</code> then you can see the layers involved.</p>\n<p>For example, ResNet final layer is under <code>model.fc</code>. You can then do this to suit it to your own final layers:</p>\n<pre><code class=\"lang-auto\">model = models.resnet18(pretrained=True)\nnum_features = model.fc.in_features #the number of nodes in the last layer for the ResNet model\nnum_classes = 20 #whatever number of classes u have.\nmodel.fc = nn.Linear(num_features, num_classes) #connect ur own layer into it.\n</code></pre>\n<p>There is more flexibility, you just have to determine from which layer u want to make the changes. This is an example of just changing the final linear layer to connect to the number of outputs that you desired.</p>",69 "post_number": 2,70 "post_type": 1,71 "posts_count": 6,72 "updated_at": "2021-04-07T07:47:57.648Z",73 "reply_count": 0,74 "reply_to_post_number": null,75 "quote_count": 0,76 "incoming_link_count": 11,77 "reads": 15,78 "readers_count": 14,79 "score": 58.0,80 "yours": false,81 "topic_id": 117325,82 "topic_slug": "connecting-another-models-final-layers-with-a-pre-trained-model",83 "display_username": "timothy",84 "primary_group_name": null,85 "flair_name": null,86 "flair_url": null,87 "flair_bg_color": null,88 "flair_color": null,89 "flair_group_id": null,90 "badges_granted": [],91 "version": 1,92 "can_edit": false,93 "can_delete": false,94 "can_recover": false,95 "can_see_hidden_post": false,96 "can_wiki": false,97 "read": true,98 "user_title": null,99 "bookmarked": false,100 "actions_summary": [],101 "moderator": false,102 "admin": false,103 "staff": false,104 "user_id": 36477,105 "hidden": false,106 "trust_level": 2,107 "deleted_at": null,108 "user_deleted": false,109 "edit_reason": null,110 "can_view_edit_history": true,111 "wiki": false,112 "post_url": "/t/connecting-another-models-final-layers-with-a-pre-trained-model/117325/2",113 "can_accept_answer": false,114 "can_unaccept_answer": false,115 "accepted_answer": false,116 "topic_accepted_answer": true117 },118 {119 "id": 275650,120 "name": "Nikola Vetnic",121 "username": "NikolaVetnic",122 "avatar_template": "/user_avatar/discuss.pytorch.org/nikolavetnic/{size}/36807_2.png",123 "created_at": "2021-04-07T08:01:11.899Z",124 "cooked": "<p>Thank you very much for your quick reply.</p>\n<p>Wouldn’t nn.Linear create a new linear layer with specified num_features and num_classes? What if I have a pre-trained model whose fc I would like to use? The architecture is completely the same, I just don’t want to generate a new final layer but use one from an already existing model.</p>",125 "post_number": 3,126 "post_type": 1,127 "posts_count": 6,128 "updated_at": "2021-04-07T08:01:11.899Z",129 "reply_count": 1,130 "reply_to_post_number": null,131 "quote_count": 0,132 "incoming_link_count": 3,133 "reads": 14,134 "readers_count": 13,135 "score": 22.8,136 "yours": false,137 "topic_id": 117325,138 "topic_slug": "connecting-another-models-final-layers-with-a-pre-trained-model",139 "display_username": "Nikola Vetnic",140 "primary_group_name": null,141 "flair_name": null,142 "flair_url": null,143 "flair_bg_color": null,144 "flair_color": null,145 "flair_group_id": null,146 "badges_granted": [],147 "version": 1,148 "can_edit": false,149 "can_delete": false,150 "can_recover": false,151 "can_see_hidden_post": false,152 "can_wiki": false,153 "read": true,154 "user_title": null,155 "bookmarked": false,156 "actions_summary": [],157 "moderator": false,158 "admin": false,159 "staff": false,160 "user_id": 43991,161 "hidden": false,162 "trust_level": 0,163 "deleted_at": null,164 "user_deleted": false,165 "edit_reason": null,166 "can_view_edit_history": true,167 "wiki": false,168 "post_url": "/t/connecting-another-models-final-layers-with-a-pre-trained-model/117325/3",169 "can_accept_answer": false,170 "can_unaccept_answer": false,171 "accepted_answer": false,172 "topic_accepted_answer": true173 },174 {175 "id": 275757,176 "name": "timothy",177 "username": "tlim",178 "avatar_template": "/user_avatar/discuss.pytorch.org/tlim/{size}/32364_2.png",179 "created_at": "2021-04-07T15:29:58.094Z",180 "cooked": "<p>I have never done that before, my guess would be to load the saved model and assign it to <code>model.fc</code>. Make sure that your connecting layer takes in <code>model.fc.in_features</code>.</p>\n<p>Edit: In the use case of adding just a few extra linear layers, it really does not make any sense to have “trained linear layers” connected at the end because backbone is different to begin with. I do not see any benefits of doing this.</p>",181 "post_number": 4,182 "post_type": 1,183 "posts_count": 6,184 "updated_at": "2021-04-07T15:31:47.614Z",185 "reply_count": 1,186 "reply_to_post_number": 3,187 "quote_count": 0,188 "incoming_link_count": 8,189 "reads": 13,190 "readers_count": 12,191 "score": 47.6,192 "yours": false,193 "topic_id": 117325,194 "topic_slug": "connecting-another-models-final-layers-with-a-pre-trained-model",195 "display_username": "timothy",196 "primary_group_name": null,197 "flair_name": null,198 "flair_url": null,199 "flair_bg_color": null,200 "flair_color": null,201 "flair_group_id": null,202 "badges_granted": [],203 "version": 1,204 "can_edit": false,205 "can_delete": false,206 "can_recover": false,207 "can_see_hidden_post": false,208 "can_wiki": false,209 "read": true,210 "user_title": null,211 "reply_to_user": {212 "id": 43991,213 "username": "NikolaVetnic",214 "name": "Nikola Vetnic",215 "avatar_template": "/user_avatar/discuss.pytorch.org/nikolavetnic/{size}/36807_2.png"216 },217 "bookmarked": false,218 "actions_summary": [],219 "moderator": false,220 "admin": false,221 "staff": false,222 "user_id": 36477,223 "hidden": false,224 "trust_level": 2,225 "deleted_at": null,226 "user_deleted": false,227 "edit_reason": null,228 "can_view_edit_history": true,229 "wiki": false,230 "post_url": "/t/connecting-another-models-final-layers-with-a-pre-trained-model/117325/4",231 "can_accept_answer": false,232 "can_unaccept_answer": false,233 "accepted_answer": false,234 "topic_accepted_answer": true235 },236 {237 "id": 275758,238 "name": "Nikola Vetnic",239 "username": "NikolaVetnic",240 "avatar_template": "/user_avatar/discuss.pytorch.org/nikolavetnic/{size}/36807_2.png",241 "created_at": "2021-04-07T15:31:14.160Z",242 "cooked": "<p>Much appreciated, you gave me an idea I will try out and revert here if it proves successful.</p>",243 "post_number": 5,244 "post_type": 1,245 "posts_count": 6,246 "updated_at": "2021-04-07T15:31:14.160Z",247 "reply_count": 0,248 "reply_to_post_number": 4,249 "quote_count": 0,250 "incoming_link_count": 6,251 "reads": 13,252 "readers_count": 12,253 "score": 32.6,254 "yours": false,255 "topic_id": 117325,256 "topic_slug": "connecting-another-models-final-layers-with-a-pre-trained-model",257 "display_username": "Nikola Vetnic",258 "primary_group_name": null,259 "flair_name": null,260 "flair_url": null,261 "flair_bg_color": null,262 "flair_color": null,263 "flair_group_id": null,264 "badges_granted": [],265 "version": 1,266 "can_edit": false,267 "can_delete": false,268 "can_recover": false,269 "can_see_hidden_post": false,270 "can_wiki": false,271 "read": true,272 "user_title": null,273 "reply_to_user": {274 "id": 36477,275 "username": "tlim",276 "name": "timothy",277 "avatar_template": "/user_avatar/discuss.pytorch.org/tlim/{size}/32364_2.png"278 },279 "bookmarked": false,280 "actions_summary": [],281 "moderator": false,282 "admin": false,283 "staff": false,284 "user_id": 43991,285 "hidden": false,286 "trust_level": 0,287 "deleted_at": null,288 "user_deleted": false,289 "edit_reason": null,290 "can_view_edit_history": true,291 "wiki": false,292 "post_url": "/t/connecting-another-models-final-layers-with-a-pre-trained-model/117325/5",293 "can_accept_answer": false,294 "can_unaccept_answer": false,295 "accepted_answer": false,296 "topic_accepted_answer": true297 },298 {299 "id": 276695,300 "name": "Nikola Vetnic",301 "username": "NikolaVetnic",302 "avatar_template": "/user_avatar/discuss.pytorch.org/nikolavetnic/{size}/36807_2.png",303 "created_at": "2021-04-12T07:17:26.812Z",304 "cooked": "<p>So this is a solution I found to be working (please correct me if I am wrong):</p>\n<pre><code class=\"lang-auto\">class NetBase(nn.Module):\n\n def __init__(self, model, num_classes):\n super().__init__()\n\n self.features = nn.ModuleList(model.children())[:-1]\n self.fc = nn.Linear(512, num_classes)\n\n def forward(self, x):\n output = self.features(x)\n output = self.fc(output)\n\n return output\n\n\nclass NetMerged(nn.Module):\n\n def __init__(self, model1, model2):\n super().__init__()\n\n self.features = model1.features\n self.fc = model2.fc\n\n\n def forward(self, x):\n output = self.features(x)\n output = self.fc(output)\n\n return output\n</code></pre>\n<p>The idea is to load both models into an object of a new class and to redefine the forward function so that it passes through features of one model and the classifier of another.</p>\n<p>I’ve used the <code>num_classes</code> parameter for testing purposes, if one would create two models with different number of output classes by combining them and printing the model it should be possible to verify that indeed the classifier of another model is used with the feature extractor of the original one:</p>\n<pre><code class=\"lang-auto\">mBase1 = NetBase(m1, 10)\nmBase2 = NetBase(m2, 20)\n\nmMerged = NetMerged(mBase1, mBase2)\nprint(mMerged)\n</code></pre>",305 "post_number": 6,306 "post_type": 1,307 "posts_count": 6,308 "updated_at": "2021-04-12T07:17:33.045Z",309 "reply_count": 0,310 "reply_to_post_number": null,311 "quote_count": 0,312 "incoming_link_count": 16,313 "reads": 10,314 "readers_count": 9,315 "score": 77.0,316 "yours": false,317 "topic_id": 117325,318 "topic_slug": "connecting-another-models-final-layers-with-a-pre-trained-model",319 "display_username": "Nikola Vetnic",320 "primary_group_name": null,321 "flair_name": null,322 "flair_url": null,323 "flair_bg_color": null,324 "flair_color": null,325 "flair_group_id": null,326 "badges_granted": [],327 "version": 1,328 "can_edit": false,329 "can_delete": false,330 "can_recover": false,331 "can_see_hidden_post": false,332 "can_wiki": false,333 "read": true,334 "user_title": null,335 "bookmarked": false,336 "actions_summary": [],337 "moderator": false,338 "admin": false,339 "staff": false,340 "user_id": 43991,341 "hidden": false,342 "trust_level": 0,343 "deleted_at": null,344 "user_deleted": false,345 "edit_reason": null,346 "can_view_edit_history": true,347 "wiki": false,348 "post_url": "/t/connecting-another-models-final-layers-with-a-pre-trained-model/117325/6",349 "can_accept_answer": false,350 "can_unaccept_answer": false,351 "accepted_answer": true,352 "topic_accepted_answer": true353 }354 ],355 "stream": [356 275630,357 275642,358 275650,359 275757,360 275758,361 276695362 ]363 },364 "timeline_lookup": [365 [366 1,367 1663368 ],369 [370 4,371 1662372 ],373 [374 6,375 1658376 ]377 ],378 "suggested_topics": [379 {380 "fancy_title": "Semantic Segmentation with Attention based CycleGAN",381 "id": 217647,382 "title": "Semantic Segmentation with Attention based CycleGAN",383 "slug": "semantic-segmentation-with-attention-based-cyclegan",384 "posts_count": 5,385 "reply_count": 1,386 "highest_post_number": 5,387 "image_url": null,388 "created_at": "2025-03-10T06:44:59.887Z",389 "last_posted_at": "2025-03-11T22:57:33.781Z",390 "bumped": true,391 "bumped_at": "2025-03-11T22:57:33.781Z",392 "archetype": "regular",393 "unseen": false,394 "pinned": false,395 "unpinned": null,396 "visible": true,397 "closed": false,398 "archived": false,399 "bookmarked": null,400 "liked": null,401 "tags_descriptions": {},402 "like_count": 0,403 "views": 133,404 "category_id": 5,405 "featured_link": null,406 "has_accepted_answer": false,407 "posters": [408 {409 "extras": null,410 "description": "Original Poster",411 "user": {412 "id": 81422,413 "username": "Idrees11",414 "name": "Idrees Bhat",415 "avatar_template": "/user_avatar/discuss.pytorch.org/idrees11/{size}/74448_2.png",416 "trust_level": 1417 }418 },419 {420 "extras": "latest",421 "description": "Most Recent Poster",422 "user": {423 "id": 18088,424 "username": "KFrank",425 "name": "K. Frank",426 "avatar_template": "/letter_avatar_proxy/v4/letter/k/ecb155/{size}.png",427 "trust_level": 2428 }429 }430 ]431 },432 {433 "fancy_title": "Contrastive Learning Loss Function",434 "id": 214209,435 "title": "Contrastive Learning Loss Function",436 "slug": "contrastive-learning-loss-function",437 "posts_count": 1,438 "reply_count": 0,439 "highest_post_number": 1,440 "image_url": null,441 "created_at": "2024-12-13T20:15:02.419Z",442 "last_posted_at": "2024-12-13T20:15:02.458Z",443 "bumped": true,444 "bumped_at": "2024-12-14T00:41:05.465Z",445 "archetype": "regular",446 "unseen": false,447 "pinned": false,448 "unpinned": null,449 "visible": true,450 "closed": false,451 "archived": false,452 "bookmarked": null,453 "liked": null,454 "tags_descriptions": {},455 "like_count": 0,456 "views": 209,457 "category_id": 5,458 "featured_link": null,459 "has_accepted_answer": false,460 "posters": [461 {462 "extras": "latest single",463 "description": "Original Poster, Most Recent Poster",464 "user": {465 "id": 81495,466 "username": "Tonnar_Castellano",467 "name": "Tonnar Castellano",468 "avatar_template": "/user_avatar/discuss.pytorch.org/tonnar_castellano/{size}/73374_2.png",469 "trust_level": 1470 }471 }472 ]473 },474 {475 "fancy_title": "Stack expects each tensor to be equal size, but got [0, 4] at entry 0 and [1, 4] at entry 4",476 "id": 212329,477 "title": "Stack expects each tensor to be equal size, but got [0, 4] at entry 0 and [1, 4] at entry 4",478 "slug": "stack-expects-each-tensor-to-be-equal-size-but-got-0-4-at-entry-0-and-1-4-at-entry-4",479 "posts_count": 1,480 "reply_count": 0,481 "highest_post_number": 1,482 "image_url": null,483 "created_at": "2024-10-30T20:15:07.986Z",484 "last_posted_at": "2024-10-30T20:15:08.045Z",485 "bumped": true,486 "bumped_at": "2024-10-30T20:15:08.045Z",487 "archetype": "regular",488 "unseen": false,489 "pinned": false,490 "unpinned": null,491 "visible": true,492 "closed": false,493 "archived": false,494 "bookmarked": null,495 "liked": null,496 "tags_descriptions": {},497 "like_count": 0,498 "views": 133,499 "category_id": 5,500 "featured_link": null,501 "has_accepted_answer": false,502 "posters": [503 {504 "extras": "latest single",505 "description": "Original Poster, Most Recent Poster",506 "user": {507 "id": 60493,508 "username": "d3tk",509 "name": "Declan",510 "avatar_template": "/letter_avatar_proxy/v4/letter/d/f08c70/{size}.png",511 "trust_level": 1512 }513 }514 ]515 },516 {517 "fancy_title": "Finetuning of MaskRCNN generates no predictions (no boxes, no masks)",518 "id": 217412,519 "title": "Finetuning of MaskRCNN generates no predictions (no boxes, no masks)",520 "slug": "finetuning-of-maskrcnn-generates-no-predictions-no-boxes-no-masks",521 "posts_count": 3,522 "reply_count": 0,523 "highest_post_number": 3,524 "image_url": null,525 "created_at": "2025-03-04T01:50:09.476Z",526 "last_posted_at": "2025-03-08T03:51:10.097Z",527 "bumped": true,528 "bumped_at": "2025-03-08T03:51:10.097Z",529 "archetype": "regular",530 "unseen": false,531 "pinned": false,532 "unpinned": null,533 "visible": true,534 "closed": false,535 "archived": false,536 "bookmarked": null,537 "liked": null,538 "tags_descriptions": {},539 "like_count": 2,540 "views": 64,541 "category_id": 5,542 "featured_link": null,543 "has_accepted_answer": true,544 "posters": [545 {546 "extras": "latest",547 "description": "Original Poster, Most Recent Poster, Accepted Answer",548 "user": {549 "id": 83051,550 "username": "Igor_Borja",551 "name": "Igor Borja",552 "avatar_template": "/user_avatar/discuss.pytorch.org/igor_borja/{size}/75884_2.png",553 "trust_level": 1554 }555 },556 {557 "extras": null,558 "description": "Frequent Poster",559 "user": {560 "id": 72430,561 "username": "Eduardo_Lawson",562 "name": "Eduardo Lawson da Silva",563 "avatar_template": "/user_avatar/discuss.pytorch.org/eduardo_lawson/{size}/66899_2.png",564 "trust_level": 2565 }566 }567 ]568 },569 {570 "fancy_title": "Make Image Classification faster",571 "id": 222375,572 "title": "Make Image Classification faster",573 "slug": "make-image-classification-faster",574 "posts_count": 2,575 "reply_count": 0,576 "highest_post_number": 2,577 "image_url": null,578 "created_at": "2025-08-15T10:08:24.171Z",579 "last_posted_at": "2025-08-15T14:26:06.821Z",580 "bumped": true,581 "bumped_at": "2025-08-15T14:26:06.821Z",582 "archetype": "regular",583 "unseen": false,584 "pinned": false,585 "unpinned": null,586 "visible": true,587 "closed": false,588 "archived": false,589 "bookmarked": null,590 "liked": null,591 "tags_descriptions": {},592 "like_count": 0,593 "views": 31,594 "category_id": 5,595 "featured_link": null,596 "has_accepted_answer": false,597 "posters": [598 {599 "extras": null,600 "description": "Original Poster",601 "user": {602 "id": 85510,603 "username": "david_pom",604 "name": "",605 "avatar_template": "/user_avatar/discuss.pytorch.org/david_pom/{size}/75290_2.png",606 "trust_level": 0607 }608 },609 {610 "extras": "latest",611 "description": "Most Recent Poster",612 "user": {613 "id": 3534,614 "username": "ptrblck",615 "name": "",616 "avatar_template": "/user_avatar/discuss.pytorch.org/ptrblck/{size}/1823_2.png",617 "admin": true,618 "moderator": true,619 "trust_level": 2620 }621 }622 ]623 }624 ],625 "tags_descriptions": {},626 "fancy_title": "Connecting another model’s final layers with a pre-trained model",627 "id": 117325,628 "title": "Connecting another model's final layers with a pre-trained model",629 "posts_count": 6,630 "created_at": "2021-04-07T06:40:01.247Z",631 "views": 1579,632 "reply_count": 2,633 "like_count": 0,634 "last_posted_at": "2021-04-12T07:17:26.812Z",635 "visible": true,636 "closed": false,637 "archived": false,638 "has_summary": false,639 "archetype": "regular",640 "slug": "connecting-another-models-final-layers-with-a-pre-trained-model",641 "category_id": 5,642 "word_count": 579,643 "deleted_at": null,644 "user_id": 43991,645 "featured_link": null,646 "pinned_globally": false,647 "pinned_at": null,648 "pinned_until": null,649 "image_url": null,650 "slow_mode_seconds": 0,651 "draft": null,652 "draft_key": "topic_117325",653 "draft_sequence": null,654 "unpinned": null,655 "pinned": false,656 "current_post_number": 1,657 "highest_post_number": 6,658 "deleted_by": null,659 "actions_summary": [660 {661 "id": 4,662 "count": 0,663 "hidden": false,664 "can_act": false665 },666 {667 "id": 8,668 "count": 0,669 "hidden": false,670 "can_act": false671 },672 {673 "id": 10,674 "count": 0,675 "hidden": false,676 "can_act": false677 },678 {679 "id": 7,680 "count": 0,681 "hidden": false,682 "can_act": false683 }684 ],685 "chunk_size": 20,686 "bookmarked": false,687 "topic_timer": null,688 "message_bus_last_id": 0,689 "participant_count": 2,690 "show_read_indicator": false,691 "thumbnails": null,692 "slow_mode_enabled_until": null,693 "accepted_answer": {694 "post_number": 6,695 "username": "NikolaVetnic",696 "name": "Nikola Vetnic",697 "excerpt": "So this is a solution I found to be working (please correct me if I am wrong): \nclass NetBase(nn.Module):\n\n def __init__(self, model, num_classes):\n super().__init__()\n\n self.features = nn.ModuleList(model.children())[:-1]\n self.fc = nn.Linear(512, num_classes)\n\n def forwa…"698 },699 "can_vote": false,700 "vote_count": 0,701 "user_voted": false,702 "discourse_zendesk_plugin_zendesk_id": null,703 "discourse_zendesk_plugin_zendesk_url": "https://your-url.zendesk.com/agent/tickets/",704 "details": {705 "can_edit": false,706 "notification_level": 1,707 "participants": [708 {709 "id": 43991,710 "username": "NikolaVetnic",711 "name": "Nikola Vetnic",712 "avatar_template": "/user_avatar/discuss.pytorch.org/nikolavetnic/{size}/36807_2.png",713 "post_count": 4,714 "primary_group_name": null,715 "flair_name": null,716 "flair_url": null,717 "flair_color": null,718 "flair_bg_color": null,719 "flair_group_id": null,720 "trust_level": 0721 },722 {723 "id": 36477,724 "username": "tlim",725 "name": "timothy",726 "avatar_template": "/user_avatar/discuss.pytorch.org/tlim/{size}/32364_2.png",727 "post_count": 2,728 "primary_group_name": null,729 "flair_name": null,730 "flair_url": null,731 "flair_color": null,732 "flair_bg_color": null,733 "flair_group_id": null,734 "trust_level": 2735 }736 ],737 "created_by": {738 "id": 43991,739 "username": "NikolaVetnic",740 "name": "Nikola Vetnic",741 "avatar_template": "/user_avatar/discuss.pytorch.org/nikolavetnic/{size}/36807_2.png"742 },743 "last_poster": {744 "id": 43991,745 "username": "NikolaVetnic",746 "name": "Nikola Vetnic",747 "avatar_template": "/user_avatar/discuss.pytorch.org/nikolavetnic/{size}/36807_2.png"748 }749 },750 "bookmarks": []751 },752 {753 "post_stream": {754 "posts": [755 {756 "id": 276538,757 "name": "NeroArcher",758 "username": "NeroArcher",759 "avatar_template": "/user_avatar/discuss.pytorch.org/neroarcher/{size}/33334_2.png",760 "created_at": "2021-04-11T11:02:17.632Z",761 "cooked": "<p>Hello all, I am new to Pytorch and machine learning and recently I tried to run <a href=\"https://github.com/pclucas14/pixel-cnn-pp/blob/master/main.py\" rel=\"noopener nofollow ugc\">https://github.com/pclucas14/pixel-cnn-pp/blob/master/main.py</a> with my own dataset.<br>\nMy dataset is as following:</p>\n<pre><code class=\"lang-auto\"> def __init__(self, datapath, train=False, transform=None):\n self.data = []\n self.map_dic = {\"-\" : 0, \"X\" : 1, \"S\" : 2, \"?\" : 3, \"Q\" : 4, \"E\" : 5, \"<\" : 6, \">\" : 7, \"[\" : 8, \"]\" : 9, \"o\" : 10, \"B\" : 11, \"b\" : 12}\n for file_name in os.listdir(datapath): \n with open(base_path + \"/\" + file_name, 'r') as f:\n res = np.array( list( map( lambda l: [self.map_dic.get(c, c) for c in l.strip()], f.readlines() ) ) )\n self.data.append(res)\n self.transform = transform\n\n def __getitem__(self, index):\n img = self.data[index]\n\n img = torch.from_numpy(img)\n img = img.unsqueeze(0)\n\n # image transform:\n # img = Image.open('dataset_img/{}.png'.format(index)).convert('L')\n # if self.transform is not None:\n # img = self.transform(img)\n\n return img,0\n \n def __len__(self):\n return len(self.data)\n</code></pre>\n<p>The code works fine originally when I load the image from file (using the comment part code) and transform with the following</p>\n<pre><code class=\"lang-auto\"> transforms.Resize(size=(16,224)),\n transforms.ToTensor()\n ])\n</code></pre>\n<p>The image file here is created by<code>matplotlib.pyplot.imsave('name1.png', data, cmap='gray')</code> function from the numpy array(like this<br>\n[[0 0 0 … 0 0 0]<br>\n[0 0 0 … 0 0 0]<br>\n[0 0 0 … 0 0 0]<br>\n…<br>\n[1 1 1 … 1 1 1]<br>\n[1 1 1 … 1 1 1]<br>\n[1 1 1 … 1 1 1]]<br>\n)<br>\nThen I think that it is not necessary to convert numpy array to image and then to tensor. Instead I could convert directly from numpy array to tensor. After I use the above dataset, it constantly gives the error:</p>\n<pre><code class=\"lang-auto\"> File \"maintxt1.py\", line 241, in <module>\n#in original code it is line 122\n output = model(input)\n File \"/home/tangyeping/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py\", line 550, in __call__\n result = self.forward(*input, **kwargs)\n File \"/home/tangyeping/pixel-cnn-pp/model.py\", line 119, in forward\n x = x if sample else torch.cat((x, self.init_padding), 1)\nRuntimeError: Expected object of scalar type long int but got scalar type float for sequence element 1.\n</code></pre>\n<p>I have tried something like <code> img = torch.from_numpy(img).long()</code> or <code>model.double()</code> but this error keeps appering. Appreciate for any suggestion.</p>",762 "post_number": 1,763 "post_type": 1,764 "posts_count": 6,765 "updated_at": "2021-04-11T11:03:24.326Z",766 "reply_count": 0,767 "reply_to_post_number": null,768 "quote_count": 0,769 "incoming_link_count": 227,770 "reads": 7,771 "readers_count": 6,772 "score": 1136.4,773 "yours": false,774 "topic_id": 117790,775 "topic_slug": "runtimeerror-expected-object-of-scalar-type-long-int-but-got-scalar-type-float-for-sequence-element-1",776 "display_username": "NeroArcher",777 "primary_group_name": null,778 "flair_name": null,779 "flair_url": null,780 "flair_bg_color": null,781 "flair_color": null,782 "flair_group_id": null,783 "badges_granted": [],784 "version": 1,785 "can_edit": false,786 "can_delete": false,787 "can_recover": false,788 "can_see_hidden_post": false,789 "can_wiki": false,790 "link_counts": [791 {792 "url": "https://github.com/pclucas14/pixel-cnn-pp/blob/master/main.py",793 "internal": false,794 "reflection": false,795 "title": "pixel-cnn-pp/main.py at master · pclucas14/pixel-cnn-pp · GitHub",796 "clicks": 0797 }798 ],799 "read": true,800 "user_title": null,801 "bookmarked": false,802 "actions_summary": [],803 "moderator": false,804 "admin": false,805 "staff": false,806 "user_id": 40956,807 "hidden": false,808 "trust_level": 1,809 "deleted_at": null,810 "user_deleted": false,811 "edit_reason": null,812 "can_view_edit_history": true,813 "wiki": false,814 "post_url": "/t/runtimeerror-expected-object-of-scalar-type-long-int-but-got-scalar-type-float-for-sequence-element-1/117790/1",815 "can_accept_answer": false,816 "can_unaccept_answer": false,817 "accepted_answer": false,818 "topic_accepted_answer": null,819 "can_vote": false820 },821 {822 "id": 276549,823 "name": "Kapil Rana",824 "username": "Kapil_Rana",825 "avatar_template": "/user_avatar/discuss.pytorch.org/kapil_rana/{size}/20951_2.png",826 "created_at": "2021-04-11T12:47:13.394Z",827 "cooked": "<p>Firstly you have not applied the defined transformation.<br>\napply the transformation.</p>\n<p>second, if you don’t want to apply the transformation,<br>\nchange the tensor type using the torch.from_numpy(img).float</p>\n<p>show output if you still got any errors.</p>",828 "post_number": 2,829 "post_type": 1,830 "posts_count": 6,831 "updated_at": "2021-04-11T12:47:13.394Z",832 "reply_count": 1,833 "reply_to_post_number": null,834 "quote_count": 0,835 "incoming_link_count": 1,836 "reads": 5,837 "readers_count": 4,838 "score": 11.0,839 "yours": false,840 "topic_id": 117790,841 "topic_slug": "runtimeerror-expected-object-of-scalar-type-long-int-but-got-scalar-type-float-for-sequence-element-1",842 "display_username": "Kapil Rana",843 "primary_group_name": null,844 "flair_name": null,845 "flair_url": null,846 "flair_bg_color": null,847 "flair_color": null,848 "flair_group_id": null,849 "badges_granted": [],850 "version": 1,851 "can_edit": false,852 "can_delete": false,853 "can_recover": false,854 "can_see_hidden_post": false,855 "can_wiki": false,856 "read": true,857 "user_title": null,858 "bookmarked": false,859 "actions_summary": [],860 "moderator": false,861 "admin": false,862 "staff": false,863 "user_id": 28082,864 "hidden": false,865 "trust_level": 2,866 "deleted_at": null,867 "user_deleted": false,868 "edit_reason": null,869 "can_view_edit_history": true,870 "wiki": false,871 "post_url": "/t/runtimeerror-expected-object-of-scalar-type-long-int-but-got-scalar-type-float-for-sequence-element-1/117790/2",872 "can_accept_answer": false,873 "can_unaccept_answer": false,874 "accepted_answer": false,875 "topic_accepted_answer": null876 },877 {878 "id": 276552,879 "name": "NeroArcher",880 "username": "NeroArcher",881 "avatar_template": "/user_avatar/discuss.pytorch.org/neroarcher/{size}/33334_2.png",882 "created_at": "2021-04-11T13:11:07.095Z",883 "cooked": "<p>Thank you a lot! I did not use transform because now I am dealing with array and tensor instead of image. After I use <code>torch.from_numpy(img).float()</code> this error is going. However another error coming up:</p>\n<pre><code class=\"lang-auto\">loss : 6.1917, time : 0.4663\nTraceback (most recent call last):\n File \"maintxt1.py\", line 240, in <module>\n output = model(input)\n File \"/home/tangyeping/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py\", line 550, in __call__\n result = self.forward(*input, **kwargs)\n File \"/home/tangyeping/pixel-cnn-pp/model.py\", line 119, in forward\n x = x if sample else torch.cat((x, self.init_padding), 1)\nRuntimeError: Sizes of tensors must match except in dimension 3. Got 184 and 148\n</code></pre>\n<p>I have met this error before when using image object. And after I apply <code>transforms.Resize(size=(16,224))</code> the error is gone. Is there anything similar I can do with tensor or numpy array to fix it? Or is there another source of error?</p>",884 "post_number": 3,885 "post_type": 1,886 "posts_count": 6,887 "updated_at": "2021-04-11T13:11:07.095Z",888 "reply_count": 1,889 "reply_to_post_number": 2,890 "quote_count": 0,891 "incoming_link_count": 1,892 "reads": 4,893 "readers_count": 3,894 "score": 10.8,895 "yours": false,896 "topic_id": 117790,897 "topic_slug": "runtimeerror-expected-object-of-scalar-type-long-int-but-got-scalar-type-float-for-sequence-element-1",898 "display_username": "NeroArcher",899 "primary_group_name": null,900 "flair_name": null,901 "flair_url": null,902 "flair_bg_color": null,903 "flair_color": null,904 "flair_group_id": null,905 "badges_granted": [],906 "version": 1,907 "can_edit": false,908 "can_delete": false,909 "can_recover": false,910 "can_see_hidden_post": false,911 "can_wiki": false,912 "read": true,913 "user_title": null,914 "reply_to_user": {915 "id": 28082,916 "username": "Kapil_Rana",917 "name": "Kapil Rana",918 "avatar_template": "/user_avatar/discuss.pytorch.org/kapil_rana/{size}/20951_2.png"919 },920 "bookmarked": false,921 "actions_summary": [],922 "moderator": false,923 "admin": false,924 "staff": false,925 "user_id": 40956,926 "hidden": false,927 "trust_level": 1,928 "deleted_at": null,929 "user_deleted": false,930 "edit_reason": null,931 "can_view_edit_history": true,932 "wiki": false,933 "post_url": "/t/runtimeerror-expected-object-of-scalar-type-long-int-but-got-scalar-type-float-for-sequence-element-1/117790/3",934 "can_accept_answer": false,935 "can_unaccept_answer": false,936 "accepted_answer": false,937 "topic_accepted_answer": null938 },939 {940 "id": 276555,941 "name": "Kapil Rana",942 "username": "Kapil_Rana",943 "avatar_template": "/user_avatar/discuss.pytorch.org/kapil_rana/{size}/20951_2.png",944 "created_at": "2021-04-11T13:14:46.329Z",945 "cooked": "<p>Okie.</p>\n<p>Now as you have not used ToTensor().<br>\nyou have to reshape the data before feeding to model.<br>\ninput = input.permute(batch_size, input_channel, n,m)</p>",946 "post_number": 4,947 "post_type": 1,948 "posts_count": 6,949 "updated_at": "2021-04-11T13:14:46.329Z",950 "reply_count": 1,951 "reply_to_post_number": 3,952 "quote_count": 0,953 "incoming_link_count": 1,954 "reads": 4,955 "readers_count": 3,956 "score": 10.8,957 "yours": false,958 "topic_id": 117790,959 "topic_slug": "runtimeerror-expected-object-of-scalar-type-long-int-but-got-scalar-type-float-for-sequence-element-1",960 "display_username": "Kapil Rana",961 "primary_group_name": null,962 "flair_name": null,963 "flair_url": null,964 "flair_bg_color": null,965 "flair_color": null,966 "flair_group_id": null,967 "badges_granted": [],968 "version": 1,969 "can_edit": false,970 "can_delete": false,971 "can_recover": false,972 "can_see_hidden_post": false,973 "can_wiki": false,974 "read": true,975 "user_title": null,976 "reply_to_user": {977 "id": 40956,978 "username": "NeroArcher",979 "name": "NeroArcher",980 "avatar_template": "/user_avatar/discuss.pytorch.org/neroarcher/{size}/33334_2.png"981 },982 "bookmarked": false,983 "actions_summary": [],984 "moderator": false,985 "admin": false,986 "staff": false,987 "user_id": 28082,988 "hidden": false,989 "trust_level": 2,990 "deleted_at": null,991 "user_deleted": false,992 "edit_reason": null,993 "can_view_edit_history": true,994 "wiki": false,995 "post_url": "/t/runtimeerror-expected-object-of-scalar-type-long-int-but-got-scalar-type-float-for-sequence-element-1/117790/4",996 "can_accept_answer": false,997 "can_unaccept_answer": false,998 "accepted_answer": false,999 "topic_accepted_answer": null1000 },1001 {1002 "id": 276557,1003 "name": "NeroArcher",1004 "username": "NeroArcher",1005 "avatar_template": "/user_avatar/discuss.pytorch.org/neroarcher/{size}/33334_2.png",1006 "created_at": "2021-04-11T13:34:28.444Z",1007 "cooked": "<p>Thank you for the advice. I thought what permute can do is switching the dimension of tensor. I try to print the size of input and I got</p>\n<pre><code class=\"lang-auto\">starting training\ntorch.Size([1, 1, 16, 184])\nloss : 6.1917, time : 0.4552\ntorch.Size([1, 1, 16, 148])\nTraceback (most recent call last):\n File \"maintxt1.py\", line 241, in <module>\n output = model(input)\n File \"/home/tangyeping/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py\", line 550, in __call__\n result = self.forward(*input, **kwargs)\n File \"/home/tangyeping/pixel-cnn-pp/model.py\", line 119, in forward\n x = x if sample else torch.cat((x, self.init_padding), 1)\nRuntimeError: Sizes of tensors must match except in dimension 3. Got 184 and 148\n</code></pre>\n<p>I am not sure how I can use permute to reshape the data like what transform.Resize() does.</p>",1008 "post_number": 5,1009 "post_type": 1,1010 "posts_count": 6,1011 "updated_at": "2021-04-11T13:34:28.444Z",1012 "reply_count": 1,1013 "reply_to_post_number": 4,1014 "quote_count": 0,1015 "incoming_link_count": 2,1016 "reads": 4,1017 "readers_count": 3,1018 "score": 15.8,1019 "yours": false,1020 "topic_id": 117790,1021 "topic_slug": "runtimeerror-expected-object-of-scalar-type-long-int-but-got-scalar-type-float-for-sequence-element-1",1022 "display_username": "NeroArcher",1023 "primary_group_name": null,1024 "flair_name": null,1025 "flair_url": null,1026 "flair_bg_color": null,1027 "flair_color": null,1028 "flair_group_id": null,1029 "badges_granted": [],1030 "version": 1,1031 "can_edit": false,1032 "can_delete": false,1033 "can_recover": false,1034 "can_see_hidden_post": false,1035 "can_wiki": false,1036 "read": true,1037 "user_title": null,1038 "reply_to_user": {1039 "id": 28082,1040 "username": "Kapil_Rana",1041 "name": "Kapil Rana",1042 "avatar_template": "/user_avatar/discuss.pytorch.org/kapil_rana/{size}/20951_2.png"1043 },1044 "bookmarked": false,1045 "actions_summary": [],1046 "moderator": false,1047 "admin": false,1048 "staff": false,1049 "user_id": 40956,1050 "hidden": false,1051 "trust_level": 1,1052 "deleted_at": null,1053 "user_deleted": false,1054 "edit_reason": null,1055 "can_view_edit_history": true,1056 "wiki": false,1057 "post_url": "/t/runtimeerror-expected-object-of-scalar-type-long-int-but-got-scalar-type-float-for-sequence-element-1/117790/5",1058 "can_accept_answer": false,1059 "can_unaccept_answer": false,1060 "accepted_answer": false,1061 "topic_accepted_answer": null1062 },1063 {1064 "id": 276688,1065 "name": "Kapil Rana",1066 "username": "Kapil_Rana",1067 "avatar_template": "/user_avatar/discuss.pytorch.org/kapil_rana/{size}/20951_2.png",1068 "created_at": "2021-04-12T06:06:55.999Z",1069 "cooked": "<p>Cant see 184 in the input size, from where this came from.<br>\nThe issue is with the size, please check forward function also .<br>\nSomething is happening here<br>\nx = x if sample else torch.cat((x, self.init_padding), 1)</p>\n<p>print the sizes in forward function it will help to debug.</p>",1070 "post_number": 6,1071 "post_type": 1,1072 "posts_count": 6,1073 "updated_at": "2021-04-12T06:06:55.999Z",1074 "reply_count": 0,1075 "reply_to_post_number": 5,1076 "quote_count": 0,1077 "incoming_link_count": 4,1078 "reads": 3,1079 "readers_count": 2,1080 "score": 20.6,1081 "yours": false,1082 "topic_id": 117790,1083 "topic_slug": "runtimeerror-expected-object-of-scalar-type-long-int-but-got-scalar-type-float-for-sequence-element-1",1084 "display_username": "Kapil Rana",1085 "primary_group_name": null,1086 "flair_name": null,1087 "flair_url": null,1088 "flair_bg_color": null,1089 "flair_color": null,1090 "flair_group_id": null,1091 "badges_granted": [],1092 "version": 1,1093 "can_edit": false,1094 "can_delete": false,1095 "can_recover": false,1096 "can_see_hidden_post": false,1097 "can_wiki": false,1098 "read": true,1099 "user_title": null,1100 "reply_to_user": {1101 "id": 40956,1102 "username": "NeroArcher",1103 "name": "NeroArcher",1104 "avatar_template": "/user_avatar/discuss.pytorch.org/neroarcher/{size}/33334_2.png"1105 },1106 "bookmarked": false,1107 "actions_summary": [],1108 "moderator": false,1109 "admin": false,1110 "staff": false,1111 "user_id": 28082,1112 "hidden": false,1113 "trust_level": 2,1114 "deleted_at": null,1115 "user_deleted": false,1116 "edit_reason": null,1117 "can_view_edit_history": true,1118 "wiki": false,1119 "post_url": "/t/runtimeerror-expected-object-of-scalar-type-long-int-but-got-scalar-type-float-for-sequence-element-1/117790/6",1120 "can_accept_answer": false,1121 "can_unaccept_answer": false,1122 "accepted_answer": false,1123 "topic_accepted_answer": null1124 }1125 ],1126 "stream": [1127 276538,1128 276549,1129 276552,1130 276555,1131 276557,1132 2766881133 ]1134 },1135 "timeline_lookup": [1136 [1137 1,1138 16581139 ]1140 ],1141 "suggested_topics": [1142 {1143 "fancy_title": "How to Build a More Efficient DataLoader to Load Large Image Datasets?",1144 "id": 213229,1145 "title": "How to Build a More Efficient DataLoader to Load Large Image Datasets?",1146 "slug": "how-to-build-a-more-efficient-dataloader-to-load-large-image-datasets",1147 "posts_count": 3,1148 "reply_count": 1,1149 "highest_post_number": 3,1150 "image_url": null,1151 "created_at": "2024-11-20T20:09:54.513Z",1152 "last_posted_at": "2024-11-22T16:09:19.099Z",1153 "bumped": true,1154 "bumped_at": "2024-11-22T16:09:19.099Z",1155 "archetype": "regular",1156 "unseen": false,1157 "pinned": false,1158 "unpinned": null,1159 "visible": true,1160 "closed": false,1161 "archived": false,1162 "bookmarked": null,1163 "liked": null,1164 "tags_descriptions": {},1165 "like_count": 0,1166 "views": 172,1167 "category_id": 5,1168 "featured_link": null,1169 "has_accepted_answer": false,1170 "posters": [1171 {1172 "extras": null,1173 "description": "Original Poster",1174 "user": {1175 "id": 32574,1176 "username": "Nick_ishere",1177 "name": "",1178 "avatar_template": "/user_avatar/discuss.pytorch.org/nick_ishere/{size}/25121_2.png",1179 "trust_level": 11180 }1181 },1182 {1183 "extras": null,1184 "description": "Frequent Poster",1185 "user": {1186 "id": 72430,1187 "username": "Eduardo_Lawson",1188 "name": "Eduardo Lawson da Silva",1189 "avatar_template": "/user_avatar/discuss.pytorch.org/eduardo_lawson/{size}/66899_2.png",1190 "trust_level": 21191 }1192 },1193 {1194 "extras": "latest",1195 "description": "Most Recent Poster",1196 "user": {1197 "id": 64498,1198 "username": "nickums",1199 "name": "Nickums",1200 "avatar_template": "/user_avatar/discuss.pytorch.org/nickums/{size}/58652_2.png",