会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132474个问题
Python 全系列/第十二阶段:Python_Django3框架/Django初级 30376楼
JAVA 全系列/第二十一阶段:分布式医疗云平台/项目环境搭建(旧) 30377楼
JAVA 全系列/第八阶段:Linux入门到实战/Maven 30379楼
JAVA 全系列/第三阶段:数据库编程/SQL 语言 30380楼
JAVA 全系列/第三阶段:数据库编程/SQL 语言 30382楼
Python 全系列/第七阶段:网页编程基础/CSS 样式 30385楼

老师我这个代码第一遍跟着老师敲是没有问题的,我在另一个项目里面又试了一遍老是说我的post方法的enctype有问题,可是我设置了呀,老师我该怎么解决


Type 异常报告

消息 org.apache.tomcat.util.http.fileupload.impl.InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is null

描述 服务器遇到一个意外的情况,阻止它完成请求。

Exception

javax.servlet.ServletException: org.apache.tomcat.util.http.fileupload.impl.InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is null
	org.apache.catalina.connector.Request.parseParts(Request.java:2913)
	org.apache.catalina.connector.Request.getParts(Request.java:2775)
	org.apache.catalina.connector.Request.getPart(Request.java:2944)
	org.apache.catalina.connector.RequestFacade.getPart(RequestFacade.java:1105)
	com.bjsxt.servlet.FileUpLoadServlet.doPost(FileUpLoadServlet.java:32)
	com.bjsxt.servlet.FileUpLoadServlet.doGet(FileUpLoadServlet.java:20)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

Root Cause

org.apache.tomcat.util.http.fileupload.impl.InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is null
	org.apache.tomcat.util.http.fileupload.impl.FileItemIteratorImpl.init(FileItemIteratorImpl.java:140)
	org.apache.tomcat.util.http.fileupload.impl.FileItemIteratorImpl.getMultiPartStream(FileItemIteratorImpl.java:194)
	org.apache.tomcat.util.http.fileupload.impl.FileItemIteratorImpl.findNextItem(FileItemIteratorImpl.java:213)
	org.apache.tomcat.util.http.fileupload.impl.FileItemIteratorImpl.<init>(FileItemIteratorImpl.java:131)
	org.apache.tomcat.util.http.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:255)
	org.apache.tomcat.util.http.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:279)
	org.apache.catalina.connector.Request.parseParts(Request.java:2873)
	org.apache.catalina.connector.Request.getParts(Request.java:2775)
	org.apache.catalina.connector.Request.getPart(Request.java:2944)
	org.apache.catalina.connector.RequestFacade.getPart(RequestFacade.java:1105)
	com.bjsxt.servlet.FileUpLoadServlet.doPost(FileUpLoadServlet.java:32)
	com.bjsxt.servlet.FileUpLoadServlet.doGet(FileUpLoadServlet.java:20)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

):注意 主要问题的全部 stack 信息可以在 server logs 里查看


JAVA:

package com.bjsxt.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.UUID;

@WebServlet("/fileupload.do")
@MultipartConfig//基于注解开发的文件上传
public class FileUpLoadServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置请求编码
        req.setCharacterEncoding("utf-8");
        //设置响应编码
        resp.setContentType("text/html;charset=utf-8");
        //获取文件描述
        String desc = req.getParameter("desc");
        //获取part对象  参数给form表单中上传文件的name
        Part part = req.getPart("file");
        /*//设置唯一的文件名,避免因为文件名重复而造成文件覆盖,
        * 先截取文件的后缀(扩展名),然后与随机生成唯一的字符串拼接
        * UUID.randowUUID()随机生成一个唯一的字符串,返回UUID类型,需要调用toString()返回字符串类型*/
        String name = UUID.randomUUID().toString() + part.getSubmittedFileName().substring(part.getSubmittedFileName().lastIndexOf("."));
        //将相对路径转换为绝对路径
        ServletContext servletContext = this.getServletContext();
        String realPath = servletContext.getRealPath("images/" + name);
        System.out.println(realPath);
        //将上传的对象做IO流chuli,并通过字符输出流写入到路径中
        part.write(realPath);

        PrintWriter out = resp.getWriter();
        /*拼接HTML页面*/
        out.println("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.0 Transitional//EN'>");
        out.println("<HTML>");
        out.println("<HEAD><TITLE>临江仙.文件上传</TITLE></HEAD>");
        out.println("<BODY>");
        out.println("文件上传成功!"+desc);
        out.println("<BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();
    }
}


HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body><!--
        get:以字符型方式提交,
        post:既可以以字节方式提交也可以字符方式提交
        -->
    <form   action="fileupload.do"  method="post"   enctype="multipart/form-data">
        文件描述:<input type="text" name="desc"/>
        文件上传:<input type="file" name="file"/>
        <input type="submit"   value="OK"/>
    </form>
</body>
</html>











JAVA 全系列/第五阶段:JavaWeb开发/Servlet技术详解(旧) 30386楼
Python 全系列/第七阶段:网页编程基础/CSS 样式 30387楼
Python 全系列/第十阶段:Flask百战电商后台项目/Flask百战电商后台项目 30389楼

一、代码

import numpy as np
from sklearn.datasets import load_sample_images
import tensorflow as tf
import matplotlib.pyplot as plt

# from tensorflow.compat.v1 import ConfigProto
# config = ConfigProto()
# config.gpu_options.allow_growth = True
# config.gpu_options.per_process_gpu_memory_fraction = 1  # 指定显存分配比例


# 加载数据集
# 输入图片通常是3D,[height, width, channels]
# mini-batch通常是4D,[mini-batch size, height, width, channels]
dataset = np.array(load_sample_images().images, dtype=np.float32)
# 数据集里面两张图片,一个中国庙宇,一个花
# m,h,w,c
batch_size, height, width, channels = dataset.shape
print(batch_size, height, width, channels)

# plt.imshow(load_sample_images().images[0])  # 绘制第一个图
# plt.show()

# plt.imshow(load_sample_images().images[1])  # 绘制第二个图
# plt.show()

# 创建两个filters
# 高,宽,通道数,卷积核的个数
# 7, 7, channels, 2
filters_test = np.zeros(shape=(7, 7, channels, 2), dtype=np.float32)
#切片操作
filters_test[:, 3, :, 0] = 1  # 垂直,3代表中间那一列
filters_test[3, :, :, 1] = 1  # 水平

# filter参数是一个filters的集合
X = tf.placeholder(tf.float32, shape=(None, height, width, channels))
# strides=[1, 2, 2, 1] 中第一最后一个为1,中间对应sh和sw
convolution = tf.nn.conv2d(X, filter=filters_test, strides=[1, 1, 1, 1], padding='SAME')

with tf.Session() as sess:
    output = sess.run(convolution, feed_dict={X: dataset})
    print(output.shape)
    # (2,427,640,2)  (多少张照片,高、宽,feature map的数量)


plt.imshow(load_sample_images().images[0])  # 绘制第一个图
plt.show()

plt.imshow(output[0, :, :, 0])  # 绘制第一个图的第一个特征图
plt.show()

plt.imshow(output[0, :, :, 1])  # 绘制第一个图的第二个特征图
plt.show()

plt.imshow(load_sample_images().images[1])  # 绘制第二个图
plt.show()

plt.imshow(output[1, :, :, 0])  # 绘制第二个图的第一个特征图
plt.show()

plt.imshow(output[1, :, :, 1])  # 绘制第二个图的第二个特征图
plt.show()

二、结果

D:\programs\Anaconda3\envs\tf115_py36\python.exe F:/人工智能/11深度学习原理进阶实战代码/04CNN_study/18_convolution.py
2020-08-09 18:07:10.122357: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_100.dll
WARNING:tensorflow:From F:/人工智能/11深度学习原理进阶实战代码/04CNN_study/18_convolution.py:37: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.

2 427 640 3
WARNING:tensorflow:From F:/人工智能/11深度学习原理进阶实战代码/04CNN_study/18_convolution.py:41: The name tf.Session is deprecated. Please use tf.compat.v1.Session instead.

2020-08-09 18:07:12.672410: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library nvcuda.dll
2020-08-09 18:07:13.479728: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1618] Found device 0 with properties: 
name: GeForce GTX 1050 major: 6 minor: 1 memoryClockRate(GHz): 1.442
pciBusID: 0000:01:00.0
2020-08-09 18:07:13.479951: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_100.dll
2020-08-09 18:07:13.482875: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_100.dll
2020-08-09 18:07:13.485579: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cufft64_100.dll
2020-08-09 18:07:13.487276: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library curand64_100.dll
2020-08-09 18:07:13.490839: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusolver64_100.dll
2020-08-09 18:07:13.493351: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusparse64_100.dll
2020-08-09 18:07:13.500328: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudnn64_7.dll
2020-08-09 18:07:13.501102: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1746] Adding visible gpu devices: 0
2020-08-09 18:07:13.501456: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2020-08-09 18:07:13.503587: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1618] Found device 0 with properties: 
name: GeForce GTX 1050 major: 6 minor: 1 memoryClockRate(GHz): 1.442
pciBusID: 0000:01:00.0
2020-08-09 18:07:13.503804: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_100.dll
2020-08-09 18:07:13.503946: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_100.dll
2020-08-09 18:07:13.504088: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cufft64_100.dll
2020-08-09 18:07:13.504231: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library curand64_100.dll
2020-08-09 18:07:13.504378: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusolver64_100.dll
2020-08-09 18:07:13.504523: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusparse64_100.dll
2020-08-09 18:07:13.504667: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudnn64_7.dll
2020-08-09 18:07:13.505375: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1746] Adding visible gpu devices: 0
2020-08-09 18:07:14.039442: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1159] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-08-09 18:07:14.039598: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1165]      0 
2020-08-09 18:07:14.039691: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1178] 0:   N 
2020-08-09 18:07:14.040333: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1304] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 2127 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1050, pci bus id: 0000:01:00.0, compute capability: 6.1)
2020-08-09 18:07:14.163121: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudnn64_7.dll
2020-08-09 18:07:14.927996: E tensorflow/stream_executor/cuda/cuda_dnn.cc:319] Loaded runtime CuDNN library: 7.4.1 but source was compiled with: 7.6.0.  CuDNN library major and minor version needs to match or have higher minor version in case of CuDNN 7.0 or later version. If using a binary install, upgrade your CuDNN library.  If building from sources, make sure the library loaded at runtime is compatible with the version specified during compile configuration.
2020-08-09 18:07:14.930492: E tensorflow/stream_executor/cuda/cuda_dnn.cc:319] Loaded runtime CuDNN library: 7.4.1 but source was compiled with: 7.6.0.  CuDNN library major and minor version needs to match or have higher minor version in case of CuDNN 7.0 or later version. If using a binary install, upgrade your CuDNN library.  If building from sources, make sure the library loaded at runtime is compatible with the version specified during compile configuration.
Traceback (most recent call last):
  File "D:\programs\Anaconda3\envs\tf115_py36\lib\site-packages\tensorflow_core\python\client\session.py", line 1365, in _do_call
    return fn(*args)
  File "D:\programs\Anaconda3\envs\tf115_py36\lib\site-packages\tensorflow_core\python\client\session.py", line 1350, in _run_fn
    target_list, run_metadata)
  File "D:\programs\Anaconda3\envs\tf115_py36\lib\site-packages\tensorflow_core\python\client\session.py", line 1443, in _call_tf_sessionrun
    run_metadata)
tensorflow.python.framework.errors_impl.UnknownError: 2 root error(s) found.
  (0) Unknown: Failed to get convolution algorithm. This is probably because cuDNN failed to initialize, so try looking to see if a warning log message was printed above.
	 [[{{node Conv2D}}]]
  (1) Unknown: Failed to get convolution algorithm. This is probably because cuDNN failed to initialize, so try looking to see if a warning log message was printed above.
	 [[{{node Conv2D}}]]
	 [[Conv2D/_3]]
0 successful operations.
0 derived errors ignored.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "F:/人工智能/11深度学习原理进阶实战代码/04CNN_study/18_convolution.py", line 42, in <module>
    output = sess.run(convolution, feed_dict={X: dataset})
  File "D:\programs\Anaconda3\envs\tf115_py36\lib\site-packages\tensorflow_core\python\client\session.py", line 956, in run
    run_metadata_ptr)
  File "D:\programs\Anaconda3\envs\tf115_py36\lib\site-packages\tensorflow_core\python\client\session.py", line 1180, in _run
    feed_dict_tensor, options, run_metadata)
  File "D:\programs\Anaconda3\envs\tf115_py36\lib\site-packages\tensorflow_core\python\client\session.py", line 1359, in _do_run
    run_metadata)
  File "D:\programs\Anaconda3\envs\tf115_py36\lib\site-packages\tensorflow_core\python\client\session.py", line 1384, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.UnknownError: 2 root error(s) found.
  (0) Unknown: Failed to get convolution algorithm. This is probably because cuDNN failed to initialize, so try looking to see if a warning log message was printed above.
	 [[node Conv2D (defined at D:\programs\Anaconda3\envs\tf115_py36\lib\site-packages\tensorflow_core\python\framework\ops.py:1748) ]]
  (1) Unknown: Failed to get convolution algorithm. This is probably because cuDNN failed to initialize, so try looking to see if a warning log message was printed above.
	 [[node Conv2D (defined at D:\programs\Anaconda3\envs\tf115_py36\lib\site-packages\tensorflow_core\python\framework\ops.py:1748) ]]
	 [[Conv2D/_3]]
0 successful operations.
0 derived errors ignored.

Original stack trace for 'Conv2D':
  File "F:/人工智能/11深度学习原理进阶实战代码/04CNN_study/18_convolution.py", line 39, in <module>
    convolution = tf.nn.conv2d(X, filter=filters_test, strides=[1, 1, 1, 1], padding='SAME')
  File "D:\programs\Anaconda3\envs\tf115_py36\lib\site-packages\tensorflow_core\python\ops\nn_ops.py", line 2010, in conv2d
    name=name)
  File "D:\programs\Anaconda3\envs\tf115_py36\lib\site-packages\tensorflow_core\python\ops\gen_nn_ops.py", line 1071, in conv2d
    data_format=data_format, dilations=dilations, name=name)
  File "D:\programs\Anaconda3\envs\tf115_py36\lib\site-packages\tensorflow_core\python\framework\op_def_library.py", line 794, in _apply_op_helper
    op_def=op_def)
  File "D:\programs\Anaconda3\envs\tf115_py36\lib\site-packages\tensorflow_core\python\util\deprecation.py", line 507, in new_func
    return func(*args, **kwargs)
  File "D:\programs\Anaconda3\envs\tf115_py36\lib\site-packages\tensorflow_core\python\framework\ops.py", line 3357, in create_op
    attrs, op_def, compute_device)
  File "D:\programs\Anaconda3\envs\tf115_py36\lib\site-packages\tensorflow_core\python\framework\ops.py", line 3426, in _create_op_internal
    op_def=op_def)
  File "D:\programs\Anaconda3\envs\tf115_py36\lib\site-packages\tensorflow_core\python\framework\ops.py", line 1748, in __init__
    self._traceback = tf_stack.extract_stack()


Process finished with exit code 1

三、问题

tensorflow.python.framework.errors_impl.UnknownError: 2 root error(s) found.

  (0) Unknown: Failed to get convolution algorithm. This is probably because cuDNN failed to initialize, so try looking to see if a warning log message was printed above.

[[node Conv2D (defined at D:\programs\Anaconda3\envs\tf115_py36\lib\site-packages\tensorflow_core\python\framework\ops.py:1748) ]]

  (1) Unknown: Failed to get convolution algorithm. This is probably because cuDNN failed to initialize, so try looking to see if a warning log message was printed above.

[[node Conv2D (defined at D:\programs\Anaconda3\envs\tf115_py36\lib\site-packages\tensorflow_core\python\framework\ops.py:1748) ]]

[[Conv2D/_3]]

0 successful operations.

0 derived errors ignored.


老师,这里的程序是老师的程序,但是运行会出现上述错误,我的cuda和cudann是按照视频里里的教程安装的

image.pngTensorFlow为1.15的版本,也是按照老师教程安装的,为什么会出现“ cuDNN failed to initialize”这个问题。


我上网查了可能是因为显存分配问题,所以加上下述这段代码也不行,麻烦老师帮忙找一下原因?

from tensorflow.compat.v1 import ConfigProto
config = ConfigProto()
config.gpu_options.allow_growth = True
config.gpu_options.per_process_gpu_memory_fraction = 0.5  # 指定显存分配比例


Python 全系列/第二十四阶段:人工智能基础_深度学习理论和实战(旧)/卷积神经网络CNN 30390楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637