Princess3 commited on
Commit
2ab54e6
1 Parent(s): 4f30fe1

Update model.py

Browse files
Files changed (1) hide show
  1. model.py +27 -7
model.py CHANGED
@@ -3,7 +3,7 @@ import xml.etree.ElementTree as ET
3
  import torch
4
  import torch.nn as nn
5
  import torch.nn.functional as F
6
- from typing import List, Dict, Any
7
  from collections import defaultdict
8
  from accelerate import Accelerator
9
 
@@ -12,6 +12,16 @@ class DynamicModel(nn.Module):
12
  super(DynamicModel, self).__init__()
13
  self.sections = nn.ModuleDict()
14
 
 
 
 
 
 
 
 
 
 
 
15
  for section_name, layers in sections.items():
16
  self.sections[section_name] = nn.ModuleList()
17
  for layer_params in layers:
@@ -42,9 +52,9 @@ def parse_xml_file(file_path: str) -> List[Dict[str, Any]]:
42
  layers = []
43
  for prov in root.findall('.//prov'):
44
  layer_params = {
45
- 'input_size': 128, # Example: fixed input size
46
- 'output_size': 256, # Example: fixed output size
47
- 'activation': 'relu' # Default activation
48
  }
49
  layers.append(layer_params)
50
 
@@ -53,9 +63,15 @@ def parse_xml_file(file_path: str) -> List[Dict[str, Any]]:
53
  def create_model_from_folder(folder_path: str) -> DynamicModel:
54
  sections = defaultdict(list)
55
 
 
 
 
 
 
56
  for root, dirs, files in os.walk(folder_path):
57
  for file in files:
58
  if file.endswith('.xml'):
 
59
  file_path = os.path.join(root, file)
60
  try:
61
  layers = parse_xml_file(file_path)
@@ -64,16 +80,20 @@ def create_model_from_folder(folder_path: str) -> DynamicModel:
64
  except Exception as e:
65
  print(f"Error processing {file_path}: {str(e)}")
66
 
67
- return DynamicModel(sections)
 
 
 
 
68
 
69
  def main():
70
- folder_path = 'app/data'
71
  model = create_model_from_folder(folder_path)
72
 
73
  print(f"Created dynamic PyTorch model with sections: {list(model.sections.keys())}")
74
 
75
  # Get first section's first layer's input size dynamically
76
- first_section = list(model.sections.keys())[0]
77
  first_layer = model.sections[first_section][0]
78
  input_features = first_layer[0].in_features
79
 
 
3
  import torch
4
  import torch.nn as nn
5
  import torch.nn.functional as F
6
+ from typing import List, Dict, Any, Optional
7
  from collections import defaultdict
8
  from accelerate import Accelerator
9
 
 
12
  super(DynamicModel, self).__init__()
13
  self.sections = nn.ModuleDict()
14
 
15
+ # Default section if none provided
16
+ if not sections:
17
+ sections = {
18
+ 'default': [{
19
+ 'input_size': 128,
20
+ 'output_size': 256,
21
+ 'activation': 'relu'
22
+ }]
23
+ }
24
+
25
  for section_name, layers in sections.items():
26
  self.sections[section_name] = nn.ModuleList()
27
  for layer_params in layers:
 
52
  layers = []
53
  for prov in root.findall('.//prov'):
54
  layer_params = {
55
+ 'input_size': 128,
56
+ 'output_size': 256,
57
+ 'activation': 'relu'
58
  }
59
  layers.append(layer_params)
60
 
 
63
  def create_model_from_folder(folder_path: str) -> DynamicModel:
64
  sections = defaultdict(list)
65
 
66
+ if not os.path.exists(folder_path):
67
+ print(f"Warning: Folder {folder_path} does not exist. Creating model with default configuration.")
68
+ return DynamicModel({})
69
+
70
+ xml_files_found = False
71
  for root, dirs, files in os.walk(folder_path):
72
  for file in files:
73
  if file.endswith('.xml'):
74
+ xml_files_found = True
75
  file_path = os.path.join(root, file)
76
  try:
77
  layers = parse_xml_file(file_path)
 
80
  except Exception as e:
81
  print(f"Error processing {file_path}: {str(e)}")
82
 
83
+ if not xml_files_found:
84
+ print("Warning: No XML files found. Creating model with default configuration.")
85
+ return DynamicModel({})
86
+
87
+ return DynamicModel(dict(sections))
88
 
89
  def main():
90
+ folder_path = 'Xml_Data'
91
  model = create_model_from_folder(folder_path)
92
 
93
  print(f"Created dynamic PyTorch model with sections: {list(model.sections.keys())}")
94
 
95
  # Get first section's first layer's input size dynamically
96
+ first_section = next(iter(model.sections.keys()))
97
  first_layer = model.sections[first_section][0]
98
  input_features = first_layer[0].in_features
99