Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tf2 update #7

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions 01-import.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ library(dplyr)

files <- fs::dir_ls(
path = "data/speech_commands_v0.01/",
recursive = TRUE,
recurse = TRUE,
glob = "*.wav"
)

files <- files[!str_detect(files, "background_noise")]

df <- data_frame(
df <- tibble(
fname = files,
class = fname %>% str_extract("1/.*/") %>%
str_replace_all("1/", "") %>%
Expand Down
19 changes: 8 additions & 11 deletions 02-generator.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

library(tfdatasets)

audio_ops <- tf$contrib$framework$python$ops$audio_ops

data_generator <- function(df, batch_size, shuffle = TRUE,
window_size_ms = 30, window_stride_ms = 10) {

Expand All @@ -21,18 +19,17 @@ data_generator <- function(df, batch_size, shuffle = TRUE,
dataset_map(function(obs) {

# decoding wav files
audio_binary <- tf$read_file(tf$reshape(obs$fname, shape = list()))
wav <- audio_ops$decode_wav(audio_binary, desired_channels = 1)
audio_binary <- tf$io$read_file(tf$reshape(obs$fname, shape = list()))
wav <- tf$audio$decode_wav(audio_binary, desired_channels = 1)
samples <- wav$audio
samples <- samples %>% tf$transpose(perm = c(1L, 0L))

# create the spectrogram
spectrogram <- audio_ops$audio_spectrogram(
wav$audio,
window_size = window_size,
stride = stride,
magnitude_squared = TRUE
)
spectrogram <- tf$signal$stft(samples,
frame_length = as.integer(window_size),
frame_step = as.integer(stride))

spectrogram <- tf$log(tf$abs(spectrogram) + 0.01)
spectrogram <- tf$math$log(tf$abs(spectrogram) + 0.01)
spectrogram <- tf$transpose(spectrogram, perm = c(1L, 2L, 0L))

# transform the class_id into a one-hot encoded vector
Expand Down
7 changes: 2 additions & 5 deletions 03-model.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ model %>%

# Compile model
model %>% compile(
loss = loss_categorical_crossentropy,
loss = "categorical_crossentropy",
optimizer = optimizer_adadelta(),
metrics = c('accuracy')
)
Expand All @@ -42,7 +42,4 @@ model %>% fit_generator(
validation_steps = 0.3*nrow(df)/32
)

save_model_hdf5(model, "model.hdf5")



save_model_hdf5(model, filepath = "model.hdf5")
Loading