def plot_platforms(w_train_red, x_ground_truth, results, all_inds, u=8, v=5, index=1): # i = test w_ = w_train_red[index, :].reshape(1, 53) ut.plot_contours(w_, v, u, x_vals=x_ground_truth, list_att=['OccRain', 'OccSun', 'Surf', 'Outline'], list_plot_x=[[0, 1], [2, 3]], samp_to_plot=0) pars = results[0] # results[i,:] # i = test w_ = w_train_red[index, :].reshape(1, 53) w_[0, all_inds] = pars ut.plot_contours(w_, v, u, x_vals=x_ground_truth, list_att=['OccRain', 'OccSun', 'Surf', 'Outline'], list_plot_x=[[0, 1], [2, 3]], samp_to_plot=0)
def plot_platform_grids(w_train_red, x_vals, v=5, u=8, list_att=['OccRain', 'OccSun', 'Surf', 'Outline'], list_plot_x=[[0, 1], [2, 3]], samp_to_plot=0, grids=None, pole_ind=None): w_ = w_train_red[0, :].reshape(1, 53) ut.plot_contours(w_, v=5, u=8, x_vals=x_vals, list_att=['OccRain', 'OccSun', 'Surf', 'Outline'], list_plot_x=[[0, 1], [2, 3]], samp_to_plot=0, grids=grids, pole_ind=pole_ind)
def plot_segmentation(self, labels, images, pred_overlap, pred_stardist, pred_objprob, num_proposals, iou_thres, min_objprob, epoch, plot_name): """Plot the original images, the predicted and the true segmentation. Additionally, the sampling position is shown for every predicted instance. Parameters: labels -- list of arrays of shapes (num_cells, height, width) with cell masks images -- array of shape (num_images, 1, height, width) with images pred_overlap -- array of shape (num_images, 1, height, width) with predicted overlap pred_stardist -- array of shape (num_images, 32, height, width) with predicted star distances pred_objprob -- array of shape (num_images, 1, height, width) with predicted object probabilities num_proposals -- number of proposals to generate for the segmentation iou_thres -- intersection over union threshold on two proposals above which the less confident is suppressed min_objprob -- minimum required object probability to sample a pixel position epoch -- training epoch plot_name -- string, training set / test set """ fig, axs = plt.subplots(images.shape[0], 3, figsize=(13, 13)) axs[0, 0].set_title("Input image") axs[0, 1].set_title("Segmentation prediction \n with sampling positions") axs[0, 2].set_title("Segmentation gt") for i in range(images.shape[0]): polygons_pred, center_coordinates = sampling.nms( pred_overlap[i, 0], pred_stardist[i], pred_objprob[i, 0], num_proposals=num_proposals, iou_thres=iou_thres, min_objprob=min_objprob) axs[i, 0].imshow(images[i, 0], cmap='gray') utils.plot_contours(polygons_pred, images[i, 0], axs[i, 1], center_coordinates) utils.plot_contours(labels[i], images[i, 0], axs[i, 2]) plt.tight_layout() self.writer.add_figure(plot_name + " segmentation", fig, epoch)
def plot_model_density(model, experiment_dir, summary_writer, epoch=0): ########################################################################### # We can also plot the learned density ! # ########################################################################### # create data point meshgrid x1, x2, x = make_mesh() # evaluate log-probability log_px = -model(x.reshape(-1, 2)) # convert to probability value by negation and taking the exponential log_px = log_px.reshape(x.shape[:2]).detach() px = log_px.exp() # Finally, plot the learned density fig = plt.figure(figsize=(6, 6)) plot_contours(px) plt.savefig(os.path.join(experiment_dir, f'learned_density_{epoch}')) summary_writer.add_figure('learned_density', fig, global_step=epoch) plt.close()
def plot_samples_gaussian_approx(X, Y, mu_beta, sigma_beta, eps, plot_lim, x_2_boundary, x1_grid, base_fn, title=None, ax_label_size=26, tick_fontsize=17, has_bias=True): f, axarr = plt.subplots(ncols=2, figsize=[11, 5.5]) if has_bias: mu_beta_plot, sigma_beta_plot = mu_beta[1:], sigma_beta[1:, 1:] else: mu_beta_plot, sigma_beta_plot = mu_beta, sigma_beta utils.plot_contours(axarr[1], mu_beta_plot, cov=sigma_beta_plot, color='red') axarr[1].set_xlabel(r"$\beta_1$", fontsize=ax_label_size) axarr[1].set_ylabel(r"$\beta_2$", fontsize=ax_label_size) b_plot_lim = 3.5 axarr[1].set_xlim([-b_plot_lim, b_plot_lim]) axarr[1].set_ylim([-b_plot_lim, b_plot_lim]) for tick in axarr[1].yaxis.get_major_ticks(): tick.label.set_fontsize(tick_fontsize) for tick in axarr[1].xaxis.get_major_ticks(): tick.label.set_fontsize(tick_fontsize) # plot data-points and decision boundaries axarr[0].scatter(X[:, 0], X[:, 1], c=np.float32(Y), s=140., cmap=cm.get_cmap("binary"), edgecolors="k", label="Datapoints", linewidths=2) chol = np.linalg.cholesky(sigma_beta) print("plotting samples decisions") for i, eps_i in enumerate(eps): beta_i = mu_beta + chol.dot(eps_i) x_2_boundary_vals = x_2_boundary(beta_i, x1_grid) axarr[0].plot(x1_grid, x_2_boundary_vals, label=title, c='k', linewidth=0.5) axarr[0].set_xlim([-plot_lim, plot_lim]) axarr[0].set_ylim([-plot_lim, plot_lim]) axarr[0].set_xlabel(r"$X_1$", fontsize=ax_label_size) axarr[0].set_ylabel(r"$X_2$", fontsize=ax_label_size) for tick in axarr[0].yaxis.get_major_ticks(): tick.label.set_fontsize(tick_fontsize) for tick in axarr[0].xaxis.get_major_ticks(): tick.label.set_fontsize(tick_fontsize) plt.suptitle(title, fontsize=25) plt.tight_layout(pad=2.5) plt.savefig(base_fn + title + "_2D_vis.png") plt.clf()
for i in range(X_test.shape[0]): yp = np.sign(np.dot(X_test[i], W)) y_test_predicted = np.append(y_test_predicted, yp) print("accuracy on test dataset: {}".format( accuracy_score(y_test, y_test_predicted))) # Now, let's explore the classification boundaries fig, ax = plt.subplots() title = ('Decision surface of linear SVC ') # Set-up grid for plotting. X0, X1 = X_test[:, 0], X_test[:, 1] xx, yy = make_meshgrid(X0, X1) plot_contours(ax, xx, yy, W, cmap=plt.cm.PuBu_r, alpha=1) ax.scatter(X0, X1, c=y_test, cmap=plt.cm.Set1, s=40, edgecolors=None, marker='o') ax.set_xticks(()) ax.set_yticks(()) ax.set_title(title) plt.show() plt.plot(range(1, len(lossHistory) + 1), lossHistory) plt.xlabel('Iterations') plt.ylabel('Cost')
def main(args): # Hyperparameters bb_dim = args['bbdim'] fevals = args['fevals'] glr = args['glr'] nannealing = args['nannealing'] nmag = args['nmag'] noise_dim = args['ndim'] objective_name = args['objective'] pop_size = args['popsize'] seed = args['seed'] stop_criter = args['stopcriter'] # Reproducibility tf.reset_default_graph() tf.set_random_seed(seed) np.random.seed(seed) # RUNNING OPTIMIZATION with tf.Session() as session: # Objective definition bbo = obj.create_objective(objective_name, bb_dim) # Generator instance opt = gen.Generator(bb_dim=bb_dim, noise_dim=noise_dim, step_size=glr, pop_size=pop_size) # Initialization session.run(tf.global_variables_initializer()) popv = [] # population values array popi = [] # population individuals array # Optimization i = 0 cur_min = np.inf max_iter = int(fevals * bb_dim / pop_size) stop = False while (i < max_iter and not (stop)): # Generate the input samples for the generator samples = np.random.uniform(-nmag, nmag, size=(pop_size, noise_dim)) nmag *= nannealing # Generates the outputs queries = session.run(opt.y, feed_dict={opt.x: samples}) # Generates the gradients outputs (fvals, dfvals) = bbo.bbox_oracle1(session, queries) # Train the generator session.run(opt.update, feed_dict={ opt.x: samples, opt.fgrad: dfvals }) # Fill in arrays for post-processing popv.append(fvals) popi.append(queries) popmin = np.min(fvals) # Stopping criterion if popmin <= cur_min: cur_min = popmin stop = popmin <= stop_criter i += 1 # Prints performances mins = np.min(popv, axis=1) print('End at iteration ', i, '/', max_iter) print('Minimum value found is %s at iteration %s' % (np.min(mins), np.argmin(mins))) # PLOTS img_dir = './img' if not os.path.exists(img_dir): os.makedirs(img_dir) # Simple regret plot ut.plot_perf(mins, pop_size) # Plot populations if dimension=2 if args['bbdim'] == 2: ut.plot_contours(session, bbo, popi)
h = 1e-3 xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) plt.subplot(121) plt.title("Train features") ax = plt.gca() ax.set_facecolor('xkcd:salmon') ax.set_facecolor((0.8, 0.8, 1)) plot_contours(plt, clf, xx, yy, colors=((0.8, 0.8, 1), (1, 0.8, 0.8)), levels=1) scatter_cluster_dots(plt, X_train, y_train) plt.subplot(122) plt.title("Test features") ax = plt.gca() ax.set_facecolor('xkcd:salmon') ax.set_facecolor((0.8, 0.8, 1)) plot_contours(plt, clf, xx, yy, colors=((0.8, 0.8, 1), (1, 0.8, 0.8)),
def plotContours(self): plot_contours(self.A, self.Cn, thr=0.9)
EPOCHS = 200 optimizer = tf.keras.optimizers.Adam(learning_rate=1e-3) #%% Train model losses = model.fit(dataset, EPOCHS, optimizer, 'kmeans') # losses = model.fit(data,10,'kmeans') f, ax = plt.subplots() ax.plot(range(len(losses)), np.array(losses)) ax.set_title('Training loss') ax.set_xlabel('iteration') plt.show() #%% Plot result f, ax = plt.subplots(figsize=(8, 8)) utl.plot_contours(ax, data, model, alpha=0.1) ax.set_title(name + ' with K = ' + str(K) + ', epochs = ' + str(EPOCHS)) plt.show() # f.savefig('../figures/CP/'+name+'_K_'+str(K)+'_contour.png',dpi=300) f, ax = plt.subplots(figsize=(8, 5)) utl.plot_density(ax, model, cmap='hot') ax.set_title(name + ' with K = ' + str(K) + ', epochs = ' + str(EPOCHS)) plt.show() # f.savefig('../figures/CP/'+name+'_K_'+str(K)+'_density.png',dpi=300) integrand = utl.unitTest(model, limits=[-6, 6]) print(f'Density integrates to {round(integrand,4)}') print('It should be = 1.0')
def _plot_single_channel_maps(args): # Every step if args.every[0] is None: args.every = [1] # Keyword arguments for tile plotter opts = {} ichans, opts['nrows'], opts['ncols'] = get_channel_indices(args) assert opts['nrows'] * opts['ncols'] >= len(ichans) args.logger.info('Rows, columns = %i, %i', opts['nrows'], opts['ncols']) # Velocity axis if args.freq[0] is not None: args.logger.info('Using rest frequency: %.3f GHz', args.freq[0]) args.cube = args.cube.with_spectral_unit(u.GHz, velocity_convention='radio') args.cube = args.cube.with_spectral_unit(u.km / u.s, velocity_convention='radio', rest_value=args.freq[0] * u.GHz) else: args.cube = args.cube.with_spectral_unit(u.km / u.s, velocity_convention='radio') vel = args.cube.spectral_axis ind = np.argsort(vel[ichans]) ichans = list(np.array(ichans)[ind]) if args.auto_velshift: ind = len(ichans) // 2 vel_shift = vel[ichans][ind] elif args.vlsr is not None: vel_shift = args.vlsr * u.km / u.s elif args.sources is not None: args.logger.warn('Using first source for velocity shift') vel_shift = args.sources[0].get_quantity('vlsr') vel_shift = vel_shift.to(u.km / u.s) elif args.source is not None: vel_shift = args.source.get_quantity('vlsr') vel_shift = vel_shift.to(u.km / u.s) elif args.atsources is not None: wcs = WCS(img.header, naxis=['longitude', 'latitude']) pos = args.atsources[i].position pix = wcs.all_world2pix([[pos.ra.deg, pos.dec.deg]], 0)[0] pix = map(int, pix[::-1]) spec = np.squeeze(args.cube.unmasked_data)[:, pix[0], pix[1]][ichans] maxind = np.nanargmax(spec) vel_shift = vel[ichans][maxind] else: args.logger.warn('Could not find velocity shift') vel_shift = 0. args.logger.info('Shifting velocity axis by: %s', vel_shift) vel = vel - vel_shift # Setup tile plotter cubewcs = args.cube.wcs.sub(('longitude', 'latitude')) args.logger.debug('Initializing figure') fig = MapsPlotter(config=args.config[0], section=args.section[0], projection=cubewcs, **opts) cen, radius, markers, orientation = ut.all_mapfig_setup(fig) # Data if args.images is None: data = args.cube.unmasked_data[ichans, :, :].value elif len(args.images) == 1: args.logger.debug('Using input background image') img = args.images[0] data = np.squeeze(img.data) else: raise NotImplementedError # rms, nsigma try: rms = float(fig.get_value('rms')) except TypeError: rms = None nsigma = float(fig.get_value('nsigma', default=5.)) # Get global vmin and vmax vmin, vmax = auto_vminmax(data) vmin = fig.config.getfloat('vmin', fallback=vmin) vmax = fig.config.getfloat('vmax', fallback=vmax) args.logger.info('Color map vmin, vmax = %.3e, %.3e', vmin, vmax) # Levels levels = auto_levels(args.cube.unmasked_data[ichans, :, :].value, rms=rms, nsigma=nsigma) args.logger.info('Global line levels: %r', levels) # Plot for loc, i in zip(fig.axes, ichans): cbar = ichans.index(i) == 0 #ax = fig.get_mapper(ax, include_cbar=cbar, vmin=vmin, vmax=vmax, a=a) bmaj = args.cube.beams[i].major.to(u.deg).value bmin = args.cube.beams[i].minor.to(u.deg).value bpa = args.cube.beams[i].pa.to(u.deg).value overplots = ut.get_overplots(args, i) if args.images is None or len(args.images) == 0: self_contours = True img = fits.PrimaryHDU(args.cube.unmasked_data[i, :, :].value, header=cubewcs.to_header()) img.header['BMAJ'] = bmaj img.header['BMIN'] = bmin img.header['BPA'] = bpa img.header['BUNIT'] = args.cube.header['BUNIT'] else: self_contours = False contours = fits.PrimaryHDU(args.cube.unmasked_data[i, :, :].value, header=cubewcs.to_header()) contours.header['BMAJ'] = bmaj contours.header['BMIN'] = bmin contours.header['BPA'] = bpa contcolor = fig.get_value('contour_color', default='w', ax=loc) optscont = {'colors': contcolor} contours = [(contours, optscont)] label = '%.2f %s' % (vel[i].value, vel.unit.to_string('latex_inline')) ax = ut.plot_single_map(loc, fig, img, args.logger, cen=cen[0], radius=radius[0], self_contours=self_contours, levels=levels, cbar_orientation=orientation if cbar else None, markers=markers, axlabel=label, vmin=vmin, vmax=vmax, include_cbar=cbar, contours=overplots) if not self_contours: ut.plot_contours(ax, contours, levels, zorder=2) fig.auto_config() return fig