crtal commited on
Commit
e556bb4
·
1 Parent(s): 59d2f51

refactor: consolidate 8 duplicated train/eval scripts into 2 parameterized scripts

Browse files
scripts/evaluate.py CHANGED
@@ -1,12 +1,31 @@
 
 
1
  import torch
2
  import torch.nn as nn
3
  from torchvision import datasets, models, transforms
4
  from torch.utils.data import DataLoader
5
- import os
6
  from sklearn.metrics import classification_report, confusion_matrix
7
 
8
- def evaluate_model(data_dir, model_path):
9
- # Data transformations for testing
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  data_transforms = transforms.Compose([
11
  transforms.Resize(256),
12
  transforms.CenterCrop(224),
@@ -14,17 +33,13 @@ def evaluate_model(data_dir, model_path):
14
  transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
15
  ])
16
 
17
- # Load test dataset
18
  test_dataset = datasets.ImageFolder(os.path.join(data_dir, 'test'), data_transforms)
19
  test_loader = DataLoader(test_dataset, batch_size=32, shuffle=False, num_workers=4)
20
-
21
  device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
22
  print(f"Using device: {device}")
23
 
24
- # Load model
25
- model = models.mobilenet_v2()
26
- num_ftrs = model.classifier[1].in_features
27
- model.classifier[1] = nn.Linear(num_ftrs, 2)
28
  model.load_state_dict(torch.load(model_path, map_location=device))
29
  model = model.to(device)
30
  model.eval()
@@ -34,22 +49,23 @@ def evaluate_model(data_dir, model_path):
34
 
35
  with torch.no_grad():
36
  for inputs, labels in test_loader:
37
- inputs = inputs.to(device)
38
- labels = labels.to(device)
39
-
40
  outputs = model(inputs)
41
  _, preds = torch.max(outputs, 1)
42
-
43
  all_preds.extend(preds.cpu().numpy())
44
  all_labels.extend(labels.cpu().numpy())
45
 
46
  print("\nClassification Report:")
47
  print(classification_report(all_labels, all_preds, target_names=test_dataset.classes))
48
-
49
  print("\nConfusion Matrix:")
50
  print(confusion_matrix(all_labels, all_preds))
51
 
 
52
  if __name__ == "__main__":
53
- DATA_DIR = "./data/split"
54
- MODEL_PATH = "./models/spoilage_model.pth"
55
- evaluate_model(DATA_DIR, MODEL_PATH)
 
 
 
 
 
1
+ import argparse
2
+ import os
3
  import torch
4
  import torch.nn as nn
5
  from torchvision import datasets, models, transforms
6
  from torch.utils.data import DataLoader
 
7
  from sklearn.metrics import classification_report, confusion_matrix
8
 
9
+
10
+ def build_model(arch):
11
+ if arch == "mobilenet":
12
+ model = models.mobilenet_v2()
13
+ num_ftrs = model.classifier[1].in_features
14
+ model.classifier[1] = nn.Linear(num_ftrs, 2)
15
+ elif arch in ("swin_t", "swin_t_finetune"):
16
+ model = models.swin_t()
17
+ num_ftrs = model.head.in_features
18
+ model.head = nn.Linear(num_ftrs, 2)
19
+ elif arch == "swin_s":
20
+ model = models.swin_s()
21
+ num_ftrs = model.head.in_features
22
+ model.head = nn.Linear(num_ftrs, 2)
23
+ else:
24
+ raise ValueError(f"Unknown arch: {arch}")
25
+ return model
26
+
27
+
28
+ def evaluate(data_dir, model_path, arch):
29
  data_transforms = transforms.Compose([
30
  transforms.Resize(256),
31
  transforms.CenterCrop(224),
 
33
  transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
34
  ])
35
 
 
36
  test_dataset = datasets.ImageFolder(os.path.join(data_dir, 'test'), data_transforms)
37
  test_loader = DataLoader(test_dataset, batch_size=32, shuffle=False, num_workers=4)
38
+
39
  device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
40
  print(f"Using device: {device}")
41
 
42
+ model = build_model(arch)
 
 
 
43
  model.load_state_dict(torch.load(model_path, map_location=device))
44
  model = model.to(device)
45
  model.eval()
 
49
 
50
  with torch.no_grad():
51
  for inputs, labels in test_loader:
52
+ inputs, labels = inputs.to(device), labels.to(device)
 
 
53
  outputs = model(inputs)
54
  _, preds = torch.max(outputs, 1)
 
55
  all_preds.extend(preds.cpu().numpy())
56
  all_labels.extend(labels.cpu().numpy())
57
 
58
  print("\nClassification Report:")
59
  print(classification_report(all_labels, all_preds, target_names=test_dataset.classes))
 
60
  print("\nConfusion Matrix:")
61
  print(confusion_matrix(all_labels, all_preds))
62
 
63
+
64
  if __name__ == "__main__":
65
+ parser = argparse.ArgumentParser()
66
+ parser.add_argument("--arch", choices=["mobilenet", "swin_t", "swin_t_finetune", "swin_s"], required=True)
67
+ parser.add_argument("--data-dir", default="./data/split")
68
+ parser.add_argument("--weights", required=True, help="Path to model weights file")
69
+ args = parser.parse_args()
70
+
71
+ evaluate(args.data_dir, args.weights, args.arch)
scripts/evaluate_swin.py DELETED
@@ -1,57 +0,0 @@
1
- import torch
2
- import torch.nn as nn
3
- from torchvision import datasets, models, transforms
4
- from torch.utils.data import DataLoader
5
- import os
6
- from sklearn.metrics import classification_report, confusion_matrix
7
-
8
- def evaluate_model(data_dir, model_path):
9
- # Data transformations for testing
10
- data_transforms = transforms.Compose([
11
- transforms.Resize(256),
12
- transforms.CenterCrop(224),
13
- transforms.ToTensor(),
14
- transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
15
- ])
16
-
17
- # Load test dataset
18
- test_dataset = datasets.ImageFolder(os.path.join(data_dir, 'test'), data_transforms)
19
- test_loader = DataLoader(test_dataset, batch_size=32, shuffle=False, num_workers=4)
20
-
21
- device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
22
- print(f"Using device: {device}")
23
-
24
- # Load Swin model architecture
25
- model = models.swin_t()
26
- num_ftrs = model.head.in_features
27
- model.head = nn.Linear(num_ftrs, 2)
28
-
29
- # Load weights
30
- model.load_state_dict(torch.load(model_path, map_location=device))
31
- model = model.to(device)
32
- model.eval()
33
-
34
- all_preds = []
35
- all_labels = []
36
-
37
- with torch.no_grad():
38
- for inputs, labels in test_loader:
39
- inputs = inputs.to(device)
40
- labels = labels.to(device)
41
-
42
- outputs = model(inputs)
43
- _, preds = torch.max(outputs, 1)
44
-
45
- all_preds.extend(preds.cpu().numpy())
46
- all_labels.extend(labels.cpu().numpy())
47
-
48
- print("\nClassification Report (Swin Transformer):")
49
- print(classification_report(all_labels, all_preds, target_names=test_dataset.classes))
50
-
51
- print("\nConfusion Matrix:")
52
- print(confusion_matrix(all_labels, all_preds))
53
-
54
- if __name__ == "__main__":
55
- DATA_DIR = "./data/split"
56
- MODEL_PATH = "./models/spoilage_model_swin.pth"
57
- evaluate_model(DATA_DIR, MODEL_PATH)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
scripts/evaluate_swin_finetuned.py DELETED
@@ -1,53 +0,0 @@
1
- import torch
2
- import torch.nn as nn
3
- from torchvision import datasets, models, transforms
4
- from torch.utils.data import DataLoader
5
- import os
6
- from sklearn.metrics import classification_report, confusion_matrix
7
-
8
- def evaluate_model(data_dir, model_path):
9
- data_transforms = transforms.Compose([
10
- transforms.Resize(256),
11
- transforms.CenterCrop(224),
12
- transforms.ToTensor(),
13
- transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
14
- ])
15
-
16
- test_dataset = datasets.ImageFolder(os.path.join(data_dir, 'test'), data_transforms)
17
- test_loader = DataLoader(test_dataset, batch_size=32, shuffle=False, num_workers=4)
18
-
19
- device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
20
- print(f"Using device: {device}")
21
-
22
- model = models.swin_t()
23
- num_ftrs = model.head.in_features
24
- model.head = nn.Linear(num_ftrs, 2)
25
-
26
- model.load_state_dict(torch.load(model_path, map_location=device))
27
- model = model.to(device)
28
- model.eval()
29
-
30
- all_preds = []
31
- all_labels = []
32
-
33
- with torch.no_grad():
34
- for inputs, labels in test_loader:
35
- inputs = inputs.to(device)
36
- labels = labels.to(device)
37
-
38
- outputs = model(inputs)
39
- _, preds = torch.max(outputs, 1)
40
-
41
- all_preds.extend(preds.cpu().numpy())
42
- all_labels.extend(labels.cpu().numpy())
43
-
44
- print("\nClassification Report (Fine-tuned Swin):")
45
- print(classification_report(all_labels, all_preds, target_names=test_dataset.classes))
46
-
47
- print("\nConfusion Matrix:")
48
- print(confusion_matrix(all_labels, all_preds))
49
-
50
- if __name__ == "__main__":
51
- DATA_DIR = "./data/split"
52
- MODEL_PATH = "./models/spoilage_model_swin_finetuned.pth"
53
- evaluate_model(DATA_DIR, MODEL_PATH)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
scripts/evaluate_swin_small.py DELETED
@@ -1,53 +0,0 @@
1
- import torch
2
- import torch.nn as nn
3
- from torchvision import datasets, models, transforms
4
- from torch.utils.data import DataLoader
5
- import os
6
- from sklearn.metrics import classification_report, confusion_matrix
7
-
8
- def evaluate_model(data_dir, model_path):
9
- data_transforms = transforms.Compose([
10
- transforms.Resize(256),
11
- transforms.CenterCrop(224),
12
- transforms.ToTensor(),
13
- transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
14
- ])
15
-
16
- test_dataset = datasets.ImageFolder(os.path.join(data_dir, 'test'), data_transforms)
17
- test_loader = DataLoader(test_dataset, batch_size=32, shuffle=False, num_workers=4)
18
-
19
- device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
20
- print(f"Using device: {device}")
21
-
22
- model = models.swin_s()
23
- num_ftrs = model.head.in_features
24
- model.head = nn.Linear(num_ftrs, 2)
25
-
26
- model.load_state_dict(torch.load(model_path, map_location=device))
27
- model = model.to(device)
28
- model.eval()
29
-
30
- all_preds = []
31
- all_labels = []
32
-
33
- with torch.no_grad():
34
- for inputs, labels in test_loader:
35
- inputs = inputs.to(device)
36
- labels = labels.to(device)
37
-
38
- outputs = model(inputs)
39
- _, preds = torch.max(outputs, 1)
40
-
41
- all_preds.extend(preds.cpu().numpy())
42
- all_labels.extend(labels.cpu().numpy())
43
-
44
- print("\nClassification Report (Swin-Small):")
45
- print(classification_report(all_labels, all_preds, target_names=test_dataset.classes))
46
-
47
- print("\nConfusion Matrix:")
48
- print(confusion_matrix(all_labels, all_preds))
49
-
50
- if __name__ == "__main__":
51
- DATA_DIR = "./data/split"
52
- MODEL_PATH = "./models/spoilage_model_swin_small.pth"
53
- evaluate_model(DATA_DIR, MODEL_PATH)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
scripts/train.py CHANGED
@@ -1,56 +1,90 @@
 
 
 
1
  import torch
2
  import torch.nn as nn
3
  import torch.optim as optim
4
  from torchvision import datasets, models, transforms
5
  from torch.utils.data import DataLoader
6
- import os
7
- import copy
8
 
9
- def train_model(data_dir, model_save_path, num_epochs=10, batch_size=32, learning_rate=0.001):
10
- # Data transformations
11
- data_transforms = {
12
- 'train': transforms.Compose([
13
- transforms.RandomResizedCrop(224),
14
- transforms.RandomHorizontalFlip(),
15
- transforms.RandomRotation(15),
16
- transforms.ToTensor(),
17
- transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
18
- ]),
19
- 'val': transforms.Compose([
20
- transforms.Resize(256),
21
- transforms.CenterCrop(224),
22
- transforms.ToTensor(),
23
- transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
24
- ]),
25
- }
26
 
27
- # Load datasets
28
- image_datasets = {x: datasets.ImageFolder(os.path.join(data_dir, x), data_transforms[x])
29
- for x in ['train', 'val']}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  dataloaders = {x: DataLoader(image_datasets[x], batch_size=batch_size, shuffle=True, num_workers=4)
31
  for x in ['train', 'val']}
32
  dataset_sizes = {x: len(image_datasets[x]) for x in ['train', 'val']}
33
- class_names = image_datasets['train'].classes
34
 
35
  device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
36
  print(f"Using device: {device}")
37
 
38
- # Load pre-trained MobileNetV2
39
- model = models.mobilenet_v2(weights='DEFAULT')
40
-
41
- # Freeze initial layers
42
- for param in model.parameters():
43
- param.requires_grad = False
44
-
45
- # Replace the classifier
46
- num_ftrs = model.classifier[1].in_features
47
- model.classifier[1] = nn.Linear(num_ftrs, 2) # Binary: Fresh vs Spoiled
48
-
49
- model = model.to(device)
50
 
51
  criterion = nn.CrossEntropyLoss()
52
- # Only optimize parameters of the classifier
53
- optimizer = optim.Adam(model.classifier[1].parameters(), lr=learning_rate)
54
 
55
  best_model_wts = copy.deepcopy(model.state_dict())
56
  best_acc = 0.0
@@ -60,25 +94,18 @@ def train_model(data_dir, model_save_path, num_epochs=10, batch_size=32, learnin
60
  print('-' * 10)
61
 
62
  for phase in ['train', 'val']:
63
- if phase == 'train':
64
- model.train()
65
- else:
66
- model.eval()
67
-
68
  running_loss = 0.0
69
  running_corrects = 0
70
 
71
  for inputs, labels in dataloaders[phase]:
72
- inputs = inputs.to(device)
73
- labels = labels.to(device)
74
-
75
  optimizer.zero_grad()
76
 
77
  with torch.set_grad_enabled(phase == 'train'):
78
  outputs = model(inputs)
79
  _, preds = torch.max(outputs, 1)
80
  loss = criterion(outputs, labels)
81
-
82
  if phase == 'train':
83
  loss.backward()
84
  optimizer.step()
@@ -86,9 +113,11 @@ def train_model(data_dir, model_save_path, num_epochs=10, batch_size=32, learnin
86
  running_loss += loss.item() * inputs.size(0)
87
  running_corrects += torch.sum(preds == labels.data)
88
 
 
 
 
89
  epoch_loss = running_loss / dataset_sizes[phase]
90
  epoch_acc = running_corrects.double() / dataset_sizes[phase]
91
-
92
  print(f'{phase} Loss: {epoch_loss:.4f} Acc: {epoch_acc:.4f}')
93
 
94
  if phase == 'val' and epoch_acc > best_acc:
@@ -97,15 +126,22 @@ def train_model(data_dir, model_save_path, num_epochs=10, batch_size=32, learnin
97
 
98
  print()
99
 
100
- print(f'Best val Acc: {best_acc:4f}')
101
-
102
- # Load best model weights and save
103
  model.load_state_dict(best_model_wts)
104
  os.makedirs(os.path.dirname(model_save_path), exist_ok=True)
105
  torch.save(model.state_dict(), model_save_path)
106
  print(f"Model saved to {model_save_path}")
107
 
 
108
  if __name__ == "__main__":
109
- DATA_DIR = "./data/split"
110
- MODEL_PATH = "./models/spoilage_model.pth"
111
- train_model(DATA_DIR, MODEL_PATH, num_epochs=10)
 
 
 
 
 
 
 
 
 
1
+ import argparse
2
+ import copy
3
+ import os
4
  import torch
5
  import torch.nn as nn
6
  import torch.optim as optim
7
  from torchvision import datasets, models, transforms
8
  from torch.utils.data import DataLoader
 
 
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
+ def build_model(arch):
12
+ if arch == "mobilenet":
13
+ model = models.mobilenet_v2(weights='DEFAULT')
14
+ for param in model.parameters():
15
+ param.requires_grad = False
16
+ num_ftrs = model.classifier[1].in_features
17
+ model.classifier[1] = nn.Linear(num_ftrs, 2)
18
+ elif arch == "swin_t":
19
+ model = models.swin_t(weights='DEFAULT')
20
+ for param in model.parameters():
21
+ param.requires_grad = False
22
+ num_ftrs = model.head.in_features
23
+ model.head = nn.Linear(num_ftrs, 2)
24
+ elif arch == "swin_t_finetune":
25
+ model = models.swin_t(weights='DEFAULT')
26
+ for param in model.parameters():
27
+ param.requires_grad = False
28
+ num_ftrs = model.head.in_features
29
+ model.head = nn.Linear(num_ftrs, 2)
30
+ for param in model.features[7].parameters():
31
+ param.requires_grad = True
32
+ for param in model.norm.parameters():
33
+ param.requires_grad = True
34
+ for param in model.head.parameters():
35
+ param.requires_grad = True
36
+ elif arch == "swin_s":
37
+ model = models.swin_s(weights='DEFAULT')
38
+ for param in model.parameters():
39
+ param.requires_grad = False
40
+ num_ftrs = model.head.in_features
41
+ model.head = nn.Linear(num_ftrs, 2)
42
+ for param in model.features[7].parameters():
43
+ param.requires_grad = True
44
+ for param in model.norm.parameters():
45
+ param.requires_grad = True
46
+ for param in model.head.parameters():
47
+ param.requires_grad = True
48
+ else:
49
+ raise ValueError(f"Unknown arch: {arch}")
50
+ return model
51
+
52
+
53
+ def train(data_dir, model_save_path, arch, num_epochs, batch_size, lr, use_scheduler):
54
+ train_transforms = transforms.Compose([
55
+ transforms.RandomResizedCrop(224),
56
+ transforms.RandomHorizontalFlip(),
57
+ transforms.RandomVerticalFlip(),
58
+ transforms.RandomRotation(30),
59
+ transforms.ColorJitter(brightness=0.3, contrast=0.3, saturation=0.3, hue=0.1),
60
+ transforms.RandomGrayscale(p=0.1),
61
+ transforms.ToTensor(),
62
+ transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
63
+ ])
64
+ val_transforms = transforms.Compose([
65
+ transforms.Resize(256),
66
+ transforms.CenterCrop(224),
67
+ transforms.ToTensor(),
68
+ transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
69
+ ])
70
+
71
+ image_datasets = {
72
+ 'train': datasets.ImageFolder(os.path.join(data_dir, 'train'), train_transforms),
73
+ 'val': datasets.ImageFolder(os.path.join(data_dir, 'val'), val_transforms),
74
+ }
75
  dataloaders = {x: DataLoader(image_datasets[x], batch_size=batch_size, shuffle=True, num_workers=4)
76
  for x in ['train', 'val']}
77
  dataset_sizes = {x: len(image_datasets[x]) for x in ['train', 'val']}
 
78
 
79
  device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
80
  print(f"Using device: {device}")
81
 
82
+ model = build_model(arch).to(device)
83
+ trainable_params = filter(lambda p: p.requires_grad, model.parameters())
 
 
 
 
 
 
 
 
 
 
84
 
85
  criterion = nn.CrossEntropyLoss()
86
+ optimizer = optim.AdamW(trainable_params, lr=lr, weight_decay=0.05)
87
+ scheduler = optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=num_epochs) if use_scheduler else None
88
 
89
  best_model_wts = copy.deepcopy(model.state_dict())
90
  best_acc = 0.0
 
94
  print('-' * 10)
95
 
96
  for phase in ['train', 'val']:
97
+ model.train() if phase == 'train' else model.eval()
 
 
 
 
98
  running_loss = 0.0
99
  running_corrects = 0
100
 
101
  for inputs, labels in dataloaders[phase]:
102
+ inputs, labels = inputs.to(device), labels.to(device)
 
 
103
  optimizer.zero_grad()
104
 
105
  with torch.set_grad_enabled(phase == 'train'):
106
  outputs = model(inputs)
107
  _, preds = torch.max(outputs, 1)
108
  loss = criterion(outputs, labels)
 
109
  if phase == 'train':
110
  loss.backward()
111
  optimizer.step()
 
113
  running_loss += loss.item() * inputs.size(0)
114
  running_corrects += torch.sum(preds == labels.data)
115
 
116
+ if phase == 'train' and scheduler:
117
+ scheduler.step()
118
+
119
  epoch_loss = running_loss / dataset_sizes[phase]
120
  epoch_acc = running_corrects.double() / dataset_sizes[phase]
 
121
  print(f'{phase} Loss: {epoch_loss:.4f} Acc: {epoch_acc:.4f}')
122
 
123
  if phase == 'val' and epoch_acc > best_acc:
 
126
 
127
  print()
128
 
129
+ print(f'Best val Acc: {best_acc:.4f}')
 
 
130
  model.load_state_dict(best_model_wts)
131
  os.makedirs(os.path.dirname(model_save_path), exist_ok=True)
132
  torch.save(model.state_dict(), model_save_path)
133
  print(f"Model saved to {model_save_path}")
134
 
135
+
136
  if __name__ == "__main__":
137
+ parser = argparse.ArgumentParser()
138
+ parser.add_argument("--arch", choices=["mobilenet", "swin_t", "swin_t_finetune", "swin_s"], required=True)
139
+ parser.add_argument("--data-dir", default="./data/split")
140
+ parser.add_argument("--output", required=True, help="Path to save model weights")
141
+ parser.add_argument("--epochs", type=int, default=20)
142
+ parser.add_argument("--batch-size", type=int, default=32)
143
+ parser.add_argument("--lr", type=float, default=0.0001)
144
+ parser.add_argument("--scheduler", action="store_true")
145
+ args = parser.parse_args()
146
+
147
+ train(args.data_dir, args.output, args.arch, args.epochs, args.batch_size, args.lr, args.scheduler)
scripts/train_swin.py DELETED
@@ -1,114 +0,0 @@
1
- import torch
2
- import torch.nn as nn
3
- import torch.optim as optim
4
- from torchvision import datasets, models, transforms
5
- from torch.utils.data import DataLoader
6
- import os
7
- import copy
8
-
9
- def train_model(data_dir, model_save_path, num_epochs=15, batch_size=32, learning_rate=0.0005):
10
- # Data transformations - Swin-T usually expects 224x224
11
- data_transforms = {
12
- 'train': transforms.Compose([
13
- transforms.RandomResizedCrop(224),
14
- transforms.RandomHorizontalFlip(),
15
- transforms.RandomRotation(15),
16
- transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2),
17
- transforms.ToTensor(),
18
- transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
19
- ]),
20
- 'val': transforms.Compose([
21
- transforms.Resize(256),
22
- transforms.CenterCrop(224),
23
- transforms.ToTensor(),
24
- transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
25
- ]),
26
- }
27
-
28
- # Load datasets
29
- image_datasets = {x: datasets.ImageFolder(os.path.join(data_dir, x), data_transforms[x])
30
- for x in ['train', 'val']}
31
- dataloaders = {x: DataLoader(image_datasets[x], batch_size=batch_size, shuffle=True, num_workers=4)
32
- for x in ['train', 'val']}
33
- dataset_sizes = {x: len(image_datasets[x]) for x in ['train', 'val']}
34
-
35
- device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
36
- print(f"Using device: {device}")
37
-
38
- # Load pre-trained Swin Transformer (Tiny)
39
- print("Loading Swin Transformer (Tiny)...")
40
- model = models.swin_t(weights='DEFAULT')
41
-
42
- # Freeze initial layers (Feature extractor)
43
- for param in model.parameters():
44
- param.requires_grad = False
45
-
46
- # Replace the head (classification layer)
47
- # Swin head is usually a Linear layer at model.head
48
- num_ftrs = model.head.in_features
49
- model.head = nn.Linear(num_ftrs, 2) # Binary: Fresh vs Spoiled
50
-
51
- model = model.to(device)
52
-
53
- criterion = nn.CrossEntropyLoss()
54
- # Optimize only the head parameters
55
- optimizer = optim.AdamW(model.head.parameters(), lr=learning_rate, weight_decay=0.05)
56
-
57
- best_model_wts = copy.deepcopy(model.state_dict())
58
- best_acc = 0.0
59
-
60
- for epoch in range(num_epochs):
61
- print(f'Epoch {epoch}/{num_epochs - 1}')
62
- print('-' * 10)
63
-
64
- for phase in ['train', 'val']:
65
- if phase == 'train':
66
- model.train()
67
- else:
68
- model.eval()
69
-
70
- running_loss = 0.0
71
- running_corrects = 0
72
-
73
- for inputs, labels in dataloaders[phase]:
74
- inputs = inputs.to(device)
75
- labels = labels.to(device)
76
-
77
- optimizer.zero_grad()
78
-
79
- with torch.set_grad_enabled(phase == 'train'):
80
- outputs = model(inputs)
81
- _, preds = torch.max(outputs, 1)
82
- loss = criterion(outputs, labels)
83
-
84
- if phase == 'train':
85
- loss.backward()
86
- optimizer.step()
87
-
88
- running_loss += loss.item() * inputs.size(0)
89
- running_corrects += torch.sum(preds == labels.data)
90
-
91
- epoch_loss = running_loss / dataset_sizes[phase]
92
- epoch_acc = running_corrects.double() / dataset_sizes[phase]
93
-
94
- print(f'{phase} Loss: {epoch_loss:.4f} Acc: {epoch_acc:.4f}')
95
-
96
- if phase == 'val' and epoch_acc > best_acc:
97
- best_acc = epoch_acc
98
- best_model_wts = copy.deepcopy(model.state_dict())
99
-
100
- print()
101
-
102
- print(f'Best val Acc: {best_acc:4f}')
103
-
104
- # Load best model weights and save
105
- model.load_state_dict(best_model_wts)
106
- os.makedirs(os.path.dirname(model_save_path), exist_ok=True)
107
- torch.save(model.state_dict(), model_save_path)
108
- print(f"Swin Transformer model saved to {model_save_path}")
109
-
110
- if __name__ == "__main__":
111
- DATA_DIR = "./data/split"
112
- MODEL_PATH = "./models/spoilage_model_swin.pth"
113
- # Increased epochs slightly for transformer convergence
114
- train_model(DATA_DIR, MODEL_PATH, num_epochs=15)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
scripts/train_swin_finetune.py DELETED
@@ -1,132 +0,0 @@
1
- import torch
2
- import torch.nn as nn
3
- import torch.optim as optim
4
- from torchvision import datasets, models, transforms
5
- from torch.utils.data import DataLoader
6
- import os
7
- import copy
8
-
9
- def train_model(data_dir, model_save_path, num_epochs=20, batch_size=32, learning_rate=0.0001):
10
- # Data transformations with more aggressive augmentation
11
- data_transforms = {
12
- 'train': transforms.Compose([
13
- transforms.RandomResizedCrop(224),
14
- transforms.RandomHorizontalFlip(),
15
- transforms.RandomVerticalFlip(),
16
- transforms.RandomRotation(30),
17
- transforms.ColorJitter(brightness=0.3, contrast=0.3, saturation=0.3, hue=0.1),
18
- transforms.RandomGrayscale(p=0.1),
19
- transforms.ToTensor(),
20
- transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
21
- ]),
22
- 'val': transforms.Compose([
23
- transforms.Resize(256),
24
- transforms.CenterCrop(224),
25
- transforms.ToTensor(),
26
- transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
27
- ]),
28
- }
29
-
30
- # Load datasets
31
- image_datasets = {x: datasets.ImageFolder(os.path.join(data_dir, x), data_transforms[x])
32
- for x in ['train', 'val']}
33
- dataloaders = {x: DataLoader(image_datasets[x], batch_size=batch_size, shuffle=True, num_workers=4)
34
- for x in ['train', 'val']}
35
- dataset_sizes = {x: len(image_datasets[x]) for x in ['train', 'val']}
36
-
37
- device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
38
- print(f"Using device: {device}")
39
-
40
- # Load pre-trained Swin Transformer (Tiny)
41
- print("Loading Swin Transformer (Tiny) for fine-tuning...")
42
- model = models.swin_t(weights='DEFAULT')
43
-
44
- # Replace the head
45
- num_ftrs = model.head.in_features
46
- model.head = nn.Linear(num_ftrs, 2)
47
-
48
- # UNFREEZE Strategy:
49
- # We freeze everything initially, then unfreeze the last block (features[7]) and the head.
50
- for param in model.parameters():
51
- param.requires_grad = False
52
-
53
- # Unfreeze the last sequential block and the head
54
- for param in model.features[7].parameters():
55
- param.requires_grad = True
56
- for param in model.norm.parameters():
57
- param.requires_grad = True
58
- for param in model.head.parameters():
59
- param.requires_grad = True
60
-
61
- model = model.to(device)
62
-
63
- criterion = nn.CrossEntropyLoss()
64
-
65
- # Using AdamW with a lower learning rate for fine-tuning
66
- optimizer = optim.AdamW(filter(lambda p: p.requires_grad, model.parameters()),
67
- lr=learning_rate, weight_decay=0.05)
68
-
69
- # Learning rate scheduler: Cosine Annealing
70
- scheduler = optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=num_epochs)
71
-
72
- best_model_wts = copy.deepcopy(model.state_dict())
73
- best_acc = 0.0
74
-
75
- for epoch in range(num_epochs):
76
- print(f'Epoch {epoch}/{num_epochs - 1}')
77
- print('-' * 10)
78
-
79
- for phase in ['train', 'val']:
80
- if phase == 'train':
81
- model.train()
82
- else:
83
- model.eval()
84
-
85
- running_loss = 0.0
86
- running_corrects = 0
87
-
88
- for inputs, labels in dataloaders[phase]:
89
- inputs = inputs.to(device)
90
- labels = labels.to(device)
91
-
92
- optimizer.zero_grad()
93
-
94
- with torch.set_grad_enabled(phase == 'train'):
95
- outputs = model(inputs)
96
- _, preds = torch.max(outputs, 1)
97
- loss = criterion(outputs, labels)
98
-
99
- if phase == 'train':
100
- loss.backward()
101
- optimizer.step()
102
-
103
- running_loss += loss.item() * inputs.size(0)
104
- running_corrects += torch.sum(preds == labels.data)
105
-
106
- if phase == 'train':
107
- scheduler.step()
108
-
109
- epoch_loss = running_loss / dataset_sizes[phase]
110
- epoch_acc = running_corrects.double() / dataset_sizes[phase]
111
-
112
- print(f'{phase} Loss: {epoch_loss:.4f} Acc: {epoch_acc:.4f}')
113
-
114
- if phase == 'val' and epoch_acc > best_acc:
115
- best_acc = epoch_acc
116
- best_model_wts = copy.deepcopy(model.state_dict())
117
-
118
- print()
119
-
120
- print(f'Best val Acc: {best_acc:4f}')
121
-
122
- # Load best model weights and save
123
- model.load_state_dict(best_model_wts)
124
- os.makedirs(os.path.dirname(model_save_path), exist_ok=True)
125
- torch.save(model.state_dict(), model_save_path)
126
- print(f"Fine-tuned Swin model saved to {model_save_path}")
127
-
128
- if __name__ == "__main__":
129
- DATA_DIR = "./data/split"
130
- MODEL_PATH = "./models/spoilage_model_swin_finetuned.pth"
131
- # More epochs for fine-tuning
132
- train_model(DATA_DIR, MODEL_PATH, num_epochs=20)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
scripts/train_swin_small.py DELETED
@@ -1,126 +0,0 @@
1
- import torch
2
- import torch.nn as nn
3
- import torch.optim as optim
4
- from torchvision import datasets, models, transforms
5
- from torch.utils.data import DataLoader
6
- import os
7
- import copy
8
-
9
- def train_model(data_dir, model_save_path, num_epochs=20, batch_size=16, learning_rate=0.00005):
10
- # Data transformations
11
- data_transforms = {
12
- 'train': transforms.Compose([
13
- transforms.RandomResizedCrop(224),
14
- transforms.RandomHorizontalFlip(),
15
- transforms.RandomVerticalFlip(),
16
- transforms.RandomRotation(30),
17
- transforms.ColorJitter(brightness=0.3, contrast=0.3, saturation=0.3, hue=0.1),
18
- transforms.RandomGrayscale(p=0.1),
19
- transforms.ToTensor(),
20
- transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
21
- ]),
22
- 'val': transforms.Compose([
23
- transforms.Resize(256),
24
- transforms.CenterCrop(224),
25
- transforms.ToTensor(),
26
- transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
27
- ]),
28
- }
29
-
30
- image_datasets = {x: datasets.ImageFolder(os.path.join(data_dir, x), data_transforms[x])
31
- for x in ['train', 'val']}
32
- # Smaller batch size for Swin-Small to manage VRAM
33
- dataloaders = {x: DataLoader(image_datasets[x], batch_size=batch_size, shuffle=True, num_workers=4)
34
- for x in ['train', 'val']}
35
- dataset_sizes = {x: len(image_datasets[x]) for x in ['train', 'val']}
36
-
37
- device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
38
- print(f"Using device: {device}")
39
-
40
- print("Loading Swin-Small...")
41
- model = models.swin_s(weights='DEFAULT')
42
-
43
- # Replace the head
44
- num_ftrs = model.head.in_features
45
- model.head = nn.Linear(num_ftrs, 2)
46
-
47
- # Freeze strategy
48
- for param in model.parameters():
49
- param.requires_grad = False
50
-
51
- # Unfreeze the last block and the head
52
- for param in model.features[7].parameters():
53
- param.requires_grad = True
54
- for param in model.norm.parameters():
55
- param.requires_grad = True
56
- for param in model.head.parameters():
57
- param.requires_grad = True
58
-
59
- model = model.to(device)
60
-
61
- criterion = nn.CrossEntropyLoss()
62
- # Lower learning rate for the larger model
63
- optimizer = optim.AdamW(filter(lambda p: p.requires_grad, model.parameters()),
64
- lr=learning_rate, weight_decay=0.05)
65
-
66
- scheduler = optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=num_epochs)
67
-
68
- best_model_wts = copy.deepcopy(model.state_dict())
69
- best_acc = 0.0
70
-
71
- for epoch in range(num_epochs):
72
- print(f'Epoch {epoch}/{num_epochs - 1}')
73
- print('-' * 10)
74
-
75
- for phase in ['train', 'val']:
76
- if phase == 'train':
77
- model.train()
78
- else:
79
- model.eval()
80
-
81
- running_loss = 0.0
82
- running_corrects = 0
83
-
84
- for inputs, labels in dataloaders[phase]:
85
- inputs = inputs.to(device)
86
- labels = labels.to(device)
87
-
88
- optimizer.zero_grad()
89
-
90
- with torch.set_grad_enabled(phase == 'train'):
91
- outputs = model(inputs)
92
- _, preds = torch.max(outputs, 1)
93
- loss = criterion(outputs, labels)
94
-
95
- if phase == 'train':
96
- loss.backward()
97
- optimizer.step()
98
-
99
- running_loss += loss.item() * inputs.size(0)
100
- running_corrects += torch.sum(preds == labels.data)
101
-
102
- if phase == 'train':
103
- scheduler.step()
104
-
105
- epoch_loss = running_loss / dataset_sizes[phase]
106
- epoch_acc = running_corrects.double() / dataset_sizes[phase]
107
-
108
- print(f'{phase} Loss: {epoch_loss:.4f} Acc: {epoch_acc:.4f}')
109
-
110
- if phase == 'val' and epoch_acc > best_acc:
111
- best_acc = epoch_acc
112
- best_model_wts = copy.deepcopy(model.state_dict())
113
-
114
- print()
115
-
116
- print(f'Best val Acc: {best_acc:4f}')
117
-
118
- model.load_state_dict(best_model_wts)
119
- os.makedirs(os.path.dirname(model_save_path), exist_ok=True)
120
- torch.save(model.state_dict(), model_save_path)
121
- print(f"Swin-Small model saved to {model_save_path}")
122
-
123
- if __name__ == "__main__":
124
- DATA_DIR = "./data/split"
125
- MODEL_PATH = "./models/spoilage_model_swin_small.pth"
126
- train_model(DATA_DIR, MODEL_PATH, num_epochs=20)