def init_repo(args={},debug=False,init=False): ''' initilize a repo as coolkit repo with default configuration if any parent repo is already initilized then we will just copy the config from there ''' cwd = abs_path(os.getcwd()) home_loc = abs_path('~') root_loc = abs_path('/') now = cwd while(now != home_loc and now != root_loc): if('.coolkit' in os.listdir(now) and os.path.isdir(os.path.join(now,'.coolkit'))): if(debug): print('got .coolkit at ',now) break now = abs_path(os.path.join(now,os.pardir)) if(now == root_loc): Colour.print('Coolkit should be run in path under home directory',Colour.RED) sys.exit(1) if(now == home_loc): SrbJson(cwd+'/.coolkit/config',srbjson.local_template) Colour.print('initialized empty CoolKit repository in '+cwd+'/.coolkit/',Colour.GREEN) elif(now != cwd): verify_folder(cwd+'/.coolkit/') shutil.copy(now+'/.coolkit/config',cwd+'/.coolkit/config') Colour.print('initialized empty CoolKit repository in '+cwd+'/.coolkit/',Colour.GREEN) else: if(init): Colour.print('Already a coolkit repo',Colour.YELLOW) if(not 'c_name' in args or not args['c_name']): contest_name = get_contest_name(cwd.split('/')[-1]) if(not contest_name): args['c_name'] = None else: args['c_name'] = contest_name SrbJson.dump_data(args,cwd+'/.coolkit/config',srbjson.local_template)
def __init__(self, code1, code2, tester_script, maxlim=10000, idd="0", timeout="10"): self.idd = '_' + str(idd) if os_name == 'windows': self.timeout = '' else: self.timeout = 'timeout ' + str(timeout) + 's ' code1 = abs_path(code1) code2 = abs_path(code2) tester_script = abs_path(tester_script) self.code1 = code1 self.code2 = code2 self.base1 = code1.split('/')[-1].split('\\')[-1].split('.')[0] self.base2 = code2.split('/')[-1].split('\\')[-1].split('.')[0] self.out1 = '.' + '-'.join( relative_path(code1).split('/')[1:]) + self.idd + '.tester' self.out2 = '.' + '-'.join( relative_path(code2).split('/')[1:]) + self.idd + '.tester' self.tester_script = tester_script self.maxlim = int(maxlim) if not Code_tester.check_exception(code1, code2, tester_script): sys.exit(0) self.exec1 = self.compile_code(self.code1) self.exec2 = self.compile_code(self.code2) self.exec3 = self.compile_code(self.tester_script)
def install_tester(): os_name = get_os_name() pwd = str(os.getcwd()) #always return without / at end; pwd = abs_path(pwd) binary_path = pwd + '/even_tester/binaries/' + os_name + '/even_validator' if (os_name == 'windows'): binary_path += '.exe' if (os_name == 'windows'): install_path = abs_path('~/programs', '\\') binary_path = abs_path(binary_path, '\\') user_path = os.environ['path'] + ';' if not install_path + ';' in user_path: print('adding to userpath') print(install_path) print(user_path) os.system('setx path "%path%;' + install_path + '"') else: install_path = abs_path('~/.local/bin') verify_folder(install_path) dependency_map = { 'even_validator': { 'ubuntu': 'cp ' + binary_path + ' ' + install_path, 'windows': 'copy "' + binary_path + '" "' + install_path + '"', }, } install_dependencies(dependency_map, verbose=True)
def get_true_bbox(img_path, base_xml_dir=abs_path(settings.imagenet_val_xml_path)): # parse the xml for bounding box coordinates temp_img = Image.open(img_path) sz = temp_img.size # width x height im_name = img_path.split('/')[-1].split('.')[0] tree = ET.parse(os.path.join(abs_path(base_xml_dir), f'{im_name}.xml')) root = tree.getroot() # Get Ground Truth ImageNet masks # temp_area = 0 gt_mask = np.zeros((sz[1], sz[0])) # because we want rox x col for iIdx, type_tag in enumerate(root.findall('object/bndbox')): xmin = int(type_tag[0].text) ymin = int(type_tag[1].text) xmax = int(type_tag[2].text) ymax = int(type_tag[3].text) # if (ymax - ymin)*(xmax - xmin) > temp_area: # temp_area = (ymax - ymin)*(xmax - xmin) gt_mask[ymin:ymax, xmin:xmax] = 1 gt = preprocess_gt_bb(gt_mask, 224) gt = (gt >= 0.5).astype(float) #binarize after resize return gt
def fetch_data_from_local_config(): Args._verify_init() cwd = abs_path(os.getcwd()) home_loc = abs_path('~') root_loc = abs_path('/') now = cwd while(now != home_loc and now!= root_loc): if('.coolkit' in os.listdir(now) and os.path.isdir(os.path.join(now,'.coolkit'))): break now = abs_path(os.path.join(now,os.pardir)) if(now == root_loc): Colour.print('Coolkit should be run in path under home directory',Colour.RED) sys.exit(1) return SrbJson(now+'/.coolkit/config',srbjson.local_template).data
def check_init(): ''' check if directory is initilized or not ''' cwd = abs_path(os.getcwd()) home_loc = abs_path('~') root_loc = abs_path('/') now = cwd while(now != home_loc and now != root_loc): if('.coolkit' in os.listdir(now) and os.path.isdir(os.path.join(now,'.coolkit'))): return now # path of .coolkit now = abs_path(os.path.join(now,os.pardir)) if(now == root_loc): Colour.print('Coolkit should be run in path under home directory',Colour.RED) sys.exit(1) return False
def __init__(self, data_path, transform, img_idxs=[0, 1], idx_flag=1, args=None): self.data_path = data_path self.transform = transform self.args = args if data_path == abs_path(settings.imagenet_val_path): aa = img_name_list[img_idxs[0]:img_idxs[1]] self.img_filenames = [ os.path.join(data_path, f'{ii}.JPEG') for ii in aa ] # self.img_filenames.sort() else: self.img_filenames = [] for file in glob.glob(os.path.join(data_path, "*.JPEG")): self.img_filenames.append(file) self.img_filenames.sort() if idx_flag == 1: print('Only prodicing results for 1 image') img_idxs = [0] self.img_filenames = [self.img_filenames[i] for i in img_idxs]
def get_init_path(): ''' set config to global config file. ''' cwd = abs_path(os.getcwd()) home_loc = abs_path('~') root_loc = abs_path('/') now = cwd while(now != home_loc and now != root_loc): if('.coolkit' in os.listdir(now) and os.path.isdir(os.path.join(now,'.coolkit'))): return now+'/.coolkit' now = abs_path(os.path.join(now,os.pardir)) if(now == root_loc): Colour.print('Coolkit should be run in path under home directory',Colour.RED) sys.exit(1) return None
def get_image_class(self, filepath): base_dir = abs_path('~/CS231n/heatmap_tests/') # ipdb.set_trace() # ImageNet 2012 validation set images? with open(os.path.join(settings.imagenet_class_mappings, "ground_truth_val2012")) as f: # with open(os.path.join(base_dir, "imagenet_class_mappings", "ground_truth_val2012")) as f: ground_truth_val2012 = {x.split()[0]: int(x.split()[1]) for x in f.readlines() if len(x.strip()) > 0} with open(os.path.join(settings.imagenet_class_mappings, "synset_id_to_class")) as f: # with open(os.path.join(base_dir, "imagenet_class_mappings", "synset_id_to_class")) as f: synset_to_class = {x.split()[1]: int(x.split()[0]) for x in f.readlines() if len(x.strip()) > 0} def get_class(f): # ipdb.set_trace() # File from ImageNet 2012 validation set ret = ground_truth_val2012.get(f, None) if ret is None: # File from ImageNet training sets ret = synset_to_class.get(f.split("_")[0], None) if ret is None: # Random JPEG file ret = 1000 return ret image_class = get_class(filepath.split('/')[-1]) return image_class
def export(self, path): from srblib import abs_path path = abs_path(path) writer = PyPDF2.PdfFileWriter() for doc, pnum in self._outpages: writer.addPage(doc.get_page(pnum)) with open(path, 'wb') as f: writer.write(f)
def _verified_file(file_path): if not file_path: return None file_path = abs_path(file_path) if not os.path.isfile(file_path): Colour.print('No such file : ' + Colour.END + file_path, Colour.RED) sys.exit() return file_path
class Const: cache_dir = abs_path('~/.config/coolkit') mult_soln_words = [ 'multiple possible', 'multiple answers', 'output any', 'any', 'multiple' ] def __init__(): pass
def write_data(data,fout): dump_data(data,'result/json/'+fout+'.json') out = "" rank = 1 for item in data: out+=str(rank)+" "+item.get_result()+"\n\n" rank+=1 fout = abs_path('result/text/' + fout + '.txt') verify_file(fout) open(fout,'w').write(out)
def data_to_excel(data, out_path): out_path = abs_path(out_path) book = xlwt.Workbook(encoding="utf-8") sheet = book.add_sheet("Sheet1") for i in range(len(data)): row = data[i] for j in range(len(row)): sheet.write(i, j, row[j]) book.save(out_path)
def os_cd(path): """ A short-cut for _`cd` like_ commands **Throws** - `Throws/Raises` if path does not exist """ abspath = srblib.abs_path(path) if os.path.isdir(abspath) is False: raise TypeError("No directory at {abspath}".format(abspath = abspath)) os.chdir(abspath)
def main(args): ## Debugging Mode if sys.gettrace() is not None: print('In debug mode') args.profiler = 'simple' args.num_workers = 0 args.pin_memory = False args.default_root_dir = mkdir_p(abs_path('./tmp_dir')) args.stochastic_weight_avg = True args.limit_train_batches = 0.001 args.limit_val_batches = 0.001 args.num_sanity_val_steps = 0 args.terminate_on_nan = True args.deterministic = True args.auto_select_gpus = False args.fast_dev_run = False # Quick Check Working args.progress_bar_refresh_rate = 0 args.gpus = 1 args.precision = 16 args.train_batch_size = 32 # args.val_batch_size=4 args.freeze_encoder = True args.verbose = True args.max_epochs = 2 ## Model dict_vars = vars(args) model = BertForRegression(**dict_vars) print(f'Model Init Done') ## TODO: Early Stopping Callback ## Callbacks checkpoint_callback = pl.callbacks.ModelCheckpoint( filename='{epoch:02d}-{val_loss:.5f}-{step_count}', dirpath=None, prefix="best_model_checkpoint", monitor="val_loss", mode="min", save_top_k=args.save_top_k, ) # lr_monitor = pl.callbacks.LearningRateMonitor(logging_interval='step') trainer = pl.Trainer.from_argparse_args( args, callbacks=[LoggingCallback(), checkpoint_callback], ) trainer.fit(model) best_model_path = trainer.checkpoint_callback.best_model_path print(f'\nBEST MODEL PATH IS {best_model_path}')
def set_local_config(args={},debug=False): ''' set config to config file. parent config if found creates init if not ''' if(not Args.check_init()): Args.init_repo() cwd = abs_path(os.getcwd()) home_loc = abs_path('~') root_loc = abs_path('/') now = cwd while(now != home_loc and now != root_loc): if('.coolkit' in os.listdir(now) and os.path.isdir(os.path.join(now,'.coolkit'))): if(debug): print('got .coolkit at ',now) break now = abs_path(os.path.join(now,os.pardir)) if(now == root_loc): Colour.print('Coolkit should be run in path under home directory',Colour.RED) sys.exit(1) SrbJson.dump_data(args,now+'/.coolkit/config',srbjson.local_template)
def mkdir_p(mypath): """Creates a directory. equivalent to using mkdir -p on the command line""" from errno import EEXIST from os import makedirs, path from srblib import abs_path mypath = abs_path(mypath) try: makedirs(mypath) except OSError as exc: # Python >2.5 if exc.errno == EEXIST and path.isdir(mypath): pass else: raise return mypath
def __init__(self, data_path, img_idxs=[0, 1], idx_flag=1): self.data_path = data_path if data_path == abs_path(settings.imagenet_val_path): aa = img_name_list[img_idxs[0]:img_idxs[1]] self.img_filenames = [ os.path.join(data_path, f'{ii}.JPEG') for ii in aa ] else: self.img_filenames = [] for file in glob.glob(os.path.join(data_path, "*.JPEG")): self.img_filenames.append(file) self.img_filenames.sort() self.img_filenames = self.img_filenames[:50] print( f'\nNo. of images to be analyzed are {len(self.img_filenames)}\n') if idx_flag == 1: print('Only prodicing results for 1 image') img_idxs = [0] self.img_filenames = [self.img_filenames[i] for i in img_idxs]
def excel_to_data(inp_path): inp_path = abs_path(inp_path) raw_data = pandas.read_excel(inp_path) header = list(raw_data.columns) if (len(header) == 0): return [] temp_data = [] for head in header: col = list(raw_data[head]) temp_data.append(col) data = [header] for i in range(len(temp_data[0])): row = [] for j in range(len(header)): row.append(temp_data[j][i]) data.append(row) return data
def test_view(): if not on_travis: return print() cwd = abs_path(os.getcwd()) try: loc = cwd+'/test/contests/837' os.chdir(loc) remove(loc+'/.coolkit') assert(os.system('coolkit init')==0) assert(os.system('coolkit view user srbcheema1')==0) assert(os.system('coolkit view contest')==0) assert(os.system('coolkit view prob A')==0) assert(os.system('coolkit view upcoming')==0) if(utils.do_online_test()): assert(os.system('coolkit view friends')==0) assert(os.system('coolkit view standings 935')==0) os.chdir(cwd) except Exception as e: remove(loc+'/.coolkit') os.chdir(cwd) raise e
import os import shutil from importlib.util import spec_from_file_location, module_from_spec from srblib import abs_path, verify_file from srblib import Module from .Constants import Const _path_of_default_global_config = '/'.join( abs_path(__file__).split('/')[:-2]) + '/extra/global_config.py' _global_config = Module(Const.cache_dir + '/global_config.py', _path_of_default_global_config) def get_contest_name(folder): return _global_config.get_contest_name(folder) def get_problem_name(file_name): return _global_config.get_problem_name(file_name)
# from robustness import model_utils, datasets from user_constants import DATA_PATH_DICT import settings import warnings warnings.filterwarnings("ignore") import utils as eutils use_cuda = torch.cuda.is_available() FloatTensor = torch.cuda.FloatTensor if use_cuda else torch.FloatTensor LongTensor = torch.cuda.LongTensor if use_cuda else torch.LongTensor Tensor = FloatTensor # os.environ.set("MAX_LEN_IDENTIFIER", 300) text_file = abs_path(settings.paper_img_txt_file) # text_file = f'/home/naman/CS231n/heatmap_tests/' \ # f'Madri/Madri_New/robustness_applications/img_name_files/' \ # f'time_15669152608009198_seed_0_' \ # f'common_correct_imgs_model_names_madry_ressnet50_googlenet.txt' img_name_list = [] with open(text_file, 'r') as f: for line in f: img_name_list.append(line.split('\n')[0]) ## For reproducebility torch.manual_seed(0) torch.backends.cudnn.deterministic = True torch.backends.cudnn.benchmark = False
def test_files_verifiers(): if(not on_travis): return verify_folder(abs_path('./hello/world')) assert(os.path.exists(abs_path('./hello')) == True) assert(os.path.exists(abs_path('./hello/world')) == True) remove(abs_path('./hello/world')) assert(os.path.exists(abs_path('./hello')) == True) assert(os.path.exists(abs_path('./hello/world')) == False) verify_file(abs_path('./hello/world/helloworld.py')) assert(os.path.exists(abs_path('./hello')) == True) assert(os.path.exists(abs_path('./hello/world')) == True) assert(os.path.exists(abs_path('./hello/world/helloworld.py')) == True) remove(abs_path('./hello')) assert(os.path.exists(abs_path('./hello')) == False) assert(os.path.exists(abs_path('./hello/world')) == False) assert(os.path.exists(abs_path('./hello/world/helloworld.py')) == False)
from srblib import abs_path import os data_dir = abs_path('./datasets') file_name = os.path.join(data_dir, 'Eluvio_DS_Challenge.csv') n_train_samples = 254618 n_val_samples = 127309 n_test_samples = 127309 # file_name = abs_path('./datasets/temp_data.csv') # n_train_samples = 14 # n_val_samples = 7 # n_test_samples = 7
import os from srblib import abs_path from srblib import SrbJson config_file_path = abs_path('~/.config/exam_scheduler/config.json') default_output_xlsx_path = abs_path('./output.xls') _default_room_list = None _default_teachers_list = None _default_schedule_list = None if os.path.exists(abs_path('input/')): for fname in os.listdir(abs_path('input/')): fname = 'input/' + fname if not _default_room_list and 'room_list' in fname: _default_room_list = abs_path(fname) elif not _default_teachers_list and 'teachers_list' in fname: _default_teachers_list = abs_path(fname) elif not _default_schedule_list and 'schedule_list' in fname: _default_schedule_list = abs_path(fname) for fname in os.listdir(abs_path('./')): if not _default_room_list and 'room_list' in fname: _default_room_list = abs_path(fname) elif not _default_teachers_list and 'teachers_list' in fname: _default_teachers_list = abs_path(fname) elif not _default_schedule_list and 'schedule_list' in fname: _default_schedule_list = abs_path(fname) _config_template = \
hog2 = hog(h2, pixels_per_cell=(16, 16)) out, _ = pearsonr(hog1, hog2) elif metric_name.lower() == 'spearman': out, _ = spearmanr(h1, h2, axis=None) else: print(f'Still not implemented.\nExiting') sys.exit(1) return out ######################################################################################################################## if __name__ == '__main__': base_img_dir = abs_path(settings.imagenet_val_path) # base_img_dir = '/home/naman/CS231n/heatmap_tests/images/ILSVRC2012_img_val' # text_file = f'/home/naman/CS231n/heatmap_tests/' \ # f'Madri/Madri_New/robustness_applications/img_name_files/' \ # f'time_15669152608009198_seed_0_' \ # f'common_correct_imgs_model_names_madry_ressnet50_googlenet.txt' s_time = time.time() f_time = ''.join(str(s_time).split('.')) args = get_arguments() im_label_map = eutils.imagenet_label_mappings() eutils.mkdir_p(args.out_path) img_filenames = os.listdir(args.input_dir_path) img_filenames = [ i for i in img_filenames if 'ILSVRC2012_val_000' in i and int(i.split('_')[-1]) in range(1, 50001)
def test_Args(): if not on_travis: return print() cwd = abs_path(os.getcwd()) try: loc = cwd+'/test/contests/837' os.chdir(loc) remove(loc+'/.coolkit') args = {} args['c_name'] = '837' args['c_type'] = 'contest' args['p_name'] = None args['c_site'] = 'codeforces' Args.init_repo(args) print(Colour.CYAN+'Content of confing file'+Colour.END) os.system('cat '+loc+'/.coolkit/config') print(Colour.CYAN+'trying to init it again'+Colour.END) Args.init_repo(args) print(Colour.CYAN+'setting problem name to A'+Colour.END) Args.set_local_config({'p_name':'A'}) print(Colour.CYAN+'Content of confing file'+Colour.END) os.system('cat '+loc+'/.coolkit/config') print(Colour.CYAN+'Deinitialize the folder'+Colour.END) remove(loc+'/.coolkit') print(Colour.CYAN+'Try to run the file'+Colour.END) os.system('coolkit run one.cpp') if(os.environ['USER'] == 'travis'): os.system('coolkit config --user coolkit --pswd coolkit') print(Colour.CYAN+'Try to submit wrong file'+Colour.END) os.system('coolkit submit three.cpp') if(utils.do_online_test()): print(Colour.CYAN+'Try to submit right file'+Colour.END) make_unique(loc) os.system('coolkit submit hidden_one.cpp') remove(loc+'/hidden_one.cpp') print(Colour.CYAN+'Deinitialize the folder'+Colour.END) remove(loc+'/.coolkit') remove('~/.config/coolkit/contest/222') print(Colour.CYAN+'Fetching some contest'+Colour.END) os.system('coolkit fetch -c 222 ') print(Colour.CYAN+'Fetching contest without force'+Colour.END) os.system('coolkit fetch -c 222') print(Colour.CYAN+'After making files defected'+Colour.END) os.remove(abs_path('~/.config/coolkit/contest/222/prob/A/io/Input1')) os.system('coolkit fetch -c 222') remove('~/.config/coolkit/contest/222') os.chdir(cwd) except Exception as e: # do clean up even in case of exception loc = cwd+'/test/contests/837' remove('~/.config/coolkit/contest/222') remove(loc+'/.coolkit') remove(loc+'/hidden_one.cpp') os.chdir(cwd) raise e
def get_arguments(): # Initialize the parser parser = argparse.ArgumentParser( description='Paramters for sensitivity analysis of heatmaps') parser.add_argument('-idp', '--input_dir_path', help='Path of the input directory', metavar='DIR') parser.add_argument( '-op', '--out_path', help= 'Path of the output directory where you want to save the text files (Default is ./)' ) parser.add_argument( '-mn', '--method_name', choices=['sg'], #['occlusion', 'ig', 'sg', 'grad', 'lime', 'mp', 'inpgrad'], help='Method you are analysing') parser.add_argument('--exp_num', choices=[ 'a30', 'a31', ], help='Which experiment of SmoothGrad') parser.add_argument('--metric_name', choices=['ssim', 'hog', 'spearman'], help='Metric to be computed') # parser.add_argument('--num_variations', type=int, # help='Number of variations for a particular method.') # parser.add_argument('--no_img_name_dir_flag', action='store_false', default=True, # help=f'Flag to say that image name is stored as seperate directory in the input path.' # f'Default=True') # # parser.add_argument('--no_model_name_dir_flag', action='store_false', default=True, # help=f'Flag to say that model name is stored as seperate directory in the input path. ' # f'Default=True') parser.add_argument( '--idx_flag', type=int, help=f'Flag whether to use some images in the folder (1) or all (0). ' f'This is just for testing purposes. ' f'Default=0', default=0, ) parser.add_argument( '-s_idx', '--start_idx', type=int, help='Start index for selecting images. Default: 0', default=0, ) parser.add_argument( '-e_idx', '--end_idx', type=int, help='End index for selecting images. Default: 2K', default=1735, ) # parser.add_argument('--if_random', action='store_true', default=False, # help=f'Flag to say you want to compute results for baseline' # f'Default=False') # Parse the arguments args = parser.parse_args() args.no_model_name_dir_flag = False args.if_random = False args.no_img_name_dir_flag = True # if args.if_random: # np.random.seed(0) # if args.num_variations is None: # print('Please provide this number.\nExiting') # sys.exit(0) # elif args.num_variations < 2: # print('This number cant be less than 2.\nExiting') # sys.exit(0) if args.method_name is None: print('Please provide the name of the method.\nExiting') sys.exit(0) if args.exp_num is None: print('Please provide the experiment number.\nExiting') sys.exit(0) if args.metric_name is None: print('Please provide the name of the metric.\nExiting') sys.exit(0) if args.input_dir_path is None: print('Please provide image dir path. Exiting') sys.exit(1) args.input_dir_path = abs_path(args.input_dir_path) if args.out_path is None: args.out_path = './' args.out_path = os.path.abspath(args.out_path) return args
## Testing loop tot_loss = 0 for l_idx, test_batch in enumerate(test_loader): ## Put data on device for key in test_batch.keys(): test_batch[key] = test_batch[key].to(torch_device) ## Compute the loss loss = model._step(test_batch).item() tot_loss += loss if verbose: print(f'Type: Test, ' f'BatchIdx: {l_idx+1: 3d}/{len(test_loader)}, ' f'Per_Step_Test_Loss: {loss: 8.3f}') avg_loss = tot_loss / len(test_loader) print(f'Avergae test loss is {avg_loss}') if __name__ == '__main__': # model_path = '/home/nzb0040/eluvio/lightning_logs/best_model_checkpoint-epoch=29-val_loss=290297.96875-step_count=0.ckpt' args = get_arguments() # import ipdb # ipdb.set_trace() main(model_path=abs_path(args.model_path), batch_sz=args.test_batch_size, num_workers=args.num_workers, pin_memory=args.pin_memory, verbose=args.verbose)