Spaces:
Runtime error
Runtime error
Commit
·
0a9b70a
1
Parent(s):
b371f72
Rename Train.py to app.py
Browse files
Train.py
DELETED
@@ -1,370 +0,0 @@
|
|
1 |
-
#!/usr/bin/env python
|
2 |
-
# coding: utf-8
|
3 |
-
|
4 |
-
# In[1]:
|
5 |
-
|
6 |
-
|
7 |
-
import pandas as pd
|
8 |
-
import joblib
|
9 |
-
import os
|
10 |
-
import numpy as np
|
11 |
-
from tqdm import tqdm
|
12 |
-
from sklearn.preprocessing import LabelBinarizer
|
13 |
-
|
14 |
-
|
15 |
-
# In[2]:
|
16 |
-
|
17 |
-
|
18 |
-
# get all the image folder paths
|
19 |
-
all_paths = os.listdir(r'C:/Users/abdul/Desktop/Research/work/mhamad syrian/ziad/data')
|
20 |
-
folder_paths = [x for x in all_paths if os.path.isdir('C:/Users/abdul/Desktop/Research/work/mhamad syrian/ziad/data/' + x)]
|
21 |
-
print(f"Folder paths: {folder_paths}")
|
22 |
-
print(f"Number of folders: {len(folder_paths)}")
|
23 |
-
|
24 |
-
|
25 |
-
# In[3]:
|
26 |
-
|
27 |
-
|
28 |
-
# we will create the data for the following labels,
|
29 |
-
# add more to list to use those for creating the data as well
|
30 |
-
create_labels = ['bend', 'jack', 'jump', 'pjump', 'walk', 'wave1', 'wave2']
|
31 |
-
# create a DataFrame
|
32 |
-
data = pd.DataFrame()
|
33 |
-
|
34 |
-
|
35 |
-
# In[4]:
|
36 |
-
|
37 |
-
|
38 |
-
image_formats = ['jpg', 'JPG', 'PNG', 'png'] # we only want images that are in this format
|
39 |
-
labels = []
|
40 |
-
counter = 0
|
41 |
-
for i, folder_path in tqdm(enumerate(folder_paths), total=len(folder_paths)):
|
42 |
-
if folder_path not in create_labels:
|
43 |
-
continue
|
44 |
-
image_paths = os.listdir('C:/Users/abdul/Desktop/Research/work/mhamad syrian/ziad/data/'+folder_path)
|
45 |
-
label = folder_path
|
46 |
-
# save image paths in the DataFrame
|
47 |
-
for image_path in image_paths:
|
48 |
-
if image_path.split('.')[-1] in image_formats:
|
49 |
-
data.loc[counter, 'image_path'] = f"C:/Users/abdul/Desktop/Research/work/mhamad syrian/ziad/data/{folder_path}/{image_path}"
|
50 |
-
labels.append(label)
|
51 |
-
counter += 1
|
52 |
-
|
53 |
-
|
54 |
-
# In[5]:
|
55 |
-
|
56 |
-
|
57 |
-
labels = np.array(labels)
|
58 |
-
# one-hot encode the labels
|
59 |
-
lb = LabelBinarizer()
|
60 |
-
labels = lb.fit_transform(labels)
|
61 |
-
|
62 |
-
|
63 |
-
# In[6]:
|
64 |
-
|
65 |
-
|
66 |
-
if len(labels[0]) == 1:
|
67 |
-
for i in range(len(labels)):
|
68 |
-
index = labels[i]
|
69 |
-
data.loc[i, 'target'] = int(index)
|
70 |
-
elif len(labels[0]) > 1:
|
71 |
-
for i in range(len(labels)):
|
72 |
-
index = np.argmax(labels[i])
|
73 |
-
data.loc[i, 'target'] = int(index)
|
74 |
-
|
75 |
-
|
76 |
-
# In[7]:
|
77 |
-
|
78 |
-
|
79 |
-
# shuffle the dataset
|
80 |
-
data = data.sample(frac=1).reset_index(drop=True)
|
81 |
-
print(f"Number of labels or classes: {len(lb.classes_)}")
|
82 |
-
print(f"The first one hot encoded labels: {labels[0]}")
|
83 |
-
print(f"Mapping the first one hot encoded label to its category: {lb.classes_[0]}")
|
84 |
-
print(f"Total instances: {len(data)}")
|
85 |
-
|
86 |
-
# save as CSV file
|
87 |
-
data.to_csv('C:/Users/abdul/Desktop/Research/work/mhamad syrian/ziad/data.csv', index=False)
|
88 |
-
|
89 |
-
# pickle the binarized labels
|
90 |
-
print('Saving the binarized labels as pickled file')
|
91 |
-
joblib.dump(lb, 'C:/Users/abdul/Desktop/Research/work/mhamad syrian/ziad/lb.pkl')
|
92 |
-
|
93 |
-
print(data.head(5))
|
94 |
-
|
95 |
-
|
96 |
-
# In[8]:
|
97 |
-
|
98 |
-
|
99 |
-
import torch
|
100 |
-
import torch.nn as nn
|
101 |
-
import torch.nn.functional as F
|
102 |
-
import joblib
|
103 |
-
# load the binarized labels file
|
104 |
-
lb = joblib.load('C:/Users/abdul/Desktop/Research/work/mhamad syrian/ziad/lb.pkl')
|
105 |
-
class CustomCNN(nn.Module):
|
106 |
-
def __init__(self):
|
107 |
-
super(CustomCNN, self).__init__()
|
108 |
-
self.conv1 = nn.Conv2d(3, 16, 5)
|
109 |
-
self.conv2 = nn.Conv2d(16, 32, 5)
|
110 |
-
self.conv3 = nn.Conv2d(32, 64, 3)
|
111 |
-
self.conv4 = nn.Conv2d(64, 128, 5)
|
112 |
-
self.fc1 = nn.Linear(128, 256)
|
113 |
-
self.fc2 = nn.Linear(256, len(lb.classes_))
|
114 |
-
self.pool = nn.MaxPool2d(2, 2)
|
115 |
-
def forward(self, x):
|
116 |
-
x = self.pool(F.relu(self.conv1(x)))
|
117 |
-
x = self.pool(F.relu(self.conv2(x)))
|
118 |
-
x = self.pool(F.relu(self.conv3(x)))
|
119 |
-
x = self.pool(F.relu(self.conv4(x)))
|
120 |
-
bs, _, _, _ = x.shape
|
121 |
-
x = F.adaptive_avg_pool2d(x, 1).reshape(bs, -1)
|
122 |
-
x = F.relu(self.fc1(x))
|
123 |
-
x = self.fc2(x)
|
124 |
-
return x
|
125 |
-
|
126 |
-
|
127 |
-
# In[9]:
|
128 |
-
|
129 |
-
|
130 |
-
import torch
|
131 |
-
import argparse
|
132 |
-
import torch.nn as nn
|
133 |
-
import torch.nn.functional as F
|
134 |
-
import numpy as np
|
135 |
-
import joblib
|
136 |
-
import albumentations
|
137 |
-
import torch.optim as optim
|
138 |
-
import os
|
139 |
-
# import cnn_models
|
140 |
-
import matplotlib
|
141 |
-
import matplotlib.pyplot as plt
|
142 |
-
import time
|
143 |
-
import pandas as pd
|
144 |
-
matplotlib.style.use('ggplot')
|
145 |
-
from imutils import paths
|
146 |
-
from sklearn.preprocessing import LabelBinarizer
|
147 |
-
from sklearn.model_selection import train_test_split
|
148 |
-
from torch.utils.data import DataLoader, Dataset
|
149 |
-
from tqdm import tqdm
|
150 |
-
from PIL import Image
|
151 |
-
|
152 |
-
|
153 |
-
# In[31]:
|
154 |
-
|
155 |
-
|
156 |
-
# learning_parameters
|
157 |
-
lr = 1e-3
|
158 |
-
epochs=100
|
159 |
-
batch_size = 64
|
160 |
-
device = 'cuda:0'
|
161 |
-
print(f"Computation device: {device}\n")
|
162 |
-
|
163 |
-
|
164 |
-
# In[ ]:
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
# In[32]:
|
171 |
-
|
172 |
-
|
173 |
-
# read the data.csv file and get the image paths and labels
|
174 |
-
df = pd.read_csv('C:/Users/abdul/Desktop/Research/work/mhamad syrian/ziad/data.csv')
|
175 |
-
X = df.image_path.values # image paths
|
176 |
-
y = df.target.values # targets
|
177 |
-
(xtrain, xtest, ytrain, ytest) = train_test_split(X, y,
|
178 |
-
test_size=0.10, random_state=42)
|
179 |
-
print(f"Training instances: {len(xtrain)}")
|
180 |
-
print(f"Validation instances: {len(xtest)}")
|
181 |
-
|
182 |
-
|
183 |
-
# In[33]:
|
184 |
-
|
185 |
-
|
186 |
-
# custom dataset
|
187 |
-
class ImageDataset(Dataset):
|
188 |
-
def __init__(self, images, labels=None, tfms=None):
|
189 |
-
self.X = images
|
190 |
-
self.y = labels
|
191 |
-
# apply augmentations
|
192 |
-
if tfms == 0: # if validating
|
193 |
-
self.aug = albumentations.Compose([
|
194 |
-
albumentations.Resize(224, 224, always_apply=True),
|
195 |
-
])
|
196 |
-
else: # if training
|
197 |
-
self.aug = albumentations.Compose([
|
198 |
-
albumentations.Resize(224, 224, always_apply=True),
|
199 |
-
albumentations.HorizontalFlip(p=0.5),
|
200 |
-
albumentations.ShiftScaleRotate(
|
201 |
-
shift_limit=0.3,
|
202 |
-
scale_limit=0.3,
|
203 |
-
rotate_limit=15,
|
204 |
-
p=0.5
|
205 |
-
),
|
206 |
-
])
|
207 |
-
|
208 |
-
def __len__(self):
|
209 |
-
return (len(self.X))
|
210 |
-
|
211 |
-
def __getitem__(self, i):
|
212 |
-
image = Image.open(self.X[i])
|
213 |
-
image = image.convert('RGB')
|
214 |
-
image = self.aug(image=np.array(image))['image']
|
215 |
-
image = np.transpose(image, (2, 0, 1)).astype(np.float32)
|
216 |
-
label = self.y[i]
|
217 |
-
return (torch.tensor(image, dtype=torch.float), torch.tensor(label, dtype=torch.long))
|
218 |
-
|
219 |
-
|
220 |
-
# In[34]:
|
221 |
-
|
222 |
-
|
223 |
-
train_data = ImageDataset(xtrain, ytrain, tfms=1)
|
224 |
-
test_data = ImageDataset(xtest, ytest, tfms=0)
|
225 |
-
# dataloaders
|
226 |
-
trainloader = DataLoader(train_data, batch_size=batch_size, shuffle=True)
|
227 |
-
testloader = DataLoader(test_data, batch_size=batch_size, shuffle=False)
|
228 |
-
|
229 |
-
|
230 |
-
# In[35]:
|
231 |
-
|
232 |
-
|
233 |
-
model = CustomCNN().to(device)
|
234 |
-
print(model)
|
235 |
-
# total parameters and trainable parameters
|
236 |
-
total_params = sum(p.numel() for p in model.parameters())
|
237 |
-
print(f"{total_params:,} total parameters.")
|
238 |
-
total_trainable_params = sum(
|
239 |
-
p.numel() for p in model.parameters() if p.requires_grad)
|
240 |
-
print(f"{total_trainable_params:,} training parameters.")
|
241 |
-
|
242 |
-
|
243 |
-
# In[36]:
|
244 |
-
|
245 |
-
|
246 |
-
# optimizer
|
247 |
-
optimizer = optim.Adam(model.parameters(), lr=lr)
|
248 |
-
# loss function
|
249 |
-
criterion = nn.CrossEntropyLoss()
|
250 |
-
|
251 |
-
|
252 |
-
# In[37]:
|
253 |
-
|
254 |
-
|
255 |
-
scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(
|
256 |
-
optimizer,
|
257 |
-
mode='min',
|
258 |
-
patience=5,
|
259 |
-
factor=0.5,
|
260 |
-
min_lr=1e-6,
|
261 |
-
verbose=True
|
262 |
-
)
|
263 |
-
|
264 |
-
|
265 |
-
# In[38]:
|
266 |
-
|
267 |
-
|
268 |
-
# training function
|
269 |
-
def fit(model, train_dataloader):
|
270 |
-
print('Training')
|
271 |
-
model.train()
|
272 |
-
train_running_loss = 0.0
|
273 |
-
train_running_correct = 0
|
274 |
-
for i, data in tqdm(enumerate(train_dataloader), total=int(len(train_data)/train_dataloader.batch_size)):
|
275 |
-
data, target = data[0].to(device), data[1].to(device)
|
276 |
-
optimizer.zero_grad()
|
277 |
-
outputs = model(data)
|
278 |
-
loss = criterion(outputs, target)
|
279 |
-
train_running_loss += loss.item()
|
280 |
-
_, preds = torch.max(outputs.data, 1)
|
281 |
-
train_running_correct += (preds == target).sum().item()
|
282 |
-
loss.backward()
|
283 |
-
optimizer.step()
|
284 |
-
|
285 |
-
train_loss = train_running_loss/len(train_dataloader.dataset)
|
286 |
-
train_accuracy = 100. * train_running_correct/len(train_dataloader.dataset)
|
287 |
-
|
288 |
-
print(f"Train Loss: {train_loss:.4f}, Train Acc: {train_accuracy:.2f}")
|
289 |
-
|
290 |
-
return train_loss, train_accuracy
|
291 |
-
|
292 |
-
|
293 |
-
# In[39]:
|
294 |
-
|
295 |
-
|
296 |
-
#validation function
|
297 |
-
def validate(model, test_dataloader):
|
298 |
-
print('Validating')
|
299 |
-
model.eval()
|
300 |
-
val_running_loss = 0.0
|
301 |
-
val_running_correct = 0
|
302 |
-
with torch.no_grad():
|
303 |
-
for i, data in tqdm(enumerate(test_dataloader), total=int(len(test_data)/test_dataloader.batch_size)):
|
304 |
-
data, target = data[0].to(device), data[1].to(device)
|
305 |
-
outputs = model(data)
|
306 |
-
loss = criterion(outputs, target)
|
307 |
-
|
308 |
-
val_running_loss += loss.item()
|
309 |
-
_, preds = torch.max(outputs.data, 1)
|
310 |
-
val_running_correct += (preds == target).sum().item()
|
311 |
-
|
312 |
-
val_loss = val_running_loss/len(test_dataloader.dataset)
|
313 |
-
val_accuracy = 100. * val_running_correct/len(test_dataloader.dataset)
|
314 |
-
print(f'Val Loss: {val_loss:.4f}, Val Acc: {val_accuracy:.2f}')
|
315 |
-
|
316 |
-
return val_loss, val_accuracy
|
317 |
-
|
318 |
-
|
319 |
-
# In[40]:
|
320 |
-
|
321 |
-
|
322 |
-
train_loss , train_accuracy = [], []
|
323 |
-
val_loss , val_accuracy = [], []
|
324 |
-
start = time.time()
|
325 |
-
for epoch in range(epochs):
|
326 |
-
print(f"Epoch {epoch+1} of {epochs}")
|
327 |
-
train_epoch_loss, train_epoch_accuracy = fit(model, trainloader)
|
328 |
-
val_epoch_loss, val_epoch_accuracy = validate(model, testloader)
|
329 |
-
train_loss.append(train_epoch_loss)
|
330 |
-
train_accuracy.append(train_epoch_accuracy)
|
331 |
-
val_loss.append(val_epoch_loss)
|
332 |
-
val_accuracy.append(val_epoch_accuracy)
|
333 |
-
scheduler.step(val_epoch_loss)
|
334 |
-
end = time.time()
|
335 |
-
print(f"{(end-start)/60:.3f} minutes")
|
336 |
-
|
337 |
-
|
338 |
-
# In[42]:
|
339 |
-
|
340 |
-
|
341 |
-
# accuracy plots
|
342 |
-
plt.figure(figsize=(10, 7))
|
343 |
-
plt.plot(train_accuracy, color='green', label='train accuracy')
|
344 |
-
plt.plot(val_accuracy, color='blue', label='validataion accuracy')
|
345 |
-
plt.xlabel('Epochs')
|
346 |
-
plt.ylabel('Accuracy')
|
347 |
-
plt.legend()
|
348 |
-
plt.savefig(r'C:\Users\abdul\Desktop\Research\work\mhamad syrian\ziad\accuracy.png')
|
349 |
-
plt.show()
|
350 |
-
# loss plots
|
351 |
-
plt.figure(figsize=(10, 7))
|
352 |
-
plt.plot(train_loss, color='orange', label='train loss')
|
353 |
-
plt.plot(val_loss, color='red', label='validataion loss')
|
354 |
-
plt.xlabel('Epochs')
|
355 |
-
plt.ylabel('Loss')
|
356 |
-
plt.legend()
|
357 |
-
plt.savefig(r'C:\Users\abdul\Desktop\Research\work\mhamad syrian\ziad\loss.png')
|
358 |
-
plt.show()
|
359 |
-
|
360 |
-
# serialize the model to disk
|
361 |
-
print('Saving model...')
|
362 |
-
torch.save(model.state_dict(), 'model')
|
363 |
-
|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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.py
ADDED
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import math
|
2 |
+
import numpy as np
|
3 |
+
import tensorflow as tf
|
4 |
+
from tensorflow import keras
|
5 |
+
import tensorflow_addons as tfa
|
6 |
+
import matplotlib.pyplot as plt
|
7 |
+
from tensorflow.keras import layers
|
8 |
+
|
9 |
+
|
10 |
+
model = load_model('modelCerv.h5')
|
11 |
+
|
12 |
+
response = requests.get("https://github.com/abdulkader902017/CervixNet/blob/main/labels.txt")
|
13 |
+
labels = response.text.split("\n")
|
14 |
+
|
15 |
+
def classify_image(inp):
|
16 |
+
inp = inp.reshape((-1, 32, 32, 3))
|
17 |
+
inp = tf.keras.applications.mobilenet_v2.preprocess_input(inp)
|
18 |
+
prediction = inception_net.predict(inp).flatten()
|
19 |
+
confidences = {labels[i]: float(prediction[i]) for i in range(3)}
|
20 |
+
return confidences
|
21 |
+
|
22 |
+
gr.Interface(fn=classify_image,
|
23 |
+
inputs=gr.Image(shape=(32, 32)),
|
24 |
+
outputs=gr.Label(num_top_classes=3)).launch()
|