-
Notifications
You must be signed in to change notification settings - Fork 0
/
training.py
executable file
·114 lines (74 loc) · 2.95 KB
/
training.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# -*- coding: utf-8 -*-
"""
Created on Wed May 3 18:13:16 2017
@author: CELIA
"""
import random as random
import numpy as np
import os
def selectClass_InvariantMass(Data, setNumbers, origen):
# It creates a new list with image name and its decay class
# If setNumbers is True it balances the sample.
## MUON EXAMPLE ONLY
numClass = 5
Class = []
Events = Data['Event']
if setNumbers:
#Receives the vector data and the number of classes
counter = np.zeros(numClass)
EventNames = list(Events.keys())
for EventNumber in EventNames:
c = int(Events[EventNumber]['class'])
counter[c]+=1
factor= np.amax(counter)/counter
for n in range(0,len(factor)):
factor[n] = int(factor[n])
for EventNumber in iter(Events):
c = int(Events[EventNumber]['class'])
for times in range(0,int(factor[c])):
ImageName = origen+'event_'+EventNumber+'.jpg'
Class.append([ImageName, Events[EventNumber]['class']])
else:
for EventNumber in iter(Events):
ImageName = origen+'event_'+EventNumber+'.jpg'
Class.append([ImageName, Events[EventNumber]['class']])
random.shuffle(Class)
return Class
def selectClass_KnownClass(Data, ClassName, ClassNumber):
# It adds a label with the class number to the variable data and creates a new variable with the name of the image and its class
# Initiallize the variable
Class = []
# Access the event
Events = Data['Event']
# Loop over the events
for EventNumber in iter(Events):
Events[EventNumber]['class'] = ClassNumber
ImageName = ClassName+'_event_'+EventNumber+'.jpg'
Class.append([ImageName, ClassNumber])
return Class
def setNumbers(Class, num):
# This function clones the elements of a list to have a balanced sample
newClass = Class[:] # creates a new copy of the list
while len(newClass)<num:
for element in Class:
if len(newClass) < num:
newClass.append(element)
return newClass
def writeTrain(Class):
# Write the train.txt file
Dir = 'Training Files/'
My_text=open(Dir+'train.txt','a')
My_text.write(Class[0]+' '+Class[1]+'\n')
My_text.close()
def writeVal(Class):
# Write the val.txt file
Dir = 'Training Files/'
My_text=open(Dir+'val.txt','a')
My_text.write(Class[0]+' '+Class[1]+'\n')
My_text.close()
def writeTest(Class):
# Write the test.txt file
Dir = 'Training Files/Data/'
My_text=open(Dir+'test.txt','a')
My_text.write(Class[0]+' '+Class[1]+'\n')
My_text.close()