鱼C论坛

 找回密码
 立即注册
查看: 900|回复: 9

cudnn环境变量问题

[复制链接]
发表于 2018-10-25 17:15:21 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
import matplotlib.pyplot as plt
import numpy as np
import os
import tensorflow as tf
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as vis_util
from PIL import Image


def test():
    #重置图
    tf.reset_default_graph()
    '''
    载入模型以及数据集样本标签,加载待测试的图片文件
    '''
    #指定要使用的模型的路径  包含图结构,以及参数
    PATH_TO_CKPT = 'E:/models/research/object_detection/flower_identification/export/frozen_inference_graph.pb'

   
    #测试图片所在的路径
    PATH_TO_TEST_IMAGES_DIR ='E:/models/research/object_detection/flower_identification/data/test'
   
    TEST_IMAGE_PATHS = [os.path.join(PATH_TO_TEST_IMAGES_DIR,'image{}.jpg'.format(i)) for i in range(1,6) ]
   
    #数据集对应的label mscoco_label_map.pbtxt文件保存了index到类别名的映射
    PATH_TO_LABELS = os.path.join('E:/models/research/object_detection/flower_identification/data', 'label_map.pbtxt')
   
    NUM_CLASSES = 2
     
    #重新定义一个图
    output_graph_def = tf.GraphDef()
   
    with tf.gfile.GFile(PATH_TO_CKPT,'rb') as fid:
        #将*.pb文件读入serialized_graph
        serialized_graph = fid.read()
        #将serialized_graph的内容恢复到图中
        output_graph_def.ParseFromString(serialized_graph)
        #print(output_graph_def)
        #将output_graph_def导入当前默认图中(加载模型)
        tf.import_graph_def(output_graph_def,name='')
        
    print('模型加载完成')   
   
    #载入coco数据集标签文件
    label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
    categories = label_map_util.convert_label_map_to_categories(label_map,max_num_classes = NUM_CLASSES,use_display_name = True)
    category_index = label_map_util.create_category_index(categories)
   
   
    '''
    定义session
    '''
    def load_image_into_numpy_array(image):
        '''
        将图片转换为ndarray数组的形式
        '''
        im_width,im_height = image.size
        return np.array(image.getdata()).reshape((im_height,im_width,3)).astype(np.uint0)
   
    #设置输出图片的大小
    IMAGE_SIZE = (8,6)
   
    #使用默认图,此时已经加载了模型
    detection_graph = tf.get_default_graph()
   
    with tf.Session(graph=detection_graph) as sess:
        for image_path in TEST_IMAGE_PATHS:
            image = Image.open(image_path)
            #将图片转换为numpy格式
            image_np = load_image_into_numpy_array(image)
            
            '''
            定义节点,运行并可视化
            '''
            #将图片扩展一维,最后进入神经网络的图片格式应该是[1,?,?,3]
            image_np_expanded = np.expand_dims(image_np,axis = 0)
            
            '''
            获取模型中的tensor
            '''
            image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
                        
            #boxes用来显示识别结果
            boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
            
            #Echo score代表识别出的物体与标签匹配的相似程度,在类型标签后面
            scores = detection_graph.get_tensor_by_name('detection_scores:0')
            classes = detection_graph.get_tensor_by_name('detection_classes:0')
            num_detections = detection_graph.get_tensor_by_name('num_detections:0')
            
            #开始检查
            boxes,scores,classes,num_detections = sess.run([boxes,scores,classes,num_detections],
                                                           feed_dict={image_tensor:image_np_expanded})
            
            #可视化结果
            vis_util.visualize_boxes_and_labels_on_image_array(
                    image_np,
                    np.squeeze(boxes),
                    np.squeeze(classes).astype(np.int32),
                    np.squeeze(scores),
                    category_index,
                    use_normalized_coordinates=True,
                    line_thickness=8)
            plt.figure(figsize=IMAGE_SIZE)
            print(type(image_np))
            print(image_np.shape)
            image_np = np.array(image_np,dtype=np.uint8)            
            plt.imshow(image_np)
   
   
               
if __name__ == '__main__':
    test()
该代码运行时出现下面错误
ImportError: Could not find 'cudnn64_7.dll'. TensorFlow requires that this DLL be installed in a directory that is named in your %PATH% environment variable. Note that installing cuDNN is a separate step from installing CUDA, and this DLL is often found in a different directory from the CUDA DLLs. You may install the necessary DLL by downloading cuDNN 7 from this URL: https://developer.nvidia.com/cudnn
怎么解决
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-10-25 17:19:00 | 显示全部楼层
本帖最后由 露转溪桥 于 2018-10-25 17:20 编辑

文件缺失了,重装cuda和cudnn试过了吗?装完添加相应的环境变量:https://blog.csdn.net/u011821462/article/details/50145221 这个看看能否解决
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-25 18:13:23 | 显示全部楼层
你之前跑过tf么。。。

cudnn那个拷到对应文件夹了吗。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-10-25 18:42:30 | 显示全部楼层
Charles未晞 发表于 2018-10-25 18:13
你之前跑过tf么。。。

cudnn那个拷到对应文件夹了吗。。。

在jupyter notebook里跑过,cudnn要拷到哪个文件夹里
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-25 18:46:09 | 显示全部楼层
https://developer.nvidia.com/cudnn下载对应的版本,拷到CUDA文件夹下,比如CUDA/V9.0下
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-10-25 18:59:57 | 显示全部楼层
Charles未晞 发表于 2018-10-25 18:46
https://developer.nvidia.com/cudnn下载对应的版本,拷到CUDA文件夹下,比如CUDA/V9.0下

我下载的就是cudnn v7.3.1Library for windows7,已经下载好了,解压过后生成cuda文件
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-25 19:10:50 | 显示全部楼层
江老师实验室 发表于 2018-10-25 18:59
我下载的就是cudnn v7.3.1Library for windows7,已经下载好了,解压过后生成cuda文件

解压后拷到对应的文件夹下
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-10-25 19:34:11 | 显示全部楼层
Charles未晞 发表于 2018-10-25 19:10
解压后拷到对应的文件夹下

对应文件夹是哪个
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-10-25 19:42:03 | 显示全部楼层

能加一下你的qq问吗

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-25 22:54:25 | 显示全部楼层

cuda安装完之后找到类似下图这样的路径(图源网络):
SRE2WY3XK6G96XKF4~`P34E.png
把cudnn解压后的文件复制进去。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-1-3 04:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表