char-cnn+torch+ubuntu16.04(RNN) 安装过程

本文详细介绍了如何在Ubuntu16.04环境下安装Torch7,并配置Char-CNN模型所需的各种依赖。首先通过命令行安装Torch及其依赖项,接着解决安装过程中可能出现的问题,如cmake未找到等。最后,利用luarocks安装额外的Torch包,确保Char-CNN能够顺利运行。

char-cnn+torch+ubuntu16.04(RNN) 安装过程

1. 搭建Torch 环境

1.1 安装Torch7 的依赖项

# in a terminal, run the commands
curl -sk https://raw.githubusercontent.com/torch/ezinstall/master/install-deps | bash

cd ~/torch; ./install.sh

安装成功的标志是最后显示“==> Torch7’s dependencies have been installed ”

1.2 安装 torch distribution

git clone https://github.com/torch/distro.git ~/torch --recursive
cd ~/torch; ./install.sh

注:安装过程出现以下问题:

1.  ./install.sh: line 59: cmake: command not found

意思就是cmake 命令程序没有安装,执行命令

sudo apt install cmake

安装,安装成功后继续 cd ~/torch; ./install.sh 命令。

2.  
error: RPC failed; curl 56 GnuTLS recv error (-9): A TLS packet with unexpected length was received.
fatal: The remote end hung up unexpectedly
fatal: early EOF
fatal: index-pack failed
fatal: clone of 'https://github.com/torch/cutorch.git' into submodule path 'extra/cutorch' failed

这个报错,原因可能是网络的原因,切到你安装torch 的目录下重新安装:

git clone https://github.com/torch/distro.git ~/torch --recursive

直到成功安装,然后继续cd ~/torch; ./install.sh

编译程序
执行下列命令,使得安装过程中被更改的PATH生效:

source ~/.bashrc

执行下列命令查看torch 是否安装成功:
$th
这里写图片描述

出现上图图片中的内容表示torch安装成功。

1.3 卸载torch

如果不使用torch,可以使用下列命令卸载:

rm -rf ~/torch

可以使用luarocks命令安装其他的torch包

$ luarocks install image
$ luarocks list

至此Torch 环境 搭建完成

2. 运行char-cnn

2.1 安装必要的包

$ luarocks install nngraph 
$ luarocks install optim
### 使用 Faster R-CNN 和 OpenCV 进行目标检测 #### 准备工作 为了使用 Faster R-CNN 结合 OpenCV 进行目标检测,需先安装必要的库。这通常涉及 Python 的 `opencv-python` 库以及预训练的 Faster R-CNN 模型。 ```bash pip install opencv-python numpy ``` 对于模型加载部分,如果采用的是 PyTorch 版本,则还需要安装 PyTorch 及其工具: ```bash pip install torch torchvision ``` #### 加载预训练模型并读取图片 利用 OpenCV 读入待检测图像,并准备用于推理的 Faster R-CNN 模型。这里假设已经有一个基于 COCO 数据集训练过的 Faster R-CNN 模型可用[^2]。 ```python import cv2 import numpy as np from PIL import Image import torch from torchvision.models.detection.faster_rcnn import fasterrcnn_resnet50_fpn, FastRCNNPredictor def load_model(): model = fasterrcnn_resnet50_fpn(pretrained=True) device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu') model.to(device) model.eval() return model image_path = 'path_to_image.jpg' img = cv2.imread(image_path) model = load_model() transformed_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB).astype(np.float32)/255.0 tensor_img = torch.tensor(transformed_img.transpose((2, 0, 1))).unsqueeze(0) ``` #### 执行预测操作 一旦准备好输入张量形式的数据,就可以将其传递给模型来进行前向传播计算,从而获得检测结果。 ```python with torch.no_grad(): predictions = model(tensor_img)[0] boxes = predictions['boxes'].numpy().tolist() scores = predictions['scores'].numpy().tolist() labels = predictions['labels'].numpy().tolist() ``` #### 绘制边界框与标签 最后一步是在原始图像上绘制出识别出来的物体位置及其类别名称。 ```python for box, score, label in zip(boxes, scores, labels): if score >= 0.8: # 设置置信度阈值过滤低质量的结果 x_min, y_min, x_max, y_max = map(int, box) color = (np.random.randint(0, 255), np.random.randint(0, 255), np.random.randint(0, 255)) img = cv2.rectangle(img, (x_min, y_min), (x_max, y_max), color=color, thickness=2) text = f'{label}:{score:.2f}' font_scale = 0.7 font_thickness = 2 ((text_width, text_height), _) = cv2.getTextSize(text=text, fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=font_scale, thickness=font_thickness) img = cv2.rectangle(img, (x_min, y_min - int(1.3 * text_height)), (x_min + text_width, y_min), color=tuple([int(c) for c in color]), thickness=-1) img = cv2.putText(img=img, text=text, org=(x_min, y_min - int(0.3 * text_height)), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=font_scale, color=(0, 0, 0), thickness=font_thickness) cv2.imshow("Detected Objects", img) cv2.waitKey(0) cv2.destroyAllWindows() ``` 上述代码展示了如何集成 Faster R-CNN 和 OpenCV 来执行高效的目标检测任务。通过这种方式不仅可以快速部署现有模型,还能充分利用 GPU 资源加速运算过程[^4]。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值