Commit
·
0dab8bf
1
Parent(s):
5a0e0a4
wow
Browse files- trained_model.pth +3 -0
- vit_model_original.py +148 -0
- vit_model_original_test.py +120 -0
trained_model.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:3975df5a443c080bba56efd8ffe56e10cb2d2ff08649b75892ae1a30d1bb9229
|
3 |
+
size 343282922
|
vit_model_original.py
ADDED
@@ -0,0 +1,148 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torch.nn as nn
|
3 |
+
from torch.utils.data import Dataset, DataLoader
|
4 |
+
from torchvision import transforms
|
5 |
+
from transformers import ViTForImageClassification
|
6 |
+
from PIL import Image
|
7 |
+
import torch.optim as optim
|
8 |
+
import os
|
9 |
+
import pandas as pd
|
10 |
+
from sklearn.model_selection import train_test_split
|
11 |
+
## working 18.5.24
|
12 |
+
|
13 |
+
|
14 |
+
def labeling(path_real, path_fake):
|
15 |
+
image_paths = []
|
16 |
+
labels = []
|
17 |
+
|
18 |
+
for filename in os.listdir(path_real):
|
19 |
+
image_paths.append(os.path.join(path_real, filename))
|
20 |
+
labels.append(0)
|
21 |
+
|
22 |
+
for filename in os.listdir(path_fake):
|
23 |
+
image_paths.append(os.path.join(path_fake, filename))
|
24 |
+
labels.append(1)
|
25 |
+
|
26 |
+
dataset = pd.DataFrame({'image_path': image_paths, 'label': labels})
|
27 |
+
|
28 |
+
return dataset
|
29 |
+
|
30 |
+
class CustomDataset(Dataset):
|
31 |
+
def __init__(self, dataframe, transform=None):
|
32 |
+
self.dataframe = dataframe
|
33 |
+
self.transform = transform
|
34 |
+
|
35 |
+
def __len__(self):
|
36 |
+
return len(self.dataframe)
|
37 |
+
|
38 |
+
def __getitem__(self, idx):
|
39 |
+
image_path = self.dataframe.iloc[idx, 0] # Image path is in the first column
|
40 |
+
image = Image.open(image_path).convert('RGB') # Convert to RGB format
|
41 |
+
|
42 |
+
if self.transform:
|
43 |
+
image = self.transform(image)
|
44 |
+
|
45 |
+
label = self.dataframe.iloc[idx, 1] # Label is in the second column
|
46 |
+
return image, label
|
47 |
+
|
48 |
+
|
49 |
+
|
50 |
+
|
51 |
+
def shuffle_and_split_data(dataframe, test_size=0.2, random_state=59):
|
52 |
+
# Shuffle the DataFrame
|
53 |
+
shuffled_df = dataframe.sample(frac=1, random_state=random_state).reset_index(drop=True)
|
54 |
+
|
55 |
+
# Split the DataFrame into train and validation sets
|
56 |
+
train_df, val_df = train_test_split(shuffled_df, test_size=test_size, random_state=random_state)
|
57 |
+
|
58 |
+
return train_df, val_df
|
59 |
+
|
60 |
+
|
61 |
+
if __name__ == "__main__":
|
62 |
+
# Check for GPU availability
|
63 |
+
device = torch.device('cuda')
|
64 |
+
|
65 |
+
# Load the pre-trained ViT model and move it to GPU
|
66 |
+
model = ViTForImageClassification.from_pretrained('google/vit-base-patch16-224').to(device)
|
67 |
+
|
68 |
+
# Freeze pre-trained layers
|
69 |
+
for param in model.parameters():
|
70 |
+
param.requires_grad = False
|
71 |
+
|
72 |
+
# Define a new classifier and move it to GPU
|
73 |
+
model.classifier = nn.Linear(model.config.hidden_size, 2).to(device) # Two output classes: 'REAL' and 'FAKE'
|
74 |
+
|
75 |
+
print(model)
|
76 |
+
# Define the optimizer
|
77 |
+
optimizer = optim.Adam(model.parameters(), lr=0.001)
|
78 |
+
|
79 |
+
# Define the image preprocessing pipeline
|
80 |
+
preprocess = transforms.Compose([
|
81 |
+
transforms.Resize((224, 224)),
|
82 |
+
transforms.ToTensor()
|
83 |
+
])
|
84 |
+
|
85 |
+
# Assuming you have already defined your dataset class and split it into training and validation sets
|
86 |
+
# Let's call it CustomDataset
|
87 |
+
|
88 |
+
|
89 |
+
|
90 |
+
train_real_folder = 'train/art/real'
|
91 |
+
train_fake_folder = 'train/art/fake'
|
92 |
+
|
93 |
+
|
94 |
+
|
95 |
+
|
96 |
+
|
97 |
+
train_dataset_df = labeling(train_real_folder, train_fake_folder)
|
98 |
+
|
99 |
+
train_dataset_df , val_dataset_df = shuffle_and_split_data(train_dataset_df)
|
100 |
+
|
101 |
+
|
102 |
+
|
103 |
+
|
104 |
+
# Define the dataset and dataloaders
|
105 |
+
train_dataset = CustomDataset(train_dataset_df, transform=preprocess)
|
106 |
+
train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)
|
107 |
+
|
108 |
+
val_dataset = CustomDataset(val_dataset_df, transform=preprocess)
|
109 |
+
val_loader = DataLoader(val_dataset, batch_size=32)
|
110 |
+
|
111 |
+
# Define the loss function and move it to GPU
|
112 |
+
criterion = nn.CrossEntropyLoss().to(device)
|
113 |
+
|
114 |
+
# Training loop
|
115 |
+
num_epochs = 10
|
116 |
+
for epoch in range(num_epochs):
|
117 |
+
model.train()
|
118 |
+
running_loss = 0.0
|
119 |
+
for images, labels in train_loader:
|
120 |
+
# Move inputs and labels to GPU
|
121 |
+
images, labels = images.to(device), labels.to(device)
|
122 |
+
|
123 |
+
optimizer.zero_grad()
|
124 |
+
outputs = model(images)
|
125 |
+
logits = outputs.logits # Extract logits from the output
|
126 |
+
loss = criterion(logits, labels)
|
127 |
+
loss.backward()
|
128 |
+
optimizer.step()
|
129 |
+
running_loss += loss.item()
|
130 |
+
print(f"Epoch {epoch+1}/{num_epochs}, Loss: {running_loss / len(train_loader)}")
|
131 |
+
|
132 |
+
# Validation loop
|
133 |
+
model.eval()
|
134 |
+
correct = 0
|
135 |
+
total = 0
|
136 |
+
with torch.no_grad():
|
137 |
+
for images, labels in val_loader:
|
138 |
+
images, labels = images.to(device), labels.to(device) # Move inputs and labels to GPU
|
139 |
+
outputs = model(images)
|
140 |
+
logits = outputs.logits # Extract logits from the output
|
141 |
+
_, predicted = torch.max(logits, 1)
|
142 |
+
total += labels.size(0)
|
143 |
+
correct += (predicted == labels).sum().item()
|
144 |
+
print(f"Validation Accuracy: {correct / total}")
|
145 |
+
|
146 |
+
# Save the trained model
|
147 |
+
torch.save(model.state_dict(), 'trained_model.pth')
|
148 |
+
|
vit_model_original_test.py
ADDED
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torch.nn as nn
|
3 |
+
from torch.utils.data import Dataset, DataLoader
|
4 |
+
from torchvision import transforms
|
5 |
+
from transformers import ViTForImageClassification
|
6 |
+
from PIL import Image
|
7 |
+
import os
|
8 |
+
import pandas as pd
|
9 |
+
from sklearn.model_selection import train_test_split
|
10 |
+
from sklearn.metrics import accuracy_score, precision_score, confusion_matrix, f1_score, average_precision_score
|
11 |
+
import matplotlib.pyplot as plt
|
12 |
+
import seaborn as sns
|
13 |
+
from sklearn.metrics import recall_score
|
14 |
+
|
15 |
+
def labeling(path_real, path_fake):
|
16 |
+
image_paths = []
|
17 |
+
labels = []
|
18 |
+
|
19 |
+
for filename in os.listdir(path_real):
|
20 |
+
image_paths.append(os.path.join(path_real, filename))
|
21 |
+
labels.append(0)
|
22 |
+
|
23 |
+
for filename in os.listdir(path_fake):
|
24 |
+
image_paths.append(os.path.join(path_fake, filename))
|
25 |
+
labels.append(1)
|
26 |
+
|
27 |
+
dataset = pd.DataFrame({'image_path': image_paths, 'label': labels})
|
28 |
+
|
29 |
+
return dataset
|
30 |
+
|
31 |
+
class CustomDataset(Dataset):
|
32 |
+
def __init__(self, dataframe, transform=None):
|
33 |
+
self.dataframe = dataframe
|
34 |
+
self.transform = transform
|
35 |
+
|
36 |
+
def __len__(self):
|
37 |
+
return len(self.dataframe)
|
38 |
+
|
39 |
+
def __getitem__(self, idx):
|
40 |
+
image_path = self.dataframe.iloc[idx, 0] # Image path is in the first column
|
41 |
+
image = Image.open(image_path).convert('RGB') # Convert to RGB format
|
42 |
+
|
43 |
+
if self.transform:
|
44 |
+
image = self.transform(image)
|
45 |
+
|
46 |
+
label = self.dataframe.iloc[idx, 1] # Label is in the second column
|
47 |
+
return image, label
|
48 |
+
|
49 |
+
def shuffle_and_split_data(dataframe, test_size=0.2, random_state=59):
|
50 |
+
# Shuffle the DataFrame
|
51 |
+
shuffled_df = dataframe.sample(frac=1, random_state=random_state).reset_index(drop=True)
|
52 |
+
|
53 |
+
# Split the DataFrame into train and validation sets
|
54 |
+
train_df, val_df = train_test_split(shuffled_df, test_size=test_size, random_state=random_state)
|
55 |
+
|
56 |
+
return train_df, val_df
|
57 |
+
|
58 |
+
|
59 |
+
if __name__ == "__main__":
|
60 |
+
# Check for GPU availability
|
61 |
+
device = torch.device('cuda')
|
62 |
+
|
63 |
+
# Load the pre-trained ViT model and move it to GPU
|
64 |
+
model = ViTForImageClassification.from_pretrained('google/vit-base-patch16-224').to(device)
|
65 |
+
|
66 |
+
|
67 |
+
|
68 |
+
model.classifier = nn.Linear(model.config.hidden_size, 2).to(device)
|
69 |
+
# Define the image preprocessing pipeline
|
70 |
+
preprocess = transforms.Compose([
|
71 |
+
transforms.Resize((224, 224)),
|
72 |
+
transforms.ToTensor()
|
73 |
+
])
|
74 |
+
|
75 |
+
# Load the test dataset
|
76 |
+
test_real_folder = 'test/art/real'
|
77 |
+
test_fake_folder = 'test/art/fake'
|
78 |
+
test_set = labeling(test_real_folder, test_fake_folder)
|
79 |
+
test_dataset = CustomDataset(test_set, transform=preprocess)
|
80 |
+
test_loader = DataLoader(test_dataset, batch_size=32)
|
81 |
+
|
82 |
+
# Load the trained model
|
83 |
+
model.load_state_dict(torch.load('trained_model.pth'))
|
84 |
+
|
85 |
+
# Evaluate the model
|
86 |
+
model.eval()
|
87 |
+
true_labels = []
|
88 |
+
predicted_labels = []
|
89 |
+
|
90 |
+
with torch.no_grad():
|
91 |
+
for images, labels in test_loader:
|
92 |
+
images, labels = images.to(device), labels.to(device)
|
93 |
+
outputs = model(images)
|
94 |
+
logits = outputs.logits # Extract logits from the output
|
95 |
+
_, predicted = torch.max(logits, 1)
|
96 |
+
true_labels.extend(labels.cpu().numpy())
|
97 |
+
predicted_labels.extend(predicted.cpu().numpy())
|
98 |
+
|
99 |
+
# Calculate evaluation metrics
|
100 |
+
accuracy = accuracy_score(true_labels, predicted_labels)
|
101 |
+
precision = precision_score(true_labels, predicted_labels)
|
102 |
+
cm = confusion_matrix(true_labels, predicted_labels)
|
103 |
+
f1 = f1_score(true_labels, predicted_labels)
|
104 |
+
ap = average_precision_score(true_labels, predicted_labels)
|
105 |
+
recall = recall_score(true_labels, predicted_labels)
|
106 |
+
|
107 |
+
|
108 |
+
print(f"Test Accuracy: {accuracy:.2%}")
|
109 |
+
print(f"Precision: {precision:.2%}")
|
110 |
+
print(f"F1 Score: {f1:.2%}")
|
111 |
+
print(f"Average Precision: {ap:.2%}")
|
112 |
+
print(f"Recall: {recall:.2%}")
|
113 |
+
|
114 |
+
# Plot the confusion matrix
|
115 |
+
plt.figure(figsize=(8, 6))
|
116 |
+
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', cbar=False)
|
117 |
+
plt.xlabel('Predicted Labels')
|
118 |
+
plt.ylabel('True Labels')
|
119 |
+
plt.title('Confusion Matrix')
|
120 |
+
plt.show()
|