def test_initialize_weights_gdn(self): """Tests the function `initialize_weights_gdn`. An image is saved at "tfutils/pseudo_visualization/initialize_weights_gdn.png". The test is successful if the image contains a symmetric matrix. Besides, the GDN/IGDN weights must belong to the expected range. """ nb_maps = 12 min_gamma = 2.e-5 node_gamma = tfuls.initialize_weights_gdn(nb_maps, min_gamma) with tf.Session() as sess: gamma = sess.run(node_gamma) minimum = numpy.amin(gamma) maximum = numpy.amax(gamma) print('The GDN/IGDN weights must belong to [{}, 0.01].'.format( min_gamma)) print('Minimum of the GDN/IGDN weights: {}'.format(minimum)) print('Maximum of the GDN/IGDN weights: {}'.format(maximum)) image_uint8 = numpy.round(255. * (gamma - minimum) / (maximum - minimum)).astype(numpy.uint8) tls.save_image( 'tfutils/pseudo_visualization/initialize_weights_gdn.png', image_uint8)
def test_save_image(self): """Tests the function `save_image`. An image is saved at "tools/pseudo_visualization/save_image.png". The test is successful if this image is identical to "tools/pseudo_data/rgb_web.png". """ rgb_uint8 = tls.read_image_mode('tools/pseudo_data/rgb_web.jpg', 'RGB') tls.save_image('tools/pseudo_visualization/save_image.png', rgb_uint8)
def test_rows_to_images(self): """Tests the function `rows_to_images`. The test is successful if, for i = 0 ... 3, "tools/pseudo_visualization/rows_to_images/rows_to_images_i.png" is identical to "tools/pseudo_visualization/images_to_rows/images_to_rows_i.png". """ rows_uint8 = numpy.load('tools/pseudo_data/rows_uint8.npy') images_uint8 = tls.rows_to_images(rows_uint8, 64, 64) for i in range(images_uint8.shape[3]): tls.save_image('tools/pseudo_visualization/rows_to_images/rows_to_images_{}.png'.format(i), images_uint8[:, :, :, i])
def write_luminances(reference_uint8, path_to_before_jpeg2000): """Writes the luminance images. Parameters ---------- reference_uint8 : numpy.ndarray 3D array with data-type `numpy.uint8`. Luminance images. `reference_uint8[i, :, :]` is the ith luminance image. path_to_before_jpeg2000 : str Path to the folder in which the luminance images are saved. """ for i in range(reference_uint8.shape[0]): tls.save_image(os.path.join(path_to_before_jpeg2000, 'reference_{}.png'.format(i)), reference_uint8[i, :, :])
def test_read_400(self): """Tests the function `read_400`. An image is saved at "hevc/pseudo_visualization/read_400.png". The test is successful if this image is identical to the image at "hevc/pseudo_visualization/write_400.png". """ height = 525 width = 700 nb_frames = 1 data_type = numpy.uint8 expanded_luminance_uint8 = hevc.hevc.read_400( 'hevc/pseudo_data/luminance_nightshot.yuv', height, width, nb_frames, data_type) luminance_uint8 = numpy.squeeze(expanded_luminance_uint8, axis=2) tls.save_image('hevc/pseudo_visualization/read_400.png', luminance_uint8)
def test_crop_option_2d(self): """Tests the function `crop_option_2d`. A 1st image is saved at "tools/pseudo_visualization/crop_option_2d/luminance_image.png". A 2nd image is saved at "tools/pseudo_visualization/crop_option_2d/crop_random.png". A 3rd image is saved at "tools/pseudo_visualization/crop_option_2d/crop_center.png". The test is successful if the 2nd image is a random crop of the 1st image and the 3rd image is a central crop of the 1st image. """ width_crop = 64 luminance_uint8 = numpy.load('tools/pseudo_data/luminances_uint8.npy')[0, :, :, 0] crop_0 = tls.crop_option_2d(luminance_uint8, width_crop, True) crop_1 = tls.crop_option_2d(luminance_uint8, width_crop, False) tls.save_image('tools/pseudo_visualization/crop_option_2d/luminance_image.png', luminance_uint8) tls.save_image('tools/pseudo_visualization/crop_option_2d/crop_random.png', crop_0) tls.save_image('tools/pseudo_visualization/crop_option_2d/crop_center.png', crop_1)
def test_crop_repeat_2d(self): """Tests the function `crop_repeat_2d`. A 1st image is saved at "tools/pseudo_visualization/crop_repeat_2d/luminance_image.png". A 2nd image is saved at "tools/pseudo_visualization/crop_repeat_2d/crop_0.png". A 3rd image is saved at "tools/pseudo_visualization/crop_repeat_2d/crop_1.png". The test is successful if the 2nd image is an enlarged crop of the bottom-left of the 1st image and the 3rd image is an enlarged crop of the top-right of the 1st image. """ image_uint8 = numpy.load('tools/pseudo_data/luminances_uint8.npy')[0, :, :, 0] crop_0 = tls.crop_repeat_2d(image_uint8, image_uint8.shape[0] - 81, 0) crop_1 = tls.crop_repeat_2d(image_uint8, 0, image_uint8.shape[1] - 81) tls.save_image('tools/pseudo_visualization/crop_repeat_2d/luminance_image.png', image_uint8) tls.save_image('tools/pseudo_visualization/crop_repeat_2d/crop_0.png', crop_0) tls.save_image('tools/pseudo_visualization/crop_repeat_2d/crop_1.png', crop_1)
def test_create_imagenet(self): """Tests the function `create_imagenet` in the file "datasets/imagenet/imagenet.py". An image is saved at "datasets/imagenet/pseudo_visualization/create_imagenet/training_i.png", i in {0, 1}. An image is saved at "datasets/imagenet/pseudo_visualization/create_imagenet/validation_i.png", i in {0, 1}. The test is successful if each saved image is the luminance crop of a different RGB image in the folder "datasets/imagenet/pseudo_data/". Besides, if the saved image is named "training_...", it must be a random crop. If the saved image is named "validation_...", it must be a central crop. """ width_crop = 192 nb_training = 2 nb_validation = 2 path_to_training = 'datasets/imagenet/pseudo_data/pseudo_training_data.npy' path_to_validation = 'datasets/imagenet/pseudo_data/pseudo_validation_data.npy' # The images in the folder "datasets/imagenet/pseudo_data/" # are large. Therefore, none of them is dumped. datasets.imagenet.imagenet.create_imagenet('datasets/imagenet/pseudo_data/', width_crop, nb_training, nb_validation, path_to_training, path_to_validation) pseudo_training_data = numpy.load(path_to_training) path_to_folder_vis = 'datasets/imagenet/pseudo_visualization/' for i in range(nb_training): tls.save_image(os.path.join(path_to_folder_vis, 'training_{}.png'.format(i)), pseudo_training_data[i, :, :, 0]) pseudo_validation_data = numpy.load(path_to_validation) for i in range(nb_validation): tls.save_image(os.path.join(path_to_folder_vis, 'validation_{}.png'.format(i)), pseudo_validation_data[i, :, :, 0])
def write_digits(reference_uint8, path_to_before): """Writes the RGB digits. Parameters ---------- reference_uint8 : numpy.ndarray 2D array with data-type `numpy.uint8`. RGB digits. `reference_uint8[i, :]` contains the ith RGB digit. path_to_before : str Path to the folder in which the RGB digits are saved. """ # The function `tls.row_to_images` checks that # `reference_uint8.dtype` is equal to `numpy.uint8` # and `reference_uint8.ndim` is equal to 2. images_uint8 = tls.rows_to_images(reference_uint8, 32, 32) for i in range(images_uint8.shape[3]): tls.save_image( os.path.join(path_to_before, 'reference_{}.png'.format(i)), images_uint8[:, :, :, i])
def test_write_400(self): """Tests the function `write_400`. An image is saved at "hevc/pseudo_visualization/write_400.png". The test is successful if this image is identical to the image at "hevc/pseudo_visualization/read_400.png". """ path_to_yuv = 'hevc/pseudo_data/luminance_nightshot.yuv' rgb_uint8 = tls.read_image_mode('hevc/pseudo_data/rgb_nightshot.jpg', 'RGB') luminance_uint8 = tls.rgb_to_ycbcr(rgb_uint8)[:, :, 0] tls.save_image('hevc/pseudo_visualization/write_400.png', luminance_uint8) if os.path.isfile(path_to_yuv): print('"{}" exists. Remove it manually and restart the same test.'. format(path_to_yuv)) else: hevc.hevc.write_400(numpy.expand_dims(luminance_uint8, axis=2), path_to_yuv)
def test_compress_hevc(self): """Tests the function `compress_hevc`. A 1st image is saved at "hevc/pseudo_visualization/compress_hevc/luminance_before.png". A 2nd image is saved at "hevc/pseudo_visualization/compress_hevc/luminance_after.png". The test is successful if the 2nd image corresponds to the 1st image with HEVC compression artefacts. """ path_to_before_hevc = 'hevc/temp/luminance_before_hevc.yuv' path_to_after_hevc = 'hevc/temp/luminance_after_hevc.yuv' path_to_cfg = 'hevc/configuration/intra.cfg' path_to_bitstream = 'hevc/temp/bitstream.bin' qp = 42 rgb_uint8 = tls.read_image_mode('hevc/pseudo_data/rgb_nightshot.jpg', 'RGB') (height_initial, width_initial, _) = rgb_uint8.shape height_surplus = height_initial % 8 width_surplus = width_initial % 8 luminance_uint8 = tls.rgb_to_ycbcr(rgb_uint8)[0:height_initial - height_surplus, 0:width_initial - width_surplus, 0] tls.save_image( 'hevc/pseudo_visualization/compress_hevc/luminance_before.png', luminance_uint8) luminance_before_hevc_uint8 = numpy.expand_dims(luminance_uint8, axis=2) luminance_after_hevc_uint8 = hevc.hevc.compress_hevc( luminance_before_hevc_uint8, path_to_before_hevc, path_to_after_hevc, path_to_cfg, path_to_bitstream, qp, True) tls.save_image( 'hevc/pseudo_visualization/compress_hevc/luminance_after.png', numpy.squeeze(luminance_after_hevc_uint8, axis=2))
def checking_p_3(self, str_scope, index_gdn, path): """Saves the image of the GDN/IGDN weights. Parameters ---------- str_scope : str Scope of the GDN/IGDN weights. index_gdn : int Index of the GDN/IGDN weights. The index belongs to [|1, 6|]. path : str Path to the saved image. The path ends with ".png". """ with tf.variable_scope(str_scope, reuse=True): gamma_float32 = tf.get_variable('gamma_{}'.format(index_gdn), dtype=tf.float32).eval() minimum = numpy.amin(gamma_float32) maximum = numpy.amax(gamma_float32) gamma_uint8 = numpy.round(255. * (gamma_float32 - minimum) / (maximum - minimum)).astype(numpy.uint8) tls.save_image(path, gamma_uint8)
"""A script to create the Kodak test set.""" import argparse import numpy import datasets.kodak.kodak import tools.tools as tls if __name__ == '__main__': parser = argparse.ArgumentParser(description='Creates the Kodak test set.') parser.parse_args() path_to_kodak = 'datasets/kodak/results/kodak.npy' path_to_list_rotation = 'datasets/kodak/results/list_rotation.pkl' datasets.kodak.kodak.create_kodak('http://r0k.us/graphics/kodak/kodak/', 'datasets/kodak/data', path_to_kodak, path_to_list_rotation) # Two luminance images from the Kodak test set are checked visually. reference_uint8 = numpy.load(path_to_kodak) tls.save_image('datasets/kodak/visualization/luminance_3.png', reference_uint8[2, :, :]) tls.save_image('datasets/kodak/visualization/luminance_11.png', reference_uint8[10, :, :])
# Definissez la comvoliution à laquelle correspond diff_mean_combo() : def diff_mean_combo_2(x): return # Definissez la comvoliution à laquelle correspond mean_3x3_combo(x, 2) : def mean_3x3_combo_x2(x): return # Exercice 7: # Vous allez à présent programmer un détecteur de bord. Appliquez votre filtre # build_conv_grad_sum_xy à une image I pour estimer son gradient grad_I. # Construisez alors une image J valant 0 sur tous les points où abs(grad_I) < 100 # et 1 ailleurs. # Faites variez ce seuil pour en regarder les effets def extraction_des_bords(x): return # Exercice 8 # if __name__ == "__main__": x = load_image('isha.jpg') # A vous de jouez ! # N'oubliez pas de définir rgb_to_grey x = rgb_to_grey(x) save_image(x, 'isha2.jpg')
def activate_latent_variable(sess, isolated_decoder, h_in, w_in, bin_widths, row_activation, col_activation, idx_map_activation, activation_value, map_mean, height_crop, width_crop, path_to_crop): """Activates one latent variable and deactivates the others. One latent variable is activated and the others are deactivated. Then, the latent variable feature maps are quantized. Finally, the quantized latent variable feature maps are passed through the decoder of the entropy autoencoder. Parameters ---------- sess : Session Session that runs the graph. isolated_decoder : IsolatedDecoder Decoder of the entropy autoencoder. The graph of the decoder is built to process one example at a time. h_in : int Height of the images returned by the isolated decoder. w_in : int Width of the images returned by the isolated decoder. bin_widths : numpy.ndarray 1D array with data-type `numpy.float32`. Quantization bin widths at the end of the training. row_activation : int Row of the activated latent variable in the latent variable feature map of index `idx_map_activation`. col_activation : int Column of the activated latent variable in the latent variable feature map of index `idx_map_activation`. idx_map_activation : int Index of the latent variable feature map containing the activated latent variable. activation_value : float Activation value. map_mean : numpy.ndarray 1D array with data-type `numpy.float32`. Latent variable feature map means. height_crop : int Height of the crop of the decoder output. width_crop : int Width of the crop of the decoder output. path_to_crop : str Path to the saved crop of the decoder output. The path ends with ".png". Raises ------ ValueError If `row_activation` is not strictly positive. ValueError If `col_activation` is not strictly positive. """ if row_activation <= 0: raise ValueError('`row_activation` is not strictly positive.') if col_activation <= 0: raise ValueError('`col_activation` is not strictly positive.') y_float32 = numpy.tile(numpy.reshape(map_mean, (1, 1, 1, csts.NB_MAPS_3)), (1, h_in//csts.STRIDE_PROD, w_in//csts.STRIDE_PROD, 1)) y_float32[0, row_activation, col_activation, idx_map_activation] = activation_value quantized_y_float32 = tls.quantize_per_map(y_float32, bin_widths) reconstruction_float32 = sess.run( isolated_decoder.node_reconstruction, feed_dict={isolated_decoder.node_quantized_y:quantized_y_float32} ) reconstruction_uint8 = numpy.squeeze(tls.cast_bt601(reconstruction_float32), axis=(0, 3)) # The ratio between the feature map height and the # decoded image height is equal to `csts.STRIDE_PROD`. row_offset = (row_activation - 1)*csts.STRIDE_PROD col_offset = (col_activation - 1)*csts.STRIDE_PROD tls.save_image(path_to_crop, reconstruction_uint8[row_offset:row_offset + height_crop, col_offset:col_offset + width_crop])
def mask_maps(y_float32, sess, isolated_decoder, bin_widths, idx_unmasked_map, map_mean, height_crop, width_crop, paths): """Masks all the latent variable feature maps except one. All the latent variable feature maps except one are masked. Then, the latent variable feature maps are quantized. Finally, the quantized latent variable feature maps are passed through the decoder of the entropy autoencoder. Parameters ---------- y_float32 : numpy.ndarray 4D array with data-type `numpy.float32`. Latent variables. `y_float32[i, :, :, j]` is the jth latent variable feature map of the ith example. sess : Session Session that runs the graph. isolated_decoder : IsolatedDecoder Decoder of the entropy autoencoder. The graph of the decoder is built to process one example at a time. bin_widths : numpy.ndarray 1D array with data-type `numpy.float32`. Quantization bin widths at the end of the training. idx_unmasked_map : int Index of the unmasked latent variable feature map. map_mean : numpy.ndarray 1D array with data-type `numpy.float32`. Latent variable feature map means. height_crop : int Height of the crop of the decoder output. width_crop : int Width of the crop of the decoder output. paths : list The ith string in this list is the path to the ith saved crop of the decoder output. Each path ends with ".png". Raises ------ ValueError If `len(paths)` is not equal to `y_float32.shape[0]`. """ if len(paths) != y_float32.shape[0]: raise ValueError('`len(paths)` is not equal to `y_float32.shape[0]`.') # The same latent variable feature map is # iteratively overwritten in the loop below. masked_y_float32 = numpy.tile(numpy.reshape(map_mean, (1, 1, 1, y_float32.shape[3])), (1, y_float32.shape[1], y_float32.shape[2], 1)) for i in range(y_float32.shape[0]): masked_y_float32[0, :, :, idx_unmasked_map] = y_float32[i, :, :, idx_unmasked_map] quantized_y_float32 = tls.quantize_per_map(masked_y_float32, bin_widths) reconstruction_float32 = sess.run( isolated_decoder.node_reconstruction, feed_dict={isolated_decoder.node_quantized_y:quantized_y_float32} ) reconstruction_uint8 = numpy.squeeze(tls.cast_bt601(reconstruction_float32), axis=(0, 3)) tls.save_image(paths[i], reconstruction_uint8[0:height_crop, 0:width_crop])
def deepid(sample_path, predict): # align for image out_url = './util/data/' sample = align_face.alignMain(sample_path, out_url, predict) if sample == "Unable_align": return sample, None else: sample = tools.load_sample(sample) # load model from pre-trained model model = mx.model.FeedForward.load(prefix=a_args.prefix, epoch=a_args.epoch, ctx=mx.gpu(), numpy_batch_size=1) # create feature_extractor for deepid internals = model.symbol.get_internals() # get feature layer symbol out of internals fea_symbol = internals["fc3_output"] feature_extractor = mx.model.FeedForward(ctx=mx.gpu(), symbol=fea_symbol, numpy_batch_size=1, arg_params=model.arg_params, aux_params=model.aux_params, allow_extra_params=True) #exact sample_feature sample_feature = feature_extractor.predict(sample) # image for own class people = os.listdir('./people_image/') people_feature = [] people_label = [] if len(people) == 0: tools.save_image(sample_path, './people_image/x_' + str(len(people)), sample_feature) return 'x' else: for i in people: path = './people_image/' + i img = os.listdir(path) random.shuffle(img) k = 0 # load feature from file for j in img: if '.npy' in j and k < 5: k += 1 people_feature.append(np.load(path + '/' + j)) people_label.append(i) # calculate the ratio of these images A, G = tools.load_A_G(a_args.A, a_args.G) people_ratio = [ jb_model.Verify(A, G, sample_feature, people_feature[i]) for i in xrange(len(people_label)) ] people_result = tools.predict_face(people_ratio, 13, people_label) if people_result.count(0) == len(people_result): return 'unknow_person', sample_feature else: dic = {i: people_result.count(i) for i in people} fname = max(dic, key=dic.get) return fname, sample_feature
help= 'path to the folder storing the downloaded archive containing the original BSDS dataset', default='', metavar='') args = parser.parse_args() path_to_bsds = 'datasets/bsds/results/bsds.npy' path_to_list_rotation = 'datasets/bsds/results/list_rotation.pkl' # If `args.path_to_folder_tar` is equal to '', this # means that there is no need to download and extract # the archive "BSDS300-images.tgz". if args.path_to_folder_tar: path_to_tar = os.path.join(args.path_to_folder_tar, 'BSDS300-images.tgz') else: path_to_tar = '' datasets.bsds.bsds.create_bsds( 'https://www2.eecs.berkeley.edu/Research/Projects/CS/vision/bsds/BSDS300-images.tgz', args.path_to_folder_rgbs, path_to_bsds, path_to_list_rotation, path_to_tar=path_to_tar) # Two luminance images from the BSDS test set are checked visually. reference_uint8 = numpy.load(path_to_bsds) tls.save_image('datasets/bsds/visualization/luminance_7.png', reference_uint8[7, :, :]) tls.save_image('datasets/bsds/visualization/luminance_39.png', reference_uint8[39, :, :])