| import random |
|
|
| import numpy as np |
| import torch |
| from PIL import Image |
|
|
| |
| |
| |
| |
| def cvtColor(image): |
| if len(np.shape(image)) == 3 and np.shape(image)[2] == 3: |
| return image |
| else: |
| image = image.convert('RGB') |
| return image |
|
|
| |
| |
| |
| def resize_image(image, size): |
| w, h = size |
| new_image = image.resize((w, h), Image.BICUBIC) |
| return new_image |
|
|
| |
| |
| |
| def get_classes(classes_path): |
| with open(classes_path, encoding='utf-8') as f: |
| class_names = f.readlines() |
| class_names = [c.strip() for c in class_names] |
| return class_names, len(class_names) |
|
|
| |
| |
| |
| def get_lr(optimizer): |
| for param_group in optimizer.param_groups: |
| return param_group['lr'] |
|
|
| |
| |
| |
| def seed_everything(seed=11): |
| random.seed(seed) |
| np.random.seed(seed) |
| torch.manual_seed(seed) |
| torch.cuda.manual_seed(seed) |
| torch.cuda.manual_seed_all(seed) |
| torch.backends.cudnn.deterministic = True |
| torch.backends.cudnn.benchmark = False |
|
|
| |
| |
| |
| def worker_init_fn(worker_id, rank, seed): |
| worker_seed = rank + seed |
| random.seed(worker_seed) |
| np.random.seed(worker_seed) |
| torch.manual_seed(worker_seed) |
|
|
| def preprocess_input(image): |
| image /= 255.0 |
| return image |
|
|
| def show_config(**kwargs): |
| print('Configurations:') |
| print('-' * 70) |
| print('|%25s | %40s|' % ('keys', 'values')) |
| print('-' * 70) |
| for key, value in kwargs.items(): |
| print('|%25s | %40s|' % (str(key), str(value))) |
| print('-' * 70) |
|
|
| def get_new_img_size(height, width, img_min_side=600): |
| if width <= height: |
| f = float(img_min_side) / width |
| resized_height = int(f * height) |
| resized_width = int(img_min_side) |
| else: |
| f = float(img_min_side) / height |
| resized_width = int(f * width) |
| resized_height = int(img_min_side) |
|
|
| return resized_height, resized_width |
|
|