-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodels.py
90 lines (60 loc) · 2.96 KB
/
models.py
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
import numpy as np
import torch
import torch.nn as nn
# =============================================================================
# APACHE 2.0 / https://github.com/christophschuhmann/improved-aesthetic-predictor/blob/main/LICENSE
# - Credit: https://github.com/christophschuhmann/improved-aesthetic-predictor
# - - I reused the trained model
# =============================================================================
class MLP(nn.Module):
def __init__(self, input_size):
super().__init__()
self.input_size = input_size
self.layers = nn.Sequential(
nn.Linear(self.input_size, 1024), nn.Dropout(0.2),
nn.Linear(1024, 128), nn.Dropout(0.2),
nn.Linear(128, 64), nn.Dropout(0.1),
nn.Linear(64, 16),
nn.Linear(16, 1))
def forward(self, x):
return self.layers(x)
# =============================================================================
# Misc
# =============================================================================
import torchvision.transforms.functional as FT
#normalize tensor
def normalized_pt(b, axis=-1):
l2 = torch.norm(b)
l2[l2 == 0] = 1
return b / l2.reshape(1,1)
#get transforms to try
def get_transform_to_params(mode='soft'):
if mode == 'hard':
a = 0.5
transform_to_params = {}
transform_to_params[FT.adjust_brightness] = {'brightness_factor':((a, 1/a),)}
transform_to_params[FT.adjust_contrast] = {'contrast_factor':((a, 1/a),)}
transform_to_params[FT.adjust_gamma] = {'gamma':((a, 1/a),), 'gain':((0.9,1.1),)}
transform_to_params[FT.adjust_hue] = {'hue_factor':((-0.1, 0.1),)}
transform_to_params[FT.invert] = {}
transform_to_params[FT.solarize] = {'threshold':((0, 1),)}
transform_to_params[FT.posterize] = {'bits': (np.random.randint,(0,9))}
transform_to_params[FT.adjust_saturation] = {'saturation_factor':((a,1/a),)}
transform_to_params[FT.adjust_sharpness] = {'sharpness_factor':((a, 1/a),)}
transform_to_params[FT.autocontrast] = {}
# transform_to_params[FT.gaussian_blur] = {'kernel_size': (np.random.randint,(1, 5))}
transform_to_params[FT.equalize] = {}
print('- HARD MODE')
elif mode == 'soft':
a = 0.9
transform_to_params = {}
transform_to_params[FT.adjust_brightness] = {'brightness_factor':((a, 1/a),)}
transform_to_params[FT.adjust_contrast] = {'contrast_factor':((a, 1/a),)}
transform_to_params[FT.adjust_gamma] = {'gamma':((a, 1/a),), 'gain':((0.9,1.1),)}
transform_to_params[FT.adjust_saturation] = {'saturation_factor':((a,1/a),)}
transform_to_params[FT.adjust_sharpness] = {'sharpness_factor':((a, 1/a),)}
transform_to_params[FT.autocontrast] = {}
transform_to_params[FT.gaussian_blur] = {'kernel_size': (np.random.randint,(1, 3))}
transform_to_params[FT.equalize] = {}
print('- SOFT MODE')
return transform_to_params