├── CV_net ├── LeNet │ ├── results │ │ ├── horse.jpg │ │ └── airplane.jpg │ ├── model │ │ └── LeNet_mnist.pth │ ├── predict.py │ ├── model.py │ ├── train_mnist.py │ └── train.py ├── class_indices.json ├── LeNet_tf │ ├── LeNet5_MNIST │ │ └── saved_model.pb │ └── model.py ├── WRN │ ├── utils │ │ ├── __pycache__ │ │ │ └── misc.cpython-37.pyc │ │ └── misc.py │ ├── predict.py │ └── WRN_cifar.py ├── AlexNet │ ├── classes_indices.json │ ├── predict.py │ └── model.py ├── ECANet │ ├── model.py │ ├── train.py │ └── predict.py ├── MobileNetv4 │ ├── classes_indices.json │ ├── results │ │ └── 15061894841_e5aca59ecd_n.jpg │ ├── readme.md │ ├── predict.py │ └── utils │ │ └── misc.py ├── transNeXt │ ├── class_indices.json │ ├── readme.md │ ├── predict.py │ └── utils │ │ └── misc.py ├── DenseNet │ ├── predict.py │ └── model.py ├── ResNeXt │ ├── predict.py │ └── train_pre.py ├── MobileViT │ ├── __pycache__ │ │ ├── model_config.cpython-37.pyc │ │ └── transformer.cpython-37.pyc │ ├── readme.md │ └── utils.py ├── NonLocalNN │ ├── utils │ │ ├── __pycache__ │ │ │ └── misc.cpython-39.pyc │ │ └── misc.py │ └── predict.py ├── ShuffleNet │ ├── __init__.py │ └── shufflenet_v1.py ├── pytorch-center-offical │ ├── transforms.py │ ├── datasets.py │ ├── center_loss.py │ ├── models.py │ ├── utils.py │ ├── split_dataset.py │ └── model.py ├── RegNet │ ├── readme.md │ └── utils.py ├── RepVGG │ ├── readme.md │ └── utils.py ├── EfficientNet │ ├── readme.md │ └── utils.py ├── EfficientNetv2 │ ├── readme.md │ └── utils.py ├── GhostNet │ ├── readme.md │ └── utils.py ├── get_classes_dict.py ├── DeiT │ ├── readme.md │ ├── utils.py │ ├── losses.py │ └── datasets.py ├── readme.md ├── VGG │ ├── predict.py │ └── model.py ├── GooLeNet │ └── predict.py ├── SeNet │ └── predict.py ├── ResNet │ └── predict.py ├── SKnet │ └── predict.py ├── PreActResNet │ ├── predict.py │ └── utils │ │ └── misc.py ├── MobileNet │ └── predict.py ├── swin_transformer │ ├── predict.py │ └── utils │ │ └── misc.py ├── vision_transformer │ ├── predict.py │ └── utils │ │ └── misc.py ├── SPPNet │ ├── predict.py │ ├── sppnet.py │ └── utils │ │ └── misc.py ├── VanillaNet │ ├── predict.py │ └── utils │ │ └── misc.py ├── split_dataset.py └── ConvMixer │ ├── predict.py │ └── utils │ └── misc.py ├── mofan_python ├── mnist │ └── MNIST │ │ └── processed │ │ └── test.pt ├── 7_batch_train.py ├── 2_tutorial.py ├── 1_numpy_torch.py ├── 3_regression.py ├── 5_save_reload.py ├── 8_optimizer.py ├── GPU,py.py ├── 4_classification.py ├── 6_build_nn_quickly.py ├── GAN.py ├── 11_RNN_regressor.py └── 10_RNN_classifier.py ├── README.md └── 2020July_education_pytorch_teaching └── data ├── NLP_fizz_buzz_simple.py ├── two_layers_nn_Modules.py ├── two_layer_numpy.py ├── two_layers_tensors.py ├── two_layers_tensor_autograd.py ├── NLP_fizz_buzz.py └── two_layers_nn.py /CV_net/LeNet/results/horse.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Reversev/Deeplearning_pytorch/HEAD/CV_net/LeNet/results/horse.jpg -------------------------------------------------------------------------------- /CV_net/LeNet/model/LeNet_mnist.pth: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Reversev/Deeplearning_pytorch/HEAD/CV_net/LeNet/model/LeNet_mnist.pth -------------------------------------------------------------------------------- /CV_net/LeNet/results/airplane.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Reversev/Deeplearning_pytorch/HEAD/CV_net/LeNet/results/airplane.jpg -------------------------------------------------------------------------------- /CV_net/class_indices.json: -------------------------------------------------------------------------------- 1 | { 2 | "0": "daisy", 3 | "1": "dandelion", 4 | "2": "rose", 5 | "3": "sunflower", 6 | "4": "tulip" 7 | } -------------------------------------------------------------------------------- /CV_net/LeNet_tf/LeNet5_MNIST/saved_model.pb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Reversev/Deeplearning_pytorch/HEAD/CV_net/LeNet_tf/LeNet5_MNIST/saved_model.pb -------------------------------------------------------------------------------- /mofan_python/mnist/MNIST/processed/test.pt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Reversev/Deeplearning_pytorch/HEAD/mofan_python/mnist/MNIST/processed/test.pt -------------------------------------------------------------------------------- /CV_net/WRN/utils/__pycache__/misc.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Reversev/Deeplearning_pytorch/HEAD/CV_net/WRN/utils/__pycache__/misc.cpython-37.pyc -------------------------------------------------------------------------------- /CV_net/AlexNet/classes_indices.json: -------------------------------------------------------------------------------- 1 | { 2 | "0": "daisy", 3 | "1": "dandelion", 4 | "2": "rose", 5 | "3": "sunflower", 6 | "4": "tulip" 7 | } -------------------------------------------------------------------------------- /CV_net/ECANet/model.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2021/11/29 18:42 4 | # @Author : '' 5 | # @FileName: model.py 6 | 7 | 8 | -------------------------------------------------------------------------------- /CV_net/ECANet/train.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2021/11/29 18:42 4 | # @Author : '' 5 | # @FileName: train.py 6 | 7 | 8 | -------------------------------------------------------------------------------- /CV_net/MobileNetv4/classes_indices.json: -------------------------------------------------------------------------------- 1 | { 2 | "0": "daisy", 3 | "1": "dandelion", 4 | "2": "rose", 5 | "3": "sunflower", 6 | "4": "tulip" 7 | } -------------------------------------------------------------------------------- /CV_net/transNeXt/class_indices.json: -------------------------------------------------------------------------------- 1 | { 2 | "0": "daisy", 3 | "1": "dandelion", 4 | "2": "rose", 5 | "3": "sunflower", 6 | "4": "tulip" 7 | } -------------------------------------------------------------------------------- /CV_net/DenseNet/predict.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2021/9/19 17:23 4 | # @Author : '' 5 | # @FileName: predict.py 6 | 7 | 8 | -------------------------------------------------------------------------------- /CV_net/ResNeXt/predict.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2021/9/19 17:21 4 | # @Author : '' 5 | # @FileName: predict.py 6 | 7 | 8 | -------------------------------------------------------------------------------- /CV_net/ResNeXt/train_pre.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2021/9/19 17:21 4 | # @Author : '' 5 | # @FileName: train_pre.py 6 | 7 | 8 | -------------------------------------------------------------------------------- /CV_net/AlexNet/predict.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2021/8/9 22:57 4 | # @Author : 'IReverser' 5 | # @FileName: predict.py 6 | 7 | 8 | -------------------------------------------------------------------------------- /CV_net/MobileNetv4/results/15061894841_e5aca59ecd_n.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Reversev/Deeplearning_pytorch/HEAD/CV_net/MobileNetv4/results/15061894841_e5aca59ecd_n.jpg -------------------------------------------------------------------------------- /CV_net/MobileViT/__pycache__/model_config.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Reversev/Deeplearning_pytorch/HEAD/CV_net/MobileViT/__pycache__/model_config.cpython-37.pyc -------------------------------------------------------------------------------- /CV_net/MobileViT/__pycache__/transformer.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Reversev/Deeplearning_pytorch/HEAD/CV_net/MobileViT/__pycache__/transformer.cpython-37.pyc -------------------------------------------------------------------------------- /CV_net/NonLocalNN/utils/__pycache__/misc.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Reversev/Deeplearning_pytorch/HEAD/CV_net/NonLocalNN/utils/__pycache__/misc.cpython-39.pyc -------------------------------------------------------------------------------- /CV_net/ShuffleNet/__init__.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2022/2/19 10:30 4 | # @Author : '' 5 | # @FileName: __init__.py.py 6 | 7 | 8 | -------------------------------------------------------------------------------- /CV_net/ShuffleNet/shufflenet_v1.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2022/2/19 10:30 4 | # @Author : '' 5 | # @FileName: shufflenet_v1.py 6 | 7 | 8 | -------------------------------------------------------------------------------- /CV_net/pytorch-center-offical/transforms.py: -------------------------------------------------------------------------------- 1 | from torchvision.transforms import * 2 | from PIL import Image 3 | 4 | class ToGray(object): 5 | """ 6 | Convert image from RGB to gray level. 7 | """ 8 | def __call__(self, img): 9 | return img.convert('L') -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Deep learning with pytorch library 2 | 3 | > There are some notebooks during pytorch learning (2020): 4 | 5 | - 1. 2020July_education_pytorch 6 | 7 | - 2. mofan_pytorch 8 | 9 | - 3. DeepLearning_with_pytorch.ipynb 10 | 11 | > This is a summary for classical architectures (2020 - current): 12 | 13 | - 4. CV_modol (classical architectures in Computer Vision) 14 | -------------------------------------------------------------------------------- /CV_net/RegNet/readme.md: -------------------------------------------------------------------------------- 1 | # RegNet 2 | Designing Network Design Spaces [https://arxiv.org/abs/2003.13678] 3 | 4 | #Usage: 5 | ```python 6 | # train without cache 7 | python train.py --gpu 1 8 | # train with pretrained model 9 | python train.py --pretrained --model-path checkpoints/model_RegNetx_200mf_best.pt 10 | # train with resume 11 | python train.py --resume checkpoints/model_RegNetx_200mf_best.pt 12 | 13 | # validate 14 | python train.py --evaluate --gpu 1 --resume checkpoints/model_RegNetx_200mf_best.pt 15 | ``` -------------------------------------------------------------------------------- /CV_net/RepVGG/readme.md: -------------------------------------------------------------------------------- 1 | 2 | # RepVGG 3 | RepVGG: Making VGG-style ConvNets Great Again [https://arxiv.org/abs/2101.03697] 4 | 5 | #Usage: 6 | ```python 7 | # train without cache 8 | python train.py --gpu 1 9 | # train with pretrained model 10 | python train.py --pretrained --model-path checkpoints/model_RegNetx_200mf_best.pt 11 | # train with resume 12 | python train.py --resume checkpoints/model_RegNetx_200mf_best.pt 13 | 14 | # validate 15 | python train.py --evaluate --gpu 1 --resume checkpoints/model_RegNetx_200mf_best.pt 16 | ``` 17 | -------------------------------------------------------------------------------- /CV_net/EfficientNet/readme.md: -------------------------------------------------------------------------------- 1 | 2 | # EfficientNetv1 3 | EfficientNet: Rethinking Model Scaling for Convolutional Neural Networks [https://arxiv.org/abs/1905.11946] 4 | 5 | #Usage: 6 | ```python 7 | # train without cache 8 | python train.py --gpu 1 9 | # train with pretrained model 10 | python train.py --pretrained --model-path checkpoints/model_effnetv1_b1_best.pt 11 | # train with resume 12 | python train.py --resume checkpoints/model_effnetv1_b1_best.pt 13 | 14 | # validate 15 | python train.py --evaluate --gpu 1 --resume checkpoints/model_effnetv1_b1_best.pt 16 | ``` 17 | 18 | -------------------------------------------------------------------------------- /CV_net/EfficientNetv2/readme.md: -------------------------------------------------------------------------------- 1 | 2 | # EfficientNetv2 3 | EfficientNetV2: Smaller Models and Faster Training [https://arxiv.org/abs/2104.00298] 4 | 5 | #Usage: 6 | ```python 7 | # train without cache 8 | python train.py --gpu 1 9 | # train with pretrained model 10 | python train.py --pretrained --model-path checkpoints/model_effnetv2_s_best.pt 11 | # train with resume 12 | python train.py --resume checkpoints/model_effnetv2_s_best.pt 13 | 14 | # validate 15 | python train.py --evaluate --gpu 1 --resume checkpoints/model_effnetv2_s_best.pt 16 | ``` 17 | Top-1: 90.581 (seed:32) 18 | -------------------------------------------------------------------------------- /CV_net/GhostNet/readme.md: -------------------------------------------------------------------------------- 1 | # GhostNet 2 | GhostNet: More Features from Cheap Operations [https://arxiv.org/abs/1911.11907] 3 | 4 | #Usage: 5 | ```pythons 6 | # train without cache 7 | python train.py --gpu 1 8 | # train with pretrained model 9 | python train.py --pretrained --model-path checkpoints/model_ghostnet_seed578_best.pt.pt 10 | # train with resume 11 | python train.py --resume checkpoints/model_ghostnet_seed578_best.pt.pt 12 | 13 | # validate 14 | python train.py --evaluate --gpu 1 --resume checkpoints/model_ghostnet_seed578_best.pt.pt 15 | ``` 16 | 17 | Top-1: 84.884 (seed:578) -------------------------------------------------------------------------------- /CV_net/get_classes_dict.py: -------------------------------------------------------------------------------- 1 | # !/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time: 2023/3/6 22:53 4 | # @Author: 'IReverser' 5 | # @ FileName: get_classes_dict.py 6 | import json 7 | from torchvision import datasets 8 | 9 | file_path = "../datasets/flowers/train/" 10 | json_name = "classes_dict.json" 11 | 12 | train_dataset = datasets.ImageFolder(root=file_path) 13 | # {'daisy':0, 'dandelion':1, 'roses':2, 'sunflower':3, 'tulips':4} 14 | flower_list = train_dataset.class_to_idx 15 | cla_dict = dict((val, key) for key, val in flower_list.items()) 16 | # write dict into json file 17 | json_str = json.dumps(cla_dict, indent=4) 18 | with open('class_indices.json', 'w') as json_file: 19 | json_file.write(json_str) 20 | -------------------------------------------------------------------------------- /CV_net/MobileViT/readme.md: -------------------------------------------------------------------------------- 1 | # MobileViT 2 | MobileViT: Light-weight, General-purpose, and Mobile-friendly Vision Transformer 3 | 4 | paper: [https://arxiv.org/abs/2110.02178](https://arxiv.org/abs/2110.02178) 5 | 6 | offical code: [https://github.com/apple/ml-cvnets](https://github.com/apple/ml-cvnets) 7 | 8 | # Usage: 9 | ```python 10 | # train without cache 11 | # --arch mobilevit_xxs/moblevit_xs/mobilevit_s 12 | python train.py --gpu 1 13 | # train with pretrained model 14 | python train.py --pretrained --model-path checkpoints/model_RegNetx_200mf_best.pt 15 | # train with resume 16 | python train.py --resume checkpoints/model_RegNetx_200mf_best.pt 17 | 18 | # validate 19 | python train.py --evaluate --gpu 1 --resume checkpoints/model_RegNetx_200mf_best.pt 20 | ``` 21 | -------------------------------------------------------------------------------- /2020July_education_pytorch_teaching/data/NLP_fizz_buzz_simple.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2020/2/25 4 | # @Author :'IReverser' 5 | # @FileName: NLP_fizz_buzz_simple.py 6 | 7 | 8 | # One-hot encode the desired outputs: [number, "fizz", "buzz", "fizzbuzz"] 9 | def fizz_buzz_encode(i): 10 | if i % 15 == 0: return 3 11 | elif i % 5 == 0: return 2 12 | elif i % 3 == 0: return 1 13 | else: return 0 14 | 15 | 16 | def fizz_buzz_decode(i, prediction): 17 | return [str(i), "fizz", "buzz", "fizzbuzz"][prediction] 18 | 19 | 20 | print(fizz_buzz_decode(1, fizz_buzz_encode(1))) 21 | print(fizz_buzz_decode(2, fizz_buzz_encode(2))) 22 | print(fizz_buzz_decode(5, fizz_buzz_encode(5))) 23 | print(fizz_buzz_decode(12, fizz_buzz_encode(12))) 24 | print(fizz_buzz_decode(15, fizz_buzz_encode(15))) 25 | 26 | 27 | -------------------------------------------------------------------------------- /mofan_python/7_batch_train.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.utils.data as Data 3 | 4 | torch.manual_seed(1) # reproducible 5 | 6 | BATCH_SIZE = 8 7 | 8 | x = torch.linspace(1, 10, 10) # this is x data (torch tensor) 9 | y = torch.linspace(10, 1, 10) # this is y data (torch tensor) 10 | 11 | torch_dataset = Data.TensorDataset(x, y) 12 | loader = Data.DataLoader( 13 | dataset=torch_dataset, # torch TensorDataset format 14 | batch_size=BATCH_SIZE, # mini batch size 15 | shuffle=True, # random shuffle for training 16 | num_workers=2, # subprocesses for loading data 17 | ) 18 | 19 | 20 | def show_batch(): 21 | for epoch in range(3): # train entire dataset 3 times 22 | for step, (batch_x, batch_y) in enumerate(loader): # for each training step 23 | # train your data... 24 | print('Epoch: ', epoch, '| Step: ', step, '| batch x: ', 25 | batch_x.numpy(), '| batch y: ', batch_y.numpy()) 26 | 27 | 28 | if __name__ == '__main__': 29 | show_batch() 30 | -------------------------------------------------------------------------------- /mofan_python/2_tutorial.py: -------------------------------------------------------------------------------- 1 | # activate function as sigmoid, relu, tanh, softplus. 2 | import torch 3 | import torch.nn.functional as F 4 | from torch.autograd import Variable 5 | import matplotlib.pyplot as plt 6 | 7 | # fake data 8 | x = torch.linspace(-5, 5, 200) # x data(tensor),shape=(100, 1) 9 | x = Variable(x) 10 | x_np = x.data.numpy() 11 | 12 | y_relu = torch.relu(x).data.numpy() 13 | y_sigmoid = torch.sigmoid(x).data.numpy() 14 | y_tanh = torch.tanh(x).data.numpy() 15 | y_softplus = F.softplus(x).data.numpy() 16 | 17 | plt.figure(1, figsize=(8, 6)) 18 | plt.subplot(221) 19 | plt.plot(x_np, y_relu, c='red', label='relu') 20 | plt.ylim(-1, 5) 21 | plt.legend(loc='best') 22 | 23 | plt.subplot(222) 24 | plt.plot(x_np, y_sigmoid, c='red', label='sigmoid') 25 | plt.ylim(-0.2, 1.2) 26 | plt.legend(loc='best') 27 | 28 | plt.subplot(223) 29 | plt.plot(x_np, y_tanh, c='red', label='tanh') 30 | plt.ylim(-1.2, 1.2) 31 | plt.legend(loc='best') 32 | 33 | plt.subplot(224) 34 | plt.plot(x_np, y_softplus, c='red', label='softplus') 35 | plt.ylim(-0.2, 6) 36 | plt.legend(loc='best') 37 | plt.show() -------------------------------------------------------------------------------- /CV_net/transNeXt/readme.md: -------------------------------------------------------------------------------- 1 | # TransNeXt 2 | TransNeXt: Robust Foveal Visual Perception for Vision Transformers 3 | 4 | paper: [https://arxiv.org/pdf/2311.17132.pdf](https://arxiv.org/pdf/2311.17132.pdf) 5 | 6 | offical code: [https://github.com/DaiShiResearch/TransNeXt](https://github.com/DaiShiResearch/TransNeXt) 7 | 8 | #Usage: 9 | 1. Install timm=0.3.2 10 | ```Shell 11 | pip install timm==0.3.2 12 | ``` 13 | 2. Train or eval 14 | # Train model in transNeXt series 15 | ```python 16 | # train without cache 17 | python train.py --gpu 0 18 | python train.py --gpu 0 --arch 'transnext_micro' --batch_size 16 # 'transnext_tiny', 'transnext_small', 'transnext_base' 19 | 20 | # train with resume 21 | python train.py --arch deit_tiny --resume checkpoints/model_transnext_micro_seed561_best.pt --gpu 0 22 | 23 | ``` 24 | 25 | # Validate 26 | ```python 27 | # validate 28 | python train.py --evaluate --gpu 1 --resume checkpoints/model_transnext_micro_seed561_best.pt 29 | ``` 30 | 31 | # Predict 32 | Modify ```model_name```, ```dataset_name```, ```MODEL_PATH``` and ```CLASS_NUM``` in ```predict.py``` script. Put pictures into ```results``` directory. 33 | ```python 34 | python predict.py 35 | ``` 36 | -------------------------------------------------------------------------------- /CV_net/ECANet/predict.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2021/11/29 18:42 4 | # @Author : '' 5 | # @FileName: predict.py 6 | import torch 7 | from torch import nn 8 | from torch.nn.parameter import Parameter 9 | 10 | 11 | class ECA_layer(nn.Module): 12 | """ 13 | Construct a ECA module 14 | channel: the number of channels of the input feature map 15 | k_size: adaptive selection of kernel size 16 | """ 17 | def __init__(self, channel, k_size=3): 18 | super(ECA_layer, self).__init__() 19 | self.avg_pool = nn.AdaptiveAvgPool2d(1) 20 | self.conv = nn.Conv2d(1, 1, kernel_size=k_size, padding=(k_size-1)//2, bias=False) 21 | self.sigmoid = nn.Sigmoid() 22 | 23 | def forward(self, x): 24 | # x:[B, C, H, W] 25 | # feature descriptor on the global spatial information 26 | out = self.avg_pool(x) 27 | 28 | # two different branches of ECA module 29 | out = self.conv(out.squeeze(-1).transpose(-1, -2)) 30 | out = out.transpose(-1, -2).unsqueeze(-1) 31 | 32 | # multi-scale information fusion 33 | out = self.sigmoid(out) 34 | 35 | return x * out.expand_as(x) # expand_as is expand out such like x 36 | 37 | -------------------------------------------------------------------------------- /2020July_education_pytorch_teaching/data/two_layers_nn_Modules.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2020/2/25 4 | # @Author :'' 5 | # @FileName: two_layers_nn_Modules.py 6 | import torch 7 | from torch import nn 8 | 9 | 10 | class TwoLayerNet(nn.Module): 11 | def __init__(self, D_in, H, D_out): 12 | 13 | super(TwoLayerNet, self).__init__() 14 | self.linear1 = nn.Linear(D_in, H) 15 | self.linear2 = nn.Linear(H, D_out) 16 | 17 | def forward(self, x): 18 | h_relu = self.linear1(x).clamp(min=0) 19 | y_pred = self.linear2(h_relu) 20 | return y_pred 21 | 22 | 23 | # N is batch size; 24 | # D_in is input dimension; 25 | # H is hidden dimension; 26 | # D_out is output dimension. 27 | N, D_in, H, D_out = 64, 1000, 100, 10 28 | learning_rate = 1e-4 29 | 30 | # Create random Tensors to hold inputs and outputs 31 | x = torch.randn(N, D_in) 32 | y = torch.randn(N, D_out) 33 | 34 | # Construct our model by instantiating the class defined above 35 | model = TwoLayerNet(D_in, H, D_out) 36 | 37 | criterion = nn.MSELoss(reduction='sum') 38 | optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate) 39 | 40 | for t in range(500): 41 | y_pred = model(x) 42 | 43 | loss = criterion(y_pred, y) 44 | print(t, loss.item()) 45 | 46 | optimizer.zero_grad() 47 | loss.backward() 48 | optimizer.step() 49 | -------------------------------------------------------------------------------- /CV_net/DeiT/readme.md: -------------------------------------------------------------------------------- 1 | # DeiT 2 | DeiT: Training data-efficient image transformers & distillation through attention 3 | 4 | paper: [https://arxiv.org/abs/2012.12877](https://arxiv.org/abs/2012.12877) 5 | 6 | offical code: [https://github.com/facebookresearch/deit](https://github.com/facebookresearch/deit) 7 | 8 | # Usage: 9 | 1. Install timm=0.3.2 10 | ```Shell 11 | pip install timm==0.3.2 12 | ``` 13 | 2. Train or eval 14 | 15 | # Train teacher 16 | ```python 17 | python teacher --gpu 0 --teacher_model "resnet34" 18 | ``` 19 | 20 | # Train student or Train teacher in deit/vit series 21 | ```python 22 | # train without cache 23 | # --arch deit_tiny(default)/deit_small/deit_base/deit_base_384 24 | python train.py --gpu 0 25 | python train.py --gpu 0 --arch deit_small 26 | python train.py --gpu 0 --arch deit_base 27 | python train.py --gpu 0 --arch deit_base_384 input_size 384 28 | 29 | # train with pretrained model 30 | python train.py --gpu 0 --pretrained --model-path checkpoints/model_RegNetx_200mf_best.pt 31 | 32 | # train with resume 33 | python train.py --arch deit_tiny --resume checkpoints/model_deit_tiny_seed561_best.pt 34 | 35 | python train.py --gpu 0 --teacher_model "resnet34" --distillation-type "soft" --distillation-alpha 0.5 --distillation-tau 1.0 36 | 37 | ``` 38 | 39 | # Validate 40 | ```python 41 | # validate 42 | python train.py --evaluate --gpu 1 --resume checkpoints/model_deit_tiny_seed561_best.pt 43 | ``` 44 | -------------------------------------------------------------------------------- /CV_net/pytorch-center-offical/datasets.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torchvision 3 | from torch.utils.data import DataLoader 4 | 5 | import transforms 6 | 7 | class MNIST(object): 8 | def __init__(self, batch_size, use_gpu, num_workers): 9 | transform = transforms.Compose([ 10 | transforms.ToTensor(), 11 | transforms.Normalize((0.1307,), (0.3081,)) 12 | ]) 13 | 14 | pin_memory = True if use_gpu else False 15 | 16 | trainset = torchvision.datasets.MNIST(root='./data/mnist', train=True, download=True, transform=transform) 17 | 18 | trainloader = torch.utils.data.DataLoader( 19 | trainset, batch_size=batch_size, shuffle=True, 20 | num_workers=num_workers, pin_memory=pin_memory, 21 | ) 22 | 23 | testset = torchvision.datasets.MNIST(root='./data/mnist', train=False, download=True, transform=transform) 24 | 25 | testloader = torch.utils.data.DataLoader( 26 | testset, batch_size=batch_size, shuffle=False, 27 | num_workers=num_workers, pin_memory=pin_memory, 28 | ) 29 | 30 | self.trainloader = trainloader 31 | self.testloader = testloader 32 | self.num_classes = 10 33 | 34 | __factory = { 35 | 'mnist': MNIST, 36 | } 37 | 38 | def create(name, batch_size, use_gpu, num_workers): 39 | if name not in __factory.keys(): 40 | raise KeyError("Unknown dataset: {}".format(name)) 41 | return __factory[name](batch_size, use_gpu, num_workers) -------------------------------------------------------------------------------- /2020July_education_pytorch_teaching/data/two_layer_numpy.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2020/2/24 4 | # @Author :'IReverser' 5 | # @FileName: two_layer_numpy.py 6 | from __future__ import print_function 7 | import numpy as np 8 | 9 | # N is batch size; 10 | # D_in is input dimension; 11 | # H is hidden dimension; 12 | # D_out is output dimension. 13 | N, D_in, H, D_out = 64, 100, 100, 10 14 | learning_rate = 1e-5 15 | 16 | # Create random input and output data 17 | x = np.random.randn(N, D_in) 18 | y = np.random.randn(N, D_out) 19 | 20 | # Randomly initialize weights 21 | w1 = np.random.randn(D_in, H) 22 | w2 = np.random.randn(H, D_out) 23 | 24 | for i in range(500): 25 | # Forward pass: compute predicted y 26 | # .dot don't use in float if you change x(y) with normal. 27 | h = x.dot(w1) 28 | h_relu = np.maximum(h, 0) 29 | y_pred = h_relu.dot(w2) 30 | 31 | # Compute and print loss 32 | loss = np.square(y_pred-y).sum() 33 | print(i, loss) 34 | 35 | # Backprop to compute gradients of w1 and w2 with respect to loss. 36 | # loss = (y_pred - y) ** 2 37 | grad_y_pred = 2.0 * (y_pred - y) 38 | grad_w2 = h_relu.T.dot(grad_y_pred) 39 | grad_h_relu = grad_y_pred.dot(w2.T) 40 | grad_h = grad_h_relu.copy() 41 | grad_h[h < 0] = 0 42 | grad_w1 = x.T.dot(grad_h) 43 | 44 | # Update weights 45 | w1 -= learning_rate * grad_w1 46 | w2 -= learning_rate * grad_w2 47 | 48 | 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /CV_net/MobileNetv4/readme.md: -------------------------------------------------------------------------------- 1 | # MobileNetV4 2 | MobileNetV4 - Universal Models for the Mobile Ecosystem 3 | 4 | paper: [http://arxiv.org/abs/2404.10518](http://arxiv.org/abs/2404.10518) 5 | 6 | offical code: [https://github.com/tensorflow/models/blob/master/official/vision/modeling/backbones/mobilenet.py(tensorflow)](https://github.com/tensorflow/models/blob/master/official/vision/modeling/backbones/mobilenet.py(tensorflow)) 7 | 8 | # Usage 9 | 1. Install timm=0.3.2 10 | ```Shell 11 | pip install timm==0.3.2 12 | ``` 13 | 2. Train or eval 14 | # Train model in MobileNetv4 series (MNV4ConvSmall, MNV4ConvMedium, MNV4ConvLarge, MNV4HybridMedium, MNV4HybridLarge) 15 | ```python 16 | # train without cache 17 | python train.py --gpu 0 18 | python train.py --gpu 0 --arch 'MNV4ConvSmall' --batch_size 16 # 'MNV4ConvSmall, MNV4ConvMedium', 'MNV4ConvLarge', 'MNV4HybridMedium', 'MNV4HybridLarge' 19 | 20 | # train with resume 21 | python train.py --arch MNV4ConvSmall --resume checkpoints/model_MNV4ConvSmall_seed561_best.pt --gpu 0 22 | 23 | ``` 24 | 25 | # Validate 26 | ```python 27 | # validate 28 | python train.py --evaluate --gpu 1 --resume checkpoints/model_MNV4ConvSmall_seed561_best.pt 29 | ``` 30 | 31 | # Predict 32 | Modify ```model_name```, ```dataset_name```, ```MODEL_PATH``` and ```CLASS_NUM``` in ```predict.py``` script. Put pictures into ```results``` directory. 33 | ```python 34 | python predict.py # you need to modify model path at 18th line in the script. Default: './checkpoints/model_MNV4HybridMedium_seed772_best.pt' 35 | ``` 36 | -------------------------------------------------------------------------------- /2020July_education_pytorch_teaching/data/two_layers_tensors.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2020/2/24 4 | # @Author :'IReverser' 5 | # @FileName: two_layers_tensors.py 6 | import torch 7 | 8 | dtype = torch.float 9 | # device = torch.device("cpu") 10 | device = torch.device("cuda:0") 11 | 12 | # N is batch size; 13 | # D_in is input dimension; 14 | # H is hidden dimension; 15 | # D_out is output dimension. 16 | N, D_in, H, D_out = 64, 1000, 100, 10 17 | 18 | # Create random input and output data 19 | x = torch.randn(N, D_in, device=device, dtype=dtype) 20 | y = torch.randn(N, D_out, device=device, dtype=dtype) 21 | 22 | # Randomly initialize weights 23 | w1 = torch.randn(D_in, H, device=device, dtype=dtype) 24 | w2 = torch.randn(H, D_out, device=device, dtype=dtype) 25 | 26 | learning_rate = 1e-6 27 | for i in range(600): 28 | # Forward pass: compute predicted y 29 | h = x.mm(w1) 30 | h_relu = h.clamp(min=0) 31 | y_pred = h_relu.mm(w2) 32 | 33 | # Compute and print loss 34 | loss = (y_pred-y).pow(2).sum().item() 35 | print(i, loss) 36 | 37 | # Backprop to compute gradients of w1 and w2 with respect to loss. 38 | grad_y_pred = 2.0 * (y_pred - y) 39 | grad_w2 = h_relu.t().mm(grad_y_pred) 40 | grad_h_relu = grad_y_pred.mm(w2.t()) 41 | grad_h = grad_h_relu.clone() 42 | grad_h[h < 0] = 0 43 | grad_w1 = x.t().mm(grad_h) 44 | 45 | # Update weights using gradient descent 46 | w1 -= learning_rate * grad_w1 47 | w2 -= learning_rate * grad_w2 48 | 49 | 50 | -------------------------------------------------------------------------------- /CV_net/readme.md: -------------------------------------------------------------------------------- 1 | # Usage 2 | * Firstly, you need to download the flower dataset or your dataset in the corresponding file. (flowers dataset link: https://www.kaggle.com/alxmamaev/flowers-recognition) 3 | 4 | * And then, use the python script split_dataset.py. In the end, you can join my codes. 5 | 6 | * Datasets path is described as follow: 7 | 8 | 9 | datasets 10 | └─flowers 11 | └─train 12 | ├─daisy 13 | │ └─xxx.jpg 14 | ├─dandelion 15 | │ └─xxx.jpg 16 | ├─rose 17 | │ └─xxx.jpg 18 | ├─sunflower 19 | │ └─xxx.jpg 20 | ├─tulip 21 | └─xxx.jpg 22 | └─val 23 | ├─daisy 24 | │ └─xxx.jpg 25 | ├─dandelion 26 | │ └─xxx.jpg 27 | ├─rose 28 | │ └─xxx.jpg 29 | ├─sunflower 30 | │ └─xxx.jpg 31 | ├─tulip 32 | └─xxx.jpg 33 | 34 | ​ 35 | # Backbone List 36 | * LeNet 37 | * AlexNet (2012) 38 | * GoogLeNet (2014) 39 | * VGG (2016) 40 | * ResNet (2016) 41 | * WRN 42 | * SPPNet 43 | * ResNeXt 44 | * PreActResNet 45 | * DenseNet 46 | * MobileNet (v2/v3) 47 | * ShuffleNet (v1) 48 | * SENet 49 | * SKNet 50 | * ECANet 51 | * Vision Transformer 52 | * RegNet 53 | * RepVGG 54 | * GhostNet 55 | * MobileViT 56 | * DeiT 57 | * EffieientNetv1(EffieientNet) 58 | * EffieientNetv2 59 | * Swin Transformer 60 | * VanillaNet 61 | * NonLocalNN 62 | * TransNeXt (2024) 63 | * ConvMixer (2022) 64 | * MobileNetv4 (2024) 65 | * 66 | 67 | 68 | 69 | -------------------------------------------------------------------------------- /CV_net/LeNet/predict.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2021/7/29 16:44 4 | # @Author : '' 5 | # @FileName: predict.py 6 | 7 | import torch 8 | import torchvision.transforms as transform 9 | from PIL import Image 10 | import glob 11 | from model import LeNet 12 | 13 | 14 | MODEL_PATH = './model/LeNet1.pth' 15 | TEST_PATH = './results/' 16 | # category labels 17 | classes = ('airplane', 'automobile', 'bird', 'cat', 18 | 'deer', 'dog', 'frog', 'horse', 'ship', 'truck') 19 | device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') 20 | print('Use device: ', device) 21 | # pre-processing 22 | transform = transform.Compose( 23 | [transform.Resize((32, 32)), 24 | transform.ToTensor(), 25 | transform.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]) 26 | 27 | net = LeNet() 28 | net = net.to(device) 29 | net.load_state_dict(torch.load(MODEL_PATH)) 30 | 31 | for im_path in glob.glob(TEST_PATH + '*.jpg'): 32 | im = Image.open(im_path) 33 | im = transform(im) # [H, W, C] -> [C, H, W] 34 | im = torch.unsqueeze(im, dim=0) # [C, H, W] -> [N, C, H, W] 35 | im = im.to(device) 36 | 37 | with torch.no_grad(): 38 | output = net(im) 39 | # confidence = torch.max(output, dim=1)[0].cpu().data.numpy()[0] # option 40 | confidence = torch.max(torch.softmax(output, dim=1)).cpu().data.numpy() 41 | predict = torch.max(output, dim=1)[1].cpu().data.numpy() 42 | 43 | print('Vertification picture:', im_path.split('/')[-1], 44 | 'Recognition result:', classes[int(predict)], 45 | 'Recognition confidence:', str(confidence)) 46 | -------------------------------------------------------------------------------- /CV_net/pytorch-center-offical/center_loss.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | 4 | class CenterLoss(nn.Module): 5 | """Center loss. 6 | 7 | Reference: 8 | Wen et al. A Discriminative Feature Learning Approach for Deep Face Recognition. ECCV 2016. 9 | 10 | Args: 11 | num_classes (int): number of classes. 12 | feat_dim (int): feature dimension. 13 | """ 14 | def __init__(self, num_classes=10, feat_dim=2, use_gpu=True): 15 | super(CenterLoss, self).__init__() 16 | self.num_classes = num_classes 17 | self.feat_dim = feat_dim 18 | self.use_gpu = use_gpu 19 | 20 | if self.use_gpu: 21 | self.centers = nn.Parameter(torch.randn(self.num_classes, self.feat_dim).cuda()) 22 | else: 23 | self.centers = nn.Parameter(torch.randn(self.num_classes, self.feat_dim)) 24 | 25 | def forward(self, x, labels): 26 | """ 27 | Args: 28 | x: feature matrix with shape (batch_size, feat_dim). 29 | labels: ground truth labels with shape (batch_size). 30 | """ 31 | batch_size = x.size(0) 32 | distmat = torch.pow(x, 2).sum(dim=1, keepdim=True).expand(batch_size, self.num_classes) + \ 33 | torch.pow(self.centers, 2).sum(dim=1, keepdim=True).expand(self.num_classes, batch_size).t() 34 | distmat.addmm_(1, -2, x, self.centers.t()) 35 | 36 | classes = torch.arange(self.num_classes).long() 37 | if self.use_gpu: classes = classes.cuda() 38 | labels = labels.unsqueeze(1).expand(batch_size, self.num_classes) 39 | mask = labels.eq(classes.expand(batch_size, self.num_classes)) 40 | 41 | dist = distmat * mask.float() 42 | loss = dist.clamp(min=1e-12, max=1e+12).sum() / batch_size 43 | 44 | return loss 45 | -------------------------------------------------------------------------------- /CV_net/pytorch-center-offical/models.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | from torch.nn import functional as F 4 | 5 | import math 6 | 7 | class ConvNet(nn.Module): 8 | """LeNet++ as described in the Center Loss paper.""" 9 | def __init__(self, num_classes): 10 | super(ConvNet, self).__init__() 11 | self.conv1_1 = nn.Conv2d(1, 32, 5, stride=1, padding=2) 12 | self.prelu1_1 = nn.PReLU() 13 | self.conv1_2 = nn.Conv2d(32, 32, 5, stride=1, padding=2) 14 | self.prelu1_2 = nn.PReLU() 15 | 16 | self.conv2_1 = nn.Conv2d(32, 64, 5, stride=1, padding=2) 17 | self.prelu2_1 = nn.PReLU() 18 | self.conv2_2 = nn.Conv2d(64, 64, 5, stride=1, padding=2) 19 | self.prelu2_2 = nn.PReLU() 20 | 21 | self.conv3_1 = nn.Conv2d(64, 128, 5, stride=1, padding=2) 22 | self.prelu3_1 = nn.PReLU() 23 | self.conv3_2 = nn.Conv2d(128, 128, 5, stride=1, padding=2) 24 | self.prelu3_2 = nn.PReLU() 25 | 26 | self.fc1 = nn.Linear(128*3*3, 2) 27 | self.prelu_fc1 = nn.PReLU() 28 | self.fc2 = nn.Linear(2, num_classes) 29 | 30 | def forward(self, x): 31 | x = self.prelu1_1(self.conv1_1(x)) 32 | x = self.prelu1_2(self.conv1_2(x)) 33 | x = F.max_pool2d(x, 2) 34 | 35 | x = self.prelu2_1(self.conv2_1(x)) 36 | x = self.prelu2_2(self.conv2_2(x)) 37 | x = F.max_pool2d(x, 2) 38 | 39 | x = self.prelu3_1(self.conv3_1(x)) 40 | x = self.prelu3_2(self.conv3_2(x)) 41 | x = F.max_pool2d(x, 2) 42 | 43 | x = x.view(-1, 128*3*3) 44 | x = self.prelu_fc1(self.fc1(x)) 45 | y = self.fc2(x) 46 | 47 | return x, y 48 | 49 | __factory = { 50 | 'cnn': ConvNet, 51 | } 52 | 53 | def create(name, num_classes): 54 | if name not in __factory.keys(): 55 | raise KeyError("Unknown model: {}".format(name)) 56 | return __factory[name](num_classes) 57 | 58 | if __name__ == '__main__': 59 | pass -------------------------------------------------------------------------------- /2020July_education_pytorch_teaching/data/two_layers_tensor_autograd.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2020/2/24 4 | # @Author :'IReverser' 5 | # @FileName: two_layers_tensor_autograd.py 6 | 7 | import torch 8 | 9 | dtype = torch.float 10 | # device = torch.device("cpu") 11 | device = torch.device("cuda:0") 12 | 13 | # N is batch size; 14 | # D_in is input dimension; 15 | # H is hidden dimension; 16 | # D_out is output dimension. 17 | N, D_in, H, D_out = 64, 1000, 100, 10 18 | learning_rate = 1e-6 19 | 20 | # 创建随机的Tensor来保存输入和输出 21 | # 设定requires_grad=False表示在反向传播的时候我们不需要计算gradient 22 | x = torch.randn(N, D_in, device=device, dtype=dtype) 23 | y = torch.randn(N, D_out, device=device, dtype=dtype) 24 | 25 | # 创建随机的Tensor和权重。 26 | # 设置requires_grad=True表示我们希望反向传播的时候计算Tensor的gradient 27 | w1 = torch.randn(D_in, H, device=device, dtype=dtype, requires_grad=True) 28 | w2 = torch.randn(H, D_out, device=device, dtype=dtype, requires_grad=True) 29 | 30 | for t in range(500): 31 | # 前向传播:通过Tensor预测y;这个和普通的神经网络的前向传播没有任何不同, 32 | # 但是我们不需要保存网络的中间运算结果,因为我们不需要手动计算反向传播。 33 | y_pred = x.mm(w1).clamp(min=0).mm(w2) 34 | 35 | # 通过前向传播计算loss 36 | # loss是一个形状为(1,)的Tensor 37 | # loss.item()可以给我们返回一个loss的scalar 38 | loss = (y_pred - y).pow(2).sum() 39 | print(t, loss.item()) 40 | 41 | # PyTorch给我们提供了autograd的方法做反向传播。如果一个Tensor的requires_grad=True, 42 | # backward会自动计算loss相对于每个Tensor的gradient。 43 | # 在backward之后,w1.grad和w2.grad会包含两个loss相对于两个Tensor的gradient信息。 44 | loss.backward() 45 | 46 | # 用torch.no_grad()包含以下statements,因为w1和w2都是requires_grad=True, 47 | # 但是在更新weights之后我们并不需要再做autograd。 48 | # 另一种方法是在weight.data和weight.grad.data上做操作,这样就不会对grad产生影响。 49 | # tensor.data会我们一个tensor,这个tensor和原来的tensor指向相同的内存空间,但是不会记录计算图的历史。 50 | with torch.no_grad(): 51 | w1 -= learning_rate * w1.grad 52 | w2 -= learning_rate * w2.grad 53 | 54 | # Manually zero the gradients after updating weights 55 | w1.grad.zero_() 56 | w2.grad.zero_() 57 | 58 | 59 | -------------------------------------------------------------------------------- /mofan_python/1_numpy_torch.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import numpy as np 3 | 4 | # 1 5 | # np_data = np.arange(6).reshape((2, 3)) 6 | # torch_data = torch.from_numpy(np_data) 7 | # tensor2array = torch_data.numpy() 8 | # print( 9 | # '\nnumpy:', np_data, 10 | # '\ntorch:', torch_data, 11 | # '\ntensor2array', tensor2array, 12 | # ) 13 | 14 | 15 | # 2 abs 16 | data = [-1, -2, 1, 2] 17 | tensor = torch.FloatTensor(data) # 32bit floating point 18 | # http://pytorch.org/docs/torch.html#math-operations 19 | # print abs() 20 | # print( 21 | # '\nabs', 22 | # '\nnumpy:', np.abs(data), # [1 2 1 2] 23 | # '\ntorch:', tensor.abs(tensor) # [1 2 1 2] 24 | # ) 25 | # print sin() 26 | # print( 27 | # '\nsin', 28 | # '\nnumpy:', np.sin(data), # [1 2 1 2] 29 | # '\ntorch:', torch.sin(tensor) # [1 2 1 2] 30 | # ) 31 | # print( 32 | # '\nmean', 33 | # '\nnumpy:', np.mean(data), # [1 2 1 2] 34 | # '\ntorch:', torch.mean(tensor) # [1 2 1 2] 35 | # ) 36 | 37 | # 3 mutiply 38 | # data = [[1, 2], [3, 4]] 39 | # tensor = torch.FloatTensor(data) # 32-bit floating point 40 | # data = np.array(data) 41 | # print( 42 | # # '\ndata', np.matmul(data, data) 43 | # '\nnumpy:', data.dot(data), 44 | 45 | # '\ntorch:', sensor.mm(tensor) 46 | # '\ntorch:', torch.mm(tensor, tensor) 47 | 48 | # '\ntorch:', torch.dot(tensor) # different 49 | # ) 50 | 51 | ################################################## 52 | # variable applement 53 | from torch.autograd import Variable 54 | 55 | tensor = torch.FloatTensor([[1, 2], [3, 4]]) 56 | variable = Variable(tensor, requires_grad=True) # requires_grad use to define if infer backward(gradient) 57 | 58 | t_out = torch.mean(tensor*tensor) # x^2 59 | v_out = torch.mean(variable*variable) 60 | 61 | # print(tensor) 62 | # print(variable) 63 | print(t_out) 64 | print(v_out) 65 | # calculate gradient 66 | v_out.backward() 67 | # v_out = 1/4*sum(var*var) 68 | # d(v_out)/d(var) = 1/4*2*variable=variable/2 69 | print(variable.grad) 70 | print(variable.data) 71 | print(variable.data.numpy()) 72 | print(variable) 73 | 74 | 75 | -------------------------------------------------------------------------------- /CV_net/VGG/predict.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2021/8/25 16:14 4 | # @Author : 'IReverser' 5 | # @FileName: predict.py 6 | import os 7 | import json 8 | import glob 9 | import torch 10 | from PIL import Image 11 | from torchvision import transforms 12 | import matplotlib.pyplot as plt 13 | from model import vgg 14 | 15 | model_name = 'vgg16' 16 | MODEL_PATH = './model/' + model_name + '_flowers.pth' 17 | TEST_PATH = './results/' 18 | CLASS_NUM = 5 19 | device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') 20 | print('Use device: ', device) 21 | # pre-processing 22 | transform = transforms.Compose([transforms.Resize((224, 224)), 23 | transforms.ToTensor(), 24 | transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]) 25 | 26 | # read class_dictionary 27 | json_path = './classes_indices.json' 28 | assert os.path.exists(json_path), "file: '{}' does not exists.".format(json_path) 29 | 30 | json_file = open(json_path, "r") 31 | classes_indict = json.load(json_file) 32 | print(classes_indict) 33 | # create model 34 | net = vgg(model_name=model_name, n_classes=CLASS_NUM) 35 | net = net.to(device) 36 | 37 | # load model weights 38 | assert os.path.exists(MODEL_PATH), "file: '{}' does not exist.".format(MODEL_PATH) 39 | net.load_state_dict(torch.load(MODEL_PATH)) 40 | 41 | for im_path in glob.glob(TEST_PATH + '*.jpg'): 42 | # load data 43 | im = Image.open(im_path) 44 | im = transform(im) # [H, W, C] -> [C, H, W] 45 | im = torch.unsqueeze(im, dim=0) # [C, H, W] -> [N, C, H, W] 46 | im = im.to(device) 47 | 48 | net.eval() 49 | with torch.no_grad(): 50 | output = net(im) 51 | # confidence = torch.max(output, dim=1)[0].cpu().data.numpy()[0] # option 52 | confidence = torch.max(torch.softmax(output, dim=1)).cpu().data.numpy() 53 | predict = torch.max(output, dim=1)[1].cpu().data.numpy() 54 | 55 | print('Vertification picture:', im_path.split('/')[-1], '\n', 56 | 'Recognition result:', classes_indict[str(int(predict))], '\n', 57 | 'Recognition confidence:', str(confidence)) 58 | 59 | -------------------------------------------------------------------------------- /CV_net/GooLeNet/predict.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2021/8/28 17:32 4 | # @Author : '' 5 | # @FileName: predict.py 6 | import os 7 | import json 8 | import glob 9 | import torch 10 | from PIL import Image 11 | from torchvision import transforms 12 | import matplotlib.pyplot as plt 13 | from model import GoogLeNet 14 | 15 | model_name = 'GoogLeNet' 16 | MODEL_PATH = './model/' + model_name + '_flowers.pth' 17 | TEST_PATH = './results/' 18 | CLASS_NUM = 5 19 | device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') 20 | print('Use device: ', device) 21 | # pre-processing 22 | transform = transforms.Compose([transforms.Resize((224, 224)), 23 | transforms.ToTensor(), 24 | transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]) 25 | 26 | # read class_dictionary 27 | json_path = './classes_indices.json' 28 | assert os.path.exists(json_path), "file: '{}' does not exists.".format(json_path) 29 | 30 | json_file = open(json_path, "r") 31 | classes_indict = json.load(json_file) 32 | print(classes_indict) 33 | # create model 34 | net = GoogLeNet(n_classes=CLASS_NUM, aux_logits=False) 35 | net = net.to(device) 36 | 37 | # load model weights 38 | assert os.path.exists(MODEL_PATH), "file: '{}' does not exist.".format(MODEL_PATH) 39 | missing_keys, unexpected_keys = net.load_state_dict(torch.load(MODEL_PATH), state_dict=False) 40 | 41 | for im_path in glob.glob(TEST_PATH + '*.jpg'): 42 | # load data 43 | im = Image.open(im_path) 44 | im = transform(im) # [H, W, C] -> [C, H, W] 45 | im = torch.unsqueeze(im, dim=0) # [C, H, W] -> [N, C, H, W] 46 | im = im.to(device) 47 | 48 | net.eval() 49 | with torch.no_grad(): 50 | output = net(im) 51 | # confidence = torch.max(output, dim=1)[0].cpu().data.numpy()[0] # option 52 | confidence = torch.max(torch.softmax(output, dim=1)).cpu().data.numpy() 53 | predict = torch.max(output, dim=1)[1].cpu().data.numpy() 54 | 55 | print('Vertification picture:', im_path.split('/')[-1], '\t', 56 | 'Recognition result:', classes_indict[str(int(predict))], '\t', 57 | 'Recognition confidence:', str(confidence)) 58 | 59 | 60 | -------------------------------------------------------------------------------- /CV_net/SeNet/predict.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2021/11/25 14:07 4 | # @Author : '' 5 | # @FileName: predict.py 6 | import os 7 | import json 8 | import glob 9 | import torch 10 | from PIL import Image 11 | from torchvision import transforms 12 | import matplotlib.pyplot as plt 13 | from model import senet18, senet34, senet50, senet101, senet152 14 | 15 | model_name = 'senet50' 16 | dataset_name = 'flowers' 17 | MODEL_PATH = './model/' + model_name + '_' + dataset_name + '.pth' 18 | TEST_PATH = './results/' 19 | CLASS_NUM = 5 20 | device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') 21 | print('Use device: ', device) 22 | # pre-processing 23 | transform = transforms.Compose([transforms.Resize((224, 224)), 24 | transforms.ToTensor(), 25 | transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])]) 26 | 27 | # read class_dictionary 28 | json_path = './classes_indices.json' 29 | assert os.path.exists(json_path), "file: '{}' does not exists.".format(json_path) 30 | 31 | json_file = open(json_path, "r") 32 | classes_indict = json.load(json_file) 33 | print(classes_indict) 34 | # create model 35 | net = senet50(n_classes=CLASS_NUM) 36 | net = net.to(device) 37 | 38 | # load model weights 39 | assert os.path.exists(MODEL_PATH), "file: '{}' does not exist.".format(MODEL_PATH) 40 | net.load_state_dict(torch.load(MODEL_PATH)) 41 | 42 | for im_path in glob.glob(TEST_PATH + '*.jpg'): 43 | # load data 44 | im = Image.open(im_path) 45 | im = transform(im) # [H, W, C] -> [C, H, W] 46 | im = torch.unsqueeze(im, dim=0) # [C, H, W] -> [N, C, H, W] 47 | im = im.to(device) 48 | 49 | net.eval() 50 | with torch.no_grad(): 51 | output = net(im) 52 | # confidence = torch.max(output, dim=1)[0].cpu().data.numpy()[0] # option 53 | confidence = torch.max(torch.softmax(output, dim=1)).cpu().data.numpy() 54 | predict = torch.max(output, dim=1)[1].cpu().data.numpy() 55 | 56 | print('Vertification picture:', im_path.split('/')[-1], '\t', 57 | 'Recognition result:', classes_indict[str(int(predict))], '\t', 58 | 'Recognition confidence:', str(confidence)) 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /CV_net/LeNet/model.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2021/7/29 16:43 4 | # @Author : 'IReverser' 5 | # @FileName: model.py 6 | import torch 7 | import torch.nn.functional as F 8 | import torch.nn as nn 9 | 10 | 11 | class LeNet(nn.Module): 12 | def __init__(self): 13 | super(LeNet, self).__init__() 14 | self.conv1 = nn.Conv2d(3, 16, 5) # conv2d: in_channel, out_channel, kernel_size 15 | self.pool1 = nn.MaxPool2d(2, 2) # MaxPool2d: h, w 16 | self.conv2 = nn.Conv2d(16, 32, 5) 17 | self.pool2 = nn.MaxPool2d(2, 2) 18 | self.fc1 = nn.Linear(32*5*5, 120) # Linear: in_connection, out_connection 19 | self.fc2 = nn.Linear(120, 84) 20 | self.fc3 = nn.Linear(84, 10) # classes = 10 21 | 22 | def forward(self, x): 23 | x = F.relu(self.conv1(x)) 24 | x = self.pool1(x) 25 | x = F.relu(self.conv2(x)) 26 | x = self.pool2(x) 27 | x = x.view(-1, 32*5*5) 28 | x = F.relu(self.fc1(x)) 29 | x = F.relu(self.fc2(x)) 30 | x = self.fc3(x) 31 | return x 32 | 33 | 34 | class LeNet_MNIST(nn.Module): 35 | """ 36 | LeNet for mnist dataset. 37 | """ 38 | def __init__(self): 39 | super(LeNet_MNIST, self).__init__() 40 | self.conv1 = nn.Conv2d(1, 6, 5, padding=2) # conv2d: in_channel, out_channel, kernel_size 41 | self.pool1 = nn.MaxPool2d(2, 2) # MaxPool2d: h, w 42 | self.conv2 = nn.Conv2d(6, 16, 5) 43 | self.pool2 = nn.MaxPool2d(2, 2) 44 | self.fc1 = nn.Linear(16*5*5, 120) # Linear: in_connection, out_connection 45 | self.fc2 = nn.Linear(120, 84) 46 | self.fc3 = nn.Linear(84, 10) # classes = 10 47 | 48 | def forward(self, x): 49 | x = F.relu(self.conv1(x)) # 6*5*5,output:6*28*28 50 | x = self.pool1(x) # 6*14*14 51 | x = F.relu(self.conv2(x)) # 16*10*10 52 | x = self.pool2(x) # 16*5*5 53 | x = x.view(-1, 16*5*5) 54 | x = F.relu(self.fc1(x)) 55 | x = F.relu(self.fc2(x)) 56 | x = self.fc3(x) 57 | return x 58 | 59 | # test 60 | # x = torch.rand((1, 1, 28, 28)) 61 | # net = LeNet_MNIST() 62 | # y = net(x) 63 | # print(y.shape) 64 | -------------------------------------------------------------------------------- /CV_net/ResNet/predict.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2021/9/19 14:53 4 | # @Author : 'IReverser' 5 | # @FileName: predict.py 6 | import os 7 | import json 8 | import glob 9 | import torch 10 | from PIL import Image 11 | from torchvision import transforms 12 | import matplotlib.pyplot as plt 13 | from model import resnet18, resnet34, resnet50, resnet101, resnet152 14 | 15 | model_name = 'resnet34' 16 | dataset_name = 'flowers' 17 | MODEL_PATH = './model/' + model_name + '_' + dataset_name + '.pth' 18 | TEST_PATH = './results/' 19 | CLASS_NUM = 5 20 | device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') 21 | print('Use device: ', device) 22 | # pre-processing 23 | transform = transforms.Compose([transforms.Resize((224, 224)), 24 | transforms.ToTensor(), 25 | transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])]) 26 | 27 | # read class_dictionary 28 | json_path = './classes_indices.json' 29 | assert os.path.exists(json_path), "file: '{}' does not exists.".format(json_path) 30 | 31 | json_file = open(json_path, "r") 32 | classes_indict = json.load(json_file) 33 | print(classes_indict) 34 | # create model 35 | net = resnet34(n_classes=CLASS_NUM) 36 | net = net.to(device) 37 | 38 | # load model weights 39 | assert os.path.exists(MODEL_PATH), "file: '{}' does not exist.".format(MODEL_PATH) 40 | net.load_state_dict(torch.load(MODEL_PATH)) 41 | 42 | for im_path in glob.glob(TEST_PATH + '*.jpg'): 43 | # load data 44 | im = Image.open(im_path).convert("RGB") 45 | im = transform(im) # [H, W, C] -> [C, H, W] 46 | im = torch.unsqueeze(im, dim=0) # [C, H, W] -> [N, C, H, W] 47 | im = im.to(device) 48 | 49 | net.eval() 50 | with torch.no_grad(): 51 | output = net(im) 52 | # confidence = torch.max(output, dim=1)[0].cpu().data.numpy()[0] # option 53 | confidence = torch.max(torch.softmax(output, dim=1)).cpu().data.numpy() 54 | predict = torch.max(output, dim=1)[1].cpu().data.numpy() 55 | 56 | print('Vertification picture:', im_path.split('/')[-1], '\t', 57 | 'Recognition result:', classes_indict[str(int(predict))], '\t', 58 | 'Recognition confidence:', str(confidence)) 59 | 60 | -------------------------------------------------------------------------------- /mofan_python/3_regression.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # Author = 'IReverser' 4 | """ 5 | 这是一个利用了回归处理的神经网络教程 6 | """ 7 | 8 | import torch 9 | import torch.nn.functional as F 10 | from torch.autograd import Variable 11 | import matplotlib.pyplot as plt 12 | import numpy 13 | import time 14 | 15 | # unsqueeze use to transfor one dimension to two dimension 16 | x = torch.unsqueeze(torch.linspace(-1, 1, 100), dim=1) # x data (tensor),shape(100, 1) 17 | y = x.pow(2) + 0.2 * torch.rand(x.size()) # noisy y data (tensor), shape=(100, 1) 18 | 19 | # x, y = Variable(x), Variable(y) 20 | 21 | # plt.scatter(x.data.numpy(), y.data.numpy()) 22 | # plt.show() 23 | 24 | 25 | class Net(torch.nn.Module): 26 | def __init__(self, n_feature, n_hidden, n_output): 27 | super(Net, self).__init__() 28 | self.hidden = torch.nn.Linear(n_feature, n_hidden) 29 | self.predict = torch.nn.Linear(n_hidden, n_output) 30 | 31 | def forward(self, x): 32 | x = F.relu(self.hidden(x)) 33 | x = self.predict(x) 34 | return x 35 | 36 | 37 | net = Net(1, 10, 1) 38 | print(net) 39 | 40 | optimizer = torch.optim.SGD(net.parameters(), lr=0.2) 41 | loss_func = torch.nn.MSELoss() 42 | 43 | start_time = time.time() 44 | plt.ion() 45 | 46 | for t in range(500): 47 | print(t) 48 | prediction = net(x) 49 | 50 | loss = loss_func(prediction, y) 51 | 52 | optimizer.zero_grad() # set gradient of parameters to zero 53 | loss.backward() # calculate gradient of per node 54 | optimizer.step() # optimize gradient 55 | 56 | if t % 5 == 0: 57 | # plot and show learning process 58 | plt.cla() 59 | plt.scatter(x.data.numpy(), y.data.numpy()) 60 | plt.plot(x.data.numpy(), prediction.data.numpy(), 'r-', lw=5) 61 | plt.text(0.5, 0, 'Loss=%.4f' % loss.data.numpy(), fontdict={'size': 20, 'color': 'red'}) 62 | plt.pause(0.1) 63 | 64 | end_time = time.time() 65 | print('The total cost time:', str(end_time - start_time) + 'sec') 66 | plt.ioff() 67 | plt.show() 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | -------------------------------------------------------------------------------- /CV_net/pytorch-center-offical/utils.py: -------------------------------------------------------------------------------- 1 | import os 2 | import sys 3 | import errno 4 | import shutil 5 | import os.path as osp 6 | 7 | import torch 8 | 9 | 10 | def mkdir_if_missing(directory): 11 | if not osp.exists(directory): 12 | try: 13 | os.makedirs(directory) 14 | except OSError as e: 15 | if e.errno != errno.EEXIST: 16 | raise 17 | 18 | 19 | class AverageMeter(object): 20 | """Computes and stores the average and current value. 21 | 22 | Code imported from https://github.com/pytorch/examples/blob/master/imagenet/main.py#L247-L262 23 | """ 24 | def __init__(self): 25 | self.reset() 26 | 27 | def reset(self): 28 | self.val = 0 29 | self.avg = 0 30 | self.sum = 0 31 | self.count = 0 32 | 33 | def update(self, val, n=1): 34 | self.val = val 35 | self.sum += val * n 36 | self.count += n 37 | self.avg = self.sum / self.count 38 | 39 | 40 | def save_checkpoint(state, is_best, fpath='checkpoint.pth.tar'): 41 | mkdir_if_missing(osp.dirname(fpath)) 42 | torch.save(state, fpath) 43 | if is_best: 44 | shutil.copy(fpath, osp.join(osp.dirname(fpath), 'best_model.pth.tar')) 45 | 46 | class Logger(object): 47 | """ 48 | Write console output to external text file. 49 | 50 | Code imported from https://github.com/Cysu/open-reid/blob/master/reid/utils/logging.py. 51 | """ 52 | def __init__(self, fpath=None): 53 | self.console = sys.stdout 54 | self.file = None 55 | if fpath is not None: 56 | mkdir_if_missing(os.path.dirname(fpath)) 57 | self.file = open(fpath, 'w') 58 | 59 | def __del__(self): 60 | self.close() 61 | 62 | def __enter__(self): 63 | pass 64 | 65 | def __exit__(self, *args): 66 | self.close() 67 | 68 | def write(self, msg): 69 | self.console.write(msg) 70 | if self.file is not None: 71 | self.file.write(msg) 72 | 73 | def flush(self): 74 | self.console.flush() 75 | if self.file is not None: 76 | self.file.flush() 77 | os.fsync(self.file.fileno()) 78 | 79 | def close(self): 80 | self.console.close() 81 | if self.file is not None: 82 | self.file.close() -------------------------------------------------------------------------------- /2020July_education_pytorch_teaching/data/NLP_fizz_buzz.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2020/2/25 20:22 4 | # @Author :'' 5 | # @FileName: NLP_fizz_buzz.py 6 | import numpy as np 7 | import torch 8 | from torch import nn 9 | from torch import optim 10 | 11 | 12 | NUM_DIGITS = 10 13 | 14 | 15 | def fizz_buzz_encode(i): 16 | if i % 15 == 0: return 3 17 | elif i % 5 == 0: return 2 18 | elif i % 3 == 0: return 1 19 | else: return 0 20 | 21 | 22 | def fizz_buzz_decode(i, prediction): 23 | return [str(i), "fizz", "buzz", "fizzbuzz"][prediction] 24 | 25 | 26 | # Represent each input by an array of its binary digits. 27 | def binary_encode(i, NUM_DIGITS): 28 | return np.array([i >> d & 1 for d in range(NUM_DIGITS)]) 29 | 30 | 31 | # Define the model 32 | NUM_HIDDEN = 100 33 | model = nn.Sequential( 34 | nn.Linear(NUM_DIGITS, NUM_HIDDEN), 35 | nn.ReLU(), 36 | nn.Linear(NUM_HIDDEN, 4) 37 | ) 38 | 39 | loss_fn = nn.CrossEntropyLoss() 40 | optimizer = optim.SGD(model.parameters(), lr=0.05) 41 | 42 | 43 | BATCH_SIZE = 128 44 | trX = torch.Tensor([binary_encode(i, NUM_DIGITS) for i in range(101, 2 ** NUM_DIGITS)]) 45 | trY = torch.LongTensor([fizz_buzz_encode(i) for i in range(101, 2 ** NUM_DIGITS)]) 46 | 47 | # Start training it 48 | for epoch in range(10000): 49 | for start in range(0, len(trX), BATCH_SIZE): 50 | end = start + BATCH_SIZE 51 | batchX = trX[start:end] 52 | batchY = trY[start:end] 53 | 54 | y_pred = model(batchX) 55 | loss = loss_fn(y_pred, batchY) 56 | 57 | optimizer.zero_grad() 58 | loss.backward() 59 | optimizer.step() 60 | 61 | # Find loss on training data 62 | loss = loss_fn(model(trX), trY).item() 63 | print("Epoch:", epoch, "Loss:", loss) 64 | 65 | 66 | # Output now 67 | testX = torch.Tensor([binary_encode(i, NUM_DIGITS) for i in range(1, 101)]) 68 | 69 | with torch.no_grad(): 70 | testY = model(testX) 71 | predictions = zip(range(1, 101), list(testY.max(1)[1].data.tolist())) 72 | 73 | print([fizz_buzz_decode(i, x) for (i, x) in predictions]) 74 | print(testY.max(1)[1].numpy() == np.array([fizz_buzz_encode(i) for i in range(1, 101)])) 75 | print(np.sum(testY.max(1)[1].numpy() == np.array([fizz_buzz_encode(i) for i in range(1, 101)]))) 76 | -------------------------------------------------------------------------------- /CV_net/SKnet/predict.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2021/11/29 9:01 4 | # @Author : '' 5 | # @FileName: predict.py 6 | import os 7 | import json 8 | import glob 9 | import torch 10 | from PIL import Image 11 | from torchvision import transforms 12 | import matplotlib.pyplot as plt 13 | from model import SKNet50, SKNet101, SKNet152 14 | 15 | model_name = 'SKNet50' 16 | dataset_name = 'flowers' 17 | MODEL_PATH = './model/' + model_name + '_' + dataset_name + '.pth' 18 | CLASS_NUM = 5 19 | TEST_PATH = './results/' 20 | assert os.path.exists(TEST_PATH), "file: '{}' does not exists.".format(TEST_PATH) 21 | device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') 22 | print('Use device: ', device) 23 | # pre-processing 24 | transform = transforms.Compose([transforms.Resize((224, 224)), 25 | transforms.ToTensor(), 26 | transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])]) 27 | 28 | # read class_dictionary 29 | json_path = './classes_indices.json' 30 | assert os.path.exists(json_path), "file: '{}' does not exists.".format(json_path) 31 | 32 | json_file = open(json_path, "r") 33 | classes_indict = json.load(json_file) 34 | print(classes_indict) 35 | # create model 36 | net = SKNet50(n_classes=CLASS_NUM) 37 | net = net.to(device) 38 | 39 | # load model weights 40 | assert os.path.exists(MODEL_PATH), "file: '{}' does not exist.".format(MODEL_PATH) 41 | net.load_state_dict(torch.load(MODEL_PATH)) 42 | 43 | for im_path in glob.glob(TEST_PATH + '*.jpg'): 44 | # load data 45 | im = Image.open(im_path) 46 | im = transform(im) # [H, W, C] -> [C, H, W] 47 | im = torch.unsqueeze(im, dim=0) # [C, H, W] -> [N, C, H, W] 48 | im = im.to(device) 49 | 50 | net.eval() 51 | with torch.no_grad(): 52 | output = net(im) 53 | # confidence = torch.max(output, dim=1)[0].cpu().data.numpy()[0] # option 54 | confidence = torch.max(torch.softmax(output, dim=1)).cpu().data.numpy() 55 | predict = torch.max(output, dim=1)[1].cpu().data.numpy() 56 | 57 | print('Vertification picture:', im_path.split('/')[-1], '\t', 58 | 'Recognition result:', classes_indict[str(int(predict))], '\t', 59 | 'Recognition confidence:', str(confidence)) 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /CV_net/WRN/predict.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2021/9/19 14:53 4 | # @Author : 'IReverser' 5 | # @FileName: predict.py 6 | import os 7 | import json 8 | import glob 9 | import torch 10 | from PIL import Image 11 | from torchvision import transforms 12 | import matplotlib.pyplot as plt 13 | from model import wrn_40_2, wrn_50_2, wrn_101_2, wrn_28_10 14 | 15 | model_name = 'wrn_28_10' 16 | dataset_name = 'flowers' 17 | MODEL_PATH = './model/' + model_name + '_' + dataset_name + '.pth' 18 | CLASS_NUM = 10 19 | TEST_PATH = './results/' 20 | assert os.path.exists(TEST_PATH), "file: '{}' does not exists.".format(TEST_PATH) 21 | device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') 22 | print('Use device: ', device) 23 | # pre-processing 24 | transform = transforms.Compose([transforms.Resize((32, 32)), 25 | transforms.ToTensor(), 26 | transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])]) 27 | 28 | # read class_dictionary 29 | json_path = './classes_indices.json' 30 | assert os.path.exists(json_path), "file: '{}' does not exists.".format(json_path) 31 | 32 | json_file = open(json_path, "r") 33 | classes_indict = json.load(json_file) 34 | print(classes_indict) 35 | # create model 36 | net = wrn_28_10(num_classes=CLASS_NUM) 37 | net = net.to(device) 38 | 39 | # load model weights 40 | assert os.path.exists(MODEL_PATH), "file: '{}' does not exist.".format(MODEL_PATH) 41 | net.load_state_dict(torch.load(MODEL_PATH)) 42 | 43 | for im_path in glob.glob(TEST_PATH + '*.jpg'): 44 | # load data 45 | im = Image.open(im_path) 46 | im = transform(im) # [H, W, C] -> [C, H, W] 47 | im = torch.unsqueeze(im, dim=0) # [C, H, W] -> [N, C, H, W] 48 | im = im.to(device) 49 | 50 | net.eval() 51 | with torch.no_grad(): 52 | output = net(im) 53 | # confidence = torch.max(output, dim=1)[0].cpu().data.numpy()[0] # option 54 | confidence = torch.max(torch.softmax(output, dim=1)).cpu().data.numpy() 55 | predict = torch.max(output, dim=1)[1].cpu().data.numpy() 56 | 57 | print('Vertification picture:', im_path.split('/')[-1], '\t', 58 | 'Recognition result:', classes_indict[str(int(predict))], '\t', 59 | 'Recognition confidence:', str(confidence)) 60 | 61 | -------------------------------------------------------------------------------- /CV_net/NonLocalNN/predict.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2021/11/26 17:12 4 | # @Author : '' 5 | # @FileName: predict.py 6 | import os 7 | import json 8 | import glob 9 | import torch 10 | from PIL import Image 11 | from torchvision import transforms 12 | import matplotlib.pyplot as plt 13 | from nonlocalnn import model_list 14 | 15 | 16 | model_name = 'resnet56' 17 | dataset_name = 'flowers' 18 | MODEL_PATH = './model/' + model_name + '_' + dataset_name + '.pth' 19 | CLASS_NUM = 5 20 | TEST_PATH = './results/' 21 | assert os.path.exists(TEST_PATH), "file: '{}' does not exists.".format(TEST_PATH) 22 | device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') 23 | print('Use device: ', device) 24 | # pre-processing 25 | transform = transforms.Compose([transforms.Resize((224, 224)), 26 | transforms.ToTensor(), 27 | transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])]) 28 | 29 | # read class_dictionary 30 | json_path = './classes_indices.json' 31 | assert os.path.exists(json_path), "file: '{}' does not exists.".format(json_path) 32 | 33 | json_file = open(json_path, "r") 34 | classes_indict = json.load(json_file) 35 | print(classes_indict) 36 | # create model 37 | net = model_list.get(model_name)(num_classes=CLASS_NUM) 38 | net = net.to(device) 39 | 40 | # load model weights 41 | assert os.path.exists(MODEL_PATH), "file: '{}' does not exist.".format(MODEL_PATH) 42 | net.load_state_dict(torch.load(MODEL_PATH)) 43 | 44 | for im_path in glob.glob(TEST_PATH + '*.jpg'): 45 | # load data 46 | im = Image.open(im_path) 47 | im = transform(im) # [H, W, C] -> [C, H, W] 48 | im = torch.unsqueeze(im, dim=0) # [C, H, W] -> [N, C, H, W] 49 | im = im.to(device) 50 | 51 | net.eval() 52 | with torch.no_grad(): 53 | output = net(im) 54 | # confidence = torch.max(output, dim=1)[0].cpu().data.numpy()[0] # option 55 | confidence = torch.max(torch.softmax(output, dim=1)).cpu().data.numpy() 56 | predict = torch.max(output, dim=1)[1].cpu().data.numpy() 57 | 58 | print('Vertification picture:', im_path.split('/')[-1], '\t', 59 | 'Recognition result:', classes_indict[str(int(predict))], '\t', 60 | 'Recognition confidence:', str(confidence)) 61 | 62 | 63 | -------------------------------------------------------------------------------- /CV_net/PreActResNet/predict.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2021/9/19 14:53 4 | # @Author : 'IReverser' 5 | # @FileName: predict.py 6 | import os 7 | import json 8 | import glob 9 | import torch 10 | from PIL import Image 11 | from torchvision import transforms 12 | import matplotlib.pyplot as plt 13 | from model import wrn_40_2, wrn_50_2, wrn_101_2, wrn_28_10 14 | 15 | model_name = 'wrn_28_10' 16 | dataset_name = 'flowers' 17 | MODEL_PATH = './model/' + model_name + '_' + dataset_name + '.pth' 18 | CLASS_NUM = 10 19 | TEST_PATH = './results/' 20 | assert os.path.exists(TEST_PATH), "file: '{}' does not exists.".format(TEST_PATH) 21 | device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') 22 | print('Use device: ', device) 23 | # pre-processing 24 | transform = transforms.Compose([transforms.Resize((32, 32)), 25 | transforms.ToTensor(), 26 | transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])]) 27 | 28 | # read class_dictionary 29 | json_path = './classes_indices.json' 30 | assert os.path.exists(json_path), "file: '{}' does not exists.".format(json_path) 31 | 32 | json_file = open(json_path, "r") 33 | classes_indict = json.load(json_file) 34 | print(classes_indict) 35 | # create model 36 | net = wrn_28_10(num_classes=CLASS_NUM) 37 | net = net.to(device) 38 | 39 | # load model weights 40 | assert os.path.exists(MODEL_PATH), "file: '{}' does not exist.".format(MODEL_PATH) 41 | net.load_state_dict(torch.load(MODEL_PATH)) 42 | 43 | for im_path in glob.glob(TEST_PATH + '*.jpg'): 44 | # load data 45 | im = Image.open(im_path) 46 | im = transform(im) # [H, W, C] -> [C, H, W] 47 | im = torch.unsqueeze(im, dim=0) # [C, H, W] -> [N, C, H, W] 48 | im = im.to(device) 49 | 50 | net.eval() 51 | with torch.no_grad(): 52 | output = net(im) 53 | # confidence = torch.max(output, dim=1)[0].cpu().data.numpy()[0] # option 54 | confidence = torch.max(torch.softmax(output, dim=1)).cpu().data.numpy() 55 | predict = torch.max(output, dim=1)[1].cpu().data.numpy() 56 | 57 | print('Vertification picture:', im_path.split('/')[-1], '\t', 58 | 'Recognition result:', classes_indict[str(int(predict))], '\t', 59 | 'Recognition confidence:', str(confidence)) 60 | 61 | -------------------------------------------------------------------------------- /CV_net/MobileNet/predict.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2021/12/8 22:21 4 | # @Author : '' 5 | # @FileName: predict.py 6 | import os 7 | import json 8 | import glob 9 | import torch 10 | from PIL import Image 11 | from torchvision import transforms 12 | import matplotlib.pyplot as plt 13 | from mobilenet_v2 import MobileNetv2 14 | 15 | model_name = 'mobilenet_v2' 16 | dataset_name = 'flowers' 17 | MODEL_PATH = './model/' + model_name + '_' + dataset_name + '.pth' 18 | CLASS_NUM = 5 19 | TEST_PATH = './results/' 20 | assert os.path.exists(TEST_PATH), "file: '{}' does not exists.".format(TEST_PATH) 21 | device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') 22 | print('Use device: ', device) 23 | # pre-processing 24 | transform = transforms.Compose([transforms.Resize((224, 224)), 25 | transforms.ToTensor(), 26 | transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])]) 27 | 28 | # read class_dictionary 29 | json_path = './classes_indices.json' 30 | assert os.path.exists(json_path), "file: '{}' does not exists.".format(json_path) 31 | 32 | json_file = open(json_path, "r") 33 | classes_indict = json.load(json_file) 34 | print(classes_indict) 35 | # create model 36 | net = MobileNetv2(n_classes=CLASS_NUM, alpha=1.0, round_nearest=8) 37 | net = net.to(device) 38 | 39 | # load model weights 40 | assert os.path.exists(MODEL_PATH), "file: '{}' does not exist.".format(MODEL_PATH) 41 | net.load_state_dict(torch.load(MODEL_PATH)) 42 | 43 | for im_path in glob.glob(TEST_PATH + '*.jpg'): 44 | # load data 45 | im = Image.open(im_path) 46 | im = transform(im) # [H, W, C] -> [C, H, W] 47 | im = torch.unsqueeze(im, dim=0) # [C, H, W] -> [N, C, H, W] 48 | im = im.to(device) 49 | 50 | net.eval() 51 | with torch.no_grad(): 52 | output = net(im) 53 | # confidence = torch.max(output, dim=1)[0].cpu().data.numpy()[0] # option 54 | confidence = torch.max(torch.softmax(output, dim=1)).cpu().data.numpy() 55 | predict = torch.max(output, dim=1)[1].cpu().data.numpy() 56 | 57 | print('Vertification picture:', im_path.split('/')[-1], '\t', 58 | 'Recognition result:', classes_indict[str(int(predict))], '\t', 59 | 'Recognition confidence:', str(confidence)) 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /CV_net/AlexNet/model.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2021/8/6 22:11 4 | # @Author : '' 5 | # @FileName: model.py 6 | import torch 7 | import torch.nn as nn 8 | 9 | 10 | class AlexNet(nn.Module): 11 | def __init__(self, n_classes=1000, init_weights=False): 12 | super(AlexNet, self).__init__() 13 | self.feature = nn.Sequential( 14 | nn.ZeroPad2d(padding=(1, 2, 1, 2)), 15 | nn.Conv2d(3, 96, kernel_size=11, stride=4, padding=0), 16 | nn.ReLU(inplace=True), 17 | nn.MaxPool2d(kernel_size=3, stride=2), 18 | nn.Conv2d(96, 256, kernel_size=5, stride=1, padding=2), 19 | nn.MaxPool2d(kernel_size=3, stride=2), 20 | nn.Conv2d(256, 384, kernel_size=3, stride=1, padding=1), 21 | nn.ReLU(inplace=True), 22 | nn.Conv2d(384, 384, kernel_size=3, stride=1, padding=1), 23 | nn.ReLU(inplace=True), 24 | nn.Conv2d(384, 256, kernel_size=3, stride=1, padding=1), 25 | nn.ReLU(inplace=True), 26 | nn.MaxPool2d(kernel_size=3, stride=2) 27 | ) 28 | 29 | self.classify = nn.Sequential( 30 | nn.Dropout(p=0.5), 31 | nn.Linear(256 * 6 * 6, 4096), 32 | nn.ReLU(inplace=True), 33 | nn.Dropout(p=0.5), 34 | nn.Linear(4096, 4096), 35 | nn.ReLU(inplace=True), 36 | nn.Linear(4096, n_classes) 37 | ) 38 | 39 | if init_weights: 40 | self._initialize_weights() 41 | 42 | def forward(self, x): 43 | x = self.feature(x) 44 | # print(x.shape) 45 | x = torch.flatten(x, start_dim=1) 46 | # print(x.shape) 47 | x = self.classify(x) 48 | return x 49 | 50 | def _initialize_weights(self): 51 | for m in self.modules(): 52 | if isinstance(m, nn.Conv2d): 53 | nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') 54 | if m.bias is not None: 55 | nn.init.constant_(m.bias, 0) 56 | elif isinstance(m, nn.Linear): 57 | nn.init.normal_(m.weight, 0, 0.01) 58 | if m.bias is not None: 59 | nn.init.constant_(m.bias, 0) 60 | 61 | # X = torch.rand((1, 3, 224, 224)) 62 | # net = AlexNet(10) 63 | # y = net(X) 64 | # print(y.shape) 65 | -------------------------------------------------------------------------------- /CV_net/MobileNetv4/predict.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2024/3/24 10:25 4 | # @Author : 'IReverser' 5 | # @FileName: predict.py 6 | # Reference: 7 | import os 8 | import json 9 | import glob 10 | import torch 11 | from PIL import Image 12 | from torchvision import transforms 13 | from MobileNetv4 import create_mobilenetv4 14 | ''' 'MNV4ConvSmall', 'MNV4ConvMedium', 'MNV4ConvLarge', 'MNV4HybridMedium', 'MNV4HybridLarge' ''' 15 | 16 | model_name = 'MNV4HybridMedium' # chance from above structure 17 | dataset_name = 'flowers' 18 | MODEL_PATH = './checkpoints/model_MNV4HybridMedium_seed772_best.pt' 19 | CLASS_NUM = 5 20 | TEST_PATH = './results/' 21 | if os.path.exists(TEST_PATH): 22 | pass 23 | else: 24 | os.path.mkdir(TEST_PATH) 25 | 26 | device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') 27 | print('Use device: ', device) 28 | # pre-processing 29 | transform = transforms.Compose([transforms.Resize((224, 224)), 30 | transforms.ToTensor(), 31 | transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])]) 32 | 33 | # read class_dictionary 34 | json_path = './classes_indices.json' 35 | 36 | json_file = open(json_path, "r") 37 | classes_indict = json.load(json_file) 38 | print(classes_indict) 39 | # create model 40 | net = create_mobilenetv4(model_name,num_classes=CLASS_NUM) 41 | net = net.to(device) 42 | 43 | # load model weights 44 | assert os.path.exists(MODEL_PATH), "file: '{}' does not exist.".format(MODEL_PATH) 45 | net.load_state_dict(torch.load(MODEL_PATH)["state_dict"]) 46 | 47 | for im_path in glob.glob(TEST_PATH + '*.jpg'): 48 | # load data 49 | im = Image.open(im_path) 50 | im = transform(im) # [H, W, C] -> [C, H, W] 51 | im = torch.unsqueeze(im, dim=0) # [C, H, W] -> [N, C, H, W] 52 | im = im.to(device) 53 | 54 | net.eval() 55 | with torch.no_grad(): 56 | output = net(im) 57 | # confidence = torch.max(output, dim=1)[0].cpu().data.numpy()[0] # option 58 | confidence = torch.max(torch.softmax(output, dim=1)).cpu().data.numpy() 59 | predict = torch.max(output, dim=1)[1].cpu().data.numpy() 60 | 61 | print('Vertification picture:', im_path.split('/')[-1], '\t', 62 | 'Recognition result:', classes_indict[str(int(predict))], '\t', 63 | 'Recognition confidence:', str(confidence)) 64 | -------------------------------------------------------------------------------- /2020July_education_pytorch_teaching/data/two_layers_nn.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2020/2/25 4 | # @Author :'IReverser' 5 | # @FileName: two_layers_nn.py 6 | import torch 7 | from torch import nn 8 | 9 | # N is batch size; 10 | # D_in is input dimension; 11 | # H is hidden dimension; 12 | # D_out is output dimension. 13 | N, D_in, H, D_out = 64, 100, 100, 10 14 | learning_rate = 1e-3 15 | 16 | # Create random Tensors to hold inputs and outputs 17 | x = torch.randn(N, D_in) 18 | y = torch.randn(N, D_out) 19 | 20 | # Use the nn package to define our model as a sequence of layers. 21 | # nn.Sequential is a Module which contains other Modules, and applies them in sequence to 22 | # produce its output. Each Linear Module computes output from input using a linear function, 23 | # and holds internal Tensors for its weight and bias. 24 | model = torch.nn.Sequential( 25 | nn.Linear(D_in, H), 26 | nn.ReLU(), 27 | nn.Linear(H, D_out) 28 | ) 29 | 30 | # The nn package also contains definitions of popular loss functions; in this 31 | # case we will use Mean Squared Error (MSE) as our loss function. 32 | loss_fn = nn.MSELoss(reduction='sum') 33 | 34 | for i in range(500): 35 | # Forward pass: compute predicted y by passing x to the model. Module objects 36 | # override the __call__ operator so you can call them like functions. When 37 | # doing so you pass a Tensor of input data to the Module and it produces a Tensor of output data. 38 | y_pred = model(x) 39 | 40 | # Compute and print loss. We pass Tensors containing the predicted and true values of y, 41 | # and the loss function returns a Tensor containing the loss. 42 | loss = loss_fn(y_pred, y) 43 | print(i, loss.item()) 44 | 45 | # Zero the gradients before running the backward pass. 46 | model.zero_grad() 47 | 48 | # Backward pass: compute gradient of the loss with respect to all the learnable parameters of the model. 49 | # Internally, the parameters of each Module are stored in Tensors with requires_grad=True, so this call will 50 | # compute gradients for all learnable parameters in the model. 51 | loss.backward() 52 | 53 | # Update the weights using gradient descent. Each parameter is a Tensor, so 54 | # we can access its gradients like we did before. 55 | with torch.no_grad(): 56 | for param in model.parameters(): 57 | param -= learning_rate * param.grad -------------------------------------------------------------------------------- /CV_net/transNeXt/predict.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2024/3/24 10:25 4 | # @Author : 'IReverser' 5 | # @FileName: predict.py 6 | # Reference: 7 | import os 8 | import json 9 | import glob 10 | import torch 11 | from PIL import Image 12 | from torchvision import transforms 13 | import matplotlib.pyplot as plt 14 | from transNeXt import model_dict 15 | ''' 'transnext_micro', 'transnext_tiny', 'transnext_small', 'transnext_base' ''' 16 | 17 | model_name = 'transnext_micro' 18 | dataset_name = 'flowers' 19 | MODEL_PATH = './checkpoints/model_transnext_micro_seed608_best.pt' 20 | CLASS_NUM = 5 21 | TEST_PATH = './results/' 22 | assert os.path.exists(TEST_PATH), "file: '{}' does not exists.".format(TEST_PATH) 23 | device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') 24 | print('Use device: ', device) 25 | # pre-processing 26 | transform = transforms.Compose([transforms.Resize((224, 224)), 27 | transforms.ToTensor(), 28 | transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])]) 29 | 30 | # read class_dictionary 31 | json_path = './classes_indices.json' 32 | assert os.path.exists(json_path), "file: '{}' does not exists.".format(json_path) 33 | 34 | json_file = open(json_path, "r") 35 | classes_indict = json.load(json_file) 36 | print(classes_indict) 37 | # create model 38 | net = model_dict.get(model_name)(num_classes=CLASS_NUM) 39 | net = net.to(device) 40 | 41 | # load model weights 42 | assert os.path.exists(MODEL_PATH), "file: '{}' does not exist.".format(MODEL_PATH) 43 | net.load_state_dict(torch.load(MODEL_PATH)["state_dict"]) 44 | 45 | for im_path in glob.glob(TEST_PATH + '*.jpg'): 46 | # load data 47 | im = Image.open(im_path) 48 | im = transform(im) # [H, W, C] -> [C, H, W] 49 | im = torch.unsqueeze(im, dim=0) # [C, H, W] -> [N, C, H, W] 50 | im = im.to(device) 51 | 52 | net.eval() 53 | with torch.no_grad(): 54 | output = net(im) 55 | # confidence = torch.max(output, dim=1)[0].cpu().data.numpy()[0] # option 56 | confidence = torch.max(torch.softmax(output, dim=1)).cpu().data.numpy() 57 | predict = torch.max(output, dim=1)[1].cpu().data.numpy() 58 | 59 | print('Vertification picture:', im_path.split('/')[-1], '\t', 60 | 'Recognition result:', classes_indict[str(int(predict))], '\t', 61 | 'Recognition confidence:', str(confidence)) -------------------------------------------------------------------------------- /CV_net/swin_transformer/predict.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2021/11/26 17:12 4 | # @Author : '' 5 | # @FileName: predict.py 6 | import os 7 | import json 8 | import glob 9 | import torch 10 | from PIL import Image 11 | from torchvision import transforms 12 | import matplotlib.pyplot as plt 13 | from model import model_list 14 | # 'swin_tiny, swin_small, swin_base, ' 15 | # 'swin_base384, swin_base_22k, swin_base384_22k, swin_large_22k, swin_large384_22k' 16 | 17 | model_name = 'swin_tiny' 18 | dataset_name = 'flowers' 19 | MODEL_PATH = './model/' + model_name + '_' + dataset_name + '.pth' 20 | CLASS_NUM = 5 21 | TEST_PATH = './results/' 22 | assert os.path.exists(TEST_PATH), "file: '{}' does not exists.".format(TEST_PATH) 23 | device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') 24 | print('Use device: ', device) 25 | # pre-processing 26 | transform = transforms.Compose([transforms.Resize((224, 224)), 27 | transforms.ToTensor(), 28 | transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])]) 29 | 30 | # read class_dictionary 31 | json_path = './classes_indices.json' 32 | assert os.path.exists(json_path), "file: '{}' does not exists.".format(json_path) 33 | 34 | json_file = open(json_path, "r") 35 | classes_indict = json.load(json_file) 36 | print(classes_indict) 37 | # create model 38 | net = model_list.get(model_name)(num_classes=CLASS_NUM) 39 | net = net.to(device) 40 | 41 | # load model weights 42 | assert os.path.exists(MODEL_PATH), "file: '{}' does not exist.".format(MODEL_PATH) 43 | net.load_state_dict(torch.load(MODEL_PATH)) 44 | 45 | for im_path in glob.glob(TEST_PATH + '*.jpg'): 46 | # load data 47 | im = Image.open(im_path) 48 | im = transform(im) # [H, W, C] -> [C, H, W] 49 | im = torch.unsqueeze(im, dim=0) # [C, H, W] -> [N, C, H, W] 50 | im = im.to(device) 51 | 52 | net.eval() 53 | with torch.no_grad(): 54 | output = net(im) 55 | # confidence = torch.max(output, dim=1)[0].cpu().data.numpy()[0] # option 56 | confidence = torch.max(torch.softmax(output, dim=1)).cpu().data.numpy() 57 | predict = torch.max(output, dim=1)[1].cpu().data.numpy() 58 | 59 | print('Vertification picture:', im_path.split('/')[-1], '\t', 60 | 'Recognition result:', classes_indict[str(int(predict))], '\t', 61 | 'Recognition confidence:', str(confidence)) 62 | 63 | 64 | -------------------------------------------------------------------------------- /CV_net/vision_transformer/predict.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2021/11/26 17:12 4 | # @Author : '' 5 | # @FileName: predict.py 6 | import os 7 | import json 8 | import glob 9 | import torch 10 | from PIL import Image 11 | from torchvision import transforms 12 | import matplotlib.pyplot as plt 13 | from models import model_list 14 | # 'vitbase16, vitbase32, vitlarge16, ' \ 15 | # 'vitbase16_21k, vitbase32_21k, vitlarge16_21k, vitlarge32_21k, vithuge14_21k' 16 | 17 | model_name = 'vitbase16' 18 | dataset_name = 'flowers' 19 | MODEL_PATH = './model/' + model_name + '_' + dataset_name + '.pth' 20 | CLASS_NUM = 5 21 | TEST_PATH = './results/' 22 | assert os.path.exists(TEST_PATH), "file: '{}' does not exists.".format(TEST_PATH) 23 | device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') 24 | print('Use device: ', device) 25 | # pre-processing 26 | transform = transforms.Compose([transforms.Resize((224, 224)), 27 | transforms.ToTensor(), 28 | transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])]) 29 | 30 | # read class_dictionary 31 | json_path = './classes_indices.json' 32 | assert os.path.exists(json_path), "file: '{}' does not exists.".format(json_path) 33 | 34 | json_file = open(json_path, "r") 35 | classes_indict = json.load(json_file) 36 | print(classes_indict) 37 | # create model 38 | net = model_list.get(model_name)(num_classes=CLASS_NUM) 39 | net = net.to(device) 40 | 41 | # load model weights 42 | assert os.path.exists(MODEL_PATH), "file: '{}' does not exist.".format(MODEL_PATH) 43 | net.load_state_dict(torch.load(MODEL_PATH)) 44 | 45 | for im_path in glob.glob(TEST_PATH + '*.jpg'): 46 | # load data 47 | im = Image.open(im_path) 48 | im = transform(im) # [H, W, C] -> [C, H, W] 49 | im = torch.unsqueeze(im, dim=0) # [C, H, W] -> [N, C, H, W] 50 | im = im.to(device) 51 | 52 | net.eval() 53 | with torch.no_grad(): 54 | output = net(im) 55 | # confidence = torch.max(output, dim=1)[0].cpu().data.numpy()[0] # option 56 | confidence = torch.max(torch.softmax(output, dim=1)).cpu().data.numpy() 57 | predict = torch.max(output, dim=1)[1].cpu().data.numpy() 58 | 59 | print('Vertification picture:', im_path.split('/')[-1], '\t', 60 | 'Recognition result:', classes_indict[str(int(predict))], '\t', 61 | 'Recognition confidence:', str(confidence)) 62 | 63 | 64 | -------------------------------------------------------------------------------- /mofan_python/5_save_reload.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # Author = 'IReverser' 4 | 5 | import torch 6 | import matplotlib.pyplot as plt 7 | 8 | # torch.manual_seed(1) # reproducible 9 | 10 | # fake data 11 | x = torch.unsqueeze(torch.linspace(-1, 1, 100), dim=1) # x data (tensor), shape=(100, 1) 12 | y = x.pow(2) + 0.2 * torch.rand(x.size()) # noisy y data (tensor), shape=(100, 1) 13 | 14 | 15 | def save(): 16 | # save net1 17 | net1 = torch.nn.Sequential( 18 | torch.nn.Linear(1, 10), 19 | torch.nn.ReLU(), 20 | torch.nn.Linear(10, 1) 21 | ) 22 | optimizer = torch.optim.SGD(net1.parameters(), lr=0.5) 23 | loss_func = torch.nn.MSELoss() 24 | 25 | for t in range(200): 26 | print(t) 27 | prediction = net1(x) 28 | loss = loss_func(prediction, y) 29 | optimizer.zero_grad() 30 | loss.backward() 31 | optimizer.step() 32 | 33 | # plot result 34 | plt.figure(1, figsize=(10, 3)) 35 | plt.subplot(131) 36 | plt.title('Net1') 37 | plt.scatter(x.data.numpy(), y.data.numpy()) 38 | plt.plot(x.data.numpy(), prediction.data.numpy(), 'r-', lw=5) 39 | 40 | # 2 ways to save the net 41 | torch.save(net1, 'net.pkl') # save entire net 42 | torch.save(net1.state_dict(), 'net_params.pkl') # save only the parameters 43 | 44 | 45 | def restore_net(): 46 | # restore entire net1 to net2 47 | net2 = torch.load('net.pkl') 48 | prediction = net2(x) 49 | 50 | # plot result 51 | plt.subplot(132) 52 | plt.title('Net2') 53 | plt.scatter(x.data.numpy(), y.data.numpy()) 54 | plt.plot(x.data.numpy(), prediction.data.numpy(), 'r-', lw=5) 55 | 56 | 57 | def restore_params(): 58 | # restore only the parameters in net1 to net3 59 | net3 = torch.nn.Sequential( 60 | torch.nn.Linear(1, 10), 61 | torch.nn.ReLU(), 62 | torch.nn.Linear(10, 1) 63 | ) 64 | 65 | # copy net1's parameters into net3 66 | net3.load_state_dict(torch.load('net_params.pkl')) 67 | prediction = net3(x) 68 | 69 | # plot result 70 | plt.subplot(133) 71 | plt.title('Net3') 72 | plt.scatter(x.data.numpy(), y.data.numpy()) 73 | plt.plot(x.data.numpy(), prediction.data.numpy(), 'r-', lw=5) 74 | plt.show() 75 | 76 | 77 | # save net1 78 | save() 79 | # restore entire net (may slow) 80 | restore_net() 81 | # restore only the net parameters 82 | restore_params() 83 | -------------------------------------------------------------------------------- /CV_net/SPPNet/predict.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2021/11/26 17:12 4 | # @Author : '' 5 | # @FileName: predict.py 6 | import os 7 | import json 8 | import glob 9 | import torch 10 | from PIL import Image 11 | from torchvision import transforms 12 | import matplotlib.pyplot as plt 13 | from models import model_list 14 | '''vanillanet_5, vanillanet_6,vanillanet_7,vanillanet_9, 15 | vanillanet_11,vanillanet_12,vanillanet_13,vanillanet_13x1,vanillanet_13x1a 16 | ''' 17 | 18 | model_name = 'vanillanet_5' 19 | dataset_name = 'flowers' 20 | MODEL_PATH = './model/' + model_name + '_' + dataset_name + '.pth' 21 | CLASS_NUM = 5 22 | TEST_PATH = './results/' 23 | assert os.path.exists(TEST_PATH), "file: '{}' does not exists.".format(TEST_PATH) 24 | device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') 25 | print('Use device: ', device) 26 | # pre-processing 27 | transform = transforms.Compose([transforms.Resize((224, 224)), 28 | transforms.ToTensor(), 29 | transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])]) 30 | 31 | # read class_dictionary 32 | json_path = './classes_indices.json' 33 | assert os.path.exists(json_path), "file: '{}' does not exists.".format(json_path) 34 | 35 | json_file = open(json_path, "r") 36 | classes_indict = json.load(json_file) 37 | print(classes_indict) 38 | # create model 39 | net = model_list.get(model_name)(num_classes=CLASS_NUM) 40 | net = net.to(device) 41 | 42 | # load model weights 43 | assert os.path.exists(MODEL_PATH), "file: '{}' does not exist.".format(MODEL_PATH) 44 | net.load_state_dict(torch.load(MODEL_PATH)) 45 | 46 | for im_path in glob.glob(TEST_PATH + '*.jpg'): 47 | # load data 48 | im = Image.open(im_path) 49 | im = transform(im) # [H, W, C] -> [C, H, W] 50 | im = torch.unsqueeze(im, dim=0) # [C, H, W] -> [N, C, H, W] 51 | im = im.to(device) 52 | 53 | net.eval() 54 | with torch.no_grad(): 55 | output = net(im) 56 | # confidence = torch.max(output, dim=1)[0].cpu().data.numpy()[0] # option 57 | confidence = torch.max(torch.softmax(output, dim=1)).cpu().data.numpy() 58 | predict = torch.max(output, dim=1)[1].cpu().data.numpy() 59 | 60 | print('Vertification picture:', im_path.split('/')[-1], '\t', 61 | 'Recognition result:', classes_indict[str(int(predict))], '\t', 62 | 'Recognition confidence:', str(confidence)) 63 | 64 | 65 | -------------------------------------------------------------------------------- /CV_net/VanillaNet/predict.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2021/11/26 17:12 4 | # @Author : '' 5 | # @FileName: predict.py 6 | import os 7 | import json 8 | import glob 9 | import torch 10 | from PIL import Image 11 | from torchvision import transforms 12 | import matplotlib.pyplot as plt 13 | from vanillanet import model_list 14 | '''vanillanet_5, vanillanet_6,vanillanet_7,vanillanet_9, 15 | vanillanet_11,vanillanet_12,vanillanet_13,vanillanet_13x1,vanillanet_13x1a 16 | ''' 17 | 18 | model_name = 'vanillanet_5' 19 | dataset_name = 'flowers' 20 | MODEL_PATH = './model/' + model_name + '_' + dataset_name + '.pth' 21 | CLASS_NUM = 5 22 | TEST_PATH = './results/' 23 | assert os.path.exists(TEST_PATH), "file: '{}' does not exists.".format(TEST_PATH) 24 | device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') 25 | print('Use device: ', device) 26 | # pre-processing 27 | transform = transforms.Compose([transforms.Resize((224, 224)), 28 | transforms.ToTensor(), 29 | transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])]) 30 | 31 | # read class_dictionary 32 | json_path = './classes_indices.json' 33 | assert os.path.exists(json_path), "file: '{}' does not exists.".format(json_path) 34 | 35 | json_file = open(json_path, "r") 36 | classes_indict = json.load(json_file) 37 | print(classes_indict) 38 | # create model 39 | net = model_list.get(model_name)(num_classes=CLASS_NUM) 40 | net = net.to(device) 41 | 42 | # load model weights 43 | assert os.path.exists(MODEL_PATH), "file: '{}' does not exist.".format(MODEL_PATH) 44 | net.load_state_dict(torch.load(MODEL_PATH)) 45 | 46 | for im_path in glob.glob(TEST_PATH + '*.jpg'): 47 | # load data 48 | im = Image.open(im_path) 49 | im = transform(im) # [H, W, C] -> [C, H, W] 50 | im = torch.unsqueeze(im, dim=0) # [C, H, W] -> [N, C, H, W] 51 | im = im.to(device) 52 | 53 | net.eval() 54 | with torch.no_grad(): 55 | output = net(im) 56 | # confidence = torch.max(output, dim=1)[0].cpu().data.numpy()[0] # option 57 | confidence = torch.max(torch.softmax(output, dim=1)).cpu().data.numpy() 58 | predict = torch.max(output, dim=1)[1].cpu().data.numpy() 59 | 60 | print('Vertification picture:', im_path.split('/')[-1], '\t', 61 | 'Recognition result:', classes_indict[str(int(predict))], '\t', 62 | 'Recognition confidence:', str(confidence)) 63 | 64 | 65 | -------------------------------------------------------------------------------- /CV_net/split_dataset.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2021/8/9 23:16 4 | # @Author : '' 5 | # @FileName: split_dataset.py 6 | # download: https://www.kaggle.com/alxmamaev/flowers-recognition 7 | 8 | import os 9 | from shutil import copy, rmtree 10 | import random 11 | random.seed(0) 12 | split_ratio = 0.1 13 | DATASET = 'datasets' 14 | ORIGIN_PATH = 'flowers' 15 | 16 | 17 | def make_file(path): 18 | if os.path.exists(path): 19 | rmtree(path) # the directory of path is rebuild when it exist 20 | os.makedirs(path) 21 | 22 | 23 | def main(): 24 | cwd = os.getcwd() 25 | data_root = os.path.join(cwd, DATASET) 26 | origin_path = os.path.join(data_root, ORIGIN_PATH) 27 | assert os.path.exists(origin_path), 'file path: {} is not exist'.format(origin_path) 28 | classes = [cla for cla in os.listdir(origin_path) if os.path.join(origin_path, cla)] 29 | 30 | # build and save training samples and directory 31 | train_root = os.path.join(data_root, ORIGIN_PATH + '/train') 32 | make_file(train_root) 33 | for cla in classes: 34 | make_file(os.path.join(train_root, cla)) 35 | 36 | # build and save test samples and directory 37 | val_root = os.path.join(data_root, ORIGIN_PATH + '/val') 38 | make_file(val_root) 39 | for cla in classes: 40 | make_file(os.path.join(val_root, cla)) # build directory for each categories 41 | 42 | for cla in classes: 43 | cla_path = os.path.join(origin_path, cla) 44 | images = os.listdir(cla_path) 45 | num = len(images) 46 | # randomly sampling index of val dataset 47 | eval_index = random.sample(images, k=int(num*split_ratio)) 48 | for index, image in enumerate(images): 49 | if image in eval_index: 50 | # contribute eval samples 51 | image_path = os.path.join(cla_path, image) 52 | new_path = os.path.join(val_root, cla) 53 | copy(image_path, new_path) 54 | else: 55 | # contribute train samples 56 | image_path = os.path.join(cla_path, image) 57 | new_path = os.path.join(train_root, cla) 58 | copy(image_path, new_path) 59 | print('\r[{}] processing [{}/{}]'.format(cla, index+1, num), end="") # processing bar 60 | print("\nSplit dataset Finish!") 61 | 62 | 63 | if __name__ == '__main__': 64 | main() 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /CV_net/ConvMixer/predict.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2024/3/24 10:25 4 | # @Author : 'IReverser' 5 | # @FileName: predict.py 6 | # Reference: 7 | import os 8 | import json 9 | import glob 10 | import torch 11 | from PIL import Image 12 | from torchvision import transforms 13 | from convmixer import create_convmixer 14 | ''' 'ConvMixer-512_12, ConvMixer-512_16, ConvMixer-1024_12, ConvMixer-1024_16,' 15 | 'ConvMixer-768_32, ConvMixer-768_32_7, ConvMixer-1024_20, ' 16 | 'ConvMixer-1536_20_3, ConvMixer-1536_20_9, ConvMixer-1536_20_9_7' ''' 17 | 18 | model_name = 'ConvMixer-512_12' 19 | dataset_name = 'flowers' 20 | MODEL_PATH = './checkpoints/model_ConvMixer-512_12_seed131_best.pt' 21 | CLASS_NUM = 5 22 | TEST_PATH = './results/' 23 | assert os.path.exists(TEST_PATH), "file: '{}' does not exists.".format(TEST_PATH) 24 | device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') 25 | print('Use device: ', device) 26 | # pre-processing 27 | transform = transforms.Compose([transforms.Resize((224, 224)), 28 | transforms.ToTensor(), 29 | transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])]) 30 | 31 | # read class_dictionary 32 | json_path = './classes_indices.json' 33 | assert os.path.exists(json_path), "file: '{}' does not exists.".format(json_path) 34 | 35 | json_file = open(json_path, "r") 36 | classes_indict = json.load(json_file) 37 | print(classes_indict) 38 | # create model 39 | net = create_convmixer(model_name, num_classes=CLASS_NUM) 40 | net = net.to(device) 41 | 42 | # load model weights 43 | assert os.path.exists(MODEL_PATH), "file: '{}' does not exist.".format(MODEL_PATH) 44 | net.load_state_dict(torch.load(MODEL_PATH)["state_dict"]) 45 | 46 | for im_path in glob.glob(TEST_PATH + '*.jpg'): 47 | # load data 48 | im = Image.open(im_path) 49 | im = transform(im) # [H, W, C] -> [C, H, W] 50 | im = torch.unsqueeze(im, dim=0) # [C, H, W] -> [N, C, H, W] 51 | im = im.to(device) 52 | 53 | net.eval() 54 | with torch.no_grad(): 55 | output = net(im) 56 | # confidence = torch.max(output, dim=1)[0].cpu().data.numpy()[0] # option 57 | confidence = torch.max(torch.softmax(output, dim=1)).cpu().data.numpy() 58 | predict = torch.max(output, dim=1)[1].cpu().data.numpy() 59 | 60 | print('Vertification picture:', im_path.split('/')[-1], '\t', 61 | 'Recognition result:', classes_indict[str(int(predict))], '\t', 62 | 'Recognition confidence:', str(confidence)) -------------------------------------------------------------------------------- /mofan_python/8_optimizer.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.utils.data as Data 3 | import torch.nn.functional as F 4 | import matplotlib.pyplot as plt 5 | 6 | # hyper parameter 7 | LR = 0.01 8 | BATCH_SIZE = 32 9 | EPOCH = 12 10 | 11 | # fake dataset 12 | x = torch.unsqueeze(torch.linspace(-1, 1, 1000), dim=1) 13 | y = x.pow(2) + 0.1*torch.normal(torch.zeros(*x.size())) 14 | 15 | # plot dataset 16 | # plt.scatter(x.numpy(), y.numpy()) 17 | # plt.show() 18 | 19 | torch_dataset = Data.TensorDataset(x, y) 20 | loader = Data.DataLoader(dataset=torch_dataset, batch_size=BATCH_SIZE, shuffle=True, num_workers=2) 21 | 22 | 23 | # default network 24 | class Net(torch.nn.Module): 25 | def __init__(self): 26 | super(Net, self).__init__() 27 | self.hidden = torch.nn.Linear(1, 20) # hidden layer 28 | self.predict = torch.nn.Linear(20, 1) # output layer 29 | 30 | def forward(self, x): 31 | x = F.relu(self.hidden(x)) # activation function for hidden layer 32 | x = self.predict(x) # linear output 33 | return x 34 | 35 | 36 | if __name__ == '__main__': 37 | # different nets 38 | net_SGD = Net() 39 | net_Momentum = Net() 40 | net_RMSprop = Net() 41 | net_Adam = Net() 42 | nets = [net_SGD, net_Momentum, net_RMSprop, net_Adam] 43 | 44 | # different optimizers 45 | opt_SGD = torch.optim.SGD(net_SGD.parameters(), lr=LR) 46 | opt_Momentum = torch.optim.SGD(net_Momentum.parameters(), lr=LR, momentum=0.8) 47 | opt_RMSprop = torch.optim.RMSprop(net_RMSprop.parameters(), lr=LR, alpha=0.9) 48 | opt_Adam = torch.optim.Adam(net_Adam.parameters(), lr=LR, betas=(0.9, 0.99)) 49 | optimizers = [opt_SGD, opt_Momentum, opt_RMSprop, opt_Adam] 50 | 51 | loss_func = torch.nn.MSELoss() 52 | losses_his = [[], [], [], []] 53 | 54 | for epoch in range(EPOCH): 55 | print(epoch) 56 | for step, (b_x, b_y) in enumerate(loader): 57 | 58 | for net, opt, l_his in zip(nets, optimizers, losses_his): 59 | output = net(b_x) # get output for every net 60 | loss = loss_func(output, b_y) # compute loss for every net 61 | opt.zero_grad() # clear gradients for next train 62 | loss.backward() # backpropagation, compute gradients 63 | opt.step() # apply gradients 64 | l_his.append(loss.data.numpy()) # loss recoder 65 | 66 | labels = ['SGD', 'Momentum', 'RMSprop', 'Adam'] 67 | for i, l_his in enumerate(losses_his): 68 | plt.plot(l_his, label=labels[i]) 69 | plt.legend(loc='best') 70 | plt.xlabel('Steps') 71 | plt.ylabel('Loss') 72 | plt.ylim((0, 0.2)) 73 | plt.show() -------------------------------------------------------------------------------- /CV_net/pytorch-center-offical/split_dataset.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2021/8/9 23:16 4 | # @Author : '' 5 | # @FileName: split_dataset.py 6 | # download: https://www.kaggle.com/alxmamaev/flowers-recognition 7 | 8 | import os 9 | from shutil import copy, rmtree 10 | import random 11 | random.seed(0) 12 | split_ratio = 0.2 13 | DATASET = 'datasets' 14 | ORIGIN_PATH = 'flowers1' 15 | # ORIGIN_PATH = 'LFW' 16 | 17 | 18 | def make_file(path): 19 | if os.path.exists(path): 20 | rmtree(path) # the directory of path is rebuild when it exist 21 | os.makedirs(path) 22 | 23 | 24 | def main(): 25 | cwd = os.getcwd() 26 | data_root = os.path.join(cwd, DATASET) 27 | print(data_root) 28 | origin_path = os.path.join(data_root, ORIGIN_PATH) 29 | assert os.path.exists(origin_path), 'file path: {} is not exist'.format(origin_path) 30 | classes = [cla for cla in os.listdir(origin_path) if os.path.join(origin_path, cla)] 31 | 32 | # build and save training samples and directory 33 | train_root = os.path.join(data_root, os.path.join(ORIGIN_PATH, 'train')) 34 | make_file(train_root) 35 | for cla in classes: 36 | make_file(os.path.join(train_root, cla)) 37 | 38 | # build and save test samples and directory 39 | val_root = os.path.join('../', data_root, os.path.join(ORIGIN_PATH, 'val')) 40 | make_file(val_root) 41 | for cla in classes: 42 | make_file(os.path.join(val_root, cla)) # build directory for each categories 43 | 44 | for cla in classes: 45 | cla_path = os.path.join(origin_path, cla) 46 | images = os.listdir(cla_path) 47 | num = len(images) 48 | # randomly sampling index of val dataset 49 | eval_index = random.sample(images, k=int(num*split_ratio)) 50 | for index, image in enumerate(images): 51 | if image in eval_index: 52 | # contribute eval samples 53 | image_path = os.path.join(cla_path, image) 54 | new_path = os.path.join(val_root, cla) 55 | copy(image_path, new_path) 56 | with open(os.path.join(origin_path, 'val.txt'), 'a') as f: 57 | f.write(os.path.join(new_path, image) + '\n') 58 | else: 59 | # contribute train samples 60 | image_path = os.path.join(cla_path, image) 61 | new_path = os.path.join(train_root, cla) 62 | copy(image_path, new_path) 63 | with open(os.path.join(origin_path, 'train.txt'), 'a') as f: 64 | f.write(os.path.join(new_path, image) + '\n') 65 | print('\r[{}] processing [{}/{}]'.format(cla, index+1, num), end="") # processing bar 66 | print("\nSplit dataset Finish!") 67 | 68 | 69 | if __name__ == '__main__': 70 | main() 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | -------------------------------------------------------------------------------- /mofan_python/GPU,py.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # Author = '' 4 | 5 | import torch 6 | import torch.nn as nn 7 | import torch.utils.data as Data 8 | import torchvision 9 | 10 | print(torch.cuda.is_available()) 11 | # torch.manual_seed(1) 12 | 13 | EPOCH = 1 14 | BATCH_SIZE = 1 15 | LR = 0.001 16 | DOWNLOAD_MNIST = False 17 | 18 | train_data = torchvision.datasets.MNIST(root='./mnist/', train=True, transform=torchvision.transforms.ToTensor(), download=DOWNLOAD_MNIST,) 19 | train_loader = Data.DataLoader(dataset=train_data, batch_size=BATCH_SIZE, shuffle=True) 20 | 21 | test_data = torchvision.datasets.MNIST(root='./mnist/', train=False) 22 | 23 | # !!!!!!!! Change in here !!!!!!!!! # 24 | test_x = torch.unsqueeze(test_data.test_data, dim=1).type(torch.FloatTensor)[:2000].cuda()/255. # Tensor on GPU ** 25 | test_y = test_data.test_labels[:2000].cuda() # ** 26 | 27 | 28 | class CNN(nn.Module): 29 | def __init__(self): 30 | super(CNN, self).__init__() 31 | self.conv1 = nn.Sequential(nn.Conv2d(in_channels=1, out_channels=16, kernel_size=5, stride=1, padding=2,), 32 | nn.ReLU(), nn.MaxPool2d(kernel_size=2),) 33 | self.conv2 = nn.Sequential(nn.Conv2d(16, 32, 5, 1, 2), nn.ReLU(), nn.MaxPool2d(2),) 34 | self.out = nn.Linear(32 * 7 * 7, 10) 35 | 36 | def forward(self, x): 37 | x = self.conv1(x) 38 | x = self.conv2(x) 39 | x = x.view(x.size(0), -1) 40 | output = self.out(x) 41 | return output 42 | 43 | 44 | cnn = CNN() 45 | # !!!!!!!! Change in here !!!!!!!!! # 46 | cnn.cuda() # Moves all model parameters and buffers to the GPU.** 47 | 48 | optimizer = torch.optim.Adam(cnn.parameters(), lr=LR) 49 | loss_func = nn.CrossEntropyLoss() 50 | 51 | for epoch in range(EPOCH): 52 | for step, (x, y) in enumerate(train_loader): 53 | 54 | # !!!!!!!! Change in here !!!!!!!!! # 55 | b_x = x.cuda() # Tensor on GPU ** 56 | b_y = y.cuda() # Tensor on GPU ** 57 | 58 | output = cnn(b_x) 59 | loss = loss_func(output, b_y) 60 | optimizer.zero_grad() 61 | loss.backward() 62 | optimizer.step() 63 | 64 | if step % 50 == 0: 65 | test_output = cnn(test_x) 66 | 67 | # !!!!!!!! Change in here !!!!!!!!! # 68 | pred_y = torch.max(test_output, 1)[1].cuda().data # move the computation in GPU ** 69 | 70 | accuracy = torch.sum(pred_y == test_y).type(torch.FloatTensor) / test_y.size(0) 71 | print('Epoch: ', epoch, '| train loss: %.4f' % loss.data.cpu().numpy(), '| test accuracy: %.2f' % accuracy) 72 | 73 | 74 | test_output = cnn(test_x[:10]) 75 | 76 | # !!!!!!!! Change in here !!!!!!!!! # 77 | pred_y = torch.max(test_output, 1)[1].cuda().data # move the computation in GPU 78 | 79 | # pred_y = pred_y.cpu() 80 | print(pred_y, 'prediction number') 81 | print(test_y[:10], 'real number') -------------------------------------------------------------------------------- /CV_net/LeNet_tf/model.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2021/7/30 13:55 4 | # @Author : '' 5 | # @FileName: model.py 6 | import tensorflow as tf 7 | import os 8 | import matplotlib.pyplot as plt 9 | import numpy as np 10 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' 11 | 12 | 13 | class LeNet(tf.keras.Model): 14 | def __init__(self): 15 | super().__init__() 16 | self.conv1 = tf.keras.layers.Conv2D(filters=6, kernel_size=[3, 3], strides=1, padding='same') 17 | self.pool1 = tf.keras.layers.MaxPool2D(pool_size=[2, 2], strides=2) 18 | self.conv2 = tf.keras.layers.Conv2D(filters=16, kernel_size=[3, 3], strides=1, padding='valid') 19 | self.pool2 = tf.keras.layers.MaxPool2D(pool_size=[2, 2], strides=2) 20 | self.flatten = tf.keras.layers.Flatten() 21 | self.dense1 = tf.keras.layers.Dense(units=120, activation=tf.nn.relu) 22 | self.dense2 = tf.keras.layers.Dense(units=84, activation=tf.nn.relu) 23 | self.dense3 = tf.keras.layers.Dense(units=10, activation=tf.nn.softmax) 24 | self.bn1 = tf.keras.layers.BatchNormalization() 25 | self.bn2 = tf.keras.layers.BatchNormalization() 26 | 27 | def call(self, input): 28 | x = self.conv1(input) 29 | x = self.bn1(x) 30 | x = tf.nn.relu(self.pool1(x)) 31 | x = self.conv2(x) 32 | x = self.bn2(x) 33 | x = tf.nn.relu(self.pool2(x)) 34 | x = self.flatten(x) 35 | x = self.dense1(x) 36 | x = self.dense2(x) 37 | x = self.dense3(x) 38 | return x 39 | 40 | 41 | EPOCH = 10 42 | BATCH_SIZE = 64 43 | (train_data, train_label), (val_data, val_label) = tf.keras.datasets.mnist.load_data() 44 | train_data = np.expand_dims(train_data.astype(np.float32) / 255, axis=-1) 45 | train_label = train_label.astype(np.int32) 46 | val_data = np.expand_dims(val_data.astype(np.float32) / 255, axis=-1) 47 | val_label = val_label.astype(np.int32) 48 | 49 | model = LeNet() 50 | model.compile( 51 | optimizer=tf.keras.optimizers.Adam(learning_rate=1e-3), 52 | loss=tf.keras.losses.sparse_categorical_crossentropy, 53 | metrics=[tf.keras.metrics.sparse_categorical_accuracy] 54 | ) 55 | reduce_lr = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_sparse_categorical_accuracy', factor=0.2, patience=3) 56 | early_stopping = tf.keras.callbacks.EarlyStopping(monitor='val_sparse_categorical_accuracy', patience=6) 57 | history = model.fit(train_data, train_label, epochs=EPOCH, batch_size=BATCH_SIZE, verbose=2, validation_split=0.1, 58 | callbacks=[reduce_lr, early_stopping]) 59 | model.save("LeNet5_MNIST") 60 | model.evaluate(val_data, val_label, verbose=2) 61 | # visualization 62 | plt.plot(history.epoch, history.history["sparse_categorical_accuracy"], 'r--', label='sparse_categorical_accuracy') 63 | plt.plot(history.epoch, history.history["val_sparse_categorical_accuracy"], label='val_sparse_categorical_accuracy') 64 | plt.legend() 65 | plt.show() 66 | 67 | -------------------------------------------------------------------------------- /CV_net/ConvMixer/utils/misc.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2022/2/10 12:43 4 | # @Author : '' 5 | # @FileName: misc.py 6 | import os 7 | import torch 8 | 9 | 10 | def make_dir(folder): 11 | if not os.path.isdir(folder): 12 | os.makedirs(folder) 13 | 14 | 15 | class AverageMeter(object): 16 | """Computes and stores the average and current value""" 17 | def __init__(self): 18 | self.reset() 19 | 20 | def reset(self): 21 | self.val = 0 22 | self.avg = 0 23 | self.sum = 0 24 | self.count = 0 25 | 26 | def update(self, val, n=1): 27 | self.val = val 28 | self.sum += val * n 29 | self.count += n 30 | self.avg = self.sum / self.count 31 | 32 | 33 | class Logger(): 34 | def __init__(self, args, filename='log.txt'): 35 | 36 | self.filename = filename 37 | self.file = open(filename, 'a') 38 | # Write model configuration at top of file 39 | for arg in vars(args): 40 | self.file.write(arg+': '+str(getattr(args, arg))+'\n') 41 | self.file.flush() 42 | 43 | def writerow(self, row): 44 | for k in row: 45 | self.file.write(k+': '+row[k]+' ') 46 | self.file.write('\n') 47 | self.file.flush() 48 | 49 | def close(self): 50 | self.file.close() 51 | 52 | 53 | def format_time(seconds): 54 | days = int(seconds / 3600/24) 55 | seconds = seconds - days*3600*24 56 | hours = int(seconds / 3600) 57 | seconds = seconds - hours*3600 58 | minutes = int(seconds / 60) 59 | seconds = seconds - minutes*60 60 | secondsf = int(seconds) 61 | seconds = seconds - secondsf 62 | millis = int(seconds*1000) 63 | 64 | f = '' 65 | i = 1 66 | if days > 0: 67 | f += str(days) + 'D' 68 | i += 1 69 | if hours > 0 and i <= 2: 70 | f += str(hours) + 'h' 71 | i += 1 72 | if minutes > 0 and i <= 2: 73 | f += str(minutes) + 'm' 74 | i += 1 75 | if secondsf > 0 and i <= 2: 76 | f += str(secondsf) + 's' 77 | i += 1 78 | if millis > 0 and i <= 2: 79 | f += str(millis) + 'ms' 80 | i += 1 81 | if f == '': 82 | f = '0ms' 83 | return f 84 | 85 | 86 | def accuracy(output, target, topk=(1,)): 87 | """Compute the accuracy over the k top predictions for the specified values of k""" 88 | with torch.no_grad(): 89 | maxk = max(topk) 90 | batch_size = target.size(0) 91 | # print(output) 92 | 93 | _, pred = output.topk(maxk, dim=1, largest=True, sorted=True) 94 | pred = pred.t() 95 | # print(pred) 96 | correct = pred.eq(target.view(1, -1).expand_as(pred)) 97 | 98 | res = [] 99 | for k in topk: 100 | correct_k = correct[:k].reshape(-1).float().sum(0, keepdim=True) 101 | res.append(correct_k.mul_(100./batch_size)) 102 | return res -------------------------------------------------------------------------------- /CV_net/transNeXt/utils/misc.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2022/2/10 12:43 4 | # @Author : '' 5 | # @FileName: misc.py 6 | import os 7 | import torch 8 | 9 | 10 | def make_dir(folder): 11 | if not os.path.isdir(folder): 12 | os.makedirs(folder) 13 | 14 | 15 | class AverageMeter(object): 16 | """Computes and stores the average and current value""" 17 | def __init__(self): 18 | self.reset() 19 | 20 | def reset(self): 21 | self.val = 0 22 | self.avg = 0 23 | self.sum = 0 24 | self.count = 0 25 | 26 | def update(self, val, n=1): 27 | self.val = val 28 | self.sum += val * n 29 | self.count += n 30 | self.avg = self.sum / self.count 31 | 32 | 33 | class Logger(): 34 | def __init__(self, args, filename='log.txt'): 35 | 36 | self.filename = filename 37 | self.file = open(filename, 'a') 38 | # Write model configuration at top of file 39 | for arg in vars(args): 40 | self.file.write(arg+': '+str(getattr(args, arg))+'\n') 41 | self.file.flush() 42 | 43 | def writerow(self, row): 44 | for k in row: 45 | self.file.write(k+': '+row[k]+' ') 46 | self.file.write('\n') 47 | self.file.flush() 48 | 49 | def close(self): 50 | self.file.close() 51 | 52 | 53 | def format_time(seconds): 54 | days = int(seconds / 3600/24) 55 | seconds = seconds - days*3600*24 56 | hours = int(seconds / 3600) 57 | seconds = seconds - hours*3600 58 | minutes = int(seconds / 60) 59 | seconds = seconds - minutes*60 60 | secondsf = int(seconds) 61 | seconds = seconds - secondsf 62 | millis = int(seconds*1000) 63 | 64 | f = '' 65 | i = 1 66 | if days > 0: 67 | f += str(days) + 'D' 68 | i += 1 69 | if hours > 0 and i <= 2: 70 | f += str(hours) + 'h' 71 | i += 1 72 | if minutes > 0 and i <= 2: 73 | f += str(minutes) + 'm' 74 | i += 1 75 | if secondsf > 0 and i <= 2: 76 | f += str(secondsf) + 's' 77 | i += 1 78 | if millis > 0 and i <= 2: 79 | f += str(millis) + 'ms' 80 | i += 1 81 | if f == '': 82 | f = '0ms' 83 | return f 84 | 85 | 86 | def accuracy(output, target, topk=(1,)): 87 | """Compute the accuracy over the k top predictions for the specified values of k""" 88 | with torch.no_grad(): 89 | maxk = max(topk) 90 | batch_size = target.size(0) 91 | # print(output) 92 | 93 | _, pred = output.topk(maxk, dim=1, largest=True, sorted=True) 94 | pred = pred.t() 95 | # print(pred) 96 | correct = pred.eq(target.view(1, -1).expand_as(pred)) 97 | 98 | res = [] 99 | for k in topk: 100 | correct_k = correct[:k].reshape(-1).float().sum(0, keepdim=True) 101 | res.append(correct_k.mul_(100./batch_size)) 102 | return res -------------------------------------------------------------------------------- /mofan_python/4_classification.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # Author = 'IReverser' 4 | """ 5 | 这是一个利用了分类处理的神经网络教程 6 | """ 7 | 8 | import torch 9 | import torch.nn.functional as F 10 | from torch.autograd import Variable 11 | import matplotlib.pyplot as plt 12 | import time 13 | 14 | n_data = torch.ones([100, 2]) 15 | x0 = torch.normal(2*n_data, 1) # class0 x data (tensor), shape=(100, 2) 16 | y0 = torch.zeros(100) # class0 y data (tensor), shape=(100, 1) 17 | x1 = torch.normal(-2*n_data, 1) # class1 x data (tensor), shape=(100, 2) 18 | y1 = torch.ones(100) # class1 y data (tensor), shape=(100, 1) 19 | x = torch.cat((x0, x1), 0).type(torch.FloatTensor) # FloatTensor = 32-bit floating 20 | y = torch.cat((y0, y1), ).type(torch.LongTensor) # LongTensor = 64-bit integer 21 | 22 | 23 | # The code below is deprecated in Pytorch 0.4. Now, autograd directly supports tensors 24 | # x, y = Variable(x), Variable(y) 25 | # 26 | # plt.scatter(x.data.numpy()[:, 0], x.data.numpy()[:, 1], c=y.data.numpy(), s=100, lw=0, cmap='RdYlGn') 27 | # plt.show() 28 | 29 | 30 | class Net(torch.nn.Module): 31 | def __init__(self, n_feature, n_hidden, n_output): 32 | super(Net, self).__init__() 33 | self.hidden = torch.nn.Linear(n_feature, n_hidden) 34 | self.predict = torch.nn.Linear(n_hidden, n_output) 35 | 36 | def forward(self, x): 37 | x = F.relu(self.hidden(x)) 38 | x = self.predict(x) 39 | return x 40 | 41 | 42 | net = Net(2, 10, 2) 43 | print(net) 44 | 45 | optimizer = torch.optim.SGD(net.parameters(), lr=0.02) 46 | loss_func = torch.nn.CrossEntropyLoss() 47 | 48 | start_time = time.time() 49 | plt.ion() 50 | 51 | for t in range(150): 52 | print(t) 53 | out = net(x) # [-2, -.12, 20] F,softmax(out) [.1, 0.2, .7] 54 | 55 | loss = loss_func(out, y) # must be (1. nn output, 2. target), the target label is NOT one-hotted 56 | 57 | optimizer.zero_grad() # set gradient of parameters to zero 58 | loss.backward() # calculate gradient of per node 59 | optimizer.step() # optimize gradient 60 | 61 | if t % 2 == 0: 62 | # plot and show learning process 63 | plt.cla() 64 | prediction = torch.max(out, 1)[1] 65 | pred_y = prediction.data.numpy() 66 | target_y = y.data.numpy() 67 | plt.scatter(x.data.numpy()[:, 0], x.data.numpy()[:, 1], c=pred_y, s=100, lw=0, cmap='RdYlGn') 68 | accuracy = float((pred_y == target_y).astype(int).sum()) / float(target_y.size) 69 | plt.text(1.5, -4, 'Accuracy=%.2f' % accuracy, fontdict={'size': 20, 'color': 'red'}) 70 | plt.pause(0.1) 71 | 72 | end_time = time.time() 73 | print('The total cost time:', str(end_time - start_time) + 'sec') 74 | plt.ioff() 75 | plt.show() 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | -------------------------------------------------------------------------------- /CV_net/MobileNetv4/utils/misc.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2022/2/10 12:43 4 | # @Author : '' 5 | # @FileName: misc.py 6 | import os 7 | import torch 8 | 9 | 10 | def make_dir(folder): 11 | if not os.path.isdir(folder): 12 | os.makedirs(folder) 13 | 14 | 15 | class AverageMeter(object): 16 | """Computes and stores the average and current value""" 17 | def __init__(self): 18 | self.reset() 19 | 20 | def reset(self): 21 | self.val = 0 22 | self.avg = 0 23 | self.sum = 0 24 | self.count = 0 25 | 26 | def update(self, val, n=1): 27 | self.val = val 28 | self.sum += val * n 29 | self.count += n 30 | self.avg = self.sum / self.count 31 | 32 | 33 | class Logger(): 34 | def __init__(self, args, filename='log.txt'): 35 | 36 | self.filename = filename 37 | self.file = open(filename, 'a') 38 | # Write model configuration at top of file 39 | for arg in vars(args): 40 | self.file.write(arg+': '+str(getattr(args, arg))+'\n') 41 | self.file.flush() 42 | 43 | def writerow(self, row): 44 | for k in row: 45 | self.file.write(k+': '+row[k]+' ') 46 | self.file.write('\n') 47 | self.file.flush() 48 | 49 | def close(self): 50 | self.file.close() 51 | 52 | 53 | def format_time(seconds): 54 | days = int(seconds / 3600/24) 55 | seconds = seconds - days*3600*24 56 | hours = int(seconds / 3600) 57 | seconds = seconds - hours*3600 58 | minutes = int(seconds / 60) 59 | seconds = seconds - minutes*60 60 | secondsf = int(seconds) 61 | seconds = seconds - secondsf 62 | millis = int(seconds*1000) 63 | 64 | f = '' 65 | i = 1 66 | if days > 0: 67 | f += str(days) + 'D' 68 | i += 1 69 | if hours > 0 and i <= 2: 70 | f += str(hours) + 'h' 71 | i += 1 72 | if minutes > 0 and i <= 2: 73 | f += str(minutes) + 'm' 74 | i += 1 75 | if secondsf > 0 and i <= 2: 76 | f += str(secondsf) + 's' 77 | i += 1 78 | if millis > 0 and i <= 2: 79 | f += str(millis) + 'ms' 80 | i += 1 81 | if f == '': 82 | f = '0ms' 83 | return f 84 | 85 | 86 | def accuracy(output, target, topk=(1,)): 87 | """Compute the accuracy over the k top predictions for the specified values of k""" 88 | with torch.no_grad(): 89 | maxk = max(topk) 90 | batch_size = target.size(0) 91 | # print(output) 92 | 93 | _, pred = output.topk(maxk, dim=1, largest=True, sorted=True) 94 | pred = pred.t() 95 | # print(pred) 96 | correct = pred.eq(target.view(1, -1).expand_as(pred)) 97 | 98 | res = [] 99 | for k in topk: 100 | correct_k = correct[:k].reshape(-1).float().sum(0, keepdim=True) 101 | res.append(correct_k.mul_(100./batch_size)) 102 | return res -------------------------------------------------------------------------------- /CV_net/DeiT/utils.py: -------------------------------------------------------------------------------- 1 | # !/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time: 2023/2/12 10:12 4 | # @Author: 'IReverser' 5 | # @ FileName: utils.py 6 | import os 7 | import torch 8 | 9 | 10 | def make_dir(folder): 11 | if not os.path.isdir(folder): 12 | os.makedirs(folder) 13 | 14 | 15 | class AverageMeter(object): 16 | """Computes and stores the average and current value""" 17 | def __init__(self): 18 | self.reset() 19 | 20 | def reset(self): 21 | self.val = 0 22 | self.avg = 0 23 | self.sum = 0 24 | self.count = 0 25 | 26 | def update(self, val, n=1): 27 | self.val = val 28 | self.sum += val * n 29 | self.count += n 30 | self.avg = self.sum / self.count 31 | 32 | 33 | class Logger(): 34 | def __init__(self, args, filename='log.txt'): 35 | 36 | self.filename = filename 37 | self.file = open(filename, 'a') 38 | # Write model configuration at top of file 39 | for arg in vars(args): 40 | self.file.write(arg+': '+str(getattr(args, arg))+'\n') 41 | self.file.flush() 42 | 43 | def writerow(self, row): 44 | for k in row: 45 | self.file.write(k+': '+row[k]+' ') 46 | self.file.write('\n') 47 | self.file.flush() 48 | 49 | def close(self): 50 | self.file.close() 51 | 52 | 53 | def format_time(seconds): 54 | days = int(seconds / 3600/24) 55 | seconds = seconds - days*3600*24 56 | hours = int(seconds / 3600) 57 | seconds = seconds - hours*3600 58 | minutes = int(seconds / 60) 59 | seconds = seconds - minutes*60 60 | secondsf = int(seconds) 61 | seconds = seconds - secondsf 62 | millis = int(seconds*1000) 63 | 64 | f = '' 65 | i = 1 66 | if days > 0: 67 | f += str(days) + 'D' 68 | i += 1 69 | if hours > 0 and i <= 2: 70 | f += str(hours) + 'h' 71 | i += 1 72 | if minutes > 0 and i <= 2: 73 | f += str(minutes) + 'm' 74 | i += 1 75 | if secondsf > 0 and i <= 2: 76 | f += str(secondsf) + 's' 77 | i += 1 78 | if millis > 0 and i <= 2: 79 | f += str(millis) + 'ms' 80 | i += 1 81 | if f == '': 82 | f = '0ms' 83 | return f 84 | 85 | 86 | def accuracy(output, target, topk=(1,)): 87 | """Compute the accuracy over the k top predictions for the specified values of k""" 88 | with torch.no_grad(): 89 | maxk = max(topk) 90 | batch_size = target.size(0) 91 | # print(output) 92 | 93 | _, pred = output.topk(maxk, dim=1, largest=True, sorted=True) 94 | pred = pred.t() 95 | # print(pred) 96 | correct = pred.eq(target.view(1, -1).expand_as(pred)) 97 | 98 | res = [] 99 | for k in topk: 100 | correct_k = correct[:k].reshape(-1).float().sum(0, keepdim=True) 101 | res.append(correct_k.mul_(100./batch_size)) 102 | return res 103 | 104 | # class confision_matrix(object): 105 | -------------------------------------------------------------------------------- /CV_net/GhostNet/utils.py: -------------------------------------------------------------------------------- 1 | # !/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time: 2023/2/12 10:12 4 | # @Author: 'IReverser' 5 | # @ FileName: utils.py 6 | import os 7 | import torch 8 | 9 | 10 | def make_dir(folder): 11 | if not os.path.isdir(folder): 12 | os.makedirs(folder) 13 | 14 | 15 | class AverageMeter(object): 16 | """Computes and stores the average and current value""" 17 | def __init__(self): 18 | self.reset() 19 | 20 | def reset(self): 21 | self.val = 0 22 | self.avg = 0 23 | self.sum = 0 24 | self.count = 0 25 | 26 | def update(self, val, n=1): 27 | self.val = val 28 | self.sum += val * n 29 | self.count += n 30 | self.avg = self.sum / self.count 31 | 32 | 33 | class Logger(): 34 | def __init__(self, args, filename='log.txt'): 35 | 36 | self.filename = filename 37 | self.file = open(filename, 'a') 38 | # Write model configuration at top of file 39 | for arg in vars(args): 40 | self.file.write(arg+': '+str(getattr(args, arg))+'\n') 41 | self.file.flush() 42 | 43 | def writerow(self, row): 44 | for k in row: 45 | self.file.write(k+': '+row[k]+' ') 46 | self.file.write('\n') 47 | self.file.flush() 48 | 49 | def close(self): 50 | self.file.close() 51 | 52 | 53 | def format_time(seconds): 54 | days = int(seconds / 3600/24) 55 | seconds = seconds - days*3600*24 56 | hours = int(seconds / 3600) 57 | seconds = seconds - hours*3600 58 | minutes = int(seconds / 60) 59 | seconds = seconds - minutes*60 60 | secondsf = int(seconds) 61 | seconds = seconds - secondsf 62 | millis = int(seconds*1000) 63 | 64 | f = '' 65 | i = 1 66 | if days > 0: 67 | f += str(days) + 'D' 68 | i += 1 69 | if hours > 0 and i <= 2: 70 | f += str(hours) + 'h' 71 | i += 1 72 | if minutes > 0 and i <= 2: 73 | f += str(minutes) + 'm' 74 | i += 1 75 | if secondsf > 0 and i <= 2: 76 | f += str(secondsf) + 's' 77 | i += 1 78 | if millis > 0 and i <= 2: 79 | f += str(millis) + 'ms' 80 | i += 1 81 | if f == '': 82 | f = '0ms' 83 | return f 84 | 85 | 86 | def accuracy(output, target, topk=(1,)): 87 | """Compute the accuracy over the k top predictions for the specified values of k""" 88 | with torch.no_grad(): 89 | maxk = max(topk) 90 | batch_size = target.size(0) 91 | # print(output) 92 | 93 | _, pred = output.topk(maxk, dim=1, largest=True, sorted=True) 94 | pred = pred.t() 95 | # print(pred) 96 | correct = pred.eq(target.view(1, -1).expand_as(pred)) 97 | 98 | res = [] 99 | for k in topk: 100 | correct_k = correct[:k].reshape(-1).float().sum(0, keepdim=True) 101 | res.append(correct_k.mul_(100./batch_size)) 102 | return res 103 | 104 | # class confision_matrix(object): 105 | -------------------------------------------------------------------------------- /CV_net/RegNet/utils.py: -------------------------------------------------------------------------------- 1 | # !/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time: 2023/2/12 10:12 4 | # @Author: 'IReverser' 5 | # @ FileName: utils.py 6 | import os 7 | import torch 8 | 9 | 10 | def make_dir(folder): 11 | if not os.path.isdir(folder): 12 | os.makedirs(folder) 13 | 14 | 15 | class AverageMeter(object): 16 | """Computes and stores the average and current value""" 17 | def __init__(self): 18 | self.reset() 19 | 20 | def reset(self): 21 | self.val = 0 22 | self.avg = 0 23 | self.sum = 0 24 | self.count = 0 25 | 26 | def update(self, val, n=1): 27 | self.val = val 28 | self.sum += val * n 29 | self.count += n 30 | self.avg = self.sum / self.count 31 | 32 | 33 | class Logger(): 34 | def __init__(self, args, filename='log.txt'): 35 | 36 | self.filename = filename 37 | self.file = open(filename, 'a') 38 | # Write model configuration at top of file 39 | for arg in vars(args): 40 | self.file.write(arg+': '+str(getattr(args, arg))+'\n') 41 | self.file.flush() 42 | 43 | def writerow(self, row): 44 | for k in row: 45 | self.file.write(k+': '+row[k]+' ') 46 | self.file.write('\n') 47 | self.file.flush() 48 | 49 | def close(self): 50 | self.file.close() 51 | 52 | 53 | def format_time(seconds): 54 | days = int(seconds / 3600/24) 55 | seconds = seconds - days*3600*24 56 | hours = int(seconds / 3600) 57 | seconds = seconds - hours*3600 58 | minutes = int(seconds / 60) 59 | seconds = seconds - minutes*60 60 | secondsf = int(seconds) 61 | seconds = seconds - secondsf 62 | millis = int(seconds*1000) 63 | 64 | f = '' 65 | i = 1 66 | if days > 0: 67 | f += str(days) + 'D' 68 | i += 1 69 | if hours > 0 and i <= 2: 70 | f += str(hours) + 'h' 71 | i += 1 72 | if minutes > 0 and i <= 2: 73 | f += str(minutes) + 'm' 74 | i += 1 75 | if secondsf > 0 and i <= 2: 76 | f += str(secondsf) + 's' 77 | i += 1 78 | if millis > 0 and i <= 2: 79 | f += str(millis) + 'ms' 80 | i += 1 81 | if f == '': 82 | f = '0ms' 83 | return f 84 | 85 | 86 | def accuracy(output, target, topk=(1,)): 87 | """Compute the accuracy over the k top predictions for the specified values of k""" 88 | with torch.no_grad(): 89 | maxk = max(topk) 90 | batch_size = target.size(0) 91 | # print(output) 92 | 93 | _, pred = output.topk(maxk, dim=1, largest=True, sorted=True) 94 | pred = pred.t() 95 | # print(pred) 96 | correct = pred.eq(target.view(1, -1).expand_as(pred)) 97 | 98 | res = [] 99 | for k in topk: 100 | correct_k = correct[:k].reshape(-1).float().sum(0, keepdim=True) 101 | res.append(correct_k.mul_(100./batch_size)) 102 | return res 103 | 104 | # class confision_matrix(object): 105 | -------------------------------------------------------------------------------- /CV_net/RepVGG/utils.py: -------------------------------------------------------------------------------- 1 | # !/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time: 2023/2/12 10:12 4 | # @Author: 'IReverser' 5 | # @ FileName: utils.py 6 | import os 7 | import torch 8 | 9 | 10 | def make_dir(folder): 11 | if not os.path.isdir(folder): 12 | os.makedirs(folder) 13 | 14 | 15 | class AverageMeter(object): 16 | """Computes and stores the average and current value""" 17 | def __init__(self): 18 | self.reset() 19 | 20 | def reset(self): 21 | self.val = 0 22 | self.avg = 0 23 | self.sum = 0 24 | self.count = 0 25 | 26 | def update(self, val, n=1): 27 | self.val = val 28 | self.sum += val * n 29 | self.count += n 30 | self.avg = self.sum / self.count 31 | 32 | 33 | class Logger(): 34 | def __init__(self, args, filename='log.txt'): 35 | 36 | self.filename = filename 37 | self.file = open(filename, 'a') 38 | # Write model configuration at top of file 39 | for arg in vars(args): 40 | self.file.write(arg+': '+str(getattr(args, arg))+'\n') 41 | self.file.flush() 42 | 43 | def writerow(self, row): 44 | for k in row: 45 | self.file.write(k+': '+row[k]+' ') 46 | self.file.write('\n') 47 | self.file.flush() 48 | 49 | def close(self): 50 | self.file.close() 51 | 52 | 53 | def format_time(seconds): 54 | days = int(seconds / 3600/24) 55 | seconds = seconds - days*3600*24 56 | hours = int(seconds / 3600) 57 | seconds = seconds - hours*3600 58 | minutes = int(seconds / 60) 59 | seconds = seconds - minutes*60 60 | secondsf = int(seconds) 61 | seconds = seconds - secondsf 62 | millis = int(seconds*1000) 63 | 64 | f = '' 65 | i = 1 66 | if days > 0: 67 | f += str(days) + 'D' 68 | i += 1 69 | if hours > 0 and i <= 2: 70 | f += str(hours) + 'h' 71 | i += 1 72 | if minutes > 0 and i <= 2: 73 | f += str(minutes) + 'm' 74 | i += 1 75 | if secondsf > 0 and i <= 2: 76 | f += str(secondsf) + 's' 77 | i += 1 78 | if millis > 0 and i <= 2: 79 | f += str(millis) + 'ms' 80 | i += 1 81 | if f == '': 82 | f = '0ms' 83 | return f 84 | 85 | 86 | def accuracy(output, target, topk=(1,)): 87 | """Compute the accuracy over the k top predictions for the specified values of k""" 88 | with torch.no_grad(): 89 | maxk = max(topk) 90 | batch_size = target.size(0) 91 | # print(output) 92 | 93 | _, pred = output.topk(maxk, dim=1, largest=True, sorted=True) 94 | pred = pred.t() 95 | # print(pred) 96 | correct = pred.eq(target.view(1, -1).expand_as(pred)) 97 | 98 | res = [] 99 | for k in topk: 100 | correct_k = correct[:k].reshape(-1).float().sum(0, keepdim=True) 101 | res.append(correct_k.mul_(100./batch_size)) 102 | return res 103 | 104 | # class confision_matrix(object): 105 | -------------------------------------------------------------------------------- /CV_net/EfficientNet/utils.py: -------------------------------------------------------------------------------- 1 | # !/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time: 2023/2/12 10:12 4 | # @Author: 'IReverser' 5 | # @ FileName: utils.py 6 | import os 7 | import torch 8 | 9 | 10 | def make_dir(folder): 11 | if not os.path.isdir(folder): 12 | os.makedirs(folder) 13 | 14 | 15 | class AverageMeter(object): 16 | """Computes and stores the average and current value""" 17 | def __init__(self): 18 | self.reset() 19 | 20 | def reset(self): 21 | self.val = 0 22 | self.avg = 0 23 | self.sum = 0 24 | self.count = 0 25 | 26 | def update(self, val, n=1): 27 | self.val = val 28 | self.sum += val * n 29 | self.count += n 30 | self.avg = self.sum / self.count 31 | 32 | 33 | class Logger(): 34 | def __init__(self, args, filename='log.txt'): 35 | 36 | self.filename = filename 37 | self.file = open(filename, 'a') 38 | # Write model configuration at top of file 39 | for arg in vars(args): 40 | self.file.write(arg+': '+str(getattr(args, arg))+'\n') 41 | self.file.flush() 42 | 43 | def writerow(self, row): 44 | for k in row: 45 | self.file.write(k+': '+row[k]+' ') 46 | self.file.write('\n') 47 | self.file.flush() 48 | 49 | def close(self): 50 | self.file.close() 51 | 52 | 53 | def format_time(seconds): 54 | days = int(seconds / 3600/24) 55 | seconds = seconds - days*3600*24 56 | hours = int(seconds / 3600) 57 | seconds = seconds - hours*3600 58 | minutes = int(seconds / 60) 59 | seconds = seconds - minutes*60 60 | secondsf = int(seconds) 61 | seconds = seconds - secondsf 62 | millis = int(seconds*1000) 63 | 64 | f = '' 65 | i = 1 66 | if days > 0: 67 | f += str(days) + 'D' 68 | i += 1 69 | if hours > 0 and i <= 2: 70 | f += str(hours) + 'h' 71 | i += 1 72 | if minutes > 0 and i <= 2: 73 | f += str(minutes) + 'm' 74 | i += 1 75 | if secondsf > 0 and i <= 2: 76 | f += str(secondsf) + 's' 77 | i += 1 78 | if millis > 0 and i <= 2: 79 | f += str(millis) + 'ms' 80 | i += 1 81 | if f == '': 82 | f = '0ms' 83 | return f 84 | 85 | 86 | def accuracy(output, target, topk=(1,)): 87 | """Compute the accuracy over the k top predictions for the specified values of k""" 88 | with torch.no_grad(): 89 | maxk = max(topk) 90 | batch_size = target.size(0) 91 | # print(output) 92 | 93 | _, pred = output.topk(maxk, dim=1, largest=True, sorted=True) 94 | pred = pred.t() 95 | # print(pred) 96 | correct = pred.eq(target.view(1, -1).expand_as(pred)) 97 | 98 | res = [] 99 | for k in topk: 100 | correct_k = correct[:k].reshape(-1).float().sum(0, keepdim=True) 101 | res.append(correct_k.mul_(100./batch_size)) 102 | return res 103 | 104 | # class confision_matrix(object): 105 | -------------------------------------------------------------------------------- /CV_net/MobileViT/utils.py: -------------------------------------------------------------------------------- 1 | # !/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time: 2023/2/12 10:12 4 | # @Author: 'IReverser' 5 | # @ FileName: utils.py 6 | import os 7 | import torch 8 | 9 | 10 | def make_dir(folder): 11 | if not os.path.isdir(folder): 12 | os.makedirs(folder) 13 | 14 | 15 | class AverageMeter(object): 16 | """Computes and stores the average and current value""" 17 | def __init__(self): 18 | self.reset() 19 | 20 | def reset(self): 21 | self.val = 0 22 | self.avg = 0 23 | self.sum = 0 24 | self.count = 0 25 | 26 | def update(self, val, n=1): 27 | self.val = val 28 | self.sum += val * n 29 | self.count += n 30 | self.avg = self.sum / self.count 31 | 32 | 33 | class Logger(): 34 | def __init__(self, args, filename='log.txt'): 35 | 36 | self.filename = filename 37 | self.file = open(filename, 'a') 38 | # Write model configuration at top of file 39 | for arg in vars(args): 40 | self.file.write(arg+': '+str(getattr(args, arg))+'\n') 41 | self.file.flush() 42 | 43 | def writerow(self, row): 44 | for k in row: 45 | self.file.write(k+': '+row[k]+' ') 46 | self.file.write('\n') 47 | self.file.flush() 48 | 49 | def close(self): 50 | self.file.close() 51 | 52 | 53 | def format_time(seconds): 54 | days = int(seconds / 3600/24) 55 | seconds = seconds - days*3600*24 56 | hours = int(seconds / 3600) 57 | seconds = seconds - hours*3600 58 | minutes = int(seconds / 60) 59 | seconds = seconds - minutes*60 60 | secondsf = int(seconds) 61 | seconds = seconds - secondsf 62 | millis = int(seconds*1000) 63 | 64 | f = '' 65 | i = 1 66 | if days > 0: 67 | f += str(days) + 'D' 68 | i += 1 69 | if hours > 0 and i <= 2: 70 | f += str(hours) + 'h' 71 | i += 1 72 | if minutes > 0 and i <= 2: 73 | f += str(minutes) + 'm' 74 | i += 1 75 | if secondsf > 0 and i <= 2: 76 | f += str(secondsf) + 's' 77 | i += 1 78 | if millis > 0 and i <= 2: 79 | f += str(millis) + 'ms' 80 | i += 1 81 | if f == '': 82 | f = '0ms' 83 | return f 84 | 85 | 86 | def accuracy(output, target, topk=(1,)): 87 | """Compute the accuracy over the k top predictions for the specified values of k""" 88 | with torch.no_grad(): 89 | maxk = max(topk) 90 | batch_size = target.size(0) 91 | # print(output) 92 | 93 | _, pred = output.topk(maxk, dim=1, largest=True, sorted=True) 94 | pred = pred.t() 95 | # print(pred) 96 | correct = pred.eq(target.view(1, -1).expand_as(pred)) 97 | 98 | res = [] 99 | for k in topk: 100 | correct_k = correct[:k].reshape(-1).float().sum(0, keepdim=True) 101 | res.append(correct_k.mul_(100./batch_size)) 102 | return res 103 | 104 | # class confision_matrix(object): 105 | -------------------------------------------------------------------------------- /CV_net/EfficientNetv2/utils.py: -------------------------------------------------------------------------------- 1 | # !/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time: 2023/2/12 10:12 4 | # @Author: 'IReverser' 5 | # @ FileName: utils.py 6 | import os 7 | import torch 8 | 9 | 10 | def make_dir(folder): 11 | if not os.path.isdir(folder): 12 | os.makedirs(folder) 13 | 14 | 15 | class AverageMeter(object): 16 | """Computes and stores the average and current value""" 17 | def __init__(self): 18 | self.reset() 19 | 20 | def reset(self): 21 | self.val = 0 22 | self.avg = 0 23 | self.sum = 0 24 | self.count = 0 25 | 26 | def update(self, val, n=1): 27 | self.val = val 28 | self.sum += val * n 29 | self.count += n 30 | self.avg = self.sum / self.count 31 | 32 | 33 | class Logger(): 34 | def __init__(self, args, filename='log.txt'): 35 | 36 | self.filename = filename 37 | self.file = open(filename, 'a') 38 | # Write model configuration at top of file 39 | for arg in vars(args): 40 | self.file.write(arg+': '+str(getattr(args, arg))+'\n') 41 | self.file.flush() 42 | 43 | def writerow(self, row): 44 | for k in row: 45 | self.file.write(k+': '+row[k]+' ') 46 | self.file.write('\n') 47 | self.file.flush() 48 | 49 | def close(self): 50 | self.file.close() 51 | 52 | 53 | def format_time(seconds): 54 | days = int(seconds / 3600/24) 55 | seconds = seconds - days*3600*24 56 | hours = int(seconds / 3600) 57 | seconds = seconds - hours*3600 58 | minutes = int(seconds / 60) 59 | seconds = seconds - minutes*60 60 | secondsf = int(seconds) 61 | seconds = seconds - secondsf 62 | millis = int(seconds*1000) 63 | 64 | f = '' 65 | i = 1 66 | if days > 0: 67 | f += str(days) + 'D' 68 | i += 1 69 | if hours > 0 and i <= 2: 70 | f += str(hours) + 'h' 71 | i += 1 72 | if minutes > 0 and i <= 2: 73 | f += str(minutes) + 'm' 74 | i += 1 75 | if secondsf > 0 and i <= 2: 76 | f += str(secondsf) + 's' 77 | i += 1 78 | if millis > 0 and i <= 2: 79 | f += str(millis) + 'ms' 80 | i += 1 81 | if f == '': 82 | f = '0ms' 83 | return f 84 | 85 | 86 | def accuracy(output, target, topk=(1,)): 87 | """Compute the accuracy over the k top predictions for the specified values of k""" 88 | with torch.no_grad(): 89 | maxk = max(topk) 90 | batch_size = target.size(0) 91 | # print(output) 92 | 93 | _, pred = output.topk(maxk, dim=1, largest=True, sorted=True) 94 | pred = pred.t() 95 | # print(pred) 96 | correct = pred.eq(target.view(1, -1).expand_as(pred)) 97 | 98 | res = [] 99 | for k in topk: 100 | correct_k = correct[:k].reshape(-1).float().sum(0, keepdim=True) 101 | res.append(correct_k.mul_(100./batch_size)) 102 | return res 103 | 104 | # class confision_matrix(object): 105 | -------------------------------------------------------------------------------- /CV_net/DeiT/losses.py: -------------------------------------------------------------------------------- 1 | # !/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time: 2023/2/26 9:22 4 | # @Author: 'IReverser' 5 | # @ FileName: losses.py 6 | import torch 7 | import torch.nn as nn 8 | from torch.nn import functional as F 9 | 10 | 11 | class DistillationLoss(nn.Module): 12 | """ 13 | This module wraps a standard criterion and adds an extra knowledge distillation loss by 14 | taking a teacher model prediction and using it as additional supervision. 15 | """ 16 | def __init__(self, 17 | base_criterion: nn.Module, 18 | teacher_model: nn.Module, 19 | distillation_type: str, 20 | alpha: float, 21 | tau: float): 22 | super(DistillationLoss, self).__init__() 23 | self.base_criterion = base_criterion 24 | self.teacher_model = teacher_model 25 | assert distillation_type in ["none", "soft", "hard"] 26 | self.distillation_type = distillation_type 27 | self.alpha = alpha 28 | self.tau = tau 29 | 30 | def forward(self, inputs, outputs, labels): 31 | """ 32 | Args: 33 | inputs: The original inputs that are feed to the teacher model 34 | outputs: the outputs of the model to be trained. It is expected to be 35 | either a Tensor, or a Tuple[Tensor, Tensor], with the original output 36 | in the first position and the distillation predictions as the second output 37 | labels: the labels for the base criterion 38 | """ 39 | outputs_kd = None 40 | if not isinstance(outputs, torch.Tensor): 41 | # assume that the model outputs a tuple of [outputs (class_token), outputs_kd (dist_token)] 42 | outputs, outputs_kd = outputs 43 | base_loss = self.base_criterion(outputs, labels) 44 | if self.distillation_type == "none": 45 | return base_loss 46 | 47 | if outputs_kd is None: 48 | raise ValueError("When knowledge distillation is enabled, the model is " 49 | "expected to return a Tuple[Tensor, Tensor] with the output of the " 50 | "class_token and the dist_token") 51 | # don't backprop throught the teacher 52 | with torch.no_grad(): 53 | teacher_outputs = self.teacher_model(inputs) 54 | 55 | if self.distillation_type == "soft": 56 | T = self.tau 57 | # taken from https://github.com/peterliht/knowledge-distillation-pytorch/blob/master/model/net.py#L100 58 | # batchmean等于KLDiv的真正数学定义 59 | distillation_loss = F.kl_div(F.log_softmax(outputs_kd / T, dim=1), 60 | F.log_softmax(teacher_outputs / T, dim=1), 61 | reduction="mean", 62 | log_target=True) * (T ** 2) 63 | elif self.distillation_type == "hard": 64 | distillation_loss = F.cross_entropy(outputs_kd, teacher_outputs.argmax(dim=1)) 65 | 66 | loss = base_loss * (1 - self.alpha) + distillation_loss * self.alpha 67 | return loss 68 | 69 | 70 | -------------------------------------------------------------------------------- /mofan_python/6_build_nn_quickly.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # Author = 'IReverser' 4 | 5 | import torch 6 | import torch.nn.functional as F 7 | from torch.autograd import Variable 8 | import matplotlib.pyplot as plt 9 | import time 10 | 11 | n_data = torch.ones([100, 2]) 12 | x0 = torch.normal(2*n_data, 1) # class0 x data (tensor), shape=(100, 2) 13 | y0 = torch.zeros(100) # class0 y data (tensor), shape=(100, 1) 14 | x1 = torch.normal(-2*n_data, 1) # class1 x data (tensor), shape=(100, 2) 15 | y1 = torch.ones(100) # class1 y data (tensor), shape=(100, 1) 16 | x = torch.cat((x0, x1), 0).type(torch.FloatTensor) # FloatTensor = 32-bit floating 17 | y = torch.cat((y0, y1), ).type(torch.LongTensor) # LongTensor = 64-bit integer 18 | 19 | 20 | # The code below is deprecated in Pytorch 0.4. Now, autograd directly supports tensors 21 | # x, y = Variable(x), Variable(y) 22 | # 23 | # plt.scatter(x.data.numpy()[:, 0], x.data.numpy()[:, 1], c=y.data.numpy(), s=100, lw=0, cmap='RdYlGn') 24 | # plt.show() 25 | 26 | # method 1 27 | class Net(torch.nn.Module): 28 | def __init__(self, n_feature, n_hidden, n_output): 29 | super(Net, self).__init__() 30 | self.hidden = torch.nn.Linear(n_feature, n_hidden) 31 | self.predict = torch.nn.Linear(n_hidden, n_output) 32 | 33 | def forward(self, x): 34 | x = F.relu(self.hidden(x)) 35 | x = self.predict(x) 36 | return x 37 | 38 | 39 | net1 = Net(2, 10, 2) 40 | 41 | # method 2 42 | net2 = torch.nn.Sequential( 43 | torch.nn.Linear(2, 10), 44 | torch.nn.ReLU(), 45 | torch.nn.Linear(10, 2), 46 | ) 47 | 48 | print(net1) 49 | print(net2) 50 | 51 | # optimizer = torch.optim.SGD(net.parameters(), lr=0.02) 52 | # loss_func = torch.nn.CrossEntropyLoss() 53 | # 54 | # start_time = time.time() 55 | # plt.ion() 56 | # 57 | # for t in range(150): 58 | # print(t) 59 | # out = net(x) # [-2, -.12, 20] F,softmax(out) [.1, 0.2, .7] 60 | # 61 | # loss = loss_func(out, y) # must be (1. nn output, 2. target), the target label is NOT one-hotted 62 | # 63 | # optimizer.zero_grad() # set gradient of parameters to zero 64 | # loss.backward() # calculate gradient of per node 65 | # optimizer.step() # optimize gradient 66 | # 67 | # if t % 2 == 0: 68 | # # plot and show learning process 69 | # plt.cla() 70 | # prediction = torch.max(out, 1)[1] 71 | # pred_y = prediction.data.numpy() 72 | # target_y = y.data.numpy() 73 | # plt.scatter(x.data.numpy()[:, 0], x.data.numpy()[:, 1], c=pred_y, s=100, lw=0, cmap='RdYlGn') 74 | # accuracy = float((pred_y == target_y).astype(int).sum()) / float(target_y.size) 75 | # plt.text(1.5, -4, 'Accuracy=%.2f' % accuracy, fontdict={'size': 20, 'color': 'red'}) 76 | # plt.pause(0.1) 77 | # 78 | # end_time = time.time() 79 | # print('The total cost time:', str(end_time - start_time) + 'sec') 80 | # plt.ioff() 81 | # plt.show() 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | -------------------------------------------------------------------------------- /CV_net/VGG/model.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2021/8/25 16:14 4 | # @Author : 'IReverser' 5 | # @FileName: model.py 6 | import torch 7 | import torch.nn as nn 8 | 9 | 10 | class VGG(nn.Module): 11 | def __init__(self, features, n_classes=1000, init_weights=False): 12 | super(VGG, self).__init__() 13 | self.features = features 14 | self.classifier = nn.Sequential( 15 | nn.Linear(512*7*7, 4096), 16 | nn.ReLU(inplace=True), 17 | nn.Dropout(p=0.5), 18 | nn.Linear(4096, 4096), 19 | nn.ReLU(inplace=True), 20 | nn.Dropout(p=0.5), 21 | nn.Linear(4096, n_classes) 22 | ) 23 | if init_weights: 24 | self._initalize_weights() 25 | 26 | def forward(self, x): 27 | # BATCH_SIZE * 3 * 224 * 224 28 | x = self.features(x) 29 | # print(x.shape) 30 | # BATCH_SIZE * 512 * 7 * 7 31 | x = torch.flatten(x, start_dim=1) 32 | # BATCH_SIZE * 512 * 7 * 7 33 | x = self.classifier(x) 34 | return x 35 | 36 | def _initalize_weights(self): 37 | for m in self.modules(): 38 | if isinstance(nn, nn.Conv2d): 39 | # nn.init.kaiming_normal_(m.weight, mode="fan_out", nonlinearity='relu') 40 | nn.init.xavier_uniform_(m.weight) 41 | if m.bias is not None: 42 | nn.init.constant_(m.bias, 0) 43 | elif isinstance(m, nn.Linear): 44 | nn.init.xavier_uniform_(m.weight) 45 | # nn.init.normal_(m.weight, 0, 0.01) 46 | nn.init.constant_(m.bias, 0) 47 | 48 | 49 | def make_feature_layer(cfg: list, flag=0): 50 | layers = [] 51 | in_channels = 3 52 | for ch in cfg: 53 | if ch == 'M': 54 | layers += [nn.MaxPool2d(kernel_size=2, stride=2)] 55 | else: 56 | conv2d = nn.Conv2d(in_channels, ch, kernel_size=3, padding=1) 57 | layers += [conv2d, nn.ReLU(inplace=True)] 58 | if flag == 1 and in_channels == 3: 59 | layers += [nn.LocalResponseNorm(size=2, alpha=0.0001, beta=0.75, k=1.0)], 60 | in_channels = ch 61 | return nn.Sequential(*layers) 62 | 63 | 64 | cfgs = { 65 | "vgg11": [64, 'M', 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'], 66 | "vgg11L": [64, 'M', 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'], 67 | "vgg13": [64, 64, 'M', 128, 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'], 68 | "vgg16": [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M', 512, 512, 512, 'M'], 69 | "vgg19": [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 256, 'M', 512, 512, 512, 512, 'M', 512, 512, 512, 512, 'M'] 70 | } 71 | 72 | 73 | def vgg(model_name='vgg16', n_classes=1000, **kwargs): 74 | assert model_name in cfgs, "Warning: model {} is not in cfgs dictionary!".format(model_name) 75 | cfg = cfgs[model_name] 76 | 77 | flag = 0 78 | if model_name[-1] == 'L': 79 | flag = 1 80 | model = VGG(make_feature_layer(cfg, flag), n_classes, **kwargs) 81 | 82 | return model 83 | 84 | 85 | # x = torch.randn((1, 3, 224, 224)) 86 | # net = vgg(model_name='vgg16', n_classes=1000, init_weights=True) 87 | # y = net(x) 88 | # print(y.shape) 89 | # # output parameters 90 | # from torchsummary import summary 91 | # summary(net, input_size=[[3, 224, 224]], batch_size=2, device='cpu') 92 | -------------------------------------------------------------------------------- /CV_net/LeNet/train_mnist.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2021/7/29 16:44 4 | # @Author : 'IReverser' 5 | # @FileName: train.py 6 | 7 | import torch 8 | import torchvision 9 | import torch.nn as nn 10 | import torch.optim as optim 11 | import torchvision.transforms as transforms 12 | from model import LeNet_MNIST 13 | import os 14 | 15 | BATCH_SIZE = 36 16 | RATE_LEARNING = 0.0001 17 | EPOCH = 7 18 | PRINT_EPOCH = 100 19 | DATA_PATH = './data' 20 | SAVE_PATH = './model/' 21 | DOWNLOAD = True # if you want to download data, it sets 'True', otherwise. 22 | device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') 23 | print('Use device: ', device) 24 | transform = transforms.Compose([ 25 | transforms.ToTensor(), 26 | transforms.Normalize(0.5, 0.5)]) 27 | # load train set and test set 28 | train_samples = torchvision.datasets.MNIST(root=DATA_PATH, train=True, 29 | download=DOWNLOAD, transform=transform) 30 | val_samples = torchvision.datasets.MNIST(root=DATA_PATH, train=False, 31 | download=DOWNLOAD, transform=transform) 32 | # data format transform 33 | train_loader = torch.utils.data.DataLoader(train_samples, batch_size=BATCH_SIZE, 34 | shuffle=True, num_workers=0) 35 | val_loader = torch.utils.data.DataLoader(val_samples, batch_size=10000, 36 | shuffle=True, num_workers=0) 37 | 38 | # category labels 39 | # classes = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9') 40 | 41 | # train set are load for classification 42 | val_iter = iter(val_loader) 43 | val_images, val_labels = val_iter.next() 44 | val_images = val_images.to(device) 45 | val_labels = val_labels.to(device) 46 | 47 | # initialize net 48 | net = LeNet_MNIST() 49 | net = net.to(device) 50 | 51 | # design loss function 52 | loss_function = nn.CrossEntropyLoss() 53 | # design optimizer 54 | optimizer = optim.Adam(net.parameters(), lr=RATE_LEARNING) 55 | 56 | # start training 57 | for epoch in range(EPOCH): 58 | run_loss = 0.0 59 | for step, data in enumerate(train_loader, start=0): 60 | inputs, labels = data 61 | 62 | inputs = inputs.to(device) 63 | labels = labels.to(device) 64 | 65 | optimizer.zero_grad() 66 | output = net(inputs) 67 | loss = loss_function(output, labels) 68 | # Update Visualization 69 | loss.backward() 70 | optimizer.step() 71 | 72 | run_loss += loss.item() 73 | if step % PRINT_EPOCH == (PRINT_EPOCH - 1): 74 | with torch.no_grad(): 75 | outputs = net(val_images) 76 | pred_y = torch.max(outputs, dim=1)[1].to(device) 77 | acc_rate = torch.eq(pred_y, val_labels).sum().item() / val_labels.size(0) 78 | print('Epoch: %d, iter: %d, train_loss: %.3f, test_accuracy: %.3f' 79 | % (epoch, step, run_loss/PRINT_EPOCH, acc_rate)) 80 | run_loss = 0.0 81 | 82 | print('Finished Training!') 83 | # save model 84 | save_path = SAVE_PATH + 'LeNet_mnist.pth' 85 | if not os.path.exists(SAVE_PATH): 86 | os.makedirs(SAVE_PATH) 87 | torch.save(net.state_dict(), save_path) 88 | # save onnx model format 89 | # dummy_input = torch.randn(1, 1, 28, 28, requires_grad=True).to(device) 90 | # torch.onnx.export(net, dummy_input, SAVE_PATH + "lenet_mnist.onnx") 91 | -------------------------------------------------------------------------------- /CV_net/SPPNet/sppnet.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2023/7/13 12:29 4 | # @Author : '' 5 | # @FileName: sppnet.py 6 | # modified from https://github.com/yifanjiang19/sppnet-pytorch 7 | import math 8 | import torch 9 | import torch.nn as nn 10 | import torch.nn.functional as F 11 | 12 | 13 | def spatial_pyramid_pool(previous_conv, num_sample, previous_conv_size, out_pool_size): 14 | """ 15 | previous_conv: a tensor vector of previous convolution layer 16 | num_sample: the number of image in the batch 17 | previous_conv_size: the vector [height, width] of the matrix features size of previous convolution layer 18 | out_pool_size: a int vector of expected output size of max pooling layer 19 | returns: a tensor vector with shape [1 x n] is the concentration of multi-level pooling 20 | """ 21 | # print(previous_conv.size()) 22 | for i in range(len(out_pool_size)): 23 | h_wid = math.ceil(previous_conv_size[0] / out_pool_size[i]) 24 | w_wid = math.ceil(previous_conv_size[1] / out_pool_size[i]) 25 | h_str = math.floor(previous_conv_size[0] / out_pool_size[i]) 26 | w_str = math.floor(previous_conv_size[1] / out_pool_size[i]) 27 | maxpool = nn.MaxPool2d((h_wid, w_wid), stride=(h_str, w_str)) 28 | x = maxpool(previous_conv) 29 | # print("x.size ", i, ":", x.size()) 30 | if i == 0: 31 | spp = x.view(num_sample, -1) 32 | # print("0spp size: ", spp.size()) 33 | else: 34 | # print("size: ", i, " ", spp.size()) 35 | spp = torch.cat((spp, x.view(num_sample, -1)), 1) 36 | return spp 37 | 38 | 39 | class SPPNet(nn.Module): 40 | def __init__(self, ndf=64, num_classes=1000): 41 | super(SPPNet, self).__init__() 42 | self.output_num = [4, 2, 1] 43 | 44 | self.conv1 = nn.Conv2d(3, ndf, 4, 2, 1, bias=False) 45 | 46 | self.conv2 = nn.Conv2d(ndf, ndf * 2, 4, 1, 1, bias=False) 47 | self.bn1 = nn.BatchNorm2d(ndf * 2) 48 | 49 | self.conv3 = nn.Conv2d(ndf * 2, ndf * 4, 4, 1, 1, bias=False) 50 | self.bn2 = nn.BatchNorm2d(ndf * 4) 51 | 52 | self.conv4 = nn.Conv2d(ndf * 4, ndf * 8, 4, 1, 1, bias=False) 53 | self.bn3 = nn.BatchNorm2d(ndf * 8) 54 | 55 | self.conv5 = nn.Conv2d(ndf * 8, 64, 4, 1, 0, bias=False) 56 | self.fc1 = nn.Linear(64 * (self.output_num[0]**2 + self.output_num[1]**2 + self.output_num[2]**2), 1024) 57 | self.dropout = nn.Dropout(0.2) 58 | self.fc2 = nn.Linear(1024, num_classes) 59 | 60 | def forward(self, x): 61 | x = self.conv1(x) 62 | x = F.leaky_relu(x) 63 | 64 | x = self.conv2(x) 65 | x = F.leaky_relu(self.bn1(x)) 66 | 67 | x = self.conv3(x) 68 | x = F.leaky_relu(self.bn2(x)) 69 | 70 | x = self.conv4(x) 71 | x = F.leaky_relu(self.bn3(x)) 72 | x = self.conv5(x) 73 | # print(x.shape) # 1 64 106 106 74 | 75 | spp = spatial_pyramid_pool(x, x.size(0), [int(x.size(2)), int(x.size(3))], self.output_num) # [106, 106] 76 | # print(spp.size()) # 1 1344 77 | 78 | fc1 = self.dropout(self.fc1(spp)) 79 | fc2 = self.fc2(fc1) 80 | out = torch.sigmoid(fc2) 81 | return out 82 | 83 | 84 | model_list = { 85 | "sppnet": SPPNet, 86 | } 87 | 88 | 89 | if __name__ == '__main__': 90 | x = torch.randn((2, 3, 224, 224)) 91 | net = model_list["sppnet"](num_classes=1000) 92 | y = net(x) 93 | print(y.shape) 94 | -------------------------------------------------------------------------------- /mofan_python/GAN.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # Author = '' 4 | 5 | import torch 6 | import torch.nn as nn 7 | import numpy as np 8 | import matplotlib.pyplot as plt 9 | 10 | # torch.manual_seed(1) # reproducible 11 | # np.random.seed(1) 12 | 13 | # Hyper Parameters 14 | BATCH_SIZE = 64 15 | LR_G = 0.00013 # learning rate for generator 16 | LR_D = 0.00013 # learning rate for discriminator 17 | N_IDEAS = 5 # think of this as number of ideas for generating an art work (Generator) 18 | ART_COMPONENTS = 15 # it could be total point G can draw in the canvas 19 | PAINT_POINTS = np.vstack([np.linspace(-1, 1, ART_COMPONENTS) for _ in range(BATCH_SIZE)]) 20 | 21 | # show our beautiful painting range 22 | # plt.plot(PAINT_POINTS[0], 2 * np.power(PAINT_POINTS[0], 2) + 1, c='#74BCFF', lw=3, label='upper bound') 23 | # plt.plot(PAINT_POINTS[0], 1 * np.power(PAINT_POINTS[0], 2) + 0, c='#FF9359', lw=3, label='lower bound') 24 | # plt.legend(loc='upper right') 25 | # plt.show() 26 | 27 | 28 | def artist_works(): # painting from the famous artist (real target) 29 | a = np.random.uniform(1, 2, size=BATCH_SIZE)[:, np.newaxis] 30 | paintings = a * np.power(PAINT_POINTS, 2) + (a-1) 31 | paintings = torch.from_numpy(paintings).float() 32 | return paintings 33 | 34 | 35 | G = nn.Sequential( # Generator 36 | nn.Linear(N_IDEAS, 128), # random ideas (could from normal distribution) 37 | nn.ReLU(), 38 | nn.Linear(128, ART_COMPONENTS), # making a painting from these random ideas 39 | ) 40 | 41 | D = nn.Sequential( # Discriminator 42 | nn.Linear(ART_COMPONENTS, 128), # receive art work either from the famous artist or a newbie like G 43 | nn.ReLU(), 44 | nn.Linear(128, 1), 45 | nn.Sigmoid(), # tell the probability that the art work is made by artist 46 | ) 47 | 48 | opt_D = torch.optim.Adam(D.parameters(), lr=LR_D) 49 | opt_G = torch.optim.Adam(G.parameters(), lr=LR_G) 50 | 51 | plt.ion() # something about continuous plotting 52 | 53 | for step in range(10000): 54 | artist_paintings = artist_works() # real painting from artist 55 | G_ideas = torch.randn(BATCH_SIZE, N_IDEAS) # random ideas 56 | G_paintings = G(G_ideas) # fake painting from G (random ideas) 57 | 58 | prob_artist0 = D(artist_paintings) # D try to increase this prob 59 | prob_artist1 = D(G_paintings) # D try to reduce this prob 60 | 61 | D_loss = - torch.mean(torch.log(prob_artist0) + torch.log(1. - prob_artist1)) 62 | G_loss = torch.mean(torch.log(1. - prob_artist1)) 63 | 64 | opt_D.zero_grad() 65 | D_loss.backward(retain_graph=True) # reusing computational graph 66 | opt_D.step() 67 | 68 | opt_G.zero_grad() 69 | G_loss.backward() 70 | opt_G.step() 71 | 72 | if step % 50 == 0: # plotting 73 | plt.cla() 74 | plt.plot(PAINT_POINTS[0], G_paintings.data.numpy()[0], c='#4AD631', lw=3, label='Generated painting',) 75 | plt.plot(PAINT_POINTS[0], 2 * np.power(PAINT_POINTS[0], 2) + 1, c='#74BCFF', lw=3, label='upper bound') 76 | plt.plot(PAINT_POINTS[0], 1 * np.power(PAINT_POINTS[0], 2) + 0, c='#FF9359', lw=3, label='lower bound') 77 | plt.text(-.5, 2.3, 'D accuracy=%.2f (0.5 for D to converge)' % prob_artist0.data.numpy().mean(), fontdict={'size': 13}) 78 | plt.text(-.5, 2, 'D score= %.2f (-1.38 for G to converge)' % -D_loss.data.numpy(), fontdict={'size': 13}) 79 | plt.ylim((0, 3));plt.legend(loc='upper right', fontsize=10);plt.draw();plt.pause(0.01) 80 | 81 | plt.ioff() 82 | plt.show() 83 | -------------------------------------------------------------------------------- /mofan_python/11_RNN_regressor.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # Author = '' 4 | 5 | import torch 6 | from torch import nn 7 | import numpy as np 8 | import matplotlib.pyplot as plt 9 | 10 | # torch.manual_seed(1) # reproducible 11 | 12 | # Hyper Parameters 13 | EPOCH = 10 14 | TIME_STEP = 10 # rnn time step 15 | INPUT_SIZE = 1 # rnn input size 16 | LR = 0.02 # learning rate 17 | 18 | # show data 19 | # steps = np.linspace(0, np.pi * 2, 100, dtype=np.float32) # float32 for converting torch FloatTensor 20 | # x_np = np.sin(steps) 21 | # y_np = np.cos(steps) 22 | # plt.plot(steps, y_np, 'r-', label='target (cos)') 23 | # plt.plot(steps, x_np, 'b-', label='input (sin)') 24 | # plt.legend(loc='best') 25 | # plt.show() 26 | 27 | 28 | class RNN(nn.Module): 29 | def __init__(self): 30 | super(RNN, self).__init__() 31 | 32 | self.rnn = nn.RNN( 33 | input_size=INPUT_SIZE, # 1 34 | hidden_size=32, # rnn hidden unit 35 | num_layers=1, # number of rnn layer 36 | batch_first=True, # input & output will has batch size as 1s dimension. e.g. (batch, time_step, input_size) 37 | ) 38 | self.out = nn.Linear(32, 1) 39 | 40 | def forward(self, x, h_state): 41 | # x (batch, time_step, input_size) 42 | # h_state (n_layers, batch, hidden_size) 43 | # r_out (batch, time_step, hidden_size) 44 | r_out, h_state = self.rnn(x, h_state) 45 | 46 | outs = [] # save all predictions 47 | for time_step in range(r_out.size(1)): # calculate output for each time step 48 | outs.append(self.out(r_out[:, time_step, :])) 49 | return torch.stack(outs, dim=1), h_state # transform list as tensor 50 | 51 | # instead, for simplicity, you can replace above codes by follows 52 | # r_out = r_out.view(-1, 32) 53 | # outs = self.out(r_out) 54 | # outs = outs.view(-1, TIME_STEP, 1) 55 | # return outs, h_state 56 | 57 | # or even simpler, since nn.Linear can accept inputs of any dimension 58 | # and returns outputs with same dimension except for the last 59 | # outs = self.out(r_out) 60 | # return outs 61 | 62 | 63 | rnn = RNN() 64 | print(rnn) 65 | 66 | optimizer = torch.optim.Adam(rnn(), lr=LR) # optimize all cnn parameters 67 | loss_func = nn.MSELoss() 68 | 69 | h_state = None # for initial hidden state 70 | 71 | plt.figure(1, figsize=(12, 5)) 72 | plt.ion() # continuously plot 73 | 74 | for step in range(TIME_STEP * EPOCH): 75 | start, end = step * np.pi, (step + 1) * np.pi # time range 76 | # use sin predicts cos 77 | steps = np.linspace(start, end, TIME_STEP, dtype=np.float32, 78 | endpoint=False) # float32 for converting torch FloatTensor 79 | x_np = np.sin(steps) 80 | y_np = np.cos(steps) 81 | 82 | x = torch.from_numpy(x_np[np.newaxis, :, np.newaxis]) # shape (batch, time_step, input_size) 83 | y = torch.from_numpy(y_np[np.newaxis, :, np.newaxis]) 84 | 85 | prediction, h_state = rnn(x, h_state) # rnn output 86 | # !! next step is important !! 87 | h_state = h_state.data # repack the hidden state, break the connection from last iteration 88 | 89 | loss = loss_func(prediction, y) # calculate loss 90 | optimizer.zero_grad() # clear gradients for this training step 91 | loss.backward() # backpropagation, compute gradients 92 | optimizer.step() # apply gradients 93 | 94 | # plotting 95 | plt.plot(steps, y_np.flatten(), 'r-') 96 | plt.plot(steps, prediction.data.numpy().flatten(), 'b-') 97 | plt.draw() 98 | plt.pause(0.05) 99 | 100 | plt.ioff() 101 | plt.show() 102 | -------------------------------------------------------------------------------- /CV_net/DeiT/datasets.py: -------------------------------------------------------------------------------- 1 | # !/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time: 2023/2/25 20:19 4 | # @Author: 'IReverser' 5 | # @ FileName: datasets.py 6 | import os 7 | 8 | from torchvision import datasets, transforms 9 | from torchvision.transforms import InterpolationMode 10 | from timm.data.constants import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD 11 | from timm.data import create_transform 12 | import warnings 13 | warnings.filterwarnings("ignore") 14 | 15 | 16 | def build_transform(is_train, args): 17 | resize_im = args.input_size > 32 18 | 19 | if is_train: 20 | # this should always dispatch to transforms_imagenet_train 21 | transform = create_transform(input_size=args.input_size, 22 | is_training=True, 23 | color_jitter=0.3, 24 | auto_augment='rand-m9-mstd0.5-inc1', 25 | interpolation='bicubic', 26 | re_prob=0.25, 27 | re_mode="pixel", 28 | re_count=1) 29 | # transform = transforms.Compose([transforms.RandomResizedCrop(224), 30 | # transforms.RandomHorizontalFlip(), 31 | # transforms.ToTensor(), 32 | # transforms.Normalize(IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD)]) 33 | if not resize_im: 34 | # replace RandomResizedCropAndInterpolation with RandomCrop 35 | transform.transforms[0] = transforms.RandomCrop(args.input_size, padding=4) 36 | 37 | return transform 38 | 39 | t = [] 40 | if resize_im: 41 | size = int(args.input_size / 0.875) # 224 / 0.875 = 256 42 | t.append(transforms.Resize(size, interpolation=InterpolationMode.BICUBIC)) # 3: InterpolationMode.BICUBIC 43 | t.append(transforms.CenterCrop(args.input_size)) 44 | 45 | t.append(transforms.ToTensor()) 46 | t.append(transforms.Normalize(IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD)) 47 | return transforms.Compose(t) 48 | 49 | 50 | def build_dataset(train, args): 51 | transform = build_transform(train, args) 52 | if args.data == "CIFAR10": 53 | dataset = datasets.CIFAR10(args.data_path, train=train, transform=transform) 54 | num_classes = 10 55 | elif args.data == "CIFAR100": 56 | dataset = datasets.CIFAR100(args.data_path, train=train, transform=transform) 57 | num_classes = 100 58 | elif args.data == 'IMNET': 59 | root = os.path.join(args.data_path, 'train' if train else 'val') 60 | dataset = datasets.ImageFolder(root, transform=transform) 61 | num_classes = 1000 62 | elif args.data == 'flowers': 63 | root = os.path.join(args.data_path, 'train' if train else 'val') 64 | dataset = datasets.ImageFolder(root, transform=transform) 65 | num_classes = 5 66 | return dataset, num_classes 67 | 68 | 69 | if __name__ == '__main__': 70 | import torch 71 | transform = transforms.Compose([transforms.RandomResizedCrop(224), 72 | transforms.RandomHorizontalFlip(), 73 | transforms.ToTensor(), 74 | transforms.Normalize(IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD)]) 75 | root = os.path.join("../datasets/flowers", 'train') 76 | dataset = datasets.ImageFolder(root, transform=transform) 77 | train_loader = torch.utils.data.DataLoader(dataset, batch_size=64, shuffle=True, 78 | num_workers=0, pin_memory=True, drop_last=True) 79 | for i, (im, target) in enumerate(train_loader): 80 | print(im.shape, target.shape) 81 | -------------------------------------------------------------------------------- /CV_net/pytorch-center-offical/model.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2021/9/20 15:11 4 | # @Author : 'IReverser' 5 | # @FileName: model.py 6 | from abc import ABC 7 | 8 | import torch 9 | import torch.nn as nn 10 | 11 | 12 | class TrunkNet(nn.Module, ABC): 13 | def __init__(self, num_classes=1000): 14 | super(TrunkNet, self).__init__() 15 | self.trunk = nn.Sequential( 16 | nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3, stride=1, padding=1), 17 | Basicblock(32, 64), 18 | self._make_resblock(64, 64, num_layers=1), 19 | Basicblock(64, 128), 20 | self._make_resblock(128, 128, num_layers=2), 21 | Basicblock(128, 256), 22 | self._make_resblock(256, 256, num_layers=5), 23 | Basicblock(256, 512), 24 | self._make_resblock(512, 512, num_layers=3), 25 | ) 26 | self.fc1 = nn.Linear(512 * 7 * 6, 512) # 512 * h * w = 25088 27 | # self.fc1 = nn.Linear(512 * 14 * 14, 512) # 512 * h * w = 25088 28 | 29 | self.classify = nn.Linear(512, num_classes) 30 | 31 | for m in self.modules(): 32 | if isinstance(m, nn.Conv2d): 33 | nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') 34 | 35 | def _make_resblock(self, in_channels, out_channels, num_layers): 36 | block = [] 37 | for _ in range(num_layers): 38 | block.append(Bottleneck(in_channels, out_channels)) 39 | return nn.Sequential(*block) 40 | 41 | def forward(self, x): 42 | x = self.trunk(x) 43 | # print(x.shape) 44 | x = torch.flatten(x, 1) 45 | x1 = self.fc1(x) 46 | x2 = self.classify(x1) 47 | return x1, x2 48 | 49 | 50 | class Basicblock(nn.Module): 51 | def __init__(self, in_channels, out_channels): 52 | super(Basicblock, self).__init__() 53 | self.conv = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=1, padding=1) 54 | self.prelu = nn.PReLU() 55 | self.maxpool = nn.MaxPool2d(kernel_size=2, stride=2) 56 | 57 | def forward(self, x): 58 | x = self.conv(x) 59 | x = self.maxpool(self.prelu(x)) 60 | return x 61 | 62 | 63 | class Bottleneck(nn.Module): 64 | def __init__(self, in_channels, out_channels): 65 | super(Bottleneck, self).__init__() 66 | self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=1, padding=1) 67 | self.prelu = nn.PReLU() 68 | self.conv2 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=1, padding=1) 69 | 70 | def forward(self, x): 71 | x = self.conv1(x) 72 | x = self.prelu(x) 73 | x = self.conv2(x) 74 | return x 75 | 76 | 77 | class Branch(nn.Module, ABC): 78 | def __init__(self, num_classes=1000): 79 | super(Branch, self).__init__() 80 | self.fc1 = nn.Linear(512, 512, bias=True) 81 | self.classify = nn.Linear(512, num_classes, bias=True) 82 | 83 | def forward(self, fea): 84 | fea = self.fc1(fea) 85 | # print(x.shape) 86 | fea_cm = self.classify(fea) 87 | return fea, fea_cm 88 | 89 | 90 | __factory = { 91 | 'dcr-trunk': TrunkNet, 92 | } 93 | 94 | 95 | def create(name, num_classes): 96 | if name not in __factory.keys(): 97 | raise KeyError("Unknown model: {}".format(name)) 98 | return __factory[name](num_classes) 99 | 100 | 101 | if __name__ == '__main__': 102 | pass 103 | 104 | # test 105 | # x = torch.rand((1, 3, 224, 224)) 106 | # num_classes = 10 107 | # net = TrunkNet(num_classes=num_classes) 108 | # print(net) 109 | # [center_y, soft_x] = net(x) 110 | # print(center_y.shape, soft_x.shape) 111 | -------------------------------------------------------------------------------- /CV_net/WRN/WRN_cifar.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2022/2/11 23:05 4 | # @Author : '' 5 | # @FileName: WRN_cifar.py 6 | import math 7 | from torch import nn 8 | 9 | 10 | def conv3x3(in_planes, out_planes, stride=1): 11 | """ 3x3 convolution with padding """ 12 | return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride, padding=1, bias=False) 13 | 14 | 15 | class BasicBlock(nn.Module): 16 | expansion = 1 17 | 18 | def __init__(self, inplanes, planes, stride=1, downsample=None): 19 | super(BasicBlock, self).__init__() 20 | self.conv1 = conv3x3(inplanes, planes, stride) 21 | self.bn1 = nn.BatchNorm2d(planes) 22 | self.relu = nn.ReLU(inplace=True) 23 | self.conv2 = conv3x3(planes, planes) 24 | self.bn2 = nn.BatchNorm2d(planes) 25 | self.downsample = downsample 26 | self.stride = stride 27 | 28 | def forward(self, x): 29 | residual = x 30 | 31 | out = self.conv1(x) 32 | out = self.bn1(out) 33 | out = self.relu(out) 34 | 35 | out = self.conv2(out) 36 | out = self.bn2(out) 37 | 38 | if self.downsample is not None: 39 | residual = self.downsample(x) 40 | 41 | out += residual 42 | out = self.relu(out) 43 | 44 | return out 45 | 46 | 47 | class WRN(nn.Module): 48 | 49 | def __init__(self, block, layers, wide, num_classes=10): 50 | super(WRN, self).__init__() 51 | self.inplanes = 16 52 | self.conv1 = nn.Conv2d(3, 16, kernel_size=3, stride=1, padding=1, bias=False) 53 | self.bn1 = nn.BatchNorm2d(16) 54 | self.relu = nn.ReLU(inplace=True) 55 | self.layer1 = self._make_layer(block, 16*wide, layers[0]) 56 | self.layer2 = self._make_layer(block, 32*wide, layers[1], stride=2) 57 | self.layer3 = self._make_layer(block, 64*wide, layers[2], stride=2) 58 | self.avgpool = nn.AvgPool2d(8, stride=1) 59 | self.fc = nn.Linear(64*block.expansion*wide, num_classes) 60 | 61 | for m in self.modules(): 62 | if isinstance(m, nn.Conv2d): 63 | n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels 64 | m.weight.data.normal_(0, math.sqrt(2. / n)) 65 | elif isinstance(m, nn.BatchNorm2d): 66 | m.weight.data.fill_(1) 67 | m.bias.data.zero_() 68 | 69 | def _make_layer(self, block, planes, blocks, stride=1): 70 | downsample = None 71 | if stride != 1 or self.inplanes != planes * block.expansion: 72 | downsample = nn.Sequential( 73 | nn.Conv2d(self.inplanes, planes * block.expansion, kernel_size=1, stride=stride, bias=False), 74 | nn.BatchNorm2d(planes * block.expansion) 75 | ) 76 | 77 | layers = nn.ModuleList() 78 | layers.append(block(self.inplanes, planes, stride, downsample)) 79 | self.inplanes = planes * block.expansion 80 | for _ in range(1, int(blocks)): 81 | layers.append(block(self.inplanes, planes)) 82 | 83 | return nn.Sequential(*layers) 84 | 85 | def forward(self, x): 86 | x = self.conv1(x) 87 | x = self.bn1(x) 88 | x = self.relu(x) 89 | 90 | x = self.layer1(x) 91 | x = self.layer2(x) 92 | x = self.layer3(x) 93 | 94 | x = self.avgpool(x) 95 | x = x.view(x.size(0), -1) 96 | x = self.fc(x) 97 | 98 | return x 99 | 100 | 101 | def wide_resnet_cifar(depth, width, **kwargs): 102 | assert (depth - 2) % 6 == 0 103 | n = (depth - 2) / 6 104 | return WRN(BasicBlock, [n, n, n], wide=width, **kwargs) 105 | 106 | 107 | # if __name__ == '__main__': 108 | # import torch 109 | # net = wide_resnet_cifar(20, 10) 110 | # y = net(torch.randn(1, 3, 32, 32)) 111 | # print(isinstance(net, WRN)) 112 | # print(y.size()) 113 | -------------------------------------------------------------------------------- /CV_net/DenseNet/model.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2021/9/19 17:22 4 | # @Author : '' 5 | # @FileName: model.py 6 | import torch 7 | import torch.nn as nn 8 | 9 | 10 | def conv_block(in_channel, out_channel): 11 | layer = nn.Sequential( 12 | nn.BatchNorm2d(in_channel), 13 | nn.ReLU(inplace=True), 14 | nn.Conv2d(in_channel, out_channel, kernel_size=3, padding=1, bias=False)) 15 | return layer 16 | 17 | 18 | class DenseBlock(nn.Module): 19 | def __init__(self, in_channel, growth_rate, num_layers): 20 | super(DenseBlock, self).__init__() 21 | block = [] 22 | channel = in_channel 23 | for i in range(num_layers): 24 | block.append(conv_block(channel, growth_rate)) 25 | channel += growth_rate 26 | 27 | self.net = nn.Sequential(*block) 28 | 29 | def forward(self, x): 30 | for layer in self.net: 31 | out = layer(x) 32 | x = torch.cat((out, x), dim=1) # concat from channels 33 | return x 34 | 35 | 36 | def transition_layer(in_channel, out_channel): 37 | trans_layer = nn.Sequential( 38 | nn.BatchNorm2d(in_channel), 39 | nn.ReLU(inplace=True), 40 | nn.Conv2d(in_channel, out_channel, kernel_size=1), 41 | nn.AvgPool2d(2, 2)) 42 | return trans_layer 43 | 44 | 45 | class DenseNet(nn.Module): 46 | def __init__(self, in_channel, block_layers, num_classes, growth_rate=32): 47 | super(DenseNet, self).__init__() 48 | self.block1 = nn.Sequential( 49 | nn.Conv2d(in_channel, out_channels=64, kernel_size=7, stride=2, padding=3), 50 | nn.BatchNorm2d(64), 51 | nn.ReLU(inplace=True), 52 | nn.MaxPool2d(3, stride=2, padding=1)) 53 | self.denseblock1 = self._make_dense_block(64, growth_rate, num=block_layers[0]) 54 | self.translayer1 = self._make_transition_layer(256) 55 | self.denseblock2 = self._make_dense_block(128, growth_rate, num=block_layers[1]) 56 | self.translayer2 = self._make_transition_layer(512) 57 | self.denseblock3 = self._make_dense_block(256, growth_rate, num=block_layers[2]) 58 | self.translayer3 = self._make_transition_layer(1024) 59 | self.denseblock4 = self._make_dense_block(512, growth_rate, num=block_layers[3]) 60 | self.global_average = nn.Sequential( 61 | nn.BatchNorm2d(1024), 62 | nn.ReLU(inplace=True), 63 | nn.AdaptiveAvgPool2d((1, 1))) 64 | 65 | self.classifier = nn.Linear(1024, num_classes) 66 | 67 | def forward(self, x): 68 | x = self.block1(x) 69 | x = self.denseblock1(x) 70 | x = self.translayer1(x) 71 | x = self.denseblock2(x) 72 | x = self.translayer2(x) 73 | x = self.denseblock3(x) 74 | x = self.translayer3(x) 75 | x = self.denseblock4(x) 76 | x = self.global_average(x) 77 | x = x.view(x.shape[0], -1) 78 | x = self.classifier(x) 79 | 80 | return x 81 | 82 | def _make_dense_block(self, channels, growth_rate, num): 83 | block = [DenseBlock(channels, growth_rate, num)] 84 | channels += num * growth_rate 85 | return nn.Sequential(*block) 86 | 87 | def _make_transition_layer(self, channels): 88 | block = [transition_layer(channels, channels // 2)] 89 | return nn.Sequential(*block) 90 | 91 | 92 | def DenseNet121(num_classes=1000): 93 | return DenseNet(3, [6, 12, 24, 16], num_classes=num_classes, growth_rate=32) 94 | 95 | 96 | def DenseNet169(num_classes=1000): 97 | return DenseNet(3, [6, 12, 32, 32], num_classes=num_classes, growth_rate=32) 98 | 99 | 100 | def DenseNet201(num_classes=1000): 101 | return DenseNet(3, [6, 12, 48, 32], num_classes=num_classes, growth_rate=32) 102 | 103 | 104 | def DenseNet161(num_classes=1000): 105 | return DenseNet(3, [6, 12, 36, 24], num_classes=num_classes, growth_rate=48) 106 | 107 | -------------------------------------------------------------------------------- /mofan_python/10_RNN_classifier.py: -------------------------------------------------------------------------------- 1 | import torch 2 | from torch import nn 3 | import torchvision.datasets as dsets 4 | import torchvision.transforms as transforms 5 | import matplotlib.pyplot as plt 6 | 7 | # torch.manual_seed(1) # reproducible 8 | 9 | # Hyper Parameters 10 | EPOCH = 1 # train the training data n times, to save time, we just train 1 epoch 11 | BATCH_SIZE = 64 12 | TIME_STEP = 28 # rnn time step / image height 13 | INPUT_SIZE = 28 # rnn input size / image width 14 | LR = 0.01333 # learning rate 15 | DOWNLOAD_MNIST = True # set to True if haven't download the data 16 | 17 | 18 | # Mnist digital dataset 19 | train_data = dsets.MNIST( 20 | root='./mnist/', 21 | train=True, # this is training data 22 | transform=transforms.ToTensor(), # Converts a PIL.Image or numpy.ndarray to 23 | # torch.FloatTensor of shape (C x H x W) and normalize in the range [0.0, 1.0] 24 | download=DOWNLOAD_MNIST, # download it if you don't have it 25 | ) 26 | 27 | # plot one example 28 | # print(train_data.train_data.size()) # (60000, 28, 28) 29 | # print(train_data.train_labels.size()) # (60000) 30 | # plt.imshow(train_data.train_data[0].numpy(), cmap='gray') 31 | # plt.title('%i' % train_data.train_labels[0]) 32 | # plt.show() 33 | 34 | # Data Loader for easy mini-batch return in training 35 | train_loader = torch.utils.data.DataLoader(dataset=train_data, batch_size=BATCH_SIZE, shuffle=True) 36 | 37 | # convert test data into Variable, pick 2000 samples to speed up testing 38 | test_data = dsets.MNIST(root='./mnist/', train=False, transform=transforms.ToTensor()) 39 | test_x = test_data.test_data.type(torch.FloatTensor)[:2000]/255. # shape (2000, 28, 28) value in range(0,1) 40 | test_y = test_data.test_labels.numpy()[:2000] # covert to numpy array 41 | 42 | 43 | class RNN(nn.Module): 44 | def __init__(self): 45 | super(RNN, self).__init__() 46 | 47 | self.rnn = nn.LSTM( # if use nn.RNN(), it hardly learns 48 | input_size=INPUT_SIZE, 49 | hidden_size=64, # rnn hidden unit 50 | num_layers=1, # number of rnn layer 51 | batch_first=True, # input & output will has batch size as 1s dimension. e.g. (batch, time_step, input_size) 52 | ) 53 | 54 | self.out = nn.Linear(64, 10) 55 | 56 | def forward(self, x): 57 | # x shape (batch, time_step, input_size) 58 | # r_out shape (batch, time_step, output_size) 59 | # h_n shape (n_layers, batch, hidden_size) 60 | # h_c shape (n_layers, batch, hidden_size) 61 | r_out, (h_n, h_c) = self.rnn(x, None) # None represents zero initial hidden state 62 | 63 | # choose r_out at the last time step 64 | out = self.out(r_out[:, -1, :]) 65 | return out 66 | 67 | 68 | rnn = RNN() 69 | print(rnn) 70 | 71 | optimizer = torch.optim.Adam(rnn.parameters(), lr=LR) # optimize all cnn parameters 72 | loss_func = nn.CrossEntropyLoss() # the target label is not one-hotted 73 | 74 | # training and testing 75 | for epoch in range(EPOCH): 76 | for step, (b_x, b_y) in enumerate(train_loader): # gives batch data 77 | b_x = b_x.view(-1, 28, 28) # reshape x to (batch, time_step, input_size) 78 | 79 | output = rnn(b_x) # rnn output 80 | loss = loss_func(output, b_y) # cross entropy loss 81 | optimizer.zero_grad() # clear gradients for this training step 82 | loss.backward() # backpropagation, compute gradients 83 | optimizer.step() # apply gradients 84 | 85 | if step % 50 == 0: 86 | test_output = rnn(test_x) # (samples, time_step, input_size) 87 | pred_y = torch.max(test_output, 1)[1].data.numpy() 88 | accuracy = float((pred_y == test_y).astype(int).sum()) / float(test_y.size) 89 | print('Epoch: ', epoch, '| train loss: %.4f' % loss.data.numpy(), '| test accuracy: %.2f' % accuracy) 90 | 91 | # print 10 predictions from test data 92 | test_output = rnn(test_x[:10].view(-1, 28, 28)) 93 | pred_y = torch.max(test_output, 1)[1].data.numpy() 94 | print(pred_y, 'prediction number') 95 | print(test_y[:10], 'real number') 96 | -------------------------------------------------------------------------------- /CV_net/SPPNet/utils/misc.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2022/2/10 12:43 4 | # @Author : '' 5 | # @FileName: misc.py 6 | from __future__ import absolute_import 7 | import os 8 | import sys 9 | import torch 10 | import torch.backends.cudnn 11 | 12 | 13 | def setup_seed(an_int): 14 | torch.manual_seed(an_int) 15 | torch.cuda.manual_seed(an_int) 16 | torch.cuda.manual_seed_all(an_int) 17 | torch.backends.cudnn.deterministic = True 18 | torch.backends.cudnn.benchmark = False 19 | 20 | 21 | def mkdir_file(path): 22 | if not os.path.exists(path): 23 | os.makedirs(path) 24 | 25 | 26 | def format_time(seconds): 27 | days = int(seconds / (3600 * 24)) 28 | seconds = seconds - days * 3600 * 24 29 | hours = int(seconds / 3600) 30 | seconds = seconds - hours * 3600 31 | minutes = int(seconds / 60) 32 | seconds = seconds - minutes * 60 33 | secondsf = int(seconds) 34 | seconds = seconds - secondsf 35 | millis = int(seconds * 1000) 36 | 37 | f = '' 38 | i = 1 39 | if days > 0: 40 | f += str(days) + 'D' 41 | i += 1 42 | if hours > 0 and i <= 2: 43 | f += str(hours) + 'h' 44 | i += 1 45 | if minutes > 0 and i <= 2: 46 | f += str(minutes) + 'm' 47 | if secondsf > 0 and i <= 2: 48 | f += str(millis) + 'ms' 49 | i += 1 50 | if f == '': 51 | f = '0ms' 52 | return f 53 | 54 | 55 | class Logger(object): 56 | """ 57 | Write console output to external text file. 58 | 59 | Code imported from https://github.com/Cysu/open-reid/blob/master/reid/utils/logging.py. 60 | """ 61 | 62 | def __init__(self, args, fpath=None): 63 | self.console = sys.stdout 64 | self.file = None 65 | if fpath is not None: 66 | mkdir_file(os.path.dirname(fpath)) 67 | self.file = open(fpath, 'w') 68 | # write model configure at the top of file 69 | for arg in vars(args): 70 | self.file.write(arg + ': ' + str(getattr(args, arg)) + '\n') 71 | self.file.flush() 72 | 73 | def __del__(self): 74 | self.close() 75 | 76 | def __enter__(self): 77 | pass 78 | 79 | def __exit__(self, *args): 80 | self.close() 81 | 82 | def write(self, msg): 83 | self.console.write(msg) 84 | if self.file is not None: 85 | self.file.write(msg) 86 | self.file.flush() 87 | 88 | def write_dict(self, dict): 89 | if self.file is not None: 90 | for k in dict: 91 | self.file.write(k + ': ' + dict[k] + ' ') 92 | self.file.write('\n') 93 | self.file.flush() 94 | 95 | def flush(self): 96 | self.console.flush() 97 | if self.file is not None: 98 | self.file.flush() 99 | os.fsync(self.file.fileno()) 100 | 101 | def close(self): 102 | self.console.close() 103 | if self.file is not None: 104 | self.file.close() 105 | 106 | 107 | class AverageMeter(object): 108 | """Computes and stores the average and current value 109 | Imported from https://github.com/pytorch/examples/blob/master/imagenet/main.py#L247-L262 110 | """ 111 | def __init__(self): 112 | self.reset() 113 | 114 | def reset(self): 115 | self.val = 0 116 | self.avg = 0 117 | self.sum = 0 118 | self.count = 0 119 | 120 | def update(self, val, n=1): 121 | self.val = val 122 | self.sum += val * n 123 | self.count += n 124 | self.avg = self.sum / self.count 125 | 126 | 127 | def save_checkpoint(state, filename='checkpoint.pt'): 128 | torch.save(state, filename) 129 | 130 | 131 | @torch.no_grad() 132 | def accuracy(output, target, topk=(1, )): 133 | """""" 134 | maxk = max(topk) 135 | batch_size = target.size(0) 136 | 137 | _, pred = output.topk(maxk, dim=1, largest=True, sorted=True) 138 | pred = pred.t() 139 | correct = pred.eq(target.view(1, -1).expand_as(pred)) 140 | 141 | res = [] 142 | for k in topk: 143 | correct_k = correct[:k].reshape(-1).float().sum(axis=0, keepdim=True) 144 | res.append(correct_k.mul_(100.0 / batch_size)) # element-wise multiply in matrices 145 | return res 146 | -------------------------------------------------------------------------------- /CV_net/WRN/utils/misc.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2022/2/10 12:43 4 | # @Author : '' 5 | # @FileName: misc.py 6 | from __future__ import absolute_import 7 | import os 8 | import sys 9 | import torch 10 | import torch.backends.cudnn 11 | 12 | 13 | def setup_seed(an_int): 14 | torch.manual_seed(an_int) 15 | torch.cuda.manual_seed(an_int) 16 | torch.cuda.manual_seed_all(an_int) 17 | torch.backends.cudnn.deterministic = True 18 | torch.backends.cudnn.benchmark = False 19 | 20 | 21 | def mkdir_file(path): 22 | if not os.path.exists(path): 23 | os.makedirs(path) 24 | 25 | 26 | def format_time(seconds): 27 | days = int(seconds / (3600 * 24)) 28 | seconds = seconds - days * 3600 * 24 29 | hours = int(seconds / 3600) 30 | seconds = seconds - hours * 3600 31 | minutes = int(seconds / 60) 32 | seconds = seconds - minutes * 60 33 | secondsf = int(seconds) 34 | seconds = seconds - secondsf 35 | millis = int(seconds * 1000) 36 | 37 | f = '' 38 | i = 1 39 | if days > 0: 40 | f += str(days) + 'D' 41 | i += 1 42 | if hours > 0 and i <= 2: 43 | f += str(hours) + 'h' 44 | i += 1 45 | if minutes > 0 and i <= 2: 46 | f += str(minutes) + 'm' 47 | if secondsf > 0 and i <= 2: 48 | f += str(millis) + 'ms' 49 | i += 1 50 | if f == '': 51 | f = '0ms' 52 | return f 53 | 54 | 55 | class Logger(object): 56 | """ 57 | Write console output to external text file. 58 | 59 | Code imported from https://github.com/Cysu/open-reid/blob/master/reid/utils/logging.py. 60 | """ 61 | 62 | def __init__(self, args, fpath=None): 63 | self.console = sys.stdout 64 | self.file = None 65 | if fpath is not None: 66 | mkdir_file(os.path.dirname(fpath)) 67 | self.file = open(fpath, 'w') 68 | # write model configure at the top of file 69 | for arg in vars(args): 70 | self.file.write(arg + ': ' + str(getattr(args, arg)) + '\n') 71 | self.file.flush() 72 | 73 | def __del__(self): 74 | self.close() 75 | 76 | def __enter__(self): 77 | pass 78 | 79 | def __exit__(self, *args): 80 | self.close() 81 | 82 | def write(self, msg): 83 | self.console.write(msg) 84 | if self.file is not None: 85 | self.file.write(msg) 86 | self.file.flush() 87 | 88 | def write_dict(self, dict): 89 | if self.file is not None: 90 | for k in dict: 91 | self.file.write(k + ': ' + dict[k] + ' ') 92 | self.file.write('\n') 93 | self.file.flush() 94 | 95 | def flush(self): 96 | self.console.flush() 97 | if self.file is not None: 98 | self.file.flush() 99 | os.fsync(self.file.fileno()) 100 | 101 | def close(self): 102 | self.console.close() 103 | if self.file is not None: 104 | self.file.close() 105 | 106 | 107 | class AverageMeter(object): 108 | """Computes and stores the average and current value 109 | Imported from https://github.com/pytorch/examples/blob/master/imagenet/main.py#L247-L262 110 | """ 111 | def __init__(self): 112 | self.reset() 113 | 114 | def reset(self): 115 | self.val = 0 116 | self.avg = 0 117 | self.sum = 0 118 | self.count = 0 119 | 120 | def update(self, val, n=1): 121 | self.val = val 122 | self.sum += val * n 123 | self.count += n 124 | self.avg = self.sum / self.count 125 | 126 | 127 | def save_checkpoint(state, filename='checkpoint.pt'): 128 | torch.save(state, filename) 129 | 130 | 131 | @torch.no_grad() 132 | def accuracy(output, target, topk=(1, )): 133 | """""" 134 | maxk = max(topk) 135 | batch_size = target.size(0) 136 | 137 | _, pred = output.topk(maxk, dim=1, largest=True, sorted=True) 138 | pred = pred.t() 139 | correct = pred.eq(target.view(1, -1).expand_as(pred)) 140 | 141 | res = [] 142 | for k in topk: 143 | correct_k = correct[:k].reshape(-1).float().sum(axis=0, keepdim=True) 144 | res.append(correct_k.mul_(100.0 / batch_size)) # element-wise multiply in matrices 145 | return res 146 | -------------------------------------------------------------------------------- /CV_net/NonLocalNN/utils/misc.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2022/2/10 12:43 4 | # @Author : '' 5 | # @FileName: misc.py 6 | from __future__ import absolute_import 7 | import os 8 | import sys 9 | import torch 10 | import torch.backends.cudnn 11 | 12 | 13 | def setup_seed(an_int): 14 | torch.manual_seed(an_int) 15 | torch.cuda.manual_seed(an_int) 16 | torch.cuda.manual_seed_all(an_int) 17 | torch.backends.cudnn.deterministic = True 18 | torch.backends.cudnn.benchmark = False 19 | 20 | 21 | def mkdir_file(path): 22 | if not os.path.exists(path): 23 | os.makedirs(path) 24 | 25 | 26 | def format_time(seconds): 27 | days = int(seconds / (3600 * 24)) 28 | seconds = seconds - days * 3600 * 24 29 | hours = int(seconds / 3600) 30 | seconds = seconds - hours * 3600 31 | minutes = int(seconds / 60) 32 | seconds = seconds - minutes * 60 33 | secondsf = int(seconds) 34 | seconds = seconds - secondsf 35 | millis = int(seconds * 1000) 36 | 37 | f = '' 38 | i = 1 39 | if days > 0: 40 | f += str(days) + 'D' 41 | i += 1 42 | if hours > 0 and i <= 2: 43 | f += str(hours) + 'h' 44 | i += 1 45 | if minutes > 0 and i <= 2: 46 | f += str(minutes) + 'm' 47 | if secondsf > 0 and i <= 2: 48 | f += str(millis) + 'ms' 49 | i += 1 50 | if f == '': 51 | f = '0ms' 52 | return f 53 | 54 | 55 | class Logger(object): 56 | """ 57 | Write console output to external text file. 58 | 59 | Code imported from https://github.com/Cysu/open-reid/blob/master/reid/utils/logging.py. 60 | """ 61 | 62 | def __init__(self, args, fpath=None): 63 | self.console = sys.stdout 64 | self.file = None 65 | if fpath is not None: 66 | mkdir_file(os.path.dirname(fpath)) 67 | self.file = open(fpath, 'w') 68 | # write model configure at the top of file 69 | for arg in vars(args): 70 | self.file.write(arg + ': ' + str(getattr(args, arg)) + '\n') 71 | self.file.flush() 72 | 73 | def __del__(self): 74 | self.close() 75 | 76 | def __enter__(self): 77 | pass 78 | 79 | def __exit__(self, *args): 80 | self.close() 81 | 82 | def write(self, msg): 83 | self.console.write(msg) 84 | if self.file is not None: 85 | self.file.write(msg) 86 | self.file.flush() 87 | 88 | def write_dict(self, dict): 89 | if self.file is not None: 90 | for k in dict: 91 | self.file.write(k + ': ' + dict[k] + ' ') 92 | self.file.write('\n') 93 | self.file.flush() 94 | 95 | def flush(self): 96 | self.console.flush() 97 | if self.file is not None: 98 | self.file.flush() 99 | os.fsync(self.file.fileno()) 100 | 101 | def close(self): 102 | self.console.close() 103 | if self.file is not None: 104 | self.file.close() 105 | 106 | 107 | class AverageMeter(object): 108 | """Computes and stores the average and current value 109 | Imported from https://github.com/pytorch/examples/blob/master/imagenet/main.py#L247-L262 110 | """ 111 | def __init__(self): 112 | self.reset() 113 | 114 | def reset(self): 115 | self.val = 0 116 | self.avg = 0 117 | self.sum = 0 118 | self.count = 0 119 | 120 | def update(self, val, n=1): 121 | self.val = val 122 | self.sum += val * n 123 | self.count += n 124 | self.avg = self.sum / self.count 125 | 126 | 127 | def save_checkpoint(state, filename='checkpoint.pt'): 128 | torch.save(state, filename) 129 | 130 | 131 | @torch.no_grad() 132 | def accuracy(output, target, topk=(1, )): 133 | """""" 134 | maxk = max(topk) 135 | batch_size = target.size(0) 136 | 137 | _, pred = output.topk(maxk, dim=1, largest=True, sorted=True) 138 | pred = pred.t() 139 | correct = pred.eq(target.view(1, -1).expand_as(pred)) 140 | 141 | res = [] 142 | for k in topk: 143 | correct_k = correct[:k].reshape(-1).float().sum(axis=0, keepdim=True) 144 | res.append(correct_k.mul_(100.0 / batch_size)) # element-wise multiply in matrices 145 | return res 146 | -------------------------------------------------------------------------------- /CV_net/PreActResNet/utils/misc.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2022/2/10 12:43 4 | # @Author : '' 5 | # @FileName: misc.py 6 | from __future__ import absolute_import 7 | import os 8 | import sys 9 | import torch 10 | import torch.backends.cudnn 11 | 12 | 13 | def setup_seed(an_int): 14 | torch.manual_seed(an_int) 15 | torch.cuda.manual_seed(an_int) 16 | torch.cuda.manual_seed_all(an_int) 17 | torch.backends.cudnn.deterministic = True 18 | torch.backends.cudnn.benchmark = False 19 | 20 | 21 | def mkdir_file(path): 22 | if not os.path.exists(path): 23 | os.makedirs(path) 24 | 25 | 26 | def format_time(seconds): 27 | days = int(seconds / (3600 * 24)) 28 | seconds = seconds - days * 3600 * 24 29 | hours = int(seconds / 3600) 30 | seconds = seconds - hours * 3600 31 | minutes = int(seconds / 60) 32 | seconds = seconds - minutes * 60 33 | secondsf = int(seconds) 34 | seconds = seconds - secondsf 35 | millis = int(seconds * 1000) 36 | 37 | f = '' 38 | i = 1 39 | if days > 0: 40 | f += str(days) + 'D' 41 | i += 1 42 | if hours > 0 and i <= 2: 43 | f += str(hours) + 'h' 44 | i += 1 45 | if minutes > 0 and i <= 2: 46 | f += str(minutes) + 'm' 47 | if secondsf > 0 and i <= 2: 48 | f += str(millis) + 'ms' 49 | i += 1 50 | if f == '': 51 | f = '0ms' 52 | return f 53 | 54 | 55 | class Logger(object): 56 | """ 57 | Write console output to external text file. 58 | 59 | Code imported from https://github.com/Cysu/open-reid/blob/master/reid/utils/logging.py. 60 | """ 61 | 62 | def __init__(self, args, fpath=None): 63 | self.console = sys.stdout 64 | self.file = None 65 | if fpath is not None: 66 | mkdir_file(os.path.dirname(fpath)) 67 | self.file = open(fpath, 'w') 68 | # write model configure at the top of file 69 | for arg in vars(args): 70 | self.file.write(arg + ': ' + str(getattr(args, arg)) + '\n') 71 | self.file.flush() 72 | 73 | def __del__(self): 74 | self.close() 75 | 76 | def __enter__(self): 77 | pass 78 | 79 | def __exit__(self, *args): 80 | self.close() 81 | 82 | def write(self, msg): 83 | self.console.write(msg) 84 | if self.file is not None: 85 | self.file.write(msg) 86 | self.file.flush() 87 | 88 | def write_dict(self, dict): 89 | if self.file is not None: 90 | for k in dict: 91 | self.file.write(k + ': ' + dict[k] + ' ') 92 | self.file.write('\n') 93 | self.file.flush() 94 | 95 | def flush(self): 96 | self.console.flush() 97 | if self.file is not None: 98 | self.file.flush() 99 | os.fsync(self.file.fileno()) 100 | 101 | def close(self): 102 | self.console.close() 103 | if self.file is not None: 104 | self.file.close() 105 | 106 | 107 | class AverageMeter(object): 108 | """Computes and stores the average and current value 109 | Imported from https://github.com/pytorch/examples/blob/master/imagenet/main.py#L247-L262 110 | """ 111 | def __init__(self): 112 | self.reset() 113 | 114 | def reset(self): 115 | self.val = 0 116 | self.avg = 0 117 | self.sum = 0 118 | self.count = 0 119 | 120 | def update(self, val, n=1): 121 | self.val = val 122 | self.sum += val * n 123 | self.count += n 124 | self.avg = self.sum / self.count 125 | 126 | 127 | def save_checkpoint(state, filename='checkpoint.pt'): 128 | torch.save(state, filename) 129 | 130 | 131 | @torch.no_grad() 132 | def accuracy(output, target, topk=(1, )): 133 | """""" 134 | maxk = max(topk) 135 | batch_size = target.size(0) 136 | 137 | _, pred = output.topk(maxk, dim=1, largest=True, sorted=True) 138 | pred = pred.t() 139 | correct = pred.eq(target.view(1, -1).expand_as(pred)) 140 | 141 | res = [] 142 | for k in topk: 143 | correct_k = correct[:k].reshape(-1).float().sum(axis=0, keepdim=True) 144 | res.append(correct_k.mul_(100.0 / batch_size)) # element-wise multiply in matrices 145 | return res 146 | -------------------------------------------------------------------------------- /CV_net/VanillaNet/utils/misc.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2022/2/10 12:43 4 | # @Author : '' 5 | # @FileName: misc.py 6 | from __future__ import absolute_import 7 | import os 8 | import sys 9 | import torch 10 | import torch.backends.cudnn 11 | 12 | 13 | def setup_seed(an_int): 14 | torch.manual_seed(an_int) 15 | torch.cuda.manual_seed(an_int) 16 | torch.cuda.manual_seed_all(an_int) 17 | torch.backends.cudnn.deterministic = True 18 | torch.backends.cudnn.benchmark = False 19 | 20 | 21 | def mkdir_file(path): 22 | if not os.path.exists(path): 23 | os.makedirs(path) 24 | 25 | 26 | def format_time(seconds): 27 | days = int(seconds / (3600 * 24)) 28 | seconds = seconds - days * 3600 * 24 29 | hours = int(seconds / 3600) 30 | seconds = seconds - hours * 3600 31 | minutes = int(seconds / 60) 32 | seconds = seconds - minutes * 60 33 | secondsf = int(seconds) 34 | seconds = seconds - secondsf 35 | millis = int(seconds * 1000) 36 | 37 | f = '' 38 | i = 1 39 | if days > 0: 40 | f += str(days) + 'D' 41 | i += 1 42 | if hours > 0 and i <= 2: 43 | f += str(hours) + 'h' 44 | i += 1 45 | if minutes > 0 and i <= 2: 46 | f += str(minutes) + 'm' 47 | if secondsf > 0 and i <= 2: 48 | f += str(millis) + 'ms' 49 | i += 1 50 | if f == '': 51 | f = '0ms' 52 | return f 53 | 54 | 55 | class Logger(object): 56 | """ 57 | Write console output to external text file. 58 | 59 | Code imported from https://github.com/Cysu/open-reid/blob/master/reid/utils/logging.py. 60 | """ 61 | 62 | def __init__(self, args, fpath=None): 63 | self.console = sys.stdout 64 | self.file = None 65 | if fpath is not None: 66 | mkdir_file(os.path.dirname(fpath)) 67 | self.file = open(fpath, 'w') 68 | # write model configure at the top of file 69 | for arg in vars(args): 70 | self.file.write(arg + ': ' + str(getattr(args, arg)) + '\n') 71 | self.file.flush() 72 | 73 | def __del__(self): 74 | self.close() 75 | 76 | def __enter__(self): 77 | pass 78 | 79 | def __exit__(self, *args): 80 | self.close() 81 | 82 | def write(self, msg): 83 | self.console.write(msg) 84 | if self.file is not None: 85 | self.file.write(msg) 86 | self.file.flush() 87 | 88 | def write_dict(self, dict): 89 | if self.file is not None: 90 | for k in dict: 91 | self.file.write(k + ': ' + dict[k] + ' ') 92 | self.file.write('\n') 93 | self.file.flush() 94 | 95 | def flush(self): 96 | self.console.flush() 97 | if self.file is not None: 98 | self.file.flush() 99 | os.fsync(self.file.fileno()) 100 | 101 | def close(self): 102 | self.console.close() 103 | if self.file is not None: 104 | self.file.close() 105 | 106 | 107 | class AverageMeter(object): 108 | """Computes and stores the average and current value 109 | Imported from https://github.com/pytorch/examples/blob/master/imagenet/main.py#L247-L262 110 | """ 111 | def __init__(self): 112 | self.reset() 113 | 114 | def reset(self): 115 | self.val = 0 116 | self.avg = 0 117 | self.sum = 0 118 | self.count = 0 119 | 120 | def update(self, val, n=1): 121 | self.val = val 122 | self.sum += val * n 123 | self.count += n 124 | self.avg = self.sum / self.count 125 | 126 | 127 | def save_checkpoint(state, filename='checkpoint.pt'): 128 | torch.save(state, filename) 129 | 130 | 131 | @torch.no_grad() 132 | def accuracy(output, target, topk=(1, )): 133 | """""" 134 | maxk = max(topk) 135 | batch_size = target.size(0) 136 | 137 | _, pred = output.topk(maxk, dim=1, largest=True, sorted=True) 138 | pred = pred.t() 139 | correct = pred.eq(target.view(1, -1).expand_as(pred)) 140 | 141 | res = [] 142 | for k in topk: 143 | correct_k = correct[:k].reshape(-1).float().sum(axis=0, keepdim=True) 144 | res.append(correct_k.mul_(100.0 / batch_size)) # element-wise multiply in matrices 145 | return res 146 | -------------------------------------------------------------------------------- /CV_net/vision_transformer/utils/misc.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2022/2/10 12:43 4 | # @Author : '' 5 | # @FileName: misc.py 6 | from __future__ import absolute_import 7 | import os 8 | import sys 9 | import torch 10 | import torch.backends.cudnn 11 | 12 | 13 | def setup_seed(an_int): 14 | torch.manual_seed(an_int) 15 | torch.cuda.manual_seed(an_int) 16 | torch.cuda.manual_seed_all(an_int) 17 | torch.backends.cudnn.deterministic = True 18 | torch.backends.cudnn.benchmark = False 19 | 20 | 21 | def mkdir_file(path): 22 | if not os.path.exists(path): 23 | os.makedirs(path) 24 | 25 | 26 | def format_time(seconds): 27 | days = int(seconds / (3600 * 24)) 28 | seconds = seconds - days * 3600 * 24 29 | hours = int(seconds / 3600) 30 | seconds = seconds - hours * 3600 31 | minutes = int(seconds / 60) 32 | seconds = seconds - minutes * 60 33 | secondsf = int(seconds) 34 | seconds = seconds - secondsf 35 | millis = int(seconds * 1000) 36 | 37 | f = '' 38 | i = 1 39 | if days > 0: 40 | f += str(days) + 'D' 41 | i += 1 42 | if hours > 0 and i <= 2: 43 | f += str(hours) + 'h' 44 | i += 1 45 | if minutes > 0 and i <= 2: 46 | f += str(minutes) + 'm' 47 | if secondsf > 0 and i <= 2: 48 | f += str(millis) + 'ms' 49 | i += 1 50 | if f == '': 51 | f = '0ms' 52 | return f 53 | 54 | 55 | class Logger(object): 56 | """ 57 | Write console output to external text file. 58 | 59 | Code imported from https://github.com/Cysu/open-reid/blob/master/reid/utils/logging.py. 60 | """ 61 | 62 | def __init__(self, args, fpath=None): 63 | self.console = sys.stdout 64 | self.file = None 65 | if fpath is not None: 66 | mkdir_file(os.path.dirname(fpath)) 67 | self.file = open(fpath, 'w') 68 | # write model configure at the top of file 69 | for arg in vars(args): 70 | self.file.write(arg + ': ' + str(getattr(args, arg)) + '\n') 71 | self.file.flush() 72 | 73 | def __del__(self): 74 | self.close() 75 | 76 | def __enter__(self): 77 | pass 78 | 79 | def __exit__(self, *args): 80 | self.close() 81 | 82 | def write(self, msg): 83 | self.console.write(msg) 84 | if self.file is not None: 85 | self.file.write(msg) 86 | self.file.flush() 87 | 88 | def write_dict(self, dict): 89 | if self.file is not None: 90 | for k in dict: 91 | self.file.write(k + ': ' + dict[k] + ' ') 92 | self.file.write('\n') 93 | self.file.flush() 94 | 95 | def flush(self): 96 | self.console.flush() 97 | if self.file is not None: 98 | self.file.flush() 99 | os.fsync(self.file.fileno()) 100 | 101 | def close(self): 102 | self.console.close() 103 | if self.file is not None: 104 | self.file.close() 105 | 106 | 107 | class AverageMeter(object): 108 | """Computes and stores the average and current value 109 | Imported from https://github.com/pytorch/examples/blob/master/imagenet/main.py#L247-L262 110 | """ 111 | def __init__(self): 112 | self.reset() 113 | 114 | def reset(self): 115 | self.val = 0 116 | self.avg = 0 117 | self.sum = 0 118 | self.count = 0 119 | 120 | def update(self, val, n=1): 121 | self.val = val 122 | self.sum += val * n 123 | self.count += n 124 | self.avg = self.sum / self.count 125 | 126 | 127 | def save_checkpoint(state, filename='checkpoint.pt'): 128 | torch.save(state, filename) 129 | 130 | 131 | @torch.no_grad() 132 | def accuracy(output, target, topk=(1, )): 133 | """""" 134 | maxk = max(topk) 135 | batch_size = target.size(0) 136 | 137 | _, pred = output.topk(maxk, dim=1, largest=True, sorted=True) 138 | pred = pred.t() 139 | correct = pred.eq(target.view(1, -1).expand_as(pred)) 140 | 141 | res = [] 142 | for k in topk: 143 | correct_k = correct[:k].reshape(-1).float().sum(axis=0, keepdim=True) 144 | res.append(correct_k.mul_(100.0 / batch_size)) # element-wise multiply in matrices 145 | return res -------------------------------------------------------------------------------- /CV_net/swin_transformer/utils/misc.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2022/2/10 12:43 4 | # @Author : '' 5 | # @FileName: misc.py 6 | from __future__ import absolute_import 7 | import os 8 | import sys 9 | import torch 10 | import torch.backends.cudnn 11 | 12 | 13 | def setup_seed(an_int): 14 | torch.manual_seed(an_int) 15 | torch.cuda.manual_seed(an_int) 16 | torch.cuda.manual_seed_all(an_int) 17 | torch.backends.cudnn.deterministic = True 18 | torch.backends.cudnn.benchmark = False 19 | 20 | 21 | def mkdir_file(path): 22 | if not os.path.exists(path): 23 | os.makedirs(path) 24 | 25 | 26 | def format_time(seconds): 27 | days = int(seconds / (3600 * 24)) 28 | seconds = seconds - days * 3600 * 24 29 | hours = int(seconds / 3600) 30 | seconds = seconds - hours * 3600 31 | minutes = int(seconds / 60) 32 | seconds = seconds - minutes * 60 33 | secondsf = int(seconds) 34 | seconds = seconds - secondsf 35 | millis = int(seconds * 1000) 36 | 37 | f = '' 38 | i = 1 39 | if days > 0: 40 | f += str(days) + 'D' 41 | i += 1 42 | if hours > 0 and i <= 2: 43 | f += str(hours) + 'h' 44 | i += 1 45 | if minutes > 0 and i <= 2: 46 | f += str(minutes) + 'm' 47 | if secondsf > 0 and i <= 2: 48 | f += str(millis) + 'ms' 49 | i += 1 50 | if f == '': 51 | f = '0ms' 52 | return f 53 | 54 | 55 | class Logger(object): 56 | """ 57 | Write console output to external text file. 58 | 59 | Code imported from https://github.com/Cysu/open-reid/blob/master/reid/utils/logging.py. 60 | """ 61 | 62 | def __init__(self, args, fpath=None): 63 | self.console = sys.stdout 64 | self.file = None 65 | if fpath is not None: 66 | mkdir_file(os.path.dirname(fpath)) 67 | self.file = open(fpath, 'w') 68 | # write model configure at the top of file 69 | for arg in vars(args): 70 | self.file.write(arg + ': ' + str(getattr(args, arg)) + '\n') 71 | self.file.flush() 72 | 73 | def __del__(self): 74 | self.close() 75 | 76 | def __enter__(self): 77 | pass 78 | 79 | def __exit__(self, *args): 80 | self.close() 81 | 82 | def write(self, msg): 83 | self.console.write(msg) 84 | if self.file is not None: 85 | self.file.write(msg) 86 | self.file.flush() 87 | 88 | def write_dict(self, dict): 89 | if self.file is not None: 90 | for k in dict: 91 | self.file.write(k + ': ' + dict[k] + ' ') 92 | self.file.write('\n') 93 | self.file.flush() 94 | 95 | def flush(self): 96 | self.console.flush() 97 | if self.file is not None: 98 | self.file.flush() 99 | os.fsync(self.file.fileno()) 100 | 101 | def close(self): 102 | self.console.close() 103 | if self.file is not None: 104 | self.file.close() 105 | 106 | 107 | class AverageMeter(object): 108 | """Computes and stores the average and current value 109 | Imported from https://github.com/pytorch/examples/blob/master/imagenet/main.py#L247-L262 110 | """ 111 | def __init__(self): 112 | self.reset() 113 | 114 | def reset(self): 115 | self.val = 0 116 | self.avg = 0 117 | self.sum = 0 118 | self.count = 0 119 | 120 | def update(self, val, n=1): 121 | self.val = val 122 | self.sum += val * n 123 | self.count += n 124 | self.avg = self.sum / self.count 125 | 126 | 127 | def save_checkpoint(state, filename='checkpoint.pt'): 128 | torch.save(state, filename) 129 | 130 | 131 | @torch.no_grad() 132 | def accuracy(output, target, topk=(1, )): 133 | """""" 134 | maxk = max(topk) 135 | batch_size = target.size(0) 136 | 137 | _, pred = output.topk(maxk, dim=1, largest=True, sorted=True) 138 | pred = pred.t() 139 | correct = pred.eq(target.view(1, -1).expand_as(pred)) 140 | 141 | res = [] 142 | for k in topk: 143 | correct_k = correct[:k].reshape(-1).float().sum(axis=0, keepdim=True) 144 | res.append(correct_k.mul_(100.0 / batch_size)) # element-wise multiply in matrices 145 | return res 146 | -------------------------------------------------------------------------------- /CV_net/LeNet/train.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2021/7/29 16:44 4 | # @Author : 'IReverser' 5 | # @FileName: train.py 6 | 7 | import torch 8 | import torchvision 9 | import torch.nn as nn 10 | import torch.optim as optim 11 | import torchvision.transforms as transforms 12 | import numpy as np 13 | import matplotlib.pyplot as plt 14 | import os 15 | from tensorboardX import SummaryWriter 16 | from model import LeNet 17 | writer = SummaryWriter() # start tensorboardx monitor 18 | 19 | BATCH_SIZE = 64 20 | RATE_LEARNING = 1e-5 21 | EPOCH = 5 22 | PRINT_EPOCH = 100 23 | DATA_PATH = './data' # dataset save path 24 | SAVE_PATH = './model/' # trained model path 25 | DOWNLOAD = False # if you want to download data, it sets 'True', otherwise. 26 | device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') 27 | print('Use device: ', device) # print used device during the training 28 | # data preprocess 29 | transform = transforms.Compose([ 30 | transforms.ToTensor(), 31 | transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]) 32 | # load train set and test set 33 | train_samples = torchvision.datasets.CIFAR10(root=DATA_PATH, train=True, 34 | download=DOWNLOAD, transform=transform) 35 | val_samples = torchvision.datasets.CIFAR10(root=DATA_PATH, train=False, 36 | download=DOWNLOAD, transform=transform) 37 | # data format transform 38 | train_loader = torch.utils.data.DataLoader(train_samples, batch_size=BATCH_SIZE, 39 | shuffle=True, num_workers=0) 40 | val_loader = torch.utils.data.DataLoader(val_samples, batch_size=4, 41 | shuffle=True, num_workers=0) 42 | 43 | # category labels 44 | classes = ('airplane', 'automobile', 'bird', 'cat', 45 | 'deer', 'dog', 'frog', 'horse', 'ship', 'truck') 46 | 47 | 48 | # train set are load for classification 49 | val_iter = iter(val_loader) 50 | val_images, val_labels = val_iter.next() 51 | 52 | # visualize validation samples 53 | # # print labels 54 | # print(' '.join('%5s' % classes[val_labels[j]] for j in range(4))) 55 | # # imshow images 56 | # img = torchvision.utils.make_grid(val_images) 57 | # img = img / 2 + 0.5 # unnormalize 58 | # npimg = img.numpy() 59 | # plt.imshow(np.transpose(npimg, (1, 2, 0))) 60 | # plt.show() 61 | 62 | val_images = val_images.to(device) 63 | val_labels = val_labels.to(device) 64 | # initialize net 65 | net = LeNet() 66 | print(net) 67 | net = net.to(device) 68 | 69 | # design loss function 70 | loss_function = nn.CrossEntropyLoss() 71 | # design optimizer 72 | optimizer = optim.Adam(net.parameters(), lr=RATE_LEARNING) 73 | 74 | # start training 75 | for epoch in range(EPOCH): 76 | run_loss = 0.0 77 | for step, data in enumerate(train_loader, start=0): 78 | inputs, labels = data # build samples and labels from data 79 | 80 | inputs = inputs.to(device) 81 | labels = labels.to(device) 82 | # zero the parameter gradients 83 | optimizer.zero_grad() 84 | # forward + backward + optimize 85 | output = net(inputs) 86 | loss = loss_function(output, labels) 87 | # Update Visualization 88 | loss.backward() 89 | optimizer.step() 90 | 91 | run_loss += loss.item() 92 | if step % PRINT_EPOCH == (PRINT_EPOCH - 1): 93 | with torch.no_grad(): 94 | outputs = net(val_images) 95 | pred_y = torch.max(outputs, dim=1)[1].to(device) 96 | acc_rate = torch.eq(pred_y, val_labels).sum().item() / val_labels.size(0) 97 | print('Epoch: %d, iter: %d, train_loss: %.3f, test_accuracy: %.3f' 98 | % (epoch, step, run_loss/PRINT_EPOCH, acc_rate)) 99 | # visualize for training process 100 | writer.add_scalar('Training loss', run_loss/PRINT_EPOCH, global_step=step) 101 | writer.add_scalar('Valdation accuracy', acc_rate, global_step=step) 102 | run_loss = 0.0 103 | writer.close() 104 | 105 | print('Finished Training!') 106 | # save model 107 | save_path = SAVE_PATH + 'LeNet1.pth' 108 | if not os.path.exists(SAVE_PATH): 109 | os.makedirs(SAVE_PATH) 110 | torch.save(net.state_dict(), save_path) 111 | # save onnx model format 112 | # dummy_input = torch.randn(1, 3, 32, 32, requires_grad=True).to(device) 113 | # torch.onnx.export(net, dummy_input, SAVE_PATH + "lenet1.onnx") 114 | --------------------------------------------------------------------------------