📑 TensorFlow Workout 03
Code Modules
xxxxxxxxxx
#!python3 -m pip install tensorflow==2.6.0 \
#--user --quiet --no-warn-script-location
path='/home/sc_work/.sage/local/lib/python3.9/site-packages'
import sys,warnings; sys.path.append(path)
warnings.filterwarnings('ignore')
import sys,warnings,os,shutil,zipfile,h5py,urllib
import pathlib,pylab as pl,seaborn as sb
import numpy as np,pandas as pd,tensorflow as tf
from tensorflow.keras.layers.experimental \
import preprocessing as tkp
import tensorflow.keras.layers as tkl,\
tensorflow.keras.models as tkm
from IPython.display import display,Audio
if not sys.warnoptions: warnings.simplefilter('ignore')
rs=int(42); tf.random.set_seed(rs); np.random.seed(rs)
AUTOTUNE=tf.data.AUTOTUNE
File Processing
xxxxxxxxxx
file_path='http://storage.googleapis.com/'+\
'download.tensorflow.org/data/'
file_name='mini_speech_commands.zip'
file_dir='mini_speech_commands/'
input_file=urllib.request.urlopen(file_path+file_name)
output_file=open(file_name,'wb')
output_file.write(input_file.read())
output_file.close(); input_file.close()
with zipfile.ZipFile(file_name,'r') as z:
list_names=\
[x for x in z.namelist()
if (file_dir in x) and ('__MACOSX' not in x) \
and ('README.md' not in x)]
z.close()
dir_names=[x for x in list_names \
if ('.wav' not in x) and (len(x)>21)]
files=[x for x in list_names if ('.wav' in x)]
names=[x[21:-1] for x in dir_names]
files=tf.random.shuffle(files)
num_samples=len(files)
print('labels: ',names)
print('number of total examples: ',num_samples)
print('examples of the tensor array: \n',files[:int(10)].numpy())
train_files=files[:int(.8*num_samples)]
valid_files=files[int(.8*num_samples):int(.9*num_samples)]
test_files=files[-int(.1*num_samples):]
xxxxxxxxxx
def get_audio_and_label(file_path):
file_path_split=tf.strings.split(file_path,os.path.sep)
file_path=file_path.numpy().decode()
file=file_path_split[-int(1)].numpy().decode()
label=file_path_split[-int(2)]
with zipfile.ZipFile(file_name) as z:
with open(file,'wb') as f:
f.write(z.read(file_path)); f.close()
z.close()
audio_raw=tf.io.read_file(file)
audio,_=tf.audio.decode_wav(audio_raw)
audio=tf.squeeze(audio,axis=-int(1))
os.remove(file)
return audio,label
xxxxxxxxxx
def get_spectrogram(
audio,num=int(16000),
frame_length=int(255),frame_step=int(128)):
zeros=tf.zeros([num]-tf.shape(audio),dtype=tf.float32)
audio=tf.cast(audio,tf.float32)
equal_length=tf.concat([audio,zeros],int(0))
# a Fourier transform with time information
spectrogram=tf.signal.stft(
equal_length,frame_length=\
frame_length,frame_step=frame_step)
# the magnitude
spectrogram=tf.abs(spectrogram)
return spectrogram
def get_spectrogram_and_label_id(audio,label):
spectrogram=get_spectrogram(audio)
spectrogram=tf.expand_dims(spectrogram,-int(1))
label_id=tf.argmax(label==names)
return spectrogram,label_id
Data Representation
xxxxxxxxxx
for i in range(3):
n=randint(1,1000)
audio,label=get_audio_and_label(files[int(i+n)])
spectrogram,label_id=\
get_spectrogram_and_label_id(audio,label)
display(table([('label','shape of audio tensors',
'spectrogram shape'),
(label.numpy().decode('utf-8'),
audio.shape,spectrogram.shape)]))
display(Audio(audio,rate=int(16000)))
xxxxxxxxxx
rows,cols=int(2),int(4)
fig,axes=pl.subplots(rows,cols,figsize=(6,3))
for i in range(rows*cols):
audio,label=get_audio_and_label(files[int(i)])
ax=axes[i//cols][i%cols]
ax.plot(audio.numpy(),color='slategray',alpha=.7)
ax.set_yticks(np.arange(-1.2,1.2,.2))
label=label.numpy().decode('utf-8')
ax.set_title('$\\mathbb{'+label+'}$',
color='slategray',fontsize='large')
pl.tight_layout(); pl.show()
xxxxxxxxxx
def plot_spectrogram(spectrogram,ax):
log_spec=np.log(spectrogram.T)
height=log_spec.shape[0]
width=log_spec.shape[1]
x=np.linspace(0,np.size(spectrogram),num=width,dtype=int)
y=range(height)
ax.pcolormesh(x,y,log_spec,cmap='bone',shading='auto')
fig,axes=pl.subplots(2,figsize=(6,3))
timescale=np.arange(audio.shape[0])
axes[0].plot(timescale,audio.numpy(),alpha=.7,color='slategray')
axes[0].set_title('$\mathbb{'+'waveform'+'}$',
color='slategray',fontsize='large')
axes[0].set_xlim([0,16000])
plot_spectrogram(np.squeeze(spectrogram.numpy()),axes[1])
axes[1].set_title('$\mathbb{'+'spectrogram'+'}$',
color='slategray',fontsize='large')
pl.tight_layout(); pl.show()
xxxxxxxxxx
rows,cols=int(2),int(5)
fig,axes=pl.subplots(rows,cols,figsize=(6,3))
for i in range(rows*cols):
audio,label=get_audio_and_label(files[int(i)])
spectrogram,label_id=get_spectrogram_and_label_id(audio,label)
ax=axes[i//cols][i%cols]
plot_spectrogram(np.squeeze(spectrogram.numpy()+.1**10),ax)
ax.set_title('$\mathbb{'+names[label_id.numpy()]+'}$',
color='slategray',fontsize='large')
ax.axis('off')
pl.tight_layout(); pl.show()
Data H5Storing
xxxxxxxxxx
def get_arrays(file_list):
spectr_arr,label_arr=[],[]
for i in range(len(file_list)):
audio,label=get_audio_and_label(file_list[int(i)])
spectrogram,label_id=\
get_spectrogram_and_label_id(audio,label)
spectr_arr.append(spectrogram.numpy())
label_arr.append(label_id.numpy())
return np.array(spectr_arr),np.array(label_arr,dtype='int32')
spectr_train,label_train=get_arrays(train_files[:int(200)])
spectr_valid,label_valid=get_arrays(valid_files[:int(20)])
spectr_test,label_test=get_arrays(test_files[:int(20)])
xxxxxxxxxx
print(' train: \n',
spectr_train.shape,spectr_train.dtype,
label_train.shape,label_train.dtype,'\n valid: \n',
spectr_valid.shape,spectr_valid.dtype,
label_valid.shape,label_valid.dtype,'\n test: \n',
spectr_test.shape,spectr_test.dtype,
label_test.shape,label_test.dtype)
xxxxxxxxxx
rows,cols=int(2),int(5)
fig,axes=pl.subplots(rows,cols,figsize=(6,3))
for i in range(rows*cols):
ax=axes[i//cols][i%cols]
plot_spectrogram(np.squeeze(spectr_test[i]+.1**10),ax)
ax.set_title('$\mathbb{'+names[label_test[i]]+'}$',
color='slategray',fontsize='xx-large')
ax.axis('off')
pl.show()
xxxxxxxxxx
h5f='SpectrCommands124129.h5'
with h5py.File(h5f,'w') as f:
f.create_dataset('spectr_train',data=spectr_train,
compression='gzip')
f.create_dataset('label_train',data=label_train,
compression='gzip')
f.create_dataset('spectr_valid',data=spectr_valid,
compression='gzip')
f.create_dataset('label_valid',data=label_valid,
compression='gzip')
f.create_dataset('spectr_test',data=spectr_test,
compression='gzip')
f.create_dataset('label_test',data=label_test,
compression='gzip')
f.close()
pretty_print('file size: %s'%list(os.stat(h5f))[int(6)])
No comments:
Post a Comment