Skip to content

Instantly share code, notes, and snippets.

@SuperShinyEyes
Last active February 8, 2021 14:19
Show Gist options
  • Save SuperShinyEyes/02c5e5336ca74b30ed12c0b0b386f4e9 to your computer and use it in GitHub Desktop.
Save SuperShinyEyes/02c5e5336ca74b30ed12c0b0b386f4e9 to your computer and use it in GitHub Desktop.
import matplotlib.pyplot as plt
import soundfile as sf
import librosa
plt.rcParams['font.family'] = 'serif'
plt.rcParams['font.serif'] = 'Ubuntu'
plt.rcParams['font.monospace'] = 'Ubuntu Mono'
plt.rcParams['font.size'] = 10
plt.rcParams['axes.labelsize'] = 10 # Time and Hz, i.e. labels
plt.rcParams['axes.labelweight'] = 'bold'
plt.rcParams['xtick.labelsize'] = 15 # Time(x) tick values
plt.rcParams['ytick.labelsize'] = 15 # Hz(y) tick values
plt.rcParams['legend.fontsize'] = 17
plt.rcParams['figure.titlesize'] = 10
plt.rcParams['axes.titlesize'] = 14 # Title font
#---------------------------------------------------
# Plot amplitudes
fig, axarr = plt.subplots(3, 3, figsize=(17, 8))
fig.suptitle("15 sec & 3 sec & 1.5 sec", fontsize=16)
length = 5 # sec
#----------------------------------------------------
# Reference
ref, sr = sf.read("adele/test/11 Someone Like You.wav", 44100*15, )
axarr[0,0].plot(ref[:44100*length], 'b')
axarr[0,1].plot(ref[:44100*length//5], 'b')
axarr[0,2].plot(ref[:44100*length//10], 'b')
axarr[0,2].legend(['Reference'], loc=1)
#----------------------------------------------------
# Damaged
y, sr = librosa.core.load("adele/test/4410/11 Someone Like You.wav", sr=None)
axarr[1,0].plot(y[:4410*length], 'r')
axarr[1,1].plot(y[:4410*length//5], 'r')
axarr[1,2].plot(y[:4410*length//10], 'r')
axarr[1,2].legend(['Damaged'], loc=1)
#----------------------------------------------------
# Decoded
decoded, sr = librosa.core.load("adele/test/decoded/11 Someone Like You.mp3", sr=None)
axarr[2,0].plot(decoded[:44100*length], 'g')
axarr[2,1].plot(decoded[:44100*length//5], 'g')
axarr[2,2].plot(decoded[:44100*length//10], 'g')
axarr[2,2].legend(['Decoded'], loc=1)
#----------------------------------------------------
# Fine-tune figure; hide x ticks for top plots and y ticks for right plots
plt.setp([a.get_xticklabels() for a in axarr[0, :]], visible=False)
plt.setp([a.get_yticklabels() for a in axarr[:, 1]], visible=False)
# Tight layout often produces nice results
# but requires the title to be spaced accordingly
fig.tight_layout()
fig.subplots_adjust(top=0.92)
@SuperShinyEyes
Copy link
Author

def plot_predictions_batch(images: List[ndarray], predictions: List[str], gts: List[str], fig_title: str):
    n_image_per_plot = 42
    n_image = len(images)
    for i in range(math.ceil(n_image / n_image_per_plot)):
        start_i = i * n_image_per_plot
        end_i = start_i + n_image_per_plot
        plot_predictions(
            images[start_i: end_i],
            predictions[start_i: end_i],
            gts[start_i: end_i],
            fig_title=f'{fig_title}_{i}'
        )

def plot_predictions(images: List[ndarray], predictions: List[str], gts: List[str], fig_title: str):
    n_image = len(images)
    n_col = 6
    n_row = math.ceil(n_image / n_col)
    fig, axes = plt.subplots(n_row, n_col)
    for i, (y, x) in zip(range(n_image), itertools.product(range(n_row), range(n_col))):
        ax = axes[y, x]
        ax.axis('off')
        ax.imshow(images[i])
        pred, gt = predictions[i], gts[i]
        if pred == gt:
            ax.set_title(gt, fontdict={'color':'green'}, fontsize=11)
        else:
            ax.set_title(f'gt: {gt}\npr: {pred}', fontdict={'color':'red'}, fontsize=11)

    fig.set_size_inches(n_col * 2, n_row * 0.9) 
    fig.suptitle(fig_title, fontsize=16)
    fig.savefig(f'plots/{fig_title}.jpg')
    
plot_predictions_batch(val_input_images, result_validation, val_labels, fig_title='pretrained=synth-val_set')

ean_pretrained_with_synth_4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment