luismidv commited on
Commit
e5cbecb
·
1 Parent(s): 297b3eb

Commited plain concepts trial

Browse files
Files changed (4) hide show
  1. data_analysis.py +0 -38
  2. dataprepare.py +4 -15
  3. datasets/.DS_Store +0 -0
  4. train.py +50 -7
data_analysis.py CHANGED
@@ -74,43 +74,10 @@ class DataAnalyst():
74
  except Exception as e:
75
  print(f"Error while trying to resize and save and image {e}")
76
 
77
- def xml_data_extractor(self):
78
- """THIS IS THE FUNCTION THAT WILL EXTRACT LABELS DATA FROM THE XML FILES
79
- THESE XML FILES DESCRIBE THE BOUNDING BOXES AROUND THE CIRCUIT ELEMENTS"""
80
- for idx,file in enumerate(os.listdir(self.annotations_route)):
81
- file_labels = []
82
- file_path = os.path.join(self.annotations_route, file)
83
- try:
84
- with open(file_path, 'r') as f:
85
- file_data = f.read()
86
- beauti_data = BeautifulSoup(file_data, 'lxml-xml')
87
- elements_list = beauti_data.find_all('object')
88
-
89
- for element in elements_list:
90
- name = element.find("name").text
91
- if name =="Capactitor":
92
- name ="Capacitor"
93
- elif name == "DC voltage source":
94
- self.dc_circuits.append(idx)
95
- elif name == "AC voltage source":
96
- self.ac_circuits.append(idx)
97
-
98
- self.elements_names.append(name)
99
- coord_x1 = element.find("xmin").text
100
- coord_x2 = element.find("xmax").text
101
- coord_y1 = element.find("ymin").text
102
- coord_y2 = element.find("ymax").text
103
- file_labels.append([float(coord_x1), float(coord_x2), float(coord_y1), float(coord_y2), float(self.names_dict[name])])
104
- self.training_labels.append(file_labels)
105
- except Exception as e:
106
- print(f"Error while reading xml files and getting their information {e}")
107
-
108
-
109
  def dataset_info_summary(self):
110
  """HERE I AM GOING TO VISUALIZE SOME DATASET INFORMATION, TYPICAL HEIGHT AN WIDTH, CLASS DISTRIBUTION"""
111
  elements_dataframe = pd.DataFrame(self.elements_names)
112
  elements_names =elements_dataframe.value_counts()
113
- print(elements_names)
114
  fig,axes = plt.subplots(1,1)
115
  axes.pie(elements_names, labels= elements_names.index, autopct='%1.2f%%', colors=['gold', 'skyblue', 'lightgreen', 'red', 'green'], startangle=90)
116
  plt.show()
@@ -142,8 +109,6 @@ class DataAnalyst():
142
  f.close()
143
 
144
 
145
-
146
-
147
  def xml_data_getter(self, file_path):
148
  """THIS IS THE FUNCTION THAT WILL EXTRACT LABELS DATA FROM THE XML FILES
149
  THESE XML FILES DESCRIBE THE BOUNDING BOXES AROUND THE CIRCUIT ELEMENTS"""
@@ -175,11 +140,8 @@ class DataAnalyst():
175
  """THIS FUNCTION WILL PREPARE THE COORDINATES THEY WAY YOLO EXPECTS IT
176
  FIRST GET COORDINATES NORMALIZED, SINCE YOLO EXPECTS TO BE BETWEEN 0 AND 1
177
  THIS IS BECAUSE YOLO RESEARCHES REALISED THAT IT WAYS EASIER TO USE OFFSETS OF GRID CELLS RATHER THAN PURE COORDINATES"""
178
- print(bbox_tensor)
179
  normalized_width = 1.0 / image_size[0]
180
  normalized_height = 1.0 / image_size[1]
181
- print(normalized_width)
182
- print(normalized_height)
183
  width = (bbox_tensor[1] - bbox_tensor[0]) * normalized_width
184
  height = (bbox_tensor[3] - bbox_tensor[2]) * normalized_height
185
 
 
74
  except Exception as e:
75
  print(f"Error while trying to resize and save and image {e}")
76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  def dataset_info_summary(self):
78
  """HERE I AM GOING TO VISUALIZE SOME DATASET INFORMATION, TYPICAL HEIGHT AN WIDTH, CLASS DISTRIBUTION"""
79
  elements_dataframe = pd.DataFrame(self.elements_names)
80
  elements_names =elements_dataframe.value_counts()
 
81
  fig,axes = plt.subplots(1,1)
82
  axes.pie(elements_names, labels= elements_names.index, autopct='%1.2f%%', colors=['gold', 'skyblue', 'lightgreen', 'red', 'green'], startangle=90)
83
  plt.show()
 
109
  f.close()
110
 
111
 
 
 
112
  def xml_data_getter(self, file_path):
113
  """THIS IS THE FUNCTION THAT WILL EXTRACT LABELS DATA FROM THE XML FILES
114
  THESE XML FILES DESCRIBE THE BOUNDING BOXES AROUND THE CIRCUIT ELEMENTS"""
 
140
  """THIS FUNCTION WILL PREPARE THE COORDINATES THEY WAY YOLO EXPECTS IT
141
  FIRST GET COORDINATES NORMALIZED, SINCE YOLO EXPECTS TO BE BETWEEN 0 AND 1
142
  THIS IS BECAUSE YOLO RESEARCHES REALISED THAT IT WAYS EASIER TO USE OFFSETS OF GRID CELLS RATHER THAN PURE COORDINATES"""
 
143
  normalized_width = 1.0 / image_size[0]
144
  normalized_height = 1.0 / image_size[1]
 
 
145
  width = (bbox_tensor[1] - bbox_tensor[0]) * normalized_width
146
  height = (bbox_tensor[3] - bbox_tensor[2]) * normalized_height
147
 
dataprepare.py CHANGED
@@ -1,3 +1,7 @@
 
 
 
 
1
  from torch.utils.data import DataLoader, Dataset
2
  from torchvision import transforms
3
  from data_analysis import DataAnalyst
@@ -73,21 +77,6 @@ def data_asociation(mode):
73
  shutil.copy(new_file, labels)
74
 
75
 
76
-
77
-
78
-
79
-
80
- def analyst_starting(route, output_folder, annotations_route):
81
- #THIS FUNCTION IS GOING TO BE CALLED BY THE MAIN IN ORDER TO START THE JOB
82
- data_analyst = DataAnalyst(route, output_folder, annotations_route)
83
- #data_analyst.image_size_searching()
84
- #data_analyst.xml_data_extractor()
85
- #data_analyst.dataset_info_summary()
86
- #dataset = CircuitDataset(data_analyst.training_features, data_analyst.training_labels, data_analyst.route,(384,384))
87
- #training_loader = DataLoader(dataset, batch_size=2, shuffle=True)
88
- #for x,y in training_loader:
89
- # print("Imprimo imagen")
90
-
91
  def analyst_starting2(route,output_folder,annotations_route):
92
  data_analyst = DataAnalyst(route, output_folder, annotations_route)
93
 
 
1
+ """THIS FILE IS PART OF THE FIRST APPROXIMATION THAT I DID TO THE PROBLEM USING DATASET CLASS AND ITERATING OVER IT USING A DATALOADER
2
+ BUT IT IS NOT USED IN THE FINAL SOLUTION AFTER REALISING THAT IT WOULDN'T WORK IN ORDER TO FINE TUNE YOLOV8 FROM ULTRALYTICS
3
+ I KEEP IT HERE SO YOU CAN SEE HOW I USE THIS CLASSES"""
4
+
5
  from torch.utils.data import DataLoader, Dataset
6
  from torchvision import transforms
7
  from data_analysis import DataAnalyst
 
77
  shutil.copy(new_file, labels)
78
 
79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  def analyst_starting2(route,output_folder,annotations_route):
81
  data_analyst = DataAnalyst(route, output_folder, annotations_route)
82
 
datasets/.DS_Store CHANGED
Binary files a/datasets/.DS_Store and b/datasets/.DS_Store differ
 
train.py CHANGED
@@ -1,22 +1,65 @@
1
  from ultralytics import YOLO, settings
2
  import ultralytics
3
- from pathlib import Path
4
- import os
5
  import torch
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  def model_training():
 
8
  try:
9
- #ultralytics.settings.update({'datasets_dir': "./dataset"})
10
- #save_path = '/tmp/runs/detect/train2'
11
- #Path(save_path).mkdir(parents=True, exist_ok=True)
12
  device = "cuda" if torch.cuda.is_available() else "cpu"
13
  print("Available device: ", device)
14
  model = YOLO("yolov8n.pt")
15
- model.train(data="datasets/dataset.yaml", epochs=20, imgsz=244, batch = 2, device=device,verbose=True)
16
 
 
17
  except Exception as e:
18
- print(f"Error when training {e}")
 
 
 
 
 
 
 
 
 
 
19
 
 
 
 
 
 
 
 
 
20
 
21
 
22
 
 
1
  from ultralytics import YOLO, settings
2
  import ultralytics
 
 
3
  import torch
4
+ from sys import ps1
5
+ #DATA AUGMENTATION SECTION
6
+ from ultralytics.data.augment import Albumentations
7
+ from ultralytics.utils import LOGGER,colorstr
8
+ import albumentations as A
9
+ import cv2
10
+
11
+ def create_transformations(p=1.0):
12
+ """THIS IS WHERE I CREATED THE FIRST TRASNFORMATIONS OVER THE IMAGE, FINALLY I AM JUST USING THE ULTRALYTICS DEFAULT ONE"""
13
+ prefix = colorstr("albumentations: ")
14
+ try:
15
+ transforms = [
16
+ A.RandomRain(p=0.1, slant_lower=-10, slant_upper=10,
17
+ drop_length=20, drop_width=1, drop_color=(200, 200, 200),
18
+ blur_value=5, brightness_coefficient=0.9, rain_type="default"),
19
+ A.Rotate(limit = 10, p=0.5),
20
+ A.Blur(p=0.1),
21
+ A.MedianBlur(p=0.1),
22
+ A.ToGray(p=0.01),
23
+ A.CLAHE(p=0.01),
24
+ A.ImageCompression(quality_lower=75, p=0.0),
25
+ ]
26
+ transform = A.Compose(transforms, bbox_params=A.BboxParams(format="yolo", label_fields=["class_labels"]))
27
+ return transform
28
+
29
+ except Exception as e:
30
+ print(f"Error while creating transformations {e}")
31
 
32
  def model_training():
33
+ """FUNCTION USED TO TRAIN THE MODEL"""
34
  try:
35
+ transformations = create_transformations()
 
 
36
  device = "cuda" if torch.cuda.is_available() else "cpu"
37
  print("Available device: ", device)
38
  model = YOLO("yolov8n.pt")
39
+ results = model.train(data="/content/datasets/dataset.yaml", epochs=50, imgsz=384, batch = 2, augment = True,device=device,verbose=True)
40
 
41
+ return model
42
  except Exception as e:
43
+ print(e)
44
+
45
+ def image_show_fn(bboxes,path):
46
+ """THIS IS THE FUNCTION USING BY model_testing FUNCTION TO DRAW BBOXES"""
47
+ image = cv2.imread(path)
48
+ for bbox in bboxes:
49
+ bbox = [int(num) for num in bbox]
50
+ cv2.rectangle(image, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (0, 255, 0), 2)
51
+ cv2.imshow(image)
52
+ #THIS METHOD IS ONLY CALLABLE IN GOOGLE COLAB TYPES
53
+ #cv2_imshow(image)
54
 
55
+ def model_testing(model, test_image):
56
+ """THIS IS THE FUNCTION THAT THE MODEL WILL USE AT INFERENCE TIME
57
+ IT LOADS THE BEST WEIGHTS, DO THE INFERENCE ON THE IMAGE, CALL THE
58
+ FUNCTION THAT WILL DRAW THE BOUNDING BOXES AND FINALLY DRAW BBOXES """
59
+ model = YOLO("/content/runs/detect/train12/weights/best.pt")
60
+ image_results = model(test_image)
61
+ for image in image_results:
62
+ image_show_fn(image.boxes.xyxy,image.path)
63
 
64
 
65