def load_batch(dataset, batch_size=28, height=224, width=224, is_training=False): """Loads a single batch of data. Args: dataset: The dataset to load. batch_size: The number of images in the batch. height: The size of each image after preprocessing. width: The size of each image after preprocessing. is_training: Whether or not we're currently training or evaluating. Returns: images: A Tensor of size [batch_size, height, width, 3], image samples that have been preprocessed. images_raw: A Tensor of size [batch_size, height, width, 3], image samples that can be used for visualization. labels: A Tensor of size [batch_size], whose values range between 0 and dataset.num_classes. """ data_provider = slim.dataset_data_provider.DatasetDataProvider( dataset, common_queue_capacity=32, common_queue_min=8) image_raw, label = data_provider.get(['image', 'label']) # Preprocess the image for display purposes. image_raw_disp = tf.expand_dims(image_raw, 0) image_raw_disp = tf.image.resize_images(image_raw_disp, [height, width]) image_raw_disp = tf.squeeze(image_raw_disp) # Batch it up. if is_training: image = my_resnet_preprocessing.preprocess_image( image_raw, height, width, is_training=is_training) # To Use bot: flipped and non-flipped image, use: #image = tf.stack([image, image_flipped], 0) #label = tf.stack([label, label], 0) images, images_raw, labels = tf.train.batch( [image, image_raw_disp, label], batch_size=batch_size, num_threads=1, capacity=5000) return images, image_raw_disp, labels else: image = my_resnet_preprocessing.preprocess_image( image_raw, height, width, is_training=is_training) images, labels = tf.train.batch([image, label], batch_size=batch_size, num_threads=1, capacity=2 * batch_size) return images, image_raw_disp, labels
def show_examples(number=4): with tf.Graph().as_default(): dataset = get_split('train_set3', data_dir) data_provider = slim.dataset_data_provider.DatasetDataProvider( dataset, common_queue_capacity=32, common_queue_min=8) image_raw, label = data_provider.get(['image', 'label']) image = my_resnet_preprocessing.preprocess_image(image_raw, 224, 224, is_training=True) with tf.Session() as sess: with slim.queues.QueueRunners(sess): for i in range(number): np_image, np_label = sess.run([image, label]) height, width, _ = np_image.shape class_name = name = dataset.labels_to_names[np_label] plt.figure() plt.imshow(np_image) plt.title('%s, %d x %d' % (name, height, width)) plt.axis('off') plt.show() #show_examples(10)
def show_examples(number_images=4, split_name='train_set3', data_dir="mydata/PlantClefTraining2015"): """Shows a number of examples of the unprocessed flowerdataset Args: number_images: The number of images to show split_name: A train/validation split name. dataset_dir: The base directory of the dataset sources. Returns: Visualisation of images """ with tf.Graph().as_default(): dataset = get_split('train_set3', data_dir) data_provider = slim.dataset_data_provider.DatasetDataProvider( dataset, common_queue_capacity=32, common_queue_min=8) image_raw, label = data_provider.get(['image', 'label']) image = my_resnet_preprocessing.preprocess_image(image_raw, 224, 224, is_training=True) with tf.Session() as sess: with slim.queues.QueueRunners(sess): for i in range(number): np_image, np_label = sess.run([image, label]) height, width, _ = np_image.shape class_name = name = dataset.labels_to_names[np_label] plt.figure() plt.imshow(np_image) plt.title('%s, %d x %d' % (name, height, width)) plt.axis('off') plt.show()
def find_most_accurate( label_directory="mydata/PlantClefTraining2015", dataset_dir="mydata/train/", model_directory="mydata/resnet_finetuned_plantclef2015_2/model.ckpt-150000", dict_directory="mydata/labels", dict_name="most_accurate_images.txt"): """ Finds the images in Filepath, that have the highest output probability, while beeing true. Args: label_directory: Where to find the dictionary mapping from class_id to one-hot-labels dataset_dir: where to find the images model_directory: where to find your models checkpoints dict_directory: where to save the images with the highest activation dict_name: name of the txt-file of the dictionary containing images with the highest activation Returns: best_act: dictionary containing images with the highest activation """ best_act = {} label_dict = dataset_utils.read_label_file(label_directory) label_dict = dict([int(v), k] for k, v in label_dict.items()) with tf.Graph().as_default(): X = tf.placeholder(tf.float32) image_pre = my_resnet_preprocessing.preprocess_image( X, 224, 224, False) image_pre = tf.reshape(image_pre, shape=[-1, 224, 224, 3]) image_pre = tf.to_float(image_pre) logits = resNetClassifier.my_cnn(image_pre, is_training=False, dropout_rate=1.0, layer=None) with tf.Session() as sess: coord = tf.train.Coordinator() saver = tf.train.Saver() saver.restore(sess, model_directory) threads = tf.train.start_queue_runners(sess=sess, coord=coord) dataset_dir = "optimal_stimuli" for filename in os.listdir(dataset_dir): if filename.endswith(".jpg"): filepath = ("%s/%s" % (dataset_dir, filename)) im = my_functions.get_img(filepath, ).astype(np.float32) layer_act = sess.run([logits], feed_dict={X: im}) layer_act = my_functions.numpy_softmax(layer_act) label, _, _, _, _, _ = plantclef_download_and_convert_data.get_class_name_from_xml( "%s.xml" % (str.split(filepath, ".jpg")[0])) label = label_dict[label] if np.argmax(layer_act) == label: best_act.setdefault(label, [-1, "test.jpg"]) if best_act[label][0] < np.amax(layer_act): best_act[label] = [np.amax(layer_act), filename] dataset_utils.write_label_file(best_act, dict_directory, filename=dict_name) return best_act