def remove_false_positives(img, bounding_boxes, history): from visualization import Visualization # get the average of heatmaps from history heat = np.mean(history, axis=0) if len(history) > 0 else np.zeros_like( img[:, :, 0]).astype(np.float) # Add heat to each box in box list heat = Helper.add_heat(heat, bounding_boxes) # Get binary heat map heatmap = Helper.get_heatmap(heat) # Find final boxes from heatmap using label function labels = label(heatmap) # update heatmap history history.append(heatmap) # show box where label is 1 detected_cars = Helper.draw_updated_boxes(np.copy(img), labels) # save heatmaps if config["save_debug_samples"] is True: Visualization.save_heat_map(heat) return detected_cars
def test_invalid_args_visualization_empty_subplot(self): """`visualization.Visualization.empty_subplot`: Argument Validator. Tests the behavior of `Visualization.empty_subplot` with invalid argument counts and values. Raises: Exception: If at least one `Exception` raised is not of the expected kind. """ v = Visualization("Title", (1, 1)) """Visualization: Plotter instance.""" with self.assertRaises(TypeError): # No arguments. v._empty_subplot() with self.assertRaises(TypeError): # Too many arguments. v._empty_subplot(0, 0) with self.assertRaises(TypeError): # Non-integer index `i`. v._empty_subplot(None) v.close()
def draw_boxes(img, boxes, color=(0, 0, 0), thick=3): from visualization import Visualization img_with_boxes = np.copy(img) x_start_stop_left, y_start_stop_left = config["xy_start_stop_left"] x_start_stop_top, y_start_stop_top = config["xy_start_stop_top"] x_start_stop_right, y_start_stop_right = config["xy_start_stop_right"] cv.rectangle(img_with_boxes, (x_start_stop_right[0], y_start_stop_right[0]), (x_start_stop_right[1], y_start_stop_right[1]), (0, 1, 0), thick) cv.rectangle(img_with_boxes, (x_start_stop_left[0], y_start_stop_left[0]), (x_start_stop_left[1], y_start_stop_left[1]), (0, 1, 0), thick) cv.rectangle(img_with_boxes, (x_start_stop_top[0], y_start_stop_top[0]), (x_start_stop_top[1], y_start_stop_top[1]), (0, 1, 0), thick) for box in boxes: cv.rectangle(img_with_boxes, box[0], box[1], color, thick) Visualization.save_region(img_with_boxes) cv.imshow("boxes: ", img_with_boxes) cv.waitKey(1) return img_with_boxes
def __init__(self, configpath): self.config = js.load(open(configpath)) self.vz = Visualization(self.config["language"]) self.n = None #Number of simulated rolls self.i = None #Number of iterations self.ai = None #Active iteration self.ms = None #Match size in rolls self.discardtieds = None #Decide if tied rolls are part of a match self.starttime = None self.sname = None self.rollresult = [] self.simulationdata = [] self.summarydata = None self.dicefaces = ["rock", "paper", "scissors"] self.cdice = [-1, -1, -1, -1, -1, -1] self.pdice = [-1, -1, -1, -1, -1, -1] PPP.setConfig(self, self.config) print() print(self) PPP.run(self) PPP.exportRawData(self) self.summarydata = PPP.buildSummary(self) self.vz.autoCharts(self.sname, self.config["cPath"], self.cdice, self.pdice, self.simulationdata, self.summarydata) PPP.exportSummary(self)
def test_edge_cases_visualization_plot_feature(self): """`visualization.Visualization.plot_feature`: Edge Case Validator. Tests the behavior of `Visualization.plot_feature` with edge cases. Raises: Exception: If at least one `Exception` raised is not of the expected kind. """ X = _random_matrix(self.data_shape) """np.matrix: Random-valued feature set.""" Y = _random_matrix((self.data_shape[0], 1)) """np.matrix: Random-valued observation set.""" v = Visualization("Title", (1, 1)) """Visualization: Plotter instance.""" for ModelWrapper in self.wrappers.values(): with self.assertRaises(_InvalidFeatureSetError): # Empty feature set. v._plot_feature(_np.matrix([[]]), Y, 0, ModelWrapper) with self.assertRaises(_InvalidObservationSetError): # Empty observation set. v._plot_feature(X, _np.matrix([[]]), 0, ModelWrapper) with self.assertRaises(IndexError): # Feature index out of range. v._plot_feature(X, Y, self.data_shape[1], ModelWrapper) v.close()
def __init__(self, world_size, world_delta, obs_percent, agent_vision_depth, phase, rewards=10., penalty=-10., barrier_mode=0): """ Desc: runs when instance of Visualization class is created Input(s): world_size: size of box world (width,height) world_delta: interval of world obs_percent: percent of spaces that are occupied agent_vision_depth: raidus of square within agent can see obstacles Optional- rewards: reward for getting to goal penalty: penalty for being in obstacle Output(s): none """ self.world_size = world_size self.world_delta = world_delta # used on virtualization self.obs_percent = obs_percent self.agent_vision_depth = agent_vision_depth self.spots = self.generate_spots() # initialize gridworld elements self.obstacle_indices, self.obstacles = self.generate_obstacles(penalty) self.goals = [self.generate_goal(rewards)] self.agents = [self.generate_agent(0, phase)] # # initialize visualization self.visualization = Visualization(self)
def train(T_obs, T_pred, file_names, epoch_size=50): print("-------------------------------------------") writer = SummaryWriter("./log/loss") vis = Visualization() device = torch.device("cuda:0" if torch.cuda.is_available() else 'cpu') network = model.SocialLstm(hidden_dim=128, mediate_dim=32, output_dim=2) network.to(device) #network = torch.load("./log/model/559_model.pt") #network.eval() #network.to(device) criterion = torch.nn.MSELoss(reduction="sum") optimizer = torch.optim.Adam(network.parameters(), weight_decay=0.0005) loss = 0. for epoch in range(epoch_size): cost_sum = 0. cost_cnt = 0 for file_name in file_names: file_data = dataset.FrameDataset(file_name) print(file_name) for (idx, data) in enumerate(file_data): h = torch.zeros(data["ped_trajs"].shape[1], 128, device=device) c = torch.zeros(data["ped_trajs"].shape[1], 128, device=device) optimizer.zero_grad() with torch.autograd.set_detect_anomaly(True): Y = data["ped_trajs"][:T_pred, :, 1:].clone() trajs = data["ped_trajs"][:T_pred, :, 1:].clone() traj_masks = data["ped_masks"] # forward propagation output = network.forward(trajs, traj_masks, h, c, Y, T_obs, T_pred) # loss Y_pred = output[T_obs + 1:T_pred] Y_g = Y[T_obs + 1:T_pred] cost = criterion(Y_pred, Y_g) cost_sum += cost.item() cost_cnt += 1 if cost_cnt % 50 == 0: vis.plot( Y[:T_pred, :, :].clone().detach().cpu().tolist(), output[:T_pred, :, :].clone().detach().cpu( ).tolist(), T_obs, T_pred) # backward propagation cost.backward() optimizer.step() loss = cost_sum / cost_cnt print("epoch: ", epoch, "loss: ", loss) writer.add_scalar("Loss/train", loss) torch.save(network, "./log/model/" + str(int(loss * 100)) + "_model.pt") writer.close()
def test_random_visualization_plot_feature(self): """`visualization.Visualization.plot_feature`: Randomized Validator. Tests the behavior of `Visualization.plot_feature` by feeding it randomly generated arguments. Raises: AssertionError: If `Visualization.plot_feature` needs debugging. """ for i in range(self.n_tests): for ModelWrapper in self.wrappers.values(): X = _random_matrix(self.data_shape) """np.matrix: Random-valued feature set.""" Y = _random_matrix((self.data_shape[0], 1)) """np.matrix: Random-valued observation set.""" v = Visualization("Title", (3, 3)) """Visualization: Plotter instance.""" # Intialize model parameters to random values. ModelWrapper.model = dict(X=X) for i in range(9): x, y, error = v._plot_feature(X, Y, i, ModelWrapper) """(list of float, :obj:`list of float`): X- and y-values to plot.""" # `x` should be a list of floats. self.assertIsInstance(x, list) map(_appendargs(self.assertIsInstance, float), x) # Number of `x` values should match number of data points in # `X`. self.assertEqual(len(x), X.shape[0]) # `x` values should match all values in `X`. self.assertEqual(*map(_np.linalg.norm, [x, X[:, i]])) # `y` should be a dict. self.assertIsInstance(y, dict) for j, values in _compose(enumerate, y.values)(): # `values` should be a list of floats. self.assertIsInstance(values, list) map(_appendargs(self.assertIsInstance, float), values) # Number of values in `values` should match number of # data points in `Y`. self.assertEqual(len(values), Y.shape[0]) if j == 0: # Observation values should match all values in `Y`. self.assertEqual( *map(_np.linalg.norm, [values, Y[:, 0]])) v.close()
def score_result(reducer_function, data, x_scaled, ini, dimensions, label_data, title): knn = Knn() score = [] valor_k = range(ini, dimensions) for k in valor_k: new_data = reducer_function(data, x_scaled, k) score.append(knn.avg(new_data, label_data)) Visualization.hit_rate_per_k(valor_k, score, title)
def main(): print "Reading landmark positions" landmarks = read_world("../map/landmarks_sim.dat") print "Reading sensor data" sensor_readings = read_sensor_data("../map/sensor_data_car.dat") print "Reading Ground truth Odometry" odom_readings = read_odom("../map/odom_trajectory_car.dat") # initialize the particles map_limits = [0, 320, 0, 350] particles = initialize_particles(args.particles, map_limits) curr_pose_x = [] curr_pose_y = [] cov_noise = np.array([[0.01, 0, 0],[0,0.01,0],[0,0,0.01]]) vis = Visualization(landmarks, map_limits, sensor_readings, odom_readings) for timestep in range(len(sensor_readings) / 2): # plot_state(particles, landmarks, map_limits) # curr_mean = mean_pose(particles) # curr_pose_x.append(curr_mean[0]) # curr_pose_y.append(curr_mean[1]) # plot_trajectories_v3( sensor_readings[timestep, 'sensor'], curr_mean ,landmarks, map_limits) new_particles = sample_motion_model(sensor_readings[timestep, 'odometry'], particles) curr_mean = mean_pose(new_particles) curr_pose_x.append(curr_mean[0]) curr_pose_y.append(curr_mean[1]) vis.robot_environment(timestep, new_particles, curr_mean) # predict particles by sampling from motion model with odometry info # if timestep==0: # new_particles =particles # errors = data_association(sensor_readings[timestep, 'sensor'], new_particles, particles, landmarks, args.DA, cov_noise, sensor_readings[timestep, 'odometry']) # else: # errors = data_association(sensor_readings[timestep, 'sensor'], new_particles, particles, landmarks, args.DA, cov_noise, sensor_readings[timestep, 'odometry']) weights = eval_sensor_model(sensor_readings[timestep, 'sensor'], new_particles, landmarks) # weights = weighting_model(errors) particles = resample_particles(new_particles, weights) print("Current TimeStep: ", timestep) raw_input("Press Enter to continue...") plot_trajectories(odom_readings, curr_pose_x,curr_pose_y ,landmarks, map_limits) plot_on_maps(curr_pose_x, curr_pose_y) plt.show('hold')
def tsneVisualization(self): # Init the widget self.visualization = Visualization(self.config) # t-SNE features tSNE_features = getTSNEFeatures(self.bovwTrainingFeatures) self.visualization.show() self.visualization.updateNodes(tSNE_features, labels=self.labelTrainingArray) # self.visualization.graph_widget.fitInView() self.visualization.exec_()
def run_trial(self, trial_num, fullscreen=True): """ Runs the visualization for the specified file number and returns the visualization object. """ folder = os.path.join(self.folder, self.trials[trial_num - 1]) + os.path.sep vis = Visualization(folder) vis.load_data() vis.run(fullscreen, inverse_speed=0.25) return vis
def run_visualization(self,fullscreen=True,inverse_speed=.25): ''' Displays the visualization if an outputfolder exist and the simulation has been run. ''' if self.run: window = Visualization(self.folder) window.load_data() window.run(fullscreen,inverse_speed) else: print("The visualization cannot be started becase simulation has not run.")
def __init__(self, src_vocab, tgt_vocab, max_len=300, hidden_size=300, n_layers=2, clip=5, n_epochs=30): # hyper-parameters self.max_len = max_len self.hidden_size = hidden_size self.n_layers = n_layers self.clip = clip self.n_epochs = n_epochs # vocab self.src_vocab = src_vocab self.tgt_vocab = tgt_vocab self.pad_idx = self.src_vocab.stoi[PAD] # prepare model self.encoder = Encoder(self.src_vocab, self.max_len, self.hidden_size, self.n_layers) self.decoder = Decoder(self.tgt_vocab, self.max_len, self.hidden_size * 2, self.n_layers) self.reverse_decoder = Decoder(self.tgt_vocab, self.max_len, self.hidden_size * 2, self.n_layers, reverse=True) self.model = Seq2SeqConcat(self.encoder, self.decoder, self.reverse_decoder, self.pad_idx) self.model.to(device) print(self.model) print("Total parameters:", sum([p.nelement() for p in self.model.parameters()])) # initialize weights for name, param in self.model.named_parameters(): if "lstm.bias" in name: # set lstm forget gate to 1 (Jozefowicz et al., 2015) n = param.size(0) param.data[n//4:n//2].fill_(1.0) elif "lstm.weight" in name: nn.init.xavier_uniform_(param) # prepare loss function; don't calculate loss on PAD tokens self.criterion = nn.NLLLoss(ignore_index=self.pad_idx) # prepare optimizer and scheduler self.optimizer = Adam(self.model.parameters()) self.scheduler = CyclicLR(self.optimizer, base_lr=0.00001, max_lr=0.00005, step_size_up=4000, step_size_down=4000, mode="triangular", gamma=1.0, cycle_momentum=False) # book keeping vars self.global_iter = 0 self.global_numel = [] self.global_loss = [] self.global_acc = [] # visualization self.vis_loss = Visualization(env_name="aivivn_tone", xlabel="step", ylabel="loss", title="loss (mean per 300 steps)") self.vis_acc = Visualization(env_name="aivivn_tone", xlabel="step", ylabel="acc", title="training accuracy (mean per 300 steps)")
def test_random_visualization_init(self): """`visualization.Visualization.init`: Randomized Validator. Tests the behavior of `Visualization.init` by feeding it randomly generated arguments. Raises: AssertionError: If `Visualization.init` needs debugging. """ v = Visualization("Title", (1, 1)) """Visualization: Plotter instance.""" # Axes should be a list of length 1. self.assertIsInstance(v._ax, list) self.assertEqual(len(v._ax), 1) v.close() combinations = [(2, 3), (3, 2), (6, 1), (1, 6)] """list of (int, int): Possible subplot layouts.""" for i in range(5): k = _np.random.randint(0, len(combinations)) """int: Subplot layout index.""" v = Visualization("Title", combinations[k]) # Axes should be a list of length 1. self.assertIsInstance(v._ax, list) self.assertEqual(len(v._ax), 6) v.close()
def test_random_evolution(self): np.random.seed(0) params = {"min": 0, "max": 5, "dim": 1} original_ind = Individual( np.random.uniform(params["min"], params["max"], params["dim"])) results = [] labels = [1] epochs = 50 for _ in labels: evo = REvolution(original_ind=original_ind, combine_params=0.1, mutate_params={ "min": 0, "max": 5 }, fitness=fitness, pop_params={ "min": 0, "max": 5, "dim": 1 }, method="compare") evo.run_random(epochs) results.append(evo.result) for _ in labels: evo = REvolution(original_ind=original_ind, combine_params=0.1, mutate_params={ "std": 0.5, "dim": 1, "min": 0, "max": 5 }, fitness=fitness, pop_params={ "min": 0, "max": 5, "dim": 1 }, method="compare") evo.run_1_1(epochs) results.append(evo.result) v = Visualization() v.visualize(labels + labels, results, epochs) self.assertEqual(True, True)
def visualize(self, type=None, value=None, tile_provider=None, world_view=True): if not type: if self.get_num_dimensions() <= 3: Visualization(self.cube, self.element).show_cube() else: Visualization(self.cube, self.element).show_table() elif type == "table": Visualization(self.cube, self.element).show_table() elif type == "map": Visualization(self.cube, self.element).show_map() elif type == "map_html": return map_html(self, value, tile_provider, world_view)
def demonstrate_wrong_predictions(self): from dir_processing import DirProcessing from visualization import Visualization for i in range(len(self.predict_labels)): test_label = self.test_labels[i] predict_label = self.predict_labels[i] if test_label != predict_label: test_expression = Classifier.get_expression_from_label(test_label) predict_expression = Classifier.get_expression_from_label(predict_label) print "Wrong prediction: {}, Right prediction: {}.".format(predict_expression, test_expression) landmarks_url = self.test_urls[i] img_url = DirProcessing.generate_img_url_from_landmarks_url(landmarks_url) Visualization.draw_landmarks_on_img(img_url)
def result(): error = None script = '' div = {} if request.method == 'POST': result = request.form for key, val in result.items(): if key == 'prod': prod = val elif key == 'timeWindow': time = val print(type(time)) if time == '' or prod == '': # checks to see if user filled everything in error = 'Please fill in all fields.' script = ' ' div = {} cheapest_dates = "" elif time.isdigit() and int(time) >= 80 and int( time ) <= 3650: #checks to see if input is a number and in the proper range if prod == 'Oil' and time != '': myinterpreter = Interpreter('oil_prices', int(time)) myinterpreter.differencing() myinterpreter.create_acf() myinterpreter.get_p_and_q() myinterpreter.build_model() data = myinterpreter.get_data_source() visualization = Visualization(data) plot = visualization.get_graph2() cheapest_dates = visualization.find_lowest_prices() script, div = components(plot) elif prod == 'Electricity' and time != '': myinterpreter = Interpreter('avg_elec_price', int(time)) myinterpreter.differencing() #myinterpreter.test_stationarity() myinterpreter.create_acf() myinterpreter.get_p_and_q() myinterpreter.build_model() data = myinterpreter.get_data_source() visualization = Visualization(data) cheapest_dates = visualization.find_lowest_prices() plot = visualization.get_graph2() script, div = components(plot) else: error = "Please type your specified time period as a number between 80 and 3650." script = ' ' div = {} cheapest_dates = "" return render_template("result.html", prod=prod, time=time, error=error, script=script, div=div, cheapest_dates=cheapest_dates)
def main(): number_of_pages = 10000 if len(sys.argv[1:]): number_of_pages = int(sys.argv[1]) client = gRPCClient() page_count = client.method2(number_of_pages).count res = client.query_stream(queryPages(page_count, number_of_pages)) count = 0 for re in res: viz = Visualization(tile_number=count, tile_width=100, tile_height=100, data_list=list(zip(re.page_names, re.page_count))) viz.visualize_data() count += 1
def save_to_visualize(measurements): ''' Method to save and visualize data (in order to avoid repetitions) ''' for m in measurements: data, filename, title, xlabel, ylabel = m[0], m[1], m[2], m[3], m[4] scenarios = ['High', 'Low', 'EW', 'NS'] Visualization.save_data_and_plot_multiple_curves( list_of_data=[[data[i] for i in range(len(data)) if i % 4 == 0], [data[i] for i in range(len(data)) if i % 4 == 1], [data[i] for i in range(len(data)) if i % 4 == 2], [data[i] for i in range(len(data)) if i % 4 == 3]], filename=filename, title=title, xlabel=xlabel, ylabel=ylabel, scenarios=scenarios)
def random_show_cluster(landmarks_urls_list, label, cluster_index): from dir_processing import DirProcessing from file_op import FileOp from visualization import Visualization sample_list = np.where(label == cluster_index)[0] sample_list = np.random.permutation(sample_list) print len(sample_list) landmarks_urls_sublist = [] for i in sample_list: landmarks_urls_sublist.append(landmarks_urls_list[i]) img_urls_sublist = DirProcessing.generate_img_urls_from_landmarks_urls(landmarks_urls_sublist) Visualization.draw_landmarks_on_sequence(img_urls_sublist)
def runSimulation(self): ''' Initializes the visualization and runs the simulation. ''' print 'running simulation' # Set proper delay between simulations: if self.stepDelay < 0: if self.space.step > 60*60*24 and self.space.step/(60*60*24) < 50: self.stepDelay = int(self.space.step/(60*60*24)*10) elif self.space.step/(60*60*24) >= 50: self.stepDelay = 500 else: self.stepDelay = 0 # visualization: vis = Visualization(self) vis.initialize() # initialize visualization vis.visualize() # main visualization/simulation loop
def start(self): self.controllers.append(self.test_controller) # this will block until self.finished == True if self.enable_visualization: self.thread = Thread(None, self.run, "World").start() self.sim_visualization = Visualization(self) self.sim_visualization.run() else: self.run()
def run(self, alg, bean_list, filename="img.png", weighted=False): accuracy = [] #self.set_train_test_set(generator,bean_list) for k in self.k_values: for train in self.train_set: # Train alg.train([bean_list[a] for a in train]) for test in self.test_set: # Test result = alg.teste([bean_list[a] for a in test], k=k) # Result accuracy.append(Knn.accuracy(result)) print("acuracy knn {0} with k {1}".format(accuracy[-1], k)) self.mean_accuracy(accuracy) # Plot the graphic Visualization.hit_rate_per_k(self.mean_accuracy_list, self.k_values, filename, weighted)
def test_invalid_args_visualization_init(self): """`visualization.Visualization.init`: Argument Validator. Tests the behavior of `Visualization.init` with invalid argument counts and values. Raises: Exception: If at least one `Exception` raised is not of the expected kind. """ with self.assertRaises(TypeError): # No arguments. Visualization() with self.assertRaises(TypeError): # Only one argument. Visualization("Title") with self.assertRaises(TypeError): # Invalid kwarg. Visualization("Title", (3, 3), key="value") with self.assertRaises(TypeError): # `None` instead of `subplots`. Visualization("Title", None) with self.assertRaises(TypeError): # Integer for `subplots`. Visualization("Title", 3) with self.assertRaises(TypeError): # Floats instead of `subplots` integers. Visualization("Title", (1.2, 3.4)) with self.assertRaises(ValueError): # Negative integers instead of `subplots`. Visualization("Title", (-3, -3)) with self.assertRaises(IndexError): # Zero integers instead of row number. Visualization("Title", (0, 3)) with self.assertRaises(IndexError): # Zero integers instead of column number. Visualization("Title", (3, 0))
def test_density(self): """Test whether the calculation of probability density from state is correct. """ proba_calculated = Visualization(test_data())\ .density(n_grid=101)\ .astype(np.float16) proba_expected = np.load("example_proba.npz")['arr_0'] np.testing.assert_array_almost_equal(proba_calculated, proba_expected, decimal=3)
def main(): print(Welcome.WELCOME) #Init visualization v = Visualization() #Ask the data to load data_file = input(Data.Q_DATA_2_LOAD) #Save the data file to the preprocessing class pp = Preprocessing(data_file) #Ask if the user wants to see the raw data v.show_raw_information(pp.raw) #Ask if the user wants to see les raw data time pp.decrease_time_channels() #Ask and apply a notch filter if required if (pp.notch_filter() == Notch_filter.APPLY_NOTCH_FILTER): #If the user has applied the filter, we ask if wants to see the results v.plot_data(pp.raw) #Ask and apply a bandpass filter if required bandpass_filter(pp) #Ask and apply an ica filter if required if (pp.ica_filter(v) == ICA_filter.APPLY_ICA_FILTER): print("TODO: Crec que s'ha de treure")
def main(): if len(sys.argv) < 2: print("Behavioral Cloning\n", "Usage: python3 main.py config.json") else: config_file = sys.argv[1] with open(config_file) as yaml_file: # The FullLoader parameter handles the conversion from YAML # scalar values to Python the dictionary format configs = yaml.load(yaml_file, Loader=yaml.FullLoader) # Data configurations data_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), configs["data_path"]) labels_file = configs["labels_file"] skip_header = bool(configs["skipHeader"]) use_side_images = bool(configs["useSideImages"]) correction_factor = float(configs["correctionFactor"]) train_valid_split = float(configs["trainValidSplit"]) do_flip = bool(configs["doFlip"]) # Training configurations top_crop = int(configs["topCrop"]) bottom_crop = int(configs["bottomCrop"]) batch_size = int(configs["batchSize"]) epochs = int(configs["epochs"]) loss = configs["loss"] optimizer = configs["optimizer"] verbose = int(configs["verbose"]) model_name = configs["modelName"] output_dir = configs["outputDir"] # Init Preprocessing preprocess = PreprocessData(data_path, labels_file, correction_factor, skip_header, use_side_images, train_valid_split, do_flip) # Preprocess data and extract training and validation samples train_samples, validation_samples = preprocess.splitData() # Initialize train and validation generators train_generator = preprocess.generator(train_samples, batch_size=batch_size) validation_generator = preprocess.generator(validation_samples, batch_size=batch_size) # Get image shape img_shape = preprocess.get_image_shape() # Initialize training network network = Model(img_shape, top_crop, bottom_crop, batch_size, epochs, loss, optimizer, verbose, train_generator, validation_generator, train_samples, validation_samples, model_name) model = network.create_model() # Initialize visualization visualize = Visualization(model, output_dir) visualize.visualize_model() network.get_summary(model) results = network.train_model(model) visualize.save_plots(results)
def main(): config = configparser.ConfigParser() config.read(r'config.ini') cluster_count = int(config['KMEANS']['ClusterCount']) file = open( config['DATASET']['InputDirectory'] + config['DATASET']['InputFile'], 'r') fdata = np.genfromtxt(file, dtype=float, delimiter='\t') mapreduce_inputfile_name = config['HADOOP']['mapreduce_inputfile_name'] streaming_jar = config['HADOOP']['StreamingJar'] mapper = config['HADOOP']['mapper'] reducer = config['HADOOP']['reducer'] hdfs_input_dir = config['HADOOP']['hdfs_input_directory'] hdfs_output_dir = config['HADOOP']['hdfs_output_directory'] tmp_dir = config['HADOOP']['temporary_directory'] mrkm = MapReduceKMeans(fdata.data[:, 2:], fdata.data[:, 1], cluster_count, mapreduce_inputfile_name, streaming_jar, mapper, reducer, hdfs_input_dir, hdfs_output_dir, tmp_dir) if config['KMEANS']['Random'] == 'True': mrkm.initial_random_centroids(config['DEFAULT']['ClusterCount']) else: indices = config['KMEANS']['Centroids'] mrkm.initial_centroids([int(i) for i in indices.split(',')]) mrkm.kmeans() ei = ExternalIndex(mrkm.ground_truth_clusters, mrkm.clusters) print('Rand Index : ', ei.rand_index()) print('Jaccard Coefficient : ', ei.jaccard_coefficient()) visual = Visualization(mrkm.data, mrkm.clusters, mrkm.ground_truth_clusters) visual.plot('demo.jpg') return
def test_edge_cases_visualization_init(self): """`visualization.Visualization.init`: Edge Case Validator. Tests the behavior of `Visualization.init` with edge cases. Raises: Exception: If at least one `Exception` raised is not of the expected kind. """ with self.assertRaises(IndexError): # No subplots. Visualization("Title", (0, 0))
def display_alch_profit(): ge = OSRS_GE_Data() #This doesn't work right if the traded quantity is 0 alchfunc = lambda x: ge.ge_json[x][ 'sp'] * 0.6 - ge.nature_rune - ge.ge_json[x][ 'overall_average'] if ge.ge_json[x]['overall_average' ] > 0 else -sys.maxsize - 1 lf = alchfunc jl = ge.sort_json(ge.ge_json, lf, True) small_list = ge.get_top_values(10, jl) sorted_list = [] for id in small_list: #TODO: This should be parameterized and maybe made into an insert function temp_entry = ge.get_entry(id) temp_entry['alch_diff'] = lf(id) print(temp_entry) sorted_list.append(temp_entry) '''header = sorted_list[0].keys() rows = [x.values() for x in sorted_list] print(tabulate.tabulate(rows, header))''' vis = Visualization() vis.display(sorted_list, x='name', y='alch_diff')
def pipeline(img, lanes_fit, camera_matrix, dist_coef): # debug flag is_debug_enabled = True # checkbox dimensions for calibration nx, ny, channels = 9, 6, 3 # calibrate camera and undistort the image undistorted_image = PreProcessing.get_undistorted_image( nx, ny, img, camera_matrix, dist_coef) # get the color and gradient threshold image binary_image = PreProcessing.get_binary_image(undistorted_image) # get source and destination points src, dst = PerspectiveTransform.get_perspective_points(img) # get image with source and destination points drawn img_src, img_dst = PerspectiveTransform.get_sample_wrapped_images( img, src, dst) # perspective transform to bird eye view warped_image = PerspectiveTransform.get_wrapped_image( binary_image, src, dst) # find the lanes lines and polynomial fit if len(lanes_fit) == 0: lane_lines, lanes_fit, left_xy, right_xy = LanesFitting.get_lanes_fit( warped_image) else: lane_lines, lanes_fit, left_xy, right_xy = LanesFitting.update_lanes_fit( warped_image, lanes_fit) # find the radius of curvature radius = Metrics.get_curvature_radius(lane_lines, left_xy, right_xy) # find the car distance from center lane center_distance, lane_width = Metrics.get_distance_from_center( lane_lines, lanes_fit) # unwrap the image resultant_img = PerspectiveTransform.get_unwrapped_image( undistorted_image, warped_image, src, dst, lanes_fit) # visualize the pipeline if is_debug_enabled is True: resultant_img = Visualization.visualize_pipeline( resultant_img, img_dst, binary_image, lane_lines, radius, center_distance, lane_width) return lanes_fit, resultant_img
def test_edge_cases_visualization_subplot(self): """`visualization.Visualization.subplot`: Edge Case Validator. Tests the behavior of `Visualization.subplot` with edge cases. Raises: Exception: If at least one `Exception` raised is not of the expected kind. """ x = _compose(list, _np.random.uniform)(0.0, 100.0, size=self.data_shape[0]) """list of float: Random x-values.""" values = { name: _compose(list, _np.random.uniform)(0.0, 100.0, size=self.data_shape[0]) \ for name in ["observations", "predictions"] } """:obj:`list of float`: Contains all y-values.""" v = Visualization("Title", (1, 1)) """Visualization: Plotter instance.""" for ModelWrapper in self.wrappers.values(): with self.assertRaises(IndexError): # Suplot index out of range. v._subplot(1, x, values) with self.assertRaises(TypeError): # Empty `x`-values. v._subplot([], values, 0) with self.assertRaises(TypeError): # Empty `y`-values. v._subplot(x, {}, 0) v.close()
def test(self): v = Visualization() l = v.file_path_get() data = v.data_get(l[1])[0] print('data======', data) v.show(data) data = SF.trans_float_list(data) data = SF.data_format([data]) self.run(data)
def __init__(self, hps, device): self.tool = Tool(hps.sens_num, hps.sen_len, hps.key_len, hps.topic_slots, 0.0) self.tool.load_dic(hps.vocab_path, hps.ivocab_path) vocab_size = self.tool.get_vocab_size() print("vocabulary size: %d" % (vocab_size)) PAD_ID = self.tool.get_PAD_ID() B_ID = self.tool.get_B_ID() assert vocab_size > 0 and PAD_ID >= 0 and B_ID >= 0 self.hps = hps._replace(vocab_size=vocab_size, pad_idx=PAD_ID, bos_idx=B_ID) self.device = device # load model model = WorkingMemoryModel(self.hps, device) # load trained model utils.restore_checkpoint(self.hps.model_dir, device, model) self.model = model.to(device) self.model.eval() null_idxes = self.tool.load_function_tokens(self.hps.data_dir + "fchars.txt").to( self.device) self.model.set_null_idxes(null_idxes) self.model.set_tau(hps.min_tau) # load poetry filter print("loading poetry filter...") self.filter = PoetryFilter(self.tool.get_vocab(), self.tool.get_ivocab(), self.hps.data_dir) self.visual_tool = Visualization(hps.topic_slots, hps.his_mem_slots, "../log/") print("--------------------------")
from visualization import Visualization one_thousand = 'C:\\SAP 2000\\Jul-08\\14_53_17\\' four_robots = "C:\\SAP 2000\\Jul-08\\17_55_09\\" ten_robots = 'C:\\SAP 2000\\Jul-08\\18_09_43\\' to_test = 'C:\\SAP 2000\\Aug-15\\19_39_21\\' vis = Visualization(to_test) vis.load_data() vis.run(False,inverse_speed=.1)
import socket import time, datetime from visualization import Visualization from config import Config dest = (Config.config.get("general","host"), Config.config.getint("general","port")) width = Config.config.getint("general","width") height = Config.config.getint("general","height") sleeptime = Config.config.getfloat("general","sleeptime") visualization = Visualization(width,height) sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, width*height*3+1); while True: visualization.draw() sock.sendto(chr(0) + visualization.pixels, dest) time.sleep(sleeptime) if visualization.frame % 100 == 0: print "{:0.2f} FPS".format(visualization.fps())
def addVar(self, name): widget = Visualization(name) widget.setWindowTitle(name) self.addSubWindow(widget) widget.show()
class Simulation: width = 100 height = 100 amount_food = 100 num_worms = 100 start_health = 100 smell_range = 10 controllers = [] finished = False sim_world = None slomo = 0 enable_visualization = False test_controller = GreedyController() def assign_controllers(self, generation): for worm in generation.worms: self.controllers[0].control_worm(worm) def stop_controllers(self): for controller in self.controllers: controller.stop() def stop(self): self.sim_world.stop() def start(self): self.controllers.append(self.test_controller) # this will block until self.finished == True if self.enable_visualization: self.thread = Thread(None, self.run, "World").start() self.sim_visualization = Visualization(self) self.sim_visualization.run() else: self.run() def initialize_generation(self): # a new world self.sim_world = World(self.width, self.height, self.amount_food) # a generation to live in it self.sim_generation = Generation(self.num_worms, self.sim_world, self.start_health, self.smell_range) # some controllers that control the worms self.assign_controllers(self.sim_generation) def run(self): # TODO implement generation iteration self.initialize_generation() self.run_world() print("Bye") self.stop_controllers() self.finished = True def run_world(self): while True: try: if self.slomo: sleep(self.slomo) self.sim_world.step() except: print("ERRORRRR") if self.sim_world.alive_count == 0: break print("WormWorld just became very silent") self.sim_world.stop()
def get_svm_importances(coef): """Normalize the SVM weights.""" factor = 1.0 / np.linalg.norm(coef) return (coef * factor).ravel() if __name__ == "__main__": svm = LinearSVC(C=0.1) category = "Faces" dataset = "all" datamanager = CaltechManager() datamanager.PATHS["RESULTS"] = os.path.join(datamanager.PATHS["BASE"], "results_Faces_LinearSVC_normalized") vcd = VisualConceptDetection(svm, datamanager) clf = vcd.load_object("Classifier", category) importances = get_svm_importances(clf.coef_) sample_matrix = vcd.datamanager.build_sample_matrix(dataset, category) class_vector = vcd.datamanager.build_class_vector(dataset, category) pred = clf.decision_function(sample_matrix) del clf image_titles = [get_image_title(prediction, real) for prediction, real in izip(pred, class_vector)] del class_vector del sample_matrix img_names = [f for f in vcd.datamanager.get_image_names(dataset, category)] vis = Visualization(datamanager) vis.visualize_images(img_names, importances, image_titles)
def main(argv): cmd, mazefile, inpathfile, outpathfile, qlconfig = parseArgv(argv) world = environment.GridWorld(Cell, None, None, mazefile) if (cmd == 'astar'): # TOTO write a search agent to handle all this if not inpathfile: problem = search.ProblemImpl(world); s = planner.search.AStar(problem) s.search() path = s.getPath() results = s.getResults() if outpathfile: path = s.savePath(outpathfile, path) else: path = loadPath(inpathfile) results = {} vi = Visualization(world) vi.setPath(path) vi.setResults(results) vi.animatePath = True while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit(0) vi.update() elif (cmd == 'qlearn'): problem = learning.RLProblemImpl(world) actions = problem.actions(13) policy = qlearning.EpsilonGreedyPolicy(0.05) ai = qlearning.QLearn(problem, policy, qlconfig["alpha"], qlconfig["gamma"]) # place agent in random position npc = learning.LearnAgent('@', getRandomLocation(problem), random.randrange(4), ai) world.addAgent(npc) steps = 150 for x in range(qlconfig["episodes"]): for step in range(steps): world.update() if world.isDone() == True: break loc = getRandomLocation(problem) npc.setLocation(loc) if True: print "Exploit:\n" ai.trace = True policy.epsilon = 0.0 for x in range(2): print "Episode= %d" % (x+1) for step in range(steps): world.update() if world.isDone() == True: break; print "Number of steps= %d" % (step+1) loc = getRandomLocation(problem) npc.setLocation(loc) npc._ai.viResults()