示例#1
0
def tune(classifier_name, init_shape, final_shape):
    kernel = weights_file[classifier_name][classifier_name]['kernel:0'].value
    bias = weights_file[classifier_name][classifier_name]['bias:0'].value

    if init_shape == kernel.shape:
        new_kernel, new_bias = sample_tensors(
            weights_list=[kernel, bias],
            sampling_instructions=[
                final_shape[0], final_shape[1], final_shape[2], final_shape[3]
            ],
            axes=[
                [3]
            ],  # The one bias dimension corresponds to the last kernel dimension.
            init=['gaussian', 'zeros'],
            mean=0.0,
            stddev=0.005)

        del tuned_weights_file[classifier_name][classifier_name]['kernel:0']
        del tuned_weights_file[classifier_name][classifier_name]['bias:0']
        tuned_weights_file[classifier_name][classifier_name].create_dataset(
            name='kernel:0', data=new_kernel)
        tuned_weights_file[classifier_name][classifier_name].create_dataset(
            name='bias:0', data=new_bias)

        print("{}, shape is tuned:\n\tKernel: {} -> {}\n\tBias: {} -> {}\n".
              format(classifier_name, kernel.shape, new_kernel.shape,
                     bias.shape, new_bias.shape))
def apprentissage_trft():

	poids_source_lien = 'C:\\Users\\lamin\\Desktop\\PFE\\traffic_objetct_detection\\VGG_coco_SSD_300x300_iter_400000.h5'
	poids_destination_lien='C:\\Users\\lamin\\Desktop\\PFE\\traffic_objetct_detection\\VGG_SSD_300x300_8_classes.h5'
	shutil.copy(poids_source_lien, poids_destination_lien)
	poids_source_file = h5py.File(poids_source_lien, 'r')
	poids_destination_file = h5py.File(poids_destination_lien)

	classifier_nom = ['conv4_3_norm_mbox_conf',
                    	'fc7_mbox_conf',
                    	'conv6_2_mbox_conf',
                    	'conv7_2_mbox_conf',
                    	'conv8_2_mbox_conf',
                    	'conv9_2_mbox_conf']
    

	n_classes_source = 81
	classes_interet = [0, 3, 8, 1, 2, 10, 4, 6, 12]
	
	for nom in classifier_nom:
    	
    	kernel = poids_source_file[nom][nom]['kernel:0'].value
    	bias = poids_source_file[nom][nom]['bias:0'].value

    	
    	height, width, in_channels, out_channels = kernel.shape
    
    	
    	if isinstance(classes_interet, (list, tuple)):
        	subsampling_indices = []
        	for i in range(int(out_channels/n_classes_source)):
            	indices = np.array(classes_interet) + i * n_classes_source
            	subsampling_indices.append(indices)
        	subsampling_indices = list(np.concatenate(subsampling_indices))
    	elif isinstance(classes_interet, int):
        	subsampling_indices = int(classes_interet * (out_channels/n_classes_source))
    	else:
        	raise ValueError("`classes_interet` doit etre entier ou(list/tuple).")
   
    	new_kernel, new_bias = sample_tensors(weights_list=[kernel, bias],
                                          	sampling_instructions=[height, width, in_channels, subsampling_indices],
                                          	axes=[[3]], 
                                          	init=['gaussian', 'zeros'],
                                          	mean=0.0,
                                          	stddev=0.005)
    
    	del poids_destination_file[name][name]['kernel:0']
    	del poids_destination_file[name][name]['bias:0']
    	
    	poids_destination_file[name][name].create_dataset(name='kernel:0', data=new_kernel)
    	poids_destination_file[name][name].create_dataset(name='bias:0', data=new_bias)

	poids_destination_file.flush()

	
示例#3
0
        for i in range(int(out_channels/n_classes_source)):
            indices = np.array(classes_of_interest) + i * n_classes_source
            subsampling_indices.append(indices)
        subsampling_indices = list(np.concatenate(subsampling_indices))
    elif isinstance(classes_of_interest, int):
        subsampling_indices = int(classes_of_interest * (out_channels/n_classes_source))
    else:
        raise ValueError("`classes_of_interest` must be either an integer or a list/tuple.")
    
    # Sub-sample the kernel and bias.
    # The `sample_tensors()` function used below provides extensive
    # documentation, so don't hesitate to read it if you want to know
    # what exactly is going on here.
    new_kernel, new_bias = sample_tensors(weights_list=[kernel, bias],
                                          sampling_instructions=[height, width, in_channels, subsampling_indices],
                                          axes=[[3]], # The one bias dimension corresponds to the last kernel dimension.
                                          init=['gaussian', 'zeros'],
                                          mean=0.0,
                                          stddev=0.005)
    
    # Delete the old weights from the destination file.
    del weights_destination_file[name][name]['kernel:0']
    del weights_destination_file[name][name]['bias:0']
    # Create new datasets for the sub-sampled weights.
    weights_destination_file[name][name].create_dataset(name='kernel:0', data=new_kernel)
    weights_destination_file[name][name].create_dataset(name='bias:0', data=new_bias)

# Make sure all data is written to our output file before this sub-routine exits.
weights_destination_file.flush()

conv4_3_norm_mbox_conf_kernel = weights_destination_file[classifier_names[0]][classifier_names[0]]['kernel:0']
conv4_3_norm_mbox_conf_bias = weights_destination_file[classifier_names[0]][classifier_names[0]]['bias:0']
示例#4
0
def sample_model_for_TR():

	weights_source_path = '/Users/royhirsch/Downloads/VGG_coco_SSD_512x512_iter_360000.h5'
	weights_destination_path = '/Users/royhirsch/Downloads/VGG_coco_SSD_512x512_iter_360000_2_classes.h5'

	# Make a copy of the weights file.
	shutil.copy(weights_source_path, weights_destination_path)

	# Load both the source weights file and the copy we made.
	# We will load the original weights file in read-only mode so that we can't mess up anything.
	weights_source_file = h5py.File(weights_source_path, 'r')
	weights_destination_file = h5py.File(weights_destination_path)

	classifier_names = ['conv4_3_norm_mbox_conf',
	                    'fc7_mbox_conf',
	                    'conv6_2_mbox_conf',
	                    'conv7_2_mbox_conf',
	                    'conv8_2_mbox_conf',
	                    'conv9_2_mbox_conf',
	                    'conv10_2_mbox_conf']

	# TODO: Set the number of classes in the source weights file. Note that this number must include
	#       the background class, so for MS COCO's 80 classes, this must be 80 + 1 = 81.
	n_classes_source = 81
	# TODO: Set the indices of the classes that you want to pick for the sub-sampled weight tensors.
	#       In case you would like to just randomly sample a certain number of classes, you can just set
	#       `classes_of_interest` to an integer instead of the list below. Either way, don't forget to
	#       include the background class. That is, if you set an integer, and you want `n` positive classes,
	#       then you must set `classes_of_interest = n + 1`.
	classes_of_interest = [0, 6]
	# classes_of_interest = 12 # Uncomment this in case you want to just randomly sub-sample the last axis instead of providing a list of indices.

	for name in classifier_names:
		# Get the trained weights for this layer from the source HDF5 weights file.
		kernel = weights_source_file[name][name]['kernel:0'].value
		bias = weights_source_file[name][name]['bias:0'].value

		# Get the shape of the kernel. We're interested in sub-sampling
		# the last dimension, 'o'.
		height, width, in_channels, out_channels = kernel.shape

		# Compute the indices of the elements we want to sub-sample.
		# Keep in mind that each classification predictor layer predicts multiple
		# bounding boxes for every spatial location, so we want to sub-sample
		# the relevant classes for each of these boxes.
		if isinstance(classes_of_interest, (list, tuple)):
			subsampling_indices = []
			for i in range(int(out_channels / n_classes_source)):
				indices = np.array(classes_of_interest) + i * n_classes_source
				subsampling_indices.append(indices)
			subsampling_indices = list(np.concatenate(subsampling_indices))
		elif isinstance(classes_of_interest, int):
			subsampling_indices = int(classes_of_interest * (out_channels / n_classes_source))
		else:
			raise ValueError("`classes_of_interest` must be either an integer or a list/tuple.")

		# Sub-sample the kernel and bias.
		# The `sample_tensors()` function used below provides extensive
		# documentation, so don't hesitate to read it if you want to know
		# what exactly is going on here.
		new_kernel, new_bias = sample_tensors(weights_list=[kernel, bias],
		                                      sampling_instructions=[height, width, in_channels, subsampling_indices],
		                                      axes=[[3]],
		                                      # The one bias dimension corresponds to the last kernel dimension.
		                                      init=['gaussian', 'zeros'],
		                                      mean=0.0,
		                                      stddev=0.005)

		# Delete the old weights from the destination file.
		del weights_destination_file[name][name]['kernel:0']
		del weights_destination_file[name][name]['bias:0']
		# Create new datasets for the sub-sampled weights.
		weights_destination_file[name][name].create_dataset(name='kernel:0', data=new_kernel)
		weights_destination_file[name][name].create_dataset(name='bias:0', data=new_bias)

	# Make sure all data is written to our output file before this sub-routine exits.
	weights_destination_file.flush()

	conv4_3_norm_mbox_conf_kernel = weights_destination_file[classifier_names[0]][classifier_names[0]]['kernel:0']
	conv4_3_norm_mbox_conf_bias = weights_destination_file[classifier_names[0]][classifier_names[0]]['bias:0']

	print("Shape of the '{}' weights:".format(classifier_names[0]))
	print()
	print("kernel:\t", conv4_3_norm_mbox_conf_kernel.shape)
	print("bias:\t", conv4_3_norm_mbox_conf_bias.shape)
示例#5
0
    def init_weights(self, classes=''):
        # saves coco trained weights in destination # that can be loaded by model
        # if passing classes, original coco classes may be passed, e.g 1=person,9=boat
        #looky here https://github.com/cocodataset/cocoapi/blob/master/PythonAPI/pycocoDemo.ipynb
        print('\nloading weights')
        #pdb.set_trace()
        if classes == '':
            classes = self.classes

        weights_source_path = f'{json_conf["ssd_base_weights"]}'  # /home/gidish/models/VGG_coco_SSD_300x300_iter_400000.h5'

        weights_destination_path = f'{json_conf["ssd_base_weights"]}_subsampled_{len(classes)}_classes.h5'
        shutil.copy(weights_source_path, weights_destination_path)
        weights_source_file = h5py.File(weights_source_path, 'r')
        weights_destination_file = h5py.File(weights_destination_path)

        classifier_names = [
            'conv4_3_norm_mbox_conf', 'fc7_mbox_conf', 'conv6_2_mbox_conf',
            'conv7_2_mbox_conf', 'conv8_2_mbox_conf', 'conv9_2_mbox_conf'
        ]

        n_classes_source = 81

        classes_of_interest = list(classes)
        print('classes are:', classes_of_interest)
        subsampling_indices = []
        for i in range(int(324 / n_classes_source)):
            indices = np.array(classes_of_interest) + i * n_classes_source
            subsampling_indices.append(indices)
        subsampling_indices = list(np.concatenate(subsampling_indices))
        print('last layer indicies', subsampling_indices)

        for name in classifier_names:
            # Get the trained weights for this layer from the source HDF5 weights file.
            kernel = weights_source_file[name][name]['kernel:0'].value
            bias = weights_source_file[name][name]['bias:0'].value

            # Get the shape of the kernel. We're interested in sub-sampling
            # the last dimension, 'o'.
            height, width, in_channels, out_channels = kernel.shape

            # Compute the indices of the elements we want to sub-sample.
            # Keep in mind that each classification predictor layer predicts multiple
            # bounding boxes for every spatial location, so we want to sub-sample
            # the relevant classes for each of these boxes.
            if isinstance(classes_of_interest, (list, tuple)):
                subsampling_indices = []
                for i in range(int(out_channels / n_classes_source)):
                    indices = np.array(
                        classes_of_interest) + i * n_classes_source
                    subsampling_indices.append(indices)
                subsampling_indices = list(np.concatenate(subsampling_indices))
            elif isinstance(classes_of_interest, int):
                subsampling_indices = int(classes_of_interest *
                                          (out_channels / n_classes_source))
            else:
                raise ValueError(
                    "`classes_of_interest` must be either an integer or a list/tuple."
                )

            # Sub-sample the kernel and bias.
            # The `sample_tensors()` function used below provides extensive
            # documentation, so don't hesitate to read it if you want to know
            # what exactly is going on here.
            new_kernel, new_bias = sample_tensors(
                weights_list=[kernel, bias],
                sampling_instructions=[
                    height, width, in_channels, subsampling_indices
                ],
                axes=[
                    [3]
                ],  # The one bias dimension corresponds to the last kernel dimension.
                init=['gaussian', 'zeros'],
                mean=0.0,
                stddev=0.005)

            # Delete the old weights from the destination file.
            del weights_destination_file[name][name]['kernel:0']
            del weights_destination_file[name][name]['bias:0']
            # Create new datasets for the sub-sampled weights.
            weights_destination_file[name][name].create_dataset(
                name='kernel:0', data=new_kernel)
            weights_destination_file[name][name].create_dataset(name='bias:0',
                                                                data=new_bias)

        # Make sure all data is written to our output file before this sub-routine exits.
        weights_destination_file.flush()
        conv4_3_norm_mbox_conf_kernel = weights_destination_file[
            classifier_names[0]][classifier_names[0]]['kernel:0']
        conv4_3_norm_mbox_conf_bias = weights_destination_file[
            classifier_names[0]][classifier_names[0]]['bias:0']

        print("Shape of the '{}' weights:".format(classifier_names[0]))
        print()
        print("kernel:\t", conv4_3_norm_mbox_conf_kernel.shape)
        print("bias:\t", conv4_3_norm_mbox_conf_bias.shape)
        return weights_destination_path