序文
GPU トレーニング マシンをお持ちでない場合は、クラウド サーバーを使用してモデルをトレーニングすることが最良の選択です。トレーニング中に時間通りに請求されるサーバーを開くだけです。トレーニングが完了し、環境イメージが保存されたら、サーバーを完全に停止することも可能で、期間中は費用もかからず、次回トレーニングするときも環境を起動するだけでトレーニング環境を汚染から守ることが簡単です。
1. サーバーを選択します
1. ここでは Alibaba サーバーが選択されており、Alipay アカウントで直接ログインできます。
2.構成を選択し、従量課金制で支払います。私は 20,000 を超えるデータセットを使用して yolov5 モデルをトレーニングしましたが、V100 で完全に十分です。
3. システムを選択し、GPU をインストールして開始します
。 3. ネットワーク速度 (アップリンク速度とダウンリンク速度) を選択し、順序を確認します。
2、サーバーを構成する
1. サーバーに接続するには、「リモート接続」を直接クリックします。
2. root に切り替えて、必要なファイルをインストールします。
su
cd
sudo apt-get install vim #vim
sudo apt-get install unzip #解压
sudo apt-get install zip #压缩
sudo apt-get install screen #保护进程会话
3. 環境の設定(Anaconda3を使用)
1. まず Anaconda3 をローカル ディレクトリにダウンロードし、次に scp を使用してサーバーにアップロードします。
scp data/Anaconda3-5.3.0-Linux-x86_64.sh [email protected]:/home/data/
アップロードが完了するのを待った後、Anaconda3 をサーバーにインストールし、インストールが完了するまで確認します。
Anaconda3を環境変数に追加します
sudo vim ~/.bashrc
ファイルの末尾に bin パスを追加し、ファイルを保存して終了します。
export PATH=$PATH:/root/anaconda3/bin
環境を使用して効果を発揮する
source ~/.bashrc
2. 環境を作成する
conda create --name yolov5 python=3.7
conda activate yolov5
3. アルゴリズム コードをダウンロードします。ソース コードを git から直接ダウンロードすることも、変更したソース コードをアップロードすることもできます。
git からソース コードをダウンロードします (例として yolov5s)。
gitのダウンロード
git clone https://github.com/ultralytics/yolov5.git
cd yolov5
pip install -r requirements.txt
ローカルアップロードと手動インストールの依存関係
scp data/yolov5.zip [email protected]:/home/data/
アップロードが完了したらサーバーに切り替えます
unzip yolov5.zip
cd yolov5
conda install pytorch torchvision cudatoolkit=10.2 -c pytorch
pip install cython matplotlib tqdm opencv-python tensorboard scipy pillow onnx pyyaml pandas seaborn
4. データ処理
1. データセットをサーバーにアップロードします
scp data/dataset.zip [email protected]:/home/data/yolov5
2. アップロードが完了したら、データをトレーニング セットとテスト セットに分割します。ここで使用する coco データ形式を yolov5 形式に変換する必要があります。
unzip dataset.zip
python generate_txt.py --img_path data/XXXXX/JPEGImages --xml_path data/XXXXX/Annotations --out_path data/XXXXX
データ変換と生成generate_txt.py ソース コード
import os
import glob
import argparse
import random
import xml.etree.ElementTree as ET
from PIL import Image
from tqdm import tqdm
def get_all_classes(xml_path):
xml_fns = glob.glob(os.path.join(xml_path, '*.xml'))
class_names = []
for xml_fn in xml_fns:
tree = ET.parse(xml_fn)
root = tree.getroot()
for obj in root.iter('object'):
cls = obj.find('name').text
class_names.append(cls)
return sorted(list(set(class_names)))
def convert_annotation(img_path, xml_path, class_names, out_path):
output = []
im_fns = glob.glob(os.path.join(img_path, '*.jpg'))
for im_fn in tqdm(im_fns):
if os.path.getsize(im_fn) == 0:
continue
xml_fn = os.path.join(xml_path, os.path.splitext(os.path.basename(im_fn))[0] + '.xml')
if not os.path.exists(xml_fn):
continue
img = Image.open(im_fn)
height, width = img.height, img.width
tree = ET.parse(xml_fn)
root = tree.getroot()
anno = []
xml_height = int(root.find('size').find('height').text)
xml_width = int(root.find('size').find('width').text)
if height != xml_height or width != xml_width:
print((height, width), (xml_height, xml_width), im_fn)
continue
for obj in root.iter('object'):
cls = obj.find('name').text
cls_id = class_names.index(cls)
xmlbox = obj.find('bndbox')
xmin = int(xmlbox.find('xmin').text)
ymin = int(xmlbox.find('ymin').text)
xmax = int(xmlbox.find('xmax').text)
ymax = int(xmlbox.find('ymax').text)
cx = (xmax + xmin) / 2.0 / width
cy = (ymax + ymin) / 2.0 / height
bw = (xmax - xmin) * 1.0 / width
bh = (ymax - ymin) * 1.0 / height
anno.append('{} {} {} {} {}'.format(cls_id, cx, cy, bw, bh))
if len(anno) > 0:
output.append(im_fn)
with open(im_fn.replace('.jpg', '.txt'), 'w') as f:
f.write('\n'.join(anno))
random.shuffle(output)
train_num = int(len(output) * 0.9)
with open(os.path.join(out_path, 'train.txt'), 'w') as f:
f.write('\n'.join(output[:train_num]))
with open(os.path.join(out_path, 'val.txt'), 'w') as f:
f.write('\n'.join(output[train_num:]))
def parse_args():
parser = argparse.ArgumentParser('generate annotation')
parser.add_argument('--img_path', type=str, help='input image directory')
parser.add_argument('--xml_path', type=str, help='input xml directory')
parser.add_argument('--out_path', type=str, help='output directory')
args = parser.parse_args()
return args
if __name__ == '__main__':
args = parse_args()
class_names = get_all_classes(args.xml_path)
print(class_names)
convert_annotation(args.img_path, args.xml_path, class_names, args.out_path)
5. トレーニングモデル
1. モデルをトレーニングする前に、最初に screen を実行して、サービス端末が切断された後もトレーニングを継続できることを確認します。
screen -S yolo
yolo 是一个标记,可以自己随便填,用来分辨该窗口的用途,避免窗口多了自己混淆
画面共通コマンド
screen -ls #查看进程
screen -r -d 1020 #后台运行的screen进程
kill -9 1020 #杀死不需要的进程
screen -wipe #检查目前所有的screen作业,并删除已经无法使用的screen作业
2. 環境を再アクティブ化する
conda activate yolov5
3. トレーニングモデル
python train.py --cfg models/yolov5s.yaml --data data/ODID.yaml --hyp data/hyp.scratch.yaml --epochs 100 --multi-scale --device 0
6. Linux で一般的に使用されるいくつかのコマンド
- scp
サーバーからファイルをダウンロードする
scp username@IP:/remote_path/filename ~/local_destination
ローカルファイルをサーバーにアップロードする
scp ~/local_path/local_filename username@IP:/remote_path
从服务器下载整个目录
scp -r username@servername:/remote_path/remote_dir/ ~/local_destination
ディレクトリをサーバーにアップロードする
scp -r ~/local_dir username@servername:/remote_path/remote_dir
- vim 構成
システムレベルの構成ファイルのディレクトリ: /etc/vim/vimrc
ユーザーレベルの構成ファイルのディレクトリ: ~/.vim/vimrc
構成ファイル vimrc または .vimrc を変更します
//行番号を表示
set nu
- zip の圧縮および解凍
コマンド
zip -r file.zip ./*
現在のディレクトリ内のすべてのファイルとフォルダーを file.zip ファイルに圧縮します。 -r は、サブディレクトリ内のすべてのファイルを再帰的に圧縮することを意味します。
unzip コマンド
unzip -o -d /home/sunny file.zip は、
file.zip ファイルを /home/sunny/ に解凍します
。 -o: プロンプトを表示せずにファイルを上書きします。
-d:-d /home/sunny は、ファイルの解凍を指定します。それを /home/sunny ディレクトリに圧縮します。
その他
zip -d file.zip Smart.txt
圧縮ファイル内のsmart.txtファイルを削除
zip -m file.zip ./rpm_info.txt
rpm_info.txtファイルを圧縮ファイル内のfile.zipに追加
- ターミナルエージェント
export ALL_PROXY=socks5://127.0.0.1:4081 #端口号
curl -L cip.cc #验证是否成功
- グラフィックドライバー
nvidia-smi