venkyyuvy commited on
Commit
60930a3
·
1 Parent(s): c87ccf3

ckpt to pth file

Browse files
Files changed (4) hide show
  1. app.py +9 -14
  2. config.py +176 -0
  3. utils.py +2 -59
  4. epoch=36-step=19166.ckpt → yolov3.pth +2 -2
app.py CHANGED
@@ -1,6 +1,5 @@
1
  import gradio as gr
2
  import torch
3
- from PIL import Image
4
  import cv2
5
  import albumentations as A
6
  from albumentations.pytorch import ToTensorV2
@@ -8,6 +7,7 @@ import matplotlib.pyplot as plt
8
  import matplotlib
9
  matplotlib.use('agg')
10
 
 
11
  from model import YOLOv3
12
  from utils import (
13
  cells_to_bboxes,
@@ -15,23 +15,18 @@ from utils import (
15
  plot_image
16
  )
17
 
18
-
19
- ANCHORS = [
20
- [(0.28, 0.22), (0.38, 0.48), (0.9, 0.78)],
21
- [(0.07, 0.15), (0.15, 0.11), (0.14, 0.29)],
22
- [(0.02, 0.03), (0.04, 0.07), (0.08, 0.06)],
23
- ] # Note these have been rescaled to be between [0, 1]
24
-
25
- fname = 'epoch=36-step=19166.ckpt'
26
- checkpoint = torch.load(fname, map_location=torch.device('cpu'))
27
- model_state_dict = checkpoint['state_dict']
28
  model = YOLOv3(num_classes=20)
29
- model.load_state_dict(model_state_dict)
30
 
31
  IMAGE_SIZE = 416
32
  S = [IMAGE_SIZE // 32, IMAGE_SIZE // 16, IMAGE_SIZE // 8]
33
- anchors = ( torch.tensor(ANCHORS)
34
- * torch.tensor(S).unsqueeze(1)\
35
  .unsqueeze(1).repeat(1, 3, 2)
36
  )
37
 
 
1
  import gradio as gr
2
  import torch
 
3
  import cv2
4
  import albumentations as A
5
  from albumentations.pytorch import ToTensorV2
 
7
  import matplotlib
8
  matplotlib.use('agg')
9
 
10
+ import config
11
  from model import YOLOv3
12
  from utils import (
13
  cells_to_bboxes,
 
15
  plot_image
16
  )
17
 
18
+ # fname = 'epoch=36-step=19166.ckpt'
19
+ fname = 'yolov3.pth'
20
+ # checkpoint = torch.load(fname, map_location=torch.device('cpu'))
21
+ # model_state_dict = checkpoint['state_dict']
22
+ # torch.save(model.state_dict(), 'yolov3.pth')
 
 
 
 
 
23
  model = YOLOv3(num_classes=20)
24
+ model.load_state_dict(torch.load(fname))
25
 
26
  IMAGE_SIZE = 416
27
  S = [IMAGE_SIZE // 32, IMAGE_SIZE // 16, IMAGE_SIZE // 8]
28
+ anchors = ( torch.tensor(config.ANCHORS)
29
+ * torch.tensor(config.S).unsqueeze(1)\
30
  .unsqueeze(1).repeat(1, 3, 2)
31
  )
32
 
config.py ADDED
@@ -0,0 +1,176 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import albumentations as A
2
+ import cv2
3
+
4
+ from albumentations.pytorch import ToTensorV2
5
+
6
+ DATASET='PASCAL_VOC'
7
+ DEVICE = "cpu"
8
+ NUM_WORKERS = 0
9
+ BATCH_SIZE = 16
10
+ IMAGE_SIZE = 416
11
+ NUM_CLASSES = 20
12
+ CONF_THRESHOLD = 0.05
13
+ MAP_IOU_THRESH = 0.5
14
+ NMS_IOU_THRESH = 0.45
15
+ S = [IMAGE_SIZE // 32, IMAGE_SIZE // 16, IMAGE_SIZE // 8]
16
+ PIN_MEMORY = True
17
+ LOAD_MODEL = False
18
+ SAVE_MODEL = True
19
+
20
+ ANCHORS = [
21
+ [(0.28, 0.22), (0.38, 0.48), (0.9, 0.78)],
22
+ [(0.07, 0.15), (0.15, 0.11), (0.14, 0.29)],
23
+ [(0.02, 0.03), (0.04, 0.07), (0.08, 0.06)],
24
+ ] # Note these have been rescaled to be between [0, 1]
25
+
26
+ means = [0.485, 0.456, 0.406]
27
+
28
+ scale = 1.1
29
+ train_transforms = A.Compose(
30
+ [
31
+ A.LongestMaxSize(max_size=int(IMAGE_SIZE * scale)),
32
+ A.PadIfNeeded(
33
+ min_height=int(IMAGE_SIZE * scale),
34
+ min_width=int(IMAGE_SIZE * scale),
35
+ border_mode=cv2.BORDER_CONSTANT,
36
+ ),
37
+ A.Rotate(limit = 10, interpolation=1, border_mode=4),
38
+ A.RandomCrop(width=IMAGE_SIZE, height=IMAGE_SIZE),
39
+ A.ColorJitter(brightness=0.6, contrast=0.6, saturation=0.6, hue=0.6, p=0.4),
40
+ A.OneOf(
41
+ [
42
+ A.ShiftScaleRotate(
43
+ rotate_limit=20, p=0.5, border_mode=cv2.BORDER_CONSTANT
44
+ ),
45
+ # A.Affine(shear=15, p=0.5, mode="constant"),
46
+ ],
47
+ p=1.0,
48
+ ),
49
+ A.HorizontalFlip(p=0.5),
50
+ A.Blur(p=0.1),
51
+ A.CLAHE(p=0.1),
52
+ A.Posterize(p=0.1),
53
+ A.ToGray(p=0.1),
54
+ A.ChannelShuffle(p=0.05),
55
+ A.Normalize(mean=[0, 0, 0], std=[1, 1, 1], max_pixel_value=255,),
56
+ ToTensorV2(),
57
+ ],
58
+ bbox_params=A.BboxParams(format="yolo", min_visibility=0.4, label_fields=[],),
59
+ )
60
+ test_transforms = A.Compose(
61
+ [
62
+ A.LongestMaxSize(max_size=IMAGE_SIZE),
63
+ A.PadIfNeeded(
64
+ min_height=IMAGE_SIZE, min_width=IMAGE_SIZE, border_mode=cv2.BORDER_CONSTANT
65
+ ),
66
+ A.Normalize(mean=[0, 0, 0], std=[1, 1, 1], max_pixel_value=255,),
67
+ ToTensorV2(),
68
+ ],
69
+ bbox_params=A.BboxParams(format="yolo", min_visibility=0.4, label_fields=[]),
70
+ )
71
+
72
+ PASCAL_CLASSES = [
73
+ "aeroplane",
74
+ "bicycle",
75
+ "bird",
76
+ "boat",
77
+ "bottle",
78
+ "bus",
79
+ "car",
80
+ "cat",
81
+ "chair",
82
+ "cow",
83
+ "diningtable",
84
+ "dog",
85
+ "horse",
86
+ "motorbike",
87
+ "person",
88
+ "pottedplant",
89
+ "sheep",
90
+ "sofa",
91
+ "train",
92
+ "tvmonitor"
93
+ ]
94
+
95
+ COCO_LABELS = ['person',
96
+ 'bicycle',
97
+ 'car',
98
+ 'motorcycle',
99
+ 'airplane',
100
+ 'bus',
101
+ 'train',
102
+ 'truck',
103
+ 'boat',
104
+ 'traffic light',
105
+ 'fire hydrant',
106
+ 'stop sign',
107
+ 'parking meter',
108
+ 'bench',
109
+ 'bird',
110
+ 'cat',
111
+ 'dog',
112
+ 'horse',
113
+ 'sheep',
114
+ 'cow',
115
+ 'elephant',
116
+ 'bear',
117
+ 'zebra',
118
+ 'giraffe',
119
+ 'backpack',
120
+ 'umbrella',
121
+ 'handbag',
122
+ 'tie',
123
+ 'suitcase',
124
+ 'frisbee',
125
+ 'skis',
126
+ 'snowboard',
127
+ 'sports ball',
128
+ 'kite',
129
+ 'baseball bat',
130
+ 'baseball glove',
131
+ 'skateboard',
132
+ 'surfboard',
133
+ 'tennis racket',
134
+ 'bottle',
135
+ 'wine glass',
136
+ 'cup',
137
+ 'fork',
138
+ 'knife',
139
+ 'spoon',
140
+ 'bowl',
141
+ 'banana',
142
+ 'apple',
143
+ 'sandwich',
144
+ 'orange',
145
+ 'broccoli',
146
+ 'carrot',
147
+ 'hot dog',
148
+ 'pizza',
149
+ 'donut',
150
+ 'cake',
151
+ 'chair',
152
+ 'couch',
153
+ 'potted plant',
154
+ 'bed',
155
+ 'dining table',
156
+ 'toilet',
157
+ 'tv',
158
+ 'laptop',
159
+ 'mouse',
160
+ 'remote',
161
+ 'keyboard',
162
+ 'cell phone',
163
+ 'microwave',
164
+ 'oven',
165
+ 'toaster',
166
+ 'sink',
167
+ 'refrigerator',
168
+ 'book',
169
+ 'clock',
170
+ 'vase',
171
+ 'scissors',
172
+ 'teddy bear',
173
+ 'hair drier',
174
+ 'toothbrush'
175
+ ]
176
+
utils.py CHANGED
@@ -7,7 +7,6 @@ import random
7
  import torch
8
 
9
  from collections import Counter
10
- from torch.utils.data import DataLoader
11
  from tqdm import tqdm
12
 
13
 
@@ -235,7 +234,8 @@ def mean_average_precision(
235
  def plot_image(image, boxes, return_fig=False):
236
  """Plots predicted bounding boxes on the image"""
237
  cmap = plt.get_cmap("tab20b")
238
- class_labels = config.COCO_LABELS if config.DATASET=='COCO' else config.PASCAL_CLASSES
 
239
  colors = [cmap(i) for i in np.linspace(0, 1, len(class_labels))]
240
  im = np.array(image)
241
  height, width, _ = im.shape
@@ -446,63 +446,6 @@ def load_checkpoint(checkpoint_file, model, optimizer, lr, device):
446
  for param_group in optimizer.param_groups:
447
  param_group["lr"] = lr
448
 
449
-
450
- def get_loaders(train_csv_path, test_csv_path):
451
- from dataset import YOLODataset
452
-
453
- IMAGE_SIZE = config.IMAGE_SIZE
454
- train_dataset = YOLODataset(
455
- train_csv_path,
456
- transform=config.train_transforms,
457
- S=[IMAGE_SIZE // 32, IMAGE_SIZE // 16, IMAGE_SIZE // 8],
458
- img_dir=config.IMG_DIR,
459
- label_dir=config.LABEL_DIR,
460
- anchors=config.ANCHORS,
461
- )
462
- test_dataset = YOLODataset(
463
- test_csv_path,
464
- transform=config.test_transforms,
465
- S=[IMAGE_SIZE // 32, IMAGE_SIZE // 16, IMAGE_SIZE // 8],
466
- img_dir=config.IMG_DIR,
467
- label_dir=config.LABEL_DIR,
468
- anchors=config.ANCHORS,
469
- )
470
- train_loader = DataLoader(
471
- dataset=train_dataset,
472
- batch_size=config.BATCH_SIZE,
473
- num_workers=config.NUM_WORKERS,
474
- pin_memory=config.PIN_MEMORY,
475
- shuffle=True,
476
- drop_last=False,
477
- )
478
- test_loader = DataLoader(
479
- dataset=test_dataset,
480
- batch_size=config.BATCH_SIZE,
481
- num_workers=config.NUM_WORKERS,
482
- pin_memory=config.PIN_MEMORY,
483
- shuffle=False,
484
- drop_last=False,
485
- )
486
-
487
- train_eval_dataset = YOLODataset(
488
- train_csv_path,
489
- transform=config.test_transforms,
490
- S=[IMAGE_SIZE // 32, IMAGE_SIZE // 16, IMAGE_SIZE // 8],
491
- img_dir=config.IMG_DIR,
492
- label_dir=config.LABEL_DIR,
493
- anchors=config.ANCHORS,
494
- )
495
- train_eval_loader = DataLoader(
496
- dataset=train_eval_dataset,
497
- batch_size=config.BATCH_SIZE,
498
- num_workers=config.NUM_WORKERS,
499
- pin_memory=config.PIN_MEMORY,
500
- shuffle=False,
501
- drop_last=False,
502
- )
503
-
504
- return train_loader, test_loader, train_eval_loader
505
-
506
  def plot_couple_examples(model, batch, thresh, iou_thresh, anchors):
507
  model.eval()
508
  x, _ = batch
 
7
  import torch
8
 
9
  from collections import Counter
 
10
  from tqdm import tqdm
11
 
12
 
 
234
  def plot_image(image, boxes, return_fig=False):
235
  """Plots predicted bounding boxes on the image"""
236
  cmap = plt.get_cmap("tab20b")
237
+ class_labels = config.COCO_LABELS if config.DATASET=='COCO' \
238
+ else config.PASCAL_CLASSES
239
  colors = [cmap(i) for i in np.linspace(0, 1, len(class_labels))]
240
  im = np.array(image)
241
  height, width, _ = im.shape
 
446
  for param_group in optimizer.param_groups:
447
  param_group["lr"] = lr
448
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
449
  def plot_couple_examples(model, batch, thresh, iou_thresh, anchors):
450
  model.eval()
451
  x, _ = batch
epoch=36-step=19166.ckpt → yolov3.pth RENAMED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:cd0d67eac457f890a4c9557393e9b4c705dcbac707d30f9cbe658a1b7d00a678
3
- size 740104313
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:af069adc24fec136b92b068e6e6c1361dd4b9dba7797dac7798489e8181d021c
3
+ size 246865311