-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
124 lines (102 loc) · 4.49 KB
/
main.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
115
116
117
118
119
120
121
122
123
124
from trainer import Trainer
# from tester import Tester
from data_loader import Data_Loader
from torch.backends import cudnn
from utils import make_folder
from config import get_parameters
import os
import torch
##### Import libary for dataloader #####
##### https://github.com/kenshohara/3D-ResNets-PyTorch/blob/master/main.py
from preprocessing.Dataloader.transforms.spatial import (
Compose, Normalize, Scale, CenterCrop, CornerCrop, MultiScaleCornerCrop,
MultiScaleRandomCrop, RandomHorizontalFlip, ToTensor)
from preprocessing.Dataloader.transforms.temporal import LoopPadding, TemporalRandomCrop
from preprocessing.Dataloader.transforms.target import ClassLabel, VideoID
from preprocessing.Dataloader.transforms.target import Compose as TargetCompose
from preprocessing.Dataloader.dataloader import get_training_set, get_validation_set, get_test_set
from preprocessing.Dataloader.mean import get_mean
def main(config):
# For fast training
cudnn.benchmark = True
##### Dataloader #####
config.video_path = os.path.join(config.root_path, config.video_path)
config.annotation_path = os.path.join(config.root_path, config.annotation_path)
config.mean = get_mean(config.norm_value, dataset=config.mean_dataset)
if config.no_mean_norm and not config.std_norm:
norm_method = Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
elif not config.std_norm:
norm_method = Normalize(config.mean, [1, 1, 1])
config.scales = [config.initial_scale]
for i in range(1, config.n_scales):
config.scales.append(config.scales[-1] * config.scale_step)
if config.train:
assert config.train_crop in ['random', 'corner', 'center']
if config.train_crop == 'random':
crop_method = MultiScaleRandomCrop(config.scales, config.sample_size)
elif config.train_crop == 'corner':
crop_method = MultiScaleCornerCrop(config.scales, config.sample_size)
elif config.train_crop == 'center':
crop_method = MultiScaleCornerCrop(
config.scales, config.sample_size, crop_positions=['c'])
spatial_transform = Compose([
crop_method,
RandomHorizontalFlip(),
ToTensor(config.norm_value), norm_method
])
temporal_transform = TemporalRandomCrop(config.n_frames)
target_transform = ClassLabel()
print("="*30,"\nLoading data...")
training_data = get_training_set(config, spatial_transform,
temporal_transform, target_transform)
train_loader = torch.utils.data.DataLoader(
training_data,
batch_size=config.batch_size,
shuffle=True,
num_workers=config.num_workers,
pin_memory=True)
else:
spatial_transform = Compose([
Scale(config.sample_size),
CenterCrop(config.sample_size),
ToTensor(config.norm_value), norm_method
])
temporal_transform = LoopPadding(config.n_frames)
target_transform = ClassLabel()
validation_data = get_validation_set(
config, spatial_transform, temporal_transform, target_transform)
val_loader = torch.utils.data.DataLoader(
validation_data,
batch_size=config.batch_size,
shuffle=False,
num_workers=config.num_workers,
pin_memory=True)
##### End dataloader #####
# Use Big-GAN dataset to test only
# The random data is used in the trainer
# Need to pre-process data and use the dataloader (above)
# config.n_class = len(glob.glob(os.path.join(config.root_path, config.video_path)))
## Data loader
print('number class:', config.n_class)
# # Data loader
# data_loader = Data_Loader(config.train, config.dataset, config.image_path, config.imsize,
# config.batch_size, shuf=config.train)
# Create directories if not exist
make_folder(config.model_save_path, config.version)
# make_folder(config.sample_path, config.version)
make_folder(config.log_path, config.version)
if config.train:
if config.model=='dvd-gan':
trainer = Trainer(train_loader, config)
else:
trainer = None
torch.cuda.empty_cache()
trainer.train()
# else:
# tester = Tester(val_loader, config)
# tester.test()
if __name__ == '__main__':
config = get_parameters()
for key in config.__dict__.keys():
print(key, "=", config.__dict__[key])
main(config)