Commit b29de0cc authored by Fabio Frazao's avatar Fabio Frazao
Browse files

tutorial.ipynb-Update model loading

parent 38cb94f7
......@@ -53,12 +53,10 @@
%% Cell type:code id: tags:
``` python
import tables
from recipe import recipe # We'll use this recipe to build a ResNet
# with precisely the same configuration as used during training
from plot_utils import plot_spec_scalar_pred as plot_spec# a helper function to plot model outputs
from ketos.neural_networks.resnet import ResNetInterface # the ResNet implementation
#Display plots within this notebook, instead of opening a new window
%matplotlib inline
......@@ -69,129 +67,24 @@
<a id='step_2'></a>
## 2. Load pre-trained neural network
%% Cell type:markdown id: tags:
In order to use a pre-trained model, we need to reconstruct that model's network architecture and then load the saved weights.
%% Cell type:markdown id: tags:
The architecture can be reconstructed from the recipe we imported above. It specifies a few key parameters used in the pre-trained model, such as the number of residual blocks and the number of convolutional layers in each block, among others.
%% Cell type:code id: tags:
``` python
recipe
```
%%%% Output: execute_result
{'block_list': [2, 2, 2, 2],
'n_classes': 2,
'initial_filters': 16,
'optimizer': <tensorflow.python.keras.optimizer_v2.adam.Adam at 0x7f090bcc8450>,
'loss_function': <ketos.neural_networks.losses.FScoreLoss at 0x7f090bcc84d0>,
'metrics': [<tensorflow.python.keras.metrics.CategoricalAccuracy at 0x7f090b4d0c50>]}
%% Cell type:markdown id: tags:
The weights are available in the *pre_trained_model* folder included with this tutorial.
%% Cell type:code id: tags:
``` python
weights_path = "./pre_trained_model/weights"
```
%% Cell type:markdown id: tags:
With those, the load method can reconstruct the pre-trained model
%% Cell type:code id: tags:
``` python
resnet_model = ResNetInterface.load(recipe, weights_path )
resnet_model = ResNetInterface.load_model("assets/narw.kt")
```
%% Cell type:markdown id: tags:
<a id='step_3'></a>
## 3. Load test data
%% Cell type:markdown id: tags:
Now that we have the model, we can run it on some examples from the database
%% Cell type:code id: tags:
``` python
h5 = tables.open_file("data/RWhales.h5",'r') #creates a connection to the HDF5 database
test_table = h5.get_node("/test/specs_3s") #loads the test dataset (i.e.: data that has not been used during model training)
```
%% Cell type:code id: tags:
``` python
test_table.nrows
```
%%%% Output: execute_result
896
%% Cell type:markdown id: tags:
The test dataset has 896 rows, each containing the spectrogram representation of a 3 seconds audio segment, the expert-provided label (1 if the segment contained a NARW upcall, 0 if not). Other fields include the corresponding .wav file name, the unix timestamp when that audio segment was recorded and a dataset identification code
%% Cell type:code id: tags:
``` python
test_table
```
%%%% Output: execute_result
/test/specs_3s (Table(896,)) ''
description := {
"data": Float32Col(shape=(94, 129), dflt=0.0, pos=0),
"dataset_id": StringCol(itemsize=4, shape=(), dflt=b'', pos=1),
"datetime": StringCol(itemsize=25, shape=(), dflt=b'', pos=2),
"label": Int8Col(shape=(), dflt=0, pos=3),
"sound_file": StringCol(itemsize=50, shape=(), dflt=b'', pos=4)}
byteorder := 'little'
chunkshape := (5,)
%% Cell type:markdown id: tags:
We can select any of the entries and any of the fields in the following manner
%% Cell type:code id: tags:
``` python
test_table[0]['sound_file'] # Print the name of the wav file from which the first entry was generated
```
%%%% Output: execute_result
b'VAS_Sample_2019-08-06_214956.wav'
%% Cell type:code id: tags:
``` python
test_table[799]['label'] # Show the label associated with the 800th entry (1 = contains a right whale upcall)
```
%%%% Output: execute_result
1
%% Cell type:markdown id: tags:
We can use the plot_spec function, which we imported at the beginning, to view the spectrogram
%% Cell type:code id: tags:
``` python
fig = plot_spec(file_name = test_table[799]['sound_file'],
spec_data = test_table[799]['data'],
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment