File size: 30,776 Bytes
58523d7
 
 
 
 
 
 
 
f2c1f2d
b35336d
 
 
 
 
88929e3
 
 
b35336d
77ba3f6
 
bd02ca3
7062fcc
bd02ca3
83d7796
b35336d
 
 
 
 
 
 
 
 
 
 
 
 
 
58523d7
 
e3ec44f
 
 
 
58523d7
 
b35336d
 
 
6593a85
 
 
b35336d
d057364
 
 
 
 
 
eed813e
d057364
 
 
 
 
 
5e98a37
 
 
 
df33c08
 
 
 
 
 
 
 
 
 
 
5e98a37
d057364
 
 
 
 
5e98a37
 
 
 
df33c08
 
 
 
 
 
 
 
 
 
 
 
d057364
 
c8b2b1d
 
 
5e98a37
 
 
 
b35336d
df33c08
 
 
 
 
 
 
 
 
 
 
d057364
6593a85
 
 
 
 
 
99675da
 
 
 
 
 
6593a85
dc07e50
 
5ceac77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40c4e77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ceac77
 
 
 
 
 
 
 
 
 
 
 
6593a85
b35336d
 
d036e93
 
ec26a3c
 
 
 
e3ec44f
b35336d
d036e93
 
 
b35336d
d036e93
 
b35336d
d036e93
 
b35336d
d036e93
b35336d
d036e93
 
b35336d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
534f0c2
 
 
b3129fe
 
 
534f0c2
 
 
b35336d
dc2921b
8b0ac60
d8cf79b
8b0ac60
b35336d
71d05bb
b35336d
52627de
e3ec44f
 
 
 
 
 
 
52627de
 
b35336d
 
 
 
 
 
 
 
 
 
 
 
 
 
04a90cf
e3ec44f
 
 
 
 
 
 
 
 
b35336d
 
 
 
 
 
 
 
 
 
 
 
8b0ac60
 
 
b35336d
8b0ac60
b35336d
8b0ac60
b35336d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b0ac60
58523d7
 
 
 
 
 
386b2fe
8b0ac60
58523d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ecf603
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4cddd05
8b0ac60
 
4cddd05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b35336d
8b0ac60
 
b35336d
8b0ac60
b35336d
 
 
 
 
 
8b0ac60
b35336d
 
 
 
 
8b0ac60
 
 
386b2fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eaa63ed
 
 
 
 
 
 
 
 
75f40a9
71d05bb
58523d7
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
---
datasets:
- quandao92/ad-clip-dataset
metrics:
- f1
base_model:
- openai/clip-vit-base-patch32
---
<div style='text-align: center; font-size: 28px; font-weight: bold'>CLIP 기반 μ œν’ˆ 결함 탐지 λͺ¨λΈ μΉ΄λ“œ</div>

## λͺ¨λΈ 세뢀사항

### λͺ¨λΈ μ„€λͺ…

AnomalyCLIP은 νŠΉμ • 객체에 μ˜μ‘΄ν•˜μ§€ μ•ŠλŠ” ν…μŠ€νŠΈ ν”„λ‘¬ν”„νŠΈλ₯Ό ν•™μŠ΅ν•˜μ—¬ 이미지 λ‚΄μ˜ μ „κ²½ 객체와 상관없이 일반적인 정상 및 비정상 νŒ¨ν„΄μ„ ν¬μ°©ν•˜λŠ” 것을 λͺ©ν‘œλ‘œ ν•©λ‹ˆλ‹€.
이 λͺ¨λΈμ€ CLIP 기반 이상 탐지 기법을 ν™œμš©ν•˜μ—¬ μ œν’ˆ 결함을 νƒμ§€ν•©λ‹ˆλ‹€. 
사전 ν•™μŠ΅λœ CLIP λͺ¨λΈμ„ νŒŒμΈνŠœλ‹(Fine-tuning)ν•˜μ—¬ μ œν’ˆ μ΄λ―Έμ§€μ—μ„œ 결함을 μ‹λ³„ν•˜λ©°, 이λ₯Ό 톡해 생산 라인의 ν’ˆμ§ˆ 관리 및 결함 탐지 μž‘μ—…μ„ μžλ™ν™”ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

- **Developed by:** μœ€μ„λ―Ό
- **Funded by:** SOLUWINS Co., Ltd. (μ†”λ£¨μœˆμŠ€)
- **Referenced by:** zhou2023 anomalyclip [[github](https://github.com/zqhang/AnomalyCLIP.git)]
- **Model type:** CLIP (Contrastive Language-Image Pretraining) - Domain-Agnostic Prompt Learning Model
- **Language(s):** Python
- **License:** Apache 2.0, MIT, OpenAI

### 기술적 μ œν•œμ‚¬ν•­

- λͺ¨λΈμ€ 결함 탐지λ₯Ό μœ„ν•œ μΆ©λΆ„ν•˜κ³  λ‹€μ–‘ν•œ ν›ˆλ ¨ 데이터λ₯Ό ν•„μš”λ‘œ ν•©λ‹ˆλ‹€. ν›ˆλ ¨ 데이터셋이 λΆ€μ‘±ν•˜κ±°λ‚˜ λΆˆκ· ν˜•ν•  경우, λͺ¨λΈμ˜ μ„±λŠ₯이 μ €ν•˜λ  수 μžˆμŠ΅λ‹ˆλ‹€.
- μ‹€μ‹œκ°„ 결함 감지 μ„±λŠ₯은 ν•˜λ“œμ›¨μ–΄ 사양에 따라 λ‹¬λΌμ§ˆ 수 있으며, 높은 ν•΄μƒλ„μ—μ„œ 결함을 νƒμ§€ν•˜λŠ” 정확도가 λ–¨μ–΄μ§ˆ 수 μžˆμŠ΅λ‹ˆλ‹€.
- 결함이 λ―Έμ„Έν•˜κ±°λ‚˜ μ œν’ˆ κ°„ μœ μ‚¬μ„±μ΄ 맀우 높은 경우, λͺ¨λΈμ΄ 결함을 μ •ν™•ν•˜κ²Œ νƒμ§€ν•˜μ§€ λͺ»ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

## ν•™μŠ΅ 세뢀사항

### Hardware
  - **CPU:** Intel Core i9-13900K (24 Cores, 32 Threads)
  - **RAM:** 64GB DDR5
  - **GPU:** NVIDIA RTX 4090Ti 24GB
  - **Storage:** 1TB NVMe SSD + 2TB HDD

### Software
  - **OS:** Windows 11 64 bit/ Ubuntu 20.04LTS
  - **Python:** 3.8 (anaconda)
  - **PyTorch:** 1.9.0
  - **OpenCV:** 4.5.3
  - **Cuda Toolkit:** 11.8
  - **CudDNN:** 9.3.0.75 for cuda11

### 데이터셋 정보

이 λͺ¨λΈμ€ μ œν’ˆμ˜ 정상 이미지와 결함 이미지λ₯Ό μ‚¬μš©ν•˜μ—¬ ν›ˆλ ¨λ©λ‹ˆλ‹€. 
이 λ°μ΄ν„°λŠ” μ œν’ˆμ˜ 이미지, 결함 μ˜μ—­μ— λŒ€ν•œ ground truth 정보, 그리고 기타 κ΄€λ ¨ νŠΉμ„±μ„ ν¬ν•¨ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.
μ΄λ―Έμ§€λŠ” CLIP λͺ¨λΈμ˜ μž…λ ₯ ν˜•μ‹μ— μ ν•©ν•˜λ„λ‘ μ „μ²˜λ¦¬λ˜λ©°, 결함 μ˜μ—­μ˜ 평가λ₯Ό μœ„ν•΄ ground truth λ§ˆν‚Ήμ΄ ν¬ν•¨λ©λ‹ˆλ‹€.

- **데이터 μ†ŒμŠ€:**  https://huggingface.co/datasets/quandao92/ad-clip-dataset
- **데이터 μˆ˜μ§‘ μž₯λΉ„:**
  - μˆ˜μ§‘ H/W: jetson orin nano 8GB
  - 카메라: BFS-U3-89S6C Color Camera
  - 렌즈: 8mm Fiexd Focal Length Lens
  - μ‘°λͺ…: LIDLA-120070
  - 데이터 ν˜•μ‹: .bpm, .jpg
- **데이터 버전 관리:**
   - **1μ°¨ : 20240910_V0_간이 ν™˜κ²½ 데이터 μˆ˜μ§‘**
     데이터 버전 및 μ‚¬μš© 이λ ₯
      - V01: μ „μ²˜λ¦¬ μ „ 데이터 원본 -> 데이터 μˆ˜μ§‘ 원본: 7ea
      - V02: 데이터 λΆ„λ₯˜ -> 정상/λΆˆλŸ‰ λΆ„λ₯˜: 4ea/3ea
      - V03: 데이터 λΆ„λ₯˜, 데이터 νšŒμ „ -> 이미지 증강_45/90/135λ„λ‘œ νšŒμ „_28ea
    <div style="text-align: center;">
    <img src="https://cdn-uploads.huggingface.co/production/uploads/65e7d0935ea025ead9623dde/6kvzgbH81jJrHJECaEspY.png" height="500" width="100%">
    <p>Ground Truth Marking</p>
    </div>
    
    <div style="display: flex; justify-content: space-between;">
      <div style="text-align: center; margin-right: 5px;">
        <img src="https://cdn-uploads.huggingface.co/production/uploads/65e7d0935ea025ead9623dde/_fkcI52_BTcqvQyrJ4EXl.png" height="80%" width="90%" style="margin-right:5px;">
        <p>PCA 뢄포 μ‹œκ°ν™”</p>
      </div>
      <div style="text-align: center; margin-right: 5px;">
        <img src="https://cdn-uploads.huggingface.co/production/uploads/65e7d0935ea025ead9623dde/biaWPJtbm6iwNf7ZqnW5O.png" height="80%" width="90%" style="margin-right:5px;">
        <p>Isolation Forest둜 이상값 식별 κ²°κ³Ό</p>
      </div>
    </div>

   - **2μ°¨ : 20240920_V1_ν•˜μš°μ§• λ‚΄ 이미지 μˆ˜μ§‘**
     데이터 버전 및 μ‚¬μš© 이λ ₯
      - V01: μ „μ²˜λ¦¬ μ „ 데이터 원본 -> 데이터 μˆ˜μ§‘ 원본: 16ea
      - V02: 데이터 λΆ„λ₯˜ -> 정상/λΆˆλŸ‰ λΆ„λ₯˜: 14ea/2ea
      - V03: 데이터 λΆ„λ₯˜, 데이터 νšŒμ „ -> 이미지 증강__64ea
    <div style="text-align: center;">
    <img src="https://cdn-uploads.huggingface.co/production/uploads/65e7d0935ea025ead9623dde/YsP7UwejFabUFp2Im0xWj.png" height="500" width="100%">
    <p>Ground Truth Marking</p>
    </div>

    <div style="display: flex; justify-content: space-between;">
      <div style="text-align: center; margin-right: 5px;">
        <img src="https://cdn-uploads.huggingface.co/production/uploads/65e7d0935ea025ead9623dde/CNFdse5mHQY1KkMb5BYpb.png" height="80%" width="90%" style="margin-right:5px;">
        <p>PCA 뢄포 μ‹œκ°ν™”</p>
      </div>
      <div style="text-align: center; margin-right: 5px;">
        <img src="https://cdn-uploads.huggingface.co/production/uploads/65e7d0935ea025ead9623dde/nRO00DJFT0-B1EJYf8lzK.png" height="80%" width="90%" style="margin-right:5px;">
        <p>Isolation Forest둜 이상값 식별 κ²°κ³Ό</p>
      </div>
    </div>

   - **3μ°¨ : 20241002_V2_μ„€λΉ„ λ‚΄ 데이터 μˆ˜μ§‘**
     데이터 버전 및 μ‚¬μš© 이λ ₯
      - V01: μ „μ²˜λ¦¬ μ „ 데이터 원본 -> 이미지 μˆ˜μ§‘_49개
      - V02: 데이터 λΆ„λ₯˜ -> 정상/λΆˆλŸ‰ λΆ„λ₯˜ μˆ˜ν–‰_error/normal
      - V03: 데이터 λΆ„λ₯˜, 데이터 νšŒμ „ -> 이미지 증강 μˆ˜ν–‰_이미지 νšŒμ „μ„ 톡해 이미지 개수 102개
    <div style="text-align: center;">
    <img src="https://cdn-uploads.huggingface.co/production/uploads/65e7d0935ea025ead9623dde/MFyVWaqr4GDNs8W2mWzGZ.png" height="500" width="100%">
    <p>Ground Truth Marking</p>
    </div>

    <div style="display: flex; justify-content: space-between;">
      <div style="text-align: center; margin-right: 5px;">
        <img src="https://cdn-uploads.huggingface.co/production/uploads/65e7d0935ea025ead9623dde/Kc3EMbY05frUFQh5HbVHn.png" height="80%" width="90%" style="margin-right:5px;">
        <p>PCA 뢄포 μ‹œκ°ν™”</p>
      </div>
      <div style="text-align: center; margin-right: 5px;">
        <img src="https://cdn-uploads.huggingface.co/production/uploads/65e7d0935ea025ead9623dde/SP4R5LjGo2M1Zvby1Bar_.png" height="80%" width="90%" style="margin-right:5px;">
        <p>Isolation Forest둜 이상값 식별 κ²°κ³Ό</p>
      </div>
    </div>

- **Data Configuration:**
  - **이미지 크기 μ‘°μ • 및 μ •κ·œν™”:**
     - μ΄λ―Έμ§€λŠ” μΌμ •ν•œ 크기(예: 518x518)둜 λ¦¬μ‚¬μ΄μ¦ˆλ˜λ©°, CLIP λͺ¨λΈμ˜ μž…λ ₯으둜 μ ν•©ν•˜κ²Œ μ²˜λ¦¬λ©λ‹ˆλ‹€.
     - μ •κ·œν™”λ₯Ό 톡해 ν”½μ…€ 값을 [0, 1] λ²”μœ„λ‘œ λ³€ν™˜ν•©λ‹ˆλ‹€.
  - **Ground Truth λ§ˆν‚Ή:**
     - 결함이 μžˆλŠ” 이미지에 λŒ€ν•΄ 결함 μ˜μ—­μ„ bounding box ν˜•μ‹ λ˜λŠ” binary mask둜 ν‘œμ‹œν•©λ‹ˆλ‹€.
     - λ§ˆν‚Ήλœ 데이터λ₯Ό JSON λ˜λŠ” CSV ν˜•μ‹μœΌλ‘œ μ €μž₯ν•˜μ—¬ λͺ¨λΈ 평가 μ‹œ μ‚¬μš©ν•©λ‹ˆλ‹€.
       
    <div style="text-align: center;">
    <img src="https://cdn-uploads.huggingface.co/production/uploads/65e7d0935ea025ead9623dde/k8GQgaTK7JfQExNpCYpzz.png" height="500" width="100%" style="margin-right:5px;">
    <p>Ground Truth Marking</p>
    </div>

  - **데이터 λΆ„λ₯˜:**
     - Normal: 결함이 μ—†λŠ” 정상 μ œν’ˆμ˜ 이미지.
     - Error: 결함이 μžˆλŠ” μ œν’ˆμ˜ 이미지. 결함 μœ„μΉ˜μ™€ κ΄€λ ¨ 정보가 ν¬ν•¨λ©λ‹ˆλ‹€.
    <div style="display: flex;justify-content: space-between;">
      <div style="text-align: center;margin-right: 5px;">
        <img src="https://cdn-uploads.huggingface.co/production/uploads/65e7d0935ea025ead9623dde/5pGwZ-sptjWjf7WpHifyJ.jpeg" height="400" width="450"">
      </div>
      <div style="text-align: center;justify-content: space-between; margin-right: 5px;">
        <img src="https://cdn-uploads.huggingface.co/production/uploads/65e7d0935ea025ead9623dde/3iihck7VfkXKw9VcIl06x.jpeg" height="400" width="450"">
      </div>
      <div style="text-align: center;justify-content: space-between;margin-right: 5px;">
        <img src="https://cdn-uploads.huggingface.co/production/uploads/65e7d0935ea025ead9623dde/tjsmiXq9pp0K6KSuS1iOS.jpeg" height="400" width="450"">
      </div>
    </div>
    <p style="text-align: center;">Normal Product Images</p>
    
    <div style="display: flex;justify-content: space-between;">
      <div style="text-align: center;margin-right: 5px;">
        <img src="https://cdn-uploads.huggingface.co/production/uploads/65e7d0935ea025ead9623dde/Qv01zDzEM5u8cQYdALrSU.jpeg" height="400" width="450"">
      </div>
      <div style="text-align: center;justify-content: space-between; margin-right: 5px;">
        <img src="https://cdn-uploads.huggingface.co/production/uploads/65e7d0935ea025ead9623dde/B5q_FKiTVXkuElTSlUc4s.jpeg" height="400" width="450"">
      </div>
      <div style="text-align: center;justify-content: space-between;margin-right: 5px;">
        <img src="https://cdn-uploads.huggingface.co/production/uploads/65e7d0935ea025ead9623dde/3pro8oEqMTiEwiwFKcACn.jpeg" height="400" width="450"">
      </div>
    </div>
    <p style="text-align: center;">Error Product Images</p>

### 데이터 라벨링 κ°€μ΄λ“œ
λ³Έ 데이터 라벨링 κ°€μ΄λ“œλŠ” AnomalyDetection 기반 λͺ¨λΈ ν•™μŠ΅μ„ μœ„ν•΄ μˆ˜μ§‘λœ 데이터λ₯Ό λΌλ²¨λ§ν•˜λŠ” κΈ°μ€€κ³Ό ν”„λ‘œμ„ΈμŠ€λ₯Ό λͺ…ν™•νžˆ μ •μ˜ν•©λ‹ˆλ‹€. 
λ°μ΄ν„°λŠ” 주둜 정상(normal) 데이터λ₯Ό μ€‘μ‹¬μœΌλ‘œ κ΅¬μ„±λ˜λ©°, μ΅œμ†Œν•œμ˜ 비정상(anomaly) 데이터λ₯Ό ν¬ν•¨ν•©λ‹ˆλ‹€. 
λ³Έ κ°€μ΄λ“œλŠ” λ°μ΄ν„°μ˜ ν’ˆμ§ˆμ„ μœ μ§€ν•˜κ³  λͺ¨λΈ ν•™μŠ΅ 및 ν…ŒμŠ€νŠΈλ₯Ό μ΅œμ ν™”ν•˜λŠ” 데 λͺ©ν‘œλ₯Ό λ‘‘λ‹ˆλ‹€.
- **라벨링 λ²”μœ„**

  1. **정상(normal) 데이터**:
      - 전체 λ°μ΄ν„°μ˜ μ•½ **95% 이상**을 μ°¨μ§€.
      - λ‹€μ–‘ν•œ ν™˜κ²½ μ‘°κ±΄μ—μ„œ μˆ˜μ§‘λœ 데이터λ₯Ό 포함 (μ‘°λͺ…, 각도, λ°°κ²½ λ“±).
      - 정상적인 μƒνƒœμ˜ κΈˆμ† ν‘œλ©΄, μ •λ°€ν•œ ꡬ쑰, κ· μΌν•œ 광택을 κ°€μ§„ 데이터.
  2. **비정상(anomaly) 데이터**:
      - 전체 λ°μ΄ν„°μ˜ μ•½ 5**% μ΄ν•˜**둜 μ œν•œ.
      - 결함 μœ ν˜•:
          - **Scratch**: 슀크래치.
          - **Contamination**: 얼룩 λ˜λŠ” 이물질.
          - **Crack**: ν‘œλ©΄ κ· μ—΄.
          - **결함 이미지 μ˜ˆμ‹œ**
- **데이터 라벨링 κΈ°μ€€**

  -**1. 파일 넀이밍 κ·œμΉ™**
  
  - 데이터 버전별 파일λͺ…은 λ²„μ „λ³„λ‘œ 상이함.
  - 각 λ²„μ „μ˜ 데이터 관리 λ¬Έμ„œ μ°Έκ³ 
  - 데이터 폴더λͺ…은 **`<μˆ˜μ§‘λ…„μ›”μΌ>_<V버전>_<κ°„λ‹¨ν•œ μ„€λͺ…>`** ν˜•μ‹μœΌλ‘œ μž‘μ„±.
  - μ˜ˆμ‹œ:20240910_V0_간이 ν™˜κ²½ 데이터 μˆ˜μ§‘
  
  - **2. 라벨 메타데이터**
  
  라벨 λ©”νƒ€λ°μ΄ν„°λŠ” csv ν˜•μ‹μœΌλ‘œ μ €μž₯ν•˜λ©°, 각 λ°μ΄ν„°μ˜ 라벨 및 μ„€λͺ…을 포함.
  
  - **ν•„μˆ˜ ν•„λ“œ**:
      - `image_id`: 이미지 파일λͺ….
      - `label`: 정상(`normal`) λ˜λŠ” 비정상(`anomaly`) μ—¬λΆ€.
      - `description`: 상세 μ„€λͺ…(예: 결함 μœ ν˜•).
      
- **μ˜ˆμ‹œ:**
    ```ruby
    {
      "image_id": "normal_20241111_001.jpg",
      "label": "normal",
      "description": "λ§€λ„λŸ¬μš΄ ν‘œλ©΄μ„ κ°€μ§„ 정상적인 κΈˆμ† λΆ€ν’ˆ, 광택이 균일함."
    }
    {
      "image_id": "abnormal_20241111_002.jpg",
      "label": "error",
      "description": "ν‘œλ©΄μ— μ„ ν˜• μŠ€ν¬λž˜μΉ˜κ°€ 발견됨."
    }
    ```


# AD-CLIP Model Architecture
AD-CLIP λͺ¨λΈμ€ CLIP (ViT-B-32)을 백본으둜 μ‚¬μš©ν•˜μ—¬ μ΄λ―Έμ§€μ—μ„œ νŠΉμ§•μ„ μΆ”μΆœν•˜κ³ , λŒ€μ‘° ν•™μŠ΅μ„ 톡해 이상을 νƒμ§€ν•©λ‹ˆλ‹€. 
μ΅œμ’… 좜λ ₯은 이미지가 비정상인지 정상인지λ₯Ό νŒλ³„ν•˜λŠ” 이상 μ μˆ˜μ™€ 각 클래슀의 ν™•λ₯ μ„ μ œκ³΅ν•©λ‹ˆλ‹€.
<div style="display: flex; justify-content: center; align-items: center; flex-direction: column;">
  <img src="https://cdn-uploads.huggingface.co/production/uploads/65e7d0935ea025ead9623dde/62sYcSncxxzqGjQAa0MgQ.png" height="500" width="70%">
  <p>CLIP-based Anomaly Detection Model Architecture</p>
</div>

- **model:**
  - μž…λ ₯ 계측 (Input Layer):
      - μž…λ ₯ 이미지: λͺ¨λΈμ€ 크기 [640, 640, 3]의 이미지λ₯Ό μž…λ ₯λ°›μŠ΅λ‹ˆλ‹€. μ—¬κΈ°μ„œ 640x640은 μ΄λ―Έμ§€μ˜ κ°€λ‘œμ™€ μ„Έλ‘œ 크기이며, 3은 RGB μƒ‰μƒμ˜ 채널 수λ₯Ό λ‚˜νƒ€λƒ…λ‹ˆλ‹€.
      - κΈ°λŠ₯: 이 계측은 μž…λ ₯된 이미지λ₯Ό μ²˜λ¦¬ν•˜κ³  λͺ¨λΈμ˜ λ‚˜λ¨Έμ§€ 뢀뢄에 λ§žλŠ” ν˜•μ‹μœΌλ‘œ 데이터λ₯Ό μ€€λΉ„ν•˜λŠ” 역할을 ν•©λ‹ˆλ‹€.
  - backbone:
      - CLIP (ViT-B-32): λͺ¨λΈμ€ CLIP의 Vision Transformer (ViT-B-32) μ•„ν‚€ν…μ²˜λ₯Ό μ‚¬μš©ν•˜μ—¬ μ΄λ―Έμ§€μ—μ„œ νŠΉμ§•μ„ μΆ”μΆœν•©λ‹ˆλ‹€. ViT-B-32λŠ” 이미지λ₯Ό μ΄ν•΄ν•˜λŠ” 데 ν•„μš”ν•œ κ³ κΈ‰ νŠΉμ„±μ„ μΆ”μΆœν•  수 μžˆλŠ” λŠ₯λ ₯을 κ°€μ§€κ³  μžˆμŠ΅λ‹ˆλ‹€.
      - ν•„ν„°: ν•„ν„° 크기 [32, 64, 128, 256, 512]λŠ” 각 ViT λ ˆμ΄μ–΄μ—μ„œ μ‚¬μš©λ˜λ©°, μ΄λ―Έμ§€μ˜ 각 λ ˆλ²¨μ—μ„œ μ€‘μš”ν•œ 정보λ₯Ό μΆ”μΆœν•˜μ—¬ νŠΉμ§•μ„ ν•™μŠ΅ν•©λ‹ˆλ‹€.
  - neck:
      - 이상 탐지 λͺ¨λ“ˆ (Anomaly Detection Module): 이 λͺ¨λ“ˆμ€ CLIPμ—μ„œ μΆ”μΆœλœ νŠΉμ§•μ„ 기반으둜 이미지λ₯Ό λΆ„μ„ν•˜κ³  이상 μ—¬λΆ€λ₯Ό νŒλ‹¨ν•©λ‹ˆλ‹€. 이 λ‹¨κ³„μ—μ„œλŠ” 이미지 λ‚΄μ—μ„œ 정상과 비정상 데이터λ₯Ό κ΅¬λ³„ν•˜κΈ° μœ„ν•œ μ€‘μš”ν•œ μ²˜λ¦¬κ°€ μ΄λ£¨μ–΄μ§‘λ‹ˆλ‹€.
      - λŒ€μ‘° ν•™μŠ΅ (Contrastive Learning): λŒ€μ‘° ν•™μŠ΅ 방법은 정상 이미지와 비정상 이미지 κ°„μ˜ 차이λ₯Ό ν•™μŠ΅ν•˜μ—¬, μ΄λ―Έμ§€μ˜ 이상 μ—¬λΆ€λ₯Ό λ”μš± λͺ…ν™•ν•˜κ²Œ ꡬ뢄할 수 있게 λ„μ™€μ€λ‹ˆλ‹€.
  - head:
      - 이상 탐지 ν—€λ“œ (Anomaly Detection Head): λͺ¨λΈμ˜ λ§ˆμ§€λ§‰ λΆ€λΆ„μœΌλ‘œ, 이 계측은 이미지가 비정상적인지 정상적인지λ₯Ό κ²°μ •ν•©λ‹ˆλ‹€.
      - outputs:
          - 이상 점수 (Anomaly Score): λͺ¨λΈμ€ 이미지가 이상인지 μ•„λ‹Œμ§€λ₯Ό λ‚˜νƒ€λ‚΄λŠ” 점수(예: 1은 이상, 0은 정상)λ₯Ό 좜λ ₯ν•©λ‹ˆλ‹€.
          - 클래슀 ν™•λ₯  (Class Probabilities): λͺ¨λΈμ€ 각 ν΄λž˜μŠ€μ— λŒ€ν•œ ν™•λ₯ μ„ 좜λ ₯ν•˜λ©°, 이 ν™•λ₯ μ„ 톡해 결함이 μžˆλŠ”μ§€ μ—†λŠ”μ§€μ˜ μ—¬λΆ€λ₯Ό νŒλ‹¨ν•©λ‹ˆλ‹€.

# Optimizer and Loss Function
- **training:**
  - optimizer:
      - name: AdamW  # AdamW μ˜΅ν‹°λ§ˆμ΄μ € (κ°€μ€‘μΉ˜ 감쇠 포함)
      - lr: 0.0001  # ν•™μŠ΅λ₯ 
  - loss:
      - classification_loss: 1.0  # λΆ„λ₯˜ 손싀 (ꡐ차 μ—”νŠΈλ‘œν”Ό)
      - anomaly_loss: 1.0  # 결함 탐지 손싀 (이상 탐지 λͺ¨λΈμ— λŒ€ν•œ 손싀)
      - contrastive_loss: 1.0  # λŒ€μ‘° ν•™μŠ΅ 손싀 (μœ μ‚¬λ„ 기반 손싀)

# Metrics
- **metrics:**
  - Precision  # 정밀도 (Precision)
  - Recall  # μž¬ν˜„μœ¨ (Recall)
  - mAP  # 평균 정밀도 (Mean Average Precision)
  - F1-Score  # F1-점수 (κ· ν˜• 작힌 평가 μ§€ν‘œ)

# Training Parameters
 **ν•˜μ΄νΌνŒŒλΌλ―Έν„° μ„€μ •**
- Learning Rate: 0.001.
- Batch Size: 8.
- Epochs: 200.

# Pre-trained CLIP model
| Model | Download |
| ---     | ---   |
| ViT-B/32 | [download](https://openaipublic.azureedge.net/clip/models/40d365715913c9da98579312b702a82c18be219cc2a73407c4526f58eba950af/ViT-B-32.pt) |
| ViT-B/16 | [download](https://openaipublic.azureedge.net/clip/models/5806e77cd80f8b59890b7e101eabd078d9fb84e6937f9e85e4ecb61988df416f/ViT-B-16.pt) |
| ViT-L/14 | [download](https://openaipublic.azureedge.net/clip/models/b8cca3fd41ae0c99ba7e8951adf17d267cdb84cd88be6f7c2e0eca1737a03836/ViT-L-14.pt) |
| ViT-L/14@336px | [download](https://openaipublic.azureedge.net/clip/models/3035c92b350959924f9f00213499208652fc7ea050643e8b385c2dac08641f02/ViT-L-14-336px.pt) |

# Evaluation Parameters
- F1-score: 90%이상.



# ν•™μŠ΅ μ„±λŠ₯ 및 ν…ŒμŠ€νŠΈ κ²°κ³Ό

- **ν•™μŠ΅μ„±λŠ₯ κ²°κ³Όκ³Ό κ·Έλž˜ν”„**:
<div style="display: flex; justify-content: space-between; margin-bottom: 10px;">
  <div style="text-align: center; margin-right: 20px;">
    <img src="https://cdn-uploads.huggingface.co/production/uploads/65e7d0935ea025ead9623dde/7Q1RzKyia-WNSCJHnk2-d.png" height="80%" width="100%" style="margin-right:5px;">
  </div>
  <div style="text-align: center; margin-right: 20px;">
    <img src="https://cdn-uploads.huggingface.co/production/uploads/65e7d0935ea025ead9623dde/9PyBtPZMACgN1lJOqlVbG.png" height="80%" width="100%" style="margin-right:5px;">
  </div>
</div>
<p style="text-align: center;">ν•™μŠ΅ κ³Όμ • μ˜ˆμ‹œ</p>

<div style="display: flex; justify-content: space-between;">
  <div style="text-align: center; margin-right: 20px;">
    <img src="https://cdn-uploads.huggingface.co/production/uploads/65e7d0935ea025ead9623dde/_lUD77x-yueXycuIn7jya.png" height="80%" width="100%" style="margin-right:5px;">
    <p>1μ°¨ ν•™μŠ΅ μ„±λŠ₯</p>
  </div>
  <div style="text-align: center; margin-right: 20px;">
    <img src="https://cdn-uploads.huggingface.co/production/uploads/65e7d0935ea025ead9623dde/NHDH9N94cI-KqP8k-ASUN.png" height="80%" width="100%" style="margin-right:5px;">
    <p>2μ°¨ ν•™μŠ΅ μ„±λŠ₯</p>
  </div>
  <div style="text-align: center; margin-right: 20px;">
    <img src="https://cdn-uploads.huggingface.co/production/uploads/65e7d0935ea025ead9623dde/6n0DnnQjXD8Ql-p3Owxan.png" height="80%" width="100%" style="margin-right:5px;">
    <p>3μ°¨ ν•™μŠ΅ μ„±λŠ₯</p>
  </div>
</div>

- **ν…ŒμŠ€νŠΈ κ²°κ³Όν‘œ**:
<div style="display: flex; justify-content: space-between;">
  <div style="text-align: center; margin-right: 20px;">
    <img src="https://cdn-uploads.huggingface.co/production/uploads/65e7d0935ea025ead9623dde/u1DQHjXM41DMq1JIUOGlp.png" height="100%" width="100%" style="margin-right:5px;">
  </div>
  <div style="text-align: center; margin-right: 20px;">
    <img src="https://cdn-uploads.huggingface.co/production/uploads/65e7d0935ea025ead9623dde/ndQ60TKlheW8hmOrMBELU.png" height="100%" width="100%" style="margin-right:5px;">
  </div>
</div>

- **ν…ŒμŠ€νŠΈ κ²°κ³Ό**:
<div style="display: flex; justify-content: space-between;">
  <div style="text-align: center; margin-right: 20px;">
    <img src="https://cdn-uploads.huggingface.co/production/uploads/65e7d0935ea025ead9623dde/A91V0GdrcUcX01cC-biG9.png" height="600" width="1000" style="margin-right:5px;">
    <p>Anomaly Product</p>
  </div>
  <div style="text-align: center; margin-right: 20px;">
    <img src="https://cdn-uploads.huggingface.co/production/uploads/65e7d0935ea025ead9623dde/PxleIhphzViTGCubVhWn7.png" height="600" width="1000" style="margin-right:5px;">
    <p>Normal Product</p>
  </div>
</div>



# μ„€μΉ˜ 및 μ‹€ν–‰ 가이라인

이 λͺ¨λΈμ„ μ‹€ν–‰ν•˜λ €λ©΄ Pythonκ³Ό ν•¨κ»˜ λ‹€μŒ λΌμ΄λΈŒλŸ¬λ¦¬κ°€ ν•„μš”ν•©λ‹ˆλ‹€:

- **ftfy==6.2.0**: ν…μŠ€νŠΈ μ •κ·œν™” 및 인코딩 문제λ₯Ό ν•΄κ²°ν•˜λŠ” 라이브러리.
- **matplotlib==3.9.0**: 데이터 μ‹œκ°ν™” 및 κ·Έλž˜ν”„ 생성을 μœ„ν•œ 라이브러리.
- **numpy==1.24.3**: 수치 연산을 μœ„ν•œ 핡심 라이브러리.
- **opencv_python==4.9.0.80**: 이미지 및 λΉ„λ””μ˜€ 처리용 라이브러리.
- **pandas==2.2.2**: 데이터 뢄석 및 μ‘°μž‘μ„ μœ„ν•œ 라이브러리.
- **Pillow==10.3.0**: 이미지 파일 처리 및 λ³€ν™˜μ„ μœ„ν•œ 라이브러리.
- **PyQt5==5.15.10**: GUI μ• ν”Œλ¦¬μΌ€μ΄μ…˜ κ°œλ°œμ„ μœ„ν•œ ν”„λ ˆμž„μ›Œν¬.
- **PyQt5_sip==12.13.0**: PyQt5와 Python κ°„μ˜ μΈν„°νŽ˜μ΄μŠ€λ₯Ό μ œκ³΅ν•˜λŠ” 라이브러리.
- **regex==2024.5.15**: μ •κ·œ ν‘œν˜„μ‹ 처리λ₯Ό μœ„ν•œ 라이브러리.
- **scikit_learn==1.2.2**: 기계 ν•™μŠ΅ 및 데이터 뢄석을 μœ„ν•œ 라이브러리.
- **scipy==1.9.1**: κ³Όν•™ 및 기술 계산을 μœ„ν•œ 라이브러리.
- **setuptools==59.5.0**: Python νŒ¨ν‚€μ§€ 배포 및 μ„€μΉ˜λ₯Ό μœ„ν•œ 라이브러리.
- **scikit-image**: 이미지 처리 및 뢄석을 μœ„ν•œ 라이브러리.
- **tabulate==0.9.0**: ν‘œ ν˜•νƒœλ‘œ 데이터λ₯Ό 좜λ ₯ν•˜λŠ” 라이브러리.
- **thop==0.1.1.post2209072238**: PyTorch λͺ¨λΈμ˜ FLOP 수λ₯Ό κ³„μ‚°ν•˜λŠ” 도ꡬ.
- **timm==0.6.13**: λ‹€μ–‘ν•œ μ΅œμ‹  이미지 λΆ„λ₯˜ λͺ¨λΈμ„ μ œκ³΅ν•˜λŠ” 라이브러리.
- **torch==2.0.0**: PyTorch λ”₯λŸ¬λ‹ ν”„λ ˆμž„μ›Œν¬.
- **torchvision==0.15.1**: 컴퓨터 λΉ„μ „ μž‘μ—…μ„ μœ„ν•œ PyTorch ν™•μž₯ 라이브러리.
- **tqdm==4.65.0**: μ§„ν–‰ 상황을 μ‹œκ°μ μœΌλ‘œ ν‘œμ‹œν•˜λŠ” 라이브러리.
- **pyautogui**: GUI μžλ™ν™”λ₯Ό μœ„ν•œ 라이브러리.

- Install Python libraries
  ```
  pip install -r requirements.txt
  ```


## λͺ¨λΈ μ‹€ν–‰ 단계:

### βœ…Dataset configuration

- Dataset configuration as example below
```
β”œβ”€β”€ data/
β”‚   β”œβ”€β”€ COMP_1/
β”‚   β”‚   β”œβ”€β”€ product_1/
β”‚   β”‚   β”‚   β”œβ”€β”€grouth_truth
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€anomaly_1
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€anomaly_2
β”‚   β”‚   β”‚   β”‚
β”‚   β”‚   β”‚   β”œβ”€β”€test/
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€good
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€anomaly_1
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€anomaly_2
β”‚   β”‚   β”‚   β”‚
β”‚   β”‚   β”‚   β”œβ”€β”€train/
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€good
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€anomaly_1
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€anomaly_2
β”‚   β”‚   β”‚   β”‚ 
β”‚   β”‚   β”œβ”€β”€ product_2/
β”‚   β”‚   β”‚   β”‚
β”‚   β”‚   β”œβ”€β”€ meta.json
β”‚   β”‚   β”‚
β”‚   β”œβ”€β”€ COMP_2/
β”‚   β”‚ 
```

- Generate JSON file storing all the above information of dataset ( -> meta_train.json, meta_test.json)
```ruby
cd dataset_config
python dataset_get_json.py
```

- Making all grouth_truth (only anomaly mask) by hand
```ruby
cd dataset_config
python image_ground_truth.py
```

- Dataset configuration for train and test
```ruby
cd training_libs
python dataset.py
```

 β†’  _ _init_ _ λ©”μ„œλ“œλŠ” λ°μ΄ν„°μ…‹μ˜ 루트 디렉토리, λ³€ν™˜ ν•¨μˆ˜, 데이터셋 이름, λͺ¨λ“œλ₯Ό μž…λ ₯으둜 λ°›μŒ  
 β†’  메타 정보λ₯Ό 담은 JSON 파일 (meta_train.json)을 읽어와 클래슀 이름 λͺ©λ‘κ³Ό λͺ¨λ“  데이터 ν•­λͺ©μ„ λ¦¬μŠ€νŠΈμ— μ €μž₯  
 β†’  generate_class_info ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•˜μ—¬ 클래슀 정보λ₯Ό μƒμ„±ν•˜κ³  클래슀 이름을 클래슀 ID에 λ§€ν•‘  
 β†’  _ _len_ _ λ©”μ„œλ“œλŠ” λ°μ΄ν„°μ…‹μ˜ μƒ˜ν”Œ 수λ₯Ό λ°˜ν™˜   
 β†’  _ _getitem_ _ λ©”μ„œλ“œλŠ” μ£Όμ–΄μ§„ 인덱슀의 μƒ˜ν”Œ 데이터λ₯Ό λ°˜ν™˜  
 β†’  이미지 경둜λ₯Ό 톡해 이미지λ₯Ό 읽고, 이상 여뢀에 따라 마슀크 이미지λ₯Ό 생성  
 β†’  ν•„μš”μ‹œ 이미지와 λ§ˆμŠ€ν¬μ— λ³€ν™˜ ν•¨μˆ˜λ₯Ό 적용  
 β†’  이미지, 마슀크, 클래슀 이름, 이상 μ—¬λΆ€, 이미지 경둜, 클래슀 IDλ₯Ό ν¬ν•¨ν•œ λ”•μ…”λ„ˆλ¦¬λ₯Ό λ°˜ν™˜ 


### βœ… Image pre-processing (transformation) for train and test
```ruby
  training_libs/utils.py
```
```ruby
  AnomalyCLIP_lib/transform.py
```
- **Data Processing Techniques:**
  - normalization:
      description: "이미지 ν”½μ…€ 값을 평균 및 ν‘œμ€€νŽΈμ°¨λ‘œ ν‘œμ€€ν™”"
      method: "'Normalize' from 'torchvision.transforms'"
  - max_resize:
      description: "μ΄λ―Έμ§€μ˜ μ΅œλŒ€ 크기λ₯Ό μœ μ§€ν•˜λ©°, λΉ„μœ¨μ„ λ§žμΆ”κ³  νŒ¨λ”©μ„ μΆ”κ°€ν•˜μ—¬ 크기 μ‘°μ •"
      method: "Custom 'ResizeMaxSize' class"
  - random_resized_crop:
      description: "ν›ˆλ ¨ 쀑에 이미지λ₯Ό 랜덀으둜 자λ₯΄κ³  크기λ₯Ό μ‘°μ •ν•˜μ—¬ λ³€ν˜•μ„ μΆ”κ°€"
      method: "'RandomResizedCrop' from 'torchvision.transforms'"
  - resize:
      description: "λͺ¨λΈ μž…λ ₯에 맞게 이미지λ₯Ό κ³ μ •λœ 크기둜 μ‘°μ •"
      method: "'Resize' with BICUBIC interpolation"
  - center_crop:
      description: "μ΄λ―Έμ§€μ˜ 쀑앙 뢀뢄을 μ§€μ •λœ 크기둜 자λ₯΄κΈ°"
      method: "'CenterCrop'"
  - to_tensor:
      description: "이미지λ₯Ό PyTorch ν…μ„œλ‘œ λ³€ν™˜"
      method: "'ToTensor'"
  - augmentation (optional):
      description: "데이터 증강을 μœ„ν•΄ λ‹€μ–‘ν•œ 랜덀 λ³€ν™˜ 적용, 'AugmentationCfg'둜 μ„€μ • κ°€λŠ₯"
      method: "Uses 'timm' library if specified"

### βœ… Prompt generating
```ruby
  training_lib/prompt_ensemble.py
```
πŸ‘ **Prompts Built in the Code**
1. Normal Prompt: *'["{  }"]'*  
 β†’ Normal Prompt Example: "object"
2. Anomaly Prompt: *'["damaged { }"]'*  
 β†’ Anomaly Prompt Example: "damaged object"

πŸ‘ **Construction Process**
1. *'prompts_pos (Normal)'*: Combines the class name with the normal template
2. *'prompts_neg (Anomaly)'*: Combines the class name with the anomaly template

### βœ… Initial setting for training

- Define the path to the training dataset and model checkpoint saving
```ruby
parser.add_argument("--train_data_path", type=str, default="./data/", help="train dataset path")
parser.add_argument("--dataset", type=str, default='smoke_cloud', help="train dataset name")
parser.add_argument("--save_path", type=str, default='./checkpoint/', help='path to save results')
```

### βœ… Hyper parameters setting

- Set the depth parameter: depth of the embedding learned during prompt training. This affects the model's ability to learn complex features from the data
```ruby
parser.add_argument("--depth", type=int, default=9, help="image size")
```

- Define the size of input images used for training (pixel)
```ruby
parser.add_argument("--image_size", type=int, default=518, help="image size")
```

- Setting parameters for training
```ruby
parser.add_argument("--epoch", type=int, default=500, help="epochs")
parser.add_argument("--learning_rate", type=float, default=0.0001, help="learning rate")
parser.add_argument("--batch_size", type=int, default=8, help="batch size")
```

- Size/depth parameter for the DPAM (Deep Prompt Attention Mechanism)
```ruby
parser.add_argument("--dpam", type=int, default=20, help="dpam size")

1. ViT-B/32 and ViT-B/16: --dpam should be around 10-13
2. ViT-L/14 and ViT-L/14@336px: --dpam should be around 20-24
```
```ruby
β†’ DPAM is used to refine and enhance specific layers of a model, particularly in Vision Transformers (ViT).
β†’ Helps the model focus on important features within each layer through an attention mechanism
β†’ Layers: DPAM is applied across multiple layers, allowing deeper and more detailed feature extraction
β†’ Number of layers DPAM influences is adjustable (--dpam), controlling how much of the model is fine-tuned.
β†’ If you want to refine the entire model, you can set --dpam to the number of layers in the model (e.g., 12 for ViT-B and 24 for ViT-L).
β†’  If you want to focus only on the final layers (where the model usually learns complex features), you can choose fewer DPAM layers.
```

### βœ… Test process

πŸ‘ **Load pre-trained and Fine tuned (Checkpoints) models**
1. Pre-trained mode (./pre-trained model/): 
 ```ruby
β†’ Contains the pre-trained model (ViT-B, ViT-L,....)
β†’ Used as the starting point for training the CLIP model
β†’ Pre-trained model helps speed up and improve training by leveraging previously learned features
```
2. Fine-tuned models (./checkpoint/): 
 ```ruby
β†’ "epoch_N.pth" files in this folder store the model's states during the fine-tuning process.
β†’ Each ".pth" file represents a version of the model fine-tuned from the pre-trained model
β†’ These checkpoints can be used to resume fine-tuning, evaluate the model at different stages, or select the best-performing version
```


# λͺ¨λΈ 곡격 취약점 뢄석
λ³Έ λ¬Έμ„œλŠ” AnomalyCLIP λͺ¨λΈμ˜ 취약점 뢄석 및 μ λŒ€μ  곡격(Adversarial Attacks)에 λŒ€ν•œ λ°©μ–΄ λŒ€μ±…μ„ μ²΄κ³„μ μœΌλ‘œ μˆ˜λ¦½ν•˜κΈ° μœ„ν•΄ μž‘μ„±λ˜μ—ˆμŠ΅λ‹ˆλ‹€. 
λͺ¨λΈμ˜ μ‹ λ’°μ„±κ³Ό μ•ˆμ •μ„±μ„ ν™•λ³΄ν•˜κ³  데이터 무결성을 μœ μ§€ν•˜κΈ° μœ„ν•΄, 데이터 및 λͺ¨λΈ μˆ˜μ€€μ˜ λ°©μ–΄ μ „λž΅μ„ κ΅¬ν˜„ν•˜κ³  μ„±λŠ₯을 ν‰κ°€ν•œ κ²°κ³Όλ₯Ό ν¬ν•¨ν•©λ‹ˆλ‹€.
## **1. 취약점 뢄석**
- ### ** μ λŒ€μ  곡격 μ‹œλ‚˜λ¦¬μ˜€**
1. **Adversarial Examples:**
    - **μ„€λͺ…:** μž…λ ₯ 데이터에 μž‘μ€ λ…Έμ΄μ¦ˆλ₯Ό μΆ”κ°€ν•˜μ—¬ λͺ¨λΈμ˜ μ˜ˆμΈ‘μ„ μ™œκ³‘.
    - **예:** 정상 이미지λ₯Ό 결함 μ΄λ―Έμ§€λ‘œ μ˜ˆμΈ‘ν•˜λ„λ‘ μœ λ„.
2. **Data Poisoning:**
    - **μ„€λͺ…:** ν•™μŠ΅ 데이터에 μ•…μ˜μ  데이터λ₯Ό μ‚½μž…ν•˜μ—¬ λͺ¨λΈ ν•™μŠ΅μ„ μ™œκ³‘.
    - **예:** 비정상 데이터λ₯Ό 정상 λ°μ΄ν„°λ‘œ ν•™μŠ΅μ‹œν‚€λŠ” 경우.
3. **Evasion Attacks:**
    - **μ„€λͺ…:** μΆ”λ‘  μ‹œ λͺ¨λΈμ˜ λΆ„λ₯˜ κ²°κ³Όλ₯Ό μ‘°μž‘.
    - **예:** 결함 데이터λ₯Ό μ •μƒμœΌλ‘œ μ˜ˆμΈ‘ν•˜λ„λ‘ μœ λ„.

- ### **λͺ¨λΈ 및 데이터셋 영ν–₯**
  - **μ„±λŠ₯ μ €ν•˜:** μ λŒ€μ  μƒ˜ν”Œ μž…λ ₯ μ‹œ λͺ¨λΈμ˜ 정확도 κ°μ†Œ.
  - **무결성 손상:** 데이터 λ³€μ‘°λ‘œ 인해 ν•™μŠ΅λœ λͺ¨λΈμ΄ μ‹€μ œ ν™˜κ²½μ—μ„œ 신뒰성을 상싀.
  - **μ•…μ˜μ  ν™œμš© κ°€λŠ₯μ„±:** λͺ¨λΈμ˜ μ˜μ‚¬κ²°μ •μ΄ μ˜€μž‘λ™ν•˜μ—¬ 생산 ν’ˆμ§ˆ 관리 μ‹€νŒ¨ κ°€λŠ₯μ„± 증가.

## **2. λŒ€μ‘ λ°©μ•ˆ**

- ### ** 데이터 μˆ˜μ€€ λ°©μ–΄ λŒ€μ±…**
  1. **데이터 μ •μ œ:**
      - νλ¦Ών•˜κ±°λ‚˜ 잘린 이미지 제거.
      - 데이터 λ…Έμ΄μ¦ˆ 제거 및 결함 볡ꡬ.
      - **κ²°κ³Ό:** 데이터 ν’ˆμ§ˆ κ°•ν™”λ‘œ μ λŒ€μ  λ…Έμ΄μ¦ˆ 효과 κ°μ†Œ.
  2. **데이터 증강(Data Augmentation):**
      - 랜덀 νšŒμ „, 크기 μ‘°μ •, 밝기 및 λŒ€λΉ„ μ‘°μ •.
      - Gaussian Noise 및 Salt-and-Pepper Noise μΆ”κ°€.
      - **κ²°κ³Ό:** 데이터 λ‹€μ–‘μ„± 확보 및 λͺ¨λΈ μΌλ°˜ν™” μ„±λŠ₯ κ°•ν™”.
  3. **데이터 무결성 검증:**
      - 각 데이터 ν•΄μ‹œκ°’(MD5) μ €μž₯ 및 μœ„λ³€μ‘° μ—¬λΆ€ 확인.
      - **κ²°κ³Ό:** 데이터셋 μ‹ λ’°μ„± 및 무결성 보μž₯.

- ### **λͺ¨λΈ μˆ˜μ€€ λ°©μ–΄ λŒ€μ±…**
  1. **Adversarial Training:**
      - FGSM 기반의 μ λŒ€μ  μƒ˜ν”Œμ„ ν•™μŠ΅ 데이터에 포함.
      - **κ²°κ³Ό:** μ λŒ€μ  μƒ˜ν”Œμ—μ„œλ„ 평균 정확도 5% ν–₯상.
  2. **Gradient Masking:**
      - κ·Έλž˜λ””μ–ΈνŠΈλ₯Ό 숨겨 λͺ¨λΈμ΄ μ λŒ€μ  곡격에 λ…ΈμΆœλ˜μ§€ μ•Šλ„λ‘ λ°©μ–΄.
  3. **Temperature Scaling:**
      - λͺ¨λΈμ˜ 예츑 ν™•λ₯ μ„ μ‘°μ •ν•˜μ—¬ μ λŒ€μ  μƒ˜ν”Œ 민감도 μ™„ν™”.

- ### **μ‹œμŠ€ν…œ μˆ˜μ€€ λ°©μ–΄ λŒ€μ±…**
  1. **μ‹€μ‹œκ°„ 탐지 및 λŒ€μ‘:**
      - μž…λ ₯ λ°μ΄ν„°μ˜ 이상 νŒ¨ν„΄μ„ μ‹€μ‹œκ°„μœΌλ‘œ νƒμ§€ν•˜λŠ” μ‹œμŠ€ν…œ ꡬ좕.
      - **κ²°κ³Ό:** μ λŒ€μ  곡격 λ°œμƒ μ‹œ 즉각적인 κ²½κ³  및 λŒ€μ‘ κ°€λŠ₯.
  2. **μžλ™ν™”λœ λ°©μ–΄ 도ꡬ:**
      - Adversarial Examples 생성 및 λ°©μ–΄ ν…ŒμŠ€νŠΈ μžλ™ν™”.

## **3. μ‹€ν—˜ κ²°κ³Ό**

- ### **평가 데이터**

  - **데이터셋 ꡬ성:**
      - 정상 데이터: 110건
      - 결함 데이터: 10건
      - μ λŒ€μ  데이터(FGSM 곡격): 100건

- ### **μ£Όμš” μ„±λŠ₯ μ§€ν‘œ**
λ©”νŠΈλ¦­            | κΈ°λ³Έ 데이터  | μ λŒ€μ  데이터 | λ³€ν™”μœ¨
-----------------|-------------|---------------|--------
Accuracy         | 98%         | 92%           | -6%
F1 Score         | 0.935       | 0.91          | -2.5%
False Positive   | 2%          | 5%            | +3%
False Negative   | 3%          | 7%            | +4%

## **4. ν–₯ν›„ κ³„νš**
  1. **λ‹€μ–‘ν•œ 곡격 기법 ν…ŒμŠ€νŠΈ:**
      - PGD, DeepFool λ“± μƒˆλ‘œμš΄ 곡격 기법 적용 및 평가.
  2. **λͺ¨λΈ κ°œμ„ :**
      - Contrastive Learning 및 앙상블 ν•™μŠ΅μ„ ν†΅ν•œ 견고성 κ°•ν™”.
  3. **μ‹€μ‹œκ°„ λ°©μ–΄ μ‹œμŠ€ν…œ ꡬ좕:**
      - λͺ¨λΈμ˜ μ‹€μ‹œκ°„ 예츑 데이터λ₯Ό λΆ„μ„ν•˜μ—¬ μ λŒ€μ  μž…λ ₯ 탐지 및 차단.



# References
- AnomalyCLIP: Object-agnostic Prompt Learning for Zero-shot Anomaly Detection [[github](https://github.com/zqhang/AnomalyCLIP.git)]