From e64b1f15baef138a0957024772e116d877f8e65b Mon Sep 17 00:00:00 2001 From: "gayoun.park" Date: Thu, 9 Apr 2026 15:05:16 +0900 Subject: [PATCH] =?UTF-8?q?json=20=EC=83=9D=EC=84=B1=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/ModelTestMetricsJobService.java | 79 +++++++++++-------- 1 file changed, 44 insertions(+), 35 deletions(-) diff --git a/src/main/java/com/kamco/cd/training/train/service/ModelTestMetricsJobService.java b/src/main/java/com/kamco/cd/training/train/service/ModelTestMetricsJobService.java index 69de8ef..3cdcda6 100644 --- a/src/main/java/com/kamco/cd/training/train/service/ModelTestMetricsJobService.java +++ b/src/main/java/com/kamco/cd/training/train/service/ModelTestMetricsJobService.java @@ -220,8 +220,8 @@ public class ModelTestMetricsJobService { } /** - * 기존 방식: F-Score 기준 통합 ZIP 파일 생성 파일명: {modelNo}.{modelVer}.{uuid}.zip 포함 파일: model_config.py, - * best_changed_fscore_epoch_N.pth, {modelVersion}.json, yolov8_6th-6m.pt + * 기존 방식: F-Score 기준 통합 ZIP 파일 생성 파일명: {modelVersion}.zip (예: G4_000001.zip) 포함 파일: + * model_config.py, best_changed_fscore_epoch_N.pth, {modelVersion}.json, yolov8_6th-6m.pt * * @param modelInfo 모델 정보 * @param responsePath Response 디렉토리 경로 @@ -239,12 +239,22 @@ public class ModelTestMetricsJobService { return; } - // 2. JSON 파일명: {modelVersion}.json (예: G4_000001.json) - String legacyJsonFileName = jsonDto.getModelVersion() + ".json"; - Path legacyJsonPath = responsePath.resolve(legacyJsonFileName); - writeJsonFile(jsonDto, legacyJsonPath); + // 2. JSON 파일 생성: {modelVersion}.json (예: G4_000001.json) + try { + writeJsonFile( + jsonDto, + Paths.get( + responseDir + + "/" + + modelInfo.getUuid() + + "/" + + jsonDto.getModelVersion() + + ".json")); + } catch (IOException e) { + throw new RuntimeException(e); + } - log.info("JSON 파일 생성 완료: {}", legacyJsonFileName); + log.info("JSON 파일 생성 완료: {}.json", jsonDto.getModelVersion()); // 3. Best Epoch 파일명 찾기 (F-Score 기준) ModelTestFileName fileInfo = @@ -257,48 +267,47 @@ public class ModelTestMetricsJobService { log.info("Best Epoch 파일명: {}.pth", fileInfo.getBestEpochFileName()); - // 4. ZIP에 포함할 파일 리스트 + // 4. ZIP 파일 경로: {modelVersion}.zip (예: G4_000001.zip) + Path zipPath = + Paths.get( + responseDir + "/" + modelInfo.getUuid() + "/" + fileInfo.getModelVersion() + ".zip"); + + // 5. ZIP에 포함할 파일 리스트 Set targetNames = Set.of( "model_config.py", fileInfo.getBestEpochFileName() + ".pth", // best_changed_fscore_epoch_N.pth - legacyJsonFileName); // {modelVersion}.json + fileInfo.getModelVersion() + ".json"); // {modelVersion}.json - List filesToZip = new ArrayList<>(); + List files = new ArrayList<>(); // Response 폴더에서 파일 수집 try (Stream stream = Files.list(responsePath)) { - filesToZip.addAll( + files.addAll( stream .filter(Files::isRegularFile) .filter(p -> targetNames.contains(p.getFileName().toString())) .collect(Collectors.toList())); } - log.info("Response 폴더에서 수집한 파일 {}개", filesToZip.size()); + log.info("Response 폴더에서 수집한 파일 {}개", files.size()); // PT 파일 추가 (사전학습 모델) - Path ptPath = findPretrainedModel(); - if (ptPath != null) { - filesToZip.add(ptPath); - log.info("사전학습 모델 추가: {}", ptPath.getFileName()); - } else { - log.warn("사전학습 모델(.pt) 파일을 찾을 수 없습니다."); + try (Stream stream = Files.list(Path.of(ptPathDir))) { + files.addAll( + stream + .filter(Files::isRegularFile) + .limit(1) // yolov8_6th-6m.pt 파일 1개만 + .collect(Collectors.toList())); } - // 5. ZIP 파일명 생성: {modelNo}.{modelVer}.{uuid}.zip - String legacyZipFileName = - String.format( - "%s.zip", jsonDto.getModelVersion() // G4.G4_000001.uuid - ); - - Path legacyZipPath = responsePath.resolve(legacyZipFileName); + log.info("최종 ZIP에 포함할 파일 {}개", files.size()); // 6. ZIP 압축 - zipFiles(filesToZip, legacyZipPath); + zipFiles(files, zipPath); - long zipSize = Files.size(legacyZipPath); - log.info("기존 방식 ZIP 파일 생성 완료: fileName={}, size={} bytes", legacyZipFileName, zipSize); + long zipSize = Files.size(zipPath); + log.info("기존 방식 ZIP 파일 생성 완료: fileName={}, size={} bytes", zipPath.getFileName(), zipSize); } catch (Exception e) { log.error("기존 방식 ZIP 파일 생성 실패: modelId={}", modelInfo.getModelId(), e); @@ -321,8 +330,8 @@ public class ModelTestMetricsJobService { // best_changed_{어떤문자든}_epoch_{숫자}.pth 형식의 모든 파일 검색 Pattern pattern = Pattern.compile("best_changed_(.+?)_epoch_(\\d+)\\.pth"); - log.info("📂 Best PTH 파일 검색 시작: path={}", responsePath); - log.info("🔍 검색 패턴: best_changed_{{metricType}}_epoch_{{N}}.pth"); + log.info("Best PTH 파일 검색 시작: path={}", responsePath); + log.info("검색 패턴: best_changed_{{metricType}}_epoch_{{N}}.pth"); try (Stream stream = Files.list(responsePath)) { stream @@ -350,8 +359,8 @@ public class ModelTestMetricsJobService { log.info("Best PTH 파일 검색 완료: 총 {}개 발견", bestFiles.size()); if (bestFiles.isEmpty()) { - log.warn(" Best PTH 파일이 하나도 발견되지 않았습니다. 파일명 패턴을 확인하세요."); - log.warn(" 예상 패턴: best_changed_{{metricType}}_epoch_{{N}}.pth"); + log.warn("Best PTH 파일이 하나도 발견되지 않았습니다. 파일명 패턴을 확인하세요."); + log.warn("예상 패턴: best_changed_{{metricType}}_epoch_{{N}}.pth"); // 디버깅: response 폴더의 모든 .pth 파일 출력 try (Stream debugStream = Files.list(responsePath)) { @@ -363,10 +372,10 @@ public class ModelTestMetricsJobService { .collect(Collectors.toList()); if (!allPthFiles.isEmpty()) { - log.info(" Response 폴더의 .pth 파일 목록:"); + log.info("Response 폴더의 .pth 파일 목록:"); allPthFiles.forEach(name -> log.info(" - {}", name)); } else { - log.warn(" Response 폴더에 .pth 파일이 전혀 없습니다."); + log.warn("Response 폴더에 .pth 파일이 전혀 없습니다."); } } catch (IOException e) { log.error("디버깅 중 에러 발생", e); @@ -449,7 +458,7 @@ public class ModelTestMetricsJobService { String zipFileName = String.format( "%s.%s.zip", - metricJson.getModelVersion(), // G1.G1_000001.{uuid} + metricJson.getModelVersion(), // G1_000001 bestPth.getMetricType()); // fscore/precision/recall Path zipPath = responsePath.resolve(zipFileName);