Toggle navigation
Toggle navigation
This project
Loading...
Sign in
2020-1-capstone-design1
/
BSH_Project2
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
이혜리
2020-06-04 00:26:35 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
711218708bcc0a3f1a43bea4c48c781db675f341
71121870
1 parent
43ce267e
train
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
89 additions
and
0 deletions
train.py
train.py
0 → 100644
View file @
7112187
#한번 한 다음에는 from kera.models import load_model 이용해서 모델 가지고와서 다시학습,,,,,그거 구현,,
import
os
import
numpy
as
np
np
.
random
.
seed
(
123
)
from
six.moves
import
cPickle
from
keras
import
backend
as
K
from
keras.models
import
Model
from
keras.layers
import
Input
,
Dense
,
Flatten
from
keras.layers
import
LSTM
from
keras.layers
import
TimeDistributed
from
keras.callbacks
import
LearningRateScheduler
,
ModelCheckpoint
from
keras.optimizers
import
Adam
from
prednet
import
PredNet
from
data_utils
import
SequenceGenerator
from
setting
import
*
save_model
=
True
# if weights will be saved
weights_file
=
os
.
path
.
join
(
WEIGHTS_DIR
,
'prednet_weights.hdf5'
)
# where weights will be saved
json_file
=
os
.
path
.
join
(
WEIGHTS_DIR
,
'prednet_model.json'
)
# Data files
train_file
=
os
.
path
.
join
(
DATA_DIR
,
'X_train.hkl'
)
train_sources
=
os
.
path
.
join
(
DATA_DIR
,
'sources_train.hkl'
)
val_file
=
os
.
path
.
join
(
DATA_DIR
,
'X_val.hkl'
)
val_sources
=
os
.
path
.
join
(
DATA_DIR
,
'sources_val.hkl'
)
# Training parameters
nb_epoch
=
150
batch_size
=
10
samples_per_epoch
=
900
N_seq_val
=
100
# number of sequences to use for validation
# Model parameters
n_channels
,
im_height
,
im_width
=
(
3
,
128
,
160
)
input_shape
=
(
n_channels
,
im_height
,
im_width
)
if
K
.
image_data_format
()
==
'channels_first'
else
(
im_height
,
im_width
,
n_channels
)
stack_sizes
=
(
n_channels
,
48
,
96
,
192
)
R_stack_sizes
=
stack_sizes
A_filt_sizes
=
(
3
,
3
,
3
)
Ahat_filt_sizes
=
(
3
,
3
,
3
,
3
)
R_filt_sizes
=
(
3
,
3
,
3
,
3
)
layer_loss_weights
=
np
.
array
([
1.
,
0.
,
0.
,
0.
])
# weighting for each layer in final loss; "L_0" model: [1, 0, 0, 0], "L_all": [1, 0.1, 0.1, 0.1]
layer_loss_weights
=
np
.
expand_dims
(
layer_loss_weights
,
1
)
nt
=
10
# number of timesteps used for sequences in training
time_loss_weights
=
1.
/
(
nt
-
1
)
*
np
.
ones
((
nt
,
1
))
# equally weight all timesteps except the first
time_loss_weights
[
0
]
=
0
prednet
=
PredNet
(
stack_sizes
,
R_stack_sizes
,
A_filt_sizes
,
Ahat_filt_sizes
,
R_filt_sizes
,
output_mode
=
'error'
,
return_sequences
=
True
)
#모델만들기
inputs
=
Input
(
shape
=
(
nt
,)
+
input_shape
)
errors
=
prednet
(
inputs
)
# errors will be (batch_size, nt, nb_layers)
# TimeDistributed - LSTM이 many-to-many로 동작
# 매 스텝마다 cost가 계산되고 하위 스텝으로 오류가 전파
errors_by_time
=
TimeDistributed
(
Dense
(
1
,
trainable
=
False
),
weights
=
[
layer_loss_weights
,
np
.
zeros
(
1
)],
trainable
=
False
)(
errors
)
# calculate weighted error by layer
errors_by_time
=
Flatten
()(
errors_by_time
)
# will be (batch_size, nt)
final_errors
=
Dense
(
1
,
weights
=
[
time_loss_weights
,
np
.
zeros
(
1
)],
trainable
=
False
)(
errors_by_time
)
# weight errors by time
model
=
Model
(
inputs
=
inputs
,
outputs
=
final_errors
)
model
.
compile
(
loss
=
'mean_absolute_error'
,
optimizer
=
'adam'
)
train_generator
=
SequenceGenerator
(
train_file
,
train_sources
,
nt
,
batch_size
=
batch_size
,
shuffle
=
True
)
val_generator
=
SequenceGenerator
(
val_file
,
val_sources
,
nt
,
batch_size
=
batch_size
,
N_seq
=
N_seq_val
)
lr_schedule
=
lambda
epoch
:
0.001
if
epoch
<
75
else
0.0001
# start with lr of 0.001 and then drop to 0.0001 after 75 epochs
# 여기 콜백함수기능: 일정 구간이나 특정 accuracy threshold를 초과할 때 모델에서 checkpointing
callbacks
=
[
LearningRateScheduler
(
lr_schedule
)]
if
save_model
:
if
not
os
.
path
.
exists
(
WEIGHTS_DIR
):
os
.
mkdir
(
WEIGHTS_DIR
)
callbacks
.
append
(
ModelCheckpoint
(
filepath
=
weights_file
,
monitor
=
'val_loss'
,
save_weights_only
=
True
))
# ModelCheckpoint
# 상대적으로 큰 데이터셋을 학습할 때, 빈번하게 모델의 체크포인트를 저장하는 것은 매우 중요
# monitor='val_loss', val_loss 값이 개선되었을때 호출됩니다
# save_best_only=True, 가장 best 값만 저장합니다
history
=
model
.
fit_generator
(
train_generator
,
int
(
samples_per_epoch
/
batch_size
),
nb_epoch
,
callbacks
=
callbacks
,
validation_data
=
val_generator
,
validation_steps
=
int
(
N_seq_val
/
batch_size
))
if
save_model
:
json_string
=
model
.
to_json
()
with
open
(
json_file
,
"w"
)
as
f
:
f
.
write
(
json_string
)
\ No newline at end of file
Please
register
or
login
to post a comment