def get_number_of_octaves(self): return self.number_of_octaves def get_initial_frequency(self): return self.initial_frequency def get_lacunarity(self): return self.lacunarity def get_persistence(self): return self.persistence def get_seed(self): return self.seed if __name__ == '__main__': parallel_utils.set_number_of_threads(12) N = 256 x_coordinates = np.linspace(-0.5, 0.5, N, dtype='float32') y_coordinates = np.linspace(-0.5, 0.5, N, dtype='float32') z_coordinates = np.linspace(-0.5, 0.5, N, dtype='float32') fractal_noise_pattern = FractalNoisePattern(number_of_octaves=9, initial_frequency=1) noise = fractal_noise_pattern.compute(x_coordinates, y_coordinates, z_coordinates, largest_scale=1, smallest_scale=1/(N-1)) import plot_utils fig, ax = plot_utils.subplots() plot_utils.plot_image(fig, ax, noise[:, :, 0]) plot_utils.render()
predictions = model.predict(test_images) successes = np.sum([ x[0] == x[1] for x in zip([np.argmax(x) for x in predictions], test_labels) ]) success_ratio = successes / len(test_images) print("success ratio: {}".format(success_ratio)) x = 0 # i = 0 # plt.figure(figsize=(6, 3)) # plt.subplot(1, 2, 1) # plot_image(i, predictions, test_labels, test_images, class_names) # plt.subplot(1, 2, 2) # plot_value_array(i, predictions, test_labels) # plt.show() # Plot the first X test images, their predicted label, and the true label # Color correct predictions in blue, incorrect predictions in red num_rows = 5 num_cols = 3 num_images = num_rows * num_cols plt.figure(figsize=(2 * 2 * num_cols, 2 * num_rows)) for i in range(num_images): plt.subplot(num_rows, 2 * num_cols, 2 * i + 1) plot_image(i, predictions, test_labels, test_images, class_names) plt.subplot(num_rows, 2 * num_cols, 2 * i + 2) plot_value_array(i, predictions, test_labels) plt.show()
def visualize_field(field, only_window=True, approximate_wavelength=None, filter_label=None, use_autostretch=False, white_point_scale=1, use_log=False, title='', output_path=None): use_colors = False was_stretched = False wavelength = None vmin = None vmax = None if only_window: field_values = field.get_values_inside_window() extent = field.grid.get_window_bounds() else: field_values = field.values extent = field.grid.get_bounds() if isinstance(field, FilteredSpectralField): if filter_label is None: use_colors = True use_log = False field_values = np.moveaxis(field_values, 0, 2) if not use_autostretch: field_values = image_utils.perform_histogram_clip(field_values, white_point_scale=white_point_scale) was_stretched = True else: channel_idx = field.channels[filter_label] field_values = field_values[channel_idx, :, :] wavelength = field.central_wavelengths[channel_idx] elif isinstance(field, SpectralField): wavelength_idx = 0 if approximate_wavelength is None else np.argmin(np.abs(field.wavelengths - approximate_wavelength)) wavelength = field.wavelengths[wavelength_idx] field_values = field_values[wavelength_idx, :, :] if field.dtype == np.dtype('complex128') and not use_colors: phases = np.angle(field_values) field_values = np.abs(field_values) phases[field_values == 0] = np.nan else: field_values = field_values.astype('float64') if use_log: field_values = np.log10(field_values) log_label = 'log10 ' else: log_label = '' if not was_stretched: if use_autostretch: field_values = image_utils.perform_autostretch(field_values) vmin = 0 vmax = 1 else: vmin = np.min(field_values) vmax = np.max(field_values)*white_point_scale if wavelength is None: wavelength = 1 else: wavelength_text = '{:g} nm'.format(wavelength*1e9) title = wavelength_text if title == '' else '{} ({})'.format(title, wavelength_text) colorbar = not (use_colors or use_autostretch) xlabel = r'$x$' ylabel = r'$y$' phase_clabel = 'Field phase [rad]' if field.grid.grid_type == 'source': xlabel = r'$d_x$' ylabel = r'$d_y$' clabel = '{}Field amplitude [sqrt(W/m^2/m)]'.format(log_label) elif field.grid.grid_type == 'aperture': xlabel = r'$x$ [m]' ylabel = r'$y$ [m]' clabel = '{}Field amplitude [sqrt(W/m^2/m)]'.format(log_label) extent *= wavelength elif field.grid.grid_type == 'image': xlabel = r'$x$ [focal lengths]' ylabel = r'$y$ [focal lengths]' clabel = '{}Flux [W/m^2/m]'.format(log_label) fig = plot_utils.figure() if field.dtype == np.dtype('complex128') and not use_colors: left_ax = fig.add_subplot(121) plot_utils.plot_image(fig, left_ax, field_values, xlabel=xlabel, ylabel=ylabel, title=title, extent=extent, clabel=clabel) right_ax = fig.add_subplot(122) plot_utils.plot_image(fig, right_ax, phases, xlabel=xlabel, ylabel=ylabel, title=title, extent=extent, clabel=phase_clabel) else: ax = fig.add_subplot(111) plot_utils.plot_image(fig, ax, field_values, vmin=vmin, vmax=vmax, xlabel=xlabel, ylabel=ylabel, title=title, extent=extent, clabel=clabel, colorbar=colorbar) plot_utils.tight_layout() if output_path is None: plot_utils.show() else: plot_utils.savefig(output_path)
# Train the model! :calculator: model.fit(train_images, train_labels, epochs=6) # Test it against the test set :pray: test_loss, test_acc = model.evaluate(test_images, test_labels) print('Test accuracy:', test_acc) # Let's make some predictions predictions = model.predict(test_images) print('Predictions', predictions[0]) print('First prediction (most likely):', class_names[test_labels[np.argmax(predictions[0])]]) # Plot the first 10 test images, their predicted label, and the true label # Color correct predictions in blue, incorrect predictions in red num_rows = 5 num_cols = 3 num_images = num_rows * num_cols plt.figure(figsize=(2 * 2 * num_cols, 2 * num_rows)) for i in range(10): plt.subplot(num_rows, 2 * num_cols, 2 * i + 1) plot_utils.plot_image(i, predictions, test_labels, test_images) plt.subplot(num_rows, 2 * num_cols, 2 * i + 2) plot_utils.plot_value_array(i, predictions, test_labels) plt.show()