Windows下anaconda搭建、测试安装tensorflow和opencv以及第一次使用Jupyter notebook的一些问题记录

Windows下anaconda搭建

  1. 下载并安装anaconda
  2. 下载并安装tensorflow和opencv
    (anaconda->environment->all下载)
    或者终端下载(另一篇文章会具体讲我遇到的一些问题)
  3. 下载并安装notebook

测试安装tensorflow和opencv

#1、导入opencv  2、输出
import cv2 #其实只要import导入运行没问题,说明下载成功,如果还有问题可能是版本问题
print('Hello opencv!')
#结果输出Hello opencv!
import tensorflow as tf
hello = tf.constant('Hello Tensorflow,this is my firat tfHello!')
print(hello)
tf.__version__ # 检测版本
#结果输出tf.Tensor(b'Hello Tensorflow,this is my firat tfHello!', shape=(), dtype=string)'1.14.0'
#图片读取与展示
#1、导入opencv 2、调用API  3、运行暂停
import cv2
img = cv2.imread('image0.jpg',1) #1、文件中的图片名字(此图片与程序在一个目录下) 2、参数0灰度 1彩色
cv2.imshow('image',img) # 1、图片 2、已经定义好的图片名字
cv2.waitKey(0) #K大写
#结果显示图片

运行结果:

问题记录

  1. 在使用Jupyter notebook时,写了一段代码,run发现它并没有执行。

    左边显示int[*]。

    • 可能的问题只安装了Jupyter,而没有安装Notebook,Anaconda Prompt下重新输入如下命令安装即可。
    • 可能程序卡了,点击上方像刷新一样的旋转标记重启服务(带窗口)
      • 了解提示符in[ ]几种状态的含义:
        in[ ] 程序未运行
        in[*] 程序正在运行
        in[编号] 程序正在运行
  2. import tensorflow as tf
    tf.Session()
    

    错误结果:

    ---------------------------------------------------------------------------
    AttributeError                            Traceback (most recent call last)
     in 
          1 import tensorflow as tf
          2 hello = tf.constant('hello tf')
    ----> 3 sess = tf.Session()
          4 print(sess.run(hello))
    
    AttributeError: module 'tensorflow' has no attribute 'Session'
    

    原因:

    报错AttributeError: module ‘tensorflow’ has no attribute ‘Session’。这其实不是安装错误,是因为在新的Tensorflow 2.0版本中已经移除了Session这一模块

    **法一:**改换运行代码tf.compat.v1.Session()

    **法二:**如果觉得不方便,也可以改换低版本的Tensorflow,直接用pip即可安装pip install tensorflow==1.14(如果涉及到anaconda不同环境得下载,在我的另一篇文章会详细介绍)

    1. 错误**:IndentationError: expected an indented block

      报错翻译过来就是: 缩进错误: 期望一个缩进的块

你可能感兴趣的:(#,基本操作,人工智能杂七杂八)