In [1]:
import h5py
import numpy as np
from pprint import pprint
from termcolor import colored as c, cprint
from IPython.display import FileLink
from tqdm import tqdm 

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
%matplotlib inline

In [2]:
color_palette = ['#507ba6', '#f08e39', '#e0595c', '#79b8b3', '#5ca153', 
                 '#edc854', '#af7ba2', '#fe9fa9', '#9c7561', '#bbb0ac']

def plot_deep_features(deep_features, labels, save=False, **kwargs):
    bins = {}
    for logit, deep_feature in zip(labels, deep_features):
        label = np.argmax(logit)
        
        # print(label)
        try:
            bins[str(label)].append(list(deep_feature))
        except KeyError:
            bins[str(label)] = [list(deep_feature)]

    fig = plt.figure(figsize=(5, 5))
    
    for numeral in map(str, range(10)):
        try:
            features = np.array(bins[numeral])
        except KeyError:
            print(numeral + " does not exist")
            features = []
        
        try:
            x, y = np.transpose(features)
        except ValueError:
            x = []; y = []
        plt.scatter(
            x, 
            y, 
            color=color_palette[int(numeral)],
            label=numeral
        )

    plt.legend(loc=(1.1, 0.1), frameon=False)

    title = 'MNIST LeNet++ with 2 Deep Features (PReLU)'
    plt.title(title)
    
    plt.xlabel('activation of hidden neuron 1')
    plt.ylabel('activation of hidden neuron 2')
    
    if 'xlim' in kwargs:
        plt.xlim(kwargs['xlim'][0], kwargs['xlim'][1])
    if 'ylim' in kwargs:
        plt.ylim(kwargs['ylim'][0], kwargs['ylim'][1])
    
    if save and 'frame_index' in kwargs:
        prefix = 'frame'
        fname = './figures/animation/' + prefix + "_" + str(1000 + kwargs['frame_index'])[-3:] + '.png'
        plt.savefig(fname, dpi=300, bbox_inches='tight')
        plt.close(fig)
    elif save:
        fname = './figures/animation/' + title + '.png'
        plt.savefig(fname, dpi=300, bbox_inches='tight')
        plt.close(fig)
    else:
        plt.show()

In [ ]:
with h5py.File('./dumps/training.h5') as f:
    for i in tqdm(range(0, 20000, 50)):
        step_key = "step_{}".format(str(1000000 + i)[-6:])
        step_entry = f[step_key]
        
        deep_features = step_entry['deep_features']
        logits = step_entry['logits']
        target_labels = step_entry['target_labels']
        
        target_labels_output = list(target_labels)
        
        plot_deep_features(deep_features, target_labels, xlim=(-250, 200), ylim=(-250, 200), save=True, frame_index=int(i/50))

In [3]:
%%sh
pwd
cd figures/animation
ffmpeg -framerate 10 -i frame_%03d.png -c:v libx264 -r 30 -pix_fmt yuv420p output/MNIST_LeNet_faster.mp4


/home/ubuntu/deep_learning_notes/Proj_Centroid_Loss_LeNet/LeNet_plus
ffmpeg version N-80901-gfebc862 Copyright (c) 2000-2016 the FFmpeg developers
  built with gcc 4.8 (Ubuntu 4.8.4-2ubuntu1~14.04.3)
  configuration: --extra-libs=-ldl --prefix=/opt/ffmpeg --mandir=/usr/share/man --enable-avresample --disable-debug --enable-nonfree --enable-gpl --enable-version3 --enable-libopencore-amrnb --enable-libopencore-amrwb --disable-decoder=amrnb --disable-decoder=amrwb --enable-libpulse --enable-libfreetype --enable-gnutls --enable-libx264 --enable-libx265 --enable-libfdk-aac --enable-libvorbis --enable-libmp3lame --enable-libopus --enable-libvpx --enable-libspeex --enable-libass --enable-avisynth --enable-libsoxr --enable-libxvid --enable-libvidstab
  libavutil      55. 28.100 / 55. 28.100
  libavcodec     57. 48.101 / 57. 48.101
  libavformat    57. 41.100 / 57. 41.100
  libavdevice    57.  0.102 / 57.  0.102
  libavfilter     6. 47.100 /  6. 47.100
  libavresample   3.  0.  0 /  3.  0.  0
  libswscale      4.  1.100 /  4.  1.100
  libswresample   2.  1.100 /  2.  1.100
  libpostproc    54.  0.100 / 54.  0.100
Input #0, image2, from 'frame_%03d.png':
  Duration: 00:00:40.00, start: 0.000000, bitrate: N/A
    Stream #0:0: Video: png, rgba(pc), 1730x1398 [SAR 11811:11811 DAR 865:699], 10 fps, 10 tbr, 10 tbn, 10 tbc
[libx264 @ 0x25f0d80] using SAR=1/1
[libx264 @ 0x25f0d80] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX
[libx264 @ 0x25f0d80] profile High, level 5.0
[libx264 @ 0x25f0d80] 264 - core 148 r2643 5c65704 - H.264/MPEG-4 AVC codec - Copyleft 2003-2015 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=12 lookahead_threads=2 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
[mp4 @ 0x25cb9e0] Using AVStream.codec to pass codec parameters to muxers is deprecated, use AVStream.codecpar instead.
Output #0, mp4, to 'output/MNIST_LeNet_faster.mp4':
  Metadata:
    encoder         : Lavf57.41.100
    Stream #0:0: Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 1730x1398 [SAR 1:1 DAR 865:699], q=-1--1, 30 fps, 15360 tbn, 30 tbc
    Metadata:
      encoder         : Lavc57.48.101 libx264
    Side data:
      cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
Stream mapping:
  Stream #0:0 -> #0:0 (png (native) -> h264 (libx264))
Press [q] to stop, [?] for help
frame= 1200 fps= 62 q=-1.0 Lsize=    2807kB time=00:00:39.90 bitrate= 576.3kbits/s dup=800 drop=0 speed=2.07x    
video:2793kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.507388%
[libx264 @ 0x25f0d80] frame I:5     Avg QP:17.94  size: 64138
[libx264 @ 0x25f0d80] frame P:449   Avg QP:20.51  size:  4926
[libx264 @ 0x25f0d80] frame B:746   Avg QP:15.91  size:   438
[libx264 @ 0x25f0d80] consecutive B-frames: 17.1%  0.0%  0.2% 82.7%
[libx264 @ 0x25f0d80] mb I  I16..4: 29.3% 55.2% 15.5%
[libx264 @ 0x25f0d80] mb P  I16..4:  0.1%  0.7%  0.1%  P16..4:  2.7%  2.0%  1.0%  0.0%  0.0%    skip:93.4%
[libx264 @ 0x25f0d80] mb B  I16..4:  0.1%  0.0%  0.0%  B16..8:  3.8%  0.1%  0.0%  direct: 0.0%  skip:95.9%  L0:51.9% L1:47.4% BI: 0.8%
[libx264 @ 0x25f0d80] 8x8 transform intra:59.8% inter:15.8%
[libx264 @ 0x25f0d80] coded y,uvDC,uvAC intra: 7.2% 9.6% 8.0% inter: 0.4% 0.6% 0.5%
[libx264 @ 0x25f0d80] i16 v,h,dc,p: 77% 17%  6%  0%
[libx264 @ 0x25f0d80] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 35%  4% 61%  0%  0%  0%  0%  0%  0%
[libx264 @ 0x25f0d80] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 36% 17% 29%  3%  3%  4%  3%  3%  2%
[libx264 @ 0x25f0d80] i8c dc,h,v,p: 79% 10% 11%  0%
[libx264 @ 0x25f0d80] Weighted P-Frames: Y:0.0% UV:0.0%
[libx264 @ 0x25f0d80] ref P L0: 61.2%  3.3% 19.6% 15.8%
[libx264 @ 0x25f0d80] ref B L0: 76.8% 13.8%  9.4%
[libx264 @ 0x25f0d80] ref B L1: 98.8%  1.2%
[libx264 @ 0x25f0d80] kb/s:571.85

In [ ]:


In [ ]: