def classification_queue_input(feature_extractor, image_path, logits_name, batch_size, num_classes): ''' Example function for performing image classification using a pre-trained network. This tests the filename queue as input method. Given a list of image files to process, these are fed into the filename queue and then the images are dequeued from the queue and classified. :param feature_extractor: object, TF feature extractor :param image_path: str, path to directory containing images :param logits_name: str, name of logits layer in network :param batch_size: int, batch size :param num_classes: int, number of classes for ImageNet (1000 or 1001) :return: ''' # Add a list of images to process image_files = utils.find_files(image_path, ("jpg", "png")) # Push the images through the network feature_extractor.enqueue_image_files(image_files) outputs = feature_extractor.feed_forward_batch([logits_name], fetch_images=True) # Compute the predictions, note that we asume layer_names[0] corresponds to logits predictions = np.squeeze(outputs[logits_name]) predictions = np.argmax(predictions, axis=1) for i in range(batch_size): image = misc.imread(image_files[i]) class_index = predictions[i] if num_classes == 1001 else predictions[i]+1 utils.display_imagenet_prediction(image, class_index)
def classification_queue_input(feature_extractor, image_path, logits_name, batch_size, num_classes): ''' Example function for performing image classification using a pre-trained network. This tests the filename queue as input method. Given a list of image files to process, these are fed into the filename queue and then the images are dequeued from the queue and classified. :param feature_extractor: object, TF feature extractor :param image_path: str, path to directory containing images :param logits_name: str, name of logits layer in network :param batch_size: int, batch size :param num_classes: int, number of classes for ImageNet (1000 or 1001) :return: ''' # Add a list of images to process image_files = utils.find_files(image_path, ("jpg", "png")) # Push the images through the network feature_extractor.enqueue_image_files(image_files) outputs = feature_extractor.feed_forward_batch([logits_name], fetch_images=True) # Compute the predictions, note that we asume layer_names[0] corresponds to logits predictions = np.squeeze(outputs[logits_name]) predictions = np.argmax(predictions, axis=1) for i in range(batch_size): image = misc.imread(image_files[i]) class_index = predictions[ i] if num_classes == 1001 else predictions[i] + 1 utils.display_imagenet_prediction(image, class_index)
def classification_queue_input(feature_extractor, image_path, logits_name, batch_size, num_classes): ''' 使用预先训练的图像分类的示例函数网络。这将测试文件名队列作为输入方法。列出了要处理的图像文件,这些文件将被输入文件名队列,然后图像从队列中出列并分类。 :param feature_extractor: object, TF feature extractor :param image_path: str, path to directory containing images :param logits_name: str, name of logits layer in network :param batch_size: int, batch size :param num_classes: int, number of classes for ImageNet (1000 or 1001) :return: ''' # Add a list of images to process excel_files = utils.find_files(image_path, ("csv", "xls")) # Push the images through the network feature_extractor.enqueue_image_files(excel_files) outputs = feature_extractor.feed_forward_batch([logits_name], fetch_images=True) # Compute the predictions, note that we asume layer_names[0] corresponds to logits predictions = np.squeeze(outputs[logits_name]) predictions = np.argmax(predictions, axis=1) for i in range(batch_size): image = misc.imread(excel_files[i]) class_index = predictions[i] if num_classes == 1001 else predictions[i]+1 utils.display_imagenet_prediction(image, class_index)
def classification_placeholder_input(feature_extractor, image_path, logits_name, batch_size, num_classes): ''' Example function for performing image classification using a pre-trained network. This function test simple the simple input method using placeholders. It loads one batch of images from disk, pre-processes them using Inception pre-processing and then feed-forwards them through the network. Input images and predicted ImageNet classes are displayed once finished. :param feature_extractor: object, TF feature extractor :param image_path: str, path to directory containing images :param logits_name: str, name of logits layer in network :param batch_size: int, batch size :param num_classes: int, number of classes for ImageNet (1000 or 1001) :return: ''' # Add a list of images to process image_files = utils.find_files(image_path, ("jpg", "png")) # Load one batch of images batch_images = np.zeros([ batch_size, feature_extractor.image_size, feature_extractor.image_size, 3 ], dtype=np.float32) for i in range(batch_size): # Note: this corresponds to 'inception' preprocessing. You don't need # this when using the queues as input pipeline, since the get_preprocessing() # function automatically determines it. image = misc.imread(image_files[i]) image = misc.imresize( image, (feature_extractor.image_size, feature_extractor.image_size)) image = (image / 255.0).astype(dtype=np.float32) image -= 0.5 image *= 2.0 batch_images[i] = image # Push the images through the network outputs = feature_extractor.feed_forward_batch([logits_name], batch_images, fetch_images=True) # Compute the predictions, note that we asume layer_names[0] corresponds to logits predictions = np.squeeze(outputs[logits_name]) predictions = np.argmax(predictions, axis=1) # Display predictions for i in range(batch_size): image = (((batch_images[i] / 2.0) + 0.5) * 255.0).astype(np.uint8) class_index = predictions[ i] if num_classes == 1001 else predictions[i] + 1 utils.display_imagenet_prediction(image, class_index)
def classification_placeholder_input(feature_extractor, image_path, logits_name, batch_size, num_classes): ''' Example function for performing image classification using a pre-trained network. This function test simple the simple input method using placeholders. It loads one batch of images from disk, pre-processes them using Inception pre-processing and then feed-forwards them through the network. Input images and predicted ImageNet classes are displayed once finished. :param feature_extractor: object, TF feature extractor :param image_path: str, path to directory containing images :param logits_name: str, name of logits layer in network :param batch_size: int, batch size :param num_classes: int, number of classes for ImageNet (1000 or 1001) :return: ''' # Add a list of images to process image_files = utils.find_files(image_path, ("jpg", "png")) # Load one batch of images batch_images = np.zeros([batch_size, feature_extractor.image_size, feature_extractor.image_size, 3], dtype=np.float32) for i in range(batch_size): # Note: this corresponds to 'inception' preprocessing. You don't need # this when using the queues as input pipeline, since the get_preprocessing() # function automatically determines it. image = misc.imread(image_files[i]) image = misc.imresize( image, (feature_extractor.image_size, feature_extractor.image_size)) image = (image/255.0).astype(dtype=np.float32) image -= 0.5 image *= 2.0 batch_images[i] = image # Push the images through the network outputs = feature_extractor.feed_forward_batch( [logits_name], batch_images, fetch_images=True) # Compute the predictions, note that we asume layer_names[0] corresponds to logits predictions = np.squeeze(outputs[logits_name]) predictions = np.argmax(predictions, axis=1) # Display predictions for i in range(batch_size): image = (((batch_images[i]/2.0)+0.5)*255.0).astype(np.uint8) class_index = predictions[i] if num_classes == 1001 else predictions[i]+1 utils.display_imagenet_prediction(image, class_index)
def classification_placeholder_input(feature_extractor, data, logits_name, batch_size, num_classes): ''' 使用预先训练的图像分类的示例函数网络。这个函数使用占位符测试简单的输入方法。 它从磁盘加载一批图像,使用Inception对它们进行预处理预处理,然后通过网络转发。输入图像完成后,将显示预测的ImageNet类。 :param feature_extractor: object, TF feature extractor :param image_path: str, path to directory containing images :param logits_name: str, name of logits layer in network :param batch_size: int, batch size :param num_classes: int, number of classes for ImageNet (1000 or 1001) :return: ''' # Add a list of images to process excel_files = utils.find_files(data, ("csv", "xls")) # Load one batch of images batch_images = np.zeros([batch_size, feature_extractor.image_size, feature_extractor.image_size, 3], dtype=np.float32) for i in range(batch_size): # Note: this corresponds to 'inception' preprocessing. You don't need # this when using the queues as input pipeline, since the get_preprocessing() # function automatically determines it. image = misc.imread(excel_files[i]) image = misc.imresize( image, (feature_extractor.image_size, feature_extractor.image_size)) image = (image/255.0).astype(dtype=np.float32) image -= 0.5 image *= 2.0 batch_images[i] = image # Push the images through the network outputs = feature_extractor.feed_forward_batch( [logits_name], batch_images, fetch_images=True) # Compute the predictions, note that we asume layer_names[0] corresponds to logits predictions = np.squeeze(outputs[logits_name]) predictions = np.argmax(predictions, axis=1) # Display predictions for i in range(batch_size): image = (((batch_images[i]/2.0)+0.5)*255.0).astype(np.uint8) class_index = predictions[i] if num_classes == 1001 else predictions[i]+1 utils.display_imagenet_prediction(image, class_index)
def feature_extraction_queue(feature_extractor, image_path, layer_names, batch_size, num_classes, num_images=100000): ''' Given a directory containing images, this function extracts features for all images. The layers to extract features from are specified as a list of strings. First, we seek for all images in the directory, sort the list and feed them to the filename queue. Then, batches are processed and features are stored in a large object `features`. :param feature_extractor: object, TF feature extractor :param image_path: str, path to directory containing images :param layer_names: list of str, list of layer names :param batch_size: int, batch size :param num_classes: int, number of classes for ImageNet (1000 or 1001) :param num_images: int, number of images to process (default=100000) :return: ''' # Add a list of images to process, note that the list is ordered. image_files = utils.find_files(image_path, ("jpg", "png")) num_images = min(len(image_files), num_images) image_files = image_files[0:num_images] image_files = natsorted(image_files, key=lambda y: y.lower()) num_examples = len(image_files) num_batches = int(np.ceil(num_examples / batch_size)) # Fill-up last batch so it is full (otherwise queue hangs) utils.fill_last_batch(image_files, batch_size) print("#" * 80) print("Batch Size: {}".format(batch_size)) print("Number of Examples: {}".format(num_examples)) print("Number of Batches: {}".format(num_batches)) # Add all the images to the filename queue feature_extractor.enqueue_image_files(image_files) # Initialize containers for storing processed filenames and features feature_dataset = {'filenames': []} for i, layer_name in enumerate(layer_names): layer_shape = feature_extractor.layer_size(layer_name) layer_shape[0] = len(image_files) # replace ? by number of examples feature_dataset[layer_name] = np.zeros(layer_shape, np.float32) print("Extracting features for layer '{}' with shape {}".format( layer_name, layer_shape)) print("#" * 80) # Perform feed-forward through the batches for batch_index in range(num_batches): t1 = time.time() # Feed-forward one batch through the network outputs = feature_extractor.feed_forward_batch(layer_names) for layer_name in layer_names: start = batch_index * batch_size end = start + batch_size feature_dataset[layer_name][start:end] = outputs[layer_name] # Save the filenames of the images in the batch feature_dataset['filenames'].extend(outputs['filenames']) t2 = time.time() examples_in_queue = outputs['examples_in_queue'] examples_per_second = batch_size / float(t2 - t1) print( "[{}] Batch {:04d}/{:04d}, Batch Size = {}, Examples in Queue = {}, Examples/Sec = {:.2f}" .format(datetime.now().strftime("%Y-%m-%d %H:%M"), batch_index + 1, num_batches, batch_size, examples_in_queue, examples_per_second)) # If the number of pre-processing threads >1 then the output order is # non-deterministic. Therefore, we order the outputs again by filenames so # the images and corresponding features are sorted in alphabetical order. if feature_extractor.num_preproc_threads > 1: utils.sort_feature_dataset(feature_dataset) # We cut-off the last part of the final batch since this was filled-up feature_dataset['filenames'] = feature_dataset['filenames'][0:num_examples] for layer_name in layer_names: feature_dataset[layer_name] = feature_dataset[layer_name][ 0:num_examples] return feature_dataset
# Support class by class feature extraction for large datasets class_by_class = args.class_by_class if class_by_class == 'yes': in_path = args.image_path out_file = args.out_file for file in os.listdir(in_path): # check if already extracted class_features_file = out_file + '.' + file class_path = os.path.join(in_path, file) if os.path.isfile(class_features_file): print("Already extracted: {}".format(class_features_file)) # check if filenames saved json_filename = in_path + file + '.json' if not os.path.isfile(json_filename): image_files = utils.find_files(class_path, ("jpg", "JPEG", "png")) num_images = min(len(image_files), 100000) image_files = image_files[0:num_images] # -- save filenames with open(json_filename, 'w') as json_file: json.dump(image_files, json_file) print('Save all filenames to {}'.format(json_filename)) continue # extract features for one class if os.path.isdir(class_path): feature_dataset = feature_extraction_queue( feature_extractor, class_path, layer_names, args.batch_size, args.num_classes) utils.write_hdf5(class_features_file, layer_names, feature_dataset) print("Successfully written features to: {}".format(
def feature_extraction_queue(feature_extractor, image_path, layer_names, batch_size, num_classes, num_excels=100000): ''' 给定包含图像的目录,此函数提取特征所有图像。指定要从中提取特征的图层作为字符串列表。首先,我们寻找目录中的所有图像, 对列表进行排序并将其馈送到文件名队列。那么,批次是已处理和特征存储在对象“features”中。 :param feature_extractor: object, TF feature extractor :param image_path: str, path to directory containing images :param layer_names: list of str, list of layer names :param batch_size: int, batch size :param num_classes: int, number of classes for ImageNet (1000 or 1001) :param num_images: int, number of images to process (default=100000) ''' # 处理的文件位置信息 excel_files = utils.find_files(image_path, ("xls", "csv")) num_excels = min(len(excel_files),num_excels) excel_files = excel_files[0:num_excels] num_examples = len(excel_files) num_batches = int(np.ceil(num_examples/batch_size)) # Fill-up last batch so it is full (otherwise queue hangs) utils.fill_last_batch(excel_files, batch_size)#批量大小 print("#"*80) print("Batch Size: {}".format(batch_size)) print("Number of Examples: {}".format(num_examples)) print("Number of Batches: {}".format(num_batches)) # Add all the images to the filename queue feature_extractor.enqueue_excel_files(excel_files) # Initialize containers for storing processed filenames and features feature_dataset = {'filenames': []} for i, layer_name in enumerate(layer_names): layer_shape = feature_extractor.layer_size(layer_name) layer_shape[0] = len(excel_files) # replace ? by number of examples feature_dataset[layer_name] = np.zeros(layer_shape, np.float32) print("Extracting features for layer '{}' with shape {}".format(layer_name, layer_shape)) print("#"*80) # Perform feed-forward through the batches for batch_index in range(num_batches): t1 = time.time() # Feed-forward one batch through the network outputs = feature_extractor.feed_forward_batch(layer_names) for layer_name in layer_names: start = batch_index*batch_size end = start+batch_size feature_dataset[layer_name][start:end] = outputs[layer_name] # Save the filenames of the images in the batch feature_dataset['filenames'].extend(outputs['filenames']) t2 = time.time() examples_in_queue = outputs['examples_in_queue'] examples_per_second = batch_size/float(t2-t1) print("[{}] Batch {:04d}/{:04d}, Batch Size = {}, Examples in Queue = {}, Examples/Sec = {:.2f}".format( datetime.now().strftime("%Y-%m-%d %H:%M"), batch_index+1, num_batches, batch_size, examples_in_queue, examples_per_second )) # If the number of pre-processing threads >1 then the output order is # non-deterministic. Therefore, we order the outputs again by filenames so # the images and corresponding features are sorted in alphabetical order. if feature_extractor.num_preproc_threads > 1: utils.sort_feature_dataset(feature_dataset) #因为已经装满了我们把最后一部分切断了 feature_dataset['filenames'] = feature_dataset['filenames'][0:num_examples] for layer_name in layer_names: feature_dataset[layer_name] = feature_dataset[layer_name][0:num_examples] return feature_dataset