EasyCV/docs/source/tutorials/EasyCV度量学习resnet50.ipynb

334 lines
8.3 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

{
"cells": [
{
"cell_type": "markdown",
"id": "23af4ff9-2300-458b-9486-9278cb2abea5",
"metadata": {
"tags": []
},
"source": [
"# EasyCV度量学习\n",
"\n",
"本文将介绍如何使用EasyCV快速使用Resnet50进行度量学习模型的训练、推理"
]
},
{
"cell_type": "markdown",
"id": "6c93bdca-6cb5-451e-92fd-653e33d2aa83",
"metadata": {
"tags": []
},
"source": [
"## 安装依赖包\n",
"\n",
"注: 在PAI-DSW docker中无需安装相关依赖可跳过此部分 在本地notebook环境中执行\n"
]
},
{
"cell_type": "markdown",
"id": "846076f6-ac34-49c9-aaff-e82aecd6336d",
"metadata": {},
"source": [
"1. 首先安装pytorch和对应版本的torchvision支持Pytorch1.5.1以上版本"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "259487df-b711-4457-ba0a-b32caf34079a",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# install pytorch and torch vision\n",
"! conda install --yes pytorch==1.10.0 torchvision==0.11.0 -c pytorch"
]
},
{
"cell_type": "markdown",
"id": "323f9718-927e-4377-aa80-cf5c398d54c5",
"metadata": {
"jp-MarkdownHeadingCollapsed": true,
"tags": []
},
"source": [
"2. 获取torch和cuda版本安装对应版本的mmcv和nvidia-dali"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "57f87096-7801-4254-9dfd-cbdb4ab54cfc",
"metadata": {
"jp-MarkdownHeadingCollapsed": true,
"tags": []
},
"outputs": [],
"source": [
"import torch\n",
"import os\n",
"os.environ['CUDA']='cu' + torch.version.cuda.replace('.', '')\n",
"os.environ['Torch']='torch'+torch.version.__version__.replace('+PAI', '')\n",
"!echo $CUDA\n",
"! echo $Torch"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "99e24dec-b747-4a23-a981-9aae14e433cf",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# install some python deps\n",
"! pip install mmcv-full==1.4.4 -f https://download.openmmlab.com/mmcv/dist/${CUDA}/${Torch}/index.html\n",
"! pip install http://pai-vision-data-hz.oss-cn-zhangjiakou.aliyuncs.com/third_party/nvidia_dali_cuda100-0.25.0-1535750-py3-none-manylinux2014_x86_64.whl"
]
},
{
"cell_type": "markdown",
"id": "6166dd4e-0e31-4061-9229-5f79968732e0",
"metadata": {
"jp-MarkdownHeadingCollapsed": true,
"tags": []
},
"source": [
"3. 安装EasyCV算法包"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d55d2b68-b9d3-484a-abf6-561187153c54",
"metadata": {
"jp-MarkdownHeadingCollapsed": true,
"tags": []
},
"outputs": [],
"source": [
"pip install pai-easycv"
]
},
{
"cell_type": "markdown",
"id": "d5e4d314-f59f-464c-950d-12c94b11020c",
"metadata": {
"tags": []
},
"source": [
"## 简单验证"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "80c51674-b1cc-4acf-a8e3-ed20306afebf",
"metadata": {},
"outputs": [],
"source": [
"from easycv.apis import *"
]
},
{
"cell_type": "markdown",
"id": "0512498b-a900-4969-9563-67029a1009e2",
"metadata": {
"tags": []
},
"source": [
"## 运行环境要求\n",
"\n",
"PAI-Pytorch 1.7/1.8镜像, GPU机型 P100 or V100 内存 32G"
]
},
{
"cell_type": "markdown",
"id": "8df2d210-4bcf-4b41-a981-b264c41e91a7",
"metadata": {
"tags": []
},
"source": [
"## CUB200 度量学习"
]
},
{
"cell_type": "markdown",
"id": "fce3f492-cfe0-4121-8f7d-877b31e3438b",
"metadata": {},
"source": [
"下面示例介绍如何利用[CUB200](http://www.vision.caltech.edu/datasets/cub_200_2011/)数据使用ResNet50模型快速进行度量学习模型的训练评估、模型预测过程"
]
},
{
"cell_type": "markdown",
"id": "6e769ad6-3ae0-4d5b-bf23-55673a239aa8",
"metadata": {},
"source": [
"### 数据准备\n",
"下载cub200数据解压到`data/cub200`目录, 目录结构如下\n",
"\n",
"```text\n",
"data/cub200\n",
"├── images\n",
"├── images.txt\n",
"├── image_class_labels.txt\n",
"├── train_test_split.txt\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "62eeff3d-274d-4ff9-8123-8e11d97c0d38",
"metadata": {},
"outputs": [],
"source": [
"! mkdir -p data/ && wget https://s3.amazonaws.com/fast-ai-imageclas/CUB_200_2011.tgz && tar -xzf CUB_200_2011.tgz -C data/ && mv data/CUB_200_2011 data/cub200"
]
},
{
"cell_type": "markdown",
"id": "ed28f2be-1519-4d81-9d03-42891d1e9bc7",
"metadata": {},
"source": [
"### 训练模型\n",
"下载训练配置文件"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bd8b6d19-652c-4a58-89d3-38394dbb9a20",
"metadata": {},
"outputs": [],
"source": [
"! rm -rf cub_resnet50_jpg.py\n",
"!wget https://raw.githubusercontent.com/alibaba/EasyCV/master/configs/metric_learning/cub_resnet50_jpg.py"
]
},
{
"cell_type": "markdown",
"id": "3d2ed375-f5ef-475e-9dc8-3386d328b8b0",
"metadata": {},
"source": [
"使用单卡gpu进行训练和验证集评估"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9b6ec6da-d125-4ebe-b301-0d8cc8881512",
"metadata": {
"scrolled": true,
"tags": []
},
"outputs": [],
"source": [
"! CUDA_VISIBLE_DEVICES=0 python -m torch.distributed.launch --nproc_per_node=1 --master_port=29500 tools/train.py configs/metric_learning/cub_resnet50_jpg.py --work_dir work_dirs/metric_learning/cub/r50 --launcher pytorch --fp16"
]
},
{
"cell_type": "markdown",
"id": "82b04e66-ef62-4d2e-963f-34015a588786",
"metadata": {},
"source": [
"### 导出模型\n",
"\n",
"模型训练完成使用export命令导出模型进行推理导出的模型包含推理时所需的预处理信息、后处理信息"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1d4f7868-88aa-428c-9852-22e3eca45a07",
"metadata": {},
"outputs": [],
"source": [
"# 查看训练产生的pt文件\n",
"! ls work_dirs/metric_learning/cub/r50*"
]
},
{
"cell_type": "markdown",
"id": "a5dc516f-fe35-4b1f-acbe-9ec96b72cc95",
"metadata": {},
"source": [
"ClsEvaluator_neck_top1_best.pth 是训练过程中产生的acc最高的pth,导出该模型"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "67aa8262-834a-4afc-a6cd-fd882a181371",
"metadata": {},
"outputs": [],
"source": [
"! python tools/export.py configs/metric_learning/cub_resnet50_jpg.py work_dirs/metric_learning/cub/r50/ClsEvaluator_neck_top1_best.pth work_dirs/metric_learning/cub/r50/best_export.pth"
]
},
{
"cell_type": "markdown",
"id": "72d78ef4-0d90-4f3a-a1d9-c532e17d065b",
"metadata": {},
"source": [
"### 预测\n",
"下载测试图片"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "732fa5b4-fe0f-4a9b-ba19-d73d3ebd1736",
"metadata": {},
"outputs": [],
"source": [
"! wget http://pai-vision-data-hz.oss-cn-zhangjiakou.aliyuncs.com/data/fine_grain_cls/cub_raw/images/001.Black_footed_Albatross/Black_Footed_Albatross_0001_796111.jpg"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cb3b24c2-7a19-4618-8bdf-42ebe1cdfeb3",
"metadata": {},
"outputs": [],
"source": [
"import cv2\n",
"from easycv.predictors.feature_extractor import TorchFeatureExtractor\n",
"\n",
"output_ckpt = 'work_dirs/metric_learning/cub/r50/best_export.pth'\n",
"tcls = TorchFeatureExtractor(output_ckpt)\n",
"\n",
"img = cv2.imread('Black_Footed_Albatross_0001_796111.jpg')\n",
"# input image should be RGB order\n",
"img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n",
"output = tcls.predict([img])\n",
"print(output)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}