<jupyter_start><jupyter_text>Read config files and set global variables<jupyter_code>run_id = 'VAE_6dB_valve_id_00'

base_conf = train.read_config('conf/conf_base.ini')

conf_path = naming.get_conf_path(run_id)
param_conf = train.read_config(conf_path)
# Directories
RAW_DATA_DIR = base_conf['directories']['raw_data_dir']
BASE_DIR = base_conf['directories']['base_dir']


# Mel spectrograms
N_MELS = param_conf.getint('melspec', 'n_mels')
N_FFT = param_conf.getint('melspec', 'n_fft')
HOP_LENGTH = param_conf.getint('melspec', 'hop_length')
POWER = param_conf.getfloat('melspec', 'power')
WINDOW = param_conf.get('melspec', 'window')


# Subsampling
DIM = param_conf.getint('melspec', 'dim')
STEP = param_conf.getint('melspec', 'step')


# Scaler and cost function
SCALER_TYPE = param_conf.get('model', 'scaler')
LOSS = param_conf.get('model', 'loss')


# Optimizer
OPTIMIZER = param_conf.get('model', 'optimizer')
EPOCHS = param_conf.getint('model', 'epochs')
BATCH_SIZE = param_conf.getint('model', 'batch_size')
VALIDATION_SPLIT = param_conf.getfloat('model', 'val_split')
SHUFFLE = param_conf.getboolean('model', 'shuffle')


# Autoencoder
MODEL_NAME = param_conf.get('autoencoder', 'model_name')

if MODEL_NAME == 'lstmAE':
    NUM_NODES = json.loads(param_conf.get('autoencoder', 'num_nodes'))
    INPUT_SHAPE = (param_conf.getint('melspec', 'dim'),
                   param_conf.getint('melspec', 'n_mels'))
elif MODEL_NAME in ['AE', 'VAE']:
    LATENT_DIM = param_conf.getint('autoencoder', 'latentdim')
    NUM_NODES = json.loads(param_conf.get('autoencoder', 'num_nodes'))
    NUM_KERNEL = json.loads(param_conf.get('autoencoder', 'num_kernel'))
    NUM_STRIDES = param_conf.get('autoencoder', 'num_strides')
    NUM_STRIDES = list(ast.literal_eval(NUM_STRIDES))
    INPUT_SHAPE = (param_conf.getint('melspec', 'dim'),
                   param_conf.getint('melspec', 'n_mels'),
                   1)
else:
    print('Invalid model name.')


# Data
DB = param_conf.get('data', 'noise')
MACHINE_TYPE = param_conf.get('data', 'machine')
MACHINE_ID = param_conf.get('data', 'machine_id')<jupyter_output><empty_output><jupyter_text>Load saved tensorflow model for variational autoencoder<jupyter_code>model_path = naming.get_model_path(run_id)
saved_model = train.load_saved_model(model_path)<jupyter_output><empty_output><jupyter_text>Load fitted scaler for variational autoencoder<jupyter_code>path_saved_scaler = naming.get_scaler_path(run_id)
saved_scaler = spec.load_saved_scaler(path_saved_scaler)
saved_model.summary()<jupyter_output>Model: "vae"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
encoder_input (InputLayer)   [(None, 64, 32, 1)]       0         
_________________________________________________________________
encoder (Functional)         [(None, 20), (None, 20),  587740    
_________________________________________________________________
decoder (Functional)         (None, 64, 32, 1)         724865    
=================================================================
Total params: 1,312,605
Trainable params: 1,311,197
Non-trainable params: 1,408
_________________________________________________________________<jupyter_text>Extract the encoder and decoder from the VAE<jupyter_code># Load encoder and decoder
encoder, decoder = viz.load_encoder_decoder(saved_model)<jupyter_output><empty_output><jupyter_text>Visualizations A. Visualization of the decoded space i.e. output from the decoder<jupyter_code>normal_file = spec.get_normal_mel_files(BASE_DIR, DB, MACHINE_TYPE, MACHINE_ID)[0]
abnormal_file = spec.get_abnormal_mel_files(BASE_DIR, DB, MACHINE_TYPE, MACHINE_ID)[0]<jupyter_output><empty_output><jupyter_text>1. Visualize the normal and abnormal spectrograms<jupyter_code>viz.plot_spectrogram(normal_file, sr=16000)
viz.plot_spectrogram(abnormal_file, sr=16000)<jupyter_output><empty_output><jupyter_text>For the chosen normal spectrogram, break it up into slices by subsampling and extract the original and decoded slices.<jupyter_code>orig_normal, decoded_normal = eval.decode_spectrogram(saved_model, saved_scaler, DIM, STEP, normal_file)<jupyter_output>/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:2325: UserWarning: `Model.state_updates` will be removed in a future version. This property should not be used in TensorFlow 2.0, as `updates` are applied automatically.
  warnings.warn('`Model.state_updates` will be removed in a future version. '<jupyter_text>2. Plot the original and decoded spectrogram slices after subsampling for normal sound (first 10 slices)<jupyter_code>viz.render_predictions(orig_normal, decoded_normal, sr=16000)<jupyter_output><empty_output><jupyter_text>For the chosen abnormal/anomalous spectrogram, break it up into slices by subsampling and extract the original and decoded slices.<jupyter_code>orig_abnormal, decoded_abnormal = eval.decode_spectrogram(saved_model, saved_scaler, DIM, STEP, abnormal_file)<jupyter_output><empty_output><jupyter_text>3. Plot the original and decoded spectrogram slices after subsampling for anomalous/abnormal sound (first 10 slices)<jupyter_code>viz.render_predictions(orig_abnormal, decoded_abnormal, sr=16000)<jupyter_output><empty_output><jupyter_text>B. Visualizations for the encoded space i.e. output from the encoder Choose a set of normal and abnormal files to encode into the latent space<jupyter_code>set_of_normal_files = spec.get_normal_mel_files(BASE_DIR, DB, MACHINE_TYPE, MACHINE_ID)[0:30]
set_of_abnormal_files = spec.get_abnormal_mel_files(BASE_DIR, DB, MACHINE_TYPE, MACHINE_ID)[0:30]<jupyter_output><empty_output><jupyter_text>Encode the spectrograms into the latent space and extract the mean and log_variance vectors<jupyter_code>normal_mean, normal_logvar = viz.encode_test_files(encoder, saved_scaler, DIM, STEP, set_of_normal_files)
abnormal_mean, abnormal_logvar = viz.encode_test_files(encoder, saved_scaler, DIM, STEP, set_of_abnormal_files)
encoded_test_data = np.concatenate([normal_mean, abnormal_mean])
test_labels = [0] * len(normal_mean) + [1] * len(abnormal_mean)<jupyter_output><empty_output><jupyter_text>4. Plot the latent space embeddings of the chosen normal and anomalous set of spectrograms  - Label 1: Abnormal data  - Label 0: Normal dataThe Latent Space having 20 dimensions is further condensed into 2 dimensions for visualization using TSNE dimensionality reduction.<jupyter_code>viz.Tsne_Projection_Of_Latent(encoded_test_data, test_labels)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1211663393261822, 'fraction_numerical': 0.015108022359873092, 'mean_word_length': 7.306148055207026, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '566504', 'license': 'MIT', 'n_tokens_mistral': 2308, 'n_tokens_neox': 2154, 'n_words': 442, 'path': 'notebooks/WB_20210331_visualization_VarAE.ipynb', 'repo_name': 'saraghsm/sound-of-failure', 'size': '6619'}