Spaces:
Sleeping
Sleeping
File size: 7,335 Bytes
22e1b62 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
import argparse
import logging
import os
import pandas as pd
import pytorch_lightning as pl
import timm
import torch
import torchvision.transforms as transforms
from data_split import *
from dataloader import *
from PIL import Image
from pytorch_lightning.callbacks import (
EarlyStopping,
ModelCheckpoint,
)
from sklearn.metrics import roc_auc_score
from torchmetrics import (
Accuracy,
Recall,
)
from utils_sampling import *
logging.basicConfig(
filename="training.log", filemode="w", level=logging.INFO, force=True
)
class ImageClassifier(pl.LightningModule):
def __init__(self, lmd=0):
super().__init__()
self.model = timm.create_model(
"resnet50", pretrained=True, num_classes=1
)
self.accuracy = Accuracy(task="binary", threshold=0.5)
self.recall = Recall(task="binary", threshold=0.5)
self.validation_outputs = []
self.lmd = lmd
def forward(self, x):
return self.model(x)
def training_step(self, batch):
images, labels, _ = batch
outputs = self.forward(images).squeeze()
print(f"Shape of outputs (training): {outputs.shape}")
print(f"Shape of labels (training): {labels.shape}")
loss = F.binary_cross_entropy_with_logits(outputs, labels.float())
logging.info(f"Training Step - ERM loss: {loss.item()}")
loss += self.lmd * (outputs**2).mean() # SD loss penalty
logging.info(f"Training Step - SD loss: {loss.item()}")
return loss
def validation_step(self, batch):
images, labels, _ = batch
outputs = self.forward(images).squeeze()
if outputs.shape == torch.Size([]):
return
print(f"Shape of outputs (validation): {outputs.shape}")
print(f"Shape of labels (validation): {labels.shape}")
loss = F.binary_cross_entropy_with_logits(outputs, labels.float())
preds = torch.sigmoid(outputs)
self.log("val_loss", loss, prog_bar=True, sync_dist=True)
self.log(
"val_acc",
self.accuracy(preds, labels.int()),
prog_bar=True,
sync_dist=True,
)
self.log(
"val_recall",
self.recall(preds, labels.int()),
prog_bar=True,
sync_dist=True,
)
output = {"val_loss": loss, "preds": preds, "labels": labels}
self.validation_outputs.append(output)
logging.info(f"Validation Step - Batch loss: {loss.item()}")
return output
def predict_step(self, batch):
images, label, domain = batch
outputs = self.forward(images).squeeze()
preds = torch.sigmoid(outputs)
return preds, label, domain
def on_validation_epoch_end(self):
if not self.validation_outputs:
logging.warning("No outputs in validation step to process")
return
preds = torch.cat([x["preds"] for x in self.validation_outputs])
labels = torch.cat([x["labels"] for x in self.validation_outputs])
if labels.unique().size(0) == 1:
logging.warning("Only one class in validation step")
return
auc_score = roc_auc_score(labels.cpu(), preds.cpu())
self.log("val_auc", auc_score, prog_bar=True, sync_dist=True)
logging.info(f"Validation Epoch End - AUC score: {auc_score}")
self.validation_outputs = []
def configure_optimizers(self):
optimizer = torch.optim.Adam(self.model.parameters(), lr=0.0005)
return optimizer
checkpoint_callback = ModelCheckpoint(
monitor="val_loss",
dirpath="./model_checkpoints/",
filename="image-classifier-{step}-{val_loss:.2f}",
save_top_k=3,
mode="min",
every_n_train_steps=1001,
enable_version_counter=True,
)
early_stop_callback = EarlyStopping(
monitor="val_loss",
patience=4,
mode="min",
)
def load_image(image_path, transform=None):
image = Image.open(image_path).convert("RGB")
if transform:
image = transform(image)
return image
def predict_single_image(image_path, model, transform=None):
image = load_image(image_path, transform)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
image = image.to(device)
model.eval()
with torch.no_grad():
image = image.unsqueeze(0)
output = model(image).squeeze()
print(output)
prediction = torch.sigmoid(output).item()
return prediction
parser = argparse.ArgumentParser()
parser.add_argument(
"--ckpt_path", help="checkpoint to continue from", required=False
)
parser.add_argument(
"--predict", help="predict on test set", action="store_true"
)
parser.add_argument("--reset", help="reset training", action="store_true")
parser.add_argument(
"--predict_image",
help="predict the class of a single image",
action="store_true",
)
parser.add_argument(
"--image_path",
help="path to the image to predict",
type=str,
required=False,
)
args = parser.parse_args()
train_domains = [0, 1, 4]
val_domains = [0, 1, 4]
lmd_value = 0
if args.predict:
test_dl = load_dataloader(
[0, 1, 2, 3, 4], "test", batch_size=128, num_workers=1
)
model = ImageClassifier.load_from_checkpoint(args.ckpt_path)
trainer = pl.Trainer()
predictions = trainer.predict(model, dataloaders=test_dl)
preds, labels, domains = zip(*predictions)
preds = torch.cat(preds).cpu().numpy()
labels = torch.cat(labels).cpu().numpy()
domains = torch.cat(domains).cpu().numpy()
print(preds.shape, labels.shape, domains.shape)
df = pd.DataFrame({"preds": preds, "labels": labels, "domains": domains})
filename = "preds-" + args.ckpt_path.split("/")[-1]
df.to_csv(f"outputs/{filename}.csv", index=False)
elif args.predict_image:
image_path = args.image_path
model = ImageClassifier.load_from_checkpoint(args.ckpt_path)
# Define the transformations for the image
transform = transforms.Compose(
[
transforms.Resize((224, 224)), # Image size expected by ResNet50
transforms.ToTensor(),
transforms.Normalize(
mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]
),
]
)
prediction = predict_single_image(image_path, model, transform)
print("prediction", prediction)
# Output the prediction
print(
f"Prediction for {image_path}: {'Human' if prediction <= 0.001 else 'Generated'}"
)
else:
train_dl = load_dataloader(
train_domains, "train", batch_size=128, num_workers=4
)
logging.info("Training dataloader loaded")
val_dl = load_dataloader(val_domains, "val", batch_size=128, num_workers=4)
logging.info("Validation dataloader loaded")
if args.reset:
model = ImageClassifier.load_from_checkpoint(args.ckpt_path)
else:
model = ImageClassifier(lmd=lmd_value)
trainer = pl.Trainer(
callbacks=[checkpoint_callback, early_stop_callback],
max_steps=20000,
val_check_interval=1000,
check_val_every_n_epoch=None,
)
trainer.fit(
model=model,
train_dataloaders=train_dl,
val_dataloaders=val_dl,
ckpt_path=args.ckpt_path if not args.reset else None,
)
|