Update app.py
Browse files
app.py
CHANGED
|
@@ -54,154 +54,7 @@ else:
|
|
| 54 |
print(f"Dataset zip file '{dataset_zip}' not found.")
|
| 55 |
|
| 56 |
|
| 57 |
-
# Path to the data directory
|
| 58 |
-
data_dir = '/home/user/app/data'
|
| 59 |
|
| 60 |
-
# Define data transformations
|
| 61 |
-
data_transforms = {
|
| 62 |
-
'train': transforms.Compose([
|
| 63 |
-
transforms.RandomResizedCrop(224),
|
| 64 |
-
transforms.RandomRotation(15),
|
| 65 |
-
transforms.RandomHorizontalFlip(),
|
| 66 |
-
transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2, hue=0.2),
|
| 67 |
-
transforms.ToTensor(),
|
| 68 |
-
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
|
| 69 |
-
]),
|
| 70 |
-
'valid': transforms.Compose([
|
| 71 |
-
transforms.Resize(256),
|
| 72 |
-
transforms.CenterCrop(224),
|
| 73 |
-
transforms.ToTensor(),
|
| 74 |
-
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
|
| 75 |
-
]),
|
| 76 |
-
}
|
| 77 |
-
|
| 78 |
-
# Create the datasets from the image folder
|
| 79 |
-
image_datasets = {x: datasets.ImageFolder(os.path.join(data_dir, x), data_transforms[x])
|
| 80 |
-
for x in ['train', 'valid']}
|
| 81 |
-
|
| 82 |
-
# Create the dataloaders
|
| 83 |
-
dataloaders = {x: torch.utils.data.DataLoader(image_datasets[x], batch_size=32, shuffle=True, num_workers=4)
|
| 84 |
-
for x in ['train', 'valid']}
|
| 85 |
-
|
| 86 |
-
# Class names
|
| 87 |
-
class_names = image_datasets['train'].classes
|
| 88 |
-
print(f"Classes: {class_names}")
|
| 89 |
-
|
| 90 |
-
# Check if a GPU is available
|
| 91 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 92 |
-
|
| 93 |
-
# Load pre-trained ResNet50 model
|
| 94 |
-
model = models.resnet50(weights='ResNet50_Weights.DEFAULT') # Use weights instead of pretrained
|
| 95 |
-
|
| 96 |
-
# Modify the final layer to match the number of classes
|
| 97 |
-
num_ftrs = model.fc.in_features
|
| 98 |
-
model.fc = nn.Linear(num_ftrs, len(class_names)) # Output classes match
|
| 99 |
-
|
| 100 |
-
# Move the model to the GPU if available
|
| 101 |
-
model = model.to(device)
|
| 102 |
-
|
| 103 |
-
# Loss function and optimizer
|
| 104 |
-
criterion = nn.CrossEntropyLoss()
|
| 105 |
-
optimizer = optim.Adam(model.parameters(), lr=0.001)
|
| 106 |
-
|
| 107 |
-
# Learning rate scheduler
|
| 108 |
-
scheduler = lr_scheduler.StepLR(optimizer, step_size=7, gamma=0.1)
|
| 109 |
-
|
| 110 |
-
# Number of epochs
|
| 111 |
-
num_epochs = 10
|
| 112 |
-
|
| 113 |
-
# Training function with detailed output for each epoch
|
| 114 |
-
def train_model(model, criterion, optimizer, scheduler, num_epochs=10):
|
| 115 |
-
since = time.time()
|
| 116 |
-
|
| 117 |
-
best_model_wts = model.state_dict()
|
| 118 |
-
best_acc = 0.0
|
| 119 |
-
|
| 120 |
-
for epoch in range(num_epochs):
|
| 121 |
-
epoch_start = time.time() # Start time for this epoch
|
| 122 |
-
print(f'Epoch {epoch + 1}/{num_epochs}')
|
| 123 |
-
print('-' * 10)
|
| 124 |
-
|
| 125 |
-
# Each epoch has a training and validation phase
|
| 126 |
-
for phase in ['train', 'valid']:
|
| 127 |
-
if phase == 'train':
|
| 128 |
-
model.train() # Set model to training mode
|
| 129 |
-
else:
|
| 130 |
-
model.eval() # Set model to evaluate mode
|
| 131 |
-
|
| 132 |
-
running_loss = 0.0
|
| 133 |
-
running_corrects = 0
|
| 134 |
-
|
| 135 |
-
# Iterate over data
|
| 136 |
-
for inputs, labels in dataloaders[phase]:
|
| 137 |
-
inputs = inputs.to(device)
|
| 138 |
-
labels = labels.to(device)
|
| 139 |
-
|
| 140 |
-
# Zero the parameter gradients
|
| 141 |
-
optimizer.zero_grad()
|
| 142 |
-
|
| 143 |
-
# Forward
|
| 144 |
-
with torch.set_grad_enabled(phase == 'train'):
|
| 145 |
-
outputs = model(inputs)
|
| 146 |
-
_, preds = torch.max(outputs, 1)
|
| 147 |
-
loss = criterion(outputs, labels)
|
| 148 |
-
|
| 149 |
-
# Backward + optimize only if in training phase
|
| 150 |
-
if phase == 'train':
|
| 151 |
-
loss.backward()
|
| 152 |
-
optimizer.step()
|
| 153 |
-
|
| 154 |
-
# Statistics
|
| 155 |
-
running_loss += loss.item() * inputs.size(0)
|
| 156 |
-
running_corrects += torch.sum(preds == labels.data)
|
| 157 |
-
|
| 158 |
-
if phase == 'train':
|
| 159 |
-
scheduler.step()
|
| 160 |
-
|
| 161 |
-
# Calculate epoch loss and accuracy
|
| 162 |
-
epoch_loss = running_loss / len(image_datasets[phase])
|
| 163 |
-
epoch_acc = running_corrects.double() / len(image_datasets[phase])
|
| 164 |
-
|
| 165 |
-
# Print loss and accuracy for each phase
|
| 166 |
-
print(f'{phase} Loss: {epoch_loss:.4f} Acc: {epoch_acc:.4f}')
|
| 167 |
-
|
| 168 |
-
# Deep copy the model if it's the best accuracy
|
| 169 |
-
if phase == 'valid' and epoch_acc > best_acc:
|
| 170 |
-
best_acc = epoch_acc
|
| 171 |
-
best_model_wts = model.state_dict()
|
| 172 |
-
|
| 173 |
-
epoch_end = time.time() # End time for this epoch
|
| 174 |
-
print(f'Epoch {epoch + 1} completed in {epoch_end - epoch_start:.2f} seconds.')
|
| 175 |
-
|
| 176 |
-
time_elapsed = time.time() - since
|
| 177 |
-
print(f'Training complete in {time_elapsed // 60:.0f}m {time_elapsed % 60:.0f}s')
|
| 178 |
-
print(f'Best val Acc: {best_acc:.4f}')
|
| 179 |
-
|
| 180 |
-
# Load best model weights
|
| 181 |
-
model.load_state_dict(best_model_wts)
|
| 182 |
-
return model
|
| 183 |
-
|
| 184 |
-
# Train the model
|
| 185 |
-
best_model = train_model(model, criterion, optimizer, scheduler, num_epochs=num_epochs)
|
| 186 |
-
|
| 187 |
-
# Save the model
|
| 188 |
-
torch.save(model.state_dict(), 'resnet50_garbage_classification.pth')
|
| 189 |
-
|
| 190 |
-
import pickle
|
| 191 |
-
|
| 192 |
-
# Manually creating the history dictionary based on the logs you provided
|
| 193 |
-
history = {
|
| 194 |
-
'train_loss': [1.0083, 0.7347, 0.6510, 0.5762, 0.5478, 0.5223, 0.4974, 0.3464, 0.2896, 0.2604],
|
| 195 |
-
'train_acc': [0.6850, 0.7687, 0.7913, 0.8126, 0.8210, 0.8272, 0.8355, 0.8870, 0.9049, 0.9136],
|
| 196 |
-
'val_loss': [0.6304, 0.8616, 0.5594, 0.4006, 0.3968, 0.4051, 0.3223, 0.2221, 0.2125, 0.2076],
|
| 197 |
-
'val_acc': [0.7985, 0.7307, 0.8260, 0.8655, 0.8793, 0.8729, 0.9094, 0.9338, 0.9338, 0.9326]
|
| 198 |
-
}
|
| 199 |
-
|
| 200 |
-
# Save the history as a pickle file
|
| 201 |
-
with open('training_history.pkl', 'wb') as f:
|
| 202 |
-
pickle.dump(history, f)
|
| 203 |
-
|
| 204 |
-
print('Training history saved as training_history.pkl')
|
| 205 |
|
| 206 |
|
| 207 |
|
|
|
|
| 54 |
print(f"Dataset zip file '{dataset_zip}' not found.")
|
| 55 |
|
| 56 |
|
|
|
|
|
|
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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