@@ -32,19 +32,18 @@ - (nullable instancetype)initWithFileAtPath:(NSString*)filePath {
3232- (NSArray <NSNumber*>*)detectImage : (void *)imageBuffer {
3333 try {
3434 at::Tensor tensor = torch::from_blob (imageBuffer, { 3 , input_width, input_height }, at::kFloat );
35- torch::autograd::AutoGradMode guard (false );
36- at::AutoNonVariableTypeMode non_var_type_mode (true );
37-
35+ c10::InferenceMode guard;
36+
3837 std::vector<torch::Tensor> v;
3938 v.push_back (tensor);
40-
41-
39+
40+
4241 CFTimeInterval startTime = CACurrentMediaTime ();
4342 auto outputTuple = _impl.forward ({ at::TensorList (v) }).toTuple ();
4443 CFTimeInterval elapsedTime = CACurrentMediaTime () - startTime;
4544 NSLog (@" inference time:%f " , elapsedTime);
4645
47-
46+
4847 auto outputDict = outputTuple->elements ()[1 ].toList ().get (0 ).toGenericDict ();
4948 auto boxesTensor = outputDict.at (" boxes" ).toTensor ();
5049 auto scoresTensor = outputDict.at (" scores" ).toTensor ();
@@ -62,7 +61,7 @@ - (nullable instancetype)initWithFileAtPath:(NSString*)filePath {
6261 if (!labelsBuffer) {
6362 return nil ;
6463 }
65-
64+
6665 NSMutableArray * results = [[NSMutableArray alloc ] init ];
6766 long num = scoresTensor.numel ();
6867 for (int i = 0 ; i < num; i++) {
@@ -76,9 +75,9 @@ - (nullable instancetype)initWithFileAtPath:(NSString*)filePath {
7675 [results addObject: @(scoresBuffer[i])];
7776 [results addObject: @(labelsBuffer[i])];
7877 }
79-
78+
8079 return [results copy ];
81-
80+
8281 } catch (const std::exception& exception) {
8382 NSLog (@" %s " , exception.what());
8483 }
0 commit comments