Vijayendra commited on
Commit
5d8d9be
·
verified ·
1 Parent(s): 0630c94

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +24 -74
README.md CHANGED
@@ -5,7 +5,7 @@ datasets:
5
  language:
6
  - en
7
  metrics:
8
- - accuracy:96.7 %
9
 
10
 
11
  tags:
@@ -19,7 +19,6 @@ pipeline_tag: image-classification
19
  # Install necessary libraries
20
 
21
  ```python
22
- # Import necessary libraries
23
  import os
24
  import torch
25
  import torch.nn as nn
@@ -28,9 +27,7 @@ import numpy as np
28
  from torch.utils.data import DataLoader
29
  import torchvision.transforms as transforms
30
  import torchvision.datasets as datasets
31
- import xgboost as xgb
32
  from sklearn.metrics import accuracy_score, confusion_matrix, ConfusionMatrixDisplay
33
- from sklearn.model_selection import train_test_split
34
  import matplotlib.pyplot as plt
35
  from huggingface_hub import hf_hub_download
36
 
@@ -38,9 +35,8 @@ from huggingface_hub import hf_hub_download
38
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
39
  print(f'Using device: {device}')
40
 
41
- # Define Hugging Face username and repository names
42
  username = "Vijayendra"
43
- model_name_epoch_125 = "QST-CIFAR10-Epoch125"
44
  model_name_best = "QST-CIFAR10-BestModel"
45
 
46
  # Directory where the models will be downloaded
@@ -48,13 +44,13 @@ save_dir = './hf_models'
48
  os.makedirs(save_dir, exist_ok=True)
49
 
50
  # Data normalization for CIFAR-10
51
- transform_test = transforms.Compose([
52
  transforms.ToTensor(),
53
  transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2470, 0.2435, 0.2616))
54
  ])
55
 
56
  # Load CIFAR-10 test set
57
- cifar10_test = datasets.CIFAR10(root='./data', train=False, download=True, transform=transform_test)
58
  test_loader = DataLoader(cifar10_test, batch_size=128, shuffle=False, num_workers=4)
59
 
60
  # Define Patch Embedding with optional convolutional layers
@@ -91,7 +87,6 @@ class SequentialAttentionBlock(nn.Module):
91
  self.dropout = nn.Dropout(dropout)
92
 
93
  def forward(self, x):
94
- # x shape: [seq_length, batch_size, embed_dim]
95
  seq_length = x.size(0)
96
  attn_mask = torch.triu(torch.ones(seq_length, seq_length), diagonal=1).bool().to(x.device)
97
  attn_output, _ = self.attention(x, x, x, attn_mask=attn_mask)
@@ -155,7 +150,6 @@ class CombinedTransformerBlock(nn.Module):
155
  ff_output = self.ff(x)
156
  x = self.norm3(x + self.dropout(ff_output))
157
  return x
158
-
159
  # Decoder Block
160
  class DecoderBlock(nn.Module):
161
  def __init__(self, embed_dim, num_heads, ff_dim, dropout=0.1):
@@ -178,8 +172,7 @@ class DecoderBlock(nn.Module):
178
  ff_output = self.ff(x)
179
  x = self.norm2(x + self.dropout(ff_output))
180
  return x
181
-
182
- # Custom Transformer Model with increased depth and learnable positional encodings
183
  class CustomTransformer(nn.Module):
184
  def __init__(self, embed_dim, num_heads, ff_dim, num_classes, num_layers=6, dropconnect_p=0.5):
185
  super(CustomTransformer, self).__init__()
@@ -189,13 +182,13 @@ class CustomTransformer(nn.Module):
189
  self.positional_encoding = nn.Parameter(torch.zeros(1, self.num_patches, embed_dim))
190
  nn.init.trunc_normal_(self.positional_encoding, std=0.02)
191
 
192
- # Create multiple encoder blocks
193
  self.encoder_blocks = nn.ModuleList([
194
  CombinedTransformerBlock(embed_dim, num_heads, ff_dim, dropconnect_p=dropconnect_p)
195
  for _ in range(num_layers)
196
  ])
197
-
198
- # Create multiple decoder blocks
199
  self.decoder_blocks = nn.ModuleList([
200
  DecoderBlock(embed_dim, num_heads, ff_dim)
201
  for _ in range(num_layers)
@@ -208,10 +201,12 @@ class CustomTransformer(nn.Module):
208
  x += self.positional_encoding
209
  x = x.transpose(0, 1) # Shape: [num_patches, batch_size, embed_dim]
210
 
 
211
  encoder_output = x
212
  for encoder in self.encoder_blocks:
213
  encoder_output = encoder(encoder_output)
214
 
 
215
  decoder_output = encoder_output
216
  for decoder in self.decoder_blocks:
217
  decoder_output = decoder(decoder_output, encoder_output)
@@ -220,95 +215,50 @@ class CustomTransformer(nn.Module):
220
  logits = self.fc(decoder_output)
221
  return logits
222
 
223
- # Initialize two instances of the model for 'model_epoch_125' and 'best_model'
 
224
  embed_dim = 512
225
  num_heads = 32
226
  ff_dim = 1024
227
  num_classes = 10
228
  num_layers = 10 # Ensure it matches the architecture
229
 
230
- model_epoch_125 = CustomTransformer(embed_dim, num_heads, ff_dim, num_classes, num_layers=num_layers).to(device)
231
  model_best = CustomTransformer(embed_dim, num_heads, ff_dim, num_classes, num_layers=num_layers).to(device)
232
 
233
- # Download the models from Hugging Face Hub
234
- from huggingface_hub import hf_hub_download
235
-
236
- # Paths where the models will be saved
237
- model_epoch_125_path = hf_hub_download(repo_id=f"{username}/{model_name_epoch_125}", filename="model_epoch_125.pth")
238
  model_best_path = hf_hub_download(repo_id=f"{username}/{model_name_best}", filename="model_best.pth")
239
-
240
- # Load the saved models from Hugging Face Hub
241
- model_epoch_125.load_state_dict(torch.load(model_epoch_125_path, map_location=device))
242
  model_best.load_state_dict(torch.load(model_best_path, map_location=device))
 
243
 
244
- # Set both models to evaluation mode
245
- model_epoch_125.eval()
246
- model_best.eval()
247
-
248
- # Prepare the feature and label arrays
249
- test_preds_epoch_125 = []
250
- test_preds_best = []
251
  test_labels = []
 
252
 
253
  with torch.no_grad():
254
  for images_test, labels_test in test_loader:
255
  images_test = images_test.to(device)
256
-
257
- # Get predictions from model_epoch_125
258
- logits_epoch_125 = model_epoch_125(images_test)
259
- probs_epoch_125 = F.softmax(logits_epoch_125, dim=1).cpu().numpy() # Convert to probabilities
260
-
261
- # Get predictions from model_best
262
  logits_best = model_best(images_test)
263
  probs_best = F.softmax(logits_best, dim=1).cpu().numpy() # Convert to probabilities
264
 
265
  # Store predictions and labels
266
- test_preds_epoch_125.extend(probs_epoch_125)
267
  test_preds_best.extend(probs_best)
268
  test_labels.extend(labels_test.numpy())
269
 
270
- # Convert predictions to NumPy arrays
271
- test_preds_epoch_125 = np.array(test_preds_epoch_125)
272
- test_preds_best = np.array(test_preds_best)
273
  test_labels = np.array(test_labels)
274
 
275
- # Stack the predictions from both models to create meta-features
276
- meta_features = np.hstack((test_preds_epoch_125, test_preds_best)) # Shape: (num_samples, 20)
277
-
278
- # Split the data for training and validation of the XGBoost meta-learner
279
- X_train, X_val, y_train, y_val = train_test_split(meta_features, test_labels, test_size=0.2, random_state=42)
280
-
281
- # Train an XGBoost classifier as a meta-learner
282
- xgb_model = xgb.XGBClassifier(
283
- objective='multi:softmax',
284
- num_class=10,
285
- eval_metric='mlogloss',
286
- use_label_encoder=False
287
- )
288
-
289
- xgb_model.fit(X_train, y_train)
290
-
291
- # Validate the XGBoost model
292
- y_pred_val = xgb_model.predict(X_val)
293
- val_accuracy = accuracy_score(y_val, y_pred_val)
294
- print(f'Validation Accuracy of Meta-learner: {val_accuracy * 100:.2f}%')
295
-
296
- # Test the XGBoost model on the entire test set
297
- y_pred_test = xgb_model.predict(meta_features)
298
- test_accuracy = accuracy_score(test_labels, y_pred_test)
299
- print(f'Test Accuracy of Meta-learner: {test_accuracy * 100:.2f}%')
300
 
301
  # Plot the confusion matrix for the test set predictions
302
- cm = confusion_matrix(test_labels, y_pred_test)
303
  disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=cifar10_test.classes)
304
  disp.plot(cmap=plt.cm.Blues)
305
 
306
  # Rotate the x-axis labels to prevent overlapping
307
  plt.xticks(rotation=45, ha='right')
308
- plt.title('Confusion Matrix for XGBoost Meta-learner on CIFAR-10 Test Set')
309
- plt.savefig(os.path.join(save_dir, 'xgboost_meta_confusion_matrix.png'))
310
  plt.show()
311
-
312
- # Save the XGBoost model
313
- xgb_model.save_model(os.path.join(save_dir, 'xgboost_meta_learner.json'))
314
- print('Meta-learner model saved.')
 
5
  language:
6
  - en
7
  metrics:
8
+
9
 
10
 
11
  tags:
 
19
  # Install necessary libraries
20
 
21
  ```python
 
22
  import os
23
  import torch
24
  import torch.nn as nn
 
27
  from torch.utils.data import DataLoader
28
  import torchvision.transforms as transforms
29
  import torchvision.datasets as datasets
 
30
  from sklearn.metrics import accuracy_score, confusion_matrix, ConfusionMatrixDisplay
 
31
  import matplotlib.pyplot as plt
32
  from huggingface_hub import hf_hub_download
33
 
 
35
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
36
  print(f'Using device: {device}')
37
 
38
+ # Define Hugging Face username and repository name for the best model
39
  username = "Vijayendra"
 
40
  model_name_best = "QST-CIFAR10-BestModel"
41
 
42
  # Directory where the models will be downloaded
 
44
  os.makedirs(save_dir, exist_ok=True)
45
 
46
  # Data normalization for CIFAR-10
47
+ transform = transforms.Compose([
48
  transforms.ToTensor(),
49
  transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2470, 0.2435, 0.2616))
50
  ])
51
 
52
  # Load CIFAR-10 test set
53
+ cifar10_test = datasets.CIFAR10(root='./data', train=False, download=True, transform=transform)
54
  test_loader = DataLoader(cifar10_test, batch_size=128, shuffle=False, num_workers=4)
55
 
56
  # Define Patch Embedding with optional convolutional layers
 
87
  self.dropout = nn.Dropout(dropout)
88
 
89
  def forward(self, x):
 
90
  seq_length = x.size(0)
91
  attn_mask = torch.triu(torch.ones(seq_length, seq_length), diagonal=1).bool().to(x.device)
92
  attn_output, _ = self.attention(x, x, x, attn_mask=attn_mask)
 
150
  ff_output = self.ff(x)
151
  x = self.norm3(x + self.dropout(ff_output))
152
  return x
 
153
  # Decoder Block
154
  class DecoderBlock(nn.Module):
155
  def __init__(self, embed_dim, num_heads, ff_dim, dropout=0.1):
 
172
  ff_output = self.ff(x)
173
  x = self.norm2(x + self.dropout(ff_output))
174
  return x
175
+ # Custom Transformer Model with increased depth, encoder and decoder blocks, and learnable positional encodings
 
176
  class CustomTransformer(nn.Module):
177
  def __init__(self, embed_dim, num_heads, ff_dim, num_classes, num_layers=6, dropconnect_p=0.5):
178
  super(CustomTransformer, self).__init__()
 
182
  self.positional_encoding = nn.Parameter(torch.zeros(1, self.num_patches, embed_dim))
183
  nn.init.trunc_normal_(self.positional_encoding, std=0.02)
184
 
185
+ # Encoder blocks
186
  self.encoder_blocks = nn.ModuleList([
187
  CombinedTransformerBlock(embed_dim, num_heads, ff_dim, dropconnect_p=dropconnect_p)
188
  for _ in range(num_layers)
189
  ])
190
+
191
+ # Decoder blocks to match saved model structure
192
  self.decoder_blocks = nn.ModuleList([
193
  DecoderBlock(embed_dim, num_heads, ff_dim)
194
  for _ in range(num_layers)
 
201
  x += self.positional_encoding
202
  x = x.transpose(0, 1) # Shape: [num_patches, batch_size, embed_dim]
203
 
204
+ # Pass through encoder blocks
205
  encoder_output = x
206
  for encoder in self.encoder_blocks:
207
  encoder_output = encoder(encoder_output)
208
 
209
+ # Pass through decoder blocks
210
  decoder_output = encoder_output
211
  for decoder in self.decoder_blocks:
212
  decoder_output = decoder(decoder_output, encoder_output)
 
215
  logits = self.fc(decoder_output)
216
  return logits
217
 
218
+
219
+ # Initialize the best model for evaluation
220
  embed_dim = 512
221
  num_heads = 32
222
  ff_dim = 1024
223
  num_classes = 10
224
  num_layers = 10 # Ensure it matches the architecture
225
 
 
226
  model_best = CustomTransformer(embed_dim, num_heads, ff_dim, num_classes, num_layers=num_layers).to(device)
227
 
228
+ # Download and load the best model from Hugging Face Hub
 
 
 
 
229
  model_best_path = hf_hub_download(repo_id=f"{username}/{model_name_best}", filename="model_best.pth")
 
 
 
230
  model_best.load_state_dict(torch.load(model_best_path, map_location=device))
231
+ model_best.eval() # Set to evaluation mode
232
 
233
+ # Evaluate the best model directly on the test set
 
 
 
 
 
 
234
  test_labels = []
235
+ test_preds_best = []
236
 
237
  with torch.no_grad():
238
  for images_test, labels_test in test_loader:
239
  images_test = images_test.to(device)
 
 
 
 
 
 
240
  logits_best = model_best(images_test)
241
  probs_best = F.softmax(logits_best, dim=1).cpu().numpy() # Convert to probabilities
242
 
243
  # Store predictions and labels
 
244
  test_preds_best.extend(probs_best)
245
  test_labels.extend(labels_test.numpy())
246
 
247
+ # Convert test set predictions to labels
248
+ test_preds_best_labels = np.argmax(test_preds_best, axis=1)
 
249
  test_labels = np.array(test_labels)
250
 
251
+ # Calculate and print test accuracy
252
+ test_accuracy = accuracy_score(test_labels, test_preds_best_labels)
253
+ print(f'Test Accuracy of Best Model: {test_accuracy * 100:.2f}%')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
254
 
255
  # Plot the confusion matrix for the test set predictions
256
+ cm = confusion_matrix(test_labels, test_preds_best_labels)
257
  disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=cifar10_test.classes)
258
  disp.plot(cmap=plt.cm.Blues)
259
 
260
  # Rotate the x-axis labels to prevent overlapping
261
  plt.xticks(rotation=45, ha='right')
262
+ plt.title('Confusion Matrix for Best Model on CIFAR-10 Test Set')
263
+ plt.savefig(os.path.join(save_dir, 'best_model_confusion_matrix.png'))
264
  plt.show()