# Clip-Pytorch **Repository Path**: dlml2/Clip-Pytorch ## Basic Information - **Project Name**: Clip-Pytorch - **Description**: No description available - **Primary Language**: Python - **License**: MIT - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-01-03 - **Last Updated**: 2025-01-03 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README

Clip-Pytorch

This is a warehouse for Clip-Pytorch-model, can be used to train your image-datasets for classification tasks. The code mainly contains two models: ResNet-Clip & ViT-Clip, new models will be added in the future. [Code Reference](https://github.com/hsouri/bob-classification) ## Preparation ### 1.Download the dataset: [flower_dataset](https://www.kaggle.com/datasets/alxmamaev/flowers-recognition). ### 2.Install clip package ```shell pip install git+https://github.com/openai/CLIP.git ``` ## Project Structure ``` ├── datasets: Load datasets ├── my_dataset.py: Customize reading data sets and define transforms data enhancement methods ├── split_data.py: Define the function to read the image dataset and divide the training-set and test-set ├── threeaugment.py: Additional data augmentation methods ├── models: Clip Model ├── resnet-clip.py: Construct "resnet-clip" model ├── vit-clip.py: Construct "vit-clip" model ├── util: ├── engine.py: Function code for a training/validation process ├── losses.py: Knowledge distillation loss, combined with teacher model (if any) ├── optimizer.py: Define Sophia optimizer ├── samplers.py: Define the parameter of "sampler" in DataLoader ├── utils.py: Record various indicator information and output and distributed environment ├── estimate_model.py: Visualized evaluation indicators ROC curve, confusion matrix, classification report, etc. └── train_gpu.py: Training model startup file ``` ## Precautions Before you use the code to train your own data set, please first enter the ___train_gpu.py___ file and modify the ___data_root___, ___batch_size___, ___num_workers___ and ___nb_classes___ parameters. If you want to draw the confusion matrix and ROC curve, you only need to set the ___predict___ parameter to __True__ ## Use Sophia Optimizer (in util/optimizer.py) You can use anther optimizer sophia, just need to change the optimizer in ___train_gpu.py___, for this training sample, can achieve better results ``` # optimizer = create_optimizer(args, model_without_ddp) optimizer = SophiaG(model.parameters(), lr=2e-4, betas=(0.965, 0.99), rho=0.01, weight_decay=args.weight_decay) ``` ## Train this model ### Parameters Meaning: ``` 1. nproc_per_node: 2. CUDA_VISIBLE_DEVICES: 3. nnodes: 4. node_rank: 5. master_addr: 6. master_port: ``` ### Note: If you want to use multiple GPU for training, whether it is a single machine with multiple GPUs or multiple machines with multiple GPUs, each GPU will divide the batch_size equally. For example, batch_size=4 in my train_gpu.py. If I want to use 2 GPUs for training, it means that the batch_size on each GPU is 4. ___Do not let batch_size=1 on each GPU___, otherwise BN layer maybe report an error. If you recive an error like "___error: unrecognized arguments: --local-rank=1___" when you use distributed multi-GPUs training, just replace the command "___torch.distributed.launch___" to "___torch.distributed.run___". ### train model with single-machine single-GPU: ``` python train_gpu.py ``` ### train model with single-machine multi-GPU: ``` python -m torch.distributed.launch --nproc_per_node=8 train_gpu.py ``` ### train model with single-machine multi-GPU: (using a specified part of the GPUs: for example, I want to use the second and fourth GPUs) ``` CUDA_VISIBLE_DEVICES=1,3 python -m torch.distributed.launch --nproc_per_node=2 train_gpu.py ``` ### train model with multi-machine multi-GPU: (For the specific number of GPUs on each machine, modify the value of --nproc_per_node. If you want to specify a certain GPU, just add CUDA_VISIBLE_DEVICES= to specify the index number of the GPU before each command. The principle is the same as single-machine multi-GPU training) ``` On the first machine: python -m torch.distributed.launch --nproc_per_node=1 --nnodes=2 --node_rank=0 --master_addr= --master_port= train_gpu.py On the second machine: python -m torch.distributed.launch --nproc_per_node=1 --nnodes=2 --node_rank=1 --master_addr= --master_port= train_gpu.py ``` ## Citation ``` @inproceedings{radford2021learning, title={Learning transferable visual models from natural language supervision}, author={Radford, Alec and Kim, Jong Wook and Hallacy, Chris and Ramesh, Aditya and Goh, Gabriel and Agarwal, Sandhini and Sastry, Girish and Askell, Amanda and Mishkin, Pamela and Clark, Jack and others}, booktitle={International conference on machine learning}, pages={8748--8763}, year={2021}, organization={PMLR} } ``` ``` @inproceedings{he2016deep, title={Deep residual learning for image recognition}, author={He, Kaiming and Zhang, Xiangyu and Ren, Shaoqing and Sun, Jian}, booktitle={Proceedings of the IEEE conference on computer vision and pattern recognition}, pages={770--778}, year={2016} } ``` ``` @article{dosovitskiy2020image, title={An image is worth 16x16 words: Transformers for image recognition at scale}, author={Dosovitskiy, Alexey and Beyer, Lucas and Kolesnikov, Alexander and Weissenborn, Dirk and Zhai, Xiaohua and Unterthiner, Thomas and Dehghani, Mostafa and Minderer, Matthias and Heigold, Georg and Gelly, Sylvain and others}, journal={arXiv preprint arXiv:2010.11929}, year={2020} } ```