Vvaann commited on
Commit
d48f03e
·
verified ·
1 Parent(s): 1b575bc

Delete classify

Browse files
Files changed (3) hide show
  1. classify/predict.py +0 -224
  2. classify/train.py +0 -333
  3. classify/val.py +0 -170
classify/predict.py DELETED
@@ -1,224 +0,0 @@
1
- # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2
- """
3
- Run YOLOv5 classification inference on images, videos, directories, globs, YouTube, webcam, streams, etc.
4
-
5
- Usage - sources:
6
- $ python classify/predict.py --weights yolov5s-cls.pt --source 0 # webcam
7
- img.jpg # image
8
- vid.mp4 # video
9
- screen # screenshot
10
- path/ # directory
11
- 'path/*.jpg' # glob
12
- 'https://youtu.be/Zgi9g1ksQHc' # YouTube
13
- 'rtsp://example.com/media.mp4' # RTSP, RTMP, HTTP stream
14
-
15
- Usage - formats:
16
- $ python classify/predict.py --weights yolov5s-cls.pt # PyTorch
17
- yolov5s-cls.torchscript # TorchScript
18
- yolov5s-cls.onnx # ONNX Runtime or OpenCV DNN with --dnn
19
- yolov5s-cls_openvino_model # OpenVINO
20
- yolov5s-cls.engine # TensorRT
21
- yolov5s-cls.mlmodel # CoreML (macOS-only)
22
- yolov5s-cls_saved_model # TensorFlow SavedModel
23
- yolov5s-cls.pb # TensorFlow GraphDef
24
- yolov5s-cls.tflite # TensorFlow Lite
25
- yolov5s-cls_edgetpu.tflite # TensorFlow Edge TPU
26
- yolov5s-cls_paddle_model # PaddlePaddle
27
- """
28
-
29
- import argparse
30
- import os
31
- import platform
32
- import sys
33
- from pathlib import Path
34
-
35
- import torch
36
- import torch.nn.functional as F
37
-
38
- FILE = Path(__file__).resolve()
39
- ROOT = FILE.parents[1] # YOLOv5 root directory
40
- if str(ROOT) not in sys.path:
41
- sys.path.append(str(ROOT)) # add ROOT to PATH
42
- ROOT = Path(os.path.relpath(ROOT, Path.cwd())) # relative
43
-
44
- from models.common import DetectMultiBackend
45
- from utils.augmentations import classify_transforms
46
- from utils.dataloaders import IMG_FORMATS, VID_FORMATS, LoadImages, LoadScreenshots, LoadStreams
47
- from utils.general import (LOGGER, Profile, check_file, check_img_size, check_imshow, check_requirements, colorstr, cv2,
48
- increment_path, print_args, strip_optimizer)
49
- from utils.plots import Annotator
50
- from utils.torch_utils import select_device, smart_inference_mode
51
-
52
-
53
- @smart_inference_mode()
54
- def run(
55
- weights=ROOT / 'yolov5s-cls.pt', # model.pt path(s)
56
- source=ROOT / 'data/images', # file/dir/URL/glob/screen/0(webcam)
57
- data=ROOT / 'data/coco128.yaml', # dataset.yaml path
58
- imgsz=(224, 224), # inference size (height, width)
59
- device='', # cuda device, i.e. 0 or 0,1,2,3 or cpu
60
- view_img=False, # show results
61
- save_txt=False, # save results to *.txt
62
- nosave=False, # do not save images/videos
63
- augment=False, # augmented inference
64
- visualize=False, # visualize features
65
- update=False, # update all models
66
- project=ROOT / 'runs/predict-cls', # save results to project/name
67
- name='exp', # save results to project/name
68
- exist_ok=False, # existing project/name ok, do not increment
69
- half=False, # use FP16 half-precision inference
70
- dnn=False, # use OpenCV DNN for ONNX inference
71
- vid_stride=1, # video frame-rate stride
72
- ):
73
- source = str(source)
74
- save_img = not nosave and not source.endswith('.txt') # save inference images
75
- is_file = Path(source).suffix[1:] in (IMG_FORMATS + VID_FORMATS)
76
- is_url = source.lower().startswith(('rtsp://', 'rtmp://', 'http://', 'https://'))
77
- webcam = source.isnumeric() or source.endswith('.txt') or (is_url and not is_file)
78
- screenshot = source.lower().startswith('screen')
79
- if is_url and is_file:
80
- source = check_file(source) # download
81
-
82
- # Directories
83
- save_dir = increment_path(Path(project) / name, exist_ok=exist_ok) # increment run
84
- (save_dir / 'labels' if save_txt else save_dir).mkdir(parents=True, exist_ok=True) # make dir
85
-
86
- # Load model
87
- device = select_device(device)
88
- model = DetectMultiBackend(weights, device=device, dnn=dnn, data=data, fp16=half)
89
- stride, names, pt = model.stride, model.names, model.pt
90
- imgsz = check_img_size(imgsz, s=stride) # check image size
91
-
92
- # Dataloader
93
- bs = 1 # batch_size
94
- if webcam:
95
- view_img = check_imshow(warn=True)
96
- dataset = LoadStreams(source, img_size=imgsz, transforms=classify_transforms(imgsz[0]), vid_stride=vid_stride)
97
- bs = len(dataset)
98
- elif screenshot:
99
- dataset = LoadScreenshots(source, img_size=imgsz, stride=stride, auto=pt)
100
- else:
101
- dataset = LoadImages(source, img_size=imgsz, transforms=classify_transforms(imgsz[0]), vid_stride=vid_stride)
102
- vid_path, vid_writer = [None] * bs, [None] * bs
103
-
104
- # Run inference
105
- model.warmup(imgsz=(1 if pt else bs, 3, *imgsz)) # warmup
106
- seen, windows, dt = 0, [], (Profile(), Profile(), Profile())
107
- for path, im, im0s, vid_cap, s in dataset:
108
- with dt[0]:
109
- im = torch.Tensor(im).to(model.device)
110
- im = im.half() if model.fp16 else im.float() # uint8 to fp16/32
111
- if len(im.shape) == 3:
112
- im = im[None] # expand for batch dim
113
-
114
- # Inference
115
- with dt[1]:
116
- results = model(im)
117
-
118
- # Post-process
119
- with dt[2]:
120
- pred = F.softmax(results, dim=1) # probabilities
121
-
122
- # Process predictions
123
- for i, prob in enumerate(pred): # per image
124
- seen += 1
125
- if webcam: # batch_size >= 1
126
- p, im0, frame = path[i], im0s[i].copy(), dataset.count
127
- s += f'{i}: '
128
- else:
129
- p, im0, frame = path, im0s.copy(), getattr(dataset, 'frame', 0)
130
-
131
- p = Path(p) # to Path
132
- save_path = str(save_dir / p.name) # im.jpg
133
- txt_path = str(save_dir / 'labels' / p.stem) + ('' if dataset.mode == 'image' else f'_{frame}') # im.txt
134
-
135
- s += '%gx%g ' % im.shape[2:] # print string
136
- annotator = Annotator(im0, example=str(names), pil=True)
137
-
138
- # Print results
139
- top5i = prob.argsort(0, descending=True)[:5].tolist() # top 5 indices
140
- s += f"{', '.join(f'{names[j]} {prob[j]:.2f}' for j in top5i)}, "
141
-
142
- # Write results
143
- text = '\n'.join(f'{prob[j]:.2f} {names[j]}' for j in top5i)
144
- if save_img or view_img: # Add bbox to image
145
- annotator.text((32, 32), text, txt_color=(255, 255, 255))
146
- if save_txt: # Write to file
147
- with open(f'{txt_path}.txt', 'a') as f:
148
- f.write(text + '\n')
149
-
150
- # Stream results
151
- im0 = annotator.result()
152
- if view_img:
153
- if platform.system() == 'Linux' and p not in windows:
154
- windows.append(p)
155
- cv2.namedWindow(str(p), cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO) # allow window resize (Linux)
156
- cv2.resizeWindow(str(p), im0.shape[1], im0.shape[0])
157
- cv2.imshow(str(p), im0)
158
- cv2.waitKey(1) # 1 millisecond
159
-
160
- # Save results (image with detections)
161
- if save_img:
162
- if dataset.mode == 'image':
163
- cv2.imwrite(save_path, im0)
164
- else: # 'video' or 'stream'
165
- if vid_path[i] != save_path: # new video
166
- vid_path[i] = save_path
167
- if isinstance(vid_writer[i], cv2.VideoWriter):
168
- vid_writer[i].release() # release previous video writer
169
- if vid_cap: # video
170
- fps = vid_cap.get(cv2.CAP_PROP_FPS)
171
- w = int(vid_cap.get(cv2.CAP_PROP_FRAME_WIDTH))
172
- h = int(vid_cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
173
- else: # stream
174
- fps, w, h = 30, im0.shape[1], im0.shape[0]
175
- save_path = str(Path(save_path).with_suffix('.mp4')) # force *.mp4 suffix on results videos
176
- vid_writer[i] = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (w, h))
177
- vid_writer[i].write(im0)
178
-
179
- # Print time (inference-only)
180
- LOGGER.info(f"{s}{dt[1].dt * 1E3:.1f}ms")
181
-
182
- # Print results
183
- t = tuple(x.t / seen * 1E3 for x in dt) # speeds per image
184
- LOGGER.info(f'Speed: %.1fms pre-process, %.1fms inference, %.1fms NMS per image at shape {(1, 3, *imgsz)}' % t)
185
- if save_txt or save_img:
186
- s = f"\n{len(list(save_dir.glob('labels/*.txt')))} labels saved to {save_dir / 'labels'}" if save_txt else ''
187
- LOGGER.info(f"Results saved to {colorstr('bold', save_dir)}{s}")
188
- if update:
189
- strip_optimizer(weights[0]) # update model (to fix SourceChangeWarning)
190
-
191
-
192
- def parse_opt():
193
- parser = argparse.ArgumentParser()
194
- parser.add_argument('--weights', nargs='+', type=str, default=ROOT / 'yolov5s-cls.pt', help='model path(s)')
195
- parser.add_argument('--source', type=str, default=ROOT / 'data/images', help='file/dir/URL/glob/screen/0(webcam)')
196
- parser.add_argument('--data', type=str, default=ROOT / 'data/coco128.yaml', help='(optional) dataset.yaml path')
197
- parser.add_argument('--imgsz', '--img', '--img-size', nargs='+', type=int, default=[224], help='inference size h,w')
198
- parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
199
- parser.add_argument('--view-img', action='store_true', help='show results')
200
- parser.add_argument('--save-txt', action='store_true', help='save results to *.txt')
201
- parser.add_argument('--nosave', action='store_true', help='do not save images/videos')
202
- parser.add_argument('--augment', action='store_true', help='augmented inference')
203
- parser.add_argument('--visualize', action='store_true', help='visualize features')
204
- parser.add_argument('--update', action='store_true', help='update all models')
205
- parser.add_argument('--project', default=ROOT / 'runs/predict-cls', help='save results to project/name')
206
- parser.add_argument('--name', default='exp', help='save results to project/name')
207
- parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')
208
- parser.add_argument('--half', action='store_true', help='use FP16 half-precision inference')
209
- parser.add_argument('--dnn', action='store_true', help='use OpenCV DNN for ONNX inference')
210
- parser.add_argument('--vid-stride', type=int, default=1, help='video frame-rate stride')
211
- opt = parser.parse_args()
212
- opt.imgsz *= 2 if len(opt.imgsz) == 1 else 1 # expand
213
- print_args(vars(opt))
214
- return opt
215
-
216
-
217
- def main(opt):
218
- check_requirements(exclude=('tensorboard', 'thop'))
219
- run(**vars(opt))
220
-
221
-
222
- if __name__ == "__main__":
223
- opt = parse_opt()
224
- main(opt)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
classify/train.py DELETED
@@ -1,333 +0,0 @@
1
- # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2
- """
3
- Train a YOLOv5 classifier model on a classification dataset
4
-
5
- Usage - Single-GPU training:
6
- $ python classify/train.py --model yolov5s-cls.pt --data imagenette160 --epochs 5 --img 224
7
-
8
- Usage - Multi-GPU DDP training:
9
- $ python -m torch.distributed.run --nproc_per_node 4 --master_port 1 classify/train.py --model yolov5s-cls.pt --data imagenet --epochs 5 --img 224 --device 0,1,2,3
10
-
11
- Datasets: --data mnist, fashion-mnist, cifar10, cifar100, imagenette, imagewoof, imagenet, or 'path/to/data'
12
- YOLOv5-cls models: --model yolov5n-cls.pt, yolov5s-cls.pt, yolov5m-cls.pt, yolov5l-cls.pt, yolov5x-cls.pt
13
- Torchvision models: --model resnet50, efficientnet_b0, etc. See https://pytorch.org/vision/stable/models.html
14
- """
15
-
16
- import argparse
17
- import os
18
- import subprocess
19
- import sys
20
- import time
21
- from copy import deepcopy
22
- from datetime import datetime
23
- from pathlib import Path
24
-
25
- import torch
26
- import torch.distributed as dist
27
- import torch.hub as hub
28
- import torch.optim.lr_scheduler as lr_scheduler
29
- import torchvision
30
- from torch.cuda import amp
31
- from tqdm import tqdm
32
-
33
- FILE = Path(__file__).resolve()
34
- ROOT = FILE.parents[1] # YOLOv5 root directory
35
- if str(ROOT) not in sys.path:
36
- sys.path.append(str(ROOT)) # add ROOT to PATH
37
- ROOT = Path(os.path.relpath(ROOT, Path.cwd())) # relative
38
-
39
- from classify import val as validate
40
- from models.experimental import attempt_load
41
- from models.yolo import ClassificationModel, DetectionModel
42
- from utils.dataloaders import create_classification_dataloader
43
- from utils.general import (DATASETS_DIR, LOGGER, TQDM_BAR_FORMAT, WorkingDirectory, check_git_info, check_git_status,
44
- check_requirements, colorstr, download, increment_path, init_seeds, print_args, yaml_save)
45
- from utils.loggers import GenericLogger
46
- from utils.plots import imshow_cls
47
- from utils.torch_utils import (ModelEMA, model_info, reshape_classifier_output, select_device, smart_DDP,
48
- smart_optimizer, smartCrossEntropyLoss, torch_distributed_zero_first)
49
-
50
- LOCAL_RANK = int(os.getenv('LOCAL_RANK', -1)) # https://pytorch.org/docs/stable/elastic/run.html
51
- RANK = int(os.getenv('RANK', -1))
52
- WORLD_SIZE = int(os.getenv('WORLD_SIZE', 1))
53
- GIT_INFO = check_git_info()
54
-
55
-
56
- def train(opt, device):
57
- init_seeds(opt.seed + 1 + RANK, deterministic=True)
58
- save_dir, data, bs, epochs, nw, imgsz, pretrained = \
59
- opt.save_dir, Path(opt.data), opt.batch_size, opt.epochs, min(os.cpu_count() - 1, opt.workers), \
60
- opt.imgsz, str(opt.pretrained).lower() == 'true'
61
- cuda = device.type != 'cpu'
62
-
63
- # Directories
64
- wdir = save_dir / 'weights'
65
- wdir.mkdir(parents=True, exist_ok=True) # make dir
66
- last, best = wdir / 'last.pt', wdir / 'best.pt'
67
-
68
- # Save run settings
69
- yaml_save(save_dir / 'opt.yaml', vars(opt))
70
-
71
- # Logger
72
- logger = GenericLogger(opt=opt, console_logger=LOGGER) if RANK in {-1, 0} else None
73
-
74
- # Download Dataset
75
- with torch_distributed_zero_first(LOCAL_RANK), WorkingDirectory(ROOT):
76
- data_dir = data if data.is_dir() else (DATASETS_DIR / data)
77
- if not data_dir.is_dir():
78
- LOGGER.info(f'\nDataset not found ⚠️, missing path {data_dir}, attempting download...')
79
- t = time.time()
80
- if str(data) == 'imagenet':
81
- subprocess.run(f"bash {ROOT / 'data/scripts/get_imagenet.sh'}", shell=True, check=True)
82
- else:
83
- url = f'https://github.com/ultralytics/yolov5/releases/download/v1.0/{data}.zip'
84
- download(url, dir=data_dir.parent)
85
- s = f"Dataset download success ✅ ({time.time() - t:.1f}s), saved to {colorstr('bold', data_dir)}\n"
86
- LOGGER.info(s)
87
-
88
- # Dataloaders
89
- nc = len([x for x in (data_dir / 'train').glob('*') if x.is_dir()]) # number of classes
90
- trainloader = create_classification_dataloader(path=data_dir / 'train',
91
- imgsz=imgsz,
92
- batch_size=bs // WORLD_SIZE,
93
- augment=True,
94
- cache=opt.cache,
95
- rank=LOCAL_RANK,
96
- workers=nw)
97
-
98
- test_dir = data_dir / 'test' if (data_dir / 'test').exists() else data_dir / 'val' # data/test or data/val
99
- if RANK in {-1, 0}:
100
- testloader = create_classification_dataloader(path=test_dir,
101
- imgsz=imgsz,
102
- batch_size=bs // WORLD_SIZE * 2,
103
- augment=False,
104
- cache=opt.cache,
105
- rank=-1,
106
- workers=nw)
107
-
108
- # Model
109
- with torch_distributed_zero_first(LOCAL_RANK), WorkingDirectory(ROOT):
110
- if Path(opt.model).is_file() or opt.model.endswith('.pt'):
111
- model = attempt_load(opt.model, device='cpu', fuse=False)
112
- elif opt.model in torchvision.models.__dict__: # TorchVision models i.e. resnet50, efficientnet_b0
113
- model = torchvision.models.__dict__[opt.model](weights='IMAGENET1K_V1' if pretrained else None)
114
- else:
115
- m = hub.list('ultralytics/yolov5') # + hub.list('pytorch/vision') # models
116
- raise ModuleNotFoundError(f'--model {opt.model} not found. Available models are: \n' + '\n'.join(m))
117
- if isinstance(model, DetectionModel):
118
- LOGGER.warning("WARNING ⚠️ pass YOLOv5 classifier model with '-cls' suffix, i.e. '--model yolov5s-cls.pt'")
119
- model = ClassificationModel(model=model, nc=nc, cutoff=opt.cutoff or 10) # convert to classification model
120
- reshape_classifier_output(model, nc) # update class count
121
- for m in model.modules():
122
- if not pretrained and hasattr(m, 'reset_parameters'):
123
- m.reset_parameters()
124
- if isinstance(m, torch.nn.Dropout) and opt.dropout is not None:
125
- m.p = opt.dropout # set dropout
126
- for p in model.parameters():
127
- p.requires_grad = True # for training
128
- model = model.to(device)
129
-
130
- # Info
131
- if RANK in {-1, 0}:
132
- model.names = trainloader.dataset.classes # attach class names
133
- model.transforms = testloader.dataset.torch_transforms # attach inference transforms
134
- model_info(model)
135
- if opt.verbose:
136
- LOGGER.info(model)
137
- images, labels = next(iter(trainloader))
138
- file = imshow_cls(images[:25], labels[:25], names=model.names, f=save_dir / 'train_images.jpg')
139
- logger.log_images(file, name='Train Examples')
140
- logger.log_graph(model, imgsz) # log model
141
-
142
- # Optimizer
143
- optimizer = smart_optimizer(model, opt.optimizer, opt.lr0, momentum=0.9, decay=opt.decay)
144
-
145
- # Scheduler
146
- lrf = 0.01 # final lr (fraction of lr0)
147
- # lf = lambda x: ((1 + math.cos(x * math.pi / epochs)) / 2) * (1 - lrf) + lrf # cosine
148
- lf = lambda x: (1 - x / epochs) * (1 - lrf) + lrf # linear
149
- scheduler = lr_scheduler.LambdaLR(optimizer, lr_lambda=lf)
150
- # scheduler = lr_scheduler.OneCycleLR(optimizer, max_lr=lr0, total_steps=epochs, pct_start=0.1,
151
- # final_div_factor=1 / 25 / lrf)
152
-
153
- # EMA
154
- ema = ModelEMA(model) if RANK in {-1, 0} else None
155
-
156
- # DDP mode
157
- if cuda and RANK != -1:
158
- model = smart_DDP(model)
159
-
160
- # Train
161
- t0 = time.time()
162
- criterion = smartCrossEntropyLoss(label_smoothing=opt.label_smoothing) # loss function
163
- best_fitness = 0.0
164
- scaler = amp.GradScaler(enabled=cuda)
165
- val = test_dir.stem # 'val' or 'test'
166
- LOGGER.info(f'Image sizes {imgsz} train, {imgsz} test\n'
167
- f'Using {nw * WORLD_SIZE} dataloader workers\n'
168
- f"Logging results to {colorstr('bold', save_dir)}\n"
169
- f'Starting {opt.model} training on {data} dataset with {nc} classes for {epochs} epochs...\n\n'
170
- f"{'Epoch':>10}{'GPU_mem':>10}{'train_loss':>12}{f'{val}_loss':>12}{'top1_acc':>12}{'top5_acc':>12}")
171
- for epoch in range(epochs): # loop over the dataset multiple times
172
- tloss, vloss, fitness = 0.0, 0.0, 0.0 # train loss, val loss, fitness
173
- model.train()
174
- if RANK != -1:
175
- trainloader.sampler.set_epoch(epoch)
176
- pbar = enumerate(trainloader)
177
- if RANK in {-1, 0}:
178
- pbar = tqdm(enumerate(trainloader), total=len(trainloader), bar_format=TQDM_BAR_FORMAT)
179
- for i, (images, labels) in pbar: # progress bar
180
- images, labels = images.to(device, non_blocking=True), labels.to(device)
181
-
182
- # Forward
183
- with amp.autocast(enabled=cuda): # stability issues when enabled
184
- loss = criterion(model(images), labels)
185
-
186
- # Backward
187
- scaler.scale(loss).backward()
188
-
189
- # Optimize
190
- scaler.unscale_(optimizer) # unscale gradients
191
- torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=10.0) # clip gradients
192
- scaler.step(optimizer)
193
- scaler.update()
194
- optimizer.zero_grad()
195
- if ema:
196
- ema.update(model)
197
-
198
- if RANK in {-1, 0}:
199
- # Print
200
- tloss = (tloss * i + loss.item()) / (i + 1) # update mean losses
201
- mem = '%.3gG' % (torch.cuda.memory_reserved() / 1E9 if torch.cuda.is_available() else 0) # (GB)
202
- pbar.desc = f"{f'{epoch + 1}/{epochs}':>10}{mem:>10}{tloss:>12.3g}" + ' ' * 36
203
-
204
- # Test
205
- if i == len(pbar) - 1: # last batch
206
- top1, top5, vloss = validate.run(model=ema.ema,
207
- dataloader=testloader,
208
- criterion=criterion,
209
- pbar=pbar) # test accuracy, loss
210
- fitness = top1 # define fitness as top1 accuracy
211
-
212
- # Scheduler
213
- scheduler.step()
214
-
215
- # Log metrics
216
- if RANK in {-1, 0}:
217
- # Best fitness
218
- if fitness > best_fitness:
219
- best_fitness = fitness
220
-
221
- # Log
222
- metrics = {
223
- "train/loss": tloss,
224
- f"{val}/loss": vloss,
225
- "metrics/accuracy_top1": top1,
226
- "metrics/accuracy_top5": top5,
227
- "lr/0": optimizer.param_groups[0]['lr']} # learning rate
228
- logger.log_metrics(metrics, epoch)
229
-
230
- # Save model
231
- final_epoch = epoch + 1 == epochs
232
- if (not opt.nosave) or final_epoch:
233
- ckpt = {
234
- 'epoch': epoch,
235
- 'best_fitness': best_fitness,
236
- 'model': deepcopy(ema.ema).half(), # deepcopy(de_parallel(model)).half(),
237
- 'ema': None, # deepcopy(ema.ema).half(),
238
- 'updates': ema.updates,
239
- 'optimizer': None, # optimizer.state_dict(),
240
- 'opt': vars(opt),
241
- 'git': GIT_INFO, # {remote, branch, commit} if a git repo
242
- 'date': datetime.now().isoformat()}
243
-
244
- # Save last, best and delete
245
- torch.save(ckpt, last)
246
- if best_fitness == fitness:
247
- torch.save(ckpt, best)
248
- del ckpt
249
-
250
- # Train complete
251
- if RANK in {-1, 0} and final_epoch:
252
- LOGGER.info(f'\nTraining complete ({(time.time() - t0) / 3600:.3f} hours)'
253
- f"\nResults saved to {colorstr('bold', save_dir)}"
254
- f"\nPredict: python classify/predict.py --weights {best} --source im.jpg"
255
- f"\nValidate: python classify/val.py --weights {best} --data {data_dir}"
256
- f"\nExport: python export.py --weights {best} --include onnx"
257
- f"\nPyTorch Hub: model = torch.hub.load('ultralytics/yolov5', 'custom', '{best}')"
258
- f"\nVisualize: https://netron.app\n")
259
-
260
- # Plot examples
261
- images, labels = (x[:25] for x in next(iter(testloader))) # first 25 images and labels
262
- pred = torch.max(ema.ema(images.to(device)), 1)[1]
263
- file = imshow_cls(images, labels, pred, model.names, verbose=False, f=save_dir / 'test_images.jpg')
264
-
265
- # Log results
266
- meta = {"epochs": epochs, "top1_acc": best_fitness, "date": datetime.now().isoformat()}
267
- logger.log_images(file, name='Test Examples (true-predicted)', epoch=epoch)
268
- logger.log_model(best, epochs, metadata=meta)
269
-
270
-
271
- def parse_opt(known=False):
272
- parser = argparse.ArgumentParser()
273
- parser.add_argument('--model', type=str, default='yolov5s-cls.pt', help='initial weights path')
274
- parser.add_argument('--data', type=str, default='imagenette160', help='cifar10, cifar100, mnist, imagenet, ...')
275
- parser.add_argument('--epochs', type=int, default=10, help='total training epochs')
276
- parser.add_argument('--batch-size', type=int, default=64, help='total batch size for all GPUs')
277
- parser.add_argument('--imgsz', '--img', '--img-size', type=int, default=224, help='train, val image size (pixels)')
278
- parser.add_argument('--nosave', action='store_true', help='only save final checkpoint')
279
- parser.add_argument('--cache', type=str, nargs='?', const='ram', help='--cache images in "ram" (default) or "disk"')
280
- parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
281
- parser.add_argument('--workers', type=int, default=8, help='max dataloader workers (per RANK in DDP mode)')
282
- parser.add_argument('--project', default=ROOT / 'runs/train-cls', help='save to project/name')
283
- parser.add_argument('--name', default='exp', help='save to project/name')
284
- parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')
285
- parser.add_argument('--pretrained', nargs='?', const=True, default=True, help='start from i.e. --pretrained False')
286
- parser.add_argument('--optimizer', choices=['SGD', 'Adam', 'AdamW', 'RMSProp'], default='Adam', help='optimizer')
287
- parser.add_argument('--lr0', type=float, default=0.001, help='initial learning rate')
288
- parser.add_argument('--decay', type=float, default=5e-5, help='weight decay')
289
- parser.add_argument('--label-smoothing', type=float, default=0.1, help='Label smoothing epsilon')
290
- parser.add_argument('--cutoff', type=int, default=None, help='Model layer cutoff index for Classify() head')
291
- parser.add_argument('--dropout', type=float, default=None, help='Dropout (fraction)')
292
- parser.add_argument('--verbose', action='store_true', help='Verbose mode')
293
- parser.add_argument('--seed', type=int, default=0, help='Global training seed')
294
- parser.add_argument('--local_rank', type=int, default=-1, help='Automatic DDP Multi-GPU argument, do not modify')
295
- return parser.parse_known_args()[0] if known else parser.parse_args()
296
-
297
-
298
- def main(opt):
299
- # Checks
300
- if RANK in {-1, 0}:
301
- print_args(vars(opt))
302
- check_git_status()
303
- check_requirements()
304
-
305
- # DDP mode
306
- device = select_device(opt.device, batch_size=opt.batch_size)
307
- if LOCAL_RANK != -1:
308
- assert opt.batch_size != -1, 'AutoBatch is coming soon for classification, please pass a valid --batch-size'
309
- assert opt.batch_size % WORLD_SIZE == 0, f'--batch-size {opt.batch_size} must be multiple of WORLD_SIZE'
310
- assert torch.cuda.device_count() > LOCAL_RANK, 'insufficient CUDA devices for DDP command'
311
- torch.cuda.set_device(LOCAL_RANK)
312
- device = torch.device('cuda', LOCAL_RANK)
313
- dist.init_process_group(backend="nccl" if dist.is_nccl_available() else "gloo")
314
-
315
- # Parameters
316
- opt.save_dir = increment_path(Path(opt.project) / opt.name, exist_ok=opt.exist_ok) # increment run
317
-
318
- # Train
319
- train(opt, device)
320
-
321
-
322
- def run(**kwargs):
323
- # Usage: from yolov5 import classify; classify.train.run(data=mnist, imgsz=320, model='yolov5m')
324
- opt = parse_opt(True)
325
- for k, v in kwargs.items():
326
- setattr(opt, k, v)
327
- main(opt)
328
- return opt
329
-
330
-
331
- if __name__ == "__main__":
332
- opt = parse_opt()
333
- main(opt)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
classify/val.py DELETED
@@ -1,170 +0,0 @@
1
- # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2
- """
3
- Validate a trained YOLOv5 classification model on a classification dataset
4
-
5
- Usage:
6
- $ bash data/scripts/get_imagenet.sh --val # download ImageNet val split (6.3G, 50000 images)
7
- $ python classify/val.py --weights yolov5m-cls.pt --data ../datasets/imagenet --img 224 # validate ImageNet
8
-
9
- Usage - formats:
10
- $ python classify/val.py --weights yolov5s-cls.pt # PyTorch
11
- yolov5s-cls.torchscript # TorchScript
12
- yolov5s-cls.onnx # ONNX Runtime or OpenCV DNN with --dnn
13
- yolov5s-cls_openvino_model # OpenVINO
14
- yolov5s-cls.engine # TensorRT
15
- yolov5s-cls.mlmodel # CoreML (macOS-only)
16
- yolov5s-cls_saved_model # TensorFlow SavedModel
17
- yolov5s-cls.pb # TensorFlow GraphDef
18
- yolov5s-cls.tflite # TensorFlow Lite
19
- yolov5s-cls_edgetpu.tflite # TensorFlow Edge TPU
20
- yolov5s-cls_paddle_model # PaddlePaddle
21
- """
22
-
23
- import argparse
24
- import os
25
- import sys
26
- from pathlib import Path
27
-
28
- import torch
29
- from tqdm import tqdm
30
-
31
- FILE = Path(__file__).resolve()
32
- ROOT = FILE.parents[1] # YOLOv5 root directory
33
- if str(ROOT) not in sys.path:
34
- sys.path.append(str(ROOT)) # add ROOT to PATH
35
- ROOT = Path(os.path.relpath(ROOT, Path.cwd())) # relative
36
-
37
- from models.common import DetectMultiBackend
38
- from utils.dataloaders import create_classification_dataloader
39
- from utils.general import (LOGGER, TQDM_BAR_FORMAT, Profile, check_img_size, check_requirements, colorstr,
40
- increment_path, print_args)
41
- from utils.torch_utils import select_device, smart_inference_mode
42
-
43
-
44
- @smart_inference_mode()
45
- def run(
46
- data=ROOT / '../datasets/mnist', # dataset dir
47
- weights=ROOT / 'yolov5s-cls.pt', # model.pt path(s)
48
- batch_size=128, # batch size
49
- imgsz=224, # inference size (pixels)
50
- device='', # cuda device, i.e. 0 or 0,1,2,3 or cpu
51
- workers=8, # max dataloader workers (per RANK in DDP mode)
52
- verbose=False, # verbose output
53
- project=ROOT / 'runs/val-cls', # save to project/name
54
- name='exp', # save to project/name
55
- exist_ok=False, # existing project/name ok, do not increment
56
- half=False, # use FP16 half-precision inference
57
- dnn=False, # use OpenCV DNN for ONNX inference
58
- model=None,
59
- dataloader=None,
60
- criterion=None,
61
- pbar=None,
62
- ):
63
- # Initialize/load model and set device
64
- training = model is not None
65
- if training: # called by train.py
66
- device, pt, jit, engine = next(model.parameters()).device, True, False, False # get model device, PyTorch model
67
- half &= device.type != 'cpu' # half precision only supported on CUDA
68
- model.half() if half else model.float()
69
- else: # called directly
70
- device = select_device(device, batch_size=batch_size)
71
-
72
- # Directories
73
- save_dir = increment_path(Path(project) / name, exist_ok=exist_ok) # increment run
74
- save_dir.mkdir(parents=True, exist_ok=True) # make dir
75
-
76
- # Load model
77
- model = DetectMultiBackend(weights, device=device, dnn=dnn, fp16=half)
78
- stride, pt, jit, engine = model.stride, model.pt, model.jit, model.engine
79
- imgsz = check_img_size(imgsz, s=stride) # check image size
80
- half = model.fp16 # FP16 supported on limited backends with CUDA
81
- if engine:
82
- batch_size = model.batch_size
83
- else:
84
- device = model.device
85
- if not (pt or jit):
86
- batch_size = 1 # export.py models default to batch-size 1
87
- LOGGER.info(f'Forcing --batch-size 1 square inference (1,3,{imgsz},{imgsz}) for non-PyTorch models')
88
-
89
- # Dataloader
90
- data = Path(data)
91
- test_dir = data / 'test' if (data / 'test').exists() else data / 'val' # data/test or data/val
92
- dataloader = create_classification_dataloader(path=test_dir,
93
- imgsz=imgsz,
94
- batch_size=batch_size,
95
- augment=False,
96
- rank=-1,
97
- workers=workers)
98
-
99
- model.eval()
100
- pred, targets, loss, dt = [], [], 0, (Profile(), Profile(), Profile())
101
- n = len(dataloader) # number of batches
102
- action = 'validating' if dataloader.dataset.root.stem == 'val' else 'testing'
103
- desc = f"{pbar.desc[:-36]}{action:>36}" if pbar else f"{action}"
104
- bar = tqdm(dataloader, desc, n, not training, bar_format=TQDM_BAR_FORMAT, position=0)
105
- with torch.cuda.amp.autocast(enabled=device.type != 'cpu'):
106
- for images, labels in bar:
107
- with dt[0]:
108
- images, labels = images.to(device, non_blocking=True), labels.to(device)
109
-
110
- with dt[1]:
111
- y = model(images)
112
-
113
- with dt[2]:
114
- pred.append(y.argsort(1, descending=True)[:, :5])
115
- targets.append(labels)
116
- if criterion:
117
- loss += criterion(y, labels)
118
-
119
- loss /= n
120
- pred, targets = torch.cat(pred), torch.cat(targets)
121
- correct = (targets[:, None] == pred).float()
122
- acc = torch.stack((correct[:, 0], correct.max(1).values), dim=1) # (top1, top5) accuracy
123
- top1, top5 = acc.mean(0).tolist()
124
-
125
- if pbar:
126
- pbar.desc = f"{pbar.desc[:-36]}{loss:>12.3g}{top1:>12.3g}{top5:>12.3g}"
127
- if verbose: # all classes
128
- LOGGER.info(f"{'Class':>24}{'Images':>12}{'top1_acc':>12}{'top5_acc':>12}")
129
- LOGGER.info(f"{'all':>24}{targets.shape[0]:>12}{top1:>12.3g}{top5:>12.3g}")
130
- for i, c in model.names.items():
131
- aci = acc[targets == i]
132
- top1i, top5i = aci.mean(0).tolist()
133
- LOGGER.info(f"{c:>24}{aci.shape[0]:>12}{top1i:>12.3g}{top5i:>12.3g}")
134
-
135
- # Print results
136
- t = tuple(x.t / len(dataloader.dataset.samples) * 1E3 for x in dt) # speeds per image
137
- shape = (1, 3, imgsz, imgsz)
138
- LOGGER.info(f'Speed: %.1fms pre-process, %.1fms inference, %.1fms post-process per image at shape {shape}' % t)
139
- LOGGER.info(f"Results saved to {colorstr('bold', save_dir)}")
140
-
141
- return top1, top5, loss
142
-
143
-
144
- def parse_opt():
145
- parser = argparse.ArgumentParser()
146
- parser.add_argument('--data', type=str, default=ROOT / '../datasets/mnist', help='dataset path')
147
- parser.add_argument('--weights', nargs='+', type=str, default=ROOT / 'yolov5s-cls.pt', help='model.pt path(s)')
148
- parser.add_argument('--batch-size', type=int, default=128, help='batch size')
149
- parser.add_argument('--imgsz', '--img', '--img-size', type=int, default=224, help='inference size (pixels)')
150
- parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
151
- parser.add_argument('--workers', type=int, default=8, help='max dataloader workers (per RANK in DDP mode)')
152
- parser.add_argument('--verbose', nargs='?', const=True, default=True, help='verbose output')
153
- parser.add_argument('--project', default=ROOT / 'runs/val-cls', help='save to project/name')
154
- parser.add_argument('--name', default='exp', help='save to project/name')
155
- parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')
156
- parser.add_argument('--half', action='store_true', help='use FP16 half-precision inference')
157
- parser.add_argument('--dnn', action='store_true', help='use OpenCV DNN for ONNX inference')
158
- opt = parser.parse_args()
159
- print_args(vars(opt))
160
- return opt
161
-
162
-
163
- def main(opt):
164
- check_requirements(exclude=('tensorboard', 'thop'))
165
- run(**vars(opt))
166
-
167
-
168
- if __name__ == "__main__":
169
- opt = parse_opt()
170
- main(opt)