各类问题(持续更新)

本文出现的问题是我自己遇到的,但是解决办法参考各个博客大佬们的!

1、修改jupyter文件保存路径

  jupyter默认的文件保存路径是“C:\Users\Administrator”,我正常是不可能把编写的代码保存到这里的,所以修改路径。方法:
先尝试在“C:\Users\Administrator.jupyter”这个路径下打开“jupyter_notebook_config.py”这个文件,然后在里面修改“#c.NotebookApp.notebook_dir = ''”这一句,把你要保存的路径写到里面,比如我的路径是“F:\python\jupyter_test”,就把那句代码改成“#c.NotebookApp.notebook_dir = 'F:\python\jupyter_test'”。

  但是这个方法可能并不行,所以就用另一种方法,找到jupyter的快捷方式,右键属性,把目标里面的%USERPROFILE%改成你的路径,然后确定再去重启一下jupyter就阔以了!

2、使用PIL中resize函数出现“image has wrong mode”

  不同的mode有不同的bit像素,但是所有类别中没有16bit的pixels(可能是这个原因),所以在使用resize函数式需要改一下mode,通过img = img.convert(‘F’)来进行更改。里面的“F”就是对应的mode,可以更改成其他的格式,如下:

图片来源于https://www.cnblogs.com/chimeiwangliang/p/7130434.html
如果想深究的话,请看:https://www.cnblogs.com/zk-icewall/p/9349517.html。有写的更早的博文,但是我觉得这篇写的能很清楚的看懂!

3、使用PIL中的fromarray函数出现(keyError(1,1,1),'|u1')和"ValueError:too many dimensions:3>2"的错误:

我最开始的代码是这样的(如下),image是(512,512,1)的numpy.ndarray格式的数据,当然我的目的是为了图片(灰度的图片),所以需要处理一下最后输出出来,但是用下面的代码就是会出错,报出"keyError(1,1,1),'|u1'"和"TypeError:cannot handle this data type"的错误。

image = image * 127.5 + 127.5
image = Image.fromarray(image.astype(np.uint8))
image.save("Image_data/" + str(epoch) + "_" + str(index) + ".png")

  原因是Image.fromarray()函数的输入的shape是三维的且最后一个维度是3,(按照我的条件)就是shape=(512,512,3),我的最后一个是1,显然不行,所以需要转换一下mode,就是这篇博客问题2中的mode,设为'L'。

image = Image.fromarray(image.astype(np.uint8),mode='L')

  但是,又出现了新的问题,就是"ValueError:too many dimensions:3>2",为啥呢?因为灰度图像最后一个channel可以去掉并不影响结果啊,没去掉的话还是留了一手1在最后(我是这样想的),所以用到了np.squeeze()函数直接把最后的1干掉了,“秒~~啊”:

image = np.squeeze(image, axis=2)

  最后的代码解决完事的代码是:

image = image * 127.5 + 127.5
image = np.squeeze(image, axis = 2)
image = Image.fromarray(image.astype(np.uint8),mode = 'L')
image.save("Image_data/" + str(epoch) + "_" + str(index) + ".png")

  问题是解决了,也需要反思一下自己,缺乏自己去研究的能力,我是通过找百度,看大佬的博客才解决的,那以后如果遇到一个问题百度没有怎么办呢!所以努力学习吧!

4、导入pytorch出现'NameError: name '_C' is not defined'

  我在pycharm上'import torch'没有问题,但是直接去python编译器里面就有问题,网上搜了一些大佬们的说法,是因为使用pytorch需要Cython这个包,而pycharm上好像是自带这个包,所以没问题,所以如果要使用的话就安装一下,'pip install Cython'就行了。错误信息看下面!

5、'NotImplementedError: Layer CAM has arguments in __init__ and therefore must override get_config.'

  这个问题是keras自己定义网络层后,模型训练完进行保存时会出现的错误,我这里是自己定义通道注意力机制CAM出现这个问题,只要在定义的网络层里面添加一个函数就行了,如下:

def get_config(self):
    cfg = super().get_config()
    return cfg

我放在了定义的call()函数上方。

6、'"tf.function-decorated function tried to create "ValueError: tf.function-decorated function tried to create variables on non-first call.'

  一般情况下,这个是在模型训练前出现的错误,也就是model.fit()前,只要在前面加上一句tf.config.experimental_run_functions_eagerly(True)就可以了。想要知道为什么的话阔以看一下这位大佬写的内容:'https://blog.csdn.net/weixin_43824178/article/details/99297237'.

7、'RuntimeError: 1D target tensor expected, multi-target not supported'

  在使用pytorch进行多分类的时候,习惯将标签变成one-hot类型,但是torch的CrossEntropy直接输入标签就行了,不用变成one-hot标签,这个错误就是我使用了one-hot时一直报的错。

8、问题如下:

  File "D:\Anaconda\install\envs\all\lib\site-packages\torch\tensor.py", line 221, in backward
    torch.autograd.backward(self, gradient, retain_graph, create_graph)
  File "D:\Anaconda\install\envs\all\lib\site-packages\torch\autograd\__init__.py", line 132, in backward
    allow_unreachable=True)  # allow_unreachable flag
RuntimeError: Trying to backward through the graph a second time, but the saved intermediate results have already been freed. Specify retain_graph=True when calling backward the first time.

报这个错误就是需要在反向传递的代码中加入'retain_graph = True'这句话

posted @ 2020-09-08 11:22  Yuzuriha_Inori  阅读(1767)  评论(0编辑  收藏  举报