def preprocess(img, bbox_labels, mode): img_width, img_height = img.size sampled_labels = bbox_labels if mode == 'train': if train_parameters['apply_distort']: img = distort_image(img) if train_parameters['apply_expand']: img, bbox_labels, img_width, img_height = expand_image( img, bbox_labels, img_width, img_height) if train_parameters['apply_corp']: batch_sampler = [] # hard-code here batch_sampler.append(sampler(1, 1, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0)) batch_sampler.append(sampler(1, 50, 0.3, 1.0, 0.5, 2.0, 0.1, 0.0)) batch_sampler.append(sampler(1, 50, 0.3, 1.0, 0.5, 2.0, 0.3, 0.0)) batch_sampler.append(sampler(1, 50, 0.3, 1.0, 0.5, 2.0, 0.5, 0.0)) batch_sampler.append(sampler(1, 50, 0.3, 1.0, 0.5, 2.0, 0.7, 0.0)) batch_sampler.append(sampler(1, 50, 0.3, 1.0, 0.5, 2.0, 0.9, 0.0)) batch_sampler.append(sampler(1, 50, 0.3, 1.0, 0.5, 2.0, 0.0, 1.0)) sampled_bbox = generate_batch_samples(batch_sampler, bbox_labels) if len(sampled_bbox) > 0: idx = int(np.random.uniform(0, len(sampled_bbox))) img, sampled_labels = crop_image(img, bbox_labels, sampled_bbox[idx], img_width, img_height) mirror = int(np.random.uniform(0, 2)) if mirror == 1: img = img.transpose(Image.FLIP_LEFT_RIGHT) for i in six.moves.xrange(len(sampled_labels)): tmp = sampled_labels[i][1] sampled_labels[i][1] = 1 - sampled_labels[i][3] sampled_labels[i][3] = 1 - tmp img = resize_img(img, sampled_labels) if train_parameters['log_feed_image']: log_feed_image(img, sampled_labels) img = np.array(img).astype('float32') img -= train_parameters['mean_rgb'] img = img.transpose((2, 0, 1)) # HWC to CHW img *= 0.007843 return img, sampled_labels
def train(self, epochs, train_iter, d_steps, g_steps): true_labels = torch.ones(self.bs) fake_labels = torch.zeros(self.bs) for epoch in range(epochs): for iteration in range(train_iter): for p in self.D.parameters(): p.requires_grad = True for i in range(d_steps): sample = torch.Tensor( utils.sampler(self.bs, self.sample_dimension)) d_real_decision = self.D(sample) d_real_loss = self.criterion(d_real_decision.squeeze(), true_labels) d_fake_data = self.G( torch.randn(self.bs, self.noise_dimension)) d_fake_decision = self.D(d_fake_data) d_fake_loss = self.criterion(d_fake_decision.squeeze(), fake_labels) d_loss = d_real_loss + d_fake_loss self.optimizer_d.zero_grad() d_loss.backward() self.optimizer_d.step() for j in range(g_steps): for p in self.D.parameters(): p.requires_grad = False g_fake_data = self.G( torch.randn(self.bs, self.noise_dimension)) g_fake_decision = self.D(g_fake_data) g_loss = self.criterion(g_fake_decision.squeeze(), true_labels) self.optimizer_g.zero_grad() g_loss.backward() self.optimizer_g.step()
def __call__(self, start_point, goal_point, obstacle_list, animation=False): """Plans path from start to goal avoiding obstacles. Args: start_point: tuple with start point coordinates. end_point: tuple with end point coordinates. obstacle_list: list of obstacles which themselves are list of points animation: flag for showing planning visualization (default False) Returns: A list of points representing the path determined from start to goal while avoiding obstacles. An list containing just the start point means path could not be planned. """ # Initialize start and goal nodes start = Node.from_coordinates(start_point) goal_node = Node.from_coordinates(goal_point) # Initialize node_list with start node_list = [start] # Calculate distances between start and goal del_x, del_y = start.x - goal_node.x, start.y - goal_node.y distance_to_goal = math.sqrt(del_x**2 + del_y**2) # Loop to keep expanding the tree towards goal if there is no direct connection if check_intersection([start_point, goal_point], obstacle_list): while True: # Sample random point in specified area rnd_point = sampler(self.sample_area, goal_point, self.goal_sample_rate) # Find nearest node to the sampled point distance_list = [ (node.x - rnd_point[0])**2 + (node.y - rnd_point[1])**2 for node in node_list ] nearest_node_index = min(xrange(len(distance_list)), key=distance_list.__getitem__) nearest_node = node_list[nearest_node_index] # Create new point in the direction of sampled point theta = math.atan2(rnd_point[1] - nearest_node.y, rnd_point[0] - nearest_node.x) new_point = nearest_node.x + self.expand_dis*math.cos(theta), \ nearest_node.y + self.expand_dis*math.sin(theta) # Check whether new point is inside an obstacles for obstacle in obstacle_list: if Point(new_point).within(Polygon(obstacle)): new_point = float('nan'), float('nan') continue # Expand tree if math.isnan(new_point[0]): continue else: new_node = Node.from_coordinates(new_point) new_node.parent = nearest_node node_list.append(new_node) # Check if goal has been reached or if there is direct connection to goal del_x, del_y = new_node.x - goal_node.x, new_node.y - goal_node.y distance_to_goal = math.sqrt(del_x**2 + del_y**2) if distance_to_goal < self.expand_dis or not check_intersection(\ [new_node.to_tuple(), goal_node.to_tuple()], obstacle_list): goal_node.parent = node_list[-1] node_list.append(goal_node) print("Goal reached!") break else: goal_node.parent = start node_list = [start, goal_node] # Construct path by traversing backwards through the tree path = [] last_node = node_list[-1] while node_list[node_list.index(last_node)].parent is not None: node = node_list[node_list.index(last_node)] path.append(node.to_tuple()) last_node = node.parent path.append(start.to_tuple()) if animation == True: RRT.visualize_tree(node_list, obstacle_list) return list(reversed(path)), node_list
}, { 'aprox': [1, -1], 'eat': [10, 0], 'dead': [-100, 0] }, { 'aprox': [3, -1], 'eat': [10, 0], 'dead': [-100, 0] }, { 'aprox': [1, 0], 'eat': [20, 0], 'dead': [-10, 0] }, { 'aprox': [3, -1], 'eat': [20, 0], 'dead': [-10, 0] }, ] #training_loops(snakes_games, rewards,2) #print(samples_3(snakes_games[0], rewards[0], 0, 0,1)) for i in range(50): a = sampler(snakes_games[0], rewards[0], 0, 0, 1, True) print(len(a), a[-1]) #training_5(snakes_games, rewards, 1)
from utils import ResNet50Bottom, sampler, render_box, render_pcl, visualize_result, IoU import matplotlib.pyplot as plt import numpy as np import pdb import os logger = Logger('./logs/4') nusc_classes = [ '__background__', 'pedestrian', 'barrier', 'trafficcone', 'bicycle', 'bus', 'car', 'construction', 'motorcycle', 'trailer', 'truck' ] batch_size = 4 nusc_sampler_batch = sampler(400, 2) nusc_set = nuscenes_dataloader(batch_size, len(nusc_classes), training=True) nusc_dataloader = torch.utils.data.DataLoader(nusc_set, batch_size=batch_size, sampler=nusc_sampler_batch) nusc_iters_per_epoch = int(len(nusc_set) / batch_size) num_epochs = 200 model = MLP() model.cuda() optimizer = torch.optim.Adam(model.parameters(), lr=0.001) regressor = nn.SmoothL1Loss() classifier = nn.BCELoss()
from torch import nn import torch import matplotlib.pyplot as plt from PIL import Image import os import utils import generate sample_dimension = 2 noise_dimension = 2 bs = 1000 frame = generate.Framework(sample_dimension, noise_dimension, bs) frame.train(1, 1000, 6, 1) fakes = frame.G(torch.randn(200, noise_dimension)).detach().numpy() fig = plt.figure() ax1 = fig.add_subplot(121) ax1.set_xlim([-0.5, 0.5]) ax1.set_ylim([-0.5, 0.5]) ax1.plot(fakes[:, 0], fakes[:, 1], '.') ax2 = fig.add_subplot(122) data = utils.sampler(200, sample_dimension) ax2.set_xlim([-0.5, 0.5]) ax2.set_ylim([-0.5, 0.5]) ax2.plot(data[:, 0], data[:, 1], '.') plt.show()
# train set # -- Note: Use validation set and disable the flipped to enable faster loading. cfg.TRAIN.USE_FLIPPED = True cfg.USE_GPU_NMS = args.cuda out_dir = os.path.dirname(os.path.abspath(__file__)) output_dir = out_dir + "/" + args.save_dir + "/" + args.net + "/" + args.dataset + "/" + args.timestamp if not os.path.exists(output_dir): os.makedirs(output_dir) # Loading train and val dataset using NuScenes dataloader dataset_size = 20000 train_size = int(dataset_size * 0.8) val_size = dataset_size - train_size nusc_sampler_batch = sampler(dataset_size, args.batch_size) train_sampler_batch = sampler(train_size, args.batch_size) val_sampler_batch = sampler(val_size, args.batch_size) nusc_set = nuscenes_dataloader(args.batch_size, len(nusc_classes), training=True) training_set, validation_set = random_split(nusc_set, [train_size, val_size]) nusc_dataloader = torch.utils.data.DataLoader(nusc_set, batch_size=args.batch_size, num_workers=args.num_workers, sampler=nusc_sampler_batch) train_loader = torch.utils.data.DataLoader(training_set, batch_size=args.batch_size, num_workers=args.num_workers,