def create_datasets(param_list): file_content = open("compute_models.py").read() tree_init = ast.parse(file_content) generate_tac_code(tree_init) line_durations = profile([str(i) for i in param_list]) f = open('tac.py', 'r') src = f.read() tree = ast.parse(src) lhs_func = [ stmt for stmt in tree.body if isinstance(stmt, ast.FunctionDef) and stmt.name == 'lhs' ][0] rhs_func = [ stmt for stmt in tree.body if isinstance(stmt, ast.FunctionDef) and stmt.name == 'rhs' ][0] lhs_peg = compute_peg(lhs_func, line_durations) rhs_peg = compute_peg(rhs_func, line_durations) lhs_nodes_costs = [node.cost for node in get_nodes(lhs_peg.root)] rhs_nodes_costs = [node.cost for node in get_nodes(rhs_peg.root)] return lhs_nodes_costs, rhs_nodes_costs
def test_profile(): expected_distances = [0., 9166.70306504, 18329.92411276, 27489.65733578, 36645.89692202, 45798.63705469, 54947.87191227, 64093.59566856, 73235.80249261, 82374.48654874] expected_elevations = [280., 326., 228., 184., 241., 158., 224., 209., 190., 184.] expected_latitudes = [43.2, 43.2666, 43.3333, 43.4, 43.4666, 43.5333, 43.6, 43.6666, 43.7333, 43.8] expected_longitudes = [1.2, 1.2666, 1.3333, 1.4, 1.4666, 1.5333, 1.6, 1.6666, 1.7333, 1.8] expected_overheads = [0., 52.6953, 92.1818, 118.4743, 131.5879, 131.5375, 118.3384, 92.0056, 52.5544, 0.] expected_sights = [280., 269.3333, 258.6667, 248.0, 237.3333, 226.6667, 216.0, 205.3333, 194.6667, 184.] gdal.AllRegister() data_source = gdal.Open(DS_FILENAME, GA_ReadOnly) actual = profiler.profile(data_source, 43.2, 1.2, 43.8, 1.8, definition=10) for exp_d, act_d in zip(expected_distances, actual['distances']): assert abs(exp_d - act_d) <= EPSILON for exp_d, act_d in zip(expected_elevations, actual['elevations']): assert abs(exp_d - act_d) <= EPSILON for exp_d, act_d in zip(expected_latitudes, actual['latitudes']): assert abs(exp_d - act_d) <= EPSILON for exp_d, act_d in zip(expected_longitudes, actual['longitudes']): assert abs(exp_d - act_d) <= EPSILON for exp_d, act_d in zip(expected_overheads, actual['overheads']): assert abs(exp_d - act_d) <= EPSILON for exp_d, act_d in zip(expected_sights, actual['sights']): assert abs(exp_d - act_d) <= EPSILON
def serve_profile(self, lat1, long1, lat2, long2, content_type='application/json', profile_format=JSON, og1=None, os1=None, og2=None, os2=None): """ Generate and format a profile for the given parameters. :param lat1: latitude of the first point :param long1: longitude of the first point :param lat2: latitude of the second point :param long2: longitude of the second point :param content_type: response content-type to send, defaults to 'application/json' :param profile_format: profile format to use, defaults to profile_format.JSON :param og1: line of sight offset from the ground level of the first point :param os1: line of sight offset from the sea level of the first point :param og2: line of sight offset from the ground level of the second point :param os2: line of sight offset from the sea level of the second point :return: the formatted elevation profile between the two points """ if og1 is not None and os1 is not None: raise cherrypy.HTTPError( 400, "Incompatible parameters 'og1' and 'os1'") if og2 is not None and os2 is not None: raise cherrypy.HTTPError( 400, "Incompatible parameters 'og2' and 'os2'") kwargs = {} if os1 is not None: kwargs['height1'] = os1 kwargs['above_ground1'] = False elif og1 is not None: kwargs['height1'] = og1 kwargs['above_ground1'] = True if os2 is not None: kwargs['height2'] = os2 kwargs['above_ground2'] = False elif og2 is not None: kwargs['height2'] = og2 kwargs['above_ground2'] = True elevations = profiler.profile(self.data_source, float(lat1), float(long1), float(lat2), float(long2), **kwargs) cherrypy.response.headers['Content-Type'] = content_type return profile_format.get_data(elevations)
def main(): """Main entrypoint""" config = ConfigParser.ConfigParser() config.read('config.ini') config_dem_location = config.get('dem', 'location') args = parse_args() kwargs = {} if args.offset_sea1 is not None: kwargs['height1'] = args.offset_sea1 kwargs['above_ground1'] = False elif args.offset_ground1 is not None: kwargs['height1'] = args.offset_ground1 kwargs['above_ground1'] = True if args.offset_sea2 is not None: kwargs['height2'] = args.offset_sea2 kwargs['above_ground2'] = False elif args.offset_ground1 is not None: kwargs['height2'] = args.offset_ground2 kwargs['above_ground2'] = True LOGGER.debug("using the following DEM: %s", args.dem) LOGGER.debug("requesting profile for the following 'GPS' coordinates") LOGGER.debug("first wgs84 lat: %f, long: %f", args.lat1, args.long1) LOGGER.debug("second wgs84 lat: %f, long: %f", args.lat2, args.long2) # register all of the drivers gdal.AllRegister() # open the DEM dem_location = args.dem or config_dem_location data_source = gdal.Open(dem_location, GA_ReadOnly) profile_data = profiler.profile(data_source, args.lat1, args.long1, args.lat2, args.long2, **kwargs) if args.output_format == 'png': if args.style == 'detailed': output_format = profile_format.PNG_detailed elif args.style == 'curved_sight': output_format = profile_format.PNG_curved_sight else: output_format = profile_format.PNG default_filename = "profile.png" else: output_format = profile_format.JSON default_filename = "profile.json" if args.stdout: output_format.write_to_fd(profile_data, sys.stdout) else: filename = args.filename or default_filename output_format.write_to_filename(profile_data, filename)
def test_run(): # Calling training and profile memory usage profile_output["MODEL"] = "MNIST MLP" run_time, memory_usage = profile(train_model) profile_output['TRAINING_TIME'] = float(run_time) profile_output['MEM_CONSUMPTION'] = float(memory_usage) score = model.evaluate(X_test, Y_test, verbose=0) profile_output["TEST_ACCURACY"] = score[1] assert_results(MACHINE_TYPE, IS_GPU, GPU_NUM, profile_output, CPU_BENCHMARK_RESULTS, GPU_1_BENCHMARK_RESULTS, GPU_2_BENCHMARK_RESULTS, GPU_4_BENCHMARK_RESULTS, GPU_8_BENCHMARK_RESULTS)
def time_compare(function, iterattions): flag = 1 array_size = 500 best_case = worst_case = avg_case = 0 for i in range(iterattions): c = [0] * array_size c = [i for i in range(array_size, 0, -1)] # unsorted b = [0] * array_size b = [random.randint(1, array_size) for i in range(array_size, 0, -1)] #sorted if function.__name__ == 'quickSort': if flag == 1: a = [0] * array_size a = [ random.randint(1, array_size) for i in range(array_size, 0, -1) ] flag = 0 best_case += profiler.profile(function, a, 1) avg_case += profiler.profile(function, b, 1) worst_case += profiler.profile(function, c, 1) else: a = [0] * array_size a = [i + 1 for i in range(array_size)] best_case += profiler.profile(function, a, 1) avg_case += profiler.profile(function, b, 1) worst_case += profiler.profile(function, c, 1) print function.__name__, "\t\t ", best_case, "\t\t", avg_case, "\t\t", worst_case
def get_all_trained_models_info(models_path, use_profiler=False, device='gpu'): print('Testing all models in: {}'.format(models_path)) for model_name in sorted(os.listdir(models_path)): try: model_params = arcs.load_params(models_path, model_name, -1) train_time = model_params['total_time'] num_epochs = model_params['epochs'] architecture = model_params['architecture'] print(model_name) task = model_params['task'] print(task) net_type = model_params['network_type'] print(net_type) top1_test = model_params['test_top1_acc'] top1_train = model_params['train_top1_acc'] top5_test = model_params['test_top5_acc'] top5_train = model_params['train_top5_acc'] print('Top1 Test accuracy: {}'.format(top1_test[-1])) print('Top5 Test accuracy: {}'.format(top5_test[-1])) print('\nTop1 Train accuracy: {}'.format(top1_train[-1])) print('Top5 Train accuracy: {}'.format(top5_train[-1])) print('Training time: {}, in {} epochs'.format( train_time, num_epochs)) if use_profiler: model, _ = arcs.load_model(models_path, model_name, epoch=-1) model.to(device) input_size = model_params['input_size'] if architecture == 'dsn': total_ops, total_params = profile_dsn( model, input_size, device) print("#Ops (GOps): {}".format(total_ops)) print("#Params (mil): {}".format(total_params)) else: total_ops, total_params = profile(model, input_size, device) print("#Ops: %f GOps" % (total_ops / 1e9)) print("#Parameters: %f M" % (total_params / 1e6)) print('------------------------') except: print('FAIL: {}'.format(model_name)) continue
def test_profile(): expected_distances = [ 0., 9166.70306504, 18329.92411276, 27489.65733578, 36645.89692202, 45798.63705469, 54947.87191227, 64093.59566856, 73235.80249261, 82374.48654874 ] expected_elevations = [ 280., 326., 228., 184., 241., 158., 224., 209., 190., 184. ] expected_latitudes = [ 43.2, 43.2666, 43.3333, 43.4, 43.4666, 43.5333, 43.6, 43.6666, 43.7333, 43.8 ] expected_longitudes = [ 1.2, 1.2666, 1.3333, 1.4, 1.4666, 1.5333, 1.6, 1.6666, 1.7333, 1.8 ] expected_overheads = [ 0., 52.6953, 92.1818, 118.4743, 131.5879, 131.5375, 118.3384, 92.0056, 52.5544, 0. ] expected_sights = [ 280., 269.3333, 258.6667, 248.0, 237.3333, 226.6667, 216.0, 205.3333, 194.6667, 184. ] gdal.AllRegister() data_source = gdal.Open(DS_FILENAME, GA_ReadOnly) actual = profiler.profile(data_source, 43.2, 1.2, 43.8, 1.8, definition=10) for exp_d, act_d in zip(expected_distances, actual['distances']): assert abs(exp_d - act_d) <= EPSILON for exp_d, act_d in zip(expected_elevations, actual['elevations']): assert abs(exp_d - act_d) <= EPSILON for exp_d, act_d in zip(expected_latitudes, actual['latitudes']): assert abs(exp_d - act_d) <= EPSILON for exp_d, act_d in zip(expected_longitudes, actual['longitudes']): assert abs(exp_d - act_d) <= EPSILON for exp_d, act_d in zip(expected_overheads, actual['overheads']): assert abs(exp_d - act_d) <= EPSILON for exp_d, act_d in zip(expected_sights, actual['sights']): assert abs(exp_d - act_d) <= EPSILON
def serve_profile(self, lat1, long1, lat2, long2, content_type='application/json', profile_format=JSON, og1=None, os1=None, og2=None, os2=None): """ Generate and format a profile for the given parameters. :param lat1: latitude of the first point :param long1: longitude of the first point :param lat2: latitude of the second point :param long2: longitude of the second point :param content_type: response content-type to send, defaults to 'application/json' :param profile_format: profile format to use, defaults to profile_format.JSON :param og1: line of sight offset from the ground level of the first point :param os1: line of sight offset from the sea level of the first point :param og2: line of sight offset from the ground level of the second point :param os2: line of sight offset from the sea level of the second point :return: the formatted elevation profile between the two points """ if og1 is not None and os1 is not None: raise cherrypy.HTTPError(400, "Incompatible parameters 'og1' and 'os1'") if og2 is not None and os2 is not None: raise cherrypy.HTTPError(400, "Incompatible parameters 'og2' and 'os2'") kwargs = {} if os1 is not None: kwargs['height1'] = os1 kwargs['above_ground1'] = False elif og1 is not None: kwargs['height1'] = og1 kwargs['above_ground1'] = True if os2 is not None: kwargs['height2'] = os2 kwargs['above_ground2'] = False elif og2 is not None: kwargs['height2'] = og2 kwargs['above_ground2'] = True elevations = profiler.profile(self.data_source, float(lat1), float(long1), float(lat2), float(long2), **kwargs) cherrypy.response.headers['Content-Type'] = content_type return profile_format.get_data(elevations)
# -*- coding: utf-8 -*- __author__ = 'gchlebus' import tensorflow as tf import numpy as np from u_net import UNet, GradientType from profiler import profile, OutputType import pprint import os if __name__ == '__main__': ret = {} for gradientType in GradientType: ret[gradientType.value] = profile(gradient_type=gradientType, disable_optimizer=False) pprint.pprint(ret)
face_recognizer_cheap = cv2.face.FisherFaceRecognizer_create() face_recognizer_cheap.read(RECOGNIZER_MODEL_CHEAP) frc_wrapper = Wrapper('cheap', face_recognizer_cheap) f_recognize_faces = MFAFunction(function=recognize_faces, params={ 'face_recognizer': { fre_wrapper: 10, frm_wrapper: 5, frc_wrapper: 3 } }) #====================================== IO =====================================# testset = sorted(glob.glob(TESTING_SIMULATOR_FOLDER + '*.png')) pipeline_inputs = [] for img_path in testset: img = cv2.imread(img_path, cv2.IMREAD_COLOR) pipeline_inputs.append( MFAIO(io_id=img_path[img_path.rfind(os.sep) + 1:], io_format='png', io_size=os.path.getsize(img_path), io_value=img)) #=================================== SIMULATE ==================================# # The first function takes the input from pipeline_inputs, the next ones, take # as input the output of the previous function, i.e.: f_recognize_faces(f_detect_faces(p_input)). # The output of the last function will be save to results file. pipeline = [f_detect_faces, f_recognize_faces] profile('pc_nec', pipeline, pipeline_inputs, 5)
# python 4.pstats.py # pip install gprof2dot # brew/yum/apt install graphviz # gprof2dot -f pstats output.pstats | dot -Tpng -o output.png from profiler import profile import cStringIO from collections import OrderedDict import pexpect PROMPTS = OrderedDict({ pexpect.EOF: None, }) with profile(): child = pexpect.spawn('cat', ['large']) child.logfile_read = cStringIO.StringIO() while child.isalive(): index = child.expect(PROMPTS.keys()) answer = PROMPTS.values()[index] if answer is not None: child.sendline(answer)
model.add(Dense(512, input_shape=(784,))) model.add(Activation('relu')) model.add(Dropout(0.2)) model.add(Dense(512)) model.add(Activation('relu')) model.add(Dropout(0.2)) model.add(Dense(10)) model.add(Activation('softmax')) model.summary() make_model(model, loss='categorical_crossentropy', optimizer=SGD(), metrics=['accuracy']) def train_model(): history = model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1, validation_data=(X_test, Y_test)) profile_output['TRAIN_ACCURACY'] = history.history['acc'][-1] # Calling training and profile memory usage profile_output["MODEL"] = "MNIST MLP" run_time, memory_usage = profile(train_model) profile_output['TRAINING_TIME'] = float(run_time) profile_output['MEM_CONSUMPTION'] = float(memory_usage) score = model.evaluate(X_test, Y_test, verbose=0) profile_output["TEST_ACCURACY"] = score[1] assert_results(MACHINE_TYPE, IS_GPU, GPU_NUM, profile_output, CPU_BENCHMARK_RESULTS, GPU_1_BENCHMARK_RESULTS, GPU_2_BENCHMARK_RESULTS, GPU_4_BENCHMARK_RESULTS, GPU_8_BENCHMARK_RESULTS)
else: print 'Excecution aborted' return # Patch view to enable profiling view_function = getattr(views_module, view_name) # Construct GET request, attach user to it rf = client.RequestFactory() get_request = rf.get(url) get_request.user = user # Render response with profiling for profiler in profilers: response = profile(view_function, args=[get_request] + view_args, profiler=profiler, log_file=output, limit=limit) print '[OK] Profiled ``%s.%s``, response status code: %s\n' % \ (prefix, view_name, response.status_code) # Clean db queries list connection.queries = [] # Print SQL queries full_args = [get_request] + view_args response = view_function(*full_args) sqlprinting_middleware = SqlPrintingMiddleware() sqlprinting_middleware.process_response({}, response) # Clean db queries list
def botresponse(message): print(message) if(message["type"] == "conversationUpdate"): if(message["membersAdded"][0]["name"] == "Bot"): print("") elif message["membersAdded"][0]["name"] != "Bot": name = message["from"]["name"] ReplyToActivity(fill=message, text="Welcome, " + name).send() ReplyToActivity(fill=message, text="I am LifeBot. I can give healthy recommendations.").send() elif(matcher(message["text"]) == True): user_id = message["from"]["id"] interest = message["text"] sententenceclass = sentenceClass(interest) storeInterestwithClass(user_id, "personal", sententenceclass, Trimmer(interest)) ReplyToActivity(fill=message, text='Great, I have made a note of that.').send() elif message["text"] == "None": offset =3 info = chathistory.DataStore() lastchats = info.get_lastnumberofchats(offset) intentId = lastchats[0]["_id"] print(intentId) print(lastchats) sentence = lastchats[0]["activity"]["text"] if(sentence == "None"): offset = offset + 2 lastchats = info.get_lastnumberofchats(offset) print('Fetching {0} messages'.format(offset)) print(lastchats) sentence = lastchats[0]["activity"]["text"] sententenceclass = sentenceClass(sentence) responder(sententenceclass, message) elif message["type"] == "message": botreply = sendResponse(message["text"]) sententenceclass = sentenceClass(message["text"]) input = message["text"] print('The input is {0}'.format(input)) user_id = message["from"]["id"] if sententenceclass == "food": if(wordCounter(message["text"])>=3): if(suggestPersonal("personal",sententenceclass, user_id) != None): jsonToPython = json.loads(suggestPersonal("personal",sententenceclass, user_id)) ReplyToActivity(fill=message, text="Which of the following would you consider ?", inputHint="acceptingInput", suggestedActions=jsonToPython).send() else: jsonToPython = json.loads(suggest('food')) ReplyToActivity(fill=message, text="Which of the following would you consider ?", inputHint="acceptingInput", suggestedActions=jsonToPython).send() else: guide(sententenceclass, message) elif sententenceclass == "breakfast": if(wordCounter(message["text"])>=3): if(suggestPersonal("personal",sententenceclass, user_id)): jsonToPython = json.loads(suggestPersonal("personal",sententenceclass, user_id)) else: jsonToPython = json.loads(foodTypesSuggest('breakfast')) ReplyToActivity(fill=message, text="Which of the following would you consider ?", \ inputHint="acceptingInput", suggestedActions=jsonToPython).send() else: guide(sententenceclass, message) elif sententenceclass == "lunch": if(wordCounter(message["text"])>=3): jsonToPython = json.loads(foodTypesSuggest('lunch')) ReplyToActivity(fill=message, text="Which of the following would you consider ?", inputHint="acceptingInput", suggestedActions=jsonToPython).send() else: guide(sententenceclass, message) elif sententenceclass == "dinner": if(wordCounter(message["text"])>=3): jsonToPython = json.loads(foodTypesSuggest('dinner')) ReplyToActivity(fill=message, text="Which of the following would you consider ?", inputHint="acceptingInput", suggestedActions=jsonToPython).send() else: guide(sententenceclass, message) elif sententenceclass == "drink": if(wordCounter(message["text"])>=3): jsonToPython = json.loads(suggest('drinks')) ReplyToActivity(fill=message, text="Which of the following would you consider ?", inputHint="acceptingInput", suggestedActions=jsonToPython).send() else: guide(sententenceclass, message) elif sententenceclass == "exercise": if(wordCounter(message["text"])>=3): jsonToPython = json.loads(suggest('exercise')) ReplyToActivity(fill=message, text="Which of the following would you consider ?", inputHint="acceptingInput", suggestedActions=jsonToPython).send() else: guide(sententenceclass, message) elif sententenceclass == "blood-sugar": sentence = message["text"] if(wordCounter(sentence)>=3): if("status" in sentence or "profile" in sentence): result_pre = lastPremealBloodResult("./app/blood_sugar_five.json") result_after = lastAfterMealBloodResult("./app/blood_sugar_five.json") status = profile(result_pre, result_after) templateStatus = "Your last blood result is {0} mg/dL, which mean your status is {1} ".format(result_pre, status) ReplyToActivity(fill=message, text=templateStatus).send() elif("lastest" in sentence or "result" in sentence or "five" in sentence): before = averageBloodSugarin5DaysBeforeMeal("./app/blood_sugar_five.json") after = averageBloodSugarin5DaysAfterMeal("./app/blood_sugar_five.json") templateBefore = "Your average pre meal blood sugar in the last 5 days is {0} mg/dL".format(before) ReplyToActivity(fill=message, text=templateBefore).send() templateAfter = "Your average post meal blood sugar in the last 5 days is {0} mg/dL".format(after) ReplyToActivity(fill=message, text=templateAfter).send() else: before = averageBloodSugarin5DaysBeforeMeal("./app/blood_sugar_five.json") after = averageBloodSugarin5DaysAfterMeal("./app/blood_sugar_five.json") template = "Your average blood sugar in the last 5 days is Fasting {0} mg/dL, random: {1} mg/dL ".format(before, after) ReplyToActivity(fill=message, text=template).send() elif sententenceclass == "goodbye": name = message["from"]["name"] jsonToPython = None messagetosend = botreply + ", " + name ReplyToActivity(fill=message, text=messagetosend, inputHint="acceptingInput", suggestedActions=jsonToPython).send() else: jsonToPython = None ReplyToActivity(fill=message, text=botreply, inputHint="acceptingInput", suggestedActions=jsonToPython).send()
def run(self, desired_result, input, limit): result = None """ Compile and run a given configuration then return performance """ # Read the configuration to run cfg = desired_result.configuration.data if (not cfg): # no ranges sys.exit(0) return # Translate the configuration into a string representation cfgStr = self.getCfgStr(cfg) self.configurationsExplored += 1 sys.stderr.write('AUTOTUNER: Configuration ' + cfgStr + '\n') configurations, pathToFiles = self.readConfigurations() if (cfgStr in configurations): # The configuration has been executed. Return the previous results result = configurations[cfgStr] sys.stderr.write( 'AUTOTUNER: This configuration has already been evaluated: ' + str(result) + '\n') time.sleep(5) else: crashedFlag = False sys.stderr.write('AUTOTUNER: Configuration ' + str(self.configurationsRun) + ' to test (explored by the autotuner ' + str(self.configurationsExplored - 1) + ')\n') self.configurationsRun += 1 # Generate the binary didWeCompileSuccesfully = self.myCompile(cfg, 0) # Check if the compiler succeded if didWeCompileSuccesfully == 0: # Invoke the profiler to test the current configuration try: # Define the output directory pathToOutputDir = self.getInfoStr() pathToOutputDir += '/' + str(self.configurationsExplored) # Run result = profiler.profile(pathToOutputDir) except KeyboardInterrupt: #raise result = self.getResultForCrash() sys.exit(1) except Exception as e: sys.stderr.write('AUTOTUNER: ERROR = ' + str(e) + '\n') traceback.print_exc() result = self.getResultForCrash() self.writeConfigurations( cfgStr, result, os.path.join(pathToFiles, self.crashedConfigurationsFileName)) crashedFlag = True # The configuration run. Compute the output distortion sys.stderr.write('AUTOTUNER: Binary run\n') outputDistortionFlag = bool( int(os.environ['OUTPUT_DISTORTION_FLAG'])) isDistorted = False if (('outputDistortion' in result) and (outputDistortionFlag)): inputName = os.environ['INPUT_NAME'] isDistorted = self.outputDistortionModule.isDistorted( inputName, result['outputDistortion']) if (isDistorted): sys.stderr.write( 'AUTOTUNER: The output is distorted\n' ) result['experimentTime'] = float('inf') # Dump the results if ((not crashedFlag) and (not isDistorted)): self.writeConfigurations( cfgStr, result, os.path.join(pathToFiles, self.configurationsFileName)) self.createConfigurationFile( pathToOutputDir + '/' + self.configurationFileName, cfgStr) # Print results sys.stderr.write('AUTOTUNER: Time = ' + str(result['experimentTime']) + '\n') else: # The compiler failed sys.stderr.write('AUTOTUNER: The compiler failed\n') result = self.getResultForCrash() return Result(time=result['experimentTime'])
def create_cube(n): for x in range(0, n): yield x + 3 for a in (create_cube(100000)): a += a finish_1 = datetime.datetime.now() print('Time taken by generator : ' + str(finish_1 - start_1)) def without_generator(): start_2 = datetime.datetime.now() def normal_create_cube(n): arr = [] for x in range(0, n): arr.append(x + 3) return arr for a in (normal_create_cube(100000)): a += a finish_2 = datetime.datetime.now() print('Time taken without generator : ' + str(finish_2 - start_2)) run_profiling1 = profile(with_generator) run_profiling2 = profile(without_generator) run_profiling1() run_profiling2()
def train(run_name, start_epoch, stop_epoch, img_w): # Input Parameters img_h = 64 words_per_epoch = 16000 val_split = 0.2 val_words = int(words_per_epoch * (val_split)) # Network parameters conv_num_filters = 16 filter_size = 3 pool_size = 2 time_dense_size = 32 rnn_size = 512 if K.image_dim_ordering() == 'th': input_shape = (1, img_w, img_h) else: input_shape = (img_w, img_h, 1) fdir = os.path.dirname( get_file('wordlists.tgz', origin='http://www.isosemi.com/datasets/wordlists.tgz', untar=True)) img_gen = TextImageGenerator( monogram_file=os.path.join(fdir, 'wordlist_mono_clean.txt'), bigram_file=os.path.join(fdir, 'wordlist_bi_clean.txt'), minibatch_size=32, img_w=img_w, img_h=img_h, downsample_factor=(pool_size**2), val_split=words_per_epoch - val_words) act = 'relu' input_data = Input(name='the_input', shape=input_shape, dtype='float32') inner = Convolution2D(conv_num_filters, filter_size, filter_size, border_mode='same', activation=act, init='he_normal', name='conv1')(input_data) inner = MaxPooling2D(pool_size=(pool_size, pool_size), name='max1')(inner) inner = Convolution2D(conv_num_filters, filter_size, filter_size, border_mode='same', activation=act, init='he_normal', name='conv2')(inner) inner = MaxPooling2D(pool_size=(pool_size, pool_size), name='max2')(inner) conv_to_rnn_dims = (img_w // (pool_size**2), (img_h // (pool_size**2)) * conv_num_filters) inner = Reshape(target_shape=conv_to_rnn_dims, name='reshape')(inner) # cuts down input size going into RNN: inner = Dense(time_dense_size, activation=act, name='dense1')(inner) # Two layers of bidirecitonal GRUs # GRU seems to work as well, if not better than LSTM: gru_1 = GRU(rnn_size, return_sequences=True, init='he_normal', name='gru1')(inner) gru_1b = GRU(rnn_size, return_sequences=True, go_backwards=True, init='he_normal', name='gru1_b')(inner) gru1_merged = merge([gru_1, gru_1b], mode='sum') gru_2 = GRU(rnn_size, return_sequences=True, init='he_normal', name='gru2')(gru1_merged) gru_2b = GRU(rnn_size, return_sequences=True, go_backwards=True, init='he_normal', name='gru2_b')(gru1_merged) # transforms RNN output to character activations: inner = Dense(img_gen.get_output_size(), init='he_normal', name='dense2')(merge([gru_2, gru_2b], mode='concat')) y_pred = Activation('softmax', name='softmax')(inner) Model(input=[input_data], output=y_pred).summary() labels = Input(name='the_labels', shape=[img_gen.absolute_max_string_len], dtype='float32') input_length = Input(name='input_length', shape=[1], dtype='int64') label_length = Input(name='label_length', shape=[1], dtype='int64') # Keras doesn't currently support loss funcs with extra parameters # so CTC loss is implemented in a lambda layer loss_out = Lambda(ctc_lambda_func, output_shape=(1, ), name='ctc')([y_pred, labels, input_length, label_length]) # clipnorm seems to speeds up convergence sgd = SGD(lr=0.02, decay=1e-6, momentum=0.9, nesterov=True, clipnorm=5) model = Model(input=[input_data, labels, input_length, label_length], output=[loss_out]) # the loss calc occurs elsewhere, so use a dummy lambda func for the loss model = make_model(model, loss={ 'ctc': lambda y_true, y_pred: y_pred }, optimizer=sgd) if start_epoch > 0: weight_file = os.path.join( OUTPUT_DIR, os.path.join(run_name, 'weights%02d.h5' % (start_epoch - 1))) model.load_weights(weight_file) # captures output of softmax so we can decode the output during visualization test_func = K.function([input_data], [y_pred]) viz_cb = VizCallback(run_name, test_func, img_gen.next_val()) def train_func(): history = model.fit_generator(generator=img_gen.next_train(), samples_per_epoch=(words_per_epoch - val_words), nb_epoch=stop_epoch, validation_data=img_gen.next_val(), nb_val_samples=val_words, callbacks=[viz_cb, img_gen], initial_epoch=start_epoch) ret_dict["training_accuracy"] = history.history['acc'][-1] ret_dict["test_accuracy"] = history.history['val_acc'][-1] ret = profile(train_func) ret_dict["training_time"] = str(ret[0]) + ' sec' ret_dict["max_memory"] = str(ret[1]) + ' MB'
def early_exit_experiments(models_path, device='cpu'): sdn_training_type = 'ic_only' # IC-only training #sdn_training_type = 'sdn_training' # SDN training # task = 'cifar10' # task = 'cifar100' task = 'tinyimagenet' #sdn_names = ['vgg16bn_sdn', 'resnet56_sdn', 'wideresnet32_4_sdn', 'mobilenet_sdn']; add_trigger = False sdn_names = ['vgg16bn_sdn']; add_trigger = False sdn_names = [task + '_' + sdn_name + '_' + sdn_training_type for sdn_name in sdn_names] for sdn_name in sdn_names: cnn_name = sdn_name.replace('sdn', 'cnn') cnn_name = cnn_name.replace('_ic_only', '') cnn_name = cnn_name.replace('_sdn_training', '') print(sdn_name) print(cnn_name) sdn_model, sdn_params = arcs.load_model(models_path, sdn_name, epoch=-1) sdn_model.to(device) dataset = af.get_dataset(sdn_params['task']) cnn_model, _ = arcs.load_model(models_path, cnn_name, epoch=-1) cnn_model.to(device) print('Get CNN results') top1_test, top5_test, total_time = mf.cnn_test_time(cnn_model, dataset.test_loader, device) total_ops, total_params = profile(cnn_model, cnn_model.input_size, device) print("#Ops: %f GOps"%(total_ops/1e9)) print("#Parameters: %f M"%(total_params/1e6)) print('Top1 Test accuracy: {}'.format(top1_test)) #print('Top5 Test accuracy: {}'.format(top5_test)) print('25 percent cost: {}'.format((total_ops/1e9)*0.25)) print('50 percent cost: {}'.format((total_ops/1e9)*0.5)) print('75 percent cost: {}'.format((total_ops/1e9)*0.75)) # to test early-exits with the SDN one_batch_dataset = af.get_dataset(sdn_params['task'], 1) print('Get SDN early exit results') total_ops, total_params = profile_sdn(sdn_model, sdn_model.input_size, device) print("#Ops (GOps): {}".format(total_ops)) print("#Params (mil): {}".format(total_params)) top1_test, top5_test = mf.sdn_test(sdn_model, dataset.test_loader, device) print('Top1 Test accuracy: {}'.format(top1_test)) #print('Top5 Test accuracy: {}'.format(top5_test)) print('Calibrate confidence_thresholds') confidence_thresholds = [0.1, 0.15, 0.25, 0.5, 0.6, 0.7, 0.8, 0.9, 0.95, 0.99, 0.999] # search for the confidence threshold for early exits sdn_model.forward = sdn_model.early_exit for threshold in confidence_thresholds: print(threshold) sdn_model.confidence_threshold = threshold # change the forward func for sdn to forward with cascade top1_test, top5_test, early_exit_counts, non_conf_exit_counts, total_time = mf.sdn_test_early_exits(sdn_model, one_batch_dataset.test_loader, device) average_mult_ops = 0 total_num_instances = 0 for output_id, output_count in enumerate(early_exit_counts): average_mult_ops += output_count*total_ops[output_id] total_num_instances += output_count for output_count in non_conf_exit_counts: total_num_instances += output_count average_mult_ops += output_count*total_ops[output_id] average_mult_ops /= total_num_instances print('Early exit Counts:') print(early_exit_counts) print('Non confident exit counts:') print(non_conf_exit_counts) print('Top1 Test accuracy: {}'.format(top1_test)) print('Top5 Test accuracy: {}'.format(top5_test)) print('SDN cascading took {} seconds.'.format(total_time)) print('Average Mult-Ops: {}'.format(average_mult_ops))
model.add(RNN(EMBED_HIDDEN_SIZE, return_sequences=False)) model.add(Dropout(0.3)) model.add(Dense(vocab_size, activation='softmax')) model = make_model(model, optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) print('Training') def train_func(): model.fit([X, Xq], Y, batch_size=BATCH_SIZE, nb_epoch=EPOCHS, validation_split=0.05) ret = profile(train_func) ret_dict["training_time"] = str(ret[0]) + ' sec' ret_dict["max_memory"] = str(ret[1]) + ' MB' ret_dict["training_accuracy"] = model.evaluate([X, Xq], Y, batch_size=BATCH_SIZE)[1] loss, acc = model.evaluate([tX, tXq], tY, batch_size=BATCH_SIZE) ret_dict["test_accuracy"] = acc print('Test loss / test accuracy = {:.4f} / {:.4f}'.format(loss, acc))
num_steps = len(train_dataloader) * Params.num_epochs cerwer = CerWer(Params.data_root) if Params.wandb_log: wandb.init(project=Params.wandb_name) start_epoch = Params.start_epoch + 1 if Params.from_pretrained else 1 best_cer = 10.0 for epoch in range(start_epoch, Params.num_epochs + 1): train_cer, train_wer, val_wer, val_cer = 0.0, 0.0, 0.0, 0.0 train_losses = [] model.train() with profiler.profile() as prof: for idx, sample in enumerate(train_dataloader): sample = to_gpu(sample, device) outputs, extra = model(**sample["net_input"]) outputs = outputs.permute(0, 2, 1) optimizer.optimizer.zero_grad() loss = criterion(outputs, sample["targets"]).cpu() loss.backward() torch.nn.utils.clip_grad_norm_(model.parameters(), Params.clip_grad_norm) optimizer.step() train_losses.append(loss.item()) _, max_probs = torch.max(outputs, 1) ( train_epoch_cer, train_epoch_wer,