-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrainModel.py
76 lines (60 loc) · 3.21 KB
/
trainModel.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
from zzdeeprollover.cleanFolders import refreshTrainingDataset, cleanModel, removeIpynbCheckpointsFromTrainingDataset
from zzdeeprollover.createInitialImages import createInitialImages
from zzdeeprollover.createTrainOrTestDataset import createTrainOrTestDataset
from zzdeeprollover.learnModel import learnModel
from zzdeeprollover.addAlternativePathToOriginalVideo import addAlternativePathToOriginalVideo
import subprocess
from subprocess import Popen
import numpy as np
import pandas as pd
import random
import os
localComputer = True
generateInitialImages = True
generateTrainingDataset = True
# Size of image on which DL algorithm will be applied
resizeCropDimension = 34
# Approximate half dimension of validation video and of initial image extracted
imagesToClassifyHalfDiameter = 50
# Number of epoch for DL training
epochsNbTraining = 1 if localComputer else 10
# Window of median rolling mean applied on rollover detected
medianRollingMean = 5
if True:
learningParameters = {}
else:
learningParameters = {'maxCrop': 1, 'brightness_limit': 0.05, 'contrast_limit': 0.05, 'invert_probability': 0, 'sharpness_probability': 0}
showImagesUsedForTraining = False
initialImagesFolder = 'initialImages' if localComputer else 'drive/MyDrive/initialImages'
pathToZZoutput = 'ZZoutputNew' if localComputer else 'drive/MyDrive/ZZoutputNew'
file_path = 'listOfVideosToTakeIntoAccount.txt'
with open(file_path, 'r') as file:
lines = file.readlines()
videos = [line.strip() for line in lines]
# If launched on a computer other than the one used to launch the tracking, the paths to the original raw videos saved in the result file are incorrect: they are thus corrected with the lines below
if not(localComputer):
alternativePathToFolderContainingOriginalVideos = "drive/MyDrive/rawVideos/"
for video in videos:
addAlternativePathToOriginalVideo(pathToZZoutput, video, alternativePathToFolderContainingOriginalVideos)
###
if __name__ == '__main__':
results = pd.DataFrame(np.zeros((len(videos), 6)), index=videos, columns=['normalClassedAsRolloNb', 'totalNbTrueNormal', 'rolloClassedAsRolloNb', 'totalNbTrueRollo', 'normalClassedAsRolloPercent', 'rolloClassedAsRolloNbPercent'])
if generateInitialImages:
for video in videos:
createInitialImages(video, 'rolloverManualClassification.json', pathToZZoutput + '/', imagesToClassifyHalfDiameter, initialImagesFolder)
if generateTrainingDataset:
refreshTrainingDataset()
cleanModel('model')
# Creating learning dataset
if generateTrainingDataset:
trainingVid = videos.copy()
testingVid = trainingVid.pop(random.randint(0, len(videos)-1))
print("Videos used for training:", trainingVid)
print("Videos used for testing:", testingVid)
for vidTrain in trainingVid:
createTrainOrTestDataset(initialImagesFolder + '/', vidTrain, os.path.join('trainingDataset','train'), int(resizeCropDimension/2))
createTrainOrTestDataset(initialImagesFolder + '/', testingVid, os.path.join('trainingDataset','val'), int(resizeCropDimension/2))
print("Training and testing dataset created")
# Transfert learning
removeIpynbCheckpointsFromTrainingDataset()
learnModel(epochsNbTraining, 'model', resizeCropDimension, learningParameters, showImagesUsedForTraining)