|
| 1 | +# deep_tensor |
| 2 | + |
| 3 | +A lightweight, high-performance tensor library for ROS 2 deep learning applications. This package provides a generic tensor container with automatic memory management and seamless conversions between ROS sensor messages and tensor data structures. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Multi-dimensional tensor container** with support for various data types (float32, float64, int8, int16, int32, int64, uint8, uint16, uint32, uint64, bool) |
| 8 | +- **Automatic memory management** with both owned and borrowed memory patterns |
| 9 | +- **ROS message conversions** for sensor_msgs (Image, PointCloud2, LaserScan, IMU) |
| 10 | +- **Batch processing support** for efficient handling of multiple images |
| 11 | +- **Zero-copy operations** where possible for optimal performance |
| 12 | + |
| 13 | +## Basic Usage |
| 14 | + |
| 15 | +Note: more usage patterns can be found in the tests directory. |
| 16 | + |
| 17 | +### Creating Tensors |
| 18 | + |
| 19 | +```cpp |
| 20 | +#include "deep_tensor/tensor.hpp" |
| 21 | + |
| 22 | +using namespace deep_ros; |
| 23 | + |
| 24 | +// Create a 2D tensor (3x4) with float32 data |
| 25 | +std::vector<size_t> shape = {3, 4}; |
| 26 | +Tensor tensor(shape, DataType::FLOAT32); |
| 27 | + |
| 28 | +// Access raw data |
| 29 | +float* data = tensor.data_as<float>(); |
| 30 | + |
| 31 | +// Get tensor properties |
| 32 | +std::cout << "Shape: "; |
| 33 | +for (auto dim : tensor.shape()) { |
| 34 | + std::cout << dim << " "; |
| 35 | +} |
| 36 | +std::cout << "\nRank: " << tensor.rank() << std::endl; |
| 37 | +std::cout << "Total elements: " << tensor.size() << std::endl; |
| 38 | +std::cout << "Bytes: " << tensor.byte_size() << std::endl; |
| 39 | +``` |
| 40 | + |
| 41 | +### Working with Existing Data |
| 42 | + |
| 43 | +```cpp |
| 44 | +// Wrap existing data (non-owning) |
| 45 | +float existing_data[12] = {1.0f, 2.0f, /* ... */}; |
| 46 | +std::vector<size_t> shape = {3, 4}; |
| 47 | +Tensor tensor(existing_data, shape, DataType::FLOAT32); |
| 48 | +``` |
| 49 | +
|
| 50 | +### Tensor Operations |
| 51 | +
|
| 52 | +```cpp |
| 53 | +// Reshape tensor (total size must match) |
| 54 | +auto reshaped = tensor.reshape({2, 6}); |
| 55 | +
|
| 56 | +// Check if tensor is contiguous in memory |
| 57 | +bool contiguous = tensor.is_contiguous(); |
| 58 | +``` |
| 59 | + |
| 60 | +## ROS Message Conversions |
| 61 | + |
| 62 | +### Image Conversions |
| 63 | + |
| 64 | +```cpp |
| 65 | +#include "deep_tensor/ros_conversions.hpp" |
| 66 | + |
| 67 | +using namespace deep_ros::ros_conversions; |
| 68 | + |
| 69 | +// Convert ROS Image to Tensor |
| 70 | +sensor_msgs::msg::Image image_msg; |
| 71 | +// ... populate image_msg ... |
| 72 | + |
| 73 | +// Basic conversion |
| 74 | +Tensor tensor = from_image(image_msg); |
| 75 | + |
| 76 | +// With normalization (uint8 → float32, divided by 255) |
| 77 | +Tensor normalized_tensor = from_image(image_msg, true); |
| 78 | + |
| 79 | +// Convert Tensor back to ROS Image |
| 80 | +std_msgs::msg::Header header; |
| 81 | +header.stamp = rclcpp::Clock().now(); |
| 82 | +auto image_out = to_image(tensor, "rgb8", header); |
| 83 | +``` |
| 84 | +
|
| 85 | +### Batch Image Processing |
| 86 | +
|
| 87 | +```cpp |
| 88 | +// Convert multiple images to a batch tensor |
| 89 | +std::vector<sensor_msgs::msg::Image> images; |
| 90 | +// ... populate images ... |
| 91 | +
|
| 92 | +Tensor batch_tensor = from_image_batch(images, true); |
| 93 | +// Result shape: [batch_size, height, width, channels] |
| 94 | +``` |
| 95 | + |
| 96 | +### Point Cloud Conversions |
| 97 | + |
| 98 | +```cpp |
| 99 | +// Convert PointCloud2 to Tensor |
| 100 | +sensor_msgs::msg::PointCloud2 cloud_msg; |
| 101 | +// ... populate cloud_msg ... |
| 102 | + |
| 103 | +Tensor cloud_tensor = from_pointcloud2(cloud_msg); |
| 104 | +// Result shape: [num_points, num_fields] |
| 105 | +``` |
| 106 | + |
| 107 | +### Laser Scan Conversions |
| 108 | + |
| 109 | +```cpp |
| 110 | +// Convert LaserScan to Tensor |
| 111 | +sensor_msgs::msg::LaserScan scan_msg; |
| 112 | +// ... populate scan_msg ... |
| 113 | + |
| 114 | +Tensor scan_tensor = from_laserscan(scan_msg); |
| 115 | +// Result shape: [num_ranges] or [num_ranges, 2] if intensities present |
| 116 | +``` |
| 117 | + |
| 118 | +### IMU Conversions |
| 119 | + |
| 120 | +```cpp |
| 121 | +// Convert IMU to Tensor |
| 122 | +sensor_msgs::msg::Imu imu_msg; |
| 123 | +// ... populate imu_msg ... |
| 124 | + |
| 125 | +Tensor imu_tensor = from_imu(imu_msg); |
| 126 | +// Result shape: [10] containing [qx,qy,qz,qw,ax,ay,az,gx,gy,gz] |
| 127 | +``` |
| 128 | + |
| 129 | +## Supported Image Encodings |
| 130 | + |
| 131 | +The library supports a wide range of ROS image encodings: |
| 132 | + |
| 133 | +- **RGB/BGR formats**: `rgb8`, `rgba8`, `rgb16`, `rgba16`, `bgr8`, `bgra8`, `bgr16`, `bgra16` |
| 134 | +- **Grayscale**: `mono8`, `mono16` |
| 135 | +- **Bayer patterns**: `bayer_rggb8`, `bayer_bggr8`, `bayer_gbrg8`, `bayer_grbg8` (and 16-bit variants) |
| 136 | +- **OpenCV formats**: `8UC1`, `8UC2`, `8UC3`, `8UC4`, `16SC1`, `32FC1`, `64FC1`, etc. |
| 137 | +- **YUV formats**: `yuv422`, `YUV422_YUY2`, `UYVY`, `YUYV` |
| 138 | + |
| 139 | +## Data Types |
| 140 | + |
| 141 | +Supported tensor data types: |
| 142 | + |
| 143 | +| DataType | C++ Type | Size (bytes) | |
| 144 | +|----------|----------|--------------| |
| 145 | +| FLOAT32 | float | 4 | |
| 146 | +| FLOAT64 | double | 8 | |
| 147 | +| INT8 | int8_t | 1 | |
| 148 | +| INT16 | int16_t | 2 | |
| 149 | +| INT32 | int32_t | 4 | |
| 150 | +| INT64 | int64_t | 8 | |
| 151 | +| UINT8 | uint8_t | 1 | |
| 152 | +| UINT16 | uint16_t | 2 | |
| 153 | +| UINT32 | uint32_t | 4 | |
| 154 | +| UINT64 | uint64_t | 8 | |
| 155 | +| BOOL | bool | 1 | |
| 156 | + |
| 157 | +## Integration in Your Package |
| 158 | + |
| 159 | +### CMakeLists.txt |
| 160 | + |
| 161 | +```cmake |
| 162 | +find_package(deep_tensor REQUIRED) |
| 163 | +
|
| 164 | +target_link_libraries(your_target |
| 165 | + deep_tensor::deep_tensor_lib |
| 166 | +) |
| 167 | +``` |
| 168 | + |
| 169 | +### package.xml |
| 170 | + |
| 171 | +```xml |
| 172 | +<depend>deep_tensor</depend> |
| 173 | +``` |
| 174 | + |
| 175 | +## Performance Considerations |
| 176 | + |
| 177 | +- **Memory Management**: The library uses automatic memory management. Tensors created with the shape constructor own their memory, while tensors created with existing data pointers do not. |
| 178 | +- **Zero-Copy**: When possible, conversions avoid unnecessary data copying. |
| 179 | +- **Contiguous Memory**: Tensors maintain contiguous memory layout for optimal cache performance. |
| 180 | +- **Move Semantics**: Full support for move construction and assignment to minimize copying. |
0 commit comments