背景:在改造caffe自带demo时,增加了一个更新模型的功能,需要将用户训练好的caffemodel上传到服务器,并替换到已经存在的caffemodel文件,重新加载上传的caffemodel文件并运行。最终改动结果页面如下:
Tornado上传文件
在海洋生物识别的过程中有一个上传本地图片进行检测的功能,对应这个功能进行了修改,但是出现以下错误,基本意思是Tornado默认上传大小为100MB 如果超过该大小就会出现以下错误。这里可以通过修改max_buffer_size属性来改变默认的上传大小,但是这里会有风险,因为Tornado是通过单字符串读入内存中的。
'Content-Length too' long when uploading file using Tornado
错误解释:
Tornado has a configurable limit on upload size (defaulting to 10MB). You can increase the limit by
passing max_buffer_size to the HTTPServer constructor (or Application.listen). However, since
Tornado (version 3.1) reads the entire upload body into a single contiguous string in memory, it's
dangerous to make the limit too high.
不过这里如果小于100MB 的小文件还是推荐使用的,代码可以参考caffedemo的,关键代码如下
前端
Python后端代码:
@app.route('/model_upload', methods=['POST'])
def model_upload():
try:
# We will save the file to disk for possible data collection.
model_file = flask.request.files['model_file']
filename_ = werkzeug.secure_filename(model_file.filename)
filename = os.path.join(UPLOAD_MODEL_FOLDER, filename_)
model_file.save(filename)
#filename = filename
#imagefile.save(filename)
logging.info('Saving to %s.', filename)
#image = exifutil.open_oriented_im(filename)
return flask.render_template(
'right_update.html', has_result=True,
result=(True, '上传成功')
)
#start_from_terminal(app)
except Exception as err:
logging.info('Uploaded model open error: %s', err)
return flask.render_template(
'right_update.html', has_result=True,
result=(False, 'Cannot open uploaded caffemodel.')
)
感谢博主提供的代码,十分有帮助。请参考 大文件分片传输
这里为了展示效果粘贴一下效果图
上传成功
上传错误
由于在使用pip install 下载一下包时候由于网络原因总是超时,这里解决有三种方法,分别是:
(1)更换下载源
(2)fq
(3)修改默认超时时间
这里参考了替换pip下载源
写入内容为:
[global]
timeout = 6000
index-url = https://pypi.douban.com/simple
trusted-host = pypi.douban.com