detach().cpu() 杀死内核

detach().cpu() kills kernel

背景
我正在尝试使用 pytorch 绘制图像噪声,但是,当我达到这一点时,内核就会死掉。我正在 Google Colab 尝试使用相同的代码,但我确实得到了结果

结果在 Google Colab

Jupyter 的结果

我不认为它与代码本身有关,但我发布了绘制网格的函数:

def Exec_ShowImgGrid(ObjTensor, NumCh=1, NumSizeData=(28,28), NumImgs=16):
    #tensor: 128(pictures at the time ) * 784 (28*28)
    Objdata= ObjTensor.detach().cpu().view(-1,NumCh,*NumSizeData) #128 *1 *28*28 
    Objgrid= make_grid(Objdata[:NumCh],nrow=4).permute(1,2,0) #1*28*28 = 28*28*1 #Mathplot library isnt the saame as pytorch, we are accomodating the args
    Objpyplot.imshow(Objgrid)
    Objpyplot.show()

我设置了一个 pdb,我注意到它没有 运行 Objdata 行,所以我假设它与 detach() 有关。cpu()

这些是所用环境中的库,我认为这可能是罪魁祸首

name: GPUBase
channels:
  - pytorch
  - defaults
dependencies:
  - argon2-cffi=21.3.0=pyhd3eb1b0_0
  - argon2-cffi-bindings=21.2.0=py39h2bbff1b_0
  - async_generator=1.10=pyhd3eb1b0_0
  - attrs=21.4.0=pyhd3eb1b0_0
  - backcall=0.2.0=pyhd3eb1b0_0
  - blas=1.0=mkl
  - bleach=4.1.0=pyhd3eb1b0_0
  - ca-certificates=2021.10.26=haa95532_4
  - certifi=2021.10.8=py39haa95532_2
  - cffi=1.15.0=py39h2bbff1b_1
  - colorama=0.4.4=pyhd3eb1b0_0
  - cpuonly=2.0=0
  - cudatoolkit=11.3.1=h59b6b97_2
  - debugpy=1.5.1=py39hd77b12b_0
  - decorator=5.1.1=pyhd3eb1b0_0
  - defusedxml=0.7.1=pyhd3eb1b0_0
  - entrypoints=0.3=py39haa95532_0
  - freetype=2.10.4=hd328e21_0
  - importlib_metadata=4.8.2=hd3eb1b0_0
  - intel-openmp=2021.4.0=haa95532_3556
  - ipykernel=6.4.1=py39haa95532_1
  - ipython=7.31.1=py39haa95532_0
  - ipython_genutils=0.2.0=pyhd3eb1b0_1
  - jedi=0.18.1=py39haa95532_1
  - jinja2=3.0.2=pyhd3eb1b0_0
  - jpeg=9d=h2bbff1b_0
  - jsonschema=3.2.0=pyhd3eb1b0_2
  - jupyter_client=7.1.2=pyhd3eb1b0_0
  - jupyter_core=4.9.1=py39haa95532_0
  - jupyterlab_pygments=0.1.2=py_0
  - libpng=1.6.37=h2a8f88b_0
  - libtiff=4.2.0=hd0e1b90_0
  - libuv=1.40.0=he774522_0
  - libwebp=1.2.0=h2bbff1b_0
  - lz4-c=1.9.3=h2bbff1b_1
  - markupsafe=2.0.1=py39h2bbff1b_0
  - matplotlib-inline=0.1.2=pyhd3eb1b0_2
  - mistune=0.8.4=py39h2bbff1b_1000
  - mkl=2021.4.0=haa95532_640
  - mkl-service=2.4.0=py39h2bbff1b_0
  - mkl_fft=1.3.1=py39h277e83a_0
  - mkl_random=1.2.2=py39hf11a4ad_0
  - nbclient=0.5.3=pyhd3eb1b0_0
  - nbconvert=6.1.0=py39haa95532_0
  - nbformat=5.1.3=pyhd3eb1b0_0
  - nest-asyncio=1.5.1=pyhd3eb1b0_0
  - notebook=6.4.8=py39haa95532_0
  - numpy-base=1.21.5=py39hc2deb75_0
  - olefile=0.46=pyhd3eb1b0_0
  - openssl=1.1.1m=h2bbff1b_0
  - packaging=21.3=pyhd3eb1b0_0
  - pandocfilters=1.5.0=pyhd3eb1b0_0
  - parso=0.8.3=pyhd3eb1b0_0
  - pickleshare=0.7.5=pyhd3eb1b0_1003
  - pip=21.2.4=py39haa95532_0
  - prometheus_client=0.13.1=pyhd3eb1b0_0
  - prompt-toolkit=3.0.20=pyhd3eb1b0_0
  - pycparser=2.21=pyhd3eb1b0_0
  - pygments=2.11.2=pyhd3eb1b0_0
  - pyrsistent=0.18.0=py39h196d8e1_0
  - python=3.9.7=h6244533_1
  - python-dateutil=2.8.2=pyhd3eb1b0_0
  - pytorch-mutex=1.0=cpu
  - pywin32=302=py39h827c3e9_1
  - pywinpty=2.0.2=py39h5da7b33_0
  - pyzmq=22.3.0=py39hd77b12b_2
  - send2trash=1.8.0=pyhd3eb1b0_1
  - setuptools=58.0.4=py39haa95532_0
  - six=1.16.0=pyhd3eb1b0_1
  - sqlite=3.37.2=h2bbff1b_0
  - terminado=0.13.1=py39haa95532_0
  - testpath=0.5.0=pyhd3eb1b0_0
  - tk=8.6.11=h2bbff1b_0
  - tornado=6.1=py39h2bbff1b_0
  - traitlets=5.1.1=pyhd3eb1b0_0
  - typing_extensions=3.10.0.2=pyh06a4308_0
  - tzdata=2021e=hda174b7_0
  - vc=14.2=h21ff451_1
  - vs2015_runtime=14.27.29016=h5e58377_2
  - wcwidth=0.2.5=pyhd3eb1b0_0
  - wheel=0.37.1=pyhd3eb1b0_0
  - wincertstore=0.2=py39haa95532_2
  - winpty=0.4.3=4
  - xz=5.2.5=h62dcd97_0
  - zipp=3.7.0=pyhd3eb1b0_0
  - zlib=1.2.11=h8cc25b3_4
  - zstd=1.4.9=h19a0ad4_0
  - pip:
    - absl-py==1.0.0
    - astunparse==1.6.3
    - cachetools==5.0.0
    - charset-normalizer==2.0.12
    - cycler==0.11.0
    - docutils==0.18.1
    - flatbuffers==2.0
    - fonttools==4.29.1
    - gast==0.5.3
    - google-auth==2.6.0
    - google-auth-oauthlib==0.4.6
    - google-pasta==0.2.0
    - grpcio==1.44.0
    - h5py==3.6.0
    - htmlmin==0.1.12
    - idna==3.3
    - imagehash==4.2.1
    - importlib-metadata==4.11.1
    - ipywidgets==7.6.5
    - joblib==1.0.1
    - jupyterlab-widgets==1.0.2
    - keras==2.8.0
    - keras-preprocessing==1.1.2
    - keyring==23.5.0
    - kiwisolver==1.3.2
    - libclang==13.0.0
    - markdown==3.3.6
    - matplot==0.1.9
    - matplotlib==3.5.1
    - missingno==0.5.0
    - multimethod==1.7
    - networkx==2.6.3
    - numpy==1.22.2
    - oauthlib==3.2.0
    - opt-einsum==3.3.0
    - pandas==1.4.1
    - pandas-profiling==3.1.0
    - phik==0.12.0
    - pillow==9.0.1
    - pkginfo==1.8.2
    - protobuf==3.19.4
    - pyasn1==0.4.8
    - pyasn1-modules==0.2.8
    - pydantic==1.9.0
    - pyloco==0.0.139
    - pyparsing==3.0.7
    - pytz==2021.3
    - pywavelets==1.2.0
    - pywin32-ctypes==0.2.0
    - pyyaml==6.0
    - readme-renderer==32.0
    - requests==2.27.1
    - requests-oauthlib==1.3.1
    - requests-toolbelt==0.9.1
    - rfc3986==2.0.0
    - rsa==4.8
    - scikit-learn==1.0.2
    - scipy==1.8.0
    - seaborn==0.11.2
    - simplewebsocketserver==0.1.1
    - tangled-up-in-unicode==0.1.0
    - tensorboard==2.8.0
    - tensorboard-data-server==0.6.1
    - tensorboard-plugin-wit==1.8.1
    - tensorflow==2.8.0
    - tensorflow-gpu==2.8.0
    - tensorflow-io-gcs-filesystem==0.24.0
    - termcolor==1.1.0
    - tf-estimator-nightly==2.8.0.dev2021122109
    - threadpoolctl==3.1.0
    - torch==1.10.2
    - torchaudio==0.7.2
    - torchutils==0.0.4
    - torchvision==0.8.2+cu110
    - tqdm==4.62.3
    - twine==3.8.0
    - typing==3.7.4.3
    - typing-extensions==4.1.1
    - urllib3==1.26.8
    - ushlex==0.99.1
    - visions==0.7.4
    - webencodings==0.5.1
    - websocket-client==1.2.3
    - werkzeug==2.0.3
    - widgetsnbextension==3.5.2
    - wrapt==1.13.3
    - xlwings==0.26.3

问题
我怎样才能像 google colab 中那样为所述函数绘图?

几天后我找到了解决方案

首先,我的代码需要修复才能使用正确的名称正确调用所需的参数

def Exec_ShowImgGrid(ObjTensor, ch=1, size=(28,28), num=16):
    #tensor: 128(pictures at the time ) * 784 (28*28)
    Objdata= ObjTensor.detach().cpu().view(-1,ch,*size) #128 *1 *28*28 
    Objgrid= make_grid(Objdata[:num],nrow=4).permute(1,2,0) #1*28*28 = 28*28*1 #Mathplot library isnt the saame as pytorch, we are accomodating the args
    Objpyplot.imshow(Objgrid)

    Objpyplot.show()

内核还在死机,唯一的解决办法是在 状态的环境中安装 numba。我只需要在我的声明中附加 from numba import cuda 声明。