1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
| 把pt文件放到assets目录下面
sourceSets { main { assets.srcDirs = ['src/main/assets'] } }
implementation 'org.pytorch:pytorch_android:1.12.1' // PyTorch Android implementation 'org.pytorch:pytorch_android_torchvision:1.12.1' // 包含TorchVision
public static void example(Context context) throws IOException { int airType = 0; int M = 3000; int inputSize = 1; int maxRows = 3000; // CSV 数据总行数 String csvFilename = "air_data.csv";
// PT 模型路径 String ptModelPath = context.getFilesDir() + "/air_model_1.pt"; copyAssetToInternal(context,"air_model_1.pt", ptModelPath);
// 1. 读取 CSV float[][] data = readCSVFromAssets(context, csvFilename, airType, maxRows); int length = data.length;
// 2. 构造 trainX 和 x float[][] trainX = new float[M][inputSize]; float[][] x = new float[length][inputSize]; for (int i = 0; i < M; i++) trainX[i][0] = data[i][0]; for (int i = 0; i < length; i++) x[i][0] = data[i][0];
// 3. 设置训练时 mean/std(必须和训练时一致) float trainMean = 12.90614f; float trainStd = 8.80730f;
// 4. 加载 PT 模型 Module module = Module.load(ptModelPath); if (module != null) { Log.d("Torch", "Module 非空,说明加载成功"); } else { Log.e("Torch", "Module 是 null,加载失败"); }
// float[][] out = predict(module, trainX, x, trainStd, trainMean, inputSize, length, M);
// getWarn(module,data,0,3000,inputSize,length,10,3.0f,3.0f,trainMean,trainStd); // 设置阈值 0.1,可根据训练数据调整 // boolean[] abnormal = detectAnomalies(out, x, 0.1f); // Result result = new Result(out, abnormal); // 5. 调用模型 forward
float[][] data2 = readCSVFromAssetsAll(context, csvFilename, maxRows);
// getPtData(data2,module,context); // getTestPt(module); // Object[] results = DateutilsGetDataTest.getDataTest(context,inputSize,60,csvFilename,3);//测试数据 Object[] results = DateutilsGetDataTest.getDataAndroidWithLog(context,inputSize,60,csvFilename,1);//真实数据 getVcsGetDatePt(results,module); }
//获取cvs数据,归一化 反归一化 public static void getVcsGetDatePt(Object[] results,Module module){ int length = 3120; int inputsize = 1;
Tensor testXTensor = (Tensor) results[0]; Tensor testYTensor = (Tensor) results[1]; int lengethtensor = (int) results[2]; float mean= (float) results[3]; float std = (float) results[4];
Tensor trainMeanTensor = Tensor.fromBlob(new float[]{mean}, new long[]{1}); Tensor trainStdTensor = Tensor.fromBlob(new float[]{std}, new long[]{1}); Tensor inputSizeTensor = Tensor.fromBlob(new long[]{inputsize}, new long[]{1}); Tensor lengthTensor = Tensor.fromBlob(new long[]{lengethtensor}, new long[]{1});
IValue[] inputs = new IValue[]{ IValue.from(testXTensor), IValue.from(testYTensor), IValue.from(trainStdTensor), IValue.from(trainMeanTensor), IValue.from(inputSizeTensor), // int IValue.from(lengthTensor) // int };
IValue output = module.forward(inputs);
// 解析输出 (模型返回 Tuple(Tensor, Tensor)) Tensor outTensor = output.toTuple()[0].toTensor(); float[] outArray = outTensor.getDataAsFloatArray();
// 打印前5行 Log.i("xqm", "PT model output 前5行:"); for (int i = 0; i < Math.min(5, length); i++) { Log.i("xqm", String.valueOf(outArray[i])); }
IValue[] outputs = output.toTuple(); Tensor firstOutput = outputs[0].toTensor(); // 第一个预测结果 Tensor Tensor secondOutput = outputs[1].toTensor(); // 第二个 Tensor,如果需要 firstFifity(firstOutput,mean,std);
float[] outFlat = outTensor.getDataAsFloatArray();
float[][] out_o = new float[length][inputsize]; for (int i = 0; i < length; i++) { for (int j = 0; j < inputsize; j++) { out_o[i][j] = outFlat[i * inputsize + j] * std + mean; } }
for (int i = 0; i < Math.min(5, length); i++) { StringBuilder sb = new StringBuilder(); for (int j = 0; j < inputsize; j++) { sb.append(out_o[i][j]).append(","); } Log.d("TorchOut", "xqm Android output line " + i + ": " + sb.toString()); }
}
public static void firstFifity(Tensor outputTensor, float trainMean, float trainStd){ float[] outputArray = outputTensor.getDataAsFloatArray();
// 反归一化 float[] realOutput = new float[outputArray.length]; for (int i = 0; i < outputArray.length; i++) { realOutput[i] = outputArray[i] * trainStd + trainMean; }
// 打印前50个预测值 int printCount = Math.min(50, realOutput.length); for (int i = 0; i < printCount; i++) { Log.d("InferenceOutput", "Pred[" + i + "] = " + realOutput[i]); } }
// 从 assets 拷贝模型到内部存储 public static void copyAssetToInternal(Context context,String assetName, String destPath) { try { InputStream is = context.getAssets().open(assetName); File outFile = new File(destPath); FileOutputStream os = new FileOutputStream(outFile); byte[] buffer = new byte[1024]; int read; while ((read = is.read(buffer)) != -1) { os.write(buffer, 0, read); } is.close(); os.flush(); os.close(); } catch (IOException e) { e.printStackTrace(); } }
//读取csv列表数据,并组装成pt需要的数据返回 public static Object[] getDataAndroidWithLog(Context context,int inputsize, int M, String assetFileName, int airType) throws IOException { // 1. 读取 assets 目录下 CSV AssetManager assetManager = context.getAssets(); InputStream is = assetManager.open(assetFileName); BufferedReader br = new BufferedReader(new InputStreamReader(is));
br.readLine(); // 第一行是表头,跳过 List<Float> dataList = new ArrayList<>(); String line; while ((line = br.readLine()) != null) { String[] tokens = line.split(","); dataList.add(Float.parseFloat(tokens[airType])); } br.close();
dataList = new ArrayList<>(dataList.subList(0, Math.min(3120, dataList.size())));//后续删掉
int totalLength = dataList.size(); float[] data = new float[totalLength]; for (int i = 0; i < totalLength; i++) data[i] = dataList.get(i);
Log.d("xqm", "总数据: " + totalLength);
Log.d("xqm", "原始数据前10条: " + Arrays.toString(Arrays.copyOf(data, Math.min(10, totalLength))));
// 2. 均值和标准差 float sum = 0f; for (float v : data) sum += v; float mean = sum / totalLength;
float std = 0f; for (float v : data) std += (v - mean) * (v - mean); std = (float)Math.sqrt(std / totalLength);
Log.d("xqm", "mean: " + mean + ", std: " + std);
// 3. 归一化 float[] normalizedData = new float[totalLength]; if (std != 0f) { for (int i = 0; i < totalLength; i++) normalizedData[i] = (data[i] - mean) / std; } else { for (int i = 0; i < totalLength; i++) normalizedData[i] = data[i] - mean; } Log.d("xqm", "归一化后前10条: " + Arrays.toString(Arrays.copyOf(normalizedData, Math.min(10, totalLength))));
// 4. Tensor 处理 int lengthTest = totalLength; // 全部数据作为测试集 float[] testY = new float[lengthTest * inputsize]; float[] testXFlat = new float[lengthTest * inputsize];
for (int i = 0; i < lengthTest; i++) { for (int j = 0; j < inputsize; j++) { // testY 是反归一化 if (std != 0f) testY[i * inputsize + j] = normalizedData[i] * std + mean; else testY[i * inputsize + j] = normalizedData[i] + mean;
// testXFlat 是归一化 if (std != 0f) testXFlat[i * inputsize + j] = (testY[i * inputsize + j] - mean) / std; else testXFlat[i * inputsize + j] = testY[i * inputsize + j] - mean; } }
Log.d("xqm", "testY 前10条: " + Arrays.toString(Arrays.copyOf(testY, Math.min(10, testY.length)))); Log.d("xqm", "testXFlat 前10条: " + Arrays.toString(Arrays.copyOf(testXFlat, Math.min(10, testXFlat.length))));
// 5. 保证 reshape 可整除 int newLengthTest = (lengthTest / M) * M; float[] testXFlatTrim = Arrays.copyOf(testXFlat, newLengthTest * inputsize);
Tensor testXTensor = Tensor.fromBlob(testXFlatTrim, new long[]{M, newLengthTest / M, inputsize}); Tensor testYTensor = Tensor.fromBlob(testY, new long[]{1,lengthTest, inputsize});
return new Object[]{testXTensor, testYTensor, lengthTest, mean, std}; }
|