title: Still error trying to run libtorch helloworld pod install on apple silicon m1

body:
When trying to create the cocoapod for ios pytorch helloworld on my mac mini m1 (big sur 11.2.1)
with installed miniconda3, homebrew 3.1.1, ruby 3.0.1.p64, gem 3.2.15, pod 1.10.1
and PATH set to
/opt/homebrew/lib/ruby/gems/3.0.0/bin:/opt/homebrew/opt/ruby/bin:$PATH
according to pod install giving error related to ruby gems and libffi
running pod install dies with a segmentation fault, see the following ruby crash file
So maybe someone can give me an idea how to get ios 14.4 and m1 big sur 11  working together?
Process:               ruby [80736]
Path:                  /opt/homebrew/*/ruby
Identifier:            ruby
Version:               0
Code Type:             ARM-64 (Native)
Parent Process:        bash [70856]
Responsible:           Terminal [70854]
User ID:               474179448

Date/Time:             2021-04-15 16:58:57.213 +0200
OS Version:            macOS 11.2.1 (20D74)
Report Version:        12
Anonymous UUID:        5893C804-2FF8-FBB1-5889-A883C6E0135A

Sleep/Wake UUID:       0044F30E-9B3E-4B4D-B6D2-B508B271D6B1

Time Awake Since Boot: 85000 seconds
Time Since Wake:       2300 seconds

System Integrity Protection: enabled

Crashed Thread:        0  Dispatch queue: com.apple.main-thread

Exception Type:        EXC_BAD_ACCESS (SIGABRT)
Exception Codes:       KERN_INVALID_ADDRESS at 0x0000000000000001
Exception Note:        EXC_CORPSE_NOTIFY

VM Regions Near 0x1:
-->
    __TEXT                      10234c000-102350000    [   16K] r-x/r-x SM=COW  /opt/homebrew/*

Application Specific Information:
abort() called

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   libsystem_kernel.dylib          0x0000000185430cec __pthread_kill + 8
1   libsystem_pthread.dylib         0x0000000185461c24 pthread_kill + 292
2   libsystem_c.dylib               0x00000001853a9864 abort + 104
3   libruby.3.0.dylib               0x00000001026923a0 die + 12
4   libruby.3.0.dylib               0x00000001026923f8 rb_bug_for_fatal_signal + 88
5   libruby.3.0.dylib               0x0000000102774490 sigsegv + 96
6   libsystem_platform.dylib        0x00000001854a9c44 _sigtramp + 56
7   ???                             0xffff80019719d8cc 0 + 18446603343051217100
8   libcurl.4.dylib                 0x000000019719d8cc curl_easy_getinfo + 40
9   libffi.dylib                    0x0000000191ef4050 ffi_call_SYSV + 80
10  libffi.dylib                    0x0000000191efc9d8 ffi_call_int + 944
11  ffi_c.bundle                    0x00000001042394cc rbffi_CallFunction + 260 (Call.c:400)
12  ffi_c.bundle                    0x000000010423d250 attached_method_invoke + 44 (MethodHandle.c:174)
13  libffi.dylib                    0x0000000191efce10 ffi_closure_SYSV_inner + 800
14  libffi.dylib                    0x0000000191ef41e8 ffi_closure_SYSV + 56
15  libruby.3.0.dylib               0x00000001027e0b84 vm_call_cfunc_with_frame + 228
16  libruby.3.0.dylib               0x00000001027db078 vm_sendish + 1116
17  libruby.3.0.dylib               0x00000001027c7f70 vm_exec_core + 6948
18  libruby.3.0.dylib               0x00000001027d72c8 rb_vm_exec + 1652
19  libruby.3.0.dylib               0x00000001027e3df8 invoke_block_from_c_bh + 616
20  libruby.3.0.dylib               0x00000001027d1bf4 rb_yield + 180
21  libruby.3.0.dylib               0x00000001026359a0 rb_ary_each + 84
22  libruby.3.0.dylib               0x00000001027e0b84 vm_call_cfunc_with_frame + 228
23  libruby.3.0.dylib               0x00000001027db078 vm_sendish + 1116
24  libruby.3.0.dylib               0x00000001027c7f10 vm_exec_core + 6852
25  libruby.3.0.dylib               0x00000001027d72c8 rb_vm_exec + 1652
26  libruby.3.0.dylib               0x00000001027e3df8 invoke_block_from_c_bh + 616
27  libruby.3.0.dylib               0x00000001027d1bf4 rb_yield + 180
28  libruby.3.0.dylib               0x00000001026359a0 rb_ary_each + 84
29  libruby.3.0.dylib               0x00000001027e0b84 vm_call_cfunc_with_frame + 228
30  libruby.3.0.dylib               0x00000001027db078 vm_sendish + 1116
31  libruby.3.0.dylib               0x00000001027c7f10 vm_exec_core + 6852
32  libruby.3.0.dylib               0x00000001027d72c8 rb_vm_exec + 1652
33  libruby.3.0.dylib               0x00000001027e3df8 invoke_block_from_c_bh + 616
34  libruby.3.0.dylib               0x00000001027d1bf4 rb_yield + 180
35  libruby.3.0.dylib               0x0000000102639ca8 rb_ary_collect + 168
36  libruby.3.0.dylib               0x00000001027e0b84 vm_call_cfunc_with_frame + 228
37  libruby.3.0.dylib               0x00000001027db078 vm_sendish + 1116
38  libruby.3.0.dylib               0x00000001027c7f10 vm_exec_core + 6852
39  libruby.3.0.dylib               0x00000001027d72c8 rb_vm_exec + 1652
40  libruby.3.0.dylib               0x00000001026dd2cc load_iseq_eval + 200
41  libruby.3.0.dylib               0x00000001026db528 rb_load_internal + 56
42  libruby.3.0.dylib               0x00000001026dc850 rb_f_load + 180
43  libruby.3.0.dylib               0x00000001027e0b84 vm_call_cfunc_with_frame + 228
44  libruby.3.0.dylib               0x00000001027db078 vm_sendish + 1116
45  libruby.3.0.dylib               0x00000001027c7f70 vm_exec_core + 6948
46  libruby.3.0.dylib               0x00000001027d72c8 rb_vm_exec + 1652
47  libruby.3.0.dylib               0x0000000102698f8c rb_ec_exec_node + 268
48  libruby.3.0.dylib               0x0000000102698e20 ruby_run_node + 96
49  ruby                            0x000000010234fec0 main + 92
50  libdyld.dylib                   0x000000018547df34 start + 4

Thread 1:
0   libsystem_kernel.dylib          0x0000000185430e04 poll + 8
1   libruby.3.0.dylib               0x00000001027a9530 timer_pthread_fn + 124
2   libsystem_pthread.dylib         0x000000018546206c _pthread_start + 320
3   libsystem_pthread.dylib         0x000000018545cda0 thread_start + 8

Thread 2:: FFI Callback Dispatcher
0   libsystem_kernel.dylib          0x000000018542c488 __psynch_cvwait + 8
1   libsystem_pthread.dylib         0x0000000185462568 _pthread_cond_wait + 1192
2   ffi_c.bundle                    0x000000010423b80c async_cb_wait + 88 (Function.c:605)
3   libruby.3.0.dylib               0x00000001027a1c48 rb_nogvl + 280
4   ffi_c.bundle                    0x000000010423afa0 async_cb_event + 140 (Function.c:545)
5   libruby.3.0.dylib               0x00000001027a86b8 thread_start_func_2 + 1104
6   libruby.3.0.dylib               0x00000001027a8174 thread_start_func_1 + 152
7   libsystem_pthread.dylib         0x000000018546206c _pthread_start + 320
8   libsystem_pthread.dylib         0x000000018545cda0 thread_start + 8

Thread 3:: open3.rb:403
0   libsystem_kernel.dylib          0x000000018542c488 __psynch_cvwait + 8
1   libsystem_pthread.dylib         0x0000000185462568 _pthread_cond_wait + 1192
2   libruby.3.0.dylib               0x000000010279f7b0 native_cond_timedwait + 120
3   libruby.3.0.dylib               0x00000001027a81e0 thread_start_func_1 + 260
4   libsystem_pthread.dylib         0x000000018546206c _pthread_start + 320
5   libsystem_pthread.dylib         0x000000018545cda0 thread_start + 8

Thread 0 crashed with ARM Thread State (64-bit):
...

Binary Images:
       0x10234c000 -        0x10234ffff +ruby (0) <F0E1ACB6-DADC-3709-A7C9-B20CA91730D9> /opt/homebrew/*/ruby
       0x1023ec000 -        0x1023effff +encdb.bundle (0) <0512F22A-C22F-3133-B7DF-C0630FBB7291> /opt/homebrew/*/encdb.bundle
       0x102400000 -        0x102403fff +transdb.bundle (0) <0749C9D7-559E-3089-8034-3F04F71589F5> /opt/homebrew/*/transdb.bundle
       0x102414000 -        0x102417fff +monitor.bundle (0) <46C2FFE4-EE5B-3C46-98D9-E6FE36564805> /opt/homebrew/*/monitor.bundle
       0x102428000 -        0x10242ffff +pathname.bundle (0) <52304C1D-6B1E-3CF4-927D-65D050FB8E7F> /opt/homebrew/*/pathname.bundle
       0x102440000 -        0x102443fff +escape.bundle (0) <80C6BFF1-ECCE-3004-96A4-3BC8A836F380> /opt/homebrew/*/escape.bundle
       0x102454000 -        0x102483fff +date_core.bundle (0) <5BEB2E2A-A969-3022-91F0-91F2A901D94B> /opt/homebrew/*/date_core.bundle
       0x1024a0000 -        0x1024affff +bigdecimal.bundle (0) <1292C18E-90FF-3113-A794-70FC5E7A61E6> /opt/homebrew/*/bigdecimal.bundle
       0x1024c0000 -        0x1024c7fff +stringio.bundle (0) <1A3F1181-6690-3E8C-BD5F-27F8A4A2F38D> /opt/homebrew/*/stringio.bundle
       0x1024d8000 -        0x1024dbfff +etc.bundle (0) <1E536D8B-A4A9-30F3-84E5-ADF950666B25> /opt/homebrew/*/etc.bundle
       0x1024ec000 -        0x1024effff +digest.bundle (0) <F596BA9F-A8BF-3D95-B5EB-6C617E4F0927> /opt/homebrew/*/digest.bundle
       0x102500000 -        0x102503fff +strscan.bundle (0) <9548F035-0164-3C07-A923-BE0DC07C4229> /opt/homebrew/*/strscan.bundle
       0x102514000 -        0x102517fff +wait.bundle (0) <AFC11C5F-E127-30EF-86C1-9DAD1B8027DA> /opt/homebrew/*/wait.bundle
       0x10252c000 -        0x1025a7fff  dyld (832.7.3) <4AB185B3-DC20-3C03-A193-67C0E6C589D7> /usr/lib/dyld
       0x102630000 -        0x10289bfff +libruby.3.0.dylib (0) <F87CC4ED-8475-3AEE-AD7B-2AF63B4EA539> /opt/homebrew/*/libruby.3.0.dylib
       0x103e1c000 -        0x103e3bfff +socket.bundle (0) <278655A9-7512-3AFC-AA68-6A824BA8C2FA> /opt/homebrew/*/socket.bundle
       0x103e54000 -        0x103e5ffff +zlib.bundle (0) <34A11F55-C4B0-3781-84CD-7847CC56B29E> /opt/homebrew/*/zlib.bundle
       0x103e74000 -        0x103e77fff +windows_31j.bundle (0) <610116E4-A042-3F9E-8CEF-0A0B4A608E4F> /opt/homebrew/*/windows_31j.bundle
       0x103e88000 -        0x103e8ffff +parser.bundle (0) <E08CB57E-FA0E-368E-8C2A-988494BD82A6> /opt/homebrew/*/parser.bundle
       0x103ea0000 -        0x103ea7fff +generator.bundle (0) <EA3FFFF6-797E-3172-9750-EFA602F9DA15> /opt/homebrew/*/generator.bundle
       0x103eb8000 -        0x103ef7fff +openssl.bundle (0) <D2ACCAC6-35B7-365D-A590-DC0A5683A383> /opt/homebrew/*/openssl.bundle
       0x103f24000 -        0x103f6ffff +libssl.1.1.dylib (0) <387D1B0F-EE36-3D80-96B0-21BCDA8B4E62> /opt/homebrew/*/libssl.1.1.dylib
       0x103fa4000 -        0x104123fff +libcrypto.1.1.dylib (0) <6703626D-9366-31E0-8697-C74FFA806779> /opt/homebrew/*/libcrypto.1.1.dylib
       0x1041c4000 -        0x1041c7fff +nonblock.bundle (0) <4285445A-338C-3B5F-9501-8132C66F1367> /opt/homebrew/*/nonblock.bundle
       0x1041d8000 -        0x1041dffff +psych.bundle (0) <6E68A386-AEB1-38A8-9AA3-4CB61FFA0439> /opt/homebrew/*/psych.bundle
       0x1041f0000 -        0x104207fff +libyaml-0.2.dylib (0) <86720355-4A4B-32E0-8BD0-18AB73A96463> /opt/homebrew/*/libyaml-0.2.dylib
       0x104218000 -        0x10421bfff +sha2.bundle (0) <F641AD10-D5E2-3D7D-BBFA-56F8294B442B> /opt/homebrew/*/sha2.bundle
       0x10422c000 -        0x104247fff +ffi_c.bundle (0) <6ADCB3BE-9056-33F0-AD30-10DAB38241E2> /opt/homebrew/*/ffi_c.bundle
       0x104270000 -        0x104277fff  libffi-trampolines.dylib (27) <ADFD2779-8444-3C1E-8AF1-F5BDCFDDA05B> /usr/lib/libffi-trampolines.dylib
       0x104280000 -        0x104283fff +sha1.bundle (0) <B63454E5-2319-352D-864B-8CEFF2C94880> /opt/homebrew/*/sha1.bundle
       0x1851be000 -        0x1851bffff  libsystem_blocks.dylib (78) <9B6D4883-03E9-3785-851E-EA79FA64ADC1> /usr/lib/system/libsystem_blocks.dylib
       0x1851c0000 -        0x1851f7fff  libxpc.dylib (2038.80.3) <BD0DFD42-0DC3-3F3D-9C04-5A2B3D93794D> /usr/lib/system/libxpc.dylib
       0x1851f8000 -        0x18520ffff  libsystem_trace.dylib (1277.80.2) <4A466196-D2DD-367B-80AB-988F281EC3B8> /usr/lib/system/libsystem_trace.dylib
       0x185210000 -        0x185284fff  libcorecrypto.dylib (1000.80.5) <9BD8FED7-2A36-3602-A5A7-0CA87C03FB84> /usr/lib/system/libcorecrypto.dylib
       0x185285000 -        0x1852b0fff  libsystem_malloc.dylib (317.40.8) <21120432-52C1-34E4-BF01-623722FA3A41> /usr/lib/system/libsystem_malloc.dylib
       0x1852b1000 -        0x1852f4fff  libdispatch.dylib (1271.40.12) <F5BFBD55-EF70-3659-854D-9061325EB26D> /usr/lib/system/libdispatch.dylib
       0x1852f5000 -        0x18532dfff  libobjc.A.dylib (818.2) <B03625B0-501E-3AC1-8E16-08B621120EAD> /usr/lib/libobjc.A.dylib
       0x18532e000 -        0x185330fff  libsystem_featureflags.dylib (28.60.1) <297CC4DD-AFA4-3BA3-B4E1-0DF47E49C21E> /usr/lib/system/libsystem_featureflags.dylib
       0x185331000 -        0x1853b1fff  libsystem_c.dylib (1439.40.11) <A7147E08-E7C0-3842-916E-F2270A689F47> /usr/lib/system/libsystem_c.dylib
       0x1853b2000 -        0x18540dfff  libc++.1.dylib (904.4) <B139607F-1E80-3A8E-870D-0AC022069EA1> /usr/lib/libc++.1.dylib
       0x18540e000 -        0x185427fff  libc++abi.dylib (904.4) <1DD3A1C9-D765-34FB-B8C1-87BF52CE49C0> /usr/lib/libc++abi.dylib
       0x185428000 -        0x18545afff  libsystem_kernel.dylib (7195.81.3) <55FCA547-4877-3075-8A08-FE1620BFC682> /usr/lib/system/libsystem_kernel.dylib
       0x18545b000 -        0x185466fff  libsystem_pthread.dylib (454.80.2) <8E907E6C-C227-312E-944C-767093692AFF> /usr/lib/system/libsystem_pthread.dylib
       0x185467000 -        0x1854a5fff  libdyld.dylib (832.7.3) <EF759BF3-97FA-30EA-A1CA-EDECFEA726FE> /usr/lib/system/libdyld.dylib
       0x1854a6000 -        0x1854acfff  libsystem_platform.dylib (254.80.2) <8633A39C-10A2-3B44-93F7-617AB09FF640> /usr/lib/system/libsystem_platform.dylib
       0x1854ad000 -        0x1854d8fff  libsystem_info.dylib (542.40.3) <4CC96CFC-7198-3F26-8C8C-20FB010CDF98> /usr/lib/system/libsystem_info.dylib
       0x1854d9000 -        0x185982fff  com.apple.CoreFoundation (6.9 - 1774.101) <EA76C90A-23ED-3791-8FBC-8292916F0B16> /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation
       0x185983000 -        0x185bb5fff  com.apple.LaunchServices (1122.11 - 1122.11) <B79A592B-8036-3E24-AD9D-3FB4E7BE2983> /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/LaunchServices
       0x185bb6000 -        0x185c8efff  com.apple.gpusw.MetalTools (1.0 - 1) <ED9E3F77-4900-3B5B-978A-70AA6762DFBA> /System/Library/PrivateFrameworks/MetalTools.framework/Versions/A/MetalTools
       0x185c8f000 -        0x185ee9fff  libBLAS.dylib (1336.40.1) <96EAD889-D898-3884-A36C-F433DC2C64DD> /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
       0x185eea000 -        0x185f34fff  com.apple.Lexicon-framework (1.0 - 86.1) <81EA9F2C-6059-322A-B336-56CD7F3AB6C2> /System/Library/PrivateFrameworks/Lexicon.framework/Versions/A/Lexicon
       0x185f35000 -        0x185f97fff  libSparse.dylib (106) <1A70E696-43E3-3D8B-A3E1-ADB624729BF4> /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libSparse.dylib
       0x185f98000 -        0x18601ffff  com.apple.SystemConfiguration (1.20 - 1.20) <DD6AB615-BB7B-3634-9D33-3923E5038BAA> /System/Library/Frameworks/SystemConfiguration.framework/Versions/A/SystemConfiguration
       0x186020000 -        0x186053fff  libCRFSuite.dylib (50) <79C1501B-B0F6-341A-96CC-F4FE066E3D59> /usr/lib/libCRFSuite.dylib
       0x186054000 -        0x18627efff  libmecabra.dylib (929.1.1) <29B77781-FA91-3180-AFE8-608A355AE97E> /usr/lib/libmecabra.dylib
       0x18627f000 -        0x186610fff  com.apple.Foundation (6.9 - 1774.101) <8F7A0D5B-0E89-36F6-AC84-D3BEC2C44792> /System/Library/Frameworks/Foundation.framework/Versions/C/Foundation
       0x186611000 -        0x186702fff  com.apple.LanguageModeling (1.0 - 247.1) <66B05273-1979-3DB2-9F68-C0E3A6CD88B1> /System/Library/PrivateFrameworks/LanguageModeling.framework/Versions/A/LanguageModeling
       0x18728c000 -        0x1875eafff  com.apple.security (7.0 - 59754.80.3) <C76855AD-6EE4-3413-9E6E-CC450BDB20E2> /System/Library/Frameworks/Security.framework/Versions/A/Security
       0x1875eb000 -        0x18785cfff  libicucore.A.dylib (66109) <3CE58F97-7FC3-37D8-BB99-D6BECFC86DD2> /usr/lib/libicucore.A.dylib
       0x18785d000 -        0x187867fff  libsystem_darwin.dylib (1439.40.11) <B790A863-2D74-300E-9698-A25B5602B32F> /usr/lib/system/libsystem_darwin.dylib
       0x187868000 -        0x187b57fff  com.apple.CoreServices.CarbonCore (1307 - 1307) <3EC22291-65E5-3EB6-9498-9A1244C90147> /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/CarbonCore.framework/Versions/A/CarbonCore
       0x187b93000 -        0x187bcefff  com.apple.CSStore (1122.11 - 1122.11) <52D8D7A1-4879-3488-9D39-5F2C7696EFA8> /System/Library/PrivateFrameworks/CoreServicesStore.framework/Versions/A/CoreServicesStore
       0x187bcf000 -        0x187c99fff  com.apple.framework.IOKit (2.0.2 - 1845.81.1) <516911DA-18D7-3D17-8646-BBF7C75CD070> /System/Library/Frameworks/IOKit.framework/Versions/A/IOKit
       0x187c9a000 -        0x187ca5fff  libsystem_notify.dylib (279.40.4) <A7B6BDA8-5371-352E-8A36-95D46C4B07F1> /usr/lib/system/libsystem_notify.dylib
       0x1890bc000 -        0x1897c6fff  libnetwork.dylib (2288.80.2) <07EC53A0-293C-3403-8394-755AE0BDDFA4> /usr/lib/libnetwork.dylib
       0x1897c7000 -        0x189c54fff  com.apple.CFNetwork (1220.1 - 1220.1) <3C5F5D1E-DB7C-3027-BBB0-91E6DEA3E264> /System/Library/Frameworks/CFNetwork.framework/Versions/A/CFNetwork
       0x189c55000 -        0x189c64fff  libsystem_networkextension.dylib (1295.80.3) <B6BD1267-BE59-3E42-B2B5-2BF13F17D02D> /usr/lib/system/libsystem_networkextension.dylib
       0x189c65000 -        0x189c66fff  libenergytrace.dylib (22) <C5CFEF87-BB69-3351-A0C8-9B601383A45C> /usr/lib/libenergytrace.dylib
       0x189c67000 -        0x189cbbfff  libMobileGestalt.dylib (978.80.1) <93C6E288-C098-357F-B8A5-3E133DF39ECE> /usr/lib/libMobileGestalt.dylib
       0x189cbc000 -        0x189cd3fff  libsystem_asl.dylib (385) <31E28E59-1CDD-3B83-8BF0-56C675227FA2> /usr/lib/system/libsystem_asl.dylib
       0x189cd4000 -        0x189cedfff  com.apple.TCC (1.0 - 1) <C55FE947-0C86-3AAC-9306-9EFA7C033D07> /System/Library/PrivateFrameworks/TCC.framework/Versions/A/TCC
       0x18aea2000 -        0x18aeb9fff  com.apple.ProtocolBuffer (1 - 285.23.11.29.1) <38163CA8-14FF-34A9-8AE4-D7D69B8C8854> /System/Library/PrivateFrameworks/ProtocolBuffer.framework/Versions/A/ProtocolBuffer
       0x18aeba000 -        0x18b061fff  libsqlite3.dylib (321.1) <8592B35B-9EA3-3C84-8453-9C86FB5C039C> /usr/lib/libsqlite3.dylib
       0x18b1c8000 -        0x18b23bfff  com.apple.AE (918.0.1 - 918.0.1) <7D13C9B5-D195-3E9E-B6C7-254F95A925C6> /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/AE.framework/Versions/A/AE
       0x18b23c000 -        0x18b243fff  libdns_services.dylib (1310.80.1) <64D4BA25-C388-3AB8-BDA8-2E81459DA46A> /usr/lib/libdns_services.dylib
       0x18b244000 -        0x18b24bfff  libsystem_symptoms.dylib (1431.40.36) <0657E539-C0CE-30F8-B630-FBAE36109542> /usr/lib/system/libsystem_symptoms.dylib
       0x18b24c000 -        0x18b3a8fff  com.apple.Network (1.0 - 1) <486C55B3-900C-3D09-AB0D-F99A152CFB84> /System/Library/Frameworks/Network.framework/Versions/A/Network
       0x18b3a9000 -        0x18b3cefff  com.apple.analyticsd (1.0 - 1) <E47FE17B-2ED2-3BE2-A5AB-046DB3C02EA0> /System/Library/PrivateFrameworks/CoreAnalytics.framework/Versions/A/CoreAnalytics
       0x18b3cf000 -        0x18b3d1fff  libDiagnosticMessagesClient.dylib (112) <20AD555E-DF00-3C91-A95B-AB2AD23780AA> /usr/lib/libDiagnosticMessagesClient.dylib
       0x18b3d2000 -        0x18b41dfff  com.apple.spotlight.metadata.utilities (1.0 - 2150.7.5) <5400DF7A-9249-30E9-B692-AC431C7F74D9> /System/Library/PrivateFrameworks/MetadataUtilities.framework/Versions/A/MetadataUtilities
       0x18b41e000 -        0x18b4b7fff  com.apple.Metadata (10.7.0 - 2150.7.5) <4B03E6F8-1568-338B-AA75-480F9D824516> /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/Metadata.framework/Versions/A/Metadata
       0x18b4b8000 -        0x18b4befff  com.apple.DiskArbitration (2.7 - 2.7) <7ED2211D-BA3C-37EC-BBA4-4320FBBC8A6A> /System/Library/Frameworks/DiskArbitration.framework/Versions/A/DiskArbitration
       0x18b4bf000 -        0x18b7ddfff  com.apple.vImage (8.1 - 544.2) <B1B84588-8B57-3F98-9D50-AAC142DFF36E> /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vImage.framework/Versions/A/vImage
       0x18bcda000 -        0x18bce7fff  com.apple.OpenDirectory (11.2 - 230.40.1) <C509DC91-F994-34B7-A5C5-A108E7DA5E4E> /System/Library/Frameworks/OpenDirectory.framework/Versions/A/OpenDirectory
       0x18bce8000 -        0x18bd07fff  com.apple.CFOpenDirectory (11.2 - 230.40.1) <A5449895-6129-3BDF-864B-49ACA82E3052> /System/Library/Frameworks/OpenDirectory.framework/Versions/A/Frameworks/CFOpenDirectory.framework/Versions/A/CFOpenDirectory
       0x18bd08000 -        0x18bd10fff  com.apple.CoreServices.FSEvents (1290.40.2 - 1290.40.2) <72CF142E-3792-318C-B2D6-B60C5E219312> /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/FSEvents.framework/Versions/A/FSEvents
       0x18bd11000 -        0x18bd35fff  com.apple.coreservices.SharedFileList (144 - 144) <E4152CCC-4A03-3959-B788-D7DD1ADFF8A6> /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/SharedFileList.framework/Versions/A/SharedFileList
       0x18bd36000 -        0x18bd38fff  libapp_launch_measurement.dylib (14.1) <A5637442-ADCB-30F0-AAB4-72FA1C5E3811> /usr/lib/libapp_launch_measurement.dylib
       0x18bd39000 -        0x18bd7cfff  com.apple.CoreAutoLayout (1.0 - 21.10.1) <6026D662-F75F-3C82-8C80-F6CEBF6369BF> /System/Library/PrivateFrameworks/CoreAutoLayout.framework/Versions/A/CoreAutoLayout
       0x18bd7d000 -        0x18be68fff  libxml2.2.dylib (34.9) <E170FFEE-EB9F-3252-9B16-4A47FBAC425A> /usr/lib/libxml2.2.dylib
       0x18ccb9000 -        0x18cccafff  libsystem_containermanager.dylib (318.80.2) <0B742EA4-AFA2-36B6-AB4B-2F8ACA7211AA> /usr/lib/system/libsystem_containermanager.dylib
       0x18cccb000 -        0x18ccdcfff  com.apple.IOSurface (289.3 - 289.3) <64E3394D-C908-378C-B5CE-B89C6BE61E9F> /System/Library/Frameworks/IOSurface.framework/Versions/A/IOSurface
       0x18ccdd000 -        0x18cce6fff  com.apple.IOAccelerator (439.52 - 439.52) <2995471C-4A7A-342A-B702-496519D138E2> /System/Library/PrivateFrameworks/IOAccelerator.framework/Versions/A/IOAccelerator
       0x18cce7000 -        0x18cdddfff  com.apple.Metal (244.32.7 - 244.32.7) <E8BC8E6D-4359-3A04-87CB-6F0D4F8F5C4B> /System/Library/Frameworks/Metal.framework/Versions/A/Metal
       0x18d8e4000 -        0x18d93bfff  com.apple.MetalPerformanceShaders.MPSCore (1.0 - 1) <F07355C4-C893-3534-B74E-DD5FBCBEC76C> /System/Library/Frameworks/MetalPerformanceShaders.framework/Versions/A/Frameworks/MPSCore.framework/Versions/A/MPSCore
       0x18d93c000 -        0x18d940fff  libsystem_configuration.dylib (1109.60.2) <AEC5E654-A5B8-343E-80B7-27D5D0D856D9> /usr/lib/system/libsystem_configuration.dylib
       0x18d941000 -        0x18d945fff  libsystem_sandbox.dylib (1441.60.4) <D7CDDE27-978E-3511-AE6F-296D901290B2> /usr/lib/system/libsystem_sandbox.dylib
       0x18d946000 -        0x18d947fff  com.apple.AggregateDictionary (1.0 - 1) <516D38F6-E0E1-36B4-AC96-E5079ECC6ED4> /System/Library/PrivateFrameworks/AggregateDictionary.framework/Versions/A/AggregateDictionary
       0x18d948000 -        0x18d94bfff  com.apple.AppleSystemInfo (3.1.5 - 3.1.5) <E6509790-A434-3A6A-AF9E-EA1FDBF15F6A> /System/Library/PrivateFrameworks/AppleSystemInfo.framework/Versions/A/AppleSystemInfo
       0x18d94c000 -        0x18d94dfff  liblangid.dylib (136) <12979BA7-28E3-3E74-AC24-65166A921235> /usr/lib/liblangid.dylib
       0x18d94e000 -        0x18d9dcfff  com.apple.CoreNLP (1.0 - 245.1) <92E28F08-9AB8-3B02-A889-677A716E393C> /System/Library/PrivateFrameworks/CoreNLP.framework/Versions/A/CoreNLP
       0x18d9dd000 -        0x18d9e4fff  com.apple.LinguisticData (1.0 - 399) <2B3E7B26-D669-38C0-9B7F-FFB2E94BC23B> /System/Library/PrivateFrameworks/LinguisticData.framework/Versions/A/LinguisticData
       0x18d9e5000 -        0x18de9afff  libBNNS.dylib (288.80.1) <695BEB14-BA36-3386-8C72-A219A73C9601> /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBNNS.dylib
       0x18de9b000 -        0x18df4dfff  libvDSP.dylib (760.40.6) <9FAC0A5B-0CD5-3999-815A-3C663EC71F65> /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libvDSP.dylib
       0x18df4e000 -        0x18df60fff  com.apple.CoreEmoji (1.0 - 128) <0FCD33E9-8DC4-3FB1-86DC-8ECA2FA10E37> /System/Library/PrivateFrameworks/CoreEmoji.framework/Versions/A/CoreEmoji
       0x18df61000 -        0x18df6bfff  com.apple.IOMobileFramebuffer (343.0.0 - 343.0.0) <57F4592F-D6FA-3ED9-ACEF-B1D7A7359DC1> /System/Library/PrivateFrameworks/IOMobileFramebuffer.framework/Versions/A/IOMobileFramebuffer
       0x18e26b000 -        0x18e2f0fff  com.apple.securityfoundation (6.0 - 55240.40.4) <6D4E864F-4AAC-31AF-85AF-C308528C0F0B> /System/Library/Frameworks/SecurityFoundation.framework/Versions/A/SecurityFoundation
       0x18e2f1000 -        0x18e2fafff  com.apple.coreservices.BackgroundTaskManagement (1.0 - 104) <8981AD5E-DC12-3B54-BAC0-E9E113C4B1A0> /System/Library/PrivateFrameworks/BackgroundTaskManagement.framework/Versions/A/BackgroundTaskManagement
       0x18e2fb000 -        0x18e300fff  com.apple.xpc.ServiceManagement (1.0 - 1) <7B4325ED-9CF7-3D9E-A39C-F0AE7819F0C0> /System/Library/Frameworks/ServiceManagement.framework/Versions/A/ServiceManagement
       0x18e301000 -        0x18e303fff  libquarantine.dylib (119.40.2) <66942A5C-57B3-3524-BA49-0F2DA4A584D1> /usr/lib/system/libquarantine.dylib
       0x18e304000 -        0x18e313fff  libCheckFix.dylib (31) <05E93C9D-45F9-3758-95FD-481E5EA2D5EF> /usr/lib/libCheckFix.dylib
       0x1913f9000 -        0x19140cfff  libsasl2.2.dylib (214) <D2C32F92-0728-3C67-B774-5CFC95F83497> /usr/lib/libsasl2.2.dylib
       0x191cb4000 -        0x191cc4fff  com.apple.Kerberos (3.0 - 1) <862CCDF8-B5F2-3EDE-B728-B62CE0F158F7> /System/Library/Frameworks/Kerberos.framework/Versions/A/Kerberos
       0x191cc5000 -        0x191d0dfff  com.apple.GSS (4.0 - 2.0) <F4CC9D06-2046-3621-B654-96E7575115ED> /System/Library/Frameworks/GSS.framework/Versions/A/GSS
       0x191d0e000 -        0x191d1dfff  com.apple.CommonAuth (4.0 - 2.0) <DEE913BB-9741-3BB8-933A-64623CBCCC70> /System/Library/PrivateFrameworks/CommonAuth.framework/Versions/A/CommonAuth
       0x191eea000 -        0x191eeafff  liblaunch.dylib (2038.80.3) <327A64BF-8C2C-303D-91E4-70A168C5D87F> /usr/lib/system/liblaunch.dylib
       0x191eec000 -        0x191efdfff  libffi.dylib (27) <92403FAB-6424-3AA3-ADB4-98FCD46D892A> /usr/lib/libffi.dylib


title: TypeError: img should be PIL Image. Got  - PyTorch

body:
I'm trying to prepare some image data for my neural to classify. As part of the image preprocessing step, I'm applying the HOG filter in my dataset class as such:
class GetHogData(Dataset):

  def __init__(self, df, root, transform = None):
    self.df = df
    self.root = root
    self.transform = transform

  def __len__(self):
    return len(self.df)

  def __getitem__(self, idx):
    if torch.is_tensor(idx):
      idx = idx.tolist()

    img_path = os.path.join(self.root, self.df.iloc[idx, 0])
    # image = Image.open(img_path)
    image = cv2.imread(img_path)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    label = self.df.iloc[idx, 1]

    if self.transform:
      image = self.transform(image)

    hog_, hog_image = hog(
        image,
        orientations = 9,
        pixels_per_cell = (14,14),
        cells_per_block = (2,2),
        block_norm = "L1")

    image = np.transpose(image, (2, 0, 1))

    img_hog_lbl = {
        "image" : torch.tensor(image, dtype = torch.float32),
        "label" : torch.tensor(label, dtype = torch.long),
        "hog": torch.tensor(hog_, dtype = torch.float32)
    }
    return img_hog_lbl

After this, I define my train and validation transformation as such:
# Image mean and standard dev

img_mean = [0.485, 0.456, 0.406]
img_std = [0.229, 0.224, 0.225]

train_trans = transforms.Compose([
        transforms.RandomResizedCrop(224),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize(img_mean, img_std)
    ])

test_trans = transforms.Compose([
        transforms.Resize(256),
        transforms.CenterCrop(224),
        transforms.ToTensor(),
        transforms.Normalize(img_mean, img_std)
    ])

and finally, I create the loaders as such:
train_img = GetHogData(df = train_lab, root = "/content/train", transform = train_trans)
test_img = GetHogData(df = test_lab ,root = "/content/test", transform = test_trans)

However, when I attempt to preview the training image with test_img[1]  I get the error:
TypeError                                 Traceback (most recent call last)
<ipython-input-132-b9a9394eb1e0> in <module>()
----> 1 test_img[1]

5 frames
/usr/local/lib/python3.7/dist-packages/torchvision/transforms/functional_pil.py in resize(img, size, interpolation)
    207 def resize(img, size, interpolation=Image.BILINEAR):
    208     if not _is_pil_image(img):
--> 209         raise TypeError('img should be PIL Image. Got {}'.format(type(img)))
    210     if not (isinstance(size, int) or (isinstance(size, Sequence) and len(size) in (1, 2))):
    211         raise TypeError('Got inappropriate size arg: {}'.format(size))

TypeError: img should be PIL Image. Got <class 'numpy.ndarray'>

I've tried to add transforms.ToPILImage() to my transforms by doing:
# Image mean and standard dev

img_mean = [0.485, 0.456, 0.406]
img_std = [0.229, 0.224, 0.225]

train_trans = transforms.Compose([
        transforms.ToPILImage(),
        transforms.RandomResizedCrop(224),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize(img_mean, img_std)
    ])

test_trans = transforms.Compose([
        transforms.ToPILImage(),
        transforms.Resize(256),
        transforms.CenterCrop(224),
        transforms.ToTensor(),
        transforms.Normalize(img_mean, img_std)
    ])

but I got the error:
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-135-b9a9394eb1e0> in <module>()
----> 1 test_img[1]

1 frames
<ipython-input-129-8551c2e76038> in __getitem__(self, idx)
     27         pixels_per_cell = (14,14),
     28         cells_per_block = (2,2),
---> 29         block_norm = "L1")
     30
     31     image = np.transpose(image, (2, 0, 1))

/usr/local/lib/python3.7/dist-packages/skimage/feature/_hog.py in hog(image, orientations, pixels_per_cell, cells_per_block, block_norm, visualize, transform_sqrt, feature_vector, multichannel)
    273     n_blocks_col = (n_cells_col - b_col) + 1
    274     normalized_blocks = np.zeros((n_blocks_row, n_blocks_col,
--> 275                                   b_row, b_col, orientations))
    276
    277     for r in range(n_blocks_row):

ValueError: negative dimensions are not allowed

Does anybody have any ideas? Thanks in advance!
Edit - New Error:
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-154-b9a9394eb1e0> in <module>()
----> 1 test_img[1]

<ipython-input-151-8551c2e76038> in __getitem__(self, idx)
     27         pixels_per_cell = (14,14),
     28         cells_per_block = (2,2),
---> 29         block_norm = "L1")
     30
     31     image = np.transpose(image, (2, 0, 1))

ValueError: too many values to unpack (expected 2)


title: Custom Loss Function becomes zero when backpropagated

body:
I am trying to write my own custom loss function that is based on the false positive and negative rates. I made a dummy code so you can check the first 2 defenitions as well. I added the rest, so you can see how it is implemented. However, still somewhere the gradient turns out to be zero. What is now the step where the gradient turns zero, or how can I check this? Please I would like to know how I can fix this :).
I tried providing you with more information so you can play around as well, but if you miss anything please do let me know!

The gradient stays True during every step. However, still during the training of the model the loss is not updated, therefore the NN does not train.

y = Variable(torch.tensor((0, 0, 0, 1, 1,1), dtype=torch.float), requires_grad = True)
y_pred = Variable(torch.tensor((0.333, 0.2, 0.01, 0.99, 0.49, 0.51), dtype=torch.float), requires_grad = True)
x = Variable(torch.tensor((0, 0, 0, 1, 1,1), dtype=torch.float), requires_grad = True)
x_pred = Variable(torch.tensor((0.55, 0.25, 0.01, 0.99, 0.65, 0.51), dtype=torch.float), requires_grad = True)

def binary_y_pred(y_pred):
    y_pred.register_hook(lambda grad: print(grad))
    y_pred = y_pred+torch.tensor(0.5, requires_grad=True, dtype=torch.float)
    y_pred = y_pred.pow(5)  # this is my way working around using torch.where()
    y_pred = y_pred.pow(10)
    y_pred = y_pred.pow(15)
    m = nn.Sigmoid()
    y_pred = m(y_pred)
    y_pred = y_pred-torch.tensor(0.5, requires_grad=True, dtype=torch.float)
    y_pred = y_pred*2
    y_pred.register_hook(lambda grad: print(grad))
    return y_pred

def confusion_matrix(y_pred, y):
    TP = torch.sum(y*y_pred)
    TN = torch.sum((1-y)*(1-y_pred))
    FP = torch.sum((1-y)*y_pred)
    FN = torch.sum(y*(1-y_pred))

    k_eps = torch.tensor(1e-12, requires_grad=True, dtype=torch.float)
    FN_rate = FN/(TP + FN + k_eps)
    FP_rate = FP/(TN + FP + k_eps)

    return FN_rate, FP_rate

def dif_rate(FN_rate_y, FN_rate_x):
    dif = (FN_rate_y - FN_rate_x).pow(2)
    return dif

def custom_loss_function(y_pred, y, x_pred, x):
    y_pred = binary_y_pred(y_pred)
    FN_rate_y, FP_rate_y = confusion_matrix(y_pred, y)

    x_pred= binary_y_pred(x_pred)
    FN_rate_x, FP_rate_x = confusion_matrix(x_pred, x)

    FN_dif = dif_rate(FN_rate_y, FN_rate_x)
    FP_dif = dif_rate(FP_rate_y, FP_rate_x)

    cost = FN_dif+FP_dif
    return cost

# I added the rest so you can see how it is implemented, but this peace does not fully run well! If you want this part to run as well, I can add more code.
class FeedforwardNeuralNetModel(nn.Module):
    def __init__(self, input_dim, hidden_dim, output_dim):
        super(FeedforwardNeuralNetModel, self).__init__()
        self.fc1 = nn.Linear(input_dim, hidden_dim)
        self.relu1 = nn.ReLU()
        self.fc2 = nn.Linear(hidden_dim, output_dim)
        self.sigmoid = nn.Sigmoid()

     def forward(self, x):
        out = self.fc1(x)
        out = self.relu1(out)
        out = self.fc2(out)
        out = self.sigmoid(out)
        return out

model = FeedforwardNeuralNetModel(input_dim, hidden_dim, output_dim)

optimizer = torch.optim.Adam(model.parameters(), lr=0.0001, betas=[0.9, 0.99], amsgrad=True)
criterion = torch.nn.BCELoss(weight=None, size_average=None, reduce=None, reduction='mean')
for epoch in range(num_epochs):

train_err = 0
for i, (samples, truths) in enumerate(train_loader):
    samples = Variable(samples)
    truths = Variable(truths)
    optimizer.zero_grad()   # Reset gradients
    outputs = model(samples)  # Do the forward pass
    loss2 = criterion(outputs, truths) # Calculate loss

    samples_y = Variable(samples_y)
    samples_x = Variable(samples_x)

    y_pred = model(samples_y)
    y = Variable(y, requires_grad=True)

    x_pred = model(samples_x)
    x= Variable(x, requires_grad=True)

    cost = custom_loss_function(y_pred, y, x_pred, x)
    loss = loss2*0+cost #checking only if cost works.
    loss.backward()
    optimizer.step()
    train_err += loss.item()
train_loss.append(train_err)

I expect the model to update during training. There is no error message.


title: 2.3 ratio between Pytorch BCEloss and my own "log" calculations

body:
I'm scripting a toy model to both practice PyTorch and GAN models, and I'm making sure I understand each step as much as possible.
That leaded me to checking my understanding of the BCEloss function, and apparently I understand it... with a ratio of 2.3.

To check the results, I write the intermediate values for Excel:

tmp1 = y_pred.tolist()  # predicted values in list (to copy/paste on Excel)
tmploss = nn.BCELoss(reduction='none')  # redefining a loss giving the whole BCEloss tensor
tmp2 = tmploss(y_pred, y_real).tolist()  # BCEloss values in list (to copy/paste Exel)

Then I copy tmp1 on Excel and calculate: -log(x) for each values, which is the BCEloss formula for y_target = y_real = 1.

Then I compare the resulting values with the values of tmp2: these values are 2.3x higher than "mine".

(Sorry, I couldn't figure out how to format tables on this site...)

Can you please tell me what is happening? I feel a PEBCAK coming :-)


title: Transforming every training points without using dataloaders

body:
I just found out that even though torchvision.dataset.MNIST accepts the transformer parameter, ...

transform = transforms.compose(
    [transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))]
)
mnist_trainset = datasets.mnist(
    root="mnist", train=True, download=True, transform=transform
)

...the value obtained from the mnist_trainset.data variable is still not transformed (please observe that the data in the range of (0, 255) should be normalised to (-1, 1) regarding the transformer's behaviour).

[102] mnist_testset.data[0].min()
tensor(0, dtype=torch.uint8)

[103] mnist_testset.data[0].max()
tensor(255, dtype=torch.uint8)

I tried calling mnist_trainset.transform over mnist_trainset.data, but the output shape is not what I intended

[104] mnist_testset.data.shape
torch.Size([10000, 28, 28])

[105] transform(mnist_testset.data).shape
torch.Size([3, 28, 28])

# Should be [10000, 28, 28] as identical to the original data.

I can use the DataLoader to load the entire training set and set the shuffling to False, but I think it's too overkilling. What is the best way to transform the entire mnist_testset using the defined transformer object, in order to obtain the intended transformed image, without having to manually transform it one-by-one?


title: Python/Pytorch - how to use Image arrays?

body:
I want to put image data in a neural network, but I am having trouble using the Image datatype.
I read my data here using Pytorch;

import torch
import torchvision
import numpy as np
from settings import Settings

class Data_Read:
    @staticmethod
    def getTrain():
        train_dataset = torchvision.datasets.ImageFolder(
            root=Settings.pathTrainImagesCopy,
            transform=torchvision.transforms.ToTensor()
        )
        train_loader = torch.utils.data.DataLoader(
            train_dataset,
            batch_size=64,
            num_workers=0,
            shuffle=True
        )
        return train_loader

    @staticmethod
    def getTest():
        test_dataset = torchvision.datasets.ImageFolder(
            root=Settings.pathTestImagesCopy,
            transform=torchvision.transforms.ToTensor()
        )
        test_loader = torch.utils.data.DataLoader(
            test_dataset,
            batch_size=64,
            num_workers=0,
            shuffle=True
        )
        return test_loader

The following code creates a single dimension column of images;

class Imagez:
    @staticmethod
    def Get(arr):
        imageData = []
        for item in arr:
            filePath = item
            img = Image.open(filePath).convert('LA')
            imageData.append(img)

        return imageData

And these methods are called from the main class as follows;

trainData = Data_Read.getTrain()
testData = Data_Read.getTest()

arrTrain = np.array(trainData.dataset.imgs)[:,0]
labelTrain = trainData.dataset.targets

arrTest = np.array(testData.dataset.imgs)[:,0]
labelTest = testData.dataset.targets

X_Train = Imagez.Get(arrTest)

I find whenever I try to use the Images datatype I in X_Train I get into trouble with error messages.
For example;

mlp = MLPClassifier(hidden_layer_sizes=(10,10,10), max_iter=1000 )
mlp.fit(X_Train, labelTrain)

Will give me this error message;

  Traceback (most recent call last):   File
  "c:\Users\hijik.vscode\extensions\ms-python.python-2019.11.50794\pythonFiles\ptvsd_launcher.py",
  line 43, in
      main(ptvsdArgs)   File "c:\Users\hijik.vscode\extensions\ms-python.python-2019.11.50794\pythonFiles\lib\python\old_ptvsd\ptvsd__main__.py",
  line 432, in main
      run()   File "c:\Users\hijik.vscode\extensions\ms-python.python-2019.11.50794\pythonFiles\lib\python\old_ptvsd\ptvsd__main__.py",
  line 316, in run_file
      runpy.run_path(target, run_name='main')   File "C:\Users\hijik\AppData\Local\Continuum\anaconda3\lib\runpy.py", line
  263, in run_path
      pkg_name=pkg_name, script_name=fname)   File "C:\Users\hijik\AppData\Local\Continuum\anaconda3\lib\runpy.py", line
  96, in _run_module_code
      mod_name, mod_spec, pkg_name, script_name)   File "C:\Users\hijik\AppData\Local\Continuum\anaconda3\lib\runpy.py", line
  85, in _run_code
      exec(code, run_globals)   File "d:\702\702-Coursework-Task-5\src\Main.py", line 74, in
      mlp.fit(X_Train, Y_TrainLabels)   File "C:\Users\hijik\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\neural_network\multilayer_perceptron.py",
  line 981, in fit
      return self._fit(X, y, incremental=(self.warm_start and   File "C:\Users\hijik\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\neural_network\multilayer_perceptron.py",
  line 323, in _fit
      X, y = self._validate_input(X, y, incremental)   File "C:\Users\hijik\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\neural_network\multilayer_perceptron.py",
  line 919, in _validate_input
      multi_output=True)   File "C:\Users\hijik\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\utils\validation.py",
  line 719, in check_X_y
      estimator=estimator)   File "C:\Users\hijik\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\utils\validation.py",
  line 496, in check_array
      array = np.asarray(array, dtype=dtype, order=order)   File "C:\Users\hijik\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\core\numeric.py",
  line 538, in asarray
      return array(a, dtype, copy=False, order=order) TypeError: int() argument must be a string, a bytes-like object or a number, not
  'Image'

I am thinking I need to convert my images to another data type. What advice would you give?

EDIT - this is the minimum reproducible error;

X_Train = []
filePath = '..\\images\\Train\\anger\\S010_004_00000014.png'
img = Image.open(filePath).convert('LA')
X_Train.append(img)
Y_TrainLabels = ["0"]
mlp = MLPClassifier(hidden_layer_sizes=(10,10,10), max_iter=1000 )
mlp.fit(X_Train, Y_TrainLabels)


title: why torchvision.models.alexnet() not work when i test the input and output?

body:
The Code is here:

import torch
from torchvision.models import alexnet

if __name__ == "__main__":
    net = alexnet()
    x = torch.rand((1, 3, 224, 224))

    for name, layer in net.named_children():
        x = layer(x)
        print(name, ' output shape:\t', x.shape)

The output is here:

features  output shape:  torch.Size([1, 256, 6, 6])
avgpool  output shape:   torch.Size([1, 256, 6, 6])

File "AlexNet.py", line 9, in <module>
    x = layer(x)
RuntimeError: size mismatch, m1: [1536 x 6], m2: [9216 x 4096]

I want to test the input and output of the AlexNet, but i failed with the 'size mismatch' error. i use the image size(3,224,224) provided by the paper of AlexNet, and i wanna get the right output. After this error, i try to fix the

self.avgpool = nn.AdaptiveAvgPool2d((6, 6))

to

self.avgpool = nn.AdaptiveAvgPool2d((1, 9216))

and i get the right output.

I really want to know what did i do wrong with the torchvision. And i also want to know the what people would do when they need to test the input and output of the cnn.

thx for your help!


title: PyTorch: how to apply another transform to an existing Dataset?

body:
This is a code example:

dataset = datasets.MNIST(root=root, train=istrain, transform=None)  #preserve raw img

print(type(dataset[0][0]))
# <class 'PIL.Image.Image'>

dataset = torch.utils.data.Subset(dataset, indices=SAMPLED_INDEX) # for resample

for ind in range(len(dataset)):
    img, label = dataset[ind] # <class 'PIL.Image.Image'> <class 'int'>/<class 'numpy.int64'>
    img.save(fp=os.path.join(saverawdir, f'{ind:02d}-{int(label):02d}.png'))

dataset.transform = transforms.Compose([
                transforms.RandomResizedCrop(image_size),
                transforms.ToTensor(),
                transforms.Normalize((0.1307,), (0.3081,))
            ])
#transform for net forwarding

print(type(dataset[0][0]))
# expected <class 'torch.Tensor'>, however it's still <class 'PIL.Image.Image'>

Since dataset is randomly resampled, I don't want to reload a new dataset with transform, but just apply transform to the already existing dataset.

Thanks for your help :D


title: Model's outputs from a sigmoid function are almost equal to 0.5 and remain unchanged

body:
So I've been training Inception-Resnet V2 on a binary image classification problem and while training I have observed that the logits do not converge either to a 0 or a 1. They just keep fluctuating around 0.5. What may seem to be the error? I am training the pretrained model for 4 epochs on a highly imbalanced dataset which is why I am also using a weighted random sampler. The batch size is 128 and the optimizer is adam with a learning rate of 0.001
device = "cuda"

epochs=4
print("======== Training for ", epochs, "epochs=============")
for epoch in range(epochs):
    total_loss = 0
    model.train()
    print("Training.......")
    print("======== EPOCH #",epoch,"=================")
    tmp_acc = 0
    for i,batch in enumerate(train_loader):

        img,label = batch["images"],batch["labels"]

        label = label.type(torch.FloatTensor)

        img,label = img.to(device),label.to(device)
        model.zero_grad()

        op,aux = model(img)

        label_cpu = label.cpu().numpy()

        op = F.sigmoid(op)

        output = op.detach().cpu().numpy()
        tmp_acc += accuracy_score(output,label_cpu)
        loss = criterion(op,label)
        total_loss = loss.item()
        loss.backward()

        adam.step()
        if(i%10==0 and i>0):
            print("STEP: ",i, "of steps ",len(train_loader))

            print("Current loss: ",total_loss/i)
            print("Training Accuracy ",tmp_acc/i)
            print("OP",op)
            print("Label",label_cpu)

    avg_loss = total_loss/len(train_loader)
    print("The loss after ",epoch," epochs is ",avg_loss)

    model.eval()
    print("Validating.....")
    tmp_accuracy = 0
    z_count,o_count=0,0
    z_count_truth,o_count_truth = 0,0

    for i,batch in enumerate(val_loader):
        img,label = batch["images"],batch["labels"]
        img = img.to(device)
        with torch.no_grad():
            op = F.sigmoid(model(img))
        op_cpu = op.detach().cpu().numpy()
        label = label.numpy()
        tmp_accuracy += accuracy_score(op_cpu,label)
        z_count += np.sum(op_cpu==0)
        o_count += np.sum(op_cpu==1)
        z_count_truth += np.sum(label==0)
        o_count_truth += np.sum(label==1)
    percent_correct_z = z_count/z_count_truth
    percent_correct_o = o_count/o_count_truth
    accuracy = tmp_accuracy/len(val_loader)
    print("Accuracy: ", "is ",accuracy)
    #print("Percent of correct zero labels ",percent_correct_z)
    #print("Percent of correct one labels ",percent_correct_o)```

The output looks like
STEP:  90 of steps  99
Current loss:  0.007694996065563626
Training Accuracy  0.5019965277777778
OP tensor([[0.4962],
        [0.4956],
        [0.4950],
        [0.4957],
        [0.4945],
        [0.4957],
        [0.4952],
        [0.4965],
        [0.4950],
        [0.4962],
        [0.4956],
        [0.4956],
        [0.4951],
        [0.4953],
        [0.4956],
        [0.4958],
        [0.4949],
        [0.4945],
        [0.4955],
        [0.4924],
        [0.4952],
        [0.4952],
        [0.4958],
        [0.4953],
        [0.4959],
        [0.4952],
        [0.4965],
        [0.4956],
        [0.4956],
        [0.4381],
        [0.4951],
        [0.4946],
        [0.4957],
        [0.4951],
        [0.4955],
        [0.4952],
        [0.4955],
        [0.4948],
        [0.4951],
        [0.4960],
        [0.4956],
        [0.4955],
        [0.4958],
        [0.4957],
        [0.4953],
        [0.4954],
        [0.4955],
        [0.4959],
        [0.4949],
        [0.4960],
        [0.4953],
        [0.4949],
        [0.4951],
        [0.4952],
        [0.4949],
        [0.4954],
        [0.4956],
        [0.4951],
        [0.4947],
        [0.4958],
        [0.4953],
        [0.4960],
        [0.4959],
        [0.4958],
        [0.4948],
        [0.4947],
        [0.4957],
        [0.4961],
        [0.4955],
        [0.4959],
        [0.4955],
        [0.4954],
        [0.4959],
        [0.4952],
        [0.4955],
        [0.4951],
        [0.4962],
        [0.4961],
        [0.4961],
        [0.4960],
        [0.4956],
        [0.4959],
        [0.4953],
        [0.4960],
        [0.4955],
        [0.4949],
        [0.4958],
        [0.4953],
        [0.4955],
        [0.4959],
        [0.4951],
        [0.4961],
        [0.4939],
        [0.4954],
        [0.4953],
        [0.4958],
        [0.4953],
        [0.4949],
        [0.4959],
        [0.4958],
        [0.4960],
        [0.4949],
        [0.4957],
        [0.4964],
        [0.4949],
        [0.4956],
        [0.4952],
        [0.4959],
        [0.4954],
        [0.4958],
        [0.4954],
        [0.4951],
        [0.4953],
        [0.4953],
        [0.4958],
        [0.4954],
        [0.4955],
        [0.4954],
        [0.4960],
        [0.4946],
        [0.4950],
        [0.4953],
        [0.4957],
        [0.4956],
        [0.4954],
        [0.4940],
        [0.4951],
        [0.4955]], device='cuda:0', grad_fn=<SigmoidBackward>)
Label [0. 1. 0. 1. 1. 0. 1. 0. 1. 1. 1. 0. 0. 0. 0. 1. 0. 1. 1. 0. 1. 0. 1. 0.
 1. 0. 0. 1. 1. 0. 0. 1. 0. 0. 0. 0. 1. 0. 0. 1. 0. 0. 1. 1. 1. 0. 1. 1.
 0. 0. 1. 1. 1. 0. 1. 0. 0. 0. 1. 1. 1. 1. 0. 1. 0. 1. 1. 0. 0. 0. 1. 1.
 0. 1. 1. 0. 0. 0. 1. 1. 0. 0. 1. 0. 1. 0. 0. 0. 1. 0. 1. 0. 0. 1. 1. 0.
 0. 1. 1. 0. 0. 1. 1. 0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 0. 0. 0. 0. 0. 0.
 1. 0. 1. 1. 0. 1. 1. 0.]

OP Tensor corresponds to the output logits
Label corresponds to the original labels


title: Pytorch convnet isn't learning

body:
I'm new to PyTorch and I'm trying to build a model for a Kaggle competition. I used a pre-trained resnet but the training and the validation loss don't decrease. I suspect I did something wrong in my implementation:
#================================================================================
class TransferResnet(nn.Module):
    def __init__(self, classes=4):
        super().__init__()
        # Use a pretrained model
        self.network = models.resnet34(pretrained=True)

        # Replace last layer
        num_ftrs = self.network.fc.in_features
        self.network.fc = nn.Sequential(nn.Linear(num_ftrs, 128),
                                    nn.ReLU(),
                                    nn.Dropout(0.50),

                                    nn.Linear(128,classes))

    def forward(self, xb):
        out = self.network(xb)
        return out

    def feed_to_network(self, batch):
        images, labels = batch
        out = self(images)
        out = F.softmax(out, dim=1)
        loss = F.cross_entropy(out, labels)

        return loss, out
#======================================================
def get_scores(labels, prediction, loss=None):
    "Return classification scores"
    accuracy = accuracy_score(labels, prediction)
    f1 = f1_score(labels, prediction,
                  average='weighted', zero_division=0)
    precision = precision_score(labels, prediction,
                                average='weighted', zero_division=0)
    recall = recall_score(labels, prediction,
                          average='weighted', zero_division=0)
    if loss:
        return [accuracy, f1, precision, recall, loss]
    else:
        return [accuracy, f1, precision, recall]

def get_predictions(model, loader):
    """This function takes a model and a data loader,
    returns the list of losses, the predictions and the labels"""
    with torch.no_grad():
        model.eval()

        losses = []
        predictions = []
        labels = []
        for batch in loader:
            loss, out = model.feed_to_network(batch)

            predictions += torch.max(out, dim=1)[1].tolist()
            labels += batch[1].tolist()
            losses.append(loss.item())

        return labels, predictions, sum(losses)/len(losses)
#=================================================================
def fit(epochs, model, train_loader, val_loader,
        opt_func=torch.optim.Adam, lr=3e-4, step_size=100):

    def get_parameter(optimizer, parameter="lr"):
        """Retrieve learning rate or parameter"""
        if parameter == 'lr':
            for param_group in optimizer.param_groups:
                return param_group['lr']

    torch.cuda.empty_cache()
    model.train()

    #Dataframe that will store the metrics
    train_metrics_df = pd.DataFrame(columns=['accuracy', 'f1', 'precision',
                                         'recall', 'loss'])
    valid_metrics_df = pd.DataFrame(columns=['accuracy', 'f1', 'precision',
                                         'recall', 'loss'])

    momentum_list = []
    lr_list = []

    optimizer = opt_func([{"params": model.network.fc.parameters(), "lr": lr},
                          {"params": model.network.layer4.parameters(), "lr": lr/2},
                          {"params": model.network.layer3.parameters(), "lr": lr/4},
                          {"params": model.network.layer2.parameters(), "lr": lr/6},
                          {"params": model.network.layer1.parameters(), "lr": lr/8},], lr)

    for epoch in range(epochs):
        # Training Phase

        train_label = []
        train_prediction = []
        train_losses = []
        for batch in tqdm(train_loader):

            loss, out = model.feed_to_network(batch)
            loss.backward()

            #momentum_list.append(get_parameter(optimizer, parameter="momentum"))
            lr_list.append(get_parameter(optimizer, parameter="lr"))

            optimizer.step()
            optimizer.zero_grad()

            #Extract labels, predictions and loss of the training set
            train_prediction += torch.max(out, dim=1)[1].tolist()
            train_label += batch[1].tolist()
            train_losses.append(loss.item())

        #Evaluation phase
        val_labels, val_predictions, val_loss = get_predictions(model, val_loader)

        train_metrics_df.loc[epoch] = get_scores(train_label,train_prediction,
                                                 loss=sum(train_losses)/len(train_losses))
        valid_metrics_df.loc[epoch] = get_scores(val_labels, val_predictions,
                                                 loss=val_loss)

        print_epoch_trainLoss = train_metrics_df.iloc[epoch]["loss"]
        print_epoch_validLoss = valid_metrics_df.iloc[epoch]["loss"]
        print_epoch_validAccu = valid_metrics_df.iloc[epoch]["accuracy"]
        print_epoch_trainAccu = train_metrics_df.iloc[epoch]["accuracy"]

        print(f"Epoch: {epoch+1}, train loss: {print_epoch_trainLoss:.2f}, "
              f"validation loss: {print_epoch_validLoss:.2f}, "
              f"validation accuracy: {print_epoch_validAccu:.2f}, "
              f"training accuracy: {print_epoch_trainAccu:.2f}, ")

        return train_metrics_df, valid_metrics_df, (momentum_list,lr_list)

All the images are normalized, cropped to proper dimensions (490x490) and some data augmentation is performed (random flip, rotations, etc...). All this code is executed on a GPU using Kaggle notebooks (my GPU is not enough for this dataset). This is my first implementation of a CNN and I do not know what I did wrong. I also tried to learn the classifier with a learning rate = 0.1, but the loss does not decrease.


title: Failed to generate adversarial examples using trained NSGA-Net PyTorch models

body:
I have used NSGA-Net neural architecture search to generate and train several architectures. I am trying to generate PGD adversarial examples using my trained PyTorch models. I tried using both Adversarial Robustness Toolbox 1.3 (ART) and torchattacks 2.4 but I get the same error.
These few lines of code describe the main functionality of my code and what I am trying to achieve here:
# net is my trained NSGA-Net PyTorch model

# Defining PGA attack

pgd_attack = PGD(net, eps=4 / 255, alpha=2 / 255, steps=3)

# Creating adversarial examples using validation data and the defined PGD attack

for images, labels in valid_data:
    images = pgd_attack(images, labels).cuda()
    outputs = net(images)

So here is what the error generally looks like:
Traceback (most recent call last):
  File "torch-attacks.py", line 296, in <module>
    main()
  File "torch-attacks.py", line 254, in main
    images = pgd_attack(images, labels).cuda()
  File "\Anaconda3\envs\GPU\lib\site-packages\torchattacks\attack.py", line 114, in __call__
    images = self.forward(*input, **kwargs)
  File "\Anaconda3\envs\GPU\lib\site-packages\torchattacks\attacks\pgd.py", line 57, in forward
    outputs = self.model(adv_images)
  File "\envs\GPU\lib\site-packages\torch\nn\modules\module.py", line 532, in __call__
    result = self.forward(*input, **kwargs)
  File "\codes\NSGA\nsga-net\models\macro_models.py", line 79, in forward
    x = self.gap(self.model(x))
  File "\Anaconda3\envs\GPU\lib\site-packages\torch\nn\modules\module.py", line 532, in __call__
    result = self.forward(*input, **kwargs)
  File "\Anaconda3\envs\GPU\lib\site-packages\torch\nn\modules\container.py", line 100, in forward
    input = module(input)
  File "\Anaconda3\envs\GPU\lib\site-packages\torch\nn\modules\module.py", line 532, in __call__
    result = self.forward(*input, **kwargs)
  File "\codes\NSGA\nsga-net\models\macro_decoder.py", line 978, in forward
    x = self.first_conv(x)
  File "\Anaconda3\envs\GPU\lib\site-packages\torch\nn\modules\module.py", line 532, in __call__
    result = self.forward(*input, **kwargs)
  File "\Anaconda3\envs\GPU\lib\site-packages\torch\nn\modules\conv.py", line 345, in forward
    return self.conv2d_forward(input, self.weight)
  File "\Anaconda3\envs\GPU\lib\site-packages\torch\nn\modules\conv.py", line 342, in conv2d_forward
    self.padding, self.dilation, self.groups)
RuntimeError: Expected object of scalar type Float but got scalar type Double for argument #2 'weight' in call to _thnn_conv2d_forward

I have used the same the code with a simple PyTorch model and it worked but I am using NSGA-Net so I haven't designed the model myself. I also tried using .float() on both the model and inputs and still got the same error.
Keep in mind that I only have access to the following files:

torch-attacks.py
macro_models.py
macro_decoder.py


title: File too short when import torch

body:
I am connecting to a GPU linux server. I successfully installed anaconda with python 3.8 and pytorch package using the command "conda install pytorch torchvision cudatoolkit=10.2 -c pytorch". It was successfully installed as well.
However, when I try to import the torch library:
import torch

I got the following error:
OSError: /koti/eeldeeb19/anaconda3/lib/python3.8/site-packages/torch/lib/../../../../libcurand.so.10: file too short

Does anyone know a solution for that?


title: How to know whether the data passed to GPU will cause CUDA out of memory or not

body:
I am using GPU to run some very large deep learning models, when I choose a batch size of 8, it can fit into the memory, but if I use a batch size of 16, it will cause CUDA out-of-memory error, and I have to kill the process.
My question is, before actually passing the data into GPU, is there a way that I could know how large the data will occupy in the GPU?
For example, the following code is about how I create a pytorch dataloader and pass each batch of the dataloader to the GPU, could I know how large it is before I call batch.to(device)
train_dataloader = DataLoader(train_data, sampler=train_sampler, batch_size=batch_size)
for step, batch in enumerate(train_dataloader):
    b_input_ids = batch[0].to(device)
    b_input_mask = batch[1].to(device)
    b_labels = batch[2].to(device)


title: customizing CNN pytorch by adding additional information before classification

body:
I am trying to make a custom CNN architecture using Pytorch. The current architecture is for text multilabel classification but I want to add the category of that text as an additional input to my CNN to help it remember from which parent category the text is coming. I thought of adding one hot vector that has all the categories.
My current code :
class CNN(nn.Module):
    """
    Convolutional Neural Model used for training the models. The total number of kernels that will be used in this
    CNN is Co * len(Ks).
    Args:
        weights_matrix: numpy.ndarray, the shape of this n-dimensional array must be (words, dims) were words is
        the number of words in the vocabulary and dims is the dimensionality of the word embeddings.
        Co (number of filters): integer, stands for channels out and it is the number of kernels of the same size that will be used.
        Hu: integer, stands for number of hidden units in the hidden layer.
        C: integer, number of units in the last layer (number of classes)
        Ks: list, list of integers specifying the size of the kernels to be used.

    """
    def __init__(self, vocab_size, emb_dim, Co, Hu, C, Ks, name = 'generic'):

        super(CNN, self).__init__()

        self.num_embeddings = vocab_size

        self.embeddings_dim = emb_dim

        self.padding_index = 0

        self.cnn_name = 'cnn_' + str(emb_dim) + '_' + str(Co) + '_' + str(Hu) + '_' + str(C) + '_' + str(Ks) + '_' + name

        self.Co = Co

        self.Hu = Hu

        self.C = C

        self.Ks = Ks

        self.embedding = nn.Embedding(self.num_embeddings, self.embeddings_dim, self.padding_index)
        self.convolutions = nn.ModuleList([nn.Conv2d(1,self.Co,(k, self.embeddings_dim)) for k in self.Ks])
        self.relu = nn.ReLU()
        self.drop_out = nn.Dropout(p=0.5)
        units = [self.Co * len(self.Ks)] + Hu

        self.linear_layers = nn.ModuleList([nn.Linear(units[k],units[k+1]) for k in range(len(units)-1)])

        self.linear_last = nn.Linear(self.Hu[-1], self.C)
        self.sigmoid = nn.Sigmoid()

     def forward(self,x):

        x = self.embedding(x)

        x = [self.relu(conv(x)).squeeze(3) for conv in self.convolutions]

        x = [F.max_pool1d(i, i.size(2)).squeeze(2) for i in x]

        x = torch.cat(x,1)

            x = linear(x)

            x = self.relu(x)

        x = self.drop_out(x)
        x = self.linear_last(x)

        x = self.sigmoid(x)

        return x

I thought of adding a Linear layer that has as input a one-hot vector and connects this layer to my neural network (concatenate the output of CNN with new layers) and AFAIK PyTorch does the backpropagation by itself.
I am new to Pytorch so if you can help me with what modifications I should add to the code or point me to any direction that can be useful. Thanks


title: Python PyTorch Pyro - Multivariate Distributions

body:
How does one sample a multivariate distribution in Pyro? I just want a (M, N) Beta distribution, but the following doesn't work:
impor torch
import pyro
with pyro.plate("theta_plate", M):
    theta = pyro.sample("theta",
                        pyro.distributions.Beta(concentration0=torch.ones(N),
                                                concentration1=torch.ones(N)))


title: How to calculate entropy of each class to measure model uncertainty on pytorch

body:
I am trying to calculate Entropy to measure model's uncertainty using MC Dropout for image classification task on pytorch,I have calculated the Entropy for each sample using the samples mean output_mean
as shown in the code below
    for images,labels in testloader:
        images = images.to(device)
        labels = labels.to(device)

        with torch.no_grad():
          output_list = []
          for i in range(T):
            output_list.append(torch.unsqueeze(model(images), 0))

        #calculating samples mean
        output_mean = torch.cat(output_list, 0).mean(0) #shape (n_samples, n_classes)
        output_mean = np.asarray(output_mean.cpu())

        epsilon = sys.float_info.min
        # Calculating entropy across multiple MCD forward passes
        entropy = -np.sum(output_mean*np.log(output_mean + epsilon), axis=-1) #shape (n_samples, n_classes)

After calculating Entropy of each sample, I am trying to calculate Entropy for each class to get the model uncertainty about each one of them.
Can anyone help me to get the right formula to calculate entropy .


title: Pytorch 3D Convolution Network Crashes RAM in Google Colab

body:
I am very new to DL (and coding in general). I am implementing a very simple 3D CNN based loosely on V-Net in pytorch (code below). At the moment, I can get the code to run on small test images (shape: (1,1,128,128,32), but the model will crash Colab if I input something larger (1,1,256,256,128) for example. Not sure if this is to be expected, or if there is some obvious error I am making here. Thanks in advance.
class Test3D(nn.Module):

def __init__(self):
    super(Test3D,self).__init__()
    self.input_layer = self._conv_input()
    self.conv_layer1 = self.ResidualBlock(32, 40)
    self.conv_layer2 = self.ResidualBlock(40, 48)
    self.conv_layer3 = self.ResidualBlock(48,56)
    self.conv_layer4 = self.ResidualBlock(56,48)
    self.conv_layer5 = self.ResidualBlock(48,40)
    self.conv_layer6 = self.ResidualBlock(40,32)
    self.conv_layer7 = self._conv_output()

  def _conv_input(self):
    conv_layer= nn.Sequential(
       nn.Conv3d(1, 32, kernel_size=(3, 3, 3), stride=2,padding=1)
    )
    return conv_layer

  def _conv_output(self):
    conv_layer= nn.Sequential(

      nn.ConvTranspose3d(32, 6, kernel_size=2, stride=2,padding=0)
    )
    return conv_layer

  def ResidualBlock(self,in_c,out_c):
    conv_layer=nn.Sequential(
        nn.Conv3d(in_c,out_c,kernel_size=(3,3,3),padding=1),
        nn.Conv3d(out_c,out_c,kernel_size=(3,3,3),padding=1),
        ContBatchNorm3d(out_c),
        nn.ReLU()

    )
    return conv_layer

  def forward(self, x):

      out = self.input_layer(x)
      print(out.shape)
      out = self.conv_layer1(out)
      print(out.shape)
      out = self.conv_layer2(out)
      print(out.shape)
      out = self.conv_layer3(out)
      print(out.shape)
      out = self.conv_layer4(out)
      print(out.shape)
      out = self.conv_layer5(out)
      print(out.shape)
      out = self.conv_layer6(out)
      print(out.shape)
      out = self.conv_layer7(out)
      print(out.shape)


title: how Run Caffe models using PyTorch as backend

body:
every one
i try do this:
import torch
import caffemodel2pytorch

model = caffemodel2pytorch.Net(
prototxt = 'VGG_ILSVRC_16_layers_deploy.prototxt',
weights = 'VGG_ILSVRC_16_layers.caffemodel',
caffe_proto = 'https://raw.githubusercontent.com/BVLC/caffe/master/src/caffe/proto/caffe.proto'
)
model.cuda()
model.eval()

it just showed the error :
if not self._IDENTIFIER_OR_NUMBER.match(result):
raise self.ParseError('Expected identifier or number, got %s.' % result)
self.NextToken()
return result

Can anyone help me fix the error? thanks.


title: Reproducibility of the results for GNN using DGL grahSAGE

body:
I'm working on a node classification problem using graphSAGE. I'm new to GNN so my code is based on the tutorials of GraphSAGE with DGL for classification task [1] and [2]. This is the code that I'm using, its a 3 layer GNN with imput size 20 and output size 2 (binary classification problem):
class GraphSAGE(nn.Module):
    def __init__(self,in_feats,n_hidden,n_classes,n_layers,
                 activation,dropout,aggregator_type):
        super(GraphSAGE, self).__init__()
        self.layers = nn.ModuleList()
        self.dropout = nn.Dropout(dropout)
        self.activation = activation

        self.layers.append(dglnn.SAGEConv(in_feats, n_hidden, aggregator_type))
        for i in range(n_layers - 1):
            self.layers.append(dglnn.SAGEConv(n_hidden, n_hidden, aggregator_type))
        self.layers.append(dglnn.SAGEConv(n_hidden, n_classes, aggregator_type))

    def forward(self, graph, inputs):
        h = self.dropout(inputs)
        for l, layer in enumerate(self.layers):
            h = layer(graph, h)
            if l != len(self.layers) - 1:
                h = self.activation(h)
                h = self.dropout(h)
        return h

modelG = GraphSAGE(in_feats=n_features, #20
                   n_hidden=16,
                   n_classes=n_labels, #2
                   n_layers=3,
                   activation=F.relu,
                   dropout=0,
                   aggregator_type='mean')

opt = torch.optim.Adam(modelG.parameters())

for epoch in range(50):
    modelG.train()

    logits = modelG(g, node_features)

    loss = F.cross_entropy(logits[train_mask], node_labels[train_mask])

    acc = evaluate(modelG, g, node_features, node_labels, valid_mask)

    opt.zero_grad()
    loss.backward()
    opt.step()

    if epoch % 5 == 0:
        print('In epoch {}, loss: {}'.format(epoch, loss),)

Every time I train the model (without changing anything), the performance changes a lot, the acurracy varies between 0.45 and 0.87. How can I guarantee the reproducibility of the results? I have tried setting the pytorch seed torch.manual_seed(), numpy seed and set the drop out to 0 but the results keep varying. Is this normal or am I missing something?


title: How to do 'same' padding in PyTorch if (n - 1) / 2 is no integer value

body: I am trying to reconstruct a neural network written in tensorflow. For the convolutional layer, they just use padding='SAME'. This doesn't exist in pytorch. I know, that I can calculate the padding with p = (n - 1) / 2 for stride=1. But what if this doesn't result in an integer value? In my case, n is 4 and I always want to achieve same padding.


title: PyTorch: defined layer was not involved in the forward propagation but influenced the loss value

body:
Recently, I met a confusing phenomenon when I used Pytorch to do simple experiments on logistic regression.
The question is when I fixed the random seed like this:
def set_seed(seed, cuda=True):
    np.random.seed(seed)
    torch.manual_seed(seed)
    if cuda:
        torch.cuda.manual_seed(seed)

and defined the following model with 2 layers:
class net(nn.Module):
    def __init__(self):
        super(net, self).__init__()
        self.hidden = nn.Linear(784, 100)
        self.output = nn.Linear(100, 10)

    def forward(self, x):
        x = self.hidden(x)
        x = self.output(x)
        return x

trained the network with:
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(net.parameters(), lr=0.1)

The orignal loss value was 0.6422, which is reproducible.
However, when I added an additional layer that was not involoved in the forward process like this:
class net(nn.Module):
    def __init__(self):
        super(net, self).__init__()
        self.hidden = nn.Linear(784, 100)
        self.output = nn.Linear(100, 10)
        self.add = nn.Linear(10,10)

    def forward(self, x):
        x = self.hidden(x)
        x = self.output(x)
        return x

The original loss value changed to 0.7431, which was not equal to the previous one and the model performance dropped simultaneously.
I really wonder the reason to this. Thank you!


title: Inconsistency in loss on SAME data for train and validation modes tensorflow

body:
I'm implementing a semantic segmentation model with images. As a good practice I tested my training pipeline with just one image and tried to over-fit that image. To my surprise, when training with the exactly the same images, the loss goes to near 0 as expected but when evaluating THE SAME IMAGES, the loss is much much higher, and it keeps going up as the training continues. So the segmentation output is garbage when training=False, but when run with training=True is works perfectly.
To be able to anyone to reproduce this I took the official segmentation tutorial and modified it a little for training a convnet from scratch and just 1 image. The model is very simple, just a sequence of Conv2D with batch normalization and Relu. The results are the following

As you see the loss and eval_loss are really different, and making inference to the image gives perfect result in training mode and in eval mode is garbage.
I know Batchnormalization behaves differently in inference time since it uses the averaged statistics calculated whilst training. Nonetheless, since we are training with just 1 same image and evaluating in the same image, this shouldn't happen right? Moreover I implemented the same architecture with the same optimizer in Pytorch and this does not happen there. With pytorch it trains and eval_loss converges to train loss
Here you can find the above mentioned https://colab.research.google.com/drive/18LipgAmKVDA86n3ljFW8X0JThVEeFf0a#scrollTo=TWDATghoRczu
and at the end also the Pytorch implementation


title: How can I use an LSTM to classify a series of vectors into two categories in Pytorch

body:
I have a series of vectors representing a signal over time. I'd like to classify parts of the signal into two categories: 1 or 0. The reason for using LSTM is that I believe the network will need knowledge of the entire signal to classify.
My problem is developing the PyTorch model. Below is the class I've come up with.
class LSTMClassifier(nn.Module):

   def __init__(self, input_dim, hidden_dim, label_size, batch_size):
       self.lstm = nn.LSTM(input_dim, hidden_dim)
       self.hidden2label = nn.Linear(hidden_dim, label_size)
       self.hidden = self.init_hidden()

   def init_hidden(self):
       return (torch.zeros(1, self.batch_size, self.hidden_dim),
               torch.zeros(1, self.batch_size, self.hidden_dim))

   def forward(self, x):
       lstm_out, self.hidden = self.lstm(x, self.hidden)
       y  = self.hidden2label(lstm_out[-1])
       log_probs = F.log_softmax(y)
       return log_probs

However this model is giving a bunch of shape errors, and I'm having trouble understanding everything going on.  I looked at this SO question first.


title: why autoencoder tutorial of pytorch changes the view of embedding layer output?

body:
As shown here in PyTorch tutorials the code for an autoencoder model is like this:
class EncoderRNN(nn.Module):
    def __init__(self, input_size, hidden_size):
        super(EncoderRNN, self).__init__()
        self.hidden_size = hidden_size

        self.embedding = nn.Embedding(input_size, hidden_size)
        self.gru = nn.GRU(hidden_size, hidden_size)

    def forward(self, input, hidden):
        embedded = self.embedding(input).view(1, 1, -1)
        output = embedded
        output, hidden = self.gru(output, hidden)
        return output, hidden

    def initHidden(self):
        return torch.zeros(1, 1, self.hidden_size, device=device)

My question is what is the reason for use of view function on output of embedding layer?


title: how to manage gpu RAM in google colab?

body:
When I run some DL models written with PyTorch I have the error:

RuntimeError: CUDA out of memory. Tried to allocate 108.00 MiB (GPU 0;
14.73 GiB total capacity; 13.68 GiB already allocated; 11.88 MiB free; 13.78 GiB reserved in total by PyTorch).

It's happened when I try to track some bugs and run cell over and over.
Are there some lifehacks to avoid it?
Here is an example of code (I marked out cells in google colab):
def train(...):
  ...
  assert False
  ...

model = SegNet().to(device)

max_epochs = 20
optim = torch.optim.Adam(model.parameters(), lr=10e-5)
train(model, optim, bce_loss, max_epochs, data_tr, data_val)

I found operator del so I could make
del model

after used model. But how I can manage memory using after manual or some error assert?


title: understanding how PyTorch Linear works

body:
I am considering the sample code from the documentation:
import torch
from torch import nn
#
m = nn.Linear(20, 30)
input = torch.randn(128, 20)
output = m(input)
print(output.size())

The output is :
torch.Size([128, 30])

The constructor of Linear is :
def __init__(self, in_features: int, out_features: int, bias: bool = True) -> None:

This is consistent with the way the instance is created, i.e.:
m = nn.Linear(20, 30)

However, when m is used, it receives a tensor
output = m(input)

as input. I do not understand why. Where is this tensor defined in the source code?


title: Scale down image represented in a tensor

body:
I use the MNIST dataset to learn Pytorch.
This is from the documentation to get a picture.
import torch.nn.functional as F
import torch
from torchvision import datasets, transforms

Tensor comes from the torchvision dataset.
# Create prediction
images, labels = next(iter(trainloader))
images[0].shape

This is the Tensor:
tensor([[-1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000],
        [-1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000],
        [-1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000],
        [-1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000],
        [-1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -0.2549, -0.5059, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000],
        [-1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -0.8667, -0.3961,  0.2471,  0.6784,  0.9922,  0.8824,  0.3882,
         -0.8353, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000],
        [-1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -0.5922,
          0.3490,  0.6235,  0.9922,  0.9922,  0.9922,  0.9922,  0.9922,  0.9922,
         -0.2235, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000],
        [-1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -0.3647,  0.9451,
          0.9922,  0.9922,  0.9922,  0.9922,  0.9922,  0.9922,  0.8510,  0.9922,
          0.8745, -0.3725, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000],
        [-1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -0.0118,  0.9922,
          0.9922,  0.9922,  0.9843,  0.8902,  0.3255, -0.1922, -0.6314,  0.7725,
          0.9922,  0.7804, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000],
        [-1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,  0.4431,  0.9922,
          0.9922,  0.7725, -0.1608, -1.0000, -1.0000, -1.0000, -1.0000,  0.6235,
          0.9922,  0.7804, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000],
        [-1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -0.0745,  0.9922,
          0.7333, -0.7020, -1.0000, -1.0000, -1.0000, -1.0000, -0.3020,  0.9843,
          0.9922,  0.7804, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000],
        [-1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -0.8980,  0.2078,
         -0.0902, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -0.2863,  0.9922,
          0.9922,  0.7804, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000],
        [-1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,  0.0039,  0.9922,
          0.9922,  0.7804, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000],
        [-1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -0.8275,  0.8275,  0.9922,
          0.9922, -0.0118, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000],
        [-1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,  0.5765,  0.9922,  1.0000,
          0.9529, -0.3725, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000],
        [-1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -0.9922, -0.3804,  0.9765,  0.9922,  0.9922,
          0.0039, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000],
        [-1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -0.8667,  0.0275,  0.9922,  0.9922,  0.9922,  0.9922,
         -0.4667, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000],
        [-1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
          0.0039,  0.8980,  0.9137,  0.9922,  0.9922,  0.9922,  0.9922,  0.9922,
          0.9216,  0.8980,  0.8980,  0.8980,  0.8980,  0.8980,  0.6314, -0.1843,
         -0.6941, -1.0000, -1.0000, -1.0000],
        [-1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
          0.6784,  0.9922,  0.9922,  0.9922,  0.9922,  0.9922,  0.9922,  0.9922,
          0.9922,  1.0000,  0.9922,  0.9922,  0.9922,  0.9922,  0.9922,  0.9922,
          0.8353,  0.1451, -1.0000, -1.0000],
        [-1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
          0.6784,  0.9922,  0.9922,  0.9922,  0.9922,  0.9922,  0.9922,  0.9922,
          0.9922,  0.9922,  0.9922,  0.9922,  0.9922,  0.9922,  0.8745,  0.5059,
          0.4196, -0.5686, -1.0000, -1.0000],
        [-1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -0.1765,  0.9216,  0.9922,  0.9765,  0.6706,  0.6706,  0.2863, -0.4118,
         -0.4118, -0.4118, -0.4118, -0.4118, -0.4118, -0.4118, -0.5529, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000],
        [-1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -0.4275, -0.2471, -0.2863, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000],
        [-1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000],
        [-1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000],
        [-1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000],
        [-1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000],
        [-1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000],
        [-1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
         -1.0000, -1.0000, -1.0000, -1.0000]])

Out[181]: torch.Size([1, 28, 28])

I want to scale the image down to a 14x14 picture, so I guess I need a torch.Size([1, 14, 14])
I tried this, but it results in a different format:
F.interpolate(images[0], 14).shape

Out[184]: torch.Size([1, 28, 14])

I expected this to work, but it results in an error:
F.interpolate(images[0], (14, 14))

ValueError: size shape must match input shape. Input is 1D, size is 2

Does anyone how to to get my desired result?


title: module '__main__' has no attribute '__spec__' in jupyter

body:
I run this code in Jupyter notebook but received the error module 'main' has no attribute 'spec' do you know what should I do? do you know what this error caused? because of PyTorch or related to Jupyter notebook?
def main():
    device = 'cuda:0' if torch.cuda.is_available() else 'cpu'
    train_dataset = VehicleDataset(mode='train')
    test_dataset = VehicleDataset(mode='test')
    train_loader = DataLoader(dataset=train_dataset, batch_size=20, shuffle=False, num_workers=4)
    test_loader = DataLoader(dataset=test_dataset, batch_size=20, shuffle=False, num_workers=4)
    # hyper-parameters
    num_epochs = 20
    lr = 0.0005
    model = VehicleLSTM(
        input_size=30,
        hidden_size=256,
        output_size=2,
        num_layers=1,
        dropout=0.1,
        device=device
    ).to(device)
    mse_loss = nn.MSELoss()
    l1_loss = nn.L1Loss()
    optim = Adam(model.parameters(), lr=lr)
    train_loss = []
    test_loss = []
    for epoch in range(num_epochs):
        model.train()
        epoch_loss = 0
        for n_batch, (in_batch, label) in enumerate(train_loader):
            in_batch, label = in_batch.to(device), label.to(device)
            pred = model(in_batch)
            loss = mse_loss(pred, label)
            epoch_loss += loss.item()
            optim.zero_grad()
            loss.backward()
            optim.step()
        train_loss.append(epoch_loss)
        l1_err, l2_err = 0, 0
        lateral_loss, long_loss = 0, 0
        losses = []
        model.eval()
        with torch.no_grad():
            for n_batch, (in_batch, label) in enumerate(test_loader):
                if n_batch == 1:
                    in_batch, label = in_batch.to(device), label.to(device)
                    pred = model.test(in_batch)
                    if epoch == num_epochs - 1 and n_batch == 1:
                        print('pred: ', pred)
                        print('label: ', label)

                    l1_err += l1_loss(pred, label).item()
                    l2_err += mse_loss(pred, label).item()
                    lateral_loss += mse_loss(pred[:,:,0], label[:,:,0]).item()
                    long_loss += mse_loss(pred[:,:,1], label[:,:,1]).item()
        test_loss.append(l2_err)
if __name__ == "__main__":
    main()

do you know what should I do? when I run those in the separate cell I receive this error :
TypeError: Caught TypeError in DataLoader worker process 0.
Original Traceback (most recent call last):
  File "C:\Users\f\Anaconda3\lib\site-packages\torch\utils\data\_utils\worker.py", line 185, in _worker_loop
    data = fetcher.fetch(index)
  File "C:\Users\f\Anaconda3\lib\site-packages\torch\utils\data\_utils\fetch.py", line 47, in fetch
    return self.collate_fn(data)
  File "C:\Users\f\Anaconda3\lib\site-packages\torch\utils\data\_utils\collate.py", line 86, in default_collate
    raise TypeError(default_collate_err_msg_format.format(elem_type))

TypeError: default_collate: batch must contain tensors, numpy arrays, numbers, dicts or lists; found <class 'NoneType'>


title: Python 3.7 Device or resource busy: '.nfs000000000000ad1e000047d3'

body:
I have a python script that I run on a remote Linux server to train a CNN in pytorch .
In the last few days I got this error when I run the script :
 File "/usr/lib/python3.7/multiprocessing/util.py", line 265, in _run_finalizers
    finalizer()
  File "/usr/lib/python3.7/multiprocessing/util.py", line 189, in __call__
    res = self._callback(*self._args, **self._kwargs)
  File "/home/user/virtualenvs/classifier/lib/python3.7/shutil.py", line 491, in rmtree
    _rmtree_safe_fd(fd, path, onerror)
  File "/home/user/virtualenvs/classifier/lib/python3.7/shutil.py", line 449, in _rmtree_safe_fd
    onerror(os.unlink, fullname, sys.exc_info())
  File "/home/user/virtualenvs/classifier/lib/python3.7/shutil.py", line 447, in _rmtree_safe_fd
    os.unlink(entry.name, dir_fd=topfd)
OSError: [Errno 16] Device or resource busy: '.nfs000000000000ad1e000047d3'

I also found it strange that after running the Python script, a lot of pymp* files were created in the script folder, which was not the case a week ago.
thanks in advance for your help.
I run my code on a virtual enviroment


title: PyTorch: batches of training set from two datasets

body:
So, I need to build the training set for my network with batches of N images, where the N/2 are from the first dataset and N/2 from the other one. I've thought about using batch_sampler parameter of Dataloader, and created this class:
def chunk(indices, chunk_size):
    return torch.split(torch.tensor(indices), chunk_size)

class MixedBatch(Sampler):
    def __init__(self, dataset, batch_size, split_index):
        self.first_half_indices = list(range(split_index))
        self.second_half_indices = list(range(split_index, len(dataset)))
        self.batch_size = batch_size

    def __iter__(self):
        random.shuffle(self.first_half_indices)
        random.shuffle(self.second_half_indices)
        first_half_batches = chunk(self.first_half_indices, self.batch_size//2)
        second_half_batches = chunk(self.second_half_indices, self.batch_size//2)

        mixed = []
        for i, j in zip(first_half_batches, second_half_batches):
            mixed.append((torch.cat((i, j))))
        mixed = torch.cat(mixed)

        combined = list(torch.split(mixed, self.batch_size))

        # combined = list(first_half_batches + second_half_batches)
        combined = [batch.tolist() for batch in combined]
        random.shuffle(combined)
        return iter(combined)

    def __len__(self):
        return (len(self.first_half_indices) + len(self.second_half_indices)) // self.batch_size

But it returned me the error: AttributeError: 'SequentialSampler' object has no attribute 'set_epoch'
Any other way I can do this, maybe using sampler=DistributeSampler?
Thanks so much!


title: Pytorch Crowd Counting - CRSNet "Unable to open object (object doesn't exist)"

body:
This is a question related to pytorch CRSNet for crowd counting training.
When I run the train.py, this error block happens. Could anyone solve the problem or tell the reason?
(pytorch) C:\Users\pcp25\Downloads\PETS2009_copy>python train.py part_A_train.json part_A_val.json 0 0
epoch 0, processed 0 samples, lr 0.0000001000
Epoch: [0][0/1200]      Time 1.633 (1.633)      Data 0.043 (0.043)      Loss 28.0382 (28.0382)
Epoch: [0][30/1200]     Time 0.537 (0.561)      Data 0.032 (0.029)      Loss 22.3519 (514.1272)
Epoch: [0][60/1200]     Time 0.633 (0.535)      Data 0.030 (0.027)      Loss 173.2239 (516.8844)
Traceback (most recent call last):
  File "train.py", line 226, in <module>
    main()
  File "train.py", line 91, in main
    train(train_list, model, criterion, optimizer, epoch)
  File "train.py", line 129, in train
    for i, (img, target) in enumerate(train_loader):
  File "C:\Users\pcp25\anaconda3\envs\pytorch\lib\site-packages\torch\utils\data\dataloader.py", line 435, in __next__
    data = self._next_data()
  File "C:\Users\pcp25\anaconda3\envs\pytorch\lib\site-packages\torch\utils\data\dataloader.py", line 475, in _next_data
    data = self._dataset_fetcher.fetch(index)  # may raise StopIteration
  File "C:\Users\pcp25\anaconda3\envs\pytorch\lib\site-packages\torch\utils\data\_utils\fetch.py", line 44, in fetch
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "C:\Users\pcp25\anaconda3\envs\pytorch\lib\site-packages\torch\utils\data\_utils\fetch.py", line 44, in <listcomp>
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "C:\Users\pcp25\Downloads\PETS2009_copy\dataset.py", line 35, in __getitem__
    img, target = load_data(img_path, self.train)
  File "C:\Users\pcp25\Downloads\PETS2009_copy\image.py", line 13, in load_data
    target = np.asarray(gt_file['density'])
  File "h5py\_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
  File "h5py\_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
  File "C:\Users\pcp25\anaconda3\envs\pytorch\lib\site-packages\h5py\_hl\group.py", line 264, in __getitem__
    oid = h5o.open(self.id, self._e(name), lapl=self._lapl)
  File "h5py\_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
  File "h5py\_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
  File "h5py\h5o.pyx", line 190, in h5py.h5o.open
KeyError: "Unable to open object (object 'density' doesn't exist)"


title: Build a pytorch model wrap around another pytorch model

body:
Is it possible to wrap a pytorch model inside another pytorch module? I could not do it the normal way like in transfer learning (simply concatenating some more layers) because in order to get the intended value for the next 'layer', I need to wait the last layer of the first module to generate multiple outputs (say 100) and to use all those outputs to get the value for the next 'layer' (say taking the max of those outputs). I tried to define the integrated model as something like the following:
class integrated(nn.Module):
    def __init__(self):
        super(integrated, self)._init_()

    def forward(self, x):

        model = VAE(
        encoder_layer_sizes=args.encoder_layer_sizes,
        latent_size=args.latent_size,
        decoder_layer_sizes=args.decoder_layer_sizes,
        conditional=args.conditional,
        num_labels=10 if args.conditional else 0).to(device)
        device = torch.device('cpu')
        model.load_state_dict(torch.load(r'...'))  # the first model is saved somewhere else beforehand
        model.eval()

        temp = []
        for j in range(100):
                x = model(x)
                temp.append(x)

        y=max(temp)

        return y

The reason I would like to do that is the library I need to use requires the input itself to be a pytorch module. Otherwise I could simply leave the last part outside of the module.


title: Understanding the Original Transformer (Pytorch, linear layer & permutation)

body:
I am learning about a slight variant of the original transformer model (Attention is all you need) and came across a Pytorch implementation for time-series that I wish to seek some help understanding parts of it.

Why use a self.dense_shape = nn.Linear(num_time_series, d_model) to change the last dimension from num_time_series to d_model? Why not skip this linear layer and set d_model of Transformer(d_model, nheads=nheads) to be equal to num_time_series? Link

Why was the input tensor x permutated inside the basic_feature() method? Link
x = x.permute(1, 0, 2)

I believe that after permutation, the tensors are of the shape (sequence_length, batch_size, num_features)

transformer_basic.py:
From the flow-forecast Github repo
class SimpleTransformer(torch.nn.Module):
    def __init__(
            self,
            number_time_series: int,
            seq_length: int = 48,
            output_seq_len: int = None,
            d_model: int = 128,
            n_heads: int = 8,
            dropout=0.1,
            forward_dim=2048,
            sigmoid=False):
        """
        Full transformer model
        """
        super().__init__()
        if output_seq_len is None:
            output_seq_len = seq_length
        self.out_seq_len = output_seq_len
        self.mask = generate_square_subsequent_mask(seq_length)
        self.dense_shape = torch.nn.Linear(number_time_series, d_model)
        self.pe = SimplePositionalEncoding(d_model)
        self.transformer = Transformer(d_model, nhead=n_heads)
        self.final_layer = torch.nn.Linear(d_model, 1)
        self.sequence_size = seq_length
        self.tgt_mask = generate_square_subsequent_mask(output_seq_len)
        self.sigmoid = None
        if sigmoid:
            self.sigmoid = torch.nn.Sigmoid()

    def forward(self, x: torch.Tensor, t: torch.Tensor, tgt_mask=None, src_mask=None):
        if src_mask:
            x = self.encode_sequence(x[:, :-1, :], src_mask)
        else:
            x = self.encode_sequence(x[:, :-1, :], src_mask)
        return self.decode_seq(x, t, tgt_mask)

    def basic_feature(self, x: torch.Tensor):
        x = self.dense_shape(x)
        x = self.pe(x)
        x = x.permute(1, 0, 2)
        return x

    def encode_sequence(self, x, src_mask=None):
        x = self.basic_feature(x)
        x = self.transformer.encoder(x, src_mask)
        return x

    def decode_seq(self, mem, t, tgt_mask=None, view_number=None) -> torch.Tensor:
        if view_number is None:
            view_number = self.out_seq_len
        if tgt_mask is None:
            tgt_mask = self.tgt_mask
        t = self.basic_feature(t)
        x = self.transformer.decoder(t, mem, tgt_mask=tgt_mask)
        x = self.final_layer(x)
        if self.sigmoid:
            x = self.sigmoid(x)
        return x.view(-1, view_number)


title: Tensorboard - ValueError: too many values to unpack (expected 2)

body:
I have tried to use tensorboard to visualize a model. I was following the pytorch.org tutorial. Here is the code for dataloader.
writer_train = SummaryWriter('runs/training')

train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=config.train_batch_size, shuffle=True,
                                           num_workers=config.num_workers, pin_memory=True)

images, labels = next(iter(train_loader))
writer_train.graph_model(light_net, images)

and I got this error in the iter line.

images, labels = next(iter(train_loader))
ValueError: too many values to unpack (expected 2)

I have debugged the code and found this.