Venkata Pydipalli commited on
Commit
fb24ec6
·
1 Parent(s): 9404685

README fixes tags.

Browse files
Files changed (1) hide show
  1. README.md +34 -17
README.md CHANGED
@@ -96,13 +96,15 @@ These attacks require access to model gradients:
96
  ## Usage
97
 
98
  ```python
99
- from datasets import load_dataset
100
  import torch
101
  from torchvision import transforms
102
  from PIL import Image
 
103
 
104
- # Load the dataset
105
- dataset = load_dataset("your-org/adversarial_pcam")
 
106
 
107
  # Define transformation
108
  transform = transforms.Compose([
@@ -110,20 +112,35 @@ transform = transforms.Compose([
110
  transforms.ToTensor()
111
  ])
112
 
113
- # Process samples
114
- def process_sample(example):
115
- img = Image.open(example["image"]).convert("RGB")
116
- return {"image": transform(img), "label": example["label"]}
117
-
118
- # Apply processing
119
- dataset = dataset.map(process_sample)
120
-
121
- # Access data
122
- img_tensor = dataset["train"][0]["image"]
123
- label = dataset["train"][0]["label"]
124
-
125
- print("Label:", label)
126
- print("Image shape:", img_tensor.shape) # Should be (3, 224, 224)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
  ```
128
 
129
  ## **📊 Attack Success Rates**
 
96
  ## Usage
97
 
98
  ```python
99
+ import json
100
  import torch
101
  from torchvision import transforms
102
  from PIL import Image
103
+ from pathlib import Path
104
 
105
+ # Load the dataset information
106
+ with open('organized_dataset/dataset.json', 'r') as f:
107
+ dataset_info = json.load(f)
108
 
109
  # Define transformation
110
  transform = transforms.Compose([
 
112
  transforms.ToTensor()
113
  ])
114
 
115
+ # Function to load and process images
116
+ def load_image(image_path):
117
+ img = Image.open(image_path).convert("RGB")
118
+ return transform(img)
119
+
120
+ # Example: Loading a set of related images (original, adversarial, and perturbation)
121
+ for entry in dataset_info:
122
+ # Load adversarial image
123
+ adv_path = Path('organized_dataset') / entry['adversarial']
124
+ adv_image = load_image(adv_path)
125
+
126
+ # Load original image
127
+ orig_path = Path('organized_dataset') / entry['original'][0]
128
+ orig_image = load_image(orig_path)
129
+
130
+ # Load perturbation if available
131
+ if entry['perturbation']:
132
+ pert_path = Path('organized_dataset') / entry['perturbation']
133
+ pert_image = load_image(pert_path)
134
+
135
+ # Access metadata
136
+ attack_type = entry['attack']
137
+ label = entry['label']
138
+ prediction = entry['prediction']
139
+
140
+ print(f"Attack: {attack_type}")
141
+ print(f"True Label: {label}")
142
+ print(f"Model Prediction: {prediction}")
143
+ print(f"Image shapes: {adv_image.shape}") # Should be (3, 224, 224)
144
  ```
145
 
146
  ## **📊 Attack Success Rates**