def updatefig(*args): nonlocal done nonlocal obs nonlocal HORIZON nonlocal timestep rebuild, obs_torch = transform_observation(obs) if not done and timestep < HORIZON: #action, action_proba = vape.act(obs_torch) #action = action[0].detach().numpy() action = env.action_space.sample() action = [action[0], 0.3, 0.0] obs, reward, done, info = env.step(action) #env.render(mode='human') timestep += 1 else: done = False obs = env.reset() timestep = 0 c = NHWC(vape.centroids.detach().numpy()) im.set_array( side_by_side( side_by_side(side_by_side(obs, rebuild), c[0, :, :, 0]), c[1, :, :, 0])) vape.optimize_vae(obs_torch, optimizer) time.sleep(0.01) return im,
def test1(self): got = side_by_side(["one", "two",], ["1","2",], 12) #12 for convenience expected = "\n".join(['', #1234567890123456789012345 " Side-by-side comparison ", "____Got_____|__Expected__", "one |1 ", "two |2 ",]) self.assertEqual(got, expected)
def ref_region( img: np.ndarray, selem: Any = disk(5), sigma: int = 3, opening_se: np.ndarray = np.ones((10, 10)), closing_se: np.ndarray = np.ones((5, 5)), verbose: bool = False ): """ """ # Perform histogram equalisation : _img_eq = rank.equalize(img, selem=selem) # Perform edge detection : _edges = canny(_img_eq, sigma=3) _filled = ndi.binary_fill_holes(_edges) # Morphological processing : _eroded = utils.closing( utils.opening(np.float64(_filled), opening_se), closing_se ) if verbose: utils.side_by_side(img, _img_eq, title1="Original", title2="Histogram Equalised") #plt.title('Lol') utils.side_by_side(_img_eq, _filled, title1="Histogram Equalised", title2="Canny Edge Detection + Filled image") #plt.title('Lal') utils.side_by_side(_filled, _eroded, title1="Canny Edge Detection + Filled image", title2="Opening, closing") #plt.title('Lel') return _eroded
def updatefig(*args): global done global obs if not done: obs_torch = torch.from_numpy(NCHW([obs])).float().to(device) mu, _ = vape.encode(obs_torch) action, logp = vape.act(obs_torch) obs, reward, done, info = env.step(action) env.render(mode='human') else: done = False obs = env.reset() obs_torch = torch.from_numpy(NCHW([obs])).float().to(device) rebuild, mu, log_sigma, z = vape.encode_decode(obs_torch) rebuild = NHWC(rebuild.detach().numpy()[0]) im.set_array(side_by_side(obs, rebuild)) time.sleep(0.01) return im,
def updatefig(*args): nonlocal done nonlocal obs nonlocal HORIZON nonlocal timestep obs_torch = torch.from_numpy(NCHW([obs])).float().to(device) if not done and timestep < HORIZON: action, action_proba = vape.act(obs_torch) action = action[0].detach().numpy() obs, reward, done, info = env.step(action) env.render(mode='human') timestep += 1 else: done = False obs = env.reset() timestep = 0 rebuild = vape.encode_decode(obs_torch) rebuild = NHWC(rebuild.detach().numpy()[0]) im.set_array(side_by_side(obs, rebuild)) vape.optimize_vae(obs_torch, optimizer) time.sleep(0.01) return im,
def updatefig(*args): global done global obs if not done: if policy: obs_torch = torch.from_numpy(NCHW([obs])).float().to(device) mu, _ = vae.encode(obs_torch) action, action_proba = policy.act(mu.detach().numpy()) action = action[0] else: action = env.action_space.sample() action = [action[0], 0.3, 0.0] obs, reward, done, info = env.step(action) env.render(mode='human') else: done = False obs = env.reset() obs_torch = torch.from_numpy(NCHW([obs])).float().to(device) mu, log_sigma, z, rebuild = vae(obs_torch) rebuild = NHWC(rebuild.detach().numpy()[0]) im.set_array(side_by_side(obs, rebuild)) time.sleep(0.01) return im,
def main(): # Parse arguments parser = argparse.ArgumentParser(description='REINFORCE using PyTorch') # Logging parser.add_argument('--alias', type=str, default='base', help="""Alias of the model.""") parser.add_argument('--render_interval', type=int, default=100, help='interval between rendered epochs (default: 100)') # Learning parameters parser.add_argument('--gamma', type=float, default=0.99, help='discount factor (default: 0.99)') parser.add_argument('--lr', type=float, default=0.01, help='learning rate (default: 0.01)') parser.add_argument('--no-cuda', action='store_true', default=False, help='Enables CUDA training') parser.add_argument('--eb', type=int, default=1, help='episode batch (default: 1)') parser.add_argument('--episodes', type=int, default=10000, help='simulated episodes (default: 10000)') parser.add_argument('--policy', type=str, default=None, help="""Policy checkpoint to restore.""") parser.add_argument('--seed', type=int, default=42, help='random seed (default: 42)') parser.add_argument('--horizon', type=int, default=1000, help='horizon (default: 1000)') parser.add_argument('--baseline', action='store_true', help='use the baseline for the REINFORCE algorithm') args = parser.parse_args() # Check cuda args.cuda = not args.no_cuda and torch.cuda.is_available() device = torch.device("cuda" if args.cuda else "cpu") # Initialize environment env = gym.make('CarRacing-v0') env = CropCarRacing(env) env = ResizeObservation(env, (32, 32, 3)) env = Scolorized(env, weights=[0.0, 1.0, 0.0]) env = NormalizeRGB(env) env.seed(args.seed) torch.manual_seed(args.seed) print("Env final goal:", env.spec.reward_threshold) # Create the alias for the run alias = '%s_%s' % (args.alias, time.time()) # Use alias for checkpoints checkpoint_best_filename = 'policy_weights/' + alias + '_best.torch' checkpoint_final_filename = 'policy_weights/' + alias + '_final.torch' if not os.path.exists('weights/'): os.makedirs('weights/') # Tensorboard writer writer = SummaryWriter('logs/' + alias) # Create VAE policy vape = VAEPolicy() optimizer = optim.Adam(vape.parameters(), lr=1e-04) # Animation of environment obs = env.reset() obs_torch = torch.from_numpy(NCHW([obs])).float().to(device) rebuild = vape.encode_decode(obs_torch) rebuild = NHWC(rebuild.detach().numpy()[0]) fig1 = plt.figure() if len(obs.shape) == 3 and (obs.shape[-1] == 1): im = plt.imshow(side_by_side(obs, rebuild), cmap="Greys") else: im = plt.imshow(side_by_side(obs, rebuild)) done = False HORIZON = 200 timestep = 0 # Setting animation update function def updatefig(*args): nonlocal done nonlocal obs nonlocal HORIZON nonlocal timestep obs_torch = torch.from_numpy(NCHW([obs])).float().to(device) if not done and timestep < HORIZON: action, action_proba = vape.act(obs_torch) action = action[0].detach().numpy() obs, reward, done, info = env.step(action) env.render(mode='human') timestep += 1 else: done = False obs = env.reset() timestep = 0 rebuild = vape.encode_decode(obs_torch) rebuild = NHWC(rebuild.detach().numpy()[0]) im.set_array(side_by_side(obs, rebuild)) vape.optimize_vae(obs_torch, optimizer) time.sleep(0.01) return im, # Start animation ani = animation.FuncAnimation(fig1, updatefig, interval=50, blit=True) plt.show() # Close env and writer env.close() writer.close()
mangueras_segmentadas = { key: auto_segment(mangueras[key], verbose=False, groups=2, skew=None) for key in mangueras.keys() } # Aquí segmentamos automáticamente la región de la manguera, gracias al gran contraste que existe entre éste nuestro ente de interés y el fondo (muy claro el primero, oscuro el segundo). # # Usamos la función que diseñamos : ```auto_segment()``` # In[13]: for nombre in mangueras.keys(): utils.side_by_side( mangueras[nombre], mangueras_segmentadas[nombre], title1=nombre, title2=f"{nombre} : manguera segmentada" ) # Aquí podemos observar las imágenes con su respectiva máscara de segmentación. # In[14]: region_ref1 = { key: auto_segment(mangueras[key], groups=3) for key in mangueras.keys() } # In[15]:
env = Scolorized(env, weights=[0.0, 1.0, 0.0]) env.seed(args.seed) # Network creation VAE_class = VAEbyArch(args.arch) vae = VAE_class(latent_size=args.latent_size).to(device) # Restore checkpoint assert args.vae, "No checkpoint provided." vae.load_state_dict(torch.load(args.vae)) vae.eval() if args.dataset: # Single observation display mu, log_sigma, z, rebuild = vae(dataset_torch[args.sample:args.sample+1]) rebuild = rebuild.detach().numpy()[0] imshow_bw_or_rgb(side_by_side(dataset[args.sample], NHWC(rebuild))) plt.show() else: # Check if we use a policy policy = None if args.policy and args.vae_old: policy_env = VAEObservation(env, args.vae_old, arch=args.arch) policy = Policy(policy_env) policy.load_state_dict(torch.load(args.policy)) policy.eval() vae_old = VAE_class(latent_size=args.latent_size).to(device) vae_old.load_state_dict(torch.load(args.vae_old)) vae_old.eval() # Animation of environment obs = env.reset() obs_torch = torch.from_numpy(NCHW([obs])).float().to(device)
def _test(self, source, expected): got, ignore = pretty.consolidate_php(source, []) msg = side_by_side(got, expected) self.assertEqual(got, expected, msg)
def _test(self, source, expected): got, ignore = comments.standardize_comments(source, []) msg = side_by_side(got, expected) self.assertEqual(got, expected, msg)
sns.distplot(intensities, kde=False, rug=True, bins=10) plt.axvline(K, color='r') lmap(lambda x: plt.axvline(x, color='g'), centers1) _ = plt.title(f"Means = {centers1}, K = {K}", size=16) # In[14]: thresh1 = cv.threshold(img, K, 255, cv.THRESH_BINARY)[1] # In[15]: utils.side_by_side(img, thresh1) # Como podemos ver, una técinca de umbralización estándar como k-medias móviles, con dos medias, da resultados muy pobres. # In[16]: otsu1 = cv.threshold(img,0,255,cv.THRESH_BINARY+cv.THRESH_OTSU)[1] # In[17]: utils.side_by_side(img, otsu1)
# In[9]: binaria = binarise(x) plt.imshow(binaria, cmap='gray') # In[10]: help(opening) # # Opening # In[11]: kernel = np.ones((10, 10)) side_by_side(binaria, opening(binaria, kernel), title1='Original', title2=f'opening() with Kernel {kernel.shape}') # In[12]: kernel = np.ones((10, 10)) side_by_side(binaria, cv.morphologyEx(binaria, cv.MORPH_OPEN, kernel), title1='Original', title2=f'cv.MORPH_OPEN with Kernel {kernel.shape}') # In[13]: kernel = np.ones((2, 50)) side_by_side(binaria, opening(binaria, kernel),
plt.imshow(x, cmap='gray') # In[7]: binaria = binarise(x) plt.imshow(binaria, cmap='gray') # In[8]: help(cv.dilate) # In[9]: kernel = np.ones((10, 10)) side_by_side(binaria, cv.dilate(binaria, kernel), title1='Original', title2=f'Kernel {kernel.shape}') # In[10]: kernel = np.ones((2, 50)) side_by_side(binaria, cv.dilate(binaria, kernel), title1='Original', title2=f'Kernel {kernel.shape}') # In[11]: kernel = np.ones((50, 2)) side_by_side(binaria, cv.dilate(binaria, kernel),
plt.imshow(x, cmap='gray') # In[7]: binaria = binarise(x) plt.imshow(binaria, cmap='gray') # In[8]: help(cv.erode) # In[9]: kernel = np.ones((10, 10)) side_by_side(binaria, cv.erode(binaria, kernel), title1='Original', title2=f'Kernel {kernel.shape}') # In[10]: kernel = np.ones((2, 30)) side_by_side(binaria, cv.erode(binaria, kernel), title1='Original', title2=f'Kernel {kernel.shape}') # In[11]: kernel = np.ones((70, 2)) side_by_side(binaria, cv.erode(binaria, kernel),
vape = VAEPolicy() # Restore checkpoint assert args.controller, "No checkpoint provided." vape.load_state_dict(torch.load(args.controller)) vape.eval() # Animation of environment obs = env.reset() obs_torch = torch.from_numpy(NCHW([obs])).float().to(device) rebuild, mu, log_sigma, z = vape.encode_decode(obs_torch) rebuild = NHWC(rebuild.detach().numpy()[0]) fig1 = plt.figure() if len(obs.shape) == 3 and (obs.shape[-1] == 1): im = plt.imshow(side_by_side(obs, rebuild), cmap="Greys") else: im = plt.imshow(side_by_side(obs, rebuild)) done = False # Setting animation update function def updatefig(*args): global done global obs if not done: obs_torch = torch.from_numpy(NCHW([obs])).float().to(device) mu, _ = vape.encode(obs_torch) action, logp = vape.act(obs_torch) obs, reward, done, info = env.step(action) env.render(mode='human') else: