pranav2711 commited on
Commit
42ce65a
·
verified ·
1 Parent(s): 38beae7

Upload 2 files

Browse files
Files changed (2) hide show
  1. CudaCheck.ipynb +649 -0
  2. ViTFINAL.ipynb +0 -0
CudaCheck.ipynb ADDED
@@ -0,0 +1,649 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": null,
6
+ "id": "4c4eb645",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": [
10
+ "import torch\n",
11
+ "print(\"CUDA Available:\", torch.cuda.is_available())\n",
12
+ "print(\"CUDA Device Name:\", torch.cuda.get_device_name(0) if torch.cuda.is_available() else \"No GPU Found\")"
13
+ ]
14
+ },
15
+ {
16
+ "cell_type": "code",
17
+ "execution_count": null,
18
+ "id": "bff87546",
19
+ "metadata": {},
20
+ "outputs": [],
21
+ "source": [
22
+ "# ViT Training Script for Digital Forensics using DFD Frame Dataset with Intel GPU (XPU)\n",
23
+ "\n",
24
+ "import os\n",
25
+ "import glob\n",
26
+ "import torch\n",
27
+ "import torch.nn as nn\n",
28
+ "import torch.optim as optim\n",
29
+ "from torch.utils.data import Dataset, DataLoader\n",
30
+ "from torchvision import transforms\n",
31
+ "from PIL import Image\n",
32
+ "import timm\n",
33
+ "from tqdm import tqdm\n",
34
+ "import random\n",
35
+ "import intel_extension_for_pytorch as ipex\n",
36
+ "\n",
37
+ "# ----------------------------\n",
38
+ "# CONFIGURATION\n",
39
+ "# ----------------------------\n",
40
+ "DATASET_DIR = 'frames' # Path to 'frames/real' and 'frames/fake'\n",
41
+ "BATCH_SIZE = 32\n",
42
+ "EPOCHS = 10\n",
43
+ "LEARNING_RATE = 3e-5\n",
44
+ "NUM_CLASSES = 2\n",
45
+ "IMG_SIZE = 224\n",
46
+ "DEVICE = 'xpu' if torch.xpu.is_available() else 'cpu'\n",
47
+ "MODEL_SAVE_PATH = './vit_digital_forensics_xpu.pth'\n",
48
+ "\n",
49
+ "print(\"--- Starting ViT Training on Digital Forensics Frames ---\")\n",
50
+ "print(\"Using device:\", DEVICE)\n",
51
+ "if DEVICE == 'xpu':\n",
52
+ " print(\"Intel Extension for PyTorch (IPEX) is active.\")\n",
53
+ "\n",
54
+ "# ----------------------------\n",
55
+ "# DATASET CLASS\n",
56
+ "# ----------------------------\n",
57
+ "class ForgeryImageDataset(Dataset):\n",
58
+ " def __init__(self, root_dir, transform=None):\n",
59
+ " self.samples = []\n",
60
+ " self.transform = transform\n",
61
+ " for label, name in enumerate(['real', 'fake']):\n",
62
+ " folder = os.path.join(root_dir, name)\n",
63
+ " for img_path in glob.glob(os.path.join(folder, '*.jpg')):\n",
64
+ " self.samples.append((img_path, label))\n",
65
+ "\n",
66
+ " real_samples = [s for s in self.samples if s[1] == 0]\n",
67
+ " fake_samples = [s for s in self.samples if s[1] == 1]\n",
68
+ " min_len = min(len(real_samples), len(fake_samples))\n",
69
+ "\n",
70
+ " self.samples = random.sample(real_samples, min_len) + random.sample(fake_samples, min_len)\n",
71
+ " random.shuffle(self.samples)\n",
72
+ "\n",
73
+ " def __len__(self):\n",
74
+ " return len(self.samples)\n",
75
+ "\n",
76
+ " def __getitem__(self, idx):\n",
77
+ " img_path, label = self.samples[idx]\n",
78
+ " image = Image.open(img_path).convert('RGB')\n",
79
+ " if self.transform:\n",
80
+ " image = self.transform(image)\n",
81
+ " return image, label\n",
82
+ "\n",
83
+ "# ----------------------------\n",
84
+ "# TRANSFORMS & LOADER\n",
85
+ "# ----------------------------\n",
86
+ "transform = transforms.Compose([\n",
87
+ " transforms.Resize((IMG_SIZE, IMG_SIZE)),\n",
88
+ " transforms.RandomHorizontalFlip(),\n",
89
+ " transforms.RandomRotation(10),\n",
90
+ " transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2),\n",
91
+ " transforms.ToTensor(),\n",
92
+ " transforms.Normalize([0.5]*3, [0.5]*3)\n",
93
+ "])\n",
94
+ "\n",
95
+ "dataset = ForgeryImageDataset(DATASET_DIR, transform=transform)\n",
96
+ "loader = DataLoader(dataset, batch_size=BATCH_SIZE, shuffle=True, num_workers=4, pin_memory=True)\n",
97
+ "\n",
98
+ "# ----------------------------\n",
99
+ "# MODEL SETUP with IPEX Optimizations\n",
100
+ "# ----------------------------\n",
101
+ "model = timm.create_model('vit_base_patch16_224', pretrained=True, num_classes=NUM_CLASSES)\n",
102
+ "model = model.to(memory_format=torch.channels_last)\n",
103
+ "model.to(DEVICE)\n",
104
+ "\n",
105
+ "criterion = nn.CrossEntropyLoss()\n",
106
+ "optimizer = optim.AdamW(model.parameters(), lr=LEARNING_RATE)\n",
107
+ "\n",
108
+ "# Apply Intel IPEX optimization\n",
109
+ "model, optimizer = ipex.optimize(model, optimizer=optimizer)\n",
110
+ "\n",
111
+ "# Enable Automatic Mixed Precision (AMP)\n",
112
+ "scaler = torch.cuda.amp.GradScaler(enabled=False) if DEVICE != 'cuda' else torch.cuda.amp.GradScaler()\n",
113
+ "\n",
114
+ "# ----------------------------\n",
115
+ "# TRAINING LOOP\n",
116
+ "# ----------------------------\n",
117
+ "def train():\n",
118
+ " model.train()\n",
119
+ " for epoch in range(EPOCHS):\n",
120
+ " total_loss, correct = 0, 0\n",
121
+ " for imgs, labels in tqdm(loader, desc=f\"Epoch {epoch+1}/{EPOCHS}\"):\n",
122
+ " imgs, labels = imgs.to(DEVICE, non_blocking=True), labels.to(DEVICE, non_blocking=True)\n",
123
+ "\n",
124
+ " optimizer.zero_grad()\n",
125
+ "\n",
126
+ " # AMP not yet fully supported on all XPU devices, so we avoid scaler here\n",
127
+ " outputs = model(imgs)\n",
128
+ " loss = criterion(outputs, labels)\n",
129
+ " loss.backward()\n",
130
+ " optimizer.step()\n",
131
+ "\n",
132
+ " total_loss += loss.item()\n",
133
+ " preds = torch.argmax(outputs, dim=1)\n",
134
+ " correct += (preds == labels).sum().item()\n",
135
+ "\n",
136
+ " acc = 100. * correct / len(dataset)\n",
137
+ " print(f\"Epoch {epoch+1}: Loss={total_loss:.4f} | Accuracy={acc:.2f}%\")\n",
138
+ "\n",
139
+ " torch.save(model.state_dict(), MODEL_SAVE_PATH)\n",
140
+ " print(f\"Model saved to {MODEL_SAVE_PATH}\")\n",
141
+ "\n",
142
+ "# ----------------------------\n",
143
+ "# MAIN EXECUTION\n",
144
+ "# ----------------------------\n",
145
+ "if __name__ == '__main__':\n",
146
+ " train()\n"
147
+ ]
148
+ },
149
+ {
150
+ "cell_type": "code",
151
+ "execution_count": 5,
152
+ "id": "c07737e2",
153
+ "metadata": {},
154
+ "outputs": [
155
+ {
156
+ "name": "stdout",
157
+ "output_type": "stream",
158
+ "text": [
159
+ "\n",
160
+ ">>> DEVICE selected: xpu\n",
161
+ ">>> Intel Arc GPU is active ✅ (watch Task Manager!)\n",
162
+ ">>> Loaded 20 images (10 real + 10 fake)\n",
163
+ "\n",
164
+ ">>> Warm‑up pass …\n",
165
+ ">>> Warm‑up done.\n",
166
+ "\n",
167
+ ">>> Quick training run (1 epoch) …\n",
168
+ "Batch loss: 2.2140\n",
169
+ "Batch loss: 0.3490\n",
170
+ "Batch loss: 0.0285\n",
171
+ "Batch loss: 0.0046\n",
172
+ "Batch loss: 0.0039\n",
173
+ "Batch loss: 6.5563\n",
174
+ "Batch loss: 6.2575\n",
175
+ "Batch loss: 4.4933\n",
176
+ "Batch loss: 2.5277\n",
177
+ "Batch loss: 0.9984\n",
178
+ "\n",
179
+ "✅ Finished. Check GPU utilisation in TaskManager.\n"
180
+ ]
181
+ }
182
+ ],
183
+ "source": [
184
+ "\"\"\"\n",
185
+ "Mini‑ViT training smoke‑test on Intel Arc (XPU)\n",
186
+ "\n",
187
+ "• Loads first 10 images from frames/real and frames/fake\n",
188
+ "• Runs one epoch, batch size 2\n",
189
+ "• Prints the device in use so you can look for GPU activity\n",
190
+ "\"\"\"\n",
191
+ "\n",
192
+ "import os, glob, torch, random\n",
193
+ "import torch.nn as nn\n",
194
+ "from torch.utils.data import Dataset, DataLoader\n",
195
+ "from torchvision import transforms\n",
196
+ "from PIL import Image\n",
197
+ "import timm # Requires timm>=0.9\n",
198
+ "import intel_extension_for_pytorch as ipex # Already installed\n",
199
+ "\n",
200
+ "# ---------------- CONFIG ----------------\n",
201
+ "ROOT_DIR = \"frames\" # frames/real and frames/fake\n",
202
+ "IMG_SIZE = 224\n",
203
+ "MAX_SAMPLES = 10 # ← first 10 from each class\n",
204
+ "BATCH_SIZE = 2\n",
205
+ "EPOCHS = 5 # just a quick smoke-test\n",
206
+ "LR = 3e-5\n",
207
+ "DEVICE = torch.device(\"xpu\" if torch.xpu.is_available() else \"cpu\")\n",
208
+ "\n",
209
+ "print(f\"\\n>>> DEVICE selected: {DEVICE}\")\n",
210
+ "if DEVICE.type == \"xpu\":\n",
211
+ " print(\">>> Intel Arc GPU is active ✅ (watch Task Manager!)\")\n",
212
+ "else:\n",
213
+ " print(\">>> Falling back to CPU ❌\")\n",
214
+ "\n",
215
+ "# ------------- DATASET ------------------\n",
216
+ "class FirstKForgeryDataset(Dataset):\n",
217
+ " def __init__(self, root, k=10, transform=None):\n",
218
+ " self.items, self.transform = [], transform\n",
219
+ " for label, sub in enumerate([\"real\", \"fake\"]):\n",
220
+ " files = sorted(glob.glob(os.path.join(root, sub, \"*.jpg\")))[:k]\n",
221
+ " self.items += [(p, label) for p in files]\n",
222
+ "\n",
223
+ " def __len__(self): return len(self.items)\n",
224
+ "\n",
225
+ " def __getitem__(self, idx):\n",
226
+ " path, label = self.items[idx]\n",
227
+ " img = Image.open(path).convert(\"RGB\")\n",
228
+ " return (self.transform(img) if self.transform else img), label\n",
229
+ "\n",
230
+ "basic_tf = transforms.Compose([\n",
231
+ " transforms.Resize((IMG_SIZE, IMG_SIZE)),\n",
232
+ " transforms.ToTensor(),\n",
233
+ " transforms.Normalize([0.5]*3, [0.5]*3),\n",
234
+ "])\n",
235
+ "\n",
236
+ "ds = FirstKForgeryDataset(ROOT_DIR, k=MAX_SAMPLES, transform=basic_tf)\n",
237
+ "dl = DataLoader(ds, batch_size=BATCH_SIZE, shuffle=False,\n",
238
+ " num_workers=0, pin_memory=False)\n",
239
+ "\n",
240
+ "print(f\">>> Loaded {len(ds)} images ({len(ds)//2} real + {len(ds)//2} fake)\")\n",
241
+ "\n",
242
+ "# ------------- MODEL --------------------\n",
243
+ "model = timm.create_model(\"vit_tiny_patch16_224\",\n",
244
+ " pretrained=True, num_classes=2)\n",
245
+ "model = model.to(memory_format=torch.channels_last).to(DEVICE)\n",
246
+ "\n",
247
+ "criterion = nn.CrossEntropyLoss()\n",
248
+ "optimizer = torch.optim.AdamW(model.parameters(), lr=LR)\n",
249
+ "model, optimizer = ipex.optimize(model, optimizer=optimizer)\n",
250
+ "\n",
251
+ "# ------------- WARM‑UP ------------------\n",
252
+ "print(\"\\n>>> Warm‑up pass …\")\n",
253
+ "with torch.no_grad():\n",
254
+ " model(torch.randn(1, 3, IMG_SIZE, IMG_SIZE).to(DEVICE))\n",
255
+ "print(\">>> Warm‑up done.\\n\")\n",
256
+ "\n",
257
+ "# ------------- TRAIN --------------------\n",
258
+ "print(\">>> Quick training run (1 epoch) …\")\n",
259
+ "model.train()\n",
260
+ "for imgs, labels in dl:\n",
261
+ " imgs, labels = imgs.to(DEVICE), labels.to(DEVICE)\n",
262
+ " out = model(imgs)\n",
263
+ " loss = criterion(out, labels)\n",
264
+ " loss.backward()\n",
265
+ " optimizer.step(); optimizer.zero_grad()\n",
266
+ " print(f\"Batch loss: {loss.item():.4f}\")\n",
267
+ "print(\"\\n✅ Finished. Check GPU utilisation in TaskManager.\")\n"
268
+ ]
269
+ },
270
+ {
271
+ "cell_type": "code",
272
+ "execution_count": null,
273
+ "id": "e04da6d6",
274
+ "metadata": {},
275
+ "outputs": [],
276
+ "source": [
277
+ "import os\n",
278
+ "import cv2\n",
279
+ "\n",
280
+ "def extract_frames_from_folder(input_folder, output_folder, label, sample_rate=30):\n",
281
+ " # Ensure the input path points to the 'ViT Project' directory\n",
282
+ " input_path = os.path.join(input_folder) # no need to join 'ViT Project' here, assuming it's already the root folder\n",
283
+ " output_path = os.path.join(output_folder, label) # frames will be created inside the same directory as 'ViT Project'\n",
284
+ " \n",
285
+ " # Create the frames folder if it doesn't exist\n",
286
+ " os.makedirs(output_path, exist_ok=True)\n",
287
+ "\n",
288
+ " for video_file in os.listdir(input_path):\n",
289
+ " if not video_file.lower().endswith(('.mp4', '.mov', '.avi')):\n",
290
+ " continue\n",
291
+ " video_path = os.path.join(input_path, video_file)\n",
292
+ " cap = cv2.VideoCapture(video_path)\n",
293
+ " count, saved = 0, 0\n",
294
+ " while cap.isOpened():\n",
295
+ " ret, frame = cap.read()\n",
296
+ " if not ret:\n",
297
+ " break\n",
298
+ " if count % sample_rate == 0:\n",
299
+ " frame_name = f\"{video_file[:-4]}_frame_{saved:05d}.jpg\"\n",
300
+ " cv2.imwrite(os.path.join(output_path, frame_name), frame)\n",
301
+ " saved += 1\n",
302
+ " count += 1\n",
303
+ " cap.release()\n",
304
+ "\n",
305
+ "# Run extraction with updated directory paths\n",
306
+ "extract_frames_from_folder('DFD_original sequences', 'frames', 'real')\n",
307
+ "extract_frames_from_folder('DFD_manipulated_sequences', 'frames', 'fake')\n"
308
+ ]
309
+ },
310
+ {
311
+ "cell_type": "code",
312
+ "execution_count": null,
313
+ "id": "1f70dea7",
314
+ "metadata": {},
315
+ "outputs": [],
316
+ "source": [
317
+ "import os\n",
318
+ "import cv2\n",
319
+ "\n",
320
+ "def extract_fake_frames(sample_rate=30):\n",
321
+ " input_folder = \"DFD_manipulated_sequences\"\n",
322
+ " output_folder = os.path.join(\"frames\", \"fake\")\n",
323
+ "\n",
324
+ " # Ensure input folder exists\n",
325
+ " if not os.path.exists(input_folder):\n",
326
+ " raise FileNotFoundError(f\"Input folder not found: {input_folder}\")\n",
327
+ "\n",
328
+ " # Create output folder if it doesn't exist\n",
329
+ " os.makedirs(output_folder, exist_ok=True)\n",
330
+ "\n",
331
+ " for video_file in os.listdir(input_folder):\n",
332
+ " if not video_file.lower().endswith(('.mp4', '.mov', '.avi')):\n",
333
+ " continue\n",
334
+ " video_path = os.path.join(input_folder, video_file)\n",
335
+ " cap = cv2.VideoCapture(video_path)\n",
336
+ " count, saved = 0, 0\n",
337
+ " while cap.isOpened():\n",
338
+ " ret, frame = cap.read()\n",
339
+ " if not ret:\n",
340
+ " break\n",
341
+ " if count % sample_rate == 0:\n",
342
+ " frame_name = f\"{video_file[:-4]}_frame_{saved:05d}.jpg\"\n",
343
+ " cv2.imwrite(os.path.join(output_folder, frame_name), frame)\n",
344
+ " saved += 1\n",
345
+ " count += 1\n",
346
+ " cap.release()\n",
347
+ "\n",
348
+ "# Run extraction\n",
349
+ "extract_fake_frames()\n"
350
+ ]
351
+ },
352
+ {
353
+ "cell_type": "code",
354
+ "execution_count": null,
355
+ "id": "d044ac20",
356
+ "metadata": {},
357
+ "outputs": [],
358
+ "source": [
359
+ "import torch\n",
360
+ "import openvino as ov\n",
361
+ "import intel_extension_for_pytorch as ipex # Just to check if installed\n",
362
+ "\n",
363
+ "def check_environment():\n",
364
+ " print(\"=== Environment Verification ===\")\n",
365
+ " \n",
366
+ " # PyTorch checks\n",
367
+ " print(\"\\nPyTorch:\")\n",
368
+ " print(f\"Version: {torch.__version__}\")\n",
369
+ " print(f\"CUDA available: {torch.cuda.is_available()}\")\n",
370
+ " print(f\"XPU available: {hasattr(torch, 'xpu') and torch.xpu.is_available()}\")\n",
371
+ " \n",
372
+ " # OpenVINO checks\n",
373
+ " print(\"\\nOpenVINO:\")\n",
374
+ " print(f\"Version: {ov.__version__}\")\n",
375
+ " core = ov.Core()\n",
376
+ " devices = core.available_devices\n",
377
+ " print(\"Available devices:\", devices)\n",
378
+ " \n",
379
+ " # Intel Arc specific checks\n",
380
+ " if \"GPU\" in devices:\n",
381
+ " print(\"\\nIntel Arc GPU Info:\")\n",
382
+ " gpu_properties = core.get_property(\"GPU\", \"FULL_DEVICE_NAME\")\n",
383
+ " print(f\"GPU Name: {gpu_properties}\")\n",
384
+ " print(f\"GPU Capabilities: {core.get_property('GPU', 'OPTIMIZATION_CAPABILITIES')}\")\n",
385
+ " \n",
386
+ " print(\"\\nRecommendations:\")\n",
387
+ " if \"GPU\" not in devices:\n",
388
+ " print(\"- Install latest Intel GPU drivers\")\n",
389
+ " print(\"- Update OpenVINO to latest version\")\n",
390
+ " else:\n",
391
+ " print(\"- Environment looks good for Intel Arc GPU acceleration\")\n",
392
+ "\n",
393
+ "if __name__ == \"__main__\":\n",
394
+ " check_environment()"
395
+ ]
396
+ },
397
+ {
398
+ "cell_type": "code",
399
+ "execution_count": null,
400
+ "id": "ff95d0b0",
401
+ "metadata": {},
402
+ "outputs": [],
403
+ "source": [
404
+ "import os\n",
405
+ "import glob\n",
406
+ "import torch\n",
407
+ "import torch.nn as nn\n",
408
+ "import torch.optim as optim\n",
409
+ "from torch.utils.data import Dataset, DataLoader\n",
410
+ "from torchvision import transforms\n",
411
+ "from PIL import Image\n",
412
+ "import timm\n",
413
+ "from tqdm import tqdm\n",
414
+ "import random\n",
415
+ "import numpy as np\n",
416
+ "from openvino.runtime import Core, serialize\n",
417
+ "from openvino.tools.mo import convert_model\n",
418
+ "\n",
419
+ "# ----------------------------\n",
420
+ "# CONFIGURATION (Reduced for testing)\n",
421
+ "# ----------------------------\n",
422
+ "DATASET_DIR = 'frames'\n",
423
+ "BATCH_SIZE = 16 # Reduced from 32\n",
424
+ "EPOCHS = 5 # Reduced from 10\n",
425
+ "LEARNING_RATE = 3e-5\n",
426
+ "NUM_CLASSES = 2\n",
427
+ "IMG_SIZE = 224\n",
428
+ "MODEL_SAVE_PT = './vit_model.pth'\n",
429
+ "MODEL_SAVE_OV = './vit_model.xml'\n",
430
+ "\n",
431
+ "# Initialize OpenVINO Core (simplified)\n",
432
+ "core = Core()\n",
433
+ "\n",
434
+ "# ----------------------------\n",
435
+ "# SIMPLIFIED DATASET CLASS\n",
436
+ "# ----------------------------\n",
437
+ "class SimpleDataset(Dataset):\n",
438
+ " def __init__(self, root_dir, transform=None, max_samples=1000):\n",
439
+ " self.samples = []\n",
440
+ " self.transform = transform\n",
441
+ " for label, name in enumerate(['real', 'fake']):\n",
442
+ " folder = os.path.join(root_dir, name)\n",
443
+ " for img_path in glob.glob(os.path.join(folder, '*.jpg'))[:max_samples//2]:\n",
444
+ " self.samples.append((img_path, label))\n",
445
+ " random.shuffle(self.samples)\n",
446
+ "\n",
447
+ " def __len__(self):\n",
448
+ " return len(self.samples)\n",
449
+ "\n",
450
+ " def __getitem__(self, idx):\n",
451
+ " img_path, label = self.samples[idx]\n",
452
+ " image = Image.open(img_path).convert('RGB')\n",
453
+ " if self.transform:\n",
454
+ " image = self.transform(image)\n",
455
+ " return image, label\n",
456
+ "\n",
457
+ "# ----------------------------\n",
458
+ "# OPTIMIZED TRANSFORMS\n",
459
+ "# ----------------------------\n",
460
+ "transform = transforms.Compose([\n",
461
+ " transforms.Resize((IMG_SIZE, IMG_SIZE)),\n",
462
+ " transforms.ToTensor(),\n",
463
+ " transforms.Normalize([0.5]*3, [0.5]*3)\n",
464
+ "])\n",
465
+ "\n",
466
+ "# ----------------------------\n",
467
+ "# DEBUGGING TRAINING LOOP\n",
468
+ "# ----------------------------\n",
469
+ "def debug_train():\n",
470
+ " # 1. Test dataset loading\n",
471
+ " print(\"Testing dataset loading...\")\n",
472
+ " test_dataset = SimpleDataset(DATASET_DIR, transform, max_samples=100)\n",
473
+ " test_loader = DataLoader(test_dataset, batch_size=4, shuffle=False)\n",
474
+ " \n",
475
+ " # Try loading a single batch\n",
476
+ " try:\n",
477
+ " test_batch = next(iter(test_loader))\n",
478
+ " print(\"Successfully loaded batch of shape:\", test_batch[0].shape)\n",
479
+ " except Exception as e:\n",
480
+ " print(f\"Dataset loading failed: {str(e)}\")\n",
481
+ " return\n",
482
+ "\n",
483
+ " # 2. Test model creation\n",
484
+ " print(\"\\nTesting model creation...\")\n",
485
+ " try:\n",
486
+ " model = timm.create_model('vit_tiny_patch16_224', pretrained=True, num_classes=NUM_CLASSES) # Smaller model\n",
487
+ " print(\"Model created successfully\")\n",
488
+ " except Exception as e:\n",
489
+ " print(f\"Model creation failed: {str(e)}\")\n",
490
+ " return\n",
491
+ "\n",
492
+ " # 3. Test basic forward pass\n",
493
+ " print(\"\\nTesting forward pass...\")\n",
494
+ " try:\n",
495
+ " device = torch.device(\"cpu\") # Force CPU for debugging\n",
496
+ " model = model.to(device)\n",
497
+ " outputs = model(test_batch[0].to(device))\n",
498
+ " print(\"Forward pass successful. Output shape:\", outputs.shape)\n",
499
+ " except Exception as e:\n",
500
+ " print(f\"Forward pass failed: {str(e)}\")\n",
501
+ " return\n",
502
+ "\n",
503
+ " # 4. Test training step\n",
504
+ " print(\"\\nTesting training step...\")\n",
505
+ " try:\n",
506
+ " optimizer = optim.AdamW(model.parameters(), lr=LEARNING_RATE)\n",
507
+ " criterion = nn.CrossEntropyLoss()\n",
508
+ " \n",
509
+ " optimizer.zero_grad()\n",
510
+ " loss = criterion(outputs, test_batch[1].to(device))\n",
511
+ " loss.backward()\n",
512
+ " optimizer.step()\n",
513
+ " print(\"Training step completed successfully. Loss:\", loss.item())\n",
514
+ " except Exception as e:\n",
515
+ " print(f\"Training step failed: {str(e)}\")\n",
516
+ " return\n",
517
+ "\n",
518
+ " print(\"\\nAll tests passed! You can now run full training.\")\n",
519
+ "\n",
520
+ "# ----------------------------\n",
521
+ "# MAIN EXECUTION\n",
522
+ "# ----------------------------\n",
523
+ "if __name__ == '__main__':\n",
524
+ " print(\"Running diagnostic tests...\")\n",
525
+ " debug_train()\n",
526
+ " \n",
527
+ " # Only proceed if debugging succeeds\n",
528
+ " if input(\"\\nProceed with full training? (y/n): \").lower() == 'y':\n",
529
+ " from tqdm.auto import tqdm # Use the more reliable auto version\n",
530
+ " \n",
531
+ " # Full training setup\n",
532
+ " dataset = SimpleDataset(DATASET_DIR, transform)\n",
533
+ " loader = DataLoader(dataset, batch_size=BATCH_SIZE, shuffle=True, num_workers=0)\n",
534
+ " \n",
535
+ " model = timm.create_model('vit_tiny_patch16_224', pretrained=True, num_classes=NUM_CLASSES)\n",
536
+ " device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
537
+ " model = model.to(device)\n",
538
+ " \n",
539
+ " criterion = nn.CrossEntropyLoss()\n",
540
+ " optimizer = optim.AdamW(model.parameters(), lr=LEARNING_RATE)\n",
541
+ " \n",
542
+ " # Simple training loop with progress monitoring\n",
543
+ " for epoch in range(EPOCHS):\n",
544
+ " model.train()\n",
545
+ " pbar = tqdm(loader, desc=f\"Epoch {epoch+1}/{EPOCHS}\")\n",
546
+ " for imgs, labels in pbar:\n",
547
+ " imgs, labels = imgs.to(device), labels.to(device)\n",
548
+ " \n",
549
+ " optimizer.zero_grad()\n",
550
+ " outputs = model(imgs)\n",
551
+ " loss = criterion(outputs, labels)\n",
552
+ " loss.backward()\n",
553
+ " optimizer.step()\n",
554
+ " \n",
555
+ " pbar.set_postfix(loss=loss.item())"
556
+ ]
557
+ },
558
+ {
559
+ "cell_type": "code",
560
+ "execution_count": null,
561
+ "id": "57287685",
562
+ "metadata": {},
563
+ "outputs": [],
564
+ "source": [
565
+ "from openvino.runtime import Core\n",
566
+ "core = Core()\n",
567
+ "print(\"Available devices:\", core.available_devices)\n",
568
+ "\n",
569
+ "# Check GPU specifically\n",
570
+ "if \"GPU\" in core.available_devices:\n",
571
+ " print(\"GPU device name:\", core.get_property(\"GPU\", \"FULL_DEVICE_NAME\"))\n",
572
+ " print(\"GPU capabilities:\", core.get_property(\"GPU\", \"OPTIMIZATION_CAPABILITIES\"))\n",
573
+ "else:\n",
574
+ " print(\"GPU not detected by OpenVINO\")"
575
+ ]
576
+ },
577
+ {
578
+ "cell_type": "code",
579
+ "execution_count": 1,
580
+ "id": "637d217a",
581
+ "metadata": {},
582
+ "outputs": [
583
+ {
584
+ "name": "stdout",
585
+ "output_type": "stream",
586
+ "text": [
587
+ "Dataset split complete.\n"
588
+ ]
589
+ }
590
+ ],
591
+ "source": [
592
+ "import os\n",
593
+ "import shutil\n",
594
+ "import random\n",
595
+ "\n",
596
+ "def split_data(source_dir, train_dir, val_dir, split_ratio=0.8):\n",
597
+ " random.seed(42) # For reproducibility\n",
598
+ " classes = ['real', 'fake']\n",
599
+ "\n",
600
+ " for cls in classes:\n",
601
+ " src_path = os.path.join(source_dir, cls)\n",
602
+ " files = os.listdir(src_path)\n",
603
+ " random.shuffle(files)\n",
604
+ "\n",
605
+ " split = int(split_ratio * len(files))\n",
606
+ " train_files = files[:split]\n",
607
+ " val_files = files[split:]\n",
608
+ "\n",
609
+ " os.makedirs(os.path.join(train_dir, cls), exist_ok=True)\n",
610
+ " os.makedirs(os.path.join(val_dir, cls), exist_ok=True)\n",
611
+ "\n",
612
+ " for f in train_files:\n",
613
+ " shutil.copy(os.path.join(src_path, f), os.path.join(train_dir, cls, f))\n",
614
+ "\n",
615
+ " for f in val_files:\n",
616
+ " shutil.copy(os.path.join(src_path, f), os.path.join(val_dir, cls, f))\n",
617
+ "\n",
618
+ "# Run it with your paths\n",
619
+ "source_dir = \"frames\"\n",
620
+ "train_dir = os.path.join(source_dir, 'train')\n",
621
+ "val_dir = os.path.join(source_dir, 'val')\n",
622
+ "\n",
623
+ "split_data(source_dir, train_dir, val_dir)\n",
624
+ "print(\"Dataset split complete.\")\n"
625
+ ]
626
+ }
627
+ ],
628
+ "metadata": {
629
+ "kernelspec": {
630
+ "display_name": "Python 3",
631
+ "language": "python",
632
+ "name": "python3"
633
+ },
634
+ "language_info": {
635
+ "codemirror_mode": {
636
+ "name": "ipython",
637
+ "version": 3
638
+ },
639
+ "file_extension": ".py",
640
+ "mimetype": "text/x-python",
641
+ "name": "python",
642
+ "nbconvert_exporter": "python",
643
+ "pygments_lexer": "ipython3",
644
+ "version": "3.10.11"
645
+ }
646
+ },
647
+ "nbformat": 4,
648
+ "nbformat_minor": 5
649
+ }
ViTFINAL.ipynb ADDED
The diff for this file is too large to render. See raw diff