### Install Paddle Serving Docker Environment Source: https://paddleclas.readthedocs.io/zh-cn/latest/extension/paddle_serving.html Pull the Paddle Serving Docker image and run a container for GPU-accelerated serving. Access the container's bash shell for further setup. ```bash nvidia-docker pull hub.baidubce.com/paddlepaddle/serving:0.2.0-gpu nvidia-docker run -p 9292:9292 --name test -dit hub.baidubce.com/paddlepaddle/serving:0.2.0-gpu nvidia-docker exec -it test bash ``` -------------------------------- ### Start VisualDL for Monitoring Source: https://paddleclas.readthedocs.io/zh-cn/latest/tutorials/getting_started.html Launch VisualDL to monitor training progress in real-time, such as loss changes. Specify the log directory using --logdir. ```bash visualdl --logdir ./scalar --host --port ``` -------------------------------- ### Install Paddle Serving CPU Server Package Source: https://paddleclas.readthedocs.io/zh-cn/latest/extension/paddle_serving.html Install the CPU version of the Paddle Serving server package if you intend to deploy a CPU-only service. ```python pip install paddle-serving-server ``` -------------------------------- ### Install Paddle Serving Python Packages Source: https://paddleclas.readthedocs.io/zh-cn/latest/extension/paddle_serving.html Install the necessary Paddle Serving Python client and server packages within the Docker container. Use a mirror for faster installation if needed. ```python pip install paddlepaddle-gpu pip install paddle-serving-client pip install paddle-serving-server-gpu ``` -------------------------------- ### Start Multi-GPU Training Source: https://paddleclas.readthedocs.io/zh-cn/latest/tutorials/getting_started.html Initiate multi-GPU and multi-process training using paddle.distributed.launch. Specify the GPUs to use with --selected_gpus and the configuration file with -c. ```python # PaddleClas通过launch方式启动多卡多进程训练 # 通过设置FLAGS_selected_gpus 指定GPU运行卡号 python -m paddle.distributed.launch \ --selected_gpus="0,1,2,3" \ tools/train.py \ -c ./configs/ResNet/ResNet50_vd.yaml ``` -------------------------------- ### Training Script Configuration for Cutout Source: https://paddleclas.readthedocs.io/zh-cn/latest/advanced_tutorials/image_augmentation/ImageAugment.html Example of a training script configuration using the `run.sh` utility. This snippet shows how to specify a configuration file for the Cutout data augmentation strategy. ```bash export PYTHONPATH=path_to_PaddleClas:$PYTHONPATH python -m paddle.distributed.launch \ --selected_gpus="0,1,2,3" \ --log_dir=ResNet50_Cutout \ tools/train.py \ -c ./configs/DataAugment/ResNet50_Cutout.yaml ``` -------------------------------- ### SSLD Distillation Configuration Example Source: https://paddleclas.readthedocs.io/zh-cn/latest/advanced_tutorials/distillation/distillation.html Configuration for distilling ResNeXt101_32x16d_wsl into ResNet50_vd using SSLD. It specifies the architecture name, pretrained model paths for teacher and student, and enables distillation. ```yaml ARCHITECTURE: name: 'ResNeXt101_32x16d_wsl_distill_ResNet50_vd' pretrained_model: "./pretrained/ResNeXt101_32x16d_wsl_pretrained/" # pretrained_model: # - "./pretrained/ResNeXt101_32x16d_wsl_pretrained/" # - "./pretrained/ResNet50_vd_pretrained/" use_distillation: True ``` -------------------------------- ### Start Multi-GPU Training with Overrides Source: https://paddleclas.readthedocs.io/zh-cn/latest/tutorials/getting_started.html Start multi-GPU training and override configuration parameters using the -o flag. This allows for dynamic adjustments to training settings like mixed precision and VisualDL logging directory. ```python python -m paddle.distributed.launch \ --selected_gpus="0,1,2,3" \ tools/train.py \ -c ./configs/ResNet/ResNet50_vd.yaml \ -o use_mix=1 \ --vdl_dir=./scalar/ ``` -------------------------------- ### PaddleClas Distillation Training Command Source: https://paddleclas.readthedocs.io/zh-cn/latest/advanced_tutorials/distillation/distillation.html Command to launch the distillation training process. Replace `path_to_PaddleClas` with the actual path to your PaddleClas installation. ```bash export PYTHONPATH=path_to_PaddleClas:$PYTHONPATH python -m paddle.distributed.launch \ --selected_gpus="0,1,2,3" \ --log_dir=R50_vd_distill_MV3_large_x1_0 \ tools/train.py \ -c ./configs/Distillation/R50_vd_distill_MV3_large_x1_0.yaml ``` -------------------------------- ### Start Paddle Serving HTTP Service Source: https://paddleclas.readthedocs.io/zh-cn/latest/extension/paddle_serving.html Launch the Paddle Serving HTTP prediction service using the exported model. Specify the model path, working directory, and the port for the service. ```python python tools/serving/image_service_gpu.py serving/ppcls_model workdir 9292 ``` -------------------------------- ### Execute Prediction with Persistable Model Source: https://paddleclas.readthedocs.io/zh-cn/latest/extension/paddle_inference.html Run the inference engine to get predictions. This involves executing the compiled program with the input data and fetching the output. ```python outputs = exe.run(infer_prog, feed={image.name: data}, fetch_list=[out.name], return_numpy=False) ``` -------------------------------- ### Download Paddle-Lite Benchmark Binary (ARMv7) Source: https://paddleclas.readthedocs.io/zh-cn/latest/extension/paddle_mobile_inference.html Download the pre-compiled benchmark binary for ARMv7 architecture. Use this if your Android device's ABI is v7. ```bash wget -c https://paddle-inference-dist.bj.bcebos.com/PaddleLite/benchmark_0/benchmark_bin_v7 ``` -------------------------------- ### Executing the Training Script Source: https://paddleclas.readthedocs.io/zh-cn/latest/advanced_tutorials/image_augmentation/ImageAugment.html Command to execute the training script after setting up the environment and configuration. ```bash sh tools/run.sh ``` -------------------------------- ### Get Android CPU ABI Source: https://paddleclas.readthedocs.io/zh-cn/latest/extension/paddle_mobile_inference.html Execute this command on your Android device via ADB to determine its CPU architecture (ABI). This is crucial for selecting the correct pre-compiled benchmark binary. ```bash adb shell getprop ro.product.cpu.abi ``` -------------------------------- ### Configure and Fine-tune with SSLD Pre-trained Model Source: https://paddleclas.readthedocs.io/zh-cn/latest/tutorials/quick_start.html Sets up configuration for fine-tuning with an SSLD pre-trained model, recommending a smaller learning rate for intermediate layers. This approach further boosts accuracy. ```yaml ARCHITECTURE: name: 'ResNet50_vd' params: lr_mult_list: [0.1, 0.1, 0.2, 0.2, 0.3] pretrained_model: "./pretrained/ResNet50_vd_ssld_pretrained" ``` -------------------------------- ### Execute Prediction with Paddle Predictor Source: https://paddleclas.readthedocs.io/zh-cn/latest/extension/paddle_inference.html Performs inference using a configured Paddle predictor. It involves getting input tensor names, preparing input data, and running the prediction. ```python import numpy as np input_names = predictor.get_input_names() input_tensor = predictor.get_input_tensor(input_names[0]) input = np.random.randn(1, 3, 224, 224).astype("float32") input_tensor.reshape([1, 3, 224, 224]) input_tensor.copy_from_cpu(input) predictor.zero_copy_run() ``` -------------------------------- ### Download Paddle-Lite Benchmark Binary (ARMv8) Source: https://paddleclas.readthedocs.io/zh-cn/latest/extension/paddle_mobile_inference.html Download the pre-compiled benchmark binary for ARMv8 architecture. This tool is used for evaluating model performance on Android devices. ```bash wget -c https://paddle-inference-dist.bj.bcebos.com/PaddleLite/benchmark_0/benchmark_bin_v8 ``` -------------------------------- ### Download and Prepare Flowers102 Dataset Source: https://paddleclas.readthedocs.io/zh-cn/latest/tutorials/quick_start.html Download the flowers102 dataset and its labels, then extract the image archive. ```bash cd dataset/flowers102 wget https://www.robots.ox.ac.uk/~vgg/data/flowers/102/102flowers.tgz wget https://www.robots.ox.ac.uk/~vgg/data/flowers/102/imagelabels.mat wget https://www.robots.ox.ac.uk/~vgg/data/flowers/102/setid.mat tar -xf 102flowers.tgz ``` -------------------------------- ### Execute Training Script Source: https://paddleclas.readthedocs.io/zh-cn/latest/advanced_tutorials/distillation/distillation.html Shell command to execute the training script after configuration. ```bash sh tools/run.sh ``` -------------------------------- ### Send Request to Paddle Serving HTTP Client Source: https://paddleclas.readthedocs.io/zh-cn/latest/extension/paddle_serving.html Send an image file to the running Paddle Serving HTTP service to get prediction results. Ensure the port number matches the service port. ```python python tools/serving/image_http_client.py 9292 ./docs/images/logo.png ``` -------------------------------- ### 安装Python依赖库 Source: https://paddleclas.readthedocs.io/zh-cn/latest/tutorials/install.html 安装PaddleClas所需的Python依赖库。这些依赖项在requirements.txt文件中列出。 ```bash pip install --upgrade -r requirements.txt ``` -------------------------------- ### Convert Model using Command Line Source: https://paddleclas.readthedocs.io/zh-cn/latest/extension/paddle_inference.html A command-line utility to convert persistable models to inference models. Specify the model name, path to persistable weights, and output directory. ```bash python tools/export_model.py \ --m=模型名称 \ --p=persistable 模型路径 \ --o=model和params保存路径 ``` -------------------------------- ### Benchmark Optimized Model Source: https://paddleclas.readthedocs.io/zh-cn/latest/extension/paddle_mobile_inference.html Evaluate the performance of the optimized Paddle-Lite model using the benchmark binary. This command loads the optimized model from the specified directory. ```bash bash benchmark.sh ./benchmark_bin_v8 ./opt_models result_armv8.txt ``` -------------------------------- ### Train with SSLD Pre-trained Model Source: https://paddleclas.readthedocs.io/zh-cn/latest/tutorials/quick_start.html Executes the training process using the SSLD pre-trained model configuration, aiming for higher accuracy on the flowers102 dataset. ```bash export CUDA_VISIBLE_DEVICES=0 python -m paddle.distributed.launch \       --selected_gpus="0" \       tools/train.py \         -c ./configs/quick_start/ResNet50_vd_ssld_finetune.yaml ``` -------------------------------- ### Generate Dataset Label Files Source: https://paddleclas.readthedocs.io/zh-cn/latest/tutorials/quick_start.html Create label files for training, validation, and testing sets using the provided script. Combines train and extra lists for enhanced distillation. ```bash python generate_flowers102_list.py jpg train > train_list.txt python generate_flowers102_list.py jpg valid > val_list.txt python generate_flowers102_list.py jpg test > extra_list.txt cat train_list.txt extra_list.txt > train_extra_list.txt ``` -------------------------------- ### Train from Scratch with ResNet50_vd Source: https://paddleclas.readthedocs.io/zh-cn/latest/tutorials/quick_start.html Initiates training of a ResNet50_vd model without using any pre-trained weights. This is useful for understanding baseline performance. ```bash export CUDA_VISIBLE_DEVICES=0 python -m paddle.distributed.launch \       --selected_gpus="0" \       tools/train.py \         -c ./configs/quick_start/ResNet50_vd.yaml ``` -------------------------------- ### Model Optimization using opt_mac Tool Source: https://paddleclas.readthedocs.io/zh-cn/latest/extension/paddle_mobile_inference.html Optimize a Paddle inference model using the Paddle-Lite opt tool on macOS. This process generates an optimized model file (e.g., .nb) suitable for mobile deployment. ```bash model_file="../MobileNetV1/model" param_file="../MobileNetV1/params" opt_models_dir="./opt_models" mkdir ${opt_models_dir} ./opt_mac --model_file=${model_file} \ --param_file=${param_file} \ --valid_targets=arm \ --optimize_out_type=naive_buffer \ --prefer_int8_kernel=false \ --optimize_out=${opt_models_dir}/MobileNetV1 ``` -------------------------------- ### Run Model Speed Benchmark (ARMv8) Source: https://paddleclas.readthedocs.io/zh-cn/latest/extension/paddle_mobile_inference.html Execute the benchmark script to evaluate model inference speed on the connected Android device. This command also includes an option to optimize the model before benchmarking. ```bash sh tools/lite/benchmark.sh ./benchmark_bin_v8 ./inference result_armv8.txt true ``` -------------------------------- ### 模型推理评估脚本 Source: https://paddleclas.readthedocs.io/zh-cn/latest/models/models_intro.html 用于模型推理和性能基准测试的Bash脚本。需要设置PYTHONPATH并指定模型文件、参数文件及模型名称。 ```bash #!/usr/bin/env bash export PYTHONPATH=$PWD:$PYTHONPATH python tools/infer/predict.py \ --model_file='pretrained/infer/model' \ --params_file='pretrained/infer/params' \ --enable_benchmark=True \ --model_name=ResNet50_vd \ --use_tensorrt=True \ --use_fp16=False \ --batch_size=1 ``` -------------------------------- ### Navigate to PaddleClas Directory Source: https://paddleclas.readthedocs.io/zh-cn/latest/tutorials/quick_start.html Change the current directory to the root of the PaddleClas project. ```bash cd path_to_PaddleClas ``` -------------------------------- ### Return to PaddleClas Root Directory Source: https://paddleclas.readthedocs.io/zh-cn/latest/tutorials/quick_start.html Navigate back to the main PaddleClas directory after dataset preparation. ```bash cd ../../ ``` -------------------------------- ### Perform Knowledge Distillation Training Source: https://paddleclas.readthedocs.io/zh-cn/latest/tutorials/quick_start.html Executes the knowledge distillation training process using the configured settings. This method leverages a teacher model and unlabeled data to improve the student model's accuracy. ```bash export CUDA_VISIBLE_DEVICES=0 python -m paddle.distributed.launch \       --selected_gpus="0" \       tools/train.py \         -c ./configs/quick_start/R50_vd_distill_MV3_large_x1_0.yaml ``` -------------------------------- ### 安装GPU版本PaddlePaddle Source: https://paddleclas.readthedocs.io/zh-cn/latest/tutorials/install.html 使用pip安装最新GPU版本的PaddlePaddle。请确保已安装CUDA、cuDNN和NCCL运行环境。 ```bash pip install paddlepaddle-gpu --upgrade ``` -------------------------------- ### 验证PaddlePaddle安装 Source: https://paddleclas.readthedocs.io/zh-cn/latest/tutorials/install.html 运行此Python代码以检查PaddlePaddle是否成功安装。建议在安装PaddlePaddle后执行此操作。 ```python import paddle.fluid as fluid fluid.install_check.run_check() ``` -------------------------------- ### 安装特定版本VisualDL Source: https://paddleclas.readthedocs.io/zh-cn/latest/tutorials/install.html 如果VisualDL安装失败,请尝试使用此命令安装指定版本的VisualDL。 ```bash pip3 install --upgrade visualdl==2.0.0b3 -i https://mirror.baidu.com/pypi/simple ``` -------------------------------- ### Download Pre-trained Models Source: https://paddleclas.readthedocs.io/zh-cn/latest/tutorials/quick_start.html Download specified pre-trained models for ResNet50_vd, ResNet50_vd_ssld, and MobileNetV3_large_x1_0. Models are downloaded to the specified path and decompressed. ```bash python tools/download.py -a ResNet50_vd -p ./pretrained -d True python tools/download.py -a ResNet50_vd_ssld -p ./pretrained -d True python tools/download.py -a MobileNetV3_large_x1_0 -p ./pretrained -d True ``` -------------------------------- ### Configure Paddle Predictor with AnalysisConfig Source: https://paddleclas.readthedocs.io/zh-cn/latest/extension/paddle_inference.html Sets up the prediction configuration using AnalysisConfig for optimized inference. Enables GPU, disables logging, enables IR optimization, and configures TensorRT. ```python from paddle.fluid.core import AnalysisConfig from paddle.fluid.core import create_paddle_predictor config = AnalysisConfig(model文件路径, params文件路径) config.enable_use_gpu(8000, 0) config.disable_glog_info() config.switch_ir_optim(True) config.enable_tensorrt_engine( precision_mode=AnalysisConfig.Precision.Float32, max_batch_size=1) # no zero copy方式需要去除fetch feed op config.switch_use_feed_fetch_ops(False) predictor = create_paddle_predictor(config) ``` -------------------------------- ### Build Inference Engine with Inference Model Source: https://paddleclas.readthedocs.io/zh-cn/latest/extension/paddle_inference.html Load the inference model structure and weights directly to build the inference engine. This method is used when the model already contains structural information. ```python import fluid place = fluid.CPUPlace() exe = fluid.Executor(place) [program, feed_names, fetch_lists] = fluid.io.load_inference_model( 模型的存储路径, exe, model_filename=保存的模型文件, params_filename=保存的参数文件) compiled_program = fluid.compiler.CompiledProgram(program) ``` -------------------------------- ### Build Inference Engine with Persistable Model Source: https://paddleclas.readthedocs.io/zh-cn/latest/extension/paddle_inference.html Construct the network structure and load weights to build the inference engine when using a persistable model. Ensure necessary imports and configurations are in place. ```python import fluid from ppcls.modeling.architectures.resnet_vd import ResNet50_vd place = fluid.CPUPlace() exe = fluid.Executor(place) startup_prog = fluid.Program() infer_prog = fluid.Program() with fluid.program_guard(infer_prog, startup_prog): with fluid.unique_name.guard(): image = create_input() image = fluid.data(name='image', shape=[None, 3, 224, 224], dtype='float32') out = ResNet50_vd.net(input=input, class_dim=1000) infer_prog = infer_prog.clone(for_test=True) fluid.load(program=infer_prog, model_path=persistable 模型路径, executor=exe) ``` -------------------------------- ### 克隆PaddleClas模型库 Source: https://paddleclas.readthedocs.io/zh-cn/latest/tutorials/install.html 克隆PaddleClas模型库到本地。请先导航到您希望存放代码的目录。 ```bash cd path_to_clone_PaddleClas git clone https://github.com/PaddlePaddle/PaddleClas.git ``` -------------------------------- ### Dataset List Format Source: https://paddleclas.readthedocs.io/zh-cn/latest/tutorials/data.html Format for 'train_list.txt' and 'val_list.txt' files, separating image path and label with a space. ```text # 每一行采用"空格"分隔图像路径与标注 ILSVRC2012_val_00000001.JPEG 65 ... ``` -------------------------------- ### Export Paddle Serving Model Source: https://paddleclas.readthedocs.io/zh-cn/latest/extension/paddle_serving.html Use the provided script to export a trained model into the format required by Paddle Serving. Specify the model name, pretrained path, and output directory. ```python python tools/export_serving_model.py -m ResNet50_vd -p ./pretrained/ResNet50_vd_pretrained/ -o serving ``` -------------------------------- ### Predict with Inference Model Source: https://paddleclas.readthedocs.io/zh-cn/latest/extension/paddle_inference.html Execute predictions using the inference model format. This command requires specifying the image path, model directory, model file, parameters file, and GPU usage. ```bash python tools/infer/py_infer.py \ --i=图片路径 \ --d=模型的存储路径 \ --m=保存的模型文件 \ --p=保存的参数文件 \ --use_gpu=True ``` -------------------------------- ### Train MobileNetV3 Large Model Source: https://paddleclas.readthedocs.io/zh-cn/latest/tutorials/quick_start.html Trains a MobileNetV3_large_x1_0 model, demonstrating the use of different architectures. This allows comparison of performance based on computational and storage needs. ```bash export CUDA_VISIBLE_DEVICES=0 python -m paddle.distributed.launch \       --selected_gpus="0" \       tools/train.py \         -c ./configs/quick_start/MobileNetV3_large_x1_0_finetune.yaml ``` -------------------------------- ### Using AutoAugment for Image Augmentation Source: https://paddleclas.readthedocs.io/zh-cn/latest/advanced_tutorials/image_augmentation/ImageAugment.html Demonstrates how to apply the AutoAugment image augmentation policy in PaddleClas. This method involves decoding, resizing, and then applying a randomly selected sub-policy from a predefined set of transformations. ```python from ppcls.data.imaug import DecodeImage from ppcls.data.imaug import ResizeImage from ppcls.data.imaug import ImageNetPolicy from ppcls.data.imaug import transform size = 224 decode_op = DecodeImage() resize_op = ResizeImage(size=(size, size)) autoaugment_op = ImageNetPolicy() ops = [decode_op, resize_op, autoaugment_op] imgs_dir = 图像路径 fnames = os.listdir(imgs_dir) for f in fnames: data = open(os.path.join(imgs_dir, f)).read() img = transform(data, ops) ``` -------------------------------- ### Run Inference using Command Line Source: https://paddleclas.readthedocs.io/zh-cn/latest/extension/paddle_inference.html Executes model inference using the predict.py script. Specify input image, model, and parameters files, along with GPU and TensorRT options. ```bash python ./tools/infer/predict.py \ -i=./test.jpeg \ -m=./resnet50-vd/model \ -p=./resnet50-vd/params \ --use_gpu=1 \ --use_tensorrt=True ``` -------------------------------- ### Flowers102 Data Structure Source: https://paddleclas.readthedocs.io/zh-cn/latest/tutorials/data.html Organize Flowers102 data with an 'jpg' directory containing images and 'train_list.txt' and 'val_list.txt' files. ```text PaddleClas/dataset/flowers102/ |_ jpg/ | |_ image_03601.jpg | |_ ... | |_ image_02355.jpg |_ train_list.txt |_ val_list.txt ``` -------------------------------- ### 查看PaddlePaddle版本 Source: https://paddleclas.readthedocs.io/zh-cn/latest/tutorials/install.html 使用此命令快速查看已安装的PaddlePaddle版本。适用于快速检查环境配置。 ```python python -c "import paddle; print(paddle.__version__)" ```