JustAGeek commited on
Commit
05c3afb
·
verified ·
1 Parent(s): c3764b7

Delete duplicate/old file: \/utils.py

Browse files
Files changed (1) hide show
  1. ///utils.py +0 -149
///utils.py DELETED
@@ -1,149 +0,0 @@
1
- #!/usr/bin/python
2
- '''
3
- Contains the utilities used for
4
- loading, initating and running up the networks
5
- for all training, validation and evaluation.
6
- '''
7
- import torch
8
- import torch.nn as nn
9
- from torch.nn import init
10
- import functools
11
- from torch.optim import lr_scheduler
12
- import numpy as np
13
- import os
14
- from Generators import *
15
- from mamba_model import MambaDLocNet
16
-
17
- def write_log(log_values, model_name, log_dir="", log_type='loss', type_write='a'):
18
- if not os.path.exists(log_dir):
19
- os.makedirs(log_dir)
20
- with open(log_dir+"/"+model_name+"_"+log_type+".txt", type_write) as f:
21
- f.write(','.join(log_values)+"\n")
22
-
23
- def get_model_funct(model_name):
24
- if model_name == "G":
25
- return define_G
26
-
27
- def define_G(opt, gpu_ids):
28
- net = None
29
- input_nc = opt.input_nc
30
- output_nc = opt.output_nc
31
- ngf = opt.ngf
32
- net_type = opt.base_model
33
- norm = opt.norm
34
- use_dropout = opt.no_dropout
35
- init_type = opt.init_type
36
- init_gain = opt.init_gain
37
-
38
- norm_layer = get_norm_layer(norm_type=norm)
39
-
40
- if net_type == 'resnet_encoder':
41
- n_blocks = opt.resnet_blocks
42
- net = ResnetEncoder(input_nc, output_nc, ngf, norm_layer=norm_layer, use_dropout=use_dropout, n_blocks=n_blocks)
43
- elif net_type == 'resnet_decoder':
44
- n_blocks = opt.resnet_blocks
45
- net = ResnetDecoder(input_nc, output_nc, ngf, norm_layer=norm_layer, use_dropout=use_dropout, n_blocks=n_blocks, encoder_blocks=opt.encoder_res_blocks)
46
- elif net_type == 'mamba_dloc':
47
- net = MambaDLocNet(input_nc=input_nc)
48
- else:
49
- raise NotImplementedError('Generator model name [%s] is not recognized' % net_type)
50
- return init_net(net, init_type, init_gain, gpu_ids)
51
-
52
-
53
- def get_scheduler(optimizer, opt):
54
- if opt.starting_epoch_count=='best' and opt.lr_policy == 'lambda':
55
- def lambda_rule(epoch):
56
- print("lambda update %s, %s, %s", (epoch, 0))
57
- lr_l = 1.0 - max(0, epoch + 1 - opt.niter) / float(opt.niter_decay + 1)
58
- return lr_l
59
- scheduler = lr_scheduler.LambdaLR(optimizer, lr_lambda=lambda_rule)
60
- elif opt.starting_epoch_count!='best' and opt.lr_policy == 'lambda':
61
- def lambda_rule(epoch):
62
- print("lambda update %s, %s, %s", (epoch, opt.starting_epoch_count))
63
- lr_l = 1.0 - max(0, epoch + 1 + opt.starting_epoch_count - opt.niter) / float(opt.niter_decay + 1)
64
- return lr_l
65
- scheduler = lr_scheduler.LambdaLR(optimizer, lr_lambda=lambda_rule)
66
- elif opt.starting_epoch_count!='best' and opt.lr_policy == 'step':
67
- scheduler = lr_scheduler.StepLR(optimizer, step_size=opt.lr_decay_iters, gamma=0.9)
68
- elif opt.starting_epoch_count!='best' and opt.lr_policy == 'plateau':
69
- scheduler = lr_scheduler.ReduceLROnPlateau(optimizer, mode='min', factor=0.2, threshold=0.01, patience=5)
70
- elif opt.starting_epoch_count!='best' and opt.lr_policy == 'cosine':
71
- scheduler = lr_scheduler.CosineAnnealingLR(optimizer, T_max=opt.niter, eta_min=0)
72
- else:
73
- return NotImplementedError('learning rate policy [%s] is not implemented', opt.lr_policy)
74
- return scheduler
75
-
76
-
77
- def get_norm_layer(norm_type='instance'):
78
- if norm_type == 'batch':
79
- norm_layer = functools.partial(nn.BatchNorm2d, affine=True)
80
- elif norm_type == 'instance':
81
- norm_layer = functools.partial(nn.InstanceNorm2d, affine=False, track_running_stats=False)
82
- elif norm_type == 'none':
83
- norm_layer = None
84
- else:
85
- raise NotImplementedError('normalization layer [%s] is not found' % norm_type)
86
- return norm_layer
87
-
88
-
89
- def init_weights(net, init_type='normal', gain=1):
90
- def init_func(m):
91
- classname = m.__class__.__name__
92
- if hasattr(m, 'weight') and (classname.find('Conv') != -1 or classname.find('Linear') != -1):
93
- if init_type == 'normal':
94
- init.normal_(m.weight.data, 0, gain)
95
- elif init_type == 'xavier':
96
- init.xavier_normal_(m.weight.data, gain=gain)
97
- elif init_type == 'kaiming':
98
- init.kaiming_normal_(m.weight.data, a=0, mode='fan_in')
99
- elif init_type == 'orthogonal':
100
- init.orthogonal_(m.weight.data, gain=gain)
101
- else:
102
- raise NotImplementedError('initialization method [%s] is not implemented' % init_type)
103
- if hasattr(m, 'bias') and m.bias is not None:
104
- init.constant_(m.bias.data, 0.0)
105
- elif classname.find('BatchNorm2d') != -1:
106
- init.normal_(m.weight.data, 1.0, gain)
107
- init.constant_(m.bias.data, 0.0)
108
-
109
- print('initialize network with %s' % init_type)
110
- net.apply(init_func)
111
-
112
-
113
- def init_net(net, init_type='normal', init_gain=1, gpu_ids=[]):
114
- if len(gpu_ids) > 0:
115
- assert(torch.cuda.is_available())
116
- device_ = torch.device('cuda:{}'.format(gpu_ids[0]))
117
- # net.to(d)
118
- gpu_ids_int = list(map(int,gpu_ids))
119
- net = torch.nn.DataParallel(net, gpu_ids_int)
120
- net.to(device_)
121
- init_weights(net, init_type, gain=init_gain)
122
- return net
123
-
124
- def localization_error(output_predictions,input_labels,scale=0.1):
125
- """
126
- output_predictions: (N,1,H,W), model prediction
127
- input_labels: (N,1,H,W), ground truth target
128
- """
129
- image_size = output_predictions.shape
130
- error = np.zeros(image_size[0])
131
-
132
- for i in range(image_size[0]):
133
- label_temp = input_labels[i,:,:,:].squeeze() # ground truth label
134
- pred_temp = output_predictions[i,:,:,:].squeeze() # model prediction
135
- label_index = np.asarray(np.unravel_index(np.argmax(label_temp), label_temp.shape))
136
- prediction_index = np.asarray(np.unravel_index(np.argmax(pred_temp),pred_temp.shape))
137
- error[i] = np.sqrt( np.sum( np.power(np.multiply( label_index-prediction_index, scale ), 2)) )
138
-
139
- return error
140
-
141
- class Flatten(nn.Module):
142
- def __init__(self):
143
- super(Flatten, self).__init__()
144
-
145
- def forward(self, x):
146
- return x.view(x.size(0), -1)
147
-
148
- def RGB2Gray(img):
149
- return 0.2125*img[:,:,0] + 0.7154*img[:,:,1] + 0.0721*img[:,:,2]