chiyoi commited on
Commit
804b63c
1 Parent(s): aea2a9b
movinet/data.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pathlib import Path
2
+ import random
3
+ from typing import Literal
4
+ import cv2
5
+ import numpy as np
6
+ import tensorflow as tf
7
+
8
+ TRAINING_RATIO = 0.1
9
+ VALIDATION_RATIO = 0.01
10
+
11
+ def format_frames(frame, output_size):
12
+ frame = tf.image.convert_image_dtype(frame, tf.float32)
13
+ frame = tf.image.resize_with_pad(frame, *output_size)
14
+ return frame
15
+
16
+ def frames_from_video_file(video_path: str, n_frames: int, output_size=(256, 256), frame_step=15):
17
+ capture = cv2.VideoCapture(video_path)
18
+ if not capture.isOpened(): raise ValueError('Video file could not be opened.')
19
+ total_frames = capture.get(cv2.CAP_PROP_FRAME_COUNT)
20
+ need_frames = 1 + (n_frames - 1) * frame_step
21
+
22
+ if need_frames <= total_frames:
23
+ start = random.randint(0, total_frames - need_frames + 1)
24
+ capture.set(cv2.CAP_PROP_POS_FRAMES, start)
25
+
26
+ frames = []
27
+ for _ in range(n_frames - 1):
28
+ for _ in range(frame_step):
29
+ ok, frame = capture.read()
30
+ if ok:
31
+ frames.append(format_frames(frame, output_size))
32
+ else:
33
+ frames.append(np.zeros((output_size[0], output_size[1], 3)))
34
+ capture.release()
35
+
36
+ frames = np.array(frames)
37
+ frames = frames[..., [2, 1, 0]]
38
+ return frames
39
+
40
+ def Data(data_dir: Path):
41
+ return {
42
+ 'training':{
43
+ a.name: (lambda ps: ps[:int(len(ps) * TRAINING_RATIO)])([x for x in a.iterdir()])
44
+ for a in data_dir.iterdir()
45
+ },
46
+ 'validation': {
47
+ a.name: (lambda ps: ps[
48
+ int(len(ps) * TRAINING_RATIO) :
49
+ int(len(ps) * (TRAINING_RATIO + VALIDATION_RATIO))
50
+ ])([x for x in a.iterdir()])
51
+ for a in data_dir.iterdir()
52
+ },
53
+ }
54
+
55
+ def frame_generator(data_dir: Path, n_frames: int, split: Literal['training', 'validation']):
56
+ class_names = sorted([x.name for x in data_dir.iterdir()])
57
+ class_ids_for_name = {
58
+ name: i
59
+ for i, name in enumerate(class_names)
60
+ }
61
+ data = Data(data_dir)
62
+
63
+ def generator():
64
+ pairs = [
65
+ (path, name)
66
+ for name, paths in data[split].items()
67
+ for path in paths
68
+ ]
69
+ random.shuffle(pairs)
70
+ for path, name in pairs:
71
+ video_frames = frames_from_video_file(str(path), n_frames)
72
+ label = class_ids_for_name[name]
73
+ yield video_frames, label
74
+ return generator
75
+
76
+ def total_steps(data_dir: Path):
77
+ data = Data(data_dir)
78
+ size = lambda d: sum([len(x) for x in d.values()])
79
+ return size(data['training']), size(data['validation'])
movinet/model.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ from site_packages.models.official.projects.movinet.modeling import movinet_model
2
+
3
+ def build_classifier(batch_size: int, num_frames: int, resolution: int, backbone, num_classes: int):
4
+ model = movinet_model.MovinetClassifier(
5
+ backbone=backbone,
6
+ num_classes=num_classes,
7
+ )
8
+ model.build([batch_size, num_frames, resolution, resolution, 3])
9
+ return model
movinet/scripts/train.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from pathlib import Path
3
+
4
+ import tensorflow as tf
5
+ import tf_keras as keras
6
+
7
+ from site_packages.models.official.projects.movinet.modeling import movinet
8
+
9
+ from movinet.data import frame_generator, total_steps
10
+ from movinet.model import build_classifier
11
+
12
+ model_id = 'a0'
13
+ resolution = 256
14
+ batch_size = 8
15
+ num_frames = 8
16
+ num_classes = 6
17
+ model_save_path = "out/aero-recognize-classifier.keras"
18
+ num_epochs = 2
19
+
20
+ print('Load data.')
21
+ data_dir = Path('assets/datasets/Aero')
22
+ output_signature = (
23
+ tf.TensorSpec(shape=(None, None, None, 3), dtype=tf.float32),
24
+ tf.TensorSpec(shape=(), dtype=tf.int16),
25
+ )
26
+ training_data = tf.data.Dataset.from_generator(
27
+ frame_generator(data_dir, num_frames, 'training'),
28
+ output_signature=output_signature,
29
+ )
30
+ training_data = training_data.batch(batch_size)
31
+ validation_data = tf.data.Dataset.from_generator(
32
+ frame_generator(data_dir, num_frames, 'validation'),
33
+ output_signature=output_signature,
34
+ )
35
+ validation_data = validation_data.batch(batch_size)
36
+
37
+ print('Build model.')
38
+ backbone = movinet.Movinet(model_id=model_id)
39
+ backbone.trainable = True
40
+ model = build_classifier(batch_size, num_frames, resolution, backbone, 6)
41
+
42
+ print('Start training.')
43
+ model_dir = os.path.dirname(model_save_path)
44
+ save_model = keras.callbacks.ModelCheckpoint(filepath=model_save_path)
45
+ loss = keras.losses.SparseCategoricalCrossentropy(from_logits=True)
46
+ # optimizer = keras.optimizers.legacy.Adam(learning_rate=0.001)
47
+ model.compile(optimizer='adam', loss=loss, metrics=['accuracy'])
48
+ train_steps, validation_steps = total_steps(data_dir)
49
+ results = model.fit(
50
+ training_data,
51
+ steps_per_epoch=train_steps,
52
+ validation_data=validation_data,
53
+ validation_steps=validation_steps,
54
+ epochs=num_epochs,
55
+ validation_freq=1,
56
+ verbose=1,
57
+ callbacks=[save_model],
58
+ )
playgrounds/main.py DELETED
@@ -1,3 +0,0 @@
1
- from playgrounds.yolo import main
2
-
3
- main()
 
 
 
 
playgrounds/verify_metal.py ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import tensorflow as tf
2
+
3
+ cifar = tf.keras.datasets.cifar100
4
+ (x_train, y_train), (x_test, y_test) = cifar.load_data()
5
+ model = tf.keras.applications.ResNet50(
6
+ include_top=True,
7
+ weights=None,
8
+ input_shape=(32, 32, 3),
9
+ classes=100,
10
+ )
11
+
12
+ loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False)
13
+ model.compile(optimizer="adam", loss=loss_fn, metrics=["accuracy"])
14
+ model.fit(x_train, y_train, epochs=5, batch_size=64)
requirements.txt ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ gradio
2
+ tensorflow
3
+ opencv-python
4
+
5
+ # cspell: disable
6
+ # models/official
7
+ six
8
+ google-api-python-client>=1.6.7
9
+ kaggle>=1.3.9
10
+ numpy>=1.20
11
+ oauth2client
12
+ pandas>=0.22.0
13
+ psutil>=5.4.3
14
+ py-cpuinfo>=3.3.0
15
+ scipy>=0.19.1
16
+ tensorflow-hub>=0.6.0
17
+ tensorflow-model-optimization>=0.4.1
18
+ tensorflow-datasets
19
+ tf-keras
20
+ gin-config
21
+ tf_slim>=1.1.0
22
+ Cython
23
+ matplotlib
24
+ # Loader becomes a required positional argument in 6.0 in yaml.load
25
+ pyyaml>=6.0.0
26
+ # CV related dependencies
27
+ opencv-python-headless
28
+ Pillow
29
+ pycocotools
30
+ # NLP related dependencies
31
+ seqeval
32
+ sentencepiece
33
+ sacrebleu
34
+ # Projects/vit dependencies
35
+ immutabledict
site_packages/models ADDED
@@ -0,0 +1 @@
 
 
1
+ Subproject commit d14cf43b09cc29d68900bb9f766de19b01acde40