-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_train.py
More file actions
88 lines (70 loc) · 3.09 KB
/
Copy pathmain_train.py
File metadata and controls
88 lines (70 loc) · 3.09 KB
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
import os
import cv2
import pandas as pd
import cupy as cp
from tqdm import tqdm
from src.pyESNN.pyESNcupy import ESN
from src.utils.path import *
from src.main_helper import draw_bounding_boxes
def main():
# Define the Echo State Network (ESN) parameters
N_INPUTS = 30000 # Number of input dimensions (in this case, grayscale pixel values)
N_OUTPUTS = 2 # Number of output dimensions (x and y coordinates)
N_RESERVOIR = 700 # Number of reservoir neurons
SPECTRAL_RADIUS = 0.99 # Spectral radius of the reservoir weight matrix
INPUT_SCALING = 0.1 # Scaling of the input weights
# Create the Echo State Network (ESN)
esn = ESN(n_inputs=N_INPUTS,
n_outputs=N_OUTPUTS,
n_reservoir=N_RESERVOIR,
spectral_radius=SPECTRAL_RADIUS,
input_scaling=INPUT_SCALING,
teacher_forcing=True,
silent=False)
# Define paths to the videos
csv_directory = PREPROCESSED_BB_COORDINATES_DIR
video_directory = ORIGINAL_VIDEOS_DIR
video_files = os.listdir(video_directory)
# Read and process each video
for video_file in tqdm(video_files, desc="Processing videos ..."):
video_path = os.path.join(video_directory, video_file)
cap = cv2.VideoCapture(video_path)
# Read target outputs from corresponding CSV file
csv_file = os.path.splitext(video_file)[0] + '.csv'
csv_path = os.path.join(csv_directory, csv_file)
target_outputs_df = pd.read_csv(csv_path)
target_outputs = target_outputs_df[['X-coordinate', 'Y-coordinate']].values.astype(float)
# Read the video frame by frame
frames = []
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
frames.append(cv2.resize(frame, (100, 100)))
frames = cp.array(frames).reshape(len(frames), -1)
# Close the video capture
cap.release()
#print('FRAME ..............', frames.shape)
#print('Y TRAIN ....................', target_outputs.shape)
# Train the ESN
print('Training the ESN ...')
print(frames.shape, target_outputs.shape)
esn.fit(frames, target_outputs, inspect=True)
# Extract frames and resize them to a smaller size for simplicity
cap = cv2.VideoCapture(os.path.join(ORIGINAL_VAL_VIDEOS_DIR, 'moving_circle_3.mp4'))
frames = []
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
frames.append(cv2.resize(frame, (100, 100)))
frames = cp.array(frames).reshape(len(frames), -1)
predictions = esn.predict(frames)
# Draw bounding boxes on the video
output_video_path = os.path.join(PREDICTED_VIDEOS_DIR, 'moving_circle_val_2.mp4')
draw_bounding_boxes(video_directory, predictions, output_video_path)
# Save the trained model
model_path = os.path.join(MODELS, 'model_2.pkl')
esn.save(model_path)
if __name__ == "__main__":
main()