1.catloss.py
import visdom, sys
import numpy as np
def show_loss ( path, name, step= 1 ) :
with open ( path, "r" ) as f:
data = f. read( )
data = data. split( "\n" ) [ : - 1 ]
x = np. linspace( 1 , len ( data) + 1 , len ( data) ) * step
y = [ ]
for i in range ( len ( data) ) :
y. append( float ( data[ i] ) )
vis = visdom. Visdom( env= 'loss' )
vis. line( X= x, Y= y, win= name, opts= { 'title' : name, "xlabel" : "epoch" , "ylabel" : name} )
def compare2 ( path_1, path_2, title= "xxx" , legends= [ "a" , "b" ] , x= "epoch" , step= 20 ) :
with open ( path_1, "r" ) as f:
data_1 = f. read( )
data_1 = data_1. split( "\n" ) [ : - 1 ]
with open ( path_2, "r" ) as f:
data_2 = f. read( )
data_2 = data_2. split( "\n" ) [ : - 1 ]
x = np. linspace( 1 , len ( data_1) + 1 , len ( data_1) ) * step
y = [ ]
for i in range ( len ( data_1) ) :
y. append( [ float ( data_1[ i] ) , float ( data_2[ i] ) ] )
vis = visdom. Visdom( env= 'loss' )
vis. line( X= x, Y= y, win= "compare" ,
opts= { "title" : "compare " + title, "legend" : legends, "xlabel" : "epoch" , "ylabel" : title} )
if __name__ == "__main__" :
show_loss( sys. argv[ 1 ] , "loss" )
2.catpfm.py
import re, cv2, sys
import numpy as np
import matplotlib. pyplot as plt
""""
Pf
400 296
-1.000000
"""
def catPfm ( path) :
file = open ( path, 'rb' )
header = file . readline( ) . decode( 'UTF-8' ) . rstrip( )
if header == 'PF' :
color = True
elif header == 'Pf' :
color = False
else :
raise Exception( 'Not a PFM file.' )
dim_match = re. match ( r'^(\d+)\s(\d+)\s$' , file . readline( ) . decode( 'UTF-8' ) )
if dim_match:
width, height = map ( int , dim_match. groups( ) )
else :
raise Exception( 'Malformed PFM header.' )
scale = float ( ( file . readline( ) ) . decode( 'UTF-8' ) . rstrip( ) )
if scale < 0 :
data_type = '<f'
else :
data_type = '>f'
data_string = file . read( )
data = np. fromstring( data_string, data_type)
shape = ( height, width, 3 ) if color else ( height, width)
data = np. reshape( data, shape)
depth_image = cv2. flip( data, 0 )
ma = np. ma. masked_equal( depth_image, 0.0 , copy= False )
print ( 'value range: ' , ma. min ( ) , ma. max ( ) )
plt. title( path)
plt. imshow( depth_image, 'rainbow' )
plt. show( )
if __name__== "__main__" :
catPfm( sys. argv[ 1 ] )
3.subimg.py
import cv2, sys
import numpy as np
def subimg ( file1, file2) :
img1, img2= cv2. imread( file1) , cv2. imread( file2)
subimg= np. abs ( img1- img2)
print ( subimg. shape, subimg. min ( ) , subimg. max ( ) )
cv2. imshow( "subimg" , subimg)
cv2. waitKey( 0 )
cv2. destroyAllWindows( )
if __name__== "__main__" :
subimg( sys. argv[ 1 ] , sys. argv[ 2 ] )
4.convertpth.py
import torch
def convert_model ( para_model, single_model) :
checkpoint = torch. load( para_model , map_location= torch. device( "cpu" ) )
output= { }
for key, value in checkpoint[ 'model' ] . items( ) :
output[ key. lstrip( "module." ) ] = value
torch. save( { 'model' : output} , single_model)
if __name__== "__main__" :
convert_model( para_model= "model_2.pth" , single_model= "model.pth" )
5.clip.py
import time
import sys
import os
import re
sys. path. append( os. path. abspath( "SO_site-packages" ) )
import pyperclip
recent_value = ""
tmp_value = ""
while True :
tmp_value = pyperclip. paste( )
try :
if tmp_value != recent_value:
recent_value = tmp_value
changed = out = re. sub( r"\s{2,}" , " " , recent_value)
pyperclip. copy( changed)
print ( "\n Value changed: %s" % str ( changed) )
time. sleep( 0.1 )
except KeyboardInterrupt:
break
if tmp_value == 'getend' :
break
6.zipall.py
import zipfile, os
def zipall ( startdir, out_file) :
z = zipfile. ZipFile( out_file, 'w' , zipfile. ZIP_DEFLATED)
for dirpath, dirnames, filenames in os. walk( startdir) :
for filename in filenames:
z. write( os. path. join( dirpath, filename) )
z. close( )
print ( "complish" )
if __name__== "__main__" :
zipall( "synex" , "db.zip" )
7.movieclip.py
import sys
from moviepy. editor import *
def clip ( infile, outfile, start, end) :
video_tmp= VideoFileClip( infile) . subclip( t_start= start, t_end= end)
video_tmp. write_videofile( outfile)
if __name__== "__main__" :
if len ( sys. argv) == 5 :
clip( sys. argv[ 1 ] , sys. argv[ 2 ] , sys. argv[ 3 ] , sys. argv[ 4 ] )
print ( "clip complish!" )
else :
print ( "usage: python movie_clip.py infile_name outfile_name start_time,end_time" )