def view_raw(args): data = read_raw(args.view_count) for name, _, cls, bbox in data: print(name, cls, bbox) for d in group(data, 36): # display raw images _, images, _, _ = zip(*d) show_image(merge_images_with_border([cv2.resize(i, (150, 150)) for i in images]))
def view_train(args): _, conf = get_model_and_config(args) train_data, _ = get_train_validation_raw(conf.validation_raw_examples_ratio) train_data = batch_generator(train_data, conf.batch_size, use_hog=args.view_hog) viewed = 0 for images, _ in train_data: show_image(merge_images_with_border(images)) viewed += conf.batch_size if viewed >= args.view_count: break
def test_model(trained_ops, test_set_sz, learning_rate, batch_size, latent_dim, tau, inf_layers, gen_layers, restore_path): test_init = trained_ops[0] ops = trained_ops[1] next_batch = trained_ops[2] saver = tf.train.Saver() with tf.Session() as sess: mean_loss = 0 mean_rec = 0 mean_kl = 0 test_iters = int(test_set_sz / batch_size) lat = None lab = None gen_imgs = None rec_imgs = None saver.restore(sess, './saved_models/model.ckpt') sess.run(test_init) print("\nTesting...") for i in range(test_iters): loss, rec, kl, z, x_hat, x_gen, y = sess.run( (ops[0], ops[1], ops[2], ops[4], ops[5], ops[6], next_batch[1])) mean_loss += loss / test_iters mean_rec += rec / test_iters mean_kl += kl / test_iters if lat is None: lat = z[0] lab = y else: lat = np.append(lat, z[0], axis=0) lab = np.append(lab, y, axis=0) # reconstruct samples and generate new ones if i == test_iters - 3 or i == test_iters - 2 or i == test_iters - 1: if gen_imgs is None: rec_imgs = x_hat.reshape((batch_size * 28, 28)) gen_imgs = x_gen.reshape((batch_size * 28, 28)) else: rec_imgs = np.append(rec_imgs, x_hat.reshape((batch_size * 28, 28)), axis=1) gen_imgs = np.append(gen_imgs, x_gen.reshape((batch_size * 28, 28)), axis=1) show_image(gen_imgs, './images/generated.png') show_image(rec_imgs, './images/reconstructed.png') print("\tloss: {:.3f}".format(mean_loss), "\tmean reconstruction: {:.3f}".format(mean_rec), "\tmean KL: {:.3f}".format(mean_kl)) plot_pca(10, lat, lab, './figures/test_latent_space')
def view_test(args): _, conf = get_model_and_config(args) _, raw_test_data = get_train_validation_raw(conf.validation_raw_examples_ratio) images, _ = get_unaugmented(raw_test_data, use_hog=args.view_hog) features, labels = get_unaugmented(raw_test_data, use_hog=conf.use_hog) if args.eval: if not conf.model_dir or not args.model_name: raise ValueError("Reguired model-dir and model-name for testset evaluation.") prediction = predict_classes(args, features, labels) is_correct = [np.argmax(lbl) == np.argmax(pred) for lbl, pred in zip(labels, prediction)] assert len(is_correct) == len(images) print('good answers:', sum(is_correct)) print('accuracy:', sum(is_correct) / len(is_correct)) images = decorate_images(images, is_correct) show_image(merge_images(images, scale=0.2)) else: # show_image(merge_images_with_border([cv2.resize(i, (100, 100)) for i in images])) show_image(merge_images_with_border(images))
def check_moving_detection(): import moving_detection rgb_im = image_processing.load_image("falling balls and cylinder", "rgb_" + str(0) + ".png") depth_im = image_processing.load_image("falling balls and cylinder", "depth_" + str(0) + ".png", "depth") start = time.time() mog = moving_detection.Fast_RGBD_MoG(rgb_im, depth_im, number_of_gaussians=5) print("initialization: ", time.time() - start) for i in range(4): rgb_im = image_processing.load_image("falling balls and cylinder", "rgb_" + str(i + 1) + ".png") depth_im = image_processing.load_image("falling balls and cylinder", "depth_" + str(i + 1) + ".png", "depth") start = time.time() mask = mog.get_mask(rgb_im, depth_im) print("frame updating: ", time.time() - start) visualization.show_image(mask / 255)
BATCH_SIZE = 10 test_data = raw_file_idx3_process("data/test/t10k-images-idx3-ubyte") test_label = raw_file_idx1_process("data/test/t10k-labels-idx1-ubyte") test_set = Torch_data_set(test_data, test_label) test_loader = tud.DataLoader(test_set, batch_size=BATCH_SIZE, shuffle=False) total = 0 bingo = 0 print("\n") with torch.no_grad(): for index, data in enumerate(test_loader): inputs, label = data inputs, label = inputs.to(device), label.to(device) outputs = net(inputs) _, predicted = torch.max(outputs.data, 1) # 对total和bingo进行修改 total += label.size(0) bingo += (predicted == label).sum().item() # 显示第128号的图像 if index == 117: print(predicted.cpu().numpy()) inputs = [inp.reshape(28, 28) for inp in inputs.cpu()] v.show_image(inputs, 2, 5) # 打印正确率 print("\rAccuracy: {:.2f}".format(bingo / total), end='')
title_fs = 10 zone_color = 'darkgray' zone_lw = 1.3 line_color = zone_color line_lw = zone_lw park_color = 'orange' train_color = 'orangered' test_color = 'deepskyblue' fig = plt.figure(figsize=(13, 7)) gs = fig.add_gridspec(2, 3, hspace=0.25, wspace=0.0) # whole image ax_whole = fig.add_subplot(gs[:, 0]) ax_whole.set_title('Taï National Park', fontsize=title_fs) show_image(img_whole, meta_whole['transform'], ax=ax_whole, band_idx=[2, 1, 0]) for p in polygons_Tai: ax_whole.add_patch( matplotlib.patches.Polygon( p, **{ 'linewidth': 1, 'facecolor': (0, 0, 0, 0), 'edgecolor': 'orange' })) ax_whole.add_patch( matplotlib.patches.Rectangle(nw_bounds[0:2], width=np.abs(nw_bounds[0] - nw_bounds[2]), height=np.abs(nw_bounds[1] - nw_bounds[3]), linewidth=zone_lw, facecolor=(0, 0, 0, 0), edgecolor=zone_color))
def initialize_expectation_maximization(filepath_1, filepath_2, components, iterations, subtraction_threshold): """ perform initialization step :param filepath_1: path to first image (required) :param filepath_2: path to second image (optional) :param components: number of components :param iterations: number of iterations :return: means: list of initial mean values :return: variances: list of initial variance values :return: stdevs: list of initial stdev values :return: weights: list of initial weight values :return: log_likelihoods: list of initial log_likelihood values """ # initialize data_matrix based on number of images provided image_1 = np.divide(np.float64(cv2.imread(filepath_1)), np.float64(255)) # read image from disk. normalize. data_matrix = image_1[:, :, 0] # use only first matrix layer in case user provides non-grayscale input. if filepath_2 is not None: # given 2 images as input, data matrix is their CIE94 difference image_2 = np.divide(np.float64(cv2.imread(filepath_2)), np.float64(255)) # read image from disk. normalize. cie94_difference = \ image_processing.cie94_distance_metric(image_processing.xyz_to_lab(image_processing.lrgb_to_xyz(image_1)), image_processing.xyz_to_lab(image_processing.lrgb_to_xyz(image_2))) data_matrix = (cie94_difference + np.min(cie94_difference)) / (np.max(cie94_difference) - np.min(cie94_difference)) # normalize # initialize algorithm state based on parameters if subtraction_threshold is not None and components == 2: # BG-FG thresholded subtraction initialization # initialization based on subtraction threshold (only appropriate for 2 images input and 2 components) print("Initializing GMM state based on thresholded subtraction.") # visualization.show_image((1, 1, 1), "CIE94 Difference Image", cie76_difference, vmin=np.min(cie76_difference), # vmax=np.max(cie94_difference), postprocessing=False) cie94_segmentation_bg = np.int32(cie94_difference <= subtraction_threshold) cie94_segmentation_fg = np.int32(cie94_difference > subtraction_threshold) visualization.show_image((1, 1, 1), "CIE94 Segmentation w/ Threshold="+str(subtraction_threshold), cie94_segmentation_fg, vmin=np.min(cie94_segmentation_fg), vmax=np.max(cie94_segmentation_fg), postprocessing=False) # compute initial algorithm state rows = data_matrix.shape[0] cols = data_matrix.shape[1] fg_num_pixels = np.count_nonzero(cie94_segmentation_fg) bg_num_pixels = np.count_nonzero(cie94_segmentation_bg) fg_mean = np.sum(cie94_segmentation_fg * data_matrix) / fg_num_pixels bg_mean = np.sum(cie94_segmentation_bg * data_matrix) / bg_num_pixels fg_segmentation_array = np.reshape(cie94_segmentation_fg, (rows*cols)) bg_segmentation_array = np.reshape(cie94_segmentation_bg, (rows*cols)) data_matrix_array = np.reshape(data_matrix, (rows*cols)) fg_pixels_array = [data_matrix_array[i] for i in range(0, data_matrix_array.shape[0]) if fg_segmentation_array[i]] bg_pixels_array = [data_matrix_array[i] for i in range(0, data_matrix_array.shape[0]) if bg_segmentation_array[i]] init_means = [bg_mean, fg_mean] init_variances = [np.var(bg_pixels_array), np.var(fg_pixels_array)] init_stdevs = np.sqrt(init_variances) init_weights = [np.float64(bg_num_pixels) / np.float64(bg_num_pixels + fg_num_pixels), np.float64(fg_num_pixels) / np.float64(bg_num_pixels + fg_num_pixels)] else: # arbitrary initialization based on even component spacing assumption print("Initializing GMM state based on evenly spaced component assumption.") init_means = np.linspace(0, 1, components) # assume initial component means are evenly spaced in pixel value domain init_variance = np.float64(10 / 255) # initialized as explained in GMM tutorial paper init_variances = np.float64(np.ones(components)) * init_variance # initial variance is 10/255 - quite small init_stdevs = np.sqrt(init_variances) init_weights = np.ones(components) # log likelihoods do not have the concept of initialization; simply create a list to be populated by EM algorithm init_log_likelihoods = np.zeros(iterations) print("Completed Initialization") print("init_means=", str(init_means), "\ninit_variances=", str(init_variances), "\ninit_stdevs=", str(init_stdevs), "\ninit_weights=", str(init_weights)) return data_matrix, init_means, init_variances, init_stdevs, init_weights, init_log_likelihoods
def view_aug(args): data = read_raw(2)[1:2] images, _ = next(iter(batch_generator(data, args.view_count, use_hog=args.view_hog))) show_image(get_unaugmented(data, use_hog=args.view_hog)[0][0]) show_image(merge_images_with_border(images))
# --------------------------------------------------------------------------------------------------------------- # %% Generate new features img_train = preprocessing(np.stack(band, axis=2)) # %% vizualize new features Nband_kept = 10 - 1 fig, axs = plt.subplots(4, 4, figsize=(12, 10), gridspec_kw={ 'hspace': 0.1, 'wspace': 0.1 }) show_image(img_train[:, :, Nband_kept + 1], meta_train['transform'], ax=axs[0][0], cmap='PiYG') axs[0][0].set_title('NDVI', fontsize=10) axs[0][0].set_ylabel('Northing [m]') axs[0][0].set_xticklabels([]) show_image(img_train[:, :, Nband_kept + 2], meta_train['transform'], ax=axs[0][1], cmap='PiYG') axs[0][1].set_title('green NDVI', fontsize=10) axs[0][1].set_xticklabels([]) axs[0][1].set_yticklabels([]) titles_part1 = [ 'entropy: ', 'Opening reconstruction: ', 'closing reconstruction: ', 'LBP: '
# Visualize results fig = plt.figure(figsize=(12, 16)) gs = plt.GridSpec(4, 3, wspace=0.2, hspace=0.2) title_fs = 11 park_color = 'orange' pred_fc = 'crimson' pred_ec = None pred_alpha = 1 offset_w = [9, 9, 9, 9, 9, 9, 9, 9, 9, 9] offset_h = [5, 5, 5, 5, 5, 9, 5, 5, 5, 9] # overview img ax_overview = fig.add_subplot(gs[0:2, 0]) ax_overview.set_title('Taï National Park', fontsize=title_fs) show_image(img_whole, meta_whole['transform'], ax=ax_overview, band_idx=[2, 1, 0]) for p in polygons_park: ax_overview.add_patch( matplotlib.patches.Polygon( p, **{ 'linewidth': 1, 'facecolor': (0, 0, 0, 0), 'edgecolor': 'orange' })) for i, bound in enumerate(bounds): ax_overview.add_patch(matplotlib.patches.Rectangle(bound[0:2], \ width=np.abs(bound[0] - bound[2]), \ height=np.abs(bound[1] - bound[3]), \ linewidth=1, facecolor=(0,0,0,0), edgecolor='darkgray')) ax_overview.text(bound[2] - offset_w[i] * w,
********HOW TO USE*********** pip install -r requirements.txt 0 - Open config.py 1 - Set R, C and FEATURE_3D 2 - Set IMAGE_NAME and booleans RESIZE and BLUR (for preprocessing) 3 - Run """ from config import * from utils import pre_process, save_image, concatenate_images from imageSegmentation import segmIm from visualization import cv2, show_image if __name__ == '__main__': start_time = time.time() img, img_origin = pre_process() segmented_img, num_peaks = segmIm(img, R, C, FEATURE_3D) final_im = concatenate_images( [cv2.cvtColor(img_origin, cv2.COLOR_RGB2BGR), img, segmented_img]) seconds = time.time() - start_time print("--- %s seconds ---" % seconds) if SAVE: SAVE_NAME = IMAGE_NAME + '_r' + str(R) + '_c' + str(C) + dim_name( ) + blur_name() + '_' + str(round(seconds)) + 'sec.jpg' save_image(final_im, SAVE_NAME) else: show_image(final_im)
def show_hausdorff_distance(shape_distance_id): bitmap = render_hausdorff_distance(shape_distance_id) visualization.show_image(bitmap)