how is label indetfied for adv or not import torch import torch.nn as nn import…
- User
how is label indetfied for adv or not import torch import torch.nn as nn import torch.optim as optim from torch.utils.data import DataLoader, TensorDataset, random_split import torchvision.transforms as transforms from torchvision.models.resnet import BasicBlock from torchattacks import CW import medmnist from medmnist import INFO import numpy as np
Your ResNet18_MedMNIST class
class BasicBlock(nn.Module): expansion = 1
def __init__(self, in_channels, out_channels, stride=1): super(BasicBlock, self).__init__() self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=stride, padding=1, bias=False) self.bn1 = nn.BatchNorm2d(out_channels) self.relu = nn.ReLU(inplace=True) self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1, bias=False) self.bn2 = nn.BatchNorm2d(out_channels) self.downsample = None if stride != 1 or in_channels != out_channels: self.downsample = nn.Sequential( nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=stride, bias=False), nn.BatchNorm2d(out_channels) ) def forward(self, x): identity = x out = self.conv1(x) out = self.bn1(out) out = self.relu(out) out = self.conv2(out) out = self.bn2(out) if self.downsample is not None: identity = self.downsample(x) out += identity out = self.relu(out) return outFixed ResNet18_MedMNIST class
class ResNet18_MedMNIST(nn.Module): def init(self, num_classes=8): super(ResNet18_MedMNIST, self).init() self.in_channels = 64
# For MedMNIST: Input channels = 3 (RGB), 28x28 images self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False) self.bn1 = nn.BatchNorm2d(64) self.relu = nn.ReLU(inplace=True) self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) self.layer1 = self._make_layer(BasicBlock, 64, 2, stride=1) self.layer2 = self._make_layer(BasicBlock, 128, 2, stride=2) self.layer3 = self._make_layer(BasicBlock, 256, 2, stride=2) self.layer4 = self._make_layer(BasicBlock, 512, 2, stride=2) self.avgpool = nn.AdaptiveAvgPool2d((1, 1)) self.fc = nn.Linear(512, num_classes) # Initialize weights for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') elif isinstance(m, nn.BatchNorm2d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) def _make_layer(self, block, out_channels, num_blocks, stride): layers = [] layers.append(block(self.in_channels, out_channels, stride)) self.in_channels = out_channels for _ in range(1, num_blocks): layers.append(block(self.in_channels, out_channels)) return nn.Sequential(*layers) def forward(self, x): x = self.conv1(x) x = self.bn1(x) x = self.relu(x) x = self.maxpool(x) x = self.layer1(x) x = self.layer2(x) x = self.layer3(x) x = self.layer4(x) x = self.avgpool(x) x = x.view(x.size(0), -1) x = self.fc(x) return xdef train_model_properly(model, train_loader, val_loader, device, epochs=50): """ Properly train the ResNet18_MedMNIST model """ print("Training ResNet18_MedMNIST model...")
optimizer = optim.Adam(model.parameters(), lr=0.001, weight_decay=1e-4) criterion = nn.CrossEntropyLoss() scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=20, gamma=0.1) best_val_acc = 0.0 for epoch in range(epochs): # Training phase model.train() train_loss = 0.0 train_correct = 0 train_total = 0 for batch_idx, (data, target) in enumerate(train_loader): data, target = data.to(device), target.squeeze().long().to(device) optimizer.zero_grad() output = model(data) loss = criterion(output, target) loss.backward() optimizer.step() train_loss += loss.item() _, predicted = torch.max(output.data, 1) train_total += target.size(0) train_correct += (predicted == target).sum().item() # Validation phase model.eval() val_loss = 0.0 val_correct = 0 val_total = 0 with torch.no_grad(): for data, target in val_loader: data, target = data.to(device), target.squeeze().long().to(device) output = model(data) loss = criterion(output, target) val_loss += loss.item() _, predicted = torch.max(output.data, 1) val_total += target.size(0) val_correct += (predicted == target).sum().item() scheduler.step() train_acc = 100. * train_correct / train_total val_acc = 100. * val_correct / val_total print(f'Epoch {epoch+1}/{epochs}:') print(f' Train Loss: {train_loss/len(train_loader):.4f}, Train Acc: {train_acc:.2f}%') print(f' Val Loss: {val_loss/len(val_loader):.4f}, Val Acc: {val_acc:.2f}%') if val_acc > best_val_acc: best_val_acc = val_acc torch.save(model.state_dict(), 'best_resnet18_medmnist.pth') print(f' ✓ New best model saved! Val Acc: {val_acc:.2f}%') return modeldef generate_real_carlini_attacks(model, images, labels, c=1e-2, kappa=0, steps=1000, lr=0.01): """ Generate REAL Carlini-Wagner L2 attacks (no dummy data) """ model.eval() device = next(model.parameters()).device
images = images.to(device) labels = labels.to(device) # Create Carlini-Wagner attack attack = CW(model, c=c, kappa=kappa, steps=steps, lr=lr) print(f"Generating Carlini attacks with c={c}, steps={steps}...") # Generate adversarial examples adv_images = attack(images, labels) return adv_images.cpu()def create_complete_carlini_pipeline(data_flag='pathmnist', train_epochs=30, attack_config=None): """ Complete pipeline: Load data -> Train model -> Generate Carlini attacks -> Create dataloaders
Returns: train_loader, val_loader, test_loader with (clean_img, carlini_adv_img, label) format """ if attack_config is None: attack_config = {'c': 1e-2, 'kappa': 0, 'steps': 1000, 'lr': 0.01} # Setup device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') print(f"Using device: {device}") # Load MedMNIST dataset print(f"\n1. Loading {data_flag} dataset...") info = INFO[data_flag] n_classes = len(info['label']) DataClass = getattr(medmnist, info['python_class']) transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,)) ]) # Load all available data train_dataset = DataClass(split='train', transform=transform, download=True) test_dataset = DataClass(split='test', transform=transform, download=True) val_dataset = DataClass(split='val', transform=transform, download=True) # Combine all splits to get maximum data all_images = [] all_labels = [] print("Collecting all available samples...") for dataset in [train_dataset, val_dataset, test_dataset]: loader = DataLoader(dataset, batch_size=128, shuffle=False) for images, labels in loader: all_images.append(images) all_labels.append(labels) all_images = torch.cat(all_images) all_labels = torch.cat(all_labels) print(f"Total samples collected: {len(all_images)}") # Create initial train/val split for model training (80/20) total_samples = len(all_images) train_size = int(0.8 * total_samples) val_size = total_samples - train_size torch.manual_seed(42) # For reproducible splits indices = torch.randperm(total_samples) train_indices = indices[:train_size] val_indices = indices[train_size:] train_images = all_images[train_indices] train_labels = all_labels[train_indices] val_images = all_images[val_indices] val_labels = all_labels[val_indices] # Create training dataloaders train_dataset_temp = TensorDataset(train_images, train_labels) val_dataset_temp = TensorDataset(val_images, val_labels) train_loader_temp = DataLoader(train_dataset_temp, batch_size=64, shuffle=True) val_loader_temp = DataLoader(val_dataset_temp, batch_size=64, shuffle=False) print(f"Training split: {len(train_images)} samples") print(f"Validation split: {len(val_images)} samples") # 2. Train the model properly print(f"\n2. Training ResNet18_MedMNIST model...") model = ResNet18_MedMNIST(num_classes=n_classes).to(device) model = train_model_properly(model, train_loader_temp, val_loader_temp, device, epochs=train_epochs) # Load best model model.load_state_dict(torch.load('best_resnet18_medmnist.pth')) model.eval() # 3. Generate Carlini attacks for ALL samples print(f"\n3. Generating Carlini-Wagner attacks for all samples...") batch_size = 32 # Process in smaller batches for memory efficiency all_clean_images = [] all_carlini_images = [] all_clean_labels = [] # Process all samples in batches for i in range(0, len(all_images), batch_size): batch_images = all_images[i:i+batch_size] batch_labels = all_labels[i:i+batch_size].squeeze().long() # Generate Carlini attacks for this batch carlini_batch = generate_real_carlini_attacks( model, batch_images, batch_labels, **attack_config ) all_clean_images.append(batch_images) all_carlini_images.append(carlini_batch) all_clean_labels.append(batch_labels) if (i // batch_size + 1) % 10 == 0: print(f" Processed {i+len(batch_images)}/{len(all_images)} samples") # Concatenate all batches final_clean_images = torch.cat(all_clean_images) final_carlini_images = torch.cat(all_carlini_images) final_labels = torch.cat(all_clean_labels) print(f"✓ Generated {len(final_clean_images)} Carlini adversarial examples") # 4. Create final train/val/test splits (70/15/15) print(f"\n4. Creating final train/val/test splits...") total_final = len(final_clean_images) final_train_size = int(0.7 * total_final) final_val_size = int(0.15 * total_final) final_test_size = total_final - final_train_size - final_val_size # Shuffle and split torch.manual_seed(42) final_indices = torch.randperm(total_final) train_idx = final_indices[:final_train_size] val_idx = final_indices[final_train_size:final_train_size + final_val_size] test_idx = final_indices[final_train_size + final_val_size:] # Create final datasets train_clean = final_clean_images[train_idx] train_carlini = final_carlini_images[train_idx] train_labels_final = final_labels[train_idx] val_clean = final_clean_images[val_idx] val_carlini = final_carlini_images[val_idx] val_labels_final = final_labels[val_idx] test_clean = final_clean_images[test_idx] test_carlini = final_carlini_images[test_idx] test_labels_final = final_labels[test_idx] # Create final dataloaders with (clean_img, adv_img, label) format train_dataset_final = TensorDataset(train_clean, train_carlini, train_labels_final) val_dataset_final = TensorDataset(val_clean, val_carlini, val_labels_final) test_dataset_final = TensorDataset(test_clean, test_carlini, test_labels_final) train_loader = DataLoader(train_dataset_final, batch_size=32, shuffle=True) val_loader = DataLoader(val_dataset_final, batch_size=32, shuffle=False) test_loader = DataLoader(test_dataset_final, batch_size=32, shuffle=False) print(f"✓ Final splits created:") print(f" Train: {len(train_dataset_final)} samples ({len(train_dataset_final)/total_final:.1%})") print(f" Val: {len(val_dataset_final)} samples ({len(val_dataset_final)/total_final:.1%})") print(f" Test: {len(test_dataset_final)} samples ({len(test_dataset_final)/total_final:.1%})") return train_loader, val_loader, test_loaderdef verify_carlini_quality(model, dataloader, split_name): """ Verify that Carlini attacks are actually adversarial (not dummy) """ print(f"\n--- Verifying {split_name} Carlini Attack Quality ---")
model.eval() device = next(model.parameters()).device clean_correct = 0 adv_correct = 0 total = 0 l2_distances = [] with torch.no_grad(): for batch_idx, (clean_imgs, carlini_imgs, labels) in enumerate(dataloader): if batch_idx >= 10: # Check first 10 batches break clean_imgs = clean_imgs.to(device) carlini_imgs = carlini_imgs.to(device) labels = labels.to(device) # Get predictions clean_outputs = model(clean_imgs) adv_outputs = model(carlini_imgs) clean_preds = torch.argmax(clean_outputs, dim=1) adv_preds = torch.argmax(adv_outputs, dim=1) clean_correct += (clean_preds == labels).sum().item() adv_correct += (adv_preds == labels).sum().item() total += labels.size(0) # Calculate L2 distances l2_dist = torch.norm((carlini_imgs - clean_imgs).view(len(clean_imgs), -1), p=2, dim=1) l2_distances.extend(l2_dist.cpu().numpy()) clean_acc = clean_correct / total adv_acc = adv_correct / total attack_success = 1 - adv_acc avg_l2 = np.mean(l2_distances) print(f"Clean Accuracy: {clean_acc:.3f}") print(f"Adversarial Accuracy: {adv_acc:.3f}") print(f"Attack Success Rate: {attack_success:.3f}") print(f"Average L2 Distance: {avg_l2:.6f}") if attack_success > 0.5: print("✓ Carlini attacks are effective!") else: print("⚠ Carlini attacks may need stronger parameters") - Assistant
In your code, the label for “adversarial or not” is not explicitly stored — instead, you keep the original classification label (the MedMNIST class, e.g., disease category) and store both the clean and adversarial versions of each image in the dataset. Here’s how it works step-by-step:
- Labels come from the original dataset pythonCopyEdittrain_labels_final = final_labels[train_idx]
final_labels is taken directly from the original MedMNIST dataset — it’s the true class (e.g., “lung opacity” in ChestMNIST).
These labels never change; they are not “adversarial” or “clean” labels.
Both clean_img and adv_img have the same label.
- Adversarial status is determined by which tensor you pass to the model
The dataset is created as:
pythonCopyEditTensorDataset(clean_img, adv_img, label)
If you pass clean_img to the model → you’re testing on the original image.
If you pass adv_img to the model → you’re testing on the adversarial image.
You don’t have a binary “is_adv” label because the data structure itself makes it clear — dataset[i][0] is clean, dataset[i][1] is adversarial.
- Determining if an image is adversarial
In verify_carlini_quality():
pythonCopyEditclean_preds = torch.argmax(clean_outputs, dim=1) adv_preds = torch.argmax(adv_outputs, dim=1)
The code compares:
Clean accuracy → (clean_preds == labels)
Adversarial accuracy → (adv_preds == labels)
If the prediction on adv_img ≠ the true label (while the clean version was correct), it means the attack succeeded.
- If you wanted an explicit “adversarial or not” label You’d have to create an extra binary label, e.g.: pythonCopyEditadv_flag = torch.cat([ torch.zeros(len(clean_images)), # clean torch.ones(len(adv_images)) # adversarial ])
And then train or test with (image, adv_flag).
💡 In short: Right now, your pipeline identifies “adversarial or not” by the tensor’s position in the tuple (clean_img, adv_img, label) rather than storing it as a separate label. The actual label is always the original class from MedMNIST.
If you want, I can show you how to add a proper “is_adv” label so you could train a binary adversarial detector. That would make adversarial identification explicit instead of implicit. Would you like me to add that?
- User
how will it identify here: import torch import os from torchvision.transforms import ToPILImage
def save_carlini_attack_images(dataloader, save_dir="carlini_attack_images", split_name=""): """ Save generated Carlini attack images from dataloader
Args: dataloader: DataLoader with (clean_img, carlini_adv_img, label) format save_dir: Directory to save images split_name: Name of the split (train/val/test) for folder organization """ if split_name: save_dir = os.path.join(save_dir, split_name) os.makedirs(save_dir, exist_ok=True) to_pil = ToPILImage() count = 0 print(f"Saving {split_name} Carlini attack images to '{save_dir}'...") for batch_idx, (clean_imgs, carlini_imgs, labels) in enumerate(dataloader): for i in range(len(clean_imgs)): clean_img = clean_imgs[i].cpu() carlini_img = carlini_imgs[i].cpu() label = labels[i].item() # Save clean image clean_path = os.path.join(save_dir, f"clean_{count:05d}_label_{label}.png") to_pil(clean_img).save(clean_path) # Save Carlini adversarial image carlini_path = os.path.join(save_dir, f"carlini_{count:05d}_label_{label}.png") to_pil(carlini_img).save(carlini_path) count += 1 if batch_idx % 10 == 0: print(f" Saved {count} image pairs...") print(f"✓ Saved {count} pairs of clean and Carlini adversarial images") return countSave all your generated attacks
print("Saving all generated Carlini attacks...")
Save train set
train_count = save_carlini_attack_images(train_loader, "carlini_attacks", "train")
Save validation set
val_count = save_carlini_attack_images(val_loader, "carlini_attacks", "val")
Save test set
test_count = save_carlini_attack_images(test_loader, "carlini_attacks", "test")
print(f"\nTotal images saved:") print(f" Train: {train_count} pairs") print(f" Val: {val_count} pairs") print(f" Test: {test_count} pairs") print(f" Total: {train_count + val_count + test_count} pairs")
- Assistant
In this saving function, the code does not need to “identify” whether an image is adversarial or not based on model predictions — it already knows that the first tensor in the tuple is the clean image and the second tensor is the Carlini adversarial image because of how the dataloader was built earlier: pythonCopyEditTensorDataset(clean_img, adv_img, label)
So in: pythonCopyEditfor batch_idx, (clean_imgs, carlini_imgs, labels) in enumerate(dataloader):
clean_imgs → always clean, original dataset images
carlini_imgs → always adversarially perturbed versions
labels → always the original class label from MedMNIST (same for both clean & adv)
The “identification” here is structural, not computed:
When saving, the code names files like clean_00000_label_3.png and carlini_00000_label_3.png.
The prefix "clean_" or "carlini_" is how you’ll know later which is which.
It never checks the model’s prediction here — it already knows which one is adv because you generated and stored them in separate positions in the dataset.
If later you want to load these PNGs and automatically detect which are adversarial without relying on filenames, you’d need either:
A separate binary label (is_adv = 0/1) saved alongside the images, or
A naming pattern (like in this code) and parse it back.
If you want, I can modify this saving code so it also writes a CSV file that records: file_pathlabelis_advclean_00000_label_3.png30carlini_00000_label_3.png31 That way, the “adversarial or not” label becomes explicit rather than just implicit from the filename. Would you like me to do that?
- User
now give loading code if i wan t it as for clean_img, adv_img, label in train_loader:
- Assistant
Got it — you want a loader that reads back the saved PNGs and gives you pythonCopyEditfor clean_img, adv_img, label in train_loader:
just like before. Here’s a clean way to do it, assuming you saved them in the same folder structure your code created:
Loading Saved Carlini Attack Images pythonCopyEditimport os from PIL import Image from torch.utils.data import Dataset, DataLoader from torchvision import transforms
class CarliniImageDataset(Dataset): def init(self, root_dir): """ Args: root_dir: path to split directory (e.g., "carlini_attacks/train") """ self.root_dir = root_dir self.transform = transforms.ToTensor()
# Find all clean images and match with their corresponding adversarial images clean_files = sorted([f for f in os.listdir(root_dir) if f.startswith("clean_")]) adv_files = sorted([f for f in os.listdir(root_dir) if f.startswith("carlini_")]) if len(clean_files) != len(adv_files): raise ValueError("Mismatch between clean and Carlini image counts!") # Pair them together self.pairs = [] for clean_f, adv_f in zip(clean_files, adv_files): # Extract label from filename (last part after '_label_') label_str = clean_f.split("_label_")[-1].replace(".png", "") label = int(label_str) self.pairs.append((clean_f, adv_f, label)) def __len__(self): return len(self.pairs) def __getitem__(self, idx): clean_f, adv_f, label = self.pairs[idx] clean_img = Image.open(os.path.join(self.root_dir, clean_f)).convert("RGB") adv_img = Image.open(os.path.join(self.root_dir, adv_f)).convert("RGB") if self.transform: clean_img = self.transform(clean_img) adv_img = self.transform(adv_img) return clean_img, adv_img, labelExample usage
train_dataset = CarliniImageDataset("carlini_attacks/train") train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)
Now you can iterate like before:
for clean_img, adv_img, label in train_loader: print(clean_img.shape, adv_img.shape, label) break
How It Works
It looks for files starting with "clean_" and "carlini_" in the given directory.
It sorts them to keep matching pairs aligned.
It extracts the class label from the filename (because you stored label_X).
It returns them in (clean_img, adv_img, label) format exactly like your in-memory pipeline.
If your saving function keeps the same naming convention, this loader will always give you a dataset identical in structure to the one you had before saving.
If you want, I can make a unified loader that can load train/val/test from carlini_attacks/ in one go and give you all three dataloaders exactly like create_complete_carlini_pipeline() did. That way you can resume work without retraining or regenerating. Would you like me to do that next?
- User
will this preserve quakift of img:/kaggle/input/cw-data-blood/
- Assistant
Yes — if your /kaggle/input/cw-data-blood/ folder contains the PNGs you saved earlier, this loader will preserve the exact pixel values from those files, as long as:
You didn’t re-normalize before saving — In your saving functi
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.