-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
104 lines (75 loc) · 2.49 KB
/
utils.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
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
import torch
import shutil
import os
import pickle
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
# def save_checkpoint(state, is_best, filename='checkpoint.pth.tar'):
# torch.save(state, filename)
# if is_best:
# print('save the best model for now')
# shutil.copyfile(filename, 'model_best.pth.tar')
def save_checkpoint(state, is_best, dir, filename='model_best.pth.tar'):
if is_best:
print('save the best model for now')
torch.save(state, os.path.join(dir,filename))
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def get_lambda(use_weight):
if use_weight:
input = open('split.pkl', 'r')
data = pickle.load(input)
train_score = list(zip(*data['train'])[1])
train_score.sort()
# base: avoid 0:1000 -> base+0: base+1000 , add two base
hist, _ = np.histogram(train_score, bins=100, range=(0,100))
mount=max(hist)
hist=hist.astype('float32')
base=1.5
weights=(base-hist/mount)/base
else:
weights = np.ones(100)
# plt.ylim(0,3)
# plt.plot(weights)
# plt.show()
return weights
# left_weights=np.ones(class_num)
def load_image(filename, size=None, scale=None):
img = Image.open(filename)
if size is not None:
img = img.resize((size, size), Image.ANTIALIAS)
elif scale is not None:
img = img.resize((int(img.size[0] / scale), int(img.size[1] / scale)), Image.ANTIALIAS)
return img
def save_image(filename, data):
img = data.clone().clamp(0, 255).numpy()
img = img.transpose(1, 2, 0).astype("uint8")
img = Image.fromarray(img)
img.save(filename)
def gram_matrix(y):
(b, ch, h, w) = y.size()
features = y.view(b, ch, w * h)
features_t = features.transpose(1, 2)
gram = features.bmm(features_t) / (ch * h * w)
return gram
def normalize_batch(batch):
# normalize using imagenet mean and std
mean = batch.new_tensor([0.485, 0.456, 0.406]).view(-1, 1, 1)
std = batch.new_tensor([0.229, 0.224, 0.225]).view(-1, 1, 1)
batch = batch.div(255.0)
return (batch - mean) / std
if __name__ == '__main__':
get_lambda(100)