Spaces:
Sleeping
Sleeping
File size: 10,388 Bytes
d70f24c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
import cv2
import numpy as np
import torch
import matplotlib.pyplot as plt
import os
plt.style.use('ggplot')
# this class keeps track of the training and validation loss values...
# ... and helps to get the average for each epoch as well
class Averager:
def __init__(self):
self.current_total = 0.0
self.iterations = 0.0
def send(self, value):
self.current_total += value
self.iterations += 1
@property
def value(self):
if self.iterations == 0:
return 0
else:
return 1.0 * self.current_total / self.iterations
def reset(self):
self.current_total = 0.0
self.iterations = 0.0
class SaveBestModel:
"""
Class to save the best model while training. If the current epoch's
validation mAP @0.5:0.95 IoU higher than the previous highest, then save the
model state.
"""
def __init__(
self, best_valid_map=float(0)
):
self.best_valid_map = best_valid_map
def __call__(
self,
model,
current_valid_map,
epoch,
OUT_DIR,
config,
model_name
):
if current_valid_map > self.best_valid_map:
self.best_valid_map = current_valid_map
print(f"\nBEST VALIDATION mAP: {self.best_valid_map}")
print(f"\nSAVING BEST MODEL FOR EPOCH: {epoch+1}\n")
torch.save({
'epoch': epoch+1,
'model_state_dict': model.state_dict(),
'config': config,
'model_name': model_name
}, f"{OUT_DIR}/best_model.pth")
def show_tranformed_image(train_loader, device, classes, colors):
"""
This function shows the transformed images from the `train_loader`.
Helps to check whether the tranformed images along with the corresponding
labels are correct or not.
"""
if len(train_loader) > 0:
for i in range(2):
images, targets = next(iter(train_loader))
images = list(image.to(device) for image in images)
targets = [{k: v.to(device) for k, v in t.items()} for t in targets]
boxes = targets[i]['boxes'].cpu().numpy().astype(np.int32)
labels = targets[i]['labels'].cpu().numpy().astype(np.int32)
# Get all the predicited class names.
pred_classes = [classes[i] for i in targets[i]['labels'].cpu().numpy()]
sample = images[i].permute(1, 2, 0).cpu().numpy()
sample = cv2.cvtColor(sample, cv2.COLOR_RGB2BGR)
for box_num, box in enumerate(boxes):
class_name = pred_classes[box_num]
color = colors[classes.index(class_name)]
cv2.rectangle(sample,
(box[0], box[1]),
(box[2], box[3]),
color, 2,
cv2.LINE_AA)
cv2.putText(sample, classes[labels[box_num]],
(box[0], box[1]-10), cv2.FONT_HERSHEY_SIMPLEX,
1.0, color, 2, cv2.LINE_AA)
cv2.imshow('Transformed image', sample)
cv2.waitKey(0)
cv2.destroyAllWindows()
def save_loss_plot(
OUT_DIR,
train_loss_list,
x_label='iterations',
y_label='train loss',
save_name='train_loss_iter'
):
"""
Function to save both train loss graph.
:param OUT_DIR: Path to save the graphs.
:param train_loss_list: List containing the training loss values.
"""
figure_1 = plt.figure(figsize=(10, 7), num=1, clear=True)
train_ax = figure_1.add_subplot()
train_ax.plot(train_loss_list, color='tab:blue')
train_ax.set_xlabel(x_label)
train_ax.set_ylabel(y_label)
figure_1.savefig(f"{OUT_DIR}/{save_name}.png")
print('SAVING PLOTS COMPLETE...')
# plt.close('all')
def save_mAP(OUT_DIR, map_05, map):
"""
Saves the mAP@0.5 and mAP@0.5:0.95 per epoch.
:param OUT_DIR: Path to save the graphs.
:param map_05: List containing mAP values at 0.5 IoU.
:param map: List containing mAP values at 0.5:0.95 IoU.
"""
figure = plt.figure(figsize=(10, 7), num=1, clear=True)
ax = figure.add_subplot()
ax.plot(
map_05, color='tab:orange', linestyle='-',
label='mAP@0.5'
)
ax.plot(
map, color='tab:red', linestyle='-',
label='mAP@0.5:0.95'
)
ax.set_xlabel('Epochs')
ax.set_ylabel('mAP')
ax.legend()
figure.savefig(f"{OUT_DIR}/map.png")
# plt.close('all')
def visualize_mosaic_images(boxes, labels, image_resized, classes):
print(boxes)
print(labels)
image_resized = cv2.cvtColor(image_resized, cv2.COLOR_RGB2BGR)
for j, box in enumerate(boxes):
color = (0, 255, 0)
classn = labels[j]
cv2.rectangle(image_resized,
(int(box[0]), int(box[1])),
(int(box[2]), int(box[3])),
color, 2)
cv2.putText(image_resized, classes[classn],
(int(box[0]), int(box[1]-5)),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, color,
2, lineType=cv2.LINE_AA)
cv2.imshow('Mosaic', image_resized)
cv2.waitKey(0)
def save_model(
epoch,
model,
optimizer,
train_loss_list,
train_loss_list_epoch,
val_map,
val_map_05,
OUT_DIR,
config,
model_name
):
"""
Function to save the trained model till current epoch, or whenever called.
Saves many other dictionaries and parameters as well helpful to resume training.
May be larger in size.
:param epoch: The epoch number.
:param model: The neural network model.
:param optimizer: The optimizer.
:param optimizer: The train loss history.
:param train_loss_list_epoch: List containing loss for each epoch.
:param val_map: mAP for IoU 0.5:0.95.
:param val_map_05: mAP for IoU 0.5.
:param OUT_DIR: Output directory to save the model.
"""
torch.save({
'epoch': epoch+1,
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'train_loss_list': train_loss_list,
'train_loss_list_epoch': train_loss_list_epoch,
'val_map': val_map,
'val_map_05': val_map_05,
'config': config,
'model_name': model_name
}, f"{OUT_DIR}/last_model.pth")
def save_model_state(model, OUT_DIR, config, model_name):
"""
Saves the model state dictionary only. Has a smaller size compared
to the the saved model with all other parameters and dictionaries.
Preferable for inference and sharing.
:param model: The neural network model.
:param OUT_DIR: Output directory to save the model.
"""
torch.save({
'model_state_dict': model.state_dict(),
'config': config,
'model_name': model_name
}, f"{OUT_DIR}/last_model_state.pth")
def denormalize(x, mean=None, std=None):
# Shape of x here should be [B, 3, H, W].
for t, m, s in zip(x, mean, std):
t.mul_(s).add_(m)
# Returns tensor of shape [B, 3, H, W].
return torch.clamp(x, 0, 1)
def save_validation_results(images, detections, counter, out_dir, classes, colors):
"""
Function to save validation results.
:param images: All the images from the current batch.
:param detections: All the detection results.
:param counter: Step counter for saving with unique ID.
"""
IMG_MEAN = [0.485, 0.456, 0.406]
IMG_STD = [0.229, 0.224, 0.225]
image_list = [] # List to store predicted images to return.
for i, detection in enumerate(detections):
image_c = images[i].clone()
# image_c = denormalize(image_c, IMG_MEAN, IMG_STD)
image_c = image_c.detach().cpu().numpy().astype(np.float32)
image = np.transpose(image_c, (1, 2, 0))
image = np.ascontiguousarray(image, dtype=np.float32)
scores = detection['scores'].cpu().numpy()
labels = detection['labels']
bboxes = detection['boxes'].detach().cpu().numpy()
boxes = bboxes[scores >= 0.5].astype(np.int32)
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
# Get all the predicited class names.
pred_classes = [classes[i] for i in labels.cpu().numpy()]
for j, box in enumerate(boxes):
class_name = pred_classes[j]
color = colors[classes.index(class_name)]
cv2.rectangle(
image,
(int(box[0]), int(box[1])),
(int(box[2]), int(box[3])),
color, 2, lineType=cv2.LINE_AA
)
cv2.putText(image, class_name,
(int(box[0]), int(box[1]-5)),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, color,
2, lineType=cv2.LINE_AA)
cv2.imwrite(f"{out_dir}/image_{i}_{counter}.jpg", image*255.)
image_list.append(image[:, :, ::-1])
return image_list
def set_infer_dir():
"""
This functions counts the number of inference directories already present
and creates a new one in `outputs/inference/`.
And returns the directory path.
"""
if not os.path.exists('outputs/inference'):
os.makedirs('outputs/inference')
num_infer_dirs_present = len(os.listdir('outputs/inference/'))
next_dir_num = num_infer_dirs_present + 1
new_dir_name = f"outputs/inference/res_{next_dir_num}"
os.makedirs(new_dir_name, exist_ok=True)
return new_dir_name
def set_training_dir(dir_name=None):
"""
This functions counts the number of training directories already present
and creates a new one in `outputs/training/`.
And returns the directory path.
"""
if not os.path.exists('outputs/training'):
os.makedirs('outputs/training')
if dir_name:
new_dir_name = f"outputs/training/{dir_name}"
os.makedirs(new_dir_name, exist_ok=True)
return new_dir_name
else:
num_train_dirs_present = len(os.listdir('outputs/training/'))
next_dir_num = num_train_dirs_present + 1
new_dir_name = f"outputs/training/res_{next_dir_num}"
os.makedirs(new_dir_name, exist_ok=True)
return new_dir_name
|