B ½ú]€ã@sÆddlZddlZddlZddlZddlmZddlm Z ddl m Z ddl Z ddlmZddlmZddlZddd„Zdd „Zd d ddddgdfd d „Zd d ddddgdfdd„Zdd„Zdd„ZdS)éN)ÚVideo)Údisplay©ééçü©ñÒMbP?cCstt ¡t ¡tj|dtj|ddtj|ddtjdddtjdd d tjd d d tj d d d dS)z+Utility for plotting losses with smoothing.)ÚfigsizeZC_loss)ÚlabelÚG_lossz lower rightÚmedium)ÚlocÚfontsizeÚ Iterationzx-large)r ÚLosseszTraining Historyzxx-largeN) ÚpltÚionÚsnsÚsetÚfigureÚplotÚlegendÚxlabelÚylabelÚtitle)r ZD_lossrÚ smoothing©rú5/Users/mzliang/Downloads/Lab 2/utils/display_utils.pyÚplot_loss_logss rcCs@tj ¡}| |¡| ¡| ¡tjj |¡}| d¡dS)NÚmidi) Úmusic21rÚMidiFileÚopenÚreadÚcloseÚ translateÚmidiFileToStreamÚshow)ÚfilenameÚmfÚsrrrÚplaymidi,s   r*é-éUc sD|dk}dd„‰‡fdd„|Dƒ}t|djƒdksJtdt|djƒƒ‚|dj\}}}t ¡tjdd }|d} xÈtd ƒD]¼} | d } | d d | ¡} t  | | d ¡} |rÐ|   d t   || d ¡d ¡| d d krè|   d¡n |  g¡| j| d| d tjdd| d d|  dd¡|  d|¡| | ¡q€WdS)aN Plot a MultiTrack PianoRoll :param x: Multi Instrument PianoRoll Tensor :param min_pitch: Min Pitch / Min Y across all instruments. :param max_pitch: Max Pitch / Max Y across all instruments. :param programs: Program Number of the Tracks. :param file_name: Optional. File Name to save the plot. :return: rcSst t |¡dd¡S)Nér)ÚnpÚmoveaxisÚarray)ÚxrrrÚCóz show_pianoroll..csg|] }ˆ|ƒ‘qSrr)Ú.0r1)Ú channel_lastrrú Dsz"show_pianoroll..éz(Pianoroll shape must have 3 dims, Got %d)ré)rr8ézTime(ú)ÚPitchÚbgrcmk)r)Úcolorr+r,N)ÚlenÚshapeÚAssertionErrorrrrÚrangeÚ add_subplotr.ÚnonzeroÚ set_xlabelÚ pretty_midiÚprogram_to_instrument_classÚ set_ylabelÚ set_yticksÚscatterÚpiÚset_ylimÚset_xlim)ÚxsÚ min_pitchÚ max_pitchÚprogramsÚsave_dirÚn_tracksÚ time_stepÚ_Úfigr1ÚjÚbÚaxÚnzr)r5rÚshow_pianoroll4s* (     (  rZc s>|dk}|dk}dd„‰‡fdd„|Dƒ}‡fdd„|Dƒ}t|djƒdksdtdt|djƒƒ‚|dj\}}} t ¡tjd d } tjd d d d d} x8tdƒD]*} tj d || | ddd} || || }}xút t   dd gdd ddg¡ƒD]Ú\}\}}|   | |¡}|dkr,t ||d¡}nt ||d¡}|rd| dt ||d¡d¡|dkrz| d¡n | g¡|j|d|dtjdd|dd| dd¡| d|¡|   |¡qôWq¦Wt|tƒr tjd |¡ddtj |d|¡}ntjdddtj |d¡}t  |¡t !| ¡d S)!aN Plot a MultiTrack PianoRoll :param x: Multi Instrument PianoRoll Tensor :param min_pitch: Min Pitch / Min Y across all instruments. :param max_pitch: Max Pitch / Max Y across all instruments. :param programs: Program Number of the Tracks. :param file_name: Optional. File Name to save the plot. :return: rcSst t |¡dd¡S)Nr-r)r.r/r0)r1rrrr2qr3z plot_pianoroll..csg|] }ˆ|ƒ‘qSrr)r4r1)r5rrr6rsz"plot_pianoroll..csg|] }ˆ|ƒ‘qSrr)r4Úfake_x)r5rrr6ssr7z(Pianoroll shape must have 3 dims, Got %d)ré)rr-gš™™™™™¹?gš™™™™™É?)ÚwspaceÚhspacer8g)Ú subplot_specr]r^r9zTime(r:r;r<)r)r=r+r,z iteration: {}é)r zsample_iteration_%05d.pngZ Inferencezsample_inference.pngN)"r>r?r@rrrÚgridspecÚGridSpecrAÚGridSpecFromSubplotSpecÚ enumerateÚ itertoolsÚproductrBr.rCrDrErFrGrHrIrJrKrLÚ isinstanceÚintÚsuptitleÚformatÚosÚpathÚjoinÚsavefigr#)Ú iterationrMZfake_xsrNrOrPrQrRrSrTrUZ outer_gridÚiZ inner_gridr1r[rVÚarWrXrYr'r)r5rÚplot_pianorollasF ( *     (    rrcCs~t ¡tjddtjdd}t t|dƒ|d¡\}t t|dƒ|d¡\}t d¡t  d ¡t  ||fd ¡t |¡S) NT)Úwait)rr)rr9ÚrÚkZ Iterationsr)zC-losszG-loss) rrrÚ clear_outputrrrrArrr)roZd_lossesZg_lossesrUÚline1Úline2rrrÚ display_loss£s    rycsNt‡fdd„t ˆ¡Dƒƒ}dd„|Dƒ}tj|dd}|jdddtdƒS) Ncs$g|]}| d¡rtj ˆ|¡‘qS)z.png)Úendswithrkrlrm)r4Úf)Ú folder_dirrrr6±sz'make_training_video..cSsg|]}t |¡ d¡‘qS)r9)ÚmpyÚ ImageClipÚ set_duration)r4r{rrrr6²sÚcompose)Úmethodz movie.mp4r)Úfps)ÚsortedrkÚlistdirr}Úconcatenate_videoclipsÚwrite_videofiler)r|ÚfilesÚframesÚclipr)r|rÚmake_training_video°s rŠ)rr)rkÚnumpyr.rerEÚmatplotlib.pyplotÚpyplotrÚmatplotlib.gridspecraÚmoviepy.editorÚeditorr}rÚIPython.displayrÚIPythonrZseabornrrr*rZrrryrŠrrrrÚs$      ,A