@@ -67,11 +67,11 @@ def initialize(self, context):
67
67
self .max_db = None
68
68
69
69
def add_to_avg (self , value ):
70
- self .avg_db_historical = (self .avg_db_historical * self .avg_size + value ) / (self .avg_size + 1 )
70
+ self .avg_db_historical = (self .avg_db_historical * self .avg_size + value ) / (
71
+ self .avg_size + 1
72
+ )
71
73
self .avg_size += 1
72
74
73
-
74
-
75
75
def _load_torchscript_model (self , model_pt_path ):
76
76
"""Loads the PyTorch model and returns the NN model object.
77
77
@@ -90,7 +90,7 @@ def _load_torchscript_model(self, model_pt_path):
90
90
# model_checkpoint = torch.load(model_pt_path)
91
91
# num_classes = len(model_checkpoint["classifier.bias"])
92
92
# model = efficientnet_b4(
93
- # num_classes=num_classes
93
+ # num_classes=num_classes
94
94
# )
95
95
# model.load_state_dict(model_checkpoint)
96
96
@@ -126,15 +126,15 @@ def handle(self, data, context):
126
126
output , _ = self ._infer_with_profiler (data = data )
127
127
else :
128
128
raise RuntimeError (
129
- "Profiler is enabled but current version of torch does not support."
130
- "Install torch>=1.8.1 to use profiler."
129
+ "Profiler is enabled but current version of torch does not support."
130
+ "Install torch>=1.8.1 to use profiler."
131
131
)
132
132
else :
133
133
if self ._is_describe ():
134
134
output = [self .describe_handle ()]
135
135
elif self .gate (data ):
136
136
print (f"\n NO SIGNAL \n " )
137
- output = [{"No signal" :None }]
137
+ output = [{"No signal" : None }]
138
138
else :
139
139
data_preprocess = self .preprocess (data )
140
140
@@ -164,13 +164,12 @@ def gate(self, data):
164
164
165
165
data = torch .tensor (np .frombuffer (body , dtype = np .complex64 ), dtype = torch .cfloat )
166
166
167
- avg_pwr = 10 * torch .log10 (torch .mean (torch .abs (data )** 2 ))
167
+ avg_pwr = 10 * torch .log10 (torch .mean (torch .abs (data ) ** 2 ))
168
168
if self .max_db is None or self .max_db < avg_pwr :
169
169
self .max_db = avg_pwr
170
-
170
+
171
171
self .add_to_avg (avg_pwr )
172
172
173
-
174
173
print ("\n =====================================\n " )
175
174
print ("\n =====================================\n " )
176
175
print (f"\n { data = } \n " )
@@ -179,15 +178,11 @@ def gate(self, data):
179
178
print ("\n =====================================\n " )
180
179
print ("\n =====================================\n " )
181
180
182
- if avg_pwr > ( self .max_db + self .avg_db_historical )/ 2 :
181
+ if avg_pwr > (self .max_db + self .avg_db_historical ) / 2 :
183
182
return False
184
183
185
184
return True
186
185
187
-
188
-
189
-
190
-
191
186
def preprocess (self , data ):
192
187
"""
193
188
Transform raw input into model input data.
@@ -200,16 +195,15 @@ def preprocess(self, data):
200
195
print ("could not parse body from request: " , data )
201
196
raise ValueError from exc
202
197
203
-
204
198
data = torch .tensor (np .frombuffer (body , dtype = np .complex64 ), dtype = torch .cfloat )
205
199
print ("\n =====================================\n " )
206
200
print (f"\n { data = } \n " )
207
201
print (f"\n { torch .min (torch .abs (data )** 2 )= } , { torch .max (torch .abs (data )** 2 )= } \n " )
208
- avg_pwr = torch .mean (torch .abs (data )** 2 )
202
+ avg_pwr = torch .mean (torch .abs (data ) ** 2 )
209
203
avg_pwr_db = 10 * torch .log10 (avg_pwr )
210
204
print (f"\n { avg_pwr = } \n " )
211
205
print (f"\n { avg_pwr_db = } \n " )
212
- data = data * 1 / torch .norm (data , float ("inf" ), keepdim = True )
206
+ data = data * 1 / torch .norm (data , float ("inf" ), keepdim = True )
213
207
data = torch .view_as_real (data )
214
208
data = torch .movedim (data , - 1 , 0 ).unsqueeze (0 )
215
209
# data should be of size (N, 2, n_samples)
@@ -237,7 +231,9 @@ def postprocess(self, inference_output):
237
231
confidences , class_indexes = torch .max (inference_output .data , 1 )
238
232
results = {
239
233
str (class_index ): [{"confidence" : confidence }]
240
- for class_index , confidence in zip (class_indexes .tolist (), confidences .tolist ())
234
+ for class_index , confidence in zip (
235
+ class_indexes .tolist (), confidences .tolist ()
236
+ )
241
237
}
242
238
print (f"\n { inference_output = } \n { results = } \n " )
243
239
return [results ]
0 commit comments