Skip to content

Commit 79e37d0

Browse files
committed
Remove redundanc c++ method, look at changed code.
1 parent 664ee7d commit 79e37d0

7 files changed

Lines changed: 65 additions & 4 deletions

File tree

demos/video/include/cvtool.hpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ namespace cvtool {
2828
}
2929
return default_device;
3030
}
31+
32+
bool can_get_default_device() {
33+
return default_device_set || !torch::mps::is_available();
34+
}
35+
36+
torch::Device get_host_device() {
37+
return torch::Device(torch::kCPU);
38+
}
3139
}
3240

3341
static torch::Device default_device(torch::kCPU);
@@ -92,12 +100,47 @@ std::shared_ptr<at::Tensor> create_frame_buffer_tensor(int height,int width,torc
92100
}
93101

94102
at::Tensor to_tensor(cv::Mat &img) {
103+
104+
95105
auto t = torch::from_blob(img.data, {1, img.rows, img.cols, 3}, torch::kUInt8).clone();
96106
t = t.to(default_device);
97107
t = t.to(torch::kFloat32).permute({0, 3, 1, 2}) / 255.0;
98108
return t;//.to(default_device,true);
99109
}
100110

111+
//--------------------------------------------------------------------
112+
// • img : any H×W×C OpenCV matrix (CV_8U, CV_32F, CV_16F …, planar or packed)
113+
// • device : torch::kCUDA, torch::kMPS or torch::kCPU (default = current CUDA if available)
114+
//--------------------------------------------------------------------
115+
at::Tensor to_tensor_(const cv::Mat& img, torch::Device device = get_default_device())
116+
{
117+
// 1. Make sure the source data are contiguous
118+
cv::Mat contiguous = img.isContinuous() ? img : img.clone();
119+
120+
// 2. Convert pixel type to 32‑bit float in [0,1] so we keep enough
121+
// head‑room for the later FP16 cast. (OpenCV has only limited
122+
// native FP16 support, so converting to CV_32F first is usually
123+
// safer and portable.)
124+
cv::Mat float32;
125+
contiguous.convertTo(float32, CV_32F, 1.0 / 255.0); // scale if img was CV_8U
126+
127+
// 3. Wrap the OpenCV buffer with a *view* tensor (no copy yet).
128+
auto tmp = torch::from_blob(
129+
float32.data, // raw pointer
130+
{float32.rows, float32.cols, float32.channels()},
131+
torch::TensorOptions().dtype(torch::kFloat32));
132+
133+
// 4. Re‑arrange to CHW, move to wanted device, cast to FP16 *and* copy
134+
// so that the returned tensor owns its storage (clone() is mandatory).
135+
auto t = tmp.permute({2, 0, 1}) // HWC → CHW
136+
.to(device, /*dtype=*/torch::kFloat16,
137+
/*non_blocking=*/true, /*copy=*/true) // copy = true ⇒ owns memory
138+
.clone(); // guarantees ownership
139+
140+
return t; // C×H×W, float16, on CUDA / MPS / CPU
141+
}
142+
143+
101144
cv::Mat to_mat(at::Tensor &tensor) {
102145
// Ensure the tensor is on the CPU and not on the GPU
103146
// at::Tensor cpu_tensor = tensor.to(torch::kCPU);
0 Bytes
Binary file not shown.
6.47 MB
Binary file not shown.

demos/video/style-transfer/style_transfer.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
int run_webcam_model(torch::jit::Module& module, int cam_index, int max_fps, bool is_video_loop, std::string vid_path);
1616

17-
static torch::Device default_device_st = torch::Device(torch::kCPU);
17+
static torch::Device default_device_st = torch::Device(torch::kMPS);
1818

1919

2020
torch::jit::Module load_model(const std::string& model_path) {
@@ -48,6 +48,23 @@ torch::Tensor preprocess_input(const torch::Tensor& input) {
4848
}
4949

5050
torch::Tensor run_model(torch::jit::Module& module, const torch::Tensor& input) {
51+
52+
auto input_dtype = input.dtype();
53+
std::cout.flush();
54+
std::cout << "Input dtype: " << input.dtype() << std::endl;
55+
std::cout << "Input sizes: " << input.sizes() << std::endl;
56+
std::cout << "Input device: " << input.device() << std::endl;
57+
std::cout.flush();
58+
std::system("pause");
59+
60+
// auto model_dtype = module.dtype();
61+
// std::cout << "Module: " << module << std::endl;
62+
63+
64+
module.to(torch::kMPS);
65+
module.eval();
66+
67+
5168
std::vector<torch::jit::IValue> inputs;
5269
inputs.push_back(input);
5370

@@ -80,7 +97,7 @@ int main() {
8097

8198
// default_device = default_device_st;
8299

83-
std::string model_path = "style-transfer/models/mosaic.pt";
100+
std::string model_path = "style-transfer/models/mosaic_float32.pt";
84101
torch::jit::Module module = load_model(model_path);
85102
torch::Tensor input = torch::randn({1, 3, 1428, 1904}, device);
86103
torch::Tensor output = run_model(module, input);
Binary file not shown.
Binary file not shown.

examples/pytorch-examples/fast_neural_style/neural_style/neural_style.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,9 @@ def stylize(args):
185185
utils.save_image(args.output_image, output[0])
186186
from pathlib import Path
187187
model_name = Path(args.model).stem
188-
sm = torch.jit.script(style_model)
189-
sm.save(f"models/{model_name}.pt")
188+
189+
sm = torch.jit.script(style_model.to(torch.float32))
190+
sm.save(f"models/{model_name}_float32.pt")
190191

191192
sm = torch.jit.script(style_model.to(torch.float16))
192193
sm.save(f"models/{model_name}_float16.pt")

0 commit comments

Comments
 (0)