From 0722d9c1ed390e38ee4ea95039b031414dc815d8 Mon Sep 17 00:00:00 2001 From: hwy <931445228@qq.com> Date: Wed, 19 Oct 2022 15:24:47 +0000 Subject: [PATCH 1/9] =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hwy <931445228@qq.com> --- .../configs/rank_table_8p.json | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 TensorFlow/contrib/cv/C3D_ID2199_for_TensorFlow/configs/rank_table_8p.json diff --git a/TensorFlow/contrib/cv/C3D_ID2199_for_TensorFlow/configs/rank_table_8p.json b/TensorFlow/contrib/cv/C3D_ID2199_for_TensorFlow/configs/rank_table_8p.json new file mode 100644 index 000000000..cd9041f3e --- /dev/null +++ b/TensorFlow/contrib/cv/C3D_ID2199_for_TensorFlow/configs/rank_table_8p.json @@ -0,0 +1,52 @@ +{ + "server_count":"1", + "server_list":[ + { + "server_id":"10.147.179.27", + "device":[ + { + "device_id":"0", + "device_ip":"192.168.100.100", + "rank_id":"0" + }, + { + "device_id":"1", + "device_ip":"192.168.101.100", + "rank_id":"1" + }, + { + "device_id":"2", + "device_ip":"192.168.102.100", + "rank_id":"2" + }, + { + "device_id":"3", + "device_ip":"192.168.103.100", + "rank_id":"3" + }, + { + "device_id":"4", + "device_ip":"192.168.100.101", + "rank_id":"4" + }, + { + "device_id":"5", + "device_ip":"192.168.101.101", + "rank_id":"5" + }, + { + "device_id":"6", + "device_ip":"192.168.102.101", + "rank_id":"6" + }, + { + "device_id":"7", + "device_ip":"192.168.103.101", + "rank_id":"7" + } + ] + } + ], + "status":"completed", + "version":"1.0" +} \ No newline at end of file -- Gitee From 47186c1cd6869da8718b25b5dacd7e61d2b6449c Mon Sep 17 00:00:00 2001 From: hwy <931445228@qq.com> Date: Wed, 19 Oct 2022 15:26:58 +0000 Subject: [PATCH 2/9] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20Tens?= =?UTF-8?q?orFlow/contrib/cv/C3D=5FID2199=5Ffor=5FTensorFlow/configs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../configs/rank_table_8p.json | 52 ------------------- 1 file changed, 52 deletions(-) delete mode 100644 TensorFlow/contrib/cv/C3D_ID2199_for_TensorFlow/configs/rank_table_8p.json diff --git a/TensorFlow/contrib/cv/C3D_ID2199_for_TensorFlow/configs/rank_table_8p.json b/TensorFlow/contrib/cv/C3D_ID2199_for_TensorFlow/configs/rank_table_8p.json deleted file mode 100644 index cd9041f3e..000000000 --- a/TensorFlow/contrib/cv/C3D_ID2199_for_TensorFlow/configs/rank_table_8p.json +++ /dev/null @@ -1,52 +0,0 @@ -{ - "server_count":"1", - "server_list":[ - { - "server_id":"10.147.179.27", - "device":[ - { - "device_id":"0", - "device_ip":"192.168.100.100", - "rank_id":"0" - }, - { - "device_id":"1", - "device_ip":"192.168.101.100", - "rank_id":"1" - }, - { - "device_id":"2", - "device_ip":"192.168.102.100", - "rank_id":"2" - }, - { - "device_id":"3", - "device_ip":"192.168.103.100", - "rank_id":"3" - }, - { - "device_id":"4", - "device_ip":"192.168.100.101", - "rank_id":"4" - }, - { - "device_id":"5", - "device_ip":"192.168.101.101", - "rank_id":"5" - }, - { - "device_id":"6", - "device_ip":"192.168.102.101", - "rank_id":"6" - }, - { - "device_id":"7", - "device_ip":"192.168.103.101", - "rank_id":"7" - } - ] - } - ], - "status":"completed", - "version":"1.0" -} \ No newline at end of file -- Gitee From a088f5837688b970764feca84798873f3ea4c262 Mon Sep 17 00:00:00 2001 From: hwy <931445228@qq.com> Date: Wed, 19 Oct 2022 15:27:22 +0000 Subject: [PATCH 3/9] =?UTF-8?q?C3D=208p=E9=85=8D=E7=BD=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hwy <931445228@qq.com> --- .../configs/rank_table_8p.json | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 TensorFlow/contrib/cv/C3D_ID2199_for_TensorFlow/configs/rank_table_8p.json diff --git a/TensorFlow/contrib/cv/C3D_ID2199_for_TensorFlow/configs/rank_table_8p.json b/TensorFlow/contrib/cv/C3D_ID2199_for_TensorFlow/configs/rank_table_8p.json new file mode 100644 index 000000000..cd9041f3e --- /dev/null +++ b/TensorFlow/contrib/cv/C3D_ID2199_for_TensorFlow/configs/rank_table_8p.json @@ -0,0 +1,52 @@ +{ + "server_count":"1", + "server_list":[ + { + "server_id":"10.147.179.27", + "device":[ + { + "device_id":"0", + "device_ip":"192.168.100.100", + "rank_id":"0" + }, + { + "device_id":"1", + "device_ip":"192.168.101.100", + "rank_id":"1" + }, + { + "device_id":"2", + "device_ip":"192.168.102.100", + "rank_id":"2" + }, + { + "device_id":"3", + "device_ip":"192.168.103.100", + "rank_id":"3" + }, + { + "device_id":"4", + "device_ip":"192.168.100.101", + "rank_id":"4" + }, + { + "device_id":"5", + "device_ip":"192.168.101.101", + "rank_id":"5" + }, + { + "device_id":"6", + "device_ip":"192.168.102.101", + "rank_id":"6" + }, + { + "device_id":"7", + "device_ip":"192.168.103.101", + "rank_id":"7" + } + ] + } + ], + "status":"completed", + "version":"1.0" +} \ No newline at end of file -- Gitee From d963f7e74fd2f3ca5131a179e771fae7fe477003 Mon Sep 17 00:00:00 2001 From: hwy <931445228@qq.com> Date: Wed, 19 Oct 2022 15:29:55 +0000 Subject: [PATCH 4/9] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20hwy?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TensorFlow/contrib/cv/C3D_ID2199_for_TensorFlow/hwy/.keep | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 TensorFlow/contrib/cv/C3D_ID2199_for_TensorFlow/hwy/.keep diff --git a/TensorFlow/contrib/cv/C3D_ID2199_for_TensorFlow/hwy/.keep b/TensorFlow/contrib/cv/C3D_ID2199_for_TensorFlow/hwy/.keep new file mode 100644 index 000000000..e69de29bb -- Gitee From c41e9cf969c6e07429448f8558615e3e5bd1ca5d Mon Sep 17 00:00:00 2001 From: hwy <931445228@qq.com> Date: Wed, 19 Oct 2022 15:30:47 +0000 Subject: [PATCH 5/9] =?UTF-8?q?8p=E8=BF=90=E8=A1=8C=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hwy <931445228@qq.com> --- .../test/train_full_8p.sh | 188 ++++++++++++++++++ 1 file changed, 188 insertions(+) create mode 100644 TensorFlow/contrib/cv/C3D_ID2199_for_TensorFlow/test/train_full_8p.sh diff --git a/TensorFlow/contrib/cv/C3D_ID2199_for_TensorFlow/test/train_full_8p.sh b/TensorFlow/contrib/cv/C3D_ID2199_for_TensorFlow/test/train_full_8p.sh new file mode 100644 index 000000000..0643f7b4d --- /dev/null +++ b/TensorFlow/contrib/cv/C3D_ID2199_for_TensorFlow/test/train_full_8p.sh @@ -0,0 +1,188 @@ +#!/bin/bash + +#当前路径,不需要修改 +cur_path=`pwd`/../ + +#集合通信参数,不需要修改 + +export RANK_SIZE=8 +export RANK_TABLE_FILE=${cur_path}/configs/rank_table_8p.json +export JOB_ID=10087 +RANK_ID_START=0 + + +# 数据集路径,保持为空,不需要修改 +data_path='' +ckpt_path='' + +#设置默认日志级别,不需要修改 +export ASCEND_GLOBAL_LOG_LEVEL=3 +#export ASCEND_DEVICE_ID=3 + +#基础参数,需要模型审视修改 +#网络名称,同目录名称 +Network="C3D_ID2199_for_TensorFlow" +#训练epoch +train_epochs=40 +#训练batch_size +batch_size=10 +#训练step +train_steps=`expr 1281167 / ${batch_size}` +#学习率 +learning_rate=0.495*8 + +#TF2.X独有,需要模型审视修改 +export NPU_LOOP_SIZE=${train_steps} + +#维测参数,precision_mode需要模型审视修改 +precision_mode="allow_mix_precision" +#维持参数,以下不需要修改 +over_dump=False +data_dump_flag=False +data_dump_step="10" +profiling=False + +# 帮助信息,不需要修改 +if [[ $1 == --help || $1 == -h ]];then + echo"usage:./train_performance_1P.sh " + echo " " + echo "parameter explain: + --precision_mode precision mode(allow_fp32_to_fp16/force_fp16/must_keep_origin_dtype/allow_mix_precision) + --over_dump if or not over detection, default is False + --data_dump_flag data dump flag, default is False + --data_dump_step data dump step, default is 10 + --profiling if or not profiling for performance debug, default is False + --data_path source data of training + -h/--help show help message + " + exit 1 +fi + +#参数校验,不需要修改 +for para in $* +do + if [[ $para == --precision_mode* ]];then + precision_mode=`echo ${para#*=}` + elif [[ $para == --over_dump* ]];then + over_dump=`echo ${para#*=}` + over_dump_path=${cur_path}/test/output/overflow_dump + mkdir -p ${over_dump_path} + elif [[ $para == --data_dump_flag* ]];then + data_dump_flag=`echo ${para#*=}` + data_dump_path=${cur_path}/test/output/data_dump + mkdir -p ${data_dump_path} + elif [[ $para == --data_dump_step* ]];then + data_dump_step=`echo ${para#*=}` + elif [[ $para == --profiling* ]];then + profiling=`echo ${para#*=}` + profiling_dump_path=${cur_path}/test/output/profiling + mkdir -p ${profiling_dump_path} + elif [[ $para == --data_path* ]];then + data_path=`echo ${para#*=}` + + elif [[ $para == --ckpt_path* ]];then + ckpt_path=`echo ${para#*=}` + fi +done + +#校验是否传入data_path,不需要修改 +if [[ $data_path == "" ]];then + echo "[Error] para \"data_path\" must be confing" + exit 1 + +fi + +#训练开始时间,不需要修改 +start_time=$(date +%s) + +#进入训练脚本目录,需要模型审视修改 +cd $cur_path/ +for((RANK_ID=$RANK_ID_START;RANK_ID<$((RANK_SIZE+RANK_ID_START));RANK_ID++)); +do + #设置环境变量,不需要修改 + echo "Device ID: $ASCEND_DEVICE_ID" + export RANK_ID=$RANK_ID + export ASCEND_DEVICE_ID=$RANK_ID + ASCEND_DEVICE_ID=$RANK_ID + DEVICE_INDEX=$RANK_ID + export DEVICE_INDEX=${DEVICE_INDEX} + + + + #创建DeviceID输出目录,不需要修改 + if [ -d ${cur_path}/test/output/${ASCEND_DEVICE_ID} ];then + rm -rf ${cur_path}/test/output/${ASCEND_DEVICE_ID} + mkdir -p ${cur_path}/test/output/$ASCEND_DEVICE_ID/ckpt + else + mkdir -p ${cur_path}/test/output/$ASCEND_DEVICE_ID/ckpt + fi + + + + + # 绑核,不需要的绑核的模型删除,需要的模型审视修改 + let a=RANK_ID*12 + let b=RANK_ID+1 + let c=b*12-1 + + + #执行训练脚本,以下传参不需要修改,其他需要模型审视修改 + #--data_dir, --model_dir, --precision_mode, --over_dump, --over_dump_path,--data_dump_flag,--data_dump_step,--data_dump_path,--profiling,--profiling_dump_path + #sed -i "s|"/home/test_user05/result"|"${cur_path}/test"|g" run_npu.sh + #sed -i "s|"/home/test_user05"|"${data_path}"|g" run_npu.sh + sed -i "s|"code_dir"|"${ckpt_path}"|g" data_processing.py + + nohup python3 train_c3d.py \ + --result=${cur_path}/test/ \ + --batch_size=${batch_size} \ + --data_dir=${data_path} > ${cur_path}test/output/${ASCEND_DEVICE_ID}/train_${ASCEND_DEVICE_ID}.log 2>&1 & + + #sed -i "s|"${data_path}"|"/home/test_user05"|g" run_npu.sh + #sed -i "s|"${cur_path}/test"|"/home/test_user05/result"|g" run_npu.sh + #sed -i "s|"${ckpt_path}"|"code_dir"|g" data_processing.py + +done +wait + +#训练结束时间,不需要修改 +end_time=$(date +%s) +e2e_time=$(( $end_time - $start_time )) + +#结果打印,不需要修改 +echo "------------------ Final result ------------------" +#输出性能FPS,需要模型审视修改 +TrainingTime=`grep "Train Time" $cur_path/test/output/${ASCEND_DEVICE_ID}/train_${ASCEND_DEVICE_ID}.log |awk 'END {print}' |awk '{print $5}'` + +#输出训练精度,需要模型审视修改 +train_acc=`grep "Test accuracy" $cur_path/test/output/${ASCEND_DEVICE_ID}/train_${ASCEND_DEVICE_ID}.log|awk '{print $4}'` +#打印,不需要修改 +echo "Final Train Accuracy : ${train_acc}" +echo "E2E Training Duration sec : $e2e_time" + +#性能看护结果汇总 +#训练用例信息,不需要修改 +BatchSize=${batch_size} +DeviceType=`uname -m` +CaseName=${Network}_bs${BatchSize}_${RANK_SIZE}'p'_'acc' + +##获取性能数据,不需要修改 +#吞吐量 +ActualFPS=`awk 'BEGIN{printf "%.2f\n", 10/'${TrainingTime}'}'` + +#从train_$ASCEND_DEVICE_ID.log提取Loss到train_${CaseName}_loss.txt中,需要根据模型审视 +grep 'Average loss is:' $cur_path/test/output/$ASCEND_DEVICE_ID/train_$ASCEND_DEVICE_ID.log|awk '{print $6}' > $cur_path/test/output/$ASCEND_DEVICE_ID/train_${CaseName}_loss.txt + +#最后一个迭代loss值,不需要修改 +ActualLoss=`awk 'END {print}' $cur_path/test/output/$ASCEND_DEVICE_ID/train_${CaseName}_loss.txt` + +#关键信息打印到${CaseName}.log中,不需要修改 +echo "Network = ${Network}" > $cur_path/test/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "RankSize = ${RANK_SIZE}" >> $cur_path/test/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "BatchSize = ${BatchSize}" >> $cur_path/test/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "DeviceType = ${DeviceType}" >> $cur_path/test/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "CaseName = ${CaseName}" >> $cur_path/test/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "ActualFPS = ${ActualFPS}" >> $cur_path/test/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "TrainingTime = ${TrainingTime}" >> $cur_path/test/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "ActualLoss = ${ActualLoss}" >> $cur_path/test/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "TrainAccuracy = ${train_acc}" >> $cur_path/test/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "E2ETrainingTime = ${e2e_time}" >> $cur_path/test/output/$ASCEND_DEVICE_ID/${CaseName}.log -- Gitee From 98f317f28d130407afec6686c52cf0e6e4fd44be Mon Sep 17 00:00:00 2001 From: hwy <931445228@qq.com> Date: Wed, 19 Oct 2022 15:31:34 +0000 Subject: [PATCH 6/9] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20Tens?= =?UTF-8?q?orFlow/contrib/cv/C3D=5FID2199=5Ffor=5FTensorFlow/data=5Fproces?= =?UTF-8?q?sing.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../data_processing.py | 130 ------------------ 1 file changed, 130 deletions(-) delete mode 100644 TensorFlow/contrib/cv/C3D_ID2199_for_TensorFlow/data_processing.py diff --git a/TensorFlow/contrib/cv/C3D_ID2199_for_TensorFlow/data_processing.py b/TensorFlow/contrib/cv/C3D_ID2199_for_TensorFlow/data_processing.py deleted file mode 100644 index de97a6b8d..000000000 --- a/TensorFlow/contrib/cv/C3D_ID2199_for_TensorFlow/data_processing.py +++ /dev/null @@ -1,130 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================ -# Copyright 2021 Huawei Technologies Co., Ltd -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import PIL.Image as Image -import random -import numpy as np -import os -import time -import cv2 - -CLIP_LENGTH = 16 -VALIDATION_PRO = 0.2 - -np_mean = np.load('code_dir' + '/crop_mean.npy').reshape([CLIP_LENGTH, 112, 112, 3]) - - -def get_test_num(filename): - lines = open(filename, 'r') - return len(list(lines)) - - -def get_video_indices(filename): - lines = open(filename, 'r') - # Shuffle data - lines = list(lines) - video_indices = list(range(len(lines))) - random.seed(time.time()) - random.shuffle(video_indices) - validation_video_indices = video_indices[:int(len(video_indices) * 0.2)] - train_video_indices = video_indices[int(len(video_indices) * 0.2):] - return train_video_indices, validation_video_indices - - -def frame_process(clip, crop_size=112, channel_num=3): - frames_num = len(clip) - croped_frames = np.zeros([frames_num, crop_size, crop_size, channel_num]).astype(np.float32) - - # Crop every frame into shape[crop_size, crop_size, channel_num] - for i in range(frames_num): - img = Image.fromarray(clip[i].astype(np.uint8)) - if img.width > img.height: - scale = float(crop_size) / float(img.height) - img = np.array(cv2.resize(np.array(img), (int(img.width * scale + 1), crop_size))).astype(np.float32) - else: - scale = float(crop_size) / float(img.width) - img = np.array(cv2.resize(np.array(img), (crop_size, int(img.height * scale + 1)))).astype(np.float32) - crop_x = int((img.shape[0] - crop_size) / 2) - crop_y = int((img.shape[1] - crop_size) / 2) - img = img[crop_x: crop_x + crop_size, crop_y: crop_y + crop_size, :] - croped_frames[i, :, :, :] = img - np_mean[i] - - return croped_frames - - -def convert_images_to_clip(filename, clip_length=CLIP_LENGTH, crop_size=112, channel_num=3): - clip = [] - for parent, dirnames, filenames in os.walk(filename): - filenames = sorted(filenames) - if len(filenames) < clip_length: - for i in range(0, len(filenames)): - image_name = str(filename) + '/' + str(filenames[i]) - img = Image.open(image_name) - img_data = np.array(img) - clip.append(img_data) - for i in range(clip_length - len(filenames)): - image_name = str(filename) + '/' + str(filenames[len(filenames) - 1]) - img = Image.open(image_name) - img_data = np.array(img) - clip.append(img_data) - else: - s_index = random.randint(0, len(filenames) - clip_length) - for i in range(s_index, s_index + clip_length): - image_name = str(filename) + '/' + str(filenames[i]) - img = Image.open(image_name) - img_data = np.array(img) - clip.append(img_data) - if len(clip) == 0: - print(filename) - clip = frame_process(clip, crop_size, channel_num) - return clip # shape[clip_length, crop_size, crop_size, channel_num] - - -def get_batches(filename, num_classes, batch_index, video_indices, batch_size=10, crop_size=112, channel_num=3): - lines = open(filename, 'r') - clips = [] - labels = [] - lines = list(lines) - for i in video_indices[batch_index: batch_index + batch_size]: - line = lines[i].strip('\n').split() - dirname = line[0] - label = line[1] - i_clip = convert_images_to_clip(dirname, CLIP_LENGTH, crop_size, channel_num) - clips.append(i_clip) - labels.append(int(label)) - clips = np.array(clips).astype(np.float32) - labels = np.array(labels).astype(np.int64) - oh_labels = np.zeros([len(labels), num_classes]).astype(np.int64) - for i in range(len(labels)): - oh_labels[i, labels[i]] = 1 - batch_index = batch_index + batch_size - batch_data = {'clips': clips, 'labels': oh_labels} - return batch_data, batch_index -- Gitee From e7df52f04c8f90c37c086f2db7e5b28b119ed083 Mon Sep 17 00:00:00 2001 From: hwy <931445228@qq.com> Date: Wed, 19 Oct 2022 15:31:51 +0000 Subject: [PATCH 7/9] add codes Signed-off-by: hwy <931445228@qq.com> --- .../data_processing.py | 130 ++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 TensorFlow/contrib/cv/C3D_ID2199_for_TensorFlow/data_processing.py diff --git a/TensorFlow/contrib/cv/C3D_ID2199_for_TensorFlow/data_processing.py b/TensorFlow/contrib/cv/C3D_ID2199_for_TensorFlow/data_processing.py new file mode 100644 index 000000000..34f4cd88d --- /dev/null +++ b/TensorFlow/contrib/cv/C3D_ID2199_for_TensorFlow/data_processing.py @@ -0,0 +1,130 @@ +# Copyright 2017 The TensorFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ +# Copyright 2021 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + +import PIL.Image as Image +import random +import numpy as np +import os +import time +import cv2 + +CLIP_LENGTH = 16 +VALIDATION_PRO = 0.2 + +np_mean = np.load('code_dir' + '/crop_mean.npy').reshape([CLIP_LENGTH, 112, 112, 3]) + + +def get_test_num(filename): + lines = open(filename, 'r') + return len(list(lines)) + + +def get_video_indices(filename): + lines = open(filename, 'r') + # Shuffle data + lines = list(lines) + video_indices = list(range(len(lines))) + random.seed(time.time()) + random.shuffle(video_indices) + validation_video_indices = video_indices[:int(len(video_indices) * 0.2)] + train_video_indices = video_indices[int(len(video_indices) * 0.2):] + return train_video_indices, validation_video_indices + + +def frame_process(clip, crop_size=112, channel_num=3): + frames_num = len(clip) + croped_frames = np.zeros([frames_num, crop_size, crop_size, channel_num]).astype(np.float32) + + # Crop every frame into shape[crop_size, crop_size, channel_num] + for i in range(frames_num): + img = Image.fromarray(clip[i].astype(np.uint8)) + if img.width > img.height: + scale = float(crop_size) / float(img.height) + img = np.array(cv2.resize(np.array(img), (int(img.width * scale + 1), crop_size))).astype(np.float32) + else: + scale = float(crop_size) / float(img.width) + img = np.array(cv2.resize(np.array(img), (crop_size, int(img.height * scale + 1)))).astype(np.float32) + crop_x = int((img.shape[0] - crop_size) / 2) + crop_y = int((img.shape[1] - crop_size) / 2) + img = img[crop_x: crop_x + crop_size, crop_y: crop_y + crop_size, :] + croped_frames[i, :, :, :] = img - np_mean[i] + + return croped_frames + + +def convert_images_to_clip(filename, clip_length=CLIP_LENGTH, crop_size=112, channel_num=3): + clip = [] + for parent, dirnames, filenames in os.walk(filename): + filenames = sorted(filenames) + if len(filenames) < clip_length: + for i in range(0, len(filenames)): + image_name = str(filename) + '/' + str(filenames[i]) + img = Image.open(image_name) + img_data = np.array(img) + clip.append(img_data) + for i in range(clip_length - len(filenames)): + image_name = str(filename) + '/' + str(filenames[len(filenames) - 1]) + img = Image.open(image_name) + img_data = np.array(img) + clip.append(img_data) + else: + s_index = random.randint(0, len(filenames) - clip_length) + for i in range(s_index, s_index + clip_length): + image_name = str(filename) + '/' + str(filenames[i]) + img = Image.open(image_name) + img_data = np.array(img) + clip.append(img_data) + if len(clip) == 0: + print(filename) + clip = frame_process(clip, crop_size, channel_num) + return clip # shape[clip_length, crop_size, crop_size, channel_num] + + +def get_batches(filename, num_classes, batch_index, video_indices, batch_size=10, rank_size=1, crop_size=112, channel_num=3 ): + lines = open(filename, 'r') + clips = [] + labels = [] + lines = list(lines) + for i in video_indices[batch_index: batch_index + batch_size]: + line = lines[i].strip('\n').split() + dirname = line[0] + label = line[1] + i_clip = convert_images_to_clip(dirname, CLIP_LENGTH, crop_size, channel_num) + clips.append(i_clip) + labels.append(int(label)) + clips = np.array(clips).astype(np.float32) + labels = np.array(labels).astype(np.int64) + oh_labels = np.zeros([len(labels), num_classes]).astype(np.int64) + for i in range(len(labels)): + oh_labels[i, labels[i]] = 1 + batch_index = batch_index + batch_size*rank_size + batch_data = {'clips': clips, 'labels': oh_labels} + return batch_data, batch_index -- Gitee From 01265cfc4db5703ada78f76bcb49917e27301680 Mon Sep 17 00:00:00 2001 From: hwy <931445228@qq.com> Date: Wed, 19 Oct 2022 15:32:14 +0000 Subject: [PATCH 8/9] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20Tens?= =?UTF-8?q?orFlow/contrib/cv/C3D=5FID2199=5Ffor=5FTensorFlow/train=5Fc3d.p?= =?UTF-8?q?y?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cv/C3D_ID2199_for_TensorFlow/train_c3d.py | 164 ------------------ 1 file changed, 164 deletions(-) delete mode 100644 TensorFlow/contrib/cv/C3D_ID2199_for_TensorFlow/train_c3d.py diff --git a/TensorFlow/contrib/cv/C3D_ID2199_for_TensorFlow/train_c3d.py b/TensorFlow/contrib/cv/C3D_ID2199_for_TensorFlow/train_c3d.py deleted file mode 100644 index f6085bccc..000000000 --- a/TensorFlow/contrib/cv/C3D_ID2199_for_TensorFlow/train_c3d.py +++ /dev/null @@ -1,164 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================ -# Copyright 2021 Huawei Technologies Co., Ltd -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -import tensorflow as tf -import numpy as np -import C3D_model -import data_processing -import os.path -import time -from npu_config import npu_config -from npu_bridge.npu_init import * - -tf.logging.set_verbosity(tf.logging.ERROR) - - -flags = tf.flags -flags.DEFINE_integer('max_steps', 100, 'Number of steps to run trainer.') -flags.DEFINE_integer('batch_size', 10, 'Batch size.') -flags.DEFINE_string("result", "result", "train model result") -flags.DEFINE_string("train_model_url", "train_model_url", "save model checkpoint url") -flags.DEFINE_boolean("profiling", False, "model profiling start") -flags.DEFINE_string("data_dir", "data_dir", "dataset dir") -FLAGS = flags.FLAGS - -TRAIN_LOG_DIR = FLAGS.result -TRAIN_CHECK_POINT = FLAGS.train_model_url -TRAIN_LIST_PATH = FLAGS.data_dir + '/train.list' -TEST_LIST_PATH = FLAGS.data_dir + '/test.list' -BATCH_SIZE = FLAGS.batch_size -NUM_CLASSES = 101 -CROP_SZIE = 112 -CHANNEL_NUM = 3 -CLIP_LENGTH = 16 -EPOCH_NUM = FLAGS.max_steps -LR_DECAY_FACTOR = 0.5 -EPOCHS_PER_LR_DECAY = 2 -MOVING_AV_DECAY = 0.9999 - -# Get shuffle index -train_video_indices, validation_video_indices = data_processing.get_video_indices(TRAIN_LIST_PATH) - -with tf.Graph().as_default(): - batch_clips = tf.placeholder(tf.float32, [BATCH_SIZE, CLIP_LENGTH, CROP_SZIE, CROP_SZIE, CHANNEL_NUM], name='X') - batch_labels = tf.placeholder(tf.int32, [BATCH_SIZE, NUM_CLASSES], name='Y') - keep_prob = tf.placeholder(tf.float32) - logits = C3D_model.C3D(batch_clips, NUM_CLASSES, keep_prob) - - with tf.name_scope('loss'): - loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=batch_labels)) - tf.summary.scalar('entropy_loss', loss) - - with tf.name_scope('accuracy'): - accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(logits, 1), tf.argmax(batch_labels, 1)), np.float32)) - tf.summary.scalar('accuracy', accuracy) - # global_step = tf.Variable(0, name='global_step', trainable=False) - # decay_step = EPOCHS_PER_LR_DECAY * len(train_video_indices) // BATCH_SIZE - learning_rate = 1e-4 - optimizer = tf.train.AdamOptimizer(learning_rate).minimize(loss) # , global_step=global_step) - saver = tf.train.Saver() - summary_op = tf.summary.merge_all() - config = npu_config(FLAGS) - with tf.Session(config=config) as sess: - train_summary_writer = tf.summary.FileWriter(TRAIN_LOG_DIR, sess.graph) - sess.run(tf.global_variables_initializer()) - sess.run(tf.local_variables_initializer()) - step = 0 - for epoch in range(EPOCH_NUM): - - accuracy_epoch = 0 - loss_epoch = 0 - batch_index = 0 - for i in range(len(train_video_indices) // BATCH_SIZE): - step += 1 - batch_data, batch_index = data_processing.get_batches(TRAIN_LIST_PATH, NUM_CLASSES, batch_index, - train_video_indices, BATCH_SIZE) - train_time = time.time() - _, loss_out, accuracy_out, summary = sess.run([optimizer, loss, accuracy, summary_op], - feed_dict={batch_clips: batch_data['clips'], - batch_labels: batch_data['labels'], - keep_prob: 0.5}) - loss_epoch += loss_out - accuracy_epoch += accuracy_out - print("%d Train Time = %.3f" % (step, time.time() - train_time)) - - if i % 10 == 0: - # print('Epoch %d, Batch %d: Loss is %.5f; Accuracy is %.5f' % (epoch + 1, i, loss_out, accuracy_out)) - train_summary_writer.add_summary(summary, step) - print('Epoch %d: Average loss is: %.5f ; Average accuracy is: %.5f' % ( - epoch + 1, loss_epoch / (len(train_video_indices) // BATCH_SIZE), - accuracy_epoch / (len(train_video_indices) // BATCH_SIZE))) - accuracy_epoch = 0 - loss_epoch = 0 - batch_index = 0 - for i in range(len(validation_video_indices) // BATCH_SIZE): - batch_data, batch_index = data_processing.get_batches(TRAIN_LIST_PATH, NUM_CLASSES, batch_index, - validation_video_indices, BATCH_SIZE) - loss_out, accuracy_out = sess.run([loss, accuracy], - feed_dict={batch_clips: batch_data['clips'], - batch_labels: batch_data['labels'], - keep_prob: 1.0}) - loss_epoch += loss_out - accuracy_epoch += accuracy_out - - print('Validation loss is %.5f ; Accuracy is %.5f' % ( - loss_epoch / (len(validation_video_indices) // BATCH_SIZE), - accuracy_epoch / (len(validation_video_indices) // BATCH_SIZE))) - #print("%d Train Time = %.3f" % (epoch + 1, time.time() - train_time)) - saver.save(sess, TRAIN_CHECK_POINT + '/train.ckpt', global_step=epoch) - - -# test data -test_num = data_processing.get_test_num(TEST_LIST_PATH) -test_video_indices = range(test_num) - -with tf.Graph().as_default(): - batch_clips = tf.placeholder(tf.float32, [BATCH_SIZE, CLIP_LENGTH, CROP_SZIE, CROP_SZIE, CHANNEL_NUM], name='X') - batch_labels = tf.placeholder(tf.int32, [BATCH_SIZE, NUM_CLASSES], name='Y') - keep_prob = tf.placeholder(tf.float32) - logits = C3D_model.C3D(batch_clips, NUM_CLASSES, keep_prob) - accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(logits, 1), tf.argmax(batch_labels, 1)), np.float32)) - - restorer = tf.train.Saver() - config = npu_config(FLAGS) - with tf.Session(config=config) as sess: - sess.run(tf.global_variables_initializer()) - sess.run(tf.local_variables_initializer()) - ckpt = tf.train.get_checkpoint_state(TRAIN_CHECK_POINT) - if ckpt: - restorer.restore(sess, ckpt.model_checkpoint_path) - accuracy_epoch = 0 - batch_index = 0 - for i in range(test_num // BATCH_SIZE): - batch_data, batch_index = data_processing.get_batches(TEST_LIST_PATH, NUM_CLASSES, batch_index, - test_video_indices, BATCH_SIZE) - accuracy_out = sess.run(accuracy, feed_dict={batch_clips: batch_data['clips'], - batch_labels: batch_data['labels'], - keep_prob: 1.0}) - accuracy_epoch += accuracy_out - - print('Test accuracy is %.5f' % (accuracy_epoch / (test_num // BATCH_SIZE))) -- Gitee From d434b60371123f9c42ebf51d088ddf4af79944a9 Mon Sep 17 00:00:00 2001 From: hwy <931445228@qq.com> Date: Wed, 19 Oct 2022 15:32:31 +0000 Subject: [PATCH 9/9] add codes Signed-off-by: hwy <931445228@qq.com> --- .../cv/C3D_ID2199_for_TensorFlow/train_c3d.py | 189 ++++++++++++++++++ 1 file changed, 189 insertions(+) create mode 100644 TensorFlow/contrib/cv/C3D_ID2199_for_TensorFlow/train_c3d.py diff --git a/TensorFlow/contrib/cv/C3D_ID2199_for_TensorFlow/train_c3d.py b/TensorFlow/contrib/cv/C3D_ID2199_for_TensorFlow/train_c3d.py new file mode 100644 index 000000000..fbf3f324a --- /dev/null +++ b/TensorFlow/contrib/cv/C3D_ID2199_for_TensorFlow/train_c3d.py @@ -0,0 +1,189 @@ +# Copyright 2017 The TensorFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ +# Copyright 2021 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import tensorflow as tf +import numpy as np +import C3D_model +import data_processing +import os.path +import time +from npu_config import npu_config +from npu_bridge.npu_init import * + +tf.logging.set_verbosity(tf.logging.ERROR) + + +flags = tf.flags +flags.DEFINE_integer('max_steps', 100, 'Number of steps to run trainer.') +flags.DEFINE_integer('batch_size', 10, 'Batch size.') +flags.DEFINE_string("result", "result", "train model result") +flags.DEFINE_string("train_model_url", "train_model_url", "save model checkpoint url") +flags.DEFINE_boolean("profiling", False, "model profiling start") +flags.DEFINE_string("data_dir", "data_dir", "dataset dir") +FLAGS = flags.FLAGS + +TRAIN_LOG_DIR = FLAGS.result +TRAIN_CHECK_POINT = FLAGS.train_model_url +TRAIN_LIST_PATH = FLAGS.data_dir + '/train.list' +TEST_LIST_PATH = FLAGS.data_dir + '/test.list' +BATCH_SIZE = FLAGS.batch_size +NUM_CLASSES = 102 +CROP_SZIE = 112 +CHANNEL_NUM = 3 +CLIP_LENGTH = 16 +EPOCH_NUM = FLAGS.max_steps +LR_DECAY_FACTOR = 0.5 +EPOCHS_PER_LR_DECAY = 2 +MOVING_AV_DECAY = 0.9999 + +# Get shuffle index +train_video_indices, validation_video_indices = data_processing.get_video_indices(TRAIN_LIST_PATH) + +RANK_SIZE = int(os.getenv('RANK_SIZE')) +rank_id = int(os.getenv('DEVICE_INDEX')) + + +with tf.Graph().as_default(): + batch_clips = tf.placeholder(tf.float32, [BATCH_SIZE, CLIP_LENGTH, CROP_SZIE, CROP_SZIE, CHANNEL_NUM], name='X') + batch_labels = tf.placeholder(tf.int32, [BATCH_SIZE, NUM_CLASSES], name='Y') + keep_prob = tf.placeholder(tf.float32) + logits = C3D_model.C3D(batch_clips, NUM_CLASSES, keep_prob) + + with tf.name_scope('loss'): + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=batch_labels)) + tf.summary.scalar('entropy_loss', loss) + + with tf.name_scope('accuracy'): + accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(logits, 1), tf.argmax(batch_labels, 1)), np.float32)) + tf.summary.scalar('accuracy', accuracy) + # global_step = tf.Variable(0, name='global_step', trainable=False) + # decay_step = EPOCHS_PER_LR_DECAY * len(train_video_indices) // BATCH_SIZE + learning_rate = 1e-4 + if int(RANK_SIZE) > 1: + optimizer = tf.train.AdamOptimizer(learning_rate) # .minimize(loss) # , global_step=global_step) + optimizer = npu_distributed_optimizer_wrapper(optimizer).minimize(loss) + else: + optimizer = tf.train.AdamOptimizer(learning_rate).minimize(loss) # , global_step=global_step) + saver = tf.train.Saver() + summary_op = tf.summary.merge_all() + + config = tf.ConfigProto() + custom_op = config.graph_options.rewrite_options.custom_optimizers.add() + custom_op.name = "NpuOptimizer" + custom_op.parameter_map["hcom_parallel"].b = True + config.graph_options.rewrite_options.remapping = RewriterConfig.OFF # 必须显式关闭 + config.graph_options.rewrite_options.memory_optimization = RewriterConfig.OFF # 必须显式关闭 + + with tf.Session(config=config) as sess: + train_summary_writer = tf.summary.FileWriter(TRAIN_LOG_DIR, sess.graph) + sess.run(tf.global_variables_initializer()) + sess.run(tf.local_variables_initializer()) + if int(RANK_SIZE) > 1: + input = tf.trainable_variables() + bcast_global_variables_op = hccl_ops.broadcast(input, 0) + sess.run(bcast_global_variables_op) + step = 0 + for epoch in range(EPOCH_NUM): + + accuracy_epoch = 0 + loss_epoch = 0 + batch_index = 0+rank_id*BATCH_SIZE + for i in range(len(train_video_indices) // (BATCH_SIZE*RANK_SIZE)): + step += 1 + batch_data, batch_index = data_processing.get_batches(TRAIN_LIST_PATH, NUM_CLASSES, batch_index, + train_video_indices, BATCH_SIZE, RANK_SIZE) + train_time = time.time() + _, loss_out, accuracy_out, summary = sess.run([optimizer, loss, accuracy, summary_op], + feed_dict={batch_clips: batch_data['clips'], + batch_labels: batch_data['labels'], + keep_prob: 0.5}) + loss_epoch += loss_out + accuracy_epoch += accuracy_out + print("%d Train Time = %.3f" % (step, time.time() - train_time)) + + if i % 10 == 0: + # print('Epoch %d, Batch %d: Loss is %.5f; Accuracy is %.5f' % (epoch + 1, i, loss_out, accuracy_out)) + train_summary_writer.add_summary(summary, step) + print('Epoch %d: Average loss is: %.5f ; Average accuracy is: %.5f' % ( + epoch + 1, loss_epoch / (len(train_video_indices) // (BATCH_SIZE*RANK_SIZE)), + accuracy_epoch / (len(train_video_indices) // (BATCH_SIZE*RANK_SIZE)))) + accuracy_epoch = 0 + loss_epoch = 0 + # batch_index = 0+rank_id*BATCH_SIZE + batch_index=0 + for i in range(len(validation_video_indices) // (BATCH_SIZE)): + batch_data, batch_index = data_processing.get_batches(TRAIN_LIST_PATH, NUM_CLASSES, batch_index, + validation_video_indices, BATCH_SIZE, 1) + loss_out, accuracy_out = sess.run([loss, accuracy], + feed_dict={batch_clips: batch_data['clips'], + batch_labels: batch_data['labels'], + keep_prob: 1.0}) + loss_epoch += loss_out + accuracy_epoch += accuracy_out + + print('Validation loss is %.5f ; Accuracy is %.5f' % ( + loss_epoch / (len(validation_video_indices) // (BATCH_SIZE)), + accuracy_epoch / (len(validation_video_indices) // (BATCH_SIZE)))) + #print("%d Train Time = %.3f" % (epoch + 1, time.time() - train_time)) + saver.save(sess, TRAIN_CHECK_POINT + '/train.ckpt', global_step=epoch) + +if(rank_id==RANK_SIZE-1): + # test data + test_num = data_processing.get_test_num(TEST_LIST_PATH) + test_video_indices = range(test_num) + + with tf.Graph().as_default(): + batch_clips = tf.placeholder(tf.float32, [BATCH_SIZE, CLIP_LENGTH, CROP_SZIE, CROP_SZIE, CHANNEL_NUM], name='X') + batch_labels = tf.placeholder(tf.int32, [BATCH_SIZE, NUM_CLASSES], name='Y') + keep_prob = tf.placeholder(tf.float32) + logits = C3D_model.C3D(batch_clips, NUM_CLASSES, keep_prob) + accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(logits, 1), tf.argmax(batch_labels, 1)), np.float32)) + + restorer = tf.train.Saver() + config = npu_config(FLAGS) + with tf.Session(config=config) as sess: + sess.run(tf.global_variables_initializer()) + sess.run(tf.local_variables_initializer()) + # if int(RANK_SIZE) > 1: + # input = tf.trainable_variables() + # bcast_global_variables_op = hccl_ops.broadcast(input, 0) + # sess.run(bcast_global_variables_op) + ckpt = tf.train.get_checkpoint_state(TRAIN_CHECK_POINT) + if ckpt: + restorer.restore(sess, ckpt.model_checkpoint_path) + accuracy_epoch = 0 + batch_index = 0 + # batch_index = 0+rank_id*BATCH_SIZE + for i in range(test_num // (BATCH_SIZE)): + batch_data, batch_index = data_processing.get_batches(TEST_LIST_PATH, NUM_CLASSES, batch_index, + test_video_indices, BATCH_SIZE, 1) + accuracy_out = sess.run(accuracy, feed_dict={batch_clips: batch_data['clips'], + batch_labels: batch_data['labels'], + keep_prob: 1.0}) + accuracy_epoch += accuracy_out + + print('Test accuracy is %.5f' % (accuracy_epoch / (test_num // (BATCH_SIZE*1)))) -- Gitee