-
Notifications
You must be signed in to change notification settings - Fork 3
/
helpers.py
148 lines (127 loc) · 4.26 KB
/
helpers.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
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
'''
Created on 16 Nov 2017
@author: vermav1
'''
from time import gmtime, strftime
import torch
import numpy as np
import pandas as pd
import os
import shutil
import matplotlib.pyplot as plt
class Cutout(object):
"""Randomly mask out one or more patches from an image.
Args:
n_holes (int): Number of patches to cut out of each image.
length (int): The length (in pixels) of each square patch.
"""
def __init__(self, n_holes, length):
self.n_holes = n_holes
self.length = length
def apply(self, img):
"""
Args:
img (Tensor): Tensor image of size (C, H, W).
Returns:
Tensor: Image with n_holes of dimension length x length cut out of it.
"""
h = img.size(2)
w = img.size(3)
mask = np.ones((h, w), np.float32)
for n in range(self.n_holes):
y = np.random.randint(h)
x = np.random.randint(w)
y1 = int(np.clip(y - self.length / 2, 0, h))
y2 = int(np.clip(y + self.length / 2, 0, h))
x1 = int(np.clip(x - self.length / 2, 0, w))
x2 = int(np.clip(x + self.length / 2, 0, w))
mask[y1: y2, x1: x2] = 0.
mask = torch.from_numpy(mask)
mask = mask.expand_as(img)
img = img * mask
return img
def experiment_name(cod=True,
cod_trainable=False,
aux_nets=2,
opt='sgd',
epochs=400,
batch_size=64,
test_batch_size=1000,
lr=0.01,
momentum=0.5,
data_aug=1,
manualSeed=None,
job_id=None,
add_name=''):
if cod:
exp_name = 'cod_true'
if cod_trainable:
exp_name+='_trainable_true'
else:
exp_name+='_trainable_false'
else:
exp_name = 'cod_false'
exp_name+='_auxnets_'+str(aux_nets)
exp_name+='_opt_'+str(opt)
exp_name+='_epochs_'+str(epochs)
exp_name +='_batch_size_'+str(batch_size)
exp_name+='_test_batch_size_'+str(test_batch_size)
exp_name += '_lr_'+str(lr)
exp_name += '_momentum_'+str(momentum)
exp_name += '_data_aug_'+str(data_aug)
if manualSeed!=None:
exp_name += '_manuael_seed_'+str(manualSeed)
if job_id!=None:
exp_name += '_job_id_'+str(job_id)
if add_name!='':
exp_name += '_add_name_'+str(add_name)
# exp_name += strftime("_%Y-%m-%d_%H:%M:%S", gmtime())
print('experiement name: ' + exp_name)
return exp_name
def experiment_name_non_mnist(arch='',
aux_nets=2,
epochs=400,
dropout=True,
batch_size=64,
lr=0.01,
momentum=0.5,
data_aug=1,
manualSeed=None,
job_id=None,
add_name=''):
exp_name= str(arch)
exp_name+='_auxnets_'+str(aux_nets)
exp_name += '_epochs_'+str(epochs)
if dropout:
exp_name+='_dropout_'+'true'
else:
exp_name+='_dropout_'+'False'
exp_name +='_batch_size_'+str(batch_size)
exp_name += '_lr_'+str(lr)
exp_name += '_momentum_'+str(momentum)
exp_name += '_data_aug_'+str(data_aug)
if manualSeed!=None:
exp_name += '_manuael_seed_'+str(manualSeed)
if job_id!=None:
exp_name += '_job_id_'+str(job_id)
if add_name!='':
exp_name += '_add_name_'+str(add_name)
# exp_name += strftime("_%Y-%m-%d_%H:%M:%S", gmtime())
print('experiement name: ' + exp_name)
return exp_name
def copy_script_to_folder(caller_path, folder):
script_filename = caller_path.split('/')[-1]
script_relative_path = os.path.join(folder, script_filename)
# Copying script
shutil.copy(caller_path, script_relative_path)
def cyclic_lr(initial_lr,step,total_steps,num_cycles):
factor=np.ceil(float(total_steps)/num_cycles)
theta=np.pi*np.mod(step-1,factor)/factor
return (initial_lr/2)*(np.cos(theta)+1)
if __name__ == '__main__':
lr_list=[]
for i in xrange(1000):
lr=cyclic_lr(0.1,i+1,1100,3)
lr_list.append(lr)
plt.plot(np.asarray(lr_list))
plt.show()