致敬作者:
https://github.com/hli1221/imagefusion_densefuse
cuda 10.1
▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂
问题1:
from scipy.misc import imread
ImportError: cannot import name 'imread’
解决方案
#查看scipy版本
python -c "import scipy; print(scipy.__version__)"
#出现相关错误通常是 版本过高
pip install scipy==1.2.1 -i https://pypi.tuna.tsinghua.edu.cn/simple
#安装完后问题并未解决
pip install pillow -i https://pypi.tuna.tsinghua.edu.cn/simple
国内镜像
http://pypi.douban.com/simple/ 豆瓣
http://mirrors.aliyun.com/pypi/simple/ 阿里
http://pypi.hustunique.com/simple/ 华中理工大学
http://pypi.sdutlinux.org/simple/ 山东理工大学
http://pypi.mirrors.ustc.edu.cn/simple/ 中国科学技术大学
https://pypi.tuna.tsinghua.edu.cn/simple 清华使用办法
1、临时使用,添加“-i”或“--index”参数
2、配制成默认的pip install -i http://pypi.douban.com/simple/ flask
在你的“C:\Users\你的用户名\”目录下创建“pip”目录,“pip”目录下创建“pip.ini”文件(注意:以UTF-8 无BOM格式编码);
“pip.ini”文件内容:注意:trusted-host 选项为了避免麻烦是必须的,否则使用的时候会提示不受信任,或者添加“--trusted-host=mirrors.aliyun.com”选项;[global] index-url=http://mirrors.aliyun.com/pypi/simple/ [install] trusted-host=mirrors.aliyun.com
注意:有网页提示需要创建或修改配置文件(linux的文件在~/.pip/pip.conf,windows在%HOMEPATH%\pip\pip.ini),至少Windows7下“%HOMEPATH%\pip\pip.ini”这个目录是不起作用的。
conda常用操作
▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂
问题2
AttributeError: module 'tensorflow' has no attribute 'Session'
解决方案:
报错AttributeError: module 'tensorflow' has no attribute 'Session'。
这其实不是安装错误,是因为在新的Tensorflow 2.0版本中已经移除了Session这一模块,
改换运行代码,tensorflow2.0版本中的确没有Session这个属性,如果安装的是tensorflow2.0版本又想利用Session属性,可以将tf.Session()更改为:
import tensorflow as tf
tf.Session() 改成 tf.compat.v1.Session()
这个方法可以解决此类问题,不仅仅适用于Session属性。
查阅资料发现,原因是2.0与1.0版本不兼容,在程序开始部分添加以下代码就可以正常运行了。:
tf.compat.v1.disable_eager_execution()
tensorflow的官网对disable_eager_execution()方法是这样解释的:
This function can only be called before any Graphs, Ops, or Tensors have been created. <br>It can be used at the beginning of the program for complex migration projects from TensorFlow 1.x to 2.x.
翻译过来为:此函数只能在创建任何图、运算或张量之前调用。它可以用于从TensorFlow 1.x到2.x的复杂迁移项目的程序开头。
更新:
找到了一个更简单的方法,在引用tensorflow时,直接用:
#网络上的解决方案1
import tensorflow.compat.v1 as tf
#解决方案2
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
--------------------------------------------------------------------------------------------------------------------------------
还有一种向上兼容问题:
AttributeError: module 'tensorflow' has no attribute 'feature_column'
更新 pip install --upgrade tensorflow
更新tensorflow。解决问题。
AttributeError: module 'tensorflow' has no attribute 'placeholder'问题
把代码进入tf环境中下面的代码
import tensorflow as tf
替换成下面代码。
import tensorflow.compat.v1 as tf tf.disable_v2_behavior()
▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂
问题3:
AttributeError: module 'tensorflow' has no attribute 'skimage'
pip install skimage 改成 pip install scikit-image
▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂
问题4:
ValueError: check_hostname requires server_hostname“
关掉爬墙软件解决,但是问题应该不在这,不过解决了,不看了。
▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂
问题5(未解决问题):
'tensorflow.python.pywrap_tensorflow' has no attribute 'NewCheckpointReader'
https://blog.csdn.net/qq_39124762/article/details/82951818
对保存的参数checkpoints进行可视化读取
1.pywrap_tensorflow.NewCheckpoint(获得checkpoint的读取器)
2.np.save(对npy文件进行保存)
3.tl.file.load_npy_to_any(对保存的npy文件进行读取)
1. pywrap_tensorflow.NewCheckpoint(path)获得checkpoint的读取器
参数说明: path表示checkpoint的路径
2.np.save(path, dict) 根据路径将数据保存为npy类型
参数说明:path表示进行参数保存的路径, dict 表示需要进行保存的参数
3.tl.file.load_npy_to_any(name=path)对保存的npy文件进行读取
参数说明:name=path表示进行参数读取的路径
#代码说明:
#第一步:使用pywrap_tensorflow.NewCheckpoint(path)获得checkpoint的参数读取器
#第二步:使用reader.get_variable_to_shape_map()构造字典
#第三步:循环key,将键值对写入到all_variable.npy
#第四步:使用tl.file.load_npy_to_any将npy数据进行读取
from tensorflow.python import pywrap_tensorflow
import os
import numpy as np
import tensorlayer as tl
#print出ckpt里的所有变量
# 第一步:构建读取checkpoint的reader
model_dir = './models'
checkpoints = model_dir + os.path.sep + 'model-20180626-205832.ckpt-60000'
reader = pywrap_tensorflow.NewCheckpointReader(checkpoints)
# 第二步:构建参数字典
var_to_shape_map = reader.get_variable_to_shape_map() # 存储所有变量
# 第三步:循环key,构建数据,使用np.save()进行数据保存
for key in var_to_shape_map:
var_to_shape_map[key] = reader.get_tensor(key)
np.save('all_variable.npy', var_to_shape_map)
# 第四步:使用tl.files.load_npy_to_any进行数据的读取
# data2 = np.load('./all_variable.npy', allow_pickle=True)
data = tl.files.load_npy_to_any(name='all_variable.npy')
for key, value in data.items():
print(key, value.shape)
Tensorflow: 从checkpoint文件中读取tensor
在使用pre-train model时候,我们需要restore variables from checkpoint files.
经常出现在checkpoint 中找不到”Tensor name not found”.
这时候需要查看一下ckpt中到底有哪些变量可以显示ckpt中的tensor名字和值,当然也可以用pycharm调试。
import os
from tensorflow.python import pywrap_tensorflow
checkpoint_path = os.path.join(model_dir, "model.ckpt")
# Read data from checkpoint file
reader = pywrap_tensorflow.NewCheckpointReader(checkpoint_path)
var_to_shape_map = reader.get_variable_to_shape_map()
# Print tensor name and values
for key in var_to_shape_map:
print("tensor_name: ", key)
print(reader.get_tensor(key))
▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂
--------------------------------------------------------------------------------------------------------------------------------
环境搭建
- conda info -e
- conda env list:查看所有环境
- conda create --name ds_chinese jupyter notebook pandas : 创建一个新的环境叫ds_chinese并且安装python, jupyter notebook 以及pandas。安装多个包的时候用空格隔开。你也可以指定安装包的版本号,比如安装一个python3.6的环境。conda create --name ds_chinese python=3.6
- conda activate ds_chinese : 切换环境到ds_chinese.
- conda deactivate
- conda list : 查看当前环境中已安装的包
- conda remove --name myenv --all:删除环境和该环境下所有的包 因为我们在一台机器上操作,先要删除之前创建的环境ds_chinese.
- conda env create -f environment.yml:创建ds_chinese 环境
-
常用命令
1、conda list 查看所有已安装的包
2、conda list numpy 查看 numpy是否安装
环境下
- conda install scikit-learn:在当前环境下安装scikit-learn包
- 未来需要安装需要使用任何包都可以在当前环境下使用conda install(有些包需要使用pip install)进行包的安装。
另一种创建新环境的方式
有些时候我们需要和别人共同完成一个项目,或者是使用别人的开发环境进行学习或二次开发。这时候就需要我们使用和别人相同的开发环境。
Anaconda 提供了一种快速的方式来拷贝开发环境。假设我们需要在另一个台机器上创建一个与我们之前创建的ds_chinese一样的开发环境。我们可以按照以下步骤安装:
- 生成一个yml文件,并发送yml文件到另一台机器。
- 从另一台机器的Anaconda通过yml文件创建ds_chinese。
因为我是用一台机器模拟,在生成yml文件后,我会先删除之前创建的ds_chinese环境再用yml文件创建ds_chinese。
- conda env export > environment.yml : 生成yml文件。
在进行这个操作前我们先指定工作路经,这样yml就会生成在我们指定的工作路经下。
▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂
在python中,在导入模块时,模块的搜索顺序是:
1、当前程序根目录
2、PYTHONPATH
3、标准库目录
4、第三方库目录site-packages目录