kendrickfff commited on
Commit
96a4e6c
·
verified ·
1 Parent(s): 3dad7a1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -174
app.py CHANGED
@@ -54,181 +54,8 @@ 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 = 20
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
- # Mengupdate hasil train dan validate terbaru
193
- history = {
194
- 'train_loss': [
195
- 0.9568, 0.6937, 0.5917, 0.5718, 0.5109,
196
- 0.4824, 0.4697, 0.3318, 0.2785, 0.2680,
197
- 0.2371, 0.2333, 0.2198, 0.2060, 0.1962,
198
- 0.1951, 0.1880, 0.1912, 0.1811, 0.1810
199
- ],
200
- 'train_acc': [
201
- 0.7011, 0.7774, 0.8094, 0.8146, 0.8331,
202
- 0.8452, 0.8447, 0.8899, 0.9068, 0.9114,
203
- 0.9216, 0.9203, 0.9254, 0.9306, 0.9352,
204
- 0.9346, 0.9368, 0.9353, 0.9396, 0.9409
205
- ],
206
- 'val_loss': [
207
- 0.4934, 0.3939, 0.4377, 0.3412, 0.2614,
208
- 0.2966, 0.2439, 0.1065, 0.0926, 0.0797,
209
- 0.0738, 0.0639, 0.0555, 0.0560, 0.0490,
210
- 0.0479, 0.0455, 0.0454, 0.0438, 0.0427
211
- ],
212
- 'val_acc': [
213
- 0.8481, 0.8734, 0.8663, 0.8915, 0.9172,
214
- 0.9011, 0.9221, 0.9649, 0.9714, 0.9759,
215
- 0.9762, 0.9791, 0.9827, 0.9812, 0.9843,
216
- 0.9850, 0.9852, 0.9854, 0.9854, 0.9866
217
- ]
218
- }
219
-
220
- # Simpan history sebagai file pickle
221
- with open('training_history.pkl', 'wb') as f:
222
- pickle.dump(history, f)
223
-
224
- print('Training history saved as training_history.pkl')
225
-
226
-
227
-
228
-
229
- import torch
230
- import torch.nn as nn
231
- from torchvision import models, transforms
232
  from PIL import Image
233
  import gradio as gr
234
 
@@ -305,4 +132,3 @@ iface = gr.Interface(
305
 
306
  iface.launch(share=True)
307
 
308
- token = os.getenv("HUGGINGFACE_TOKEN")
 
54
  print(f"Dataset zip file '{dataset_zip}' not found.")
55
 
56
 
 
 
57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  from PIL import Image
60
  import gradio as gr
61
 
 
132
 
133
  iface.launch(share=True)
134